res0.c 12 KB

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