Two tiny tweaks to some arm asm
[kugel-rb.git] / apps / codecs / libtremor / mapping0.c
blobc7e7299cccf1f651b7b967d87914f9c5e06f1990
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 ********************************************************************
14 function: channel mapping 0 implementation
16 ********************************************************************/
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>
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.
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 */
41 typedef struct {
42 vorbis_info_mode *mode;
43 vorbis_info_mapping0 *map;
45 vorbis_look_floor **floor_look;
47 vorbis_look_residue **residue_look;
49 vorbis_func_floor **floor_func;
50 vorbis_func_residue **residue_func;
52 int ch;
53 long lastframe; /* if a different mode is called, we need to
54 invalidate decay */
55 } vorbis_look_mapping0;
57 static 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);
65 static void mapping0_free_look(vorbis_look_mapping *look){
66 int i;
67 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
68 if(l){
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]);
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);
84 static 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;
93 look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
95 look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
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));
100 for(i=0;i<info->submaps;i++){
101 int floornum=info->floorsubmap[i];
102 int resnum=info->residuesubmap[i];
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]);
113 look->ch=vi->channels;
115 return(look);
118 static int ilog(unsigned int v){
119 int ret=0;
120 if(v)--v;
121 while(v){
122 ret++;
123 v>>=1;
125 return(ret);
129 /* also responsible for range checking */
130 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
131 int i;
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));
136 if(oggpack_read(opb,1))
137 info->submaps=oggpack_read(opb,4)+1;
138 else
139 info->submaps=1;
141 if(oggpack_read(opb,1)){
142 info->coupling_steps=oggpack_read(opb,8)+1;
144 for(i=0;i<info->coupling_steps;i++){
145 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
146 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
148 if(testM<0 ||
149 testA<0 ||
150 testM==testA ||
151 testM>=vi->channels ||
152 testA>=vi->channels) goto err_out;
157 if(oggpack_read(opb,2)>0)goto err_out; /* 2,3:reserved */
159 if(info->submaps>1){
160 for(i=0;i<vi->channels;i++){
161 info->chmuxlist[i]=oggpack_read(opb,4);
162 if(info->chmuxlist[i]>=info->submaps)goto err_out;
165 for(i=0;i<info->submaps;i++){
166 int temp=oggpack_read(opb,8);
167 if(temp>=ci->times)goto err_out;
168 info->floorsubmap[i]=oggpack_read(opb,8);
169 if(info->floorsubmap[i]>=ci->floors)goto err_out;
170 info->residuesubmap[i]=oggpack_read(opb,8);
171 if(info->residuesubmap[i]>=ci->residues)goto err_out;
174 return info;
176 err_out:
177 mapping0_free_info(info);
178 return(NULL);
181 #ifdef _ARM_ASSEM_
182 #define MAGANG( _mag, _ang )\
184 register int temp;\
185 asm( "cmp %[mag], #0\n\t"\
186 "cmpgt %[ang], #0\n\t"\
187 "subgt %[ang], %[mag], %[ang]\n\t"\
188 "bgt 1f\n\t"\
189 "cmp %[mag], #0\n\t"\
190 "cmple %[ang], #0\n\t"\
191 "addgt %[temp], %[mag], %[ang]\n\t"\
192 "suble %[temp], %[mag], %[ang]\n\t"\
193 "cmp %[ang], #0\n\t"\
194 "movle %[ang], %[mag]\n\t"\
195 "movle %[mag], %[temp]\n\t"\
196 "movgt %[ang], %[temp]\n\t"\
197 "1:\n\t"\
198 : [mag] "+r" ( ( _mag ) ), [ang] "+r" ( ( _ang ) ), [temp] "=&r" (temp)\
200 : "cc" );\
203 static inline void channel_couple(ogg_int32_t *pcmM, ogg_int32_t *pcmA, int n)
205 ogg_int32_t * const pcmMend = pcmM + n/2;
206 while(LIKELY(pcmM < pcmMend))
208 register int M0 asm("r2"),M1 asm("r3"),M2 asm("r4"),M3 asm("r5");
209 register int A0 asm("r6"),A1 asm("r7"),A2 asm("r8"),A3 asm("r9");
210 asm volatile( "ldmia %[pcmM], {%[M0], %[M1], %[M2], %[M3]}\n\t"
211 "ldmia %[pcmA], {%[A0], %[A1], %[A2], %[A3]}\n\t"
212 : [M0] "=r" (M0), [M1] "=r" (M1), [M2] "=r" (M2), [M3] "=r" (M3),
213 [A0] "=r" (A0), [A1] "=r" (A1), [A2] "=r" (A2), [A3] "=r" (A3)
214 : [pcmM] "r" (pcmM), [pcmA] "r" (pcmA) );
215 MAGANG( M0, A0 );
216 MAGANG( M1, A1 );
217 MAGANG( M2, A2 );
218 MAGANG( M3, A3 );
219 asm volatile( "stmia %[pcmM]!, {%[M0], %[M1], %[M2], %[M3]}\n\t"
220 "stmia %[pcmA]!, {%[A0], %[A1], %[A2], %[A3]}\n\t"
221 : [pcmM] "+r" (pcmM), [pcmA] "+r" (pcmA)
222 : [M0] "r" (M0), [M1] "r" (M1), [M2] "r" (M2), [M3] "r" (M3),
223 [A0] "r" (A0), [A1] "r" (A1), [A2] "r" (A2), [A3] "r" (A3) );
226 #else
227 static inline void channel_couple(ogg_int32_t *pcmM, ogg_int32_t *pcmA, int n)
229 int j;
230 for(j=0;j<n/2;j++){
231 ogg_int32_t mag = pcmM[j], ang = pcmA[j];
232 if(mag>0)
233 if(ang>0)
234 pcmA[j]=mag-ang;
235 else{
236 pcmA[j]=mag;
237 pcmM[j]=mag+ang;
239 else
240 if(ang>0)
241 pcmA[j]=mag+ang;
242 else{
243 pcmA[j]=mag;
244 pcmM[j]=mag-ang;
248 #endif
250 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
251 vorbis_dsp_state *vd=vb->vd;
252 vorbis_info *vi=vd->vi;
253 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
254 private_state *b=(private_state *)vd->backend_state;
255 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
256 vorbis_info_mapping0 *info=look->map;
258 int i,j;
259 long n=vb->pcmend=ci->blocksizes[vb->W];
261 /* bounded mapping arrays instead of using alloca();
262 avoids memory leak; we can only deal with stereo anyway */
263 ogg_int32_t *pcmbundle[CHANNELS];
264 int zerobundle[CHANNELS];
265 int nonzero[CHANNELS];
266 void *floormemo[CHANNELS];
268 /* time domain information decode (note that applying the
269 information would have to happen later; we'll probably add a
270 function entry to the harness for that later */
271 /* NOT IMPLEMENTED */
273 /* recover the spectral envelope; store it in the PCM vector for now */
274 for(i=0;i<vi->channels;i++){
275 int submap=info->chmuxlist[i];
276 floormemo[i]=look->floor_func[submap]->
277 inverse1(vb,look->floor_look[submap]);
278 if(floormemo[i])
279 nonzero[i]=1;
280 else
281 nonzero[i]=0;
282 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
285 /* channel coupling can 'dirty' the nonzero listing */
286 for(i=0;i<info->coupling_steps;i++){
287 if(nonzero[info->coupling_mag[i]] ||
288 nonzero[info->coupling_ang[i]]){
289 nonzero[info->coupling_mag[i]]=1;
290 nonzero[info->coupling_ang[i]]=1;
294 /* recover the residue into our working vectors */
295 for(i=0;i<info->submaps;i++){
296 int ch_in_bundle=0;
297 for(j=0;j<vi->channels;j++){
298 if(info->chmuxlist[j]==i){
299 if(nonzero[j])
300 zerobundle[ch_in_bundle]=1;
301 else
302 zerobundle[ch_in_bundle]=0;
303 pcmbundle[ch_in_bundle++]=vb->pcm[j];
307 look->residue_func[i]->inverse(vb,look->residue_look[i],
308 pcmbundle,zerobundle,ch_in_bundle);
311 //for(j=0;j<vi->channels;j++)
312 //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
315 /* channel coupling */
316 for(i=info->coupling_steps-1;i>=0;i--){
317 ogg_int32_t *pcmM=vb->pcm[info->coupling_mag[i]];
318 ogg_int32_t *pcmA=vb->pcm[info->coupling_ang[i]];
319 channel_couple(pcmM,pcmA,n);
322 //for(j=0;j<vi->channels;j++)
323 //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
325 //for(j=0;j<vi->channels;j++)
326 //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
328 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
329 /* only MDCT right now.... */
331 for(i=0;i<vi->channels;i++){
332 ogg_int32_t *pcm=vb->pcm[i];
333 int submap=info->chmuxlist[i];
335 if(nonzero[i]) {
336 /* compute and apply spectral envelope */
337 look->floor_func[submap]->
338 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
340 ff_imdct_calc(ci->blocksizes_nbits[vb->W],
341 (int32_t*)pcm,
342 (int32_t*)pcm);
343 /* window the data */
344 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
346 else
347 memset(pcm, 0, sizeof(ogg_int32_t)*n);
350 //for(j=0;j<vi->channels;j++)
351 //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
353 //for(j=0;j<vi->channels;j++)
354 //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
356 //seq+=vi->channels;
357 /* all done! */
358 return(0);
361 /* export hooks */
362 const vorbis_func_mapping mapping0_exportbundle ICONST_ATTR ={
363 &mapping0_unpack,
364 &mapping0_look,
365 &mapping0_free_info,
366 &mapping0_free_look,
367 &mapping0_inverse