FfmpegDecoderPlugin.cxx 16.2 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "DecoderAPI.hxx"
26
#include "FfmpegMetaData.hxx"
27
#include "tag/TagHandler.hxx"
28
#include "InputStream.hxx"
29
#include "CheckAudioFormat.hxx"
30
#include "util/Error.hxx"
31 32
#include "util/Domain.hxx"
#include "LogV.hxx"
33 34

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

44 45 46
#include <assert.h>
#include <string.h>

47
static constexpr Domain ffmpeg_domain("ffmpeg");
48

49 50 51 52 53
/* suppress the ffmpeg compatibility macro */
#ifdef SampleFormat
#undef SampleFormat
#endif

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

60 61
	if (level <= AV_LOG_WARNING)
		return LogLevel::WARNING;
62 63

	if (level <= AV_LOG_INFO)
64
		return LogLevel::INFO;
65

66
	return LogLevel::DEBUG;
67 68 69
}

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

75
	if (ptr != nullptr)
76 77
		cls = *(const AVClass *const*)ptr;

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

87
struct AvioStream {
88
	Decoder *const decoder;
89
	InputStream &input;
90

91
	AVIOContext *io;
92

93
	unsigned char buffer[8192];
94

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

	~AvioStream() {
		if (io != nullptr)
			av_free(io);
	}

	bool Open();
104
};
105

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

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

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

	if (whence == AVSEEK_SIZE)
121
		return stream->input.size;
122

123
	Error error;
124
	if (!stream->input.LockSeek(pos, whence, error))
125 126
		return -1;

127
	return stream->input.offset;
128 129
}

130 131
bool
AvioStream::Open()
132
{
133 134 135
	io = avio_alloc_context(buffer, sizeof(buffer),
				false, this,
				mpd_ffmpeg_stream_read, nullptr,
136
				input.seekable
137 138
				? mpd_ffmpeg_stream_seek : nullptr);
	return io != nullptr;
139 140
}

141 142 143 144 145 146 147 148 149 150 151
/**
 * 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();
152
	if (context == nullptr)
153 154 155 156
		return AVERROR(ENOMEM);

	context->pb = pb;
	*ic_ptr = context;
157
	return avformat_open_input(ic_ptr, filename, fmt, nullptr);
158 159
}

160
static bool
161
ffmpeg_init(gcc_unused const config_param &param)
162
{
163 164
	av_log_set_callback(mpd_ffmpeg_log_callback);

165
	av_register_all();
166
	return true;
167 168
}

169 170 171 172 173
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 ==
174
		    AVMEDIA_TYPE_AUDIO)
175 176 177 178 179
			return i;

	return -1;
}

180
gcc_const
181 182 183 184 185
static double
time_from_ffmpeg(int64_t t, const AVRational time_base)
{
	assert(t != (int64_t)AV_NOPTS_VALUE);

186 187
	return (double)av_rescale_q(t, time_base, (AVRational){1, 1024})
		/ (double)1024;
188 189
}

190
gcc_const
191 192 193 194 195 196 197
static int64_t
time_to_ffmpeg(double t, const AVRational time_base)
{
	return av_rescale_q((int64_t)(t * 1024), (AVRational){1, 1024},
			    time_base);
}

198
static void
199 200 201
copy_interleave_frame2(uint8_t *dest, uint8_t **src,
		       unsigned nframes, unsigned nchannels,
		       unsigned sample_size)
202
{
203 204 205 206 207 208
	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;
		}
209 210 211
	}
}

212 213 214 215 216 217
/**
 * Copy PCM data from a AVFrame to an interleaved buffer.
 */
static int
copy_interleave_frame(const AVCodecContext *codec_context,
		      const AVFrame *frame,
218 219
		      uint8_t **output_buffer,
		      uint8_t **global_buffer, int *global_buffer_size)
220 221 222 223 224 225 226 227 228
{
	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);
	if (av_sample_fmt_is_planar(codec_context->sample_fmt) &&
	    codec_context->channels > 1) {
229 230 231 232 233 234 235 236 237 238 239 240
		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,
241 242 243
				       frame->nb_samples,
				       codec_context->channels,
				       av_get_bytes_per_sample(codec_context->sample_fmt));
244
	} else {
245
		*output_buffer = frame->extended_data[0];
246 247 248 249 250
	}

	return data_size;
}

251
static DecoderCommand
252
ffmpeg_send_packet(Decoder &decoder, InputStream &is,
253
		   const AVPacket *packet,
254
		   AVCodecContext *codec_context,
255
		   const AVRational *time_base,
256 257
		   AVFrame *frame,
		   uint8_t **buffer, int *buffer_size)
258
{
259
	if (packet->pts >= 0 && packet->pts != (int64_t)AV_NOPTS_VALUE)
260
		decoder_timestamp(decoder,
261
				  time_from_ffmpeg(packet->pts, *time_base));
262

263
	AVPacket packet2 = *packet;
264

265
	uint8_t *output_buffer;
266

267 268
	DecoderCommand cmd = DecoderCommand::NONE;
	while (packet2.size > 0 && cmd == DecoderCommand::NONE) {
269
		int audio_size = 0;
270 271
		int got_frame = 0;
		int len = avcodec_decode_audio4(codec_context,
272
						frame, &got_frame,
273 274 275
						&packet2);
		if (len >= 0 && got_frame) {
			audio_size = copy_interleave_frame(codec_context,
276
							   frame,
277 278
							   &output_buffer,
							   buffer, buffer_size);
279 280
			if (audio_size < 0)
				len = audio_size;
281
		}
282 283 284

		if (len < 0) {
			/* if error, we skip the frame */
285 286
			LogDefault(ffmpeg_domain,
				   "decoding failed, frame skipped");
287 288 289
			break;
		}

290 291
		packet2.data += len;
		packet2.size -= len;
292

293
		if (audio_size <= 0)
294
			continue;
295

296
		cmd = decoder_data(decoder, is,
297
				   output_buffer, audio_size,
298
				   codec_context->bit_rate / 1000);
299
	}
300
	return cmd;
301 302
}

303
gcc_const
304
static SampleFormat
305
ffmpeg_sample_format(enum AVSampleFormat sample_fmt)
306
{
307
	switch (sample_fmt) {
308
	case AV_SAMPLE_FMT_S16:
309
	case AV_SAMPLE_FMT_S16P:
310
		return SampleFormat::S16;
311

312
	case AV_SAMPLE_FMT_S32:
313
	case AV_SAMPLE_FMT_S32P:
314
		return SampleFormat::S32;
315

316
	case AV_SAMPLE_FMT_FLTP:
317
		return SampleFormat::FLOAT;
318

319
	default:
320 321 322 323 324 325
		break;
	}

	char buffer[64];
	const char *name = av_get_sample_fmt_string(buffer, sizeof(buffer),
						    sample_fmt);
326
	if (name != nullptr)
327 328 329
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %s (%d)",
			    name, sample_fmt);
330
	else
331 332 333
		FormatError(ffmpeg_domain,
			    "Unsupported libavcodec SampleFormat value: %d",
			    sample_fmt);
334
	return SampleFormat::UNDEFINED;
335 336
}

337
static AVInputFormat *
338
ffmpeg_probe(Decoder *decoder, InputStream &is)
339 340 341 342 343 344
{
	enum {
		BUFFER_SIZE = 16384,
		PADDING = 16,
	};

345 346
	Error error;

347
	unsigned char buffer[BUFFER_SIZE];
348
	size_t nbytes = decoder_read(decoder, is, buffer, BUFFER_SIZE);
349
	if (nbytes <= PADDING || !is.LockRewind(error))
350
		return nullptr;
351 352 353 354 355 356 357

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

358 359 360
	AVProbeData avpd;
	avpd.buf = buffer;
	avpd.buf_size = nbytes;
361
	avpd.filename = is.uri.c_str();
362

363
	return av_probe_input_format(&avpd, true);
364 365
}

366
static void
367
ffmpeg_decode(Decoder &decoder, InputStream &input)
368
{
369
	AVInputFormat *input_format = ffmpeg_probe(&decoder, input);
370
	if (input_format == nullptr)
371 372
		return;

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

376
	AvioStream stream(&decoder, input);
377
	if (!stream.Open()) {
378
		LogError(ffmpeg_domain, "Failed to open stream");
379 380 381
		return;
	}

382
	//ffmpeg works with ours "fileops" helper
383
	AVFormatContext *format_context = nullptr;
384
	if (mpd_ffmpeg_open_input(&format_context, stream.io,
385
				  input.uri.c_str(),
386
				  input_format) != 0) {
387
		LogError(ffmpeg_domain, "Open failed");
388 389 390
		return;
	}

391
	const int find_result =
392
		avformat_find_stream_info(format_context, nullptr);
393
	if (find_result < 0) {
394
		LogError(ffmpeg_domain, "Couldn't find stream info");
395
		avformat_close_input(&format_context);
396 397
		return;
	}
398

399
	int audio_stream = ffmpeg_find_audio_stream(format_context);
400
	if (audio_stream == -1) {
401
		LogError(ffmpeg_domain, "No audio stream inside");
402
		avformat_close_input(&format_context);
403 404
		return;
	}
405

406 407 408
	AVStream *av_stream = format_context->streams[audio_stream];

	AVCodecContext *codec_context = av_stream->codec;
409
	if (codec_context->codec_name[0] != 0)
410 411
		FormatDebug(ffmpeg_domain, "codec '%s'",
			    codec_context->codec_name);
412

413
	AVCodec *codec = avcodec_find_decoder(codec_context->codec_id);
414 415

	if (!codec) {
416
		LogError(ffmpeg_domain, "Unsupported audio codec");
417
		avformat_close_input(&format_context);
418 419 420
		return;
	}

421
	const SampleFormat sample_format =
422
		ffmpeg_sample_format(codec_context->sample_fmt);
423
	if (sample_format == SampleFormat::UNDEFINED)
424 425
		return;

426
	Error error;
427 428
	AudioFormat audio_format;
	if (!audio_format_init_checked(audio_format,
429
				       codec_context->sample_rate,
430
				       sample_format,
431
				       codec_context->channels, error)) {
432
		LogError(error);
433
		avformat_close_input(&format_context);
434 435 436 437 438 439 440 441
		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() */

442
	const int open_result = avcodec_open2(codec_context, codec, nullptr);
443
	if (open_result < 0) {
444
		LogError(ffmpeg_domain, "Could not open codec");
445
		avformat_close_input(&format_context);
446
		return;
447 448
	}

449 450 451
	int total_time = format_context->duration != (int64_t)AV_NOPTS_VALUE
		? format_context->duration / AV_TIME_BASE
		: 0;
452

453
	decoder_initialized(decoder, audio_format,
454
			    input.seekable, total_time);
455

456 457
	AVFrame *frame = avcodec_alloc_frame();
	if (!frame) {
458
		LogError(ffmpeg_domain, "Could not allocate frame");
459 460 461 462
		avformat_close_input(&format_context);
		return;
	}

463
	uint8_t *interleaved_buffer = nullptr;
464 465
	int interleaved_buffer_size = 0;

466
	DecoderCommand cmd;
467
	do {
468
		AVPacket packet;
Max Kellermann's avatar
Max Kellermann committed
469
		if (av_read_frame(format_context, &packet) < 0)
470 471 472
			/* end of file */
			break;

473 474
		if (packet.stream_index == audio_stream)
			cmd = ffmpeg_send_packet(decoder, input,
Max Kellermann's avatar
Max Kellermann committed
475
						 &packet, codec_context,
476
						 &av_stream->time_base,
477 478
						 frame,
						 &interleaved_buffer, &interleaved_buffer_size);
479 480 481 482
		else
			cmd = decoder_get_command(decoder);

		av_free_packet(&packet);
483

484
		if (cmd == DecoderCommand::SEEK) {
485
			int64_t where =
486 487
				time_to_ffmpeg(decoder_seek_where(decoder),
					       av_stream->time_base);
488

489
			if (av_seek_frame(format_context, audio_stream, where,
490
					  AV_TIME_BASE) < 0)
491
				decoder_seek_error(decoder);
492 493
			else {
				avcodec_flush_buffers(codec_context);
494
				decoder_command_finished(decoder);
495
			}
496
		}
497
	} while (cmd != DecoderCommand::STOP);
498

499 500 501 502 503
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
	avcodec_free_frame(&frame);
#else
	av_freep(&frame);
#endif
504
	av_freep(&interleaved_buffer);
505

506
	avcodec_close(codec_context);
507
	avformat_close_input(&format_context);
508 509
}

510
//no tag reading in ffmpeg, check if playable
511
static bool
512
ffmpeg_scan_stream(InputStream &is,
513
		   const struct tag_handler *handler, void *handler_ctx)
514
{
515 516
	AVInputFormat *input_format = ffmpeg_probe(nullptr, is);
	if (input_format == nullptr)
517
		return false;
518

519 520
	AvioStream stream(nullptr, is);
	if (!stream.Open())
521
		return false;
522

523
	AVFormatContext *f = nullptr;
524
	if (mpd_ffmpeg_open_input(&f, stream.io, is.uri.c_str(),
525
				  input_format) != 0)
526
		return false;
527

528
	const int find_result =
529
		avformat_find_stream_info(f, nullptr);
530 531
	if (find_result < 0) {
		avformat_close_input(&f);
532
		return false;
533 534
	}

535 536 537
	if (f->duration != (int64_t)AV_NOPTS_VALUE)
		tag_handler_invoke_duration(handler, handler_ctx,
					    f->duration / AV_TIME_BASE);
538

539
	ffmpeg_scan_dictionary(f->metadata, handler, handler_ctx);
540 541
	int idx = ffmpeg_find_audio_stream(f);
	if (idx >= 0)
542 543
		ffmpeg_scan_dictionary(f->streams[idx]->metadata,
				       handler, handler_ctx);
544

545
	avformat_close_input(&f);
546
	return true;
547 548 549
}

/**
550 551 552 553
 * 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.
554
 */
Max Kellermann's avatar
Max Kellermann committed
555
static const char *const ffmpeg_suffixes[] = {
556 557 558 559 560
	"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",
561 562 563
	"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
	"m4a", "m4b", "m4v",
	"mad",
564 565 566 567 568 569 570
	"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",
	"ogx", "oma", "ogg", "omg", "psp", "pva", "qcp", "qt", "r3d", "ra",
	"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",
571 572
	"vp6", "vmd", "wav", "webm", "wma", "wmv", "wsaud", "wsvga", "wv",
	"wve",
573
	nullptr
574 575
};

Max Kellermann's avatar
Max Kellermann committed
576
static const char *const ffmpeg_mime_types[] = {
577
	"application/flv",
578
	"application/m4a",
579 580 581 582 583
	"application/mp4",
	"application/octet-stream",
	"application/ogg",
	"application/x-ms-wmz",
	"application/x-ms-wmd",
584
	"application/x-ogg",
585 586 587 588 589 590
	"application/x-shockwave-flash",
	"application/x-shorten",
	"audio/8svx",
	"audio/16sv",
	"audio/aac",
	"audio/ac3",
591
	"audio/aiff"
592 593 594
	"audio/amr",
	"audio/basic",
	"audio/flac",
595 596
	"audio/m4a",
	"audio/mp4",
597 598 599 600 601
	"audio/mpeg",
	"audio/musepack",
	"audio/ogg",
	"audio/qcelp",
	"audio/vorbis",
602
	"audio/vorbis+ogg",
603 604 605 606 607 608 609 610 611 612 613 614
	"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",
615
	"audio/x-matroska",
616 617
	"audio/x-monkeys-audio",
	"audio/x-mpeg",
618 619
	"audio/x-ms-wma",
	"audio/x-ms-wax",
620
	"audio/x-musepack",
621 622 623
	"audio/x-ogg",
	"audio/x-vorbis",
	"audio/x-vorbis+ogg",
624 625 626 627
	"audio/x-pn-realaudio",
	"audio/x-pn-multirate-realaudio",
	"audio/x-speex",
	"audio/x-tta"
628
	"audio/x-voc",
629 630 631 632 633 634 635 636
	"audio/x-wav",
	"audio/x-wma",
	"audio/x-wv",
	"video/anim",
	"video/quicktime",
	"video/msvideo",
	"video/ogg",
	"video/theora",
637
	"video/webm",
638 639 640 641 642 643 644
	"video/x-dv",
	"video/x-flv",
	"video/x-matroska",
	"video/x-mjpeg",
	"video/x-mpeg",
	"video/x-ms-asf",
	"video/x-msvideo",
645 646 647 648
	"video/x-ms-wmv",
	"video/x-ms-wvx",
	"video/x-ms-wm",
	"video/x-ms-wmx",
649 650 651 652 653 654
	"video/x-nut",
	"video/x-pva",
	"video/x-theora",
	"video/x-vid",
	"video/x-wmv",
	"video/x-xvid",
655 656 657 658 659 660

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

661
	nullptr
662 663
};

664
const struct DecoderPlugin ffmpeg_decoder_plugin = {
665 666 667 668 669 670 671 672 673 674
	"ffmpeg",
	ffmpeg_init,
	nullptr,
	ffmpeg_decode,
	nullptr,
	nullptr,
	ffmpeg_scan_stream,
	nullptr,
	ffmpeg_suffixes,
	ffmpeg_mime_types
675
};