41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 08.05.2002 add low_delay support for B_VOP decode * |
45 |
|
* MinChen <chenm001@163.com> * |
46 |
|
* 06.05.2002 low_delay * |
47 |
|
* 06.05.2002 fixed fincr/fbase error * |
48 |
|
* 01.05.2002 added BVOP support to BitstreamWriteVopHeader * |
49 |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
* 15.04.2002 rewrite log2bin use asm386 By MinChen <chenm001@163.com> * |
50 |
* 26.03.2002 interlacing support * |
* 26.03.2002 interlacing support * |
51 |
* 03.03.2002 qmatrix writing * |
* 03.03.2002 qmatrix writing * |
62 |
#include "../quant/quant_matrix.h" |
#include "../quant/quant_matrix.h" |
63 |
|
|
64 |
|
|
65 |
static int __inline log2bin(int value) |
static uint32_t __inline log2bin(uint32_t value) |
66 |
{ |
{ |
67 |
/* Changed by Chenm001 */ |
/* Changed by Chenm001 */ |
68 |
#ifndef WIN32 |
#ifndef WIN32 |
119 |
returns coding_type, or -1 if error |
returns coding_type, or -1 if error |
120 |
*/ |
*/ |
121 |
|
|
122 |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode, uint32_t * intra_dc_threshold) |
int BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold) |
123 |
{ |
{ |
124 |
uint32_t vol_ver_id; |
uint32_t vol_ver_id; |
125 |
uint32_t time_inc_resolution; |
static uint32_t time_increment_resolution; |
126 |
uint32_t coding_type; |
uint32_t coding_type; |
127 |
uint32_t start_code; |
uint32_t start_code; |
128 |
|
uint32_t time_incr=0; |
129 |
|
int32_t time_increment; |
130 |
|
|
131 |
do |
do |
132 |
{ |
{ |
226 |
{ |
{ |
227 |
DEBUG("+ vol_control_parameters"); |
DEBUG("+ vol_control_parameters"); |
228 |
BitstreamSkip(bs, 2); // chroma_format |
BitstreamSkip(bs, 2); // chroma_format |
229 |
BitstreamSkip(bs, 1); // low_delay |
dec->low_delay=BitstreamGetBit(bs); // low_delay |
230 |
if (BitstreamGetBit(bs)) // vbv_parameters |
if (BitstreamGetBit(bs)) // vbv_parameters |
231 |
{ |
{ |
232 |
DEBUG("+ vbv_parameters"); |
DEBUG("+ vbv_parameters"); |
255 |
|
|
256 |
READ_MARKER(); |
READ_MARKER(); |
257 |
|
|
258 |
time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
// *************************** for decode B-frame time *********************** |
259 |
time_inc_resolution--; |
time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution |
260 |
if (time_inc_resolution > 0) |
time_increment_resolution--; |
261 |
|
//DEBUG1("time_increment_resolution=",time_increment_resolution); |
262 |
|
if (time_increment_resolution > 0) |
263 |
{ |
{ |
264 |
dec->time_inc_bits = log2bin(time_inc_resolution); |
dec->time_inc_bits = log2bin(time_increment_resolution); |
265 |
} |
} |
266 |
else |
else |
267 |
{ |
{ |
425 |
} |
} |
426 |
} |
} |
427 |
|
|
428 |
if (BitstreamGetBit(bs)) // scalability |
if ((dec->scalability=BitstreamGetBit(bs))) // scalability |
429 |
{ |
{ |
430 |
// TODO |
// TODO |
431 |
DEBUG("TODO: scalability"); |
DEBUG("TODO: scalability"); |
471 |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
coding_type = BitstreamGetBits(bs, 2); // vop_coding_type |
472 |
//DEBUG1("coding_type", coding_type); |
//DEBUG1("coding_type", coding_type); |
473 |
|
|
474 |
while (BitstreamGetBit(bs) != 0) ; // time_base |
// *************************** for decode B-frame time *********************** |
475 |
|
while (BitstreamGetBit(bs) != 0) // time_base |
476 |
|
time_incr++; |
477 |
|
|
478 |
READ_MARKER(); |
READ_MARKER(); |
479 |
|
|
481 |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); |
482 |
if (dec->time_inc_bits) |
if (dec->time_inc_bits) |
483 |
{ |
{ |
484 |
BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
//BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment |
485 |
|
time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment |
486 |
|
} |
487 |
|
if(coding_type != B_VOP){ |
488 |
|
dec->last_time_base = dec->time_base; |
489 |
|
dec->time_base += time_incr; |
490 |
|
dec->time = dec->time_base*time_increment_resolution + time_increment; |
491 |
|
dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time); |
492 |
|
dec->last_non_b_time= dec->time; |
493 |
|
}else{ |
494 |
|
dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment; |
495 |
|
dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time); |
496 |
} |
} |
497 |
|
//DEBUG1("time_increment=",time_increment); |
498 |
|
|
499 |
READ_MARKER(); |
READ_MARKER(); |
500 |
|
|
508 |
} |
} |
509 |
*/ |
*/ |
510 |
|
|
511 |
if (coding_type != I_VOP) |
// fix a little bug by MinChen <chenm002@163.com> |
512 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) |
513 |
{ |
{ |
514 |
*rounding = BitstreamGetBit(bs); // rounding_type |
*rounding = BitstreamGetBit(bs); // rounding_type |
515 |
//DEBUG1("rounding", *rounding); |
//DEBUG1("rounding", *rounding); |
563 |
} |
} |
564 |
} |
} |
565 |
|
|
566 |
*quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant |
if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant |
567 |
|
*quant = 1; |
568 |
|
|
569 |
//DEBUG1("quant", *quant); |
//DEBUG1("quant", *quant); |
570 |
|
|
571 |
if (coding_type != I_VOP) |
if (coding_type != I_VOP) |
572 |
{ |
{ |
573 |
*fcode = BitstreamGetBits(bs, 3); // fcode_forward |
*fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward |
574 |
} |
} |
575 |
|
|
576 |
if (coding_type == B_VOP) |
if (coding_type == B_VOP) |
577 |
{ |
{ |
578 |
// *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
*fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward |
579 |
|
} |
580 |
|
if (!dec->scalability){ |
581 |
|
if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){ |
582 |
|
BitstreamSkip(bs, 1); // vop_shape_coding_type |
583 |
|
} |
584 |
} |
} |
585 |
return coding_type; |
return coding_type; |
586 |
} |
} |
630 |
write vol header |
write vol header |
631 |
*/ |
*/ |
632 |
void BitstreamWriteVolHeader(Bitstream * const bs, |
void BitstreamWriteVolHeader(Bitstream * const bs, |
633 |
const MBParam * pParam) |
const MBParam * pParam, const FRAMEINFO * frame) |
634 |
{ |
{ |
635 |
// video object_start_code & vo_id |
// video object_start_code & vo_id |
636 |
BitstreamPad(bs); |
BitstreamPad(bs); |
645 |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
BitstreamPutBits(bs, 0, 8); // video_object_type_indication |
646 |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) |
647 |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) |
648 |
BitstreamPutBit(bs, 0); // vol_control_parameters (0=not given) |
|
649 |
|
#ifdef BFRAMES |
650 |
|
if (pParam->max_bframes > 0) |
651 |
|
{ |
652 |
|
dprintf("low_delay=1"); |
653 |
|
BitstreamPutBit(bs, 1); // vol_control_parameters |
654 |
|
BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" |
655 |
|
BitstreamPutBit(bs, 0); // low_delay |
656 |
|
BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) |
657 |
|
} |
658 |
|
else |
659 |
|
#endif |
660 |
|
{ |
661 |
|
BitstreamPutBits(bs, 0, 1); // vol_control_parameters (0=not given) |
662 |
|
} |
663 |
|
|
664 |
|
|
665 |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) |
666 |
|
|
667 |
WRITE_MARKER(); |
WRITE_MARKER(); |
671 |
25fps res=25 inc=1 |
25fps res=25 inc=1 |
672 |
29.97fps res=30000 inc=1001 |
29.97fps res=30000 inc=1001 |
673 |
*/ |
*/ |
674 |
|
#ifdef BFRAMES |
675 |
|
BitstreamPutBits(bs, pParam->fbase, 16); |
676 |
|
#else |
677 |
BitstreamPutBits(bs, 2, 16); |
BitstreamPutBits(bs, 2, 16); |
678 |
|
#endif |
679 |
|
|
680 |
WRITE_MARKER(); |
WRITE_MARKER(); |
681 |
|
|
691 |
BitstreamPutBits(bs, pParam->height, 13); // height |
BitstreamPutBits(bs, pParam->height, 13); // height |
692 |
WRITE_MARKER(); |
WRITE_MARKER(); |
693 |
|
|
694 |
BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace |
BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace |
695 |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) |
696 |
BitstreamPutBit(bs, 0); // sprite_enable |
BitstreamPutBit(bs, 0); // sprite_enable |
697 |
BitstreamPutBit(bs, 0); // not_in_bit |
BitstreamPutBit(bs, 0); // not_in_bit |
698 |
|
|
699 |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
// quant_type 0=h.263 1=mpeg4(quantizer tables) |
700 |
BitstreamPutBit(bs, pParam->quant_type); |
BitstreamPutBit(bs, pParam->m_quant_type); |
701 |
|
|
702 |
if (pParam->quant_type) |
if (pParam->m_quant_type) |
703 |
{ |
{ |
704 |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat |
705 |
if (get_intra_matrix_status()) |
if (get_intra_matrix_status()) |
731 |
(decoder uses these values to determine precise time since last resync) |
(decoder uses these values to determine precise time since last resync) |
732 |
*/ |
*/ |
733 |
void BitstreamWriteVopHeader(Bitstream * const bs, |
void BitstreamWriteVopHeader(Bitstream * const bs, |
734 |
const MBParam * pParam) |
const MBParam * pParam, |
735 |
|
const FRAMEINFO * frame) |
736 |
{ |
{ |
737 |
|
#ifdef BFRAMES |
738 |
|
uint32_t i; |
739 |
|
#endif |
740 |
BitstreamPad(bs); |
BitstreamPad(bs); |
741 |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
BitstreamPutBits(bs, VOP_START_CODE, 32); |
742 |
|
|
743 |
BitstreamPutBits(bs, pParam->coding_type, 2); |
BitstreamPutBits(bs, frame->coding_type, 2); |
744 |
|
|
745 |
// time_base = 0 write n x PutBit(1), PutBit(0) |
// time_base = 0 write n x PutBit(1), PutBit(0) |
746 |
|
#ifdef BFRAMES |
747 |
|
for (i = 0; i < frame->seconds; i++) |
748 |
|
{ |
749 |
|
BitstreamPutBit(bs, 1); |
750 |
|
} |
751 |
|
BitstreamPutBit(bs, 0); |
752 |
|
#else |
753 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
754 |
|
#endif |
755 |
|
|
756 |
WRITE_MARKER(); |
WRITE_MARKER(); |
757 |
|
|
758 |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
// time_increment: value=nth_of_sec, nbits = log2(resolution) |
759 |
|
#ifdef BFRAMES |
760 |
|
BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); |
761 |
|
dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); |
762 |
|
#else |
763 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
764 |
|
#endif |
765 |
|
|
766 |
WRITE_MARKER(); |
WRITE_MARKER(); |
767 |
|
|
768 |
BitstreamPutBits(bs, 1, 1); // vop_coded |
BitstreamPutBits(bs, 1, 1); // vop_coded |
769 |
|
|
770 |
if (pParam->coding_type != I_VOP) |
if (frame->coding_type == P_VOP) |
771 |
BitstreamPutBits(bs, pParam->rounding_type, 1); |
BitstreamPutBits(bs, frame->rounding_type, 1); |
772 |
|
|
773 |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold |
774 |
|
|
775 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) |
776 |
{ |
{ |
777 |
BitstreamPutBit(bs, 1); // top field first |
BitstreamPutBit(bs, 1); // top field first |
778 |
BitstreamPutBit(bs, 0); // alternate vertical scan |
BitstreamPutBit(bs, 0); // alternate vertical scan |
779 |
} |
} |
780 |
|
|
781 |
BitstreamPutBits(bs, pParam->quant, 5); // quantizer |
BitstreamPutBits(bs, frame->quant, 5); // quantizer |
782 |
|
|
783 |
|
if (frame->coding_type != I_VOP) |
784 |
|
BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code |
785 |
|
|
786 |
|
if (frame->coding_type == B_VOP) |
787 |
|
BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code |
788 |
|
|
|
if (pParam->coding_type != I_VOP) |
|
|
BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] |
|
789 |
} |
} |