FfmpegFilter.cxx 2.58 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 "FfmpegFilter.hxx"
21
#include "lib/ffmpeg/Interleave.hxx"
22 23 24 25 26 27 28 29
#include "lib/ffmpeg/SampleFormat.hxx"
#include "util/ConstBuffer.hxx"

extern "C" {
#include <libavfilter/buffersrc.h>
#include <libavfilter/buffersink.h>
}

30 31
#include <string.h>

32 33 34
FfmpegFilter::FfmpegFilter(const AudioFormat &in_audio_format,
			   const AudioFormat &_out_audio_format,
			   Ffmpeg::FilterGraph &&_graph,
35 36
			   AVFilterContext &_buffer_src,
			   AVFilterContext &_buffer_sink) noexcept
37 38
	:Filter(_out_audio_format),
	 graph(std::move(_graph)),
39 40
	 buffer_src(_buffer_src),
	 buffer_sink(_buffer_sink),
41 42 43
	 in_format(Ffmpeg::ToFfmpegSampleFormat(in_audio_format.format)),
	 in_sample_rate(in_audio_format.sample_rate),
	 in_channels(in_audio_format.channels),
44 45 46 47 48 49 50 51 52 53
	 in_audio_frame_size(in_audio_format.GetFrameSize()),
	 out_audio_frame_size(_out_audio_format.GetFrameSize())
{
}

ConstBuffer<void>
FfmpegFilter::FilterPCM(ConstBuffer<void> src)
{
	/* submit source data into the FFmpeg audio buffer source */

54 55 56 57 58
	frame.Unref();
	frame->format = in_format;
	frame->sample_rate = in_sample_rate;
	frame->channels = in_channels;
	frame->nb_samples = src.size / in_audio_frame_size;
59

60
	frame.GetBuffer();
61

62
	memcpy(frame.GetData(0), src.data, src.size);
63

64
	int err = av_buffersrc_add_frame(&buffer_src, frame.get());
65 66 67 68 69
	if (err < 0)
		throw MakeFfmpegError(err, "av_buffersrc_write_frame() failed");

	/* collect filtered data from the FFmpeg audio buffer sink */

70
	frame.Unref();
71

72
	err = av_buffersink_get_frame(&buffer_sink, frame.get());
73 74 75 76 77 78 79 80 81 82
	if (err < 0) {
		if (err == AVERROR(EAGAIN) || err == AVERROR_EOF)
			return nullptr;

		throw MakeFfmpegError(err, "av_buffersink_get_frame() failed");
	}

	/* TODO: call av_buffersink_get_frame() repeatedly?  Not
	   possible with MPD's current Filter API */

83
	return Ffmpeg::InterleaveFrame(*frame, interleave_buffer);
84
}