1 #include "mpeg3private.h"
2 #include "mpeg3protos.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
);
17 vec
+= ((motion_code
- 1) << r_size
) + motion_r
+ 1;
18 if(vec
>= lim
) vec
-= lim
+ lim
;
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
,
40 if(video
->pict_struct
== FRAME_PICTURE
)
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;
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;
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
)
77 int mpeg3video_get_mv(mpeg3_slice_t
*slice
)
80 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
82 if(mpeg3slice_getbit(slice_buffer
))
87 if((code
= mpeg3slice_showbits9(slice_buffer
)) >= 64)
90 mpeg3slice_flushbits(slice_buffer
, mpeg3_MVtab0
[code
].len
);
91 return mpeg3slice_getbit(slice_buffer
) ? -mpeg3_MVtab0
[code
].val
: mpeg3_MVtab0
[code
].val
;
97 mpeg3slice_flushbits(slice_buffer
, mpeg3_MVtab1
[code
].len
);
98 return mpeg3slice_getbit(slice_buffer
) ? -mpeg3_MVtab1
[code
].val
: mpeg3_MVtab1
[code
].val
;
103 /* fprintf(stdout,"mpeg3video_get_mv: invalid motion_vector code\n"); */
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;
129 /* get and decode motion vector and differential motion vector */
131 void mpeg3video_motion_vector(mpeg3_slice_t
*slice
,
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;
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
,
169 int mv_field_sel
[2][2],
179 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
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
,
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];
204 mv_field_sel
[0][s
] = mpeg3slice_getbit(slice_buffer
);
205 mpeg3video_motion_vector(slice
,
214 if(slice
->fault
) return 1;
216 mv_field_sel
[1][s
] = mpeg3slice_getbit(slice_buffer
);
217 mpeg3video_motion_vector(slice
,
226 if(slice
->fault
) return 1;