Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: bitrate tracking and management Chris@1: last mod: $Id: bitrate.c 16227 2009-07-08 06:58:46Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include "vorbis/codec.h" Chris@1: #include "codec_internal.h" Chris@1: #include "os.h" Chris@1: #include "misc.h" Chris@1: #include "bitrate.h" Chris@1: Chris@1: /* compute bitrate tracking setup */ Chris@1: void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){ Chris@1: codec_setup_info *ci=vi->codec_setup; Chris@1: bitrate_manager_info *bi=&ci->bi; Chris@1: Chris@1: memset(bm,0,sizeof(*bm)); Chris@1: Chris@1: if(bi && (bi->reservoir_bits>0)){ Chris@1: long ratesamples=vi->rate; Chris@1: int halfsamples=ci->blocksizes[0]>>1; Chris@1: Chris@1: bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0]; Chris@1: bm->managed=1; Chris@1: Chris@1: bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples); Chris@1: bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples); Chris@1: bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples); Chris@1: Chris@1: bm->avgfloat=PACKETBLOBS/2; Chris@1: Chris@1: /* not a necessary fix, but one that leads to a more balanced Chris@1: typical initialization */ Chris@1: { Chris@1: long desired_fill=bi->reservoir_bits*bi->reservoir_bias; Chris@1: bm->minmax_reservoir=desired_fill; Chris@1: bm->avg_reservoir=desired_fill; Chris@1: } Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: void vorbis_bitrate_clear(bitrate_manager_state *bm){ Chris@1: memset(bm,0,sizeof(*bm)); Chris@1: return; Chris@1: } Chris@1: Chris@1: int vorbis_bitrate_managed(vorbis_block *vb){ Chris@1: vorbis_dsp_state *vd=vb->vd; Chris@1: private_state *b=vd->backend_state; Chris@1: bitrate_manager_state *bm=&b->bms; Chris@1: Chris@1: if(bm && bm->managed)return(1); Chris@1: return(0); Chris@1: } Chris@1: Chris@1: /* finish taking in the block we just processed */ Chris@1: int vorbis_bitrate_addblock(vorbis_block *vb){ Chris@1: vorbis_block_internal *vbi=vb->internal; Chris@1: vorbis_dsp_state *vd=vb->vd; Chris@1: private_state *b=vd->backend_state; Chris@1: bitrate_manager_state *bm=&b->bms; Chris@1: vorbis_info *vi=vd->vi; Chris@1: codec_setup_info *ci=vi->codec_setup; Chris@1: bitrate_manager_info *bi=&ci->bi; Chris@1: Chris@1: int choice=rint(bm->avgfloat); Chris@1: long this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper); Chris@1: long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper); Chris@1: int samples=ci->blocksizes[vb->W]>>1; Chris@1: long desired_fill=bi->reservoir_bits*bi->reservoir_bias; Chris@1: if(!bm->managed){ Chris@1: /* not a bitrate managed stream, but for API simplicity, we'll Chris@1: buffer the packet to keep the code path clean */ Chris@1: Chris@1: if(bm->vb)return(-1); /* one has been submitted without Chris@1: being claimed */ Chris@1: bm->vb=vb; Chris@1: return(0); Chris@1: } Chris@1: Chris@1: bm->vb=vb; Chris@1: Chris@1: /* look ahead for avg floater */ Chris@1: if(bm->avg_bitsper>0){ Chris@1: double slew=0.; Chris@1: long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); Chris@1: double slewlimit= 15./bi->slew_damp; Chris@1: Chris@1: /* choosing a new floater: Chris@1: if we're over target, we slew down Chris@1: if we're under target, we slew up Chris@1: Chris@1: choose slew as follows: look through packetblobs of this frame Chris@1: and set slew as the first in the appropriate direction that Chris@1: gives us the slew we want. This may mean no slew if delta is Chris@1: already favorable. Chris@1: Chris@1: Then limit slew to slew max */ Chris@1: Chris@1: if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ Chris@1: while(choice>0 && this_bits>avg_target_bits && Chris@1: bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ Chris@1: choice--; Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: } Chris@1: }else if(bm->avg_reservoir+(this_bits-avg_target_bits)avg_reservoir+(this_bits-avg_target_bits)packetblob[choice])*8; Chris@1: } Chris@1: } Chris@1: Chris@1: slew=rint(choice-bm->avgfloat)/samples*vi->rate; Chris@1: if(slew<-slewlimit)slew=-slewlimit; Chris@1: if(slew>slewlimit)slew=slewlimit; Chris@1: choice=rint(bm->avgfloat+= slew/vi->rate*samples); Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: } Chris@1: Chris@1: Chris@1: Chris@1: /* enforce min(if used) on the current floater (if used) */ Chris@1: if(bm->min_bitsper>0){ Chris@1: /* do we need to force the bitrate up? */ Chris@1: if(this_bitsminmax_reservoir-(min_target_bits-this_bits)<0){ Chris@1: choice++; Chris@1: if(choice>=PACKETBLOBS)break; Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* enforce max (if used) on the current floater (if used) */ Chris@1: if(bm->max_bitsper>0){ Chris@1: /* do we need to force the bitrate down? */ Chris@1: if(this_bits>max_target_bits){ Chris@1: while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){ Chris@1: choice--; Chris@1: if(choice<0)break; Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* Choice of packetblobs now made based on floater, and min/max Chris@1: requirements. Now boundary check extreme choices */ Chris@1: Chris@1: if(choice<0){ Chris@1: /* choosing a smaller packetblob is insufficient to trim bitrate. Chris@1: frame will need to be truncated */ Chris@1: long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8; Chris@1: bm->choice=choice=0; Chris@1: Chris@1: if(oggpack_bytes(vbi->packetblob[choice])>maxsize){ Chris@1: Chris@1: oggpack_writetrunc(vbi->packetblob[choice],maxsize*8); Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: } Chris@1: }else{ Chris@1: long minsize=(min_target_bits-bm->minmax_reservoir+7)/8; Chris@1: if(choice>=PACKETBLOBS) Chris@1: choice=PACKETBLOBS-1; Chris@1: Chris@1: bm->choice=choice; Chris@1: Chris@1: /* prop up bitrate according to demand. pad this frame out with zeroes */ Chris@1: minsize-=oggpack_bytes(vbi->packetblob[choice]); Chris@1: while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8); Chris@1: this_bits=oggpack_bytes(vbi->packetblob[choice])*8; Chris@1: Chris@1: } Chris@1: Chris@1: /* now we have the final packet and the final packet size. Update statistics */ Chris@1: /* min and max reservoir */ Chris@1: if(bm->min_bitsper>0 || bm->max_bitsper>0){ Chris@1: Chris@1: if(max_target_bits>0 && this_bits>max_target_bits){ Chris@1: bm->minmax_reservoir+=(this_bits-max_target_bits); Chris@1: }else if(min_target_bits>0 && this_bitsminmax_reservoir+=(this_bits-min_target_bits); Chris@1: }else{ Chris@1: /* inbetween; we want to take reservoir toward but not past desired_fill */ Chris@1: if(bm->minmax_reservoir>desired_fill){ Chris@1: if(max_target_bits>0){ /* logical bulletproofing against initialization state */ Chris@1: bm->minmax_reservoir+=(this_bits-max_target_bits); Chris@1: if(bm->minmax_reservoirminmax_reservoir=desired_fill; Chris@1: }else{ Chris@1: bm->minmax_reservoir=desired_fill; Chris@1: } Chris@1: }else{ Chris@1: if(min_target_bits>0){ /* logical bulletproofing against initialization state */ Chris@1: bm->minmax_reservoir+=(this_bits-min_target_bits); Chris@1: if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill; Chris@1: }else{ Chris@1: bm->minmax_reservoir=desired_fill; Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* avg reservoir */ Chris@1: if(bm->avg_bitsper>0){ Chris@1: long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); Chris@1: bm->avg_reservoir+=this_bits-avg_target_bits; Chris@1: } Chris@1: Chris@1: return(0); Chris@1: } Chris@1: Chris@1: int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){ Chris@1: private_state *b=vd->backend_state; Chris@1: bitrate_manager_state *bm=&b->bms; Chris@1: vorbis_block *vb=bm->vb; Chris@1: int choice=PACKETBLOBS/2; Chris@1: if(!vb)return 0; Chris@1: Chris@1: if(op){ Chris@1: vorbis_block_internal *vbi=vb->internal; Chris@1: Chris@1: if(vorbis_bitrate_managed(vb)) Chris@1: choice=bm->choice; Chris@1: Chris@1: op->packet=oggpack_get_buffer(vbi->packetblob[choice]); Chris@1: op->bytes=oggpack_bytes(vbi->packetblob[choice]); Chris@1: op->b_o_s=0; Chris@1: op->e_o_s=vb->eofflag; Chris@1: op->granulepos=vb->granulepos; Chris@1: op->packetno=vb->sequence; /* for sake of completeness */ Chris@1: } Chris@1: Chris@1: bm->vb=0; Chris@1: return(1); Chris@1: }