--- branches/dev-api-3/xvidcore/src/bitstream/bitstream.c 2002/11/07 10:31:03 631 +++ branches/dev-api-3/xvidcore/src/bitstream/bitstream.c 2002/12/20 05:12:02 733 @@ -66,6 +66,9 @@ ******************************************************************************/ +#include +#include + #include "bitstream.h" #include "zigzag.h" #include "../quant/quant_matrix.h" @@ -76,7 +79,7 @@ log2bin(uint32_t value) { /* Changed by Chenm001 */ -#ifndef WIN32 +#if !defined(_MSC_VER) int n = 0; while (value) { @@ -170,7 +173,7 @@ if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { - *quant = BitstreamGetBits(bs, 5); /* quant_scale */ + *quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */ DPRINTF(DPRINTF_HEADER, "quant %i", *quant); } @@ -215,7 +218,7 @@ dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && (coding_type == P_VOP || coding_type == I_VOP)) { - BitstreamSkip(bs, 1); /* vop_reduced_resolution */ + BitstreamSkip(bs, 1); /* XXX: vop_reduced_resolution */ } if (coding_type != I_VOP && fcode_forward) @@ -253,6 +256,165 @@ + +/* vol estimation header */ +static void +read_vol_complexity_estimation_header(Bitstream * bs, DECODER * dec) +{ + ESTIMATION * e = &dec->estimation; + + e->method = BitstreamGetBits(bs, 2); /* estimation_method */ + DPRINTF(DPRINTF_HEADER,"+ complexity_estimation_header; method=%i", e->method); + + if (e->method == 0 || e->method == 1) + { + if (!BitstreamGetBit(bs)) /* shape_complexity_estimation_disable */ + { + e->opaque = BitstreamGetBit(bs); /* opaque */ + e->transparent = BitstreamGetBit(bs); /* transparent */ + e->intra_cae = BitstreamGetBit(bs); /* intra_cae */ + e->inter_cae = BitstreamGetBit(bs); /* inter_cae */ + e->no_update = BitstreamGetBit(bs); /* no_update */ + e->upsampling = BitstreamGetBit(bs); /* upsampling */ + } + + if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_1_disable */ + { + e->intra_blocks = BitstreamGetBit(bs); /* intra_blocks */ + e->inter_blocks = BitstreamGetBit(bs); /* inter_blocks */ + e->inter4v_blocks = BitstreamGetBit(bs); /* inter4v_blocks */ + e->not_coded_blocks = BitstreamGetBit(bs); /* not_coded_blocks */ + } + } + + READ_MARKER(); + + if (!BitstreamGetBit(bs)) /* texture_complexity_estimation_set_2_disable */ + { + e->dct_coefs = BitstreamGetBit(bs); /* dct_coefs */ + e->dct_lines = BitstreamGetBit(bs); /* dct_lines */ + e->vlc_symbols = BitstreamGetBit(bs); /* vlc_symbols */ + e->vlc_bits = BitstreamGetBit(bs); /* vlc_bits */ + } + + if (!BitstreamGetBit(bs)) /* motion_compensation_complexity_disable */ + { + e->apm = BitstreamGetBit(bs); /* apm */ + e->npm = BitstreamGetBit(bs); /* npm */ + e->interpolate_mc_q = BitstreamGetBit(bs); /* interpolate_mc_q */ + e->forw_back_mc_q = BitstreamGetBit(bs); /* forw_back_mc_q */ + e->halfpel2 = BitstreamGetBit(bs); /* halfpel2 */ + e->halfpel4 = BitstreamGetBit(bs); /* halfpel4 */ + } + + READ_MARKER(); + + if (e->method == 1) + { + if (!BitstreamGetBit(bs)) /* version2_complexity_estimation_disable */ + { + e->sadct = BitstreamGetBit(bs); /* sadct */ + e->quarterpel = BitstreamGetBit(bs); /* quarterpel */ + } + } +} + + +/* vop estimation header */ +static void +read_vop_complexity_estimation_header(Bitstream * bs, DECODER * dec, int coding_type) +{ + ESTIMATION * e = &dec->estimation; + + if (e->method == 0 || e->method == 1) + { + if (coding_type == I_VOP) { + if (e->opaque) BitstreamSkip(bs, 8); /* dcecs_opaque */ + if (e->transparent) BitstreamSkip(bs, 8); /* */ + if (e->intra_cae) BitstreamSkip(bs, 8); /* */ + if (e->inter_cae) BitstreamSkip(bs, 8); /* */ + if (e->no_update) BitstreamSkip(bs, 8); /* */ + if (e->upsampling) BitstreamSkip(bs, 8); /* */ + if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ + if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ + if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ + if (e->dct_lines) BitstreamSkip(bs, 8); /* */ + if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ + if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ + if (e->sadct) BitstreamSkip(bs, 8); /* */ + } + + if (coding_type == P_VOP) { + if (e->opaque) BitstreamSkip(bs, 8); /* */ + if (e->transparent) BitstreamSkip(bs, 8); /* */ + if (e->intra_cae) BitstreamSkip(bs, 8); /* */ + if (e->inter_cae) BitstreamSkip(bs, 8); /* */ + if (e->no_update) BitstreamSkip(bs, 8); /* */ + if (e->upsampling) BitstreamSkip(bs, 8); /* */ + if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ + if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ + if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ + if (e->dct_lines) BitstreamSkip(bs, 8); /* */ + if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ + if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ + if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ + if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ + if (e->apm) BitstreamSkip(bs, 8); /* */ + if (e->npm) BitstreamSkip(bs, 8); /* */ + if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ + if (e->halfpel2) BitstreamSkip(bs, 8); /* */ + if (e->halfpel4) BitstreamSkip(bs, 8); /* */ + if (e->sadct) BitstreamSkip(bs, 8); /* */ + if (e->quarterpel) BitstreamSkip(bs, 8); /* */ + } + if (coding_type == B_VOP) { + if (e->opaque) BitstreamSkip(bs, 8); /* */ + if (e->transparent) BitstreamSkip(bs, 8); /* */ + if (e->intra_cae) BitstreamSkip(bs, 8); /* */ + if (e->inter_cae) BitstreamSkip(bs, 8); /* */ + if (e->no_update) BitstreamSkip(bs, 8); /* */ + if (e->upsampling) BitstreamSkip(bs, 8); /* */ + if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ + if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ + if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ + if (e->dct_lines) BitstreamSkip(bs, 8); /* */ + if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ + if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ + if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ + if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ + if (e->apm) BitstreamSkip(bs, 8); /* */ + if (e->npm) BitstreamSkip(bs, 8); /* */ + if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ + if (e->halfpel2) BitstreamSkip(bs, 8); /* */ + if (e->halfpel4) BitstreamSkip(bs, 8); /* */ + if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */ + if (e->sadct) BitstreamSkip(bs, 8); /* */ + if (e->quarterpel) BitstreamSkip(bs, 8); /* */ + } + + if (coding_type == S_VOP && dec->sprite_enable == SPRITE_STATIC) { + if (e->intra_blocks) BitstreamSkip(bs, 8); /* */ + if (e->not_coded_blocks) BitstreamSkip(bs, 8); /* */ + if (e->dct_coefs) BitstreamSkip(bs, 8); /* */ + if (e->dct_lines) BitstreamSkip(bs, 8); /* */ + if (e->vlc_symbols) BitstreamSkip(bs, 8); /* */ + if (e->vlc_bits) BitstreamSkip(bs, 8); /* */ + if (e->inter_blocks) BitstreamSkip(bs, 8); /* */ + if (e->inter4v_blocks) BitstreamSkip(bs, 8); /* */ + if (e->apm) BitstreamSkip(bs, 8); /* */ + if (e->npm) BitstreamSkip(bs, 8); /* */ + if (e->forw_back_mc_q) BitstreamSkip(bs, 8); /* */ + if (e->halfpel2) BitstreamSkip(bs, 8); /* */ + if (e->halfpel4) BitstreamSkip(bs, 8); /* */ + if (e->interpolate_mc_q) BitstreamSkip(bs, 8); /* */ + } + } +} + + + + + /* decode headers returns coding_type, or -1 if error @@ -408,6 +570,8 @@ BitstreamSkip(bs, 15); // latter_half_vbv_occupancy READ_MARKER(); } + }else{ + dec->low_delay = dec->low_delay_default; } dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape @@ -575,10 +739,10 @@ dec->quarterpel = 0; - if (!BitstreamGetBit(bs)) // complexity_estimation_disable + dec->complexity_estimation_disable = BitstreamGetBit(bs); /* complexity estimation disable */ + if (!dec->complexity_estimation_disable) { - DPRINTF(DPRINTF_ERROR, "complexity_estimation not supported"); - return -1; + read_vol_complexity_estimation_header(bs, dec); } BitstreamSkip(bs, 1); // resync_marker_disable @@ -597,12 +761,8 @@ BitstreamSkip(bs, 2); // requested_upstream_message_type BitstreamSkip(bs, 1); // newpred_segment_type } - dec->reduced_resolution_enable = BitstreamGetBit(bs); - if (dec->reduced_resolution_enable) // reduced_resolution_vop_enable - { - DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); - //return -1; - } + dec->reduced_resolution_enable = BitstreamGetBit(bs); /* reduced_resolution_vop_enable */ + DPRINTF(DPRINTF_HEADER, "reduced_resolution_enable %i", dec->reduced_resolution_enable); } else { @@ -610,17 +770,40 @@ dec->reduced_resolution_enable = 0; } - if ((dec->scalability = BitstreamGetBit(bs))) // scalability + dec->scalability = BitstreamGetBit(bs); /* scalability */ + if (dec->scalability) { DPRINTF(DPRINTF_ERROR, "scalability not supported"); + BitstreamSkip(bs, 1); /* hierarchy_type */ + BitstreamSkip(bs, 4); /* ref_layer_id */ + BitstreamSkip(bs, 1); /* ref_layer_sampling_direc */ + BitstreamSkip(bs, 5); /* hor_sampling_factor_n */ + BitstreamSkip(bs, 5); /* hor_sampling_factor_m */ + BitstreamSkip(bs, 5); /* vert_sampling_factor_n */ + BitstreamSkip(bs, 5); /* vert_sampling_factor_m */ + BitstreamSkip(bs, 1); /* enhancement_type */ + if(dec->shape == VIDOBJLAY_SHAPE_BINARY /* && hierarchy_type==0 */) { + BitstreamSkip(bs, 1); /* use_ref_shape */ + BitstreamSkip(bs, 1); /* use_ref_texture */ + BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_n */ + BitstreamSkip(bs, 5); /* shape_hor_sampling_factor_m */ + BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_n */ + BitstreamSkip(bs, 5); /* shape_vert_sampling_factor_m */ + } return -1; } } else // dec->shape == BINARY_ONLY { if (vol_ver_id != 1) { - if (BitstreamGetBit(bs)) // scalability + dec->scalability = BitstreamGetBit(bs); /* scalability */ + if (dec->scalability) { - DPRINTF(DPRINTF_ERROR, "scalability not supported"); + DPRINTF(DPRINTF_ERROR, "scalability not supported"); + BitstreamSkip(bs, 4); /* ref_layer_id */ + BitstreamSkip(bs, 5); /* hor_sampling_factor_n */ + BitstreamSkip(bs, 5); /* hor_sampling_factor_m */ + BitstreamSkip(bs, 5); /* vert_sampling_factor_n */ + BitstreamSkip(bs, 5); /* vert_sampling_factor_m */ return -1; } } @@ -693,6 +876,8 @@ dec->time_bp = (uint32_t) (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution; } + DPRINTF(DPRINTF_HEADER,"time_pp=%i", dec->time_pp); + DPRINTF(DPRINTF_HEADER,"time_bp=%i", dec->time_bp); READ_MARKER(); @@ -731,6 +916,7 @@ (coding_type == P_VOP || coding_type == I_VOP)) { *reduced_resolution = BitstreamGetBit(bs); + DPRINTF(DPRINTF_HEADER, "reduced_resolution %i", *reduced_resolution); } else { @@ -766,6 +952,12 @@ } if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { + + if (!dec->complexity_estimation_disable) + { + read_vop_complexity_estimation_header(bs, dec, coding_type); + } + // intra_dc_vlc_threshold *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; @@ -847,11 +1039,37 @@ return coding_type; } else if (start_code == USERDATA_START_CODE) { - - DPRINTF(DPRINTF_STARTCODE, ""); + char tmp[256]; + int i, version, build; + char packed; BitstreamSkip(bs, 32); // user_data_start_code + tmp[0] = BitstreamShowBits(bs, 8); + + for(i = 1; i < 256; i++){ + tmp[i] = (BitstreamShowBits(bs, 16) & 0xFF); + + if(tmp[i] == 0) + break; + + BitstreamSkip(bs, 8); + } + + DPRINTF(DPRINTF_STARTCODE, ": %s\n", tmp); + + /* divx detection */ + i = sscanf(tmp, "DivX%dBuild%d%c", &version, &build, &packed); + if (i < 2) + i = sscanf(tmp, "DivX%db%d%c", &version, &build, &packed); + + if (i >= 2) + { + dec->packed_mode = (i == 3 && packed == 'p'); + DPRINTF(DPRINTF_HEADER, "divx version=%i, build=%i packed=%i", + version, build, dec->packed_mode); + } + } else // start_code == ? { if (BitstreamShowBits(bs, 24) == 0x000001) { @@ -896,19 +1114,20 @@ const MBParam * pParam, const FRAMEINFO * const frame) { + static const unsigned int vo_id = 0; + static const unsigned int vol_id = 0; int vol_ver_id=1; - if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) ) + if ( pParam->m_quarterpel || (frame->global_flags & XVID_GMC) || + (pParam->global & XVID_GLOBAL_REDUCED)) vol_ver_id = 2; // video object_start_code & vo_id BitstreamPad(bs); - BitstreamPutBits(bs, VO_START_CODE, 27); - BitstreamPutBits(bs, 0, 5); + BitstreamPutBits(bs, VIDOBJ_START_CODE|(vo_id&0x5), 32); // video_object_layer_start_code & vol_id - BitstreamPutBits(bs, VOL_START_CODE, 28); - BitstreamPutBits(bs, 0, 4); + BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32); BitstreamPutBit(bs, 0); // random_accessible_vol BitstreamPutBits(bs, 0, 8); // video_object_type_indication @@ -1010,7 +1229,9 @@ if (vol_ver_id != 1) { BitstreamPutBit(bs, 0); // newpred_enable - BitstreamPutBit(bs, 0); // reduced_resolution_vop_enabled + + BitstreamPutBit(bs, (pParam->global & XVID_GLOBAL_REDUCED)?1:0); + /* reduced_resolution_vop_enabled */ } BitstreamPutBit(bs, 0); // scalability @@ -1022,7 +1243,8 @@ write vop header */ void -BitstreamWriteVopHeader(Bitstream * const bs, +BitstreamWriteVopHeader( + Bitstream * const bs, const MBParam * pParam, const FRAMEINFO * const frame, int vop_coded) @@ -1061,6 +1283,9 @@ if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) BitstreamPutBits(bs, frame->rounding_type, 1); + if ((pParam->global & XVID_GLOBAL_REDUCED)) + BitstreamPutBit(bs, (frame->global_flags & XVID_REDUCED)?1:0); + BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold if (frame->global_flags & XVID_INTERLACING) { @@ -1104,6 +1329,9 @@ // no support for brightness_change! } + + DPRINTF(DPRINTF_HEADER, "quant = %i", frame->quant); + BitstreamPutBits(bs, frame->quant, 5); // quantizer if (frame->coding_type != I_VOP)