From f2a8d4d2891a95a5432cd19250025c5291463738 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Jan 2013 23:28:13 +0100 Subject: filter/convert: convert to C++ --- src/filter/AutoConvertFilterPlugin.cxx | 169 +++++++++++++++++++++++++++++++++ src/filter/AutoConvertFilterPlugin.hxx | 34 +++++++ src/filter/ConvertFilterPlugin.cxx | 148 +++++++++++++++++++++++++++++ src/filter/ConvertFilterPlugin.hxx | 36 +++++++ src/filter/autoconvert_filter_plugin.c | 167 -------------------------------- src/filter/autoconvert_filter_plugin.h | 34 ------- src/filter/convert_filter_plugin.c | 146 ---------------------------- src/filter/convert_filter_plugin.h | 36 ------- 8 files changed, 387 insertions(+), 383 deletions(-) create mode 100644 src/filter/AutoConvertFilterPlugin.cxx create mode 100644 src/filter/AutoConvertFilterPlugin.hxx create mode 100644 src/filter/ConvertFilterPlugin.cxx create mode 100644 src/filter/ConvertFilterPlugin.hxx delete mode 100644 src/filter/autoconvert_filter_plugin.c delete mode 100644 src/filter/autoconvert_filter_plugin.h delete mode 100644 src/filter/convert_filter_plugin.c delete mode 100644 src/filter/convert_filter_plugin.h (limited to 'src/filter') diff --git a/src/filter/AutoConvertFilterPlugin.cxx b/src/filter/AutoConvertFilterPlugin.cxx new file mode 100644 index 00000000..3a8c087b --- /dev/null +++ b/src/filter/AutoConvertFilterPlugin.cxx @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "AutoConvertFilterPlugin.hxx" +#include "ConvertFilterPlugin.hxx" +#include "filter_plugin.h" +#include "filter_internal.h" +#include "filter_registry.h" +#include "audio_format.h" + +#include + +struct AutoConvertFilter { + struct filter base; + + /** + * The audio format being fed to the underlying filter. This + * plugin actually doesn't need this variable, we have it here + * just so our open() method doesn't return a stack pointer. + */ + struct audio_format in_audio_format; + + /** + * The underlying filter. + */ + struct filter *filter; + + /** + * A convert_filter, just in case conversion is needed. nullptr + * if unused. + */ + struct filter *convert; + + AutoConvertFilter(const filter_plugin &plugin, struct filter *_filter) + :filter(_filter) { + filter_init(&base, &plugin); + } + + ~AutoConvertFilter() { + filter_free(filter); + } +}; + +static void +autoconvert_filter_finish(struct filter *_filter) +{ + AutoConvertFilter *filter = (AutoConvertFilter *)_filter; + + delete filter; +} + +static const struct audio_format * +autoconvert_filter_open(struct filter *_filter, + struct audio_format *in_audio_format, + GError **error_r) +{ + AutoConvertFilter *filter = (AutoConvertFilter *)_filter; + const struct audio_format *out_audio_format; + + assert(audio_format_valid(in_audio_format)); + + /* open the "real" filter */ + + filter->in_audio_format = *in_audio_format; + + out_audio_format = filter_open(filter->filter, + &filter->in_audio_format, error_r); + if (out_audio_format == nullptr) + return nullptr; + + /* need to convert? */ + + if (!audio_format_equals(&filter->in_audio_format, in_audio_format)) { + /* yes - create a convert_filter */ + struct audio_format audio_format2 = *in_audio_format; + const struct audio_format *audio_format3; + + filter->convert = filter_new(&convert_filter_plugin, nullptr, + error_r); + if (filter->convert == nullptr) { + filter_close(filter->filter); + return nullptr; + } + + audio_format3 = filter_open(filter->convert, &audio_format2, + error_r); + if (audio_format3 == nullptr) { + filter_free(filter->convert); + filter_close(filter->filter); + return nullptr; + } + + assert(audio_format_equals(&audio_format2, in_audio_format)); + + convert_filter_set(filter->convert, &filter->in_audio_format); + } else + /* no */ + filter->convert = nullptr; + + return out_audio_format; +} + +static void +autoconvert_filter_close(struct filter *_filter) +{ + AutoConvertFilter *filter = + (AutoConvertFilter *)_filter; + + if (filter->convert != nullptr) { + filter_close(filter->convert); + filter_free(filter->convert); + } + + filter_close(filter->filter); +} + +static const void * +autoconvert_filter_filter(struct filter *_filter, const void *src, + size_t src_size, size_t *dest_size_r, + GError **error_r) +{ + AutoConvertFilter *filter = (AutoConvertFilter *)_filter; + + if (filter->convert != nullptr) { + src = filter_filter(filter->convert, src, src_size, &src_size, + error_r); + if (src == nullptr) + return nullptr; + } + + return filter_filter(filter->filter, src, src_size, dest_size_r, + error_r); +} + +static const struct filter_plugin autoconvert_filter_plugin = { + "convert", + nullptr, + autoconvert_filter_finish, + autoconvert_filter_open, + autoconvert_filter_close, + autoconvert_filter_filter, +}; + +struct filter * +autoconvert_filter_new(struct filter *_filter) +{ + AutoConvertFilter *filter = + new AutoConvertFilter(autoconvert_filter_plugin, + _filter); + + return &filter->base; +} diff --git a/src/filter/AutoConvertFilterPlugin.hxx b/src/filter/AutoConvertFilterPlugin.hxx new file mode 100644 index 00000000..062efcbd --- /dev/null +++ b/src/filter/AutoConvertFilterPlugin.hxx @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2003-2013 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_AUTOCONVERT_FILTER_PLUGIN_HXX +#define MPD_AUTOCONVERT_FILTER_PLUGIN_HXX + +struct filter; + +/** + * Creates a new "autoconvert" filter. When opened, it ensures that + * the input audio format isn't changed. If the underlying filter + * requests a different format, it automatically creates a + * convert_filter. + */ +struct filter * +autoconvert_filter_new(struct filter *filter); + +#endif diff --git a/src/filter/ConvertFilterPlugin.cxx b/src/filter/ConvertFilterPlugin.cxx new file mode 100644 index 00000000..04f34842 --- /dev/null +++ b/src/filter/ConvertFilterPlugin.cxx @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "ConvertFilterPlugin.hxx" +#include "filter_plugin.h" +#include "filter_internal.h" +#include "filter_registry.h" +#include "conf.h" +#include "pcm_convert.h" +#include "audio_format.h" +#include "poison.h" + +#include +#include + +struct ConvertFilter { + struct filter base; + + /** + * The current convert, from 0 to #PCM_CONVERT_1. + */ + unsigned convert; + + /** + * The input audio format; PCM data is passed to the filter() + * method in this format. + */ + struct audio_format in_audio_format; + + /** + * The output audio format; the consumer of this plugin + * expects PCM data in this format. This defaults to + * #in_audio_format, and can be set with convert_filter_set(). + */ + struct audio_format out_audio_format; + + struct pcm_convert_state state; + + ConvertFilter() { + filter_init(&base, &convert_filter_plugin); + } +}; + +static struct filter * +convert_filter_init(gcc_unused const struct config_param *param, + gcc_unused GError **error_r) +{ + ConvertFilter *filter = new ConvertFilter(); + return &filter->base; +} + +static void +convert_filter_finish(struct filter *filter) +{ + delete filter; +} + +static const struct audio_format * +convert_filter_open(struct filter *_filter, struct audio_format *audio_format, + gcc_unused GError **error_r) +{ + ConvertFilter *filter = (ConvertFilter *)_filter; + + assert(audio_format_valid(audio_format)); + + filter->in_audio_format = filter->out_audio_format = *audio_format; + pcm_convert_init(&filter->state); + + return &filter->in_audio_format; +} + +static void +convert_filter_close(struct filter *_filter) +{ + ConvertFilter *filter = (ConvertFilter *)_filter; + + pcm_convert_deinit(&filter->state); + + poison_undefined(&filter->in_audio_format, + sizeof(filter->in_audio_format)); + poison_undefined(&filter->out_audio_format, + sizeof(filter->out_audio_format)); +} + +static const void * +convert_filter_filter(struct filter *_filter, const void *src, size_t src_size, + size_t *dest_size_r, GError **error_r) +{ + ConvertFilter *filter = (ConvertFilter *)_filter; + const void *dest; + + if (audio_format_equals(&filter->in_audio_format, + &filter->out_audio_format)) { + /* optimized special case: no-op */ + *dest_size_r = src_size; + return src; + } + + dest = pcm_convert(&filter->state, &filter->in_audio_format, + src, src_size, + &filter->out_audio_format, dest_size_r, + error_r); + if (dest == NULL) + return NULL; + + return dest; +} + +const struct filter_plugin convert_filter_plugin = { + "convert", + convert_filter_init, + convert_filter_finish, + convert_filter_open, + convert_filter_close, + convert_filter_filter, +}; + +void +convert_filter_set(struct filter *_filter, + const struct audio_format *out_audio_format) +{ + ConvertFilter *filter = (ConvertFilter *)_filter; + + assert(filter != NULL); + assert(audio_format_valid(&filter->in_audio_format)); + assert(audio_format_valid(&filter->out_audio_format)); + assert(out_audio_format != NULL); + assert(audio_format_valid(out_audio_format)); + + filter->out_audio_format = *out_audio_format; +} diff --git a/src/filter/ConvertFilterPlugin.hxx b/src/filter/ConvertFilterPlugin.hxx new file mode 100644 index 00000000..15f0fa92 --- /dev/null +++ b/src/filter/ConvertFilterPlugin.hxx @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2003-2013 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_CONVERT_FILTER_PLUGIN_HXX +#define MPD_CONVERT_FILTER_PLUGIN_HXX + +struct filter; +struct audio_format; + +/** + * Sets the output audio format for the specified filter. You must + * call this after the filter has been opened. Since this audio + * format switch is a violation of the filter API, this filter must be + * the last in a chain. + */ +void +convert_filter_set(struct filter *filter, + const audio_format *out_audio_format); + +#endif diff --git a/src/filter/autoconvert_filter_plugin.c b/src/filter/autoconvert_filter_plugin.c deleted file mode 100644 index daa66052..00000000 --- a/src/filter/autoconvert_filter_plugin.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (C) 2003-2013 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 "config.h" -#include "filter/autoconvert_filter_plugin.h" -#include "filter/convert_filter_plugin.h" -#include "filter_plugin.h" -#include "filter_internal.h" -#include "filter_registry.h" -#include "audio_format.h" - -#include - -#include - -struct autoconvert_filter { - struct filter base; - - /** - * The audio format being fed to the underlying filter. This - * plugin actually doesn't need this variable, we have it here - * just so our open() method doesn't return a stack pointer. - */ - struct audio_format in_audio_format; - - /** - * The underlying filter. - */ - struct filter *filter; - - /** - * A convert_filter, just in case conversion is needed. NULL - * if unused. - */ - struct filter *convert; -}; - -static void -autoconvert_filter_finish(struct filter *_filter) -{ - struct autoconvert_filter *filter = - (struct autoconvert_filter *)_filter; - - filter_free(filter->filter); - g_free(filter); -} - -static const struct audio_format * -autoconvert_filter_open(struct filter *_filter, - struct audio_format *in_audio_format, - GError **error_r) -{ - struct autoconvert_filter *filter = - (struct autoconvert_filter *)_filter; - const struct audio_format *out_audio_format; - - assert(audio_format_valid(in_audio_format)); - - /* open the "real" filter */ - - filter->in_audio_format = *in_audio_format; - - out_audio_format = filter_open(filter->filter, - &filter->in_audio_format, error_r); - if (out_audio_format == NULL) - return NULL; - - /* need to convert? */ - - if (!audio_format_equals(&filter->in_audio_format, in_audio_format)) { - /* yes - create a convert_filter */ - struct audio_format audio_format2 = *in_audio_format; - const struct audio_format *audio_format3; - - filter->convert = filter_new(&convert_filter_plugin, NULL, - error_r); - if (filter->convert == NULL) { - filter_close(filter->filter); - return NULL; - } - - audio_format3 = filter_open(filter->convert, &audio_format2, - error_r); - if (audio_format3 == NULL) { - filter_free(filter->convert); - filter_close(filter->filter); - return NULL; - } - - assert(audio_format_equals(&audio_format2, in_audio_format)); - - convert_filter_set(filter->convert, &filter->in_audio_format); - } else - /* no */ - filter->convert = NULL; - - return out_audio_format; -} - -static void -autoconvert_filter_close(struct filter *_filter) -{ - struct autoconvert_filter *filter = - (struct autoconvert_filter *)_filter; - - if (filter->convert != NULL) { - filter_close(filter->convert); - filter_free(filter->convert); - } - - filter_close(filter->filter); -} - -static const void * -autoconvert_filter_filter(struct filter *_filter, const void *src, - size_t src_size, size_t *dest_size_r, - GError **error_r) -{ - struct autoconvert_filter *filter = - (struct autoconvert_filter *)_filter; - - if (filter->convert != NULL) { - src = filter_filter(filter->convert, src, src_size, &src_size, - error_r); - if (src == NULL) - return NULL; - } - - return filter_filter(filter->filter, src, src_size, dest_size_r, - error_r); -} - -static const struct filter_plugin autoconvert_filter_plugin = { - .name = "convert", - .finish = autoconvert_filter_finish, - .open = autoconvert_filter_open, - .close = autoconvert_filter_close, - .filter = autoconvert_filter_filter, -}; - -struct filter * -autoconvert_filter_new(struct filter *_filter) -{ - struct autoconvert_filter *filter = - g_new(struct autoconvert_filter, 1); - - filter_init(&filter->base, &autoconvert_filter_plugin); - filter->filter = _filter; - - return &filter->base; -} diff --git a/src/filter/autoconvert_filter_plugin.h b/src/filter/autoconvert_filter_plugin.h deleted file mode 100644 index def08ab7..00000000 --- a/src/filter/autoconvert_filter_plugin.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 AUTOCONVERT_FILTER_PLUGIN_H -#define AUTOCONVERT_FILTER_PLUGIN_H - -struct filter; - -/** - * Creates a new "autoconvert" filter. When opened, it ensures that - * the input audio format isn't changed. If the underlying filter - * requests a different format, it automatically creates a - * convert_filter. - */ -struct filter * -autoconvert_filter_new(struct filter *filter); - -#endif diff --git a/src/filter/convert_filter_plugin.c b/src/filter/convert_filter_plugin.c deleted file mode 100644 index c55b69af..00000000 --- a/src/filter/convert_filter_plugin.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "config.h" -#include "filter/convert_filter_plugin.h" -#include "filter_plugin.h" -#include "filter_internal.h" -#include "filter_registry.h" -#include "conf.h" -#include "pcm_convert.h" -#include "audio_format.h" -#include "poison.h" - -#include -#include - -struct convert_filter { - struct filter base; - - /** - * The current convert, from 0 to #PCM_CONVERT_1. - */ - unsigned convert; - - /** - * The input audio format; PCM data is passed to the filter() - * method in this format. - */ - struct audio_format in_audio_format; - - /** - * The output audio format; the consumer of this plugin - * expects PCM data in this format. This defaults to - * #in_audio_format, and can be set with convert_filter_set(). - */ - struct audio_format out_audio_format; - - struct pcm_convert_state state; -}; - -static struct filter * -convert_filter_init(G_GNUC_UNUSED const struct config_param *param, - G_GNUC_UNUSED GError **error_r) -{ - struct convert_filter *filter = g_new(struct convert_filter, 1); - - filter_init(&filter->base, &convert_filter_plugin); - return &filter->base; -} - -static void -convert_filter_finish(struct filter *filter) -{ - g_free(filter); -} - -static const struct audio_format * -convert_filter_open(struct filter *_filter, struct audio_format *audio_format, - G_GNUC_UNUSED GError **error_r) -{ - struct convert_filter *filter = (struct convert_filter *)_filter; - - assert(audio_format_valid(audio_format)); - - filter->in_audio_format = filter->out_audio_format = *audio_format; - pcm_convert_init(&filter->state); - - return &filter->in_audio_format; -} - -static void -convert_filter_close(struct filter *_filter) -{ - struct convert_filter *filter = (struct convert_filter *)_filter; - - pcm_convert_deinit(&filter->state); - - poison_undefined(&filter->in_audio_format, - sizeof(filter->in_audio_format)); - poison_undefined(&filter->out_audio_format, - sizeof(filter->out_audio_format)); -} - -static const void * -convert_filter_filter(struct filter *_filter, const void *src, size_t src_size, - size_t *dest_size_r, GError **error_r) -{ - struct convert_filter *filter = (struct convert_filter *)_filter; - const void *dest; - - if (audio_format_equals(&filter->in_audio_format, - &filter->out_audio_format)) { - /* optimized special case: no-op */ - *dest_size_r = src_size; - return src; - } - - dest = pcm_convert(&filter->state, &filter->in_audio_format, - src, src_size, - &filter->out_audio_format, dest_size_r, - error_r); - if (dest == NULL) - return NULL; - - return dest; -} - -const struct filter_plugin convert_filter_plugin = { - .name = "convert", - .init = convert_filter_init, - .finish = convert_filter_finish, - .open = convert_filter_open, - .close = convert_filter_close, - .filter = convert_filter_filter, -}; - -void -convert_filter_set(struct filter *_filter, - const struct audio_format *out_audio_format) -{ - struct convert_filter *filter = (struct convert_filter *)_filter; - - assert(filter != NULL); - assert(audio_format_valid(&filter->in_audio_format)); - assert(audio_format_valid(&filter->out_audio_format)); - assert(out_audio_format != NULL); - assert(audio_format_valid(out_audio_format)); - - filter->out_audio_format = *out_audio_format; -} diff --git a/src/filter/convert_filter_plugin.h b/src/filter/convert_filter_plugin.h deleted file mode 100644 index 156adf8e..00000000 --- a/src/filter/convert_filter_plugin.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 CONVERT_FILTER_PLUGIN_H -#define CONVERT_FILTER_PLUGIN_H - -struct filter; -struct audio_format; - -/** - * Sets the output audio format for the specified filter. You must - * call this after the filter has been opened. Since this audio - * format switch is a violation of the filter API, this filter must be - * the last in a chain. - */ -void -convert_filter_set(struct filter *filter, - const struct audio_format *out_audio_format); - -#endif -- cgit v1.2.3