FfmpegDecoderPlugin.cxx 24.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20 21 22
/* necessary because libavutil/common.h uses UINT64_C */
#define __STDC_CONSTANT_MACROS

23
#include "lib/ffmpeg/Time.hxx"
24
#include "config.h"
25
#include "FfmpegDecoderPlugin.hxx"
26
#include "lib/ffmpeg/Domain.hxx"
27
#include "lib/ffmpeg/Error.hxx"
28
#include "lib/ffmpeg/LogError.hxx"
29
#include "lib/ffmpeg/Init.hxx"
30
#include "lib/ffmpeg/Buffer.hxx"
31
#include "../DecoderAPI.hxx"
32
#include "FfmpegMetaData.hxx"
33
#include "FfmpegIo.hxx"
34
#include "pcm/Interleave.hxx"
35 36
#include "tag/Builder.hxx"
#include "tag/Handler.hxx"
37 38
#include "tag/ReplayGain.hxx"
#include "tag/MixRamp.hxx"
Max Kellermann's avatar
Max Kellermann committed
39
#include "input/InputStream.hxx"
40
#include "CheckAudioFormat.hxx"
41
#include "util/ScopeExit.hxx"
42
#include "util/ConstBuffer.hxx"
43
#include "LogV.hxx"
44 45

extern "C" {
46 47 48
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
49
#include <libavutil/avutil.h>
50 51 52 53

#if LIBAVUTIL_VERSION_MAJOR >= 53
#include <libavutil/frame.h>
#endif
54
}
55

56 57 58
#include <assert.h>
#include <string.h>

59 60 61 62 63
/**
 * Muxer options to be passed to avformat_open_input().
 */
static AVDictionary *avformat_options = nullptr;

64 65
static AVFormatContext *
FfmpegOpenInput(AVIOContext *pb,
66
		const char *filename,
67
		AVInputFormat *fmt)
68 69
{
	AVFormatContext *context = avformat_alloc_context();
70 71
	if (context == nullptr)
		throw std::runtime_error("avformat_alloc_context() failed");
72 73

	context->pb = pb;
74

75 76 77 78 79
	AVDictionary *options = nullptr;
	AtScopeExit(&options) { av_dict_free(&options); };
	av_dict_copy(&options, avformat_options, 0);

	int err = avformat_open_input(&context, filename, fmt, &options);
Max Kellermann's avatar
Max Kellermann committed
80
	if (err < 0)
81
		throw MakeFfmpegError(err, "avformat_open_input() failed");
82

83
	return context;
84 85
}

86
static bool
87
ffmpeg_init(const ConfigBlock &block)
88
{
89
	FfmpegInit();
90 91 92 93 94 95 96 97 98 99 100 101

	static constexpr const char *option_names[] = {
		"probesize",
		"analyzeduration",
	};

	for (const char *name : option_names) {
		const char *value = block.GetBlockValue(name);
		if (value != nullptr)
			av_dict_set(&avformat_options, name, value, 0);
	}

102
	return true;
103 104
}

105 106 107 108 109 110
static void
ffmpeg_finish()
{
	av_dict_free(&avformat_options);
}

111
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
112 113 114

gcc_pure
static const AVCodecParameters &
115
GetCodecParameters(const AVStream &stream) noexcept
116 117 118 119 120 121
{
	return *stream.codecpar;
}

gcc_pure
static AVSampleFormat
122
GetSampleFormat(const AVCodecParameters &codec_params) noexcept
123 124 125 126 127 128
{
	return AVSampleFormat(codec_params.format);
}

#else

129 130
gcc_pure
static const AVCodecContext &
131
GetCodecParameters(const AVStream &stream) noexcept
132 133 134 135 136 137
{
	return *stream.codec;
}

gcc_pure
static AVSampleFormat
138
GetSampleFormat(const AVCodecContext &codec_context) noexcept
139 140 141 142
{
	return codec_context.sample_fmt;
}

143 144
#endif

145 146
gcc_pure
static bool
147
IsAudio(const AVStream &stream) noexcept
148
{
149
	return GetCodecParameters(stream).codec_type == AVMEDIA_TYPE_AUDIO;
150 151
}

152
gcc_pure
153
static int
154
ffmpeg_find_audio_stream(const AVFormatContext &format_context) noexcept
155
{
156
	for (unsigned i = 0; i < format_context.nb_streams; ++i)
157
		if (IsAudio(*format_context.streams[i]))
158 159 160 161 162
			return i;

	return -1;
}

163 164 165 166 167 168
/**
 * Accessor for AVStream::start_time that replaces AV_NOPTS_VALUE with
 * zero.  We can't use AV_NOPTS_VALUE in calculations, and we simply
 * assume that the stream's start time is zero, which appears to be
 * the best way out of that situation.
 */
169
static constexpr int64_t
170 171
start_time_fallback(const AVStream &stream)
{
172
	return FfmpegTimestampFallback(stream.start_time, 0);
173 174
}

175
/**
176
 * Copy PCM data from a non-empty AVFrame to an interleaved buffer.
177 178
 *
 * Throws #std::exception on error.
179
 */
180
static ConstBuffer<void>
181 182
copy_interleave_frame(const AVCodecContext &codec_context,
		      const AVFrame &frame,
183
		      FfmpegBuffer &global_buffer)
184
{
185 186
	assert(frame.nb_samples > 0);

187 188 189
	int plane_size;
	const int data_size =
		av_samples_get_buffer_size(&plane_size,
190 191 192
					   codec_context.channels,
					   frame.nb_samples,
					   codec_context.sample_fmt, 1);
193
	assert(data_size != 0);
194 195
	if (data_size < 0)
		throw MakeFfmpegError(data_size);
196

197
	void *output_buffer;
198 199
	if (av_sample_fmt_is_planar(codec_context.sample_fmt) &&
	    codec_context.channels > 1) {
200
		output_buffer = global_buffer.GetT<uint8_t>(data_size);
201
		if (output_buffer == nullptr)
202
			/* Not enough memory - shouldn't happen */
203
			throw std::bad_alloc();
204

205 206 207 208 209
		PcmInterleave(output_buffer,
			      ConstBuffer<const void *>((const void *const*)frame.extended_data,
							codec_context.channels),
			      frame.nb_samples,
			      av_get_bytes_per_sample(codec_context.sample_fmt));
210
	} else {
211
		output_buffer = frame.extended_data[0];
212 213
	}

214
	return { output_buffer, (size_t)data_size };
215 216
}

217 218 219 220 221 222
/**
 * Convert AVPacket::pts to a stream-relative time stamp (still in
 * AVStream::time_base units).  Returns a negative value on error.
 */
gcc_pure
static int64_t
223
StreamRelativePts(const AVPacket &packet, const AVStream &stream) noexcept
224 225 226 227 228 229 230 231 232
{
	auto pts = packet.pts;
	if (pts < 0 || pts == int64_t(AV_NOPTS_VALUE))
		return -1;

	auto start = start_time_fallback(stream);
	return pts - start;
}

233 234 235 236 237 238 239
/**
 * Convert a non-negative stream-relative time stamp in
 * AVStream::time_base units to a PCM frame number.
 */
gcc_pure
static uint64_t
PtsToPcmFrame(uint64_t pts, const AVStream &stream,
240
	      const AVCodecContext &codec_context) noexcept
241 242 243 244
{
	return av_rescale_q(pts, stream.time_base, codec_context.time_base);
}

245
/**
246 247
 * Invoke DecoderClient::SubmitData() with the contents of an
 * #AVFrame.
248 249
 */
static DecoderCommand
250
FfmpegSendFrame(DecoderClient &client, InputStream &is,
251 252 253 254 255
		AVCodecContext &codec_context,
		const AVFrame &frame,
		size_t &skip_bytes,
		FfmpegBuffer &buffer)
{
256 257 258 259 260
	ConstBuffer<void> output_buffer;

	try {
		output_buffer = copy_interleave_frame(codec_context, frame,
						      buffer);
261
	} catch (const std::exception &e) {
262
		/* this must be a serious error, e.g. OOM */
263
		LogError(e);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		return DecoderCommand::STOP;
	}

	if (skip_bytes > 0) {
		if (skip_bytes >= output_buffer.size) {
			skip_bytes -= output_buffer.size;
			return DecoderCommand::NONE;
		}

		output_buffer.data =
			(const uint8_t *)output_buffer.data + skip_bytes;
		output_buffer.size -= skip_bytes;
		skip_bytes = 0;
	}

279 280 281
	return client.SubmitData(is,
				 output_buffer.data, output_buffer.size,
				 codec_context.bit_rate / 1000);
282 283
}

284 285 286
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)

static DecoderCommand
287
FfmpegReceiveFrames(DecoderClient &client, InputStream &is,
288 289 290 291 292 293 294 295 296 297 298 299
		    AVCodecContext &codec_context,
		    AVFrame &frame,
		    size_t &skip_bytes,
		    FfmpegBuffer &buffer,
		    bool &eof)
{
	while (true) {
		DecoderCommand cmd;

		int err = avcodec_receive_frame(&codec_context, &frame);
		switch (err) {
		case 0:
300
			cmd = FfmpegSendFrame(client, is, codec_context,
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
					      frame, skip_bytes,
					      buffer);
			if (cmd != DecoderCommand::NONE)
				return cmd;

			break;

		case AVERROR_EOF:
			eof = true;
			return DecoderCommand::NONE;

		case AVERROR(EAGAIN):
			/* need to call avcodec_send_packet() */
			return DecoderCommand::NONE;

		default:
			{
				char msg[256];
				av_strerror(err, msg, sizeof(msg));
				FormatWarning(ffmpeg_domain,
					      "avcodec_send_packet() failed: %s",
					      msg);
			}

			return DecoderCommand::STOP;
		}
	}
}

#endif

332 333 334
/**
 * Decode an #AVPacket and send the resulting PCM data to the decoder
 * API.
Max Kellermann's avatar
Max Kellermann committed
335
 *
336 337 338
 * @param min_frame skip all data before this PCM frame number; this
 * is used after seeking to skip data in an AVPacket until the exact
 * desired time stamp has been reached
339
 */
340
static DecoderCommand
341
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
342
		   AVPacket &&packet,
343 344 345
		   AVCodecContext &codec_context,
		   const AVStream &stream,
		   AVFrame &frame,
346
		   uint64_t min_frame, size_t pcm_frame_size,
347
		   FfmpegBuffer &buffer)
348
{
349 350
	size_t skip_bytes = 0;

Max Kellermann's avatar
Max Kellermann committed
351
	const auto pts = StreamRelativePts(packet, stream);
352
	if (pts >= 0) {
353
		if (min_frame > 0) {
Max Kellermann's avatar
Max Kellermann committed
354 355
			auto cur_frame = PtsToPcmFrame(pts, stream,
						       codec_context);
356 357 358
			if (cur_frame < min_frame)
				skip_bytes = pcm_frame_size * (min_frame - cur_frame);
		} else
359 360
			client.SubmitTimestamp(FfmpegTimeToDouble(pts,
								  stream.time_base));
361
	}
362

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)
	bool eof = false;

	int err = avcodec_send_packet(&codec_context, &packet);
	switch (err) {
	case 0:
		break;

	case AVERROR_EOF:
		eof = true;
		break;

	default:
		{
			char msg[256];
			av_strerror(err, msg, sizeof(msg));
			FormatWarning(ffmpeg_domain,
				      "avcodec_send_packet() failed: %s", msg);
		}

		return DecoderCommand::NONE;
	}

386
	auto cmd = FfmpegReceiveFrames(client, is, codec_context,
387 388 389 390 391 392
				       frame,
				       skip_bytes, buffer, eof);

	if (eof)
		cmd = DecoderCommand::STOP;
#else
393
	DecoderCommand cmd = DecoderCommand::NONE;
394
	while (packet.size > 0 && cmd == DecoderCommand::NONE) {
395
		int got_frame = 0;
396 397
		int len = avcodec_decode_audio4(&codec_context,
						&frame, &got_frame,
398
						&packet);
399 400
		if (len < 0) {
			/* if error, we skip the frame */
401
			LogFfmpegError(len, "decoding failed, frame skipped");
402 403 404
			break;
		}

405 406
		packet.data += len;
		packet.size -= len;
407

408
		if (!got_frame || frame.nb_samples <= 0)
409 410
			continue;

411
		cmd = FfmpegSendFrame(client, is, codec_context,
412 413
				      frame, skip_bytes,
				      buffer);
414
	}
415
#endif
416

417
	return cmd;
418 419
}

420
static DecoderCommand
421
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
422 423 424
		   const AVPacket &packet,
		   AVCodecContext &codec_context,
		   const AVStream &stream,
425
		   AVFrame &frame,
426 427 428
		   uint64_t min_frame, size_t pcm_frame_size,
		   FfmpegBuffer &buffer)
{
429
	return ffmpeg_send_packet(client, is,
430 431 432 433 434 435 436 437
				  /* copy the AVPacket, because FFmpeg
				     < 3.0 requires this */
				  AVPacket(packet),
				  codec_context, stream,
				  frame, min_frame, pcm_frame_size,
				  buffer);
}

438
gcc_const
439
static SampleFormat
440
ffmpeg_sample_format(enum AVSampleFormat sample_fmt) noexcept
441
{
442
	switch (sample_fmt) {
443
	case AV_SAMPLE_FMT_S16:
444
	case AV_SAMPLE_FMT_S16P:
445
		return SampleFormat::S16;
446

447
	case AV_SAMPLE_FMT_S32:
448
	case AV_SAMPLE_FMT_S32P:
449
		return SampleFormat::S32;
450

451
	case AV_SAMPLE_FMT_FLT:
452
	case AV_SAMPLE_FMT_FLTP:
453
		return SampleFormat::FLOAT;
454

455
	default:
456 457 458 459 460 461
		break;
	}

	char buffer[64];
	const char *name = av_get_sample_fmt_string(buffer, sizeof(buffer),
						    sample_fmt);
462
	if (name != nullptr)
463 464 465
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %s (%d)",
			    name, sample_fmt);
466
	else
467 468 469
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %d",
			    sample_fmt);
470
	return SampleFormat::UNDEFINED;
471 472
}

473
static AVInputFormat *
474
ffmpeg_probe(DecoderClient *client, InputStream &is)
475
{
476 477
	constexpr size_t BUFFER_SIZE = 16384;
	constexpr size_t PADDING = 16;
478

479
	unsigned char buffer[BUFFER_SIZE];
480
	size_t nbytes = decoder_read(client, is, buffer, BUFFER_SIZE);
481
	if (nbytes <= PADDING)
482
		return nullptr;
483

484 485
	try {
		is.LockRewind();
486
	} catch (...) {
487 488 489
		return nullptr;
	}

490 491 492 493 494 495
	/* some ffmpeg parsers (e.g. ac3_parser.c) read a few bytes
	   beyond the declared buffer limit, which makes valgrind
	   angry; this workaround removes some padding from the buffer
	   size */
	nbytes -= PADDING;

496
	AVProbeData avpd;
497 498 499 500 501 502

	/* new versions of ffmpeg may add new attributes, and leaving
	   them uninitialized may crash; hopefully, zero-initializing
	   everything we don't know is ok */
	memset(&avpd, 0, sizeof(avpd));

503 504
	avpd.buf = buffer;
	avpd.buf_size = nbytes;
505
	avpd.filename = is.GetURI();
506

507
#ifdef AVPROBE_SCORE_MIME
508
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(56, 5, 1)
509 510 511 512
	/* this attribute was added in libav/ffmpeg version 11, but
	   unfortunately it's "uint8_t" instead of "char", and it's
	   not "const" - wtf? */
	avpd.mime_type = (uint8_t *)const_cast<char *>(is.GetMimeType());
513 514 515 516
#else
	/* API problem fixed in FFmpeg 2.5 */
	avpd.mime_type = is.GetMimeType();
#endif
517 518
#endif

519
	return av_probe_input_format(&avpd, true);
520 521
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
static void
FfmpegParseMetaData(AVDictionary &dict, ReplayGainInfo &rg, MixRampInfo &mr)
{
	AVDictionaryEntry *i = nullptr;

	while ((i = av_dict_get(&dict, "", i,
				AV_DICT_IGNORE_SUFFIX)) != nullptr) {
		const char *name = i->key;
		const char *value = i->value;

		if (!ParseReplayGainTag(rg, name, value))
			ParseMixRampTag(mr, name, value);
	}
}

static void
FfmpegParseMetaData(const AVStream &stream,
		    ReplayGainInfo &rg, MixRampInfo &mr)
{
	FfmpegParseMetaData(*stream.metadata, rg, mr);
}

static void
FfmpegParseMetaData(const AVFormatContext &format_context, int audio_stream,
		    ReplayGainInfo &rg, MixRampInfo &mr)
{
548
	assert(audio_stream >= 0);
549

550 551
	FfmpegParseMetaData(*format_context.metadata, rg, mr);
	FfmpegParseMetaData(*format_context.streams[audio_stream],
552 553 554 555
				    rg, mr);
}

static void
556
FfmpegParseMetaData(DecoderClient &client,
557 558 559 560 561 562 563 564 565 566 567
		    const AVFormatContext &format_context, int audio_stream)
{
	ReplayGainInfo rg;
	rg.Clear();

	MixRampInfo mr;
	mr.Clear();

	FfmpegParseMetaData(format_context, audio_stream, rg, mr);

	if (rg.IsDefined())
568
		client.SubmitReplayGain(&rg);
569 570

	if (mr.IsDefined())
571
		client.SubmitMixRamp(std::move(mr));
572 573
}

574 575
static void
FfmpegScanMetadata(const AVStream &stream,
576
		   const TagHandler &handler, void *handler_ctx)
577
{
578
	FfmpegScanDictionary(stream.metadata, handler, handler_ctx);
579 580 581 582
}

static void
FfmpegScanMetadata(const AVFormatContext &format_context, int audio_stream,
583
		   const TagHandler &handler, void *handler_ctx)
584
{
585 586
	assert(audio_stream >= 0);

587
	FfmpegScanDictionary(format_context.metadata, handler, handler_ctx);
588 589
	FfmpegScanMetadata(*format_context.streams[audio_stream],
			   handler, handler_ctx);
590 591
}

592 593 594 595 596 597 598 599 600 601 602 603
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)

static void
FfmpegScanTag(const AVFormatContext &format_context, int audio_stream,
	      TagBuilder &tag)
{
	FfmpegScanMetadata(format_context, audio_stream,
			   full_tag_handler, &tag);
}

/**
 * Check if a new stream tag was received and pass it to
604
 * DecoderClient::SubmitTag().
605 606
 */
static void
607
FfmpegCheckTag(DecoderClient &client, InputStream &is,
608 609 610 611 612 613 614 615 616 617 618 619
	       AVFormatContext &format_context, int audio_stream)
{
	AVStream &stream = *format_context.streams[audio_stream];
	if ((stream.event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) == 0)
		/* no new metadata */
		return;

	/* clear the flag */
	stream.event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;

	TagBuilder tag;
	FfmpegScanTag(format_context, audio_stream, tag);
620
	if (!tag.empty())
621
		client.SubmitTag(is, tag.Commit());
622 623 624 625
}

#endif

626
static void
627
FfmpegDecode(DecoderClient &client, InputStream &input,
628
	     AVFormatContext &format_context)
629
{
630
	const int find_result =
631
		avformat_find_stream_info(&format_context, nullptr);
632
	if (find_result < 0) {
633
		LogError(ffmpeg_domain, "Couldn't find stream info");
634 635
		return;
	}
636

637
	int audio_stream = ffmpeg_find_audio_stream(format_context);
638
	if (audio_stream == -1) {
639
		LogError(ffmpeg_domain, "No audio stream inside");
640 641
		return;
	}
642

643
	AVStream &av_stream = *format_context.streams[audio_stream];
644

645
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
646
	AVCodecContext *codec_context = av_stream.codec;
647 648
#endif

649
	const auto &codec_params = GetCodecParameters(av_stream);
650 651 652

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0)
	const AVCodecDescriptor *codec_descriptor =
653
		avcodec_descriptor_get(codec_params.codec_id);
654 655 656 657
	if (codec_descriptor != nullptr)
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_descriptor->name);
#else
658
	if (codec_context->codec_name[0] != 0)
659 660
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_context->codec_name);
661
#endif
662

663
	AVCodec *codec = avcodec_find_decoder(codec_params.codec_id);
664 665

	if (!codec) {
666
		LogError(ffmpeg_domain, "Unsupported audio codec");
667 668 669
		return;
	}

670 671 672 673 674 675 676 677 678 679
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 5, 0)
	AVCodecContext *codec_context = avcodec_alloc_context3(codec);
	if (codec_context == nullptr) {
		LogError(ffmpeg_domain, "avcodec_alloc_context3() failed");
		return;
	}

	AtScopeExit(&codec_context) {
		avcodec_free_context(&codec_context);
	};
680

681
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
682
	avcodec_parameters_to_context(codec_context, av_stream.codecpar);
683
#endif
684 685
#endif

686
	const SampleFormat sample_format =
687
		ffmpeg_sample_format(GetSampleFormat(codec_params));
688 689
	if (sample_format == SampleFormat::UNDEFINED) {
		// (error message already done by ffmpeg_sample_format())
690
		return;
691
	}
692

693 694 695
	const auto audio_format = CheckAudioFormat(codec_params.sample_rate,
						   sample_format,
						   codec_params.channels);
696 697 698 699 700 701

	/* the audio format must be read from AVCodecContext by now,
	   because avcodec_open() has been demonstrated to fill bogus
	   values into AVCodecContext.channels - a change that will be
	   reverted later by avcodec_decode_audio3() */

702
	const int open_result = avcodec_open2(codec_context, codec, nullptr);
703
	if (open_result < 0) {
704
		LogError(ffmpeg_domain, "Could not open codec");
705
		return;
706 707
	}

708
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
709 710 711
	AtScopeExit(codec_context) {
		avcodec_close(codec_context);
	};
712
#endif
713

714
	const SignedSongTime total_time =
715 716 717
		av_stream.duration != (int64_t)AV_NOPTS_VALUE
		? FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base)
		: FromFfmpegTimeChecked(format_context.duration, AV_TIME_BASE_Q);
718

719
	client.Ready(audio_format, input.IsSeekable(), total_time);
720

721
	FfmpegParseMetaData(client, format_context, audio_stream);
722

723 724 725
#if LIBAVUTIL_VERSION_MAJOR >= 53
	AVFrame *frame = av_frame_alloc();
#else
726
	AVFrame *frame = avcodec_alloc_frame();
727
#endif
728
	if (!frame) {
729
		LogError(ffmpeg_domain, "Could not allocate frame");
730 731 732
		return;
	}

733 734 735 736 737 738 739 740 741 742
	AtScopeExit(&frame) {
#if LIBAVUTIL_VERSION_MAJOR >= 53
		av_frame_free(&frame);
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
		avcodec_free_frame(&frame);
#else
		av_free(frame);
#endif
	};

743
	FfmpegBuffer interleaved_buffer;
744

745 746
	uint64_t min_frame = 0;

747
	DecoderCommand cmd = client.GetCommand();
748 749 750
	while (cmd != DecoderCommand::STOP) {
		if (cmd == DecoderCommand::SEEK) {
			int64_t where =
751
				ToFfmpegTime(client.GetSeekTime(),
752 753 754 755 756 757 758 759
					     av_stream.time_base) +
				start_time_fallback(av_stream);

			/* AVSEEK_FLAG_BACKWARD asks FFmpeg to seek to
			   the packet boundary before the seek time
			   stamp, not after */
			if (av_seek_frame(&format_context, audio_stream, where,
					  AVSEEK_FLAG_ANY|AVSEEK_FLAG_BACKWARD) < 0)
760
				client.SeekError();
761
			else {
762
				avcodec_flush_buffers(codec_context);
763 764
				min_frame = client.GetSeekFrame();
				client.CommandFinished();
765 766 767
			}
		}

768
		AVPacket packet;
769
		if (av_read_frame(&format_context, &packet) < 0)
770 771 772
			/* end of file */
			break;

773
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
774
		FfmpegCheckTag(client, input, format_context, audio_stream);
775 776
#endif

777
		if (packet.size > 0 && packet.stream_index == audio_stream) {
778
			cmd = ffmpeg_send_packet(client, input,
779
						 packet,
780
						 *codec_context,
781
						 av_stream,
782
						 *frame,
783
						 min_frame, audio_format.GetFrameSize(),
784
						 interleaved_buffer);
785 786
			min_frame = 0;
		} else
787
			cmd = client.GetCommand();
788

789 790 791
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 25, 100)
		av_packet_unref(&packet);
#else
792
		av_free_packet(&packet);
793
#endif
794
	}
795 796 797
}

static void
798
ffmpeg_decode(DecoderClient &client, InputStream &input)
799
{
800
	AVInputFormat *input_format = ffmpeg_probe(&client, input);
801 802 803 804 805 806
	if (input_format == nullptr)
		return;

	FormatDebug(ffmpeg_domain, "detected input format '%s' (%s)",
		    input_format->name, input_format->long_name);

807
	AvioStream stream(&client, input);
808 809 810 811 812
	if (!stream.Open()) {
		LogError(ffmpeg_domain, "Failed to open stream");
		return;
	}

813 814 815 816
	AVFormatContext *format_context;
	try {
		format_context =FfmpegOpenInput(stream.io, input.GetURI(),
						input_format);
817 818
	} catch (...) {
		LogError(std::current_exception());
819 820 821
		return;
	}

822 823 824
	AtScopeExit(&format_context) {
		avformat_close_input(&format_context);
	};
825

826
	FfmpegDecode(client, input, *format_context);
827 828
}

829 830
static bool
FfmpegScanStream(AVFormatContext &format_context,
831
		 const TagHandler &handler, void *handler_ctx)
832 833 834 835 836 837
{
	const int find_result =
		avformat_find_stream_info(&format_context, nullptr);
	if (find_result < 0)
		return false;

838 839 840 841
	const int audio_stream = ffmpeg_find_audio_stream(format_context);
	if (audio_stream < 0)
		return false;

842 843
	const AVStream &stream = *format_context.streams[audio_stream];
	if (stream.duration != (int64_t)AV_NOPTS_VALUE)
844
		tag_handler_invoke_duration(handler, handler_ctx,
845 846
					    FromFfmpegTime(stream.duration,
							   stream.time_base));
847 848 849 850
	else if (format_context.duration != (int64_t)AV_NOPTS_VALUE)
		tag_handler_invoke_duration(handler, handler_ctx,
					    FromFfmpegTime(format_context.duration,
							   AV_TIME_BASE_Q));
851

852
	FfmpegScanMetadata(format_context, audio_stream, handler, handler_ctx);
853 854 855 856

	return true;
}

857
static bool
858
ffmpeg_scan_stream(InputStream &is,
859
		   const TagHandler &handler, void *handler_ctx)
860
{
861 862
	AVInputFormat *input_format = ffmpeg_probe(nullptr, is);
	if (input_format == nullptr)
863
		return false;
864

865 866
	AvioStream stream(nullptr, is);
	if (!stream.Open())
867
		return false;
868

869 870 871
	AVFormatContext *f;
	try {
		f = FfmpegOpenInput(stream.io, is.GetURI(), input_format);
872
	} catch (...) {
873
		return false;
874
	}
875

876 877 878 879
	AtScopeExit(&f) {
		avformat_close_input(&f);
	};

880
	return FfmpegScanStream(*f, handler, handler_ctx);
881 882 883
}

/**
884 885 886 887
 * A list of extensions found for the formats supported by ffmpeg.
 * This list is current as of 02-23-09; To find out if there are more
 * supported formats, check the ffmpeg changelog since this date for
 * more formats.
888
 */
Max Kellermann's avatar
Max Kellermann committed
889
static const char *const ffmpeg_suffixes[] = {
890 891
	"16sv", "3g2", "3gp", "4xm", "8svx",
	"aa3", "aac", "ac3", "adx", "afc", "aif",
892 893 894 895
	"aifc", "aiff", "al", "alaw", "amr", "anim", "apc", "ape", "asf",
	"atrac", "au", "aud", "avi", "avm2", "avs", "bap", "bfi", "c93", "cak",
	"cin", "cmv", "cpk", "daud", "dct", "divx", "dts", "dv", "dvd", "dxa",
	"eac3", "film", "flac", "flc", "fli", "fll", "flx", "flv", "g726",
896 897 898
	"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
	"m4a", "m4b", "m4v",
	"mad",
899 900 901
	"mj2", "mjpeg", "mjpg", "mka", "mkv", "mlp", "mm", "mmf", "mov", "mp+",
	"mp1", "mp2", "mp3", "mp4", "mpc", "mpeg", "mpg", "mpga", "mpp", "mpu",
	"mve", "mvi", "mxf", "nc", "nsv", "nut", "nuv", "oga", "ogm", "ogv",
902
	"ogx", "oma", "ogg", "omg", "opus", "psp", "pva", "qcp", "qt", "r3d", "ra",
903
	"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
904
	"sol", "son", "spx", "str", "swf", "tak", "tgi", "tgq", "tgv", "thp", "ts",
905
	"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
906 907
	"vp6", "vmd", "wav", "webm", "wma", "wmv", "wsaud", "wsvga", "wv",
	"wve",
908
	nullptr
909 910
};

Max Kellermann's avatar
Max Kellermann committed
911
static const char *const ffmpeg_mime_types[] = {
912
	"application/flv",
913
	"application/m4a",
914 915 916 917 918
	"application/mp4",
	"application/octet-stream",
	"application/ogg",
	"application/x-ms-wmz",
	"application/x-ms-wmd",
919
	"application/x-ogg",
920 921 922 923 924
	"application/x-shockwave-flash",
	"application/x-shorten",
	"audio/8svx",
	"audio/16sv",
	"audio/aac",
925
	"audio/aacp",
926
	"audio/ac3",
927
	"audio/aiff"
928 929 930
	"audio/amr",
	"audio/basic",
	"audio/flac",
931 932
	"audio/m4a",
	"audio/mp4",
933 934 935
	"audio/mpeg",
	"audio/musepack",
	"audio/ogg",
936
	"audio/opus",
937 938
	"audio/qcelp",
	"audio/vorbis",
939
	"audio/vorbis+ogg",
940 941 942 943
	"audio/x-8svx",
	"audio/x-16sv",
	"audio/x-aac",
	"audio/x-ac3",
944
	"audio/x-adx",
945 946 947 948 949 950 951 952
	"audio/x-aiff"
	"audio/x-alaw",
	"audio/x-au",
	"audio/x-dca",
	"audio/x-eac3",
	"audio/x-flac",
	"audio/x-gsm",
	"audio/x-mace",
953
	"audio/x-matroska",
954 955
	"audio/x-monkeys-audio",
	"audio/x-mpeg",
956 957
	"audio/x-ms-wma",
	"audio/x-ms-wax",
958
	"audio/x-musepack",
959 960 961
	"audio/x-ogg",
	"audio/x-vorbis",
	"audio/x-vorbis+ogg",
962 963 964 965
	"audio/x-pn-realaudio",
	"audio/x-pn-multirate-realaudio",
	"audio/x-speex",
	"audio/x-tta"
966
	"audio/x-voc",
967 968 969 970 971 972 973 974
	"audio/x-wav",
	"audio/x-wma",
	"audio/x-wv",
	"video/anim",
	"video/quicktime",
	"video/msvideo",
	"video/ogg",
	"video/theora",
975
	"video/webm",
976 977 978 979 980 981 982
	"video/x-dv",
	"video/x-flv",
	"video/x-matroska",
	"video/x-mjpeg",
	"video/x-mpeg",
	"video/x-ms-asf",
	"video/x-msvideo",
983 984 985 986
	"video/x-ms-wmv",
	"video/x-ms-wvx",
	"video/x-ms-wm",
	"video/x-ms-wmx",
987 988 989 990 991 992
	"video/x-nut",
	"video/x-pva",
	"video/x-theora",
	"video/x-vid",
	"video/x-wmv",
	"video/x-xvid",
993 994 995 996 997 998

	/* special value for the "ffmpeg" input plugin: all streams by
	   the "ffmpeg" input plugin shall be decoded by this
	   plugin */
	"audio/x-mpd-ffmpeg",

999
	nullptr
1000 1001
};

1002
const struct DecoderPlugin ffmpeg_decoder_plugin = {
1003 1004
	"ffmpeg",
	ffmpeg_init,
1005
	ffmpeg_finish,
1006 1007 1008 1009 1010 1011 1012
	ffmpeg_decode,
	nullptr,
	nullptr,
	ffmpeg_scan_stream,
	nullptr,
	ffmpeg_suffixes,
	ffmpeg_mime_types
1013
};