res0.c 22 KB

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