3396
|
1
|
|
2 // liba52 sample by A'rpi/ESP-team
|
|
3 // reads ac3 stream form stdin, decodes and downmix to s16 stereo pcm and
|
|
4 // writes it to stdout. resulting stream playbackable with sox:
|
|
5 // play -c 2 -r 48000 out.sw
|
|
6
|
3507
|
7 //#define TIMING //needs Pentium or newer
|
|
8
|
3396
|
9 #include <stdio.h>
|
|
10 #include <stdlib.h>
|
|
11 #include <inttypes.h>
|
|
12
|
|
13 #include "a52.h"
|
|
14
|
|
15 static sample_t * samples;
|
|
16 static a52_state_t state;
|
|
17 static uint8_t buf[3840];
|
|
18 static int buf_size=0;
|
|
19
|
|
20 static int16_t out_buf[6*256*6];
|
|
21
|
3507
|
22 #ifdef TIMING
|
|
23 static inline long long rdtsc()
|
|
24 {
|
|
25 long long l;
|
|
26 asm volatile( "rdtsc\n\t"
|
|
27 : "=A" (l)
|
|
28 );
|
|
29 // printf("%d\n", int(l/1000));
|
|
30 return l;
|
|
31 }
|
|
32
|
|
33 #define STARTTIMING t=rdtsc();
|
|
34 #define ENDTIMING sum+=rdtsc()-t; t=rdtsc();
|
|
35 #else
|
|
36 #define STARTTIMING ;
|
|
37 #define ENDTIMING ;
|
|
38 #endif
|
|
39
|
|
40
|
3396
|
41 static inline int16_t convert (int32_t i)
|
|
42 {
|
|
43 if (i > 0x43c07fff)
|
|
44 return 32767;
|
|
45 else if (i < 0x43bf8000)
|
|
46 return -32768;
|
|
47 else
|
|
48 return i - 0x43c00000;
|
|
49 }
|
|
50
|
|
51 static inline void float_to_int (float * _f, int16_t * s16, int flags)
|
|
52 {
|
|
53 int i;
|
|
54 int32_t * f = (int32_t *) _f;
|
|
55
|
|
56 switch (flags) {
|
|
57 case A52_MONO:
|
|
58 for (i = 0; i < 256; i++) {
|
|
59 s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
|
|
60 s16[5*i+4] = convert (f[i]);
|
|
61 }
|
|
62 break;
|
|
63 case A52_CHANNEL:
|
|
64 case A52_STEREO:
|
|
65 case A52_DOLBY:
|
|
66 for (i = 0; i < 256; i++) {
|
|
67 s16[2*i] = convert (f[i]);
|
|
68 s16[2*i+1] = convert (f[i+256]);
|
|
69 }
|
|
70 break;
|
|
71 case A52_3F:
|
|
72 for (i = 0; i < 256; i++) {
|
|
73 s16[5*i] = convert (f[i]);
|
|
74 s16[5*i+1] = convert (f[i+512]);
|
|
75 s16[5*i+2] = s16[5*i+3] = 0;
|
|
76 s16[5*i+4] = convert (f[i+256]);
|
|
77 }
|
|
78 break;
|
|
79 case A52_2F2R:
|
|
80 for (i = 0; i < 256; i++) {
|
|
81 s16[4*i] = convert (f[i]);
|
|
82 s16[4*i+1] = convert (f[i+256]);
|
|
83 s16[4*i+2] = convert (f[i+512]);
|
|
84 s16[4*i+3] = convert (f[i+768]);
|
|
85 }
|
|
86 break;
|
|
87 case A52_3F2R:
|
|
88 for (i = 0; i < 256; i++) {
|
|
89 s16[5*i] = convert (f[i]);
|
|
90 s16[5*i+1] = convert (f[i+512]);
|
|
91 s16[5*i+2] = convert (f[i+768]);
|
|
92 s16[5*i+3] = convert (f[i+1024]);
|
|
93 s16[5*i+4] = convert (f[i+256]);
|
|
94 }
|
|
95 break;
|
|
96 case A52_MONO | A52_LFE:
|
|
97 for (i = 0; i < 256; i++) {
|
|
98 s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
|
|
99 s16[6*i+4] = convert (f[i+256]);
|
|
100 s16[6*i+5] = convert (f[i]);
|
|
101 }
|
|
102 break;
|
|
103 case A52_CHANNEL | A52_LFE:
|
|
104 case A52_STEREO | A52_LFE:
|
|
105 case A52_DOLBY | A52_LFE:
|
|
106 for (i = 0; i < 256; i++) {
|
|
107 s16[6*i] = convert (f[i+256]);
|
|
108 s16[6*i+1] = convert (f[i+512]);
|
|
109 s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
|
|
110 s16[6*i+5] = convert (f[i]);
|
|
111 }
|
|
112 break;
|
|
113 case A52_3F | A52_LFE:
|
|
114 for (i = 0; i < 256; i++) {
|
|
115 s16[6*i] = convert (f[i+256]);
|
|
116 s16[6*i+1] = convert (f[i+768]);
|
|
117 s16[6*i+2] = s16[6*i+3] = 0;
|
|
118 s16[6*i+4] = convert (f[i+512]);
|
|
119 s16[6*i+5] = convert (f[i]);
|
|
120 }
|
|
121 break;
|
|
122 case A52_2F2R | A52_LFE:
|
|
123 for (i = 0; i < 256; i++) {
|
|
124 s16[6*i] = convert (f[i+256]);
|
|
125 s16[6*i+1] = convert (f[i+512]);
|
|
126 s16[6*i+2] = convert (f[i+768]);
|
|
127 s16[6*i+3] = convert (f[i+1024]);
|
|
128 s16[6*i+4] = 0;
|
|
129 s16[6*i+5] = convert (f[i]);
|
|
130 }
|
|
131 break;
|
|
132 case A52_3F2R | A52_LFE:
|
|
133 for (i = 0; i < 256; i++) {
|
|
134 s16[6*i] = convert (f[i+256]);
|
|
135 s16[6*i+1] = convert (f[i+768]);
|
|
136 s16[6*i+2] = convert (f[i+1024]);
|
|
137 s16[6*i+3] = convert (f[i+1280]);
|
|
138 s16[6*i+4] = convert (f[i+512]);
|
|
139 s16[6*i+5] = convert (f[i]);
|
|
140 }
|
|
141 break;
|
|
142 }
|
|
143 }
|
|
144
|
|
145
|
|
146 int main(){
|
|
147 int accel=0;
|
|
148 int sample_rate=0;
|
|
149 int bit_rate=0;
|
3507
|
150 #ifdef TIMING
|
3511
|
151 long long t, sum=0, min=256*256*256*64;
|
3507
|
152 #endif
|
3396
|
153
|
|
154 samples = a52_init (accel);
|
|
155 if (samples == NULL) {
|
|
156 fprintf (stderr, "A52 init failed\n");
|
|
157 return 1;
|
|
158 }
|
|
159
|
|
160 while(1){
|
|
161 int length,i;
|
|
162 int16_t *s16;
|
|
163 sample_t level=1, bias=384;
|
|
164 int flags=0;
|
|
165
|
|
166 while(buf_size<7){
|
|
167 int c=getchar();
|
|
168 if(c<0) goto eof;
|
|
169 buf[buf_size++]=c;
|
|
170 }
|
3507
|
171 STARTTIMING
|
3396
|
172 length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
|
3507
|
173 ENDTIMING
|
3396
|
174 if(!length){
|
|
175 // bad file => resync
|
|
176 memcpy(buf,buf+1,6);
|
|
177 --buf_size;
|
|
178 continue;
|
|
179 }
|
|
180 fprintf(stderr,"sync. %d bytes 0x%X %d Hz %d kbit\n",length,flags,sample_rate,bit_rate);
|
|
181 while(buf_size<length){
|
|
182 buf[buf_size++]=getchar();
|
|
183 }
|
|
184
|
|
185 buf_size=0;
|
|
186
|
|
187 // decode:
|
|
188 flags=A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
|
|
189 flags |= A52_ADJUST_LEVEL;
|
3507
|
190 STARTTIMING
|
3396
|
191 if (a52_frame (&state, buf, &flags, &level, bias))
|
|
192 { fprintf(stderr,"error at decoding\n"); continue; }
|
3507
|
193 ENDTIMING
|
3396
|
194
|
|
195 // a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
|
|
196
|
|
197 s16 = out_buf;
|
|
198 for (i = 0; i < 6; i++) {
|
|
199 int32_t * f = (int32_t *) samples;
|
|
200 int i;
|
3507
|
201 STARTTIMING
|
3396
|
202 if (a52_block (&state, samples))
|
|
203 { fprintf(stderr,"error at sampling\n"); break; }
|
3507
|
204 ENDTIMING
|
3396
|
205 // resample to STEREO/DOLBY:
|
|
206 for (i = 0; i < 256; i++) {
|
|
207 s16[2*i] = convert (f[i]);
|
|
208 s16[2*i+1] = convert (f[i+256]);
|
|
209 }
|
|
210 s16+=2*i;
|
|
211 }
|
3511
|
212 #ifdef TIMING
|
|
213 if(sum<min) min=sum;
|
|
214 sum=0;
|
|
215 #endif
|
3396
|
216 fwrite(out_buf,6*256*2*2,1,stdout);
|
|
217
|
|
218 }
|
|
219
|
|
220 eof:
|
3507
|
221 #ifdef TIMING
|
3511
|
222 fprintf(stderr, "%4.4fk cycles ",min/1000.0);
|
|
223 sum=0;
|
3507
|
224 #endif
|
3396
|
225 }
|