FfmpegDecoderPlugin.cxx 23.7 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "tag/TagBuilder.hxx"
36
#include "tag/TagHandler.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
static AVFormatContext *
FfmpegOpenInput(AVIOContext *pb,
61
		const char *filename,
62
		AVInputFormat *fmt)
63 64
{
	AVFormatContext *context = avformat_alloc_context();
65 66
	if (context == nullptr)
		throw std::runtime_error("avformat_alloc_context() failed");
67 68

	context->pb = pb;
69

70 71 72
	int err = avformat_open_input(&context, filename, fmt, nullptr);
	if (err < 0) {
		avformat_free_context(context);
73
		throw MakeFfmpegError(err, "avformat_open_input() failed");
74 75
	}

76
	return context;
77 78
}

79
static bool
80
ffmpeg_init(gcc_unused const ConfigBlock &block)
81
{
82
	FfmpegInit();
83
	return true;
84 85
}

86
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

gcc_pure
static const AVCodecParameters &
GetCodecParameters(const AVStream &stream)
{
	return *stream.codecpar;
}

gcc_pure
static AVSampleFormat
GetSampleFormat(const AVCodecParameters &codec_params)
{
	return AVSampleFormat(codec_params.format);
}

#else

104 105 106 107 108 109 110 111 112 113 114 115 116 117
gcc_pure
static const AVCodecContext &
GetCodecParameters(const AVStream &stream)
{
	return *stream.codec;
}

gcc_pure
static AVSampleFormat
GetSampleFormat(const AVCodecContext &codec_context)
{
	return codec_context.sample_fmt;
}

118 119
#endif

120 121 122 123
gcc_pure
static bool
IsAudio(const AVStream &stream)
{
124
	return GetCodecParameters(stream).codec_type == AVMEDIA_TYPE_AUDIO;
125 126
}

127
gcc_pure
128
static int
129
ffmpeg_find_audio_stream(const AVFormatContext &format_context)
130
{
131
	for (unsigned i = 0; i < format_context.nb_streams; ++i)
132
		if (IsAudio(*format_context.streams[i]))
133 134 135 136 137
			return i;

	return -1;
}

138 139 140 141 142 143
/**
 * 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.
 */
144
static constexpr int64_t
145 146
start_time_fallback(const AVStream &stream)
{
147
	return FfmpegTimestampFallback(stream.start_time, 0);
148 149
}

150
/**
151
 * Copy PCM data from a non-empty AVFrame to an interleaved buffer.
152 153
 *
 * Throws #std::exception on error.
154
 */
155
static ConstBuffer<void>
156 157
copy_interleave_frame(const AVCodecContext &codec_context,
		      const AVFrame &frame,
158
		      FfmpegBuffer &global_buffer)
159
{
160 161
	assert(frame.nb_samples > 0);

162 163 164
	int plane_size;
	const int data_size =
		av_samples_get_buffer_size(&plane_size,
165 166 167
					   codec_context.channels,
					   frame.nb_samples,
					   codec_context.sample_fmt, 1);
168
	assert(data_size != 0);
169 170
	if (data_size < 0)
		throw MakeFfmpegError(data_size);
171

172
	void *output_buffer;
173 174
	if (av_sample_fmt_is_planar(codec_context.sample_fmt) &&
	    codec_context.channels > 1) {
175
		output_buffer = global_buffer.GetT<uint8_t>(data_size);
176
		if (output_buffer == nullptr)
177
			/* Not enough memory - shouldn't happen */
178
			throw std::bad_alloc();
179

180 181 182 183 184
		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));
185
	} else {
186
		output_buffer = frame.extended_data[0];
187 188
	}

189
	return { output_buffer, (size_t)data_size };
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/**
 * 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
StreamRelativePts(const AVPacket &packet, const AVStream &stream)
{
	auto pts = packet.pts;
	if (pts < 0 || pts == int64_t(AV_NOPTS_VALUE))
		return -1;

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

208 209 210 211 212 213 214 215 216 217 218 219
/**
 * 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,
	      const AVCodecContext &codec_context)
{
	return av_rescale_q(pts, stream.time_base, codec_context.time_base);
}

220
/**
221 222
 * Invoke DecoderClient::SubmitData() with the contents of an
 * #AVFrame.
223 224
 */
static DecoderCommand
225
FfmpegSendFrame(DecoderClient &client, InputStream &is,
226 227 228 229 230
		AVCodecContext &codec_context,
		const AVFrame &frame,
		size_t &skip_bytes,
		FfmpegBuffer &buffer)
{
231 232 233 234 235 236
	ConstBuffer<void> output_buffer;

	try {
		output_buffer = copy_interleave_frame(codec_context, frame,
						      buffer);
	} catch (const std::exception e) {
237
		/* this must be a serious error, e.g. OOM */
238
		LogError(e);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		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;
	}

254 255 256
	return client.SubmitData(is,
				 output_buffer.data, output_buffer.size,
				 codec_context.bit_rate / 1000);
257 258
}

259 260 261
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)

static DecoderCommand
262
FfmpegReceiveFrames(DecoderClient &client, InputStream &is,
263 264 265 266 267 268 269 270 271 272 273 274
		    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:
275
			cmd = FfmpegSendFrame(client, is, codec_context,
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
					      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

307 308 309
/**
 * Decode an #AVPacket and send the resulting PCM data to the decoder
 * API.
Max Kellermann's avatar
Max Kellermann committed
310
 *
311 312 313
 * @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
314
 */
315
static DecoderCommand
316
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
317
		   AVPacket &&packet,
318 319 320
		   AVCodecContext &codec_context,
		   const AVStream &stream,
		   AVFrame &frame,
321
		   uint64_t min_frame, size_t pcm_frame_size,
322
		   FfmpegBuffer &buffer)
323
{
324 325
	size_t skip_bytes = 0;

Max Kellermann's avatar
Max Kellermann committed
326
	const auto pts = StreamRelativePts(packet, stream);
327
	if (pts >= 0) {
328
		if (min_frame > 0) {
Max Kellermann's avatar
Max Kellermann committed
329 330
			auto cur_frame = PtsToPcmFrame(pts, stream,
						       codec_context);
331 332 333
			if (cur_frame < min_frame)
				skip_bytes = pcm_frame_size * (min_frame - cur_frame);
		} else
334 335
			client.SubmitTimestamp(FfmpegTimeToDouble(pts,
								  stream.time_base));
336
	}
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#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;
	}

361
	auto cmd = FfmpegReceiveFrames(client, is, codec_context,
362 363 364 365 366 367
				       frame,
				       skip_bytes, buffer, eof);

	if (eof)
		cmd = DecoderCommand::STOP;
#else
368
	DecoderCommand cmd = DecoderCommand::NONE;
369
	while (packet.size > 0 && cmd == DecoderCommand::NONE) {
370
		int got_frame = 0;
371 372
		int len = avcodec_decode_audio4(&codec_context,
						&frame, &got_frame,
373
						&packet);
374 375
		if (len < 0) {
			/* if error, we skip the frame */
376
			LogFfmpegError(len, "decoding failed, frame skipped");
377 378 379
			break;
		}

380 381
		packet.data += len;
		packet.size -= len;
382

383
		if (!got_frame || frame.nb_samples <= 0)
384 385
			continue;

386
		cmd = FfmpegSendFrame(client, is, codec_context,
387 388
				      frame, skip_bytes,
				      buffer);
389
	}
390
#endif
391

392
	return cmd;
393 394
}

395
static DecoderCommand
396
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
397 398 399
		   const AVPacket &packet,
		   AVCodecContext &codec_context,
		   const AVStream &stream,
400
		   AVFrame &frame,
401 402 403
		   uint64_t min_frame, size_t pcm_frame_size,
		   FfmpegBuffer &buffer)
{
404
	return ffmpeg_send_packet(client, is,
405 406 407 408 409 410 411 412
				  /* copy the AVPacket, because FFmpeg
				     < 3.0 requires this */
				  AVPacket(packet),
				  codec_context, stream,
				  frame, min_frame, pcm_frame_size,
				  buffer);
}

413
gcc_const
414
static SampleFormat
415
ffmpeg_sample_format(enum AVSampleFormat sample_fmt)
416
{
417
	switch (sample_fmt) {
418
	case AV_SAMPLE_FMT_S16:
419
	case AV_SAMPLE_FMT_S16P:
420
		return SampleFormat::S16;
421

422
	case AV_SAMPLE_FMT_S32:
423
	case AV_SAMPLE_FMT_S32P:
424
		return SampleFormat::S32;
425

426
	case AV_SAMPLE_FMT_FLT:
427
	case AV_SAMPLE_FMT_FLTP:
428
		return SampleFormat::FLOAT;
429

430
	default:
431 432 433 434 435 436
		break;
	}

	char buffer[64];
	const char *name = av_get_sample_fmt_string(buffer, sizeof(buffer),
						    sample_fmt);
437
	if (name != nullptr)
438 439 440
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %s (%d)",
			    name, sample_fmt);
441
	else
442 443 444
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %d",
			    sample_fmt);
445
	return SampleFormat::UNDEFINED;
446 447
}

448
static AVInputFormat *
449
ffmpeg_probe(DecoderClient *client, InputStream &is)
450
{
451 452
	constexpr size_t BUFFER_SIZE = 16384;
	constexpr size_t PADDING = 16;
453

454
	unsigned char buffer[BUFFER_SIZE];
455
	size_t nbytes = decoder_read(client, is, buffer, BUFFER_SIZE);
456
	if (nbytes <= PADDING)
457
		return nullptr;
458

459 460 461 462 463 464
	try {
		is.LockRewind();
	} catch (const std::runtime_error &) {
		return nullptr;
	}

465 466 467 468 469 470
	/* 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;

471
	AVProbeData avpd;
472 473 474 475 476 477

	/* 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));

478 479
	avpd.buf = buffer;
	avpd.buf_size = nbytes;
480
	avpd.filename = is.GetURI();
481

482
#ifdef AVPROBE_SCORE_MIME
483
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(56, 5, 1)
484 485 486 487
	/* 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());
488 489 490 491
#else
	/* API problem fixed in FFmpeg 2.5 */
	avpd.mime_type = is.GetMimeType();
#endif
492 493
#endif

494
	return av_probe_input_format(&avpd, true);
495 496
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
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)
{
523
	assert(audio_stream >= 0);
524

525 526
	FfmpegParseMetaData(*format_context.metadata, rg, mr);
	FfmpegParseMetaData(*format_context.streams[audio_stream],
527 528 529 530
				    rg, mr);
}

static void
531
FfmpegParseMetaData(DecoderClient &client,
532 533 534 535 536 537 538 539 540 541 542
		    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())
543
		client.SubmitReplayGain(&rg);
544 545

	if (mr.IsDefined())
546
		client.SubmitMixRamp(std::move(mr));
547 548
}

549 550
static void
FfmpegScanMetadata(const AVStream &stream,
551
		   const TagHandler &handler, void *handler_ctx)
552
{
553
	FfmpegScanDictionary(stream.metadata, handler, handler_ctx);
554 555 556 557
}

static void
FfmpegScanMetadata(const AVFormatContext &format_context, int audio_stream,
558
		   const TagHandler &handler, void *handler_ctx)
559
{
560 561
	assert(audio_stream >= 0);

562
	FfmpegScanDictionary(format_context.metadata, handler, handler_ctx);
563 564
	FfmpegScanMetadata(*format_context.streams[audio_stream],
			   handler, handler_ctx);
565 566
}

567 568 569 570 571 572 573 574 575 576 577 578
#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
579
 * DecoderClient::SubmitTag().
580 581
 */
static void
582
FfmpegCheckTag(DecoderClient &client, InputStream &is,
583 584 585 586 587 588 589 590 591 592 593 594 595
	       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);
	if (!tag.IsEmpty())
596
		client.SubmitTag(is, tag.Commit());
597 598 599 600
}

#endif

601
static void
602
FfmpegDecode(DecoderClient &client, InputStream &input,
603
	     AVFormatContext &format_context)
604
{
605
	const int find_result =
606
		avformat_find_stream_info(&format_context, nullptr);
607
	if (find_result < 0) {
608
		LogError(ffmpeg_domain, "Couldn't find stream info");
609 610
		return;
	}
611

612
	int audio_stream = ffmpeg_find_audio_stream(format_context);
613
	if (audio_stream == -1) {
614
		LogError(ffmpeg_domain, "No audio stream inside");
615 616
		return;
	}
617

618
	AVStream &av_stream = *format_context.streams[audio_stream];
619

620
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
621
	AVCodecContext *codec_context = av_stream.codec;
622 623
#endif

624
	const auto &codec_params = GetCodecParameters(av_stream);
625 626 627

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0)
	const AVCodecDescriptor *codec_descriptor =
628
		avcodec_descriptor_get(codec_params.codec_id);
629 630 631 632
	if (codec_descriptor != nullptr)
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_descriptor->name);
#else
633
	if (codec_context->codec_name[0] != 0)
634 635
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_context->codec_name);
636
#endif
637

638
	AVCodec *codec = avcodec_find_decoder(codec_params.codec_id);
639 640

	if (!codec) {
641
		LogError(ffmpeg_domain, "Unsupported audio codec");
642 643 644
		return;
	}

645 646 647 648 649 650 651 652 653 654
#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);
	};
655

656
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
657
	avcodec_parameters_to_context(codec_context, av_stream.codecpar);
658
#endif
659 660
#endif

661
	const SampleFormat sample_format =
662
		ffmpeg_sample_format(GetSampleFormat(codec_params));
663 664
	if (sample_format == SampleFormat::UNDEFINED) {
		// (error message already done by ffmpeg_sample_format())
665
		return;
666
	}
667

668 669 670
	const auto audio_format = CheckAudioFormat(codec_params.sample_rate,
						   sample_format,
						   codec_params.channels);
671 672 673 674 675 676

	/* 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() */

677
	const int open_result = avcodec_open2(codec_context, codec, nullptr);
678
	if (open_result < 0) {
679
		LogError(ffmpeg_domain, "Could not open codec");
680
		return;
681 682
	}

683
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
684 685 686
	AtScopeExit(codec_context) {
		avcodec_close(codec_context);
	};
687
#endif
688

689
	const SignedSongTime total_time =
690
		FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base);
691

692
	client.Ready(audio_format, input.IsSeekable(), total_time);
693

694
	FfmpegParseMetaData(client, format_context, audio_stream);
695

696 697 698
#if LIBAVUTIL_VERSION_MAJOR >= 53
	AVFrame *frame = av_frame_alloc();
#else
699
	AVFrame *frame = avcodec_alloc_frame();
700
#endif
701
	if (!frame) {
702
		LogError(ffmpeg_domain, "Could not allocate frame");
703 704 705
		return;
	}

706 707 708 709 710 711 712 713 714 715
	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
	};

716
	FfmpegBuffer interleaved_buffer;
717

718 719
	uint64_t min_frame = 0;

720
	DecoderCommand cmd = client.GetCommand();
721 722 723
	while (cmd != DecoderCommand::STOP) {
		if (cmd == DecoderCommand::SEEK) {
			int64_t where =
724
				ToFfmpegTime(client.GetSeekTime(),
725 726 727 728 729 730 731 732
					     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)
733
				client.SeekError();
734
			else {
735
				avcodec_flush_buffers(codec_context);
736 737
				min_frame = client.GetSeekFrame();
				client.CommandFinished();
738 739 740
			}
		}

741
		AVPacket packet;
742
		if (av_read_frame(&format_context, &packet) < 0)
743 744 745
			/* end of file */
			break;

746
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
747
		FfmpegCheckTag(client, input, format_context, audio_stream);
748 749
#endif

750
		if (packet.size > 0 && packet.stream_index == audio_stream) {
751
			cmd = ffmpeg_send_packet(client, input,
752
						 packet,
753
						 *codec_context,
754
						 av_stream,
755
						 *frame,
756
						 min_frame, audio_format.GetFrameSize(),
757
						 interleaved_buffer);
758 759
			min_frame = 0;
		} else
760
			cmd = client.GetCommand();
761

762 763 764
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 25, 100)
		av_packet_unref(&packet);
#else
765
		av_free_packet(&packet);
766
#endif
767
	}
768 769 770
}

static void
771
ffmpeg_decode(DecoderClient &client, InputStream &input)
772
{
773
	AVInputFormat *input_format = ffmpeg_probe(&client, input);
774 775 776 777 778 779
	if (input_format == nullptr)
		return;

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

780
	AvioStream stream(&client, input);
781 782 783 784 785
	if (!stream.Open()) {
		LogError(ffmpeg_domain, "Failed to open stream");
		return;
	}

786 787 788 789 790 791
	AVFormatContext *format_context;
	try {
		format_context =FfmpegOpenInput(stream.io, input.GetURI(),
						input_format);
	} catch (const std::runtime_error &e) {
		LogError(e);
792 793 794
		return;
	}

795 796 797
	AtScopeExit(&format_context) {
		avformat_close_input(&format_context);
	};
798

799
	FfmpegDecode(client, input, *format_context);
800 801
}

802 803
static bool
FfmpegScanStream(AVFormatContext &format_context,
804
		 const TagHandler &handler, void *handler_ctx)
805 806 807 808 809 810
{
	const int find_result =
		avformat_find_stream_info(&format_context, nullptr);
	if (find_result < 0)
		return false;

811 812 813 814
	const int audio_stream = ffmpeg_find_audio_stream(format_context);
	if (audio_stream < 0)
		return false;

815 816
	const AVStream &stream = *format_context.streams[audio_stream];
	if (stream.duration != (int64_t)AV_NOPTS_VALUE)
817
		tag_handler_invoke_duration(handler, handler_ctx,
818 819
					    FromFfmpegTime(stream.duration,
							   stream.time_base));
820

821
	FfmpegScanMetadata(format_context, audio_stream, handler, handler_ctx);
822 823 824 825

	return true;
}

826
static bool
827
ffmpeg_scan_stream(InputStream &is,
828
		   const TagHandler &handler, void *handler_ctx)
829
{
830 831
	AVInputFormat *input_format = ffmpeg_probe(nullptr, is);
	if (input_format == nullptr)
832
		return false;
833

834 835
	AvioStream stream(nullptr, is);
	if (!stream.Open())
836
		return false;
837

838 839 840 841
	AVFormatContext *f;
	try {
		f = FfmpegOpenInput(stream.io, is.GetURI(), input_format);
	} catch (const std::runtime_error &) {
842
		return false;
843
	}
844

845 846 847 848
	AtScopeExit(&f) {
		avformat_close_input(&f);
	};

849
	return FfmpegScanStream(*f, handler, handler_ctx);
850 851 852
}

/**
853 854 855 856
 * 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.
857
 */
Max Kellermann's avatar
Max Kellermann committed
858
static const char *const ffmpeg_suffixes[] = {
859 860 861 862 863
	"16sv", "3g2", "3gp", "4xm", "8svx", "aa3", "aac", "ac3", "afc", "aif",
	"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",
864 865 866
	"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
	"m4a", "m4b", "m4v",
	"mad",
867 868 869
	"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",
870
	"ogx", "oma", "ogg", "omg", "opus", "psp", "pva", "qcp", "qt", "r3d", "ra",
871
	"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
872
	"sol", "son", "spx", "str", "swf", "tak", "tgi", "tgq", "tgv", "thp", "ts",
873
	"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
874 875
	"vp6", "vmd", "wav", "webm", "wma", "wmv", "wsaud", "wsvga", "wv",
	"wve",
876
	nullptr
877 878
};

Max Kellermann's avatar
Max Kellermann committed
879
static const char *const ffmpeg_mime_types[] = {
880
	"application/flv",
881
	"application/m4a",
882 883 884 885 886
	"application/mp4",
	"application/octet-stream",
	"application/ogg",
	"application/x-ms-wmz",
	"application/x-ms-wmd",
887
	"application/x-ogg",
888 889 890 891 892
	"application/x-shockwave-flash",
	"application/x-shorten",
	"audio/8svx",
	"audio/16sv",
	"audio/aac",
893
	"audio/aacp",
894
	"audio/ac3",
895
	"audio/aiff"
896 897 898
	"audio/amr",
	"audio/basic",
	"audio/flac",
899 900
	"audio/m4a",
	"audio/mp4",
901 902 903
	"audio/mpeg",
	"audio/musepack",
	"audio/ogg",
904
	"audio/opus",
905 906
	"audio/qcelp",
	"audio/vorbis",
907
	"audio/vorbis+ogg",
908 909 910 911 912 913 914 915 916 917 918 919
	"audio/x-8svx",
	"audio/x-16sv",
	"audio/x-aac",
	"audio/x-ac3",
	"audio/x-aiff"
	"audio/x-alaw",
	"audio/x-au",
	"audio/x-dca",
	"audio/x-eac3",
	"audio/x-flac",
	"audio/x-gsm",
	"audio/x-mace",
920
	"audio/x-matroska",
921 922
	"audio/x-monkeys-audio",
	"audio/x-mpeg",
923 924
	"audio/x-ms-wma",
	"audio/x-ms-wax",
925
	"audio/x-musepack",
926 927 928
	"audio/x-ogg",
	"audio/x-vorbis",
	"audio/x-vorbis+ogg",
929 930 931 932
	"audio/x-pn-realaudio",
	"audio/x-pn-multirate-realaudio",
	"audio/x-speex",
	"audio/x-tta"
933
	"audio/x-voc",
934 935 936 937 938 939 940 941
	"audio/x-wav",
	"audio/x-wma",
	"audio/x-wv",
	"video/anim",
	"video/quicktime",
	"video/msvideo",
	"video/ogg",
	"video/theora",
942
	"video/webm",
943 944 945 946 947 948 949
	"video/x-dv",
	"video/x-flv",
	"video/x-matroska",
	"video/x-mjpeg",
	"video/x-mpeg",
	"video/x-ms-asf",
	"video/x-msvideo",
950 951 952 953
	"video/x-ms-wmv",
	"video/x-ms-wvx",
	"video/x-ms-wm",
	"video/x-ms-wmx",
954 955 956 957 958 959
	"video/x-nut",
	"video/x-pva",
	"video/x-theora",
	"video/x-vid",
	"video/x-wmv",
	"video/x-xvid",
960 961 962 963 964 965

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

966
	nullptr
967 968
};

969
const struct DecoderPlugin ffmpeg_decoder_plugin = {
970 971 972 973 974 975 976 977 978 979
	"ffmpeg",
	ffmpeg_init,
	nullptr,
	ffmpeg_decode,
	nullptr,
	nullptr,
	ffmpeg_scan_stream,
	nullptr,
	ffmpeg_suffixes,
	ffmpeg_mime_types
980
};