1 /* motion.c, motion vector decoding */
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
35 /* private prototypes */
36 static void decode_motion_vector _ANSI_ARGS_((int *pred, int r_size, int motion_code,
37 int motion_residualesidual, int full_pel_vector));
39 /* ISO/IEC 13818-2 sections 6.2.5.2, 6.3.17.2, and 7.6.3: Motion vectors */
40 void motion_vectors(PMV,dmvector,
41 motion_vertical_field_select,s,motion_vector_count,mv_format,h_r_size,v_r_size,dmv,mvscale)
44 int motion_vertical_field_select[2][2];
45 int s, motion_vector_count, mv_format, h_r_size, v_r_size, dmv, mvscale;
47 if (motion_vector_count==1)
49 if (mv_format==MV_FIELD && !dmv)
51 motion_vertical_field_select[1][s] = motion_vertical_field_select[0][s] = Get_Bits(1);
55 printf("motion_vertical_field_select[][%d] (%d): %d\n",s,
56 motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
61 motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
63 /* update other motion vector predictors */
64 PMV[1][s][0] = PMV[0][s][0];
65 PMV[1][s][1] = PMV[0][s][1];
69 motion_vertical_field_select[0][s] = Get_Bits(1);
73 printf("motion_vertical_field_select[0][%d] (%d): %d\n",s,
74 motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
77 motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
79 motion_vertical_field_select[1][s] = Get_Bits(1);
83 printf("motion_vertical_field_select[1][%d] (%d): %d\n",s,
84 motion_vertical_field_select[1][s],motion_vertical_field_select[1][s]);
87 motion_vector(PMV[1][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
91 /* get and decode motion vector and differential motion vector
93 void motion_vector(PMV,dmvector,
94 h_r_size,v_r_size,dmv,mvscale,full_pel_vector)
99 int dmv; /* MPEG-2 only: get differential motion vectors */
100 int mvscale; /* MPEG-2 only: field vector in frame pic */
101 int full_pel_vector; /* MPEG-1 only */
103 int motion_code, motion_residual;
105 /* horizontal component */
106 /* ISO/IEC 13818-2 Table B-10 */
107 motion_code = Get_motion_code();
109 motion_residual = (h_r_size!=0 && motion_code!=0) ? Get_Bits(h_r_size) : 0;
114 if (h_r_size!=0 && motion_code!=0)
116 printf("motion_residual (");
117 Print_Bits(motion_residual,h_r_size,h_r_size);
118 printf("): %d\n",motion_residual);
124 decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);
127 dmvector[0] = Get_dmvector();
130 /* vertical component */
131 motion_code = Get_motion_code();
132 motion_residual = (v_r_size!=0 && motion_code!=0) ? Get_Bits(v_r_size) : 0;
137 if (v_r_size!=0 && motion_code!=0)
139 printf("motion_residual (");
140 Print_Bits(motion_residual,v_r_size,v_r_size);
141 printf("): %d\n",motion_residual);
147 PMV[1] >>= 1; /* DIV 2 */
149 decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);
155 dmvector[1] = Get_dmvector();
159 printf("PMV = %d,%d\n",PMV[0],PMV[1]);
163 /* calculate motion vector component */
164 /* ISO/IEC 13818-2 section 7.6.3.1: Decoding the motion vectors */
165 /* Note: the arithmetic here is more elegant than that which is shown
166 in 7.6.3.1. The end results (PMV[][][]) should, however, be the same. */
168 static void decode_motion_vector(pred,r_size,motion_code,motion_residual,full_pel_vector)
170 int r_size, motion_code, motion_residual;
171 int full_pel_vector; /* MPEG-1 (ISO/IEC 11172-1) support */
176 vec = full_pel_vector ? (*pred >> 1) : (*pred);
180 vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
184 else if (motion_code<0)
186 vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
190 *pred = full_pel_vector ? (vec<<1) : vec;
194 /* ISO/IEC 13818-2 section 7.6.3.6: Dual prime additional arithmetic */
195 void Dual_Prime_Arithmetic(DMV,dmvector,mvx,mvy)
197 int *dmvector; /* differential motion vector */
198 int mvx, mvy; /* decoded mv components (always in field format) */
200 if (picture_structure==FRAME_PICTURE)
204 /* vector for prediction of top field from bottom field */
205 DMV[0][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
206 DMV[0][1] = ((mvy +(mvy>0))>>1) + dmvector[1] - 1;
208 /* vector for prediction of bottom field from top field */
209 DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
210 DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
214 /* vector for prediction of top field from bottom field */
215 DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
216 DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
218 /* vector for prediction of bottom field from top field */
219 DMV[1][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
220 DMV[1][1] = ((mvy +(mvy>0))>>1) + dmvector[1] + 1;
225 /* vector for prediction from field of opposite 'parity' */
226 DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
227 DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
229 /* correct for vertical field shift */
230 if (picture_structure==TOP_FIELD)