res0.c 23 KB

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