--- trunk/xvidcore/src/bitstream/bitstream.c 2002/06/12 20:38:41 195 +++ trunk/xvidcore/src/bitstream/bitstream.c 2002/06/22 07:23:10 233 @@ -38,22 +38,26 @@ ******************************************************************************/ /****************************************************************************** - * * + * * * Revision history: * - * * - * 08.05.2002 add low_delay support for B_VOP decode * + * * + * 22.05.2002 bs_put_matrix fix + * 20.05.2002 added BitstreamWriteUserData * + * 19.06.2002 Fix a little bug in use custom quant matrix * + * MinChen * + * 08.05.2002 add low_delay support for B_VOP decode * * MinChen * * 06.05.2002 low_delay * * 06.05.2002 fixed fincr/fbase error * * 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 * - * 04.12.2001 support for additional headers * - * 16.12.2001 inital version * - * * + * 26.03.2002 interlacing support * + * 03.03.2002 qmatrix writing * + * 03.03.2002 merged BITREADER and BITWRITER * + * 30.02.2002 intra_dc_threshold support * + * 04.12.2001 support for additional headers * + * 16.12.2001 inital version * + * ******************************************************************************/ @@ -76,7 +80,9 @@ return n; #else __asm { - bsr eax, value inc eax} + bsr eax, value + inc eax + } #endif } @@ -106,6 +112,7 @@ matrix[scan_tables[0][i++]] = value; } while (value != 0 && i < 64); + i--; // fix little bug at coeff not full while (i < 64) { matrix[scan_tables[0][i++]] = last; @@ -138,13 +145,13 @@ start_code = BitstreamShowBits(bs, 32); if (start_code == VISOBJSEQ_START_CODE) { - // DEBUG("visual_object_sequence"); + // DPRINTF("visual_object_sequence"); BitstreamSkip(bs, 32); // visual_object_sequence_start_code BitstreamSkip(bs, 8); // profile_and_level_indication } else if (start_code == VISOBJSEQ_STOP_CODE) { BitstreamSkip(bs, 32); // visual_object_sequence_stop_code } else if (start_code == VISOBJ_START_CODE) { - // DEBUG("visual_object"); + //DPRINTF("visual_object"); BitstreamSkip(bs, 32); // visual_object_start_code if (BitstreamGetBit(bs)) // is_visual_object_identified { @@ -156,7 +163,7 @@ if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type { - DEBUG("visual_object_type != video"); + //DPRINTF("visual_object_type != video"); return -1; } BitstreamSkip(bs, 4); @@ -165,12 +172,12 @@ if (BitstreamGetBit(bs)) // video_signal_type { - DEBUG("+ video_signal_type"); + //DPRINTF("+ video_signal_type"); BitstreamSkip(bs, 3); // video_format BitstreamSkip(bs, 1); // video_range if (BitstreamGetBit(bs)) // color_description { - DEBUG("+ color_description"); + //DPRINTF("+ color_description"); BitstreamSkip(bs, 8); // color_primaries BitstreamSkip(bs, 8); // transfer_characteristics BitstreamSkip(bs, 8); // matrix_coefficients @@ -179,7 +186,7 @@ } else if ((start_code & ~0x1f) == VIDOBJ_START_CODE) { BitstreamSkip(bs, 32); // video_object_start_code } else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE) { - // DEBUG("video_object_layer"); + //DPRINTF("video_object_layer"); BitstreamSkip(bs, 32); // video_object_layer_start_code BitstreamSkip(bs, 1); // random_accessible_vol @@ -187,8 +194,8 @@ // video_object_type_indication 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 { - DEBUG1("video_object_type_indication not supported", - BitstreamShowBits(bs, 8)); + //DPRINTF("video_object_type_indication %i not supported ", + // BitstreamShowBits(bs, 8)); return -1; } BitstreamSkip(bs, 8); @@ -196,29 +203,29 @@ if (BitstreamGetBit(bs)) // is_object_layer_identifier { - DEBUG("+ is_object_layer_identifier"); + //DPRINTF("+ is_object_layer_identifier"); vol_ver_id = BitstreamGetBits(bs, 4); // video_object_layer_verid BitstreamSkip(bs, 3); // video_object_layer_priority } else { vol_ver_id = 1; } - //DEBUGI("vol_ver_id", vol_ver_id); + //DPRINTF("vol_ver_id %i", vol_ver_id); if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info { - DEBUG("+ aspect_ratio_info"); + //DPRINTF("+ aspect_ratio_info"); BitstreamSkip(bs, 8); // par_width BitstreamSkip(bs, 8); // par_height } if (BitstreamGetBit(bs)) // vol_control_parameters { - DEBUG("+ vol_control_parameters"); + //DPRINTF("+ vol_control_parameters"); BitstreamSkip(bs, 2); // chroma_format dec->low_delay = BitstreamGetBit(bs); // low_delay if (BitstreamGetBit(bs)) // vbv_parameters { - DEBUG("+ vbv_parameters"); + //DPRINTF("+ vbv_parameters"); BitstreamSkip(bs, 15); // first_half_bitrate READ_MARKER(); BitstreamSkip(bs, 15); // latter_half_bitrate @@ -235,7 +242,7 @@ } dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape - // DEBUG1("shape", dec->shape); + //DPRINTF("shape %i", dec->shape); if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { BitstreamSkip(bs, 4); // video_object_layer_shape_extension @@ -246,7 +253,7 @@ // *************************** 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); + //DPRINTF("time_increment_resolution = %i",time_increment_resolution); if (time_increment_resolution > 0) { dec->time_inc_bits = log2bin(time_increment_resolution); } else { @@ -270,35 +277,35 @@ READ_MARKER(); width = BitstreamGetBits(bs, 13); // video_object_layer_width - //DEBUGI("width", width); + //DPRINTF("width %i", width); READ_MARKER(); height = BitstreamGetBits(bs, 13); // video_object_layer_height - //DEBUGI("height", height); + //DPRINTF("height %i", height); READ_MARKER(); if (width != dec->width || height != dec->height) { - DEBUG("FATAL: video dimension discrepancy ***"); - DEBUG2("bitstream width/height", width, height); - DEBUG2("param width/height", dec->width, dec->height); + //DPRINTF("FATAL: video dimension discrepancy ***"); + //DPRINTF("bitstream width/height %i x %i", width, height); + //DPRINTF("param width/height %i x %i", dec->width, dec->height); return -1; } } if ((dec->interlacing = BitstreamGetBit(bs))) { - DEBUG("vol: interlacing"); + //DPRINTF("vol: interlacing"); } if (!BitstreamGetBit(bs)) // obmc_disable { - DEBUG("IGNORED/TODO: !obmc_disable"); + //DPRINTF("IGNORED/TODO: !obmc_disable"); // TODO // fucking divx4.02 has this enabled } if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable { - DEBUG("sprite_enable; not supported"); + //DPRINTF("sprite_enable; not supported"); return -1; } @@ -309,7 +316,7 @@ if (BitstreamGetBit(bs)) // not_8_bit { - DEBUG("+ not_8_bit [IGNORED/TODO]"); + //DPRINTF("+ not_8_bit [IGNORED/TODO]"); dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision BitstreamSkip(bs, 4); // bits_per_pixel } else { @@ -323,7 +330,7 @@ } dec->quant_type = BitstreamGetBit(bs); // quant_type - // DEBUG1("**** quant_type", dec->quant_type); + //DPRINTF("**** quant_type %i", dec->quant_type); if (dec->quant_type) { if (BitstreamGetBit(bs)) // load_intra_quant_mat @@ -346,7 +353,7 @@ if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { // TODO - DEBUG("TODO: grayscale matrix stuff"); + //DPRINTF("TODO: grayscale matrix stuff"); return -1; } @@ -356,7 +363,7 @@ if (vol_ver_id != 1) { dec->quarterpel = BitstreamGetBit(bs); // quarter_sampe if (dec->quarterpel) { - DEBUG("IGNORED/TODO: quarter_sample"); + //DPRINTF("IGNORED/TODO: quarter_sample"); } } else { dec->quarterpel = 0; @@ -364,33 +371,33 @@ if (!BitstreamGetBit(bs)) // complexity_estimation_disable { - DEBUG("TODO: complexity_estimation header"); + //DPRINTF("TODO: complexity_estimation header"); // TODO return -1; } if (!BitstreamGetBit(bs)) // resync_marker_disable { - DEBUG("IGNORED/TODO: !resync_marker_disable"); + //DPRINTF("IGNORED/TODO: !resync_marker_disable"); // TODO } if (BitstreamGetBit(bs)) // data_partitioned { - DEBUG("+ data_partitioned"); + //DPRINTF("+ data_partitioned"); BitstreamSkip(bs, 1); // reversible_vlc } if (vol_ver_id != 1) { if (BitstreamGetBit(bs)) // newpred_enable { - DEBUG("+ newpred_enable"); + //DPRINTF("+ newpred_enable"); BitstreamSkip(bs, 2); // requested_upstream_message_type BitstreamSkip(bs, 1); // newpred_segment_type } if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable { - DEBUG("TODO: reduced_resolution_vop_enable"); + //DPRINTF("TODO: reduced_resolution_vop_enable"); // TODO return -1; } @@ -399,7 +406,7 @@ if ((dec->scalability = BitstreamGetBit(bs))) // scalability { // TODO - DEBUG("TODO: scalability"); + //DPRINTF("TODO: scalability"); return -1; } } else // dec->shape == BINARY_ONLY @@ -408,7 +415,7 @@ if (BitstreamGetBit(bs)) // scalability { // TODO - DEBUG("TODO: scalability"); + //DPRINTF("TODO: scalability"); return -1; } } @@ -417,7 +424,7 @@ } } else if (start_code == GRPOFVOP_START_CODE) { - // DEBUG("group_of_vop"); + //DPRINTF("group_of_vop"); BitstreamSkip(bs, 32); { int hours, minutes, seconds; @@ -426,16 +433,16 @@ minutes = BitstreamGetBits(bs, 6); READ_MARKER(); seconds = BitstreamGetBits(bs, 6); - // DEBUG3("hms", hours, minutes, seconds); + //DPRINTF("%ih %im %is", hours, minutes, seconds); } BitstreamSkip(bs, 1); // closed_gov BitstreamSkip(bs, 1); // broken_link } else if (start_code == VOP_START_CODE) { - // DEBUG("vop_start_code"); + //DPRINTF("vop_start_code"); BitstreamSkip(bs, 32); // vop_start_code coding_type = BitstreamGetBits(bs, 2); // vop_coding_type - //DEBUG1("coding_type", coding_type); + //DPRINTF("coding_type %i", coding_type); // *************************** for decode B-frame time *********************** while (BitstreamGetBit(bs) != 0) // time_base @@ -443,8 +450,8 @@ READ_MARKER(); - //DEBUG1("time_inc_bits", dec->time_inc_bits); - //DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits)); + //DPRINTF("time_inc_bits %i", dec->time_inc_bits); + //DPRINTF("vop_time_incr %i", BitstreamShowBits(bs, dec->time_inc_bits)); if (dec->time_inc_bits) { //BitstreamSkip(bs, dec->time_inc_bits); // vop_time_increment time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment @@ -463,7 +470,7 @@ time_incr) * time_increment_resolution + time_increment; dec->time_bp = (uint32_t) (dec->last_non_b_time - dec->time); } - //DEBUG1("time_increment=",time_increment); + //DPRINTF("time_increment %i",time_increment); READ_MARKER(); @@ -481,7 +488,7 @@ if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && (coding_type == P_VOP)) { *rounding = BitstreamGetBit(bs); // rounding_type - //DEBUG1("rounding", *rounding); + //DPRINTF("rounding %i", *rounding); } /* if (reduced_resolution_enable) @@ -502,8 +509,8 @@ vert_mc_ref = BitstreamGetBits(bs, 13); READ_MARKER(); - // DEBUG2("vop_width/height", width, height); - // DEBUG2("ref ", horiz_mc_ref, vert_mc_ref); + //DPRINTF("vop_width/height %i x %i", width, height); + //DPRINTF("ref %i x %i", horiz_mc_ref, vert_mc_ref); BitstreamSkip(bs, 1); // change_conv_ratio_disable if (BitstreamGetBit(bs)) // vop_constant_alpha @@ -520,10 +527,10 @@ if (dec->interlacing) { if ((dec->top_field_first = BitstreamGetBit(bs))) { - DEBUG("vop: top_field_first"); + //DPRINTF("vop: top_field_first"); } if ((dec->alternate_vertical_scan = BitstreamGetBit(bs))) { - DEBUG("vop: alternate_vertical_scan"); + //DPRINTF("vop: alternate_vertical_scan"); } } } @@ -531,7 +538,7 @@ if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant *quant = 1; - //DEBUG1("quant", *quant); + //DPRINTF("quant %i", *quant); if (coding_type != I_VOP) { *fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward @@ -548,20 +555,20 @@ } return coding_type; } else if (start_code == USERDATA_START_CODE) { - // DEBUG("user_data"); + //DPRINTF("user_data"); BitstreamSkip(bs, 32); // user_data_start_code } else // start_code == ? { if (BitstreamShowBits(bs, 24) == 0x000001) { - DEBUG1("*** WARNING: unknown start_code", - BitstreamShowBits(bs, 32)); + //DPRINTF("*** WARNING: unknown start_code %x", + // BitstreamShowBits(bs, 32)); } BitstreamSkip(bs, 8); } } while ((BitstreamPos(bs) >> 3) < bs->length); - DEBUG("*** WARNING: no vop_start_code found"); + //DPRINTF("*** WARNING: no vop_start_code found"); return -1; /* ignore it */ } @@ -575,7 +582,7 @@ int i, j; const int last = matrix[scan_tables[0][63]]; - 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--); for (i = 0; i <= j; i++) { BitstreamPutBits(bs, matrix[scan_tables[0][i]], 8); @@ -611,7 +618,7 @@ #ifdef BFRAMES if (pParam->max_bframes > 0) { - dprintf("low_delay=1"); + //DPRINTF("low_delay=1"); BitstreamPutBit(bs, 1); // vol_control_parameters BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" BitstreamPutBit(bs, 0); // low_delay @@ -691,7 +698,8 @@ void BitstreamWriteVopHeader(Bitstream * const bs, const MBParam * pParam, - const FRAMEINFO * frame) + const FRAMEINFO * frame, + int vop_coded) { #ifdef BFRAMES uint32_t i; @@ -716,7 +724,7 @@ // time_increment: value=nth_of_sec, nbits = log2(resolution) #ifdef BFRAMES BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); - dprintf("[%i:%i] %c\n", frame->seconds, frame->ticks, + DPRINTF("[%i:%i] %c\n", frame->seconds, frame->ticks, frame->coding_type == I_VOP ? 'I' : frame->coding_type == P_VOP ? 'P' : 'B'); #else @@ -725,6 +733,11 @@ WRITE_MARKER(); + if (!vop_coded) { + BitstreamPutBits(bs, 0, 1); + return; + } + BitstreamPutBits(bs, 1, 1); // vop_coded if (frame->coding_type == P_VOP) @@ -746,3 +759,20 @@ BitstreamPutBits(bs, frame->bcode, 3); // backward_fixed_code } + + +void +BitstreamWriteUserData(Bitstream * const bs, + uint8_t * data, + const int length) +{ + int i; + + BitstreamPad(bs); + BitstreamPutBits(bs, USERDATA_START_CODE, 32); + + for (i = 0; i < length; i++) { + BitstreamPutBits(bs, data[i], 8); + } + +} \ No newline at end of file