--- trunk/xvidcore/src/bitstream/bitstream.c 2004/03/22 22:36:25 1382 +++ trunk/xvidcore/src/bitstream/bitstream.c 2005/10/06 10:46:42 1644 @@ -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.42 2004-03-22 22:36:23 edgomez Exp $ + * $Id: bitstream.c,v 1.54 2005-10-06 10:46:42 Isibaar 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, @@ -71,8 +78,10 @@ matrix[scan_tables[0][i++]] = value; } while (value != 0 && i < 64); - i--; /* fix little bug at coeff not full */ + if (value != 0) return; + + i--; while (i < 64) { matrix[scan_tables[0][i++]] = last; } @@ -185,7 +194,6 @@ DPRINTF(XVID_DEBUG_HEADER,"fcode_backward %i\n", *fcode_backward); } } - } if (dec->newpred_enable) @@ -379,7 +387,6 @@ BitstreamReadHeaders(Bitstream * bs, DECODER * dec, uint32_t * rounding, - uint32_t * reduced_resolution, uint32_t * quant, uint32_t * fcode_forward, uint32_t * fcode_backward, @@ -393,7 +400,7 @@ int32_t time_increment = 0; int resize = 0; - do { + while ((BitstreamPos(bs) >> 3) + 4 <= bs->length) { BitstreamByteAlign(bs); start_code = BitstreamShowBits(bs, 32); @@ -542,16 +549,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 = log2bin(dec->time_inc_resolution-1); + 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; } @@ -814,23 +814,12 @@ if (coding_type != B_VOP) { dec->last_time_base = dec->time_base; dec->time_base += time_incr; - dec->time = time_increment; - -#if 0 - dec->time_base * dec->time_inc_resolution + - time_increment; -#endif - dec->time_pp = (uint32_t) - (dec->time_inc_resolution + dec->time - dec->last_non_b_time)%dec->time_inc_resolution; + dec->time = dec->time_base*dec->time_inc_resolution + time_increment; + dec->time_pp = (int32_t)(dec->time - dec->last_non_b_time); dec->last_non_b_time = dec->time; } else { - dec->time = time_increment; -#if 0 - (dec->last_time_base + - time_incr) * dec->time_inc_resolution + time_increment; -#endif - dec->time_bp = (uint32_t) - (dec->time_inc_resolution + dec->last_non_b_time - dec->time)%dec->time_inc_resolution; + dec->time = (dec->last_time_base + time_incr)*dec->time_inc_resolution + time_increment; + dec->time_bp = dec->time_pp - (int32_t)(dec->last_non_b_time - dec->time); } DPRINTF(XVID_DEBUG_HEADER,"time_pp=%i\n", dec->time_pp); DPRINTF(XVID_DEBUG_HEADER,"time_bp=%i\n", dec->time_bp); @@ -871,12 +860,8 @@ dec->shape == VIDOBJLAY_SHAPE_RECTANGULAR && (coding_type == P_VOP || coding_type == I_VOP)) { - *reduced_resolution = BitstreamGetBit(bs); - DPRINTF(XVID_DEBUG_HEADER, "reduced_resolution %i\n", *reduced_resolution); - } - else - { - *reduced_resolution = 0; + if (BitstreamGetBit(bs)); + DPRINTF(XVID_DEBUG_ERROR, "RRV not supported (anymore)\n"); } if (dec->shape != VIDOBJLAY_SHAPE_RECTANGULAR) { @@ -1001,6 +986,7 @@ BitstreamSkip(bs, 32); /* user_data_start_code */ + memset(tmp, 0, 256); tmp[0] = BitstreamShowBits(bs, 8); for(i = 1; i < 256; i++){ @@ -1023,7 +1009,7 @@ else sscanf(tmp, "XviD%d", &dec->bs_version); - DPRINTF(XVID_DEBUG_HEADER, "xvid bitstream version=%i", dec->bs_version); + DPRINTF(XVID_DEBUG_HEADER, "xvid bitstream version=%i\n", dec->bs_version); } /* divx detection */ @@ -1046,7 +1032,6 @@ BitstreamSkip(bs, 8); } } - while ((BitstreamPos(bs) >> 3) < bs->length); #if 0 DPRINTF("*** WARNING: no vop_start_code found"); @@ -1091,16 +1076,11 @@ 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_QUARTERPEL) || - (pParam->vol_flags & XVID_VOL_GMC)) { + 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; } @@ -1196,8 +1176,8 @@ WRITE_MARKER(); if (pParam->fincr>0) { - BitstreamPutBit(bs, 1); /* fixed_vop_rate = 1 */ - BitstreamPutBits(bs, pParam->fincr, log2bin(pParam->fbase-1)); /* fixed_vop_time_increment */ + BitstreamPutBit(bs, 1); /* fixed_vop_rate = 1 */ + BitstreamPutBits(bs, pParam->fincr, MAX(log2bin(pParam->fbase-1),1)); /* fixed_vop_time_increment */ }else{ BitstreamPutBit(bs, 0); /* fixed_vop_rate = 0 */ } @@ -1257,31 +1237,30 @@ 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 */ BitstreamPadAlways(bs); /* next_start_code(); */ - /* fake divx5 id, to ensure compatibility with divx5 decoder */ -#define DIVX5_ID "DivX999b000p" - if (pParam->max_bframes > 0 && (pParam->global_flags & XVID_GLOBAL_PACKED)) { - BitstreamWriteUserData(bs, DIVX5_ID, strlen(DIVX5_ID)); + /* divx5 userdata string */ +#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)) + BitstreamPutBits(bs, 'p', 8); } /* xvid id */ -#define XVID_ID "XviD" XVID_BS_VERSION { - char xvid_id_string[100]; - - if (frame->vop_flags & XVID_VOP_CARTOON) - sprintf(xvid_id_string, "%sC", XVID_ID); - else - sprintf(xvid_id_string, "%s", XVID_ID); - - BitstreamWriteUserData(bs, xvid_id_string, strlen(xvid_id_string)); + const char xvid_user_format[] = "XviD%04d%c"; + char xvid_user_data[100]; + sprintf(xvid_user_data, + xvid_user_format, + XVID_BS_VERSION, + (frame->vop_flags & XVID_VOP_CARTOON)?'C':'\0'); + BitstreamWriteUserData(bs, xvid_user_data, strlen(xvid_user_data)); } } @@ -1323,8 +1302,7 @@ WRITE_MARKER(); /* time_increment: value=nth_of_sec, nbits = log2(resolution) */ - - BitstreamPutBits(bs, frame->ticks, log2bin(pParam->fbase-1)); + BitstreamPutBits(bs, frame->ticks, MAX(log2bin(pParam->fbase-1), 1)); #if 0 DPRINTF("[%i:%i] %c", frame->seconds, frame->ticks, @@ -1353,9 +1331,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, (frame->vop_flags & XVID_VOP_REDUCED)?1:0); - BitstreamPutBits(bs, 0, 3); /* intra_dc_vlc_threshold */ if ((frame->vol_flags & XVID_VOL_INTERLACING)) { @@ -1403,8 +1378,8 @@ void BitstreamWriteUserData(Bitstream * const bs, - uint8_t * data, - const int length) + const char *data, + const unsigned int length) { int i; @@ -1416,3 +1391,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 */ +}