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
|
|
7 #include <stdio.h>
|
|
8 #include <stdlib.h>
|
|
9 #include <inttypes.h>
|
|
10
|
|
11 #include "a52.h"
|
|
12
|
|
13 static sample_t * samples;
|
|
14 static a52_state_t state;
|
|
15 static uint8_t buf[3840];
|
|
16 static int buf_size=0;
|
|
17
|
|
18 static int16_t out_buf[6*256*6];
|
|
19
|
|
20 static inline int16_t convert (int32_t i)
|
|
21 {
|
|
22 if (i > 0x43c07fff)
|
|
23 return 32767;
|
|
24 else if (i < 0x43bf8000)
|
|
25 return -32768;
|
|
26 else
|
|
27 return i - 0x43c00000;
|
|
28 }
|
|
29
|
|
30 static inline void float_to_int (float * _f, int16_t * s16, int flags)
|
|
31 {
|
|
32 int i;
|
|
33 int32_t * f = (int32_t *) _f;
|
|
34
|
|
35 switch (flags) {
|
|
36 case A52_MONO:
|
|
37 for (i = 0; i < 256; i++) {
|
|
38 s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
|
|
39 s16[5*i+4] = convert (f[i]);
|
|
40 }
|
|
41 break;
|
|
42 case A52_CHANNEL:
|
|
43 case A52_STEREO:
|
|
44 case A52_DOLBY:
|
|
45 for (i = 0; i < 256; i++) {
|
|
46 s16[2*i] = convert (f[i]);
|
|
47 s16[2*i+1] = convert (f[i+256]);
|
|
48 }
|
|
49 break;
|
|
50 case A52_3F:
|
|
51 for (i = 0; i < 256; i++) {
|
|
52 s16[5*i] = convert (f[i]);
|
|
53 s16[5*i+1] = convert (f[i+512]);
|
|
54 s16[5*i+2] = s16[5*i+3] = 0;
|
|
55 s16[5*i+4] = convert (f[i+256]);
|
|
56 }
|
|
57 break;
|
|
58 case A52_2F2R:
|
|
59 for (i = 0; i < 256; i++) {
|
|
60 s16[4*i] = convert (f[i]);
|
|
61 s16[4*i+1] = convert (f[i+256]);
|
|
62 s16[4*i+2] = convert (f[i+512]);
|
|
63 s16[4*i+3] = convert (f[i+768]);
|
|
64 }
|
|
65 break;
|
|
66 case A52_3F2R:
|
|
67 for (i = 0; i < 256; i++) {
|
|
68 s16[5*i] = convert (f[i]);
|
|
69 s16[5*i+1] = convert (f[i+512]);
|
|
70 s16[5*i+2] = convert (f[i+768]);
|
|
71 s16[5*i+3] = convert (f[i+1024]);
|
|
72 s16[5*i+4] = convert (f[i+256]);
|
|
73 }
|
|
74 break;
|
|
75 case A52_MONO | A52_LFE:
|
|
76 for (i = 0; i < 256; i++) {
|
|
77 s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
|
|
78 s16[6*i+4] = convert (f[i+256]);
|
|
79 s16[6*i+5] = convert (f[i]);
|
|
80 }
|
|
81 break;
|
|
82 case A52_CHANNEL | A52_LFE:
|
|
83 case A52_STEREO | A52_LFE:
|
|
84 case A52_DOLBY | A52_LFE:
|
|
85 for (i = 0; i < 256; i++) {
|
|
86 s16[6*i] = convert (f[i+256]);
|
|
87 s16[6*i+1] = convert (f[i+512]);
|
|
88 s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
|
|
89 s16[6*i+5] = convert (f[i]);
|
|
90 }
|
|
91 break;
|
|
92 case A52_3F | A52_LFE:
|
|
93 for (i = 0; i < 256; i++) {
|
|
94 s16[6*i] = convert (f[i+256]);
|
|
95 s16[6*i+1] = convert (f[i+768]);
|
|
96 s16[6*i+2] = s16[6*i+3] = 0;
|
|
97 s16[6*i+4] = convert (f[i+512]);
|
|
98 s16[6*i+5] = convert (f[i]);
|
|
99 }
|
|
100 break;
|
|
101 case A52_2F2R | A52_LFE:
|
|
102 for (i = 0; i < 256; i++) {
|
|
103 s16[6*i] = convert (f[i+256]);
|
|
104 s16[6*i+1] = convert (f[i+512]);
|
|
105 s16[6*i+2] = convert (f[i+768]);
|
|
106 s16[6*i+3] = convert (f[i+1024]);
|
|
107 s16[6*i+4] = 0;
|
|
108 s16[6*i+5] = convert (f[i]);
|
|
109 }
|
|
110 break;
|
|
111 case A52_3F2R | A52_LFE:
|
|
112 for (i = 0; i < 256; i++) {
|
|
113 s16[6*i] = convert (f[i+256]);
|
|
114 s16[6*i+1] = convert (f[i+768]);
|
|
115 s16[6*i+2] = convert (f[i+1024]);
|
|
116 s16[6*i+3] = convert (f[i+1280]);
|
|
117 s16[6*i+4] = convert (f[i+512]);
|
|
118 s16[6*i+5] = convert (f[i]);
|
|
119 }
|
|
120 break;
|
|
121 }
|
|
122 }
|
|
123
|
|
124
|
|
125 int main(){
|
|
126 int accel=0;
|
|
127 int sample_rate=0;
|
|
128 int bit_rate=0;
|
|
129
|
|
130 samples = a52_init (accel);
|
|
131 if (samples == NULL) {
|
|
132 fprintf (stderr, "A52 init failed\n");
|
|
133 return 1;
|
|
134 }
|
|
135
|
|
136 while(1){
|
|
137 int length,i;
|
|
138 int16_t *s16;
|
|
139 sample_t level=1, bias=384;
|
|
140 int flags=0;
|
|
141
|
|
142 while(buf_size<7){
|
|
143 int c=getchar();
|
|
144 if(c<0) goto eof;
|
|
145 buf[buf_size++]=c;
|
|
146 }
|
|
147 length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
|
|
148 if(!length){
|
|
149 // bad file => resync
|
|
150 memcpy(buf,buf+1,6);
|
|
151 --buf_size;
|
|
152 continue;
|
|
153 }
|
|
154 fprintf(stderr,"sync. %d bytes 0x%X %d Hz %d kbit\n",length,flags,sample_rate,bit_rate);
|
|
155 while(buf_size<length){
|
|
156 buf[buf_size++]=getchar();
|
|
157 }
|
|
158
|
|
159 buf_size=0;
|
|
160
|
|
161 // decode:
|
|
162 flags=A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
|
|
163 flags |= A52_ADJUST_LEVEL;
|
|
164 if (a52_frame (&state, buf, &flags, &level, bias))
|
|
165 { fprintf(stderr,"error at decoding\n"); continue; }
|
|
166
|
|
167 // a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
|
|
168
|
|
169 s16 = out_buf;
|
|
170 for (i = 0; i < 6; i++) {
|
|
171 int32_t * f = (int32_t *) samples;
|
|
172 int i;
|
|
173 if (a52_block (&state, samples))
|
|
174 { fprintf(stderr,"error at sampling\n"); break; }
|
|
175 // resample to STEREO/DOLBY:
|
|
176 for (i = 0; i < 256; i++) {
|
|
177 s16[2*i] = convert (f[i]);
|
|
178 s16[2*i+1] = convert (f[i+256]);
|
|
179 }
|
|
180 s16+=2*i;
|
|
181 }
|
|
182 fwrite(out_buf,6*256*2*2,1,stdout);
|
|
183
|
|
184 }
|
|
185
|
|
186 eof:
|
|
187
|
|
188 }
|