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-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: bitrate tracking and management
14 last mod: $Id: bitrate.c,v 1.23 2003/12/30 13:17:20 xiphmont Exp $
16 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "codec_internal.h"
28 /* compute bitrate tracking setup, allocate circular packet size queue */
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;
51 void vorbis_bitrate_clear(bitrate_manager_state *bm){
52 memset(bm,0,sizeof(*bm));
56 int vorbis_bitrate_managed(vorbis_block *vb){
57 vorbis_dsp_state *vd=vb->vd;
58 private_state *b=vd->backend_state;
59 bitrate_manager_state *bm=&b->bms;
61 if(bm && bm->managed)return(1);
65 /* finish taking in the block we just processed */
66 int vorbis_bitrate_addblock(vorbis_block *vb){
67 vorbis_block_internal *vbi=vb->internal;
68 vorbis_dsp_state *vd=vb->vd;
69 private_state *b=vd->backend_state;
70 bitrate_manager_state *bm=&b->bms;
71 vorbis_info *vi=vd->vi;
72 codec_setup_info *ci=vi->codec_setup;
73 bitrate_manager_info *bi=&ci->bi;
75 int choice=rint(bm->avgfloat);
76 long this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
77 long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper);
78 long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper);
79 int samples=ci->blocksizes[vb->W]>>1;
80 long desired_fill=bi->reservoir_bits*bi->reservoir_bias;
82 /* not a bitrate managed stream, but for API simplicity, we'll
83 buffer one packet to keep the code path clean */
85 if(bm->vb)return(-1); /* one has been submitted without
93 /* look ahead for avg floater */
94 if(bm->avg_bitsper>0){
96 long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
97 double slewlimit= 15./bi->slew_damp;
99 /* choosing a new floater:
100 if we're over target, we slew down
101 if we're under target, we slew up
103 choose slew as follows: look through packetblobs of this frame
104 and set slew as the first in the appropriate direction that
105 gives us the slew we want. This may mean no slew if delta is
108 Then limit slew to slew max */
110 if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
111 while(choice>0 && this_bits>avg_target_bits &&
112 bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){
114 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
116 }else if(bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
117 while(choice+1<PACKETBLOBS && this_bits<avg_target_bits &&
118 bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){
120 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
124 slew=rint(choice-bm->avgfloat)/samples*vi->rate;
125 if(slew<-slewlimit)slew=-slewlimit;
126 if(slew>slewlimit)slew=slewlimit;
127 choice=rint(bm->avgfloat+= slew/vi->rate*samples);
128 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
133 /* enforce min(if used) on the current floater (if used) */
134 if(bm->min_bitsper>0){
135 /* do we need to force the bitrate up? */
136 if(this_bits<min_target_bits){
137 while(bm->minmax_reservoir-(min_target_bits-this_bits)<0){
139 if(choice>=PACKETBLOBS)break;
140 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
145 /* enforce max (if used) on the current floater (if used) */
146 if(bm->max_bitsper>0){
147 /* do we need to force the bitrate down? */
148 if(this_bits>max_target_bits){
149 while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){
152 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
157 /* Choice of packetblobs now made based on floater, and min/max
158 requirements. Now boundary check extreme choices */
161 /* choosing a smaller packetblob is insufficient to trim bitrate.
162 frame will need to be truncated */
163 long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8;
166 if(oggpack_bytes(vbi->packetblob[choice])>maxsize){
168 oggpack_writetrunc(vbi->packetblob[choice],maxsize*8);
169 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
172 long minsize=(min_target_bits-bm->minmax_reservoir+7)/8;
173 if(choice>=PACKETBLOBS)
174 choice=PACKETBLOBS-1;
178 /* prop up bitrate according to demand. pad this frame out with zeroes */
179 minsize-=oggpack_bytes(vbi->packetblob[choice]);
180 while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8);
181 this_bits=oggpack_bytes(vbi->packetblob[choice])*8;
185 /* now we have the final packet and the final packet size. Update statistics */
186 /* min and max reservoir */
187 if(bm->min_bitsper>0 || bm->max_bitsper>0){
189 if(max_target_bits>0 && this_bits>max_target_bits){
190 bm->minmax_reservoir+=(this_bits-max_target_bits);
191 }else if(min_target_bits>0 && this_bits<min_target_bits){
192 bm->minmax_reservoir+=(this_bits-min_target_bits);
194 /* inbetween; we want to take reservoir toward but not past desired_fill */
195 if(bm->minmax_reservoir>desired_fill){
196 bm->minmax_reservoir+=(this_bits-max_target_bits);
197 if(bm->minmax_reservoir<desired_fill)bm->minmax_reservoir=desired_fill;
199 bm->minmax_reservoir+=(this_bits-min_target_bits);
200 if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill;
206 if(bm->avg_bitsper>0){
207 long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper);
208 bm->avg_reservoir+=this_bits-avg_target_bits;
214 int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){
215 private_state *b=vd->backend_state;
216 bitrate_manager_state *bm=&b->bms;
217 vorbis_block *vb=bm->vb;
218 int choice=PACKETBLOBS/2;
222 vorbis_block_internal *vbi=vb->internal;
224 if(vorbis_bitrate_managed(vb))
227 op->packet=oggpack_get_buffer(vbi->packetblob[choice]);
228 op->bytes=oggpack_bytes(vbi->packetblob[choice]);
230 op->e_o_s=vb->eofflag;
231 op->granulepos=vb->granulepos;
232 op->packetno=vb->sequence; /* for sake of completeness */