res012.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: residue backend 0, 1 and 2 implementation
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include "ogg.h"
  19. #include "ivorbiscodec.h"
  20. #include "codec_internal.h"
  21. #include "codebook.h"
  22. #include "misc.h"
  23. #include "os.h"
  24. void res_clear_info(vorbis_info_residue *info){
  25. if(info){
  26. if(info->stagemasks)_ogg_free(info->stagemasks);
  27. if(info->stagebooks)_ogg_free(info->stagebooks);
  28. memset(info,0,sizeof(*info));
  29. }
  30. }
  31. /* vorbis_info is for range checking */
  32. int res_unpack(vorbis_info_residue *info,
  33. vorbis_info *vi,oggpack_buffer *opb){
  34. int j,k;
  35. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  36. memset(info,0,sizeof(*info));
  37. info->type=oggpack_read(opb,16);
  38. if(info->type>2 || info->type<0)goto errout;
  39. info->begin=oggpack_read(opb,24);
  40. info->end=oggpack_read(opb,24);
  41. info->grouping=oggpack_read(opb,24)+1;
  42. info->partitions=oggpack_read(opb,6)+1;
  43. info->groupbook=oggpack_read(opb,8);
  44. if(info->groupbook>=ci->books)goto errout;
  45. info->stagemasks=_ogg_malloc(info->partitions*sizeof(*info->stagemasks));
  46. info->stagebooks=_ogg_malloc(info->partitions*8*sizeof(*info->stagebooks));
  47. for(j=0;j<info->partitions;j++){
  48. int cascade=oggpack_read(opb,3);
  49. if(oggpack_read(opb,1))
  50. cascade|=(oggpack_read(opb,5)<<3);
  51. info->stagemasks[j]=cascade;
  52. }
  53. for(j=0;j<info->partitions;j++){
  54. for(k=0;k<8;k++){
  55. if((info->stagemasks[j]>>k)&1){
  56. unsigned char book=oggpack_read(opb,8);
  57. if(book>=ci->books)goto errout;
  58. info->stagebooks[j*8+k]=book;
  59. if(k+1>info->stages)info->stages=k+1;
  60. }else
  61. info->stagebooks[j*8+k]=0xff;
  62. }
  63. }
  64. if(oggpack_eop(opb))goto errout;
  65. return 0;
  66. errout:
  67. res_clear_info(info);
  68. return 1;
  69. }
  70. int res_inverse(vorbis_dsp_state *vd,vorbis_info_residue *info,
  71. ogg_int32_t **in,int *nonzero,int ch){
  72. int i,j,k,s,used=0;
  73. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  74. codebook *phrasebook=ci->book_param+info->groupbook;
  75. int samples_per_partition=info->grouping;
  76. int partitions_per_word=phrasebook->dim;
  77. int pcmend=ci->blocksizes[vd->W];
  78. if(info->type<2){
  79. int max=pcmend>>1;
  80. int end=(info->end<max?info->end:max);
  81. int n=end-info->begin;
  82. if(n>0){
  83. int partvals=n/samples_per_partition;
  84. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  85. for(i=0;i<ch;i++)
  86. if(nonzero[i])
  87. in[used++]=in[i];
  88. ch=used;
  89. if(used){
  90. char **partword=(char **)alloca(ch*sizeof(*partword));
  91. for(j=0;j<ch;j++)
  92. partword[j]=(char *)alloca(partwords*partitions_per_word*
  93. sizeof(*partword[j]));
  94. for(s=0;s<info->stages;s++){
  95. for(i=0;i<partvals;){
  96. if(s==0){
  97. /* fetch the partition word for each channel */
  98. partword[0][i+partitions_per_word-1]=1;
  99. for(k=partitions_per_word-2;k>=0;k--)
  100. partword[0][i+k]=partword[0][i+k+1]*info->partitions;
  101. for(j=1;j<ch;j++)
  102. for(k=partitions_per_word-1;k>=0;k--)
  103. partword[j][i+k]=partword[j-1][i+k];
  104. for(j=0;j<ch;j++){
  105. int temp=vorbis_book_decode(phrasebook,&vd->opb);
  106. if(temp==-1)goto eopbreak;
  107. /* this can be done quickly in assembly due to the quotient
  108. always being at most six bits */
  109. for(k=0;k<partitions_per_word;k++){
  110. ogg_uint32_t div=partword[j][i+k];
  111. partword[j][i+k]=temp/div;
  112. temp-=partword[j][i+k]*div;
  113. }
  114. }
  115. }
  116. /* now we decode residual values for the partitions */
  117. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  118. for(j=0;j<ch;j++){
  119. long offset=info->begin+i*samples_per_partition;
  120. if(info->stagemasks[(int)partword[j][i]]&(1<<s)){
  121. codebook *stagebook=ci->book_param+
  122. info->stagebooks[(partword[j][i]<<3)+s];
  123. if(info->type){
  124. if(vorbis_book_decodev_add(stagebook,in[j]+offset,&vd->opb,
  125. samples_per_partition,-8)==-1)
  126. goto eopbreak;
  127. }else{
  128. if(vorbis_book_decodevs_add(stagebook,in[j]+offset,&vd->opb,
  129. samples_per_partition,-8)==-1)
  130. goto eopbreak;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }else{
  139. int max=(pcmend*ch)>>1;
  140. int end=(info->end<max?info->end:max);
  141. int n=end-info->begin;
  142. if(n>0){
  143. int partvals=n/samples_per_partition;
  144. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  145. char *partword=
  146. (char *)alloca(partwords*partitions_per_word*sizeof(*partword));
  147. int beginoff=info->begin/ch;
  148. for(i=0;i<ch;i++)if(nonzero[i])break;
  149. if(i==ch)return(0); /* no nonzero vectors */
  150. samples_per_partition/=ch;
  151. for(s=0;s<info->stages;s++){
  152. for(i=0;i<partvals;){
  153. if(s==0){
  154. int temp;
  155. partword[i+partitions_per_word-1]=1;
  156. for(k=partitions_per_word-2;k>=0;k--)
  157. partword[i+k]=partword[i+k+1]*info->partitions;
  158. /* fetch the partition word */
  159. temp=vorbis_book_decode(phrasebook,&vd->opb);
  160. if(temp==-1)goto eopbreak;
  161. /* this can be done quickly in assembly due to the quotient
  162. always being at most six bits */
  163. for(k=0;k<partitions_per_word;k++){
  164. ogg_uint32_t div=partword[i+k];
  165. partword[i+k]=temp/div;
  166. temp-=partword[i+k]*div;
  167. }
  168. }
  169. /* now we decode residual values for the partitions */
  170. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  171. if(info->stagemasks[(int)partword[i]]&(1<<s)){
  172. codebook *stagebook=ci->book_param+
  173. info->stagebooks[(partword[i]<<3)+s];
  174. if(vorbis_book_decodevv_add(stagebook,in,
  175. i*samples_per_partition+beginoff,ch,
  176. &vd->opb,
  177. samples_per_partition,-8)==-1)
  178. goto eopbreak;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. errout:
  185. eopbreak:
  186. return 0;
  187. }