2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-17 22:27:33 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-17 22:27:33 +02:00
|
|
|
*/
|
|
|
|
|
2013-01-26 00:37:04 +01:00
|
|
|
/* necessary because libavutil/common.h uses UINT64_C */
|
|
|
|
#define __STDC_CONSTANT_MACROS
|
|
|
|
|
2014-12-19 09:41:21 +01:00
|
|
|
#include "lib/ffmpeg/Time.hxx"
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2013-01-26 00:37:04 +01:00
|
|
|
#include "FfmpegDecoderPlugin.hxx"
|
2014-08-18 10:12:37 +02:00
|
|
|
#include "lib/ffmpeg/Domain.hxx"
|
2014-12-18 23:39:56 +01:00
|
|
|
#include "lib/ffmpeg/Error.hxx"
|
2014-12-18 22:07:38 +01:00
|
|
|
#include "lib/ffmpeg/LogError.hxx"
|
2014-12-21 20:51:41 +01:00
|
|
|
#include "lib/ffmpeg/Init.hxx"
|
2014-12-18 21:28:50 +01:00
|
|
|
#include "lib/ffmpeg/Buffer.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2013-01-26 00:37:04 +01:00
|
|
|
#include "FfmpegMetaData.hxx"
|
2014-12-19 10:35:10 +01:00
|
|
|
#include "FfmpegIo.hxx"
|
2015-06-22 14:28:23 +02:00
|
|
|
#include "pcm/Interleave.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Builder.hxx"
|
|
|
|
#include "tag/Handler.hxx"
|
2014-12-10 13:05:16 +01:00
|
|
|
#include "tag/ReplayGain.hxx"
|
|
|
|
#include "tag/MixRamp.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2016-07-29 09:08:14 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2014-12-18 23:41:08 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "LogV.hxx"
|
2013-01-26 00:37:04 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2008-10-21 08:34:19 +02:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavformat/avio.h>
|
2012-01-12 18:45:18 +01:00
|
|
|
#include <libavutil/avutil.h>
|
2014-01-15 11:13:50 +01:00
|
|
|
|
|
|
|
#if LIBAVUTIL_VERSION_MAJOR >= 53
|
|
|
|
#include <libavutil/frame.h>
|
|
|
|
#endif
|
2013-01-26 00:37:04 +01:00
|
|
|
}
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2013-10-23 21:22:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-01-10 23:05:04 +01:00
|
|
|
/**
|
|
|
|
* Muxer options to be passed to avformat_open_input().
|
|
|
|
*/
|
|
|
|
static AVDictionary *avformat_options = nullptr;
|
|
|
|
|
2014-12-22 21:58:01 +01:00
|
|
|
static AVFormatContext *
|
|
|
|
FfmpegOpenInput(AVIOContext *pb,
|
2014-12-23 10:21:34 +01:00
|
|
|
const char *filename,
|
2016-09-16 18:38:16 +02:00
|
|
|
AVInputFormat *fmt)
|
2011-07-18 23:31:47 +02:00
|
|
|
{
|
|
|
|
AVFormatContext *context = avformat_alloc_context();
|
2016-09-16 18:38:16 +02:00
|
|
|
if (context == nullptr)
|
|
|
|
throw std::runtime_error("avformat_alloc_context() failed");
|
2011-07-18 23:31:47 +02:00
|
|
|
|
|
|
|
context->pb = pb;
|
2014-12-22 21:58:01 +01:00
|
|
|
|
2017-01-10 23:05:04 +01:00
|
|
|
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);
|
2016-12-13 10:59:23 +01:00
|
|
|
if (err < 0)
|
2016-09-16 18:38:16 +02:00
|
|
|
throw MakeFfmpegError(err, "avformat_open_input() failed");
|
2016-07-29 19:16:47 +02:00
|
|
|
|
2014-12-22 21:58:01 +01:00
|
|
|
return context;
|
2011-07-18 23:31:47 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 18:34:14 +01:00
|
|
|
static bool
|
2017-01-10 23:05:04 +01:00
|
|
|
ffmpeg_init(const ConfigBlock &block)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2014-12-21 20:51:41 +01:00
|
|
|
FfmpegInit();
|
2017-01-10 23:05:04 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:38:54 +01:00
|
|
|
return true;
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 23:05:04 +01:00
|
|
|
static void
|
|
|
|
ffmpeg_finish()
|
|
|
|
{
|
|
|
|
av_dict_free(&avformat_options);
|
|
|
|
}
|
|
|
|
|
2016-08-02 19:15:23 +02:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
|
2016-07-27 15:06:42 +02:00
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static const AVCodecParameters &
|
2017-05-08 14:44:49 +02:00
|
|
|
GetCodecParameters(const AVStream &stream) noexcept
|
2016-07-27 15:06:42 +02:00
|
|
|
{
|
|
|
|
return *stream.codecpar;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static AVSampleFormat
|
2017-05-08 14:44:49 +02:00
|
|
|
GetSampleFormat(const AVCodecParameters &codec_params) noexcept
|
2016-07-27 15:06:42 +02:00
|
|
|
{
|
|
|
|
return AVSampleFormat(codec_params.format);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-07-27 15:22:40 +02:00
|
|
|
gcc_pure
|
|
|
|
static const AVCodecContext &
|
2017-05-08 14:44:49 +02:00
|
|
|
GetCodecParameters(const AVStream &stream) noexcept
|
2016-07-27 15:22:40 +02:00
|
|
|
{
|
|
|
|
return *stream.codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static AVSampleFormat
|
2017-05-08 14:44:49 +02:00
|
|
|
GetSampleFormat(const AVCodecContext &codec_context) noexcept
|
2016-07-27 15:22:40 +02:00
|
|
|
{
|
|
|
|
return codec_context.sample_fmt;
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:06:42 +02:00
|
|
|
#endif
|
|
|
|
|
2016-07-27 14:58:41 +02:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
IsAudio(const AVStream &stream) noexcept
|
2016-07-27 14:58:41 +02:00
|
|
|
{
|
2016-07-27 15:22:40 +02:00
|
|
|
return GetCodecParameters(stream).codec_type == AVMEDIA_TYPE_AUDIO;
|
2016-07-27 14:58:41 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 20:17:15 +01:00
|
|
|
gcc_pure
|
2008-11-04 16:55:12 +01:00
|
|
|
static int
|
2017-05-08 14:44:49 +02:00
|
|
|
ffmpeg_find_audio_stream(const AVFormatContext &format_context) noexcept
|
2008-11-04 16:55:12 +01:00
|
|
|
{
|
2014-12-18 23:08:08 +01:00
|
|
|
for (unsigned i = 0; i < format_context.nb_streams; ++i)
|
2016-07-27 14:58:41 +02:00
|
|
|
if (IsAudio(*format_context.streams[i]))
|
2008-11-04 16:55:12 +01:00
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-18 08:19:05 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-12-18 20:17:15 +01:00
|
|
|
static constexpr int64_t
|
2014-03-18 08:19:05 +01:00
|
|
|
start_time_fallback(const AVStream &stream)
|
|
|
|
{
|
2014-12-19 09:41:21 +01:00
|
|
|
return FfmpegTimestampFallback(stream.start_time, 0);
|
2014-03-18 08:19:05 +01:00
|
|
|
}
|
|
|
|
|
2012-01-04 22:10:38 +01:00
|
|
|
/**
|
2014-12-19 06:41:50 +01:00
|
|
|
* Copy PCM data from a non-empty AVFrame to an interleaved buffer.
|
2016-10-31 13:03:02 +01:00
|
|
|
*
|
|
|
|
* Throws #std::exception on error.
|
2012-01-04 22:10:38 +01:00
|
|
|
*/
|
2014-12-18 23:41:08 +01:00
|
|
|
static ConstBuffer<void>
|
2014-12-18 23:08:08 +01:00
|
|
|
copy_interleave_frame(const AVCodecContext &codec_context,
|
|
|
|
const AVFrame &frame,
|
2016-10-31 13:03:02 +01:00
|
|
|
FfmpegBuffer &global_buffer)
|
2012-01-04 22:10:38 +01:00
|
|
|
{
|
2014-12-18 23:39:56 +01:00
|
|
|
assert(frame.nb_samples > 0);
|
|
|
|
|
2012-01-04 22:10:38 +01:00
|
|
|
int plane_size;
|
|
|
|
const int data_size =
|
|
|
|
av_samples_get_buffer_size(&plane_size,
|
2014-12-18 23:08:08 +01:00
|
|
|
codec_context.channels,
|
|
|
|
frame.nb_samples,
|
|
|
|
codec_context.sample_fmt, 1);
|
2014-12-18 23:39:56 +01:00
|
|
|
assert(data_size != 0);
|
2016-10-31 13:03:02 +01:00
|
|
|
if (data_size < 0)
|
|
|
|
throw MakeFfmpegError(data_size);
|
2014-01-14 23:16:07 +01:00
|
|
|
|
2014-12-18 23:41:08 +01:00
|
|
|
void *output_buffer;
|
2014-12-18 23:08:08 +01:00
|
|
|
if (av_sample_fmt_is_planar(codec_context.sample_fmt) &&
|
|
|
|
codec_context.channels > 1) {
|
2014-12-18 23:41:08 +01:00
|
|
|
output_buffer = global_buffer.GetT<uint8_t>(data_size);
|
2016-10-31 13:03:02 +01:00
|
|
|
if (output_buffer == nullptr)
|
2014-12-18 21:28:50 +01:00
|
|
|
/* Not enough memory - shouldn't happen */
|
2016-10-31 13:03:02 +01:00
|
|
|
throw std::bad_alloc();
|
2013-08-07 15:04:26 +02:00
|
|
|
|
2015-06-22 14:28:23 +02:00
|
|
|
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));
|
2012-01-04 22:10:38 +01:00
|
|
|
} else {
|
2014-12-18 23:41:08 +01:00
|
|
|
output_buffer = frame.extended_data[0];
|
2012-01-04 22:10:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 23:41:08 +01:00
|
|
|
return { output_buffer, (size_t)data_size };
|
2012-01-04 22:10:38 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 18:56:29 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-05-08 14:44:49 +02:00
|
|
|
StreamRelativePts(const AVPacket &packet, const AVStream &stream) noexcept
|
2015-06-19 18:56:29 +02:00
|
|
|
{
|
|
|
|
auto pts = packet.pts;
|
|
|
|
if (pts < 0 || pts == int64_t(AV_NOPTS_VALUE))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
auto start = start_time_fallback(stream);
|
|
|
|
return pts - start;
|
|
|
|
}
|
|
|
|
|
2015-06-19 18:02:10 +02:00
|
|
|
/**
|
|
|
|
* 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,
|
2017-05-08 14:44:49 +02:00
|
|
|
const AVCodecContext &codec_context) noexcept
|
2015-06-19 18:02:10 +02:00
|
|
|
{
|
|
|
|
return av_rescale_q(pts, stream.time_base, codec_context.time_base);
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:10:39 +02:00
|
|
|
/**
|
2016-11-18 08:27:30 +01:00
|
|
|
* Invoke DecoderClient::SubmitData() with the contents of an
|
|
|
|
* #AVFrame.
|
2016-07-27 17:10:39 +02:00
|
|
|
*/
|
|
|
|
static DecoderCommand
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegSendFrame(DecoderClient &client, InputStream &is,
|
2016-07-27 17:10:39 +02:00
|
|
|
AVCodecContext &codec_context,
|
|
|
|
const AVFrame &frame,
|
|
|
|
size_t &skip_bytes,
|
|
|
|
FfmpegBuffer &buffer)
|
|
|
|
{
|
2016-10-31 13:03:02 +01:00
|
|
|
ConstBuffer<void> output_buffer;
|
|
|
|
|
|
|
|
try {
|
|
|
|
output_buffer = copy_interleave_frame(codec_context, frame,
|
|
|
|
buffer);
|
2017-11-12 18:52:11 +01:00
|
|
|
} catch (const std::exception &e) {
|
2016-07-27 17:10:39 +02:00
|
|
|
/* this must be a serious error, e.g. OOM */
|
2016-10-31 13:03:02 +01:00
|
|
|
LogError(e);
|
2016-07-27 17:10:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
return client.SubmitData(is,
|
|
|
|
output_buffer.data, output_buffer.size,
|
|
|
|
codec_context.bit_rate / 1000);
|
2016-07-27 17:10:39 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 15:10:27 +02:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)
|
|
|
|
|
|
|
|
static DecoderCommand
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegReceiveFrames(DecoderClient &client, InputStream &is,
|
2016-07-27 15:10:27 +02:00
|
|
|
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:
|
2016-11-18 07:13:35 +01:00
|
|
|
cmd = FfmpegSendFrame(client, is, codec_context,
|
2016-07-27 15:10:27 +02:00
|
|
|
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
|
|
|
|
|
2014-12-19 06:41:50 +01:00
|
|
|
/**
|
|
|
|
* Decode an #AVPacket and send the resulting PCM data to the decoder
|
|
|
|
* API.
|
2015-06-21 16:01:44 +02:00
|
|
|
*
|
2015-06-19 18:02:10 +02:00
|
|
|
* @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
|
2014-12-19 06:41:50 +01:00
|
|
|
*/
|
2013-09-27 12:11:37 +02:00
|
|
|
static DecoderCommand
|
2016-11-18 07:13:35 +01:00
|
|
|
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
|
2016-04-13 09:04:51 +02:00
|
|
|
AVPacket &&packet,
|
2014-12-18 23:08:08 +01:00
|
|
|
AVCodecContext &codec_context,
|
|
|
|
const AVStream &stream,
|
|
|
|
AVFrame &frame,
|
2015-06-19 18:02:10 +02:00
|
|
|
uint64_t min_frame, size_t pcm_frame_size,
|
2014-12-18 21:28:50 +01:00
|
|
|
FfmpegBuffer &buffer)
|
2008-10-30 19:03:20 +01:00
|
|
|
{
|
2015-06-19 18:02:10 +02:00
|
|
|
size_t skip_bytes = 0;
|
|
|
|
|
2015-06-21 16:01:44 +02:00
|
|
|
const auto pts = StreamRelativePts(packet, stream);
|
2015-06-19 18:56:29 +02:00
|
|
|
if (pts >= 0) {
|
2015-06-19 18:02:10 +02:00
|
|
|
if (min_frame > 0) {
|
2015-06-21 16:01:44 +02:00
|
|
|
auto cur_frame = PtsToPcmFrame(pts, stream,
|
|
|
|
codec_context);
|
2015-06-19 18:02:10 +02:00
|
|
|
if (cur_frame < min_frame)
|
|
|
|
skip_bytes = pcm_frame_size * (min_frame - cur_frame);
|
|
|
|
} else
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitTimestamp(FfmpegTimeToDouble(pts,
|
|
|
|
stream.time_base));
|
2014-12-11 10:50:20 +01:00
|
|
|
}
|
2010-01-18 13:11:04 +01:00
|
|
|
|
2016-07-27 15:10:27 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
auto cmd = FfmpegReceiveFrames(client, is, codec_context,
|
2016-07-27 15:10:27 +02:00
|
|
|
frame,
|
|
|
|
skip_bytes, buffer, eof);
|
|
|
|
|
|
|
|
if (eof)
|
|
|
|
cmd = DecoderCommand::STOP;
|
|
|
|
#else
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd = DecoderCommand::NONE;
|
2014-12-18 23:24:11 +01:00
|
|
|
while (packet.size > 0 && cmd == DecoderCommand::NONE) {
|
2012-01-04 22:10:38 +01:00
|
|
|
int got_frame = 0;
|
2014-12-18 23:08:08 +01:00
|
|
|
int len = avcodec_decode_audio4(&codec_context,
|
|
|
|
&frame, &got_frame,
|
2014-12-18 23:24:11 +01:00
|
|
|
&packet);
|
2014-12-11 10:58:06 +01:00
|
|
|
if (len < 0) {
|
|
|
|
/* if error, we skip the frame */
|
2014-12-18 22:07:38 +01:00
|
|
|
LogFfmpegError(len, "decoding failed, frame skipped");
|
2014-12-11 10:58:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:24:11 +01:00
|
|
|
packet.data += len;
|
|
|
|
packet.size -= len;
|
2008-12-15 18:35:34 +01:00
|
|
|
|
2014-12-18 23:39:56 +01:00
|
|
|
if (!got_frame || frame.nb_samples <= 0)
|
2014-12-18 23:21:48 +01:00
|
|
|
continue;
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
cmd = FfmpegSendFrame(client, is, codec_context,
|
2016-07-27 17:10:39 +02:00
|
|
|
frame, skip_bytes,
|
|
|
|
buffer);
|
2008-10-30 19:03:20 +01:00
|
|
|
}
|
2016-07-27 15:10:27 +02:00
|
|
|
#endif
|
2016-07-27 17:10:39 +02:00
|
|
|
|
2008-12-15 18:35:34 +01:00
|
|
|
return cmd;
|
2008-10-30 19:03:20 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 19:41:05 +02:00
|
|
|
static DecoderCommand
|
2016-11-18 07:13:35 +01:00
|
|
|
ffmpeg_send_packet(DecoderClient &client, InputStream &is,
|
2016-07-28 19:41:05 +02:00
|
|
|
const AVPacket &packet,
|
|
|
|
AVCodecContext &codec_context,
|
|
|
|
const AVStream &stream,
|
2014-12-19 10:03:35 +01:00
|
|
|
AVFrame &frame,
|
2016-07-28 19:41:05 +02:00
|
|
|
uint64_t min_frame, size_t pcm_frame_size,
|
|
|
|
FfmpegBuffer &buffer)
|
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
return ffmpeg_send_packet(client, is,
|
2016-07-28 19:41:05 +02:00
|
|
|
/* copy the AVPacket, because FFmpeg
|
|
|
|
< 3.0 requires this */
|
|
|
|
AVPacket(packet),
|
|
|
|
codec_context, stream,
|
|
|
|
frame, min_frame, pcm_frame_size,
|
|
|
|
buffer);
|
|
|
|
}
|
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_const
|
2013-08-03 21:00:50 +02:00
|
|
|
static SampleFormat
|
2017-05-08 14:44:49 +02:00
|
|
|
ffmpeg_sample_format(enum AVSampleFormat sample_fmt) noexcept
|
2009-11-10 17:11:34 +01:00
|
|
|
{
|
2012-10-05 15:14:57 +02:00
|
|
|
switch (sample_fmt) {
|
2012-02-03 09:55:25 +01:00
|
|
|
case AV_SAMPLE_FMT_S16:
|
2012-10-05 15:27:04 +02:00
|
|
|
case AV_SAMPLE_FMT_S16P:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S16;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2012-02-03 09:55:25 +01:00
|
|
|
case AV_SAMPLE_FMT_S32:
|
2012-10-05 15:27:04 +02:00
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S32;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2014-12-23 20:51:08 +01:00
|
|
|
case AV_SAMPLE_FMT_FLT:
|
2013-01-16 18:02:58 +01:00
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::FLOAT;
|
2013-01-16 18:02:58 +01:00
|
|
|
|
2010-11-04 20:04:15 +01:00
|
|
|
default:
|
2012-10-05 14:55:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[64];
|
|
|
|
const char *name = av_get_sample_fmt_string(buffer, sizeof(buffer),
|
|
|
|
sample_fmt);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (name != nullptr)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(ffmpeg_domain,
|
|
|
|
"Unsupported libavcodec SampleFormat value: %s (%d)",
|
|
|
|
name, sample_fmt);
|
2012-10-05 14:55:19 +02:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(ffmpeg_domain,
|
|
|
|
"Unsupported libavcodec SampleFormat value: %d",
|
|
|
|
sample_fmt);
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::UNDEFINED;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
|
|
|
|
2010-06-30 20:32:29 +02:00
|
|
|
static AVInputFormat *
|
2016-11-18 07:13:35 +01:00
|
|
|
ffmpeg_probe(DecoderClient *client, InputStream &is)
|
2010-06-30 20:32:29 +02:00
|
|
|
{
|
2014-12-19 11:47:25 +01:00
|
|
|
constexpr size_t BUFFER_SIZE = 16384;
|
|
|
|
constexpr size_t PADDING = 16;
|
2010-06-30 20:32:29 +02:00
|
|
|
|
2013-10-19 17:15:17 +02:00
|
|
|
unsigned char buffer[BUFFER_SIZE];
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, buffer, BUFFER_SIZE);
|
2016-09-09 18:47:42 +02:00
|
|
|
if (nbytes <= PADDING)
|
2013-10-19 17:15:17 +02:00
|
|
|
return nullptr;
|
2010-06-30 20:32:29 +02:00
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
try {
|
|
|
|
is.LockRewind();
|
|
|
|
} catch (const std::runtime_error &) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-06-30 20:32:29 +02:00
|
|
|
/* 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;
|
|
|
|
|
2013-01-26 00:37:04 +01:00
|
|
|
AVProbeData avpd;
|
2014-09-09 19:07:46 +02:00
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
2013-01-26 00:37:04 +01:00
|
|
|
avpd.buf = buffer;
|
|
|
|
avpd.buf_size = nbytes;
|
2014-05-11 15:34:48 +02:00
|
|
|
avpd.filename = is.GetURI();
|
2010-06-30 20:32:29 +02:00
|
|
|
|
2014-09-07 22:05:33 +02:00
|
|
|
#ifdef AVPROBE_SCORE_MIME
|
2014-12-08 14:25:34 +01:00
|
|
|
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(56, 5, 1)
|
2014-09-07 22:05:33 +02:00
|
|
|
/* 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());
|
2014-12-08 14:25:34 +01:00
|
|
|
#else
|
|
|
|
/* API problem fixed in FFmpeg 2.5 */
|
|
|
|
avpd.mime_type = is.GetMimeType();
|
|
|
|
#endif
|
2014-09-07 22:05:33 +02:00
|
|
|
#endif
|
|
|
|
|
2013-10-19 17:15:17 +02:00
|
|
|
return av_probe_input_format(&avpd, true);
|
2010-06-30 20:32:29 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 13:05:16 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-12-19 10:20:45 +01:00
|
|
|
assert(audio_stream >= 0);
|
2014-12-10 13:05:16 +01:00
|
|
|
|
2014-12-19 10:20:45 +01:00
|
|
|
FfmpegParseMetaData(*format_context.metadata, rg, mr);
|
|
|
|
FfmpegParseMetaData(*format_context.streams[audio_stream],
|
2014-12-10 13:05:16 +01:00
|
|
|
rg, mr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegParseMetaData(DecoderClient &client,
|
2014-12-10 13:05:16 +01:00
|
|
|
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())
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitReplayGain(&rg);
|
2014-12-10 13:05:16 +01:00
|
|
|
|
|
|
|
if (mr.IsDefined())
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitMixRamp(std::move(mr));
|
2014-12-10 13:05:16 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 10:08:08 +01:00
|
|
|
static void
|
|
|
|
FfmpegScanMetadata(const AVStream &stream,
|
2016-02-22 17:38:06 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2014-12-11 10:08:08 +01:00
|
|
|
{
|
2016-02-23 10:10:13 +01:00
|
|
|
FfmpegScanDictionary(stream.metadata, handler, handler_ctx);
|
2014-12-11 10:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FfmpegScanMetadata(const AVFormatContext &format_context, int audio_stream,
|
2016-02-22 17:38:06 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2014-12-11 10:08:08 +01:00
|
|
|
{
|
2014-12-19 10:20:25 +01:00
|
|
|
assert(audio_stream >= 0);
|
|
|
|
|
2016-02-23 10:10:13 +01:00
|
|
|
FfmpegScanDictionary(format_context.metadata, handler, handler_ctx);
|
2014-12-19 10:20:25 +01:00
|
|
|
FfmpegScanMetadata(*format_context.streams[audio_stream],
|
|
|
|
handler, handler_ctx);
|
2014-12-11 10:08:08 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 10:39:49 +01:00
|
|
|
#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
|
2016-11-18 08:27:30 +01:00
|
|
|
* DecoderClient::SubmitTag().
|
2014-12-11 10:39:49 +01:00
|
|
|
*/
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegCheckTag(DecoderClient &client, InputStream &is,
|
2014-12-11 10:39:49 +01:00
|
|
|
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);
|
2017-11-10 19:24:33 +01:00
|
|
|
if (!tag.empty())
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitTag(is, tag.Commit());
|
2014-12-11 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-01-18 10:59:11 +01:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegDecode(DecoderClient &client, InputStream &input,
|
2014-12-19 09:57:29 +01:00
|
|
|
AVFormatContext &format_context)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2012-01-05 00:02:46 +01:00
|
|
|
const int find_result =
|
2014-12-19 09:57:29 +01:00
|
|
|
avformat_find_stream_info(&format_context, nullptr);
|
2012-01-05 00:02:46 +01:00
|
|
|
if (find_result < 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(ffmpeg_domain, "Couldn't find stream info");
|
2010-01-18 10:59:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2014-12-19 09:57:29 +01:00
|
|
|
int audio_stream = ffmpeg_find_audio_stream(format_context);
|
2010-01-18 10:59:11 +01:00
|
|
|
if (audio_stream == -1) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(ffmpeg_domain, "No audio stream inside");
|
2010-01-18 10:59:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2014-12-19 10:03:35 +01:00
|
|
|
AVStream &av_stream = *format_context.streams[audio_stream];
|
2011-09-15 21:14:53 +02:00
|
|
|
|
2016-07-28 20:33:24 +02:00
|
|
|
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
|
2014-12-19 10:03:35 +01:00
|
|
|
AVCodecContext *codec_context = av_stream.codec;
|
2016-07-28 20:33:24 +02:00
|
|
|
#endif
|
|
|
|
|
2016-07-27 15:22:40 +02:00
|
|
|
const auto &codec_params = GetCodecParameters(av_stream);
|
2014-08-13 18:40:39 +02:00
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0)
|
|
|
|
const AVCodecDescriptor *codec_descriptor =
|
2016-07-27 15:22:40 +02:00
|
|
|
avcodec_descriptor_get(codec_params.codec_id);
|
2014-08-13 18:40:39 +02:00
|
|
|
if (codec_descriptor != nullptr)
|
|
|
|
FormatDebug(ffmpeg_domain, "codec '%s'",
|
|
|
|
codec_descriptor->name);
|
|
|
|
#else
|
2010-01-18 10:59:11 +01:00
|
|
|
if (codec_context->codec_name[0] != 0)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(ffmpeg_domain, "codec '%s'",
|
|
|
|
codec_context->codec_name);
|
2014-08-13 18:40:39 +02:00
|
|
|
#endif
|
2010-01-18 10:59:11 +01:00
|
|
|
|
2016-07-27 15:22:40 +02:00
|
|
|
AVCodec *codec = avcodec_find_decoder(codec_params.codec_id);
|
2010-01-18 10:59:11 +01:00
|
|
|
|
|
|
|
if (!codec) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(ffmpeg_domain, "Unsupported audio codec");
|
2010-01-18 10:59:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:33:24 +02:00
|
|
|
#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);
|
|
|
|
};
|
2016-08-23 09:37:19 +02:00
|
|
|
|
2016-08-23 10:14:59 +02:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
|
2016-08-23 09:37:19 +02:00
|
|
|
avcodec_parameters_to_context(codec_context, av_stream.codecpar);
|
2016-08-23 10:14:59 +02:00
|
|
|
#endif
|
2016-07-28 20:33:24 +02:00
|
|
|
#endif
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
const SampleFormat sample_format =
|
2016-07-27 15:22:40 +02:00
|
|
|
ffmpeg_sample_format(GetSampleFormat(codec_params));
|
2014-02-07 11:54:03 +01:00
|
|
|
if (sample_format == SampleFormat::UNDEFINED) {
|
|
|
|
// (error message already done by ffmpeg_sample_format())
|
2012-10-05 14:52:30 +02:00
|
|
|
return;
|
2014-02-07 11:54:03 +01:00
|
|
|
}
|
2012-10-05 14:52:30 +02:00
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
const auto audio_format = CheckAudioFormat(codec_params.sample_rate,
|
|
|
|
sample_format,
|
|
|
|
codec_params.channels);
|
2011-11-27 23:39:21 +01:00
|
|
|
|
|
|
|
/* 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() */
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
const int open_result = avcodec_open2(codec_context, codec, nullptr);
|
2012-01-04 21:41:28 +01:00
|
|
|
if (open_result < 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(ffmpeg_domain, "Could not open codec");
|
2010-01-18 10:59:11 +01:00
|
|
|
return;
|
2008-11-21 20:27:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:33:24 +02:00
|
|
|
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
|
2016-07-29 09:08:14 +02:00
|
|
|
AtScopeExit(codec_context) {
|
|
|
|
avcodec_close(codec_context);
|
|
|
|
};
|
2016-07-28 20:33:24 +02:00
|
|
|
#endif
|
2016-07-29 09:08:14 +02:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
const SignedSongTime total_time =
|
2017-09-18 07:44:47 +02:00
|
|
|
av_stream.duration != (int64_t)AV_NOPTS_VALUE
|
|
|
|
? FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base)
|
|
|
|
: FromFfmpegTimeChecked(format_context.duration, AV_TIME_BASE_Q);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, input.IsSeekable(), total_time);
|
2008-10-17 22:27:33 +02:00
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegParseMetaData(client, format_context, audio_stream);
|
2014-12-10 13:05:16 +01:00
|
|
|
|
2014-01-15 11:13:50 +01:00
|
|
|
#if LIBAVUTIL_VERSION_MAJOR >= 53
|
|
|
|
AVFrame *frame = av_frame_alloc();
|
|
|
|
#else
|
2013-03-22 07:05:00 +01:00
|
|
|
AVFrame *frame = avcodec_alloc_frame();
|
2014-01-15 11:13:50 +01:00
|
|
|
#endif
|
2013-03-22 07:05:00 +01:00
|
|
|
if (!frame) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(ffmpeg_domain, "Could not allocate frame");
|
2013-03-22 07:05:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-29 09:08:14 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2014-12-18 21:28:50 +01:00
|
|
|
FfmpegBuffer interleaved_buffer;
|
2013-08-07 15:04:26 +02:00
|
|
|
|
2015-06-19 18:02:10 +02:00
|
|
|
uint64_t min_frame = 0;
|
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
DecoderCommand cmd = client.GetCommand();
|
2015-06-22 14:41:44 +02:00
|
|
|
while (cmd != DecoderCommand::STOP) {
|
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
|
|
|
int64_t where =
|
2016-11-18 08:15:07 +01:00
|
|
|
ToFfmpegTime(client.GetSeekTime(),
|
2015-06-22 14:41:44 +02:00
|
|
|
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)
|
2016-11-18 08:15:07 +01:00
|
|
|
client.SeekError();
|
2015-06-22 14:41:44 +02:00
|
|
|
else {
|
2015-06-22 14:41:44 +02:00
|
|
|
avcodec_flush_buffers(codec_context);
|
2016-11-18 08:15:07 +01:00
|
|
|
min_frame = client.GetSeekFrame();
|
|
|
|
client.CommandFinished();
|
2015-06-22 14:41:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-18 10:59:11 +01:00
|
|
|
AVPacket packet;
|
2014-12-19 09:57:29 +01:00
|
|
|
if (av_read_frame(&format_context, &packet) < 0)
|
2008-10-30 19:03:41 +01:00
|
|
|
/* end of file */
|
|
|
|
break;
|
|
|
|
|
2014-12-11 10:39:49 +01:00
|
|
|
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegCheckTag(client, input, format_context, audio_stream);
|
2014-12-11 10:39:49 +01:00
|
|
|
#endif
|
|
|
|
|
2016-10-26 18:06:56 +02:00
|
|
|
if (packet.size > 0 && packet.stream_index == audio_stream) {
|
2016-11-18 07:13:35 +01:00
|
|
|
cmd = ffmpeg_send_packet(client, input,
|
2016-07-28 19:41:05 +02:00
|
|
|
packet,
|
2016-04-13 09:04:51 +02:00
|
|
|
*codec_context,
|
2014-12-19 10:03:35 +01:00
|
|
|
av_stream,
|
2014-12-18 23:08:08 +01:00
|
|
|
*frame,
|
2015-06-19 18:02:10 +02:00
|
|
|
min_frame, audio_format.GetFrameSize(),
|
2014-12-18 21:28:50 +01:00
|
|
|
interleaved_buffer);
|
2015-06-19 18:02:10 +02:00
|
|
|
min_frame = 0;
|
|
|
|
} else
|
2016-11-18 08:15:07 +01:00
|
|
|
cmd = client.GetCommand();
|
2008-10-30 19:03:41 +01:00
|
|
|
|
2016-04-12 21:15:05 +02:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 25, 100)
|
|
|
|
av_packet_unref(&packet);
|
|
|
|
#else
|
2008-10-30 19:03:41 +01:00
|
|
|
av_free_packet(&packet);
|
2016-04-12 21:15:05 +02:00
|
|
|
#endif
|
2015-06-22 14:41:44 +02:00
|
|
|
}
|
2014-12-19 09:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
ffmpeg_decode(DecoderClient &client, InputStream &input)
|
2014-12-19 09:57:29 +01:00
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
AVInputFormat *input_format = ffmpeg_probe(&client, input);
|
2014-12-19 09:57:29 +01:00
|
|
|
if (input_format == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FormatDebug(ffmpeg_domain, "detected input format '%s' (%s)",
|
|
|
|
input_format->name, input_format->long_name);
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
AvioStream stream(&client, input);
|
2014-12-19 09:57:29 +01:00
|
|
|
if (!stream.Open()) {
|
|
|
|
LogError(ffmpeg_domain, "Failed to open stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-16 18:38:16 +02:00
|
|
|
AVFormatContext *format_context;
|
|
|
|
try {
|
|
|
|
format_context =FfmpegOpenInput(stream.io, input.GetURI(),
|
|
|
|
input_format);
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
LogError(e);
|
2014-12-19 09:57:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-29 09:08:14 +02:00
|
|
|
AtScopeExit(&format_context) {
|
|
|
|
avformat_close_input(&format_context);
|
|
|
|
};
|
2014-12-19 09:57:29 +01:00
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
FfmpegDecode(client, input, *format_context);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 09:57:29 +01:00
|
|
|
static bool
|
|
|
|
FfmpegScanStream(AVFormatContext &format_context,
|
2016-02-22 17:38:06 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2014-12-19 09:57:29 +01:00
|
|
|
{
|
|
|
|
const int find_result =
|
|
|
|
avformat_find_stream_info(&format_context, nullptr);
|
|
|
|
if (find_result < 0)
|
|
|
|
return false;
|
|
|
|
|
2014-12-19 10:20:25 +01:00
|
|
|
const int audio_stream = ffmpeg_find_audio_stream(format_context);
|
|
|
|
if (audio_stream < 0)
|
|
|
|
return false;
|
|
|
|
|
2014-12-19 10:12:01 +01:00
|
|
|
const AVStream &stream = *format_context.streams[audio_stream];
|
|
|
|
if (stream.duration != (int64_t)AV_NOPTS_VALUE)
|
2016-02-23 10:10:13 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
2014-12-19 10:12:01 +01:00
|
|
|
FromFfmpegTime(stream.duration,
|
|
|
|
stream.time_base));
|
2017-09-18 07:44:47 +02:00
|
|
|
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));
|
2014-12-19 09:57:29 +01:00
|
|
|
|
2014-12-19 10:20:25 +01:00
|
|
|
FfmpegScanMetadata(format_context, audio_stream, handler, handler_ctx);
|
2014-12-19 09:57:29 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
ffmpeg_scan_stream(InputStream &is,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2008-10-17 22:27:33 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
AVInputFormat *input_format = ffmpeg_probe(nullptr, is);
|
|
|
|
if (input_format == nullptr)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2010-06-30 20:32:29 +02:00
|
|
|
|
2013-08-05 00:09:02 +02:00
|
|
|
AvioStream stream(nullptr, is);
|
|
|
|
if (!stream.Open())
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2010-01-18 10:57:57 +01:00
|
|
|
|
2016-09-16 18:38:16 +02:00
|
|
|
AVFormatContext *f;
|
|
|
|
try {
|
|
|
|
f = FfmpegOpenInput(stream.io, is.GetURI(), input_format);
|
|
|
|
} catch (const std::runtime_error &) {
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2016-09-16 18:38:16 +02:00
|
|
|
}
|
2010-01-18 10:57:57 +01:00
|
|
|
|
2016-07-29 09:08:14 +02:00
|
|
|
AtScopeExit(&f) {
|
|
|
|
avformat_close_input(&f);
|
|
|
|
};
|
|
|
|
|
2016-07-29 20:23:45 +02:00
|
|
|
return FfmpegScanStream(*f, handler, handler_ctx);
|
2008-10-17 22:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-23 19:48:43 +01:00
|
|
|
* 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.
|
2008-10-17 22:27:33 +02:00
|
|
|
*/
|
2008-11-04 16:55:11 +01:00
|
|
|
static const char *const ffmpeg_suffixes[] = {
|
2017-06-01 19:16:24 +02:00
|
|
|
"16sv", "3g2", "3gp", "4xm", "8svx",
|
|
|
|
"aa3", "aac", "ac3", "adx", "afc", "aif",
|
2009-02-23 19:48:43 +01:00
|
|
|
"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",
|
2010-11-05 02:01:35 +01:00
|
|
|
"gsm", "gxf", "iss", "m1v", "m2v", "m2t", "m2ts",
|
|
|
|
"m4a", "m4b", "m4v",
|
|
|
|
"mad",
|
2009-02-23 19:48:43 +01:00
|
|
|
"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",
|
2014-11-07 19:22:26 +01:00
|
|
|
"ogx", "oma", "ogg", "omg", "opus", "psp", "pva", "qcp", "qt", "r3d", "ra",
|
2009-02-23 19:48:43 +01:00
|
|
|
"ram", "rl2", "rm", "rmvb", "roq", "rpl", "rvc", "shn", "smk", "snd",
|
2016-02-13 03:49:10 +01:00
|
|
|
"sol", "son", "spx", "str", "swf", "tak", "tgi", "tgq", "tgv", "thp", "ts",
|
2009-02-23 19:48:43 +01:00
|
|
|
"tsp", "tta", "xa", "xvid", "uv", "uv2", "vb", "vid", "vob", "voc",
|
2012-05-22 23:36:47 +02:00
|
|
|
"vp6", "vmd", "wav", "webm", "wma", "wmv", "wsaud", "wsvga", "wv",
|
|
|
|
"wve",
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-10-17 22:27:33 +02:00
|
|
|
};
|
|
|
|
|
2008-11-04 16:55:11 +01:00
|
|
|
static const char *const ffmpeg_mime_types[] = {
|
2013-07-02 22:16:46 +02:00
|
|
|
"application/flv",
|
2010-01-17 16:00:14 +01:00
|
|
|
"application/m4a",
|
2009-02-23 19:48:43 +01:00
|
|
|
"application/mp4",
|
|
|
|
"application/octet-stream",
|
|
|
|
"application/ogg",
|
|
|
|
"application/x-ms-wmz",
|
|
|
|
"application/x-ms-wmd",
|
2009-12-29 22:33:46 +01:00
|
|
|
"application/x-ogg",
|
2009-02-23 19:48:43 +01:00
|
|
|
"application/x-shockwave-flash",
|
|
|
|
"application/x-shorten",
|
|
|
|
"audio/8svx",
|
|
|
|
"audio/16sv",
|
|
|
|
"audio/aac",
|
2014-02-09 16:47:45 +01:00
|
|
|
"audio/aacp",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/ac3",
|
2010-01-17 16:00:14 +01:00
|
|
|
"audio/aiff"
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/amr",
|
|
|
|
"audio/basic",
|
|
|
|
"audio/flac",
|
2010-01-17 16:00:14 +01:00
|
|
|
"audio/m4a",
|
|
|
|
"audio/mp4",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/mpeg",
|
|
|
|
"audio/musepack",
|
|
|
|
"audio/ogg",
|
2014-11-07 19:22:26 +01:00
|
|
|
"audio/opus",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/qcelp",
|
|
|
|
"audio/vorbis",
|
2009-12-29 22:33:46 +01:00
|
|
|
"audio/vorbis+ogg",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-8svx",
|
|
|
|
"audio/x-16sv",
|
|
|
|
"audio/x-aac",
|
|
|
|
"audio/x-ac3",
|
2017-06-01 19:16:24 +02:00
|
|
|
"audio/x-adx",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-aiff"
|
|
|
|
"audio/x-alaw",
|
|
|
|
"audio/x-au",
|
|
|
|
"audio/x-dca",
|
|
|
|
"audio/x-eac3",
|
|
|
|
"audio/x-flac",
|
|
|
|
"audio/x-gsm",
|
|
|
|
"audio/x-mace",
|
2010-01-17 16:00:14 +01:00
|
|
|
"audio/x-matroska",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-monkeys-audio",
|
|
|
|
"audio/x-mpeg",
|
2008-10-17 22:27:33 +02:00
|
|
|
"audio/x-ms-wma",
|
|
|
|
"audio/x-ms-wax",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-musepack",
|
2009-12-29 22:33:46 +01:00
|
|
|
"audio/x-ogg",
|
|
|
|
"audio/x-vorbis",
|
|
|
|
"audio/x-vorbis+ogg",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-pn-realaudio",
|
|
|
|
"audio/x-pn-multirate-realaudio",
|
|
|
|
"audio/x-speex",
|
|
|
|
"audio/x-tta"
|
2010-01-17 16:00:14 +01:00
|
|
|
"audio/x-voc",
|
2009-02-23 19:48:43 +01:00
|
|
|
"audio/x-wav",
|
|
|
|
"audio/x-wma",
|
|
|
|
"audio/x-wv",
|
|
|
|
"video/anim",
|
|
|
|
"video/quicktime",
|
|
|
|
"video/msvideo",
|
|
|
|
"video/ogg",
|
|
|
|
"video/theora",
|
2012-05-22 23:36:47 +02:00
|
|
|
"video/webm",
|
2009-02-23 19:48:43 +01:00
|
|
|
"video/x-dv",
|
|
|
|
"video/x-flv",
|
|
|
|
"video/x-matroska",
|
|
|
|
"video/x-mjpeg",
|
|
|
|
"video/x-mpeg",
|
|
|
|
"video/x-ms-asf",
|
|
|
|
"video/x-msvideo",
|
2008-10-17 22:27:33 +02:00
|
|
|
"video/x-ms-wmv",
|
|
|
|
"video/x-ms-wvx",
|
|
|
|
"video/x-ms-wm",
|
|
|
|
"video/x-ms-wmx",
|
2009-02-23 19:48:43 +01:00
|
|
|
"video/x-nut",
|
|
|
|
"video/x-pva",
|
|
|
|
"video/x-theora",
|
|
|
|
"video/x-vid",
|
|
|
|
"video/x-wmv",
|
|
|
|
"video/x-xvid",
|
2010-05-18 20:48:52 +02:00
|
|
|
|
|
|
|
/* special value for the "ffmpeg" input plugin: all streams by
|
|
|
|
the "ffmpeg" input plugin shall be decoded by this
|
|
|
|
plugin */
|
|
|
|
"audio/x-mpd-ffmpeg",
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-10-17 22:27:33 +02:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin ffmpeg_decoder_plugin = {
|
2013-01-26 00:37:04 +01:00
|
|
|
"ffmpeg",
|
|
|
|
ffmpeg_init,
|
2017-01-10 23:05:04 +01:00
|
|
|
ffmpeg_finish,
|
2013-01-26 00:37:04 +01:00
|
|
|
ffmpeg_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
ffmpeg_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
ffmpeg_suffixes,
|
|
|
|
ffmpeg_mime_types
|
2008-10-17 22:27:33 +02:00
|
|
|
};
|