res0.c 25 KB

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