mapping0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.11.2.2.2.7 2000/05/08 08:25:43 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_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. double **decay;
  45. long lastframe; /* if a different mode is called, we need to
  46. invalidate decay */
  47. } vorbis_look_mapping0;
  48. static void 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 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 *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){
  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(double *));
  117. for(i=0;i<vi->channels;i++)
  118. look->decay[i]=calloc(vi->blocksizes[vm->blockflag]/2,sizeof(double));
  119. }
  120. return(look);
  121. }
  122. static void 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 *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. free_info(info);
  160. return(NULL);
  161. }
  162. #include <stdio.h>
  163. #include "os.h"
  164. #include "lpc.h"
  165. #include "lsp.h"
  166. #include "envelope.h"
  167. #include "mdct.h"
  168. #include "psy.h"
  169. #include "bitwise.h"
  170. #include "spectrum.h"
  171. #include "scales.h"
  172. /* no time mapping implementation for now */
  173. static long seq=0;
  174. static int forward(vorbis_block *vb,vorbis_look_mapping *l){
  175. vorbis_dsp_state *vd=vb->vd;
  176. vorbis_info *vi=vd->vi;
  177. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  178. vorbis_info_mapping0 *info=look->map;
  179. vorbis_info_mode *mode=look->mode;
  180. int n=vb->pcmend;
  181. int i,j;
  182. double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  183. double **pcmbundle=alloca(sizeof(double *)*vi->channels);
  184. int *nonzero=alloca(sizeof(int)*vi->channels);
  185. /* time domain pre-window: NONE IMPLEMENTED */
  186. /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
  187. for(i=0;i<vi->channels;i++){
  188. double *pcm=vb->pcm[i];
  189. for(j=0;j<n;j++)
  190. pcm[j]*=window[j];
  191. }
  192. /* time-domain post-window: NONE IMPLEMENTED */
  193. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  194. /* only MDCT right now.... */
  195. for(i=0;i<vi->channels;i++){
  196. double *pcm=vb->pcm[i];
  197. mdct_forward(vd->transform[vb->W][0],pcm,pcm);
  198. }
  199. {
  200. double *floor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
  201. double *mask=_vorbis_block_alloc(vb,n*sizeof(double)/2);
  202. for(i=0;i<vi->channels;i++){
  203. double *pcm=vb->pcm[i];
  204. double *decay=look->decay[i];
  205. int submap=info->chmuxlist[i];
  206. /* if some other mode/mapping was called last frame, our decay
  207. accumulator is out of date. Clear it. */
  208. if(look->lastframe+1 != vb->sequence)
  209. memset(decay,0,n*sizeof(double)/2);
  210. /* perform psychoacoustics; do masking */
  211. _vp_compute_mask(look->psy_look+submap,pcm,floor,mask,decay);
  212. _analysis_output("mdct",seq,pcm,n/2,0,1);
  213. _analysis_output("lmdct",seq,pcm,n/2,0,0);
  214. _analysis_output("prefloor",seq,floor,n/2,0,1);
  215. /* perform floor encoding */
  216. nonzero[i]=look->floor_func[submap]->
  217. forward(vb,look->floor_look[submap],floor,floor);
  218. _analysis_output("floor",seq,floor,n/2,0,1);
  219. /* apply the floor, do optional noise levelling */
  220. _vp_apply_floor(look->psy_look+submap,pcm,floor,mask);
  221. _analysis_output("res",seq++,pcm,n/2,0,0);
  222. #ifdef TRAIN
  223. if(nonzero[i]){
  224. FILE *of;
  225. char buffer[80];
  226. int i;
  227. sprintf(buffer,"residue_%d.vqd",vb->mode);
  228. of=fopen(buffer,"a");
  229. for(i=0;i<n/2;i++)
  230. fprintf(of,"%g, ",pcm[i]);
  231. fprintf(of,"\n");
  232. fclose(of);
  233. }
  234. #endif
  235. }
  236. /* perform residue encoding with residue mapping; this is
  237. multiplexed. All the channels belonging to one submap are
  238. encoded (values interleaved), then the next submap, etc */
  239. for(i=0;i<info->submaps;i++){
  240. int ch_in_bundle=0;
  241. for(j=0;j<vi->channels;j++){
  242. if(info->chmuxlist[j]==i && nonzero[j]==1){
  243. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  244. }
  245. }
  246. look->residue_func[i]->forward(vb,look->residue_look[i],
  247. pcmbundle,ch_in_bundle);
  248. }
  249. }
  250. look->lastframe=vb->sequence;
  251. return(0);
  252. }
  253. static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
  254. vorbis_dsp_state *vd=vb->vd;
  255. vorbis_info *vi=vd->vi;
  256. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  257. vorbis_info_mapping0 *info=look->map;
  258. vorbis_info_mode *mode=look->mode;
  259. int i,j;
  260. long n=vb->pcmend=vi->blocksizes[vb->W];
  261. double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  262. double **pcmbundle=alloca(sizeof(double *)*vi->channels);
  263. int *nonzero=alloca(sizeof(int)*vi->channels);
  264. /* time domain information decode (note that applying the
  265. information would have to happen later; we'll probably add a
  266. function entry to the harness for that later */
  267. /* NOT IMPLEMENTED */
  268. /* recover the spectral envelope; store it in the PCM vector for now */
  269. for(i=0;i<vi->channels;i++){
  270. double *pcm=vb->pcm[i];
  271. int submap=info->chmuxlist[i];
  272. nonzero[i]=look->floor_func[submap]->
  273. inverse(vb,look->floor_look[submap],pcm);
  274. _analysis_output("ifloor",seq+i,pcm,n/2,0,1);
  275. }
  276. /* recover the residue, apply directly to the spectral envelope */
  277. for(i=0;i<info->submaps;i++){
  278. int ch_in_bundle=0;
  279. for(j=0;j<vi->channels;j++){
  280. if(info->chmuxlist[j]==i && nonzero[j])
  281. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  282. }
  283. look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
  284. }
  285. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  286. /* only MDCT right now.... */
  287. for(i=0;i<vi->channels;i++){
  288. double *pcm=vb->pcm[i];
  289. _analysis_output("out",seq++,pcm,n/2,0,0);
  290. mdct_backward(vd->transform[vb->W][0],pcm,pcm);
  291. }
  292. /* now apply the decoded pre-window time information */
  293. /* NOT IMPLEMENTED */
  294. /* window the data */
  295. for(i=0;i<vi->channels;i++){
  296. double *pcm=vb->pcm[i];
  297. if(nonzero[i])
  298. for(j=0;j<n;j++)
  299. pcm[j]*=window[j];
  300. else
  301. for(j=0;j<n;j++)
  302. pcm[j]=0.;
  303. }
  304. /* now apply the decoded post-window time information */
  305. /* NOT IMPLEMENTED */
  306. /* all done! */
  307. return(0);
  308. }
  309. /* export hooks */
  310. vorbis_func_mapping mapping0_exportbundle={
  311. &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse
  312. };