res0.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. function: residue backend 0 implementation
  12. last mod: $Id: res0.c,v 1.26.2.1 2001/03/28 03:11:06 segher Exp $
  13. ********************************************************************/
  14. /* Slow, slow, slow, simpleminded and did I mention it was slow? The
  15. encode/decode loops are coded for clarity and performance is not
  16. yet even a nagging little idea lurking in the shadows. Oh and BTW,
  17. it's slow. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <stdio.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. typedef struct {
  30. vorbis_info_residue0 *info;
  31. int map;
  32. int parts;
  33. codebook *phrasebook;
  34. codebook ***partbooks;
  35. int partvals;
  36. int **decodemap;
  37. } vorbis_look_residue0;
  38. vorbis_info_residue *res0_copy_info(vorbis_info_residue *vr){
  39. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  40. vorbis_info_residue0 *ret=_ogg_malloc(sizeof(vorbis_info_residue0));
  41. memcpy(ret,info,sizeof(vorbis_info_residue0));
  42. return(ret);
  43. }
  44. void res0_free_info(vorbis_info_residue *i){
  45. if(i){
  46. memset(i,0,sizeof(vorbis_info_residue0));
  47. _ogg_free(i);
  48. }
  49. }
  50. void res0_free_look(vorbis_look_residue *i){
  51. int j;
  52. if(i){
  53. vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
  54. for(j=0;j<look->parts;j++)
  55. if(look->partbooks[j])_ogg_free(look->partbooks[j]);
  56. _ogg_free(look->partbooks);
  57. for(j=0;j<look->partvals;j++)
  58. _ogg_free(look->decodemap[j]);
  59. _ogg_free(look->decodemap);
  60. memset(i,0,sizeof(vorbis_look_residue0));
  61. _ogg_free(i);
  62. }
  63. }
  64. void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
  65. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  66. int j,acc=0;
  67. oggpack_write(opb,info->begin,24);
  68. oggpack_write(opb,info->end,24);
  69. oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
  70. code with a partitioned book */
  71. oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  72. oggpack_write(opb,info->groupbook,8); /* group huffman book */
  73. for(j=0;j<info->partitions;j++){
  74. oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
  75. acc+=info->secondstages[j];
  76. }
  77. for(j=0;j<acc;j++)
  78. oggpack_write(opb,info->booklist[j],8);
  79. }
  80. /* vorbis_info is for range checking */
  81. vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  82. int j,acc=0;
  83. vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(vorbis_info_residue0));
  84. codec_setup_info *ci=vi->codec_setup;
  85. info->begin=oggpack_read(opb,24);
  86. info->end=oggpack_read(opb,24);
  87. info->grouping=oggpack_read(opb,24)+1;
  88. info->partitions=oggpack_read(opb,6)+1;
  89. info->groupbook=oggpack_read(opb,8);
  90. for(j=0;j<info->partitions;j++){
  91. int cascade=info->secondstages[j]=oggpack_read(opb,4);
  92. if(cascade>1)goto errout; /* temporary! when cascading gets
  93. reworked and actually used, we don't
  94. want old code to DTWT */
  95. acc+=cascade;
  96. }
  97. for(j=0;j<acc;j++){
  98. info->booklist[j]=oggpack_read(opb,8);
  99. if(info->booklist[j]==255)info->booklist[j]=-1;
  100. }
  101. if(info->groupbook>=ci->books)goto errout;
  102. for(j=0;j<acc;j++)
  103. if(info->booklist[j]>=ci->books)goto errout;
  104. return(info);
  105. errout:
  106. res0_free_info(info);
  107. return(NULL);
  108. }
  109. vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
  110. vorbis_info_residue *vr){
  111. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  112. vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(vorbis_look_residue0));
  113. backend_lookup_state *be=vd->backend_state;
  114. int j,k,acc=0;
  115. int dim;
  116. look->info=info;
  117. look->map=vm->mapping;
  118. look->parts=info->partitions;
  119. look->phrasebook=be->fullbooks+info->groupbook;
  120. dim=look->phrasebook->dim;
  121. look->partbooks=_ogg_calloc(look->parts,sizeof(codebook **));
  122. for(j=0;j<look->parts;j++){
  123. int stages=info->secondstages[j];
  124. if(stages){
  125. look->partbooks[j]=_ogg_malloc(stages*sizeof(codebook *));
  126. look->partbooks[j][0]=be->fullbooks+info->booklist[acc++];
  127. }
  128. }
  129. look->partvals=rint(pow(look->parts,dim));
  130. look->decodemap=_ogg_malloc(look->partvals*sizeof(int *));
  131. for(j=0;j<look->partvals;j++){
  132. long val=j;
  133. long mult=look->partvals/look->parts;
  134. look->decodemap[j]=_ogg_malloc(dim*sizeof(int));
  135. for(k=0;k<dim;k++){
  136. long deco=val/mult;
  137. val-=deco*mult;
  138. mult/=look->parts;
  139. look->decodemap[j][k]=deco;
  140. }
  141. }
  142. return(look);
  143. }
  144. /* does not guard against invalid settings; eg, a subn of 16 and a
  145. subgroup request of 32. Max subn of 128 */
  146. static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
  147. int auxparts,int auxpartnum){
  148. vorbis_info_residue0 *info=look->info;
  149. int i,j=0;
  150. float max,localmax=0.f;
  151. float temp[128];
  152. float entropy[8];
  153. /* setup */
  154. for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
  155. /* handle case subgrp==1 outside */
  156. for(i=0;i<n;i++)
  157. if(temp[i]>localmax)localmax=temp[i];
  158. max=localmax;
  159. for(i=0;i<n;i++)temp[i]=rint(temp[i]);
  160. while(1){
  161. entropy[j]=localmax;
  162. n>>=1;
  163. j++;
  164. if(n<=0)break;
  165. for(i=0;i<n;i++){
  166. temp[i]+=temp[i+n];
  167. }
  168. localmax=0.f;
  169. for(i=0;i<n;i++)
  170. if(temp[i]>localmax)localmax=temp[i];
  171. }
  172. for(i=0;i<auxparts-1;i++)
  173. if(auxpartnum<info->blimit[i] &&
  174. entropy[info->subgrp[i]]<=info->entmax[i] &&
  175. max<=info->ampmax[i])
  176. break;
  177. return(i);
  178. }
  179. static int _encodepart(oggpack_buffer *opb,float *vec, int n,
  180. int stages, codebook **books,int mode,int part){
  181. int i,j=0,bits=0;
  182. if(stages){
  183. int dim=books[j]->dim;
  184. int step=n/dim;
  185. for(i=0;i<step;i++){
  186. int entry=vorbis_book_besterror(books[j],vec+i,step,0);
  187. #ifdef TRAIN_RESENT
  188. {
  189. char buf[80];
  190. FILE *f;
  191. sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
  192. f=fopen(buf,"a");
  193. fprintf(f,"%d\n",entry);
  194. fclose(f);
  195. }
  196. #endif
  197. bits+=vorbis_book_encode(books[j],entry,opb);
  198. }
  199. }
  200. return(bits);
  201. }
  202. static int fast_decodepart(oggpack_buffer *opb,float *vec,int n,codebook *book){
  203. int dim=book->dim;
  204. int step=n/dim;
  205. long *entry=alloca(sizeof(long)*step);
  206. float **t=alloca(sizeof(float *)*step);
  207. int i,j,o;
  208. for(i=0;i<step;i++){
  209. entry[i]=vorbis_book_decode(book,opb);
  210. if(entry[i]==-1)return(-1);
  211. t[i]=book->valuelist+entry[i]*dim;
  212. }
  213. for(i=0,o=0;i<dim;i++){
  214. for (j=0;j<step-3;j+=4,o+=4){
  215. vec[o]*=t[j][i];
  216. vec[o+1]*=t[j+1][i];
  217. vec[o+2]*=t[j+2][i];
  218. vec[o+3]*=t[j+3][i];
  219. }
  220. for (;j<step;j++,o++)
  221. vec[o]*=t[j][i];
  222. }
  223. return(0);
  224. }
  225. static int _decodepart(oggpack_buffer *opb,float *work,float *vec, int n,
  226. int stages, codebook **books){
  227. int i;
  228. if(stages==0){
  229. memset(vec,0,sizeof(float)*n);
  230. return 0;
  231. }
  232. if(stages==1)
  233. return fast_decodepart(opb,vec,n,books[0]);
  234. memset(work,0,sizeof(float)*n);
  235. if(stages){
  236. int dim=books[0]->dim;
  237. int step=n/dim;
  238. if(s_vorbis_book_decodevs(books[0],work,opb,step,0)==-1)
  239. return(-1);
  240. }
  241. for(i=0;i<n;i++)
  242. vec[i]*=work[i];
  243. return(0);
  244. }
  245. int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
  246. float **in,int ch){
  247. long i,j,k,l;
  248. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  249. vorbis_info_residue0 *info=look->info;
  250. /* move all this setup out later */
  251. int samples_per_partition=info->grouping;
  252. int possible_partitions=info->partitions;
  253. int partitions_per_word=look->phrasebook->dim;
  254. int n=info->end-info->begin;
  255. long phrasebits=0,resbitsT=0;
  256. long *resbits=alloca(sizeof(long)*possible_partitions);
  257. long *resvals=alloca(sizeof(long)*possible_partitions);
  258. int partvals=n/samples_per_partition;
  259. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  260. long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
  261. partvals=partwords*partitions_per_word;
  262. /* we find the patition type for each partition of each
  263. channel. We'll go back and do the interleaved encoding in a
  264. bit. For now, clarity */
  265. memset(resbits,0,sizeof(long)*possible_partitions);
  266. memset(resvals,0,sizeof(long)*possible_partitions);
  267. for(i=0;i<ch;i++){
  268. partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
  269. memset(partword[i],0,n/samples_per_partition*sizeof(long));
  270. }
  271. for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
  272. for(j=0;j<ch;j++)
  273. /* do the partition decision based on the number of 'bits'
  274. needed to encode the block */
  275. partword[j][l]=
  276. _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
  277. }
  278. /* we code the partition words for each channel, then the residual
  279. words for a partition per channel until we've written all the
  280. residual words for that partition word. Then write the next
  281. partition channel words... */
  282. for(i=info->begin,l=0;i<info->end;){
  283. /* first we encode a partition codeword for each channel */
  284. for(j=0;j<ch;j++){
  285. long val=partword[j][l];
  286. for(k=1;k<partitions_per_word;k++)
  287. val= val*possible_partitions+partword[j][l+k];
  288. phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
  289. }
  290. /* now we encode interleaved residual values for the partitions */
  291. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  292. for(j=0;j<ch;j++){
  293. /*resbits[partword[j][l]]+=*/
  294. resbitsT+=_encodepart(&vb->opb,in[j]+i,samples_per_partition,
  295. info->secondstages[partword[j][l]],
  296. look->partbooks[partword[j][l]],look->map,partword[j][l]);
  297. resvals[partword[j][l]]+=samples_per_partition;
  298. }
  299. }
  300. for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
  301. /*fprintf(stderr,
  302. "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
  303. ch*(info->end-info->begin),phrasebits,resbitsT);
  304. for(i=0;i<possible_partitions;i++)
  305. fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
  306. fprintf(stderr,"\n");*/
  307. return(0);
  308. }
  309. /* a truncated packet here just means 'stop working'; it's not an error */
  310. int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
  311. long i,j,k,l,transend=vb->pcmend/2;
  312. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  313. vorbis_info_residue0 *info=look->info;
  314. /* move all this setup out later */
  315. int samples_per_partition=info->grouping;
  316. int partitions_per_word=look->phrasebook->dim;
  317. int n=info->end-info->begin;
  318. int partvals=n/samples_per_partition;
  319. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  320. int **partword=alloca(ch*sizeof(long *));
  321. float *work=alloca(sizeof(float)*samples_per_partition);
  322. partvals=partwords*partitions_per_word;
  323. /* make sure we're zeroed up to the start */
  324. for(j=0;j<ch;j++)
  325. memset(in[j],0,sizeof(float)*info->begin);
  326. for(i=info->begin,l=0;i<info->end;){
  327. /* fetch the partition word for each channel */
  328. for(j=0;j<ch;j++){
  329. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  330. if(temp==-1)goto eopbreak;
  331. partword[j]=look->decodemap[temp];
  332. if(partword[j]==NULL)goto errout;
  333. }
  334. /* now we decode interleaved residual values for the partitions */
  335. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  336. for(j=0;j<ch;j++){
  337. int part=partword[j][k];
  338. if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
  339. info->secondstages[part],
  340. look->partbooks[part])==-1)goto eopbreak;
  341. }
  342. }
  343. eopbreak:
  344. if(i<transend){
  345. for(j=0;j<ch;j++)
  346. memset(in[j]+i,0,sizeof(float)*(transend-i));
  347. }
  348. return(0);
  349. errout:
  350. for(j=0;j<ch;j++)
  351. memset(in[j],0,sizeof(float)*transend);
  352. return(0);
  353. }
  354. vorbis_func_residue residue0_exportbundle={
  355. &res0_pack,
  356. &res0_unpack,
  357. &res0_look,
  358. &res0_copy_info,
  359. &res0_free_info,
  360. &res0_free_look,
  361. &res0_forward,
  362. &res0_inverse
  363. };