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