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.8 2000/02/09 22:04:14 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 **auxbundle=alloca(sizeof(int *)*vi->channels);
194 int *nonzero=alloca(sizeof(int)*vi->channels);
196 /* time domain pre-window: NONE IMPLEMENTED */
198 /* window the PCM data: takes PCM vector, vb; modifies PCM vector */
200 for(i=0;i<vi->channels;i++){
201 double *pcm=vb->pcm[i];
206 /* time-domain post-window: NONE IMPLEMENTED */
208 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
209 /* only MDCT right now.... */
210 for(i=0;i<vi->channels;i++){
211 double *pcm=vb->pcm[i];
212 mdct_forward(vd->transform[vb->W][0],pcm,pcm);
216 double *decfloor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
217 double *floor=_vorbis_block_alloc(vb,n*sizeof(double)/2);
218 double *mask=_vorbis_block_alloc(vb,n*sizeof(double)/2);
219 int **pcmaux=_vorbis_block_alloc(vb,vi->channels*sizeof(int *));
221 for(i=0;i<vi->channels;i++){
222 double *pcm=vb->pcm[i];
223 int submap=map->chmuxlist[i];
225 pcmaux[i]=_vorbis_block_alloc(vb,n/2*sizeof(int));
227 /* perform psychoacoustics; takes PCM vector;
228 returns two curves: the desired transform floor and the masking curve */
229 memset(floor,0,sizeof(double)*n/2);
230 memset(mask,0,sizeof(double)*n/2);
231 _vp_mask_floor(look->psy_look+submap,pcm,mask,floor);
233 /* perform floor encoding; takes transform floor, returns decoded floor */
234 nonzero[i]=look->floor_func[submap]->
235 forward(vb,look->floor_look[submap],floor,decfloor);
237 /* no iterative residue/floor tuning at the moment */
239 /* perform residue prequantization. Do it now so we have all
240 the values if we need them */
241 _vp_quantize(look->psy_look+submap,pcm,mask,decfloor,pcmaux[i],0,n/2,n/2);
245 /* perform residue encoding with residue mapping; this is
246 multiplexed. All the channels belonging to one submap are
247 encoded (values interleaved), then the next submap, etc */
249 for(i=0;i<map->submaps;i++){
251 for(j=0;j<vi->channels;j++){
252 if(map->chmuxlist[j]==i && nonzero[j]==1){
253 pcmbundle[ch_in_bundle]=vb->pcm[j];
254 auxbundle[ch_in_bundle++]=pcmaux[j];
258 look->residue_func[i]->forward(vb,look->residue_look[i],pcmbundle,auxbundle,
266 static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
267 vorbis_dsp_state *vd=vb->vd;
268 vorbis_info *vi=vd->vi;
269 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
270 vorbis_info_mapping0 *map=look->map;
271 vorbis_info_mode *mode=look->mode;
273 long n=vb->pcmend=vi->blocksizes[vb->W];
275 double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
276 double **pcmbundle=alloca(sizeof(double *)*vi->channels);
277 int *nonzero=alloca(sizeof(int)*vi->channels);
279 /* time domain information decode (note that applying the
280 information would have to happen later; we'll probably add a
281 function entry to the harness for that later */
282 /* 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=map->chmuxlist[i];
288 nonzero[i]=look->floor_func[submap]->
289 inverse(vb,look->floor_look[submap],pcm);
292 /* recover the residue, apply directly to the spectral envelope */
294 for(i=0;i<map->submaps;i++){
296 for(j=0;j<vi->channels;j++){
297 if(map->chmuxlist[j]==i && nonzero[j])
298 pcmbundle[ch_in_bundle++]=vb->pcm[j];
301 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
304 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
305 /* only MDCT right now.... */
306 for(i=0;i<vi->channels;i++){
307 double *pcm=vb->pcm[i];
308 mdct_backward(vd->transform[vb->W][0],pcm,pcm);
311 /* now apply the decoded pre-window time information */
312 /* NOT IMPLEMENTED */
314 /* window the data */
315 for(i=0;i<vi->channels;i++){
316 double *pcm=vb->pcm[i];
325 /* now apply the decoded post-window time information */
326 /* NOT IMPLEMENTED */
333 vorbis_func_mapping mapping0_exportbundle={
334 &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse