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
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"
42 #include <a52dec/a52.h>
43 #include <a52dec/mm_accel.h>
44 int (* a52_resample
) (float * _f
, int16_t * s16
);
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",
77 static int a52_fillbuff(sh_audio_t
*sh_audio
){
83 sh_audio
->a_in_buffer_len
=0;
86 while(sh_audio
->a_in_buffer_len
<8){
87 int c
=demux_getc(sh_audio
->ds
);
88 if(c
<0) return -1; /* EOF*/
89 sh_audio
->a_in_buffer
[sh_audio
->a_in_buffer_len
++]=c
;
91 if(sh_audio
->format
!=0x2000) swab(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
,8);
92 length
= a52_syncinfo (sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
93 if(length
>=7 && length
<=3840) break; /* we're done.*/
94 /* bad file => resync*/
95 if(sh_audio
->format
!=0x2000) swab(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
,8);
96 memmove(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
+1,7);
97 --sh_audio
->a_in_buffer_len
;
99 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length
,flags
,sample_rate
,bit_rate
);
100 sh_audio
->samplerate
=sample_rate
;
101 sh_audio
->i_bps
=bit_rate
/8;
102 sh_audio
->samplesize
=sh_audio
->sample_format
==AF_FORMAT_FLOAT_NE
? 4 : 2;
103 demux_read_data(sh_audio
->ds
,sh_audio
->a_in_buffer
+8,length
-8);
104 if(sh_audio
->format
!=0x2000)
105 swab(sh_audio
->a_in_buffer
+8,sh_audio
->a_in_buffer
+8,length
-8);
107 #ifdef CONFIG_LIBA52_INTERNAL
108 if(crc16_block(sh_audio
->a_in_buffer
+2,length
-2)!=0)
109 mp_msg(MSGT_DECAUDIO
,MSGL_STATUS
,"a52: CRC check failed! \n");
115 /* returns: number of available channels*/
116 static int a52_printinfo(sh_audio_t
*sh_audio
){
117 int flags
, sample_rate
, bit_rate
;
118 char* mode
="unknown";
120 a52_syncinfo (sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
121 switch(flags
&A52_CHANNEL_MASK
){
122 case A52_CHANNEL
: mode
="channel"; channels
=2; break;
123 case A52_MONO
: mode
="mono"; channels
=1; break;
124 case A52_STEREO
: mode
="stereo"; channels
=2; break;
125 case A52_3F
: mode
="3f";channels
=3;break;
126 case A52_2F1R
: mode
="2f+1r";channels
=3;break;
127 case A52_3F1R
: mode
="3f+1r";channels
=4;break;
128 case A52_2F2R
: mode
="2f+2r";channels
=4;break;
129 case A52_3F2R
: mode
="3f+2r";channels
=5;break;
130 case A52_CHANNEL1
: mode
="channel1"; channels
=2; break;
131 case A52_CHANNEL2
: mode
="channel2"; channels
=2; break;
132 case A52_DOLBY
: mode
="dolby"; channels
=2; break;
134 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
135 channels
, (flags
&A52_LFE
)?1:0,
136 mode
, (flags
&A52_LFE
)?"+lfe":"",
137 sample_rate
, bit_rate
*0.001f
);
138 return (flags
&A52_LFE
) ? (channels
+1) : channels
;
141 static sample_t
dynrng_call (sample_t c
, void *data
) {
142 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)a52_drc_level, (double)pow((double)c, a52_drc_level));
143 return pow((double)c
, a52_drc_level
);
147 static int preinit(sh_audio_t
*sh
)
149 /* Dolby AC3 audio: */
150 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
151 #ifdef CONFIG_LIBA52_INTERNAL
152 if (sh
->samplesize
< 2) sh
->samplesize
= 2;
154 if (sh
->samplesize
< 4) sh
->samplesize
= 4;
156 sh
->audio_out_minsize
=audio_output_channels
*sh
->samplesize
*256*6;
157 sh
->audio_in_minsize
=3840;
163 * \brief Function to convert the "planar" float format used by liba52
164 * into the interleaved float format used by libaf/libao2.
165 * \param in the input buffer containing the planar samples.
166 * \param out the output buffer where the interleaved result is stored.
168 static int a52_resample_float(float *in
, int16_t *out
)
171 float *p
= (float*) out
;
172 for (i
= 0; i
!= 256; i
++) {
173 unsigned long map
= channel_map
;
175 unsigned long ch
= map
& 15;
179 *p
= in
[i
+ ((ch
-1)<<8)];
181 } while ((map
>>= 4));
183 return (int16_t*) p
- out
;
186 static int init(sh_audio_t
*sh_audio
)
188 uint32_t a52_accel
=0;
189 sample_t level
=a52_level
, bias
=384;
191 /* Dolby AC3 audio:*/
192 #ifdef MM_ACCEL_X86_SSE
193 if(gCpuCaps
.hasSSE
) a52_accel
|=MM_ACCEL_X86_SSE
;
195 if(gCpuCaps
.hasMMX
) a52_accel
|=MM_ACCEL_X86_MMX
;
196 if(gCpuCaps
.hasMMX2
) a52_accel
|=MM_ACCEL_X86_MMXEXT
;
197 if(gCpuCaps
.has3DNow
) a52_accel
|=MM_ACCEL_X86_3DNOW
;
198 #ifdef MM_ACCEL_X86_3DNOWEXT
199 if(gCpuCaps
.has3DNowExt
) a52_accel
|=MM_ACCEL_X86_3DNOWEXT
;
201 #ifdef MM_ACCEL_PPC_ALTIVEC
202 if(gCpuCaps
.hasAltiVec
) a52_accel
|=MM_ACCEL_PPC_ALTIVEC
;
204 a52_state
=a52_init (a52_accel
);
205 if (a52_state
== NULL
) {
206 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"A52 init failed\n");
209 #ifndef CONFIG_LIBA52_INTERNAL
210 sh_audio
->sample_format
= AF_FORMAT_FLOAT_NE
;
212 if(a52_fillbuff(sh_audio
)<0){
213 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"A52 sync failed\n");
218 /* Init a52 dynrng */
219 if (a52_drc_level
< 0.001) {
220 /* level == 0 --> no compression, init library without callback */
221 a52_drc_action
= DRC_NO_COMPRESSION
;
222 } else if (a52_drc_level
> 0.999) {
223 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
224 a52_drc_action
= DRC_NO_ACTION
;
226 a52_drc_action
= DRC_CALLBACK
;
228 /* Library init for dynrng has to be done for each frame, see decode_audio() */
231 /* 'a52 cannot upmix' hotfix:*/
232 a52_printinfo(sh_audio
);
233 sh_audio
->channels
=audio_output_channels
;
234 while(sh_audio
->channels
>0){
235 switch(sh_audio
->channels
){
236 case 1: a52_flags
=A52_MONO
; break;
237 /* case 2: a52_flags=A52_STEREO; break;*/
238 case 2: a52_flags
=A52_DOLBY
; break;
239 /* case 3: a52_flags=A52_3F; break;*/
240 case 3: a52_flags
=A52_2F1R
; break;
241 case 4: a52_flags
=A52_2F2R
; break; /* 2+2*/
242 case 5: a52_flags
=A52_3F2R
; break;
243 case 6: a52_flags
=A52_3F2R
|A52_LFE
; break; /* 5.1*/
246 flags
=a52_flags
|A52_ADJUST_LEVEL
;
247 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"A52 flags before a52_frame: 0x%X\n",flags
);
248 if (a52_frame (a52_state
, sh_audio
->a_in_buffer
, &flags
, &level
, bias
)){
249 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"a52: error decoding frame -> nosound\n");
252 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"A52 flags after a52_frame: 0x%X\n",flags
);
253 /* frame decoded, let's init resampler:*/
255 if (sh_audio
->sample_format
== AF_FORMAT_FLOAT_NE
) {
256 if (!(flags
& A52_LFE
)) {
257 switch ((flags
<<3) | sh_audio
->channels
) {
258 case (A52_MONO
<< 3) | 1: channel_map
= 0x1; break;
259 case (A52_CHANNEL
<< 3) | 2:
260 case (A52_STEREO
<< 3) | 2:
261 case (A52_DOLBY
<< 3) | 2: channel_map
= 0x21; break;
262 case (A52_2F1R
<< 3) | 3: channel_map
= 0x321; break;
263 case (A52_2F2R
<< 3) | 4: channel_map
= 0x4321; break;
264 case (A52_3F
<< 3) | 5: channel_map
= 0x2ff31; break;
265 case (A52_3F2R
<< 3) | 5: channel_map
= 0x25431; break;
267 } else if (sh_audio
->channels
== 6) {
268 switch (flags
& ~A52_LFE
) {
269 case A52_MONO
: channel_map
= 0x12ffff; break;
272 case A52_DOLBY
: channel_map
= 0x1fff32; break;
273 case A52_3F
: channel_map
= 0x13ff42; break;
274 case A52_2F1R
: channel_map
= 0x1f4432; break;
275 case A52_2F2R
: channel_map
= 0x1f5432; break;
276 case A52_3F2R
: channel_map
= 0x136542; break;
280 a52_resample
= a52_resample_float
;
284 #ifdef CONFIG_LIBA52_INTERNAL
285 if(a52_resample_init(a52_accel
,flags
,sh_audio
->channels
)) break;
286 --sh_audio
->channels
; /* try to decrease no. of channels*/
291 if(sh_audio
->channels
<=0){
292 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"a52: no resampler. try different channel setup!\n");
298 static void uninit(sh_audio_t
*sh
)
303 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
307 case ADCTRL_RESYNC_STREAM
:
308 case ADCTRL_SKIP_FRAME
:
311 case ADCTRL_SET_VOLUME
: {
312 float vol
= *(float*)arg
;
313 if (vol
> 60.0) vol
= 60.0;
314 a52_level
= vol
<= -200.0 ? 0 : pow(10.0,vol
/20.0);
317 case ADCTRL_QUERY_FORMAT
:
318 if (*(int*)arg
== AF_FORMAT_S16_NE
||
319 *(int*)arg
== AF_FORMAT_FLOAT_NE
)
321 return CONTROL_FALSE
;
323 return CONTROL_UNKNOWN
;
326 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
328 sample_t level
=a52_level
, bias
=384;
329 int flags
=a52_flags
|A52_ADJUST_LEVEL
;
331 if (sh_audio
->sample_format
== AF_FORMAT_FLOAT_NE
)
333 if(!sh_audio
->a_in_buffer_len
)
334 if(a52_fillbuff(sh_audio
)<0) return len
; /* EOF */
335 sh_audio
->a_in_buffer_len
=0;
336 if (a52_frame (a52_state
, sh_audio
->a_in_buffer
, &flags
, &level
, bias
)){
337 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"a52: error decoding frame\n");
342 if (a52_drc_action
!= DRC_NO_ACTION
) {
343 if (a52_drc_action
== DRC_NO_COMPRESSION
)
344 a52_dynrng(a52_state
, NULL
, NULL
);
346 a52_dynrng(a52_state
, dynrng_call
, NULL
);
350 for (i
= 0; i
< 6; i
++) {
351 if (a52_block (a52_state
)){
352 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"a52: error at resampling\n");
355 len
+=2*a52_resample(a52_samples(a52_state
),(int16_t *)&buf
[len
]);
357 assert(len
<= maxlen
);