Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Mpeg Layer-1,2,3 audio decoder |
||
3 | * ------------------------------ |
||
4 | * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README' |
||
5 | * version for slower machines .. decodes only every second sample |
||
6 | * sounds like 24000,22050 or 16000 kHz .. (depending on original sample freq.) |
||
7 | * |
||
8 | */ |
||
9 | |||
10 | #include <stdlib.h> |
||
11 | #include <math.h> |
||
12 | #include <string.h> |
||
13 | |||
14 | #include "mpg123.h" |
||
15 | |||
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; } |
||
20 | |||
21 | int synth_2to1_8bit(real *bandPtr,int channel,unsigned char *samples) |
||
22 | { |
||
23 | short samples_tmp[32]; |
||
24 | short *tmp1 = samples_tmp + channel; |
||
25 | int i,ret; |
||
26 | |||
27 | samples += channel; |
||
28 | ret = synth_2to1(bandPtr,channel,(unsigned char *) samples_tmp); |
||
29 | |||
30 | for(i=0;i<16;i++) { |
||
31 | *samples = conv16to8[*tmp1>>4]; |
||
32 | samples += 2; |
||
33 | tmp1 += 2; |
||
34 | } |
||
35 | |||
36 | return ret; |
||
37 | } |
||
38 | |||
39 | int synth_2to1_8bit_mono(real *bandPtr,unsigned char *samples) |
||
40 | { |
||
41 | short samples_tmp[32]; |
||
42 | short *tmp1 = samples_tmp; |
||
43 | int i,ret; |
||
44 | |||
45 | ret = synth_2to1(bandPtr,0,(unsigned char *) samples_tmp); |
||
46 | |||
47 | for(i=0;i<16;i++) { |
||
48 | *samples++ = conv16to8[*tmp1>>4]; |
||
49 | *samples++ = conv16to8[*tmp1>>4]; |
||
50 | tmp1 += 2; |
||
51 | } |
||
52 | |||
53 | return ret; |
||
54 | } |
||
55 | |||
56 | int synth_2to1_mono(real *bandPtr,unsigned char *samples) |
||
57 | { |
||
58 | int i,ret = synth_2to1(bandPtr,0,samples); |
||
59 | for(i=0;i<16;i++) { |
||
60 | ((short *)samples)[1] = ((short *)samples)[0]; |
||
61 | samples+=4; |
||
62 | } |
||
63 | return ret; |
||
64 | } |
||
65 | |||
66 | int synth_2to1(real *bandPtr,int channel,unsigned char *out) |
||
67 | { |
||
68 | static real buffs[2][2][0x110]; |
||
69 | static const int step = 2; |
||
70 | static int bo = 1; |
||
71 | short *samples = (short *) out; |
||
72 | |||
73 | real *b0,(*buf)[0x110]; |
||
74 | int clip = 0; |
||
75 | int bo1; |
||
76 | |||
77 | if(!channel) { |
||
78 | bo--; |
||
79 | bo &= 0xf; |
||
80 | buf = buffs[0]; |
||
81 | } |
||
82 | else { |
||
83 | samples++; |
||
84 | buf = buffs[1]; |
||
85 | } |
||
86 | |||
87 | if(bo & 0x1) { |
||
88 | b0 = buf[0]; |
||
89 | bo1 = bo; |
||
90 | dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr); |
||
91 | } |
||
92 | else { |
||
93 | b0 = buf[1]; |
||
94 | bo1 = bo+1; |
||
95 | dct64(buf[0]+bo,buf[1]+bo+1,bandPtr); |
||
96 | } |
||
97 | |||
98 | { |
||
99 | register int j; |
||
100 | real *window = decwin + 16 - bo1; |
||
101 | |||
102 | for (j=8;j;j--,b0+=0x10,window+=0x30) |
||
103 | { |
||
104 | real sum; |
||
105 | sum = *window++ * *b0++; |
||
106 | sum -= *window++ * *b0++; |
||
107 | sum += *window++ * *b0++; |
||
108 | sum -= *window++ * *b0++; |
||
109 | sum += *window++ * *b0++; |
||
110 | sum -= *window++ * *b0++; |
||
111 | sum += *window++ * *b0++; |
||
112 | sum -= *window++ * *b0++; |
||
113 | sum += *window++ * *b0++; |
||
114 | sum -= *window++ * *b0++; |
||
115 | sum += *window++ * *b0++; |
||
116 | sum -= *window++ * *b0++; |
||
117 | sum += *window++ * *b0++; |
||
118 | sum -= *window++ * *b0++; |
||
119 | sum += *window++ * *b0++; |
||
120 | sum -= *window++ * *b0++; |
||
121 | |||
122 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
123 | #if 0 |
||
124 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
125 | #endif |
||
126 | } |
||
127 | |||
128 | { |
||
129 | real sum; |
||
130 | sum = window[0x0] * b0[0x0]; |
||
131 | sum += window[0x2] * b0[0x2]; |
||
132 | sum += window[0x4] * b0[0x4]; |
||
133 | sum += window[0x6] * b0[0x6]; |
||
134 | sum += window[0x8] * b0[0x8]; |
||
135 | sum += window[0xA] * b0[0xA]; |
||
136 | sum += window[0xC] * b0[0xC]; |
||
137 | sum += window[0xE] * b0[0xE]; |
||
138 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
139 | #if 0 |
||
140 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
141 | #endif |
||
142 | b0-=0x20,window-=0x40; |
||
143 | } |
||
144 | window += bo1<<1; |
||
145 | |||
146 | for (j=7;j;j--,b0-=0x30,window-=0x30) |
||
147 | { |
||
148 | real sum; |
||
149 | sum = -*(--window) * *b0++; |
||
150 | sum -= *(--window) * *b0++; |
||
151 | sum -= *(--window) * *b0++; |
||
152 | sum -= *(--window) * *b0++; |
||
153 | sum -= *(--window) * *b0++; |
||
154 | sum -= *(--window) * *b0++; |
||
155 | sum -= *(--window) * *b0++; |
||
156 | sum -= *(--window) * *b0++; |
||
157 | sum -= *(--window) * *b0++; |
||
158 | sum -= *(--window) * *b0++; |
||
159 | sum -= *(--window) * *b0++; |
||
160 | sum -= *(--window) * *b0++; |
||
161 | sum -= *(--window) * *b0++; |
||
162 | sum -= *(--window) * *b0++; |
||
163 | sum -= *(--window) * *b0++; |
||
164 | sum -= *(--window) * *b0++; |
||
165 | |||
166 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
167 | #if 0 |
||
168 | WRITE_SAMPLE(samples,sum,clip); samples += step; |
||
169 | #endif |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return clip; |
||
174 | } |
||
175 | |||
176 |