--- trunk/xvidcore/src/bitstream/bitstream.c 2005/04/10 00:27:25 1610 +++ trunk/xvidcore/src/bitstream/bitstream.c 2006/03/27 11:21:48 1696 @@ -20,7 +20,7 @@ * 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.50 2005-04-10 00:27:25 suxen_drol Exp $ + * $Id: bitstream.c,v 1.56 2006-03-27 11:21:48 Skal Exp $ * ****************************************************************************/ @@ -33,19 +33,26 @@ #include "mbcoding.h" -static uint32_t __inline -log2bin(uint32_t value) +static const uint8_t log2_tab_16[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; + +static uint32_t __inline log2bin(uint32_t value) { - int n = 0; - - while (value) { - value >>= 1; - n++; - } - return n; + int n = 0; + if (value & 0xffff0000) { + value >>= 16; + n += 16; + } + if (value & 0xff00) { + value >>= 8; + n += 8; + } + if (value & 0xf0) { + value >>= 4; + n += 4; + } + return n + log2_tab_16[value]; } - static const uint32_t intra_dc_threshold_table[] = { 32, /* never use */ 13, @@ -58,7 +65,7 @@ }; -void +static void bs_get_matrix(Bitstream * bs, uint8_t * matrix) { @@ -187,7 +194,6 @@ DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward); } } - } if (dec->newpred_enable) @@ -417,18 +423,17 @@ DPRINTF(XVID_DEBUG_STARTCODE, "\n"); } else if (start_code == VISOBJ_START_CODE) { - int visobj_ver_id; DPRINTF(XVID_DEBUG_STARTCODE, "\n"); BitstreamSkip(bs, 32); /* visual_object_start_code */ if (BitstreamGetBit(bs)) /* is_visual_object_identified */ { - visobj_ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */ - DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", visobj_ver_id); + dec->ver_id = BitstreamGetBits(bs, 4); /* visual_object_ver_id */ + DPRINTF(XVID_DEBUG_HEADER,"visobj_ver_id %i\n", dec->ver_id); BitstreamSkip(bs, 3); /* visual_object_priority */ } else { - visobj_ver_id = 1; + dec->ver_id = 1; } if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO) /* visual_object_type */ @@ -477,7 +482,7 @@ DPRINTF(XVID_DEBUG_HEADER,"ver_id %i\n", vol_ver_id); BitstreamSkip(bs, 3); /* video_object_layer_priority */ } else { - vol_ver_id = 1; + vol_ver_id = dec->ver_id; } dec->aspect_ratio = BitstreamGetBits(bs, 4); @@ -543,16 +548,9 @@ dec->time_inc_resolution = BitstreamGetBits(bs, 16); /* vop_time_increment_resolution */ DPRINTF(XVID_DEBUG_HEADER,"vop_time_increment_resolution %i\n", dec->time_inc_resolution); -#if 0 - dec->time_inc_resolution--; -#endif - if (dec->time_inc_resolution > 0) { dec->time_inc_bits = MAX(log2bin(dec->time_inc_resolution-1), 1); } else { -#if 0 - dec->time_inc_bits = 0; -#endif /* for "old" xvid compatibility, set time_inc_bits = 1 */ dec->time_inc_bits = 1; } @@ -1077,14 +1075,9 @@ int vol_profile = pParam->profile; if ( (pParam->vol_flags & XVID_VOL_QUARTERPEL) || - (pParam->vol_flags & XVID_VOL_GMC) || - (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)) + (pParam->vol_flags & XVID_VOL_GMC)) 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_MPEGQUANT|XVID_VOL_QUARTERPEL|XVID_VOL_GMC|XVID_VOL_INTERLACING)) || pParam->max_bframes>0) { vol_type_ind = VIDOBJLAY_TYPE_ASP; @@ -1243,8 +1236,7 @@ if (vol_ver_id != 1) { BitstreamPutBit(bs, 0); /* newpred_enable */ - BitstreamPutBit(bs, (pParam->vol_flags & XVID_VOL_REDUCED_ENABLE)?1:0); - /* reduced_resolution_vop_enabled */ + BitstreamPutBit(bs, 0); /* reduced_resolution_vop_enabled */ } BitstreamPutBit(bs, 0); /* scalability */ @@ -1252,7 +1244,7 @@ BitstreamPadAlways(bs); /* next_start_code(); */ /* divx5 userdata string */ -#define DIVX5_ID "DivX503b1393" +#define DIVX5_ID ((char *)"DivX503b1393") if ((pParam->global_flags & XVID_GLOBAL_DIVX5_USERDATA)) { BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID)); if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) @@ -1338,9 +1330,6 @@ if ( (frame->coding_type == P_VOP) || (frame->coding_type == S_VOP) ) BitstreamPutBits(bs, frame->rounding_type, 1); - if ((frame->vol_flags & XVID_VOL_REDUCED_ENABLE)) - BitstreamPutBit(bs, 0); - BitstreamPutBits(bs, 0, 3); /* intra_dc_vlc_threshold */ if ((frame->vol_flags & XVID_VOL_INTERLACING)) { @@ -1388,8 +1377,8 @@ void BitstreamWriteUserData(Bitstream * const bs, - uint8_t * data, - const int length) + const char *data, + const unsigned int length) { int i; @@ -1401,3 +1390,64 @@ } } + +/* + * Group of VOP + */ +void +BitstreamWriteGroupOfVopHeader(Bitstream * const bs, + const MBParam * pParam, + uint32_t is_closed_gov) +{ + int64_t time = (pParam->m_stamp + (pParam->fbase/2)) / pParam->fbase; + int hours, minutes, seconds; + + /* compute time_code */ + seconds = time % 60; time /= 60; + minutes = time % 60; time /= 60; + hours = time % 24; /* don't overflow */ + + BitstreamPutBits(bs, GRPOFVOP_START_CODE, 32); + BitstreamPutBits(bs, hours, 5); + BitstreamPutBits(bs, minutes, 6); + BitstreamPutBit(bs, 1); + BitstreamPutBits(bs, seconds, 6); + BitstreamPutBits(bs, is_closed_gov, 1); + BitstreamPutBits(bs, 0, 1); /* broken_link */ +} + +/* + * End of Sequence + */ +void +BitstreamWriteEndOfSequence(Bitstream * const bs) +{ + BitstreamPadAlways(bs); + BitstreamPutBits(bs, VISOBJSEQ_STOP_CODE, 32); +} + +/* + * Video Packet (resync marker) + */ + +void write_video_packet_header(Bitstream * const bs, + const MBParam * pParam, + const FRAMEINFO * const frame, + int mbnum) +{ + const int mbnum_bits = log2bin(pParam->mb_width * pParam->mb_height - 1); + uint32_t nbitsresyncmarker; + + if (frame->coding_type == I_VOP) + nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER; /* 16 zeros followed by a 1. */ + else if (frame->coding_type == P_VOP) + nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER-1 + frame->fcode; + else /* B_VOP */ + nbitsresyncmarker = MAX(NUMBITS_VP_RESYNC_MARKER, NUMBITS_VP_RESYNC_MARKER-1 + MAX(frame->fcode, frame->bcode)); + + BitstreamPadAlways(bs); + BitstreamPutBits(bs, RESYNC_MARKER, nbitsresyncmarker); + BitstreamPutBits(bs, mbnum, mbnum_bits); + BitstreamPutBits(bs, frame->quant, 5); + BitstreamPutBit(bs, 0); /* hec */ +}