~ubuntu-branches/ubuntu/gutsy/vtk/gutsy

« back to all changes in this revision

Viewing changes to Utilities/vtkmpeg2encode/putseq.c

  • Committer: Bazaar Package Importer
  • Author(s): Michele Angrisano
  • Date: 2007-06-30 22:39:48 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070630223948-6tn51upaurwfrcz8
Tags: 5.0.3-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add export SHELL=/bin/bash in debian/rules.
  - Update maintainer in field debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* putseq.c, sequence level routines                                        */
2
 
 
3
 
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
 
5
 
/*
6
 
 * Disclaimer of Warranty
7
 
 *
8
 
 * These software programs are available to the user without any license fee or
9
 
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10
 
 * any and all warranties, whether express, implied, or statuary, including any
11
 
 * implied warranties or merchantability or of fitness for a particular
12
 
 * purpose.  In no event shall the copyright-holder be liable for any
13
 
 * incidental, punitive, or consequential damages of any kind whatsoever
14
 
 * arising from the use of these programs.
15
 
 *
16
 
 * This disclaimer of warranty extends to the user of these programs and user's
17
 
 * customers, employees, agents, transferees, successors, and assigns.
18
 
 *
19
 
 * The MPEG Software Simulation Group does not represent or warrant that the
20
 
 * programs furnished hereunder are free of infringement of any third-party
21
 
 * patents.
22
 
 *
23
 
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
 
 * are subject to royalty fees to patent holders.  Many of these patents are
25
 
 * general enough such that they are unavoidable regardless of implementation
26
 
 * design.
27
 
 *
28
 
 */
29
 
 
30
 
#include <stdio.h>
31
 
#include <string.h>
32
 
#include "mpeg2enc_config.h"
33
 
#include "mpeg2enc_global.h"
34
 
 
35
 
static void MPEG2_evaluate_frame_info(int cframe, int* f0, int* realframe, unsigned char* neworg[], unsigned char* newref[], int* sxf, int* syf, int* sxb, int* syb,struct MPEG2_structure *mpeg2_struct)
36
 
{
37
 
  int j, n, np, nb;
38
 
 
39
 
  /* f0: lowest frame number in current GOP
40
 
   *
41
 
   * first GOP contains N-(M-1) frames,
42
 
   * all other GOPs contain N frames
43
 
   */
44
 
  *f0 = mpeg2_struct->N_val*((cframe+(mpeg2_struct->M_val-1))/mpeg2_struct->N_val) - (mpeg2_struct->M_val-1);
45
 
 
46
 
  if (*f0<0)
47
 
    *f0=0;
48
 
 
49
 
  if (cframe==0 || (cframe-1)%mpeg2_struct->M_val==0)
50
 
    {
51
 
    /* I or P frame */
52
 
    for (j=0; j<3; j++)
53
 
      {
54
 
      /* shuffle reference frames */
55
 
      neworg[j] = mpeg2_struct->oldorgframe[j];
56
 
      newref[j] = mpeg2_struct->oldrefframe[j];
57
 
      mpeg2_struct->oldorgframe[j] = mpeg2_struct->neworgframe[j];
58
 
      mpeg2_struct->oldrefframe[j] = mpeg2_struct->newrefframe[j];
59
 
      mpeg2_struct->neworgframe[j] = neworg[j];
60
 
      mpeg2_struct->newrefframe[j] = newref[j];
61
 
      }
62
 
 
63
 
    /* f: frame number in display order */
64
 
    *realframe = (cframe==0) ? 0 : cframe+mpeg2_struct->M_val-1;
65
 
    if (*realframe>=mpeg2_struct->nframes)
66
 
      *realframe = mpeg2_struct->nframes - 1;
67
 
 
68
 
    if (cframe==*f0) /* first displayed frame in GOP is I */
69
 
      {
70
 
      /* I frame */
71
 
      mpeg2_struct->pict_type = I_TYPE;
72
 
      mpeg2_struct->forw_hor_f_code = mpeg2_struct->forw_vert_f_code = 15;
73
 
      mpeg2_struct->back_hor_f_code = mpeg2_struct->back_vert_f_code = 15;
74
 
 
75
 
      /* n: number of frames in current GOP
76
 
       *
77
 
       * first GOP contains (M-1) less (B) frames
78
 
       */
79
 
      n = (cframe==0) ? mpeg2_struct->N_val-(mpeg2_struct->M_val-1) : mpeg2_struct->N_val;
80
 
 
81
 
      /* last GOP may contain less frames */
82
 
      if (n > mpeg2_struct->nframes-*f0)
83
 
        n = mpeg2_struct->nframes-*f0;
84
 
 
85
 
      /* number of P frames */
86
 
      if (cframe==0)
87
 
        np = (n + 2*(mpeg2_struct->M_val-1))/mpeg2_struct->M_val - 1; /* first GOP */
88
 
      else
89
 
        np = (n + (mpeg2_struct->M_val-1))/mpeg2_struct->M_val - 1;
90
 
 
91
 
      /* number of B frames */
92
 
      nb = n - np - 1;
93
 
 
94
 
      MPEG2_rc_init_GOP(np,nb,mpeg2_struct);
95
 
 
96
 
      MPEG2_putgophdr(*f0,cframe==0,mpeg2_struct); /* set closed_GOP in first GOP only */
97
 
      }
98
 
    else
99
 
      {
100
 
      /* P frame */
101
 
      mpeg2_struct->pict_type = P_TYPE;
102
 
      mpeg2_struct->forw_hor_f_code = mpeg2_struct->motion_data[0].forw_hor_f_code;
103
 
      mpeg2_struct->forw_vert_f_code = mpeg2_struct->motion_data[0].forw_vert_f_code;
104
 
      mpeg2_struct->back_hor_f_code = mpeg2_struct->back_vert_f_code = 15;
105
 
      *sxf = mpeg2_struct->motion_data[0].sxf;
106
 
      *syf = mpeg2_struct->motion_data[0].syf;
107
 
      }
108
 
    }
109
 
  else
110
 
    {
111
 
    /* B frame */
112
 
    for (j=0; j<3; j++)
113
 
      {
114
 
      neworg[j] = mpeg2_struct->auxorgframe[j];
115
 
      newref[j] = mpeg2_struct->auxframe[j];
116
 
      }
117
 
 
118
 
    /* f: frame number in display order */
119
 
    *realframe = cframe - 1;
120
 
    mpeg2_struct->pict_type = B_TYPE;
121
 
    n = (cframe-2)%mpeg2_struct->M_val + 1; /* first B: n=1, second B: n=2, ... */
122
 
    mpeg2_struct->forw_hor_f_code = mpeg2_struct->motion_data[n].forw_hor_f_code;
123
 
    mpeg2_struct->forw_vert_f_code = mpeg2_struct->motion_data[n].forw_vert_f_code;
124
 
    mpeg2_struct->back_hor_f_code = mpeg2_struct->motion_data[n].back_hor_f_code;
125
 
    mpeg2_struct->back_vert_f_code = mpeg2_struct->motion_data[n].back_vert_f_code;
126
 
    *sxf = mpeg2_struct->motion_data[n].sxf;
127
 
    *syf = mpeg2_struct->motion_data[n].syf;
128
 
    *sxb = mpeg2_struct->motion_data[n].sxb;
129
 
    *syb = mpeg2_struct->motion_data[n].syb;
130
 
    }
131
 
}
132
 
 
133
 
VTK_MPEG2ENC_EXPORT int MPEG2_putseq_one(int cframe, int max,struct MPEG2_structure *mpeg2_struct)
134
 
{
135
 
  int j, k, realframe, f0, sxf, syf, sxb, syb;
136
 
  int ipflag;
137
 
  char name[256];
138
 
  unsigned char *neworg[3], *newref[3];
139
 
  static char ipb[5] = {' ','I','P','B','D'};
140
 
  if (!mpeg2_struct->quiet)
141
 
    {
142
 
    fprintf(stderr,"Encoding frame %d ",cframe);
143
 
    fflush(stderr);
144
 
    }
145
 
 
146
 
  MPEG2_evaluate_frame_info(cframe, &f0, &realframe, neworg, newref,
147
 
    &sxf, &syf, &sxb, &syb,mpeg2_struct);
148
 
  if ( realframe > max )
149
 
    {
150
 
    return -1;
151
 
    }
152
 
 
153
 
  mpeg2_struct->temp_ref = realframe - f0;
154
 
  mpeg2_struct->frame_pred_dct = mpeg2_struct->frame_pred_dct_tab[mpeg2_struct->pict_type-1];
155
 
  mpeg2_struct->q_scale_type = mpeg2_struct->qscale_tab[mpeg2_struct->pict_type-1];
156
 
  mpeg2_struct->intravlc = mpeg2_struct->intravlc_tab[mpeg2_struct->pict_type-1];
157
 
  mpeg2_struct->altscan = mpeg2_struct->altscan_tab[mpeg2_struct->pict_type-1];
158
 
 
159
 
  if ( mpeg2_struct->statfile )
160
 
    {
161
 
    fprintf(mpeg2_struct->statfile,"\nFrame %d (#%d in display order):\n",cframe,realframe);
162
 
    fprintf(mpeg2_struct->statfile," picture_type=%c\n",ipb[mpeg2_struct->pict_type]);
163
 
    fprintf(mpeg2_struct->statfile," temporal_reference=%d\n",mpeg2_struct->temp_ref);
164
 
    fprintf(mpeg2_struct->statfile," frame_pred_frame_dct=%d\n",mpeg2_struct->frame_pred_dct);
165
 
    fprintf(mpeg2_struct->statfile," q_scale_type=%d\n",mpeg2_struct->q_scale_type);
166
 
    fprintf(mpeg2_struct->statfile," intra_vlc_format=%d\n",mpeg2_struct->intravlc);
167
 
    fprintf(mpeg2_struct->statfile," alternate_scan=%d\n",mpeg2_struct->altscan);
168
 
 
169
 
    if (mpeg2_struct->pict_type!=I_TYPE)
170
 
      {
171
 
      fprintf(mpeg2_struct->statfile," forward search window: %d...%d / %d...%d\n",
172
 
        -sxf,sxf,-syf,syf);
173
 
      fprintf(mpeg2_struct->statfile," forward vector range: %d...%d.5 / %d...%d.5\n",
174
 
        -(4<<mpeg2_struct->forw_hor_f_code),(4<<mpeg2_struct->forw_hor_f_code)-1,
175
 
        -(4<<mpeg2_struct->forw_vert_f_code),(4<<mpeg2_struct->forw_vert_f_code)-1);
176
 
      }
177
 
 
178
 
    if (mpeg2_struct->pict_type==B_TYPE)
179
 
      {
180
 
      fprintf(mpeg2_struct->statfile," backward search window: %d...%d / %d...%d\n",
181
 
        -sxb,sxb,-syb,syb);
182
 
      fprintf(mpeg2_struct->statfile," backward vector range: %d...%d.5 / %d...%d.5\n",
183
 
        -(4<<mpeg2_struct->back_hor_f_code),(4<<mpeg2_struct->back_hor_f_code)-1,
184
 
        -(4<<mpeg2_struct->back_vert_f_code),(4<<mpeg2_struct->back_vert_f_code)-1);
185
 
      }
186
 
    }
187
 
 
188
 
  sprintf(name,mpeg2_struct->tplorg,realframe+mpeg2_struct->frame0);
189
 
  MPEG2_readframe(name,neworg,mpeg2_struct);
190
 
 
191
 
  if (mpeg2_struct->fieldpic)
192
 
    {
193
 
    if (!mpeg2_struct->quiet)
194
 
      {
195
 
      fprintf(stderr,"\nfirst field  (%s) ",mpeg2_struct->topfirst ? "top" : "bot");
196
 
      fflush(stderr);
197
 
      }
198
 
 
199
 
    mpeg2_struct->pict_struct = mpeg2_struct->topfirst ? TOP_FIELD : BOTTOM_FIELD;
200
 
 
201
 
    MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
202
 
      mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
203
 
      neworg[0],newref[0],
204
 
      sxf,syf,sxb,syb,mpeg2_struct->mbinfo,0,0,mpeg2_struct);
205
 
 
206
 
    MPEG2_predict(mpeg2_struct->oldrefframe,mpeg2_struct->newrefframe,mpeg2_struct->predframe,0,mpeg2_struct->mbinfo,mpeg2_struct);
207
 
    MPEG2_dct_type_estimation(mpeg2_struct->predframe[0],neworg[0],mpeg2_struct->mbinfo,mpeg2_struct);
208
 
    MPEG2_transform(mpeg2_struct->predframe,neworg,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
209
 
 
210
 
    MPEG2_putpict(neworg[0],mpeg2_struct);
211
 
 
212
 
    for (k=0; k<mpeg2_struct->mb_height2*mpeg2_struct->mb_width; k++)
213
 
      {
214
 
      if (mpeg2_struct->mbinfo[k].mb_type & MB_INTRA)
215
 
        for (j=0; j<mpeg2_struct->block_count; j++)
216
 
          MPEG2_iquant_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
217
 
            mpeg2_struct->dc_prec,mpeg2_struct->intra_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
218
 
      else
219
 
        for (j=0;j<mpeg2_struct->block_count;j++)
220
 
          MPEG2_iquant_non_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
221
 
            mpeg2_struct->inter_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
222
 
      }
223
 
 
224
 
    MPEG2_itransform(mpeg2_struct->predframe,newref,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
225
 
    MPEG2_calcSNR(neworg,newref,mpeg2_struct);
226
 
    MPEG2_stats(mpeg2_struct);
227
 
 
228
 
    if (!mpeg2_struct->quiet)
229
 
      {
230
 
      fprintf(stderr,"second field (%s) ",mpeg2_struct->topfirst ? "bot" : "top");
231
 
      fflush(stderr);
232
 
      }
233
 
 
234
 
    mpeg2_struct->pict_struct = mpeg2_struct->topfirst ? BOTTOM_FIELD : TOP_FIELD;
235
 
 
236
 
    ipflag = (mpeg2_struct->pict_type==I_TYPE);
237
 
    if (ipflag)
238
 
      {
239
 
      /* first field = I, second field = P */
240
 
      mpeg2_struct->pict_type = P_TYPE;
241
 
      mpeg2_struct->forw_hor_f_code = mpeg2_struct->motion_data[0].forw_hor_f_code;
242
 
      mpeg2_struct->forw_vert_f_code = mpeg2_struct->motion_data[0].forw_vert_f_code;
243
 
      mpeg2_struct->back_hor_f_code = mpeg2_struct->back_vert_f_code = 15;
244
 
      sxf = mpeg2_struct->motion_data[0].sxf;
245
 
      syf = mpeg2_struct->motion_data[0].syf;
246
 
      }
247
 
 
248
 
    MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
249
 
      mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
250
 
      neworg[0],newref[0],
251
 
      sxf,syf,sxb,syb,mpeg2_struct->mbinfo,1,ipflag,mpeg2_struct);
252
 
 
253
 
    MPEG2_predict(mpeg2_struct->oldrefframe,mpeg2_struct->newrefframe,mpeg2_struct->predframe,1,mpeg2_struct->mbinfo,mpeg2_struct);
254
 
    MPEG2_dct_type_estimation(mpeg2_struct->predframe[0],neworg[0],mpeg2_struct->mbinfo,mpeg2_struct);
255
 
    MPEG2_transform(mpeg2_struct->predframe,neworg,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
256
 
 
257
 
    MPEG2_putpict(neworg[0],mpeg2_struct);
258
 
 
259
 
    for (k=0; k<mpeg2_struct->mb_height2*mpeg2_struct->mb_width; k++)
260
 
      {
261
 
      if (mpeg2_struct->mbinfo[k].mb_type & MB_INTRA)
262
 
        for (j=0; j<mpeg2_struct->block_count; j++)
263
 
          MPEG2_iquant_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
264
 
            mpeg2_struct->dc_prec,mpeg2_struct->intra_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
265
 
      else
266
 
        for (j=0;j<mpeg2_struct->block_count;j++)
267
 
          MPEG2_iquant_non_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
268
 
            mpeg2_struct->inter_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
269
 
      }
270
 
 
271
 
    MPEG2_itransform(mpeg2_struct->predframe,newref,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
272
 
    MPEG2_calcSNR(neworg,newref,mpeg2_struct);
273
 
    MPEG2_stats(mpeg2_struct);
274
 
    }
275
 
  else
276
 
    {
277
 
    mpeg2_struct->pict_struct = FRAME_PICTURE;
278
 
 
279
 
    /* do MPEG2_motion_estimation
280
 
     *
281
 
     * uses source frames (...orgframe) for full pel search
282
 
     * and reconstructed frames (...refframe) for half pel search
283
 
     */
284
 
 
285
 
    MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
286
 
      mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
287
 
      neworg[0],newref[0],
288
 
      sxf,syf,sxb,syb,mpeg2_struct->mbinfo,0,0,mpeg2_struct);
289
 
 
290
 
    MPEG2_predict(mpeg2_struct->oldrefframe,mpeg2_struct->newrefframe,mpeg2_struct->predframe,0,mpeg2_struct->mbinfo,mpeg2_struct);
291
 
    MPEG2_dct_type_estimation(mpeg2_struct->predframe[0],neworg[0],mpeg2_struct->mbinfo,mpeg2_struct);
292
 
    MPEG2_transform(mpeg2_struct->predframe,neworg,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
293
 
 
294
 
    MPEG2_putpict(neworg[0],mpeg2_struct);
295
 
 
296
 
    for (k=0; k<mpeg2_struct->mb_height*mpeg2_struct->mb_width; k++)
297
 
      {
298
 
      if (mpeg2_struct->mbinfo[k].mb_type & MB_INTRA)
299
 
        for (j=0; j<mpeg2_struct->block_count; j++)
300
 
          MPEG2_iquant_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
301
 
            mpeg2_struct->dc_prec,mpeg2_struct->intra_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
302
 
      else
303
 
        for (j=0;j<mpeg2_struct->block_count;j++)
304
 
          MPEG2_iquant_non_intra(mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],mpeg2_struct->blocks[k*mpeg2_struct->block_count+j],
305
 
            mpeg2_struct->inter_q,mpeg2_struct->mbinfo[k].mquant,mpeg2_struct);
306
 
      }
307
 
 
308
 
    MPEG2_itransform(mpeg2_struct->predframe,newref,mpeg2_struct->mbinfo,mpeg2_struct->blocks,mpeg2_struct);
309
 
    MPEG2_calcSNR(neworg,newref,mpeg2_struct);
310
 
    MPEG2_stats(mpeg2_struct);
311
 
    }
312
 
 
313
 
  sprintf(name,mpeg2_struct->tplref,realframe+mpeg2_struct->frame0);
314
 
  MPEG2_writeframe(name,newref,mpeg2_struct);
315
 
  return realframe;
316
 
}