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
37 /* this routine assumes (N % M) == 0 */
38 int i, j, k, f, f0, n, np, nb, sxf, syf, sxb, syb;
42 unsigned char *neworg[3], *newref[3];
43 static char ipb[5] = {' ','I','P','B','D'};
45 rc_init_seq(); /* initialize rate control */
47 /* sequence header, sequence extension and sequence display extension */
55 /* optionally output some text data (description, copyright or whatever) */
56 if (strlen(id_string) > 1)
57 putuserdata(id_string);
59 /* loop through all frames in encoding/decoding order */
60 for (i=0; i<nframes; i++)
64 fprintf(stderr,"Encoding frame %d ",i);
68 /* f0: lowest frame number in current GOP
70 * first GOP contains N-(M-1) frames,
71 * all other GOPs contain N frames
73 f0 = N*((i+(M-1))/N) - (M-1);
78 if (i==0 || (i-1)%M==0)
83 /* shuffle reference frames */
84 neworg[j] = oldorgframe[j];
85 newref[j] = oldrefframe[j];
86 oldorgframe[j] = neworgframe[j];
87 oldrefframe[j] = newrefframe[j];
88 neworgframe[j] = neworg[j];
89 newrefframe[j] = newref[j];
92 /* f: frame number in display order */
93 f = (i==0) ? 0 : i+M-1;
97 if (i==f0) /* first displayed frame in GOP is I */
101 forw_hor_f_code = forw_vert_f_code = 15;
102 back_hor_f_code = back_vert_f_code = 15;
104 /* n: number of frames in current GOP
106 * first GOP contains (M-1) less (B) frames
108 n = (i==0) ? N-(M-1) : N;
110 /* last GOP may contain less frames */
114 /* number of P frames */
116 np = (n + 2*(M-1))/M - 1; /* first GOP */
118 np = (n + (M-1))/M - 1;
120 /* number of B frames */
125 putgophdr(f0,i==0); /* set closed_GOP in first GOP only */
131 forw_hor_f_code = motion_data[0].forw_hor_f_code;
132 forw_vert_f_code = motion_data[0].forw_vert_f_code;
133 back_hor_f_code = back_vert_f_code = 15;
134 sxf = motion_data[0].sxf;
135 syf = motion_data[0].syf;
143 neworg[j] = auxorgframe[j];
144 newref[j] = auxframe[j];
147 /* f: frame number in display order */
150 n = (i-2)%M + 1; /* first B: n=1, second B: n=2, ... */
151 forw_hor_f_code = motion_data[n].forw_hor_f_code;
152 forw_vert_f_code = motion_data[n].forw_vert_f_code;
153 back_hor_f_code = motion_data[n].back_hor_f_code;
154 back_vert_f_code = motion_data[n].back_vert_f_code;
155 sxf = motion_data[n].sxf;
156 syf = motion_data[n].syf;
157 sxb = motion_data[n].sxb;
158 syb = motion_data[n].syb;
162 frame_pred_dct = frame_pred_dct_tab[pict_type-1];
163 q_scale_type = qscale_tab[pict_type-1];
164 intravlc = intravlc_tab[pict_type-1];
165 altscan = altscan_tab[pict_type-1];
167 fprintf(statfile,"\nFrame %d (#%d in display order):\n",i,f);
168 fprintf(statfile," picture_type=%c\n",ipb[pict_type]);
169 fprintf(statfile," temporal_reference=%d\n",temp_ref);
170 fprintf(statfile," frame_pred_frame_dct=%d\n",frame_pred_dct);
171 fprintf(statfile," q_scale_type=%d\n",q_scale_type);
172 fprintf(statfile," intra_vlc_format=%d\n",intravlc);
173 fprintf(statfile," alternate_scan=%d\n",altscan);
175 if (pict_type!=I_TYPE)
177 fprintf(statfile," forward search window: %d...%d / %d...%d\n",
179 fprintf(statfile," forward vector range: %d...%d.5 / %d...%d.5\n",
180 -(4<<forw_hor_f_code),(4<<forw_hor_f_code)-1,
181 -(4<<forw_vert_f_code),(4<<forw_vert_f_code)-1);
184 if (pict_type==B_TYPE)
186 fprintf(statfile," backward search window: %d...%d / %d...%d\n",
188 fprintf(statfile," backward vector range: %d...%d.5 / %d...%d.5\n",
189 -(4<<back_hor_f_code),(4<<back_hor_f_code)-1,
190 -(4<<back_vert_f_code),(4<<back_vert_f_code)-1);
193 sprintf(name,tplorg,f+frame0);
194 readframe(name,neworg);
200 fprintf(stderr,"\nfirst field (%s) ",topfirst ? "top" : "bot");
204 pict_struct = topfirst ? TOP_FIELD : BOTTOM_FIELD;
206 motion_estimation(oldorgframe[0],neworgframe[0],
207 oldrefframe[0],newrefframe[0],
209 sxf,syf,sxb,syb,mbinfo,0,0);
211 predict(oldrefframe,newrefframe,predframe,0,mbinfo);
212 dct_type_estimation(predframe[0],neworg[0],mbinfo);
213 transform(predframe,neworg,mbinfo,blocks);
217 for (k=0; k<mb_height2*mb_width; k++)
219 if (mbinfo[k].mb_type & MB_INTRA)
220 for (j=0; j<block_count; j++)
221 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
222 dc_prec,intra_q,mbinfo[k].mquant);
224 for (j=0;j<block_count;j++)
225 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
226 inter_q,mbinfo[k].mquant);
229 itransform(predframe,newref,mbinfo,blocks);
230 calcSNR(neworg,newref);
235 fprintf(stderr,"second field (%s) ",topfirst ? "bot" : "top");
239 pict_struct = topfirst ? BOTTOM_FIELD : TOP_FIELD;
241 ipflag = (pict_type==I_TYPE);
244 /* first field = I, second field = P */
246 forw_hor_f_code = motion_data[0].forw_hor_f_code;
247 forw_vert_f_code = motion_data[0].forw_vert_f_code;
248 back_hor_f_code = back_vert_f_code = 15;
249 sxf = motion_data[0].sxf;
250 syf = motion_data[0].syf;
253 motion_estimation(oldorgframe[0],neworgframe[0],
254 oldrefframe[0],newrefframe[0],
256 sxf,syf,sxb,syb,mbinfo,1,ipflag);
258 predict(oldrefframe,newrefframe,predframe,1,mbinfo);
259 dct_type_estimation(predframe[0],neworg[0],mbinfo);
260 transform(predframe,neworg,mbinfo,blocks);
264 for (k=0; k<mb_height2*mb_width; k++)
266 if (mbinfo[k].mb_type & MB_INTRA)
267 for (j=0; j<block_count; j++)
268 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
269 dc_prec,intra_q,mbinfo[k].mquant);
271 for (j=0;j<block_count;j++)
272 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
273 inter_q,mbinfo[k].mquant);
276 itransform(predframe,newref,mbinfo,blocks);
277 calcSNR(neworg,newref);
282 pict_struct = FRAME_PICTURE;
284 /* do motion_estimation
286 * uses source frames (...orgframe) for full pel search
287 * and reconstructed frames (...refframe) for half pel search
290 motion_estimation(oldorgframe[0],neworgframe[0],
291 oldrefframe[0],newrefframe[0],
293 sxf,syf,sxb,syb,mbinfo,0,0);
295 predict(oldrefframe,newrefframe,predframe,0,mbinfo);
296 dct_type_estimation(predframe[0],neworg[0],mbinfo);
297 transform(predframe,neworg,mbinfo,blocks);
301 for (k=0; k<mb_height*mb_width; k++)
303 if (mbinfo[k].mb_type & MB_INTRA)
304 for (j=0; j<block_count; j++)
305 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
306 dc_prec,intra_q,mbinfo[k].mquant);
308 for (j=0;j<block_count;j++)
309 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
310 inter_q,mbinfo[k].mquant);
313 itransform(predframe,newref,mbinfo,blocks);
314 calcSNR(neworg,newref);
318 sprintf(name,tplref,f+frame0);
319 writeframe(name,newref);