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;
41 unsigned char *neworg[3], *newref[3];
42 static char ipb[5] = {' ','I','P','B','D'};
44 rc_init_seq(); /* initialize rate control */
46 /* sequence header, sequence extension and sequence display extension */
54 /* optionally output some text data (description, copyright or whatever) */
55 if (strlen(id_string) > 1)
56 putuserdata(id_string);
58 /* loop through all frames in encoding/decoding order */
59 for (i=0; i<nframes; i++)
63 fprintf(stderr,"Encoding frame %d ",i);
67 /* f0: lowest frame number in current GOP
69 * first GOP contains N-(M-1) frames,
70 * all other GOPs contain N frames
72 f0 = N*((i+(M-1))/N) - (M-1);
77 if (i==0 || (i-1)%M==0)
82 /* shuffle reference frames */
83 neworg[j] = oldorgframe[j];
84 newref[j] = oldrefframe[j];
85 oldorgframe[j] = neworgframe[j];
86 oldrefframe[j] = newrefframe[j];
87 neworgframe[j] = neworg[j];
88 newrefframe[j] = newref[j];
91 /* f: frame number in display order */
92 f = (i==0) ? 0 : i+M-1;
96 if (i==f0) /* first displayed frame in GOP is I */
100 forw_hor_f_code = forw_vert_f_code = 15;
101 back_hor_f_code = back_vert_f_code = 15;
103 /* n: number of frames in current GOP
105 * first GOP contains (M-1) less (B) frames
107 n = (i==0) ? N-(M-1) : N;
109 /* last GOP may contain less frames */
113 /* number of P frames */
115 np = (n + 2*(M-1))/M - 1; /* first GOP */
117 np = (n + (M-1))/M - 1;
119 /* number of B frames */
124 putgophdr(f0,i==0); /* set closed_GOP in first GOP only */
130 forw_hor_f_code = motion_data[0].forw_hor_f_code;
131 forw_vert_f_code = motion_data[0].forw_vert_f_code;
132 back_hor_f_code = back_vert_f_code = 15;
133 sxf = motion_data[0].sxf;
134 syf = motion_data[0].syf;
142 neworg[j] = auxorgframe[j];
143 newref[j] = auxframe[j];
146 /* f: frame number in display order */
149 n = (i-2)%M + 1; /* first B: n=1, second B: n=2, ... */
150 forw_hor_f_code = motion_data[n].forw_hor_f_code;
151 forw_vert_f_code = motion_data[n].forw_vert_f_code;
152 back_hor_f_code = motion_data[n].back_hor_f_code;
153 back_vert_f_code = motion_data[n].back_vert_f_code;
154 sxf = motion_data[n].sxf;
155 syf = motion_data[n].syf;
156 sxb = motion_data[n].sxb;
157 syb = motion_data[n].syb;
161 frame_pred_dct = frame_pred_dct_tab[pict_type-1];
162 q_scale_type = qscale_tab[pict_type-1];
163 intravlc = intravlc_tab[pict_type-1];
164 altscan = altscan_tab[pict_type-1];
166 fprintf(statfile,"\nFrame %d (#%d in display order):\n",i,f);
167 fprintf(statfile," picture_type=%c\n",ipb[pict_type]);
168 fprintf(statfile," temporal_reference=%d\n",temp_ref);
169 fprintf(statfile," frame_pred_frame_dct=%d\n",frame_pred_dct);
170 fprintf(statfile," q_scale_type=%d\n",q_scale_type);
171 fprintf(statfile," intra_vlc_format=%d\n",intravlc);
172 fprintf(statfile," alternate_scan=%d\n",altscan);
174 if (pict_type!=I_TYPE)
176 fprintf(statfile," forward search window: %d...%d / %d...%d\n",
178 fprintf(statfile," forward vector range: %d...%d.5 / %d...%d.5\n",
179 -(4<<forw_hor_f_code),(4<<forw_hor_f_code)-1,
180 -(4<<forw_vert_f_code),(4<<forw_vert_f_code)-1);
183 if (pict_type==B_TYPE)
185 fprintf(statfile," backward search window: %d...%d / %d...%d\n",
187 fprintf(statfile," backward vector range: %d...%d.5 / %d...%d.5\n",
188 -(4<<back_hor_f_code),(4<<back_hor_f_code)-1,
189 -(4<<back_vert_f_code),(4<<back_vert_f_code)-1);
192 sprintf(name,tplorg,f+frame0);
193 readframe(name,neworg);
199 fprintf(stderr,"\nfirst field (%s) ",topfirst ? "top" : "bot");
203 pict_struct = topfirst ? TOP_FIELD : BOTTOM_FIELD;
205 motion_estimation(oldorgframe[0],neworgframe[0],
206 oldrefframe[0],newrefframe[0],
208 sxf,syf,sxb,syb,mbinfo,0,0);
210 predict(oldrefframe,newrefframe,predframe,0,mbinfo);
211 dct_type_estimation(predframe[0],neworg[0],mbinfo);
212 transform(predframe,neworg,mbinfo,blocks);
216 for (k=0; k<mb_height2*mb_width; k++)
218 if (mbinfo[k].mb_type & MB_INTRA)
219 for (j=0; j<block_count; j++)
220 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
221 dc_prec,intra_q,mbinfo[k].mquant);
223 for (j=0;j<block_count;j++)
224 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
225 inter_q,mbinfo[k].mquant);
228 itransform(predframe,newref,mbinfo,blocks);
229 calcSNR(neworg,newref);
234 fprintf(stderr,"second field (%s) ",topfirst ? "bot" : "top");
238 pict_struct = topfirst ? BOTTOM_FIELD : TOP_FIELD;
240 ipflag = (pict_type==I_TYPE);
243 /* first field = I, second field = P */
245 forw_hor_f_code = motion_data[0].forw_hor_f_code;
246 forw_vert_f_code = motion_data[0].forw_vert_f_code;
247 back_hor_f_code = back_vert_f_code = 15;
248 sxf = motion_data[0].sxf;
249 syf = motion_data[0].syf;
252 motion_estimation(oldorgframe[0],neworgframe[0],
253 oldrefframe[0],newrefframe[0],
255 sxf,syf,sxb,syb,mbinfo,1,ipflag);
257 predict(oldrefframe,newrefframe,predframe,1,mbinfo);
258 dct_type_estimation(predframe[0],neworg[0],mbinfo);
259 transform(predframe,neworg,mbinfo,blocks);
263 for (k=0; k<mb_height2*mb_width; k++)
265 if (mbinfo[k].mb_type & MB_INTRA)
266 for (j=0; j<block_count; j++)
267 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
268 dc_prec,intra_q,mbinfo[k].mquant);
270 for (j=0;j<block_count;j++)
271 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
272 inter_q,mbinfo[k].mquant);
275 itransform(predframe,newref,mbinfo,blocks);
276 calcSNR(neworg,newref);
281 pict_struct = FRAME_PICTURE;
283 /* do motion_estimation
285 * uses source frames (...orgframe) for full pel search
286 * and reconstructed frames (...refframe) for half pel search
289 motion_estimation(oldorgframe[0],neworgframe[0],
290 oldrefframe[0],newrefframe[0],
292 sxf,syf,sxb,syb,mbinfo,0,0);
294 predict(oldrefframe,newrefframe,predframe,0,mbinfo);
295 dct_type_estimation(predframe[0],neworg[0],mbinfo);
296 transform(predframe,neworg,mbinfo,blocks);
300 for (k=0; k<mb_height*mb_width; k++)
302 if (mbinfo[k].mb_type & MB_INTRA)
303 for (j=0; j<block_count; j++)
304 iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
305 dc_prec,intra_q,mbinfo[k].mquant);
307 for (j=0;j<block_count;j++)
308 iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
309 inter_q,mbinfo[k].mquant);
312 itransform(predframe,newref,mbinfo,blocks);
313 calcSNR(neworg,newref);
317 sprintf(name,tplref,f+frame0);
318 writeframe(name,newref);