2007-12-30 22:15:45 +00:00
|
|
|
/*
|
|
|
|
* liba52 sample by A'rpi/ESP-team
|
|
|
|
* Reads an AC-3 stream from stdin, decodes and downmixes to s16 stereo PCM
|
|
|
|
* and writes it to stdout. The resulting stream is playable with sox:
|
|
|
|
* play -c2 -r48000 -sw -fs out.sw
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Árpád Gereöffy
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2001-12-09 15:30:03 +00:00
|
|
|
|
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>
|
2004-08-07 14:15:55 +00:00
|
|
|
#include <string.h>
|
2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
#include "a52.h"
|
2001-12-30 21:38:53 +00:00
|
|
|
#include "mm_accel.h"
|
2008-04-28 14:50:24 +00:00
|
|
|
#include "cpudetect.h"
|
2001-12-09 15:30:03 +00:00
|
|
|
|
2007-12-23 11:29:36 +00:00
|
|
|
static a52_state_t *state;
|
2001-12-09 15:30:03 +00:00
|
|
|
static uint8_t buf[3840];
|
|
|
|
static int buf_size=0;
|
|
|
|
|
|
|
|
static int16_t out_buf[6*256*6];
|
|
|
|
|
2007-12-23 11:29:36 +00:00
|
|
|
void mp_msg( int x, const char *format, ... ) // stub for cpudetect.c
|
2004-08-07 14:15:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-12-15 22:32:14 +00:00
|
|
|
#ifdef TIMING
|
|
|
|
static inline long long rdtsc()
|
|
|
|
{
|
|
|
|
long long l;
|
2008-10-16 18:59:27 +00:00
|
|
|
__asm__ volatile("rdtsc\n\t"
|
2001-12-15 22:32:14 +00:00
|
|
|
: "=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
|
|
|
|
|
|
|
|
|
2007-11-21 09:22:33 +00:00
|
|
|
int main(void){
|
2001-12-09 15:30:03 +00:00
|
|
|
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-30 21:38:53 +00:00
|
|
|
// gCpuCaps.hasMMX=0;
|
|
|
|
// gCpuCaps.hasSSE=0;
|
|
|
|
if(gCpuCaps.hasMMX) accel |= MM_ACCEL_X86_MMX;
|
|
|
|
if(gCpuCaps.hasMMX2) accel |= MM_ACCEL_X86_MMXEXT;
|
|
|
|
if(gCpuCaps.hasSSE) accel |= MM_ACCEL_X86_SSE;
|
|
|
|
if(gCpuCaps.has3DNow) accel |= MM_ACCEL_X86_3DNOW;
|
|
|
|
// if(gCpuCaps.has3DNowExt) accel |= MM_ACCEL_X86_3DNOWEXT;
|
2001-12-18 04:00:29 +00:00
|
|
|
|
2007-12-23 11:29:36 +00:00
|
|
|
state = a52_init (accel);
|
|
|
|
if (state == NULL) {
|
2001-12-09 15:30:03 +00:00
|
|
|
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-30 21:38:53 +00:00
|
|
|
flags=A52_STEREO; //A52_STEREO; //A52_DOLBY; //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
|
2007-12-23 11:29:36 +00:00
|
|
|
if (a52_frame (state, buf, &flags, &level, bias))
|
2001-12-09 15:30:03 +00:00
|
|
|
{ fprintf(stderr,"error at decoding\n"); continue; }
|
2001-12-15 22:32:14 +00:00
|
|
|
ENDTIMING
|
2001-12-09 15:30:03 +00:00
|
|
|
|
2007-12-23 11:29:36 +00:00
|
|
|
// a52_dynrng (state, NULL, NULL); // disable dynamic range compensation
|
2001-12-09 15:30:03 +00:00
|
|
|
|
2001-12-18 04:00:29 +00:00
|
|
|
STARTTIMING
|
2001-12-30 21:38:53 +00:00
|
|
|
a52_resample_init(accel,flags,channels);
|
2001-12-09 15:30:03 +00:00
|
|
|
s16 = out_buf;
|
|
|
|
for (i = 0; i < 6; i++) {
|
2007-12-23 11:29:36 +00:00
|
|
|
if (a52_block (state))
|
2001-12-09 15:30:03 +00:00
|
|
|
{ fprintf(stderr,"error at sampling\n"); break; }
|
2001-12-17 17:21:50 +00:00
|
|
|
// float->int + channels interleaving:
|
2007-12-23 11:29:36 +00:00
|
|
|
s16+=a52_resample(a52_samples(state),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-30 21:38:53 +00:00
|
|
|
fwrite(out_buf,6*256*2*channels,1,stdout);
|
2001-12-09 15:30:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
eof:
|
2001-12-15 22:32:14 +00:00
|
|
|
#ifdef TIMING
|
2007-12-23 11:29:36 +00:00
|
|
|
fprintf(stderr, "%4.4fk cycles\n",min/1000.0);
|
2001-12-16 02:47:03 +00:00
|
|
|
sum=0;
|
2001-12-15 22:32:14 +00:00
|
|
|
#endif
|
2007-12-23 11:29:36 +00:00
|
|
|
return 0;
|
2001-12-09 15:30:03 +00:00
|
|
|
}
|