--- trunk/xvidcore/src/bitstream/bitstream.c 2002/03/28 12:26:44 74 +++ trunk/xvidcore/src/bitstream/bitstream.c 2002/05/03 08:37:55 157 @@ -40,8 +40,10 @@ /****************************************************************************** * * * Revision history: * - * * - * 26.03.2002 interlacing support + * * + * 01.05.2002 added BVOP support to BitstreamWriteVopHeader + * 15.04.2002 rewrite log2bin use asm386 By MinChen * + * 26.03.2002 interlacing support * * 03.03.2002 qmatrix writing * * 03.03.2002 merged BITREADER and BITWRITER * * 30.02.2002 intra_dc_threshold support * @@ -55,8 +57,11 @@ #include "zigzag.h" #include "../quant/quant_matrix.h" + static int __inline log2bin(int value) { +/* Changed by Chenm001 */ +#ifndef WIN32 int n = 0; while (value) { @@ -64,6 +69,12 @@ n++; } return n; +#else + __asm{ + bsr eax,value + inc eax + } +#endif } @@ -104,13 +115,15 @@ returns coding_type, or -1 if error */ -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) { uint32_t vol_ver_id; - uint32_t time_inc_resolution; + static uint32_t time_increment_resolution; uint32_t coding_type; uint32_t start_code; - + uint32_t time_incr=0; + int32_t time_increment; + do { BitstreamByteAlign(bs); @@ -172,7 +185,7 @@ // DEBUG("video_object_layer"); BitstreamSkip(bs, 32); // video_object_layer_start_code - BitstreamSkip(bs, 1); // random_accessible_vol + BitstreamSkip(bs, 1); // random_accessible_vol // video_object_type_indication if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE && @@ -205,7 +218,7 @@ BitstreamSkip(bs, 8); // par_height } - if (BitstreamGetBit(bs)) // vol_control_parameters + if (BitstreamGetBit(bs)) // vol_control_parameters { DEBUG("+ vol_control_parameters"); BitstreamSkip(bs, 2); // chroma_format @@ -238,11 +251,13 @@ READ_MARKER(); - time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution - time_inc_resolution--; - if (time_inc_resolution > 0) +// *************************** for decode B-frame time *********************** + time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution + time_increment_resolution--; + //DEBUG1("time_increment_resolution=",time_increment_resolution); + if (time_increment_resolution > 0) { - dec->time_inc_bits = log2bin(time_inc_resolution); + dec->time_inc_bits = log2bin(time_increment_resolution); } else { @@ -406,7 +421,7 @@ } } - if (BitstreamGetBit(bs)) // scalability + if ((dec->scalability=BitstreamGetBit(bs))) // scalability { // TODO DEBUG("TODO: scalability"); @@ -452,7 +467,9 @@ coding_type = BitstreamGetBits(bs, 2); // vop_coding_type //DEBUG1("coding_type", coding_type); - while (BitstreamGetBit(bs) != 0) ; // time_base +// *************************** for decode B-frame time *********************** + while (BitstreamGetBit(bs) != 0) // time_base + time_incr++; READ_MARKER(); @@ -460,8 +477,20 @@ //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); if (dec->time_inc_bits) { - BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment + //BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment + time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment + } + if(coding_type != B_VOP){ + dec->last_time_base = dec->time_base; + dec->time_base += time_incr; + dec->time = dec->time_base*time_increment_resolution + time_increment; + dec->time_pp= (uint32_t)(dec->time - dec->last_non_b_time); + dec->last_non_b_time= dec->time; + }else{ + dec->time = (dec->last_time_base + time_incr)*time_increment_resolution + time_increment; + dec->time_bp= (uint32_t)(dec->last_non_b_time - dec->time); } + //DEBUG1("time_increment=",time_increment); READ_MARKER(); @@ -475,7 +504,8 @@ } */ - if (coding_type != I_VOP) + // fix a little bug by MinChen + if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) { *rounding = BitstreamGetBit(bs); // rounding_type //DEBUG1("rounding", *rounding); @@ -529,17 +559,24 @@ } } - *quant = BitstreamGetBits(bs, dec->quant_bits); // vop_quant + if((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant + *quant = 1; + //DEBUG1("quant", *quant); if (coding_type != I_VOP) { - *fcode = BitstreamGetBits(bs, 3); // fcode_forward + *fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward } if (coding_type == B_VOP) { - // *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward + *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward + } + if (!dec->scalability){ + if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && (coding_type != I_VOP)){ + BitstreamSkip(bs, 1); // vop_shape_coding_type + } } return coding_type; } @@ -589,7 +626,7 @@ write vol header */ void BitstreamWriteVolHeader(Bitstream * const bs, - const MBParam * pParam) + const MBParam * pParam, const FRAMEINFO * frame) { // video object_start_code & vo_id BitstreamPad(bs); @@ -630,15 +667,15 @@ BitstreamPutBits(bs, pParam->height, 13); // height WRITE_MARKER(); - BitstreamPutBit(bs, pParam->global_flags & XVID_INTERLACING); // interlace + BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) BitstreamPutBit(bs, 0); // sprite_enable BitstreamPutBit(bs, 0); // not_in_bit // quant_type 0=h.263 1=mpeg4(quantizer tables) - BitstreamPutBit(bs, pParam->quant_type); + BitstreamPutBit(bs, pParam->m_quant_type); - if (pParam->quant_type) + if (pParam->m_quant_type) { BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat if (get_intra_matrix_status()) @@ -670,38 +707,59 @@ (decoder uses these values to determine precise time since last resync) */ void BitstreamWriteVopHeader(Bitstream * const bs, - const MBParam * pParam) + const MBParam * pParam, + const FRAMEINFO * frame) { +#ifdef BFRAMES + uint32_t i; +#endif BitstreamPad(bs); BitstreamPutBits(bs, VOP_START_CODE, 32); - BitstreamPutBits(bs, pParam->coding_type, 2); + BitstreamPutBits(bs, frame->coding_type, 2); // time_base = 0 write n x PutBit(1), PutBit(0) +#ifdef BFRAMES + for (i = 0; i < frame->seconds; i++) + { + BitstreamPutBit(bs, 1); + } + BitstreamPutBit(bs, 0); +#else BitstreamPutBits(bs, 0, 1); +#endif WRITE_MARKER(); // time_increment: value=nth_of_sec, nbits = log2(resolution) +#ifdef BFRAMES + BitstreamPutBits(bs, frame->ticks, 5); + dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); +#else BitstreamPutBits(bs, 1, 1); +#endif WRITE_MARKER(); BitstreamPutBits(bs, 1, 1); // vop_coded - if (pParam->coding_type != I_VOP) - BitstreamPutBits(bs, pParam->rounding_type, 1); + if (frame->coding_type != I_VOP) + BitstreamPutBits(bs, frame->rounding_type, 1); BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold - if (pParam->global_flags & XVID_INTERLACING) + if (frame->global_flags & XVID_INTERLACING) { BitstreamPutBit(bs, 1); // top field first BitstreamPutBit(bs, 0); // alternate vertical scan } - BitstreamPutBits(bs, pParam->quant, 5); // quantizer + BitstreamPutBits(bs, frame->quant, 5); // quantizer - if (pParam->coding_type != I_VOP) - BitstreamPutBits(bs, pParam->fixed_code, 3); // fixed_code = [1,4] + if (frame->coding_type != I_VOP) + BitstreamPutBits(bs, frame->fcode, 3); // forward_fixed_code + + if (frame->coding_type == B_VOP) + BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code + }