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
7 //#define TIMING //needs Pentium or newer
16 #include "../cpudetect.h"
18 static sample_t
* samples
;
19 static a52_state_t state
;
20 static uint8_t buf
[3840];
21 static int buf_size
=0;
23 static int16_t out_buf
[6*256*6];
25 void mp_msg_c( int x
, const char *format
, ... ) // stub for cpudetect.c
30 static inline long long rdtsc()
33 asm volatile( "rdtsc\n\t"
36 // printf("%d\n", int(l/1000));
40 #define STARTTIMING t=rdtsc();
41 #define ENDTIMING sum+=rdtsc()-t; t=rdtsc();
53 long long t
, sum
=0, min
=256*256*256*64;
57 stdout
= stderr
; //EVIL HACK FIXME
58 GetCpuCaps(&gCpuCaps
);
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;
68 samples
= a52_init (accel
);
69 if (samples
== NULL
) {
70 fprintf (stderr
, "A52 init failed\n");
77 sample_t level
=1, bias
=384;
87 length
= a52_syncinfo (buf
, &flags
, &sample_rate
, &bit_rate
);
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();
103 flags
=A52_STEREO
; //A52_STEREO; //A52_DOLBY; //A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
106 flags
|= A52_ADJUST_LEVEL
;
108 if (a52_frame (&state
, buf
, &flags
, &level
, bias
))
109 { fprintf(stderr
,"error at decoding\n"); continue; }
112 // a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
115 a52_resample_init(accel
,flags
,channels
);
117 for (i
= 0; i
< 6; i
++) {
118 if (a52_block (&state
, samples
))
119 { fprintf(stderr
,"error at sampling\n"); break; }
120 // float->int + channels interleaving:
121 s16
+=a52_resample(samples
,s16
);
128 fwrite(out_buf
,6*256*2*channels
,1,stdout
);
134 fprintf(stderr
, "%4.4fk cycles ",min
/1000.0);