mapping0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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: channel mapping 0 implementation
  14. last mod: $Id: mapping0.c,v 1.15.2.2.2.1 2000/09/03 08:34:52 jack Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <ogg/ogg.h>
  20. #include "vorbis/codec.h"
  21. #include "vorbis/backends.h"
  22. #include "bookinternal.h"
  23. #include "registry.h"
  24. #include "psy.h"
  25. #include "misc.h"
  26. /* simplistic, wasteful way of doing this (unique lookup for each
  27. mode/submapping); there should be a central repository for
  28. identical lookups. That will require minor work, so I'm putting it
  29. off as low priority.
  30. Why a lookup for each backend in a given mode? Because the
  31. blocksize is set by the mode, and low backend lookups may require
  32. parameters from other areas of the mode/mapping */
  33. typedef struct {
  34. vorbis_info_mode *mode;
  35. vorbis_info_mapping0 *map;
  36. vorbis_look_time **time_look;
  37. vorbis_look_floor **floor_look;
  38. vorbis_look_residue **residue_look;
  39. vorbis_look_psy *psy_look;
  40. vorbis_func_time **time_func;
  41. vorbis_func_floor **floor_func;
  42. vorbis_func_residue **residue_func;
  43. int ch;
  44. float **decay;
  45. long lastframe; /* if a different mode is called, we need to
  46. invalidate decay */
  47. } vorbis_look_mapping0;
  48. static void mapping0_free_info(vorbis_info_mapping *i){
  49. if(i){
  50. memset(i,0,sizeof(vorbis_info_mapping0));
  51. free(i);
  52. }
  53. }
  54. static void mapping0_free_look(vorbis_look_mapping *look){
  55. int i;
  56. vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
  57. if(l){
  58. for(i=0;i<l->map->submaps;i++){
  59. l->time_func[i]->free_look(l->time_look[i]);
  60. l->floor_func[i]->free_look(l->floor_look[i]);
  61. l->residue_func[i]->free_look(l->residue_look[i]);
  62. if(l->psy_look)_vp_psy_clear(l->psy_look+i);
  63. }
  64. if(l->decay){
  65. for(i=0;i<l->ch;i++){
  66. if(l->decay[i])free(l->decay[i]);
  67. }
  68. free(l->decay);
  69. }
  70. free(l->time_func);
  71. free(l->floor_func);
  72. free(l->residue_func);
  73. free(l->time_look);
  74. free(l->floor_look);
  75. free(l->residue_look);
  76. if(l->psy_look)free(l->psy_look);
  77. memset(l,0,sizeof(vorbis_look_mapping0));
  78. free(l);
  79. }
  80. }
  81. static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
  82. vorbis_info_mapping *m){
  83. int i;
  84. vorbis_info *vi=vd->vi;
  85. vorbis_look_mapping0 *look=calloc(1,sizeof(vorbis_look_mapping0));
  86. vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
  87. look->mode=vm;
  88. look->time_look=calloc(info->submaps,sizeof(vorbis_look_time *));
  89. look->floor_look=calloc(info->submaps,sizeof(vorbis_look_floor *));
  90. look->residue_look=calloc(info->submaps,sizeof(vorbis_look_residue *));
  91. if(vi->psys)look->psy_look=calloc(info->submaps,sizeof(vorbis_look_psy));
  92. look->time_func=calloc(info->submaps,sizeof(vorbis_func_time *));
  93. look->floor_func=calloc(info->submaps,sizeof(vorbis_func_floor *));
  94. look->residue_func=calloc(info->submaps,sizeof(vorbis_func_residue *));
  95. for(i=0;i<info->submaps;i++){
  96. int timenum=info->timesubmap[i];
  97. int floornum=info->floorsubmap[i];
  98. int resnum=info->residuesubmap[i];
  99. look->time_func[i]=_time_P[vi->time_type[timenum]];
  100. look->time_look[i]=look->time_func[i]->
  101. look(vd,vm,vi->time_param[timenum]);
  102. look->floor_func[i]=_floor_P[vi->floor_type[floornum]];
  103. look->floor_look[i]=look->floor_func[i]->
  104. look(vd,vm,vi->floor_param[floornum]);
  105. look->residue_func[i]=_residue_P[vi->residue_type[resnum]];
  106. look->residue_look[i]=look->residue_func[i]->
  107. look(vd,vm,vi->residue_param[resnum]);
  108. if(vi->psys && vd->analysisp){
  109. int psynum=info->psysubmap[i];
  110. _vp_psy_init(look->psy_look+i,vi->psy_param[psynum],
  111. vi->blocksizes[vm->blockflag]/2,vi->rate);
  112. }
  113. }
  114. look->ch=vi->channels;
  115. if(vi->psys){
  116. look->decay=calloc(vi->channels,sizeof(float *));
  117. for(i=0;i<vi->channels;i++)
  118. look->decay[i]=calloc(vi->blocksizes[vm->blockflag]/2,sizeof(float));
  119. }
  120. return(look);
  121. }
  122. static void mapping0_pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
  123. int i;
  124. vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
  125. oggpack_write(opb,info->submaps-1,4);
  126. /* we don't write the channel submappings if we only have one... */
  127. if(info->submaps>1){
  128. for(i=0;i<vi->channels;i++)
  129. oggpack_write(opb,info->chmuxlist[i],4);
  130. }
  131. for(i=0;i<info->submaps;i++){
  132. oggpack_write(opb,info->timesubmap[i],8);
  133. oggpack_write(opb,info->floorsubmap[i],8);
  134. oggpack_write(opb,info->residuesubmap[i],8);
  135. }
  136. }
  137. /* also responsible for range checking */
  138. static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  139. int i;
  140. vorbis_info_mapping0 *info=calloc(1,sizeof(vorbis_info_mapping0));
  141. memset(info,0,sizeof(vorbis_info_mapping0));
  142. info->submaps=oggpack_read(opb,4)+1;
  143. if(info->submaps>1){
  144. for(i=0;i<vi->channels;i++){
  145. info->chmuxlist[i]=oggpack_read(opb,4);
  146. if(info->chmuxlist[i]>=info->submaps)goto err_out;
  147. }
  148. }
  149. for(i=0;i<info->submaps;i++){
  150. info->timesubmap[i]=oggpack_read(opb,8);
  151. if(info->timesubmap[i]>=vi->times)goto err_out;
  152. info->floorsubmap[i]=oggpack_read(opb,8);
  153. if(info->floorsubmap[i]>=vi->floors)goto err_out;
  154. info->residuesubmap[i]=oggpack_read(opb,8);
  155. if(info->residuesubmap[i]>=vi->residues)goto err_out;
  156. }
  157. return info;
  158. err_out:
  159. mapping0_free_info(info);
  160. return(NULL);
  161. }
  162. #include "os.h"
  163. #include "lpc.h"
  164. #include "lsp.h"
  165. #include "envelope.h"
  166. #include "mdct.h"
  167. #include "psy.h"
  168. #include "scales.h"
  169. /* no time mapping implementation for now */
  170. static long seq=0;
  171. static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
  172. vorbis_dsp_state *vd=vb->vd;
  173. vorbis_info *vi=vd->vi;
  174. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  175. vorbis_info_mapping0 *info=look->map;
  176. vorbis_info_mode *mode=look->mode;
  177. int n=vb->pcmend;
  178. int i,j;
  179. float *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  180. float **pcmbundle=alloca(sizeof(float *)*vi->channels);
  181. int *nonzero=alloca(sizeof(int)*vi->channels);
  182. /* time domain pre-window: NONE IMPLEMENTED */
  183. /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
  184. for(i=0;i<vi->channels;i++){
  185. float *pcm=vb->pcm[i];
  186. for(j=0;j<n;j++)
  187. pcm[j]*=window[j];
  188. }
  189. /* time-domain post-window: NONE IMPLEMENTED */
  190. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  191. /* only MDCT right now.... */
  192. for(i=0;i<vi->channels;i++){
  193. float *pcm=vb->pcm[i];
  194. mdct_forward(vd->transform[vb->W][0],pcm,pcm);
  195. }
  196. {
  197. float *floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
  198. for(i=0;i<vi->channels;i++){
  199. float *pcm=vb->pcm[i];
  200. float *decay=look->decay[i];
  201. int submap=info->chmuxlist[i];
  202. /* if some other mode/mapping was called last frame, our decay
  203. accumulator is out of date. Clear it. */
  204. if(look->lastframe+1 != vb->sequence)
  205. memset(decay,0,n*sizeof(float)/2);
  206. /* perform psychoacoustics; do masking */
  207. _vp_compute_mask(look->psy_look+submap,pcm,floor,decay);
  208. _analysis_output("decay",seq,decay,n/2,0,1);
  209. _analysis_output("mdct",seq,pcm,n/2,0,1);
  210. _analysis_output("lmdct",seq,pcm,n/2,0,0);
  211. _analysis_output("prefloor",seq,floor,n/2,0,1);
  212. /* perform floor encoding */
  213. nonzero[i]=look->floor_func[submap]->
  214. forward(vb,look->floor_look[submap],floor,floor);
  215. _analysis_output("floor",seq,floor,n/2,0,1);
  216. /* apply the floor, do optional noise levelling */
  217. _vp_apply_floor(look->psy_look+submap,pcm,floor);
  218. _analysis_output("res",seq++,pcm,n/2,0,0);
  219. #ifdef TRAIN_RES
  220. if(nonzero[i]){
  221. FILE *of;
  222. char buffer[80];
  223. int i;
  224. sprintf(buffer,"residue_%d.vqd",vb->mode);
  225. of=fopen(buffer,"a");
  226. for(i=0;i<n/2;i++)
  227. fprintf(of,"%.2f, ",pcm[i]);
  228. fprintf(of,"\n");
  229. fclose(of);
  230. }
  231. #endif
  232. }
  233. /* perform residue encoding with residue mapping; this is
  234. multiplexed. All the channels belonging to one submap are
  235. encoded (values interleaved), then the next submap, etc */
  236. for(i=0;i<info->submaps;i++){
  237. int ch_in_bundle=0;
  238. for(j=0;j<vi->channels;j++){
  239. if(info->chmuxlist[j]==i && nonzero[j]==1){
  240. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  241. }
  242. }
  243. look->residue_func[i]->forward(vb,look->residue_look[i],
  244. pcmbundle,ch_in_bundle);
  245. }
  246. }
  247. look->lastframe=vb->sequence;
  248. return(0);
  249. }
  250. static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
  251. vorbis_dsp_state *vd=vb->vd;
  252. vorbis_info *vi=vd->vi;
  253. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  254. vorbis_info_mapping0 *info=look->map;
  255. vorbis_info_mode *mode=look->mode;
  256. int i,j;
  257. long n=vb->pcmend=vi->blocksizes[vb->W];
  258. float *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  259. float **pcmbundle=alloca(sizeof(float *)*vi->channels);
  260. int *nonzero=alloca(sizeof(int)*vi->channels);
  261. /* time domain information decode (note that applying the
  262. information would have to happen later; we'll probably add a
  263. function entry to the harness for that later */
  264. /* NOT IMPLEMENTED */
  265. /* recover the spectral envelope; store it in the PCM vector for now */
  266. for(i=0;i<vi->channels;i++){
  267. float *pcm=vb->pcm[i];
  268. int submap=info->chmuxlist[i];
  269. nonzero[i]=look->floor_func[submap]->
  270. inverse(vb,look->floor_look[submap],pcm);
  271. _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
  272. }
  273. /* recover the residue, apply directly to the spectral envelope */
  274. for(i=0;i<info->submaps;i++){
  275. int ch_in_bundle=0;
  276. for(j=0;j<vi->channels;j++){
  277. if(info->chmuxlist[j]==i && nonzero[j])
  278. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  279. }
  280. look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
  281. }
  282. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  283. /* only MDCT right now.... */
  284. for(i=0;i<vi->channels;i++){
  285. float *pcm=vb->pcm[i];
  286. _analysis_output("out",seq+i,pcm,n/2,0,1);
  287. mdct_backward(vd->transform[vb->W][0],pcm,pcm);
  288. }
  289. /* now apply the decoded pre-window time information */
  290. /* NOT IMPLEMENTED */
  291. /* window the data */
  292. for(i=0;i<vi->channels;i++){
  293. float *pcm=vb->pcm[i];
  294. if(nonzero[i])
  295. for(j=0;j<n;j++)
  296. pcm[j]*=window[j];
  297. else
  298. for(j=0;j<n;j++)
  299. pcm[j]=0.;
  300. _analysis_output("final",seq++,pcm,n,0,0);
  301. }
  302. /* now apply the decoded post-window time information */
  303. /* NOT IMPLEMENTED */
  304. /* all done! */
  305. return(0);
  306. }
  307. /* export hooks */
  308. vorbis_func_mapping mapping0_exportbundle={
  309. &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_free_info,
  310. &mapping0_free_look,&mapping0_forward,&mapping0_inverse
  311. };