mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / ad_ffmpeg.c
blob19c2f871df138a0ff374794992ee127edbb8fe8c
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/audioconvert.h>
27 #include <libavutil/opt.h>
28 #include <libavutil/samplefmt.h>
30 #include "talloc.h"
32 #include "config.h"
33 #include "mp_msg.h"
34 #include "options.h"
36 #include "ad_internal.h"
37 #include "libaf/reorder_ch.h"
39 #include "mpbswap.h"
41 #ifdef CONFIG_LIBAVRESAMPLE
42 #include <libavresample/avresample.h>
43 #endif
45 static const ad_info_t info =
47 "libavcodec audio decoders",
48 "ffmpeg",
49 "",
50 "",
51 "",
52 .print_name = "libavcodec",
55 LIBAD_EXTERN(ffmpeg)
57 struct priv {
58 AVCodecContext *avctx;
59 AVFrame *avframe;
60 char *output;
61 int output_left;
62 int unitsize;
63 int previous_data_left; // input demuxer packet data
65 #ifdef CONFIG_LIBAVRESAMPLE
66 AVAudioResampleContext *avr;
67 enum AVSampleFormat resample_fmt;
68 enum AVSampleFormat out_fmt;
69 int resample_channels;
70 uint8_t *resample_buf;
71 uint64_t resample_buf_size;
72 #endif
75 static int preinit(sh_audio_t *sh)
77 return 1;
80 static const int sample_fmt_map[][2] = {
81 { AV_SAMPLE_FMT_U8, AF_FORMAT_U8 },
82 { AV_SAMPLE_FMT_S16, AF_FORMAT_S16_NE },
83 { AV_SAMPLE_FMT_S32, AF_FORMAT_S32_NE },
84 { AV_SAMPLE_FMT_FLT, AF_FORMAT_FLOAT_NE },
87 static int sample_fmt_lavc2native(enum AVSampleFormat sample_fmt)
89 for (int i = 0; i < FF_ARRAY_ELEMS(sample_fmt_map); i++)
90 if (sample_fmt_map[i][0] == sample_fmt)
91 return sample_fmt_map[i][1];
92 return AF_FORMAT_UNKNOWN;
95 /* Prefer playing audio with the samplerate given in container data
96 * if available, but take number the number of channels and sample format
97 * from the codec, since if the codec isn't using the correct values for
98 * those everything breaks anyway.
100 static int setup_format(sh_audio_t *sh_audio)
102 struct priv *priv = sh_audio->context;
103 AVCodecContext *codec = priv->avctx;
105 int sample_format = sample_fmt_lavc2native(codec->sample_fmt);
106 if (sample_format == AF_FORMAT_UNKNOWN) {
107 #ifndef CONFIG_LIBAVRESAMPLE
108 if (av_sample_fmt_is_planar(codec->sample_fmt))
109 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
110 "The player has been compiled without libavresample "
111 "support,\nwhich is needed with this libavcodec decoder "
112 "version.\nCompile with libavresample enabled to make "
113 "audio decoding work!\n");
114 else
115 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Unsupported sample format\n");
116 goto error;
117 #else
118 if (priv->avr && (priv->resample_fmt != codec->sample_fmt ||
119 priv->resample_channels != codec->channels))
120 avresample_free(&priv->avr);
122 if (!priv->avr) {
123 int ret;
124 uint8_t error[128];
125 enum AVSampleFormat out_fmt =
126 av_get_packed_sample_fmt(codec->sample_fmt);
127 uint64_t ch_layout = codec->channel_layout;
129 mp_msg(MSGT_DECAUDIO, MSGL_V,
130 "(Re)initializing libavresample format conversion...\n");
132 if (!ch_layout)
133 ch_layout = av_get_default_channel_layout(codec->channels);
135 /* if lavc format is planar, try just getting packed equivalent */
136 sample_format = sample_fmt_lavc2native(out_fmt);
137 if (sample_format == AF_FORMAT_UNKNOWN) {
138 /* fallback to s16 */
139 out_fmt = AV_SAMPLE_FMT_S16;
140 sample_format = AF_FORMAT_S16_NE;
143 priv->avr = avresample_alloc_context();
144 if (!priv->avr) {
145 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Out of memory.\n");
146 abort();
148 av_opt_set_int(priv->avr, "in_channel_layout", ch_layout, 0);
149 av_opt_set_int(priv->avr, "out_channel_layout", ch_layout, 0);
150 av_opt_set_int(priv->avr, "in_sample_rate", codec->sample_rate, 0);
151 av_opt_set_int(priv->avr, "out_sample_rate", codec->sample_rate, 0);
152 av_opt_set_int(priv->avr, "in_sample_fmt", codec->sample_fmt, 0);
153 av_opt_set_int(priv->avr, "out_sample_fmt", out_fmt, 0);
155 if ((ret = avresample_open(priv->avr)) < 0) {
156 av_strerror(ret, error, sizeof(error));
157 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
158 "Error opening libavresample: %s.\n", error);
159 goto error;
161 priv->resample_fmt = codec->sample_fmt;
162 priv->resample_channels = codec->channels;
163 priv->out_fmt = out_fmt;
164 priv->unitsize = av_get_bytes_per_sample(out_fmt) *
165 codec->channels;
166 } else
167 sample_format = sh_audio->sample_format;
168 } else if (priv->avr) {
169 avresample_free(&priv->avr);
170 #endif
173 bool broken_srate = false;
174 int samplerate = codec->sample_rate;
175 int container_samplerate = sh_audio->container_out_samplerate;
176 if (!container_samplerate && sh_audio->wf)
177 container_samplerate = sh_audio->wf->nSamplesPerSec;
178 if (codec->codec_id == CODEC_ID_AAC
179 && samplerate == 2 * container_samplerate)
180 broken_srate = true;
181 else if (container_samplerate)
182 samplerate = container_samplerate;
184 if (codec->channels != sh_audio->channels ||
185 samplerate != sh_audio->samplerate ||
186 sample_format != sh_audio->sample_format) {
187 sh_audio->channels = codec->channels;
188 sh_audio->samplerate = samplerate;
189 sh_audio->sample_format = sample_format;
190 sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8;
191 if (broken_srate)
192 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
193 "Ignoring broken container sample rate for AAC with SBR\n");
194 return 1;
196 return 0;
197 error:
198 #ifdef CONFIG_LIBAVRESAMPLE
199 avresample_free(&priv->avr);
200 #endif
201 return -1;
204 static int init(sh_audio_t *sh_audio)
206 struct MPOpts *opts = sh_audio->opts;
207 AVCodecContext *lavc_context;
208 AVCodec *lavc_codec;
210 if (sh_audio->codec->dll) {
211 lavc_codec = avcodec_find_decoder_by_name(sh_audio->codec->dll);
212 if (!lavc_codec) {
213 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR,
214 "Cannot find codec '%s' in libavcodec...\n",
215 sh_audio->codec->dll);
216 return 0;
218 } else if (!sh_audio->libav_codec_id) {
219 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "No Libav codec ID known. "
220 "Generic lavc decoder is not applicable.\n");
221 return 0;
222 } else {
223 lavc_codec = avcodec_find_decoder(sh_audio->libav_codec_id);
224 if (!lavc_codec) {
225 mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Libavcodec has no decoder "
226 "for this codec\n");
227 return 0;
231 sh_audio->codecname = lavc_codec->long_name;
232 if (!sh_audio->codecname)
233 sh_audio->codecname = lavc_codec->name;
235 struct priv *ctx = talloc_zero(NULL, struct priv);
236 sh_audio->context = ctx;
237 lavc_context = avcodec_alloc_context3(lavc_codec);
238 ctx->avctx = lavc_context;
239 ctx->avframe = avcodec_alloc_frame();
241 // Always try to set - option only exists for AC3 at the moment
242 av_opt_set_double(lavc_context, "drc_scale", opts->drc_level,
243 AV_OPT_SEARCH_CHILDREN);
244 lavc_context->sample_rate = sh_audio->samplerate;
245 lavc_context->bit_rate = sh_audio->i_bps * 8;
246 if (sh_audio->wf) {
247 lavc_context->channels = sh_audio->wf->nChannels;
248 lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec;
249 lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8;
250 lavc_context->block_align = sh_audio->wf->nBlockAlign;
251 lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample;
253 lavc_context->request_channels = opts->audio_output_channels;
254 lavc_context->codec_tag = sh_audio->format; //FOURCC
255 lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
256 lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi
258 /* alloc extra data */
259 if (sh_audio->wf && sh_audio->wf->cbSize > 0) {
260 lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE);
261 lavc_context->extradata_size = sh_audio->wf->cbSize;
262 memcpy(lavc_context->extradata, sh_audio->wf + 1,
263 lavc_context->extradata_size);
266 // for QDM2
267 if (sh_audio->codecdata_len && sh_audio->codecdata &&
268 !lavc_context->extradata) {
269 lavc_context->extradata = av_malloc(sh_audio->codecdata_len +
270 FF_INPUT_BUFFER_PADDING_SIZE);
271 lavc_context->extradata_size = sh_audio->codecdata_len;
272 memcpy(lavc_context->extradata, (char *)sh_audio->codecdata,
273 lavc_context->extradata_size);
276 /* open it */
277 if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
278 mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n");
279 uninit(sh_audio);
280 return 0;
282 mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n",
283 lavc_codec->name);
285 if (sh_audio->format == 0x3343414D) {
286 // MACE 3:1
287 sh_audio->ds->ss_div = 2 * 3; // 1 samples/packet
288 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
289 } else if (sh_audio->format == 0x3643414D) {
290 // MACE 6:1
291 sh_audio->ds->ss_div = 2 * 6; // 1 samples/packet
292 sh_audio->ds->ss_mul = 2 * sh_audio->wf->nChannels; // 1 byte*ch/packet
295 // Decode at least 1 byte: (to get header filled)
296 for (int tries = 0;;) {
297 int x = decode_audio(sh_audio, sh_audio->a_buffer, 1,
298 sh_audio->a_buffer_size);
299 if (x > 0) {
300 sh_audio->a_buffer_len = x;
301 break;
303 if (++tries >= 5) {
304 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
305 "ad_ffmpeg: initial decode failed\n");
306 uninit(sh_audio);
307 return 0;
311 sh_audio->i_bps = lavc_context->bit_rate / 8;
312 if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec)
313 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
315 return 1;
318 static void uninit(sh_audio_t *sh)
320 sh->codecname = NULL;
321 struct priv *ctx = sh->context;
322 if (!ctx)
323 return;
324 AVCodecContext *lavc_context = ctx->avctx;
326 if (lavc_context) {
327 if (avcodec_close(lavc_context) < 0)
328 mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n");
329 av_freep(&lavc_context->extradata);
330 av_freep(&lavc_context);
332 #ifdef CONFIG_LIBAVRESAMPLE
333 avresample_free(&ctx->avr);
334 #endif
335 #if LIBAVCODEC_VERSION_INT >= (54 << 16 | 28 << 8)
336 avcodec_free_frame(&ctx->avframe);
337 #else
338 av_free(ctx->avframe);
339 #endif
340 talloc_free(ctx);
341 sh->context = NULL;
344 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
346 struct priv *ctx = sh->context;
347 switch (cmd) {
348 case ADCTRL_RESYNC_STREAM:
349 avcodec_flush_buffers(ctx->avctx);
350 ds_clear_parser(sh->ds);
351 ctx->previous_data_left = 0;
352 ctx->output_left = 0;
353 return CONTROL_TRUE;
355 return CONTROL_UNKNOWN;
358 static int decode_new_packet(struct sh_audio *sh)
360 struct priv *priv = sh->context;
361 AVCodecContext *avctx = priv->avctx;
362 double pts = MP_NOPTS_VALUE;
363 int insize;
364 bool packet_already_used = priv->previous_data_left;
365 struct demux_packet *mpkt = ds_get_packet2(sh->ds,
366 priv->previous_data_left);
367 unsigned char *start;
368 if (!mpkt) {
369 assert(!priv->previous_data_left);
370 start = NULL;
371 insize = 0;
372 ds_parse(sh->ds, &start, &insize, pts, 0);
373 if (insize <= 0)
374 return -1; // error or EOF
375 } else {
376 assert(mpkt->len >= priv->previous_data_left);
377 if (!priv->previous_data_left) {
378 priv->previous_data_left = mpkt->len;
379 pts = mpkt->pts;
381 insize = priv->previous_data_left;
382 start = mpkt->buffer + mpkt->len - priv->previous_data_left;
383 int consumed = ds_parse(sh->ds, &start, &insize, pts, 0);
384 priv->previous_data_left -= consumed;
385 priv->previous_data_left = FFMAX(priv->previous_data_left, 0);
388 AVPacket pkt;
389 av_init_packet(&pkt);
390 pkt.data = start;
391 pkt.size = insize;
392 if (mpkt && mpkt->avpacket) {
393 pkt.side_data = mpkt->avpacket->side_data;
394 pkt.side_data_elems = mpkt->avpacket->side_data_elems;
396 if (pts != MP_NOPTS_VALUE && !packet_already_used) {
397 sh->pts = pts;
398 sh->pts_bytes = 0;
400 int got_frame = 0;
401 int ret = avcodec_decode_audio4(avctx, priv->avframe, &got_frame, &pkt);
402 // LATM may need many packets to find mux info
403 if (ret == AVERROR(EAGAIN))
404 return 0;
405 if (ret < 0) {
406 mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n");
407 return -1;
409 // The "insize >= ret" test is sanity check against decoder overreads
410 if (!sh->parser && insize >= ret)
411 priv->previous_data_left = insize - ret;
412 if (!got_frame)
413 return 0;
415 int format_result = setup_format(sh);
416 if (format_result < 0)
417 return format_result;
419 #ifdef CONFIG_LIBAVRESAMPLE
420 if (priv->avr) {
421 int ret;
422 uint64_t needed_size = av_samples_get_buffer_size(
423 NULL, priv->resample_channels, priv->avframe->nb_samples,
424 priv->resample_fmt, 0);
425 if (needed_size > priv->resample_buf_size) {
426 priv->resample_buf = talloc_realloc(priv, priv->resample_buf,
427 uint8_t, needed_size);
428 priv->resample_buf_size = needed_size;
431 ret = avresample_convert(priv->avr, &priv->resample_buf,
432 priv->resample_buf_size, priv->avframe->nb_samples,
433 priv->avframe->extended_data, priv->avframe->linesize[0],
434 priv->avframe->nb_samples);
435 if (ret < 0) {
436 uint8_t error[128];
437 av_strerror(ret, error, sizeof(error));
438 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
439 "Error during sample format conversion: %s.\n", error);
440 return -1;
443 assert(ret == priv->avframe->nb_samples);
445 priv->output = priv->resample_buf;
446 priv->output_left = priv->unitsize * ret;
447 } else
448 #endif
450 uint64_t unitsize = av_get_bytes_per_sample(avctx->sample_fmt) *
451 (uint64_t)avctx->channels;
452 if (unitsize > 100000)
453 abort();
454 priv->unitsize = unitsize;
455 uint64_t output_left = unitsize * priv->avframe->nb_samples;
456 if (output_left > 500000000)
457 abort();
458 priv->output_left = output_left;
459 priv->output = priv->avframe->data[0];
462 mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", insize,
463 priv->output_left);
464 return format_result;
468 static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen,
469 int maxlen)
471 struct priv *priv = sh_audio->context;
472 AVCodecContext *avctx = priv->avctx;
474 int len = -1;
475 while (len < minlen) {
476 if (!priv->output_left) {
477 if (decode_new_packet(sh_audio) != 0)
478 break;
479 continue;
481 int size = (minlen - len + priv->unitsize - 1);
482 size -= size % priv->unitsize;
483 size = FFMIN(size, priv->output_left);
484 if (size > maxlen)
485 abort();
486 memcpy(buf, priv->output, size);
487 priv->output += size;
488 priv->output_left -= size;
489 if (avctx->channels >= 5) {
490 int samplesize = av_get_bytes_per_sample(avctx->sample_fmt);
491 reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT,
492 AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
493 avctx->channels,
494 size / samplesize, samplesize);
496 if (len < 0)
497 len = size;
498 else
499 len += size;
500 buf += size;
501 maxlen -= size;
502 sh_audio->pts_bytes += size;
504 return len;