2
decode_ntom.c: N->M down/up sampling. Not optimized for speed.
4
copyright 1995-2006 by the mpg123 project - free software under the terms of the LGPL 2.1
5
see COPYING and AUTHORS files in distribution or http://mpg123.de
6
initially written by Michael Hipp
16
#define WRITE_SAMPLE(samples,sum,clip) \
17
if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
18
else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
19
else { *(samples) = sum; }
21
#define NTOM_MUL (32768)
22
static unsigned long ntom_val[2] = { NTOM_MUL>>1,NTOM_MUL>>1 };
23
static unsigned long ntom_step = NTOM_MUL;
26
void synth_ntom_set_step(long m,long n)
29
fprintf(stderr,"Init rate converter: %ld->%ld\n",m,n);
31
if(n >= 96000 || m >= 96000 || m == 0 || n == 0) {
32
fprintf(stderr,"NtoM converter: illegal rates\n");
39
if(ntom_step > 8*NTOM_MUL) {
40
fprintf(stderr,"max. 1:8 conversion allowed!\n");
44
ntom_val[0] = ntom_val[1] = NTOM_MUL>>1;
47
int synth_ntom_8bit(real *bandPtr,int channel,unsigned char *samples,int *pnt)
49
short samples_tmp[8*64];
50
short *tmp1 = samples_tmp + channel;
54
ret = synth_ntom(bandPtr,channel,(unsigned char *) samples_tmp,&pnt1);
55
samples += channel + *pnt;
57
for(i=0;i<(pnt1>>2);i++) {
58
*samples = conv16to8[*tmp1>>AUSHIFT];
67
int synth_ntom_8bit_mono(real *bandPtr,unsigned char *samples,int *pnt)
69
short samples_tmp[8*64];
70
short *tmp1 = samples_tmp;
74
ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
77
for(i=0;i<(pnt1>>2);i++) {
78
*samples++ = conv16to8[*tmp1>>AUSHIFT];
86
int synth_ntom_8bit_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
88
short samples_tmp[8*64];
89
short *tmp1 = samples_tmp;
93
ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
96
for(i=0;i<(pnt1>>2);i++) {
97
*samples++ = conv16to8[*tmp1>>AUSHIFT];
98
*samples++ = conv16to8[*tmp1>>AUSHIFT];
106
int synth_ntom_mono(real *bandPtr,unsigned char *samples,int *pnt)
108
short samples_tmp[8*64];
109
short *tmp1 = samples_tmp;
113
ret = synth_ntom(bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
116
for(i=0;i<(pnt1>>2);i++) {
117
*( (short *)samples) = *tmp1;
127
int synth_ntom_mono2stereo(real *bandPtr,unsigned char *samples,int *pnt)
132
ret = synth_ntom(bandPtr,0,samples,pnt);
135
for(i=0;i<((*pnt-pnt1)>>2);i++) {
136
((short *)samples)[1] = ((short *)samples)[0];
144
int synth_ntom(real *bandPtr,int channel,unsigned char *out,int *pnt)
146
static real buffs[2][2][0x110];
147
static const int step = 2;
149
short *samples = (short *) (out + *pnt);
151
real *b0,(*buf)[0x110];
157
do_equalizer(bandPtr,channel);
163
ntom = ntom_val[1] = ntom_val[0];
167
out += 2; /* to compute the right *pnt value */
175
dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
180
dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
186
real *window = decwin + 16 - bo1;
188
for (j=16;j;j--,window+=0x10)
193
if(ntom < NTOM_MUL) {
199
sum = *window++ * *b0++;
200
sum -= *window++ * *b0++;
201
sum += *window++ * *b0++;
202
sum -= *window++ * *b0++;
203
sum += *window++ * *b0++;
204
sum -= *window++ * *b0++;
205
sum += *window++ * *b0++;
206
sum -= *window++ * *b0++;
207
sum += *window++ * *b0++;
208
sum -= *window++ * *b0++;
209
sum += *window++ * *b0++;
210
sum -= *window++ * *b0++;
211
sum += *window++ * *b0++;
212
sum -= *window++ * *b0++;
213
sum += *window++ * *b0++;
214
sum -= *window++ * *b0++;
216
while(ntom >= NTOM_MUL) {
217
WRITE_SAMPLE(samples,sum,clip);
227
sum = window[0x0] * b0[0x0];
228
sum += window[0x2] * b0[0x2];
229
sum += window[0x4] * b0[0x4];
230
sum += window[0x6] * b0[0x6];
231
sum += window[0x8] * b0[0x8];
232
sum += window[0xA] * b0[0xA];
233
sum += window[0xC] * b0[0xC];
234
sum += window[0xE] * b0[0xE];
236
while(ntom >= NTOM_MUL) {
237
WRITE_SAMPLE(samples,sum,clip);
243
b0-=0x10,window-=0x20;
246
for (j=15;j;j--,b0-=0x20,window-=0x10)
251
if(ntom < NTOM_MUL) {
257
sum = -*(--window) * *b0++;
258
sum -= *(--window) * *b0++;
259
sum -= *(--window) * *b0++;
260
sum -= *(--window) * *b0++;
261
sum -= *(--window) * *b0++;
262
sum -= *(--window) * *b0++;
263
sum -= *(--window) * *b0++;
264
sum -= *(--window) * *b0++;
265
sum -= *(--window) * *b0++;
266
sum -= *(--window) * *b0++;
267
sum -= *(--window) * *b0++;
268
sum -= *(--window) * *b0++;
269
sum -= *(--window) * *b0++;
270
sum -= *(--window) * *b0++;
271
sum -= *(--window) * *b0++;
272
sum -= *(--window) * *b0++;
274
while(ntom >= NTOM_MUL) {
275
WRITE_SAMPLE(samples,sum,clip);
282
ntom_val[channel] = ntom;
283
*pnt = ((unsigned char *) samples - out);