res0.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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.50 2003/12/30 11:02:22 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. #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. {
  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. acc[entry]++;
  300. #endif
  301. bits+=vorbis_book_encode(book,entry,opb);
  302. }
  303. return(bits);
  304. }
  305. static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
  306. float **in,int ch){
  307. long i,j,k;
  308. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  309. vorbis_info_residue0 *info=look->info;
  310. vorbis_info *vi=vb->vd->vi;
  311. codec_setup_info *ci=vi->codec_setup;
  312. /* move all this setup out later */
  313. int samples_per_partition=info->grouping;
  314. int possible_partitions=info->partitions;
  315. int n=info->end-info->begin;
  316. int partvals=n/samples_per_partition;
  317. long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
  318. float scale=100./samples_per_partition;
  319. /* we find the partition type for each partition of each
  320. channel. We'll go back and do the interleaved encoding in a
  321. bit. For now, clarity */
  322. for(i=0;i<ch;i++){
  323. partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
  324. memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
  325. }
  326. for(i=0;i<partvals;i++){
  327. int offset=i*samples_per_partition+info->begin;
  328. for(j=0;j<ch;j++){
  329. float max=0.;
  330. float ent=0.;
  331. for(k=0;k<samples_per_partition;k++){
  332. if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]);
  333. ent+=fabs(rint(in[j][offset+k]));
  334. }
  335. ent*=scale;
  336. for(k=0;k<possible_partitions-1;k++)
  337. if(max<=info->classmetric1[k] &&
  338. (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k]))
  339. break;
  340. partword[j][i]=k;
  341. }
  342. }
  343. #ifdef TRAIN_RESAUX
  344. {
  345. FILE *of;
  346. char buffer[80];
  347. for(i=0;i<ch;i++){
  348. sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  349. of=fopen(buffer,"a");
  350. for(j=0;j<partvals;j++)
  351. fprintf(of,"%ld, ",partword[i][j]);
  352. fprintf(of,"\n");
  353. fclose(of);
  354. }
  355. }
  356. #endif
  357. look->frames++;
  358. return(partword);
  359. }
  360. /* designed for stereo or other modes where the partition size is an
  361. integer multiple of the number of channels encoded in the current
  362. submap */
  363. static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in,
  364. int ch){
  365. long i,j,k,l;
  366. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  367. vorbis_info_residue0 *info=look->info;
  368. /* move all this setup out later */
  369. int samples_per_partition=info->grouping;
  370. int possible_partitions=info->partitions;
  371. int n=info->end-info->begin;
  372. int partvals=n/samples_per_partition;
  373. long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
  374. #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
  375. FILE *of;
  376. char buffer[80];
  377. #endif
  378. partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0]));
  379. memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0]));
  380. for(i=0,l=info->begin/ch;i<partvals;i++){
  381. float magmax=0.f;
  382. float angmax=0.f;
  383. for(j=0;j<samples_per_partition;j+=ch){
  384. if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]);
  385. for(k=1;k<ch;k++)
  386. if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]);
  387. l++;
  388. }
  389. for(j=0;j<possible_partitions-1;j++)
  390. if(magmax<=info->classmetric1[j] &&
  391. angmax<=info->classmetric2[j])
  392. break;
  393. partword[0][i]=j;
  394. }
  395. #ifdef TRAIN_RESAUX
  396. sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  397. of=fopen(buffer,"a");
  398. for(i=0;i<partvals;i++)
  399. fprintf(of,"%ld, ",partword[0][i]);
  400. fprintf(of,"\n");
  401. fclose(of);
  402. #endif
  403. look->frames++;
  404. return(partword);
  405. }
  406. static int _01forward(oggpack_buffer *opb,
  407. vorbis_block *vb,vorbis_look_residue *vl,
  408. float **in,int ch,
  409. long **partword,
  410. int (*encode)(oggpack_buffer *,float *,int,
  411. codebook *,long *)){
  412. long i,j,k,s;
  413. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  414. vorbis_info_residue0 *info=look->info;
  415. vorbis_dsp_state *vd=vb->vd;
  416. /* move all this setup out later */
  417. int samples_per_partition=info->grouping;
  418. int possible_partitions=info->partitions;
  419. int partitions_per_word=look->phrasebook->dim;
  420. int n=info->end-info->begin;
  421. int partvals=n/samples_per_partition;
  422. long resbits[128];
  423. long resvals[128];
  424. #ifdef TRAIN_RES
  425. for(i=0;i<ch;i++)
  426. for(j=info->begin;j<info->end;j++){
  427. if(in[i][j]>look->tmax)look->tmax=in[i][j];
  428. if(in[i][j]<look->tmin)look->tmin=in[i][j];
  429. }
  430. #endif
  431. memset(resbits,0,sizeof(resbits));
  432. memset(resvals,0,sizeof(resvals));
  433. /* we code the partition words for each channel, then the residual
  434. words for a partition per channel until we've written all the
  435. residual words for that partition word. Then write the next
  436. partition channel words... */
  437. for(s=0;s<look->stages;s++){
  438. for(i=0;i<partvals;){
  439. /* first we encode a partition codeword for each channel */
  440. if(s==0){
  441. for(j=0;j<ch;j++){
  442. long val=partword[j][i];
  443. for(k=1;k<partitions_per_word;k++){
  444. val*=possible_partitions;
  445. if(i+k<partvals)
  446. val+=partword[j][i+k];
  447. }
  448. /* training hack */
  449. if(val<look->phrasebook->entries)
  450. look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
  451. #if 0 /*def TRAIN_RES*/
  452. else
  453. fprintf(stderr,"!");
  454. #endif
  455. }
  456. }
  457. /* now we encode interleaved residual values for the partitions */
  458. for(k=0;k<partitions_per_word && i<partvals;k++,i++){
  459. long offset=i*samples_per_partition+info->begin;
  460. for(j=0;j<ch;j++){
  461. if(s==0)resvals[partword[j][i]]+=samples_per_partition;
  462. if(info->secondstages[partword[j][i]]&(1<<s)){
  463. codebook *statebook=look->partbooks[partword[j][i]][s];
  464. if(statebook){
  465. int ret;
  466. long *accumulator=NULL;
  467. #ifdef TRAIN_RES
  468. accumulator=look->training_data[s][partword[j][i]];
  469. {
  470. int l;
  471. float *samples=in[j]+offset;
  472. for(l=0;l<samples_per_partition;l++){
  473. if(samples[l]<look->training_min[s][partword[j][i]])
  474. look->training_min[s][partword[j][i]]=samples[l];
  475. if(samples[l]>look->training_max[s][partword[j][i]])
  476. look->training_max[s][partword[j][i]]=samples[l];
  477. }
  478. }
  479. #endif
  480. ret=encode(opb,in[j]+offset,samples_per_partition,
  481. statebook,accumulator);
  482. look->postbits+=ret;
  483. resbits[partword[j][i]]+=ret;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. /*{
  491. long total=0;
  492. long totalbits=0;
  493. fprintf(stderr,"%d :: ",vb->mode);
  494. for(k=0;k<possible_partitions;k++){
  495. fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
  496. total+=resvals[k];
  497. totalbits+=resbits[k];
  498. }
  499. fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
  500. }*/
  501. return(0);
  502. }
  503. /* a truncated packet here just means 'stop working'; it's not an error */
  504. static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
  505. float **in,int ch,
  506. long (*decodepart)(codebook *, float *,
  507. oggpack_buffer *,int)){
  508. long i,j,k,l,s;
  509. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  510. vorbis_info_residue0 *info=look->info;
  511. /* move all this setup out later */
  512. int samples_per_partition=info->grouping;
  513. int partitions_per_word=look->phrasebook->dim;
  514. int n=info->end-info->begin;
  515. int partvals=n/samples_per_partition;
  516. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  517. int ***partword=alloca(ch*sizeof(*partword));
  518. for(j=0;j<ch;j++)
  519. partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
  520. for(s=0;s<look->stages;s++){
  521. /* each loop decodes on partition codeword containing
  522. partitions_pre_word partitions */
  523. for(i=0,l=0;i<partvals;l++){
  524. if(s==0){
  525. /* fetch the partition word for each channel */
  526. for(j=0;j<ch;j++){
  527. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  528. if(temp==-1)goto eopbreak;
  529. partword[j][l]=look->decodemap[temp];
  530. if(partword[j][l]==NULL)goto errout;
  531. }
  532. }
  533. /* now we decode residual values for the partitions */
  534. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  535. for(j=0;j<ch;j++){
  536. long offset=info->begin+i*samples_per_partition;
  537. if(info->secondstages[partword[j][l][k]]&(1<<s)){
  538. codebook *stagebook=look->partbooks[partword[j][l][k]][s];
  539. if(stagebook){
  540. if(decodepart(stagebook,in[j]+offset,&vb->opb,
  541. samples_per_partition)==-1)goto eopbreak;
  542. }
  543. }
  544. }
  545. }
  546. }
  547. errout:
  548. eopbreak:
  549. return(0);
  550. }
  551. #if 0
  552. /* residue 0 and 1 are just slight variants of one another. 0 is
  553. interleaved, 1 is not */
  554. long **res0_class(vorbis_block *vb,vorbis_look_residue *vl,
  555. float **in,int *nonzero,int ch){
  556. /* we encode only the nonzero parts of a bundle */
  557. int i,used=0;
  558. for(i=0;i<ch;i++)
  559. if(nonzero[i])
  560. in[used++]=in[i];
  561. if(used)
  562. /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/
  563. return(_01class(vb,vl,in,used));
  564. else
  565. return(0);
  566. }
  567. int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
  568. float **in,float **out,int *nonzero,int ch,
  569. long **partword){
  570. /* we encode only the nonzero parts of a bundle */
  571. int i,j,used=0,n=vb->pcmend/2;
  572. for(i=0;i<ch;i++)
  573. if(nonzero[i]){
  574. if(out)
  575. for(j=0;j<n;j++)
  576. out[i][j]+=in[i][j];
  577. in[used++]=in[i];
  578. }
  579. if(used){
  580. int ret=_01forward(vb,vl,in,used,partword,
  581. _interleaved_encodepart);
  582. if(out){
  583. used=0;
  584. for(i=0;i<ch;i++)
  585. if(nonzero[i]){
  586. for(j=0;j<n;j++)
  587. out[i][j]-=in[used][j];
  588. used++;
  589. }
  590. }
  591. return(ret);
  592. }else{
  593. return(0);
  594. }
  595. }
  596. #endif
  597. int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  598. float **in,int *nonzero,int ch){
  599. int i,used=0;
  600. for(i=0;i<ch;i++)
  601. if(nonzero[i])
  602. in[used++]=in[i];
  603. if(used)
  604. return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
  605. else
  606. return(0);
  607. }
  608. int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
  609. float **in,float **out,int *nonzero,int ch,
  610. long **partword){
  611. int i,j,used=0,n=vb->pcmend/2;
  612. for(i=0;i<ch;i++)
  613. if(nonzero[i]){
  614. if(out)
  615. for(j=0;j<n;j++)
  616. out[i][j]+=in[i][j];
  617. in[used++]=in[i];
  618. }
  619. if(used){
  620. int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart);
  621. if(out){
  622. used=0;
  623. for(i=0;i<ch;i++)
  624. if(nonzero[i]){
  625. for(j=0;j<n;j++)
  626. out[i][j]-=in[used][j];
  627. used++;
  628. }
  629. }
  630. return(ret);
  631. }else{
  632. return(0);
  633. }
  634. }
  635. long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
  636. float **in,int *nonzero,int ch){
  637. int i,used=0;
  638. for(i=0;i<ch;i++)
  639. if(nonzero[i])
  640. in[used++]=in[i];
  641. if(used)
  642. return(_01class(vb,vl,in,used));
  643. else
  644. return(0);
  645. }
  646. int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  647. float **in,int *nonzero,int ch){
  648. int i,used=0;
  649. for(i=0;i<ch;i++)
  650. if(nonzero[i])
  651. in[used++]=in[i];
  652. if(used)
  653. return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
  654. else
  655. return(0);
  656. }
  657. long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
  658. float **in,int *nonzero,int ch){
  659. int i,used=0;
  660. for(i=0;i<ch;i++)
  661. if(nonzero[i])used++;
  662. if(used)
  663. return(_2class(vb,vl,in,ch));
  664. else
  665. return(0);
  666. }
  667. /* res2 is slightly more different; all the channels are interleaved
  668. into a single vector and encoded. */
  669. int res2_forward(oggpack_buffer *opb,
  670. vorbis_block *vb,vorbis_look_residue *vl,
  671. float **in,float **out,int *nonzero,int ch,
  672. long **partword){
  673. long i,j,k,n=vb->pcmend/2,used=0;
  674. /* don't duplicate the code; use a working vector hack for now and
  675. reshape ourselves into a single channel res1 */
  676. /* ugly; reallocs for each coupling pass :-( */
  677. float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
  678. for(i=0;i<ch;i++){
  679. float *pcm=in[i];
  680. if(nonzero[i])used++;
  681. for(j=0,k=i;j<n;j++,k+=ch)
  682. work[k]=pcm[j];
  683. }
  684. if(used){
  685. int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart);
  686. /* update the sofar vector */
  687. if(out){
  688. for(i=0;i<ch;i++){
  689. float *pcm=in[i];
  690. float *sofar=out[i];
  691. for(j=0,k=i;j<n;j++,k+=ch)
  692. sofar[j]+=pcm[j]-work[k];
  693. }
  694. }
  695. return(ret);
  696. }else{
  697. return(0);
  698. }
  699. }
  700. /* duplicate code here as speed is somewhat more important */
  701. int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  702. float **in,int *nonzero,int ch){
  703. long i,k,l,s;
  704. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  705. vorbis_info_residue0 *info=look->info;
  706. /* move all this setup out later */
  707. int samples_per_partition=info->grouping;
  708. int partitions_per_word=look->phrasebook->dim;
  709. int n=info->end-info->begin;
  710. int partvals=n/samples_per_partition;
  711. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  712. int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
  713. for(i=0;i<ch;i++)if(nonzero[i])break;
  714. if(i==ch)return(0); /* no nonzero vectors */
  715. for(s=0;s<look->stages;s++){
  716. for(i=0,l=0;i<partvals;l++){
  717. if(s==0){
  718. /* fetch the partition word */
  719. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  720. if(temp==-1)goto eopbreak;
  721. partword[l]=look->decodemap[temp];
  722. if(partword[l]==NULL)goto errout;
  723. }
  724. /* now we decode residual values for the partitions */
  725. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  726. if(info->secondstages[partword[l][k]]&(1<<s)){
  727. codebook *stagebook=look->partbooks[partword[l][k]][s];
  728. if(stagebook){
  729. if(vorbis_book_decodevv_add(stagebook,in,
  730. i*samples_per_partition+info->begin,ch,
  731. &vb->opb,samples_per_partition)==-1)
  732. goto eopbreak;
  733. }
  734. }
  735. }
  736. }
  737. errout:
  738. eopbreak:
  739. return(0);
  740. }
  741. vorbis_func_residue residue0_exportbundle={
  742. NULL,
  743. &res0_unpack,
  744. &res0_look,
  745. &res0_free_info,
  746. &res0_free_look,
  747. NULL,
  748. NULL,
  749. &res0_inverse
  750. };
  751. vorbis_func_residue residue1_exportbundle={
  752. &res0_pack,
  753. &res0_unpack,
  754. &res0_look,
  755. &res0_free_info,
  756. &res0_free_look,
  757. &res1_class,
  758. &res1_forward,
  759. &res1_inverse
  760. };
  761. vorbis_func_residue residue2_exportbundle={
  762. &res0_pack,
  763. &res0_unpack,
  764. &res0_look,
  765. &res0_free_info,
  766. &res0_free_look,
  767. &res2_class,
  768. &res2_forward,
  769. &res2_inverse
  770. };