media/libvorbis/lib/vorbis_floor1.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: floor backend 1 implementation
michael@0 14 last mod: $Id: floor1.c 19031 2013-12-03 19:20:50Z tterribe $
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 "registry.h"
michael@0 25 #include "codebook.h"
michael@0 26 #include "misc.h"
michael@0 27 #include "scales.h"
michael@0 28
michael@0 29 #include <stdio.h>
michael@0 30
michael@0 31 #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */
michael@0 32
michael@0 33 typedef struct lsfit_acc{
michael@0 34 int x0;
michael@0 35 int x1;
michael@0 36
michael@0 37 int xa;
michael@0 38 int ya;
michael@0 39 int x2a;
michael@0 40 int y2a;
michael@0 41 int xya;
michael@0 42 int an;
michael@0 43
michael@0 44 int xb;
michael@0 45 int yb;
michael@0 46 int x2b;
michael@0 47 int y2b;
michael@0 48 int xyb;
michael@0 49 int bn;
michael@0 50 } lsfit_acc;
michael@0 51
michael@0 52 /***********************************************/
michael@0 53
michael@0 54 static void floor1_free_info(vorbis_info_floor *i){
michael@0 55 vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
michael@0 56 if(info){
michael@0 57 memset(info,0,sizeof(*info));
michael@0 58 _ogg_free(info);
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 static void floor1_free_look(vorbis_look_floor *i){
michael@0 63 vorbis_look_floor1 *look=(vorbis_look_floor1 *)i;
michael@0 64 if(look){
michael@0 65 /*fprintf(stderr,"floor 1 bit usage %f:%f (%f total)\n",
michael@0 66 (float)look->phrasebits/look->frames,
michael@0 67 (float)look->postbits/look->frames,
michael@0 68 (float)(look->postbits+look->phrasebits)/look->frames);*/
michael@0 69
michael@0 70 memset(look,0,sizeof(*look));
michael@0 71 _ogg_free(look);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 static int ilog(unsigned int v){
michael@0 76 int ret=0;
michael@0 77 while(v){
michael@0 78 ret++;
michael@0 79 v>>=1;
michael@0 80 }
michael@0 81 return(ret);
michael@0 82 }
michael@0 83
michael@0 84 static int ilog2(unsigned int v){
michael@0 85 int ret=0;
michael@0 86 if(v)--v;
michael@0 87 while(v){
michael@0 88 ret++;
michael@0 89 v>>=1;
michael@0 90 }
michael@0 91 return(ret);
michael@0 92 }
michael@0 93
michael@0 94 static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){
michael@0 95 vorbis_info_floor1 *info=(vorbis_info_floor1 *)i;
michael@0 96 int j,k;
michael@0 97 int count=0;
michael@0 98 int rangebits;
michael@0 99 int maxposit=info->postlist[1];
michael@0 100 int maxclass=-1;
michael@0 101
michael@0 102 /* save out partitions */
michael@0 103 oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */
michael@0 104 for(j=0;j<info->partitions;j++){
michael@0 105 oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */
michael@0 106 if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
michael@0 107 }
michael@0 108
michael@0 109 /* save out partition classes */
michael@0 110 for(j=0;j<maxclass+1;j++){
michael@0 111 oggpack_write(opb,info->class_dim[j]-1,3); /* 1 to 8 */
michael@0 112 oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */
michael@0 113 if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8);
michael@0 114 for(k=0;k<(1<<info->class_subs[j]);k++)
michael@0 115 oggpack_write(opb,info->class_subbook[j][k]+1,8);
michael@0 116 }
michael@0 117
michael@0 118 /* save out the post list */
michael@0 119 oggpack_write(opb,info->mult-1,2); /* only 1,2,3,4 legal now */
michael@0 120 oggpack_write(opb,ilog2(maxposit),4);
michael@0 121 rangebits=ilog2(maxposit);
michael@0 122
michael@0 123 for(j=0,k=0;j<info->partitions;j++){
michael@0 124 count+=info->class_dim[info->partitionclass[j]];
michael@0 125 for(;k<count;k++)
michael@0 126 oggpack_write(opb,info->postlist[k+2],rangebits);
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 static int icomp(const void *a,const void *b){
michael@0 131 return(**(int **)a-**(int **)b);
michael@0 132 }
michael@0 133
michael@0 134 static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){
michael@0 135 codec_setup_info *ci=vi->codec_setup;
michael@0 136 int j,k,count=0,maxclass=-1,rangebits;
michael@0 137
michael@0 138 vorbis_info_floor1 *info=_ogg_calloc(1,sizeof(*info));
michael@0 139 /* read partitions */
michael@0 140 info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */
michael@0 141 for(j=0;j<info->partitions;j++){
michael@0 142 info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */
michael@0 143 if(info->partitionclass[j]<0)goto err_out;
michael@0 144 if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j];
michael@0 145 }
michael@0 146
michael@0 147 /* read partition classes */
michael@0 148 for(j=0;j<maxclass+1;j++){
michael@0 149 info->class_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */
michael@0 150 info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */
michael@0 151 if(info->class_subs[j]<0)
michael@0 152 goto err_out;
michael@0 153 if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8);
michael@0 154 if(info->class_book[j]<0 || info->class_book[j]>=ci->books)
michael@0 155 goto err_out;
michael@0 156 for(k=0;k<(1<<info->class_subs[j]);k++){
michael@0 157 info->class_subbook[j][k]=oggpack_read(opb,8)-1;
michael@0 158 if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books)
michael@0 159 goto err_out;
michael@0 160 }
michael@0 161 }
michael@0 162
michael@0 163 /* read the post list */
michael@0 164 info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */
michael@0 165 rangebits=oggpack_read(opb,4);
michael@0 166 if(rangebits<0)goto err_out;
michael@0 167
michael@0 168 for(j=0,k=0;j<info->partitions;j++){
michael@0 169 count+=info->class_dim[info->partitionclass[j]];
michael@0 170 if(count>VIF_POSIT) goto err_out;
michael@0 171 for(;k<count;k++){
michael@0 172 int t=info->postlist[k+2]=oggpack_read(opb,rangebits);
michael@0 173 if(t<0 || t>=(1<<rangebits))
michael@0 174 goto err_out;
michael@0 175 }
michael@0 176 }
michael@0 177 info->postlist[0]=0;
michael@0 178 info->postlist[1]=1<<rangebits;
michael@0 179
michael@0 180 /* don't allow repeated values in post list as they'd result in
michael@0 181 zero-length segments */
michael@0 182 {
michael@0 183 int *sortpointer[VIF_POSIT+2];
michael@0 184 for(j=0;j<count+2;j++)sortpointer[j]=info->postlist+j;
michael@0 185 qsort(sortpointer,count+2,sizeof(*sortpointer),icomp);
michael@0 186
michael@0 187 for(j=1;j<count+2;j++)
michael@0 188 if(*sortpointer[j-1]==*sortpointer[j])goto err_out;
michael@0 189 }
michael@0 190
michael@0 191 return(info);
michael@0 192
michael@0 193 err_out:
michael@0 194 floor1_free_info(info);
michael@0 195 return(NULL);
michael@0 196 }
michael@0 197
michael@0 198 static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd,
michael@0 199 vorbis_info_floor *in){
michael@0 200
michael@0 201 int *sortpointer[VIF_POSIT+2];
michael@0 202 vorbis_info_floor1 *info=(vorbis_info_floor1 *)in;
michael@0 203 vorbis_look_floor1 *look=_ogg_calloc(1,sizeof(*look));
michael@0 204 int i,j,n=0;
michael@0 205
michael@0 206 (void)vd;
michael@0 207
michael@0 208 look->vi=info;
michael@0 209 look->n=info->postlist[1];
michael@0 210
michael@0 211 /* we drop each position value in-between already decoded values,
michael@0 212 and use linear interpolation to predict each new value past the
michael@0 213 edges. The positions are read in the order of the position
michael@0 214 list... we precompute the bounding positions in the lookup. Of
michael@0 215 course, the neighbors can change (if a position is declined), but
michael@0 216 this is an initial mapping */
michael@0 217
michael@0 218 for(i=0;i<info->partitions;i++)n+=info->class_dim[info->partitionclass[i]];
michael@0 219 n+=2;
michael@0 220 look->posts=n;
michael@0 221
michael@0 222 /* also store a sorted position index */
michael@0 223 for(i=0;i<n;i++)sortpointer[i]=info->postlist+i;
michael@0 224 qsort(sortpointer,n,sizeof(*sortpointer),icomp);
michael@0 225
michael@0 226 /* points from sort order back to range number */
michael@0 227 for(i=0;i<n;i++)look->forward_index[i]=sortpointer[i]-info->postlist;
michael@0 228 /* points from range order to sorted position */
michael@0 229 for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i;
michael@0 230 /* we actually need the post values too */
michael@0 231 for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]];
michael@0 232
michael@0 233 /* quantize values to multiplier spec */
michael@0 234 switch(info->mult){
michael@0 235 case 1: /* 1024 -> 256 */
michael@0 236 look->quant_q=256;
michael@0 237 break;
michael@0 238 case 2: /* 1024 -> 128 */
michael@0 239 look->quant_q=128;
michael@0 240 break;
michael@0 241 case 3: /* 1024 -> 86 */
michael@0 242 look->quant_q=86;
michael@0 243 break;
michael@0 244 case 4: /* 1024 -> 64 */
michael@0 245 look->quant_q=64;
michael@0 246 break;
michael@0 247 }
michael@0 248
michael@0 249 /* discover our neighbors for decode where we don't use fit flags
michael@0 250 (that would push the neighbors outward) */
michael@0 251 for(i=0;i<n-2;i++){
michael@0 252 int lo=0;
michael@0 253 int hi=1;
michael@0 254 int lx=0;
michael@0 255 int hx=look->n;
michael@0 256 int currentx=info->postlist[i+2];
michael@0 257 for(j=0;j<i+2;j++){
michael@0 258 int x=info->postlist[j];
michael@0 259 if(x>lx && x<currentx){
michael@0 260 lo=j;
michael@0 261 lx=x;
michael@0 262 }
michael@0 263 if(x<hx && x>currentx){
michael@0 264 hi=j;
michael@0 265 hx=x;
michael@0 266 }
michael@0 267 }
michael@0 268 look->loneighbor[i]=lo;
michael@0 269 look->hineighbor[i]=hi;
michael@0 270 }
michael@0 271
michael@0 272 return(look);
michael@0 273 }
michael@0 274
michael@0 275 static int render_point(int x0,int x1,int y0,int y1,int x){
michael@0 276 y0&=0x7fff; /* mask off flag */
michael@0 277 y1&=0x7fff;
michael@0 278
michael@0 279 {
michael@0 280 int dy=y1-y0;
michael@0 281 int adx=x1-x0;
michael@0 282 int ady=abs(dy);
michael@0 283 int err=ady*(x-x0);
michael@0 284
michael@0 285 int off=err/adx;
michael@0 286 if(dy<0)return(y0-off);
michael@0 287 return(y0+off);
michael@0 288 }
michael@0 289 }
michael@0 290
michael@0 291 static int vorbis_dBquant(const float *x){
michael@0 292 int i= *x*7.3142857f+1023.5f;
michael@0 293 if(i>1023)return(1023);
michael@0 294 if(i<0)return(0);
michael@0 295 return i;
michael@0 296 }
michael@0 297
michael@0 298 static const float FLOOR1_fromdB_LOOKUP[256]={
michael@0 299 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F,
michael@0 300 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F,
michael@0 301 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F,
michael@0 302 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F,
michael@0 303 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F,
michael@0 304 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F,
michael@0 305 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F,
michael@0 306 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F,
michael@0 307 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F,
michael@0 308 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F,
michael@0 309 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F,
michael@0 310 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F,
michael@0 311 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F,
michael@0 312 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F,
michael@0 313 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F,
michael@0 314 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F,
michael@0 315 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F,
michael@0 316 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F,
michael@0 317 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F,
michael@0 318 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F,
michael@0 319 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F,
michael@0 320 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F,
michael@0 321 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F,
michael@0 322 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F,
michael@0 323 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F,
michael@0 324 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F,
michael@0 325 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F,
michael@0 326 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F,
michael@0 327 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F,
michael@0 328 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F,
michael@0 329 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F,
michael@0 330 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F,
michael@0 331 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F,
michael@0 332 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F,
michael@0 333 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F,
michael@0 334 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F,
michael@0 335 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F,
michael@0 336 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F,
michael@0 337 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F,
michael@0 338 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F,
michael@0 339 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F,
michael@0 340 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F,
michael@0 341 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F,
michael@0 342 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F,
michael@0 343 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F,
michael@0 344 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F,
michael@0 345 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F,
michael@0 346 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F,
michael@0 347 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F,
michael@0 348 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F,
michael@0 349 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F,
michael@0 350 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F,
michael@0 351 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F,
michael@0 352 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F,
michael@0 353 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F,
michael@0 354 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F,
michael@0 355 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F,
michael@0 356 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F,
michael@0 357 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F,
michael@0 358 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F,
michael@0 359 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F,
michael@0 360 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F,
michael@0 361 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F,
michael@0 362 0.82788260F, 0.88168307F, 0.9389798F, 1.F,
michael@0 363 };
michael@0 364
michael@0 365 static void render_line(int n, int x0,int x1,int y0,int y1,float *d){
michael@0 366 int dy=y1-y0;
michael@0 367 int adx=x1-x0;
michael@0 368 int ady=abs(dy);
michael@0 369 int base=dy/adx;
michael@0 370 int sy=(dy<0?base-1:base+1);
michael@0 371 int x=x0;
michael@0 372 int y=y0;
michael@0 373 int err=0;
michael@0 374
michael@0 375 ady-=abs(base*adx);
michael@0 376
michael@0 377 if(n>x1)n=x1;
michael@0 378
michael@0 379 if(x<n)
michael@0 380 d[x]*=FLOOR1_fromdB_LOOKUP[y];
michael@0 381
michael@0 382 while(++x<n){
michael@0 383 err=err+ady;
michael@0 384 if(err>=adx){
michael@0 385 err-=adx;
michael@0 386 y+=sy;
michael@0 387 }else{
michael@0 388 y+=base;
michael@0 389 }
michael@0 390 d[x]*=FLOOR1_fromdB_LOOKUP[y];
michael@0 391 }
michael@0 392 }
michael@0 393
michael@0 394 static void render_line0(int n, int x0,int x1,int y0,int y1,int *d){
michael@0 395 int dy=y1-y0;
michael@0 396 int adx=x1-x0;
michael@0 397 int ady=abs(dy);
michael@0 398 int base=dy/adx;
michael@0 399 int sy=(dy<0?base-1:base+1);
michael@0 400 int x=x0;
michael@0 401 int y=y0;
michael@0 402 int err=0;
michael@0 403
michael@0 404 ady-=abs(base*adx);
michael@0 405
michael@0 406 if(n>x1)n=x1;
michael@0 407
michael@0 408 if(x<n)
michael@0 409 d[x]=y;
michael@0 410
michael@0 411 while(++x<n){
michael@0 412 err=err+ady;
michael@0 413 if(err>=adx){
michael@0 414 err-=adx;
michael@0 415 y+=sy;
michael@0 416 }else{
michael@0 417 y+=base;
michael@0 418 }
michael@0 419 d[x]=y;
michael@0 420 }
michael@0 421 }
michael@0 422
michael@0 423 /* the floor has already been filtered to only include relevant sections */
michael@0 424 static int accumulate_fit(const float *flr,const float *mdct,
michael@0 425 int x0, int x1,lsfit_acc *a,
michael@0 426 int n,vorbis_info_floor1 *info){
michael@0 427 long i;
michael@0 428
michael@0 429 int xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0;
michael@0 430
michael@0 431 memset(a,0,sizeof(*a));
michael@0 432 a->x0=x0;
michael@0 433 a->x1=x1;
michael@0 434 if(x1>=n)x1=n-1;
michael@0 435
michael@0 436 for(i=x0;i<=x1;i++){
michael@0 437 int quantized=vorbis_dBquant(flr+i);
michael@0 438 if(quantized){
michael@0 439 if(mdct[i]+info->twofitatten>=flr[i]){
michael@0 440 xa += i;
michael@0 441 ya += quantized;
michael@0 442 x2a += i*i;
michael@0 443 y2a += quantized*quantized;
michael@0 444 xya += i*quantized;
michael@0 445 na++;
michael@0 446 }else{
michael@0 447 xb += i;
michael@0 448 yb += quantized;
michael@0 449 x2b += i*i;
michael@0 450 y2b += quantized*quantized;
michael@0 451 xyb += i*quantized;
michael@0 452 nb++;
michael@0 453 }
michael@0 454 }
michael@0 455 }
michael@0 456
michael@0 457 a->xa=xa;
michael@0 458 a->ya=ya;
michael@0 459 a->x2a=x2a;
michael@0 460 a->y2a=y2a;
michael@0 461 a->xya=xya;
michael@0 462 a->an=na;
michael@0 463
michael@0 464 a->xb=xb;
michael@0 465 a->yb=yb;
michael@0 466 a->x2b=x2b;
michael@0 467 a->y2b=y2b;
michael@0 468 a->xyb=xyb;
michael@0 469 a->bn=nb;
michael@0 470
michael@0 471 return(na);
michael@0 472 }
michael@0 473
michael@0 474 static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1,
michael@0 475 vorbis_info_floor1 *info){
michael@0 476 double xb=0,yb=0,x2b=0,y2b=0,xyb=0,bn=0;
michael@0 477 int i;
michael@0 478 int x0=a[0].x0;
michael@0 479 int x1=a[fits-1].x1;
michael@0 480
michael@0 481 for(i=0;i<fits;i++){
michael@0 482 double weight = (a[i].bn+a[i].an)*info->twofitweight/(a[i].an+1)+1.;
michael@0 483
michael@0 484 xb+=a[i].xb + a[i].xa * weight;
michael@0 485 yb+=a[i].yb + a[i].ya * weight;
michael@0 486 x2b+=a[i].x2b + a[i].x2a * weight;
michael@0 487 y2b+=a[i].y2b + a[i].y2a * weight;
michael@0 488 xyb+=a[i].xyb + a[i].xya * weight;
michael@0 489 bn+=a[i].bn + a[i].an * weight;
michael@0 490 }
michael@0 491
michael@0 492 if(*y0>=0){
michael@0 493 xb+= x0;
michael@0 494 yb+= *y0;
michael@0 495 x2b+= x0 * x0;
michael@0 496 y2b+= *y0 * *y0;
michael@0 497 xyb+= *y0 * x0;
michael@0 498 bn++;
michael@0 499 }
michael@0 500
michael@0 501 if(*y1>=0){
michael@0 502 xb+= x1;
michael@0 503 yb+= *y1;
michael@0 504 x2b+= x1 * x1;
michael@0 505 y2b+= *y1 * *y1;
michael@0 506 xyb+= *y1 * x1;
michael@0 507 bn++;
michael@0 508 }
michael@0 509
michael@0 510 {
michael@0 511 double denom=(bn*x2b-xb*xb);
michael@0 512
michael@0 513 if(denom>0.){
michael@0 514 double a=(yb*x2b-xyb*xb)/denom;
michael@0 515 double b=(bn*xyb-xb*yb)/denom;
michael@0 516 *y0=rint(a+b*x0);
michael@0 517 *y1=rint(a+b*x1);
michael@0 518
michael@0 519 /* limit to our range! */
michael@0 520 if(*y0>1023)*y0=1023;
michael@0 521 if(*y1>1023)*y1=1023;
michael@0 522 if(*y0<0)*y0=0;
michael@0 523 if(*y1<0)*y1=0;
michael@0 524
michael@0 525 return 0;
michael@0 526 }else{
michael@0 527 *y0=0;
michael@0 528 *y1=0;
michael@0 529 return 1;
michael@0 530 }
michael@0 531 }
michael@0 532 }
michael@0 533
michael@0 534 static int inspect_error(int x0,int x1,int y0,int y1,const float *mask,
michael@0 535 const float *mdct,
michael@0 536 vorbis_info_floor1 *info){
michael@0 537 int dy=y1-y0;
michael@0 538 int adx=x1-x0;
michael@0 539 int ady=abs(dy);
michael@0 540 int base=dy/adx;
michael@0 541 int sy=(dy<0?base-1:base+1);
michael@0 542 int x=x0;
michael@0 543 int y=y0;
michael@0 544 int err=0;
michael@0 545 int val=vorbis_dBquant(mask+x);
michael@0 546 int mse=0;
michael@0 547 int n=0;
michael@0 548
michael@0 549 ady-=abs(base*adx);
michael@0 550
michael@0 551 mse=(y-val);
michael@0 552 mse*=mse;
michael@0 553 n++;
michael@0 554 if(mdct[x]+info->twofitatten>=mask[x]){
michael@0 555 if(y+info->maxover<val)return(1);
michael@0 556 if(y-info->maxunder>val)return(1);
michael@0 557 }
michael@0 558
michael@0 559 while(++x<x1){
michael@0 560 err=err+ady;
michael@0 561 if(err>=adx){
michael@0 562 err-=adx;
michael@0 563 y+=sy;
michael@0 564 }else{
michael@0 565 y+=base;
michael@0 566 }
michael@0 567
michael@0 568 val=vorbis_dBquant(mask+x);
michael@0 569 mse+=((y-val)*(y-val));
michael@0 570 n++;
michael@0 571 if(mdct[x]+info->twofitatten>=mask[x]){
michael@0 572 if(val){
michael@0 573 if(y+info->maxover<val)return(1);
michael@0 574 if(y-info->maxunder>val)return(1);
michael@0 575 }
michael@0 576 }
michael@0 577 }
michael@0 578
michael@0 579 if(info->maxover*info->maxover/n>info->maxerr)return(0);
michael@0 580 if(info->maxunder*info->maxunder/n>info->maxerr)return(0);
michael@0 581 if(mse/n>info->maxerr)return(1);
michael@0 582 return(0);
michael@0 583 }
michael@0 584
michael@0 585 static int post_Y(int *A,int *B,int pos){
michael@0 586 if(A[pos]<0)
michael@0 587 return B[pos];
michael@0 588 if(B[pos]<0)
michael@0 589 return A[pos];
michael@0 590
michael@0 591 return (A[pos]+B[pos])>>1;
michael@0 592 }
michael@0 593
michael@0 594 int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look,
michael@0 595 const float *logmdct, /* in */
michael@0 596 const float *logmask){
michael@0 597 long i,j;
michael@0 598 vorbis_info_floor1 *info=look->vi;
michael@0 599 long n=look->n;
michael@0 600 long posts=look->posts;
michael@0 601 long nonzero=0;
michael@0 602 lsfit_acc fits[VIF_POSIT+1];
michael@0 603 int fit_valueA[VIF_POSIT+2]; /* index by range list position */
michael@0 604 int fit_valueB[VIF_POSIT+2]; /* index by range list position */
michael@0 605
michael@0 606 int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */
michael@0 607 int hineighbor[VIF_POSIT+2];
michael@0 608 int *output=NULL;
michael@0 609 int memo[VIF_POSIT+2];
michael@0 610
michael@0 611 for(i=0;i<posts;i++)fit_valueA[i]=-200; /* mark all unused */
michael@0 612 for(i=0;i<posts;i++)fit_valueB[i]=-200; /* mark all unused */
michael@0 613 for(i=0;i<posts;i++)loneighbor[i]=0; /* 0 for the implicit 0 post */
michael@0 614 for(i=0;i<posts;i++)hineighbor[i]=1; /* 1 for the implicit post at n */
michael@0 615 for(i=0;i<posts;i++)memo[i]=-1; /* no neighbor yet */
michael@0 616
michael@0 617 /* quantize the relevant floor points and collect them into line fit
michael@0 618 structures (one per minimal division) at the same time */
michael@0 619 if(posts==0){
michael@0 620 nonzero+=accumulate_fit(logmask,logmdct,0,n,fits,n,info);
michael@0 621 }else{
michael@0 622 for(i=0;i<posts-1;i++)
michael@0 623 nonzero+=accumulate_fit(logmask,logmdct,look->sorted_index[i],
michael@0 624 look->sorted_index[i+1],fits+i,
michael@0 625 n,info);
michael@0 626 }
michael@0 627
michael@0 628 if(nonzero){
michael@0 629 /* start by fitting the implicit base case.... */
michael@0 630 int y0=-200;
michael@0 631 int y1=-200;
michael@0 632 fit_line(fits,posts-1,&y0,&y1,info);
michael@0 633
michael@0 634 fit_valueA[0]=y0;
michael@0 635 fit_valueB[0]=y0;
michael@0 636 fit_valueB[1]=y1;
michael@0 637 fit_valueA[1]=y1;
michael@0 638
michael@0 639 /* Non degenerate case */
michael@0 640 /* start progressive splitting. This is a greedy, non-optimal
michael@0 641 algorithm, but simple and close enough to the best
michael@0 642 answer. */
michael@0 643 for(i=2;i<posts;i++){
michael@0 644 int sortpos=look->reverse_index[i];
michael@0 645 int ln=loneighbor[sortpos];
michael@0 646 int hn=hineighbor[sortpos];
michael@0 647
michael@0 648 /* eliminate repeat searches of a particular range with a memo */
michael@0 649 if(memo[ln]!=hn){
michael@0 650 /* haven't performed this error search yet */
michael@0 651 int lsortpos=look->reverse_index[ln];
michael@0 652 int hsortpos=look->reverse_index[hn];
michael@0 653 memo[ln]=hn;
michael@0 654
michael@0 655 {
michael@0 656 /* A note: we want to bound/minimize *local*, not global, error */
michael@0 657 int lx=info->postlist[ln];
michael@0 658 int hx=info->postlist[hn];
michael@0 659 int ly=post_Y(fit_valueA,fit_valueB,ln);
michael@0 660 int hy=post_Y(fit_valueA,fit_valueB,hn);
michael@0 661
michael@0 662 if(ly==-1 || hy==-1){
michael@0 663 exit(1);
michael@0 664 }
michael@0 665
michael@0 666 if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){
michael@0 667 /* outside error bounds/begin search area. Split it. */
michael@0 668 int ly0=-200;
michael@0 669 int ly1=-200;
michael@0 670 int hy0=-200;
michael@0 671 int hy1=-200;
michael@0 672 int ret0=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1,info);
michael@0 673 int ret1=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1,info);
michael@0 674
michael@0 675 if(ret0){
michael@0 676 ly0=ly;
michael@0 677 ly1=hy0;
michael@0 678 }
michael@0 679 if(ret1){
michael@0 680 hy0=ly1;
michael@0 681 hy1=hy;
michael@0 682 }
michael@0 683
michael@0 684 if(ret0 && ret1){
michael@0 685 fit_valueA[i]=-200;
michael@0 686 fit_valueB[i]=-200;
michael@0 687 }else{
michael@0 688 /* store new edge values */
michael@0 689 fit_valueB[ln]=ly0;
michael@0 690 if(ln==0)fit_valueA[ln]=ly0;
michael@0 691 fit_valueA[i]=ly1;
michael@0 692 fit_valueB[i]=hy0;
michael@0 693 fit_valueA[hn]=hy1;
michael@0 694 if(hn==1)fit_valueB[hn]=hy1;
michael@0 695
michael@0 696 if(ly1>=0 || hy0>=0){
michael@0 697 /* store new neighbor values */
michael@0 698 for(j=sortpos-1;j>=0;j--)
michael@0 699 if(hineighbor[j]==hn)
michael@0 700 hineighbor[j]=i;
michael@0 701 else
michael@0 702 break;
michael@0 703 for(j=sortpos+1;j<posts;j++)
michael@0 704 if(loneighbor[j]==ln)
michael@0 705 loneighbor[j]=i;
michael@0 706 else
michael@0 707 break;
michael@0 708 }
michael@0 709 }
michael@0 710 }else{
michael@0 711 fit_valueA[i]=-200;
michael@0 712 fit_valueB[i]=-200;
michael@0 713 }
michael@0 714 }
michael@0 715 }
michael@0 716 }
michael@0 717
michael@0 718 output=_vorbis_block_alloc(vb,sizeof(*output)*posts);
michael@0 719
michael@0 720 output[0]=post_Y(fit_valueA,fit_valueB,0);
michael@0 721 output[1]=post_Y(fit_valueA,fit_valueB,1);
michael@0 722
michael@0 723 /* fill in posts marked as not using a fit; we will zero
michael@0 724 back out to 'unused' when encoding them so long as curve
michael@0 725 interpolation doesn't force them into use */
michael@0 726 for(i=2;i<posts;i++){
michael@0 727 int ln=look->loneighbor[i-2];
michael@0 728 int hn=look->hineighbor[i-2];
michael@0 729 int x0=info->postlist[ln];
michael@0 730 int x1=info->postlist[hn];
michael@0 731 int y0=output[ln];
michael@0 732 int y1=output[hn];
michael@0 733
michael@0 734 int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
michael@0 735 int vx=post_Y(fit_valueA,fit_valueB,i);
michael@0 736
michael@0 737 if(vx>=0 && predicted!=vx){
michael@0 738 output[i]=vx;
michael@0 739 }else{
michael@0 740 output[i]= predicted|0x8000;
michael@0 741 }
michael@0 742 }
michael@0 743 }
michael@0 744
michael@0 745 return(output);
michael@0 746
michael@0 747 }
michael@0 748
michael@0 749 int *floor1_interpolate_fit(vorbis_block *vb,vorbis_look_floor1 *look,
michael@0 750 int *A,int *B,
michael@0 751 int del){
michael@0 752
michael@0 753 long i;
michael@0 754 long posts=look->posts;
michael@0 755 int *output=NULL;
michael@0 756
michael@0 757 if(A && B){
michael@0 758 output=_vorbis_block_alloc(vb,sizeof(*output)*posts);
michael@0 759
michael@0 760 /* overly simpleminded--- look again post 1.2 */
michael@0 761 for(i=0;i<posts;i++){
michael@0 762 output[i]=((65536-del)*(A[i]&0x7fff)+del*(B[i]&0x7fff)+32768)>>16;
michael@0 763 if(A[i]&0x8000 && B[i]&0x8000)output[i]|=0x8000;
michael@0 764 }
michael@0 765 }
michael@0 766
michael@0 767 return(output);
michael@0 768 }
michael@0 769
michael@0 770
michael@0 771 int floor1_encode(oggpack_buffer *opb,vorbis_block *vb,
michael@0 772 vorbis_look_floor1 *look,
michael@0 773 int *post,int *ilogmask){
michael@0 774
michael@0 775 long i,j;
michael@0 776 vorbis_info_floor1 *info=look->vi;
michael@0 777 long posts=look->posts;
michael@0 778 codec_setup_info *ci=vb->vd->vi->codec_setup;
michael@0 779 int out[VIF_POSIT+2];
michael@0 780 static_codebook **sbooks=ci->book_param;
michael@0 781 codebook *books=ci->fullbooks;
michael@0 782
michael@0 783 /* quantize values to multiplier spec */
michael@0 784 if(post){
michael@0 785 for(i=0;i<posts;i++){
michael@0 786 int val=post[i]&0x7fff;
michael@0 787 switch(info->mult){
michael@0 788 case 1: /* 1024 -> 256 */
michael@0 789 val>>=2;
michael@0 790 break;
michael@0 791 case 2: /* 1024 -> 128 */
michael@0 792 val>>=3;
michael@0 793 break;
michael@0 794 case 3: /* 1024 -> 86 */
michael@0 795 val/=12;
michael@0 796 break;
michael@0 797 case 4: /* 1024 -> 64 */
michael@0 798 val>>=4;
michael@0 799 break;
michael@0 800 }
michael@0 801 post[i]=val | (post[i]&0x8000);
michael@0 802 }
michael@0 803
michael@0 804 out[0]=post[0];
michael@0 805 out[1]=post[1];
michael@0 806
michael@0 807 /* find prediction values for each post and subtract them */
michael@0 808 for(i=2;i<posts;i++){
michael@0 809 int ln=look->loneighbor[i-2];
michael@0 810 int hn=look->hineighbor[i-2];
michael@0 811 int x0=info->postlist[ln];
michael@0 812 int x1=info->postlist[hn];
michael@0 813 int y0=post[ln];
michael@0 814 int y1=post[hn];
michael@0 815
michael@0 816 int predicted=render_point(x0,x1,y0,y1,info->postlist[i]);
michael@0 817
michael@0 818 if((post[i]&0x8000) || (predicted==post[i])){
michael@0 819 post[i]=predicted|0x8000; /* in case there was roundoff jitter
michael@0 820 in interpolation */
michael@0 821 out[i]=0;
michael@0 822 }else{
michael@0 823 int headroom=(look->quant_q-predicted<predicted?
michael@0 824 look->quant_q-predicted:predicted);
michael@0 825
michael@0 826 int val=post[i]-predicted;
michael@0 827
michael@0 828 /* at this point the 'deviation' value is in the range +/- max
michael@0 829 range, but the real, unique range can always be mapped to
michael@0 830 only [0-maxrange). So we want to wrap the deviation into
michael@0 831 this limited range, but do it in the way that least screws
michael@0 832 an essentially gaussian probability distribution. */
michael@0 833
michael@0 834 if(val<0)
michael@0 835 if(val<-headroom)
michael@0 836 val=headroom-val-1;
michael@0 837 else
michael@0 838 val=-1-(val<<1);
michael@0 839 else
michael@0 840 if(val>=headroom)
michael@0 841 val= val+headroom;
michael@0 842 else
michael@0 843 val<<=1;
michael@0 844
michael@0 845 out[i]=val;
michael@0 846 post[ln]&=0x7fff;
michael@0 847 post[hn]&=0x7fff;
michael@0 848 }
michael@0 849 }
michael@0 850
michael@0 851 /* we have everything we need. pack it out */
michael@0 852 /* mark nontrivial floor */
michael@0 853 oggpack_write(opb,1,1);
michael@0 854
michael@0 855 /* beginning/end post */
michael@0 856 look->frames++;
michael@0 857 look->postbits+=ilog(look->quant_q-1)*2;
michael@0 858 oggpack_write(opb,out[0],ilog(look->quant_q-1));
michael@0 859 oggpack_write(opb,out[1],ilog(look->quant_q-1));
michael@0 860
michael@0 861
michael@0 862 /* partition by partition */
michael@0 863 for(i=0,j=2;i<info->partitions;i++){
michael@0 864 int class=info->partitionclass[i];
michael@0 865 int cdim=info->class_dim[class];
michael@0 866 int csubbits=info->class_subs[class];
michael@0 867 int csub=1<<csubbits;
michael@0 868 int bookas[8]={0,0,0,0,0,0,0,0};
michael@0 869 int cval=0;
michael@0 870 int cshift=0;
michael@0 871 int k,l;
michael@0 872
michael@0 873 /* generate the partition's first stage cascade value */
michael@0 874 if(csubbits){
michael@0 875 int maxval[8];
michael@0 876 for(k=0;k<csub;k++){
michael@0 877 int booknum=info->class_subbook[class][k];
michael@0 878 if(booknum<0){
michael@0 879 maxval[k]=1;
michael@0 880 }else{
michael@0 881 maxval[k]=sbooks[info->class_subbook[class][k]]->entries;
michael@0 882 }
michael@0 883 }
michael@0 884 for(k=0;k<cdim;k++){
michael@0 885 for(l=0;l<csub;l++){
michael@0 886 int val=out[j+k];
michael@0 887 if(val<maxval[l]){
michael@0 888 bookas[k]=l;
michael@0 889 break;
michael@0 890 }
michael@0 891 }
michael@0 892 cval|= bookas[k]<<cshift;
michael@0 893 cshift+=csubbits;
michael@0 894 }
michael@0 895 /* write it */
michael@0 896 look->phrasebits+=
michael@0 897 vorbis_book_encode(books+info->class_book[class],cval,opb);
michael@0 898
michael@0 899 #ifdef TRAIN_FLOOR1
michael@0 900 {
michael@0 901 FILE *of;
michael@0 902 char buffer[80];
michael@0 903 sprintf(buffer,"line_%dx%ld_class%d.vqd",
michael@0 904 vb->pcmend/2,posts-2,class);
michael@0 905 of=fopen(buffer,"a");
michael@0 906 fprintf(of,"%d\n",cval);
michael@0 907 fclose(of);
michael@0 908 }
michael@0 909 #endif
michael@0 910 }
michael@0 911
michael@0 912 /* write post values */
michael@0 913 for(k=0;k<cdim;k++){
michael@0 914 int book=info->class_subbook[class][bookas[k]];
michael@0 915 if(book>=0){
michael@0 916 /* hack to allow training with 'bad' books */
michael@0 917 if(out[j+k]<(books+book)->entries)
michael@0 918 look->postbits+=vorbis_book_encode(books+book,
michael@0 919 out[j+k],opb);
michael@0 920 /*else
michael@0 921 fprintf(stderr,"+!");*/
michael@0 922
michael@0 923 #ifdef TRAIN_FLOOR1
michael@0 924 {
michael@0 925 FILE *of;
michael@0 926 char buffer[80];
michael@0 927 sprintf(buffer,"line_%dx%ld_%dsub%d.vqd",
michael@0 928 vb->pcmend/2,posts-2,class,bookas[k]);
michael@0 929 of=fopen(buffer,"a");
michael@0 930 fprintf(of,"%d\n",out[j+k]);
michael@0 931 fclose(of);
michael@0 932 }
michael@0 933 #endif
michael@0 934 }
michael@0 935 }
michael@0 936 j+=cdim;
michael@0 937 }
michael@0 938
michael@0 939 {
michael@0 940 /* generate quantized floor equivalent to what we'd unpack in decode */
michael@0 941 /* render the lines */
michael@0 942 int hx=0;
michael@0 943 int lx=0;
michael@0 944 int ly=post[0]*info->mult;
michael@0 945 int n=ci->blocksizes[vb->W]/2;
michael@0 946
michael@0 947 for(j=1;j<look->posts;j++){
michael@0 948 int current=look->forward_index[j];
michael@0 949 int hy=post[current]&0x7fff;
michael@0 950 if(hy==post[current]){
michael@0 951
michael@0 952 hy*=info->mult;
michael@0 953 hx=info->postlist[current];
michael@0 954
michael@0 955 render_line0(n,lx,hx,ly,hy,ilogmask);
michael@0 956
michael@0 957 lx=hx;
michael@0 958 ly=hy;
michael@0 959 }
michael@0 960 }
michael@0 961 for(j=hx;j<vb->pcmend/2;j++)ilogmask[j]=ly; /* be certain */
michael@0 962 return(1);
michael@0 963 }
michael@0 964 }else{
michael@0 965 oggpack_write(opb,0,1);
michael@0 966 memset(ilogmask,0,vb->pcmend/2*sizeof(*ilogmask));
michael@0 967 return(0);
michael@0 968 }
michael@0 969 }
michael@0 970
michael@0 971 static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){
michael@0 972 vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
michael@0 973 vorbis_info_floor1 *info=look->vi;
michael@0 974 codec_setup_info *ci=vb->vd->vi->codec_setup;
michael@0 975
michael@0 976 int i,j,k;
michael@0 977 codebook *books=ci->fullbooks;
michael@0 978
michael@0 979 /* unpack wrapped/predicted values from stream */
michael@0 980 if(oggpack_read(&vb->opb,1)==1){
michael@0 981 int *fit_value=_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value));
michael@0 982
michael@0 983 fit_value[0]=oggpack_read(&vb->opb,ilog(look->quant_q-1));
michael@0 984 fit_value[1]=oggpack_read(&vb->opb,ilog(look->quant_q-1));
michael@0 985
michael@0 986 /* partition by partition */
michael@0 987 for(i=0,j=2;i<info->partitions;i++){
michael@0 988 int class=info->partitionclass[i];
michael@0 989 int cdim=info->class_dim[class];
michael@0 990 int csubbits=info->class_subs[class];
michael@0 991 int csub=1<<csubbits;
michael@0 992 int cval=0;
michael@0 993
michael@0 994 /* decode the partition's first stage cascade value */
michael@0 995 if(csubbits){
michael@0 996 cval=vorbis_book_decode(books+info->class_book[class],&vb->opb);
michael@0 997
michael@0 998 if(cval==-1)goto eop;
michael@0 999 }
michael@0 1000
michael@0 1001 for(k=0;k<cdim;k++){
michael@0 1002 int book=info->class_subbook[class][cval&(csub-1)];
michael@0 1003 cval>>=csubbits;
michael@0 1004 if(book>=0){
michael@0 1005 if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1)
michael@0 1006 goto eop;
michael@0 1007 }else{
michael@0 1008 fit_value[j+k]=0;
michael@0 1009 }
michael@0 1010 }
michael@0 1011 j+=cdim;
michael@0 1012 }
michael@0 1013
michael@0 1014 /* unwrap positive values and reconsitute via linear interpolation */
michael@0 1015 for(i=2;i<look->posts;i++){
michael@0 1016 int predicted=render_point(info->postlist[look->loneighbor[i-2]],
michael@0 1017 info->postlist[look->hineighbor[i-2]],
michael@0 1018 fit_value[look->loneighbor[i-2]],
michael@0 1019 fit_value[look->hineighbor[i-2]],
michael@0 1020 info->postlist[i]);
michael@0 1021 int hiroom=look->quant_q-predicted;
michael@0 1022 int loroom=predicted;
michael@0 1023 int room=(hiroom<loroom?hiroom:loroom)<<1;
michael@0 1024 int val=fit_value[i];
michael@0 1025
michael@0 1026 if(val){
michael@0 1027 if(val>=room){
michael@0 1028 if(hiroom>loroom){
michael@0 1029 val = val-loroom;
michael@0 1030 }else{
michael@0 1031 val = -1-(val-hiroom);
michael@0 1032 }
michael@0 1033 }else{
michael@0 1034 if(val&1){
michael@0 1035 val= -((val+1)>>1);
michael@0 1036 }else{
michael@0 1037 val>>=1;
michael@0 1038 }
michael@0 1039 }
michael@0 1040
michael@0 1041 fit_value[i]=(val+predicted)&0x7fff;
michael@0 1042 fit_value[look->loneighbor[i-2]]&=0x7fff;
michael@0 1043 fit_value[look->hineighbor[i-2]]&=0x7fff;
michael@0 1044
michael@0 1045 }else{
michael@0 1046 fit_value[i]=predicted|0x8000;
michael@0 1047 }
michael@0 1048
michael@0 1049 }
michael@0 1050
michael@0 1051 return(fit_value);
michael@0 1052 }
michael@0 1053 eop:
michael@0 1054 return(NULL);
michael@0 1055 }
michael@0 1056
michael@0 1057 static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo,
michael@0 1058 float *out){
michael@0 1059 vorbis_look_floor1 *look=(vorbis_look_floor1 *)in;
michael@0 1060 vorbis_info_floor1 *info=look->vi;
michael@0 1061
michael@0 1062 codec_setup_info *ci=vb->vd->vi->codec_setup;
michael@0 1063 int n=ci->blocksizes[vb->W]/2;
michael@0 1064 int j;
michael@0 1065
michael@0 1066 if(memo){
michael@0 1067 /* render the lines */
michael@0 1068 int *fit_value=(int *)memo;
michael@0 1069 int hx=0;
michael@0 1070 int lx=0;
michael@0 1071 int ly=fit_value[0]*info->mult;
michael@0 1072 /* guard lookup against out-of-range values */
michael@0 1073 ly=(ly<0?0:ly>255?255:ly);
michael@0 1074
michael@0 1075 for(j=1;j<look->posts;j++){
michael@0 1076 int current=look->forward_index[j];
michael@0 1077 int hy=fit_value[current]&0x7fff;
michael@0 1078 if(hy==fit_value[current]){
michael@0 1079
michael@0 1080 hx=info->postlist[current];
michael@0 1081 hy*=info->mult;
michael@0 1082 /* guard lookup against out-of-range values */
michael@0 1083 hy=(hy<0?0:hy>255?255:hy);
michael@0 1084
michael@0 1085 render_line(n,lx,hx,ly,hy,out);
michael@0 1086
michael@0 1087 lx=hx;
michael@0 1088 ly=hy;
michael@0 1089 }
michael@0 1090 }
michael@0 1091 for(j=hx;j<n;j++)out[j]*=FLOOR1_fromdB_LOOKUP[ly]; /* be certain */
michael@0 1092 return(1);
michael@0 1093 }
michael@0 1094 memset(out,0,sizeof(*out)*n);
michael@0 1095 return(0);
michael@0 1096 }
michael@0 1097
michael@0 1098 /* export hooks */
michael@0 1099 const vorbis_func_floor floor1_exportbundle={
michael@0 1100 &floor1_pack,&floor1_unpack,&floor1_look,&floor1_free_info,
michael@0 1101 &floor1_free_look,&floor1_inverse1,&floor1_inverse2
michael@0 1102 };

mercurial