mapping0.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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-2002 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: channel mapping 0 implementation
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "ogg/ogg.h"
  20. #include "ivorbiscodec.h"
  21. #include "mdct.h"
  22. #include "codec_internal.h"
  23. #include "codebook.h"
  24. #include "window.h"
  25. #include "registry.h"
  26. #include "misc.h"
  27. /* simplistic, wasteful way of doing this (unique lookup for each
  28. mode/submapping); there should be a central repository for
  29. identical lookups. That will require minor work, so I'm putting it
  30. off as low priority.
  31. Why a lookup for each backend in a given mode? Because the
  32. blocksize is set by the mode, and low backend lookups may require
  33. parameters from other areas of the mode/mapping */
  34. typedef struct {
  35. vorbis_info_mode *mode;
  36. vorbis_info_mapping0 *map;
  37. vorbis_look_floor **floor_look;
  38. vorbis_look_residue **residue_look;
  39. vorbis_func_floor **floor_func;
  40. vorbis_func_residue **residue_func;
  41. int ch;
  42. long lastframe; /* if a different mode is called, we need to
  43. invalidate decay */
  44. } vorbis_look_mapping0;
  45. static void mapping0_free_info(vorbis_info_mapping *i){
  46. vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
  47. if(info){
  48. memset(info,0,sizeof(*info));
  49. _ogg_free(info);
  50. }
  51. }
  52. static void mapping0_free_look(vorbis_look_mapping *look){
  53. int i;
  54. vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
  55. if(l){
  56. for(i=0;i<l->map->submaps;i++){
  57. l->floor_func[i]->free_look(l->floor_look[i]);
  58. l->residue_func[i]->free_look(l->residue_look[i]);
  59. }
  60. _ogg_free(l->floor_func);
  61. _ogg_free(l->residue_func);
  62. _ogg_free(l->floor_look);
  63. _ogg_free(l->residue_look);
  64. memset(l,0,sizeof(*l));
  65. _ogg_free(l);
  66. }
  67. }
  68. static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
  69. vorbis_info_mapping *m){
  70. int i;
  71. vorbis_info *vi=vd->vi;
  72. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  73. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)_ogg_calloc(1,sizeof(*look));
  74. vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
  75. look->mode=vm;
  76. look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
  77. look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
  78. look->floor_func=(vorbis_func_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_func));
  79. look->residue_func=(vorbis_func_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_func));
  80. for(i=0;i<info->submaps;i++){
  81. int floornum=info->floorsubmap[i];
  82. int resnum=info->residuesubmap[i];
  83. look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
  84. look->floor_look[i]=look->floor_func[i]->
  85. look(vd,vm,ci->floor_param[floornum]);
  86. look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
  87. look->residue_look[i]=look->residue_func[i]->
  88. look(vd,vm,ci->residue_param[resnum]);
  89. }
  90. look->ch=vi->channels;
  91. return(look);
  92. }
  93. static int ilog(unsigned int v){
  94. int ret=0;
  95. if(v)--v;
  96. while(v){
  97. ret++;
  98. v>>=1;
  99. }
  100. return(ret);
  101. }
  102. /* also responsible for range checking */
  103. static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  104. int i,b;
  105. vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info));
  106. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  107. memset(info,0,sizeof(*info));
  108. b=oggpack_read(opb,1);
  109. if(b<0)goto err_out;
  110. if(b){
  111. info->submaps=oggpack_read(opb,4)+1;
  112. if(info->submaps<=0)goto err_out;
  113. }else
  114. info->submaps=1;
  115. b=oggpack_read(opb,1);
  116. if(b<0)goto err_out;
  117. if(b){
  118. info->coupling_steps=oggpack_read(opb,8)+1;
  119. if(info->coupling_steps<=0)goto err_out;
  120. for(i=0;i<info->coupling_steps;i++){
  121. int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
  122. int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
  123. if(testM<0 ||
  124. testA<0 ||
  125. testM==testA ||
  126. testM>=vi->channels ||
  127. testA>=vi->channels) goto err_out;
  128. }
  129. }
  130. if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */
  131. if(info->submaps>1){
  132. for(i=0;i<vi->channels;i++){
  133. info->chmuxlist[i]=oggpack_read(opb,4);
  134. if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out;
  135. }
  136. }
  137. for(i=0;i<info->submaps;i++){
  138. int temp=oggpack_read(opb,8);
  139. if(temp>=ci->times)goto err_out;
  140. info->floorsubmap[i]=oggpack_read(opb,8);
  141. if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out;
  142. info->residuesubmap[i]=oggpack_read(opb,8);
  143. if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)
  144. goto err_out;
  145. }
  146. return info;
  147. err_out:
  148. mapping0_free_info(info);
  149. return(NULL);
  150. }
  151. static int seq=0;
  152. static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
  153. vorbis_dsp_state *vd=vb->vd;
  154. vorbis_info *vi=vd->vi;
  155. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  156. private_state *b=(private_state *)vd->backend_state;
  157. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  158. vorbis_info_mapping0 *info=look->map;
  159. int i,j;
  160. long n=vb->pcmend=ci->blocksizes[vb->W];
  161. ogg_int32_t **pcmbundle=(ogg_int32_t **)alloca(sizeof(*pcmbundle)*vi->channels);
  162. int *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels);
  163. int *nonzero =(int *)alloca(sizeof(*nonzero)*vi->channels);
  164. void **floormemo=(void **)alloca(sizeof(*floormemo)*vi->channels);
  165. /* time domain information decode (note that applying the
  166. information would have to happen later; we'll probably add a
  167. function entry to the harness for that later */
  168. /* NOT IMPLEMENTED */
  169. /* recover the spectral envelope; store it in the PCM vector for now */
  170. for(i=0;i<vi->channels;i++){
  171. int submap=info->chmuxlist[i];
  172. floormemo[i]=look->floor_func[submap]->
  173. inverse1(vb,look->floor_look[submap]);
  174. if(floormemo[i])
  175. nonzero[i]=1;
  176. else
  177. nonzero[i]=0;
  178. memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
  179. }
  180. /* channel coupling can 'dirty' the nonzero listing */
  181. for(i=0;i<info->coupling_steps;i++){
  182. if(nonzero[info->coupling_mag[i]] ||
  183. nonzero[info->coupling_ang[i]]){
  184. nonzero[info->coupling_mag[i]]=1;
  185. nonzero[info->coupling_ang[i]]=1;
  186. }
  187. }
  188. /* recover the residue into our working vectors */
  189. for(i=0;i<info->submaps;i++){
  190. int ch_in_bundle=0;
  191. for(j=0;j<vi->channels;j++){
  192. if(info->chmuxlist[j]==i){
  193. if(nonzero[j])
  194. zerobundle[ch_in_bundle]=1;
  195. else
  196. zerobundle[ch_in_bundle]=0;
  197. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  198. }
  199. }
  200. look->residue_func[i]->inverse(vb,look->residue_look[i],
  201. pcmbundle,zerobundle,ch_in_bundle);
  202. }
  203. //for(j=0;j<vi->channels;j++)
  204. //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
  205. /* channel coupling */
  206. for(i=info->coupling_steps-1;i>=0;i--){
  207. ogg_int32_t *pcmM=vb->pcm[info->coupling_mag[i]];
  208. ogg_int32_t *pcmA=vb->pcm[info->coupling_ang[i]];
  209. for(j=0;j<n/2;j++){
  210. ogg_int32_t mag=pcmM[j];
  211. ogg_int32_t ang=pcmA[j];
  212. if(mag>0)
  213. if(ang>0){
  214. pcmM[j]=mag;
  215. pcmA[j]=mag-ang;
  216. }else{
  217. pcmA[j]=mag;
  218. pcmM[j]=mag+ang;
  219. }
  220. else
  221. if(ang>0){
  222. pcmM[j]=mag;
  223. pcmA[j]=mag+ang;
  224. }else{
  225. pcmA[j]=mag;
  226. pcmM[j]=mag-ang;
  227. }
  228. }
  229. }
  230. //for(j=0;j<vi->channels;j++)
  231. //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
  232. /* compute and apply spectral envelope */
  233. for(i=0;i<vi->channels;i++){
  234. ogg_int32_t *pcm=vb->pcm[i];
  235. int submap=info->chmuxlist[i];
  236. look->floor_func[submap]->
  237. inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
  238. }
  239. //for(j=0;j<vi->channels;j++)
  240. //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
  241. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  242. /* only MDCT right now.... */
  243. for(i=0;i<vi->channels;i++){
  244. ogg_int32_t *pcm=vb->pcm[i];
  245. mdct_backward(n,pcm,pcm);
  246. }
  247. //for(j=0;j<vi->channels;j++)
  248. //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
  249. /* window the data */
  250. for(i=0;i<vi->channels;i++){
  251. ogg_int32_t *pcm=vb->pcm[i];
  252. if(nonzero[i])
  253. _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
  254. else
  255. for(j=0;j<n;j++)
  256. pcm[j]=0;
  257. }
  258. //for(j=0;j<vi->channels;j++)
  259. //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
  260. seq+=vi->channels;
  261. /* all done! */
  262. return(0);
  263. }
  264. /* export hooks */
  265. vorbis_func_mapping mapping0_exportbundle={
  266. &mapping0_unpack,
  267. &mapping0_look,
  268. &mapping0_free_info,
  269. &mapping0_free_look,
  270. &mapping0_inverse
  271. };