media/libvorbis/lib/vorbis_res0.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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-2010 *
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: residue backend 0, 1 and 2 implementation
michael@0 14 last mod: $Id: res0.c 19031 2013-12-03 19:20:50Z tterribe $
michael@0 15
michael@0 16 ********************************************************************/
michael@0 17
michael@0 18 /* Slow, slow, slow, simpleminded and did I mention it was slow? The
michael@0 19 encode/decode loops are coded for clarity and performance is not
michael@0 20 yet even a nagging little idea lurking in the shadows. Oh and BTW,
michael@0 21 it's slow. */
michael@0 22
michael@0 23 #include <stdlib.h>
michael@0 24 #include <string.h>
michael@0 25 #include <math.h>
michael@0 26 #include <ogg/ogg.h>
michael@0 27 #include "vorbis/codec.h"
michael@0 28 #include "codec_internal.h"
michael@0 29 #include "registry.h"
michael@0 30 #include "codebook.h"
michael@0 31 #include "misc.h"
michael@0 32 #include "os.h"
michael@0 33
michael@0 34 //#define TRAIN_RES 1
michael@0 35 //#define TRAIN_RESAUX 1
michael@0 36
michael@0 37 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
michael@0 38 #include <stdio.h>
michael@0 39 #endif
michael@0 40
michael@0 41 typedef struct {
michael@0 42 vorbis_info_residue0 *info;
michael@0 43
michael@0 44 int parts;
michael@0 45 int stages;
michael@0 46 codebook *fullbooks;
michael@0 47 codebook *phrasebook;
michael@0 48 codebook ***partbooks;
michael@0 49
michael@0 50 int partvals;
michael@0 51 int **decodemap;
michael@0 52
michael@0 53 long postbits;
michael@0 54 long phrasebits;
michael@0 55 long frames;
michael@0 56
michael@0 57 #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
michael@0 58 int train_seq;
michael@0 59 long *training_data[8][64];
michael@0 60 float training_max[8][64];
michael@0 61 float training_min[8][64];
michael@0 62 float tmin;
michael@0 63 float tmax;
michael@0 64 int submap;
michael@0 65 #endif
michael@0 66
michael@0 67 } vorbis_look_residue0;
michael@0 68
michael@0 69 void res0_free_info(vorbis_info_residue *i){
michael@0 70 vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
michael@0 71 if(info){
michael@0 72 memset(info,0,sizeof(*info));
michael@0 73 _ogg_free(info);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 void res0_free_look(vorbis_look_residue *i){
michael@0 78 int j;
michael@0 79 if(i){
michael@0 80
michael@0 81 vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
michael@0 82
michael@0 83 #ifdef TRAIN_RES
michael@0 84 {
michael@0 85 int j,k,l;
michael@0 86 for(j=0;j<look->parts;j++){
michael@0 87 /*fprintf(stderr,"partition %d: ",j);*/
michael@0 88 for(k=0;k<8;k++)
michael@0 89 if(look->training_data[k][j]){
michael@0 90 char buffer[80];
michael@0 91 FILE *of;
michael@0 92 codebook *statebook=look->partbooks[j][k];
michael@0 93
michael@0 94 /* long and short into the same bucket by current convention */
michael@0 95 sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k);
michael@0 96 of=fopen(buffer,"a");
michael@0 97
michael@0 98 for(l=0;l<statebook->entries;l++)
michael@0 99 fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
michael@0 100
michael@0 101 fclose(of);
michael@0 102
michael@0 103 /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
michael@0 104 look->training_min[k][j],look->training_max[k][j]);*/
michael@0 105
michael@0 106 _ogg_free(look->training_data[k][j]);
michael@0 107 look->training_data[k][j]=NULL;
michael@0 108 }
michael@0 109 /*fprintf(stderr,"\n");*/
michael@0 110 }
michael@0 111 }
michael@0 112 fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
michael@0 113
michael@0 114 /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
michael@0 115 (float)look->phrasebits/look->frames,
michael@0 116 (float)look->postbits/look->frames,
michael@0 117 (float)(look->postbits+look->phrasebits)/look->frames);*/
michael@0 118 #endif
michael@0 119
michael@0 120
michael@0 121 /*vorbis_info_residue0 *info=look->info;
michael@0 122
michael@0 123 fprintf(stderr,
michael@0 124 "%ld frames encoded in %ld phrasebits and %ld residue bits "
michael@0 125 "(%g/frame) \n",look->frames,look->phrasebits,
michael@0 126 look->resbitsflat,
michael@0 127 (look->phrasebits+look->resbitsflat)/(float)look->frames);
michael@0 128
michael@0 129 for(j=0;j<look->parts;j++){
michael@0 130 long acc=0;
michael@0 131 fprintf(stderr,"\t[%d] == ",j);
michael@0 132 for(k=0;k<look->stages;k++)
michael@0 133 if((info->secondstages[j]>>k)&1){
michael@0 134 fprintf(stderr,"%ld,",look->resbits[j][k]);
michael@0 135 acc+=look->resbits[j][k];
michael@0 136 }
michael@0 137
michael@0 138 fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
michael@0 139 acc?(float)acc/(look->resvals[j]*info->grouping):0);
michael@0 140 }
michael@0 141 fprintf(stderr,"\n");*/
michael@0 142
michael@0 143 for(j=0;j<look->parts;j++)
michael@0 144 if(look->partbooks[j])_ogg_free(look->partbooks[j]);
michael@0 145 _ogg_free(look->partbooks);
michael@0 146 for(j=0;j<look->partvals;j++)
michael@0 147 _ogg_free(look->decodemap[j]);
michael@0 148 _ogg_free(look->decodemap);
michael@0 149
michael@0 150 memset(look,0,sizeof(*look));
michael@0 151 _ogg_free(look);
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 static int ilog(unsigned int v){
michael@0 156 int ret=0;
michael@0 157 while(v){
michael@0 158 ret++;
michael@0 159 v>>=1;
michael@0 160 }
michael@0 161 return(ret);
michael@0 162 }
michael@0 163
michael@0 164 static int icount(unsigned int v){
michael@0 165 int ret=0;
michael@0 166 while(v){
michael@0 167 ret+=v&1;
michael@0 168 v>>=1;
michael@0 169 }
michael@0 170 return(ret);
michael@0 171 }
michael@0 172
michael@0 173
michael@0 174 void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
michael@0 175 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
michael@0 176 int j,acc=0;
michael@0 177 oggpack_write(opb,info->begin,24);
michael@0 178 oggpack_write(opb,info->end,24);
michael@0 179
michael@0 180 oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
michael@0 181 code with a partitioned book */
michael@0 182 oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
michael@0 183 oggpack_write(opb,info->groupbook,8); /* group huffman book */
michael@0 184
michael@0 185 /* secondstages is a bitmask; as encoding progresses pass by pass, a
michael@0 186 bitmask of one indicates this partition class has bits to write
michael@0 187 this pass */
michael@0 188 for(j=0;j<info->partitions;j++){
michael@0 189 if(ilog(info->secondstages[j])>3){
michael@0 190 /* yes, this is a minor hack due to not thinking ahead */
michael@0 191 oggpack_write(opb,info->secondstages[j],3);
michael@0 192 oggpack_write(opb,1,1);
michael@0 193 oggpack_write(opb,info->secondstages[j]>>3,5);
michael@0 194 }else
michael@0 195 oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
michael@0 196 acc+=icount(info->secondstages[j]);
michael@0 197 }
michael@0 198 for(j=0;j<acc;j++)
michael@0 199 oggpack_write(opb,info->booklist[j],8);
michael@0 200
michael@0 201 }
michael@0 202
michael@0 203 /* vorbis_info is for range checking */
michael@0 204 vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
michael@0 205 int j,acc=0;
michael@0 206 vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
michael@0 207 codec_setup_info *ci=vi->codec_setup;
michael@0 208
michael@0 209 info->begin=oggpack_read(opb,24);
michael@0 210 info->end=oggpack_read(opb,24);
michael@0 211 info->grouping=oggpack_read(opb,24)+1;
michael@0 212 info->partitions=oggpack_read(opb,6)+1;
michael@0 213 info->groupbook=oggpack_read(opb,8);
michael@0 214
michael@0 215 /* check for premature EOP */
michael@0 216 if(info->groupbook<0)goto errout;
michael@0 217
michael@0 218 for(j=0;j<info->partitions;j++){
michael@0 219 int cascade=oggpack_read(opb,3);
michael@0 220 int cflag=oggpack_read(opb,1);
michael@0 221 if(cflag<0) goto errout;
michael@0 222 if(cflag){
michael@0 223 int c=oggpack_read(opb,5);
michael@0 224 if(c<0) goto errout;
michael@0 225 cascade|=(c<<3);
michael@0 226 }
michael@0 227 info->secondstages[j]=cascade;
michael@0 228
michael@0 229 acc+=icount(cascade);
michael@0 230 }
michael@0 231 for(j=0;j<acc;j++){
michael@0 232 int book=oggpack_read(opb,8);
michael@0 233 if(book<0) goto errout;
michael@0 234 info->booklist[j]=book;
michael@0 235 }
michael@0 236
michael@0 237 if(info->groupbook>=ci->books)goto errout;
michael@0 238 for(j=0;j<acc;j++){
michael@0 239 if(info->booklist[j]>=ci->books)goto errout;
michael@0 240 if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
michael@0 241 }
michael@0 242
michael@0 243 /* verify the phrasebook is not specifying an impossible or
michael@0 244 inconsistent partitioning scheme. */
michael@0 245 /* modify the phrasebook ranging check from r16327; an early beta
michael@0 246 encoder had a bug where it used an oversized phrasebook by
michael@0 247 accident. These files should continue to be playable, but don't
michael@0 248 allow an exploit */
michael@0 249 {
michael@0 250 int entries = ci->book_param[info->groupbook]->entries;
michael@0 251 int dim = ci->book_param[info->groupbook]->dim;
michael@0 252 int partvals = 1;
michael@0 253 if (dim<1) goto errout;
michael@0 254 while(dim>0){
michael@0 255 partvals *= info->partitions;
michael@0 256 if(partvals > entries) goto errout;
michael@0 257 dim--;
michael@0 258 }
michael@0 259 info->partvals = partvals;
michael@0 260 }
michael@0 261
michael@0 262 return(info);
michael@0 263 errout:
michael@0 264 res0_free_info(info);
michael@0 265 return(NULL);
michael@0 266 }
michael@0 267
michael@0 268 vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
michael@0 269 vorbis_info_residue *vr){
michael@0 270 vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
michael@0 271 vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
michael@0 272 codec_setup_info *ci=vd->vi->codec_setup;
michael@0 273
michael@0 274 int j,k,acc=0;
michael@0 275 int dim;
michael@0 276 int maxstage=0;
michael@0 277 look->info=info;
michael@0 278
michael@0 279 look->parts=info->partitions;
michael@0 280 look->fullbooks=ci->fullbooks;
michael@0 281 look->phrasebook=ci->fullbooks+info->groupbook;
michael@0 282 dim=look->phrasebook->dim;
michael@0 283
michael@0 284 look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
michael@0 285
michael@0 286 for(j=0;j<look->parts;j++){
michael@0 287 int stages=ilog(info->secondstages[j]);
michael@0 288 if(stages){
michael@0 289 if(stages>maxstage)maxstage=stages;
michael@0 290 look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
michael@0 291 for(k=0;k<stages;k++)
michael@0 292 if(info->secondstages[j]&(1<<k)){
michael@0 293 look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
michael@0 294 #ifdef TRAIN_RES
michael@0 295 look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
michael@0 296 sizeof(***look->training_data));
michael@0 297 #endif
michael@0 298 }
michael@0 299 }
michael@0 300 }
michael@0 301
michael@0 302 look->partvals=1;
michael@0 303 for(j=0;j<dim;j++)
michael@0 304 look->partvals*=look->parts;
michael@0 305
michael@0 306 look->stages=maxstage;
michael@0 307 look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
michael@0 308 for(j=0;j<look->partvals;j++){
michael@0 309 long val=j;
michael@0 310 long mult=look->partvals/look->parts;
michael@0 311 look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
michael@0 312 for(k=0;k<dim;k++){
michael@0 313 long deco=val/mult;
michael@0 314 val-=deco*mult;
michael@0 315 mult/=look->parts;
michael@0 316 look->decodemap[j][k]=deco;
michael@0 317 }
michael@0 318 }
michael@0 319 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
michael@0 320 {
michael@0 321 static int train_seq=0;
michael@0 322 look->train_seq=train_seq++;
michael@0 323 }
michael@0 324 #endif
michael@0 325 return(look);
michael@0 326 }
michael@0 327
michael@0 328 /* break an abstraction and copy some code for performance purposes */
michael@0 329 static int local_book_besterror(codebook *book,int *a){
michael@0 330 int dim=book->dim;
michael@0 331 int i,j,o;
michael@0 332 int minval=book->minval;
michael@0 333 int del=book->delta;
michael@0 334 int qv=book->quantvals;
michael@0 335 int ze=(qv>>1);
michael@0 336 int index=0;
michael@0 337 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
michael@0 338 int p[8]={0,0,0,0,0,0,0,0};
michael@0 339
michael@0 340 if(del!=1){
michael@0 341 for(i=0,o=dim;i<dim;i++){
michael@0 342 int v = (a[--o]-minval+(del>>1))/del;
michael@0 343 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
michael@0 344 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
michael@0 345 p[o]=v*del+minval;
michael@0 346 }
michael@0 347 }else{
michael@0 348 for(i=0,o=dim;i<dim;i++){
michael@0 349 int v = a[--o]-minval;
michael@0 350 int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
michael@0 351 index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
michael@0 352 p[o]=v*del+minval;
michael@0 353 }
michael@0 354 }
michael@0 355
michael@0 356 if(book->c->lengthlist[index]<=0){
michael@0 357 const static_codebook *c=book->c;
michael@0 358 int best=-1;
michael@0 359 /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
michael@0 360 int e[8]={0,0,0,0,0,0,0,0};
michael@0 361 int maxval = book->minval + book->delta*(book->quantvals-1);
michael@0 362 for(i=0;i<book->entries;i++){
michael@0 363 if(c->lengthlist[i]>0){
michael@0 364 int this=0;
michael@0 365 for(j=0;j<dim;j++){
michael@0 366 int val=(e[j]-a[j]);
michael@0 367 this+=val*val;
michael@0 368 }
michael@0 369 if(best==-1 || this<best){
michael@0 370 memcpy(p,e,sizeof(p));
michael@0 371 best=this;
michael@0 372 index=i;
michael@0 373 }
michael@0 374 }
michael@0 375 /* assumes the value patterning created by the tools in vq/ */
michael@0 376 j=0;
michael@0 377 while(e[j]>=maxval)
michael@0 378 e[j++]=0;
michael@0 379 if(e[j]>=0)
michael@0 380 e[j]+=book->delta;
michael@0 381 e[j]= -e[j];
michael@0 382 }
michael@0 383 }
michael@0 384
michael@0 385 if(index>-1){
michael@0 386 for(i=0;i<dim;i++)
michael@0 387 *a++ -= p[i];
michael@0 388 }
michael@0 389
michael@0 390 return(index);
michael@0 391 }
michael@0 392
michael@0 393 #ifdef TRAIN_RES
michael@0 394 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
michael@0 395 codebook *book,long *acc){
michael@0 396 #else
michael@0 397 static int _encodepart(oggpack_buffer *opb,int *vec, int n,
michael@0 398 codebook *book){
michael@0 399 #endif
michael@0 400 int i,bits=0;
michael@0 401 int dim=book->dim;
michael@0 402 int step=n/dim;
michael@0 403
michael@0 404 for(i=0;i<step;i++){
michael@0 405 int entry=local_book_besterror(book,vec+i*dim);
michael@0 406
michael@0 407 #ifdef TRAIN_RES
michael@0 408 if(entry>=0)
michael@0 409 acc[entry]++;
michael@0 410 #endif
michael@0 411
michael@0 412 bits+=vorbis_book_encode(book,entry,opb);
michael@0 413
michael@0 414 }
michael@0 415
michael@0 416 return(bits);
michael@0 417 }
michael@0 418
michael@0 419 static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 420 int **in,int ch){
michael@0 421 long i,j,k;
michael@0 422 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
michael@0 423 vorbis_info_residue0 *info=look->info;
michael@0 424
michael@0 425 /* move all this setup out later */
michael@0 426 int samples_per_partition=info->grouping;
michael@0 427 int possible_partitions=info->partitions;
michael@0 428 int n=info->end-info->begin;
michael@0 429
michael@0 430 int partvals=n/samples_per_partition;
michael@0 431 long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
michael@0 432 float scale=100./samples_per_partition;
michael@0 433
michael@0 434 /* we find the partition type for each partition of each
michael@0 435 channel. We'll go back and do the interleaved encoding in a
michael@0 436 bit. For now, clarity */
michael@0 437
michael@0 438 for(i=0;i<ch;i++){
michael@0 439 partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
michael@0 440 memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
michael@0 441 }
michael@0 442
michael@0 443 for(i=0;i<partvals;i++){
michael@0 444 int offset=i*samples_per_partition+info->begin;
michael@0 445 for(j=0;j<ch;j++){
michael@0 446 int max=0;
michael@0 447 int ent=0;
michael@0 448 for(k=0;k<samples_per_partition;k++){
michael@0 449 if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
michael@0 450 ent+=abs(in[j][offset+k]);
michael@0 451 }
michael@0 452 ent*=scale;
michael@0 453
michael@0 454 for(k=0;k<possible_partitions-1;k++)
michael@0 455 if(max<=info->classmetric1[k] &&
michael@0 456 (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
michael@0 457 break;
michael@0 458
michael@0 459 partword[j][i]=k;
michael@0 460 }
michael@0 461 }
michael@0 462
michael@0 463 #ifdef TRAIN_RESAUX
michael@0 464 {
michael@0 465 FILE *of;
michael@0 466 char buffer[80];
michael@0 467
michael@0 468 for(i=0;i<ch;i++){
michael@0 469 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
michael@0 470 of=fopen(buffer,"a");
michael@0 471 for(j=0;j<partvals;j++)
michael@0 472 fprintf(of,"%ld, ",partword[i][j]);
michael@0 473 fprintf(of,"\n");
michael@0 474 fclose(of);
michael@0 475 }
michael@0 476 }
michael@0 477 #endif
michael@0 478 look->frames++;
michael@0 479
michael@0 480 return(partword);
michael@0 481 }
michael@0 482
michael@0 483 /* designed for stereo or other modes where the partition size is an
michael@0 484 integer multiple of the number of channels encoded in the current
michael@0 485 submap */
michael@0 486 static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
michael@0 487 int ch){
michael@0 488 long i,j,k,l;
michael@0 489 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
michael@0 490 vorbis_info_residue0 *info=look->info;
michael@0 491
michael@0 492 /* move all this setup out later */
michael@0 493 int samples_per_partition=info->grouping;
michael@0 494 int possible_partitions=info->partitions;
michael@0 495 int n=info->end-info->begin;
michael@0 496
michael@0 497 int partvals=n/samples_per_partition;
michael@0 498 long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
michael@0 499
michael@0 500 #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
michael@0 501 FILE *of;
michael@0 502 char buffer[80];
michael@0 503 #endif
michael@0 504
michael@0 505 partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
michael@0 506 memset(partword[0],0,partvals*sizeof(*partword[0]));
michael@0 507
michael@0 508 for(i=0,l=info->begin/ch;i<partvals;i++){
michael@0 509 int magmax=0;
michael@0 510 int angmax=0;
michael@0 511 for(j=0;j<samples_per_partition;j+=ch){
michael@0 512 if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
michael@0 513 for(k=1;k<ch;k++)
michael@0 514 if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
michael@0 515 l++;
michael@0 516 }
michael@0 517
michael@0 518 for(j=0;j<possible_partitions-1;j++)
michael@0 519 if(magmax<=info->classmetric1[j] &&
michael@0 520 angmax<=info->classmetric2[j])
michael@0 521 break;
michael@0 522
michael@0 523 partword[0][i]=j;
michael@0 524
michael@0 525 }
michael@0 526
michael@0 527 #ifdef TRAIN_RESAUX
michael@0 528 sprintf(buffer,"resaux_%d.vqd",look->train_seq);
michael@0 529 of=fopen(buffer,"a");
michael@0 530 for(i=0;i<partvals;i++)
michael@0 531 fprintf(of,"%ld, ",partword[0][i]);
michael@0 532 fprintf(of,"\n");
michael@0 533 fclose(of);
michael@0 534 #endif
michael@0 535
michael@0 536 look->frames++;
michael@0 537
michael@0 538 return(partword);
michael@0 539 }
michael@0 540
michael@0 541 static int _01forward(oggpack_buffer *opb,
michael@0 542 vorbis_look_residue *vl,
michael@0 543 int **in,int ch,
michael@0 544 long **partword,
michael@0 545 #ifdef TRAIN_RES
michael@0 546 int (*encode)(oggpack_buffer *,int *,int,
michael@0 547 codebook *,long *),
michael@0 548 int submap
michael@0 549 #else
michael@0 550 int (*encode)(oggpack_buffer *,int *,int,
michael@0 551 codebook *)
michael@0 552 #endif
michael@0 553 ){
michael@0 554 long i,j,k,s;
michael@0 555 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
michael@0 556 vorbis_info_residue0 *info=look->info;
michael@0 557
michael@0 558 #ifdef TRAIN_RES
michael@0 559 look->submap=submap;
michael@0 560 #endif
michael@0 561
michael@0 562 /* move all this setup out later */
michael@0 563 int samples_per_partition=info->grouping;
michael@0 564 int possible_partitions=info->partitions;
michael@0 565 int partitions_per_word=look->phrasebook->dim;
michael@0 566 int n=info->end-info->begin;
michael@0 567
michael@0 568 int partvals=n/samples_per_partition;
michael@0 569 long resbits[128];
michael@0 570 long resvals[128];
michael@0 571
michael@0 572 #ifdef TRAIN_RES
michael@0 573 for(i=0;i<ch;i++)
michael@0 574 for(j=info->begin;j<info->end;j++){
michael@0 575 if(in[i][j]>look->tmax)look->tmax=in[i][j];
michael@0 576 if(in[i][j]<look->tmin)look->tmin=in[i][j];
michael@0 577 }
michael@0 578 #endif
michael@0 579
michael@0 580 memset(resbits,0,sizeof(resbits));
michael@0 581 memset(resvals,0,sizeof(resvals));
michael@0 582
michael@0 583 /* we code the partition words for each channel, then the residual
michael@0 584 words for a partition per channel until we've written all the
michael@0 585 residual words for that partition word. Then write the next
michael@0 586 partition channel words... */
michael@0 587
michael@0 588 for(s=0;s<look->stages;s++){
michael@0 589
michael@0 590 for(i=0;i<partvals;){
michael@0 591
michael@0 592 /* first we encode a partition codeword for each channel */
michael@0 593 if(s==0){
michael@0 594 for(j=0;j<ch;j++){
michael@0 595 long val=partword[j][i];
michael@0 596 for(k=1;k<partitions_per_word;k++){
michael@0 597 val*=possible_partitions;
michael@0 598 if(i+k<partvals)
michael@0 599 val+=partword[j][i+k];
michael@0 600 }
michael@0 601
michael@0 602 /* training hack */
michael@0 603 if(val<look->phrasebook->entries)
michael@0 604 look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
michael@0 605 #if 0 /*def TRAIN_RES*/
michael@0 606 else
michael@0 607 fprintf(stderr,"!");
michael@0 608 #endif
michael@0 609
michael@0 610 }
michael@0 611 }
michael@0 612
michael@0 613 /* now we encode interleaved residual values for the partitions */
michael@0 614 for(k=0;k<partitions_per_word && i<partvals;k++,i++){
michael@0 615 long offset=i*samples_per_partition+info->begin;
michael@0 616
michael@0 617 for(j=0;j<ch;j++){
michael@0 618 if(s==0)resvals[partword[j][i]]+=samples_per_partition;
michael@0 619 if(info->secondstages[partword[j][i]]&(1<<s)){
michael@0 620 codebook *statebook=look->partbooks[partword[j][i]][s];
michael@0 621 if(statebook){
michael@0 622 int ret;
michael@0 623 #ifdef TRAIN_RES
michael@0 624 long *accumulator=NULL;
michael@0 625 accumulator=look->training_data[s][partword[j][i]];
michael@0 626 {
michael@0 627 int l;
michael@0 628 int *samples=in[j]+offset;
michael@0 629 for(l=0;l<samples_per_partition;l++){
michael@0 630 if(samples[l]<look->training_min[s][partword[j][i]])
michael@0 631 look->training_min[s][partword[j][i]]=samples[l];
michael@0 632 if(samples[l]>look->training_max[s][partword[j][i]])
michael@0 633 look->training_max[s][partword[j][i]]=samples[l];
michael@0 634 }
michael@0 635 }
michael@0 636 ret=encode(opb,in[j]+offset,samples_per_partition,
michael@0 637 statebook,accumulator);
michael@0 638 #else
michael@0 639 ret=encode(opb,in[j]+offset,samples_per_partition,
michael@0 640 statebook);
michael@0 641 #endif
michael@0 642
michael@0 643 look->postbits+=ret;
michael@0 644 resbits[partword[j][i]]+=ret;
michael@0 645 }
michael@0 646 }
michael@0 647 }
michael@0 648 }
michael@0 649 }
michael@0 650 }
michael@0 651
michael@0 652 return(0);
michael@0 653 }
michael@0 654
michael@0 655 /* a truncated packet here just means 'stop working'; it's not an error */
michael@0 656 static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 657 float **in,int ch,
michael@0 658 long (*decodepart)(codebook *, float *,
michael@0 659 oggpack_buffer *,int)){
michael@0 660
michael@0 661 long i,j,k,l,s;
michael@0 662 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
michael@0 663 vorbis_info_residue0 *info=look->info;
michael@0 664
michael@0 665 /* move all this setup out later */
michael@0 666 int samples_per_partition=info->grouping;
michael@0 667 int partitions_per_word=look->phrasebook->dim;
michael@0 668 int max=vb->pcmend>>1;
michael@0 669 int end=(info->end<max?info->end:max);
michael@0 670 int n=end-info->begin;
michael@0 671
michael@0 672 if(n>0){
michael@0 673 int partvals=n/samples_per_partition;
michael@0 674 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
michael@0 675 int ***partword=alloca(ch*sizeof(*partword));
michael@0 676
michael@0 677 for(j=0;j<ch;j++)
michael@0 678 partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
michael@0 679
michael@0 680 for(s=0;s<look->stages;s++){
michael@0 681
michael@0 682 /* each loop decodes on partition codeword containing
michael@0 683 partitions_per_word partitions */
michael@0 684 for(i=0,l=0;i<partvals;l++){
michael@0 685 if(s==0){
michael@0 686 /* fetch the partition word for each channel */
michael@0 687 for(j=0;j<ch;j++){
michael@0 688 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
michael@0 689
michael@0 690 if(temp==-1 || temp>=info->partvals)goto eopbreak;
michael@0 691 partword[j][l]=look->decodemap[temp];
michael@0 692 if(partword[j][l]==NULL)goto errout;
michael@0 693 }
michael@0 694 }
michael@0 695
michael@0 696 /* now we decode residual values for the partitions */
michael@0 697 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
michael@0 698 for(j=0;j<ch;j++){
michael@0 699 long offset=info->begin+i*samples_per_partition;
michael@0 700 if(info->secondstages[partword[j][l][k]]&(1<<s)){
michael@0 701 codebook *stagebook=look->partbooks[partword[j][l][k]][s];
michael@0 702 if(stagebook){
michael@0 703 if(decodepart(stagebook,in[j]+offset,&vb->opb,
michael@0 704 samples_per_partition)==-1)goto eopbreak;
michael@0 705 }
michael@0 706 }
michael@0 707 }
michael@0 708 }
michael@0 709 }
michael@0 710 }
michael@0 711 errout:
michael@0 712 eopbreak:
michael@0 713 return(0);
michael@0 714 }
michael@0 715
michael@0 716 int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 717 float **in,int *nonzero,int ch){
michael@0 718 int i,used=0;
michael@0 719 for(i=0;i<ch;i++)
michael@0 720 if(nonzero[i])
michael@0 721 in[used++]=in[i];
michael@0 722 if(used)
michael@0 723 return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
michael@0 724 else
michael@0 725 return(0);
michael@0 726 }
michael@0 727
michael@0 728 int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
michael@0 729 int **in,int *nonzero,int ch, long **partword, int submap){
michael@0 730 int i,used=0;
michael@0 731 (void)vb;
michael@0 732 for(i=0;i<ch;i++)
michael@0 733 if(nonzero[i])
michael@0 734 in[used++]=in[i];
michael@0 735
michael@0 736 if(used){
michael@0 737 #ifdef TRAIN_RES
michael@0 738 return _01forward(opb,vl,in,used,partword,_encodepart,submap);
michael@0 739 #else
michael@0 740 (void)submap;
michael@0 741 return _01forward(opb,vl,in,used,partword,_encodepart);
michael@0 742 #endif
michael@0 743 }else{
michael@0 744 return(0);
michael@0 745 }
michael@0 746 }
michael@0 747
michael@0 748 long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 749 int **in,int *nonzero,int ch){
michael@0 750 int i,used=0;
michael@0 751 for(i=0;i<ch;i++)
michael@0 752 if(nonzero[i])
michael@0 753 in[used++]=in[i];
michael@0 754 if(used)
michael@0 755 return(_01class(vb,vl,in,used));
michael@0 756 else
michael@0 757 return(0);
michael@0 758 }
michael@0 759
michael@0 760 int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 761 float **in,int *nonzero,int ch){
michael@0 762 int i,used=0;
michael@0 763 for(i=0;i<ch;i++)
michael@0 764 if(nonzero[i])
michael@0 765 in[used++]=in[i];
michael@0 766 if(used)
michael@0 767 return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
michael@0 768 else
michael@0 769 return(0);
michael@0 770 }
michael@0 771
michael@0 772 long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 773 int **in,int *nonzero,int ch){
michael@0 774 int i,used=0;
michael@0 775 for(i=0;i<ch;i++)
michael@0 776 if(nonzero[i])used++;
michael@0 777 if(used)
michael@0 778 return(_2class(vb,vl,in,ch));
michael@0 779 else
michael@0 780 return(0);
michael@0 781 }
michael@0 782
michael@0 783 /* res2 is slightly more different; all the channels are interleaved
michael@0 784 into a single vector and encoded. */
michael@0 785
michael@0 786 int res2_forward(oggpack_buffer *opb,
michael@0 787 vorbis_block *vb,vorbis_look_residue *vl,
michael@0 788 int **in,int *nonzero,int ch, long **partword,int submap){
michael@0 789 long i,j,k,n=vb->pcmend/2,used=0;
michael@0 790
michael@0 791 /* don't duplicate the code; use a working vector hack for now and
michael@0 792 reshape ourselves into a single channel res1 */
michael@0 793 /* ugly; reallocs for each coupling pass :-( */
michael@0 794 int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
michael@0 795 for(i=0;i<ch;i++){
michael@0 796 int *pcm=in[i];
michael@0 797 if(nonzero[i])used++;
michael@0 798 for(j=0,k=i;j<n;j++,k+=ch)
michael@0 799 work[k]=pcm[j];
michael@0 800 }
michael@0 801
michael@0 802 if(used){
michael@0 803 #ifdef TRAIN_RES
michael@0 804 return _01forward(opb,vl,&work,1,partword,_encodepart,submap);
michael@0 805 #else
michael@0 806 (void)submap;
michael@0 807 return _01forward(opb,vl,&work,1,partword,_encodepart);
michael@0 808 #endif
michael@0 809 }else{
michael@0 810 return(0);
michael@0 811 }
michael@0 812 }
michael@0 813
michael@0 814 /* duplicate code here as speed is somewhat more important */
michael@0 815 int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
michael@0 816 float **in,int *nonzero,int ch){
michael@0 817 long i,k,l,s;
michael@0 818 vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
michael@0 819 vorbis_info_residue0 *info=look->info;
michael@0 820
michael@0 821 /* move all this setup out later */
michael@0 822 int samples_per_partition=info->grouping;
michael@0 823 int partitions_per_word=look->phrasebook->dim;
michael@0 824 int max=(vb->pcmend*ch)>>1;
michael@0 825 int end=(info->end<max?info->end:max);
michael@0 826 int n=end-info->begin;
michael@0 827
michael@0 828 if(n>0){
michael@0 829 int partvals=n/samples_per_partition;
michael@0 830 int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
michael@0 831 int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
michael@0 832
michael@0 833 for(i=0;i<ch;i++)if(nonzero[i])break;
michael@0 834 if(i==ch)return(0); /* no nonzero vectors */
michael@0 835
michael@0 836 for(s=0;s<look->stages;s++){
michael@0 837 for(i=0,l=0;i<partvals;l++){
michael@0 838
michael@0 839 if(s==0){
michael@0 840 /* fetch the partition word */
michael@0 841 int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
michael@0 842 if(temp==-1 || temp>=info->partvals)goto eopbreak;
michael@0 843 partword[l]=look->decodemap[temp];
michael@0 844 if(partword[l]==NULL)goto errout;
michael@0 845 }
michael@0 846
michael@0 847 /* now we decode residual values for the partitions */
michael@0 848 for(k=0;k<partitions_per_word && i<partvals;k++,i++)
michael@0 849 if(info->secondstages[partword[l][k]]&(1<<s)){
michael@0 850 codebook *stagebook=look->partbooks[partword[l][k]][s];
michael@0 851
michael@0 852 if(stagebook){
michael@0 853 if(vorbis_book_decodevv_add(stagebook,in,
michael@0 854 i*samples_per_partition+info->begin,ch,
michael@0 855 &vb->opb,samples_per_partition)==-1)
michael@0 856 goto eopbreak;
michael@0 857 }
michael@0 858 }
michael@0 859 }
michael@0 860 }
michael@0 861 }
michael@0 862 errout:
michael@0 863 eopbreak:
michael@0 864 return(0);
michael@0 865 }
michael@0 866
michael@0 867
michael@0 868 const vorbis_func_residue residue0_exportbundle={
michael@0 869 NULL,
michael@0 870 &res0_unpack,
michael@0 871 &res0_look,
michael@0 872 &res0_free_info,
michael@0 873 &res0_free_look,
michael@0 874 NULL,
michael@0 875 NULL,
michael@0 876 &res0_inverse
michael@0 877 };
michael@0 878
michael@0 879 const vorbis_func_residue residue1_exportbundle={
michael@0 880 &res0_pack,
michael@0 881 &res0_unpack,
michael@0 882 &res0_look,
michael@0 883 &res0_free_info,
michael@0 884 &res0_free_look,
michael@0 885 &res1_class,
michael@0 886 &res1_forward,
michael@0 887 &res1_inverse
michael@0 888 };
michael@0 889
michael@0 890 const vorbis_func_residue residue2_exportbundle={
michael@0 891 &res0_pack,
michael@0 892 &res0_unpack,
michael@0 893 &res0_look,
michael@0 894 &res0_free_info,
michael@0 895 &res0_free_look,
michael@0 896 &res2_class,
michael@0 897 &res2_forward,
michael@0 898 &res2_inverse
michael@0 899 };

mercurial