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. last mod: $Id: res0.c 17556 2010-10-21 18:25:19Z 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. static int _encodepart(oggpack_buffer *opb,int *vec, int n,
  337. codebook *book,long *acc){
  338. int i,bits=0;
  339. int dim=book->dim;
  340. int step=n/dim;
  341. for(i=0;i<step;i++){
  342. int entry=local_book_besterror(book,vec+i*dim);
  343. #ifdef TRAIN_RES
  344. if(entry>=0)
  345. acc[entry]++;
  346. #endif
  347. bits+=vorbis_book_encode(book,entry,opb);
  348. }
  349. return(bits);
  350. }
  351. static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
  352. int **in,int ch){
  353. long i,j,k;
  354. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  355. vorbis_info_residue0 *info=look->info;
  356. /* move all this setup out later */
  357. int samples_per_partition=info->grouping;
  358. int possible_partitions=info->partitions;
  359. int n=info->end-info->begin;
  360. int partvals=n/samples_per_partition;
  361. long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
  362. float scale=100./samples_per_partition;
  363. /* we find the partition type for each partition of each
  364. channel. We'll go back and do the interleaved encoding in a
  365. bit. For now, clarity */
  366. for(i=0;i<ch;i++){
  367. partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
  368. memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
  369. }
  370. for(i=0;i<partvals;i++){
  371. int offset=i*samples_per_partition+info->begin;
  372. for(j=0;j<ch;j++){
  373. int max=0;
  374. int ent=0;
  375. for(k=0;k<samples_per_partition;k++){
  376. if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
  377. ent+=abs(in[j][offset+k]);
  378. }
  379. ent*=scale;
  380. for(k=0;k<possible_partitions-1;k++)
  381. if(max<=info->classmetric1[k] &&
  382. (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
  383. break;
  384. partword[j][i]=k;
  385. }
  386. }
  387. #ifdef TRAIN_RESAUX
  388. {
  389. FILE *of;
  390. char buffer[80];
  391. for(i=0;i<ch;i++){
  392. sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  393. of=fopen(buffer,"a");
  394. for(j=0;j<partvals;j++)
  395. fprintf(of,"%ld, ",partword[i][j]);
  396. fprintf(of,"\n");
  397. fclose(of);
  398. }
  399. }
  400. #endif
  401. look->frames++;
  402. return(partword);
  403. }
  404. /* designed for stereo or other modes where the partition size is an
  405. integer multiple of the number of channels encoded in the current
  406. submap */
  407. static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
  408. int ch){
  409. long i,j,k,l;
  410. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  411. vorbis_info_residue0 *info=look->info;
  412. /* move all this setup out later */
  413. int samples_per_partition=info->grouping;
  414. int possible_partitions=info->partitions;
  415. int n=info->end-info->begin;
  416. int partvals=n/samples_per_partition;
  417. long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
  418. #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
  419. FILE *of;
  420. char buffer[80];
  421. #endif
  422. partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
  423. memset(partword[0],0,partvals*sizeof(*partword[0]));
  424. for(i=0,l=info->begin/ch;i<partvals;i++){
  425. int magmax=0;
  426. int angmax=0;
  427. for(j=0;j<samples_per_partition;j+=ch){
  428. if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
  429. for(k=1;k<ch;k++)
  430. if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
  431. l++;
  432. }
  433. for(j=0;j<possible_partitions-1;j++)
  434. if(magmax<=info->classmetric1[j] &&
  435. angmax<=info->classmetric2[j])
  436. break;
  437. partword[0][i]=j;
  438. }
  439. #ifdef TRAIN_RESAUX
  440. sprintf(buffer,"resaux_%d.vqd",look->train_seq);
  441. of=fopen(buffer,"a");
  442. for(i=0;i<partvals;i++)
  443. fprintf(of,"%ld, ",partword[0][i]);
  444. fprintf(of,"\n");
  445. fclose(of);
  446. #endif
  447. look->frames++;
  448. return(partword);
  449. }
  450. static int _01forward(oggpack_buffer *opb,
  451. vorbis_block *vb,vorbis_look_residue *vl,
  452. int **in,int ch,
  453. long **partword,
  454. int (*encode)(oggpack_buffer *,int *,int,
  455. codebook *,long *),
  456. int submap){
  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. long *accumulator=NULL;
  514. #ifdef TRAIN_RES
  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. #endif
  527. ret=encode(opb,in[j]+offset,samples_per_partition,
  528. statebook,accumulator);
  529. look->postbits+=ret;
  530. resbits[partword[j][i]]+=ret;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. /*{
  538. long total=0;
  539. long totalbits=0;
  540. fprintf(stderr,"%d :: ",vb->mode);
  541. for(k=0;k<possible_partitions;k++){
  542. fprintf(stderr,"%ld/%1.2g, ",resvals[k],(float)resbits[k]/resvals[k]);
  543. total+=resvals[k];
  544. totalbits+=resbits[k];
  545. }
  546. fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total);
  547. }*/
  548. return(0);
  549. }
  550. /* a truncated packet here just means 'stop working'; it's not an error */
  551. static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
  552. float **in,int ch,
  553. long (*decodepart)(codebook *, float *,
  554. oggpack_buffer *,int)){
  555. long i,j,k,l,s;
  556. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  557. vorbis_info_residue0 *info=look->info;
  558. /* move all this setup out later */
  559. int samples_per_partition=info->grouping;
  560. int partitions_per_word=look->phrasebook->dim;
  561. int max=vb->pcmend>>1;
  562. int end=(info->end<max?info->end:max);
  563. int n=end-info->begin;
  564. if(n>0){
  565. int partvals=n/samples_per_partition;
  566. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  567. int ***partword=alloca(ch*sizeof(*partword));
  568. for(j=0;j<ch;j++)
  569. partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
  570. for(s=0;s<look->stages;s++){
  571. /* each loop decodes on partition codeword containing
  572. partitions_per_word partitions */
  573. for(i=0,l=0;i<partvals;l++){
  574. if(s==0){
  575. /* fetch the partition word for each channel */
  576. for(j=0;j<ch;j++){
  577. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  578. if(temp==-1 || temp>=info->partvals)goto eopbreak;
  579. partword[j][l]=look->decodemap[temp];
  580. if(partword[j][l]==NULL)goto errout;
  581. }
  582. }
  583. /* now we decode residual values for the partitions */
  584. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  585. for(j=0;j<ch;j++){
  586. long offset=info->begin+i*samples_per_partition;
  587. if(info->secondstages[partword[j][l][k]]&(1<<s)){
  588. codebook *stagebook=look->partbooks[partword[j][l][k]][s];
  589. if(stagebook){
  590. if(decodepart(stagebook,in[j]+offset,&vb->opb,
  591. samples_per_partition)==-1)goto eopbreak;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. errout:
  599. eopbreak:
  600. return(0);
  601. }
  602. int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  603. float **in,int *nonzero,int ch){
  604. int i,used=0;
  605. for(i=0;i<ch;i++)
  606. if(nonzero[i])
  607. in[used++]=in[i];
  608. if(used)
  609. return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
  610. else
  611. return(0);
  612. }
  613. int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
  614. int **in,int *nonzero,int ch, long **partword, int submap){
  615. int i,used=0;
  616. for(i=0;i<ch;i++)
  617. if(nonzero[i])
  618. in[used++]=in[i];
  619. if(used){
  620. return _01forward(opb,vb,vl,in,used,partword,_encodepart,submap);
  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. return _01forward(opb,vb,vl,&work,1,partword,_encodepart,submap);
  675. }else{
  676. return(0);
  677. }
  678. }
  679. /* duplicate code here as speed is somewhat more important */
  680. int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  681. float **in,int *nonzero,int ch){
  682. long i,k,l,s;
  683. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  684. vorbis_info_residue0 *info=look->info;
  685. /* move all this setup out later */
  686. int samples_per_partition=info->grouping;
  687. int partitions_per_word=look->phrasebook->dim;
  688. int max=(vb->pcmend*ch)>>1;
  689. int end=(info->end<max?info->end:max);
  690. int n=end-info->begin;
  691. if(n>0){
  692. int partvals=n/samples_per_partition;
  693. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  694. int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
  695. for(i=0;i<ch;i++)if(nonzero[i])break;
  696. if(i==ch)return(0); /* no nonzero vectors */
  697. for(s=0;s<look->stages;s++){
  698. for(i=0,l=0;i<partvals;l++){
  699. if(s==0){
  700. /* fetch the partition word */
  701. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  702. if(temp==-1 || temp>=info->partvals)goto eopbreak;
  703. partword[l]=look->decodemap[temp];
  704. if(partword[l]==NULL)goto errout;
  705. }
  706. /* now we decode residual values for the partitions */
  707. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  708. if(info->secondstages[partword[l][k]]&(1<<s)){
  709. codebook *stagebook=look->partbooks[partword[l][k]][s];
  710. if(stagebook){
  711. if(vorbis_book_decodevv_add(stagebook,in,
  712. i*samples_per_partition+info->begin,ch,
  713. &vb->opb,samples_per_partition)==-1)
  714. goto eopbreak;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. errout:
  721. eopbreak:
  722. return(0);
  723. }
  724. const vorbis_func_residue residue0_exportbundle={
  725. NULL,
  726. &res0_unpack,
  727. &res0_look,
  728. &res0_free_info,
  729. &res0_free_look,
  730. NULL,
  731. NULL,
  732. &res0_inverse
  733. };
  734. const vorbis_func_residue residue1_exportbundle={
  735. &res0_pack,
  736. &res0_unpack,
  737. &res0_look,
  738. &res0_free_info,
  739. &res0_free_look,
  740. &res1_class,
  741. &res1_forward,
  742. &res1_inverse
  743. };
  744. const vorbis_func_residue residue2_exportbundle={
  745. &res0_pack,
  746. &res0_unpack,
  747. &res0_look,
  748. &res0_free_info,
  749. &res0_free_look,
  750. &res2_class,
  751. &res2_forward,
  752. &res2_inverse
  753. };