Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / video / motion.c
blob6a5e928bd3ff23bdd075da89c723be84a323ec17
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "vlc.h"
5 #include <stdio.h>
8 /* calculate motion vector component */
10 void mpeg3video_calc_mv(int *pred, int r_size, int motion_code, int motion_r, int full_pel_vector)
12 int lim = 16 << r_size;
13 int vec = full_pel_vector ? (*pred >> 1) : (*pred);
15 if(motion_code > 0)
17 vec += ((motion_code - 1) << r_size) + motion_r + 1;
18 if(vec >= lim) vec -= lim + lim;
20 else
21 if(motion_code < 0)
23 vec -= ((-motion_code - 1) << r_size) + motion_r + 1;
24 if(vec < -lim) vec += lim + lim;
26 *pred = full_pel_vector ? (vec << 1) : vec;
31 int *dmvector, * differential motion vector *
32 int mvx, int mvy * decoded mv components (always in field format) *
34 void mpeg3video_calc_dmv(mpeg3video_t *video,
35 int DMV[][2],
36 int *dmvector,
37 int mvx,
38 int mvy)
40 if(video->pict_struct == FRAME_PICTURE)
42 if(video->topfirst)
44 /* vector for prediction of top field from bottom field */
45 DMV[0][0] = ((mvx + (mvx>0)) >> 1) + dmvector[0];
46 DMV[0][1] = ((mvy + (mvy>0)) >> 1) + dmvector[1] - 1;
48 /* vector for prediction of bottom field from top field */
49 DMV[1][0] = ((3 * mvx + (mvx > 0)) >> 1) + dmvector[0];
50 DMV[1][1] = ((3 * mvy + (mvy > 0)) >> 1) + dmvector[1] + 1;
52 else
54 /* vector for prediction of top field from bottom field */
55 DMV[0][0] = ((3 * mvx + (mvx>0)) >> 1) + dmvector[0];
56 DMV[0][1] = ((3 * mvy + (mvy>0)) >> 1) + dmvector[1] - 1;
58 /* vector for prediction of bottom field from top field */
59 DMV[1][0] = ((mvx + (mvx>0)) >> 1) + dmvector[0];
60 DMV[1][1] = ((mvy + (mvy>0)) >> 1) + dmvector[1] + 1;
63 else
65 /* vector for prediction from field of opposite 'parity' */
66 DMV[0][0] = ((mvx + (mvx > 0)) >> 1) + dmvector[0];
67 DMV[0][1] = ((mvy + (mvy > 0)) >> 1) + dmvector[1];
69 /* correct for vertical field shift */
70 if(video->pict_struct == TOP_FIELD)
71 DMV[0][1]--;
72 else
73 DMV[0][1]++;
77 int mpeg3video_get_mv(mpeg3_slice_t *slice)
79 int code;
80 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
82 if(mpeg3slice_getbit(slice_buffer))
84 return 0;
87 if((code = mpeg3slice_showbits9(slice_buffer)) >= 64)
89 code >>= 6;
90 mpeg3slice_flushbits(slice_buffer, mpeg3_MVtab0[code].len);
91 return mpeg3slice_getbit(slice_buffer) ? -mpeg3_MVtab0[code].val : mpeg3_MVtab0[code].val;
94 if(code >= 24)
96 code >>= 3;
97 mpeg3slice_flushbits(slice_buffer, mpeg3_MVtab1[code].len);
98 return mpeg3slice_getbit(slice_buffer) ? -mpeg3_MVtab1[code].val : mpeg3_MVtab1[code].val;
101 if((code -= 12) < 0)
103 /* fprintf(stdout,"mpeg3video_get_mv: invalid motion_vector code\n"); */
104 slice->fault = 1;
105 return 1;
108 mpeg3slice_flushbits(slice_buffer, mpeg3_MVtab2[code].len);
109 return mpeg3slice_getbit(slice_buffer) ? -mpeg3_MVtab2[code].val : mpeg3_MVtab2[code].val;
112 /* get differential motion vector (for dual prime prediction) */
114 int mpeg3video_get_dmv(mpeg3_slice_t *slice)
116 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
117 if(mpeg3slice_getbit(slice_buffer))
119 return mpeg3slice_getbit(slice_buffer) ? -1 : 1;
121 else
123 return 0;
129 /* get and decode motion vector and differential motion vector */
131 void mpeg3video_motion_vector(mpeg3_slice_t *slice,
132 mpeg3video_t *video,
133 int *PMV,
134 int *dmvector,
135 int h_r_size,
136 int v_r_size,
137 int dmv,
138 int mvscale,
139 int full_pel_vector)
141 int motion_r;
142 int motion_code = mpeg3video_get_mv(slice);
143 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
145 if(slice->fault) return;
146 motion_r = (h_r_size != 0 && motion_code != 0) ? mpeg3slice_getbits(slice_buffer, h_r_size) : 0;
148 mpeg3video_calc_mv(&PMV[0], h_r_size, motion_code, motion_r, full_pel_vector);
150 if(dmv) dmvector[0] = mpeg3video_get_dmv(slice);
152 motion_code = mpeg3video_get_mv(slice);
153 if(slice->fault) return;
154 motion_r = (v_r_size != 0 && motion_code != 0) ? mpeg3slice_getbits(slice_buffer, v_r_size) : 0;
156 /* DIV 2 */
157 if(mvscale) PMV[1] >>= 1;
159 mpeg3video_calc_mv(&PMV[1], v_r_size, motion_code, motion_r, full_pel_vector);
161 if(mvscale) PMV[1] <<= 1;
162 if(dmv) dmvector[1] = mpeg3video_get_dmv(slice);
165 int mpeg3video_motion_vectors(mpeg3_slice_t *slice,
166 mpeg3video_t *video,
167 int PMV[2][2][2],
168 int dmvector[2],
169 int mv_field_sel[2][2],
170 int s,
171 int mv_count,
172 int mv_format,
173 int h_r_size,
174 int v_r_size,
175 int dmv,
176 int mvscale)
178 int result = 0;
179 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
180 if(mv_count == 1)
182 if(mv_format == MV_FIELD && !dmv)
184 mv_field_sel[1][s] = mv_field_sel[0][s] = mpeg3slice_getbit(slice_buffer);
187 mpeg3video_motion_vector(slice,
188 video,
189 PMV[0][s],
190 dmvector,
191 h_r_size,
192 v_r_size,
193 dmv,
194 mvscale,
196 if(slice->fault) return 1;
198 /* update other motion vector predictors */
199 PMV[1][s][0] = PMV[0][s][0];
200 PMV[1][s][1] = PMV[0][s][1];
202 else
204 mv_field_sel[0][s] = mpeg3slice_getbit(slice_buffer);
205 mpeg3video_motion_vector(slice,
206 video,
207 PMV[0][s],
208 dmvector,
209 h_r_size,
210 v_r_size,
211 dmv,
212 mvscale,
214 if(slice->fault) return 1;
216 mv_field_sel[1][s] = mpeg3slice_getbit(slice_buffer);
217 mpeg3video_motion_vector(slice,
218 video,
219 PMV[1][s],
220 dmvector,
221 h_r_size,
222 v_r_size,
223 dmv,
224 mvscale,
226 if(slice->fault) return 1;
228 return 0;