mapping0.c 12 KB

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