sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
blobcac671718a78605a77357049c7bd5afe37eae231
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define _XOPEN_SOURCE 600
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <math.h>
24 #include <assert.h>
26 #include "config.h"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 #include "mpbswap.h"
32 #include "ad_internal.h"
34 #include "cpudetect.h"
36 #include "libaf/af_format.h"
38 #ifdef CONFIG_LIBA52_INTERNAL
39 #include "liba52/a52.h"
40 #include "liba52/mm_accel.h"
41 #else
42 #include <a52dec/a52.h>
43 #include <a52dec/mm_accel.h>
44 int (* a52_resample) (float * _f, int16_t * s16);
45 #endif
47 static a52_state_t *a52_state;
48 static uint32_t a52_flags=0;
49 /** Used by a52_resample_float, it defines the mapping between liba52
50 * channels and output channels. The ith nibble from the right in the
51 * hex representation of channel_map is the index of the source
52 * channel corresponding to the ith output channel. Source channels are
53 * indexed 1-6. Silent output channels are marked by 0xf. */
54 static uint32_t channel_map;
56 #define DRC_NO_ACTION 0
57 #define DRC_NO_COMPRESSION 1
58 #define DRC_CALLBACK 2
60 /** The output is multiplied by this var. Used for volume control */
61 static sample_t a52_level = 1;
62 /** The value of the -a52drc switch. */
63 float a52_drc_level = 1.0;
64 static int a52_drc_action = DRC_NO_ACTION;
66 static const ad_info_t info =
68 "AC3 decoding with liba52",
69 "liba52",
70 "Nick Kurshev",
71 "Michel LESPINASSE",
75 LIBAD_EXTERN(liba52)
77 static int a52_fillbuff(sh_audio_t *sh_audio)
79 int length=0;
80 int flags=0;
81 int sample_rate=0;
82 int bit_rate=0;
84 sh_audio->a_in_buffer_len=0;
85 /* sync frame:*/
86 while(1){
87 while(sh_audio->a_in_buffer_len<8){
88 int c=demux_getc(sh_audio->ds);
89 if(c<0) return -1; /* EOF*/
90 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
92 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
93 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
94 if(length>=7 && length<=3840) break; /* we're done.*/
95 /* bad file => resync*/
96 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
97 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
98 --sh_audio->a_in_buffer_len;
100 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
101 sh_audio->samplerate=sample_rate;
102 sh_audio->i_bps=bit_rate/8;
103 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
104 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
105 if(sh_audio->format!=0x2000)
106 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
108 #ifdef CONFIG_LIBA52_INTERNAL
109 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
110 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
111 #endif
113 return length;
116 /* returns: number of available channels*/
117 static int a52_printinfo(sh_audio_t *sh_audio){
118 int flags, sample_rate, bit_rate;
119 char* mode="unknown";
120 int channels=0;
121 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
122 switch(flags&A52_CHANNEL_MASK){
123 case A52_CHANNEL: mode="channel"; channels=2; break;
124 case A52_MONO: mode="mono"; channels=1; break;
125 case A52_STEREO: mode="stereo"; channels=2; break;
126 case A52_3F: mode="3f";channels=3;break;
127 case A52_2F1R: mode="2f+1r";channels=3;break;
128 case A52_3F1R: mode="3f+1r";channels=4;break;
129 case A52_2F2R: mode="2f+2r";channels=4;break;
130 case A52_3F2R: mode="3f+2r";channels=5;break;
131 case A52_CHANNEL1: mode="channel1"; channels=2; break;
132 case A52_CHANNEL2: mode="channel2"; channels=2; break;
133 case A52_DOLBY: mode="dolby"; channels=2; break;
135 mp_msg(MSGT_DECAUDIO,MSGL_V,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
136 channels, (flags&A52_LFE)?1:0,
137 mode, (flags&A52_LFE)?"+lfe":"",
138 sample_rate, bit_rate*0.001f);
139 return (flags&A52_LFE) ? (channels+1) : channels;
142 static sample_t dynrng_call (sample_t c, void *data)
144 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)a52_drc_level, (double)pow((double)c, a52_drc_level));
145 return pow((double)c, a52_drc_level);
149 static int preinit(sh_audio_t *sh)
151 /* Dolby AC3 audio: */
152 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
153 #ifdef CONFIG_LIBA52_INTERNAL
154 if (sh->samplesize < 2) sh->samplesize = 2;
155 #else
156 if (sh->samplesize < 4) sh->samplesize = 4;
157 #endif
158 sh->audio_out_minsize=audio_output_channels*sh->samplesize*256*6;
159 sh->audio_in_minsize=3840;
160 a52_level = 1.0;
161 return 1;
165 * \brief Function to convert the "planar" float format used by liba52
166 * into the interleaved float format used by libaf/libao2.
167 * \param in the input buffer containing the planar samples.
168 * \param out the output buffer where the interleaved result is stored.
170 static int a52_resample_float(float *in, int16_t *out)
172 unsigned long i;
173 float *p = (float*) out;
174 for (i = 0; i != 256; i++) {
175 unsigned long map = channel_map;
176 do {
177 unsigned long ch = map & 15;
178 if (ch == 15)
179 *p = 0;
180 else
181 *p = in[i + ((ch-1)<<8)];
182 p++;
183 } while ((map >>= 4));
185 return (int16_t*) p - out;
188 static int init(sh_audio_t *sh_audio)
190 uint32_t a52_accel=0;
191 sample_t level=a52_level, bias=384;
192 int flags=0;
193 /* Dolby AC3 audio:*/
194 #ifdef MM_ACCEL_X86_SSE
195 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
196 #endif
197 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
198 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
199 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
200 #ifdef MM_ACCEL_X86_3DNOWEXT
201 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
202 #endif
203 #ifdef MM_ACCEL_PPC_ALTIVEC
204 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
205 #endif
206 a52_state=a52_init (a52_accel);
207 if (a52_state == NULL) {
208 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
209 return 0;
211 #ifndef CONFIG_LIBA52_INTERNAL
212 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
213 #endif
214 if(a52_fillbuff(sh_audio)<0){
215 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
216 return 0;
220 /* Init a52 dynrng */
221 if (a52_drc_level < 0.001) {
222 /* level == 0 --> no compression, init library without callback */
223 a52_drc_action = DRC_NO_COMPRESSION;
224 } else if (a52_drc_level > 0.999) {
225 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
226 a52_drc_action = DRC_NO_ACTION;
227 } else {
228 a52_drc_action = DRC_CALLBACK;
230 /* Library init for dynrng has to be done for each frame, see decode_audio() */
233 /* 'a52 cannot upmix' hotfix:*/
234 a52_printinfo(sh_audio);
235 sh_audio->channels=audio_output_channels;
236 while(sh_audio->channels>0){
237 switch(sh_audio->channels){
238 case 1: a52_flags=A52_MONO; break;
239 /* case 2: a52_flags=A52_STEREO; break;*/
240 case 2: a52_flags=A52_DOLBY; break;
241 /* case 3: a52_flags=A52_3F; break;*/
242 case 3: a52_flags=A52_2F1R; break;
243 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
244 case 5: a52_flags=A52_3F2R; break;
245 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
247 /* test:*/
248 flags=a52_flags|A52_ADJUST_LEVEL;
249 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
250 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
251 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
252 return 0;
254 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
255 /* frame decoded, let's init resampler:*/
256 channel_map = 0;
257 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
258 if (!(flags & A52_LFE)) {
259 switch ((flags<<3) | sh_audio->channels) {
260 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
261 case (A52_CHANNEL << 3) | 2:
262 case (A52_STEREO << 3) | 2:
263 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
264 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
265 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
266 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
267 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
269 } else if (sh_audio->channels == 6) {
270 switch (flags & ~A52_LFE) {
271 case A52_MONO : channel_map = 0x12ffff; break;
272 case A52_CHANNEL:
273 case A52_STEREO :
274 case A52_DOLBY : channel_map = 0x1fff32; break;
275 case A52_3F : channel_map = 0x13ff42; break;
276 case A52_2F1R : channel_map = 0x1f4432; break;
277 case A52_2F2R : channel_map = 0x1f5432; break;
278 case A52_3F2R : channel_map = 0x136542; break;
281 if (channel_map) {
282 a52_resample = a52_resample_float;
283 break;
285 } else
286 #ifdef CONFIG_LIBA52_INTERNAL
287 if(a52_resample_init(a52_accel,flags,sh_audio->channels)) break;
288 --sh_audio->channels; /* try to decrease no. of channels*/
289 #else
290 break;
291 #endif
293 if(sh_audio->channels<=0){
294 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
295 return 0;
297 return 1;
300 static void uninit(sh_audio_t *sh)
302 a52_free(a52_state);
305 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
307 switch(cmd)
309 case ADCTRL_RESYNC_STREAM:
310 case ADCTRL_SKIP_FRAME:
311 a52_fillbuff(sh);
312 return CONTROL_TRUE;
313 case ADCTRL_SET_VOLUME: {
314 float vol = *(float*)arg;
315 if (vol > 60.0) vol = 60.0;
316 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
317 return CONTROL_TRUE;
319 case ADCTRL_QUERY_FORMAT:
320 if (*(int*)arg == AF_FORMAT_S16_NE ||
321 *(int*)arg == AF_FORMAT_FLOAT_NE)
322 return CONTROL_TRUE;
323 return CONTROL_FALSE;
325 return CONTROL_UNKNOWN;
328 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
330 sample_t level=a52_level, bias=384;
331 int flags=a52_flags|A52_ADJUST_LEVEL;
332 int i,len=-1;
333 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
334 bias = 0;
335 if(!sh_audio->a_in_buffer_len)
336 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
337 sh_audio->a_in_buffer_len=0;
338 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
339 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
340 return len;
343 /* handle dynrng */
344 if (a52_drc_action != DRC_NO_ACTION) {
345 if (a52_drc_action == DRC_NO_COMPRESSION)
346 a52_dynrng(a52_state, NULL, NULL);
347 else
348 a52_dynrng(a52_state, dynrng_call, NULL);
351 len=0;
352 for (i = 0; i < 6; i++) {
353 if (a52_block (a52_state)){
354 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
355 break;
357 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
359 assert(len <= maxlen);
360 return len;