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