res0.c 24 KB

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