res0.c 24 KB

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