res0.c 24 KB

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