res0.c 24 KB

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