mapping0.c 12 KB

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