res0.c 10 KB

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