41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
45 |
|
* 26.03.2002 interlacing support * |
46 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
47 |
* 03.03.2002 merged BITREADER and BITWRITER * |
* 03.03.2002 merged BITREADER and BITWRITER * |
48 |
* 30.02.2002 intra_dc_threshold support * |
* 30.02.2002 intra_dc_threshold support * |
54 |
|
|
55 |
#include "bitstream.h" |
#include "bitstream.h" |
56 |
#include "zigzag.h" |
#include "zigzag.h" |
57 |
|
#include "../quant/quant_matrix.h" |
58 |
|
|
59 |
|
|
60 |
static int __inline log2bin(int value) |
static int __inline log2bin(int value) |
61 |
{ |
{ |
62 |
|
/* Changed by Chenm001 */ |
63 |
|
#ifndef WIN32 |
64 |
int n = 0; |
int n = 0; |
65 |
while (value) |
while (value) |
66 |
{ |
{ |
68 |
n++; |
n++; |
69 |
} |
} |
70 |
return n; |
return n; |
71 |
|
#else |
72 |
|
__asm{ |
73 |
|
bsr eax,value |
74 |
|
inc eax |
75 |
|
} |
76 |
|
#endif |
77 |
} |
} |
78 |
|
|
79 |
|
|
90 |
}; |
}; |
91 |
|
|
92 |
|
|
93 |
|
void bs_get_matrix(Bitstream * bs, uint8_t * matrix) |
94 |
|
{ |
95 |
|
int i = 0; |
96 |
|
int last, value = 0; |
97 |
|
|
98 |
|
do |
99 |
|
{ |
100 |
|
last = value; |
101 |
|
value = BitstreamGetBits(bs, 8); |
102 |
|
matrix[ scan_tables[0][i++] ] = value; |
103 |
|
} |
104 |
|
while (value != 0 && i < 64); |
105 |
|
|
106 |
|
while (i < 64) |
107 |
|
{ |
108 |
|
matrix[ scan_tables[0][i++] ] = last; |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
/* |
/* |
113 |
decode headers |
decode headers |
114 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
294 |
|
|
295 |
} |
} |
296 |
|
|
297 |
if (BitstreamGetBit(bs)) // interlaced |
if ((dec->interlacing = BitstreamGetBit(bs))) |
298 |
{ |
{ |
299 |
DEBUG("TODO: interlaced"); |
DEBUG("vol: interlacing"); |
|
// TODO |
|
|
return -1; |
|
300 |
} |
} |
301 |
|
|
302 |
if (!BitstreamGetBit(bs)) // obmc_disable |
if (!BitstreamGetBit(bs)) // obmc_disable |
342 |
{ |
{ |
343 |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
if (BitstreamGetBit(bs)) // load_intra_quant_mat |
344 |
{ |
{ |
345 |
DEBUG("TODO: load_intra_quant_mat"); |
uint8_t matrix[64]; |
346 |
// TODO |
bs_get_matrix(bs, matrix); |
347 |
return -1; |
set_intra_matrix(matrix); |
348 |
} |
} |
349 |
|
else |
350 |
|
set_intra_matrix(get_default_intra_matrix()); |
351 |
|
|
352 |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
if (BitstreamGetBit(bs)) // load_inter_quant_mat |
353 |
{ |
{ |
354 |
DEBUG("TODO: load_inter_quant_mat"); |
uint8_t matrix[64]; |
355 |
// TODO |
bs_get_matrix(bs, matrix); |
356 |
return -1; |
set_inter_matrix(matrix); |
357 |
} |
} |
358 |
|
else |
359 |
|
set_inter_matrix(get_default_inter_matrix()); |
360 |
|
|
361 |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) |
362 |
{ |
{ |
364 |
DEBUG("TODO: grayscale matrix stuff"); |
DEBUG("TODO: grayscale matrix stuff"); |
365 |
return -1; |
return -1; |
366 |
} |
} |
367 |
|
|
368 |
} |
} |
369 |
|
|
370 |
|
|
526 |
// intra_dc_vlc_threshold |
// intra_dc_vlc_threshold |
527 |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
*intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ]; |
528 |
|
|
529 |
/* if (interlaced) |
if (dec->interlacing) |
530 |
{ |
{ |
531 |
BitstreamSkip(bs, 1); // top_field_first |
if ((dec->top_field_first = BitstreamGetBit(bs))) |
532 |
BitstreamSkip(bs, 1); // alternative_vertical_scan_flag |
{ |
533 |
*/ |
DEBUG("vop: top_field_first"); |
534 |
|
} |
535 |
|
if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) |
536 |
|
{ |
537 |
|
DEBUG("vop: alternate_vertical_scan"); |
538 |
|
} |
539 |
|
} |
540 |
} |
} |
541 |
|
|
542 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
599 |
write vol header |
write vol header |
600 |
*/ |
*/ |
601 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
602 |
const int width, |
const MBParam * pParam) |
|
const int height, |
|
|
const int quant_type) |
|
603 |
{ |
{ |
604 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
605 |
BitstreamPad(bs); |
BitstreamPad(bs); |
635 |
// BitstreamPutBits(bs, 0, 15); |
// BitstreamPutBits(bs, 0, 15); |
636 |
|
|
637 |
WRITE_MARKER(); |
WRITE_MARKER(); |
638 |
BitstreamPutBits(bs, width, 13); // width |
BitstreamPutBits(bs, pParam->width, 13); // width |
639 |
WRITE_MARKER(); |
WRITE_MARKER(); |
640 |
BitstreamPutBits(bs, height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
641 |
WRITE_MARKER(); |
WRITE_MARKER(); |
642 |
|
|
643 |
BitstreamPutBit(bs, 0); // interlace |
BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace |
644 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
645 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
646 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
647 |
|
|
648 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
649 |
BitstreamPutBit(bs, quant_type); |
BitstreamPutBit(bs, pParam->quant_type); |
650 |
/* |
|
651 |
if (quant_type) |
if (pParam->quant_type) |
652 |
{ |
{ |
653 |
BitstreamPutBit(bs, qmatrix->custom_intra); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
654 |
if (qmatrix->custom_intra) |
if (get_intra_matrix_status()) |
655 |
{ |
{ |
656 |
bs_put_matrix(bs, qmatrix->intra); |
bs_put_matrix(bs, get_intra_matrix()); |
657 |
} |
} |
658 |
|
|
659 |
BitstreamPutBit(bs, qmatrix->custom_inter); // load_inter_quant_mat |
BitstreamPutBit(bs, get_inter_matrix_status()); // load_inter_quant_mat |
660 |
if (qmatrix->custom_inter) |
if (get_inter_matrix_status()) |
661 |
{ |
{ |
662 |
bs_put_matrix(bs, qmatrix->inter); |
bs_put_matrix(bs, get_inter_matrix()); |
663 |
} |
} |
664 |
|
|
665 |
} |
} |
|
*/ |
|
|
if (quant_type) |
|
|
{ |
|
|
BitstreamPutBit(bs, 0); // load_intra_quant_mat |
|
|
BitstreamPutBit(bs, 0); // load_inter_quant_mat |
|
|
} |
|
666 |
|
|
667 |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
BitstreamPutBit(bs, 1); // complexity_estimation_disable |
668 |
BitstreamPutBit(bs, 1); // resync_marker_disable |
BitstreamPutBit(bs, 1); // resync_marker_disable |
680 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
681 |
*/ |
*/ |
682 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
683 |
VOP_TYPE prediction_type, |
const MBParam * pParam) |
|
const int rounding_type, |
|
|
const uint32_t quant, |
|
|
const uint32_t fcode) |
|
684 |
{ |
{ |
685 |
BitstreamPad(bs); |
BitstreamPad(bs); |
686 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
687 |
|
|
688 |
BitstreamPutBits(bs, prediction_type, 2); |
BitstreamPutBits(bs, pParam->coding_type, 2); |
689 |
|
|
690 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
691 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
699 |
|
|
700 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
701 |
|
|
702 |
if (prediction_type != I_VOP) |
if (pParam->coding_type != I_VOP) |
703 |
BitstreamPutBits(bs, rounding_type, 1); |
BitstreamPutBits(bs, pParam->rounding_type, 1); |
704 |
|
|
705 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
706 |
|
|
707 |
BitstreamPutBits(bs, quant, 5); // quantizer |
if (pParam->global_flags & XVID_INTERLACING) |
708 |
|
{ |
709 |
|
BitstreamPutBit(bs, 1); // top field first |
710 |
|
BitstreamPutBit(bs, 0); // alternate vertical scan |
711 |
|
} |
712 |
|
|
713 |
|
BitstreamPutBits(bs, pParam->quant, 5); // quantizer |
714 |
|
|
715 |
if (prediction_type != I_VOP) |
if (pParam->coding_type != I_VOP) |
716 |
BitstreamPutBits(bs, fcode, 3); // fixed_code = [1,4] |
BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] |
717 |
} |
} |