1 /* predict.c, motion compensated prediction */
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 predict_mb _ANSI_ARGS_((
36 unsigned char *oldref[], unsigned char *newref[], unsigned char *cur[],
37 int lx, int bx, int by, int pict_type, int pict_struct, int mb_type,
38 int motion_type, int secondfield,
39 int PMV[2][2][2], int mv_field_sel[2][2], int dmvector[2]));
41 static void pred _ANSI_ARGS_((unsigned char *src[], int sfield,
42 unsigned char *dst[], int dfield,
43 int lx, int w, int h, int x, int y, int dx, int dy, int addflag));
45 static void pred_comp _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
46 int lx, int w, int h, int x, int y, int dx, int dy, int addflag));
48 static void calc_DMV _ANSI_ARGS_((int DMV[][2], int *dmvector, int mvx,
51 static void clearblock _ANSI_ARGS_((unsigned char *cur[], int i0, int j0));
54 /* form prediction for a complete picture (frontend for predict_mb)
56 * reff: reference frame for forward prediction
57 * refb: reference frame for backward prediction
58 * cur: destination (current) frame
59 * secondfield: predict second field of a frame
60 * mbi: macroblock info
66 void predict(reff,refb,cur,secondfield,mbi)
67 unsigned char *reff[],*refb[],*cur[3];
75 /* loop through all macroblocks of the picture */
76 for (j=0; j<height2; j+=16)
77 for (i=0; i<width; i+=16)
79 predict_mb(reff,refb,cur,width,i,j,pict_type,pict_struct,
80 mbi[k].mb_type,mbi[k].motion_type,secondfield,
81 mbi[k].MV,mbi[k].mv_field_sel,mbi[k].dmvector);
87 /* form prediction for one macroblock
89 * oldref: reference frame for forward prediction
90 * newref: reference frame for backward prediction
91 * cur: destination (current) frame
92 * lx: frame width (identical to global var `width')
93 * bx,by: picture (field or frame) coordinates of macroblock to be predicted
94 * pict_type: I, P or B
95 * pict_struct: FRAME_PICTURE, TOP_FIELD, BOTTOM_FIELD
96 * mb_type: MB_FORWARD, MB_BACKWARD, MB_INTRA
97 * motion_type: MC_FRAME, MC_FIELD, MC_16X8, MC_DMV
98 * secondfield: predict second field of a frame
99 * PMV[2][2][2]: motion vectors (in half pel picture coordinates)
100 * mv_field_sel[2][2]: motion vertical field selects (for field predictions)
101 * dmvector: differential motion vectors (for dual prime)
104 * - when predicting a P type picture which is the second field of
105 * a frame, the same parity reference field is in oldref, while the
106 * opposite parity reference field is assumed to be in newref!
107 * - intra macroblocks are modelled to have a constant prediction of 128
108 * for all pels; this results in a DC DCT coefficient symmetric to 0
109 * - vectors for field prediction in frame pictures are in half pel frame
110 * coordinates (vertical component is twice the field value and always
113 * already covers dual prime (not yet used)
116 static void predict_mb(oldref,newref,cur,lx,bx,by,pict_type,pict_struct,
117 mb_type,motion_type,secondfield,PMV,mv_field_sel,dmvector)
118 unsigned char *oldref[],*newref[],*cur[];
126 int PMV[2][2][2], mv_field_sel[2][2], dmvector[2];
128 int addflag, currentfield;
129 unsigned char **predframe;
132 if (mb_type&MB_INTRA)
134 clearblock(cur,bx,by);
138 addflag = 0; /* first prediction is stored, second is added and averaged */
140 if ((mb_type & MB_FORWARD) || (pict_type==P_TYPE))
142 /* forward prediction, including zero MV in P pictures */
144 if (pict_struct==FRAME_PICTURE)
148 if ((motion_type==MC_FRAME) || !(mb_type & MB_FORWARD))
150 /* frame-based prediction in frame picture */
152 lx,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);
154 else if (motion_type==MC_FIELD)
156 /* field-based prediction in frame picture
158 * note scaling of the vertical coordinates (by, PMV[][0][1])
159 * from frame to field!
162 /* top field prediction */
163 pred(oldref,mv_field_sel[0][0],cur,0,
164 lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);
166 /* bottom field prediction */
167 pred(oldref,mv_field_sel[1][0],cur,1,
168 lx<<1,16,8,bx,by>>1,PMV[1][0][0],PMV[1][0][1]>>1,0);
170 else if (motion_type==MC_DMV)
172 /* dual prime prediction */
174 /* calculate derived motion vectors */
175 calc_DMV(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]>>1);
177 /* predict top field from top field */
179 lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);
181 /* predict bottom field from bottom field */
183 lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);
185 /* predict and add to top field from bottom field */
187 lx<<1,16,8,bx,by>>1,DMV[0][0],DMV[0][1],1);
189 /* predict and add to bottom field from top field */
191 lx<<1,16,8,bx,by>>1,DMV[1][0],DMV[1][1],1);
195 /* invalid motion_type in frame picture */
197 fprintf(stderr,"invalid motion_type\n");
200 else /* TOP_FIELD or BOTTOM_FIELD */
204 currentfield = (pict_struct==BOTTOM_FIELD);
206 /* determine which frame to use for prediction */
207 if ((pict_type==P_TYPE) && secondfield
208 && (currentfield!=mv_field_sel[0][0]))
209 predframe = newref; /* same frame */
211 predframe = oldref; /* previous frame */
213 if ((motion_type==MC_FIELD) || !(mb_type & MB_FORWARD))
215 /* field-based prediction in field picture */
216 pred(predframe,mv_field_sel[0][0],cur,currentfield,
217 lx<<1,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);
219 else if (motion_type==MC_16X8)
221 /* 16 x 8 motion compensation in field picture */
224 pred(predframe,mv_field_sel[0][0],cur,currentfield,
225 lx<<1,16,8,bx,by,PMV[0][0][0],PMV[0][0][1],0);
227 /* determine which frame to use for lower half prediction */
228 if ((pict_type==P_TYPE) && secondfield
229 && (currentfield!=mv_field_sel[1][0]))
230 predframe = newref; /* same frame */
232 predframe = oldref; /* previous frame */
235 pred(predframe,mv_field_sel[1][0],cur,currentfield,
236 lx<<1,16,8,bx,by+8,PMV[1][0][0],PMV[1][0][1],0);
238 else if (motion_type==MC_DMV)
240 /* dual prime prediction */
242 /* determine which frame to use for prediction */
244 predframe = newref; /* same frame */
246 predframe = oldref; /* previous frame */
248 /* calculate derived motion vectors */
249 calc_DMV(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]);
251 /* predict from field of same parity */
252 pred(oldref,currentfield,cur,currentfield,
253 lx<<1,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);
255 /* predict from field of opposite parity */
256 pred(predframe,!currentfield,cur,currentfield,
257 lx<<1,16,16,bx,by,DMV[0][0],DMV[0][1],1);
261 /* invalid motion_type in field picture */
263 fprintf(stderr,"invalid motion_type\n");
266 addflag = 1; /* next prediction (if any) will be averaged with this one */
269 if (mb_type & MB_BACKWARD)
271 /* backward prediction */
273 if (pict_struct==FRAME_PICTURE)
277 if (motion_type==MC_FRAME)
279 /* frame-based prediction in frame picture */
281 lx,16,16,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);
285 /* field-based prediction in frame picture
287 * note scaling of the vertical coordinates (by, PMV[][1][1])
288 * from frame to field!
291 /* top field prediction */
292 pred(newref,mv_field_sel[0][1],cur,0,
293 lx<<1,16,8,bx,by>>1,PMV[0][1][0],PMV[0][1][1]>>1,addflag);
295 /* bottom field prediction */
296 pred(newref,mv_field_sel[1][1],cur,1,
297 lx<<1,16,8,bx,by>>1,PMV[1][1][0],PMV[1][1][1]>>1,addflag);
300 else /* TOP_FIELD or BOTTOM_FIELD */
304 currentfield = (pict_struct==BOTTOM_FIELD);
306 if (motion_type==MC_FIELD)
308 /* field-based prediction in field picture */
309 pred(newref,mv_field_sel[0][1],cur,currentfield,
310 lx<<1,16,16,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);
312 else if (motion_type==MC_16X8)
314 /* 16 x 8 motion compensation in field picture */
317 pred(newref,mv_field_sel[0][1],cur,currentfield,
318 lx<<1,16,8,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);
321 pred(newref,mv_field_sel[1][1],cur,currentfield,
322 lx<<1,16,8,bx,by+8,PMV[1][1][0],PMV[1][1][1],addflag);
326 /* invalid motion_type in field picture */
328 fprintf(stderr,"invalid motion_type\n");
334 /* predict a rectangular block (all three components)
336 * src: source frame (Y,U,V)
337 * sfield: source field select (0: frame or top field, 1: bottom field)
338 * dst: destination frame (Y,U,V)
339 * dfield: destination field select (0: frame or top field, 1: bottom field)
341 * the following values are in luminance picture (frame or field) dimensions
342 * lx: distance of vertically adjacent pels (selects frame or field pred.)
343 * w,h: width and height of block (only 16x16 or 16x8 are used)
344 * x,y: coordinates of destination block
345 * dx,dy: half pel motion vector
346 * addflag: store or add (= average) prediction
348 static void pred(src,sfield,dst,dfield,lx,w,h,x,y,dx,dy,addflag)
349 unsigned char *src[];
351 unsigned char *dst[];
361 for (cc=0; cc<3; cc++)
365 /* scale for color components */
366 if (chroma_format==CHROMA420)
369 h >>= 1; y >>= 1; dy /= 2;
371 if (chroma_format!=CHROMA444)
374 w >>= 1; x >>= 1; dx /= 2;
378 pred_comp(src[cc]+(sfield?lx>>1:0),dst[cc]+(dfield?lx>>1:0),
379 lx,w,h,x,y,dx,dy,addflag);
383 /* low level prediction routine
385 * src: prediction source
386 * dst: prediction destination
387 * lx: line width (for both src and dst)
388 * x,y: destination coordinates
389 * dx,dy: half pel motion vector
390 * w,h: size of prediction block
391 * addflag: store or add prediction
394 static void pred_comp(src,dst,lx,w,h,x,y,dx,dy,addflag)
403 int xint, xh, yint, yh;
405 unsigned char *s, *d;
407 /* half pel scaling */
408 xint = dx>>1; /* integer part */
409 xh = dx & 1; /* half pel flag */
414 s = src + lx*(y+yint) + (x+xint); /* motion vector */
422 d[i] = (unsigned int)(d[i]+s[i]+1)>>1;
439 d[i] = (d[i] + ((unsigned int)(s[i]+s[i+lx]+1)>>1)+1)>>1;
447 d[i] = (unsigned int)(s[i]+s[i+lx]+1)>>1;
456 d[i] = (d[i] + ((unsigned int)(s[i]+s[i+1]+1)>>1)+1)>>1;
464 d[i] = (unsigned int)(s[i]+s[i+1]+1)>>1;
468 else /* if (xh && yh) */
473 d[i] = (d[i] + ((unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2)+1)>>1;
481 d[i] = (unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2;
488 /* calculate derived motion vectors (DMV) for dual prime prediction
489 * dmvector[2]: differential motion vectors (-1,0,+1)
490 * mvx,mvy: motion vector (for same parity)
492 * DMV[2][2]: derived motion vectors (for opposite parity)
494 * uses global variables pict_struct and topfirst
497 * - all vectors are in field coordinates (even for frame pictures)
500 static void calc_DMV(DMV,dmvector,mvx,mvy)
505 if (pict_struct==FRAME_PICTURE)
509 /* vector for prediction of top field from bottom field */
510 DMV[0][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
511 DMV[0][1] = ((mvy +(mvy>0))>>1) + dmvector[1] - 1;
513 /* vector for prediction of bottom field from top field */
514 DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
515 DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
519 /* vector for prediction of top field from bottom field */
520 DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
521 DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
523 /* vector for prediction of bottom field from top field */
524 DMV[1][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
525 DMV[1][1] = ((mvy +(mvy>0))>>1) + dmvector[1] + 1;
530 /* vector for prediction from field of opposite 'parity' */
531 DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
532 DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
534 /* correct for vertical field shift */
535 if (pict_struct==TOP_FIELD)
542 static void clearblock(cur,i0,j0)
543 unsigned char *cur[];
549 p = cur[0] + ((pict_struct==BOTTOM_FIELD) ? width : 0) + i0 + width2*j0;
560 if (chroma_format!=CHROMA444)
565 if (chroma_format==CHROMA420)
570 p = cur[1] + ((pict_struct==BOTTOM_FIELD) ? chrom_width : 0) + i0
580 p = cur[2] + ((pict_struct==BOTTOM_FIELD) ? chrom_width : 0) + i0