res0.c 24 KB

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