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