Filter.cxx 4.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "Filter.hxx"
21
#include "ChannelLayout.hxx"
22
#include "SampleFormat.hxx"
23
#include "pcm/AudioFormat.hxx"
24 25
#include "util/RuntimeError.hxx"

26 27
#include <cinttypes>

28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <stdio.h>

namespace Ffmpeg {

static const auto &
RequireFilterByName(const char *name)
{
	const auto *filter = avfilter_get_by_name(name);
	if (filter == nullptr)
		throw FormatRuntimeError("No such FFmpeg filter: '%s'", name);

	return *filter;
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	AVSampleFormat src_format = ToFfmpegSampleFormat(audio_format.format);
	if (src_format == AV_SAMPLE_FMT_NONE) {
		switch (audio_format.format) {
		case SampleFormat::S24_P32:
			audio_format.format = SampleFormat::S32;
			src_format = AV_SAMPLE_FMT_S32;
			break;

		default:
			audio_format.format = SampleFormat::S16;
			src_format = AV_SAMPLE_FMT_S16;
			break;
		}
	}

	char abuffer_args[256];
	sprintf(abuffer_args,
86
		"sample_rate=%u:sample_fmt=%s:channel_layout=0x%" PRIx64 ":time_base=1/%u",
87 88
		audio_format.sample_rate,
		av_get_sample_fmt_name(src_format),
89
		ToFfmpegChannelLayout(audio_format.channels),
90 91
		audio_format.sample_rate);

92 93
	return CreateFilter(RequireFilterByName("abuffer"), "abuffer",
			    abuffer_args, nullptr, graph_ctx);
94 95
}

96 97
AVFilterContext &
MakeAudioBufferSink(AVFilterGraph &graph_ctx)
98
{
99 100
	return CreateFilter(RequireFilterByName("abuffersink"), "abuffersink",
			    graph_ctx);
101 102
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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);
}

133 134 135 136 137 138 139 140
AVFilterContext &
MakeAutoAformat(AVFilterGraph &graph_ctx)
{
	return CreateFilter(RequireFilterByName("aformat"), "aformat",
			    "sample_fmts=flt|s32|s16",
			    nullptr, graph_ctx);
}

141 142 143 144 145 146 147 148 149 150 151 152 153
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");
}

154
} // namespace Ffmpeg