res012.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: residue backend 0, 1 and 2 implementation
  34. ************************************************************************/
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <math.h>
  38. #include "ogg.h"
  39. #include "ivorbiscodec.h"
  40. #include "codec_internal.h"
  41. #include "codebook.h"
  42. #include "misc.h"
  43. #include "os.h"
  44. void res_clear_info(vorbis_info_residue *info){
  45. if(info){
  46. if(info->stagemasks)_ogg_free(info->stagemasks);
  47. if(info->stagebooks)_ogg_free(info->stagebooks);
  48. memset(info,0,sizeof(*info));
  49. }
  50. }
  51. /* vorbis_info is for range checking */
  52. int res_unpack(vorbis_info_residue *info,
  53. vorbis_info *vi,oggpack_buffer *opb){
  54. int j,k;
  55. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  56. memset(info,0,sizeof(*info));
  57. info->type=oggpack_read(opb,16);
  58. if(info->type>2 || info->type<0)goto errout;
  59. info->begin=oggpack_read(opb,24);
  60. info->end=oggpack_read(opb,24);
  61. info->grouping=oggpack_read(opb,24)+1;
  62. info->partitions=(char)(oggpack_read(opb,6)+1);
  63. info->groupbook=(unsigned char)oggpack_read(opb,8);
  64. if(info->groupbook>=ci->books)goto errout;
  65. info->stagemasks=_ogg_malloc(info->partitions*sizeof(*info->stagemasks));
  66. info->stagebooks=_ogg_malloc(info->partitions*8*sizeof(*info->stagebooks));
  67. for(j=0;j<info->partitions;j++){
  68. int cascade=oggpack_read(opb,3);
  69. if(oggpack_read(opb,1))
  70. cascade|=(oggpack_read(opb,5)<<3);
  71. info->stagemasks[j]=cascade;
  72. }
  73. for(j=0;j<info->partitions;j++){
  74. for(k=0;k<8;k++){
  75. if((info->stagemasks[j]>>k)&1){
  76. unsigned char book=(unsigned char)oggpack_read(opb,8);
  77. if(book>=ci->books)goto errout;
  78. info->stagebooks[j*8+k]=book;
  79. if(k+1>info->stages)info->stages=k+1;
  80. }else
  81. info->stagebooks[j*8+k]=0xff;
  82. }
  83. }
  84. if(oggpack_eop(opb))goto errout;
  85. return 0;
  86. errout:
  87. res_clear_info(info);
  88. return 1;
  89. }
  90. int res_inverse(vorbis_dsp_state *vd,vorbis_info_residue *info,
  91. ogg_int32_t **in,int *nonzero,int ch){
  92. int i,j,k,s,used=0;
  93. codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup;
  94. codebook *phrasebook=ci->book_param+info->groupbook;
  95. int samples_per_partition=info->grouping;
  96. int partitions_per_word=phrasebook->dim;
  97. int pcmend=ci->blocksizes[vd->W];
  98. if(info->type<2){
  99. int max=pcmend>>1;
  100. int end=(info->end<max?info->end:max);
  101. int n=end-info->begin;
  102. if(n>0){
  103. int partvals=n/samples_per_partition;
  104. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  105. for(i=0;i<ch;i++)
  106. if(nonzero[i])
  107. in[used++]=in[i];
  108. ch=used;
  109. if(used){
  110. char **partword=(char **)alloca(ch*sizeof(*partword));
  111. for(j=0;j<ch;j++)
  112. partword[j]=(char *)alloca(partwords*partitions_per_word*
  113. sizeof(*partword[j]));
  114. for(s=0;s<info->stages;s++){
  115. for(i=0;i<partvals;){
  116. if(s==0){
  117. /* fetch the partition word for each channel */
  118. partword[0][i+partitions_per_word-1]=1;
  119. for(k=partitions_per_word-2;k>=0;k--)
  120. partword[0][i+k]=partword[0][i+k+1]*info->partitions;
  121. for(j=1;j<ch;j++)
  122. for(k=partitions_per_word-1;k>=0;k--)
  123. partword[j][i+k]=partword[j-1][i+k];
  124. for(j=0;j<ch;j++){
  125. int temp=vorbis_book_decode(phrasebook,&vd->opb);
  126. if(temp==-1)goto eopbreak;
  127. /* this can be done quickly in assembly due to the quotient
  128. always being at most six bits */
  129. for(k=0;k<partitions_per_word;k++){
  130. ogg_uint32_t div=partword[j][i+k];
  131. partword[j][i+k]=temp/div;
  132. temp-=partword[j][i+k]*div;
  133. }
  134. }
  135. }
  136. /* now we decode residual values for the partitions */
  137. for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  138. for(j=0;j<ch;j++){
  139. long offset=info->begin+i*samples_per_partition;
  140. int idx = (int)partword[j][i];
  141. if(idx < info->partitions && info->stagemasks[idx]&(1<<s)){
  142. codebook *stagebook=ci->book_param+
  143. info->stagebooks[(partword[j][i]<<3)+s];
  144. if(info->type){
  145. if(vorbis_book_decodev_add(stagebook,in[j]+offset,&vd->opb,
  146. samples_per_partition,-8)==-1)
  147. goto eopbreak;
  148. }else{
  149. if(vorbis_book_decodevs_add(stagebook,in[j]+offset,&vd->opb,
  150. samples_per_partition,-8)==-1)
  151. goto eopbreak;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }else{
  160. int max=(pcmend*ch)>>1;
  161. int end=(info->end<max?info->end:max);
  162. int n=end-info->begin;
  163. if(n>0){
  164. int partvals=n/samples_per_partition;
  165. int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  166. char *partword=
  167. (char *)alloca(partwords*partitions_per_word*sizeof(*partword));
  168. int beginoff=info->begin/ch;
  169. for(i=0;i<ch;i++)if(nonzero[i])break;
  170. if(i==ch)return(0); /* no nonzero vectors */
  171. samples_per_partition/=ch;
  172. for(s=0;s<info->stages;s++){
  173. for(i=0;i<partvals;){
  174. if(s==0){
  175. int temp;
  176. partword[i+partitions_per_word-1]=1;
  177. for(k=partitions_per_word-2;k>=0;k--)
  178. partword[i+k]=partword[i+k+1]*info->partitions;
  179. /* fetch the partition word */
  180. temp=vorbis_book_decode(phrasebook,&vd->opb);
  181. if(temp==-1)goto eopbreak;
  182. /* this can be done quickly in assembly due to the quotient
  183. always being at most six bits */
  184. for(k=0;k<partitions_per_word;k++){
  185. ogg_uint32_t div=partword[i+k];
  186. partword[i+k]=temp/div;
  187. temp-=partword[i+k]*div;
  188. }
  189. }
  190. /* now we decode residual values for the partitions */
  191. for(k=0;k<partitions_per_word && i<partvals;k++,i++){
  192. if(partword[i] >= 0 && partword[i] < info->partitions &&
  193. (info->stagemasks[(int)partword[i]] & (1 << s))){
  194. codebook *stagebook=ci->book_param+
  195. info->stagebooks[(partword[i]<<3)+s];
  196. if(vorbis_book_decodevv_add(stagebook,in,
  197. i*samples_per_partition+beginoff,ch,
  198. &vd->opb,
  199. samples_per_partition,-8)==-1)
  200. goto eopbreak;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. eopbreak:
  208. return 0;
  209. }