summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libtremor/mapping0.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libtremor/mapping0.c')
-rw-r--r--lib/rbcodec/codecs/libtremor/mapping0.c416
1 files changed, 416 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libtremor/mapping0.c b/lib/rbcodec/codecs/libtremor/mapping0.c
new file mode 100644
index 0000000000..084d5e076d
--- /dev/null
+++ b/lib/rbcodec/codecs/libtremor/mapping0.c
@@ -0,0 +1,416 @@
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 "codeclib.h"
25#include "codec_internal.h"
26#include "codebook.h"
27#include "window.h"
28#include "registry.h"
29#include "misc.h"
30#include <codecs/lib/codeclib.h>
31
32/* simplistic, wasteful way of doing this (unique lookup for each
33 mode/submapping); there should be a central repository for
34 identical lookups. That will require minor work, so I'm putting it
35 off as low priority.
36
37 Why a lookup for each backend in a given mode? Because the
38 blocksize is set by the mode, and low backend lookups may require
39 parameters from other areas of the mode/mapping */
40
41typedef struct {
42 vorbis_info_mode *mode;
43 vorbis_info_mapping0 *map;
44
45 vorbis_look_floor **floor_look;
46
47 vorbis_look_residue **residue_look;
48
49 vorbis_func_floor **floor_func;
50 vorbis_func_residue **residue_func;
51
52 int ch;
53 long lastframe; /* if a different mode is called, we need to
54 invalidate decay */
55} vorbis_look_mapping0;
56
57static void mapping0_free_info(vorbis_info_mapping *i){
58 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
59 if(info){
60 memset(info,0,sizeof(*info));
61 _ogg_free(info);
62 }
63}
64
65static void mapping0_free_look(vorbis_look_mapping *look){
66 int i;
67 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
68 if(l){
69
70 for(i=0;i<l->map->submaps;i++){
71 l->floor_func[i]->free_look(l->floor_look[i]);
72 l->residue_func[i]->free_look(l->residue_look[i]);
73 }
74
75 _ogg_free(l->floor_func);
76 _ogg_free(l->residue_func);
77 _ogg_free(l->floor_look);
78 _ogg_free(l->residue_look);
79 memset(l,0,sizeof(*l));
80 _ogg_free(l);
81 }
82}
83
84static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
85 vorbis_info_mapping *m){
86 int i;
87 vorbis_info *vi=vd->vi;
88 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
89 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)_ogg_calloc(1,sizeof(*look));
90 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
91 look->mode=vm;
92
93 look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
94
95 look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
96
97 look->floor_func=(vorbis_func_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_func));
98 look->residue_func=(vorbis_func_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_func));
99
100 for(i=0;i<info->submaps;i++){
101 int floornum=info->floorsubmap[i];
102 int resnum=info->residuesubmap[i];
103
104 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
105 look->floor_look[i]=look->floor_func[i]->
106 look(vd,vm,ci->floor_param[floornum]);
107 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
108 look->residue_look[i]=look->residue_func[i]->
109 look(vd,vm,ci->residue_param[resnum]);
110
111 }
112
113 look->ch=vi->channels;
114
115 return(look);
116}
117
118static int ilog(unsigned int v){
119 int ret=0;
120 if(v)--v;
121 while(v){
122 ret++;
123 v>>=1;
124 }
125 return(ret);
126}
127
128
129/* also responsible for range checking */
130static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
131 int i,b;
132 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info));
133 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
134 memset(info,0,sizeof(*info));
135
136 b=oggpack_read(opb,1);
137 if(b<0)goto err_out;
138 if(b){
139 info->submaps=oggpack_read(opb,4)+1;
140 if(info->submaps<=0)goto err_out;
141 }else
142 info->submaps=1;
143
144 b=oggpack_read(opb,1);
145 if(b<0)goto err_out;
146 if(b){
147 info->coupling_steps=oggpack_read(opb,8)+1;
148 if(info->coupling_steps<=0)goto err_out;
149 for(i=0;i<info->coupling_steps;i++){
150 int testM=oggpack_read(opb,ilog(vi->channels));
151 info->coupling_mag[i]=testM;
152 int testA=oggpack_read(opb,ilog(vi->channels));
153 info->coupling_ang[i]=testA;
154
155 if(testM<0 ||
156 testA<0 ||
157 testM==testA ||
158 testM>=vi->channels ||
159 testA>=vi->channels) goto err_out;
160 }
161
162 }
163
164 if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */
165
166 if(info->submaps>1){
167 for(i=0;i<vi->channels;i++){
168 info->chmuxlist[i]=oggpack_read(opb,4);
169 if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out;
170 }
171 }
172 for(i=0;i<info->submaps;i++){
173 int temp=oggpack_read(opb,8);
174 if(temp>=ci->times)goto err_out;
175 info->floorsubmap[i]=oggpack_read(opb,8);
176 if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out;
177 info->residuesubmap[i]=oggpack_read(opb,8);
178 if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)
179 goto err_out;
180 }
181
182 return info;
183
184 err_out:
185 mapping0_free_info(info);
186 return(NULL);
187}
188
189#ifdef _ARM_ASSEM_
190#define MAGANG( _mag, _ang )\
191{\
192 register int temp;\
193 asm( "mov %[temp], %[ang]\n\t"\
194 "cmp %[mag], #0\n\t"\
195 "rsble %[temp], %[temp], #0\n\t"\
196 "cmp %[ang], #0\n\t"\
197 "subgt %[ang], %[mag], %[temp]\n\t"\
198 "movle %[ang], %[mag]\n\t"\
199 "addle %[mag], %[mag], %[temp]\n\t"\
200 : [mag] "+r" ( ( _mag ) ), [ang] "+r" ( ( _ang ) ), [temp] "=&r" (temp)\
201 :\
202 : "cc" );\
203}
204
205static inline void channel_couple(ogg_int32_t *pcmM, ogg_int32_t *pcmA, int n)
206{
207 ogg_int32_t * const pcmMend = pcmM + n/2;
208 while(LIKELY(pcmM < pcmMend))
209 {
210 register int M0 asm("r2"),M1 asm("r3"),M2 asm("r4"),M3 asm("r5");
211 register int A0 asm("r6"),A1 asm("r7"),A2 asm("r8"),A3 asm("r9");
212 asm volatile( "ldmia %[pcmM], {%[M0], %[M1], %[M2], %[M3]}\n\t"
213 "ldmia %[pcmA], {%[A0], %[A1], %[A2], %[A3]}\n\t"
214 : [M0] "=r" (M0), [M1] "=r" (M1), [M2] "=r" (M2), [M3] "=r" (M3),
215 [A0] "=r" (A0), [A1] "=r" (A1), [A2] "=r" (A2), [A3] "=r" (A3)
216 : [pcmM] "r" (pcmM), [pcmA] "r" (pcmA)
217 : "memory" );
218 MAGANG( M0, A0 );
219 MAGANG( M1, A1 );
220 MAGANG( M2, A2 );
221 MAGANG( M3, A3 );
222 asm volatile( "stmia %[pcmM]!, {%[M0], %[M1], %[M2], %[M3]}\n\t"
223 "stmia %[pcmA]!, {%[A0], %[A1], %[A2], %[A3]}\n\t"
224 : [pcmM] "+r" (pcmM), [pcmA] "+r" (pcmA)
225 : [M0] "r" (M0), [M1] "r" (M1), [M2] "r" (M2), [M3] "r" (M3),
226 [A0] "r" (A0), [A1] "r" (A1), [A2] "r" (A2), [A3] "r" (A3)
227 : "memory" );
228 }
229}
230
231#elif defined CPU_COLDFIRE
232
233#define MAGANG( _mag, _ang, _off )\
234{\
235 asm volatile( "tst.l %[mag]\n\t"\
236 "bgt.s 1f\n\t"\
237 "neg.l %[ang]\n\t" /* neg sets cc so we might as well jump */ \
238 "ble.s 2f\n\t"\
239 ".word 0x51fb\n\t" /* trapf.l, shadow next 2 insns */ \
240 /* if we didn't jump, the next test will */ \
241 /* false anyway */ \
242 "1:\n\t"\
243 "tst.l %[ang]\n\t"\
244 "bgt.s 2f\n\t"\
245 "add.l %[mag], %[ang]\n\t"\
246 "move.l %[ang], (" #_off ", %[pcmM])\n\t"\
247 ".word 0x51fa\n\t" /* trapf.w, shadow next insn */ \
248 "2:\n\t"\
249 "sub.l %[ang], %[mag]\n\t"\
250 "move.l %[mag], (%[pcmA])+\n\t"\
251 : [mag] "+r" ( ( _mag ) ), [ang] "+d" ( ( _ang ) ),\
252 [pcmA] "+a" (pcmA)\
253 : [pcmM] "a" (pcmM)\
254 : "cc", "memory" );\
255}
256static inline void channel_couple(ogg_int32_t *pcmM, ogg_int32_t *pcmA, unsigned int n)
257{
258 ogg_int32_t * const pcmMend = pcmM + n/2;
259 while(LIKELY(pcmM < pcmMend))
260 {
261 register int M0 asm("a2"),M1 asm("a3"),M2 asm("a4"),M3 asm("a5");
262 register int A0 asm("d2"),A1 asm("d3"),A2 asm("d4"),A3 asm("d5");
263 asm volatile( "movem.l (%[pcmM]), %[M0]-%[M3]\n\t"
264 "movem.l (%[pcmA]), %[A0]-%[A3]\n\t"
265 : [M0] "=r" (M0), [M1] "=r" (M1), [M2] "=r" (M2), [M3] "=r" (M3),
266 [A0] "=r" (A0), [A1] "=r" (A1), [A2] "=r" (A2), [A3] "=r" (A3)
267 : [pcmM] "a" (pcmM), [pcmA] "a" (pcmA) );
268
269 /* use offsets instead of autoinc since the store to pcmM is conditional */
270 MAGANG( M0, A0, 0 );
271 MAGANG( M1, A1, 4 );
272 MAGANG( M2, A2, 8 );
273 MAGANG( M3, A3, 12 );
274
275 asm volatile( "lea.l (4*4, %[pcmM]), %[pcmM]\n\t"
276 : [pcmM] "+a" (pcmM));
277 }
278}
279#else
280static inline void channel_couple(ogg_int32_t *pcmM, ogg_int32_t *pcmA, int n)
281{
282 int j;
283 for(j=0;j<n/2;j++){
284 ogg_int32_t mag = pcmM[j], ang = pcmA[j], _ang;
285 if(mag>0)
286 _ang = ang;
287 else
288 _ang = -ang;
289
290 if(ang>0)
291 pcmA[j]=mag-_ang;
292 else{
293 pcmA[j]=mag;
294 pcmM[j]=mag+_ang;
295 }
296
297 }
298}
299#endif
300
301static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
302 vorbis_dsp_state *vd=vb->vd;
303 vorbis_info *vi=vd->vi;
304 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
305 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
306 vorbis_info_mapping0 *info=look->map;
307
308 int i,j;
309 long n=vb->pcmend=ci->blocksizes[vb->W];
310
311 /* bounded mapping arrays instead of using alloca();
312 avoids memory leak; we can only deal with stereo anyway */
313 ogg_int32_t *pcmbundle[CHANNELS];
314 int zerobundle[CHANNELS];
315 int nonzero[CHANNELS];
316 void *floormemo[CHANNELS];
317
318 /* time domain information decode (note that applying the
319 information would have to happen later; we'll probably add a
320 function entry to the harness for that later */
321 /* NOT IMPLEMENTED */
322
323 /* recover the spectral envelope; store it in the PCM vector for now */
324 for(i=0;i<vi->channels;i++){
325 int submap=info->chmuxlist[i];
326 floormemo[i]=look->floor_func[submap]->
327 inverse1(vb,look->floor_look[submap]);
328 if(floormemo[i])
329 nonzero[i]=1;
330 else
331 nonzero[i]=0;
332 memset(vd->floors + i * ci->blocksizes[vb->W]/2,0,sizeof(ogg_int32_t)*n/2);
333 }
334
335 /* channel coupling can 'dirty' the nonzero listing */
336 for(i=0;i<info->coupling_steps;i++){
337 if(nonzero[info->coupling_mag[i]] ||
338 nonzero[info->coupling_ang[i]]){
339 nonzero[info->coupling_mag[i]]=1;
340 nonzero[info->coupling_ang[i]]=1;
341 }
342 }
343
344 /* recover the residue into our working vectors */
345 for(i=0;i<info->submaps;i++){
346 int ch_in_bundle=0;
347 for(j=0;j<vi->channels;j++){
348 if(info->chmuxlist[j]==i){
349 if(nonzero[j])
350 zerobundle[ch_in_bundle]=1;
351 else
352 zerobundle[ch_in_bundle]=0;
353 pcmbundle[ch_in_bundle++] = vd->floors + j * ci->blocksizes[vb->W]/2;
354 }
355 }
356
357 look->residue_func[i]->inverse(vb,look->residue_look[i],
358 pcmbundle,zerobundle,ch_in_bundle);
359 }
360
361 //for(j=0;j<vi->channels;j++)
362 //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
363
364
365 /* channel coupling */
366 for(i=info->coupling_steps-1;i>=0;i--){
367 ogg_int32_t *pcmM = vd->floors + info->coupling_mag[i] * ci->blocksizes[vb->W]/2;
368 ogg_int32_t *pcmA = vd->floors + info->coupling_ang[i] * ci->blocksizes[vb->W]/2;
369 channel_couple(pcmM,pcmA,n);
370 }
371
372 //for(j=0;j<vi->channels;j++)
373 //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
374
375 //for(j=0;j<vi->channels;j++)
376 //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
377
378 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
379 /* only MDCT right now.... */
380 for(i=0;i<vi->channels;i++){
381 ogg_int32_t *pcm = vd->floors + i*ci->blocksizes[vb->W]/2;
382 int submap=info->chmuxlist[i];
383
384 if(nonzero[i]) {
385 /* compute and apply spectral envelope */
386 look->floor_func[submap]->
387 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
388
389 ff_imdct_half(ci->blocksizes_nbits[vb->W],
390 (int32_t*)vd->residues[vd->ri] + i*ci->blocksizes[vb->W]/2,
391 (int32_t*)&vd->floors[i*ci->blocksizes[vb->W]/2]);
392 }
393 else
394 memset(vd->residues[vd->ri] + i*ci->blocksizes[vb->W]/2, 0, sizeof(ogg_int32_t)*n/2);
395 }
396
397 //for(j=0;j<vi->channels;j++)
398 //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
399
400 //for(j=0;j<vi->channels;j++)
401 //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
402
403 //seq+=vi->channels;
404 /* all done! */
405 return(0);
406}
407
408/* export hooks */
409const vorbis_func_mapping mapping0_exportbundle ICONST_ATTR ={
410 &mapping0_unpack,
411 &mapping0_look,
412 &mapping0_free_info,
413 &mapping0_free_look,
414 &mapping0_inverse
415};
416