res0.c 25 KB

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