dbf-halloween2015

annotate libs/vorbis/res0.c @ 4:4316c0c879e9

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