1
/* putseq.c, sequence level routines */
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6
* Disclaimer of Warranty
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.
16
* This disclaimer of warranty extends to the user of these programs and user's
17
* customers, employees, agents, transferees, successors, and assigns.
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
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
32
#include "mpeg2enc_config.h"
33
#include "mpeg2enc_global.h"
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)
39
/* f0: lowest frame number in current GOP
41
* first GOP contains N-(M-1) frames,
42
* all other GOPs contain N frames
44
*f0 = mpeg2_struct->N_val*((cframe+(mpeg2_struct->M_val-1))/mpeg2_struct->N_val) - (mpeg2_struct->M_val-1);
49
if (cframe==0 || (cframe-1)%mpeg2_struct->M_val==0)
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];
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;
68
if (cframe==*f0) /* first displayed frame in GOP is I */
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;
75
/* n: number of frames in current GOP
77
* first GOP contains (M-1) less (B) frames
79
n = (cframe==0) ? mpeg2_struct->N_val-(mpeg2_struct->M_val-1) : mpeg2_struct->N_val;
81
/* last GOP may contain less frames */
82
if (n > mpeg2_struct->nframes-*f0)
83
n = mpeg2_struct->nframes-*f0;
85
/* number of P frames */
87
np = (n + 2*(mpeg2_struct->M_val-1))/mpeg2_struct->M_val - 1; /* first GOP */
89
np = (n + (mpeg2_struct->M_val-1))/mpeg2_struct->M_val - 1;
91
/* number of B frames */
94
MPEG2_rc_init_GOP(np,nb,mpeg2_struct);
96
MPEG2_putgophdr(*f0,cframe==0,mpeg2_struct); /* set closed_GOP in first GOP only */
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;
114
neworg[j] = mpeg2_struct->auxorgframe[j];
115
newref[j] = mpeg2_struct->auxframe[j];
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;
133
VTK_MPEG2ENC_EXPORT int MPEG2_putseq_one(int cframe, int max,struct MPEG2_structure *mpeg2_struct)
135
int j, k, realframe, f0, sxf, syf, sxb, syb;
138
unsigned char *neworg[3], *newref[3];
139
static char ipb[5] = {' ','I','P','B','D'};
140
if (!mpeg2_struct->quiet)
142
fprintf(stderr,"Encoding frame %d ",cframe);
146
MPEG2_evaluate_frame_info(cframe, &f0, &realframe, neworg, newref,
147
&sxf, &syf, &sxb, &syb,mpeg2_struct);
148
if ( realframe > max )
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];
159
if ( mpeg2_struct->statfile )
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);
169
if (mpeg2_struct->pict_type!=I_TYPE)
171
fprintf(mpeg2_struct->statfile," forward search window: %d...%d / %d...%d\n",
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);
178
if (mpeg2_struct->pict_type==B_TYPE)
180
fprintf(mpeg2_struct->statfile," backward search window: %d...%d / %d...%d\n",
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);
188
sprintf(name,mpeg2_struct->tplorg,realframe+mpeg2_struct->frame0);
189
MPEG2_readframe(name,neworg,mpeg2_struct);
191
if (mpeg2_struct->fieldpic)
193
if (!mpeg2_struct->quiet)
195
fprintf(stderr,"\nfirst field (%s) ",mpeg2_struct->topfirst ? "top" : "bot");
199
mpeg2_struct->pict_struct = mpeg2_struct->topfirst ? TOP_FIELD : BOTTOM_FIELD;
201
MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
202
mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
204
sxf,syf,sxb,syb,mpeg2_struct->mbinfo,0,0,mpeg2_struct);
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);
210
MPEG2_putpict(neworg[0],mpeg2_struct);
212
for (k=0; k<mpeg2_struct->mb_height2*mpeg2_struct->mb_width; k++)
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);
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);
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);
228
if (!mpeg2_struct->quiet)
230
fprintf(stderr,"second field (%s) ",mpeg2_struct->topfirst ? "bot" : "top");
234
mpeg2_struct->pict_struct = mpeg2_struct->topfirst ? BOTTOM_FIELD : TOP_FIELD;
236
ipflag = (mpeg2_struct->pict_type==I_TYPE);
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;
248
MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
249
mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
251
sxf,syf,sxb,syb,mpeg2_struct->mbinfo,1,ipflag,mpeg2_struct);
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);
257
MPEG2_putpict(neworg[0],mpeg2_struct);
259
for (k=0; k<mpeg2_struct->mb_height2*mpeg2_struct->mb_width; k++)
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);
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);
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);
277
mpeg2_struct->pict_struct = FRAME_PICTURE;
279
/* do MPEG2_motion_estimation
281
* uses source frames (...orgframe) for full pel search
282
* and reconstructed frames (...refframe) for half pel search
285
MPEG2_motion_estimation(mpeg2_struct->oldorgframe[0],mpeg2_struct->neworgframe[0],
286
mpeg2_struct->oldrefframe[0],mpeg2_struct->newrefframe[0],
288
sxf,syf,sxb,syb,mpeg2_struct->mbinfo,0,0,mpeg2_struct);
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);
294
MPEG2_putpict(neworg[0],mpeg2_struct);
296
for (k=0; k<mpeg2_struct->mb_height*mpeg2_struct->mb_width; k++)
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);
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);
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);
313
sprintf(name,mpeg2_struct->tplref,realframe+mpeg2_struct->frame0);
314
MPEG2_writeframe(name,newref,mpeg2_struct);