12 #include "codec-cfg.h"
15 #include "dec_audio.h"
17 #include "libaf/af_format.h"
21 #ifdef DYNAMIC_PLUGINS
28 /* used for ac3surround decoder - set using -channels option */
29 int audio_output_channels
= 2;
30 af_cfg_t af_cfg
= {1, NULL
}; // Configuration for audio filters
34 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_AvailableAudioFm
);
36 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_AUDIO_DRIVERS\n");
37 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
," afm: info: (comment)\n");
38 for (i
=0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
39 if(mpcodecs_ad_drivers
[i
]->info
->comment
&& mpcodecs_ad_drivers
[i
]->info
->comment
[0])
40 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,"%9s %s (%s)\n",
41 mpcodecs_ad_drivers
[i
]->info
->short_name
,
42 mpcodecs_ad_drivers
[i
]->info
->name
,
43 mpcodecs_ad_drivers
[i
]->info
->comment
);
45 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,"%9s %s\n",
46 mpcodecs_ad_drivers
[i
]->info
->short_name
,
47 mpcodecs_ad_drivers
[i
]->info
->name
);
50 int init_audio_codec(sh_audio_t
*sh_audio
)
52 if ((af_cfg
.force
& AF_INIT_FORMAT_MASK
) == AF_INIT_FLOAT
) {
53 int fmt
= AF_FORMAT_FLOAT_NE
;
54 if (sh_audio
->ad_driver
->control(sh_audio
, ADCTRL_QUERY_FORMAT
,
55 &fmt
) == CONTROL_TRUE
) {
56 sh_audio
->sample_format
= fmt
;
57 sh_audio
->samplesize
= 4;
60 if(!sh_audio
->ad_driver
->preinit(sh_audio
))
62 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_ADecoderPreinitFailed
);
66 /* allocate audio in buffer: */
67 if(sh_audio
->audio_in_minsize
>0){
68 sh_audio
->a_in_buffer_size
=sh_audio
->audio_in_minsize
;
69 mp_msg(MSGT_DECAUDIO
,MSGL_V
,MSGTR_AllocatingBytesForInputBuffer
,
70 sh_audio
->a_in_buffer_size
);
71 sh_audio
->a_in_buffer
=malloc(sh_audio
->a_in_buffer_size
);
72 memset(sh_audio
->a_in_buffer
,0,sh_audio
->a_in_buffer_size
);
73 sh_audio
->a_in_buffer_len
=0;
76 /* allocate audio out buffer: */
77 sh_audio
->a_buffer_size
=sh_audio
->audio_out_minsize
+MAX_OUTBURST
; /* worst case calc.*/
79 mp_msg(MSGT_DECAUDIO
,MSGL_V
,MSGTR_AllocatingBytesForOutputBuffer
,
80 sh_audio
->audio_out_minsize
,MAX_OUTBURST
,sh_audio
->a_buffer_size
);
82 sh_audio
->a_buffer
=malloc(sh_audio
->a_buffer_size
);
83 if(!sh_audio
->a_buffer
){
84 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_CantAllocAudioBuf
);
87 memset(sh_audio
->a_buffer
,0,sh_audio
->a_buffer_size
);
88 sh_audio
->a_buffer_len
=0;
90 if(!sh_audio
->ad_driver
->init(sh_audio
)){
91 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,MSGTR_ADecoderInitFailed
);
92 uninit_audio(sh_audio
); // free buffers
98 if(!sh_audio
->channels
|| !sh_audio
->samplerate
){
99 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,MSGTR_UnknownAudio
);
100 uninit_audio(sh_audio
); // free buffers
105 sh_audio
->o_bps
=sh_audio
->channels
*sh_audio
->samplerate
*sh_audio
->samplesize
;
107 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,"AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
108 sh_audio
->samplerate
,sh_audio
->channels
,
109 af_fmt2str_short(sh_audio
->sample_format
),
110 sh_audio
->i_bps
*8*0.001,((float)sh_audio
->i_bps
/sh_audio
->o_bps
)*100.0,
111 sh_audio
->i_bps
,sh_audio
->o_bps
);
113 sh_audio
->a_out_buffer_size
=sh_audio
->a_buffer_size
;
114 sh_audio
->a_out_buffer
=sh_audio
->a_buffer
;
115 sh_audio
->a_out_buffer_len
=sh_audio
->a_buffer_len
;
120 int init_audio(sh_audio_t
*sh_audio
,char* codecname
,char* afm
,int status
){
121 unsigned int orig_fourcc
=sh_audio
->wf
?sh_audio
->wf
->wFormatTag
:0;
123 if (codecname
&& codecname
[0] == '+') {
124 codecname
= &codecname
[1];
127 sh_audio
->codec
=NULL
;
129 ad_functions_t
* mpadec
;
131 sh_audio
->ad_driver
= 0;
132 // restore original fourcc:
133 if(sh_audio
->wf
) sh_audio
->wf
->wFormatTag
=i
=orig_fourcc
;
134 if(!(sh_audio
->codec
=find_audio_codec(sh_audio
->format
,
135 sh_audio
->wf
?(&i
):NULL
, sh_audio
->codec
, force
) )) break;
136 if(sh_audio
->wf
) sh_audio
->wf
->wFormatTag
=i
;
137 // ok we found one codec
138 if(sh_audio
->codec
->flags
&CODECS_FLAG_SELECTED
) continue; // already tried & failed
139 if(codecname
&& strcmp(sh_audio
->codec
->name
,codecname
)) continue; // -ac
140 if(afm
&& strcmp(sh_audio
->codec
->drv
,afm
)) continue; // afm doesn't match
141 if(!force
&& sh_audio
->codec
->status
<status
) continue; // too unstable
142 sh_audio
->codec
->flags
|=CODECS_FLAG_SELECTED
; // tagging it
143 // ok, it matches all rules, let's find the driver!
144 for (i
=0; mpcodecs_ad_drivers
[i
] != NULL
; i
++)
145 if(!strcmp(mpcodecs_ad_drivers
[i
]->info
->short_name
,sh_audio
->codec
->drv
)) break;
146 mpadec
=mpcodecs_ad_drivers
[i
];
147 #ifdef DYNAMIC_PLUGINS
150 /* try to open shared decoder plugin */
153 ad_functions_t
*funcs_sym
;
156 buf_len
= strlen(MPLAYER_LIBDIR
)+strlen(sh_audio
->codec
->drv
)+16;
157 buf
= malloc(buf_len
);
160 snprintf(buf
, buf_len
, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR
, sh_audio
->codec
->drv
);
161 mp_msg(MSGT_DECAUDIO
, MSGL_DBG2
, "Trying to open external plugin: %s\n", buf
);
162 sh_audio
->dec_handle
= dlopen(buf
, RTLD_LAZY
);
163 if (!sh_audio
->dec_handle
)
165 snprintf(buf
, buf_len
, "mpcodecs_ad_%s", sh_audio
->codec
->drv
);
166 funcs_sym
= dlsym(sh_audio
->dec_handle
, buf
);
167 if (!funcs_sym
|| !funcs_sym
->info
|| !funcs_sym
->preinit
||
168 !funcs_sym
->init
|| !funcs_sym
->uninit
|| !funcs_sym
->control
||
169 !funcs_sym
->decode_audio
)
171 info_sym
= funcs_sym
->info
;
172 if (strcmp(info_sym
->short_name
, sh_audio
->codec
->drv
))
176 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
177 MPLAYER_LIBDIR
, sh_audio
->codec
->drv
);
180 if(!mpadec
){ // driver not available (==compiled in)
181 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_AudioCodecFamilyNotAvailableStr
,
182 sh_audio
->codec
->name
, sh_audio
->codec
->drv
);
185 // it's available, let's try to init!
187 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_OpeningAudioDecoder
,mpadec
->info
->short_name
,mpadec
->info
->name
);
188 sh_audio
->ad_driver
= mpadec
;
189 if(!init_audio_codec(sh_audio
)){
190 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_ADecoderInitFailed
);
191 continue; // try next...
199 extern char *get_path(char *filename
);
201 int init_best_audio_codec(sh_audio_t
*sh_audio
,char** audio_codec_list
,char** audio_fm_list
){
202 char* ac_l_default
[2]={"",(char*)NULL
};
204 if(!audio_codec_list
) audio_codec_list
=ac_l_default
;
205 // Go through the codec.conf and find the best codec...
207 codecs_reset_selection(1);
208 while(!sh_audio
->inited
&& *audio_codec_list
){
209 char* audio_codec
=*(audio_codec_list
++);
211 if(audio_codec
[0]=='-'){
212 // disable this codec:
213 select_codec(audio_codec
+1,1);
215 // forced codec by name:
216 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_ForcedAudioCodec
,audio_codec
);
217 init_audio(sh_audio
,audio_codec
,NULL
,-1);
221 // try in stability order: UNTESTED, WORKING, BUGGY. never try CRASHING.
223 char** fmlist
=audio_fm_list
;
224 // try first the preferred codec families:
225 while(!sh_audio
->inited
&& *fmlist
){
226 char* audio_fm
=*(fmlist
++);
227 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_TryForceAudioFmtStr
,audio_fm
);
228 for(status
=CODECS_STATUS__MAX
;status
>=CODECS_STATUS__MIN
;--status
)
229 if(init_audio(sh_audio
,NULL
,audio_fm
,status
)) break;
232 if(!sh_audio
->inited
)
233 for(status
=CODECS_STATUS__MAX
;status
>=CODECS_STATUS__MIN
;--status
)
234 if(init_audio(sh_audio
,NULL
,NULL
,status
)) break;
238 if(!sh_audio
->inited
){
239 mp_msg(MSGT_DECAUDIO
,MSGL_ERR
,MSGTR_CantFindAudioCodec
,sh_audio
->format
);
240 mp_msg(MSGT_DECAUDIO
,MSGL_HINT
, MSGTR_RTFMCodecs
);
244 mp_msg(MSGT_DECAUDIO
,MSGL_INFO
,MSGTR_SelectedAudioCodec
,
245 sh_audio
->codec
->name
,sh_audio
->codec
->drv
,sh_audio
->codec
->info
);
249 void uninit_audio(sh_audio_t
*sh_audio
)
251 if(sh_audio
->afilter
){
252 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Uninit audio filters...\n");
253 af_uninit(sh_audio
->afilter
);
254 free(sh_audio
->afilter
);
255 sh_audio
->afilter
=NULL
;
257 if(sh_audio
->inited
){
258 mp_msg(MSGT_DECAUDIO
,MSGL_V
,MSGTR_UninitAudioStr
,sh_audio
->codec
->drv
);
259 sh_audio
->ad_driver
->uninit(sh_audio
);
260 #ifdef DYNAMIC_PLUGINS
261 if (sh_audio
->dec_handle
)
262 dlclose(sh_audio
->dec_handle
);
266 if(sh_audio
->a_out_buffer
!=sh_audio
->a_buffer
) free(sh_audio
->a_out_buffer
);
267 sh_audio
->a_out_buffer
=NULL
;
268 sh_audio
->a_out_buffer_size
=0;
269 if(sh_audio
->a_buffer
) free(sh_audio
->a_buffer
);
270 sh_audio
->a_buffer
=NULL
;
271 if(sh_audio
->a_in_buffer
) free(sh_audio
->a_in_buffer
);
272 sh_audio
->a_in_buffer
=NULL
;
275 /* Init audio filters */
276 int preinit_audio_filters(sh_audio_t
*sh_audio
,
277 int in_samplerate
, int in_channels
, int in_format
,
278 int* out_samplerate
, int* out_channels
, int* out_format
){
279 return init_audio_filters(sh_audio
, in_samplerate
, in_channels
, in_format
,
280 out_samplerate
, out_channels
, out_format
, 0, 0);
283 /* Init audio filters */
284 int init_audio_filters(sh_audio_t
*sh_audio
,
285 int in_samplerate
, int in_channels
, int in_format
,
286 int *out_samplerate
, int *out_channels
, int *out_format
,
287 int out_minsize
, int out_maxsize
){
288 af_stream_t
* afs
=sh_audio
->afilter
;
290 afs
= (af_stream_t
*)malloc(sizeof(af_stream_t
));
291 memset(afs
,0,sizeof(af_stream_t
));
294 // input format: same as codec's output format:
295 afs
->input
.rate
= in_samplerate
;
296 afs
->input
.nch
= in_channels
;
297 afs
->input
.format
= in_format
;
298 af_fix_parameters(&(afs
->input
));
300 // output format: same as ao driver's input format (if missing, fallback to input)
301 afs
->output
.rate
= *out_samplerate
;
302 afs
->output
.nch
= *out_channels
;
303 afs
->output
.format
= *out_format
;
304 af_fix_parameters(&(afs
->output
));
307 memcpy(&afs
->cfg
,&af_cfg
,sizeof(af_cfg_t
));
309 mp_msg(MSGT_DECAUDIO
, MSGL_V
, MSGTR_BuildingAudioFilterChain
,
310 afs
->input
.rate
,afs
->input
.nch
,af_fmt2str_short(afs
->input
.format
),
311 afs
->output
.rate
,afs
->output
.nch
,af_fmt2str_short(afs
->output
.format
));
313 // let's autoprobe it!
314 if(0 != af_init(afs
)){
315 sh_audio
->afilter
=NULL
;
317 return 0; // failed :(
320 *out_samplerate
=afs
->output
.rate
;
321 *out_channels
=afs
->output
.nch
;
322 *out_format
=afs
->output
.format
;
324 if (out_maxsize
|| out_minsize
) {
325 // allocate the a_out_* buffers:
326 if(out_maxsize
<out_minsize
) out_maxsize
=out_minsize
;
327 if(out_maxsize
<8192) out_maxsize
=MAX_OUTBURST
; // not sure this is ok
329 sh_audio
->a_out_buffer_size
=out_maxsize
;
330 sh_audio
->a_out_buffer
=malloc(sh_audio
->a_out_buffer_size
);
331 memset(sh_audio
->a_out_buffer
,0,sh_audio
->a_out_buffer_size
);
332 sh_audio
->a_out_buffer_len
=0;
336 sh_audio
->afilter
=(void*)afs
;
340 int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
343 af_data_t afd
; // filter input
344 af_data_t
* pafd
; // filter output
345 ad_functions_t
* mpadec
= sh_audio
->ad_driver
;
347 if(!sh_audio
->inited
) return -1; // no codec
348 if(!sh_audio
->afilter
){
349 // no filter, just decode:
350 // FIXME: don't drop initial decoded data in a_buffer!
351 return mpadec
->decode_audio(sh_audio
,buf
,minlen
,maxlen
);
354 // declen=af_inputlen(sh_audio->afilter,minlen);
355 declen
=af_calc_insize_constrained(sh_audio
->afilter
,minlen
,maxlen
,
356 sh_audio
->a_buffer_size
-sh_audio
->audio_out_minsize
);
358 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"\ndecaudio: minlen=%d maxlen=%d declen=%d (max=%d)\n",
359 minlen
, maxlen
, declen
, sh_audio
->a_buffer_size
);
361 if(declen
<=0) return -1; // error!
363 // limit declen to buffer size: - DONE by af_calc_insize_constrained
364 // if(declen>sh_audio->a_buffer_size) declen=sh_audio->a_buffer_size;
367 while(declen
>sh_audio
->a_buffer_len
){
368 int len
=declen
-sh_audio
->a_buffer_len
;
369 int maxlen
=sh_audio
->a_buffer_size
-sh_audio
->a_buffer_len
;
371 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"decaudio: decoding %d bytes, max: %d (%d)\n",
372 len
, maxlen
, sh_audio
->audio_out_minsize
);
374 if(maxlen
<sh_audio
->audio_out_minsize
) break; // don't overflow buffer!
375 // not enough decoded data waiting, decode 'len' bytes more:
376 len
=mpadec
->decode_audio(sh_audio
,
377 sh_audio
->a_buffer
+sh_audio
->a_buffer_len
, len
, maxlen
);
378 if(len
<=0) break; // EOF?
379 sh_audio
->a_buffer_len
+=len
;
381 if(declen
>sh_audio
->a_buffer_len
)
382 declen
=sh_audio
->a_buffer_len
; // still no enough data (EOF) :(
384 // round to whole samples:
385 // declen/=sh_audio->samplesize*sh_audio->channels;
386 // declen*=sh_audio->samplesize*sh_audio->channels;
389 afd
.audio
=sh_audio
->a_buffer
;
391 afd
.rate
=sh_audio
->samplerate
;
392 afd
.nch
=sh_audio
->channels
;
393 afd
.format
=sh_audio
->sample_format
;
394 af_fix_parameters(&afd
);
396 // printf("\nAF: %d --> ",declen);
397 pafd
=af_play(sh_audio
->afilter
,&afd
);
398 // printf("%d \n",pafd->len);
400 if(!pafd
) return -1; // error
402 mp_msg(MSGT_DECAUDIO
,MSGL_DBG2
,"decaudio: declen=%d out=%d (max %d)\n",
403 declen
, pafd
->len
, maxlen
);
405 // copy filter==>out:
406 if(maxlen
< pafd
->len
) {
407 maxlen
-= maxlen
% (sh_audio
->channels
* sh_audio
->samplesize
);
408 mp_msg(MSGT_DECAUDIO
,MSGL_WARN
,"%i bytes of audio data lost due to buffer overflow, len = %i\n", pafd
->len
- maxlen
,pafd
->len
);
412 memmove(buf
, pafd
->audio
, maxlen
);
414 // remove processed data from decoder buffer:
415 sh_audio
->a_buffer_len
-=declen
;
416 if(sh_audio
->a_buffer_len
>0)
417 memmove(sh_audio
->a_buffer
, sh_audio
->a_buffer
+declen
, sh_audio
->a_buffer_len
);
422 void resync_audio_stream(sh_audio_t
*sh_audio
)
424 sh_audio
->a_in_buffer_len
=0; // clear audio input buffer
425 if(!sh_audio
->inited
) return;
426 sh_audio
->ad_driver
->control(sh_audio
,ADCTRL_RESYNC_STREAM
,NULL
);
429 void skip_audio_frame(sh_audio_t
*sh_audio
)
431 if(!sh_audio
->inited
) return;
432 if(sh_audio
->ad_driver
->control(sh_audio
,ADCTRL_SKIP_FRAME
,NULL
)==CONTROL_TRUE
) return;
433 // default skip code:
434 ds_fill_buffer(sh_audio
->ds
); // skip block
437 void adjust_volume(void)