1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: bitrate tracking and management
14 last mod: $Id: bitrate.c 16227 2009-07-08 06:58:46Z xiphmont $
16 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "codec_internal.h"
28 /* compute bitrate tracking setup */
29 void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){
30 codec_setup_info *ci=vi->codec_setup;
31 bitrate_manager_info *bi=&ci->bi;
33 memset(bm,0,sizeof(*bm));
35 if(bi && (bi->reservoir_bits>0)){
36 long ratesamples=vi->rate;
37 int halfsamples=ci->blocksizes[0]>>1;
39 bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0];
42 bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples);
43 bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples);
44 bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples);
46 bm->avgfloat=PACKETBLOBS/2;
48 /* not a necessary fix, but one that leads to a more balanced
49 typical initialization */
51 long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
52 bm->minmax_reservoir=desired_fill;
53 bm->avg_reservoir=desired_fill;
59 void vorbis_bitrate_clear(bitrate_manager_state *bm){
60 memset(bm,0,sizeof(*bm));
64 int vorbis_bitrate_managed(vorbis_block *vb){
65 vorbis_dsp_state *vd=vb->vd;
66 private_state *b=vd->backend_state;
67 bitrate_manager_state *bm=&b->bms;
69 if(bm && bm->managed)return(1);
73 /* finish taking in the block we just processed */
74 int vorbis_bitrate_addblock(vorbis_block *vb){
75 vorbis_block_internal *vbi=vb->internal;
76 vorbis_dsp_state *vd=vb->vd;
77 private_state *b=vd->backend_state;
78 bitrate_manager_state *bm=&b->bms;
79 vorbis_info *vi=vd->vi;
80 codec_setup_info *ci=vi->codec_setup;
81 bitrate_manager_info *bi=&ci->bi;
83 int choice=rint(bm->avgfloat);
84 long this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
85 long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper);
86 long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper);
87 int samples=ci->blocksizes[vb->W]>>1;
88 long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
90 /* not a bitrate managed stream, but for API simplicity, we'll
91 buffer the packet to keep the code path clean */
93 if(bm->vb)return(-1); /* one has been submitted without
101 /* look ahead for avg floater */
102 if(bm->avg_bitsper>0){
104 long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
105 double slewlimit= 15./bi->slew_damp;
107 /* choosing a new floater:
108 if we're over target, we slew down
109 if we're under target, we slew up
111 choose slew as follows: look through packetblobs of this frame
112 and set slew as the first in the appropriate direction that
113 gives us the slew we want. This may mean no slew if delta is
116 Then limit slew to slew max */
118 if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
119 while(choice>0 && this_bits>avg_target_bits &&
120 bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
122 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
124 }else if(bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
125 while(choice+1<PACKETBLOBS && this_bits<avg_target_bits &&
126 bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
128 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
132 slew=rint(choice-bm->avgfloat)/samples*vi->rate;
133 if(slew<-slewlimit)slew=-slewlimit;
134 if(slew>slewlimit)slew=slewlimit;
135 choice=rint(bm->avgfloat+= slew/vi->rate*samples);
136 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
141 /* enforce min(if used) on the current floater (if used) */
142 if(bm->min_bitsper>0){
143 /* do we need to force the bitrate up? */
144 if(this_bits<min_target_bits){
145 while(bm->minmax_reservoir-(min_target_bits-this_bits)<0){
147 if(choice>=PACKETBLOBS)break;
148 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
153 /* enforce max (if used) on the current floater (if used) */
154 if(bm->max_bitsper>0){
155 /* do we need to force the bitrate down? */
156 if(this_bits>max_target_bits){
157 while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){
160 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
165 /* Choice of packetblobs now made based on floater, and min/max
166 requirements. Now boundary check extreme choices */
169 /* choosing a smaller packetblob is insufficient to trim bitrate.
170 frame will need to be truncated */
171 long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8;
174 if(oggpack_bytes(vbi->packetblob[choice])>maxsize){
176 oggpack_writetrunc(vbi->packetblob[choice],maxsize*8);
177 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
180 long minsize=(min_target_bits-bm->minmax_reservoir+7)/8;
181 if(choice>=PACKETBLOBS)
182 choice=PACKETBLOBS-1;
186 /* prop up bitrate according to demand. pad this frame out with zeroes */
187 minsize-=oggpack_bytes(vbi->packetblob[choice]);
188 while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8);
189 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
193 /* now we have the final packet and the final packet size. Update statistics */
194 /* min and max reservoir */
195 if(bm->min_bitsper>0 || bm->max_bitsper>0){
197 if(max_target_bits>0 && this_bits>max_target_bits){
198 bm->minmax_reservoir+=(this_bits-max_target_bits);
199 }else if(min_target_bits>0 && this_bits<min_target_bits){
200 bm->minmax_reservoir+=(this_bits-min_target_bits);
202 /* inbetween; we want to take reservoir toward but not past desired_fill */
203 if(bm->minmax_reservoir>desired_fill){
204 if(max_target_bits>0){ /* logical bulletproofing against initialization state */
205 bm->minmax_reservoir+=(this_bits-max_target_bits);
206 if(bm->minmax_reservoir<desired_fill)bm->minmax_reservoir=desired_fill;
208 bm->minmax_reservoir=desired_fill;
211 if(min_target_bits>0){ /* logical bulletproofing against initialization state */
212 bm->minmax_reservoir+=(this_bits-min_target_bits);
213 if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill;
215 bm->minmax_reservoir=desired_fill;
222 if(bm->avg_bitsper>0){
223 long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
224 bm->avg_reservoir+=this_bits-avg_target_bits;
230 int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
231 private_state *b=vd->backend_state;
232 bitrate_manager_state *bm=&b->bms;
233 vorbis_block *vb=bm->vb;
234 int choice=PACKETBLOBS/2;
238 vorbis_block_internal *vbi=vb->internal;
240 if(vorbis_bitrate_managed(vb))
243 op->packet=oggpack_get_buffer(vbi->packetblob[choice]);
244 op->bytes=oggpack_bytes(vbi->packetblob[choice]);
246 op->e_o_s=vb->eofflag;
247 op->granulepos=vb->granulepos;
248 op->packetno=vb->sequence; /* for sake of completeness */