vrshoot

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