--- bitstream.c 2002/11/12 15:53:23 1.28.2.6 +++ bitstream.c 2002/09/22 17:01:36 1.33 @@ -1,77 +1,44 @@ - /****************************************************************************** - * * - * This file is part of XviD, a free MPEG-4 video encoder/decoder * - * * - * XviD is an implementation of a part of one or more MPEG-4 Video tools * - * as specified in ISO/IEC 14496-2 standard. Those intending to use this * - * software module in hardware or software products are advised that its * - * use may infringe existing patents or copyrights, and any such use * - * would be at such party's own risk. The original developer of this * - * software module and his/her company, and subsequent editors and their * - * companies, will have no liability for use of this software or * - * modifications or derivatives thereof. * - * * - * XviD is free software; you can redistribute it and/or modify it * - * under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * XviD is distributed in the hope that it will be useful, but * - * WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software * - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * * - ******************************************************************************/ - - /****************************************************************************** - * * - * bitstream.c * - * * - * Copyright (C) 2001 - Peter Ross * - * * - * For more information visit the XviD homepage: http://www.xvid.org * - * * - ******************************************************************************/ - - /****************************************************************************** - * * - * Revision history: * - * * - * 28.10.2002 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 * - * 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 * - * * - ******************************************************************************/ +/***************************************************************************** + * + * XVID MPEG-4 VIDEO CODEC + * - Bitstream reader/writer functions - + * + * Copyright (C) 2001-2002 - Peter Ross + * + * This program is an implementation of a part of one or more MPEG-4 + * Video tools as specified in ISO/IEC 14496-2 standard. Those intending + * to use this software module in hardware or software products are + * advised that its use may infringe existing patents or copyrights, and + * any such use would be at such party's own risk. The original + * developer of this software module and his/her company, and subsequent + * editors and their companies, will have no liability for use of this + * software or modifications or derivatives thereof. + * + * This program is free software ; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation ; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY ; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program ; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: bitstream.c,v 1.33 2002/09/22 17:01:36 edgomez Exp $ + * + ****************************************************************************/ - -#include #include "bitstream.h" #include "zigzag.h" #include "../quant/quant_matrix.h" -#include "mbcoding.h" +/***************************************************************************** + * Functions + ****************************************************************************/ static uint32_t __inline log2bin(uint32_t value) @@ -132,122 +99,40 @@ // for BVOP addbits == max(fcode,bcode) - 1 // returns mbpos int -read_video_packet_header(Bitstream *bs, - DECODER * dec, - const int addbits, - int * quant, - int * fcode_forward, - int * fcode_backward, - int * intra_dc_threshold) +read_video_packet_header(Bitstream *bs, const int addbits, int * quant) { - int startcode_bits = NUMBITS_VP_RESYNC_MARKER + addbits; - int mbnum_bits = log2bin(dec->mb_width * dec->mb_height - 1); + int nbits; int mbnum; - int hec = 0; + int hec; + + nbits = NUMBITS_VP_RESYNC_MARKER + addbits; BitstreamSkip(bs, BitstreamNumBitsToByteAlign(bs)); - BitstreamSkip(bs, startcode_bits); + BitstreamSkip(bs, nbits); DPRINTF(DPRINTF_STARTCODE, ""); - if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) - { - hec = BitstreamGetBit(bs); /* header_extension_code */ - if (hec && !(dec->sprite_enable == SPRITE_STATIC /* && current_coding_type = I_VOP */)) - { - BitstreamSkip(bs, 13); /* vop_width */ - READ_MARKER(); - BitstreamSkip(bs, 13); /* vop_height */ - READ_MARKER(); - BitstreamSkip(bs, 13); /* vop_horizontal_mc_spatial_ref */ - READ_MARKER(); - BitstreamSkip(bs, 13); /* vop_vertical_mc_spatial_ref */ - READ_MARKER(); - } - } - - mbnum = BitstreamGetBits(bs, mbnum_bits); /* macroblock_number */ - DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); + // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { + // hec + // vop_width + // marker_bit + // vop_height + // marker_bit - if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) - { - *quant = BitstreamGetBits(bs, 5); /* quant_scale */ - DPRINTF(DPRINTF_HEADER, "quant %i", *quant); - } + //} - if (dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR) - hec = BitstreamGetBit(bs); /* header_extension_code */ + mbnum = BitstreamGetBits(bs, 9); + DPRINTF(DPRINTF_HEADER, "mbnum %i", mbnum); + // if (dec->shape != VIDOBJLAY_SHAPE_BINARYONLY) + *quant = BitstreamGetBits(bs, 5); + DPRINTF(DPRINTF_HEADER, "quant %i", *quant); + // if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) + hec = BitstreamGetBit(bs); DPRINTF(DPRINTF_HEADER, "header_extension_code %i", hec); - if (hec) - { - int time_base; - int time_increment; - int coding_type; - - for (time_base=0; BitstreamGetBit(bs)!=0; time_base++); /* modulo_time_base */ - READ_MARKER(); - 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); - - coding_type = BitstreamGetBits(bs, 2); - DPRINTF(DPRINTF_HEADER,"coding_type %i", coding_type); - - if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) - { - BitstreamSkip(bs, 1); /* change_conv_ratio_disable */ - if (coding_type != I_VOP) - BitstreamSkip(bs, 1); /* vop_shape_coding_type */ - } - - if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) - { - *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; - - if (dec->sprite_enable == SPRITE_GMC && coding_type == S_VOP && - dec->sprite_warping_points > 0) - { - // TODO: sprite trajectory - } - if (dec->reduced_resolution_enable && - dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && - (coding_type == P_VOP || coding_type == I_VOP)) - { - BitstreamSkip(bs, 1); /* vop_reduced_resolution */ - } - - if (coding_type != I_VOP && fcode_forward) - { - *fcode_forward = BitstreamGetBits(bs, 3); - DPRINTF(DPRINTF_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); - } - } - - } - - if (dec->newpred_enable) - { - int vop_id; - int vop_id_for_prediction; - - vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_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); - } - READ_MARKER(); - } + // if (hec) + // .. decoder hec-header ... return mbnum; } @@ -266,22 +151,19 @@ BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, - uint32_t * reduced_resolution, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, - uint32_t * intra_dc_threshold, - VECTOR * gmc_mv) + uint32_t * intra_dc_threshold) { uint32_t vol_ver_id; + static uint32_t time_increment_resolution; uint32_t coding_type; uint32_t start_code; uint32_t time_incr = 0; - int32_t time_increment; - int resize = 0; + int32_t time_increment = 0; do { - BitstreamByteAlign(bs); start_code = BitstreamShowBits(bs, 32); @@ -355,12 +237,7 @@ 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 + 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 { DPRINTF(DPRINTF_ERROR,"video_object_type_indication %i not supported ", BitstreamShowBits(bs, 8)); @@ -379,13 +256,11 @@ vol_ver_id = 1; } - dec->aspect_ratio = BitstreamGetBits(bs, 4); - - if (dec->aspect_ratio == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info + if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR) // aspect_ratio_info { DPRINTF(DPRINTF_HEADER, "+ aspect_ratio_info"); - dec->par_width = BitstreamGetBits(bs, 8); // par_width - dec->par_height = BitstreamGetBits(bs, 8); // par_height + BitstreamSkip(bs, 8); // par_width + BitstreamSkip(bs, 8); // par_height } if (BitstreamGetBit(bs)) // vol_control_parameters @@ -408,16 +283,12 @@ READ_MARKER(); BitstreamSkip(bs, 15); // latter_half_vbv_occupancy READ_MARKER(); + } } dec->shape = BitstreamGetBits(bs, 2); // video_object_layer_shape - DPRINTF(DPRINTF_HEADER, "shape %i", dec->shape); - if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) - { - DPRINTF(DPRINTF_ERROR,"non-rectangular shapes are not supported"); - } if (dec->shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1) { BitstreamSkip(bs, 4); // video_object_layer_shape_extension @@ -426,13 +297,14 @@ READ_MARKER(); // *************************** 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); + time_increment_resolution = BitstreamGetBits(bs, 16); // vop_time_increment_resolution + + DPRINTF(DPRINTF_HEADER,"vop_time_increment_resolution %i", time_increment_resolution); -// dec->time_inc_resolution--; +// time_increment_resolution--; - if (dec->time_inc_resolution > 0) { - dec->time_inc_bits = log2bin(dec->time_inc_resolution-1); + if (time_increment_resolution > 0) { + dec->time_inc_bits = log2bin(time_increment_resolution-1); } else { // dec->time_inc_bits = 0; // for "old" xvid compatibility, set time_inc_bits = 1 @@ -461,21 +333,21 @@ DPRINTF(DPRINTF_HEADER, "width %i", width); DPRINTF(DPRINTF_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"); - return -1; - } - resize = 1; + // for auto set width & height + if (dec->width == 0) dec->width = width; + if (dec->height == 0) dec->height = height; + + if (width != dec->width || height != dec->height) { + DPRINTF(DPRINTF_ERROR, "XVID_DEC_PARAM width/height does not match bitstream"); + return -1; } + } dec->interlacing = BitstreamGetBit(bs); - DPRINTF(DPRINTF_HEADER, "interlacing %i", dec->interlacing); + DPRINTF(DPRINTF_HEADER, "interlace", dec->interlacing); if (!BitstreamGetBit(bs)) // obmc_disable { @@ -484,34 +356,10 @@ // fucking divx4.02 has this enabled } - dec->sprite_enable = BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)); // sprite_enable - - if (dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable == SPRITE_GMC) + if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2))) // sprite_enable { - int low_latency_sprite_enable; - - if (dec->sprite_enable != SPRITE_GMC) - { - int sprite_width; - int sprite_height; - int sprite_left_coord; - int sprite_top_coord; - sprite_width = BitstreamGetBits(bs, 13); // sprite_width - READ_MARKER(); - sprite_height = BitstreamGetBits(bs, 13); // sprite_height - READ_MARKER(); - sprite_left_coord = BitstreamGetBits(bs, 13); // sprite_left_coordinate - READ_MARKER(); - sprite_top_coord = BitstreamGetBits(bs, 13); // sprite_top_coordinate - READ_MARKER(); - } - dec->sprite_warping_points = BitstreamGetBits(bs, 6); // no_of_sprite_warping_points - dec->sprite_warping_accuracy = BitstreamGetBits(bs, 2); // sprite_warping_accuracy - dec->sprite_brightness_change = BitstreamGetBits(bs, 1); // brightness_change - if (dec->sprite_enable != SPRITE_GMC) - { - low_latency_sprite_enable = BitstreamGetBits(bs, 1); // low_latency_sprite_enable - } + DPRINTF(DPRINTF_ERROR, "spriate_enabled not supported"); + return -1; } if (vol_ver_id != 1 && @@ -569,8 +417,8 @@ if (vol_ver_id != 1) { + DPRINTF(DPRINTF_DEBUG, "QUARTERPEL BITSTREAM"); dec->quarterpel = BitstreamGetBit(bs); // quarter_sample - DPRINTF(DPRINTF_HEADER,"quarterpel %i", dec->quarterpel); } else dec->quarterpel = 0; @@ -591,25 +439,18 @@ } if (vol_ver_id != 1) { - dec->newpred_enable = BitstreamGetBit(bs); - if (dec->newpred_enable) // newpred_enable + if (BitstreamGetBit(bs)) // newpred_enable { DPRINTF(DPRINTF_HEADER, "+ newpred_enable"); 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 + if (BitstreamGetBit(bs)) // reduced_resolution_vop_enable { DPRINTF(DPRINTF_ERROR, "reduced_resolution_vop not supported"); - //return -1; + return -1; } } - else - { - dec->newpred_enable = 0; - dec->reduced_resolution_enable = 0; - } if ((dec->scalability = BitstreamGetBit(bs))) // scalability { @@ -629,8 +470,6 @@ } - return (resize ? -3 : -2 ); /* VOL */ - } else if (start_code == GRPOFVOP_START_CODE) { DPRINTF(DPRINTF_STARTCODE, ""); @@ -672,7 +511,7 @@ DPRINTF(DPRINTF_HEADER, "time_increment %i", time_increment); DPRINTF(DPRINTF_TIMECODE, "%c %i:%i", - coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : coding_type == B_VOP ? 'B' : 'S', + coding_type == I_VOP ? 'I' : coding_type == P_VOP ? 'P' : 'B', time_incr, time_increment); if (coding_type != B_VOP) { @@ -680,19 +519,19 @@ dec->time_base += time_incr; dec->time = time_increment; -/* dec->time_base * dec->time_inc_resolution + +/* dec->time_base * time_increment_resolution + time_increment; */ dec->time_pp = (uint32_t) - (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution; + (time_increment_resolution + dec->time - dec->last_non_b_time)%time_increment_resolution; dec->last_non_b_time = dec->time; } else { dec->time = time_increment; /* (dec->last_time_base + - time_incr) * dec->time_inc_resolution + time_increment; + time_incr) * time_increment_resolution + time_increment; */ dec->time_bp = (uint32_t) - (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution; + (time_increment_resolution + dec->last_non_b_time - dec->time)%time_increment_resolution; } READ_MARKER(); @@ -703,61 +542,40 @@ return N_VOP; } - if (dec->newpred_enable) - { - int vop_id; - int vop_id_for_prediction; - - vop_id = BitstreamGetBits(bs, MIN(dec->time_inc_bits + 3, 15)); - DPRINTF(DPRINTF_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); - } - READ_MARKER(); - } - - + /* if (newpred_enable) + { + } + */ // fix a little bug by MinChen if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && - ( (coding_type == P_VOP) || (coding_type == S_VOP && dec->sprite_enable == SPRITE_GMC) ) ) { + (coding_type == P_VOP)) { *rounding = BitstreamGetBit(bs); // rounding_type DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); } - if (dec->reduced_resolution_enable && - dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && - (coding_type == P_VOP || coding_type == I_VOP)) { - - *reduced_resolution = BitstreamGetBit(bs); - } - else - { - *reduced_resolution = 0; - } + /* if (reduced_resolution_enable) + { + } + */ if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { - if(!(dec->sprite_enable == SPRITE_STATIC && coding_type == I_VOP)) { + uint32_t width, height; + uint32_t horiz_mc_ref, vert_mc_ref; - uint32_t width, height; - uint32_t horiz_mc_ref, vert_mc_ref; - - width = BitstreamGetBits(bs, 13); - READ_MARKER(); - height = BitstreamGetBits(bs, 13); - READ_MARKER(); - horiz_mc_ref = BitstreamGetBits(bs, 13); - READ_MARKER(); - vert_mc_ref = BitstreamGetBits(bs, 13); - READ_MARKER(); + width = BitstreamGetBits(bs, 13); + READ_MARKER(); + height = BitstreamGetBits(bs, 13); + READ_MARKER(); + horiz_mc_ref = BitstreamGetBits(bs, 13); + READ_MARKER(); + 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(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); BitstreamSkip(bs, 1); // change_conv_ratio_disable if (BitstreamGetBit(bs)) // vop_constant_alpha @@ -766,14 +584,12 @@ } } + if (dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) { // intra_dc_vlc_threshold *intra_dc_threshold = intra_dc_threshold_table[BitstreamGetBits(bs, 3)]; - dec->top_field_first = 0; - dec->alternate_vertical_scan = 0; - if (dec->interlacing) { dec->top_field_first = BitstreamGetBit(bs); DPRINTF(DPRINTF_HEADER, "interlace top_field_first %i", dec->top_field_first); @@ -783,51 +599,9 @@ } } - if ((dec->sprite_enable == SPRITE_STATIC || dec->sprite_enable== SPRITE_GMC) && coding_type == S_VOP) { - - int i; - - for (i = 0 ; i < dec->sprite_warping_points; i++) - { - int length; - int x = 0, y = 0; - - /* sprite code borowed from ffmpeg; thx Michael Niedermayer */ - length = bs_get_spritetrajectory(bs); - if(length){ - x= BitstreamGetBits(bs, length); - if ((x >> (length - 1)) == 0) /* if MSB not set it is negative*/ - x = - (x ^ ((1 << length) - 1)); - } - READ_MARKER(); - - length = bs_get_spritetrajectory(bs); - if(length){ - y = BitstreamGetBits(bs, length); - if ((y >> (length - 1)) == 0) /* if MSB not set it is negative*/ - y = - (y ^ ((1 << length) - 1)); - } - READ_MARKER(); - - gmc_mv[i].x = x; - gmc_mv[i].y = y; - - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", i, x, y); - } - - if (dec->sprite_brightness_change) - { - // XXX: brightness_change_factor() - } - if (dec->sprite_enable == SPRITE_STATIC) - { - // XXX: todo - } - - } - if ((*quant = BitstreamGetBits(bs, dec->quant_bits)) < 1) // vop_quant *quant = 1; + DPRINTF(DPRINTF_HEADER, "quant %i", *quant); if (coding_type != I_VOP) { @@ -848,29 +622,11 @@ return coding_type; } else if (start_code == USERDATA_START_CODE) { - char tmp[256]; - int i; - BitstreamSkip(bs, 32); // user_data_start_code + DPRINTF(DPRINTF_STARTCODE, ""); - 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); - - if(strncmp(tmp, "DivX501b481p", 12) == 0) { - dec->packed_mode = 1; - DPRINTF(DPRINTF_STARTCODE, "packed_mode = %d\n", dec->packed_mode); + BitstreamSkip(bs, 32); // user_data_start_code - } } else // start_code == ? { if (BitstreamShowBits(bs, 24) == 0x000001) { @@ -913,13 +669,8 @@ void BitstreamWriteVolHeader(Bitstream * const bs, const MBParam * pParam, - const FRAMEINFO * const frame) + const FRAMEINFO * frame) { - int vol_ver_id=1; - - if ( (pParam->m_quarterpel) || (frame->global_flags & XVID_GMC) ) - vol_ver_id = 2; - // video object_start_code & vo_id BitstreamPad(bs); BitstreamPutBits(bs, VO_START_CODE, 27); @@ -931,42 +682,29 @@ BitstreamPutBit(bs, 0); // random_accessible_vol BitstreamPutBits(bs, 0, 8); // video_object_type_indication - - if (vol_ver_id == 1) - { - BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) - } - else - { - BitstreamPutBit(bs, 1); // is_object_layer_identified - BitstreamPutBits(bs, vol_ver_id, 4); // vol_ver_id == 2 - BitstreamPutBits(bs, 4, 3); // vol_ver_priority (1==lowest, 7==highest) ?? - } - + BitstreamPutBit(bs, 0); // is_object_layer_identified (0=not given) BitstreamPutBits(bs, 1, 4); // aspect_ratio_info (1=1:1) BitstreamPutBit(bs, 1); // vol_control_parameters BitstreamPutBits(bs, 1, 2); // chroma_format 1="4:2:0" - if (pParam->max_bframes > 0) { - BitstreamPutBit(bs, 0); // low_delay - } else - { - BitstreamPutBit(bs, 1); // low_delay - } + BitstreamPutBit(bs, 1); // low_delay + BitstreamPutBit(bs, 0); // vbv_parameters (0=not given) BitstreamPutBits(bs, 0, 2); // video_object_layer_shape (0=rectangular) WRITE_MARKER(); - /* time_inc_resolution; ignored by current decore versions - eg. 2fps res=2 inc=1 - 25fps res=25 inc=1 - 29.97fps res=30000 inc=1001 + /* + * time_increment_resolution; ignored by current decore versions + * eg. 2fps res=2 inc=1 + * 25fps res=25 inc=1 + * 29.97fps res=30000 inc=1001 */ BitstreamPutBits(bs, pParam->fbase, 16); + WRITE_MARKER(); BitstreamPutBit(bs, 1); // fixed_vop_rate = 1 @@ -980,24 +718,8 @@ BitstreamPutBit(bs, frame->global_flags & XVID_INTERLACING); // interlace BitstreamPutBit(bs, 1); // obmc_disable (overlapped block motion compensation) - - if (vol_ver_id != 1) - { if (frame->global_flags & XVID_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 - BitstreamPutBit(bs, 0); // sprite_brightness_change (not supported) - -/* currently we use no_of_sprite_warping_points==2, sprite_warping_accuracy==3 - for DivX5 compatability */ - - } else - BitstreamPutBits(bs, 0, 2); // sprite_enable==off - } - else - BitstreamPutBit(bs, 0); // sprite_enable==off - - BitstreamPutBit(bs, 0); // not_8_bit + BitstreamPutBit(bs, 0); // sprite_enable + BitstreamPutBit(bs, 0); // not_in_bit // quant_type 0=h.263 1=mpeg4(quantizer tables) BitstreamPutBit(bs, pParam->m_quant_type); @@ -1015,35 +737,25 @@ } - if (vol_ver_id != 1) { - if (pParam->m_quarterpel) - BitstreamPutBit(bs, 1); // quarterpel - else - BitstreamPutBit(bs, 0); // no quarterpel - } - BitstreamPutBit(bs, 1); // complexity_estimation_disable BitstreamPutBit(bs, 1); // resync_marker_disable BitstreamPutBit(bs, 0); // data_partitioned - - if (vol_ver_id != 1) - { - BitstreamPutBit(bs, 0); // newpred_enable - BitstreamPutBit(bs, 0); // reduced_resolution_vop_enabled - } - BitstreamPutBit(bs, 0); // scalability - } /* write vop header + + NOTE: doesnt handle bother with time_base & time_inc + time_base = n seconds since last resync (eg. last iframe) + time_inc = nth of a second since last resync + (decoder uses these values to determine precise time since last resync) */ void BitstreamWriteVopHeader(Bitstream * const bs, const MBParam * pParam, - const FRAMEINFO * const frame, + const FRAMEINFO * frame, int vop_coded) { uint32_t i; @@ -1052,8 +764,8 @@ BitstreamPutBits(bs, VOP_START_CODE, 32); BitstreamPutBits(bs, frame->coding_type, 2); - DPRINTF(DPRINTF_HEADER, "coding_type = %i", frame->coding_type); + // time_base = 0 write n x PutBit(1), PutBit(0) for (i = 0; i < frame->seconds; i++) { BitstreamPutBit(bs, 1); } @@ -1062,11 +774,7 @@ WRITE_MARKER(); // time_increment: value=nth_of_sec, nbits = log2(resolution) - BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase)); - /*DPRINTF("[%i:%i] %c", frame->seconds, frame->ticks, - frame->coding_type == I_VOP ? 'I' : frame->coding_type == - P_VOP ? 'P' : 'B');*/ WRITE_MARKER(); @@ -1077,52 +785,16 @@ BitstreamPutBits(bs, 1, 1); // vop_coded - if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) + if (frame->coding_type == P_VOP) BitstreamPutBits(bs, frame->rounding_type, 1); 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)); + BitstreamPutBit(bs, 1); // top field first + BitstreamPutBit(bs, 0); // alternate vertical scan } - - 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 (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(); - - if (pParam->m_quarterpel) - { - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i) *QPEL*", 0, frame->GMC_MV.x/2, frame->GMC_MV.y/2); - } - else - { - DPRINTF(DPRINTF_HEADER,"sprite_warping_point[%i] xy=(%i,%i)", 0, frame->GMC_MV.x, frame->GMC_MV.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! - } - BitstreamPutBits(bs, frame->quant, 5); // quantizer if (frame->coding_type != I_VOP) @@ -1132,19 +804,3 @@ 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); - } - -}