media/libvorbis/lib/vorbis_bitrate.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial