mapping0.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: channel mapping 0 implementation
  13. last mod: $Id: mapping0.c,v 1.33 2001/06/17 22:25:50 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.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 "bitbuffer.h"
  24. #include "registry.h"
  25. #include "psy.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. drft_lookup fft_look;
  36. vorbis_info_mode *mode;
  37. vorbis_info_mapping0 *map;
  38. vorbis_look_time **time_look;
  39. vorbis_look_floor **floor_look;
  40. vorbis_look_residue **residue_look;
  41. vorbis_look_psy *psy_look;
  42. vorbis_func_time **time_func;
  43. vorbis_func_floor **floor_func;
  44. vorbis_func_residue **residue_func;
  45. int ch;
  46. long lastframe; /* if a different mode is called, we need to
  47. invalidate decay */
  48. } vorbis_look_mapping0;
  49. static vorbis_info_mapping *mapping0_copy_info(vorbis_info_mapping *vm){
  50. vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)vm;
  51. vorbis_info_mapping0 *ret=_ogg_malloc(sizeof(vorbis_info_mapping0));
  52. memcpy(ret,info,sizeof(vorbis_info_mapping0));
  53. return(ret);
  54. }
  55. static void mapping0_free_info(vorbis_info_mapping *i){
  56. if(i){
  57. memset(i,0,sizeof(vorbis_info_mapping0));
  58. _ogg_free(i);
  59. }
  60. }
  61. static void mapping0_free_look(vorbis_look_mapping *look){
  62. int i;
  63. vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
  64. if(l){
  65. drft_clear(&l->fft_look);
  66. for(i=0;i<l->map->submaps;i++){
  67. l->time_func[i]->free_look(l->time_look[i]);
  68. l->floor_func[i]->free_look(l->floor_look[i]);
  69. l->residue_func[i]->free_look(l->residue_look[i]);
  70. if(l->psy_look)_vp_psy_clear(l->psy_look+i);
  71. }
  72. _ogg_free(l->time_func);
  73. _ogg_free(l->floor_func);
  74. _ogg_free(l->residue_func);
  75. _ogg_free(l->time_look);
  76. _ogg_free(l->floor_look);
  77. _ogg_free(l->residue_look);
  78. if(l->psy_look)_ogg_free(l->psy_look);
  79. memset(l,0,sizeof(vorbis_look_mapping0));
  80. _ogg_free(l);
  81. }
  82. }
  83. static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
  84. vorbis_info_mapping *m){
  85. int i;
  86. vorbis_info *vi=vd->vi;
  87. codec_setup_info *ci=vi->codec_setup;
  88. vorbis_look_mapping0 *look=_ogg_calloc(1,sizeof(vorbis_look_mapping0));
  89. vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
  90. look->mode=vm;
  91. look->time_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_time *));
  92. look->floor_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_floor *));
  93. look->residue_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_residue *));
  94. if(ci->psys)look->psy_look=_ogg_calloc(info->submaps,sizeof(vorbis_look_psy));
  95. look->time_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_time *));
  96. look->floor_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_floor *));
  97. look->residue_func=_ogg_calloc(info->submaps,sizeof(vorbis_func_residue *));
  98. for(i=0;i<info->submaps;i++){
  99. int timenum=info->timesubmap[i];
  100. int floornum=info->floorsubmap[i];
  101. int resnum=info->residuesubmap[i];
  102. look->time_func[i]=_time_P[ci->time_type[timenum]];
  103. look->time_look[i]=look->time_func[i]->
  104. look(vd,vm,ci->time_param[timenum]);
  105. look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
  106. look->floor_look[i]=look->floor_func[i]->
  107. look(vd,vm,ci->floor_param[floornum]);
  108. look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
  109. look->residue_look[i]=look->residue_func[i]->
  110. look(vd,vm,ci->residue_param[resnum]);
  111. if(ci->psys && vd->analysisp){
  112. int psynum=info->psysubmap[i];
  113. _vp_psy_init(look->psy_look+i,ci->psy_param[psynum],
  114. ci->blocksizes[vm->blockflag]/2,vi->rate);
  115. }
  116. }
  117. look->ch=vi->channels;
  118. if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
  119. return(look);
  120. }
  121. static int ilog2(unsigned int v){
  122. int ret=0;
  123. while(v>1){
  124. ret++;
  125. v>>=1;
  126. }
  127. return(ret);
  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. /* another 'we meant to do it this way' hack... up to beta 4, we
  133. packed 4 binary zeros here to signify one submapping in use. We
  134. now redefine that to mean four bitflags that indicate use of
  135. deeper features; bit0:submappings, bit1:coupling,
  136. bit2,3:reserved. This is backward compatable with all actual uses
  137. of the beta code. */
  138. if(info->submaps>1){
  139. oggpack_write(opb,1,1);
  140. oggpack_write(opb,info->submaps-1,4);
  141. }else
  142. oggpack_write(opb,0,1);
  143. if(info->coupling_steps>0){
  144. oggpack_write(opb,1,1);
  145. oggpack_write(opb,info->coupling_steps-1,8);
  146. for(i=0;i<info->coupling_steps;i++){
  147. oggpack_write(opb,info->coupling_mag[i],ilog2(vi->channels));
  148. oggpack_write(opb,info->coupling_ang[i],ilog2(vi->channels));
  149. }
  150. }else
  151. oggpack_write(opb,0,1);
  152. oggpack_write(opb,0,2); /* 2,3:reserved */
  153. /* we don't write the channel submappings if we only have one... */
  154. if(info->submaps>1){
  155. for(i=0;i<vi->channels;i++)
  156. oggpack_write(opb,info->chmuxlist[i],4);
  157. }
  158. for(i=0;i<info->submaps;i++){
  159. oggpack_write(opb,info->timesubmap[i],8);
  160. oggpack_write(opb,info->floorsubmap[i],8);
  161. oggpack_write(opb,info->residuesubmap[i],8);
  162. }
  163. }
  164. /* also responsible for range checking */
  165. static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  166. int i;
  167. vorbis_info_mapping0 *info=_ogg_calloc(1,sizeof(vorbis_info_mapping0));
  168. codec_setup_info *ci=vi->codec_setup;
  169. memset(info,0,sizeof(vorbis_info_mapping0));
  170. if(oggpack_read(opb,1))
  171. info->submaps=oggpack_read(opb,4)+1;
  172. else
  173. info->submaps=1;
  174. if(oggpack_read(opb,1)){
  175. info->coupling_steps=oggpack_read(opb,8)+1;
  176. for(i=0;i<info->coupling_steps;i++){
  177. int testM=info->coupling_mag[i]=oggpack_read(opb,ilog2(vi->channels));
  178. int testA=info->coupling_ang[i]=oggpack_read(opb,ilog2(vi->channels));
  179. if(testM<0 ||
  180. testA<0 ||
  181. testM==testA ||
  182. testM>=vi->channels ||
  183. testA>=vi->channels) goto err_out;
  184. }
  185. }
  186. if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
  187. if(info->submaps>1){
  188. for(i=0;i<vi->channels;i++){
  189. info->chmuxlist[i]=oggpack_read(opb,4);
  190. if(info->chmuxlist[i]>=info->submaps)goto err_out;
  191. }
  192. }
  193. for(i=0;i<info->submaps;i++){
  194. info->timesubmap[i]=oggpack_read(opb,8);
  195. if(info->timesubmap[i]>=ci->times)goto err_out;
  196. info->floorsubmap[i]=oggpack_read(opb,8);
  197. if(info->floorsubmap[i]>=ci->floors)goto err_out;
  198. info->residuesubmap[i]=oggpack_read(opb,8);
  199. if(info->residuesubmap[i]>=ci->residues)goto err_out;
  200. }
  201. return info;
  202. err_out:
  203. mapping0_free_info(info);
  204. return(NULL);
  205. }
  206. #include "os.h"
  207. #include "lpc.h"
  208. #include "lsp.h"
  209. #include "envelope.h"
  210. #include "mdct.h"
  211. #include "psy.h"
  212. #include "scales.h"
  213. /* no time mapping implementation for now */
  214. static long seq=0;
  215. static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){
  216. vorbis_dsp_state *vd=vb->vd;
  217. vorbis_info *vi=vd->vi;
  218. backend_lookup_state *b=vb->vd->backend_state;
  219. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  220. vorbis_info_mapping0 *info=look->map;
  221. vorbis_info_mode *mode=look->mode;
  222. vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  223. int n=vb->pcmend;
  224. int i,j;
  225. float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  226. float **pcmbundle=alloca(sizeof(float *)*vi->channels);
  227. int *zerobundle=alloca(sizeof(int)*vi->channels);
  228. int *nonzero=alloca(sizeof(int)*vi->channels);
  229. float *work=_vorbis_block_alloc(vb,n*sizeof(float));
  230. float newmax=vbi->ampmax;
  231. for(i=0;i<vi->channels;i++){
  232. float scale=4.f/n;
  233. int submap=info->chmuxlist[i];
  234. float ret;
  235. /* the following makes things clearer to *me* anyway */
  236. float *pcm =vb->pcm[i];
  237. float *mdct =pcm;
  238. float *logmdct =pcm+n/2;
  239. float *res =pcm;
  240. float *codedflr=pcm+n/2;
  241. float *fft =work;
  242. float *logfft =work;
  243. float *logmax =work;
  244. float *logmask =work+n/2;
  245. /* window the PCM data */
  246. for(j=0;j<n;j++)
  247. fft[j]=pcm[j]*=window[j];
  248. /* transform the PCM data */
  249. /* only MDCT right now.... */
  250. mdct_forward(b->transform[vb->W][0],pcm,pcm);
  251. for(j=0;j<n/2;j++)
  252. logmdct[j]=todB(mdct+j);
  253. /* FFT yields more accurate tonal estimation (not phase sensitive) */
  254. drft_forward(&look->fft_look,fft);
  255. fft[0]*=scale;
  256. fft[0]=todB(fft);
  257. for(j=1;j<n-1;j+=2){
  258. float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
  259. logfft[(j+1)>>1]=todB(&temp);
  260. }
  261. _analysis_output("fft",seq,logfft,n/2,0,0);
  262. _analysis_output("mdct",seq,logmdct,n/2,0,0);
  263. /* perform psychoacoustics; do masking */
  264. ret=_vp_compute_mask(look->psy_look+submap,
  265. logfft, /* -> logmax */
  266. logmdct,
  267. logmask,
  268. vbi->ampmax);
  269. if(ret>newmax)newmax=ret;
  270. _analysis_output("mask",seq,logmask,n/2,0,0);
  271. /* perform floor encoding */
  272. nonzero[i]=look->floor_func[submap]->
  273. forward(vb,look->floor_look[submap],
  274. mdct,
  275. logmdct,
  276. logmask,
  277. logmax,
  278. res,
  279. codedflr);
  280. /*for(j=0;j<n/2;j++)
  281. if(fabs(vb->pcm[i][j]>200))
  282. fprintf(stderr,"%ld ",seq);*/
  283. _analysis_output("res",seq-vi->channels+j,vb->pcm[i],n,0,0);
  284. _analysis_output("codedflr",seq++,codedflr,n/2,0,1);
  285. }
  286. vbi->ampmax=newmax;
  287. /* channel coupling */
  288. for(i=0;i<info->coupling_steps;i++){
  289. if(nonzero[info->coupling_mag[i]] ||
  290. nonzero[info->coupling_ang[i]]){
  291. float *pcmM=vb->pcm[info->coupling_mag[i]];
  292. float *pcmA=vb->pcm[info->coupling_ang[i]];
  293. /* +-
  294. B
  295. | A-B
  296. -4 -3 -2 -1 0
  297. |
  298. 3 | 1
  299. |
  300. -+ 2-----+-----2----A ++
  301. |
  302. 1 | 3
  303. |
  304. 0 -1 -2 -3 -4
  305. B-A |
  306. --
  307. */
  308. nonzero[info->coupling_mag[i]]=1;
  309. nonzero[info->coupling_ang[i]]=1;
  310. for(j=n/2-1;j>=0;j--){
  311. float A=rint(pcmM[j]);
  312. float B=rint(pcmA[j]);
  313. float mag;
  314. float ang;
  315. if(fabs(A)>fabs(B)){
  316. mag=A;
  317. if(A>0)
  318. ang=A-B;
  319. else
  320. ang=B-A;
  321. }else{
  322. mag=B;
  323. if(B>0)
  324. ang=A-B;
  325. else
  326. ang=B-A;
  327. }
  328. /*if(fabs(mag)<3.5f)
  329. ang=rint(ang/(mag*2.f))*mag*2.f;*/
  330. /*if(fabs(mag)<1.5)
  331. ang=0;
  332. if(j>(n*3/16))
  333. ang=0;
  334. if(ang>=fabs(mag*2))ang=-fabs(mag*2);*/
  335. pcmM[j]=mag;
  336. pcmA[j]=ang;
  337. }
  338. }
  339. }
  340. /* perform residue encoding with residue mapping; this is
  341. multiplexed. All the channels belonging to one submap are
  342. encoded (values interleaved), then the next submap, etc */
  343. for(i=0;i<info->submaps;i++){
  344. int ch_in_bundle=0;
  345. for(j=0;j<vi->channels;j++){
  346. if(info->chmuxlist[j]==i){
  347. if(nonzero[j])
  348. zerobundle[ch_in_bundle]=1;
  349. else
  350. zerobundle[ch_in_bundle]=0;
  351. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  352. }
  353. }
  354. look->residue_func[i]->forward(vb,look->residue_look[i],
  355. pcmbundle,zerobundle,ch_in_bundle);
  356. }
  357. look->lastframe=vb->sequence;
  358. return(0);
  359. }
  360. static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
  361. vorbis_dsp_state *vd=vb->vd;
  362. vorbis_info *vi=vd->vi;
  363. codec_setup_info *ci=vi->codec_setup;
  364. backend_lookup_state *b=vd->backend_state;
  365. vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
  366. vorbis_info_mapping0 *info=look->map;
  367. vorbis_info_mode *mode=look->mode;
  368. int i,j;
  369. long n=vb->pcmend=ci->blocksizes[vb->W];
  370. float *window=b->window[vb->W][vb->lW][vb->nW][mode->windowtype];
  371. float **pcmbundle=alloca(sizeof(float *)*vi->channels);
  372. int *zerobundle=alloca(sizeof(int)*vi->channels);
  373. int *nonzero =alloca(sizeof(int)*vi->channels);
  374. void **floormemo=alloca(sizeof(void *)*vi->channels);
  375. /* time domain information decode (note that applying the
  376. information would have to happen later; we'll probably add a
  377. function entry to the harness for that later */
  378. /* NOT IMPLEMENTED */
  379. /* recover the spectral envelope; store it in the PCM vector for now */
  380. for(i=0;i<vi->channels;i++){
  381. int submap=info->chmuxlist[i];
  382. floormemo[i]=look->floor_func[submap]->
  383. inverse1(vb,look->floor_look[submap]);
  384. if(floormemo[i])
  385. nonzero[i]=1;
  386. else
  387. nonzero[i]=0;
  388. memset(vb->pcm[i],0,sizeof(float)*n/2);
  389. }
  390. /* channel coupling can 'dirty' the nonzero listing */
  391. for(i=0;i<info->coupling_steps;i++){
  392. if(nonzero[info->coupling_mag[i]] ||
  393. nonzero[info->coupling_ang[i]]){
  394. nonzero[info->coupling_mag[i]]=1;
  395. nonzero[info->coupling_ang[i]]=1;
  396. }
  397. }
  398. /* recover the residue into our working vectors */
  399. for(i=0;i<info->submaps;i++){
  400. int ch_in_bundle=0;
  401. for(j=0;j<vi->channels;j++){
  402. if(info->chmuxlist[j]==i){
  403. if(nonzero[j])
  404. zerobundle[ch_in_bundle]=1;
  405. else
  406. zerobundle[ch_in_bundle]=0;
  407. pcmbundle[ch_in_bundle++]=vb->pcm[j];
  408. }
  409. }
  410. look->residue_func[i]->inverse(vb,look->residue_look[i],
  411. pcmbundle,zerobundle,ch_in_bundle);
  412. }
  413. /* channel coupling */
  414. for(i=info->coupling_steps-1;i>=0;i--){
  415. float *pcmM=vb->pcm[info->coupling_mag[i]];
  416. float *pcmA=vb->pcm[info->coupling_ang[i]];
  417. for(j=0;j<n/2;j++){
  418. float mag=pcmM[j];
  419. float ang=pcmA[j];
  420. if(mag>0)
  421. if(ang>0){
  422. pcmM[j]=mag;
  423. pcmA[j]=mag-ang;
  424. }else{
  425. pcmA[j]=mag;
  426. pcmM[j]=mag+ang;
  427. }
  428. else
  429. if(ang>0){
  430. pcmM[j]=mag;
  431. pcmA[j]=mag+ang;
  432. }else{
  433. pcmA[j]=mag;
  434. pcmM[j]=mag-ang;
  435. }
  436. }
  437. }
  438. /* compute and apply spectral envelope */
  439. for(i=0;i<vi->channels;i++){
  440. float *pcm=vb->pcm[i];
  441. int submap=info->chmuxlist[i];
  442. look->floor_func[submap]->
  443. inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
  444. }
  445. /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
  446. /* only MDCT right now.... */
  447. for(i=0;i<vi->channels;i++){
  448. float *pcm=vb->pcm[i];
  449. _analysis_output("out",seq+i,pcm,n/2,0,1);
  450. mdct_backward(b->transform[vb->W][0],pcm,pcm);
  451. }
  452. /* window the data */
  453. for(i=0;i<vi->channels;i++){
  454. float *pcm=vb->pcm[i];
  455. if(nonzero[i])
  456. for(j=0;j<n;j++)
  457. pcm[j]*=window[j];
  458. else
  459. for(j=0;j<n;j++)
  460. pcm[j]=0.f;
  461. _analysis_output("final",seq++,pcm,n,0,0);
  462. }
  463. /* now apply the decoded post-window time information */
  464. /* NOT IMPLEMENTED */
  465. /* all done! */
  466. return(0);
  467. }
  468. /* export hooks */
  469. vorbis_func_mapping mapping0_exportbundle={
  470. &mapping0_pack,&mapping0_unpack,&mapping0_look,&mapping0_copy_info,
  471. &mapping0_free_info,&mapping0_free_look,&mapping0_forward,&mapping0_inverse
  472. };