1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
7 int mpeg3video_get_macroblock_address(mpeg3_slice_t
*slice
)
10 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
12 while((code
= mpeg3slice_showbits(slice_buffer
, 11)) < 24)
14 /* Is not macroblock_stuffing */
17 /* Is macroblock_escape */
24 /* fprintf(stderr, "mpeg3video_get_macroblock_address: invalid macroblock_address_increment code\n"); */
30 mpeg3slice_flushbits(slice_buffer
, 11);
35 mpeg3slice_flushbit(slice_buffer
);
42 mpeg3slice_flushbits(slice_buffer
, mpeg3_MBAtab1
[code
].len
);
43 return val
+ mpeg3_MBAtab1
[code
].val
;
47 mpeg3slice_flushbits(slice_buffer
, mpeg3_MBAtab2
[code
].len
);
49 return val
+ mpeg3_MBAtab2
[code
].val
;
52 /* macroblock_type for pictures with spatial scalability */
54 static inline int mpeg3video_getsp_imb_type(mpeg3_slice_t
*slice
)
56 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
57 unsigned int code
= mpeg3slice_showbits(slice_buffer
, 4);
60 /* fprintf(stderr,"mpeg3video_getsp_imb_type: invalid macroblock_type code\n"); */
65 mpeg3slice_flushbits(slice_buffer
, mpeg3_spIMBtab
[code
].len
);
66 return mpeg3_spIMBtab
[code
].val
;
69 static inline int mpeg3video_getsp_pmb_type(mpeg3_slice_t
*slice
)
71 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
72 int code
= mpeg3slice_showbits(slice_buffer
, 7);
75 /* fprintf(stderr,"mpeg3video_getsp_pmb_type: invalid macroblock_type code\n"); */
83 mpeg3slice_flushbits(slice_buffer
, mpeg3_spPMBtab0
[code
].len
);
85 return mpeg3_spPMBtab0
[code
].val
;
88 mpeg3slice_flushbits(slice_buffer
, mpeg3_spPMBtab1
[code
].len
);
89 return mpeg3_spPMBtab1
[code
].val
;
92 static inline int mpeg3video_getsp_bmb_type(mpeg3_slice_t
*slice
)
95 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
96 int code
= mpeg3slice_showbits9(slice_buffer
);
99 p
= &mpeg3_spBMBtab0
[(code
>> 5) - 2];
102 p
= &mpeg3_spBMBtab1
[(code
>> 2) - 4];
105 p
= &mpeg3_spBMBtab2
[code
- 8];
108 /* fprintf(stderr,"mpeg3video_getsp_bmb_type: invalid macroblock_type code\n"); */
113 mpeg3slice_flushbits(slice_buffer
, p
->len
);
117 static inline int mpeg3video_get_imb_type(mpeg3_slice_t
*slice
)
119 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
120 if(mpeg3slice_getbit(slice_buffer
))
125 if(!mpeg3slice_getbit(slice_buffer
))
127 /* fprintf(stderr,"mpeg3video_get_imb_type: invalid macroblock_type code\n"); */
134 static inline int mpeg3video_get_pmb_type(mpeg3_slice_t
*slice
)
137 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
139 if((code
= mpeg3slice_showbits(slice_buffer
, 6)) >= 8)
142 mpeg3slice_flushbits(slice_buffer
, mpeg3_PMBtab0
[code
].len
);
143 return mpeg3_PMBtab0
[code
].val
;
148 /* fprintf(stderr,"mpeg3video_get_pmb_type: invalid macroblock_type code\n"); */
153 mpeg3slice_flushbits(slice_buffer
, mpeg3_PMBtab1
[code
].len
);
154 return mpeg3_PMBtab1
[code
].val
;
157 static inline int mpeg3video_get_bmb_type(mpeg3_slice_t
*slice
)
160 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
162 if((code
= mpeg3slice_showbits(slice_buffer
, 6)) >= 8)
165 mpeg3slice_flushbits(slice_buffer
, mpeg3_BMBtab0
[code
].len
);
166 return mpeg3_BMBtab0
[code
].val
;
171 /* fprintf(stderr,"mpeg3video_get_bmb_type: invalid macroblock_type code\n"); */
176 mpeg3slice_flushbits(slice_buffer
, mpeg3_BMBtab1
[code
].len
);
178 return mpeg3_BMBtab1
[code
].val
;
181 static inline int mpeg3video_get_dmb_type(mpeg3_slice_t
*slice
)
183 if(!mpeg3slice_getbit(slice
->slice_buffer
))
185 /* fprintf(stderr,"mpeg3video_get_dmb_type: invalid macroblock_type code\n"); */
193 static inline int mpeg3video_get_snrmb_type(mpeg3_slice_t
*slice
)
195 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
196 int code
= mpeg3slice_showbits(slice_buffer
, 3);
200 /* fprintf(stderr,"mpeg3video_get_snrmb_type: invalid macroblock_type code\n"); */
205 mpeg3slice_flushbits(slice_buffer
, mpeg3_SNRMBtab
[code
].len
);
206 return mpeg3_SNRMBtab
[code
].val
;
209 int mpeg3video_get_mb_type(mpeg3_slice_t
*slice
, mpeg3video_t
*video
)
211 if(video
->scalable_mode
== SC_SNR
)
213 return mpeg3video_get_snrmb_type(slice
);
217 switch(video
->pict_type
)
219 case I_TYPE
: return video
->pict_scal
? mpeg3video_getsp_imb_type(slice
) : mpeg3video_get_imb_type(slice
);
220 case P_TYPE
: return video
->pict_scal
? mpeg3video_getsp_pmb_type(slice
) : mpeg3video_get_pmb_type(slice
);
221 case B_TYPE
: return video
->pict_scal
? mpeg3video_getsp_bmb_type(slice
) : mpeg3video_get_bmb_type(slice
);
222 case D_TYPE
: return mpeg3video_get_dmb_type(slice
);
224 /*fprintf(stderr, "mpeg3video_getmbtype: unknown coding type\n"); */
226 /* MPEG-1 only, not implemented */
233 int mpeg3video_macroblock_modes(mpeg3_slice_t
*slice
,
246 int stwtype
, stwcode
, stwclass
;
247 int motion_type
= 0, mv_count
, mv_format
, dmv
, mvscale
;
249 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
250 static unsigned char stwc_table
[3][4]
251 = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
252 static unsigned char stwclass_table
[9]
253 = {0, 1, 2, 1, 1, 2, 3, 3, 4};
255 /* get macroblock_type */
256 mb_type
= mpeg3video_get_mb_type(slice
, video
);
258 if(slice
->fault
) return 1;
260 /* get spatial_temporal_weight_code */
261 if(mb_type
& MB_WEIGHT
)
263 if(video
->stwc_table_index
== 0)
267 stwcode
= mpeg3slice_getbits2(slice_buffer
);
268 stwtype
= stwc_table
[video
->stwc_table_index
- 1][stwcode
];
272 stwtype
= (mb_type
& MB_CLASS4
) ? 8 : 0;
274 /* derive spatial_temporal_weight_class (Table 7-18) */
275 stwclass
= stwclass_table
[stwtype
];
277 /* get frame/field motion type */
278 if(mb_type
& (MB_FORWARD
| MB_BACKWARD
))
280 if(video
->pict_struct
== FRAME_PICTURE
)
282 /* frame_motion_type */
283 motion_type
= video
->frame_pred_dct
? MC_FRAME
: mpeg3slice_getbits2(slice_buffer
);
287 /* field_motion_type */
288 motion_type
= mpeg3slice_getbits2(slice_buffer
);
292 if((mb_type
& MB_INTRA
) && video
->conceal_mv
)
294 /* concealment motion vectors */
295 motion_type
= (video
->pict_struct
== FRAME_PICTURE
) ? MC_FRAME
: MC_FIELD
;
298 /* derive mv_count, mv_format and dmv, (table 6-17, 6-18) */
299 if(video
->pict_struct
== FRAME_PICTURE
)
301 mv_count
= (motion_type
== MC_FIELD
&& stwclass
< 2) ? 2 : 1;
302 mv_format
= (motion_type
== MC_FRAME
) ? MV_FRAME
: MV_FIELD
;
306 mv_count
= (motion_type
== MC_16X8
) ? 2 : 1;
307 mv_format
= MV_FIELD
;
310 dmv
= (motion_type
== MC_DMV
); /* dual prime */
312 /* field mv predictions in frame pictures have to be scaled */
313 mvscale
= ((mv_format
== MV_FIELD
) && (video
->pict_struct
== FRAME_PICTURE
));
315 /* get dct_type (frame DCT / field DCT) */
316 dct_type
= (video
->pict_struct
== FRAME_PICTURE
) &&
317 (!video
->frame_pred_dct
) &&
318 (mb_type
& (MB_PATTERN
| MB_INTRA
)) ?
319 mpeg3slice_getbit(slice_buffer
) : 0;
324 *pstwclass
= stwclass
;
325 *pmotion_type
= motion_type
;
326 *pmv_count
= mv_count
;
327 *pmv_format
= mv_format
;
330 *pdct_type
= dct_type
;