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