ao_pulse: work around PulseAudio timing bugs
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
bloba20689eab839294a9156945ac990c840d9b599f7
1 /*
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <assert.h>
25 #include <libavcodec/avcodec.h>
26 #include <libavutil/opt.h>
28 #include "talloc.h"
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "options.h"
34 #include "ad_internal.h"
35 #include "libaf/reorder_ch.h"
37 #include "mpbswap.h"
39 static const ad_info_t info =
41 "libavcodec audio decoders",
42 "ffmpeg",
43 "",
44 "",
45 "",
46 .print_name = "libavcodec",
49 LIBAD_EXTERN(ffmpeg)
51 struct priv {
52 AVCodecContext *avctx;
53 AVFrame *avframe;
54 char *output;
55 int output_left;
56 int unitsize;
57 int previous_data_left; // input demuxer packet data
60 static int preinit(sh_audio_t *sh)
62 return 1;
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;
79 default:
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)
91 broken_srate = true;
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;
102 if (broken_srate)
103 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
104 "Ignoring broken container sample rate for AAC with SBR\n");
105 return 1;
107 return 0;
110 static int init(sh_audio_t *sh_audio)
112 struct MPOpts *opts = sh_audio->opts;
113 AVCodecContext *lavc_context;
114 AVCodec *lavc_codec;
116 if (sh_audio->codec->dll) {
117 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
118 if (!lavc_codec) {
119 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
120 "Cannot find codec '%s' in libavcodec...\n",
121 sh_audio->codec->dll);
122 return 0;
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");
127 return 0;
128 } else {
129 lavc_codec = avcodec_find_decoder(sh_audio->libav_codec_id);
130 if (!lavc_codec) {
131 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Libavcodec has no decoder "
132 "for this codec\n");
133 return 0;
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;
152 if (sh_audio->wf) {
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);
172 // for QDM2
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);
182 /* open it */
183 if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
184 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n");
185 uninit(sh_audio);
186 return 0;
188 mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n",
189 lavc_codec->name);
191 if (sh_audio->format == 0x3343414D) {
192 // MACE 3:1
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) {
196 // MACE 6:1
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);
205 if (x > 0) {
206 sh_audio->a_buffer_len = x;
207 break;
209 if (++tries >= 5) {
210 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
211 "ad_ffmpeg: initial decode failed\n");
212 uninit(sh_audio);
213 return 0;
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:
226 break;
227 default:
228 uninit(sh_audio);
229 return 0;
231 return 1;
234 static void uninit(sh_audio_t *sh)
236 sh->codecname = NULL;
237 struct priv *ctx = sh->context;
238 if (!ctx)
239 return;
240 AVCodecContext *lavc_context = ctx->avctx;
242 if (lavc_context) {
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);
249 talloc_free(ctx);
250 sh->context = NULL;
253 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
255 struct priv *ctx = sh->context;
256 switch (cmd) {
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;
262 return CONTROL_TRUE;
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;
272 int insize;
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;
277 if (!mpkt) {
278 assert(!priv->previous_data_left);
279 start = NULL;
280 insize = 0;
281 ds_parse(sh->ds, &start, &insize, pts, 0);
282 if (insize <= 0)
283 return -1; // error or EOF
284 } else {
285 assert(mpkt->len >= priv->previous_data_left);
286 if (!priv->previous_data_left) {
287 priv->previous_data_left = mpkt->len;
288 pts = mpkt->pts;
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;
296 AVPacket pkt;
297 av_init_packet(&pkt);
298 pkt.data = start;
299 pkt.size = insize;
300 if (mpkt && mpkt->avpacket) {
301 pkt.side_data = mpkt->avpacket->side_data;
302 pkt.side_data_elems = mpkt->avpacket->side_data_elems;
304 if (pts != MP_NOPTS_VALUE && !packet_already_used) {
305 sh->pts = pts;
306 sh->pts_bytes = 0;
308 int got_frame = 0;
309 int ret = avcodec_decode_audio4(avctx, priv->avframe, &got_frame, &pkt);
310 // LATM may need many packets to find mux info
311 if (ret == AVERROR(EAGAIN))
312 return 0;
313 if (ret < 0) {
314 mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n");
315 return -1;
317 if (!sh->parser)
318 priv->previous_data_left += insize - ret;
319 if (!got_frame)
320 return 0;
321 /* An error is reported later from output format checking, but make
322 * sure we don't crash by overreading first plane. */
323 if (av_sample_fmt_is_planar(avctx->sample_fmt) && avctx->channels > 1)
324 return 0;
325 uint64_t unitsize = (uint64_t)av_get_bytes_per_sample(avctx->sample_fmt) *
326 avctx->channels;
327 if (unitsize > 100000)
328 abort();
329 priv->unitsize = unitsize;
330 uint64_t output_left = unitsize * priv->avframe->nb_samples;
331 if (output_left > 500000000)
332 abort();
333 priv->output_left = output_left;
334 priv->output = priv->avframe->data[0];
335 mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", insize,
336 priv->output_left);
337 return 0;
341 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
342 int maxlen)
344 struct priv *priv = sh_audio->context;
345 AVCodecContext *avctx = priv->avctx;
347 int len = -1;
348 while (len < minlen) {
349 if (!priv->output_left) {
350 if (decode_new_packet(sh_audio) < 0)
351 break;
352 continue;
354 if (setup_format(sh_audio, avctx))
355 return len;
356 int size = (minlen - len + priv->unitsize - 1);
357 size -= size % priv->unitsize;
358 size = FFMIN(size, priv->output_left);
359 if (size > maxlen)
360 abort();
361 memcpy(buf, priv->output, size);
362 priv->output += size;
363 priv->output_left -= size;
364 if (avctx->channels >= 5) {
365 int samplesize = av_get_bytes_per_sample(avctx->sample_fmt);
366 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
367 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
368 avctx->channels,
369 size / samplesize, samplesize);
371 if (len < 0)
372 len = size;
373 else
374 len += size;
375 buf += size;
376 maxlen -= size;
377 sh_audio->pts_bytes += size;
379 return len;