1 /* Predict.c, motion compensation 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
33 /* private prototypes */
34 static void form_prediction _ANSI_ARGS_((unsigned char *src[], int sfield,
35 unsigned char *dst[], int dfield,
36 int lx, int lx2, int w, int h, int x, int y, int dx, int dy,
39 static void form_component_prediction _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
40 int lx, int lx2, int w, int h, int x, int y, int dx, int dy, int average_flag));
42 void form_predictions(bx,by,macroblock_type,motion_type,PMV,motion_vertical_field_select,dmvector,stwtype)
46 int PMV[2][2][2], motion_vertical_field_select[2][2], dmvector[2];
50 unsigned char **predframe;
54 stwtop = stwtype%3; /* 0:temporal, 1:(spat+temp)/2, 2:spatial */
57 if ((macroblock_type & MACROBLOCK_MOTION_FORWARD)
58 || (picture_coding_type==P_TYPE))
60 if (picture_structure==FRAME_PICTURE)
62 if ((motion_type==MC_FRAME)
63 || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
65 /* frame-based prediction (broken into top and bottom halves
66 for spatial scalability prediction purposes) */
68 form_prediction(forward_reference_frame,0,current_frame,0,
69 Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
70 PMV[0][0][0],PMV[0][0][1],stwtop);
73 form_prediction(forward_reference_frame,1,current_frame,1,
74 Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
75 PMV[0][0][0],PMV[0][0][1],stwbot);
77 else if (motion_type==MC_FIELD) /* field-based prediction */
79 /* top field prediction */
81 form_prediction(forward_reference_frame,motion_vertical_field_select[0][0],
82 current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
83 bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,stwtop);
85 /* bottom field prediction */
87 form_prediction(forward_reference_frame,motion_vertical_field_select[1][0],
88 current_frame,1,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
89 bx,by>>1,PMV[1][0][0],PMV[1][0][1]>>1,stwbot);
91 else if (motion_type==MC_DMV) /* dual prime prediction */
93 /* calculate derived motion vectors */
94 Dual_Prime_Arithmetic(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]>>1);
98 /* predict top field from top field */
99 form_prediction(forward_reference_frame,0,current_frame,0,
100 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
101 PMV[0][0][0],PMV[0][0][1]>>1,0);
103 /* predict and add to top field from bottom field */
104 form_prediction(forward_reference_frame,1,current_frame,0,
105 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
106 DMV[0][0],DMV[0][1],1);
111 /* predict bottom field from bottom field */
112 form_prediction(forward_reference_frame,1,current_frame,1,
113 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
114 PMV[0][0][0],PMV[0][0][1]>>1,0);
116 /* predict and add to bottom field from top field */
117 form_prediction(forward_reference_frame,0,current_frame,1,
118 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by>>1,
119 DMV[1][0],DMV[1][1],1);
123 /* invalid motion_type */
124 printf("invalid motion_type\n");
126 else /* TOP_FIELD or BOTTOM_FIELD */
129 currentfield = (picture_structure==BOTTOM_FIELD);
131 /* determine which frame to use for prediction */
132 if ((picture_coding_type==P_TYPE) && Second_Field
133 && (currentfield!=motion_vertical_field_select[0][0]))
134 predframe = backward_reference_frame; /* same frame */
136 predframe = forward_reference_frame; /* previous frame */
138 if ((motion_type==MC_FIELD)
139 || !(macroblock_type & MACROBLOCK_MOTION_FORWARD))
141 /* field-based prediction */
143 form_prediction(predframe,motion_vertical_field_select[0][0],current_frame,0,
144 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
145 PMV[0][0][0],PMV[0][0][1],stwtop);
147 else if (motion_type==MC_16X8)
151 form_prediction(predframe,motion_vertical_field_select[0][0],current_frame,0,
152 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by,
153 PMV[0][0][0],PMV[0][0][1],stwtop);
155 /* determine which frame to use for lower half prediction */
156 if ((picture_coding_type==P_TYPE) && Second_Field
157 && (currentfield!=motion_vertical_field_select[1][0]))
158 predframe = backward_reference_frame; /* same frame */
160 predframe = forward_reference_frame; /* previous frame */
162 form_prediction(predframe,motion_vertical_field_select[1][0],current_frame,0,
163 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,bx,by+8,
164 PMV[1][0][0],PMV[1][0][1],stwtop);
167 else if (motion_type==MC_DMV) /* dual prime prediction */
170 predframe = backward_reference_frame; /* same frame */
172 predframe = forward_reference_frame; /* previous frame */
174 /* calculate derived motion vectors */
175 Dual_Prime_Arithmetic(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]);
177 /* predict from field of same parity */
178 form_prediction(forward_reference_frame,currentfield,current_frame,0,
179 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
180 PMV[0][0][0],PMV[0][0][1],0);
182 /* predict from field of opposite parity */
183 form_prediction(predframe,!currentfield,current_frame,0,
184 Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,bx,by,
185 DMV[0][0],DMV[0][1],1);
188 /* invalid motion_type */
189 printf("invalid motion_type\n");
194 if (macroblock_type & MACROBLOCK_MOTION_BACKWARD)
196 if (picture_structure==FRAME_PICTURE)
198 if (motion_type==MC_FRAME)
200 /* frame-based prediction */
202 form_prediction(backward_reference_frame,0,current_frame,0,
203 Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
204 PMV[0][1][0],PMV[0][1][1],stwtop);
207 form_prediction(backward_reference_frame,1,current_frame,1,
208 Coded_Picture_Width,Coded_Picture_Width<<1,16,8,bx,by,
209 PMV[0][1][0],PMV[0][1][1],stwbot);
211 else /* field-based prediction */
213 /* top field prediction */
215 form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
216 current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
217 bx,by>>1,PMV[0][1][0],PMV[0][1][1]>>1,stwtop);
219 /* bottom field prediction */
221 form_prediction(backward_reference_frame,motion_vertical_field_select[1][1],
222 current_frame,1,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
223 bx,by>>1,PMV[1][1][0],PMV[1][1][1]>>1,stwbot);
226 else /* TOP_FIELD or BOTTOM_FIELD */
229 if (motion_type==MC_FIELD)
231 /* field-based prediction */
232 form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
233 current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,16,
234 bx,by,PMV[0][1][0],PMV[0][1][1],stwtop);
236 else if (motion_type==MC_16X8)
238 form_prediction(backward_reference_frame,motion_vertical_field_select[0][1],
239 current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
240 bx,by,PMV[0][1][0],PMV[0][1][1],stwtop);
242 form_prediction(backward_reference_frame,motion_vertical_field_select[1][1],
243 current_frame,0,Coded_Picture_Width<<1,Coded_Picture_Width<<1,16,8,
244 bx,by+8,PMV[1][1][0],PMV[1][1][1],stwtop);
247 /* invalid motion_type */
248 printf("invalid motion_type\n");
253 static void form_prediction(src,sfield,dst,dfield,lx,lx2,w,h,x,y,dx,dy,average_flag)
254 unsigned char *src[]; /* prediction source buffer */
255 int sfield; /* prediction source field number (0 or 1) */
256 unsigned char *dst[]; /* prediction destination buffer */
257 int dfield; /* prediction destination field number (0 or 1)*/
258 int lx,lx2; /* line strides */
259 int w,h; /* prediction block/sub-block width, height */
260 int x,y; /* pixel co-ordinates of top-left sample in current MB */
261 int dx,dy; /* horizontal, vertical prediction address */
262 int average_flag; /* add prediction error to prediction ? */
265 form_component_prediction(src[0]+(sfield?lx2>>1:0),dst[0]+(dfield?lx2>>1:0),
266 lx,lx2,w,h,x,y,dx,dy,average_flag);
268 if (chroma_format!=CHROMA444)
270 lx>>=1; lx2>>=1; w>>=1; x>>=1; dx/=2;
273 if (chroma_format==CHROMA420)
279 form_component_prediction(src[1]+(sfield?lx2>>1:0),dst[1]+(dfield?lx2>>1:0),
280 lx,lx2,w,h,x,y,dx,dy,average_flag);
283 form_component_prediction(src[2]+(sfield?lx2>>1:0),dst[2]+(dfield?lx2>>1:0),
284 lx,lx2,w,h,x,y,dx,dy,average_flag);
287 /* ISO/IEC 13818-2 section 7.6.4: Forming predictions */
288 /* NOTE: the arithmetic below produces numerically equivalent results
289 * to 7.6.4, yet is more elegant. It differs in the following ways:
291 * 1. the vectors (dx, dy) are based on cartesian frame
292 * coordiantes along a half-pel grid (always positive numbers)
293 * In contrast, vector[r][s][t] are differential (with positive and
294 * negative values). As a result, deriving the integer vectors
295 * (int_vec[t]) from dx, dy is accomplished by a simple right shift.
297 * 2. Half pel flags (xh, yh) are equivalent to the LSB (Least
298 * Significant Bit) of the half-pel coordinates (dx,dy).
301 * NOTE: the work of combining predictions (ISO/IEC 13818-2 section 7.6.7)
302 * is distributed among several other stages. This is accomplished by
303 * folding line offsets into the source and destination (src,dst)
304 * addresses (note the call arguments to form_prediction() in Predict()),
305 * line stride variables lx and lx2, the block dimension variables (w,h),
306 * average_flag, and by the very order in which Predict() is called.
307 * This implementation design (implicitly different than the spec)
308 * was chosen for its elegance.
311 static void form_component_prediction(src,dst,lx,lx2,w,h,x,y,dx,dy,average_flag)
314 int lx; /* raster line increment */
319 int average_flag; /* flag that signals bi-directional or Dual-Prime
320 averaging (7.6.7.1 and 7.6.7.4). if average_flag==1,
321 a previously formed prediction has been stored in
324 int xint; /* horizontal integer sample vector: analogous to int_vec[0] */
325 int yint; /* vertical integer sample vectors: analogous to int_vec[1] */
326 int xh; /* horizontal half sample flag: analogous to half_flag[0] */
327 int yh; /* vertical half sample flag: analogous to half_flag[1] */
329 unsigned char *s; /* source pointer: analogous to pel_ref[][] */
330 unsigned char *d; /* destination pointer: analogous to pel_pred[][] */
332 /* half pel scaling for integer vectors */
336 /* derive half pel flags */
340 /* compute the linear address of pel_ref[][] and pel_pred[][]
341 based on cartesian/raster cordinates provided */
342 s = src + lx*(y+yint) + x + xint;
345 if (!xh && !yh) /* no horizontal nor vertical half-pel */
354 d[i] = (v+(v>=0?1:0))>>1;
375 else if (!xh && yh) /* no horizontal but vertical half-pel */
383 v = d[i] + ((unsigned int)(s[i]+s[i+lx]+1)>>1);
384 d[i]=(v+(v>=0?1:0))>>1;
397 d[i] = (unsigned int)(s[i]+s[i+lx]+1)>>1;
405 else if (xh && !yh) /* horizontal but no vertical half-pel */
413 v = d[i] + ((unsigned int)(s[i]+s[i+1]+1)>>1);
414 d[i] = (v+(v>=0?1:0))>>1;
427 d[i] = (unsigned int)(s[i]+s[i+1]+1)>>1;
435 else /* if (xh && yh) horizontal and vertical half-pel */
443 v = d[i] + ((unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2);
444 d[i] = (v+(v>=0?1:0))>>1;
457 d[i] = (unsigned int)(s[i]+s[i+1]+s[i+lx]+s[i+lx+1]+2)>>2;