vrshoot

annotate libs/vorbis/bitrate.c @ 0:b2f14e535253

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