Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / video / macroblocks.c
blob85a224fcbf4bf043bbe0970f7fbffe20f11546b4
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "vlc.h"
5 #include <stdio.h>
7 int mpeg3video_get_macroblock_address(mpeg3_slice_t *slice)
9 int code, val = 0;
10 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
12 while((code = mpeg3slice_showbits(slice_buffer, 11)) < 24)
14 /* Is not macroblock_stuffing */
15 if(code != 15)
17 /* Is macroblock_escape */
18 if(code == 8)
20 val += 33;
22 else
24 /* fprintf(stderr, "mpeg3video_get_macroblock_address: invalid macroblock_address_increment code\n"); */
25 slice->fault = 1;
26 return 1;
30 mpeg3slice_flushbits(slice_buffer, 11);
33 if(code >= 1024)
35 mpeg3slice_flushbit(slice_buffer);
36 return val + 1;
39 if(code >= 128)
41 code >>= 6;
42 mpeg3slice_flushbits(slice_buffer, mpeg3_MBAtab1[code].len);
43 return val + mpeg3_MBAtab1[code].val;
46 code -= 24;
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);
58 if(!code)
60 /* fprintf(stderr,"mpeg3video_getsp_imb_type: invalid macroblock_type code\n"); */
61 slice->fault = 1;
62 return 0;
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);
73 if(code < 2)
75 /* fprintf(stderr,"mpeg3video_getsp_pmb_type: invalid macroblock_type code\n"); */
76 slice->fault = 1;
77 return 0;
80 if(code >= 16)
82 code >>= 3;
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)
94 mpeg3_VLCtab_t *p;
95 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
96 int code = mpeg3slice_showbits9(slice_buffer);
98 if(code >= 64)
99 p = &mpeg3_spBMBtab0[(code >> 5) - 2];
100 else
101 if(code >= 16)
102 p = &mpeg3_spBMBtab1[(code >> 2) - 4];
103 else
104 if(code >= 8)
105 p = &mpeg3_spBMBtab2[code - 8];
106 else
108 /* fprintf(stderr,"mpeg3video_getsp_bmb_type: invalid macroblock_type code\n"); */
109 slice->fault = 1;
110 return 0;
113 mpeg3slice_flushbits(slice_buffer, p->len);
114 return p->val;
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))
122 return 1;
125 if(!mpeg3slice_getbit(slice_buffer))
127 /* fprintf(stderr,"mpeg3video_get_imb_type: invalid macroblock_type code\n"); */
128 slice->fault = 1;
131 return 17;
134 static inline int mpeg3video_get_pmb_type(mpeg3_slice_t *slice)
136 int code;
137 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
139 if((code = mpeg3slice_showbits(slice_buffer, 6)) >= 8)
141 code >>= 3;
142 mpeg3slice_flushbits(slice_buffer, mpeg3_PMBtab0[code].len);
143 return mpeg3_PMBtab0[code].val;
146 if(code == 0)
148 /* fprintf(stderr,"mpeg3video_get_pmb_type: invalid macroblock_type code\n"); */
149 slice->fault = 1;
150 return 0;
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)
159 int code;
160 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
162 if((code = mpeg3slice_showbits(slice_buffer, 6)) >= 8)
164 code >>= 2;
165 mpeg3slice_flushbits(slice_buffer, mpeg3_BMBtab0[code].len);
166 return mpeg3_BMBtab0[code].val;
169 if(code == 0)
171 /* fprintf(stderr,"mpeg3video_get_bmb_type: invalid macroblock_type code\n"); */
172 slice->fault = 1;
173 return 0;
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"); */
186 slice->fault=1;
189 return 1;
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);
198 if(code == 0)
200 /* fprintf(stderr,"mpeg3video_get_snrmb_type: invalid macroblock_type code\n"); */
201 slice->fault = 1;
202 return 0;
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);
215 else
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);
223 default:
224 /*fprintf(stderr, "mpeg3video_getmbtype: unknown coding type\n"); */
225 break;
226 /* MPEG-1 only, not implemented */
230 return 0;
233 int mpeg3video_macroblock_modes(mpeg3_slice_t *slice,
234 mpeg3video_t *video,
235 int *pmb_type,
236 int *pstwtype,
237 int *pstwclass,
238 int *pmotion_type,
239 int *pmv_count,
240 int *pmv_format,
241 int *pdmv,
242 int *pmvscale,
243 int *pdct_type)
245 int mb_type;
246 int stwtype, stwcode, stwclass;
247 int motion_type = 0, mv_count, mv_format, dmv, mvscale;
248 int dct_type;
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)
264 stwtype = 4;
265 else
267 stwcode = mpeg3slice_getbits2(slice_buffer);
268 stwtype = stwc_table[video->stwc_table_index - 1][stwcode];
271 else
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);
285 else
287 /* field_motion_type */
288 motion_type = mpeg3slice_getbits2(slice_buffer);
291 else
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;
304 else
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;
321 /* return values */
322 *pmb_type = mb_type;
323 *pstwtype = stwtype;
324 *pstwclass = stwclass;
325 *pmotion_type = motion_type;
326 *pmv_count = mv_count;
327 *pmv_format = mv_format;
328 *pdmv = dmv;
329 *pmvscale = mvscale;
330 *pdct_type = dct_type;
331 return 0;