Merge tag 'v0.22.11'

release v0.22.11
This commit is contained in:
Max Kellermann
2021-08-24 22:19:38 +02:00
19 changed files with 330 additions and 137 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* 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.
*/
#ifndef MPD_FFMPEG_CHANNEL_LAYOUT_HXX
#define MPD_FFMPEG_CHANNEL_LAYOUT_HXX
extern "C" {
#include <libavutil/channel_layout.h>
}
/**
* Convert a MPD channel count to a libavutil channel_layout bit mask.
*/
static constexpr uint64_t
ToFfmpegChannelLayout(unsigned channels) noexcept
{
switch (channels) {
case 1:
return AV_CH_LAYOUT_MONO;
case 2:
return AV_CH_LAYOUT_STEREO;
case 3:
return AV_CH_LAYOUT_SURROUND;
case 4:
// TODO is this AV_CH_LAYOUT_2_2?
return AV_CH_LAYOUT_QUAD;
case 5:
// TODO is this AV_CH_LAYOUT_5POINT0_BACK?
return AV_CH_LAYOUT_5POINT0;
case 6:
return AV_CH_LAYOUT_5POINT1;
case 7:
return AV_CH_LAYOUT_6POINT1;
case 8:
return AV_CH_LAYOUT_7POINT1;
default:
/* unreachable */
return 0;
}
}
#endif

View File

@@ -62,8 +62,14 @@ DetectFilterOutputFormat(const AudioFormat &in_audio_format,
frame.Unref();
err = av_buffersink_get_frame(&buffer_sink, frame.get());
if (err < 0)
if (err < 0) {
if (err == AVERROR(EAGAIN))
/* one sample was not enough input data for
the given filter graph */
return AudioFormat::Undefined();
throw MakeFfmpegError(err, "av_buffersink_get_frame() failed");
}
const SampleFormat sample_format = FromFfmpegSampleFormat(AVSampleFormat(frame->format));
if (sample_format == SampleFormat::UNDEFINED)

View File

@@ -35,6 +35,9 @@ namespace Ffmpeg {
* between.
*
* This function can throw if the FFmpeg filter fails.
*
* @return the output format or AudioFormat::Undefined() if it was not
* possible to determine the format
*/
AudioFormat
DetectFilterOutputFormat(const AudioFormat &in_audio_format,

View File

@@ -18,10 +18,13 @@
*/
#include "Filter.hxx"
#include "ChannelLayout.hxx"
#include "SampleFormat.hxx"
#include "pcm/AudioFormat.hxx"
#include "util/RuntimeError.hxx"
#include <cinttypes>
#include <stdio.h>
namespace Ffmpeg {
@@ -36,9 +39,32 @@ RequireFilterByName(const char *name)
return *filter;
}
FilterContext
FilterContext::MakeAudioBufferSource(AudioFormat &audio_format,
AVFilterGraph &graph_ctx)
static AVFilterContext &
CreateFilter(const AVFilter &filt,
const char *name, const char *args, void *opaque,
AVFilterGraph &graph_ctx)
{
AVFilterContext *context = nullptr;
int err = avfilter_graph_create_filter(&context, &filt,
name, args, opaque,
&graph_ctx);
if (err < 0)
throw MakeFfmpegError(err, "avfilter_graph_create_filter() failed");
return *context;
}
static AVFilterContext &
CreateFilter(const AVFilter &filt,
const char *name,
AVFilterGraph &graph_ctx)
{
return CreateFilter(filt, name, nullptr, nullptr, graph_ctx);
}
AVFilterContext &
MakeAudioBufferSource(AudioFormat &audio_format,
AVFilterGraph &graph_ctx)
{
AVSampleFormat src_format = ToFfmpegSampleFormat(audio_format.format);
if (src_format == AV_SAMPLE_FMT_NONE) {
@@ -57,19 +83,72 @@ FilterContext::MakeAudioBufferSource(AudioFormat &audio_format,
char abuffer_args[256];
sprintf(abuffer_args,
"sample_rate=%u:sample_fmt=%s:channels=%u:time_base=1/%u",
"sample_rate=%u:sample_fmt=%s:channel_layout=0x%" PRIx64 ":time_base=1/%u",
audio_format.sample_rate,
av_get_sample_fmt_name(src_format),
audio_format.channels,
ToFfmpegChannelLayout(audio_format.channels),
audio_format.sample_rate);
return {RequireFilterByName("abuffer"), "abuffer", abuffer_args, nullptr, graph_ctx};
return CreateFilter(RequireFilterByName("abuffer"), "abuffer",
abuffer_args, nullptr, graph_ctx);
}
FilterContext
FilterContext::MakeAudioBufferSink(AVFilterGraph &graph_ctx)
AVFilterContext &
MakeAudioBufferSink(AVFilterGraph &graph_ctx)
{
return {RequireFilterByName("abuffersink"), "abuffersink", graph_ctx};
return CreateFilter(RequireFilterByName("abuffersink"), "abuffersink",
graph_ctx);
}
AVFilterContext &
MakeAformat(AudioFormat &audio_format,
AVFilterGraph &graph_ctx)
{
AVSampleFormat dest_format = ToFfmpegSampleFormat(audio_format.format);
if (dest_format == AV_SAMPLE_FMT_NONE) {
switch (audio_format.format) {
case SampleFormat::S24_P32:
audio_format.format = SampleFormat::S32;
dest_format = AV_SAMPLE_FMT_S32;
break;
default:
audio_format.format = SampleFormat::S16;
dest_format = AV_SAMPLE_FMT_S16;
break;
}
}
char args[256];
sprintf(args,
"sample_rates=%u:sample_fmts=%s:channel_layouts=0x%" PRIx64,
audio_format.sample_rate,
av_get_sample_fmt_name(dest_format),
ToFfmpegChannelLayout(audio_format.channels));
return CreateFilter(RequireFilterByName("aformat"), "aformat",
args, nullptr, graph_ctx);
}
AVFilterContext &
MakeAutoAformat(AVFilterGraph &graph_ctx)
{
return CreateFilter(RequireFilterByName("aformat"), "aformat",
"sample_fmts=flt|s32|s16",
nullptr, graph_ctx);
}
void
FilterGraph::ParseSingleInOut(const char *filters, AVFilterContext &in,
AVFilterContext &out)
{
auto [inputs, outputs] = Parse(filters, {"out", in}, {"in", out});
if (inputs.get() != nullptr)
throw std::runtime_error("FFmpeg filter has an open input");
if (outputs.get() != nullptr)
throw std::runtime_error("FFmpeg filter has an open output");
}
} // namespace Ffmpeg

View File

@@ -77,63 +77,38 @@ public:
}
};
class FilterContext {
AVFilterContext *context = nullptr;
/**
* Create an "abuffer" filter.
*
* @param the input audio format; may be modified by the
* function to ask the caller to do format conversion
*/
AVFilterContext &
MakeAudioBufferSource(AudioFormat &audio_format,
AVFilterGraph &graph_ctx);
public:
FilterContext() = default;
/**
* Create an "abuffersink" filter.
*/
AVFilterContext &
MakeAudioBufferSink(AVFilterGraph &graph_ctx);
FilterContext(const AVFilter &filt,
const char *name, const char *args, void *opaque,
AVFilterGraph &graph_ctx) {
int err = avfilter_graph_create_filter(&context, &filt,
name, args, opaque,
&graph_ctx);
if (err < 0)
throw MakeFfmpegError(err, "avfilter_graph_create_filter() failed");
}
/**
* Create an "aformat" filter.
*
* @param the output audio format; may be modified by the function if
* the given format is not supported by libavfilter
*/
AVFilterContext &
MakeAformat(AudioFormat &audio_format,
AVFilterGraph &graph_ctx);
FilterContext(const AVFilter &filt,
const char *name,
AVFilterGraph &graph_ctx)
:FilterContext(filt, name, nullptr, nullptr, graph_ctx) {}
FilterContext(FilterContext &&src) noexcept
:context(std::exchange(src.context, nullptr)) {}
~FilterContext() noexcept {
if (context != nullptr)
avfilter_free(context);
}
FilterContext &operator=(FilterContext &&src) noexcept {
using std::swap;
swap(context, src.context);
return *this;
}
/**
* Create an "abuffer" filter.
*
* @param the input audio format; may be modified by the
* function to ask the caller to do format conversion
*/
static FilterContext MakeAudioBufferSource(AudioFormat &audio_format,
AVFilterGraph &graph_ctx);
/**
* Create an "abuffersink" filter.
*/
static FilterContext MakeAudioBufferSink(AVFilterGraph &graph_ctx);
auto &operator*() noexcept {
return *context;
}
auto *get() noexcept {
return context;
}
};
/**
* Create an "aformat" filter which automatically converts the output
* to a format supported by MPD.
*/
AVFilterContext &
MakeAutoAformat(AVFilterGraph &graph_ctx);
class FilterGraph {
AVFilterGraph *graph = nullptr;
@@ -180,6 +155,9 @@ public:
return std::make_pair(std::move(inputs), std::move(outputs));
}
void ParseSingleInOut(const char *filters, AVFilterContext &in,
AVFilterContext &out);
std::pair<FilterInOut, FilterInOut> Parse(const char *filters) {
AVFilterInOut *inputs, *outputs;
int err = avfilter_graph_parse2(graph, filters,