Only compile fastmemcpybench on x86.
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
bloba867fae5334612caa73cd2cba68ca175eb6f3c37
1 #define _XOPEN_SOURCE 600
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <math.h>
6 #include <assert.h>
8 #include "config.h"
10 #include "mp_msg.h"
11 #include "help_mp.h"
12 #include "mpbswap.h"
14 #include "ad_internal.h"
16 #include "cpudetect.h"
18 #include "libaf/af_format.h"
20 #ifdef CONFIG_LIBA52_INTERNAL
21 #include "liba52/a52.h"
22 #include "liba52/mm_accel.h"
23 #else
24 #include <a52dec/a52.h>
25 #include <a52dec/mm_accel.h>
26 int (* a52_resample) (float * _f, int16_t * s16);
27 #endif
29 static a52_state_t *a52_state;
30 static uint32_t a52_flags=0;
31 /** Used by a52_resample_float, it defines the mapping between liba52
32 * channels and output channels. The ith nibble from the right in the
33 * hex representation of channel_map is the index of the source
34 * channel corresponding to the ith output channel. Source channels are
35 * indexed 1-6. Silent output channels are marked by 0xf. */
36 static uint32_t channel_map;
38 #define DRC_NO_ACTION 0
39 #define DRC_NO_COMPRESSION 1
40 #define DRC_CALLBACK 2
42 /** The output is multiplied by this var. Used for volume control */
43 static sample_t a52_level = 1;
44 /** The value of the -a52drc switch. */
45 float a52_drc_level = 1.0;
46 static int a52_drc_action = DRC_NO_ACTION;
48 static ad_info_t info =
50 "AC3 decoding with liba52",
51 "liba52",
52 "Nick Kurshev",
53 "Michel LESPINASSE",
57 LIBAD_EXTERN(liba52)
59 int a52_fillbuff(sh_audio_t *sh_audio){
60 int length=0;
61 int flags=0;
62 int sample_rate=0;
63 int bit_rate=0;
65 sh_audio->a_in_buffer_len=0;
66 /* sync frame:*/
67 while(1){
68 while(sh_audio->a_in_buffer_len<8){
69 int c=demux_getc(sh_audio->ds);
70 if(c<0) return -1; /* EOF*/
71 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
73 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
74 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
75 if(length>=7 && length<=3840) break; /* we're done.*/
76 /* bad file => resync*/
77 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
78 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
79 --sh_audio->a_in_buffer_len;
81 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
82 sh_audio->samplerate=sample_rate;
83 sh_audio->i_bps=bit_rate/8;
84 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
85 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
86 if(sh_audio->format!=0x2000)
87 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
89 #ifdef CONFIG_LIBA52_INTERNAL
90 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
91 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
92 #endif
94 return length;
97 /* returns: number of available channels*/
98 static int a52_printinfo(sh_audio_t *sh_audio){
99 int flags, sample_rate, bit_rate;
100 char* mode="unknown";
101 int channels=0;
102 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
103 switch(flags&A52_CHANNEL_MASK){
104 case A52_CHANNEL: mode="channel"; channels=2; break;
105 case A52_MONO: mode="mono"; channels=1; break;
106 case A52_STEREO: mode="stereo"; channels=2; break;
107 case A52_3F: mode="3f";channels=3;break;
108 case A52_2F1R: mode="2f+1r";channels=3;break;
109 case A52_3F1R: mode="3f+1r";channels=4;break;
110 case A52_2F2R: mode="2f+2r";channels=4;break;
111 case A52_3F2R: mode="3f+2r";channels=5;break;
112 case A52_CHANNEL1: mode="channel1"; channels=2; break;
113 case A52_CHANNEL2: mode="channel2"; channels=2; break;
114 case A52_DOLBY: mode="dolby"; channels=2; break;
116 mp_msg(MSGT_DECAUDIO,MSGL_V,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
117 channels, (flags&A52_LFE)?1:0,
118 mode, (flags&A52_LFE)?"+lfe":"",
119 sample_rate, bit_rate*0.001f);
120 return (flags&A52_LFE) ? (channels+1) : channels;
123 sample_t dynrng_call (sample_t c, void *data) {
124 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)a52_drc_level, (double)pow((double)c, a52_drc_level));
125 return pow((double)c, a52_drc_level);
129 static int preinit(sh_audio_t *sh)
131 /* Dolby AC3 audio: */
132 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
133 #ifdef CONFIG_LIBA52_INTERNAL
134 if (sh->samplesize < 2) sh->samplesize = 2;
135 #else
136 if (sh->samplesize < 4) sh->samplesize = 4;
137 #endif
138 sh->audio_out_minsize=audio_output_channels*sh->samplesize*256*6;
139 sh->audio_in_minsize=3840;
140 a52_level = 1.0;
141 return 1;
145 * \brief Function to convert the "planar" float format used by liba52
146 * into the interleaved float format used by libaf/libao2.
147 * \param in the input buffer containing the planar samples.
148 * \param out the output buffer where the interleaved result is stored.
150 static int a52_resample_float(float *in, int16_t *out)
152 unsigned long i;
153 float *p = (float*) out;
154 for (i = 0; i != 256; i++) {
155 unsigned long map = channel_map;
156 do {
157 unsigned long ch = map & 15;
158 if (ch == 15)
159 *p = 0;
160 else
161 *p = in[i + ((ch-1)<<8)];
162 p++;
163 } while ((map >>= 4));
165 return (int16_t*) p - out;
168 static int init(sh_audio_t *sh_audio)
170 uint32_t a52_accel=0;
171 sample_t level=a52_level, bias=384;
172 int flags=0;
173 /* Dolby AC3 audio:*/
174 #ifdef MM_ACCEL_X86_SSE
175 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
176 #endif
177 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
178 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
179 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
180 #ifdef MM_ACCEL_X86_3DNOWEXT
181 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
182 #endif
183 #ifdef MM_ACCEL_PPC_ALTIVEC
184 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
185 #endif
186 a52_state=a52_init (a52_accel);
187 if (a52_state == NULL) {
188 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
189 return 0;
191 #ifndef CONFIG_LIBA52_INTERNAL
192 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
193 #endif
194 if(a52_fillbuff(sh_audio)<0){
195 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
196 return 0;
200 /* Init a52 dynrng */
201 if (a52_drc_level < 0.001) {
202 /* level == 0 --> no compression, init library without callback */
203 a52_drc_action = DRC_NO_COMPRESSION;
204 } else if (a52_drc_level > 0.999) {
205 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
206 a52_drc_action = DRC_NO_ACTION;
207 } else {
208 a52_drc_action = DRC_CALLBACK;
210 /* Library init for dynrng has to be done for each frame, see decode_audio() */
213 /* 'a52 cannot upmix' hotfix:*/
214 a52_printinfo(sh_audio);
215 sh_audio->channels=audio_output_channels;
216 while(sh_audio->channels>0){
217 switch(sh_audio->channels){
218 case 1: a52_flags=A52_MONO; break;
219 /* case 2: a52_flags=A52_STEREO; break;*/
220 case 2: a52_flags=A52_DOLBY; break;
221 /* case 3: a52_flags=A52_3F; break;*/
222 case 3: a52_flags=A52_2F1R; break;
223 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
224 case 5: a52_flags=A52_3F2R; break;
225 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
227 /* test:*/
228 flags=a52_flags|A52_ADJUST_LEVEL;
229 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
230 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
231 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
232 return 0;
234 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
235 /* frame decoded, let's init resampler:*/
236 channel_map = 0;
237 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
238 if (!(flags & A52_LFE)) {
239 switch ((flags<<3) | sh_audio->channels) {
240 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
241 case (A52_CHANNEL << 3) | 2:
242 case (A52_STEREO << 3) | 2:
243 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
244 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
245 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
246 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
247 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
249 } else if (sh_audio->channels == 6) {
250 switch (flags & ~A52_LFE) {
251 case A52_MONO : channel_map = 0x12ffff; break;
252 case A52_CHANNEL:
253 case A52_STEREO :
254 case A52_DOLBY : channel_map = 0x1fff32; break;
255 case A52_3F : channel_map = 0x13ff42; break;
256 case A52_2F1R : channel_map = 0x1f4432; break;
257 case A52_2F2R : channel_map = 0x1f5432; break;
258 case A52_3F2R : channel_map = 0x136542; break;
261 if (channel_map) {
262 a52_resample = a52_resample_float;
263 break;
265 } else
266 #ifdef CONFIG_LIBA52_INTERNAL
267 if(a52_resample_init(a52_accel,flags,sh_audio->channels)) break;
268 --sh_audio->channels; /* try to decrease no. of channels*/
269 #else
270 break;
271 #endif
273 if(sh_audio->channels<=0){
274 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
275 return 0;
277 return 1;
280 static void uninit(sh_audio_t *sh)
282 a52_free(a52_state);
285 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
287 switch(cmd)
289 case ADCTRL_RESYNC_STREAM:
290 case ADCTRL_SKIP_FRAME:
291 a52_fillbuff(sh);
292 return CONTROL_TRUE;
293 case ADCTRL_SET_VOLUME: {
294 float vol = *(float*)arg;
295 if (vol > 60.0) vol = 60.0;
296 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
297 return CONTROL_TRUE;
299 case ADCTRL_QUERY_FORMAT:
300 if (*(int*)arg == AF_FORMAT_S16_NE ||
301 *(int*)arg == AF_FORMAT_FLOAT_NE)
302 return CONTROL_TRUE;
303 return CONTROL_FALSE;
305 return CONTROL_UNKNOWN;
308 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
310 sample_t level=a52_level, bias=384;
311 int flags=a52_flags|A52_ADJUST_LEVEL;
312 int i,len=-1;
313 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
314 bias = 0;
315 if(!sh_audio->a_in_buffer_len)
316 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
317 sh_audio->a_in_buffer_len=0;
318 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
319 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
320 return len;
323 /* handle dynrng */
324 if (a52_drc_action != DRC_NO_ACTION) {
325 if (a52_drc_action == DRC_NO_COMPRESSION)
326 a52_dynrng(a52_state, NULL, NULL);
327 else
328 a52_dynrng(a52_state, dynrng_call, NULL);
331 len=0;
332 for (i = 0; i < 6; i++) {
333 if (a52_block (a52_state)){
334 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
335 break;
337 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
339 assert(len <= maxlen);
340 return len;