--- bitstream.c 2002/11/02 15:52:30 1.28.2.4 +++ bitstream.c 2002/09/19 19:25:06 1.31 @@ -1,76 +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.31 2002/09/19 19:25:06 edgomez Exp $ + * + ****************************************************************************/ #include "bitstream.h" #include "zigzag.h" #include "../quant/quant_matrix.h" -#include "mbcoding.h" +/***************************************************************************** + * Functions + ****************************************************************************/ static uint32_t __inline log2bin(uint32_t value) @@ -193,7 +161,7 @@ uint32_t coding_type; uint32_t start_code; uint32_t time_incr = 0; - int32_t time_increment; + int32_t time_increment = 0; do { BitstreamByteAlign(bs); @@ -581,7 +549,7 @@ // fix a little bug by MinChen if ((dec->shape != VIDOBJLAY_SHAPE_BINARY_ONLY) && - ( (coding_type == P_VOP) || (coding_type == S_VOP) ) ) { + (coding_type == P_VOP)) { *rounding = BitstreamGetBit(bs); // rounding_type DPRINTF(DPRINTF_HEADER, "rounding %i", *rounding); } @@ -622,9 +590,6 @@ *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); @@ -704,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); @@ -722,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_increment_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 @@ -771,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); @@ -806,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; @@ -844,6 +765,7 @@ BitstreamPutBits(bs, frame->coding_type, 2); + // time_base = 0 write n x PutBit(1), PutBit(0) for (i = 0; i < frame->seconds; i++) { BitstreamPutBit(bs, 1); } @@ -852,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\n", frame->seconds, frame->ticks, - frame->coding_type == I_VOP ? 'I' : frame->coding_type == - P_VOP ? 'P' : 'B');*/ WRITE_MARKER(); @@ -867,41 +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(); - } -/* 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) @@ -911,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); - } - -}