mask mousewheel segv bug, mask opengl sw fallback read to ram, fix tiff config withou...
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / video / macroblocks.C
1 #include "../libzmpeg3.h"
2
3 int zslice_decoder_t::
4 get_macroblock_address()
5 {
6   int zcode, val = 0;
7   while( (zcode = slice_buffer->show_bits(11)) < 24 ) {
8     if( zcode != 15 ) {     /* Is not macroblock_stuffing */
9       if( zcode != 8 ) {    /* Is not macroblock_escape */
10 //zerrs("invalid macroblock_address_increment code %d\n",zcode);
11         fault = 1;
12         return 1;
13       }
14       val += 33;
15     }
16     slice_buffer->flush_bits(11);
17   }
18
19   if( zcode >= 1024 ) {
20     slice_buffer->flush_bit();
21     return val + 1;
22   }
23
24   if( zcode >= 128 ) {
25     zcode >>= 6;
26     slice_buffer->flush_bits(MBAtab1[zcode].len);
27     return val + MBAtab1[zcode].val;
28   }
29
30   zcode -= 24;
31   slice_buffer->flush_bits(MBAtab2[zcode].len);
32
33   return val + MBAtab2[zcode].val;
34 }
35
36 /* macroblock_type for pictures with spatial scalability */
37 inline int zslice_decoder_t::
38 getsp_imb_type()
39 {
40   uint32_t zcode = slice_buffer->show_bits(4);
41   if( !zcode ) {
42 //zerrs("invalid macroblock_type code %d\n",zcode);
43     fault = 1;
44     return 0;
45   }
46
47   slice_buffer->flush_bits(spIMBtab[zcode].len);
48   return spIMBtab[zcode].val;
49 }
50
51 inline int zslice_decoder_t::
52 getsp_pmb_type()
53 {
54   int zcode = slice_buffer->show_bits(7);
55   if( zcode < 2 ) {
56 //  zerrs("invalid macroblock_type code %d\n",zcode);
57     fault = 1;
58     return 0;
59   }
60
61   if( zcode >= 16 ) {
62     zcode >>= 3;
63     slice_buffer->flush_bits(spPMBtab0[zcode].len);
64
65     return spPMBtab0[zcode].val;
66   }
67
68   slice_buffer->flush_bits(spPMBtab1[zcode].len);
69   return spPMBtab1[zcode].val;
70 }
71
72 inline int zslice_decoder_t::
73 getsp_bmb_type()
74 {
75   VLCtab_t *p;
76   int zcode = slice_buffer->show_bits(9);
77
78   if( zcode >= 64 )
79     p = &spBMBtab0[(zcode >> 5) - 2];
80   else if( zcode >= 16 )
81     p = &spBMBtab1[(zcode >> 2) - 4];
82   else if( zcode >= 8 )  
83     p = &spBMBtab2[zcode - 8];
84   else {
85 //zerrs("invalid macroblock_type code %d\n",zcode);
86     fault = 1;
87     return 0;
88   }
89
90  slice_buffer->flush_bits(p->len);
91  return p->val;
92 }
93
94 inline int zslice_decoder_t::
95 get_imb_type()
96 {
97   if( slice_buffer->get_bit() ) return 1;
98   if( !slice_buffer->get_bit() ) {
99 //zerr("invalid macroblock_type code\n");
100     fault = 1;
101     return 0;
102   }
103
104   return 17;
105 }
106
107 inline int zslice_decoder_t::
108 get_pmb_type()
109 {
110   int zcode;
111   if( (zcode = slice_buffer->show_bits(6)) >= 8 ) {
112     zcode >>= 3;
113     slice_buffer->flush_bits(PMBtab0[zcode].len);
114     return PMBtab0[zcode].val;
115   }
116
117   if( zcode == 0 ) {
118 //zerrs("invalid macroblock_type code %d\n",zcode);
119     fault = 1;
120     return 0;
121   }
122
123   slice_buffer->flush_bits(PMBtab1[zcode].len);
124   return PMBtab1[zcode].val;
125 }
126
127 inline int zslice_decoder_t::
128 get_bmb_type()
129 {
130   int zcode;
131   if( (zcode = slice_buffer->show_bits(6)) >= 8 ) {
132     zcode >>= 2;
133     slice_buffer->flush_bits(BMBtab0[zcode].len);
134     return BMBtab0[zcode].val;
135   }
136
137   if( zcode == 0 ) {
138 //zerrs("invalid macroblock_type code %d\n",zcode);
139     fault = 1;
140     return 0;
141   }
142
143   slice_buffer->flush_bits(BMBtab1[zcode].len);
144   return BMBtab1[zcode].val;
145 }
146
147 inline int zslice_decoder_t::
148 get_dmb_type()
149 {
150   if( !slice_buffer->get_bit() ) {
151 //zerr("invalid macroblock_type code\n");
152     fault=1;
153     return 0;
154   }
155
156   return 1;
157 }
158
159
160 inline int zslice_decoder_t::
161 get_snrmb_type()
162 {
163   int zcode = slice_buffer->show_bits(3);
164   if( zcode == 0 ) {
165 //zerrs("invalid macroblock_type code %d\n",zcode);
166     fault = 1;
167     return 0;
168   }
169   slice_buffer->flush_bits(SNRMBtab[zcode].len);
170   return SNRMBtab[zcode].val;
171 }
172
173 int zslice_decoder_t::
174 get_mb_type()
175 {
176   if( video->scalable_mode == sc_SNR )
177     return get_snrmb_type();
178   switch(video->pict_type) {
179   case video_t::pic_type_I:
180     return video->pict_scal ? getsp_imb_type() : get_imb_type();
181   case video_t::pic_type_P:
182     return video->pict_scal ? getsp_pmb_type() : get_pmb_type();
183   case video_t::pic_type_B:
184     return video->pict_scal ? getsp_bmb_type() : get_bmb_type();
185   case video_t::pic_type_D:
186     return get_dmb_type();
187   default: /* MPEG-1 only, not implemented */
188 //zerrs("unknown coding type %d\n",video->pict_type);
189     break;
190   }
191
192   return 0;
193 }
194
195 int zslice_decoder_t::
196 macroblock_modes(int *pmb_type, int *pstwtype, int *pstwclass, int *pmotion_type,
197     int *pmv_count, int *pmv_format, int *pdmv, int *pmvscale, int *pdct_type)
198 {
199   int mb_type;
200   int stwtype, stwcode, stwclass;
201   int motion_type, mv_count, mv_format, dmv, mvscale;
202   int dct_type;
203   static uint8_t stwc_table[3][4] = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
204   static uint8_t stwclass_table[9] = {0, 1, 2, 1, 1, 2, 3, 3, 4};
205   motion_type = 0;
206
207   /* get macroblock_type */
208   mb_type = get_mb_type();
209   if( fault ) return 1;
210
211   /* get spatial_temporal_weight_code */
212   if( mb_type & mb_WEIGHT ) {
213     if( video->stwc_table_index == 0 )
214       stwtype = 4;
215     else {
216       stwcode = slice_buffer->get_bits(2);
217       stwtype = stwc_table[video->stwc_table_index - 1][stwcode];
218     }
219   }
220   else
221     stwtype = (mb_type & mb_CLASS4) ? 8 : 0;
222
223   /* derive spatial_temporal_weight_class (Table 7-18) */
224   stwclass = stwclass_table[stwtype];
225
226   /* get frame/field motion type */
227   if( mb_type & (mb_FORWARD | mb_BACKWARD) ) {
228     if( video->pict_struct == video_t::pics_FRAME_PICTURE ) { 
229       /* frame_motion_type */
230       motion_type = video->frame_pred_dct ?
231         mc_FRAME : slice_buffer->get_bits(2);
232     }
233     else { 
234      /* field_motion_type */
235      motion_type = slice_buffer->get_bits(2);
236     }
237   }
238   else if( (mb_type & mb_INTRA) && video->conceal_mv ) {
239     /* concealment motion vectors */
240     motion_type = video->pict_struct == video_t::pics_FRAME_PICTURE ?
241       mc_FRAME : mc_FIELD;
242   }
243
244   /* derive mv_count, mv_format and dmv, (table 6-17, 6-18) */
245   if( video->pict_struct == video_t::pics_FRAME_PICTURE ) {
246     mv_count = (motion_type == mc_FIELD && stwclass < 2) ? 2 : 1;
247     mv_format = (motion_type == mc_FRAME) ?  mv_FRAME : mv_FIELD;
248   }
249   else {
250     mv_count = (motion_type == mc_16X8) ? 2 : 1;
251     mv_format = mv_FIELD;
252   }
253
254   dmv = motion_type == mc_DMV ? 1 : 0; /* dual prime */
255
256   /* field mv predictions in frame pictures have to be scaled */
257   mvscale = mv_format == mv_FIELD &&
258     video->pict_struct == video_t::pics_FRAME_PICTURE ? 1 : 0;
259
260   /* get dct_type (frame DCT / field DCT) */
261   dct_type = video->pict_struct == video_t::pics_FRAME_PICTURE && 
262     !video->frame_pred_dct && (mb_type & (mb_PATTERN | mb_INTRA)) ? 
263        slice_buffer->get_bit() : 0;
264
265   /* return values */
266   *pmb_type = mb_type;
267   *pstwtype = stwtype;
268   *pstwclass = stwclass;
269   *pmotion_type = motion_type;
270   *pmv_count = mv_count;
271   *pmv_format = mv_format;
272   *pdmv = dmv;
273   *pmvscale = mvscale;
274   *pdct_type = dct_type;
275   return 0;
276 }