--- bitstream.c 2002/12/14 09:39:42 1.28.2.11 +++ bitstream.c 2003/05/17 13:32:47 1.39.2.8 @@ -41,8 +41,8 @@ * * * Revision history: * * * - * 28.10.2002 GMC support - gruel * - * 04.10.2002 qpel support - Isibaar * + * 05.01.2003 GMC support - gruel * + * 04.10.2002 qpel support - Isibaar * * 11.07.2002 add VOP width & height return to dec when dec->width * * or dec->height is 0 (for use in examples/ex1.c) * * MinChen * @@ -150,7 +150,7 @@ BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); BitstreamSkip(bs, startcode_bits); - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { @@ -169,19 +169,19 @@ } mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */ - DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); + DPRINTF(XVID_DEBUG_HEADER, "mbnum %i", mbnum); if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { - *quant = BitstreamGetBits(bs, 5); /* quant_scale */ - DPRINTF(DPRINTF_HEADER, "quant %i", *quant); + *quant = BitstreamGetBits(bs, dec->quant_bits); /* quant_scale */ + DPRINTF(XVID_DEBUG_HEADER, "quant %i", *quant); } if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) hec = BitstreamGetBit(bs); /* header_extension_code */ - DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); + DPRINTF(XVID_DEBUG_HEADER, "header_extension_code %i", hec); if (hec) { int time_base; @@ -193,10 +193,10 @@ if (dec->time_inc_bits) time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); /* vop_time_increment */ READ_MARKER(); - DPRINTF(DPRINTF_HEADER,"time %i:%i", time_base, time_increment); + DPRINTF(XVID_DEBUG_HEADER,"time %i:%i", time_base, time_increment); coding_type = BitstreamGetBits(bs, 2); - DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type); + DPRINTF(XVID_DEBUG_HEADER,"coding_type %i", coding_type); if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { @@ -224,13 +224,13 @@ if (coding_type != I_VOP && fcode_forward) { *fcode_forward = BitstreamGetBits(bs, 3); - DPRINTF(DPRINTF_HEADER,"fcode_forward %i", *fcode_forward); + DPRINTF(XVID_DEBUG_HEADER,"fcode_forward %i", *fcode_forward); } if (coding_type == B_VOP && fcode_backward) { *fcode_backward = BitstreamGetBits(bs, 3); - DPRINTF(DPRINTF_HEADER,"fcode_backward %i", fcode_backward); + DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i", *fcode_backward); } } @@ -242,11 +242,11 @@ int vop_id_for_prediction; vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); + DPRINTF(XVID_DEBUG_HEADER, "vop_id %i", vop_id); if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ { vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); + DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); } READ_MARKER(); } @@ -264,7 +264,7 @@ ESTIMATION * e = &dec->estimation; e->method = BitstreamGetBits(bs, 2); /* estimation_method */ - DPRINTF(DPRINTF_HEADER,"+ complexity_estimation_header; method=%i", e->method); + DPRINTF(XVID_DEBUG_HEADER,"+ complexity_estimation_header; method=%i", e->method); if (e->method == 0 || e->method == 1) { @@ -326,7 +326,7 @@ { ESTIMATION * e = &dec->estimation; - if (e->method == 0) + if (e->method == 0 || e->method == 1) { if (coding_type == I_VOP) { if (e->opaque) BitstreamSkip(bs, 8); /* dcecs_opaque */ @@ -432,13 +432,13 @@ uint32_t * fcode_forward, uint32_t * fcode_backward, uint32_t * intra_dc_threshold, - VECTOR * gmc_mv) + WARPPOINTS *gmc_warp) { uint32_t vol_ver_id; uint32_t coding_type; uint32_t start_code; uint32_t time_incr = 0; - int32_t time_increment; + int32_t time_increment = 0; int resize = 0; do { @@ -450,36 +450,37 @@ int profile; - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); BitstreamSkip(bs, 32); // visual_object_sequence_start_code profile = BitstreamGetBits(bs, 8); // profile_and_level_indication - DPRINTF(DPRINTF_HEADER, "profile_and_level_indication %i", profile); + DPRINTF(XVID_DEBUG_HEADER, "profile_and_level_indication %i", profile); } else if (start_code == VISOBJSEQ_STOP_CODE) { BitstreamSkip(bs, 32); // visual_object_sequence_stop_code - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); } else if (start_code == VISOBJ_START_CODE) { + int visobj_ver_id; - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); BitstreamSkip(bs, 32); // visual_object_start_code if (BitstreamGetBit(bs)) // is_visual_object_identified { - vol_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id - DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); + visobj_ver_id = BitstreamGetBits(bs, 4); // visual_object_ver_id + DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i", visobj_ver_id); BitstreamSkip(bs, 3); // visual_object_priority } else { - vol_ver_id = 1; + visobj_ver_id = 1; } if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) // visual_object_type { - DPRINTF(DPRINTF_ERROR, "visual_object_type != video"); + DPRINTF(XVID_DEBUG_ERROR, "visual_object_type != video"); return -1; } BitstreamSkip(bs, 4); @@ -488,12 +489,12 @@ if (BitstreamGetBit(bs)) // video_signal_type { - DPRINTF(DPRINTF_HEADER,"+ video_signal_type"); + DPRINTF(XVID_DEBUG_HEADER,"+ video_signal_type"); BitstreamSkip(bs, 3); // video_format BitstreamSkip(bs, 1); // video_range if (BitstreamGetBit(bs)) // color_description { - DPRINTF(DPRINTF_HEADER,"+ color_description"); + DPRINTF(XVID_DEBUG_HEADER,"+ color_description"); BitstreamSkip(bs, 8); // color_primaries BitstreamSkip(bs, 8); // transfer_characteristics BitstreamSkip(bs, 8); // matrix_coefficients @@ -501,40 +502,26 @@ } } else if ((start_code & ~VIDOBJ_START_CODE_MASK) == VIDOBJ_START_CODE) { - DPRINTF(DPRINTF_STARTCODE, ""); - DPRINTF(DPRINTF_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); + DPRINTF(XVID_DEBUG_STARTCODE, ""); + DPRINTF(XVID_DEBUG_HEADER, "vo id %i", start_code & VIDOBJ_START_CODE_MASK); BitstreamSkip(bs, 32); // video_object_start_code } else if ((start_code & ~VIDOBJLAY_START_CODE_MASK) == VIDOBJLAY_START_CODE) { - DPRINTF(DPRINTF_STARTCODE, ""); - DPRINTF(DPRINTF_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); + DPRINTF(XVID_DEBUG_STARTCODE, ""); + DPRINTF(XVID_DEBUG_HEADER, "vol id %i", start_code & VIDOBJLAY_START_CODE_MASK); BitstreamSkip(bs, 32); // video_object_layer_start_code - BitstreamSkip(bs, 1); // random_accessible_vol - // 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) != VIDOBJLAY_TYPE_ACE && - BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_ART_SIMPLE && - BitstreamShowBits(bs, 8) != 0) // BUGGY DIVX - { - DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", - BitstreamShowBits(bs, 8)); - return -1; - } - BitstreamSkip(bs, 8); - + BitstreamSkip(bs, 8); // video_object_type_indication if (BitstreamGetBit(bs)) // is_object_layer_identifier { - DPRINTF(DPRINTF_HEADER, "+ is_object_layer_identifier"); + DPRINTF(XVID_DEBUG_HEADER, "+ is_object_layer_identifier"); vol_ver_id = BitstreamGetBits(bs, 4); // video_object_layer_verid - DPRINTF(DPRINTF_HEADER,"ver_id %i", vol_ver_id); + DPRINTF(XVID_DEBUG_HEADER,"ver_id %i", vol_ver_id); BitstreamSkip(bs, 3); // video_object_layer_priority } else { vol_ver_id = 1; @@ -544,31 +531,42 @@ if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info { - DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); + DPRINTF(XVID_DEBUG_HEADER, "+ aspect_ratio_info"); dec->par_width = BitstreamGetBits(bs, 8); // par_width dec->par_height = BitstreamGetBits(bs, 8); // par_height } if (BitstreamGetBit(bs)) // vol_control_parameters { - DPRINTF(DPRINTF_HEADER, "+ vol_control_parameters"); + DPRINTF(XVID_DEBUG_HEADER, "+ vol_control_parameters"); BitstreamSkip(bs, 2); // chroma_format dec->low_delay = BitstreamGetBit(bs); // low_delay - DPRINTF(DPRINTF_HEADER, "low_delay %i", dec->low_delay); + DPRINTF(XVID_DEBUG_HEADER, "low_delay %i", dec->low_delay); if (BitstreamGetBit(bs)) // vbv_parameters { - DPRINTF(DPRINTF_HEADER,"+ vbv_parameters"); - BitstreamSkip(bs, 15); // first_half_bitrate + unsigned int bitrate; + unsigned int buffer_size; + unsigned int occupancy; + + DPRINTF(XVID_DEBUG_HEADER,"+ vbv_parameters"); + + bitrate = BitstreamGetBits(bs,15) << 15; // first_half_bit_rate READ_MARKER(); - BitstreamSkip(bs, 15); // latter_half_bitrate + bitrate |= BitstreamGetBits(bs,15); // latter_half_bit_rate READ_MARKER(); - BitstreamSkip(bs, 15); // first_half_vbv_buffer_size + + buffer_size = BitstreamGetBits(bs, 15) << 3; // first_half_vbv_buffer_size READ_MARKER(); - BitstreamSkip(bs, 3); // latter_half_vbv_buffer_size - BitstreamSkip(bs, 11); // first_half_vbv_occupancy + buffer_size |= BitstreamGetBits(bs, 3); // latter_half_vbv_buffer_size + + occupancy = BitstreamGetBits(bs, 11) << 15; // first_half_vbv_occupancy READ_MARKER(); - BitstreamSkip(bs, 15); // latter_half_vbv_occupancy + occupancy |= BitstreamGetBits(bs, 15); // latter_half_vbv_occupancy READ_MARKER(); + + DPRINTF(XVID_DEBUG_HEADER,"bitrate %d (unit=400 bps)", bitrate); + DPRINTF(XVID_DEBUG_HEADER,"buffer_size %d (unit=16384 bits)", buffer_size); + DPRINTF(XVID_DEBUG_HEADER,"occupancy %d (unit=64 bits)", occupancy); } }else{ dec->low_delay = dec->low_delay_default; @@ -576,10 +574,10 @@ dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape - DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); + DPRINTF(XVID_DEBUG_HEADER, "shape %i", dec->shape); if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { - DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported"); + DPRINTF(XVID_DEBUG_ERROR,"non-rectangular shapes are not supported"); } if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { @@ -590,7 +588,7 @@ // *************************** for decode B-frame time *********************** dec->time_inc_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution - DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution); + DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i", dec->time_inc_resolution); // dec->time_inc_resolution--; @@ -606,7 +604,7 @@ if (BitstreamGetBit(bs)) // fixed_vop_rate { - DPRINTF(DPRINTF_HEADER, "+ fixed_vop_rate"); + DPRINTF(XVID_DEBUG_HEADER, "+ fixed_vop_rate"); BitstreamSkip(bs, dec->time_inc_bits); // fixed_vop_time_increment } @@ -621,14 +619,14 @@ height = BitstreamGetBits(bs, 13); // video_object_layer_height READ_MARKER(); - DPRINTF(DPRINTF_HEADER, "width %i", width); - DPRINTF(DPRINTF_HEADER, "height %i", height); + DPRINTF(XVID_DEBUG_HEADER, "width %i", width); + DPRINTF(XVID_DEBUG_HEADER, "height %i", height); if (dec->width != width || dec->height != height) { if (dec->fixed_dimensions) { - DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); + DPRINTF(XVID_DEBUG_ERROR, "decoder width/height does not match bitstream"); return -1; } resize = 1; @@ -638,11 +636,11 @@ } dec->interlacing = BitstreamGetBit(bs); - DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing); + DPRINTF(XVID_DEBUG_HEADER, "interlacing %i", dec->interlacing); if (!BitstreamGetBit(bs)) // obmc_disable { - DPRINTF(DPRINTF_ERROR, "obmc_disabled==false not supported"); + DPRINTF(XVID_DEBUG_ERROR, "obmc_disabled==false not supported"); // TODO // fucking divx4.02 has this enabled } @@ -684,7 +682,7 @@ if (BitstreamGetBit(bs)) // not_8_bit { - DPRINTF(DPRINTF_HEADER, "not_8_bit==true (ignored)"); + DPRINTF(XVID_DEBUG_HEADER, "not_8_bit==true (ignored)"); dec->quant_bits = BitstreamGetBits(bs, 4); // quant_precision BitstreamSkip(bs, 4); // bits_per_pixel } else { @@ -698,14 +696,14 @@ } dec->quant_type = BitstreamGetBit(bs); // quant_type - DPRINTF(DPRINTF_HEADER, "quant_type %i", dec->quant_type); + DPRINTF(XVID_DEBUG_HEADER, "quant_type %i", dec->quant_type); if (dec->quant_type) { if (BitstreamGetBit(bs)) // load_intra_quant_mat { uint8_t matrix[64]; - DPRINTF(DPRINTF_HEADER, "load_intra_quant_mat"); + DPRINTF(XVID_DEBUG_HEADER, "load_intra_quant_mat"); bs_get_matrix(bs, matrix); set_intra_matrix(matrix); @@ -716,7 +714,7 @@ { uint8_t matrix[64]; - DPRINTF(DPRINTF_HEADER, "load_inter_quant_mat"); + DPRINTF(XVID_DEBUG_HEADER, "load_inter_quant_mat"); bs_get_matrix(bs, matrix); set_inter_matrix(matrix); @@ -724,7 +722,7 @@ set_inter_matrix(get_default_inter_matrix()); if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE) { - DPRINTF(DPRINTF_ERROR, "greyscale matrix not supported"); + DPRINTF(XVID_DEBUG_ERROR, "greyscale matrix not supported"); return -1; } @@ -733,7 +731,7 @@ if (vol_ver_id != 1) { dec->quarterpel = BitstreamGetBit(bs); // quarter_sample - DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel); + DPRINTF(XVID_DEBUG_HEADER,"quarterpel %i", dec->quarterpel); } else dec->quarterpel = 0; @@ -749,7 +747,7 @@ if (BitstreamGetBit(bs)) // data_partitioned { - DPRINTF(DPRINTF_ERROR, "data_partitioned not supported"); + DPRINTF(XVID_DEBUG_ERROR, "data_partitioned not supported"); BitstreamSkip(bs, 1); // reversible_vlc } @@ -757,12 +755,12 @@ dec->newpred_enable = BitstreamGetBit(bs); if (dec->newpred_enable) // newpred_enable { - DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); + DPRINTF(XVID_DEBUG_HEADER, "+ newpred_enable"); BitstreamSkip(bs, 2); // requested_upstream_message_type BitstreamSkip(bs, 1); // newpred_segment_type } dec->reduced_resolution_enable = BitstreamGetBit(bs); /* reduced_resolution_vop_enable */ - DPRINTF(DPRINTF_HEADER, "reduced_resolution_enable %i", dec->reduced_resolution_enable); + DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution_enable %i", dec->reduced_resolution_enable); } else { @@ -773,7 +771,7 @@ dec->scalability = BitstreamGetBit(bs); /* scalability */ if (dec->scalability) { - DPRINTF(DPRINTF_ERROR, "scalability not supported"); + DPRINTF(XVID_DEBUG_ERROR, "scalability not supported"); BitstreamSkip(bs, 1); /* hierarchy_type */ BitstreamSkip(bs, 4); /* ref_layer_id */ BitstreamSkip(bs, 1); /* ref_layer_sampling_direc */ @@ -798,7 +796,7 @@ dec->scalability = BitstreamGetBit(bs); /* scalability */ if (dec->scalability) { - DPRINTF(DPRINTF_ERROR, "scalability not supported"); + DPRINTF(XVID_DEBUG_ERROR, "scalability not supported"); BitstreamSkip(bs, 4); /* ref_layer_id */ BitstreamSkip(bs, 5); /* hor_sampling_factor_n */ BitstreamSkip(bs, 5); /* hor_sampling_factor_m */ @@ -815,7 +813,7 @@ } else if (start_code == GRPOFVOP_START_CODE) { - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); BitstreamSkip(bs, 32); { @@ -826,19 +824,19 @@ READ_MARKER(); seconds = BitstreamGetBits(bs, 6); - DPRINTF(DPRINTF_HEADER, "time %ih%im%is", hours); + DPRINTF(XVID_DEBUG_HEADER, "time %ih%im%is", hours,minutes,seconds); } BitstreamSkip(bs, 1); // closed_gov BitstreamSkip(bs, 1); // broken_link } else if (start_code == VOP_START_CODE) { - DPRINTF(DPRINTF_STARTCODE, ""); + DPRINTF(XVID_DEBUG_STARTCODE, ""); BitstreamSkip(bs, 32); // vop_start_code coding_type = BitstreamGetBits(bs, 2); // vop_coding_type - DPRINTF(DPRINTF_HEADER, "coding_type %i", coding_type); + DPRINTF(XVID_DEBUG_HEADER, "coding_type %i", coding_type); // *************************** for decode B-frame time *********************** while (BitstreamGetBit(bs) != 0) // time_base @@ -850,10 +848,10 @@ time_increment = (BitstreamGetBits(bs, dec->time_inc_bits)); // vop_time_increment } - DPRINTF(DPRINTF_HEADER, "time_base %i", time_incr); - DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); + DPRINTF(XVID_DEBUG_HEADER, "time_base %i", time_incr); + DPRINTF(XVID_DEBUG_HEADER, "time_increment %i", time_increment); - DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", + DPRINTF(XVID_DEBUG_TIMECODE, "%c %i:%i", coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S', time_incr, time_increment); @@ -876,14 +874,14 @@ 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); + DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i", dec->time_pp); + DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i", dec->time_bp); READ_MARKER(); if (!BitstreamGetBit(bs)) // vop_coded { - DPRINTF(DPRINTF_HEADER, "vop_coded==false"); + DPRINTF(XVID_DEBUG_HEADER, "vop_coded==false"); return N_VOP; } @@ -893,11 +891,11 @@ int vop_id_for_prediction; vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_HEADER, "vop_id %i", vop_id); + DPRINTF(XVID_DEBUG_HEADER, "vop_id %i", vop_id); if (BitstreamGetBit(bs)) /* vop_id_for_prediction_indication */ { vop_id_for_prediction = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); + DPRINTF(XVID_DEBUG_HEADER, "vop_id_for_prediction %i", vop_id_for_prediction); } READ_MARKER(); } @@ -908,7 +906,7 @@ if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) { *rounding = BitstreamGetBit(bs); // rounding_type - DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); + DPRINTF(XVID_DEBUG_HEADER, "rounding %i", *rounding); } if (dec->reduced_resolution_enable && @@ -916,7 +914,7 @@ (coding_type == P_VOP || coding_type == I_VOP)) { *reduced_resolution = BitstreamGetBit(bs); - DPRINTF(DPRINTF_HEADER, "reduced_resolution %i", *reduced_resolution); + DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution %i", *reduced_resolution); } else { @@ -938,10 +936,10 @@ vert_mc_ref = BitstreamGetBits(bs, 13); READ_MARKER(); - DPRINTF(DPRINTF_HEADER, "width %i", width); - DPRINTF(DPRINTF_HEADER, "height %i", height); - DPRINTF(DPRINTF_HEADER, "horiz_mc_ref %i", horiz_mc_ref); - DPRINTF(DPRINTF_HEADER, "vert_mc_ref %i", vert_mc_ref); + DPRINTF(XVID_DEBUG_HEADER, "width %i", width); + DPRINTF(XVID_DEBUG_HEADER, "height %i", height); + DPRINTF(XVID_DEBUG_HEADER, "horiz_mc_ref %i", horiz_mc_ref); + DPRINTF(XVID_DEBUG_HEADER, "vert_mc_ref %i", vert_mc_ref); } BitstreamSkip(bs, 1); // change_conv_ratio_disable @@ -967,9 +965,9 @@ if (dec->interlacing) { dec->top_field_first = BitstreamGetBit(bs); - DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); + DPRINTF(XVID_DEBUG_HEADER, "interlace top_field_first %i", dec->top_field_first); dec->alternate_vertical_scan = BitstreamGetBit(bs); - DPRINTF(DPRINTF_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan); + DPRINTF(XVID_DEBUG_HEADER, "interlace alternate_vertical_scan %i", dec->alternate_vertical_scan); } } @@ -1000,10 +998,10 @@ } READ_MARKER(); - gmc_mv[i].x = x; - gmc_mv[i].y = y; + gmc_warp->duv[i].x = x; + gmc_warp->duv[i].y = y; - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y); + DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y); } if (dec->sprite_brightness_change) @@ -1019,16 +1017,16 @@ if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant *quant = 1; - DPRINTF(DPRINTF_HEADER, "quant %i", *quant); + DPRINTF(XVID_DEBUG_HEADER, "quant %i", *quant); if (coding_type != I_VOP) { *fcode_forward = BitstreamGetBits(bs, 3); // fcode_forward - DPRINTF(DPRINTF_HEADER, "fcode_forward %i", *fcode_forward); + DPRINTF(XVID_DEBUG_HEADER, "fcode_forward %i", *fcode_forward); } if (coding_type == B_VOP) { *fcode_backward = BitstreamGetBits(bs, 3); // fcode_backward - DPRINTF(DPRINTF_HEADER, "fcode_backward %i", *fcode_backward); + DPRINTF(XVID_DEBUG_HEADER, "fcode_backward %i", *fcode_backward); } if (!dec->scalability) { if ((dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) && @@ -1056,7 +1054,7 @@ BitstreamSkip(bs, 8); } - DPRINTF(DPRINTF_STARTCODE, ": %s\n", tmp); + DPRINTF(XVID_DEBUG_STARTCODE, ": %s", tmp); /* divx detection */ i = sscanf(tmp, "DivX%dBuild%d%c", &version, &build, &packed); @@ -1066,14 +1064,14 @@ if (i >= 2) { dec->packed_mode = (i == 3 && packed == 'p'); - DPRINTF(DPRINTF_HEADER, "divx version=%i, build=%i packed=%i", + DPRINTF(XVID_DEBUG_HEADER, "divx version=%i, build=%i packed=%i", version, build, dec->packed_mode); } } else // start_code == ? { if (BitstreamShowBits(bs, 24) == 0x000001) { - DPRINTF(DPRINTF_STARTCODE, "", BitstreamShowBits(bs, 32)); + DPRINTF(XVID_DEBUG_STARTCODE, "", BitstreamShowBits(bs, 32)); } BitstreamSkip(bs, 8); } @@ -1111,26 +1109,53 @@ */ void BitstreamWriteVolHeader(Bitstream * const bs, - const MBParam * pParam, - const FRAMEINFO * const frame) + const MBParam * pParam) { + static const unsigned int vo_id = 0; + static const unsigned int vol_id = 0; int vol_ver_id=1; + int vol_type_ind=VIDOBJLAY_TYPE_SIMPLE; - if ( pParam->m_quarterpel || (frame->global_flags & XVID_GMC) || - (pParam->global & XVID_GLOBAL_REDUCED)) + if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) || + (pParam->vol_flags & XVID_VOL_GMC) || + (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)) vol_ver_id = 2; + + if ((pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)) { + vol_type_ind = VIDOBJLAY_TYPE_ART_SIMPLE; + } + + if ((pParam->vol_flags & XVID_VOL_QUARTERPEL) || + (pParam->vol_flags & XVID_VOL_GMC)) { + vol_type_ind = VIDOBJLAY_TYPE_ASP; + } + + // visual_object_sequence_start_code +// BitstreamPad(bs); +/* no padding here, anymore. You have to make sure that you are + byte aligned, and that always 1-8 padding bits have been written */ + + if (pParam->profile) { + BitstreamPutBits(bs, VISOBJSEQ_START_CODE, 32); + BitstreamPutBits(bs, pParam->profile, 8); + } + + // visual_object_start_code + BitstreamPad(bs); + BitstreamPutBits(bs, VISOBJ_START_CODE, 32); + BitstreamPutBits(bs, 0, 1); // is_visual_object_identifier + BitstreamPutBits(bs, VISOBJ_TYPE_VIDEO, 4); // visual_object_type // 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); + BitstreamPad(bs); + BitstreamPutBits(bs, VIDOBJLAY_START_CODE|(vol_id&0x4), 32); BitstreamPutBit(bs, 0); // random_accessible_vol - BitstreamPutBits(bs, 0, 8); // video_object_type_indication + BitstreamPutBits(bs, vol_type_ind, 8); // video_object_type_indication if (vol_ver_id == 1) { @@ -1169,8 +1194,12 @@ WRITE_MARKER(); - BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 - BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); // fixed_vop_time_increment + if (pParam->fincr>0) { + BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 + BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase)); // fixed_vop_time_increment + }else{ + BitstreamPutBit(bs, 0); // fixed_vop_rate = 0 + } WRITE_MARKER(); BitstreamPutBits(bs, pParam->width, 13); // width @@ -1178,11 +1207,11 @@ BitstreamPutBits(bs, pParam->height, 13); // height WRITE_MARKER(); - BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace + BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_INTERLACING); // interlace BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) if (vol_ver_id != 1) - { if (frame->global_flags & XVID_GMC) + { if ((pParam->vol_flags & XVID_VOL_GMC)) { BitstreamPutBits(bs, 2, 2); // sprite_enable=='GMC' BitstreamPutBits(bs, 2, 6); // no_of_sprite_warping_points BitstreamPutBits(bs, 3, 2); // sprite_warping_accuracy 0==1/2, 1=1/4, 2=1/8, 3=1/16 @@ -1200,9 +1229,9 @@ BitstreamPutBit(bs, 0); // not_8_bit // quant_type 0=h.263 1=mpeg4(quantizer tables) - BitstreamPutBit(bs, pParam->m_quant_type); + BitstreamPutBit(bs, pParam->vol_flags & XVID_VOL_MPEGQUANT); - if (pParam->m_quant_type) { + if ((pParam->vol_flags & XVID_VOL_MPEGQUANT)) { BitstreamPutBit(bs, get_intra_matrix_status()); // load_intra_quant_mat if (get_intra_matrix_status()) { bs_put_matrix(bs, get_intra_matrix()); @@ -1216,7 +1245,7 @@ } if (vol_ver_id != 1) { - if (pParam->m_quarterpel) + if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) BitstreamPutBit(bs, 1); // quarterpel else BitstreamPutBit(bs, 0); // no quarterpel @@ -1230,12 +1259,21 @@ { BitstreamPutBit(bs, 0); // newpred_enable - BitstreamPutBit(bs, (pParam->global & XVID_GLOBAL_REDUCED)?1:0); + BitstreamPutBit(bs, (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)?1:0); /* reduced_resolution_vop_enabled */ } BitstreamPutBit(bs, 0); // scalability + /* fake divx5 id, to ensure compatibility with divx5 decoder */ +#define DIVX5_ID "DivX000b000p" + if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) { + BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID)); + } + + /* xvid id */ +#define XVID_ID "XviD" XVID_BS_VERSION + BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); } @@ -1251,11 +1289,14 @@ { uint32_t i; - BitstreamPad(bs); +// BitstreamPad(bs); +/* no padding here, anymore. You have to make sure that you are + byte aligned, and that always 1-8 padding bits have been written */ + BitstreamPutBits(bs, VOP_START_CODE, 32); BitstreamPutBits(bs, frame->coding_type, 2); - DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type); + //DPRINTF(XVID_DEBUG_HEADER, "coding_type = %i", frame->coding_type); for (i = 0; i < frame->seconds; i++) { BitstreamPutBit(bs, 1); @@ -1283,54 +1324,41 @@ 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); + if ((frame->vol_flags & XVID_VOL_REDUCED_ENABLE)) + BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_REDUCED)?1:0); BitstreamPutBits(bs, 0, 3); // intra_dc_vlc_threshold - if (frame->global_flags & XVID_INTERLACING) { - BitstreamPutBit(bs, (frame->global_flags & XVID_TOPFIELDFIRST)); - BitstreamPutBit(bs, (frame->global_flags & XVID_ALTERNATESCAN)); + if ((frame->vol_flags & XVID_VOL_INTERLACING)) { + BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_TOPFIELDFIRST)); + BitstreamPutBit(bs, (frame->vop_flags & XVID_VOP_ALTERNATESCAN)); } if (frame->coding_type == S_VOP) { - if (1) { // no_of_sprite_warping_points>=1 - if (pParam->m_quarterpel) - bs_put_spritetrajectory(bs, frame->GMC_MV.x/2 ); // du[0] - else - bs_put_spritetrajectory(bs, frame->GMC_MV.x ); // du[0] - WRITE_MARKER(); + if (1) { // no_of_sprite_warping_points>=1 (we use 2!) + int k; + for (k=0;k<2;k++) + { + bs_put_spritetrajectory(bs, frame->warp.duv[k].x ); // du[k] + WRITE_MARKER(); - if (pParam->m_quarterpel) - bs_put_spritetrajectory(bs, frame->GMC_MV.y/2 ); // dv[0] - else - bs_put_spritetrajectory(bs, frame->GMC_MV.y ); // dv[0] - WRITE_MARKER(); + bs_put_spritetrajectory(bs, frame->warp.duv[k].y ); // dv[k] + WRITE_MARKER(); - - if (pParam->m_quarterpel) + if ((frame->vol_flags & XVID_VOL_QUARTERPEL)) { - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", 0, frame->GMC_MV.x/2, frame->GMC_MV.y/2); + DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", k, frame->warp.duv[k].x/2, frame->warp.duv[k].y/2); } else { - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", 0, frame->GMC_MV.x, frame->GMC_MV.y); + DPRINTF(XVID_DEBUG_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", k, frame->warp.duv[k].x, frame->warp.duv[k].y); + } } - - } -/* GMC is halfpel in bitstream, even though GMC_MV was pseudo-qpel (2*halfpel) */ - - if (2) { // no_of_sprite_warping_points>=2 (for DivX5 compat) - bs_put_spritetrajectory(bs, 0 ); - WRITE_MARKER(); - bs_put_spritetrajectory(bs, 0 ); - WRITE_MARKER(); } - // no support for brightness_change! } - DPRINTF(DPRINTF_HEADER, "quant = %i", frame->quant); + //DPRINTF(XVID_DEBUG_HEADER, "quant = %i", frame->quant); BitstreamPutBits(bs, frame->quant, 5); // quantizer