From 67c6d111a8e735612a884499290492db089998d6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jul 2022 18:12:12 +0200 Subject: [PATCH] filter/*: use std::span instead of ConstBuffer --- src/filter/Filter.cxx | 27 ------------------- src/filter/Filter.hxx | 10 ++++--- src/filter/NullFilter.hxx | 3 +-- src/filter/Observer.cxx | 5 ++-- src/filter/meson.build | 1 - src/filter/plugins/ConvertFilterPlugin.cxx | 9 +++---- src/filter/plugins/FfmpegFilter.cxx | 11 ++++---- src/filter/plugins/FfmpegFilter.hxx | 2 +- src/filter/plugins/NormalizeFilterPlugin.cxx | 15 +++++------ src/filter/plugins/ReplayGainFilterPlugin.cxx | 7 +++-- src/filter/plugins/RouteFilterPlugin.cxx | 13 +++++---- src/filter/plugins/TwoFilters.cxx | 9 +++---- src/filter/plugins/TwoFilters.hxx | 4 +-- src/filter/plugins/VolumeFilterPlugin.cxx | 7 +++-- src/output/Source.cxx | 2 +- test/run_filter.cxx | 11 ++++---- 16 files changed, 50 insertions(+), 86 deletions(-) delete mode 100644 src/filter/Filter.cxx diff --git a/src/filter/Filter.cxx b/src/filter/Filter.cxx deleted file mode 100644 index 36cd5e3b8..000000000 --- a/src/filter/Filter.cxx +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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. - */ - -#include "Filter.hxx" -#include "util/ConstBuffer.hxx" - -ConstBuffer -Filter::Flush() -{ - return nullptr; -} diff --git a/src/filter/Filter.hxx b/src/filter/Filter.hxx index b776b4c1d..29a17cfe0 100644 --- a/src/filter/Filter.hxx +++ b/src/filter/Filter.hxx @@ -23,8 +23,8 @@ #include "pcm/AudioFormat.hxx" #include - -template struct ConstBuffer; +#include +#include class Filter { protected: @@ -61,7 +61,7 @@ public: * invalidated by deleting this object or the next FilterPCM() * or Reset() call) */ - virtual ConstBuffer FilterPCM(ConstBuffer src) = 0; + virtual std::span FilterPCM(std::span src) = 0; /** * Flush pending data and return it. This should be called @@ -69,7 +69,9 @@ public: * * Throws on error. */ - virtual ConstBuffer Flush(); + virtual std::span Flush() { + return {}; + } }; #endif diff --git a/src/filter/NullFilter.hxx b/src/filter/NullFilter.hxx index 81336f79e..2ca70958f 100644 --- a/src/filter/NullFilter.hxx +++ b/src/filter/NullFilter.hxx @@ -21,13 +21,12 @@ #define MPD_NULL_FILTER_HXX #include "filter/Filter.hxx" -#include "util/ConstBuffer.hxx" class NullFilter final : public Filter { public: explicit NullFilter(const AudioFormat &af):Filter(af) {} - ConstBuffer FilterPCM(ConstBuffer src) override { + std::span FilterPCM(std::span src) override { return src; } }; diff --git a/src/filter/Observer.cxx b/src/filter/Observer.cxx index 53fac21d2..690678598 100644 --- a/src/filter/Observer.cxx +++ b/src/filter/Observer.cxx @@ -20,7 +20,6 @@ #include "Observer.hxx" #include "Filter.hxx" #include "Prepared.hxx" -#include "util/ConstBuffer.hxx" #include @@ -81,11 +80,11 @@ public: filter->Reset(); } - ConstBuffer FilterPCM(ConstBuffer src) override { + std::span FilterPCM(std::span src) override { return filter->FilterPCM(src); } - ConstBuffer Flush() override { + std::span Flush() override { return filter->Flush(); } }; diff --git a/src/filter/meson.build b/src/filter/meson.build index 29a831e73..cd491fcc9 100644 --- a/src/filter/meson.build +++ b/src/filter/meson.build @@ -1,7 +1,6 @@ filter_api = static_library( 'filter_api', 'Observer.cxx', - 'Filter.cxx', include_directories: inc, ) diff --git a/src/filter/plugins/ConvertFilterPlugin.cxx b/src/filter/plugins/ConvertFilterPlugin.cxx index 77915766b..ef0253d14 100644 --- a/src/filter/plugins/ConvertFilterPlugin.cxx +++ b/src/filter/plugins/ConvertFilterPlugin.cxx @@ -22,7 +22,6 @@ #include "filter/Prepared.hxx" #include "pcm/AudioFormat.hxx" #include "pcm/Convert.hxx" -#include "util/ConstBuffer.hxx" #include #include @@ -50,9 +49,9 @@ public: state->Reset(); } - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; - ConstBuffer Flush() override { + std::span Flush() override { return state ? state->Flush() : std::span{}; @@ -102,8 +101,8 @@ PreparedConvertFilter::Open(AudioFormat &audio_format) return std::make_unique(audio_format); } -ConstBuffer -ConvertFilter::FilterPCM(ConstBuffer src) +std::span +ConvertFilter::FilterPCM(std::span src) { return state ? state->Convert(src) diff --git a/src/filter/plugins/FfmpegFilter.cxx b/src/filter/plugins/FfmpegFilter.cxx index 7786407fe..49afa4c61 100644 --- a/src/filter/plugins/FfmpegFilter.cxx +++ b/src/filter/plugins/FfmpegFilter.cxx @@ -20,7 +20,6 @@ #include "FfmpegFilter.hxx" #include "lib/ffmpeg/Interleave.hxx" #include "lib/ffmpeg/SampleFormat.hxx" -#include "util/ConstBuffer.hxx" extern "C" { #include @@ -46,8 +45,8 @@ FfmpegFilter::FfmpegFilter(const AudioFormat &in_audio_format, { } -ConstBuffer -FfmpegFilter::FilterPCM(ConstBuffer src) +std::span +FfmpegFilter::FilterPCM(std::span src) { /* submit source data into the FFmpeg audio buffer source */ @@ -55,11 +54,11 @@ FfmpegFilter::FilterPCM(ConstBuffer src) frame->format = in_format; frame->sample_rate = in_sample_rate; frame->channels = in_channels; - frame->nb_samples = src.size / in_audio_frame_size; + frame->nb_samples = src.size() / in_audio_frame_size; frame.GetBuffer(); - memcpy(frame.GetData(0), src.data, src.size); + memcpy(frame.GetData(0), src.data(), src.size()); int err = av_buffersrc_add_frame(&buffer_src, frame.get()); if (err < 0) @@ -72,7 +71,7 @@ FfmpegFilter::FilterPCM(ConstBuffer src) err = av_buffersink_get_frame(&buffer_sink, frame.get()); if (err < 0) { if (err == AVERROR(EAGAIN) || err == AVERROR_EOF) - return nullptr; + return {}; throw MakeFfmpegError(err, "av_buffersink_get_frame() failed"); } diff --git a/src/filter/plugins/FfmpegFilter.hxx b/src/filter/plugins/FfmpegFilter.hxx index e889eb672..edbed69cf 100644 --- a/src/filter/plugins/FfmpegFilter.hxx +++ b/src/filter/plugins/FfmpegFilter.hxx @@ -55,7 +55,7 @@ public: AVFilterContext &_buffer_sink) noexcept; /* virtual methods from class Filter */ - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; }; #endif diff --git a/src/filter/plugins/NormalizeFilterPlugin.cxx b/src/filter/plugins/NormalizeFilterPlugin.cxx index 1c9832bff..975950fd1 100644 --- a/src/filter/plugins/NormalizeFilterPlugin.cxx +++ b/src/filter/plugins/NormalizeFilterPlugin.cxx @@ -24,7 +24,6 @@ #include "pcm/Buffer.hxx" #include "pcm/AudioFormat.hxx" #include "pcm/AudioCompress/compress.h" -#include "util/ConstBuffer.hxx" #include @@ -47,7 +46,7 @@ public: NormalizeFilter &operator=(const NormalizeFilter &) = delete; /* virtual methods from class Filter */ - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; }; class PreparedNormalizeFilter final : public PreparedFilter { @@ -70,14 +69,14 @@ PreparedNormalizeFilter::Open(AudioFormat &audio_format) return std::make_unique(audio_format); } -ConstBuffer -NormalizeFilter::FilterPCM(ConstBuffer src) +std::span +NormalizeFilter::FilterPCM(std::span src) { - auto *dest = (int16_t *)buffer.Get(src.size); - memcpy(dest, src.data, src.size); + auto *dest = (int16_t *)buffer.Get(src.size()); + memcpy(dest, src.data(), src.size()); - Compressor_Process_int16(compressor, dest, src.size / 2); - return { (const void *)dest, src.size }; + Compressor_Process_int16(compressor, dest, src.size() / 2); + return { (const std::byte *)dest, src.size() }; } const FilterPlugin normalize_filter_plugin = { diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index 4bb0ec2f0..8d67a7313 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -25,7 +25,6 @@ #include "mixer/MixerControl.hxx" #include "pcm/AudioFormat.hxx" #include "pcm/Volume.hxx" -#include "util/ConstBuffer.hxx" #include "util/Domain.hxx" #include "Idle.hxx" #include "Log.hxx" @@ -109,7 +108,7 @@ public: void Update(); /* virtual methods from class Filter */ - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; }; class PreparedReplayGainFilter final : public PreparedFilter { @@ -197,8 +196,8 @@ PreparedReplayGainFilter::Open(AudioFormat &af) af, mixer, base); } -ConstBuffer -ReplayGainFilter::FilterPCM(ConstBuffer src) +std::span +ReplayGainFilter::FilterPCM(std::span src) { return mixer != nullptr ? std::span{src} diff --git a/src/filter/plugins/RouteFilterPlugin.cxx b/src/filter/plugins/RouteFilterPlugin.cxx index 796af56a4..010cea359 100644 --- a/src/filter/plugins/RouteFilterPlugin.cxx +++ b/src/filter/plugins/RouteFilterPlugin.cxx @@ -49,7 +49,6 @@ #include "pcm/Silence.hxx" #include "util/StringStrip.hxx" #include "util/RuntimeError.hxx" -#include "util/ConstBuffer.hxx" #include #include @@ -94,7 +93,7 @@ public: const std::array &_sources); /* virtual methods from class Filter */ - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; }; class PreparedRouteFilter final : public PreparedFilter { @@ -221,15 +220,15 @@ PreparedRouteFilter::Open(AudioFormat &audio_format) sources); } -ConstBuffer -RouteFilter::FilterPCM(ConstBuffer src) +std::span +RouteFilter::FilterPCM(std::span src) { - size_t number_of_frames = src.size / input_frame_size; + size_t number_of_frames = src.size() / input_frame_size; const size_t bytes_per_frame_per_channel = input_format.GetSampleSize(); // A moving pointer that always refers to channel 0 in the input, at the currently handled frame - const auto *base_source = (const uint8_t *)src.data; + const auto *base_source = (const uint8_t *)src.data(); // Grow our reusable buffer, if needed, and set the moving pointer const size_t result_size = number_of_frames * output_frame_size; @@ -268,7 +267,7 @@ RouteFilter::FilterPCM(ConstBuffer src) } // Here it is, ladies and gentlemen! Rerouted data! - return { result, result_size }; + return { (const std::byte *)result, result_size }; } const FilterPlugin route_filter_plugin = { diff --git a/src/filter/plugins/TwoFilters.cxx b/src/filter/plugins/TwoFilters.cxx index 213df8548..339c44a36 100644 --- a/src/filter/plugins/TwoFilters.cxx +++ b/src/filter/plugins/TwoFilters.cxx @@ -19,21 +19,20 @@ #include "TwoFilters.hxx" #include "pcm/AudioFormat.hxx" -#include "util/ConstBuffer.hxx" #include "util/RuntimeError.hxx" #include "util/StringBuffer.hxx" -ConstBuffer -TwoFilters::FilterPCM(ConstBuffer src) +std::span +TwoFilters::FilterPCM(std::span src) { return second->FilterPCM(first->FilterPCM(src)); } -ConstBuffer +std::span TwoFilters::Flush() { auto result = first->Flush(); - if (!result.IsNull()) + if (result.data() != nullptr) /* Flush() output from the first Filter must be filtered by the second Filter */ return second->FilterPCM(result); diff --git a/src/filter/plugins/TwoFilters.hxx b/src/filter/plugins/TwoFilters.hxx index d5fa77cd2..bccf50b0d 100644 --- a/src/filter/plugins/TwoFilters.hxx +++ b/src/filter/plugins/TwoFilters.hxx @@ -44,8 +44,8 @@ public: second->Reset(); } - ConstBuffer FilterPCM(ConstBuffer src) override; - ConstBuffer Flush() override; + std::span FilterPCM(std::span src) override; + std::span Flush() override; }; /** diff --git a/src/filter/plugins/VolumeFilterPlugin.cxx b/src/filter/plugins/VolumeFilterPlugin.cxx index 2b5f4f34f..c6d77a2d5 100644 --- a/src/filter/plugins/VolumeFilterPlugin.cxx +++ b/src/filter/plugins/VolumeFilterPlugin.cxx @@ -22,7 +22,6 @@ #include "filter/Prepared.hxx" #include "pcm/Volume.hxx" #include "pcm/AudioFormat.hxx" -#include "util/ConstBuffer.hxx" class VolumeFilter final : public Filter { PcmVolume pv; @@ -43,7 +42,7 @@ public: } /* virtual methods from class Filter */ - ConstBuffer FilterPCM(ConstBuffer src) override; + std::span FilterPCM(std::span src) override; }; class PreparedVolumeFilter final : public PreparedFilter { @@ -58,8 +57,8 @@ PreparedVolumeFilter::Open(AudioFormat &audio_format) return std::make_unique(audio_format); } -ConstBuffer -VolumeFilter::FilterPCM(ConstBuffer src) +std::span +VolumeFilter::FilterPCM(std::span src) { return pv.Apply(src); } diff --git a/src/output/Source.cxx b/src/output/Source.cxx index 8ca863e1e..d768d00cc 100644 --- a/src/output/Source.cxx +++ b/src/output/Source.cxx @@ -255,5 +255,5 @@ AudioOutputSource::Flush() { return filter ? filter->Flush() - : nullptr; + : std::span{}; } diff --git a/test/run_filter.cxx b/test/run_filter.cxx index 04d35dfba..565ddc721 100644 --- a/test/run_filter.cxx +++ b/test/run_filter.cxx @@ -30,7 +30,6 @@ #include "mixer/MixerControl.hxx" #include "system/Error.hxx" #include "io/FileDescriptor.hxx" -#include "util/ConstBuffer.hxx" #include "util/StringBuffer.hxx" #include "util/RuntimeError.hxx" #include "util/PrintException.hxx" @@ -102,22 +101,22 @@ try { FileDescriptor output_fd(STDOUT_FILENO); while (true) { - char buffer[4096]; + std::byte buffer[4096]; ssize_t nbytes = ReadFrames(input_fd, buffer, sizeof(buffer), in_frame_size); if (nbytes == 0) break; - auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes}); - output_fd.FullWrite(dest.data, dest.size); + auto dest = filter->FilterPCM(std::span{buffer}.first(nbytes)); + output_fd.FullWrite(dest.data(), dest.size()); } while (true) { auto dest = filter->Flush(); - if (dest.IsNull()) + if (dest.data() == nullptr) break; - output_fd.FullWrite(dest.data, dest.size); + output_fd.FullWrite(dest.data(), dest.size()); } /* cleanup and exit */