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