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"
|
3908
|
14 #include "mm_accel.h"
|
3579
|
15 #include "../cpudetect.h"
|
3396
|
16
|
|
17 static sample_t * samples;
|
|
18 static a52_state_t state;
|
|
19 static uint8_t buf[3840];
|
|
20 static int buf_size=0;
|
|
21
|
|
22 static int16_t out_buf[6*256*6];
|
|
23
|
3507
|
24 #ifdef TIMING
|
|
25 static inline long long rdtsc()
|
|
26 {
|
|
27 long long l;
|
|
28 asm volatile( "rdtsc\n\t"
|
|
29 : "=A" (l)
|
|
30 );
|
|
31 // printf("%d\n", int(l/1000));
|
|
32 return l;
|
|
33 }
|
|
34
|
|
35 #define STARTTIMING t=rdtsc();
|
|
36 #define ENDTIMING sum+=rdtsc()-t; t=rdtsc();
|
|
37 #else
|
|
38 #define STARTTIMING ;
|
|
39 #define ENDTIMING ;
|
|
40 #endif
|
|
41
|
|
42
|
3396
|
43 int main(){
|
|
44 int accel=0;
|
|
45 int sample_rate=0;
|
|
46 int bit_rate=0;
|
3507
|
47 #ifdef TIMING
|
3511
|
48 long long t, sum=0, min=256*256*256*64;
|
3507
|
49 #endif
|
3396
|
50
|
3579
|
51 FILE *temp= stdout;
|
|
52 stdout= stderr; //EVIL HACK FIXME
|
|
53 GetCpuCaps(&gCpuCaps);
|
|
54 stdout= temp;
|
3908
|
55 // gCpuCaps.hasMMX=0;
|
|
56 // gCpuCaps.hasSSE=0;
|
|
57 if(gCpuCaps.hasMMX) accel |= MM_ACCEL_X86_MMX;
|
|
58 if(gCpuCaps.hasMMX2) accel |= MM_ACCEL_X86_MMXEXT;
|
|
59 if(gCpuCaps.hasSSE) accel |= MM_ACCEL_X86_SSE;
|
|
60 if(gCpuCaps.has3DNow) accel |= MM_ACCEL_X86_3DNOW;
|
|
61 // if(gCpuCaps.has3DNowExt) accel |= MM_ACCEL_X86_3DNOWEXT;
|
3579
|
62
|
3396
|
63 samples = a52_init (accel);
|
|
64 if (samples == NULL) {
|
|
65 fprintf (stderr, "A52 init failed\n");
|
|
66 return 1;
|
|
67 }
|
|
68
|
|
69 while(1){
|
|
70 int length,i;
|
|
71 int16_t *s16;
|
|
72 sample_t level=1, bias=384;
|
|
73 int flags=0;
|
3565
|
74 int channels=0;
|
3396
|
75
|
|
76 while(buf_size<7){
|
|
77 int c=getchar();
|
|
78 if(c<0) goto eof;
|
|
79 buf[buf_size++]=c;
|
|
80 }
|
3507
|
81 STARTTIMING
|
3396
|
82 length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
|
3507
|
83 ENDTIMING
|
3396
|
84 if(!length){
|
|
85 // bad file => resync
|
|
86 memcpy(buf,buf+1,6);
|
|
87 --buf_size;
|
|
88 continue;
|
|
89 }
|
|
90 fprintf(stderr,"sync. %d bytes 0x%X %d Hz %d kbit\n",length,flags,sample_rate,bit_rate);
|
|
91 while(buf_size<length){
|
|
92 buf[buf_size++]=getchar();
|
|
93 }
|
|
94
|
|
95 buf_size=0;
|
|
96
|
|
97 // decode:
|
3908
|
98 flags=A52_STEREO; //A52_STEREO; //A52_DOLBY; //A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
|
3565
|
99 channels=2;
|
|
100
|
3396
|
101 flags |= A52_ADJUST_LEVEL;
|
3507
|
102 STARTTIMING
|
3396
|
103 if (a52_frame (&state, buf, &flags, &level, bias))
|
|
104 { fprintf(stderr,"error at decoding\n"); continue; }
|
3507
|
105 ENDTIMING
|
3396
|
106
|
|
107 // a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
|
|
108
|
3579
|
109 STARTTIMING
|
3908
|
110 a52_resample_init(accel,flags,channels);
|
3396
|
111 s16 = out_buf;
|
|
112 for (i = 0; i < 6; i++) {
|
|
113 if (a52_block (&state, samples))
|
|
114 { fprintf(stderr,"error at sampling\n"); break; }
|
3565
|
115 // float->int + channels interleaving:
|
|
116 s16+=a52_resample(samples,s16);
|
3579
|
117 ENDTIMING
|
3396
|
118 }
|
3511
|
119 #ifdef TIMING
|
|
120 if(sum<min) min=sum;
|
|
121 sum=0;
|
|
122 #endif
|
3908
|
123 fwrite(out_buf,6*256*2*channels,1,stdout);
|
3396
|
124
|
|
125 }
|
|
126
|
|
127 eof:
|
3507
|
128 #ifdef TIMING
|
3511
|
129 fprintf(stderr, "%4.4fk cycles ",min/1000.0);
|
|
130 sum=0;
|
3507
|
131 #endif
|
3396
|
132 }
|