Debian Codegraph @

Xmp3/decode_ntom.c

raw symbols SCIP
1 /*
2 * Mpeg Layer-1,2,3 audio decoder
3 * ------------------------------
4 * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
5 * See also 'README'
6 *
7 * N->M down/up sampling. Not optimized for speed.
8 */
9
10 #include "asterisk.h"
11 #include "asterisk/logger.h"
12 #include <stdlib.h>
13 #include <math.h>
14 #include <string.h>
15
16 #include "mpg123.h"
17 #include "mpglib.h"
18
19 #define WRITE_SAMPLE(samples,sum,clip) \
20 if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
21 else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
22 else { *(samples) = sum; }
23
24 #define NTOM_MUL (32768)
25 static unsigned long ntom_val[2] = { NTOM_MUL>>1,NTOM_MUL>>1 };
26 static unsigned long ntom_step = NTOM_MUL;
27
28
29 int synth_ntom_set_step(long m,long n)
30 {
31 if(param.verbose > 1)
32 ast_log(LOG_WARNING,"Init rate converter: %ld->%ld\n",m,n);
33
34 if(n >= 96000 || m >= 96000 || m == 0 || n == 0) {
35 ast_log(LOG_WARNING,"NtoM converter: illegal rates\n");
36 return (1);
37 }
38
39 n *= NTOM_MUL;
40 ntom_step = n / m;
41
42 if(ntom_step > 8*NTOM_MUL) {
43 ast_log(LOG_WARNING,"max. 1:8 conversion allowed!\n");
44 return (1);
45 }
46
47 ntom_val[0] = ntom_val[1] = NTOM_MUL>>1;
48
49 return (0);
50
51 }
52
53
54 int synth_ntom_mono (struct mpstr *mp, real *bandPtr,unsigned char *samples,int *pnt)
55 {
56 short samples_tmp[8*64];
57 short *tmp1 = samples_tmp;
58 int i,ret;
59 int pnt1 = 0;
60
61 ret = synth_ntom(mp, bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
62 samples += *pnt;
63
64 for(i=0;i<(pnt1>>2);i++) {
65 *( (short *)samples) = *tmp1;
66 samples += 2;
67 tmp1 += 2;
68 }
69 *pnt += pnt1 >> 1;
70
71 return ret;
72 }
73
74
75
76 int synth_ntom(struct mpstr *mp, real *bandPtr,int channel,unsigned char *out,int *pnt)
77 {
78 static const int step = 2;
79 int bo;
80 short *samples = (short *) (out + *pnt);
81
82 real *b0,(*buf)[0x110];
83 int clip = 0;
84 int bo1;
85 int ntom;
86
87 bo = mp->synth_bo;
88
89 if(!channel) {
90 bo--;
91 bo &= 0xf;
92 buf = mp->synth_buffs[0];
93 ntom = ntom_val[1] = ntom_val[0];
94 }
95 else {
96 samples++;
97 out += 2; /* to compute the right *pnt value */
98 buf = mp->synth_buffs[1];
99 ntom = ntom_val[1];
100 }
101
102 if(bo & 0x1) {
103 b0 = buf[0];
104 bo1 = bo;
105 dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
106 }
107 else {
108 b0 = buf[1];
109 bo1 = bo+1;
110 dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
111 }
112
113 mp->synth_bo = bo;
114
115 {
116 register int j;
117 real *window = (mp->decwin) + 16 - bo1;
118
119 for (j=16;j;j--,window+=0x10)
120 {
121 real sum;
122
123 ntom += ntom_step;
124 if(ntom < NTOM_MUL) {
125 window += 16;
126 b0 += 16;
127 continue;
128 }
129
130 sum = *window++ * *b0++;
131 sum -= *window++ * *b0++;
132 sum += *window++ * *b0++;
133 sum -= *window++ * *b0++;
134 sum += *window++ * *b0++;
135 sum -= *window++ * *b0++;
136 sum += *window++ * *b0++;
137 sum -= *window++ * *b0++;
138 sum += *window++ * *b0++;
139 sum -= *window++ * *b0++;
140 sum += *window++ * *b0++;
141 sum -= *window++ * *b0++;
142 sum += *window++ * *b0++;
143 sum -= *window++ * *b0++;
144 sum += *window++ * *b0++;
145 sum -= *window++ * *b0++;
146
147 while(ntom >= NTOM_MUL) {
148 WRITE_SAMPLE(samples,sum,clip);
149 samples += step;
150 ntom -= NTOM_MUL;
151 }
152 }
153
154 ntom += ntom_step;
155 if(ntom >= NTOM_MUL)
156 {
157 real sum;
158 sum = window[0x0] * b0[0x0];
159 sum += window[0x2] * b0[0x2];
160 sum += window[0x4] * b0[0x4];
161 sum += window[0x6] * b0[0x6];
162 sum += window[0x8] * b0[0x8];
163 sum += window[0xA] * b0[0xA];
164 sum += window[0xC] * b0[0xC];
165 sum += window[0xE] * b0[0xE];
166
167 while(ntom >= NTOM_MUL) {
168 WRITE_SAMPLE(samples,sum,clip);
169 samples += step;
170 ntom -= NTOM_MUL;
171 }
172 }
173
174 b0-=0x10,window-=0x20;
175 window += bo1<<1;
176
177 for (j=15;j;j--,b0-=0x20,window-=0x10)
178 {
179 real sum;
180
181 ntom += ntom_step;
182 if(ntom < NTOM_MUL) {
183 window -= 16;
184 b0 += 16;
185 continue;
186 }
187
188 sum = -*(--window) * *b0++;
189 sum -= *(--window) * *b0++;
190 sum -= *(--window) * *b0++;
191 sum -= *(--window) * *b0++;
192 sum -= *(--window) * *b0++;
193 sum -= *(--window) * *b0++;
194 sum -= *(--window) * *b0++;
195 sum -= *(--window) * *b0++;
196 sum -= *(--window) * *b0++;
197 sum -= *(--window) * *b0++;
198 sum -= *(--window) * *b0++;
199 sum -= *(--window) * *b0++;
200 sum -= *(--window) * *b0++;
201 sum -= *(--window) * *b0++;
202 sum -= *(--window) * *b0++;
203 sum -= *(--window) * *b0++;
204
205 while(ntom >= NTOM_MUL) {
206 WRITE_SAMPLE(samples,sum,clip);
207 samples += step;
208 ntom -= NTOM_MUL;
209 }
210 }
211 }
212
213 ntom_val[channel] = ntom;
214 *pnt = ((unsigned char *) samples - out);
215
216 return clip;
217 }
218
219