From 7a3aac1843a9c84cd87512ef4e9bbc2def727591 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 30 Jul 2013 08:52:47 +0200 Subject: encoder/lame,twolame: convert to C++ --- src/encoder/LameEncoderPlugin.cxx | 303 ++++++++++++++++++++++++++++++++++ src/encoder/LameEncoderPlugin.hxx | 25 +++ src/encoder/TwolameEncoderPlugin.cxx | 311 +++++++++++++++++++++++++++++++++++ src/encoder/TwolameEncoderPlugin.hxx | 25 +++ src/encoder/lame_encoder.c | 303 ---------------------------------- src/encoder/twolame_encoder.c | 311 ----------------------------------- 6 files changed, 664 insertions(+), 614 deletions(-) create mode 100644 src/encoder/LameEncoderPlugin.cxx create mode 100644 src/encoder/LameEncoderPlugin.hxx create mode 100644 src/encoder/TwolameEncoderPlugin.cxx create mode 100644 src/encoder/TwolameEncoderPlugin.hxx delete mode 100644 src/encoder/lame_encoder.c delete mode 100644 src/encoder/twolame_encoder.c (limited to 'src/encoder') diff --git a/src/encoder/LameEncoderPlugin.cxx b/src/encoder/LameEncoderPlugin.cxx new file mode 100644 index 000000000..32fa7a323 --- /dev/null +++ b/src/encoder/LameEncoderPlugin.cxx @@ -0,0 +1,303 @@ +/* + * 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 "LameEncoderPlugin.hxx" +#include "encoder_api.h" +#include "encoder_plugin.h" +#include "audio_format.h" + +#include + +#include + +#include +#include + +struct LameEncoder final { + struct encoder encoder; + + struct audio_format audio_format; + float quality; + int bitrate; + + lame_global_flags *gfp; + + unsigned char buffer[32768]; + size_t buffer_length; + + LameEncoder() { + encoder_struct_init(&encoder, &lame_encoder_plugin); + } + + bool Configure(const config_param *param, GError **error); +}; + +static inline GQuark +lame_encoder_quark(void) +{ + return g_quark_from_static_string("lame_encoder"); +} + +bool +LameEncoder::Configure(const config_param *param, GError **error) +{ + const char *value; + char *endptr; + + value = config_get_block_string(param, "quality", nullptr); + if (value != nullptr) { + /* a quality was configured (VBR) */ + + quality = g_ascii_strtod(value, &endptr); + + if (*endptr != '\0' || quality < -1.0 || quality > 10.0) { + g_set_error(error, lame_encoder_quark(), 0, + "quality \"%s\" is not a number in the " + "range -1 to 10, line %i", + value, param->line); + return false; + } + + if (config_get_block_string(param, "bitrate", nullptr) != nullptr) { + g_set_error(error, lame_encoder_quark(), 0, + "quality and bitrate are " + "both defined (line %i)", + param->line); + return false; + } + } else { + /* a bit rate was configured */ + + value = config_get_block_string(param, "bitrate", nullptr); + if (value == nullptr) { + g_set_error(error, lame_encoder_quark(), 0, + "neither bitrate nor quality defined " + "at line %i", + param->line); + return false; + } + + quality = -2.0; + bitrate = g_ascii_strtoll(value, &endptr, 10); + + if (*endptr != '\0' || bitrate <= 0) { + g_set_error(error, lame_encoder_quark(), 0, + "bitrate at line %i should be a positive integer", + param->line); + return false; + } + } + + return true; +} + +static struct encoder * +lame_encoder_init(const struct config_param *param, GError **error_r) +{ + LameEncoder *encoder = new LameEncoder(); + + /* load configuration from "param" */ + if (!encoder->Configure(param, error_r)) { + /* configuration has failed, roll back and return error */ + delete encoder; + return nullptr; + } + + return &encoder->encoder; +} + +static void +lame_encoder_finish(struct encoder *_encoder) +{ + LameEncoder *encoder = (LameEncoder *)_encoder; + + /* the real liblame cleanup was already performed by + lame_encoder_close(), so no real work here */ + g_free(encoder); +} + +static bool +lame_encoder_setup(LameEncoder *encoder, GError **error) +{ + if (encoder->quality >= -1.0) { + /* a quality was configured (VBR) */ + + if (0 != lame_set_VBR(encoder->gfp, vbr_rh)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame VBR mode"); + return false; + } + if (0 != lame_set_VBR_q(encoder->gfp, encoder->quality)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame VBR quality"); + return false; + } + } else { + /* a bit rate was configured */ + + if (0 != lame_set_brate(encoder->gfp, encoder->bitrate)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame bitrate"); + return false; + } + } + + if (0 != lame_set_num_channels(encoder->gfp, + encoder->audio_format.channels)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame num channels"); + return false; + } + + if (0 != lame_set_in_samplerate(encoder->gfp, + encoder->audio_format.sample_rate)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame sample rate"); + return false; + } + + if (0 != lame_set_out_samplerate(encoder->gfp, + encoder->audio_format.sample_rate)) { + g_set_error(error, lame_encoder_quark(), 0, + "error setting lame out sample rate"); + return false; + } + + if (0 > lame_init_params(encoder->gfp)) { + g_set_error(error, lame_encoder_quark(), 0, + "error initializing lame params"); + return false; + } + + return true; +} + +static bool +lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, + GError **error) +{ + LameEncoder *encoder = (LameEncoder *)_encoder; + + audio_format->format = SAMPLE_FORMAT_S16; + audio_format->channels = 2; + + encoder->audio_format = *audio_format; + + encoder->gfp = lame_init(); + if (encoder->gfp == nullptr) { + g_set_error(error, lame_encoder_quark(), 0, + "lame_init() failed"); + return false; + } + + if (!lame_encoder_setup(encoder, error)) { + lame_close(encoder->gfp); + return false; + } + + encoder->buffer_length = 0; + + return true; +} + +static void +lame_encoder_close(struct encoder *_encoder) +{ + LameEncoder *encoder = (LameEncoder *)_encoder; + + lame_close(encoder->gfp); +} + +static bool +lame_encoder_write(struct encoder *_encoder, + const void *data, size_t length, + gcc_unused GError **error) +{ + LameEncoder *encoder = (LameEncoder *)_encoder; + const int16_t *src = (const int16_t*)data; + + assert(encoder->buffer_length == 0); + + const unsigned num_frames = + length / audio_format_frame_size(&encoder->audio_format); + float *left = g_new(float, num_frames); + float *right = g_new(float, num_frames); + + /* this is for only 16-bit audio */ + + for (unsigned i = 0; i < num_frames; i++) { + left[i] = *src++; + right[i] = *src++; + } + + int bytes_out = lame_encode_buffer_float(encoder->gfp, left, right, + num_frames, encoder->buffer, + sizeof(encoder->buffer)); + + g_free(left); + g_free(right); + + if (bytes_out < 0) { + g_set_error(error, lame_encoder_quark(), 0, + "lame encoder failed"); + return false; + } + + encoder->buffer_length = (size_t)bytes_out; + return true; +} + +static size_t +lame_encoder_read(struct encoder *_encoder, void *dest, size_t length) +{ + LameEncoder *encoder = (LameEncoder *)_encoder; + + if (length > encoder->buffer_length) + length = encoder->buffer_length; + + memcpy(dest, encoder->buffer, length); + + encoder->buffer_length -= length; + memmove(encoder->buffer, encoder->buffer + length, + encoder->buffer_length); + + return length; +} + +static const char * +lame_encoder_get_mime_type(gcc_unused struct encoder *_encoder) +{ + return "audio/mpeg"; +} + +const struct encoder_plugin lame_encoder_plugin = { + "lame", + lame_encoder_init, + lame_encoder_finish, + lame_encoder_open, + lame_encoder_close, + nullptr, + nullptr, + nullptr, + nullptr, + lame_encoder_write, + lame_encoder_read, + lame_encoder_get_mime_type, +}; diff --git a/src/encoder/LameEncoderPlugin.hxx b/src/encoder/LameEncoderPlugin.hxx new file mode 100644 index 000000000..3037ab4ec --- /dev/null +++ b/src/encoder/LameEncoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * 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_ENCODER_LAME_HXX +#define MPD_ENCODER_LAME_HXX + +extern const struct encoder_plugin lame_encoder_plugin; + +#endif diff --git a/src/encoder/TwolameEncoderPlugin.cxx b/src/encoder/TwolameEncoderPlugin.cxx new file mode 100644 index 000000000..a61426170 --- /dev/null +++ b/src/encoder/TwolameEncoderPlugin.cxx @@ -0,0 +1,311 @@ +/* + * 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 "TwolameEncoderPlugin.hxx" +#include "encoder_api.h" +#include "encoder_plugin.h" +#include "audio_format.h" + +#include + +#include + +#include +#include + +struct TwolameEncoder final { + struct encoder encoder; + + struct audio_format audio_format; + float quality; + int bitrate; + + twolame_options *options; + + unsigned char buffer[32768]; + size_t buffer_length; + + /** + * Call libtwolame's flush function when the buffer is empty? + */ + bool flush; + + TwolameEncoder() { + encoder_struct_init(&encoder, &twolame_encoder_plugin); + } + + bool Configure(const config_param *param, GError **error); +}; + +static inline GQuark +twolame_encoder_quark(void) +{ + return g_quark_from_static_string("twolame_encoder"); +} + +bool +TwolameEncoder::Configure(const config_param *param, GError **error) +{ + const char *value; + char *endptr; + + value = config_get_block_string(param, "quality", nullptr); + if (value != nullptr) { + /* a quality was configured (VBR) */ + + quality = g_ascii_strtod(value, &endptr); + + if (*endptr != '\0' || quality < -1.0 || quality > 10.0) { + g_set_error(error, twolame_encoder_quark(), 0, + "quality \"%s\" is not a number in the " + "range -1 to 10, line %i", + value, param->line); + return false; + } + + if (config_get_block_string(param, "bitrate", nullptr) != nullptr) { + g_set_error(error, twolame_encoder_quark(), 0, + "quality and bitrate are " + "both defined (line %i)", + param->line); + return false; + } + } else { + /* a bit rate was configured */ + + value = config_get_block_string(param, "bitrate", nullptr); + if (value == nullptr) { + g_set_error(error, twolame_encoder_quark(), 0, + "neither bitrate nor quality defined " + "at line %i", + param->line); + return false; + } + + quality = -2.0; + bitrate = g_ascii_strtoll(value, &endptr, 10); + + if (*endptr != '\0' || bitrate <= 0) { + g_set_error(error, twolame_encoder_quark(), 0, + "bitrate at line %i should be a positive integer", + param->line); + return false; + } + } + + return true; +} + +static struct encoder * +twolame_encoder_init(const struct config_param *param, GError **error_r) +{ + g_debug("libtwolame version %s", get_twolame_version()); + + TwolameEncoder *encoder = new TwolameEncoder(); + + /* load configuration from "param" */ + if (!encoder->Configure(param, error_r)) { + /* configuration has failed, roll back and return error */ + delete encoder; + return nullptr; + } + + return &encoder->encoder; +} + +static void +twolame_encoder_finish(struct encoder *_encoder) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + + /* the real libtwolame cleanup was already performed by + twolame_encoder_close(), so no real work here */ + delete encoder; +} + +static bool +twolame_encoder_setup(TwolameEncoder *encoder, GError **error) +{ + if (encoder->quality >= -1.0) { + /* a quality was configured (VBR) */ + + if (0 != twolame_set_VBR(encoder->options, true)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error setting twolame VBR mode"); + return false; + } + if (0 != twolame_set_VBR_q(encoder->options, encoder->quality)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error setting twolame VBR quality"); + return false; + } + } else { + /* a bit rate was configured */ + + if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error setting twolame bitrate"); + return false; + } + } + + if (0 != twolame_set_num_channels(encoder->options, + encoder->audio_format.channels)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error setting twolame num channels"); + return false; + } + + if (0 != twolame_set_in_samplerate(encoder->options, + encoder->audio_format.sample_rate)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error setting twolame sample rate"); + return false; + } + + if (0 > twolame_init_params(encoder->options)) { + g_set_error(error, twolame_encoder_quark(), 0, + "error initializing twolame params"); + return false; + } + + return true; +} + +static bool +twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, + GError **error) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + + audio_format->format = SAMPLE_FORMAT_S16; + audio_format->channels = 2; + + encoder->audio_format = *audio_format; + + encoder->options = twolame_init(); + if (encoder->options == nullptr) { + g_set_error(error, twolame_encoder_quark(), 0, + "twolame_init() failed"); + return false; + } + + if (!twolame_encoder_setup(encoder, error)) { + twolame_close(&encoder->options); + return false; + } + + encoder->buffer_length = 0; + encoder->flush = false; + + return true; +} + +static void +twolame_encoder_close(struct encoder *_encoder) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + + twolame_close(&encoder->options); +} + +static bool +twolame_encoder_flush(struct encoder *_encoder, gcc_unused GError **error) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + + encoder->flush = true; + return true; +} + +static bool +twolame_encoder_write(struct encoder *_encoder, + const void *data, size_t length, + gcc_unused GError **error) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + const int16_t *src = (const int16_t*)data; + + assert(encoder->buffer_length == 0); + + const unsigned num_frames = + length / audio_format_frame_size(&encoder->audio_format); + + int bytes_out = twolame_encode_buffer_interleaved(encoder->options, + src, num_frames, + encoder->buffer, + sizeof(encoder->buffer)); + if (bytes_out < 0) { + g_set_error(error, twolame_encoder_quark(), 0, + "twolame encoder failed"); + return false; + } + + encoder->buffer_length = (size_t)bytes_out; + return true; +} + +static size_t +twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length) +{ + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; + + if (encoder->buffer_length == 0 && encoder->flush) { + int ret = twolame_encode_flush(encoder->options, + encoder->buffer, + sizeof(encoder->buffer)); + if (ret > 0) + encoder->buffer_length = (size_t)ret; + + encoder->flush = false; + } + + if (length > encoder->buffer_length) + length = encoder->buffer_length; + + memcpy(dest, encoder->buffer, length); + + encoder->buffer_length -= length; + memmove(encoder->buffer, encoder->buffer + length, + encoder->buffer_length); + + return length; +} + +static const char * +twolame_encoder_get_mime_type(gcc_unused struct encoder *_encoder) +{ + return "audio/mpeg"; +} + +const struct encoder_plugin twolame_encoder_plugin = { + "twolame", + twolame_encoder_init, + twolame_encoder_finish, + twolame_encoder_open, + twolame_encoder_close, + twolame_encoder_flush, + twolame_encoder_flush, + nullptr, + nullptr, + twolame_encoder_write, + twolame_encoder_read, + twolame_encoder_get_mime_type, +}; diff --git a/src/encoder/TwolameEncoderPlugin.hxx b/src/encoder/TwolameEncoderPlugin.hxx new file mode 100644 index 000000000..585345172 --- /dev/null +++ b/src/encoder/TwolameEncoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * 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_ENCODER_TWOLAME_HXX +#define MPD_ENCODER_TWOLAME_HXX + +extern const struct encoder_plugin twolame_encoder_plugin; + +#endif diff --git a/src/encoder/lame_encoder.c b/src/encoder/lame_encoder.c deleted file mode 100644 index 53594d34e..000000000 --- a/src/encoder/lame_encoder.c +++ /dev/null @@ -1,303 +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 "encoder_api.h" -#include "encoder_plugin.h" -#include "audio_format.h" - -#include - -#include - -#include -#include - -struct lame_encoder { - struct encoder encoder; - - struct audio_format audio_format; - float quality; - int bitrate; - - lame_global_flags *gfp; - - unsigned char buffer[32768]; - size_t buffer_length; -}; - -extern const struct encoder_plugin lame_encoder_plugin; - -static inline GQuark -lame_encoder_quark(void) -{ - return g_quark_from_static_string("lame_encoder"); -} - -static bool -lame_encoder_configure(struct lame_encoder *encoder, - const struct config_param *param, GError **error) -{ - const char *value; - char *endptr; - - value = config_get_block_string(param, "quality", NULL); - if (value != NULL) { - /* a quality was configured (VBR) */ - - encoder->quality = g_ascii_strtod(value, &endptr); - - if (*endptr != '\0' || encoder->quality < -1.0 || - encoder->quality > 10.0) { - g_set_error(error, lame_encoder_quark(), 0, - "quality \"%s\" is not a number in the " - "range -1 to 10, line %i", - value, param->line); - return false; - } - - if (config_get_block_string(param, "bitrate", NULL) != NULL) { - g_set_error(error, lame_encoder_quark(), 0, - "quality and bitrate are " - "both defined (line %i)", - param->line); - return false; - } - } else { - /* a bit rate was configured */ - - value = config_get_block_string(param, "bitrate", NULL); - if (value == NULL) { - g_set_error(error, lame_encoder_quark(), 0, - "neither bitrate nor quality defined " - "at line %i", - param->line); - return false; - } - - encoder->quality = -2.0; - encoder->bitrate = g_ascii_strtoll(value, &endptr, 10); - - if (*endptr != '\0' || encoder->bitrate <= 0) { - g_set_error(error, lame_encoder_quark(), 0, - "bitrate at line %i should be a positive integer", - param->line); - return false; - } - } - - return true; -} - -static struct encoder * -lame_encoder_init(const struct config_param *param, GError **error) -{ - struct lame_encoder *encoder; - - encoder = g_new(struct lame_encoder, 1); - encoder_struct_init(&encoder->encoder, &lame_encoder_plugin); - - /* load configuration from "param" */ - if (!lame_encoder_configure(encoder, param, error)) { - /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; - } - - return &encoder->encoder; -} - -static void -lame_encoder_finish(struct encoder *_encoder) -{ - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - - /* the real liblame cleanup was already performed by - lame_encoder_close(), so no real work here */ - g_free(encoder); -} - -static bool -lame_encoder_setup(struct lame_encoder *encoder, GError **error) -{ - if (encoder->quality >= -1.0) { - /* a quality was configured (VBR) */ - - if (0 != lame_set_VBR(encoder->gfp, vbr_rh)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame VBR mode"); - return false; - } - if (0 != lame_set_VBR_q(encoder->gfp, encoder->quality)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame VBR quality"); - return false; - } - } else { - /* a bit rate was configured */ - - if (0 != lame_set_brate(encoder->gfp, encoder->bitrate)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame bitrate"); - return false; - } - } - - if (0 != lame_set_num_channels(encoder->gfp, - encoder->audio_format.channels)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame num channels"); - return false; - } - - if (0 != lame_set_in_samplerate(encoder->gfp, - encoder->audio_format.sample_rate)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame sample rate"); - return false; - } - - if (0 != lame_set_out_samplerate(encoder->gfp, - encoder->audio_format.sample_rate)) { - g_set_error(error, lame_encoder_quark(), 0, - "error setting lame out sample rate"); - return false; - } - - if (0 > lame_init_params(encoder->gfp)) { - g_set_error(error, lame_encoder_quark(), 0, - "error initializing lame params"); - return false; - } - - return true; -} - -static bool -lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, - GError **error) -{ - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - - audio_format->format = SAMPLE_FORMAT_S16; - audio_format->channels = 2; - - encoder->audio_format = *audio_format; - - encoder->gfp = lame_init(); - if (encoder->gfp == NULL) { - g_set_error(error, lame_encoder_quark(), 0, - "lame_init() failed"); - return false; - } - - if (!lame_encoder_setup(encoder, error)) { - lame_close(encoder->gfp); - return false; - } - - encoder->buffer_length = 0; - - return true; -} - -static void -lame_encoder_close(struct encoder *_encoder) -{ - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - - lame_close(encoder->gfp); -} - -static bool -lame_encoder_write(struct encoder *_encoder, - const void *data, size_t length, - gcc_unused GError **error) -{ - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - unsigned num_frames; - float *left, *right; - const int16_t *src = (const int16_t*)data; - unsigned int i; - int bytes_out; - - assert(encoder->buffer_length == 0); - - num_frames = - length / audio_format_frame_size(&encoder->audio_format); - left = g_malloc(sizeof(left[0]) * num_frames); - right = g_malloc(sizeof(right[0]) * num_frames); - - /* this is for only 16-bit audio */ - - for (i = 0; i < num_frames; i++) { - left[i] = *src++; - right[i] = *src++; - } - - bytes_out = lame_encode_buffer_float(encoder->gfp, left, right, - num_frames, encoder->buffer, - sizeof(encoder->buffer)); - - g_free(left); - g_free(right); - - if (bytes_out < 0) { - g_set_error(error, lame_encoder_quark(), 0, - "lame encoder failed"); - return false; - } - - encoder->buffer_length = (size_t)bytes_out; - return true; -} - -static size_t -lame_encoder_read(struct encoder *_encoder, void *dest, size_t length) -{ - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - - if (length > encoder->buffer_length) - length = encoder->buffer_length; - - memcpy(dest, encoder->buffer, length); - - encoder->buffer_length -= length; - memmove(encoder->buffer, encoder->buffer + length, - encoder->buffer_length); - - return length; -} - -static const char * -lame_encoder_get_mime_type(gcc_unused struct encoder *_encoder) -{ - return "audio/mpeg"; -} - -const struct encoder_plugin lame_encoder_plugin = { - .name = "lame", - .init = lame_encoder_init, - .finish = lame_encoder_finish, - .open = lame_encoder_open, - .close = lame_encoder_close, - .write = lame_encoder_write, - .read = lame_encoder_read, - .get_mime_type = lame_encoder_get_mime_type, -}; diff --git a/src/encoder/twolame_encoder.c b/src/encoder/twolame_encoder.c deleted file mode 100644 index ff184977b..000000000 --- a/src/encoder/twolame_encoder.c +++ /dev/null @@ -1,311 +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 "encoder_api.h" -#include "encoder_plugin.h" -#include "audio_format.h" - -#include - -#include - -#include -#include - -struct twolame_encoder { - struct encoder encoder; - - struct audio_format audio_format; - float quality; - int bitrate; - - twolame_options *options; - - unsigned char buffer[32768]; - size_t buffer_length; - - /** - * Call libtwolame's flush function when the buffer is empty? - */ - bool flush; -}; - -extern const struct encoder_plugin twolame_encoder_plugin; - -static inline GQuark -twolame_encoder_quark(void) -{ - return g_quark_from_static_string("twolame_encoder"); -} - -static bool -twolame_encoder_configure(struct twolame_encoder *encoder, - const struct config_param *param, GError **error) -{ - const char *value; - char *endptr; - - value = config_get_block_string(param, "quality", NULL); - if (value != NULL) { - /* a quality was configured (VBR) */ - - encoder->quality = g_ascii_strtod(value, &endptr); - - if (*endptr != '\0' || encoder->quality < -1.0 || - encoder->quality > 10.0) { - g_set_error(error, twolame_encoder_quark(), 0, - "quality \"%s\" is not a number in the " - "range -1 to 10, line %i", - value, param->line); - return false; - } - - if (config_get_block_string(param, "bitrate", NULL) != NULL) { - g_set_error(error, twolame_encoder_quark(), 0, - "quality and bitrate are " - "both defined (line %i)", - param->line); - return false; - } - } else { - /* a bit rate was configured */ - - value = config_get_block_string(param, "bitrate", NULL); - if (value == NULL) { - g_set_error(error, twolame_encoder_quark(), 0, - "neither bitrate nor quality defined " - "at line %i", - param->line); - return false; - } - - encoder->quality = -2.0; - encoder->bitrate = g_ascii_strtoll(value, &endptr, 10); - - if (*endptr != '\0' || encoder->bitrate <= 0) { - g_set_error(error, twolame_encoder_quark(), 0, - "bitrate at line %i should be a positive integer", - param->line); - return false; - } - } - - return true; -} - -static struct encoder * -twolame_encoder_init(const struct config_param *param, GError **error) -{ - struct twolame_encoder *encoder; - - g_debug("libtwolame version %s", get_twolame_version()); - - encoder = g_new(struct twolame_encoder, 1); - encoder_struct_init(&encoder->encoder, &twolame_encoder_plugin); - - /* load configuration from "param" */ - if (!twolame_encoder_configure(encoder, param, error)) { - /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; - } - - return &encoder->encoder; -} - -static void -twolame_encoder_finish(struct encoder *_encoder) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - - /* the real libtwolame cleanup was already performed by - twolame_encoder_close(), so no real work here */ - g_free(encoder); -} - -static bool -twolame_encoder_setup(struct twolame_encoder *encoder, GError **error) -{ - if (encoder->quality >= -1.0) { - /* a quality was configured (VBR) */ - - if (0 != twolame_set_VBR(encoder->options, true)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error setting twolame VBR mode"); - return false; - } - if (0 != twolame_set_VBR_q(encoder->options, encoder->quality)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error setting twolame VBR quality"); - return false; - } - } else { - /* a bit rate was configured */ - - if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error setting twolame bitrate"); - return false; - } - } - - if (0 != twolame_set_num_channels(encoder->options, - encoder->audio_format.channels)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error setting twolame num channels"); - return false; - } - - if (0 != twolame_set_in_samplerate(encoder->options, - encoder->audio_format.sample_rate)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error setting twolame sample rate"); - return false; - } - - if (0 > twolame_init_params(encoder->options)) { - g_set_error(error, twolame_encoder_quark(), 0, - "error initializing twolame params"); - return false; - } - - return true; -} - -static bool -twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, - GError **error) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - - audio_format->format = SAMPLE_FORMAT_S16; - audio_format->channels = 2; - - encoder->audio_format = *audio_format; - - encoder->options = twolame_init(); - if (encoder->options == NULL) { - g_set_error(error, twolame_encoder_quark(), 0, - "twolame_init() failed"); - return false; - } - - if (!twolame_encoder_setup(encoder, error)) { - twolame_close(&encoder->options); - return false; - } - - encoder->buffer_length = 0; - encoder->flush = false; - - return true; -} - -static void -twolame_encoder_close(struct encoder *_encoder) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - - twolame_close(&encoder->options); -} - -static bool -twolame_encoder_flush(struct encoder *_encoder, gcc_unused GError **error) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - - encoder->flush = true; - return true; -} - -static bool -twolame_encoder_write(struct encoder *_encoder, - const void *data, size_t length, - gcc_unused GError **error) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - unsigned num_frames; - const int16_t *src = (const int16_t*)data; - int bytes_out; - - assert(encoder->buffer_length == 0); - - num_frames = - length / audio_format_frame_size(&encoder->audio_format); - - bytes_out = twolame_encode_buffer_interleaved(encoder->options, - src, num_frames, - encoder->buffer, - sizeof(encoder->buffer)); - if (bytes_out < 0) { - g_set_error(error, twolame_encoder_quark(), 0, - "twolame encoder failed"); - return false; - } - - encoder->buffer_length = (size_t)bytes_out; - return true; -} - -static size_t -twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length) -{ - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - - if (encoder->buffer_length == 0 && encoder->flush) { - int ret = twolame_encode_flush(encoder->options, - encoder->buffer, - sizeof(encoder->buffer)); - if (ret > 0) - encoder->buffer_length = (size_t)ret; - - encoder->flush = false; - } - - if (length > encoder->buffer_length) - length = encoder->buffer_length; - - memcpy(dest, encoder->buffer, length); - - encoder->buffer_length -= length; - memmove(encoder->buffer, encoder->buffer + length, - encoder->buffer_length); - - return length; -} - -static const char * -twolame_encoder_get_mime_type(gcc_unused struct encoder *_encoder) -{ - return "audio/mpeg"; -} - -const struct encoder_plugin twolame_encoder_plugin = { - .name = "twolame", - .init = twolame_encoder_init, - .finish = twolame_encoder_finish, - .open = twolame_encoder_open, - .close = twolame_encoder_close, - .end = twolame_encoder_flush, - .flush = twolame_encoder_flush, - .write = twolame_encoder_write, - .read = twolame_encoder_read, - .get_mime_type = twolame_encoder_get_mime_type, -}; -- cgit v1.2.3