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.6 2000/01/28 15:25:09 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_info_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 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],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);
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);
218 int **pcmaux=_vorbis_block_alloc(vb,vi->channels*sizeof(int *));
220 for(i=0;i<vi->channels;i++){
221 double *pcm=vb->pcm[i];
222 int submap=map->chmuxlist[i];
224 pcmaux[i]=_vorbis_block_alloc(vb,n/2*sizeof(int));
226 /* perform psychoacoustics; takes PCM vector;
227 returns two curves: the desired transform floor and the masking curve */
228 memset(floor,0,sizeof(double)*n/2);
229 memset(mask,0,sizeof(double)*n/2);
230 _vp_mask_floor(look->psy_look+submap,pcm,mask,floor);
232 /* perform floor encoding; takes transform floor, returns decoded floor */
233 look->floor_func[submap]->
234 forward(vb,look->floor_look[submap],floor,decfloor);
236 /* no iterative residue/floor tuning at the moment */
238 /* perform residue prequantization. Do it now so we have all
239 the values if we need them */
240 _vp_quantize(look->psy_look+submap,pcm,mask,decfloor,pcmaux[i]);
244 /* perform residue encoding with residue mapping; this is
245 multiplexed. All the channels belonging to one submap are
246 encoded (values interleaved), then the next submap, etc */
248 for(i=0;i<map->submaps;i++){
250 for(j=0;j<vi->channels;j++){
251 if(map->chmuxlist[j]==i)
252 pcmbundle[ch_in_bundle]=vb->pcm[j];
253 auxbundle[ch_in_bundle++]=pcmaux[j];
256 look->residue_func[i]->forward(vb,look->residue_look[i],pcmbundle,auxbundle,
264 static int inverse(vorbis_block *vb,vorbis_look_mapping *l){
265 vorbis_dsp_state *vd=vb->vd;
266 vorbis_info *vi=vd->vi;
267 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
268 vorbis_info_mapping0 *map=look->map;
269 vorbis_info_mode *mode=look->mode;
271 long n=vb->pcmend=vi->blocksizes[vb->W];
273 double *window=vd->window[vb->W][vb->lW][vb->nW][mode->windowtype];
274 double **pcmbundle=alloca(sizeof(double *)*vi->channels);
276 /* time domain information decode (note that applying the
277 information would have to happen later; we'll probably add a
278 function entry to the harness for that later */
279 /* NOT IMPLEMENTED */
281 /* recover the spectral envelope; store it in the PCM vector for now */
282 for(i=0;i<vi->channels;i++){
283 double *pcm=vb->pcm[i];
284 int submap=map->chmuxlist[i];
285 look->floor_func[submap]->inverse(vb,look->floor_look[submap],pcm);
288 /* recover the residue, apply directly to the spectral envelope */
289 for(i=0;i<map->submaps;i++){
291 for(j=0;j<vi->channels;j++){
292 if(map->chmuxlist[j]==i)
293 pcmbundle[ch_in_bundle++]=vb->pcm[j];
296 look->residue_func[i]->inverse(vb,look->residue_look[i],pcmbundle,ch_in_bundle);
299 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
300 /* only MDCT right now.... */
301 for(i=0;i<vi->channels;i++){
302 double *pcm=vb->pcm[i];
303 mdct_backward(vd->transform[vb->W][0],pcm,pcm);
306 /* now apply the decoded pre-window time information */
307 /* NOT IMPLEMENTED */
309 /* window the data */
310 for(i=0;i<vi->channels;i++){
311 double *pcm=vb->pcm[i];
316 /* now apply the decoded post-window time information */
317 /* NOT IMPLEMENTED */
324 vorbis_func_mapping mapping0_exportbundle={
325 &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse