| 1 |
/******************************************************************** |
| 2 |
* * |
| 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. * |
| 7 |
* * |
| 8 |
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * |
| 9 |
* by the Xiph.Org Foundation https://xiph.org/ * |
| 10 |
* * |
| 11 |
******************************************************************** |
| 12 |
|
| 13 |
function: bitrate tracking and management |
| 14 |
|
| 15 |
********************************************************************/ |
| 16 |
|
| 17 |
#include "ogg.h" |
| 18 |
#include "vorbis_codec.h" |
| 19 |
#include "vorbis_codec_internal.h" |
| 20 |
#include "vorbis_os.h" |
| 21 |
#include "vorbis_misc.h" |
| 22 |
#include "vorbis_bitrate.h" |
| 23 |
|
| 24 |
/* compute bitrate tracking setup */ |
| 25 |
void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){ |
| 26 |
codec_setup_info *ci = (codec_setup_info *) vi->codec_setup; |
| 27 |
bitrate_manager_info *bi=&ci->bi; |
| 28 |
|
| 29 |
memset(bm,0,sizeof(*bm)); |
| 30 |
|
| 31 |
if(bi && (bi->reservoir_bits>0)){ |
| 32 |
long ratesamples=vi->rate; |
| 33 |
int halfsamples=ci->blocksizes[0]>>1; |
| 34 |
|
| 35 |
bm->short_per_long=ci->blocksizes[1]/ci->blocksizes[0]; |
| 36 |
bm->managed=1; |
| 37 |
|
| 38 |
bm->avg_bitsper= rint(1.*bi->avg_rate*halfsamples/ratesamples); |
| 39 |
bm->min_bitsper= rint(1.*bi->min_rate*halfsamples/ratesamples); |
| 40 |
bm->max_bitsper= rint(1.*bi->max_rate*halfsamples/ratesamples); |
| 41 |
|
| 42 |
bm->avgfloat=PACKETBLOBS/2; |
| 43 |
|
| 44 |
/* not a necessary fix, but one that leads to a more balanced |
| 45 |
typical initialization */ |
| 46 |
{ |
| 47 |
long desired_fill=bi->reservoir_bits*bi->reservoir_bias; |
| 48 |
bm->minmax_reservoir=desired_fill; |
| 49 |
bm->avg_reservoir=desired_fill; |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void vorbis_bitrate_clear(bitrate_manager_state *bm){ |
| 56 |
memset(bm,0,sizeof(*bm)); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
int vorbis_bitrate_managed(vorbis_block *vb){ |
| 61 |
vorbis_dsp_state *vd=vb->vd; |
| 62 |
private_state *b = (private_state *) vd->backend_state; |
| 63 |
bitrate_manager_state *bm=&b->bms; |
| 64 |
|
| 65 |
if(bm && bm->managed)return(1); |
| 66 |
return(0); |
| 67 |
} |
| 68 |
|
| 69 |
/* finish taking in the block we just processed */ |
| 70 |
int vorbis_bitrate_addblock(vorbis_block *vb){ |
| 71 |
vorbis_block_internal *vbi = (vorbis_block_internal *) vb->internal; |
| 72 |
vorbis_dsp_state *vd=vb->vd; |
| 73 |
private_state *b = (private_state *) vd->backend_state; |
| 74 |
bitrate_manager_state *bm=&b->bms; |
| 75 |
vorbis_info *vi=vd->vi; |
| 76 |
codec_setup_info *ci = ( codec_setup_info *) vi->codec_setup; |
| 77 |
bitrate_manager_info *bi=&ci->bi; |
| 78 |
|
| 79 |
int choice=rint(bm->avgfloat); |
| 80 |
long this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 81 |
long min_target_bits=(vb->W?bm->min_bitsper*bm->short_per_long:bm->min_bitsper); |
| 82 |
long max_target_bits=(vb->W?bm->max_bitsper*bm->short_per_long:bm->max_bitsper); |
| 83 |
int samples=ci->blocksizes[vb->W]>>1; |
| 84 |
long desired_fill=bi->reservoir_bits*bi->reservoir_bias; |
| 85 |
if(!bm->managed){ |
| 86 |
/* not a bitrate managed stream, but for API simplicity, we'll |
| 87 |
buffer the packet to keep the code path clean */ |
| 88 |
|
| 89 |
if(bm->vb)return(-1); /* one has been submitted without |
| 90 |
being claimed */ |
| 91 |
bm->vb=vb; |
| 92 |
return(0); |
| 93 |
} |
| 94 |
|
| 95 |
bm->vb=vb; |
| 96 |
|
| 97 |
/* look ahead for avg floater */ |
| 98 |
if(bm->avg_bitsper>0){ |
| 99 |
double slew=0.; |
| 100 |
long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); |
| 101 |
double slewlimit= 15./bi->slew_damp; |
| 102 |
|
| 103 |
/* choosing a new floater: |
| 104 |
if we're over target, we slew down |
| 105 |
if we're under target, we slew up |
| 106 |
|
| 107 |
choose slew as follows: look through packetblobs of this frame |
| 108 |
and set slew as the first in the appropriate direction that |
| 109 |
gives us the slew we want. This may mean no slew if delta is |
| 110 |
already favorable. |
| 111 |
|
| 112 |
Then limit slew to slew max */ |
| 113 |
|
| 114 |
if(bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ |
| 115 |
while(choice>0 && this_bits>avg_target_bits && |
| 116 |
bm->avg_reservoir+(this_bits-avg_target_bits)>desired_fill){ |
| 117 |
choice--; |
| 118 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 119 |
} |
| 120 |
}else if(bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){ |
| 121 |
while(choice+1<PACKETBLOBS && this_bits<avg_target_bits && |
| 122 |
bm->avg_reservoir+(this_bits-avg_target_bits)<desired_fill){ |
| 123 |
choice++; |
| 124 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
slew=rint(choice-bm->avgfloat)/samples*vi->rate; |
| 129 |
if(slew<-slewlimit)slew=-slewlimit; |
| 130 |
if(slew>slewlimit)slew=slewlimit; |
| 131 |
choice=rint(bm->avgfloat+= slew/vi->rate*samples); |
| 132 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
/* enforce min(if used) on the current floater (if used) */ |
| 138 |
if(bm->min_bitsper>0){ |
| 139 |
/* do we need to force the bitrate up? */ |
| 140 |
if(this_bits<min_target_bits){ |
| 141 |
while(bm->minmax_reservoir-(min_target_bits-this_bits)<0){ |
| 142 |
choice++; |
| 143 |
if(choice>=PACKETBLOBS)break; |
| 144 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* enforce max (if used) on the current floater (if used) */ |
| 150 |
if(bm->max_bitsper>0){ |
| 151 |
/* do we need to force the bitrate down? */ |
| 152 |
if(this_bits>max_target_bits){ |
| 153 |
while(bm->minmax_reservoir+(this_bits-max_target_bits)>bi->reservoir_bits){ |
| 154 |
choice--; |
| 155 |
if(choice<0)break; |
| 156 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/* Choice of packetblobs now made based on floater, and min/max |
| 162 |
requirements. Now boundary check extreme choices */ |
| 163 |
|
| 164 |
if(choice<0){ |
| 165 |
/* choosing a smaller packetblob is insufficient to trim bitrate. |
| 166 |
frame will need to be truncated */ |
| 167 |
long maxsize=(max_target_bits+(bi->reservoir_bits-bm->minmax_reservoir))/8; |
| 168 |
bm->choice=choice=0; |
| 169 |
|
| 170 |
if(oggpack_bytes(vbi->packetblob[choice])>maxsize){ |
| 171 |
|
| 172 |
oggpack_writetrunc(vbi->packetblob[choice],maxsize*8); |
| 173 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 174 |
} |
| 175 |
}else{ |
| 176 |
long minsize=(min_target_bits-bm->minmax_reservoir+7)/8; |
| 177 |
if(choice>=PACKETBLOBS) |
| 178 |
choice=PACKETBLOBS-1; |
| 179 |
|
| 180 |
bm->choice=choice; |
| 181 |
|
| 182 |
/* prop up bitrate according to demand. pad this frame out with zeroes */ |
| 183 |
minsize-=oggpack_bytes(vbi->packetblob[choice]); |
| 184 |
while(minsize-->0)oggpack_write(vbi->packetblob[choice],0,8); |
| 185 |
this_bits=oggpack_bytes(vbi->packetblob[choice])*8; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/* now we have the final packet and the final packet size. Update statistics */ |
| 190 |
/* min and max reservoir */ |
| 191 |
if(bm->min_bitsper>0 || bm->max_bitsper>0){ |
| 192 |
|
| 193 |
if(max_target_bits>0 && this_bits>max_target_bits){ |
| 194 |
bm->minmax_reservoir+=(this_bits-max_target_bits); |
| 195 |
}else if(min_target_bits>0 && this_bits<min_target_bits){ |
| 196 |
bm->minmax_reservoir+=(this_bits-min_target_bits); |
| 197 |
}else{ |
| 198 |
/* inbetween; we want to take reservoir toward but not past desired_fill */ |
| 199 |
if(bm->minmax_reservoir>desired_fill){ |
| 200 |
if(max_target_bits>0){ /* logical bulletproofing against initialization state */ |
| 201 |
bm->minmax_reservoir+=(this_bits-max_target_bits); |
| 202 |
if(bm->minmax_reservoir<desired_fill)bm->minmax_reservoir=desired_fill; |
| 203 |
}else{ |
| 204 |
bm->minmax_reservoir=desired_fill; |
| 205 |
} |
| 206 |
}else{ |
| 207 |
if(min_target_bits>0){ /* logical bulletproofing against initialization state */ |
| 208 |
bm->minmax_reservoir+=(this_bits-min_target_bits); |
| 209 |
if(bm->minmax_reservoir>desired_fill)bm->minmax_reservoir=desired_fill; |
| 210 |
}else{ |
| 211 |
bm->minmax_reservoir=desired_fill; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* avg reservoir */ |
| 218 |
if(bm->avg_bitsper>0){ |
| 219 |
long avg_target_bits=(vb->W?bm->avg_bitsper*bm->short_per_long:bm->avg_bitsper); |
| 220 |
bm->avg_reservoir+=this_bits-avg_target_bits; |
| 221 |
} |
| 222 |
|
| 223 |
return(0); |
| 224 |
} |
| 225 |
|
| 226 |
int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){ |
| 227 |
private_state *b = (private_state *) vd->backend_state; |
| 228 |
bitrate_manager_state *bm=&b->bms; |
| 229 |
vorbis_block *vb=bm->vb; |
| 230 |
int choice=PACKETBLOBS/2; |
| 231 |
if(!vb)return 0; |
| 232 |
|
| 233 |
if(op){ |
| 234 |
vorbis_block_internal *vbi = (vorbis_block_internal *) vb->internal; |
| 235 |
|
| 236 |
if(vorbis_bitrate_managed(vb)) |
| 237 |
choice=bm->choice; |
| 238 |
|
| 239 |
op->packet=oggpack_get_buffer(vbi->packetblob[choice]); |
| 240 |
op->bytes=oggpack_bytes(vbi->packetblob[choice]); |
| 241 |
op->b_o_s=0; |
| 242 |
op->e_o_s=vb->eofflag; |
| 243 |
op->granulepos=vb->granulepos; |
| 244 |
op->packetno=vb->sequence; /* for sake of completeness */ |
| 245 |
} |
| 246 |
|
| 247 |
bm->vb=0; |
| 248 |
return(1); |
| 249 |
} |