1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: channel mapping 0 implementation
15 last mod: $Id: mapping0.c,v 1.9 2000/02/12 08:33:06 xiphmont Exp $
17 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "vorbis/backends.h"
25 #include "bookinternal.h"
30 /* simplistic, wasteful way of doing this (unique lookup for each
31 mode/submapping); there should be a central repository for
32 identical lookups. That will require minor work, so I'm putting it
35 Why a lookup for each backend in a given mode? Because the
36 blocksize is set by the mode, and low backend lookups may require
37 parameters from other areas of the mode/mapping */
40 vorbis_info_mode *mode;
41 vorbis_info_mapping0 *map;
43 vorbis_look_time **time_look;
44 vorbis_look_floor **floor_look;
45 vorbis_look_residue **residue_look;
46 vorbis_look_psy *psy_look;
48 vorbis_func_time **time_func;
49 vorbis_func_floor **floor_func;
50 vorbis_func_residue **residue_func;
52 } vorbis_look_mapping0;
54 static void free_info(vorbis_info_mapping *i){
56 memset(i,0,sizeof(vorbis_info_mapping0));
61 static void free_look(vorbis_look_mapping *look){
63 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
65 for(i=0;i<l->map->submaps;i++){
66 l->time_func[i]->free_look(l->time_look[i]);
67 l->floor_func[i]->free_look(l->floor_look[i]);
68 l->residue_func[i]->free_look(l->residue_look[i]);
69 if(l->psy_look)_vp_psy_clear(l->psy_look+i);
73 free(l->residue_func);
76 free(l->residue_look);
77 if(l->psy_look)free(l->psy_look);
78 memset(l,0,sizeof(vorbis_look_mapping0));
83 static vorbis_look_mapping *look(vorbis_info *vi,vorbis_info_mode *vm,
84 vorbis_info_mapping *m){
86 vorbis_look_mapping0 *ret=calloc(1,sizeof(vorbis_look_mapping0));
87 vorbis_info_mapping0 *map=ret->map=(vorbis_info_mapping0 *)m;
90 ret->time_look=calloc(map->submaps,sizeof(vorbis_look_time *));
91 ret->floor_look=calloc(map->submaps,sizeof(vorbis_look_floor *));
92 ret->residue_look=calloc(map->submaps,sizeof(vorbis_look_residue *));
93 if(vi->psys)ret->psy_look=calloc(map->submaps,sizeof(vorbis_look_psy));
95 ret->time_func=calloc(map->submaps,sizeof(vorbis_func_time *));
96 ret->floor_func=calloc(map->submaps,sizeof(vorbis_func_floor *));
97 ret->residue_func=calloc(map->submaps,sizeof(vorbis_func_residue *));
99 for(i=0;i<map->submaps;i++){
100 int timenum=map->timesubmap[i];
101 int floornum=map->floorsubmap[i];
102 int resnum=map->residuesubmap[i];
104 ret->time_func[i]=_time_P[vi->time_type[timenum]];
105 ret->time_look[i]=ret->time_func[i]->
106 look(vi,vm,vi->time_param[timenum]);
107 ret->floor_func[i]=_floor_P[vi->floor_type[floornum]];
108 ret->floor_look[i]=ret->floor_func[i]->
109 look(vi,vm,vi->floor_param[floornum]);
110 ret->residue_func[i]=_residue_P[vi->residue_type[resnum]];
111 ret->residue_look[i]=ret->residue_func[i]->
112 look(vi,vm,vi->residue_param[resnum]);
115 int psynum=map->psysubmap[i];
116 _vp_psy_init(ret->psy_look+i,vi->psy_param[psynum],
117 vi->blocksizes[vm->blockflag]/2,vi->rate);
124 static void pack(vorbis_info *vi,vorbis_info_mapping *vm,oggpack_buffer *opb){
126 vorbis_info_mapping0 *d=(vorbis_info_mapping0 *)vm;
128 _oggpack_write(opb,d->submaps-1,4);
129 /* we don't write the channel submappings if we only have one... */
131 for(i=0;i<vi->channels;i++)
132 _oggpack_write(opb,d->chmuxlist[i],4);
134 for(i=0;i<d->submaps;i++){
135 _oggpack_write(opb,d->timesubmap[i],8);
136 _oggpack_write(opb,d->floorsubmap[i],8);
137 _oggpack_write(opb,d->residuesubmap[i],8);
141 /* also responsible for range checking */
142 static vorbis_info_mapping *unpack(vorbis_info *vi,oggpack_buffer *opb){
144 vorbis_info_mapping0 *d=calloc(1,sizeof(vorbis_info_mapping0));
145 memset(d,0,sizeof(vorbis_info_mapping0));
147 d->submaps=_oggpack_read(opb,4)+1;
150 for(i=0;i<vi->channels;i++){
151 d->chmuxlist[i]=_oggpack_read(opb,4);
152 if(d->chmuxlist[i]>=d->submaps)goto err_out;
155 for(i=0;i<d->submaps;i++){
156 d->timesubmap[i]=_oggpack_read(opb,8);
157 if(d->timesubmap[i]>=vi->times)goto err_out;
158 d->floorsubmap[i]=_oggpack_read(opb,8);
159 if(d->floorsubmap[i]>=vi->floors)goto err_out;
160 d->residuesubmap[i]=_oggpack_read(opb,8);
161 if(d->residuesubmap[i]>=vi->residues)goto err_out;
175 #include "envelope.h"
179 #include "spectrum.h"
181 /* no time mapping implementation for now */
182 static int forward(vorbis_block *vb,vorbis_look_mapping *l){
183 vorbis_dsp_state *vd=vb->vd;
184 vorbis_info *vi=vd->vi;
185 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
186 vorbis_info_mapping0 *map=look->map;
187 vorbis_info_mode *mode=look->mode;
190 double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
192 double **pcmbundle=alloca(sizeof(double *)*vi->channels);
193 int *nonzero=alloca(sizeof(int)*vi->channels);
195 /* time domain pre-window: NONE IMPLEMENTED */
197 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
199 for(i=0;i<vi->channels;i++){
200 double *pcm=vb->pcm[i];
205 /* time-domain post-window: NONE IMPLEMENTED */
207 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
208 /* only MDCT right now.... */
209 for(i=0;i<vi->channels;i++){
210 double *pcm=vb->pcm[i];
211 mdct_forward(vd->transform[vb->W][0],pcm,pcm);
215 double *decfloor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
216 double *floor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
217 double *mask=_vorbis_block_alloc(vb,n*sizeof(double)/2);
219 for(i=0;i<vi->channels;i++){
220 double *pcm=vb->pcm[i];
221 int submap=map->chmuxlist[i];
223 /* perform psychoacoustics; takes PCM vector;
224 returns two curves: the desired transform floor and the masking curve */
225 memset(floor,0,sizeof(double)*n/2);
226 memset(mask,0,sizeof(double)*n/2);
227 _vp_mask_floor(look->psy_look+submap,pcm,floor,0);
228 _vp_mask_floor(look->psy_look+submap,pcm,mask,1);
230 /* perform floor encoding; takes transform floor, returns decoded floor */
231 nonzero[i]=look->floor_func[submap]->
232 forward(vb,look->floor_look[submap],floor,decfloor);
234 /* no iterative residue/floor tuning at the moment */
241 sprintf(buffer,"masked_%d.vqd",vb->mode);
242 of=fopen(buffer,"a");
244 fprintf(of,"%g, ",pcm[i]/mask[i]);
247 sprintf(buffer,"floored_%d.vqd",vb->mode);
248 of=fopen(buffer,"a");
250 fprintf(of,"%g, ",pcm[i]/floor[i]);
258 /* perform residue encoding with residue mapping; this is
259 multiplexed. All the channels belonging to one submap are
260 encoded (values interleaved), then the next submap, etc */
262 for(i=0;i<map->submaps;i++){
264 for(j=0;j<vi->channels;j++){
265 if(map->chmuxlist[j]==i && nonzero[j]==1){
266 pcmbundle[ch_in_bundle]=vb->pcm[j];
270 look->residue_func[i]->forward(vb,look->residue_look[i],
271 pcmbundle,ch_in_bundle);
278 static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
279 vorbis_dsp_state *vd=vb->vd;
280 vorbis_info *vi=vd->vi;
281 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
282 vorbis_info_mapping0 *map=look->map;
283 vorbis_info_mode *mode=look->mode;
285 long n=vb->pcmend=vi->blocksizes[vb->W];
287 double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
288 double **pcmbundle=alloca(sizeof(double *)*vi->channels);
289 int *nonzero=alloca(sizeof(int)*vi->channels);
291 /* time domain information decode (note that applying the
292 information would have to happen later; we'll probably add a
293 function entry to the harness for that later */
294 /* NOT IMPLEMENTED */
296 /* recover the spectral envelope; store it in the PCM vector for now */
297 for(i=0;i<vi->channels;i++){
298 double *pcm=vb->pcm[i];
299 int submap=map->chmuxlist[i];
300 nonzero[i]=look->floor_func[submap]->
301 inverse(vb,look->floor_look[submap],pcm);
304 /* recover the residue, apply directly to the spectral envelope */
306 for(i=0;i<map->submaps;i++){
308 for(j=0;j<vi->channels;j++){
309 if(map->chmuxlist[j]==i && nonzero[j])
310 pcmbundle[ch_in_bundle++]=vb->pcm[j];
313 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
316 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
317 /* only MDCT right now.... */
318 for(i=0;i<vi->channels;i++){
319 double *pcm=vb->pcm[i];
320 mdct_backward(vd->transform[vb->W][0],pcm,pcm);
323 /* now apply the decoded pre-window time information */
324 /* NOT IMPLEMENTED */
326 /* window the data */
327 for(i=0;i<vi->channels;i++){
328 double *pcm=vb->pcm[i];
337 /* now apply the decoded post-window time information */
338 /* NOT IMPLEMENTED */
345 vorbis_func_mapping mapping0_exportbundle={
346 &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse