mapping0.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: channel mapping 0 implementation
  34. ************************************************************************/
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <math.h>
  39. #include "ogg.h"
  40. #include "os.h"
  41. #include "ivorbiscodec.h"
  42. #include "mdct.h"
  43. #include "codec_internal.h"
  44. #include "codebook.h"
  45. #include "misc.h"
  46. void mapping_clear_info(vorbis_info_mapping *info){
  47. if(info){
  48. if(info->chmuxlist)_ogg_free(info->chmuxlist);
  49. if(info->submaplist)_ogg_free(info->submaplist);
  50. if(info->coupling)_ogg_free(info->coupling);
  51. memset(info,0,sizeof(*info));
  52. }
  53. }
  54. static int ilog(unsigned int v){
  55. int ret=0;
  56. if(v)--v;
  57. while(v){
  58. ret++;
  59. v>>=1;
  60. }
  61. return(ret);
  62. }
  63. /* also responsible for range checking */
  64. int mapping_info_unpack(vorbis_info_mapping *info,vorbis_info *vi,
  65. oggpack_buffer *opb){
  66. int i;
  67. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  68. memset(info,0,sizeof(*info));
  69. if(oggpack_read(opb,1))
  70. info->submaps=oggpack_read(opb,4)+1;
  71. else
  72. info->submaps=1;
  73. if(oggpack_read(opb,1)){
  74. info->coupling_steps=oggpack_read(opb,8)+1;
  75. info->coupling=
  76. _ogg_malloc(info->coupling_steps*sizeof(*info->coupling));
  77. for(i=0;i<info->coupling_steps;i++){
  78. int testM=info->coupling[i].mag=(unsigned char)(oggpack_read(opb,ilog(vi->channels)));
  79. int testA=info->coupling[i].ang=(unsigned char)(oggpack_read(opb,ilog(vi->channels)));
  80. if(testM<0 ||
  81. testA<0 ||
  82. testM==testA ||
  83. testM>=vi->channels ||
  84. testA>=vi->channels) goto err_out;
  85. }
  86. }
  87. if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
  88. if(info->submaps>1){
  89. info->chmuxlist=_ogg_malloc(sizeof(*info->chmuxlist)*vi->channels);
  90. for(i=0;i<vi->channels;i++){
  91. info->chmuxlist[i]=(unsigned char)(oggpack_read(opb,4));
  92. if(info->chmuxlist[i]>=info->submaps)goto err_out;
  93. }
  94. }
  95. info->submaplist=_ogg_malloc(sizeof(*info->submaplist)*info->submaps);
  96. for(i=0;i<info->submaps;i++){
  97. int temp=oggpack_read(opb,8);
  98. info->submaplist[i].floor=(char)oggpack_read(opb,8);
  99. if(info->submaplist[i].floor>=ci->floors)goto err_out;
  100. info->submaplist[i].residue=(char)oggpack_read(opb,8);
  101. if(info->submaplist[i].residue>=ci->residues)goto err_out;
  102. }
  103. return 0;
  104. err_out:
  105. mapping_clear_info(info);
  106. return -1;
  107. }
  108. int mapping_inverse(vorbis_dsp_state *vd,vorbis_info_mapping *info){
  109. vorbis_info *vi=vd->vi;
  110. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  111. int i,j;
  112. long n=ci->blocksizes[vd->W];
  113. ogg_int32_t **pcmbundle=
  114. alloca(sizeof(*pcmbundle)*vi->channels);
  115. int *zerobundle=
  116. alloca(sizeof(*zerobundle)*vi->channels);
  117. int *nonzero=
  118. alloca(sizeof(*nonzero)*vi->channels);
  119. ogg_int32_t **floormemo=
  120. alloca(sizeof(*floormemo)*vi->channels);
  121. /* recover the spectral envelope; store it in the PCM vector for now */
  122. for(i=0;i<vi->channels;i++){
  123. int submap=0;
  124. int floorno;
  125. if(info->submaps>1)
  126. submap=info->chmuxlist[i];
  127. floorno=info->submaplist[submap].floor;
  128. if(ci->floor_type[floorno]){
  129. /* floor 1 */
  130. floormemo[i]=alloca(sizeof(*floormemo[i])*
  131. floor1_memosize(ci->floor_param[floorno]));
  132. floormemo[i]=floor1_inverse1(vd,ci->floor_param[floorno],floormemo[i]);
  133. }else{
  134. /* floor 0 */
  135. floormemo[i]=alloca(sizeof(*floormemo[i])*
  136. floor0_memosize(ci->floor_param[floorno]));
  137. floormemo[i]=floor0_inverse1(vd,ci->floor_param[floorno],floormemo[i]);
  138. }
  139. if(floormemo[i])
  140. nonzero[i]=1;
  141. else
  142. nonzero[i]=0;
  143. memset(vd->work[i],0,sizeof(*vd->work[i])*n/2);
  144. }
  145. /* channel coupling can 'dirty' the nonzero listing */
  146. for(i=0;i<info->coupling_steps;i++){
  147. if(nonzero[info->coupling[i].mag] ||
  148. nonzero[info->coupling[i].ang]){
  149. nonzero[info->coupling[i].mag]=1;
  150. nonzero[info->coupling[i].ang]=1;
  151. }
  152. }
  153. /* recover the residue into our working vectors */
  154. for(i=0;i<info->submaps;i++){
  155. int ch_in_bundle=0;
  156. for(j=0;j<vi->channels;j++){
  157. if(!info->chmuxlist || info->chmuxlist[j]==i){
  158. if(nonzero[j])
  159. zerobundle[ch_in_bundle]=1;
  160. else
  161. zerobundle[ch_in_bundle]=0;
  162. pcmbundle[ch_in_bundle++]=vd->work[j];
  163. }
  164. }
  165. res_inverse(vd,ci->residue_param+info->submaplist[i].residue,
  166. pcmbundle,zerobundle,ch_in_bundle);
  167. }
  168. //for(j=0;j<vi->channels;j++)
  169. //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
  170. /* channel coupling */
  171. for(i=info->coupling_steps-1;i>=0;i--){
  172. ogg_int32_t *pcmM=vd->work[info->coupling[i].mag];
  173. ogg_int32_t *pcmA=vd->work[info->coupling[i].ang];
  174. for(j=0;j<n/2;j++){
  175. ogg_int32_t mag=pcmM[j];
  176. ogg_int32_t ang=pcmA[j];
  177. if(mag>0)
  178. if(ang>0){
  179. pcmM[j]=mag;
  180. pcmA[j]=mag-ang;
  181. }else{
  182. pcmA[j]=mag;
  183. pcmM[j]=mag+ang;
  184. }
  185. else
  186. if(ang>0){
  187. pcmM[j]=mag;
  188. pcmA[j]=mag+ang;
  189. }else{
  190. pcmA[j]=mag;
  191. pcmM[j]=mag-ang;
  192. }
  193. }
  194. }
  195. //for(j=0;j<vi->channels;j++)
  196. //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
  197. /* compute and apply spectral envelope */
  198. for(i=0;i<vi->channels;i++){
  199. ogg_int32_t *pcm=vd->work[i];
  200. int submap=0;
  201. int floorno;
  202. if(info->submaps>1)
  203. submap=info->chmuxlist[i];
  204. floorno=info->submaplist[submap].floor;
  205. if(ci->floor_type[floorno]){
  206. /* floor 1 */
  207. floor1_inverse2(vd,ci->floor_param[floorno],floormemo[i],pcm);
  208. }else{
  209. /* floor 0 */
  210. floor0_inverse2(vd,ci->floor_param[floorno],floormemo[i],pcm);
  211. }
  212. }
  213. //for(j=0;j<vi->channels;j++)
  214. //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
  215. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  216. /* only MDCT right now.... */
  217. for(i=0;i<vi->channels;i++)
  218. mdct_backward(n,vd->work[i]);
  219. //for(j=0;j<vi->channels;j++)
  220. //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
  221. /* all done! */
  222. return(0);
  223. }