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 "libavcodec audio decoders",
46 .print_name
= "libavcodec",
52 AVCodecContext
*avctx
;
57 int previous_data_left
; // input demuxer packet data
60 static int preinit(sh_audio_t
*sh
)
65 /* Prefer playing audio with the samplerate given in container data
66 * if available, but take number the number of channels and sample format
67 * from the codec, since if the codec isn't using the correct values for
68 * those everything breaks anyway.
70 static int setup_format(sh_audio_t
*sh_audio
,
71 const AVCodecContext
*lavc_context
)
73 int sample_format
= sh_audio
->sample_format
;
74 switch (lavc_context
->sample_fmt
) {
75 case AV_SAMPLE_FMT_U8
: sample_format
= AF_FORMAT_U8
; break;
76 case AV_SAMPLE_FMT_S16
: sample_format
= AF_FORMAT_S16_NE
; break;
77 case AV_SAMPLE_FMT_S32
: sample_format
= AF_FORMAT_S32_NE
; break;
78 case AV_SAMPLE_FMT_FLT
: sample_format
= AF_FORMAT_FLOAT_NE
; break;
80 mp_msg(MSGT_DECAUDIO
, MSGL_FATAL
, "Unsupported sample format\n");
81 sample_format
= AF_FORMAT_UNKNOWN
;
84 bool broken_srate
= false;
85 int samplerate
= lavc_context
->sample_rate
;
86 int container_samplerate
= sh_audio
->container_out_samplerate
;
87 if (!container_samplerate
&& sh_audio
->wf
)
88 container_samplerate
= sh_audio
->wf
->nSamplesPerSec
;
89 if (lavc_context
->codec_id
== CODEC_ID_AAC
90 && samplerate
== 2 * container_samplerate
)
92 else if (container_samplerate
)
93 samplerate
= container_samplerate
;
95 if (lavc_context
->channels
!= sh_audio
->channels
||
96 samplerate
!= sh_audio
->samplerate
||
97 sample_format
!= sh_audio
->sample_format
) {
98 sh_audio
->channels
= lavc_context
->channels
;
99 sh_audio
->samplerate
= samplerate
;
100 sh_audio
->sample_format
= sample_format
;
101 sh_audio
->samplesize
= af_fmt2bits(sh_audio
->sample_format
) / 8;
103 mp_msg(MSGT_DECAUDIO
, MSGL_WARN
,
104 "Ignoring broken container sample rate for AAC with SBR\n");
110 static int init(sh_audio_t
*sh_audio
)
112 struct MPOpts
*opts
= sh_audio
->opts
;
113 AVCodecContext
*lavc_context
;
116 if (sh_audio
->codec
->dll
) {
117 lavc_codec
= avcodec_find_decoder_by_name(sh_audio
->codec
->dll
);
119 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
,
120 "Cannot find codec '%s' in libavcodec...\n",
121 sh_audio
->codec
->dll
);
124 } else if (!sh_audio
->libav_codec_id
) {
125 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "No Libav codec ID known. "
126 "Generic lavc decoder is not applicable.\n");
129 lavc_codec
= avcodec_find_decoder(sh_audio
->libav_codec_id
);
131 mp_tmsg(MSGT_DECAUDIO
, MSGL_INFO
, "Libavcodec has no decoder "
137 sh_audio
->codecname
= lavc_codec
->long_name
;
138 if (!sh_audio
->codecname
)
139 sh_audio
->codecname
= lavc_codec
->name
;
141 struct priv
*ctx
= talloc_zero(NULL
, struct priv
);
142 sh_audio
->context
= ctx
;
143 lavc_context
= avcodec_alloc_context3(lavc_codec
);
144 ctx
->avctx
= lavc_context
;
145 ctx
->avframe
= avcodec_alloc_frame();
147 // Always try to set - option only exists for AC3 at the moment
148 av_opt_set_double(lavc_context
, "drc_scale", opts
->drc_level
,
149 AV_OPT_SEARCH_CHILDREN
);
150 lavc_context
->sample_rate
= sh_audio
->samplerate
;
151 lavc_context
->bit_rate
= sh_audio
->i_bps
* 8;
153 lavc_context
->channels
= sh_audio
->wf
->nChannels
;
154 lavc_context
->sample_rate
= sh_audio
->wf
->nSamplesPerSec
;
155 lavc_context
->bit_rate
= sh_audio
->wf
->nAvgBytesPerSec
* 8;
156 lavc_context
->block_align
= sh_audio
->wf
->nBlockAlign
;
157 lavc_context
->bits_per_coded_sample
= sh_audio
->wf
->wBitsPerSample
;
159 lavc_context
->request_channels
= opts
->audio_output_channels
;
160 lavc_context
->codec_tag
= sh_audio
->format
; //FOURCC
161 lavc_context
->codec_type
= AVMEDIA_TYPE_AUDIO
;
162 lavc_context
->codec_id
= lavc_codec
->id
; // not sure if required, imho not --A'rpi
164 /* alloc extra data */
165 if (sh_audio
->wf
&& sh_audio
->wf
->cbSize
> 0) {
166 lavc_context
->extradata
= av_mallocz(sh_audio
->wf
->cbSize
+ FF_INPUT_BUFFER_PADDING_SIZE
);
167 lavc_context
->extradata_size
= sh_audio
->wf
->cbSize
;
168 memcpy(lavc_context
->extradata
, sh_audio
->wf
+ 1,
169 lavc_context
->extradata_size
);
173 if (sh_audio
->codecdata_len
&& sh_audio
->codecdata
&&
174 !lavc_context
->extradata
) {
175 lavc_context
->extradata
= av_malloc(sh_audio
->codecdata_len
+
176 FF_INPUT_BUFFER_PADDING_SIZE
);
177 lavc_context
->extradata_size
= sh_audio
->codecdata_len
;
178 memcpy(lavc_context
->extradata
, (char *)sh_audio
->codecdata
,
179 lavc_context
->extradata_size
);
183 if (avcodec_open2(lavc_context
, lavc_codec
, NULL
) < 0) {
184 mp_tmsg(MSGT_DECAUDIO
, MSGL_ERR
, "Could not open codec.\n");
188 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "INFO: libavcodec \"%s\" init OK!\n",
191 if (sh_audio
->format
== 0x3343414D) {
193 sh_audio
->ds
->ss_div
= 2 * 3; // 1 samples/packet
194 sh_audio
->ds
->ss_mul
= 2 * sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
195 } else if (sh_audio
->format
== 0x3643414D) {
197 sh_audio
->ds
->ss_div
= 2 * 6; // 1 samples/packet
198 sh_audio
->ds
->ss_mul
= 2 * sh_audio
->wf
->nChannels
; // 1 byte*ch/packet
201 // Decode at least 1 byte: (to get header filled)
202 for (int tries
= 0;;) {
203 int x
= decode_audio(sh_audio
, sh_audio
->a_buffer
, 1,
204 sh_audio
->a_buffer_size
);
206 sh_audio
->a_buffer_len
= x
;
210 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
211 "ad_ffmpeg: initial decode failed\n");
217 sh_audio
->i_bps
= lavc_context
->bit_rate
/ 8;
218 if (sh_audio
->wf
&& sh_audio
->wf
->nAvgBytesPerSec
)
219 sh_audio
->i_bps
= sh_audio
->wf
->nAvgBytesPerSec
;
221 switch (lavc_context
->sample_fmt
) {
222 case AV_SAMPLE_FMT_U8
:
223 case AV_SAMPLE_FMT_S16
:
224 case AV_SAMPLE_FMT_S32
:
225 case AV_SAMPLE_FMT_FLT
:
234 static void uninit(sh_audio_t
*sh
)
236 sh
->codecname
= NULL
;
237 struct priv
*ctx
= sh
->context
;
240 AVCodecContext
*lavc_context
= ctx
->avctx
;
243 if (avcodec_close(lavc_context
) < 0)
244 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
245 av_freep(&lavc_context
->extradata
);
246 av_freep(&lavc_context
);
248 av_free(ctx
->avframe
);
253 static int control(sh_audio_t
*sh
, int cmd
, void *arg
, ...)
255 struct priv
*ctx
= sh
->context
;
257 case ADCTRL_RESYNC_STREAM
:
258 avcodec_flush_buffers(ctx
->avctx
);
259 ds_clear_parser(sh
->ds
);
260 ctx
->previous_data_left
= 0;
261 ctx
->output_left
= 0;
264 return CONTROL_UNKNOWN
;
267 static int decode_new_packet(struct sh_audio
*sh
)
269 struct priv
*priv
= sh
->context
;
270 AVCodecContext
*avctx
= priv
->avctx
;
271 double pts
= MP_NOPTS_VALUE
;
273 bool packet_already_used
= priv
->previous_data_left
;
274 struct demux_packet
*mpkt
= ds_get_packet2(sh
->ds
,
275 priv
->previous_data_left
);
276 unsigned char *start
;
278 assert(!priv
->previous_data_left
);
281 ds_parse(sh
->ds
, &start
, &insize
, pts
, 0);
283 return -1; // error or EOF
285 assert(mpkt
->len
>= priv
->previous_data_left
);
286 if (!priv
->previous_data_left
) {
287 priv
->previous_data_left
= mpkt
->len
;
290 insize
= priv
->previous_data_left
;
291 start
= mpkt
->buffer
+ mpkt
->len
- priv
->previous_data_left
;
292 int consumed
= ds_parse(sh
->ds
, &start
, &insize
, pts
, 0);
293 priv
->previous_data_left
-= consumed
;
294 priv
->previous_data_left
= FFMAX(priv
->previous_data_left
, 0);
298 av_init_packet(&pkt
);
301 if (mpkt
&& mpkt
->avpacket
) {
302 pkt
.side_data
= mpkt
->avpacket
->side_data
;
303 pkt
.side_data_elems
= mpkt
->avpacket
->side_data_elems
;
305 if (pts
!= MP_NOPTS_VALUE
&& !packet_already_used
) {
310 int ret
= avcodec_decode_audio4(avctx
, priv
->avframe
, &got_frame
, &pkt
);
311 // LATM may need many packets to find mux info
312 if (ret
== AVERROR(EAGAIN
))
315 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "lavc_audio: error\n");
318 // The "insize >= ret" test is sanity check against decoder overreads
319 if (!sh
->parser
&& insize
>= ret
)
320 priv
->previous_data_left
= insize
- ret
;
323 /* An error is reported later from output format checking, but make
324 * sure we don't crash by overreading first plane. */
325 if (av_sample_fmt_is_planar(avctx
->sample_fmt
) && avctx
->channels
> 1)
327 uint64_t unitsize
= (uint64_t)av_get_bytes_per_sample(avctx
->sample_fmt
) *
329 if (unitsize
> 100000)
331 priv
->unitsize
= unitsize
;
332 uint64_t output_left
= unitsize
* priv
->avframe
->nb_samples
;
333 if (output_left
> 500000000)
335 priv
->output_left
= output_left
;
336 priv
->output
= priv
->avframe
->data
[0];
337 mp_dbg(MSGT_DECAUDIO
, MSGL_DBG2
, "Decoded %d -> %d \n", insize
,
343 static int decode_audio(sh_audio_t
*sh_audio
, unsigned char *buf
, int minlen
,
346 struct priv
*priv
= sh_audio
->context
;
347 AVCodecContext
*avctx
= priv
->avctx
;
350 while (len
< minlen
) {
351 if (!priv
->output_left
) {
352 if (decode_new_packet(sh_audio
) < 0)
356 if (setup_format(sh_audio
, avctx
))
358 int size
= (minlen
- len
+ priv
->unitsize
- 1);
359 size
-= size
% priv
->unitsize
;
360 size
= FFMIN(size
, priv
->output_left
);
363 memcpy(buf
, priv
->output
, size
);
364 priv
->output
+= size
;
365 priv
->output_left
-= size
;
366 if (avctx
->channels
>= 5) {
367 int samplesize
= av_get_bytes_per_sample(avctx
->sample_fmt
);
368 reorder_channel_nch(buf
, AF_CHANNEL_LAYOUT_LAVC_DEFAULT
,
369 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
371 size
/ samplesize
, samplesize
);
379 sh_audio
->pts_bytes
+= size
;