41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 22.05.2002 bs_put_matrix fix |
45 |
|
* 20.05.2002 added BitstreamWriteUserData * |
46 |
|
* 19.06.2002 Fix a little bug in use custom quant matrix * |
47 |
|
* MinChen <chenm001@163.com> * |
48 |
|
* 08.05.2002 add low_delay support for B_VOP decode * |
49 |
|
* MinChen <chenm001@163.com> * |
50 |
|
* 06.05.2002 low_delay * |
51 |
|
* 06.05.2002 fixed fincr/fbase error * |
52 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
53 |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
54 |
* 26.03.2002 interlacing support * |
* 26.03.2002 interlacing support * |
55 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
57 |
* 30.02.2002 intra_dc_threshold support * |
* 30.02.2002 intra_dc_threshold support * |
58 |
* 04.12.2001 support for additional headers * |
* 04.12.2001 support for additional headers * |
59 |
* 16.12.2001 inital version * |
* 16.12.2001 inital version * |
60 |
* * |
* |
61 |
******************************************************************************/ |
******************************************************************************/ |
62 |
|
|
63 |
|
|
66 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
67 |
|
|
68 |
|
|
69 |
static int __inline log2bin(int value) |
static uint32_t __inline |
70 |
|
log2bin(uint32_t value) |
71 |
{ |
{ |
72 |
/* Changed by Chenm001 */ |
/* Changed by Chenm001 */ |
73 |
#ifndef WIN32 |
#ifndef WIN32 |
74 |
int n = 0; |
int n = 0; |
75 |
while (value) |
|
76 |
{ |
while (value) { |
77 |
value >>= 1; |
value >>= 1; |
78 |
n++; |
n++; |
79 |
} |
} |
87 |
} |
} |
88 |
|
|
89 |
|
|
90 |
static const uint32_t intra_dc_threshold_table[] = |
static const uint32_t intra_dc_threshold_table[] = { |
|
{ |
|
91 |
32, /* never use */ |
32, /* never use */ |
92 |
13, |
13, |
93 |
15, |
15, |
99 |
}; |
}; |
100 |
|
|
101 |
|
|
102 |
void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
void |
103 |
|
bs_get_matrix(Bitstream * bs, |
104 |
|
uint8_t * matrix) |
105 |
{ |
{ |
106 |
int i = 0; |
int i = 0; |
107 |
int last, value = 0; |
int last, value = 0; |
108 |
|
|
109 |
do |
do { |
|
{ |
|
110 |
last = value; |
last = value; |
111 |
value = BitstreamGetBits(bs, 8); |
value = BitstreamGetBits(bs, 8); |
112 |
matrix[ scan_tables[0][i++] ] = value; |
matrix[ scan_tables[0][i++] ] = value; |
113 |
} |
} |
114 |
while (value != 0 && i < 64); |
while (value != 0 && i < 64); |
115 |
|
i--; // fix little bug at coeff not full |
116 |
|
|
117 |
while (i < 64) |
while (i < 64) { |
|
{ |
|
118 |
matrix[ scan_tables[0][i++] ] = last; |
matrix[ scan_tables[0][i++] ] = last; |
119 |
} |
} |
120 |
} |
} |
124 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
125 |
*/ |
*/ |
126 |
|
|
127 |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold) |
int |
128 |
|
BitstreamReadHeaders(Bitstream * bs, |
129 |
|
DECODER * dec, |
130 |
|
uint32_t * rounding, |
131 |
|
uint32_t * quant, |
132 |
|
uint32_t * fcode_forward, |
133 |
|
uint32_t * fcode_backward, |
134 |
|
uint32_t * intra_dc_threshold) |
135 |
{ |
{ |
136 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
137 |
uint32_t time_inc_resolution; |
static uint32_t time_increment_resolution; |
138 |
uint32_t coding_type; |
uint32_t coding_type; |
139 |
uint32_t start_code; |
uint32_t start_code; |
140 |
|
uint32_t time_incr = 0; |
141 |
|
int32_t time_increment; |
142 |
|
|
143 |
do |
do { |
|
{ |
|
144 |
BitstreamByteAlign(bs); |
BitstreamByteAlign(bs); |
145 |
start_code = BitstreamShowBits(bs, 32); |
start_code = BitstreamShowBits(bs, 32); |
146 |
|
|
147 |
if (start_code == VISOBJSEQ_START_CODE) |
if (start_code == VISOBJSEQ_START_CODE) { |
148 |
{ |
// DPRINTF("visual_object_sequence"); |
|
// DEBUG("visual_object_sequence"); |
|
149 |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
BitstreamSkip(bs, 32); // visual_object_sequence_start_code |
150 |
BitstreamSkip(bs, 8); // profile_and_level_indication |
BitstreamSkip(bs, 8); // profile_and_level_indication |
151 |
} |
} else if (start_code == VISOBJSEQ_STOP_CODE) { |
|
else if (start_code == VISOBJSEQ_STOP_CODE) |
|
|
{ |
|
152 |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
BitstreamSkip(bs, 32); // visual_object_sequence_stop_code |
153 |
} |
} else if (start_code == VISOBJ_START_CODE) { |
154 |
else if (start_code == VISOBJ_START_CODE) |
//DPRINTF("visual_object"); |
|
{ |
|
|
// DEBUG("visual_object"); |
|
155 |
BitstreamSkip(bs,32); // visual_object_start_code |
BitstreamSkip(bs,32); // visual_object_start_code |
156 |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
if (BitstreamGetBit(bs)) // is_visual_object_identified |
157 |
{ |
{ |
158 |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
vol_ver_id = BitstreamGetBits(bs,4); // visual_object_ver_id |
159 |
BitstreamSkip(bs, 3); // visual_object_priority |
BitstreamSkip(bs, 3); // visual_object_priority |
160 |
} |
} else { |
|
else |
|
|
{ |
|
161 |
vol_ver_id = 1; |
vol_ver_id = 1; |
162 |
} |
} |
163 |
|
|
164 |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type |
165 |
{ |
{ |
166 |
DEBUG("visual_object_type != video"); |
//DPRINTF("visual_object_type != video"); |
167 |
return -1; |
return -1; |
168 |
} |
} |
169 |
BitstreamSkip(bs, 4); |
BitstreamSkip(bs, 4); |
172 |
|
|
173 |
if (BitstreamGetBit(bs)) // video_signal_type |
if (BitstreamGetBit(bs)) // video_signal_type |
174 |
{ |
{ |
175 |
DEBUG("+ video_signal_type"); |
//DPRINTF("+ video_signal_type"); |
176 |
BitstreamSkip(bs, 3); // video_format |
BitstreamSkip(bs, 3); // video_format |
177 |
BitstreamSkip(bs, 1); // video_range |
BitstreamSkip(bs, 1); // video_range |
178 |
if (BitstreamGetBit(bs)) // color_description |
if (BitstreamGetBit(bs)) // color_description |
179 |
{ |
{ |
180 |
DEBUG("+ color_description"); |
//DPRINTF("+ color_description"); |
181 |
BitstreamSkip(bs, 8); // color_primaries |
BitstreamSkip(bs, 8); // color_primaries |
182 |
BitstreamSkip(bs, 8); // transfer_characteristics |
BitstreamSkip(bs, 8); // transfer_characteristics |
183 |
BitstreamSkip(bs, 8); // matrix_coefficients |
BitstreamSkip(bs, 8); // matrix_coefficients |
184 |
} |
} |
185 |
} |
} |
186 |
} |
} else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) { |
|
else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) |
|
|
{ |
|
187 |
BitstreamSkip(bs, 32); // video_object_start_code |
BitstreamSkip(bs, 32); // video_object_start_code |
188 |
} |
} else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) { |
189 |
else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) |
//DPRINTF("video_object_layer"); |
|
{ |
|
|
// DEBUG("video_object_layer"); |
|
190 |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
BitstreamSkip(bs, 32); // video_object_layer_start_code |
191 |
|
|
192 |
BitstreamSkip(bs, 1); // random_accessible_vol |
BitstreamSkip(bs, 1); // random_accessible_vol |
193 |
|
|
194 |
// video_object_type_indication |
// video_object_type_indication |
195 |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && |
if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE && |
|
|
BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN && |
|
|
BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX |
|
196 |
{ |
{ |
197 |
DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8)); |
//DPRINTF("video_object_type_indication %i not supported ", |
198 |
|
// BitstreamShowBits(bs, 8)); |
199 |
return -1; |
return -1; |
200 |
} |
} |
201 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
203 |
|
|
204 |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
if (BitstreamGetBit(bs)) // is_object_layer_identifier |
205 |
{ |
{ |
206 |
DEBUG("+ is_object_layer_identifier"); |
//DPRINTF("+ is_object_layer_identifier"); |
207 |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
vol_ver_id = BitstreamGetBits(bs,4); // video_object_layer_verid |
208 |
BitstreamSkip(bs, 3); // video_object_layer_priority |
BitstreamSkip(bs, 3); // video_object_layer_priority |
209 |
} |
} else { |
|
else |
|
|
{ |
|
210 |
vol_ver_id = 1; |
vol_ver_id = 1; |
211 |
} |
} |
212 |
//DEBUGI("vol_ver_id", vol_ver_id); |
//DPRINTF("vol_ver_id %i", vol_ver_id); |
213 |
|
|
214 |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info |
215 |
{ |
{ |
216 |
DEBUG("+ aspect_ratio_info"); |
//DPRINTF("+ aspect_ratio_info"); |
217 |
BitstreamSkip(bs, 8); // par_width |
BitstreamSkip(bs, 8); // par_width |
218 |
BitstreamSkip(bs, 8); // par_height |
BitstreamSkip(bs, 8); // par_height |
219 |
} |
} |
220 |
|
|
221 |
if (BitstreamGetBit(bs)) // vol_control_parameters |
if (BitstreamGetBit(bs)) // vol_control_parameters |
222 |
{ |
{ |
223 |
DEBUG("+ vol_control_parameters"); |
//DPRINTF("+ vol_control_parameters"); |
224 |
BitstreamSkip(bs, 2); // chroma_format |
BitstreamSkip(bs, 2); // chroma_format |
225 |
BitstreamSkip(bs, 1); // low_delay |
dec->low_delay = BitstreamGetBit(bs); // low_delay |
226 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
227 |
{ |
{ |
228 |
DEBUG("+ vbv_parameters"); |
//DPRINTF("+ vbv_parameters"); |
229 |
BitstreamSkip(bs, 15); // first_half_bitrate |
BitstreamSkip(bs, 15); // first_half_bitrate |
230 |
READ_MARKER(); |
READ_MARKER(); |
231 |
BitstreamSkip(bs, 15); // latter_half_bitrate |
BitstreamSkip(bs, 15); // latter_half_bitrate |
242 |
} |
} |
243 |
|
|
244 |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape |
245 |
// DEBUG1("shape", dec->shape); |
//DPRINTF("shape %i", dec->shape); |
246 |
|
|
247 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { |
|
{ |
|
248 |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
BitstreamSkip(bs, 4); // video_object_layer_shape_extension |
249 |
} |
} |
250 |
|
|
251 |
READ_MARKER(); |
READ_MARKER(); |
252 |
|
|
253 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
254 |
time_inc_resolution--; |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
255 |
if (time_inc_resolution > 0) |
time_increment_resolution--; |
256 |
{ |
//DPRINTF("time_increment_resolution = %i",time_increment_resolution); |
257 |
dec->time_inc_bits = log2bin(time_inc_resolution); |
if (time_increment_resolution > 0) { |
258 |
} |
dec->time_inc_bits = log2bin(time_increment_resolution); |
259 |
else |
} else { |
|
{ |
|
260 |
// dec->time_inc_bits = 0; |
// dec->time_inc_bits = 0; |
261 |
|
|
262 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
// for "old" xvid compatibility, set time_inc_bits = 1 |
270 |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment |
271 |
} |
} |
272 |
|
|
273 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
{ |
|
274 |
|
|
275 |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
{ |
|
276 |
uint32_t width, height; |
uint32_t width, height; |
277 |
|
|
278 |
READ_MARKER(); |
READ_MARKER(); |
279 |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
width = BitstreamGetBits(bs, 13); // video_object_layer_width |
280 |
//DEBUGI("width", width); |
//DPRINTF("width %i", width); |
281 |
READ_MARKER(); |
READ_MARKER(); |
282 |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
height = BitstreamGetBits(bs, 13); // video_object_layer_height |
283 |
//DEBUGI("height", height); |
//DPRINTF("height %i", height); |
284 |
READ_MARKER(); |
READ_MARKER(); |
285 |
|
|
286 |
if (width != dec->width || height != dec->height) |
if (width != dec->width || height != dec->height) { |
287 |
{ |
//DPRINTF("FATAL: video dimension discrepancy ***"); |
288 |
DEBUG("FATAL: video dimension discrepancy ***"); |
//DPRINTF("bitstream width/height %i x %i", width, height); |
289 |
DEBUG2("bitstream width/height", width, height); |
//DPRINTF("param width/height %i x %i", dec->width, dec->height); |
|
DEBUG2("param width/height", dec->width, dec->height); |
|
290 |
return -1; |
return -1; |
291 |
} |
} |
292 |
|
|
293 |
} |
} |
294 |
|
|
295 |
if ((dec->interlacing = BitstreamGetBit(bs))) |
if ((dec->interlacing = BitstreamGetBit(bs))) { |
296 |
{ |
//DPRINTF("vol: interlacing"); |
|
DEBUG("vol: interlacing"); |
|
297 |
} |
} |
298 |
|
|
299 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
300 |
{ |
{ |
301 |
DEBUG("IGNORED/TODO: !obmc_disable"); |
//DPRINTF("IGNORED/TODO: !obmc_disable"); |
302 |
// TODO |
// TODO |
303 |
// fucking divx4.02 has this enabled |
// fucking divx4.02 has this enabled |
304 |
} |
} |
305 |
|
|
306 |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable |
307 |
{ |
{ |
308 |
DEBUG("sprite_enable; not supported"); |
//DPRINTF("sprite_enable; not supported"); |
309 |
return -1; |
return -1; |
310 |
} |
} |
311 |
|
|
312 |
if (vol_ver_id != 1 && dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (vol_ver_id != 1 && |
313 |
{ |
dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
314 |
BitstreamSkip(bs, 1); // sadct_disable |
BitstreamSkip(bs, 1); // sadct_disable |
315 |
} |
} |
316 |
|
|
317 |
if (BitstreamGetBit(bs)) // not_8_bit |
if (BitstreamGetBit(bs)) // not_8_bit |
318 |
{ |
{ |
319 |
DEBUG("+ not_8_bit [IGNORED/TODO]"); |
//DPRINTF("+ not_8_bit [IGNORED/TODO]"); |
320 |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision |
321 |
BitstreamSkip(bs, 4); // bits_per_pixel |
BitstreamSkip(bs, 4); // bits_per_pixel |
322 |
} |
} else { |
|
else |
|
|
{ |
|
323 |
dec->quant_bits = 5; |
dec->quant_bits = 5; |
324 |
} |
} |
325 |
|
|
326 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
|
{ |
|
327 |
BitstreamSkip(bs, 1); // no_gray_quant_update |
BitstreamSkip(bs, 1); // no_gray_quant_update |
328 |
BitstreamSkip(bs, 1); // composition_method |
BitstreamSkip(bs, 1); // composition_method |
329 |
BitstreamSkip(bs, 1); // linear_composition |
BitstreamSkip(bs, 1); // linear_composition |
330 |
} |
} |
331 |
|
|
332 |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
dec->quant_type = BitstreamGetBit(bs); // quant_type |
333 |
// DEBUG1("**** quant_type", dec->quant_type); |
//DPRINTF("**** quant_type %i", dec->quant_type); |
334 |
|
|
335 |
if (dec->quant_type) |
if (dec->quant_type) { |
|
{ |
|
336 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
337 |
{ |
{ |
338 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
339 |
|
|
340 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
341 |
set_intra_matrix(matrix); |
set_intra_matrix(matrix); |
342 |
} |
} else |
|
else |
|
343 |
set_intra_matrix(get_default_intra_matrix()); |
set_intra_matrix(get_default_intra_matrix()); |
344 |
|
|
345 |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
346 |
{ |
{ |
347 |
uint8_t matrix[64]; |
uint8_t matrix[64]; |
348 |
|
|
349 |
bs_get_matrix(bs, matrix); |
bs_get_matrix(bs, matrix); |
350 |
set_inter_matrix(matrix); |
set_inter_matrix(matrix); |
351 |
} |
} else |
|
else |
|
352 |
set_inter_matrix(get_default_inter_matrix()); |
set_inter_matrix(get_default_inter_matrix()); |
353 |
|
|
354 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { |
|
{ |
|
355 |
// TODO |
// TODO |
356 |
DEBUG("TODO: grayscale matrix stuff"); |
//DPRINTF("TODO: grayscale matrix stuff"); |
357 |
return -1; |
return -1; |
358 |
} |
} |
359 |
|
|
360 |
} |
} |
361 |
|
|
362 |
|
|
363 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
|
{ |
|
364 |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe |
dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe |
365 |
if (dec->quarterpel) |
if (dec->quarterpel) { |
366 |
{ |
//DPRINTF("IGNORED/TODO: quarter_sample"); |
|
DEBUG("IGNORED/TODO: quarter_sample"); |
|
|
} |
|
367 |
} |
} |
368 |
else |
} else { |
|
{ |
|
369 |
dec->quarterpel = 0; |
dec->quarterpel = 0; |
370 |
} |
} |
371 |
|
|
372 |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
if (!BitstreamGetBit(bs)) // complexity_estimation_disable |
373 |
{ |
{ |
374 |
DEBUG("TODO: complexity_estimation header"); |
//DPRINTF("TODO: complexity_estimation header"); |
375 |
// TODO |
// TODO |
376 |
return -1; |
return -1; |
377 |
} |
} |
378 |
|
|
379 |
if (!BitstreamGetBit(bs)) // resync_marker_disable |
if (!BitstreamGetBit(bs)) // resync_marker_disable |
380 |
{ |
{ |
381 |
DEBUG("IGNORED/TODO: !resync_marker_disable"); |
//DPRINTF("IGNORED/TODO: !resync_marker_disable"); |
382 |
// TODO |
// TODO |
383 |
} |
} |
384 |
|
|
385 |
if (BitstreamGetBit(bs)) // data_partitioned |
if (BitstreamGetBit(bs)) // data_partitioned |
386 |
{ |
{ |
387 |
DEBUG("+ data_partitioned"); |
//DPRINTF("+ data_partitioned"); |
388 |
BitstreamSkip(bs, 1); // reversible_vlc |
BitstreamSkip(bs, 1); // reversible_vlc |
389 |
} |
} |
390 |
|
|
391 |
if (vol_ver_id != 1) |
if (vol_ver_id != 1) { |
|
{ |
|
392 |
if (BitstreamGetBit(bs)) // newpred_enable |
if (BitstreamGetBit(bs)) // newpred_enable |
393 |
{ |
{ |
394 |
DEBUG("+ newpred_enable"); |
//DPRINTF("+ newpred_enable"); |
395 |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
BitstreamSkip(bs, 2); // requested_upstream_message_type |
396 |
BitstreamSkip(bs, 1); // newpred_segment_type |
BitstreamSkip(bs, 1); // newpred_segment_type |
397 |
} |
} |
398 |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable |
399 |
{ |
{ |
400 |
DEBUG("TODO: reduced_resolution_vop_enable"); |
//DPRINTF("TODO: reduced_resolution_vop_enable"); |
401 |
// TODO |
// TODO |
402 |
return -1; |
return -1; |
403 |
} |
} |
404 |
} |
} |
405 |
|
|
406 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability = BitstreamGetBit(bs))) // scalability |
407 |
{ |
{ |
408 |
// TODO |
// TODO |
409 |
DEBUG("TODO: scalability"); |
//DPRINTF("TODO: scalability"); |
410 |
return -1; |
return -1; |
411 |
} |
} |
412 |
} |
} else // dec->shape == BINARY_ONLY |
|
else // dec->shape == BINARY_ONLY |
|
|
{ |
|
|
if (vol_ver_id != 1) |
|
413 |
{ |
{ |
414 |
|
if (vol_ver_id != 1) { |
415 |
if (BitstreamGetBit(bs)) // scalability |
if (BitstreamGetBit(bs)) // scalability |
416 |
{ |
{ |
417 |
// TODO |
// TODO |
418 |
DEBUG("TODO: scalability"); |
//DPRINTF("TODO: scalability"); |
419 |
return -1; |
return -1; |
420 |
} |
} |
421 |
} |
} |
423 |
|
|
424 |
} |
} |
425 |
|
|
426 |
} |
} else if (start_code == GRPOFVOP_START_CODE) { |
427 |
else if (start_code == GRPOFVOP_START_CODE) |
//DPRINTF("group_of_vop"); |
|
{ |
|
|
// DEBUG("group_of_vop"); |
|
428 |
BitstreamSkip(bs, 32); |
BitstreamSkip(bs, 32); |
429 |
{ |
{ |
430 |
int hours, minutes, seconds; |
int hours, minutes, seconds; |
431 |
|
|
432 |
hours = BitstreamGetBits(bs, 5); |
hours = BitstreamGetBits(bs, 5); |
433 |
minutes = BitstreamGetBits(bs, 6); |
minutes = BitstreamGetBits(bs, 6); |
434 |
READ_MARKER(); |
READ_MARKER(); |
435 |
seconds = BitstreamGetBits(bs, 6); |
seconds = BitstreamGetBits(bs, 6); |
436 |
// DEBUG3("hms", hours, minutes, seconds); |
//DPRINTF("%ih %im %is", hours, minutes, seconds); |
437 |
} |
} |
438 |
BitstreamSkip(bs, 1); // closed_gov |
BitstreamSkip(bs, 1); // closed_gov |
439 |
BitstreamSkip(bs, 1); // broken_link |
BitstreamSkip(bs, 1); // broken_link |
440 |
} |
} else if (start_code == VOP_START_CODE) { |
441 |
else if (start_code == VOP_START_CODE) |
//DPRINTF("vop_start_code"); |
|
{ |
|
|
// DEBUG("vop_start_code"); |
|
442 |
BitstreamSkip(bs, 32); // vop_start_code |
BitstreamSkip(bs, 32); // vop_start_code |
443 |
|
|
444 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
445 |
//DEBUG1("coding_type", coding_type); |
//DPRINTF("coding_type %i", coding_type); |
446 |
|
|
447 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
448 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
449 |
|
time_incr++; |
450 |
|
|
451 |
READ_MARKER(); |
READ_MARKER(); |
452 |
|
|
453 |
//DEBUG1("time_inc_bits", dec->time_inc_bits); |
//DPRINTF("time_inc_bits %i", dec->time_inc_bits); |
454 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
//DPRINTF("vop_time_incr %i", BitstreamShowBits(bs, dec->time_inc_bits)); |
455 |
if (dec->time_inc_bits) |
if (dec->time_inc_bits) { |
456 |
{ |
//BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
457 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
458 |
|
} |
459 |
|
if (coding_type != B_VOP) { |
460 |
|
dec->last_time_base = dec->time_base; |
461 |
|
dec->time_base += time_incr; |
462 |
|
dec->time = |
463 |
|
dec->time_base * time_increment_resolution + |
464 |
|
time_increment; |
465 |
|
dec->time_pp = (uint32_t) (dec->time - dec->last_non_b_time); |
466 |
|
dec->last_non_b_time = dec->time; |
467 |
|
} else { |
468 |
|
dec->time = |
469 |
|
(dec->last_time_base + |
470 |
|
time_incr) * time_increment_resolution + time_increment; |
471 |
|
dec->time_bp = (uint32_t) (dec->last_non_b_time - dec->time); |
472 |
} |
} |
473 |
|
//DPRINTF("time_increment %i",time_increment); |
474 |
|
|
475 |
READ_MARKER(); |
READ_MARKER(); |
476 |
|
|
484 |
} |
} |
485 |
*/ |
*/ |
486 |
|
|
487 |
if (coding_type != I_VOP) |
// fix a little bug by MinChen <chenm002@163.com> |
488 |
{ |
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && |
489 |
|
(coding_type == P_VOP)) { |
490 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
491 |
//DEBUG1("rounding", *rounding); |
//DPRINTF("rounding %i", *rounding); |
492 |
} |
} |
493 |
|
|
494 |
/* if (reduced_resolution_enable) |
/* if (reduced_resolution_enable) |
496 |
} |
} |
497 |
*/ |
*/ |
498 |
|
|
499 |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) |
if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { |
|
{ |
|
500 |
uint32_t width, height; |
uint32_t width, height; |
501 |
uint32_t horiz_mc_ref, vert_mc_ref; |
uint32_t horiz_mc_ref, vert_mc_ref; |
502 |
|
|
509 |
vert_mc_ref = BitstreamGetBits(bs, 13); |
vert_mc_ref = BitstreamGetBits(bs, 13); |
510 |
READ_MARKER(); |
READ_MARKER(); |
511 |
|
|
512 |
// DEBUG2("vop_width/height", width, height); |
//DPRINTF("vop_width/height %i x %i", width, height); |
513 |
// DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); |
//DPRINTF("ref %i x %i", horiz_mc_ref, vert_mc_ref); |
514 |
|
|
515 |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
BitstreamSkip(bs, 1); // change_conv_ratio_disable |
516 |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
if (BitstreamGetBit(bs)) // vop_constant_alpha |
520 |
} |
} |
521 |
|
|
522 |
|
|
523 |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) |
if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { |
|
{ |
|
524 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
525 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = |
526 |
|
intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; |
527 |
|
|
528 |
if (dec->interlacing) |
if (dec->interlacing) { |
529 |
{ |
if ((dec->top_field_first = BitstreamGetBit(bs))) { |
530 |
if ((dec->top_field_first = BitstreamGetBit(bs))) |
//DPRINTF("vop: top_field_first"); |
|
{ |
|
|
DEBUG("vop: top_field_first"); |
|
531 |
} |
} |
532 |
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) { |
533 |
{ |
//DPRINTF("vop: alternate_vertical_scan"); |
|
DEBUG("vop: alternate_vertical_scan"); |
|
534 |
} |
} |
535 |
} |
} |
536 |
} |
} |
537 |
|
|
538 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
539 |
//DEBUG1("quant", *quant); |
*quant = 1; |
540 |
|
|
541 |
if (coding_type != I_VOP) |
//DPRINTF("quant %i", *quant); |
542 |
{ |
|
543 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
if (coding_type != I_VOP) { |
544 |
|
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
545 |
} |
} |
546 |
|
|
547 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) { |
548 |
{ |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
|
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
|
549 |
} |
} |
550 |
return coding_type; |
if (!dec->scalability) { |
551 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && |
552 |
|
(coding_type != I_VOP)) { |
553 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
554 |
} |
} |
|
else if (start_code == USERDATA_START_CODE) |
|
|
{ |
|
|
// DEBUG("user_data"); |
|
|
BitstreamSkip(bs, 32); // user_data_start_code |
|
555 |
} |
} |
556 |
else // start_code == ? |
return coding_type; |
557 |
{ |
} else if (start_code == USERDATA_START_CODE) { |
558 |
if (BitstreamShowBits(bs, 24) == 0x000001) |
//DPRINTF("user_data"); |
559 |
|
BitstreamSkip(bs, 32); // user_data_start_code |
560 |
|
} else // start_code == ? |
561 |
{ |
{ |
562 |
DEBUG1("*** WARNING: unknown start_code", BitstreamShowBits(bs, 32)); |
if (BitstreamShowBits(bs, 24) == 0x000001) { |
563 |
|
//DPRINTF("*** WARNING: unknown start_code %x", |
564 |
|
// BitstreamShowBits(bs, 32)); |
565 |
} |
} |
566 |
BitstreamSkip(bs, 8); |
BitstreamSkip(bs, 8); |
567 |
} |
} |
568 |
} |
} |
569 |
while ((BitstreamPos(bs) >> 3) < bs->length); |
while ((BitstreamPos(bs) >> 3) < bs->length); |
570 |
|
|
571 |
DEBUG("*** WARNING: no vop_start_code found"); |
//DPRINTF("*** WARNING: no vop_start_code found"); |
572 |
return -1; /* ignore it */ |
return -1; /* ignore it */ |
573 |
} |
} |
574 |
|
|
575 |
|
|
576 |
/* write custom quant matrix */ |
/* write custom quant matrix */ |
577 |
|
|
578 |
static void bs_put_matrix(Bitstream * bs, const int16_t *matrix) |
static void |
579 |
|
bs_put_matrix(Bitstream * bs, |
580 |
|
const int16_t * matrix) |
581 |
{ |
{ |
582 |
int i, j; |
int i, j; |
583 |
const int last = matrix[scan_tables[0][63]]; |
const int last = matrix[scan_tables[0][63]]; |
584 |
|
|
585 |
for (j = 63; j >= 0 && matrix[scan_tables[0][j - 1]] == last; j--) ; |
for (j = 63; j > 0 && matrix[scan_tables[0][j - 1]] == last; j--); |
586 |
|
|
587 |
for (i = 0; i <= j; i++) |
for (i = 0; i <= j; i++) { |
|
{ |
|
588 |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); |
589 |
} |
} |
590 |
|
|
591 |
if (j < 63) |
if (j < 63) { |
|
{ |
|
592 |
BitstreamPutBits(bs, 0, 8); |
BitstreamPutBits(bs, 0, 8); |
593 |
} |
} |
594 |
} |
} |
597 |
/* |
/* |
598 |
write vol header |
write vol header |
599 |
*/ |
*/ |
600 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void |
601 |
const MBParam * pParam) |
BitstreamWriteVolHeader(Bitstream * const bs, |
602 |
|
const MBParam * pParam, |
603 |
|
const FRAMEINFO * frame) |
604 |
{ |
{ |
605 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
606 |
BitstreamPad(bs); |
BitstreamPad(bs); |
615 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
616 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
617 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
618 |
BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) |
|
619 |
|
#ifdef BFRAMES |
620 |
|
if (pParam->max_bframes > 0) { |
621 |
|
//DPRINTF("low_delay=1"); |
622 |
|
BitstreamPutBit(bs, 1); // vol_control_parameters |
623 |
|
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
624 |
|
BitstreamPutBit(bs, 0); // low_delay |
625 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
626 |
|
} else |
627 |
|
#endif |
628 |
|
{ |
629 |
|
BitstreamPutBits(bs, 0, 1); // vol_control_parameters (0=not given) |
630 |
|
} |
631 |
|
|
632 |
|
|
633 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
634 |
|
|
635 |
WRITE_MARKER(); |
WRITE_MARKER(); |
639 |
25fps res=25 inc=1 |
25fps res=25 inc=1 |
640 |
29.97fps res=30000 inc=1001 |
29.97fps res=30000 inc=1001 |
641 |
*/ |
*/ |
642 |
|
#ifdef BFRAMES |
643 |
|
BitstreamPutBits(bs, pParam->fbase, 16); |
644 |
|
#else |
645 |
BitstreamPutBits(bs, 2, 16); |
BitstreamPutBits(bs, 2, 16); |
646 |
|
#endif |
647 |
|
|
648 |
WRITE_MARKER(); |
WRITE_MARKER(); |
649 |
|
|
659 |
BitstreamPutBits(bs, pParam->height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
660 |
WRITE_MARKER(); |
WRITE_MARKER(); |
661 |
|
|
662 |
BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
663 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
664 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
665 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
666 |
|
|
667 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
668 |
BitstreamPutBit(bs, pParam->quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
669 |
|
|
670 |
if (pParam->quant_type) |
if (pParam->m_quant_type) { |
|
{ |
|
671 |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
672 |
if (get_intra_matrix_status()) |
if (get_intra_matrix_status()) { |
|
{ |
|
673 |
bs_put_matrix(bs, get_intra_matrix()); |
bs_put_matrix(bs, get_intra_matrix()); |
674 |
} |
} |
675 |
|
|
676 |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
677 |
if (get_inter_matrix_status()) |
if (get_inter_matrix_status()) { |
|
{ |
|
678 |
bs_put_matrix(bs, get_inter_matrix()); |
bs_put_matrix(bs, get_inter_matrix()); |
679 |
} |
} |
680 |
|
|
695 |
time_inc = nth of a second since last resync |
time_inc = nth of a second since last resync |
696 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
697 |
*/ |
*/ |
698 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void |
699 |
const MBParam * pParam) |
BitstreamWriteVopHeader(Bitstream * const bs, |
700 |
|
const MBParam * pParam, |
701 |
|
const FRAMEINFO * frame, |
702 |
|
int vop_coded) |
703 |
{ |
{ |
704 |
|
#ifdef BFRAMES |
705 |
|
uint32_t i; |
706 |
|
#endif |
707 |
BitstreamPad(bs); |
BitstreamPad(bs); |
708 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
709 |
|
|
710 |
BitstreamPutBits(bs, pParam->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
711 |
|
|
712 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
713 |
|
#ifdef BFRAMES |
714 |
|
for (i = 0; i < frame->seconds; i++) { |
715 |
|
BitstreamPutBit(bs, 1); |
716 |
|
} |
717 |
|
BitstreamPutBit(bs, 0); |
718 |
|
#else |
719 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
720 |
|
#endif |
721 |
|
|
722 |
WRITE_MARKER(); |
WRITE_MARKER(); |
723 |
|
|
724 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
725 |
|
#ifdef BFRAMES |
726 |
|
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
727 |
|
DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks, |
728 |
|
frame->coding_type == I_VOP ? 'I' : frame->coding_type == |
729 |
|
P_VOP ? 'P' : 'B'); |
730 |
|
#else |
731 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
732 |
|
#endif |
733 |
|
|
734 |
WRITE_MARKER(); |
WRITE_MARKER(); |
735 |
|
|
736 |
|
if (!vop_coded) { |
737 |
|
BitstreamPutBits(bs, 0, 1); |
738 |
|
return; |
739 |
|
} |
740 |
|
|
741 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
742 |
|
|
743 |
if (pParam->coding_type != I_VOP) |
if (frame->coding_type == P_VOP) |
744 |
BitstreamPutBits(bs, pParam->rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
745 |
|
|
746 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
747 |
|
|
748 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
749 |
BitstreamPutBit(bs, 1); // top field first |
BitstreamPutBit(bs, 1); // top field first |
750 |
BitstreamPutBit(bs, 0); // alternate vertical scan |
BitstreamPutBit(bs, 0); // alternate vertical scan |
751 |
} |
} |
752 |
|
|
753 |
BitstreamPutBits(bs, pParam->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
754 |
|
|
755 |
|
if (frame->coding_type != I_VOP) |
756 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
757 |
|
|
758 |
|
if (frame->coding_type == B_VOP) |
759 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
760 |
|
|
761 |
|
} |
762 |
|
|
763 |
|
|
764 |
|
void |
765 |
|
BitstreamWriteUserData(Bitstream * const bs, |
766 |
|
uint8_t * data, |
767 |
|
const int length) |
768 |
|
{ |
769 |
|
int i; |
770 |
|
|
771 |
|
BitstreamPad(bs); |
772 |
|
BitstreamPutBits(bs, USERDATA_START_CODE, 32); |
773 |
|
|
774 |
|
for (i = 0; i < length; i++) { |
775 |
|
BitstreamPutBits(bs, data[i], 8); |
776 |
|
} |
777 |
|
|
|
if (pParam->coding_type != I_VOP) |
|
|
BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] |
|
778 |
} |
} |