res0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH 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.17.2.3.2.1 2000/09/03 08:34:52 jack 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 "registry.h"
  27. #include "bookinternal.h"
  28. #include "sharedbook.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. void res0_free_info(vorbis_info_residue *i){
  41. if(i){
  42. memset(i,0,sizeof(vorbis_info_residue0));
  43. free(i);
  44. }
  45. }
  46. void res0_free_look(vorbis_look_residue *i){
  47. int j;
  48. if(i){
  49. vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
  50. for(j=0;j<look->parts;j++)
  51. if(look->partbooks[j])free(look->partbooks[j]);
  52. free(look->partbooks);
  53. for(j=0;j<look->partvals;j++)
  54. free(look->decodemap[j]);
  55. free(look->decodemap);
  56. memset(i,0,sizeof(vorbis_look_residue0));
  57. free(i);
  58. }
  59. }
  60. void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
  61. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  62. int j,acc=0;
  63. oggpack_write(opb,info->begin,24);
  64. oggpack_write(opb,info->end,24);
  65. oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
  66. code with a partitioned book */
  67. oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  68. oggpack_write(opb,info->groupbook,8); /* group huffman book */
  69. for(j=0;j<info->partitions;j++){
  70. oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
  71. acc+=info->secondstages[j];
  72. }
  73. for(j=0;j<acc;j++)
  74. oggpack_write(opb,info->booklist[j],8);
  75. }
  76. /* vorbis_info is for range checking */
  77. vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  78. int j,acc=0;
  79. vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
  80. info->begin=oggpack_read(opb,24);
  81. info->end=oggpack_read(opb,24);
  82. info->grouping=oggpack_read(opb,24)+1;
  83. info->partitions=oggpack_read(opb,6)+1;
  84. info->groupbook=oggpack_read(opb,8);
  85. for(j=0;j<info->partitions;j++){
  86. int cascade=info->secondstages[j]=oggpack_read(opb,4);
  87. if(cascade>1)goto errout; /* temporary! when cascading gets
  88. reworked and actually used, we don't
  89. want old code to DTWT */
  90. acc+=cascade;
  91. }
  92. for(j=0;j<acc;j++)
  93. info->booklist[j]=oggpack_read(opb,8);
  94. if(info->groupbook>=vi->books)goto errout;
  95. for(j=0;j<acc;j++)
  96. if(info->booklist[j]>=vi->books)goto errout;
  97. return(info);
  98. errout:
  99. res0_free_info(info);
  100. return(NULL);
  101. }
  102. vorbis_look_residue *res0_look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
  103. vorbis_info_residue *vr){
  104. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  105. vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
  106. int j,k,acc=0;
  107. int dim;
  108. look->info=info;
  109. look->map=vm->mapping;
  110. look->parts=info->partitions;
  111. look->phrasebook=vd->fullbooks+info->groupbook;
  112. dim=look->phrasebook->dim;
  113. look->partbooks=calloc(look->parts,sizeof(codebook **));
  114. for(j=0;j<look->parts;j++){
  115. int stages=info->secondstages[j];
  116. if(stages){
  117. look->partbooks[j]=malloc(stages*sizeof(codebook *));
  118. for(k=0;k<stages;k++)
  119. look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
  120. }
  121. }
  122. look->partvals=rint(pow(look->parts,dim));
  123. look->decodemap=malloc(look->partvals*sizeof(int *));
  124. for(j=0;j<look->partvals;j++){
  125. long val=j;
  126. long mult=look->partvals/look->parts;
  127. look->decodemap[j]=malloc(dim*sizeof(int));
  128. for(k=0;k<dim;k++){
  129. long deco=val/mult;
  130. val-=deco*mult;
  131. mult/=look->parts;
  132. look->decodemap[j][k]=deco;
  133. }
  134. }
  135. return(look);
  136. }
  137. /* does not guard against invalid settings; eg, a subn of 16 and a
  138. subgroup request of 32. Max subn of 128 */
  139. static int _testhack(float *vec,int n,vorbis_look_residue0 *look,
  140. int auxparts,int auxpartnum){
  141. vorbis_info_residue0 *info=look->info;
  142. int i,j=0;
  143. float max,localmax=0.;
  144. float temp[128];
  145. float entropy[8];
  146. /* setup */
  147. for(i=0;i<n;i++)temp[i]=fabs(vec[i]);
  148. /* handle case subgrp==1 outside */
  149. for(i=0;i<n;i++)
  150. if(temp[i]>localmax)localmax=temp[i];
  151. max=localmax;
  152. for(i=0;i<n;i++)temp[i]=rint(temp[i]);
  153. while(1){
  154. entropy[j]=localmax;
  155. n>>=1;
  156. j++;
  157. if(n<=0)break;
  158. for(i=0;i<n;i++){
  159. temp[i]+=temp[i+n];
  160. }
  161. localmax=0.;
  162. for(i=0;i<n;i++)
  163. if(temp[i]>localmax)localmax=temp[i];
  164. }
  165. for(i=0;i<auxparts-1;i++)
  166. if(auxpartnum<info->blimit[i] &&
  167. entropy[info->subgrp[i]]<=info->entmax[i] &&
  168. max<=info->ampmax[i])
  169. break;
  170. return(i);
  171. }
  172. static int _encodepart(oggpack_buffer *opb,float *vec, int n,
  173. int stages, codebook **books,int mode,int part){
  174. int i,j,bits=0;
  175. for(j=0;j<stages;j++){
  176. int dim=books[j]->dim;
  177. int step=n/dim;
  178. for(i=0;i<step;i++){
  179. int entry=vorbis_book_besterror(books[j],vec+i,step,0);
  180. #ifdef TRAIN_RESENT
  181. {
  182. char buf[80];
  183. FILE *f;
  184. sprintf(buf,"res0_%da%d_%d.vqd",mode,j,part);
  185. f=fopen(buf,"a");
  186. fprintf(f,"%d\n",entry);
  187. fclose(f);
  188. }
  189. #endif
  190. bits+=vorbis_book_encode(books[j],entry,opb);
  191. }
  192. }
  193. return(bits);
  194. }
  195. static int _decodepart(oggpack_buffer *opb,float *work,float *vec, int n,
  196. int stages, codebook **books){
  197. int i;
  198. memset(work,0,sizeof(float)*n);
  199. for(i=0;i<stages;i++){
  200. int dim=books[i]->dim;
  201. int step=n/dim;
  202. if(s_vorbis_book_decodevs(books[i],work,opb,step,0)==-1)
  203. return(-1);
  204. }
  205. for(i=0;i<n;i++)
  206. vec[i]*=work[i];
  207. return(0);
  208. }
  209. int res0_forward(vorbis_block *vb,vorbis_look_residue *vl,
  210. float **in,int ch){
  211. long i,j,k,l;
  212. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  213. vorbis_info_residue0 *info=look->info;
  214. /* move all this setup out later */
  215. int samples_per_partition=info->grouping;
  216. int possible_partitions=info->partitions;
  217. int partitions_per_word=look->phrasebook->dim;
  218. int n=info->end-info->begin;
  219. long phrasebits=0,resbitsT=0;
  220. long *resbits=alloca(sizeof(long)*possible_partitions);
  221. long *resvals=alloca(sizeof(long)*possible_partitions);
  222. int partvals=n/samples_per_partition;
  223. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  224. long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
  225. partvals=partwords*partitions_per_word;
  226. /* we find the patition type for each partition of each
  227. channel. We'll go back and do the interleaved encoding in a
  228. bit. For now, clarity */
  229. memset(resbits,0,sizeof(long)*possible_partitions);
  230. memset(resvals,0,sizeof(long)*possible_partitions);
  231. for(i=0;i<ch;i++){
  232. partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
  233. memset(partword[i],0,n/samples_per_partition*sizeof(long));
  234. }
  235. for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++){
  236. for(j=0;j<ch;j++)
  237. /* do the partition decision based on the number of 'bits'
  238. needed to encode the block */
  239. partword[j][l]=
  240. _testhack(in[j]+i,samples_per_partition,look,possible_partitions,l);
  241. }
  242. /* we code the partition words for each channel, then the residual
  243. words for a partition per channel until we've written all the
  244. residual words for that partition word. Then write the next
  245. partition channel words... */
  246. for(i=info->begin,l=0;i<info->end;){
  247. /* first we encode a partition codeword for each channel */
  248. for(j=0;j<ch;j++){
  249. long val=partword[j][l];
  250. for(k=1;k<partitions_per_word;k++)
  251. val= val*possible_partitions+partword[j][l+k];
  252. phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
  253. }
  254. /* now we encode interleaved residual values for the partitions */
  255. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  256. for(j=0;j<ch;j++){
  257. resbits[partword[j][l]]+=
  258. _encodepart(&vb->opb,in[j]+i,samples_per_partition,
  259. info->secondstages[partword[j][l]],
  260. look->partbooks[partword[j][l]],look->map,partword[j][l]);
  261. resvals[partword[j][l]]+=samples_per_partition;
  262. }
  263. }
  264. for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
  265. /*fprintf(stderr,
  266. "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
  267. ch*(info->end-info->begin),phrasebits,resbitsT);
  268. for(i=0;i<possible_partitions;i++)
  269. fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
  270. fprintf(stderr,"\n");*/
  271. return(0);
  272. }
  273. /* a truncated packet here just means 'stop working'; it's not an error */
  274. int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,float **in,int ch){
  275. long i,j,k,l,transend=vb->pcmend/2;
  276. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  277. vorbis_info_residue0 *info=look->info;
  278. /* move all this setup out later */
  279. int samples_per_partition=info->grouping;
  280. int partitions_per_word=look->phrasebook->dim;
  281. int n=info->end-info->begin;
  282. int partvals=n/samples_per_partition;
  283. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  284. int **partword=alloca(ch*sizeof(long *));
  285. float *work=alloca(sizeof(float)*samples_per_partition);
  286. partvals=partwords*partitions_per_word;
  287. /* make sure we're zeroed up to the start */
  288. for(j=0;j<ch;j++)
  289. memset(in[j],0,sizeof(float)*info->begin);
  290. for(i=info->begin,l=0;i<info->end;){
  291. /* fetch the partition word for each channel */
  292. for(j=0;j<ch;j++){
  293. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  294. if(temp==-1)goto eopbreak;
  295. partword[j]=look->decodemap[temp];
  296. if(partword[j]==NULL)goto errout;
  297. }
  298. /* now we decode interleaved residual values for the partitions */
  299. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  300. for(j=0;j<ch;j++){
  301. int part=partword[j][k];
  302. if(_decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
  303. info->secondstages[part],
  304. look->partbooks[part])==-1)goto eopbreak;
  305. }
  306. }
  307. eopbreak:
  308. if(i<transend){
  309. for(j=0;j<ch;j++)
  310. memset(in[j]+i,0,sizeof(float)*(transend-i));
  311. }
  312. return(0);
  313. errout:
  314. for(j=0;j<ch;j++)
  315. memset(in[j],0,sizeof(float)*transend);
  316. return(0);
  317. }
  318. vorbis_func_residue residue0_exportbundle={
  319. &res0_pack,
  320. &res0_unpack,
  321. &res0_look,
  322. &res0_free_info,
  323. &res0_free_look,
  324. &res0_forward,
  325. &res0_inverse
  326. };