synced with r24954
[mplayer/glamo.git] / liba52 / test.c
blobbc32eb4c2a6f6f5221b41bbc9d8e09f3a17e2d5e
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
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <inttypes.h>
12 #include <string.h>
14 #include "a52.h"
15 #include "mm_accel.h"
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
29 #ifdef TIMING
30 static inline long long rdtsc()
32 long long l;
33 asm volatile( "rdtsc\n\t"
34 : "=A" (l)
36 // printf("%d\n", int(l/1000));
37 return l;
40 #define STARTTIMING t=rdtsc();
41 #define ENDTIMING sum+=rdtsc()-t; t=rdtsc();
42 #else
43 #define STARTTIMING ;
44 #define ENDTIMING ;
45 #endif
48 int main(){
49 int accel=0;
50 int sample_rate=0;
51 int bit_rate=0;
52 #ifdef TIMING
53 long long t, sum=0, min=256*256*256*64;
54 #endif
56 FILE *temp= stdout;
57 stdout= stderr; //EVIL HACK FIXME
58 GetCpuCaps(&gCpuCaps);
59 stdout= temp;
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;
68 samples = a52_init (accel);
69 if (samples == NULL) {
70 fprintf (stderr, "A52 init failed\n");
71 return 1;
74 while(1){
75 int length,i;
76 int16_t *s16;
77 sample_t level=1, bias=384;
78 int flags=0;
79 int channels=0;
81 while(buf_size<7){
82 int c=getchar();
83 if(c<0) goto eof;
84 buf[buf_size++]=c;
86 STARTTIMING
87 length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
88 ENDTIMING
89 if(!length){
90 // bad file => resync
91 memcpy(buf,buf+1,6);
92 --buf_size;
93 continue;
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();
100 buf_size=0;
102 // decode:
103 flags=A52_STEREO; //A52_STEREO; //A52_DOLBY; //A52_STEREO; // A52_DOLBY // A52_2F2R // A52_3F2R | A52_LFE
104 channels=2;
106 flags |= A52_ADJUST_LEVEL;
107 STARTTIMING
108 if (a52_frame (&state, buf, &flags, &level, bias))
109 { fprintf(stderr,"error at decoding\n"); continue; }
110 ENDTIMING
112 // a52_dynrng (&state, NULL, NULL); // disable dynamic range compensation
114 STARTTIMING
115 a52_resample_init(accel,flags,channels);
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; }
120 // float->int + channels interleaving:
121 s16+=a52_resample(samples,s16);
122 ENDTIMING
124 #ifdef TIMING
125 if(sum<min) min=sum;
126 sum=0;
127 #endif
128 fwrite(out_buf,6*256*2*channels,1,stdout);
132 eof:
133 #ifdef TIMING
134 fprintf(stderr, "%4.4fk cycles ",min/1000.0);
135 sum=0;
136 #endif