res0.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.8.4.6 2000/05/08 08:25:43 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 "vorbis/codec.h"
  25. #include "bitwise.h"
  26. #include "registry.h"
  27. #include "bookinternal.h"
  28. #include "sharedbook.h"
  29. #include "misc.h"
  30. typedef struct {
  31. vorbis_info_residue0 *info;
  32. int parts;
  33. codebook *phrasebook;
  34. codebook ***partbooks;
  35. int partvals;
  36. int **decodemap;
  37. } vorbis_look_residue0;
  38. void free_info(vorbis_info_residue *i){
  39. if(i){
  40. memset(i,0,sizeof(vorbis_info_residue0));
  41. free(i);
  42. }
  43. }
  44. void free_look(vorbis_look_residue *i){
  45. int j;
  46. if(i){
  47. vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
  48. for(j=0;j<look->parts;j++)
  49. if(look->partbooks[j])free(look->partbooks[j]);
  50. free(look->partbooks);
  51. for(j=0;j<look->partvals;j++)
  52. free(look->decodemap[j]);
  53. free(look->decodemap);
  54. memset(i,0,sizeof(vorbis_look_residue0));
  55. free(i);
  56. }
  57. }
  58. void pack(vorbis_info_residue *vr,oggpack_buffer *opb){
  59. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  60. int j,acc=0;
  61. _oggpack_write(opb,info->begin,24);
  62. _oggpack_write(opb,info->end,24);
  63. _oggpack_write(opb,info->grouping-1,24); /* residue vectors to group and
  64. code with a partitioned book */
  65. _oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
  66. _oggpack_write(opb,info->groupbook,8); /* group huffman book */
  67. for(j=0;j<info->partitions;j++){
  68. _oggpack_write(opb,info->secondstages[j],4); /* zero *is* a valid choice */
  69. acc+=info->secondstages[j];
  70. }
  71. for(j=0;j<acc;j++)
  72. _oggpack_write(opb,info->booklist[j],8);
  73. }
  74. /* vorbis_info is for range checking */
  75. vorbis_info_residue *unpack(vorbis_info *vi,oggpack_buffer *opb){
  76. int j,acc=0;
  77. vorbis_info_residue0 *info=calloc(1,sizeof(vorbis_info_residue0));
  78. info->begin=_oggpack_read(opb,24);
  79. info->end=_oggpack_read(opb,24);
  80. info->grouping=_oggpack_read(opb,24)+1;
  81. info->partitions=_oggpack_read(opb,6)+1;
  82. info->groupbook=_oggpack_read(opb,8);
  83. for(j=0;j<info->partitions;j++){
  84. acc+=info->secondstages[j]=_oggpack_read(opb,4);
  85. }
  86. for(j=0;j<acc;j++)
  87. info->booklist[j]=_oggpack_read(opb,8);
  88. if(info->groupbook>=vi->books)goto errout;
  89. for(j=0;j<acc;j++)
  90. if(info->booklist[j]>=vi->books)goto errout;
  91. return(info);
  92. errout:
  93. free_info(info);
  94. return(NULL);
  95. }
  96. vorbis_look_residue *look (vorbis_dsp_state *vd,vorbis_info_mode *vm,
  97. vorbis_info_residue *vr){
  98. vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  99. vorbis_look_residue0 *look=calloc(1,sizeof(vorbis_look_residue0));
  100. int j,k,acc=0;
  101. int dim;
  102. look->info=info;
  103. look->parts=info->partitions;
  104. look->phrasebook=vd->fullbooks+info->groupbook;
  105. dim=look->phrasebook->dim;
  106. look->partbooks=calloc(look->parts,sizeof(codebook **));
  107. for(j=0;j<look->parts;j++){
  108. int stages=info->secondstages[j];
  109. if(stages){
  110. look->partbooks[j]=malloc(stages*sizeof(codebook *));
  111. for(k=0;k<stages;k++)
  112. look->partbooks[j][k]=vd->fullbooks+info->booklist[acc++];
  113. }
  114. }
  115. look->partvals=rint(pow(look->parts,dim));
  116. look->decodemap=malloc(look->partvals*sizeof(int *));
  117. for(j=0;j<look->partvals;j++){
  118. long val=j;
  119. long mult=look->partvals/look->parts;
  120. look->decodemap[j]=malloc(dim*sizeof(int));
  121. for(k=0;k<dim;k++){
  122. long deco=val/mult;
  123. val-=deco*mult;
  124. mult/=look->parts;
  125. look->decodemap[j][k]=deco;
  126. }
  127. }
  128. return(look);
  129. }
  130. /* classify by max quantized amplitude only */
  131. static int _testhack(double *vec,int n,vorbis_look_residue0 *look){
  132. vorbis_info_residue0 *info=look->info;
  133. double max=0.;
  134. int i;
  135. for(i=0;i<n;i++)
  136. if(fabs(vec[i])>max)max=fabs(vec[i]);
  137. for(i=0;i<look->parts-1;i++)
  138. if(max>=info->ampmax[i])
  139. break;
  140. return(i);
  141. }
  142. static int _encodepart(oggpack_buffer *opb,double *vec, int n,
  143. int stages, codebook **books){
  144. int i,j,bits=0;
  145. for(j=0;j<stages;j++){
  146. int dim=books[j]->dim;
  147. int step=n/dim;
  148. for(i=0;i<step;i++)
  149. bits+=vorbis_book_encodevs(books[j],vec+i,opb,step,0);
  150. }
  151. return(bits);
  152. }
  153. static int _decodepart(oggpack_buffer *opb,double *work,double *vec, int n,
  154. int stages, codebook **books){
  155. int i,j;
  156. memset(work,0,sizeof(double)*n);
  157. for(j=0;j<stages;j++){
  158. int dim=books[j]->dim;
  159. int step=n/dim;
  160. for(i=0;i<step;i++)
  161. vorbis_book_decodevs(books[j],work+i,opb,step,0);
  162. }
  163. for(i=0;i<n;i++)
  164. vec[i]*=work[i];
  165. return(0);
  166. }
  167. int forward(vorbis_block *vb,vorbis_look_residue *vl,
  168. double **in,int ch){
  169. long i,j,k,l;
  170. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  171. vorbis_info_residue0 *info=look->info;
  172. /* move all this setup out later */
  173. int samples_per_partition=info->grouping;
  174. int possible_partitions=info->partitions;
  175. int partitions_per_word=look->phrasebook->dim;
  176. int n=info->end-info->begin;
  177. long phrasebits=0,resbitsT=0;
  178. long *resbits=alloca(sizeof(long)*possible_partitions);
  179. long *resvals=alloca(sizeof(long)*possible_partitions);
  180. int partvals=n/samples_per_partition;
  181. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  182. long **partword=_vorbis_block_alloc(vb,ch*sizeof(long *));
  183. partvals=partwords*partitions_per_word;
  184. /* we find the patition type for each partition of each
  185. channel. We'll go back and do the interleaved encoding in a
  186. bit. For now, clarity */
  187. memset(resbits,0,sizeof(long)*possible_partitions);
  188. memset(resvals,0,sizeof(long)*possible_partitions);
  189. for(i=0;i<ch;i++){
  190. partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(long));
  191. memset(partword[i],0,n/samples_per_partition*sizeof(long));
  192. }
  193. for(i=info->begin,l=0;i<info->end;i+=samples_per_partition,l++)
  194. for(j=0;j<ch;j++)
  195. /* do the partition decision based on the number of 'bits'
  196. needed to encode the block */
  197. partword[j][l]=_testhack(in[j]+i,samples_per_partition,look);
  198. /* we code the partition words for each channel, then the residual
  199. words for a partition per channel until we've written all the
  200. residual words for that partition word. Then write the next
  201. parition channel words... */
  202. for(i=info->begin,l=0;i<info->end;){
  203. /* first we encode a partition codeword for each channel */
  204. for(j=0;j<ch;j++){
  205. long val=partword[j][l];
  206. for(k=1;k<partitions_per_word;k++)
  207. val= val*possible_partitions+partword[j][l+k];
  208. phrasebits+=vorbis_book_encode(look->phrasebook,val,&vb->opb);
  209. }
  210. /* now we encode interleaved residual values for the partitions */
  211. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  212. for(j=0;j<ch;j++){
  213. resbits[partword[j][l]]+=
  214. _encodepart(&vb->opb,in[j]+i,samples_per_partition,
  215. info->secondstages[partword[j][l]],
  216. look->partbooks[partword[j][l]]);
  217. resvals[partword[j][l]]+=samples_per_partition;
  218. }
  219. }
  220. for(i=0;i<possible_partitions;i++)resbitsT+=resbits[i];
  221. fprintf(stderr,
  222. "Encoded %ld res vectors in %ld phrasing and %ld res bits\n\t",
  223. ch*(info->end-info->begin),phrasebits,resbitsT);
  224. for(i=0;i<possible_partitions;i++)
  225. fprintf(stderr,"%ld(%ld):%ld ",i,resvals[i],resbits[i]);
  226. fprintf(stderr,"\n");
  227. return(0);
  228. }
  229. int inverse(vorbis_block *vb,vorbis_look_residue *vl,double **in,int ch){
  230. long i,j,k,l;
  231. vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  232. vorbis_info_residue0 *info=look->info;
  233. /* move all this setup out later */
  234. int samples_per_partition=info->grouping;
  235. int partitions_per_word=look->phrasebook->dim;
  236. int n=info->end-info->begin;
  237. int partvals=n/samples_per_partition;
  238. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  239. int **partword=alloca(ch*sizeof(long *));
  240. double *work=alloca(sizeof(double)*samples_per_partition);
  241. partvals=partwords*partitions_per_word;
  242. for(i=info->begin,l=0;i<info->end;){
  243. /* fetch the partition word for each channel */
  244. for(j=0;j<ch;j++){
  245. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  246. partword[j]=look->decodemap[temp];
  247. if(partword[j]==NULL)exit(1);
  248. }
  249. /* now we decode interleaved residual values for the partitions */
  250. for(k=0;k<partitions_per_word;k++,l++,i+=samples_per_partition)
  251. for(j=0;j<ch;j++){
  252. int part=partword[j][k];
  253. _decodepart(&vb->opb,work,in[j]+i,samples_per_partition,
  254. info->secondstages[part],
  255. look->partbooks[part]);
  256. }
  257. }
  258. return(0);
  259. }
  260. vorbis_func_residue residue0_exportbundle={
  261. &pack,
  262. &unpack,
  263. &look,
  264. &free_info,
  265. &free_look,
  266. &forward,
  267. &inverse
  268. };