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 }