1 |
2 |
kdv |
/* motion.c, motion vector decoding */
|
2 |
|
|
|
3 |
|
|
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
|
4 |
|
|
|
5 |
|
|
/*
|
6 |
|
|
* Disclaimer of Warranty
|
7 |
|
|
*
|
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.
|
15 |
|
|
*
|
16 |
|
|
* This disclaimer of warranty extends to the user of these programs and user's
|
17 |
|
|
* customers, employees, agents, transferees, successors, and assigns.
|
18 |
|
|
*
|
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
|
21 |
|
|
* patents.
|
22 |
|
|
*
|
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
|
26 |
|
|
* design.
|
27 |
|
|
*
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
#include <stdio.h>
|
31 |
|
|
|
32 |
|
|
#include "config.h"
|
33 |
|
|
#include "global.h"
|
34 |
|
|
|
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));
|
38 |
|
|
|
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)
|
42 |
|
|
int PMV[2][2][2];
|
43 |
|
|
int dmvector[2];
|
44 |
|
|
int motion_vertical_field_select[2][2];
|
45 |
|
|
int s, motion_vector_count, mv_format, h_r_size, v_r_size, dmv, mvscale;
|
46 |
|
|
{
|
47 |
|
|
if (motion_vector_count==1)
|
48 |
|
|
{
|
49 |
|
|
if (mv_format==MV_FIELD && !dmv)
|
50 |
|
|
{
|
51 |
|
|
motion_vertical_field_select[1][s] = motion_vertical_field_select[0][s] = Get_Bits(1);
|
52 |
|
|
#ifdef TRACE
|
53 |
|
|
if (Trace_Flag)
|
54 |
|
|
{
|
55 |
|
|
printf("motion_vertical_field_select[][%d] (%d): %d\n",s,
|
56 |
|
|
motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
|
57 |
|
|
}
|
58 |
|
|
#endif /* TRACE */
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
|
62 |
|
|
|
63 |
|
|
/* update other motion vector predictors */
|
64 |
|
|
PMV[1][s][0] = PMV[0][s][0];
|
65 |
|
|
PMV[1][s][1] = PMV[0][s][1];
|
66 |
|
|
#ifdef TRACE
|
67 |
|
|
if (Trace_Flag)
|
68 |
|
|
{
|
69 |
|
|
printf("updating motion vectors: PMV[1][%0d][0..1] = PMV[0][%0d][0..1]\n", s, s);
|
70 |
|
|
}
|
71 |
|
|
#endif /* TRACE */
|
72 |
|
|
}
|
73 |
|
|
else
|
74 |
|
|
{
|
75 |
|
|
motion_vertical_field_select[0][s] = Get_Bits(1);
|
76 |
|
|
#ifdef TRACE
|
77 |
|
|
if (Trace_Flag)
|
78 |
|
|
{
|
79 |
|
|
printf("motion_vertical_field_select[0][%d] (%d): %d\n",s,
|
80 |
|
|
motion_vertical_field_select[0][s],motion_vertical_field_select[0][s]);
|
81 |
|
|
}
|
82 |
|
|
#endif /* TRACE */
|
83 |
|
|
motion_vector(PMV[0][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
|
84 |
|
|
|
85 |
|
|
motion_vertical_field_select[1][s] = Get_Bits(1);
|
86 |
|
|
#ifdef TRACE
|
87 |
|
|
if (Trace_Flag)
|
88 |
|
|
{
|
89 |
|
|
printf("motion_vertical_field_select[1][%d] (%d): %d\n",s,
|
90 |
|
|
motion_vertical_field_select[1][s],motion_vertical_field_select[1][s]);
|
91 |
|
|
}
|
92 |
|
|
#endif /* TRACE */
|
93 |
|
|
motion_vector(PMV[1][s],dmvector,h_r_size,v_r_size,dmv,mvscale,0);
|
94 |
|
|
}
|
95 |
|
|
#ifdef TRACE
|
96 |
|
|
if (Trace_Flag)
|
97 |
|
|
{
|
98 |
|
|
printf("PMV[0][0][0] = %d\n", PMV[0][0][0]);
|
99 |
|
|
printf("PMV[0][0][1] = %d\n", PMV[0][0][1]);
|
100 |
|
|
printf("PMV[1][0][0] = %d\n", PMV[1][0][0]);
|
101 |
|
|
printf("PMV[1][0][1] = %d\n", PMV[1][0][1]);
|
102 |
|
|
printf("PMV[0][1][0] = %d\n", PMV[0][1][0]);
|
103 |
|
|
printf("PMV[0][1][1] = %d\n", PMV[0][1][1]);
|
104 |
|
|
printf("PMV[1][1][0] = %d\n", PMV[1][1][0]);
|
105 |
|
|
printf("PMV[1][1][1] = %d\n", PMV[1][1][1]);
|
106 |
|
|
}
|
107 |
|
|
#endif /* TRACE */
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/* get and decode motion vector and differential motion vector
|
111 |
|
|
for one prediction */
|
112 |
|
|
void motion_vector(PMV,dmvector,
|
113 |
|
|
h_r_size,v_r_size,dmv,mvscale,full_pel_vector)
|
114 |
|
|
int *PMV;
|
115 |
|
|
int *dmvector;
|
116 |
|
|
int h_r_size;
|
117 |
|
|
int v_r_size;
|
118 |
|
|
int dmv; /* MPEG-2 only: get differential motion vectors */
|
119 |
|
|
int mvscale; /* MPEG-2 only: field vector in frame pic */
|
120 |
|
|
int full_pel_vector; /* MPEG-1 only */
|
121 |
|
|
{
|
122 |
|
|
int motion_code, motion_residual;
|
123 |
|
|
|
124 |
|
|
/* horizontal component */
|
125 |
|
|
/* ISO/IEC 13818-2 Table B-10 */
|
126 |
|
|
motion_code = Get_motion_code();
|
127 |
|
|
|
128 |
|
|
motion_residual = (h_r_size!=0 && motion_code!=0) ? Get_Bits(h_r_size) : 0;
|
129 |
|
|
|
130 |
|
|
#ifdef TRACE
|
131 |
|
|
if (Trace_Flag)
|
132 |
|
|
{
|
133 |
|
|
if (h_r_size!=0 && motion_code!=0)
|
134 |
|
|
{
|
135 |
|
|
printf("motion_residual (");
|
136 |
|
|
Print_Bits(motion_residual,h_r_size,h_r_size);
|
137 |
|
|
printf("): %d\n",motion_residual);
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
#endif /* TRACE */
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);
|
144 |
|
|
|
145 |
|
|
if (dmv)
|
146 |
|
|
dmvector[0] = Get_dmvector();
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
/* vertical component */
|
150 |
|
|
motion_code = Get_motion_code();
|
151 |
|
|
motion_residual = (v_r_size!=0 && motion_code!=0) ? Get_Bits(v_r_size) : 0;
|
152 |
|
|
|
153 |
|
|
#ifdef TRACE
|
154 |
|
|
if (Trace_Flag)
|
155 |
|
|
{
|
156 |
|
|
if (v_r_size!=0 && motion_code!=0)
|
157 |
|
|
{
|
158 |
|
|
printf("motion_residual (");
|
159 |
|
|
Print_Bits(motion_residual,v_r_size,v_r_size);
|
160 |
|
|
printf("): %d\n",motion_residual);
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
#endif /* TRACE */
|
164 |
|
|
|
165 |
|
|
if (mvscale)
|
166 |
|
|
PMV[1] >>= 1; /* DIV 2 */
|
167 |
|
|
|
168 |
|
|
decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);
|
169 |
|
|
|
170 |
|
|
if (mvscale)
|
171 |
|
|
PMV[1] <<= 1;
|
172 |
|
|
|
173 |
|
|
if (dmv)
|
174 |
|
|
dmvector[1] = Get_dmvector();
|
175 |
|
|
|
176 |
|
|
#ifdef TRACE
|
177 |
|
|
if (Trace_Flag)
|
178 |
|
|
printf("PMV = %d,%d\n",PMV[0],PMV[1]);
|
179 |
|
|
#endif /* TRACE */
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
/* calculate motion vector component */
|
183 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.1: Decoding the motion vectors */
|
184 |
|
|
/* Note: the arithmetic here is more elegant than that which is shown
|
185 |
|
|
in 7.6.3.1. The end results (PMV[][][]) should, however, be the same. */
|
186 |
|
|
|
187 |
|
|
static void decode_motion_vector(pred,r_size,motion_code,motion_residual,full_pel_vector)
|
188 |
|
|
int *pred;
|
189 |
|
|
int r_size, motion_code, motion_residual;
|
190 |
|
|
int full_pel_vector; /* MPEG-1 (ISO/IEC 11172-1) support */
|
191 |
|
|
{
|
192 |
|
|
int lim, vec;
|
193 |
|
|
|
194 |
|
|
lim = 16<<r_size;
|
195 |
|
|
vec = full_pel_vector ? (*pred >> 1) : (*pred);
|
196 |
|
|
|
197 |
|
|
if (motion_code>0)
|
198 |
|
|
{
|
199 |
|
|
vec+= ((motion_code-1)<<r_size) + motion_residual + 1;
|
200 |
|
|
if (vec>=lim)
|
201 |
|
|
vec-= lim + lim;
|
202 |
|
|
}
|
203 |
|
|
else if (motion_code<0)
|
204 |
|
|
{
|
205 |
|
|
vec-= ((-motion_code-1)<<r_size) + motion_residual + 1;
|
206 |
|
|
if (vec<-lim)
|
207 |
|
|
vec+= lim + lim;
|
208 |
|
|
}
|
209 |
|
|
*pred = full_pel_vector ? (vec<<1) : vec;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
/* ISO/IEC 13818-2 section 7.6.3.6: Dual prime additional arithmetic */
|
214 |
|
|
void Dual_Prime_Arithmetic(DMV,dmvector,mvx,mvy)
|
215 |
|
|
int DMV[][2];
|
216 |
|
|
int *dmvector; /* differential motion vector */
|
217 |
|
|
int mvx, mvy; /* decoded mv components (always in field format) */
|
218 |
|
|
{
|
219 |
|
|
if (picture_structure==FRAME_PICTURE)
|
220 |
|
|
{
|
221 |
|
|
if (top_field_first)
|
222 |
|
|
{
|
223 |
|
|
/* vector for prediction of top field from bottom field */
|
224 |
|
|
DMV[0][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
|
225 |
|
|
DMV[0][1] = ((mvy +(mvy>0))>>1) + dmvector[1] - 1;
|
226 |
|
|
|
227 |
|
|
/* vector for prediction of bottom field from top field */
|
228 |
|
|
DMV[1][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
|
229 |
|
|
DMV[1][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] + 1;
|
230 |
|
|
}
|
231 |
|
|
else
|
232 |
|
|
{
|
233 |
|
|
/* vector for prediction of top field from bottom field */
|
234 |
|
|
DMV[0][0] = ((3*mvx+(mvx>0))>>1) + dmvector[0];
|
235 |
|
|
DMV[0][1] = ((3*mvy+(mvy>0))>>1) + dmvector[1] - 1;
|
236 |
|
|
|
237 |
|
|
/* vector for prediction of bottom field from top field */
|
238 |
|
|
DMV[1][0] = ((mvx +(mvx>0))>>1) + dmvector[0];
|
239 |
|
|
DMV[1][1] = ((mvy +(mvy>0))>>1) + dmvector[1] + 1;
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
else
|
243 |
|
|
{
|
244 |
|
|
/* vector for prediction from field of opposite 'parity' */
|
245 |
|
|
DMV[0][0] = ((mvx+(mvx>0))>>1) + dmvector[0];
|
246 |
|
|
DMV[0][1] = ((mvy+(mvy>0))>>1) + dmvector[1];
|
247 |
|
|
|
248 |
|
|
/* correct for vertical field shift */
|
249 |
|
|
if (picture_structure==TOP_FIELD)
|
250 |
|
|
DMV[0][1]--;
|
251 |
|
|
else
|
252 |
|
|
DMV[0][1]++;
|
253 |
|
|
}
|
254 |
|
|
#ifdef TRACE
|
255 |
|
|
if (Trace_Flag)
|
256 |
|
|
{
|
257 |
|
|
printf("DMV[0][0] = %d\n", DMV[0][0]);
|
258 |
|
|
printf("DMV[0][1] = %d\n", DMV[0][1]);
|
259 |
|
|
printf("DMV[1][0] = %d\n", DMV[1][0]);
|
260 |
|
|
printf("DMV[1][1] = %d\n", DMV[1][1]);
|
261 |
|
|
}
|
262 |
|
|
#endif /* TRACE */
|
263 |
|
|
}
|
264 |
|
|
|