2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
// liba52 sample by A'rpi/ESP-team
|
|
|
|
// reads ac3 stream form stdin, decodes and downmix to s16 stereo pcm and
|
|
|
|
// writes it to stdout. resulting stream playbackable with sox:
|
|
|
|
// play -c 2 -r 48000 out.sw
|
|
|
|
|
2001-12-15 22:32:14 +00:00
|
|
|
//#define TIMING //needs Pentium or newer
|
|
|
|
|
2001-12-09 15:30:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "a52.h"
|
2001-12-18 04:00:29 +00:00
|
|
|
#include "../cpudetect.h"
|
2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
static sample_t * samples;
|
|
|
|
static a52_state_t state;
|
|
|
|
static uint8_t buf[3840];
|
|
|
|
static int buf_size=0;
|
|
|
|
|
|
|
|
static int16_t out_buf[6*256*6];
|
|
|
|
|
2001-12-15 22:32:14 +00:00
|
|
|
#ifdef TIMING
|
|
|
|
static inline long long rdtsc()
|
|
|
|
{
|
|
|
|
long long l;
|
|
|
|
asm volatile( "rdtsc\n\t"
|
|
|
|
: "=A" (l)
|
|
|
|
);
|
|
|
|
// printf("%d\n", int(l/1000));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STARTTIMING t=rdtsc();
|
|
|
|
#define ENDTIMING sum+=rdtsc()-t; t=rdtsc();
|
|
|
|
#else
|
|
|
|
#define STARTTIMING ;
|
|
|
|
#define ENDTIMING ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-12-09 15:30:03 +00:00
|
|
|
int main(){
|
|
|
|
int accel=0;
|
|
|
|
int sample_rate=0;
|
|
|
|
int bit_rate=0;
|
2001-12-15 22:32:14 +00:00
|
|
|
#ifdef TIMING
|
2001-12-16 02:47:03 +00:00
|
|
|
long long t, sum=0, min=256*256*256*64;
|
2001-12-15 22:32:14 +00:00
|
|
|
#endif
|
2001-12-09 15:30:03 +00:00
|
|
|
|
2001-12-18 04:00:29 +00:00
|
|
|
FILE *temp= stdout;
|
|
|
|
stdout= stderr; //EVIL HACK FIXME
|
|
|
|
GetCpuCaps(&gCpuCaps);
|
|
|
|
stdout= temp;
|
|
|
|
|
2001-12-09 15:30:03 +00:00
|
|
|
samples = a52_init (accel);
|
|
|
|
if (samples == NULL) {
|
|
|
|
fprintf (stderr, "A52 init failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
int length,i;
|
|
|
|
int16_t *s16;
|
|
|
|
sample_t level=1, bias=384;
|
|
|
|
int flags=0;
|
2001-12-17 17:21:50 +00:00
|
|
|
int channels=0;
|
2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
while(buf_size<7){
|
|
|
|
int c=getchar();
|
|
|
|
if(c<0) goto eof;
|
|
|
|
buf[buf_size++]=c;
|
|
|
|
}
|
2001-12-15 22:32:14 +00:00
|
|
|
STARTTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
|
2001-12-15 22:32:14 +00:00
|
|
|
ENDTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
if(!length){
|
|
|
|
// bad file => resync
|
|
|
|
memcpy(buf,buf+1,6);
|
|
|
|
--buf_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprintf(stderr,"sync. %d bytes 0x%X %d Hz %d kbit\n",length,flags,sample_rate,bit_rate);
|
|
|
|
while(buf_size<length){
|
|
|
|
buf[buf_size++]=getchar();
|
|
|
|
}
|
|
|
|
|
|
|
|
buf_size=0;
|
|
|
|
|
|
|
|
// decode:
|
2001-12-18 04:00:29 +00:00
|
|
|
flags=A52_STEREO; //A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
|
2001-12-17 17:21:50 +00:00
|
|
|
channels=2;
|
|
|
|
|
2001-12-09 15:30:03 +00:00
|
|
|
flags |= A52_ADJUST_LEVEL;
|
2001-12-15 22:32:14 +00:00
|
|
|
STARTTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
if (a52_frame (&state, buf, &flags, &level, bias))
|
|
|
|
{ fprintf(stderr,"error at decoding\n"); continue; }
|
2001-12-15 22:32:14 +00:00
|
|
|
ENDTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
// a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
|
|
|
|
|
2001-12-18 04:00:29 +00:00
|
|
|
STARTTIMING
|
2001-12-17 17:21:50 +00:00
|
|
|
a52_resample_init(flags,channels);
|
2001-12-09 15:30:03 +00:00
|
|
|
s16 = out_buf;
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
if (a52_block (&state, samples))
|
|
|
|
{ fprintf(stderr,"error at sampling\n"); break; }
|
2001-12-17 17:21:50 +00:00
|
|
|
// float->int + channels interleaving:
|
|
|
|
s16+=a52_resample(samples,s16);
|
2001-12-18 04:00:29 +00:00
|
|
|
ENDTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
}
|
2001-12-16 02:47:03 +00:00
|
|
|
#ifdef TIMING
|
|
|
|
if(sum<min) min=sum;
|
|
|
|
sum=0;
|
|
|
|
#endif
|
2001-12-09 15:30:03 +00:00
|
|
|
fwrite(out_buf,6*256*2*2,1,stdout);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
eof:
|
2001-12-15 22:32:14 +00:00
|
|
|
#ifdef TIMING
|
2001-12-16 02:47:03 +00:00
|
|
|
fprintf(stderr, "%4.4fk cycles ",min/1000.0);
|
|
|
|
sum=0;
|
2001-12-15 22:32:14 +00:00
|
|
|
#endif
|
2001-12-09 15:30:03 +00:00
|
|
|
}
|