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