1 /* putpic.c, block and motion vector encoding 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
34 /* private prototypes */
35 static void putmvs _ANSI_ARGS_((int MV[2][2][2], int PMV[2][2][2],
36 int mv_field_sel[2][2], int dmvector[2], int s, int motion_type,
37 int hor_f_code, int vert_f_code));
39 /* quantization / variable length encoding of a complete picture */
43 int i, j, k, comp, cc;
49 rc_init_pict(frame); /* set up rate control */
51 /* picture header and picture coding extension */
57 prev_mquant = rc_start_mb(); /* initialize quantization parameter */
61 for (j=0; j<mb_height2; j++)
63 /* macroblock row loop */
65 for (i=0; i<mb_width; i++)
70 /* slice header (6.2.4) */
73 if (mpeg1 || vertical_size<=2800)
74 putbits(SLICE_MIN_START+j,32); /* slice_start_code */
77 putbits(SLICE_MIN_START+(j&127),32); /* slice_start_code */
78 putbits(j>>7,3); /* slice_vertical_position_extension */
81 /* quantiser_scale_code */
82 putbits(q_scale_type ? map_non_linear_mquant[prev_mquant]
83 : prev_mquant >> 1, 5);
85 putbits(0,1); /* extra_bit_slice */
87 /* reset predictors */
89 for (cc=0; cc<3; cc++)
92 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
93 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
95 MBAinc = i + 1; /* first MBAinc denotes absolute position */
98 mb_type = mbinfo[k].mb_type;
100 /* determine mquant (rate control) */
101 mbinfo[k].mquant = rc_calc_mquant(k);
103 /* quantize macroblock */
104 if (mb_type & MB_INTRA)
106 for (comp=0; comp<block_count; comp++)
107 quant_intra(blocks[k*block_count+comp],blocks[k*block_count+comp],
108 dc_prec,intra_q,mbinfo[k].mquant);
109 mbinfo[k].cbp = cbp = (1<<block_count) - 1;
114 for (comp=0;comp<block_count;comp++)
115 cbp = (cbp<<1) | quant_non_intra(blocks[k*block_count+comp],
116 blocks[k*block_count+comp],
117 inter_q,mbinfo[k].mquant);
122 mb_type|= MB_PATTERN;
125 /* output mquant if it has changed */
126 if (cbp && prev_mquant!=mbinfo[k].mquant)
129 /* check if macroblock can be skipped */
130 if (i!=0 && i!=mb_width-1 && !cbp)
132 /* no DCT coefficients and neither first nor last macroblock of slice */
134 if (pict_type==P_TYPE && !(mb_type&MB_FORWARD))
136 /* P picture, no motion vectors -> skip */
138 /* reset predictors */
140 for (cc=0; cc<3; cc++)
143 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
144 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
146 mbinfo[k].mb_type = mb_type;
147 mbinfo[k].skipped = 1;
153 if (pict_type==B_TYPE && pict_struct==FRAME_PICTURE
154 && mbinfo[k].motion_type==MC_FRAME
155 && ((mbinfo[k-1].mb_type^mb_type)&(MB_FORWARD|MB_BACKWARD))==0
156 && (!(mb_type&MB_FORWARD) ||
157 (PMV[0][0][0]==mbinfo[k].MV[0][0][0] &&
158 PMV[0][0][1]==mbinfo[k].MV[0][0][1]))
159 && (!(mb_type&MB_BACKWARD) ||
160 (PMV[0][1][0]==mbinfo[k].MV[0][1][0] &&
161 PMV[0][1][1]==mbinfo[k].MV[0][1][1])))
163 /* conditions for skipping in B frame pictures:
164 * - must be frame predicted
165 * - must be the same prediction type (forward/backward/interp.)
166 * as previous macroblock
167 * - relevant vectors (forward/backward/both) have to be the same
168 * as in previous macroblock
171 mbinfo[k].mb_type = mb_type;
172 mbinfo[k].skipped = 1;
178 if (pict_type==B_TYPE && pict_struct!=FRAME_PICTURE
179 && mbinfo[k].motion_type==MC_FIELD
180 && ((mbinfo[k-1].mb_type^mb_type)&(MB_FORWARD|MB_BACKWARD))==0
181 && (!(mb_type&MB_FORWARD) ||
182 (PMV[0][0][0]==mbinfo[k].MV[0][0][0] &&
183 PMV[0][0][1]==mbinfo[k].MV[0][0][1] &&
184 mbinfo[k].mv_field_sel[0][0]==(pict_struct==BOTTOM_FIELD)))
185 && (!(mb_type&MB_BACKWARD) ||
186 (PMV[0][1][0]==mbinfo[k].MV[0][1][0] &&
187 PMV[0][1][1]==mbinfo[k].MV[0][1][1] &&
188 mbinfo[k].mv_field_sel[0][1]==(pict_struct==BOTTOM_FIELD))))
190 /* conditions for skipping in B field pictures:
191 * - must be field predicted
192 * - must be the same prediction type (forward/backward/interp.)
193 * as previous macroblock
194 * - relevant vectors (forward/backward/both) have to be the same
195 * as in previous macroblock
196 * - relevant motion_vertical_field_selects have to be of same
197 * parity as current field
200 mbinfo[k].mb_type = mb_type;
201 mbinfo[k].skipped = 1;
208 /* macroblock cannot be skipped */
209 mbinfo[k].skipped = 0;
211 /* there's no VLC for 'No MC, Not Coded':
212 * we have to transmit (0,0) motion vectors
214 if (pict_type==P_TYPE && !cbp && !(mb_type&MB_FORWARD))
215 mb_type|= MB_FORWARD;
217 putaddrinc(MBAinc); /* macroblock_address_increment */
220 putmbtype(pict_type,mb_type); /* macroblock type */
222 if (mb_type & (MB_FORWARD|MB_BACKWARD) && !frame_pred_dct)
223 putbits(mbinfo[k].motion_type,2);
225 if (pict_struct==FRAME_PICTURE && cbp && !frame_pred_dct)
226 putbits(mbinfo[k].dct_type,1);
228 if (mb_type & MB_QUANT)
230 putbits(q_scale_type ? map_non_linear_mquant[mbinfo[k].mquant]
231 : mbinfo[k].mquant>>1,5);
232 prev_mquant = mbinfo[k].mquant;
235 if (mb_type & MB_FORWARD)
237 /* forward motion vectors, update predictors */
238 putmvs(mbinfo[k].MV,PMV,mbinfo[k].mv_field_sel,mbinfo[k].dmvector,0,
239 mbinfo[k].motion_type,forw_hor_f_code,forw_vert_f_code);
242 if (mb_type & MB_BACKWARD)
244 /* backward motion vectors, update predictors */
245 putmvs(mbinfo[k].MV,PMV,mbinfo[k].mv_field_sel,mbinfo[k].dmvector,1,
246 mbinfo[k].motion_type,back_hor_f_code,back_vert_f_code);
249 if (mb_type & MB_PATTERN)
251 putcbp((cbp >> (block_count-6)) & 63);
252 if (chroma_format!=CHROMA420)
253 putbits(cbp,block_count-6);
256 for (comp=0; comp<block_count; comp++)
259 if (cbp & (1<<(block_count-1-comp)))
261 if (mb_type & MB_INTRA)
263 cc = (comp<4) ? 0 : (comp&1)+1;
264 putintrablk(blocks[k*block_count+comp],cc);
267 putnonintrablk(blocks[k*block_count+comp]);
271 /* reset predictors */
272 if (!(mb_type & MB_INTRA))
273 for (cc=0; cc<3; cc++)
276 if (mb_type & MB_INTRA || (pict_type==P_TYPE && !(mb_type & MB_FORWARD)))
278 PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
279 PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
282 mbinfo[k].mb_type = mb_type;
288 vbv_end_of_picture();
292 /* output motion vectors (6.2.5.2, 6.3.16.2)
294 * this routine also updates the predictions for motion vectors (PMV)
297 static void putmvs(MV,PMV,mv_field_sel,dmvector,s,motion_type,
298 hor_f_code,vert_f_code)
299 int MV[2][2][2],PMV[2][2][2];
300 int mv_field_sel[2][2];
302 int s,motion_type,hor_f_code,vert_f_code;
304 if (pict_struct==FRAME_PICTURE)
306 if (motion_type==MC_FRAME)
308 /* frame prediction */
309 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
310 putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
311 PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
312 PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
314 else if (motion_type==MC_FIELD)
316 /* field prediction */
317 putbits(mv_field_sel[0][s],1);
318 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
319 putmv((MV[0][s][1]>>1)-(PMV[0][s][1]>>1),vert_f_code);
320 putbits(mv_field_sel[1][s],1);
321 putmv(MV[1][s][0]-PMV[1][s][0],hor_f_code);
322 putmv((MV[1][s][1]>>1)-(PMV[1][s][1]>>1),vert_f_code);
323 PMV[0][s][0]=MV[0][s][0];
324 PMV[0][s][1]=MV[0][s][1];
325 PMV[1][s][0]=MV[1][s][0];
326 PMV[1][s][1]=MV[1][s][1];
330 /* dual prime prediction */
331 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
333 putmv((MV[0][s][1]>>1)-(PMV[0][s][1]>>1),vert_f_code);
335 PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
336 PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
342 if (motion_type==MC_FIELD)
344 /* field prediction */
345 putbits(mv_field_sel[0][s],1);
346 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
347 putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
348 PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
349 PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];
351 else if (motion_type==MC_16X8)
353 /* 16x8 prediction */
354 putbits(mv_field_sel[0][s],1);
355 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
356 putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
357 putbits(mv_field_sel[1][s],1);
358 putmv(MV[1][s][0]-PMV[1][s][0],hor_f_code);
359 putmv(MV[1][s][1]-PMV[1][s][1],vert_f_code);
360 PMV[0][s][0]=MV[0][s][0];
361 PMV[0][s][1]=MV[0][s][1];
362 PMV[1][s][0]=MV[1][s][0];
363 PMV[1][s][1]=MV[1][s][1];
367 /* dual prime prediction */
368 putmv(MV[0][s][0]-PMV[0][s][0],hor_f_code);
370 putmv(MV[0][s][1]-PMV[0][s][1],vert_f_code);
372 PMV[0][s][0]=PMV[1][s][0]=MV[0][s][0];
373 PMV[0][s][1]=PMV[1][s][1]=MV[0][s][1];