FfmpegDecoderPlugin.cxx 20.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config.h"
24
#include "FfmpegDecoderPlugin.hxx"
25
#include "lib/ffmpeg/Domain.hxx"
26
#include "../DecoderAPI.hxx"
27
#include "FfmpegMetaData.hxx"
28
#include "tag/TagHandler.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "input/InputStream.hxx"
30
#include "CheckAudioFormat.hxx"
31
#include "util/Error.hxx"
32 33
#include "util/Domain.hxx"
#include "LogV.hxx"
34 35

extern "C" {
36 37 38
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
39
#include <libavutil/avutil.h>
40
#include <libavutil/log.h>
41
#include <libavutil/mathematics.h>
42 43 44 45

#if LIBAVUTIL_VERSION_MAJOR >= 53
#include <libavutil/frame.h>
#endif
46
}
47

48 49 50
#include <assert.h>
#include <string.h>

51 52 53 54 55
/* suppress the ffmpeg compatibility macro */
#ifdef SampleFormat
#undef SampleFormat
#endif

56 57
static LogLevel
import_ffmpeg_level(int level)
58 59
{
	if (level <= AV_LOG_FATAL)
60
		return LogLevel::ERROR;
61

62 63
	if (level <= AV_LOG_WARNING)
		return LogLevel::WARNING;
64 65

	if (level <= AV_LOG_INFO)
66
		return LogLevel::INFO;
67

68
	return LogLevel::DEBUG;
69 70 71
}

static void
72
mpd_ffmpeg_log_callback(gcc_unused void *ptr, int level,
73 74
			const char *fmt, va_list vl)
{
75
	const AVClass * cls = nullptr;
76

77
	if (ptr != nullptr)
78 79
		cls = *(const AVClass *const*)ptr;

80
	if (cls != nullptr) {
81 82 83
		char domain[64];
		snprintf(domain, sizeof(domain), "%s/%s",
			 ffmpeg_domain.GetName(), cls->item_name(ptr));
84 85
		const Domain d(domain);
		LogFormatV(d, import_ffmpeg_level(level), fmt, vl);
86
	}
87 88
}

89
struct AvioStream {
90
	Decoder *const decoder;
91
	InputStream &input;
92

93
	AVIOContext *io;
94

95
	AvioStream(Decoder *_decoder, InputStream &_input)
96 97 98
		:decoder(_decoder), input(_input), io(nullptr) {}

	~AvioStream() {
99 100
		if (io != nullptr) {
			av_free(io->buffer);
101
			av_free(io);
102
		}
103 104 105
	}

	bool Open();
106
};
107

108 109
static int
mpd_ffmpeg_stream_read(void *opaque, uint8_t *buf, int size)
110
{
111
	AvioStream *stream = (AvioStream *)opaque;
112

113 114
	return decoder_read(stream->decoder, stream->input,
			    (void *)buf, size);
115 116
}

117 118
static int64_t
mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence)
119
{
120
	AvioStream *stream = (AvioStream *)opaque;
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	switch (whence) {
	case SEEK_SET:
		break;

	case SEEK_CUR:
		pos += stream->input.GetOffset();
		break;

	case SEEK_END:
		if (!stream->input.KnownSize())
			return -1;

		pos += stream->input.GetSize();
		break;

	case AVSEEK_SIZE:
138 139 140
		if (!stream->input.KnownSize())
			return -1;

141
		return stream->input.GetSize();
142

143 144 145 146 147
	default:
		return -1;
	}

	if (!stream->input.LockSeek(pos, IgnoreError()))
148 149
		return -1;

150
	return stream->input.GetOffset();
151 152
}

153 154
bool
AvioStream::Open()
155
{
156 157 158 159 160 161
	constexpr size_t BUFFER_SIZE = 8192;
	auto buffer = (unsigned char *)av_malloc(BUFFER_SIZE);
	if (buffer == nullptr)
		return false;

	io = avio_alloc_context(buffer, BUFFER_SIZE,
162 163
				false, this,
				mpd_ffmpeg_stream_read, nullptr,
164
				input.IsSeekable()
165
				? mpd_ffmpeg_stream_seek : nullptr);
166 167 168 169
	/* If avio_alloc_context() fails, who frees the buffer?  The
	   libavformat API documentation does not specify this, it
	   only says that AVIOContext.buffer must be freed in the end,
	   however no AVIOContext exists in that failure code path. */
170
	return io != nullptr;
171 172
}

173 174 175 176 177 178 179 180 181 182 183
/**
 * API compatibility wrapper for av_open_input_stream() and
 * avformat_open_input().
 */
static int
mpd_ffmpeg_open_input(AVFormatContext **ic_ptr,
		      AVIOContext *pb,
		      const char *filename,
		      AVInputFormat *fmt)
{
	AVFormatContext *context = avformat_alloc_context();
184
	if (context == nullptr)
185 186 187 188
		return AVERROR(ENOMEM);

	context->pb = pb;
	*ic_ptr = context;
189
	return avformat_open_input(ic_ptr, filename, fmt, nullptr);
190 191
}

192
static bool
193
ffmpeg_init(gcc_unused const config_param &param)
194
{
195 196
	av_log_set_callback(mpd_ffmpeg_log_callback);

197
	av_register_all();
198
	return true;
199 200
}

201 202 203 204 205
static int
ffmpeg_find_audio_stream(const AVFormatContext *format_context)
{
	for (unsigned i = 0; i < format_context->nb_streams; ++i)
		if (format_context->streams[i]->codec->codec_type ==
206
		    AVMEDIA_TYPE_AUDIO)
207 208 209 210 211
			return i;

	return -1;
}

212
gcc_const
213 214 215 216 217
static double
time_from_ffmpeg(int64_t t, const AVRational time_base)
{
	assert(t != (int64_t)AV_NOPTS_VALUE);

218 219
	return (double)av_rescale_q(t, time_base, (AVRational){1, 1024})
		/ (double)1024;
220 221
}

222 223 224 225 226 227 228
template<typename Ratio>
static constexpr AVRational
RatioToAVRational()
{
	return { Ratio::num, Ratio::den };
}

229
gcc_const
230
static int64_t
231
time_to_ffmpeg(SongTime t, const AVRational time_base)
232
{
233 234
	return av_rescale_q(t.count(),
			    RatioToAVRational<SongTime::period>(),
235 236 237
			    time_base);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
/**
 * Replace #AV_NOPTS_VALUE with the given fallback.
 */
static constexpr int64_t
timestamp_fallback(int64_t t, int64_t fallback)
{
	return gcc_likely(t != int64_t(AV_NOPTS_VALUE))
		? t
		: fallback;
}

/**
 * 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.
 */
static int64_t
start_time_fallback(const AVStream &stream)
{
	return timestamp_fallback(stream.start_time, 0);
}

261
static void
262 263 264
copy_interleave_frame2(uint8_t *dest, uint8_t **src,
		       unsigned nframes, unsigned nchannels,
		       unsigned sample_size)
265
{
266 267 268 269 270 271
	for (unsigned frame = 0; frame < nframes; ++frame) {
		for (unsigned channel = 0; channel < nchannels; ++channel) {
			memcpy(dest, src[channel] + frame * sample_size,
			       sample_size);
			dest += sample_size;
		}
272 273 274
	}
}

275 276 277 278 279 280
/**
 * Copy PCM data from a AVFrame to an interleaved buffer.
 */
static int
copy_interleave_frame(const AVCodecContext *codec_context,
		      const AVFrame *frame,
281 282
		      uint8_t **output_buffer,
		      uint8_t **global_buffer, int *global_buffer_size)
283 284 285 286 287 288 289
{
	int plane_size;
	const int data_size =
		av_samples_get_buffer_size(&plane_size,
					   codec_context->channels,
					   frame->nb_samples,
					   codec_context->sample_fmt, 1);
290 291 292
	if (data_size <= 0)
		return data_size;

293 294
	if (av_sample_fmt_is_planar(codec_context->sample_fmt) &&
	    codec_context->channels > 1) {
295 296 297 298 299 300 301 302 303 304 305 306
		if(*global_buffer_size < data_size) {
			av_freep(global_buffer);

			*global_buffer = (uint8_t*)av_malloc(data_size);

			if (!*global_buffer)
				/* Not enough memory - shouldn't happen */
				return AVERROR(ENOMEM);
			*global_buffer_size = data_size;
		}
		*output_buffer = *global_buffer;
		copy_interleave_frame2(*output_buffer, frame->extended_data,
307 308 309
				       frame->nb_samples,
				       codec_context->channels,
				       av_get_bytes_per_sample(codec_context->sample_fmt));
310
	} else {
311
		*output_buffer = frame->extended_data[0];
312 313 314 315 316
	}

	return data_size;
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/**
 * 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;
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/**
 * 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);
}

/**
 * @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
 */
350
static DecoderCommand
351
ffmpeg_send_packet(Decoder &decoder, InputStream &is,
352
		   const AVPacket *packet,
353
		   AVCodecContext *codec_context,
354
		   const AVStream *stream,
355
		   AVFrame *frame,
356
		   uint64_t min_frame, size_t pcm_frame_size,
357
		   uint8_t **buffer, int *buffer_size)
358
{
359 360
	size_t skip_bytes = 0;

361 362
	const auto pts = StreamRelativePts(*packet, *stream);
	if (pts >= 0) {
363 364 365 366 367 368 369 370
		if (min_frame > 0) {
			auto cur_frame = PtsToPcmFrame(pts, *stream,
						       *codec_context);
			if (cur_frame < min_frame)
				skip_bytes = pcm_frame_size * (min_frame - cur_frame);
		} else
			decoder_timestamp(decoder,
					  time_from_ffmpeg(pts, stream->time_base));
371
	}
372

373
	AVPacket packet2 = *packet;
374

375
	uint8_t *output_buffer;
376

377 378
	DecoderCommand cmd = DecoderCommand::NONE;
	while (packet2.size > 0 && cmd == DecoderCommand::NONE) {
379
		int audio_size = 0;
380 381
		int got_frame = 0;
		int len = avcodec_decode_audio4(codec_context,
382
						frame, &got_frame,
383 384 385
						&packet2);
		if (len >= 0 && got_frame) {
			audio_size = copy_interleave_frame(codec_context,
386
							   frame,
387 388
							   &output_buffer,
							   buffer, buffer_size);
389 390
			if (audio_size < 0)
				len = audio_size;
391
		}
392 393 394

		if (len < 0) {
			/* if error, we skip the frame */
395 396
			LogDefault(ffmpeg_domain,
				   "decoding failed, frame skipped");
397 398 399
			break;
		}

400 401
		packet2.data += len;
		packet2.size -= len;
402

403
		if (audio_size <= 0)
404
			continue;
405

406 407 408 409 410 411 412 413 414 415 416 417
		const uint8_t *data = output_buffer;
		if (skip_bytes > 0) {
			if (skip_bytes >= size_t(audio_size)) {
				skip_bytes -= audio_size;
				continue;
			}

			data += skip_bytes;
			audio_size -= skip_bytes;
			skip_bytes = 0;
		}

418
		cmd = decoder_data(decoder, is,
419
				   data, audio_size,
420
				   codec_context->bit_rate / 1000);
421
	}
422
	return cmd;
423 424
}

425
gcc_const
426
static SampleFormat
427
ffmpeg_sample_format(enum AVSampleFormat sample_fmt)
428
{
429
	switch (sample_fmt) {
430
	case AV_SAMPLE_FMT_S16:
431
	case AV_SAMPLE_FMT_S16P:
432
		return SampleFormat::S16;
433

434
	case AV_SAMPLE_FMT_S32:
435
	case AV_SAMPLE_FMT_S32P:
436
		return SampleFormat::S32;
437

438
	case AV_SAMPLE_FMT_FLT:
439
	case AV_SAMPLE_FMT_FLTP:
440
		return SampleFormat::FLOAT;
441

442
	default:
443 444 445 446 447 448
		break;
	}

	char buffer[64];
	const char *name = av_get_sample_fmt_string(buffer, sizeof(buffer),
						    sample_fmt);
449
	if (name != nullptr)
450 451 452
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %s (%d)",
			    name, sample_fmt);
453
	else
454 455 456
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %d",
			    sample_fmt);
457
	return SampleFormat::UNDEFINED;
458 459
}

460
static AVInputFormat *
461
ffmpeg_probe(Decoder *decoder, InputStream &is)
462 463 464 465 466 467
{
	enum {
		BUFFER_SIZE = 16384,
		PADDING = 16,
	};

468
	unsigned char buffer[BUFFER_SIZE];
469
	size_t nbytes = decoder_read(decoder, is, buffer, BUFFER_SIZE);
470
	if (nbytes <= PADDING || !is.LockRewind(IgnoreError()))
471
		return nullptr;
472 473 474 475 476 477 478

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

479
	AVProbeData avpd;
480 481 482 483 484 485

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

486 487
	avpd.buf = buffer;
	avpd.buf_size = nbytes;
488
	avpd.filename = is.GetURI();
489

490
#ifdef AVPROBE_SCORE_MIME
491
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(56, 5, 1)
492 493 494 495
	/* 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());
496 497 498 499
#else
	/* API problem fixed in FFmpeg 2.5 */
	avpd.mime_type = is.GetMimeType();
#endif
500 501
#endif

502
	return av_probe_input_format(&avpd, true);
503 504
}

505
static void
506
ffmpeg_decode(Decoder &decoder, InputStream &input)
507
{
508
	AVInputFormat *input_format = ffmpeg_probe(&decoder, input);
509
	if (input_format == nullptr)
510 511
		return;

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

515
	AvioStream stream(&decoder, input);
516
	if (!stream.Open()) {
517
		LogError(ffmpeg_domain, "Failed to open stream");
518 519 520
		return;
	}

521
	//ffmpeg works with ours "fileops" helper
522
	AVFormatContext *format_context = nullptr;
523
	if (mpd_ffmpeg_open_input(&format_context, stream.io,
524
				  input.GetURI(),
525
				  input_format) != 0) {
526
		LogError(ffmpeg_domain, "Open failed");
527 528 529
		return;
	}

530
	const int find_result =
531
		avformat_find_stream_info(format_context, nullptr);
532
	if (find_result < 0) {
533
		LogError(ffmpeg_domain, "Couldn't find stream info");
534
		avformat_close_input(&format_context);
535 536
		return;
	}
537

538
	int audio_stream = ffmpeg_find_audio_stream(format_context);
539
	if (audio_stream == -1) {
540
		LogError(ffmpeg_domain, "No audio stream inside");
541
		avformat_close_input(&format_context);
542 543
		return;
	}
544

545 546 547
	AVStream *av_stream = format_context->streams[audio_stream];

	AVCodecContext *codec_context = av_stream->codec;
548 549 550 551 552 553 554 555

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0)
	const AVCodecDescriptor *codec_descriptor =
		avcodec_descriptor_get(codec_context->codec_id);
	if (codec_descriptor != nullptr)
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_descriptor->name);
#else
556
	if (codec_context->codec_name[0] != 0)
557 558
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_context->codec_name);
559
#endif
560

561
	AVCodec *codec = avcodec_find_decoder(codec_context->codec_id);
562 563

	if (!codec) {
564
		LogError(ffmpeg_domain, "Unsupported audio codec");
565
		avformat_close_input(&format_context);
566 567 568
		return;
	}

569
	const SampleFormat sample_format =
570
		ffmpeg_sample_format(codec_context->sample_fmt);
571 572 573
	if (sample_format == SampleFormat::UNDEFINED) {
		// (error message already done by ffmpeg_sample_format())
		avformat_close_input(&format_context);
574
		return;
575
	}
576

577
	Error error;
578 579
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format,
580
				       codec_context->sample_rate,
581
				       sample_format,
582
				       codec_context->channels, error)) {
583
		LogError(error);
584
		avformat_close_input(&format_context);
585 586 587 588 589 590 591 592
		return;
	}

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

593
	const int open_result = avcodec_open2(codec_context, codec, nullptr);
594
	if (open_result < 0) {
595
		LogError(ffmpeg_domain, "Could not open codec");
596
		avformat_close_input(&format_context);
597
		return;
598 599
	}

600 601 602 603 604
	const SignedSongTime total_time =
		format_context->duration != (int64_t)AV_NOPTS_VALUE
		? SignedSongTime::FromScale<uint64_t>(format_context->duration,
						      AV_TIME_BASE)
		: SignedSongTime::Negative();
605

606
	decoder_initialized(decoder, audio_format,
607
			    input.IsSeekable(), total_time);
608

609 610 611
#if LIBAVUTIL_VERSION_MAJOR >= 53
	AVFrame *frame = av_frame_alloc();
#else
612
	AVFrame *frame = avcodec_alloc_frame();
613
#endif
614
	if (!frame) {
615
		LogError(ffmpeg_domain, "Could not allocate frame");
616 617 618 619
		avformat_close_input(&format_context);
		return;
	}

620
	uint8_t *interleaved_buffer = nullptr;
621 622
	int interleaved_buffer_size = 0;

623 624
	uint64_t min_frame = 0;

625
	DecoderCommand cmd;
626
	do {
627
		AVPacket packet;
Max Kellermann's avatar
Max Kellermann committed
628
		if (av_read_frame(format_context, &packet) < 0)
629 630 631
			/* end of file */
			break;

632
		if (packet.stream_index == audio_stream) {
633
			cmd = ffmpeg_send_packet(decoder, input,
Max Kellermann's avatar
Max Kellermann committed
634
						 &packet, codec_context,
635
						 av_stream,
636
						 frame,
637
						 min_frame, audio_format.GetFrameSize(),
638
						 &interleaved_buffer, &interleaved_buffer_size);
639 640
			min_frame = 0;
		} else
641 642 643
			cmd = decoder_get_command(decoder);

		av_free_packet(&packet);
644

645
		if (cmd == DecoderCommand::SEEK) {
646
			int64_t where =
647
				time_to_ffmpeg(decoder_seek_time(decoder),
648
					       av_stream->time_base) +
649
				start_time_fallback(*av_stream);
650

651 652 653 654
			/* AVSEEK_FLAG_BACKWARD asks FFmpeg to seek to
			   the packet boundary before the seek time
			   stamp, not after */

655
			if (av_seek_frame(format_context, audio_stream, where,
656
					  AVSEEK_FLAG_ANY|AVSEEK_FLAG_BACKWARD) < 0)
657
				decoder_seek_error(decoder);
658 659
			else {
				avcodec_flush_buffers(codec_context);
660
				min_frame = decoder_seek_where_frame(decoder);
661
				decoder_command_finished(decoder);
662
			}
663
		}
664
	} while (cmd != DecoderCommand::STOP);
665

666 667 668
#if LIBAVUTIL_VERSION_MAJOR >= 53
	av_frame_free(&frame);
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
669 670 671 672
	avcodec_free_frame(&frame);
#else
	av_freep(&frame);
#endif
673
	av_freep(&interleaved_buffer);
674

675
	avcodec_close(codec_context);
676
	avformat_close_input(&format_context);
677 678
}

679
//no tag reading in ffmpeg, check if playable
680
static bool
681
ffmpeg_scan_stream(InputStream &is,
682
		   const struct tag_handler *handler, void *handler_ctx)
683
{
684 685
	AVInputFormat *input_format = ffmpeg_probe(nullptr, is);
	if (input_format == nullptr)
686
		return false;
687

688 689
	AvioStream stream(nullptr, is);
	if (!stream.Open())
690
		return false;
691

692
	AVFormatContext *f = nullptr;
693
	if (mpd_ffmpeg_open_input(&f, stream.io, is.GetURI(),
694
				  input_format) != 0)
695
		return false;
696

697
	const int find_result =
698
		avformat_find_stream_info(f, nullptr);
699 700
	if (find_result < 0) {
		avformat_close_input(&f);
701
		return false;
702 703
	}

704 705 706 707 708 709
	if (f->duration != (int64_t)AV_NOPTS_VALUE) {
		const auto duration =
			SongTime::FromScale<uint64_t>(f->duration,
						      AV_TIME_BASE);
		tag_handler_invoke_duration(handler, handler_ctx, duration);
	}
710

711
	ffmpeg_scan_dictionary(f->metadata, handler, handler_ctx);
712 713
	int idx = ffmpeg_find_audio_stream(f);
	if (idx >= 0)
714 715
		ffmpeg_scan_dictionary(f->streams[idx]->metadata,
				       handler, handler_ctx);
716

717
	avformat_close_input(&f);
718
	return true;
719 720 721
}

/**
722 723 724 725
 * 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.
726
 */
Max Kellermann's avatar
Max Kellermann committed
727
static const char *const ffmpeg_suffixes[] = {
728 729 730 731 732
	"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",
733 734 735
	"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
	"m4a", "m4b", "m4v",
	"mad",
736 737 738
	"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",
739
	"ogx", "oma", "ogg", "omg", "opus", "psp", "pva", "qcp", "qt", "r3d", "ra",
740 741 742
	"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
	"sol", "son", "spx", "str", "swf", "tgi", "tgq", "tgv", "thp", "ts",
	"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
743 744
	"vp6", "vmd", "wav", "webm", "wma", "wmv", "wsaud", "wsvga", "wv",
	"wve",
745
	nullptr
746 747
};

Max Kellermann's avatar
Max Kellermann committed
748
static const char *const ffmpeg_mime_types[] = {
749
	"application/flv",
750
	"application/m4a",
751 752 753 754 755
	"application/mp4",
	"application/octet-stream",
	"application/ogg",
	"application/x-ms-wmz",
	"application/x-ms-wmd",
756
	"application/x-ogg",
757 758 759 760 761
	"application/x-shockwave-flash",
	"application/x-shorten",
	"audio/8svx",
	"audio/16sv",
	"audio/aac",
762
	"audio/aacp",
763
	"audio/ac3",
764
	"audio/aiff"
765 766 767
	"audio/amr",
	"audio/basic",
	"audio/flac",
768 769
	"audio/m4a",
	"audio/mp4",
770 771 772
	"audio/mpeg",
	"audio/musepack",
	"audio/ogg",
773
	"audio/opus",
774 775
	"audio/qcelp",
	"audio/vorbis",
776
	"audio/vorbis+ogg",
777 778 779 780 781 782 783 784 785 786 787 788
	"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",
789
	"audio/x-matroska",
790 791
	"audio/x-monkeys-audio",
	"audio/x-mpeg",
792 793
	"audio/x-ms-wma",
	"audio/x-ms-wax",
794
	"audio/x-musepack",
795 796 797
	"audio/x-ogg",
	"audio/x-vorbis",
	"audio/x-vorbis+ogg",
798 799 800 801
	"audio/x-pn-realaudio",
	"audio/x-pn-multirate-realaudio",
	"audio/x-speex",
	"audio/x-tta"
802
	"audio/x-voc",
803 804 805 806 807 808 809 810
	"audio/x-wav",
	"audio/x-wma",
	"audio/x-wv",
	"video/anim",
	"video/quicktime",
	"video/msvideo",
	"video/ogg",
	"video/theora",
811
	"video/webm",
812 813 814 815 816 817 818
	"video/x-dv",
	"video/x-flv",
	"video/x-matroska",
	"video/x-mjpeg",
	"video/x-mpeg",
	"video/x-ms-asf",
	"video/x-msvideo",
819 820 821 822
	"video/x-ms-wmv",
	"video/x-ms-wvx",
	"video/x-ms-wm",
	"video/x-ms-wmx",
823 824 825 826 827 828
	"video/x-nut",
	"video/x-pva",
	"video/x-theora",
	"video/x-vid",
	"video/x-wmv",
	"video/x-xvid",
829 830 831 832 833 834

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

835
	nullptr
836 837
};

838
const struct DecoderPlugin ffmpeg_decoder_plugin = {
839 840 841 842 843 844 845 846 847 848
	"ffmpeg",
	ffmpeg_init,
	nullptr,
	ffmpeg_decode,
	nullptr,
	nullptr,
	ffmpeg_scan_stream,
	nullptr,
	ffmpeg_suffixes,
	ffmpeg_mime_types
849
};