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
31 #include "ad_internal.h"
33 #include "cpudetect.h"
35 #include "libaf/af_format.h"
37 #include <a52dec/a52.h>
38 #include <a52dec/mm_accel.h>
39 int (* a52_resample
) (float * _f
, int16_t * s16
);
41 static a52_state_t
*a52_state
;
42 static uint32_t a52_flags
=0;
43 /** Used by a52_resample_float, it defines the mapping between liba52
44 * channels and output channels. The ith nibble from the right in the
45 * hex representation of channel_map is the index of the source
46 * channel corresponding to the ith output channel. Source channels are
47 * indexed 1-6. Silent output channels are marked by 0xf. */
48 static uint32_t channel_map
;
50 #define DRC_NO_ACTION 0
51 #define DRC_NO_COMPRESSION 1
52 #define DRC_CALLBACK 2
54 /** The output is multiplied by this var. Used for volume control */
55 static sample_t a52_level
= 1;
56 static int a52_drc_action
= DRC_NO_ACTION
;
58 static const ad_info_t info
=
60 "AC3 decoding with liba52",
69 static int a52_fillbuff(sh_audio_t
*sh_audio
)
76 sh_audio
->a_in_buffer_len
=0;
79 while(sh_audio
->a_in_buffer_len
<8){
80 int c
=demux_getc(sh_audio
->ds
);
81 if(c
<0) return -1; /* EOF*/
82 sh_audio
->a_in_buffer
[sh_audio
->a_in_buffer_len
++]=c
;
84 if(sh_audio
->format
!=0x2000) swab(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
,8);
85 length
= a52_syncinfo (sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
86 if(length
>=7 && length
<=3840) break; /* we're done.*/
87 /* bad file => resync*/
88 if(sh_audio
->format
!=0x2000) swab(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
,8);
89 memmove(sh_audio
->a_in_buffer
,sh_audio
->a_in_buffer
+1,7);
90 --sh_audio
->a_in_buffer_len
;
92 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length
,flags
,sample_rate
,bit_rate
);
93 sh_audio
->samplerate
=sample_rate
;
94 sh_audio
->i_bps
=bit_rate
/8;
95 sh_audio
->samplesize
=sh_audio
->sample_format
==AF_FORMAT_FLOAT_NE
? 4 : 2;
96 demux_read_data(sh_audio
->ds
,sh_audio
->a_in_buffer
+8,length
-8);
97 if(sh_audio
->format
!=0x2000)
98 swab(sh_audio
->a_in_buffer
+8,sh_audio
->a_in_buffer
+8,length
-8);
100 #ifdef CONFIG_LIBA52_INTERNAL
101 if(crc16_block(sh_audio
->a_in_buffer
+2,length
-2)!=0)
102 mp_msg(MSGT_DECAUDIO
,MSGL_STATUS
,"a52: CRC check failed! \n");
108 /* returns: number of available channels*/
109 static int a52_printinfo(sh_audio_t
*sh_audio
){
110 int flags
, sample_rate
, bit_rate
;
111 char* mode
="unknown";
113 a52_syncinfo (sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
114 switch(flags
&A52_CHANNEL_MASK
){
115 case A52_CHANNEL
: mode
="channel"; channels
=2; break;
116 case A52_MONO
: mode
="mono"; channels
=1; break;
117 case A52_STEREO
: mode
="stereo"; channels
=2; break;
118 case A52_3F
: mode
="3f";channels
=3;break;
119 case A52_2F1R
: mode
="2f+1r";channels
=3;break;
120 case A52_3F1R
: mode
="3f+1r";channels
=4;break;
121 case A52_2F2R
: mode
="2f+2r";channels
=4;break;
122 case A52_3F2R
: mode
="3f+2r";channels
=5;break;
123 case A52_CHANNEL1
: mode
="channel1"; channels
=2; break;
124 case A52_CHANNEL2
: mode
="channel2"; channels
=2; break;
125 case A52_DOLBY
: mode
="dolby"; channels
=2; break;
127 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
128 channels
, (flags
&A52_LFE
)?1:0,
129 mode
, (flags
&A52_LFE
)?"+lfe":"",
130 sample_rate
, bit_rate
*0.001f
);
131 return (flags
&A52_LFE
) ? (channels
+1) : channels
;
134 static sample_t
dynrng_call (sample_t c
, void *data
)
136 struct MPOpts
*opts
= data
;
137 //fprintf(stderr, "(%f, %f): %f\n", (double)c, (double)drc_level, (double)pow((double)c, drc_level));
138 //fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, opts->drc_level, pow(c, opts->drc_level));
139 return pow(c
, opts
->drc_level
);
143 static int preinit(sh_audio_t
*sh
)
145 struct MPOpts
*opts
= sh
->opts
;
146 /* Dolby AC3 audio: */
147 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
148 if (sh
->samplesize
< 4) sh
->samplesize
= 4;
149 sh
->audio_out_minsize
=opts
->audio_output_channels
*sh
->samplesize
*256*6;
150 sh
->audio_in_minsize
=3840;
156 * \brief Function to convert the "planar" float format used by liba52
157 * into the interleaved float format used by libaf/libao2.
158 * \param in the input buffer containing the planar samples.
159 * \param out the output buffer where the interleaved result is stored.
161 static int a52_resample_float(float *in
, int16_t *out
)
164 float *p
= (float*) out
;
165 for (i
= 0; i
!= 256; i
++) {
166 unsigned long map
= channel_map
;
168 unsigned long ch
= map
& 15;
172 *p
= in
[i
+ ((ch
-1)<<8)];
174 } while ((map
>>= 4));
176 return (int16_t*) p
- out
;
179 static int init(sh_audio_t
*sh_audio
)
181 struct MPOpts
*opts
= sh_audio
->opts
;
182 uint32_t a52_accel
=0;
183 sample_t level
=a52_level
, bias
=384;
185 /* Dolby AC3 audio:*/
186 #ifdef MM_ACCEL_X86_SSE
187 if(gCpuCaps
.hasSSE
) a52_accel
|=MM_ACCEL_X86_SSE
;
189 if(gCpuCaps
.hasMMX
) a52_accel
|=MM_ACCEL_X86_MMX
;
190 if(gCpuCaps
.hasMMX2
) a52_accel
|=MM_ACCEL_X86_MMXEXT
;
191 if(gCpuCaps
.has3DNow
) a52_accel
|=MM_ACCEL_X86_3DNOW
;
192 #ifdef MM_ACCEL_X86_3DNOWEXT
193 if(gCpuCaps
.has3DNowExt
) a52_accel
|=MM_ACCEL_X86_3DNOWEXT
;
195 #ifdef MM_ACCEL_PPC_ALTIVEC
196 if(gCpuCaps
.hasAltiVec
) a52_accel
|=MM_ACCEL_PPC_ALTIVEC
;
198 a52_state
=a52_init (a52_accel
);
199 if (a52_state
== NULL
) {
200 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"A52 init failed\n");
203 sh_audio
->sample_format
= AF_FORMAT_FLOAT_NE
;
204 if(a52_fillbuff(sh_audio
)<0){
205 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"A52 sync failed\n");
209 /* Init a52 dynrng */
210 if (opts
->drc_level
< 0.001) {
211 /* level == 0 --> no compression, init library without callback */
212 a52_drc_action
= DRC_NO_COMPRESSION
;
213 } else if (opts
->drc_level
> 0.999 && opts
->drc_level
< 1.001) {
214 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
215 a52_drc_action
= DRC_NO_ACTION
;
217 a52_drc_action
= DRC_CALLBACK
;
219 /* Library init for dynrng has to be done for each frame, see decode_audio() */
222 /* 'a52 cannot upmix' hotfix:*/
223 a52_printinfo(sh_audio
);
224 sh_audio
->channels
=opts
->audio_output_channels
;
225 while(sh_audio
->channels
>0){
226 switch(sh_audio
->channels
){
227 case 1: a52_flags
=A52_MONO
; break;
228 /* case 2: a52_flags=A52_STEREO; break;*/
229 case 2: a52_flags
=A52_DOLBY
; break;
230 /* case 3: a52_flags=A52_3F; break;*/
231 case 3: a52_flags
=A52_2F1R
; break;
232 case 4: a52_flags
=A52_2F2R
; break; /* 2+2*/
233 case 5: a52_flags
=A52_3F2R
; break;
234 case 6: a52_flags
=A52_3F2R
|A52_LFE
; break; /* 5.1*/
237 flags
=a52_flags
|A52_ADJUST_LEVEL
;
238 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"A52 flags before a52_frame: 0x%X\n",flags
);
239 if (a52_frame (a52_state
, sh_audio
->a_in_buffer
, &flags
, &level
, bias
)){
240 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"a52: error decoding frame -> nosound\n");
243 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"A52 flags after a52_frame: 0x%X\n",flags
);
244 /* frame decoded, let's init resampler:*/
246 if (sh_audio
->sample_format
== AF_FORMAT_FLOAT_NE
) {
247 if (!(flags
& A52_LFE
)) {
248 switch ((flags
<<3) | sh_audio
->channels
) {
249 case (A52_MONO
<< 3) | 1: channel_map
= 0x1; break;
250 case (A52_CHANNEL
<< 3) | 2:
251 case (A52_STEREO
<< 3) | 2:
252 case (A52_DOLBY
<< 3) | 2: channel_map
= 0x21; break;
253 case (A52_2F1R
<< 3) | 3: channel_map
= 0x321; break;
254 case (A52_2F2R
<< 3) | 4: channel_map
= 0x4321; break;
255 case (A52_3F
<< 3) | 5: channel_map
= 0x2ff31; break;
256 case (A52_3F2R
<< 3) | 5: channel_map
= 0x25431; break;
258 } else if (sh_audio
->channels
== 6) {
259 switch (flags
& ~A52_LFE
) {
260 case A52_MONO
: channel_map
= 0x12ffff; break;
263 case A52_DOLBY
: channel_map
= 0x1fff32; break;
264 case A52_3F
: channel_map
= 0x13ff42; break;
265 case A52_2F1R
: channel_map
= 0x1f4432; break;
266 case A52_2F2R
: channel_map
= 0x1f5432; break;
267 case A52_3F2R
: channel_map
= 0x136542; break;
271 a52_resample
= a52_resample_float
;
277 if(sh_audio
->channels
<=0){
278 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,"a52: no resampler. try different channel setup!\n");
284 static void uninit(sh_audio_t
*sh
)
289 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
293 case ADCTRL_RESYNC_STREAM
:
294 case ADCTRL_SKIP_FRAME
:
297 case ADCTRL_SET_VOLUME
: {
298 float vol
= *(float*)arg
;
299 if (vol
> 60.0) vol
= 60.0;
300 a52_level
= vol
<= -200.0 ? 0 : pow(10.0,vol
/20.0);
303 case ADCTRL_QUERY_FORMAT
:
304 if (*(int*)arg
== AF_FORMAT_S16_NE
||
305 *(int*)arg
== AF_FORMAT_FLOAT_NE
)
307 return CONTROL_FALSE
;
309 return CONTROL_UNKNOWN
;
312 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
314 sample_t level
=a52_level
, bias
=384;
315 int flags
=a52_flags
|A52_ADJUST_LEVEL
;
317 if (sh_audio
->sample_format
== AF_FORMAT_FLOAT_NE
)
319 if(!sh_audio
->a_in_buffer_len
)
320 if(a52_fillbuff(sh_audio
)<0) return len
; /* EOF */
321 sh_audio
->a_in_buffer_len
=0;
322 if (a52_frame (a52_state
, sh_audio
->a_in_buffer
, &flags
, &level
, bias
)){
323 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"a52: error decoding frame\n");
328 if (a52_drc_action
!= DRC_NO_ACTION
) {
329 if (a52_drc_action
== DRC_NO_COMPRESSION
)
330 a52_dynrng(a52_state
, NULL
, NULL
);
332 a52_dynrng(a52_state
, dynrng_call
, sh_audio
->opts
);
336 for (i
= 0; i
< 6; i
++) {
337 if (a52_block (a52_state
)){
338 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"a52: error at resampling\n");
341 len
+=2*a52_resample(a52_samples(a52_state
),(int16_t *)&buf
[len
]);
343 assert(len
<= maxlen
);