comparison tremor/mapping0.c @ 14280:8631a3803289

internal Tremor decoder for Ogg/Vorbis
author henry
date Thu, 30 Dec 2004 12:11:32 +0000
parents
children cd6b211be811
comparison
equal deleted inserted replaced
14279:b4b202086260 14280:8631a3803289
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
4 * *
5 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
6 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
7 * ALL REDISTRIBUTION RIGHTS RESERVED. *
8 * *
9 ********************************************************************
10
11 function: channel mapping 0 implementation
12
13 ********************************************************************/
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <math.h>
19 #include "ogg.h"
20 #include "ivorbiscodec.h"
21 #include "mdct.h"
22 #include "codec_internal.h"
23 #include "codebook.h"
24 #include "window.h"
25 #include "registry.h"
26 #include "misc.h"
27
28 /* simplistic, wasteful way of doing this (unique lookup for each
29 mode/submapping); there should be a central repository for
30 identical lookups. That will require minor work, so I'm putting it
31 off as low priority.
32
33 Why a lookup for each backend in a given mode? Because the
34 blocksize is set by the mode, and low backend lookups may require
35 parameters from other areas of the mode/mapping */
36
37 typedef struct {
38 vorbis_info_mode *mode;
39 vorbis_info_mapping0 *map;
40
41 vorbis_look_floor **floor_look;
42
43 vorbis_look_residue **residue_look;
44
45 vorbis_func_floor **floor_func;
46 vorbis_func_residue **residue_func;
47
48 int ch;
49 long lastframe; /* if a different mode is called, we need to
50 invalidate decay */
51 } vorbis_look_mapping0;
52
53 static void mapping0_free_info(vorbis_info_mapping *i){
54 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
55 if(info){
56 memset(info,0,sizeof(*info));
57 _ogg_free(info);
58 }
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
66 for(i=0;i<l->map->submaps;i++){
67 l->floor_func[i]->free_look(l->floor_look[i]);
68 l->residue_func[i]->free_look(l->residue_look[i]);
69 }
70
71 _ogg_free(l->floor_func);
72 _ogg_free(l->residue_func);
73 _ogg_free(l->floor_look);
74 _ogg_free(l->residue_look);
75 memset(l,0,sizeof(*l));
76 _ogg_free(l);
77 }
78 }
79
80 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
81 vorbis_info_mapping *m){
82 int i;
83 vorbis_info *vi=vd->vi;
84 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
85 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)_ogg_calloc(1,sizeof(*look));
86 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
87 look->mode=vm;
88
89 look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
90
91 look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
92
93 look->floor_func=(vorbis_func_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_func));
94 look->residue_func=(vorbis_func_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_func));
95
96 for(i=0;i<info->submaps;i++){
97 int floornum=info->floorsubmap[i];
98 int resnum=info->residuesubmap[i];
99
100 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
101 look->floor_look[i]=look->floor_func[i]->
102 look(vd,vm,ci->floor_param[floornum]);
103 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
104 look->residue_look[i]=look->residue_func[i]->
105 look(vd,vm,ci->residue_param[resnum]);
106
107 }
108
109 look->ch=vi->channels;
110
111 return(look);
112 }
113
114 static int ilog(unsigned int v){
115 int ret=0;
116 if(v)--v;
117 while(v){
118 ret++;
119 v>>=1;
120 }
121 return(ret);
122 }
123
124 /* also responsible for range checking */
125 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
126 int i;
127 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info));
128 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
129 memset(info,0,sizeof(*info));
130
131 if(oggpack_read(opb,1))
132 info->submaps=oggpack_read(opb,4)+1;
133 else
134 info->submaps=1;
135
136 if(oggpack_read(opb,1)){
137 info->coupling_steps=oggpack_read(opb,8)+1;
138
139 for(i=0;i<info->coupling_steps;i++){
140 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
141 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
142
143 if(testM<0 ||
144 testA<0 ||
145 testM==testA ||
146 testM>=vi->channels ||
147 testA>=vi->channels) goto err_out;
148 }
149
150 }
151
152 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
153
154 if(info->submaps>1){
155 for(i=0;i<vi->channels;i++){
156 info->chmuxlist[i]=oggpack_read(opb,4);
157 if(info->chmuxlist[i]>=info->submaps)goto err_out;
158 }
159 }
160 for(i=0;i<info->submaps;i++){
161 int temp=oggpack_read(opb,8);
162 if(temp>=ci->times)goto err_out;
163 info->floorsubmap[i]=oggpack_read(opb,8);
164 if(info->floorsubmap[i]>=ci->floors)goto err_out;
165 info->residuesubmap[i]=oggpack_read(opb,8);
166 if(info->residuesubmap[i]>=ci->residues)goto err_out;
167 }
168
169 return info;
170
171 err_out:
172 mapping0_free_info(info);
173 return(NULL);
174 }
175
176 static int seq=0;
177 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
178 vorbis_dsp_state *vd=vb->vd;
179 vorbis_info *vi=vd->vi;
180 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
181 backend_lookup_state *b=(backend_lookup_state *)vd->backend_state;
182 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
183 vorbis_info_mapping0 *info=look->map;
184
185 int i,j;
186 long n=vb->pcmend=ci->blocksizes[vb->W];
187
188 ogg_int32_t **pcmbundle=(ogg_int32_t **)alloca(sizeof(*pcmbundle)*vi->channels);
189 int *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels);
190
191 int *nonzero =(int *)alloca(sizeof(*nonzero)*vi->channels);
192 void **floormemo=(void **)alloca(sizeof(*floormemo)*vi->channels);
193
194 /* time domain information decode (note that applying the
195 information would have to happen later; we'll probably add a
196 function entry to the harness for that later */
197 /* NOT IMPLEMENTED */
198
199 /* recover the spectral envelope; store it in the PCM vector for now */
200 for(i=0;i<vi->channels;i++){
201 int submap=info->chmuxlist[i];
202 floormemo[i]=look->floor_func[submap]->
203 inverse1(vb,look->floor_look[submap]);
204 if(floormemo[i])
205 nonzero[i]=1;
206 else
207 nonzero[i]=0;
208 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
209 }
210
211 /* channel coupling can 'dirty' the nonzero listing */
212 for(i=0;i<info->coupling_steps;i++){
213 if(nonzero[info->coupling_mag[i]] ||
214 nonzero[info->coupling_ang[i]]){
215 nonzero[info->coupling_mag[i]]=1;
216 nonzero[info->coupling_ang[i]]=1;
217 }
218 }
219
220 /* recover the residue into our working vectors */
221 for(i=0;i<info->submaps;i++){
222 int ch_in_bundle=0;
223 for(j=0;j<vi->channels;j++){
224 if(info->chmuxlist[j]==i){
225 if(nonzero[j])
226 zerobundle[ch_in_bundle]=1;
227 else
228 zerobundle[ch_in_bundle]=0;
229 pcmbundle[ch_in_bundle++]=vb->pcm[j];
230 }
231 }
232
233 look->residue_func[i]->inverse(vb,look->residue_look[i],
234 pcmbundle,zerobundle,ch_in_bundle);
235 }
236
237 //for(j=0;j<vi->channels;j++)
238 //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
239
240
241 /* channel coupling */
242 for(i=info->coupling_steps-1;i>=0;i--){
243 ogg_int32_t *pcmM=vb->pcm[info->coupling_mag[i]];
244 ogg_int32_t *pcmA=vb->pcm[info->coupling_ang[i]];
245
246 for(j=0;j<n/2;j++){
247 ogg_int32_t mag=pcmM[j];
248 ogg_int32_t ang=pcmA[j];
249
250 if(mag>0)
251 if(ang>0){
252 pcmM[j]=mag;
253 pcmA[j]=mag-ang;
254 }else{
255 pcmA[j]=mag;
256 pcmM[j]=mag+ang;
257 }
258 else
259 if(ang>0){
260 pcmM[j]=mag;
261 pcmA[j]=mag+ang;
262 }else{
263 pcmA[j]=mag;
264 pcmM[j]=mag-ang;
265 }
266 }
267 }
268
269 //for(j=0;j<vi->channels;j++)
270 //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
271
272 /* compute and apply spectral envelope */
273 for(i=0;i<vi->channels;i++){
274 ogg_int32_t *pcm=vb->pcm[i];
275 int submap=info->chmuxlist[i];
276 look->floor_func[submap]->
277 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
278 }
279
280 //for(j=0;j<vi->channels;j++)
281 //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
282
283 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
284 /* only MDCT right now.... */
285 for(i=0;i<vi->channels;i++){
286 ogg_int32_t *pcm=vb->pcm[i];
287 mdct_backward(n,pcm,pcm);
288 }
289
290 //for(j=0;j<vi->channels;j++)
291 //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
292
293 /* window the data */
294 for(i=0;i<vi->channels;i++){
295 ogg_int32_t *pcm=vb->pcm[i];
296 if(nonzero[i])
297 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
298 else
299 for(j=0;j<n;j++)
300 pcm[j]=0;
301
302 }
303
304 //for(j=0;j<vi->channels;j++)
305 //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
306
307 seq+=vi->channels;
308 /* all done! */
309 return(0);
310 }
311
312 /* export hooks */
313 vorbis_func_mapping mapping0_exportbundle={
314 &mapping0_unpack,
315 &mapping0_look,
316 &mapping0_free_info,
317 &mapping0_free_look,
318 &mapping0_inverse
319 };