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.
25 #include <libavcodec/avcodec.h>
26 #include <libavutil/opt.h>
34 #include "ad_internal.h"
35 #include "libaf/reorder_ch.h"
39 static const ad_info_t info
=
41 "FFmpeg/libavcodec audio decoders",
51 AVCodecContext
*avctx
;
52 int previous_data_left
;
55 static int preinit(sh_audio_t
*sh
)
57 sh
->audio_out_minsize
= AVCODEC_MAX_AUDIO_FRAME_SIZE
;
61 /* Prefer playing audio with the samplerate given in container data
62 * if available, but take number the number of channels and sample format
63 * from the codec, since if the codec isn't using the correct values for
64 * those everything breaks anyway.
66 static int setup_format(sh_audio_t
*sh_audio
,
67 const AVCodecContext
*lavc_context
)
69 int sample_format
= sh_audio
->sample_format
;
70 switch (lavc_context
->sample_fmt
) {
71 case AV_SAMPLE_FMT_U8
: sample_format
= AF_FORMAT_U8
; break;
72 case AV_SAMPLE_FMT_S16
: sample_format
= AF_FORMAT_S16_NE
; break;
73 case AV_SAMPLE_FMT_S32
: sample_format
= AF_FORMAT_S32_NE
; break;
74 case AV_SAMPLE_FMT_FLT
: sample_format
= AF_FORMAT_FLOAT_NE
; break;
76 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Unsupported sample format\n");
79 bool broken_srate
= false;
80 int samplerate
= lavc_context
->sample_rate
;
81 int container_samplerate
= sh_audio
->container_out_samplerate
;
82 if (!container_samplerate
&& sh_audio
->wf
)
83 container_samplerate
= sh_audio
->wf
->nSamplesPerSec
;
84 if (lavc_context
->codec_id
== CODEC_ID_AAC
85 && samplerate
== 2 * container_samplerate
)
87 else if (container_samplerate
)
88 samplerate
= container_samplerate
;
90 if (lavc_context
->channels
!= sh_audio
->channels
||
91 samplerate
!= sh_audio
->samplerate
||
92 sample_format
!= sh_audio
->sample_format
) {
93 sh_audio
->channels
= lavc_context
->channels
;
94 sh_audio
->samplerate
= samplerate
;
95 sh_audio
->sample_format
= sample_format
;
96 sh_audio
->samplesize
= af_fmt2bits(sh_audio
->sample_format
) / 8;
98 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
,
99 "Ignoring broken container sample rate for AAC with SBR\n");
105 static int init(sh_audio_t
*sh_audio
)
107 struct MPOpts
*opts
= sh_audio
->opts
;
108 AVCodecContext
*lavc_context
;
111 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "FFmpeg's libavcodec audio codec\n");
113 lavc_codec
= avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
115 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
,
116 "Cannot find codec '%s' in libavcodec...\n",
117 sh_audio
->codec
->dll
);
121 struct priv
*ctx
= talloc_zero(NULL
, struct priv
);
122 sh_audio
->context
= ctx
;
123 lavc_context
= avcodec_alloc_context3(lavc_codec
);
124 ctx
->avctx
= lavc_context
;
126 // Always try to set - option only exists for AC3 at the moment
127 av_opt_set_double(lavc_context
, "drc_scale", opts
->drc_level
,
128 AV_OPT_SEARCH_CHILDREN
);
129 lavc_context
->sample_rate
= sh_audio
->samplerate
;
130 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
132 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
133 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
134 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
135 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
136 lavc_context
->bits_per_coded_sample
= sh_audio
->wf
->wBitsPerSample
;
138 lavc_context
->request_channels
= opts
->audio_output_channels
;
139 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
140 lavc_context
->codec_type
= AVMEDIA_TYPE_AUDIO
;
141 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
143 /* alloc extra data */
144 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
145 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
146 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
147 memcpy(lavc_context
->extradata
, sh_audio
->wf
+ 1,
148 lavc_context
->extradata_size
);
152 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&&
153 !lavc_context
->extradata
) {
154 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
+
155 FF_INPUT_BUFFER_PADDING_SIZE
);
156 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
157 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
158 lavc_context
->extradata_size
);
162 if (avcodec_open2(lavc_context
, lavc_codec
, NULL
) < 0) {
163 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "Could not open codec.\n");
167 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "INFO: libavcodec \"%s\" init OK!\n",
170 if (sh_audio
->format
== 0x3343414D) {
172 sh_audio
->ds
->ss_div
= 2 * 3; // 1 samples/packet
173 sh_audio
->ds
->ss_mul
= 2 * sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
174 } else if (sh_audio
->format
== 0x3643414D) {
176 sh_audio
->ds
->ss_div
= 2 * 6; // 1 samples/packet
177 sh_audio
->ds
->ss_mul
= 2 * sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
180 // Decode at least 1 byte: (to get header filled)
181 for (int tries
= 0;;) {
182 int x
= decode_audio(sh_audio
, sh_audio
->a_buffer
, 1,
183 sh_audio
->a_buffer_size
);
185 sh_audio
->a_buffer_len
= x
;
189 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
190 "ad_ffmpeg: initial decode failed\n");
196 sh_audio
->i_bps
= lavc_context
->bit_rate
/ 8;
197 if (sh_audio
->wf
&& sh_audio
->wf
->nAvgBytesPerSec
)
198 sh_audio
->i_bps
= sh_audio
->wf
->nAvgBytesPerSec
;
200 switch (lavc_context
->sample_fmt
) {
201 case AV_SAMPLE_FMT_U8
:
202 case AV_SAMPLE_FMT_S16
:
203 case AV_SAMPLE_FMT_S32
:
204 case AV_SAMPLE_FMT_FLT
:
213 static void uninit(sh_audio_t
*sh
)
215 struct priv
*ctx
= sh
->context
;
218 AVCodecContext
*lavc_context
= ctx
->avctx
;
221 if (avcodec_close(lavc_context
) < 0)
222 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
223 av_freep(&lavc_context
->extradata
);
224 av_freep(&lavc_context
);
230 static int control(sh_audio_t
*sh
, int cmd
, void *arg
, ...)
232 struct priv
*ctx
= sh
->context
;
234 case ADCTRL_RESYNC_STREAM
:
235 avcodec_flush_buffers(ctx
->avctx
);
236 ds_clear_parser(sh
->ds
);
237 ctx
->previous_data_left
= 0;
240 return CONTROL_UNKNOWN
;
243 static int decode_audio(sh_audio_t
*sh_audio
, unsigned char *buf
, int minlen
,
246 struct priv
*ctx
= sh_audio
->context
;
247 AVCodecContext
*avctx
= ctx
->avctx
;
249 unsigned char *start
= NULL
;
251 while (len
< minlen
) {
254 double pts
= MP_NOPTS_VALUE
;
256 bool packet_already_used
= ctx
->previous_data_left
;
257 struct demux_packet
*mpkt
= ds_get_packet2(sh_audio
->ds
,
258 ctx
->previous_data_left
);
260 assert(!ctx
->previous_data_left
);
263 ds_parse(sh_audio
->ds
, &start
, &x
, pts
, 0);
267 assert(mpkt
->len
>= ctx
->previous_data_left
);
268 if (!ctx
->previous_data_left
) {
269 ctx
->previous_data_left
= mpkt
->len
;
272 x
= ctx
->previous_data_left
;
273 start
= mpkt
->buffer
+ mpkt
->len
- ctx
->previous_data_left
;
274 int consumed
= ds_parse(sh_audio
->ds
, &start
, &x
, pts
, 0);
275 ctx
->previous_data_left
-= consumed
;
277 av_init_packet(&pkt
);
280 if (mpkt
&& mpkt
->avpacket
) {
281 pkt
.side_data
= mpkt
->avpacket
->side_data
;
282 pkt
.side_data_elems
= mpkt
->avpacket
->side_data_elems
;
284 if (pts
!= MP_NOPTS_VALUE
&& !packet_already_used
) {
286 sh_audio
->pts_bytes
= 0;
288 y
= avcodec_decode_audio3(avctx
, (int16_t *)buf
, &len2
, &pkt
);
289 // LATM may need many packets to find mux info
290 if (y
== AVERROR(EAGAIN
))
293 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "lavc_audio: error\n");
296 if (!sh_audio
->parser
)
297 ctx
->previous_data_left
+= x
- y
;
299 if (avctx
->channels
>= 5) {
300 int samplesize
= av_get_bytes_per_sample(avctx
->sample_fmt
);
301 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
302 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
304 len2
/ samplesize
, samplesize
);
312 sh_audio
->pts_bytes
+= len2
;
314 mp_dbg(MSGT_DECAUDIO
, MSGL_DBG2
, "Decoded %d -> %d \n", y
, len2
);
316 if (setup_format(sh_audio
, avctx
))