diff options
Diffstat (limited to 'src/encoder')
-rw-r--r-- | src/encoder/FlacEncoderPlugin.cxx (renamed from src/encoder/flac_encoder.c) | 175 | ||||
-rw-r--r-- | src/encoder/FlacEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/LameEncoderPlugin.cxx (renamed from src/encoder/lame_encoder.c) | 148 | ||||
-rw-r--r-- | src/encoder/LameEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/NullEncoderPlugin.cxx | 119 | ||||
-rw-r--r-- | src/encoder/NullEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/OggStream.hxx | 128 | ||||
-rw-r--r-- | src/encoder/OpusEncoderPlugin.cxx | 425 | ||||
-rw-r--r-- | src/encoder/OpusEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/TwolameEncoderPlugin.cxx (renamed from src/encoder/twolame_encoder.c) | 150 | ||||
-rw-r--r-- | src/encoder/TwolameEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/VorbisEncoderPlugin.cxx (renamed from src/encoder/vorbis_encoder.c) | 186 | ||||
-rw-r--r-- | src/encoder/VorbisEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/WaveEncoderPlugin.cxx (renamed from src/encoder/wave_encoder.c) | 122 | ||||
-rw-r--r-- | src/encoder/WaveEncoderPlugin.hxx | 25 | ||||
-rw-r--r-- | src/encoder/null_encoder.c | 120 |
16 files changed, 1211 insertions, 537 deletions
diff --git a/src/encoder/flac_encoder.c b/src/encoder/FlacEncoderPlugin.cxx index e32588e29..a14b97f11 100644 --- a/src/encoder/flac_encoder.c +++ b/src/encoder/FlacEncoderPlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,37 +18,43 @@ */ #include "config.h" -#include "encoder_api.h" -#include "encoder_plugin.h" -#include "audio_format.h" -#include "pcm_buffer.h" -#include "fifo_buffer.h" -#include "growing_fifo.h" +#include "FlacEncoderPlugin.hxx" +#include "EncoderAPI.hxx" +#include "AudioFormat.hxx" +#include "pcm/PcmBuffer.hxx" +#include "util/fifo_buffer.h" + +extern "C" { +#include "util/growing_fifo.h" +} #include <assert.h> #include <string.h> #include <FLAC/stream_encoder.h> +#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 +#error libFLAC is too old +#endif + struct flac_encoder { - struct encoder encoder; + Encoder encoder; - struct audio_format audio_format; + AudioFormat audio_format; unsigned compression; FLAC__StreamEncoder *fse; - struct pcm_buffer expand_buffer; + PcmBuffer expand_buffer; /** * This buffer will hold encoded data from libFLAC until it is * picked up with flac_encoder_read(). */ struct fifo_buffer *output_buffer; -}; - -extern const struct encoder_plugin flac_encoder_plugin; + flac_encoder():encoder(flac_encoder_plugin) {} +}; static inline GQuark flac_encoder_quark(void) @@ -57,49 +63,43 @@ flac_encoder_quark(void) } static bool -flac_encoder_configure(struct flac_encoder *encoder, - const struct config_param *param, G_GNUC_UNUSED GError **error) +flac_encoder_configure(struct flac_encoder *encoder, const config_param ¶m, + gcc_unused GError **error) { - encoder->compression = config_get_block_unsigned(param, - "compression", 5); + encoder->compression = param.GetBlockValue("compression", 5u); return true; } -static struct encoder * -flac_encoder_init(const struct config_param *param, GError **error) +static Encoder * +flac_encoder_init(const config_param ¶m, GError **error) { - struct flac_encoder *encoder; - - encoder = g_new(struct flac_encoder, 1); - encoder_struct_init(&encoder->encoder, &flac_encoder_plugin); + flac_encoder *encoder = new flac_encoder(); /* load configuration from "param" */ if (!flac_encoder_configure(encoder, param, error)) { /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; + delete encoder; + return nullptr; } return &encoder->encoder; } static void -flac_encoder_finish(struct encoder *_encoder) +flac_encoder_finish(Encoder *_encoder) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; /* the real libFLAC cleanup was already performed by flac_encoder_close(), so no real work here */ - g_free(encoder); + delete encoder; } static bool flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample, GError **error) { -#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 -#else if ( !FLAC__stream_encoder_set_compression_level(encoder->fse, encoder->compression)) { g_set_error(error, flac_encoder_quark(), 0, @@ -107,7 +107,7 @@ flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample, encoder->compression); return false; } -#endif + if ( !FLAC__stream_encoder_set_channels(encoder->fse, encoder->audio_format.channels)) { g_set_error(error, flac_encoder_quark(), 0, @@ -135,11 +135,7 @@ flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample, static FLAC__StreamEncoderWriteStatus flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse, const FLAC__byte data[], -#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 - unsigned bytes, -#else size_t bytes, -#endif G_GNUC_UNUSED unsigned samples, G_GNUC_UNUSED unsigned current_frame, void *client_data) { @@ -152,47 +148,47 @@ flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse, } static void -flac_encoder_close(struct encoder *_encoder) +flac_encoder_close(Encoder *_encoder) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; FLAC__stream_encoder_delete(encoder->fse); - pcm_buffer_deinit(&encoder->expand_buffer); + encoder->expand_buffer.Clear(); fifo_buffer_free(encoder->output_buffer); } static bool -flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, +flac_encoder_open(Encoder *_encoder, AudioFormat &audio_format, GError **error) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; unsigned bits_per_sample; - encoder->audio_format = *audio_format; + encoder->audio_format = audio_format; /* FIXME: flac should support 32bit as well */ - switch (audio_format->format) { - case SAMPLE_FORMAT_S8: + switch (audio_format.format) { + case SampleFormat::S8: bits_per_sample = 8; break; - case SAMPLE_FORMAT_S16: + case SampleFormat::S16: bits_per_sample = 16; break; - case SAMPLE_FORMAT_S24_P32: + case SampleFormat::S24_P32: bits_per_sample = 24; break; default: bits_per_sample = 24; - audio_format->format = SAMPLE_FORMAT_S24_P32; + audio_format.format = SampleFormat::S24_P32; } /* allocate the encoder */ encoder->fse = FLAC__stream_encoder_new(); - if (encoder->fse == NULL) { + if (encoder->fse == nullptr) { g_set_error(error, flac_encoder_quark(), 0, "flac_new() failed"); return false; @@ -203,36 +199,16 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, return false; } - pcm_buffer_init(&encoder->expand_buffer); - encoder->output_buffer = growing_fifo_new(); /* this immediately outputs data through callback */ -#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 - { - FLAC__StreamEncoderState init_status; - - FLAC__stream_encoder_set_write_callback(encoder->fse, - flac_write_callback); - - init_status = FLAC__stream_encoder_init(encoder->fse); - - if (init_status != FLAC__STREAM_ENCODER_OK) { - g_set_error(error, flac_encoder_quark(), 0, - "failed to initialize encoder: %s\n", - FLAC__StreamEncoderStateString[init_status]); - flac_encoder_close(_encoder); - return false; - } - } -#else { FLAC__StreamEncoderInitStatus init_status; init_status = FLAC__stream_encoder_init_stream(encoder->fse, flac_write_callback, - NULL, NULL, NULL, encoder); + nullptr, nullptr, nullptr, encoder); if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { g_set_error(error, flac_encoder_quark(), 0, @@ -242,14 +218,13 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, return false; } } -#endif return true; } static bool -flac_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error) +flac_encoder_flush(Encoder *_encoder, G_GNUC_UNUSED GError **error) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; @@ -276,45 +251,51 @@ pcm16_to_flac(int32_t *out, const int16_t *in, unsigned num_samples) } static bool -flac_encoder_write(struct encoder *_encoder, +flac_encoder_write(Encoder *_encoder, const void *data, size_t length, G_GNUC_UNUSED GError **error) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; unsigned num_frames, num_samples; void *exbuffer; - const void *buffer = NULL; + const void *buffer = nullptr; /* format conversion */ - num_frames = length / audio_format_frame_size(&encoder->audio_format); + num_frames = length / encoder->audio_format.GetFrameSize(); num_samples = num_frames * encoder->audio_format.channels; switch (encoder->audio_format.format) { - case SAMPLE_FORMAT_S8: - exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*4); - pcm8_to_flac(exbuffer, data, num_samples); + case SampleFormat::S8: + exbuffer = encoder->expand_buffer.Get(length * 4); + pcm8_to_flac((int32_t *)exbuffer, (const int8_t *)data, + num_samples); buffer = exbuffer; break; - case SAMPLE_FORMAT_S16: - exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*2); - pcm16_to_flac(exbuffer, data, num_samples); + case SampleFormat::S16: + exbuffer = encoder->expand_buffer.Get(length * 2); + pcm16_to_flac((int32_t *)exbuffer, (const int16_t *)data, + num_samples); buffer = exbuffer; break; - case SAMPLE_FORMAT_S24_P32: - case SAMPLE_FORMAT_S32: + case SampleFormat::S24_P32: + case SampleFormat::S32: /* nothing need to be done; format is the same for both mpd and libFLAC */ buffer = data; break; + + default: + gcc_unreachable(); } /* feed samples to encoder */ - if (!FLAC__stream_encoder_process_interleaved(encoder->fse, buffer, - num_frames)) { + if (!FLAC__stream_encoder_process_interleaved(encoder->fse, + (const FLAC__int32 *)buffer, + num_frames)) { g_set_error(error, flac_encoder_quark(), 0, "flac encoder process failed"); return false; @@ -324,14 +305,14 @@ flac_encoder_write(struct encoder *_encoder, } static size_t -flac_encoder_read(struct encoder *_encoder, void *dest, size_t length) +flac_encoder_read(Encoder *_encoder, void *dest, size_t length) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; size_t max_length; - const char *src = fifo_buffer_read(encoder->output_buffer, - &max_length); - if (src == NULL) + const char *src = (const char *) + fifo_buffer_read(encoder->output_buffer, &max_length); + if (src == nullptr) return 0; if (length > max_length) @@ -343,21 +324,23 @@ flac_encoder_read(struct encoder *_encoder, void *dest, size_t length) } static const char * -flac_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder) +flac_encoder_get_mime_type(G_GNUC_UNUSED Encoder *_encoder) { return "audio/flac"; } -const struct encoder_plugin flac_encoder_plugin = { - .name = "flac", - .init = flac_encoder_init, - .finish = flac_encoder_finish, - .open = flac_encoder_open, - .close = flac_encoder_close, - .end = flac_encoder_flush, - .flush = flac_encoder_flush, - .write = flac_encoder_write, - .read = flac_encoder_read, - .get_mime_type = flac_encoder_get_mime_type, +const EncoderPlugin flac_encoder_plugin = { + "flac", + flac_encoder_init, + flac_encoder_finish, + flac_encoder_open, + flac_encoder_close, + flac_encoder_flush, + flac_encoder_flush, + nullptr, + nullptr, + flac_encoder_write, + flac_encoder_read, + flac_encoder_get_mime_type, }; diff --git a/src/encoder/FlacEncoderPlugin.hxx b/src/encoder/FlacEncoderPlugin.hxx new file mode 100644 index 000000000..928a7f93e --- /dev/null +++ b/src/encoder/FlacEncoderPlugin.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_FLAC_HXX +#define MPD_ENCODER_FLAC_HXX + +extern const struct EncoderPlugin flac_encoder_plugin; + +#endif diff --git a/src/encoder/lame_encoder.c b/src/encoder/LameEncoderPlugin.cxx index 3bb99ea28..db93b93a3 100644 --- a/src/encoder/lame_encoder.c +++ b/src/encoder/LameEncoderPlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,18 +18,21 @@ */ #include "config.h" -#include "encoder_api.h" -#include "encoder_plugin.h" -#include "audio_format.h" +#include "LameEncoderPlugin.hxx" +#include "EncoderAPI.hxx" +#include "AudioFormat.hxx" #include <lame/lame.h> + +#include <glib.h> + #include <assert.h> #include <string.h> -struct lame_encoder { - struct encoder encoder; +struct LameEncoder final { + Encoder encoder; - struct audio_format audio_format; + AudioFormat audio_format; float quality; int bitrate; @@ -37,9 +40,11 @@ struct lame_encoder { unsigned char buffer[32768]; size_t buffer_length; -}; -extern const struct encoder_plugin lame_encoder_plugin; + LameEncoder():encoder(lame_encoder_plugin) {} + + bool Configure(const config_param ¶m, GError **error); +}; static inline GQuark lame_encoder_quark(void) @@ -47,54 +52,52 @@ 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) +bool +LameEncoder::Configure(const config_param ¶m, GError **error) { const char *value; char *endptr; - value = config_get_block_string(param, "quality", NULL); - if (value != NULL) { + value = param.GetBlockValue("quality"); + if (value != nullptr) { /* a quality was configured (VBR) */ - encoder->quality = g_ascii_strtod(value, &endptr); + quality = g_ascii_strtod(value, &endptr); - if (*endptr != '\0' || encoder->quality < -1.0 || - encoder->quality > 10.0) { + 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); + value, param.line); return false; } - if (config_get_block_string(param, "bitrate", NULL) != NULL) { + if (param.GetBlockValue("bitrate") != nullptr) { g_set_error(error, lame_encoder_quark(), 0, "quality and bitrate are " "both defined (line %i)", - param->line); + param.line); return false; } } else { /* a bit rate was configured */ - value = config_get_block_string(param, "bitrate", NULL); - if (value == NULL) { + value = param.GetBlockValue("bitrate"); + if (value == nullptr) { g_set_error(error, lame_encoder_quark(), 0, "neither bitrate nor quality defined " "at line %i", - param->line); + param.line); return false; } - encoder->quality = -2.0; - encoder->bitrate = g_ascii_strtoll(value, &endptr, 10); + quality = -2.0; + bitrate = g_ascii_strtoll(value, &endptr, 10); - if (*endptr != '\0' || encoder->bitrate <= 0) { + if (*endptr != '\0' || bitrate <= 0) { g_set_error(error, lame_encoder_quark(), 0, "bitrate at line %i should be a positive integer", - param->line); + param.line); return false; } } @@ -102,28 +105,25 @@ lame_encoder_configure(struct lame_encoder *encoder, return true; } -static struct encoder * -lame_encoder_init(const struct config_param *param, GError **error) +static Encoder * +lame_encoder_init(const config_param ¶m, GError **error_r) { - struct lame_encoder *encoder; - - encoder = g_new(struct lame_encoder, 1); - encoder_struct_init(&encoder->encoder, &lame_encoder_plugin); + LameEncoder *encoder = new LameEncoder(); /* load configuration from "param" */ - if (!lame_encoder_configure(encoder, param, error)) { + if (!encoder->Configure(param, error_r)) { /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; + delete encoder; + return nullptr; } return &encoder->encoder; } static void -lame_encoder_finish(struct encoder *_encoder) +lame_encoder_finish(Encoder *_encoder) { - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; + LameEncoder *encoder = (LameEncoder *)_encoder; /* the real liblame cleanup was already performed by lame_encoder_close(), so no real work here */ @@ -131,7 +131,7 @@ lame_encoder_finish(struct encoder *_encoder) } static bool -lame_encoder_setup(struct lame_encoder *encoder, GError **error) +lame_encoder_setup(LameEncoder *encoder, GError **error) { if (encoder->quality >= -1.0) { /* a quality was configured (VBR) */ @@ -187,18 +187,18 @@ lame_encoder_setup(struct lame_encoder *encoder, GError **error) } static bool -lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, +lame_encoder_open(Encoder *_encoder, AudioFormat &audio_format, GError **error) { - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; + LameEncoder *encoder = (LameEncoder *)_encoder; - audio_format->format = SAMPLE_FORMAT_S16; - audio_format->channels = 2; + audio_format.format = SampleFormat::S16; + audio_format.channels = 2; - encoder->audio_format = *audio_format; + encoder->audio_format = audio_format; encoder->gfp = lame_init(); - if (encoder->gfp == NULL) { + if (encoder->gfp == nullptr) { g_set_error(error, lame_encoder_quark(), 0, "lame_init() failed"); return false; @@ -215,42 +215,38 @@ lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, } static void -lame_encoder_close(struct encoder *_encoder) +lame_encoder_close(Encoder *_encoder) { - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; + LameEncoder *encoder = (LameEncoder *)_encoder; lame_close(encoder->gfp); } static bool -lame_encoder_write(struct encoder *_encoder, +lame_encoder_write(Encoder *_encoder, const void *data, size_t length, - G_GNUC_UNUSED GError **error) + gcc_unused GError **error) { - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; - unsigned num_frames; - float *left, *right; + LameEncoder *encoder = (LameEncoder *)_encoder; 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); + const unsigned num_frames = + length / encoder->audio_format.GetFrameSize(); + float *left = g_new(float, num_frames); + float *right = g_new(float, num_frames); /* this is for only 16-bit audio */ - for (i = 0; i < num_frames; i++) { + for (unsigned 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)); + int bytes_out = lame_encode_buffer_float(encoder->gfp, left, right, + num_frames, encoder->buffer, + sizeof(encoder->buffer)); g_free(left); g_free(right); @@ -266,9 +262,9 @@ lame_encoder_write(struct encoder *_encoder, } static size_t -lame_encoder_read(struct encoder *_encoder, void *dest, size_t length) +lame_encoder_read(Encoder *_encoder, void *dest, size_t length) { - struct lame_encoder *encoder = (struct lame_encoder *)_encoder; + LameEncoder *encoder = (LameEncoder *)_encoder; if (length > encoder->buffer_length) length = encoder->buffer_length; @@ -283,18 +279,22 @@ lame_encoder_read(struct encoder *_encoder, void *dest, size_t length) } static const char * -lame_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder) +lame_encoder_get_mime_type(gcc_unused 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, +const EncoderPlugin 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..49832baee --- /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 EncoderPlugin lame_encoder_plugin; + +#endif diff --git a/src/encoder/NullEncoderPlugin.cxx b/src/encoder/NullEncoderPlugin.cxx new file mode 100644 index 000000000..5c01fbd98 --- /dev/null +++ b/src/encoder/NullEncoderPlugin.cxx @@ -0,0 +1,119 @@ +/* + * 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 "NullEncoderPlugin.hxx" +#include "EncoderAPI.hxx" +#include "util/fifo_buffer.h" +extern "C" { +#include "util/growing_fifo.h" +} +#include "gcc.h" + +#include <glib.h> + +#include <assert.h> +#include <string.h> + +struct NullEncoder final { + Encoder encoder; + + struct fifo_buffer *buffer; + + NullEncoder():encoder(null_encoder_plugin) {} +}; + +static Encoder * +null_encoder_init(gcc_unused const config_param ¶m, + gcc_unused GError **error) +{ + NullEncoder *encoder = new NullEncoder(); + return &encoder->encoder; +} + +static void +null_encoder_finish(Encoder *_encoder) +{ + NullEncoder *encoder = (NullEncoder *)_encoder; + + delete encoder; +} + +static void +null_encoder_close(Encoder *_encoder) +{ + NullEncoder *encoder = (NullEncoder *)_encoder; + + fifo_buffer_free(encoder->buffer); +} + + +static bool +null_encoder_open(Encoder *_encoder, + gcc_unused AudioFormat &audio_format, + gcc_unused GError **error) +{ + NullEncoder *encoder = (NullEncoder *)_encoder; + encoder->buffer = growing_fifo_new(); + return true; +} + +static bool +null_encoder_write(Encoder *_encoder, + const void *data, size_t length, + gcc_unused GError **error) +{ + NullEncoder *encoder = (NullEncoder *)_encoder; + + growing_fifo_append(&encoder->buffer, data, length); + return length; +} + +static size_t +null_encoder_read(Encoder *_encoder, void *dest, size_t length) +{ + NullEncoder *encoder = (NullEncoder *)_encoder; + + size_t max_length; + const void *src = fifo_buffer_read(encoder->buffer, &max_length); + if (src == nullptr) + return 0; + + if (length > max_length) + length = max_length; + + memcpy(dest, src, length); + fifo_buffer_consume(encoder->buffer, length); + return length; +} + +const EncoderPlugin null_encoder_plugin = { + "null", + null_encoder_init, + null_encoder_finish, + null_encoder_open, + null_encoder_close, + nullptr, + nullptr, + nullptr, + nullptr, + null_encoder_write, + null_encoder_read, + nullptr, +}; diff --git a/src/encoder/NullEncoderPlugin.hxx b/src/encoder/NullEncoderPlugin.hxx new file mode 100644 index 000000000..b741a2f6d --- /dev/null +++ b/src/encoder/NullEncoderPlugin.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_NULL_HXX +#define MPD_ENCODER_NULL_HXX + +extern const struct EncoderPlugin null_encoder_plugin; + +#endif diff --git a/src/encoder/OggStream.hxx b/src/encoder/OggStream.hxx new file mode 100644 index 000000000..ce847f491 --- /dev/null +++ b/src/encoder/OggStream.hxx @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2003-2012 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_OGG_STREAM_HXX +#define MPD_OGG_STREAM_HXX + +#include "check.h" + +#include <ogg/ogg.h> + +#include <assert.h> +#include <string.h> +#include <stdint.h> + +class OggStream { + ogg_stream_state state; + + bool flush; + +#ifndef NDEBUG + bool initialized; +#endif + +public: +#ifndef NDEBUG + OggStream():initialized(false) {} + ~OggStream() { + assert(!initialized); + } +#endif + + void Initialize(int serialno) { + assert(!initialized); + + ogg_stream_init(&state, serialno); + + /* set "flush" to true, so the caller gets the full + headers on the first read() */ + flush = true; + +#ifndef NDEBUG + initialized = true; +#endif + } + + void Reinitialize(int serialno) { + assert(initialized); + + ogg_stream_reset_serialno(&state, serialno); + + /* set "flush" to true, so the caller gets the full + headers on the first read() */ + flush = true; + } + + void Deinitialize() { + assert(initialized); + + ogg_stream_clear(&state); + +#ifndef NDEBUG + initialized = false; +#endif + } + + void Flush() { + assert(initialized); + + flush = true; + } + + void PacketIn(const ogg_packet &packet) { + assert(initialized); + + ogg_stream_packetin(&state, + const_cast<ogg_packet *>(&packet)); + } + + bool PageOut(ogg_page &page) { + int result = ogg_stream_pageout(&state, &page); + if (result == 0 && flush) { + flush = false; + result = ogg_stream_flush(&state, &page); + } + + return result != 0; + } + + size_t PageOut(void *_buffer, size_t size) { + ogg_page page; + if (!PageOut(page)) + return 0; + + assert(page.header_len > 0 || page.body_len > 0); + + size_t header_len = (size_t)page.header_len; + size_t body_len = (size_t)page.body_len; + assert(header_len <= size); + + if (header_len + body_len > size) + /* TODO: better overflow handling */ + body_len = size - header_len; + + uint8_t *buffer = (uint8_t *)_buffer; + memcpy(buffer, page.header, header_len); + memcpy(buffer + header_len, page.body, body_len); + + return header_len + body_len; + } +}; + +#endif diff --git a/src/encoder/OpusEncoderPlugin.cxx b/src/encoder/OpusEncoderPlugin.cxx new file mode 100644 index 000000000..d67cf1862 --- /dev/null +++ b/src/encoder/OpusEncoderPlugin.cxx @@ -0,0 +1,425 @@ +/* + * Copyright (C) 2003-2012 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 "OpusEncoderPlugin.hxx" +#include "OggStream.hxx" +#include "EncoderAPI.hxx" +#include "AudioFormat.hxx" +#include "mpd_error.h" + +#include <opus.h> +#include <ogg/ogg.h> + +#include <assert.h> + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "opus_encoder" + +struct opus_encoder { + /** the base class */ + Encoder encoder; + + /* configuration */ + + opus_int32 bitrate; + int complexity; + int signal; + + /* runtime information */ + + AudioFormat audio_format; + + size_t frame_size; + + size_t buffer_frames, buffer_size, buffer_position; + uint8_t *buffer; + + OpusEncoder *enc; + + unsigned char buffer2[1275 * 3 + 7]; + + OggStream stream; + + int lookahead; + + ogg_int64_t packetno; + + ogg_int64_t granulepos; + + opus_encoder():encoder(opus_encoder_plugin) {} +}; + +gcc_const +static inline GQuark +opus_encoder_quark(void) +{ + return g_quark_from_static_string("opus_encoder"); +} + +static bool +opus_encoder_configure(struct opus_encoder *encoder, + const config_param ¶m, GError **error_r) +{ + const char *value = param.GetBlockValue("bitrate", "auto"); + if (strcmp(value, "auto") == 0) + encoder->bitrate = OPUS_AUTO; + else if (strcmp(value, "max") == 0) + encoder->bitrate = OPUS_BITRATE_MAX; + else { + char *endptr; + encoder->bitrate = strtoul(value, &endptr, 10); + if (endptr == value || *endptr != 0 || + encoder->bitrate < 500 || encoder->bitrate > 512000) { + g_set_error(error_r, opus_encoder_quark(), 0, + "Invalid bit rate"); + return false; + } + } + + encoder->complexity = param.GetBlockValue("complexity", 10u); + if (encoder->complexity > 10) { + g_set_error(error_r, opus_encoder_quark(), 0, + "Invalid complexity"); + return false; + } + + value = param.GetBlockValue("signal", "auto"); + if (strcmp(value, "auto") == 0) + encoder->signal = OPUS_AUTO; + else if (strcmp(value, "voice") == 0) + encoder->signal = OPUS_SIGNAL_VOICE; + else if (strcmp(value, "music") == 0) + encoder->signal = OPUS_SIGNAL_MUSIC; + else { + g_set_error(error_r, opus_encoder_quark(), 0, + "Invalid signal"); + return false; + } + + return true; +} + +static Encoder * +opus_encoder_init(const config_param ¶m, GError **error) +{ + opus_encoder *encoder = new opus_encoder(); + + /* load configuration from "param" */ + if (!opus_encoder_configure(encoder, param, error)) { + /* configuration has failed, roll back and return error */ + delete encoder; + return NULL; + } + + return &encoder->encoder; +} + +static void +opus_encoder_finish(Encoder *_encoder) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + /* the real libopus cleanup was already performed by + opus_encoder_close(), so no real work here */ + delete encoder; +} + +static bool +opus_encoder_open(Encoder *_encoder, + AudioFormat &audio_format, + GError **error_r) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + /* libopus supports only 48 kHz */ + audio_format.sample_rate = 48000; + + if (audio_format.channels > 2) + audio_format.channels = 1; + + switch (audio_format.format) { + case SampleFormat::S16: + case SampleFormat::FLOAT: + break; + + case SampleFormat::S8: + audio_format.format = SampleFormat::S16; + break; + + default: + audio_format.format = SampleFormat::FLOAT; + break; + } + + encoder->audio_format = audio_format; + encoder->frame_size = audio_format.GetFrameSize(); + + int error; + encoder->enc = opus_encoder_create(audio_format.sample_rate, + audio_format.channels, + OPUS_APPLICATION_AUDIO, + &error); + if (encoder->enc == nullptr) { + g_set_error_literal(error_r, opus_encoder_quark(), error, + opus_strerror(error)); + return false; + } + + opus_encoder_ctl(encoder->enc, OPUS_SET_BITRATE(encoder->bitrate)); + opus_encoder_ctl(encoder->enc, + OPUS_SET_COMPLEXITY(encoder->complexity)); + opus_encoder_ctl(encoder->enc, OPUS_SET_SIGNAL(encoder->signal)); + + opus_encoder_ctl(encoder->enc, OPUS_GET_LOOKAHEAD(&encoder->lookahead)); + + encoder->buffer_frames = audio_format.sample_rate / 50; + encoder->buffer_size = encoder->frame_size * encoder->buffer_frames; + encoder->buffer_position = 0; + encoder->buffer = (unsigned char *)g_malloc(encoder->buffer_size); + + encoder->stream.Initialize(g_random_int()); + encoder->packetno = 0; + + return true; +} + +static void +opus_encoder_close(Encoder *_encoder) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + encoder->stream.Deinitialize(); + g_free(encoder->buffer); + opus_encoder_destroy(encoder->enc); +} + +static bool +opus_encoder_do_encode(struct opus_encoder *encoder, bool eos, + GError **error_r) +{ + assert(encoder->buffer_position == encoder->buffer_size); + + opus_int32 result = + encoder->audio_format.format == SampleFormat::S16 + ? opus_encode(encoder->enc, + (const opus_int16 *)encoder->buffer, + encoder->buffer_frames, + encoder->buffer2, + sizeof(encoder->buffer2)) + : opus_encode_float(encoder->enc, + (const float *)encoder->buffer, + encoder->buffer_frames, + encoder->buffer2, + sizeof(encoder->buffer2)); + if (result < 0) { + g_set_error_literal(error_r, opus_encoder_quark(), 0, + "Opus encoder error"); + return false; + } + + encoder->granulepos += encoder->buffer_frames; + + ogg_packet packet; + packet.packet = encoder->buffer2; + packet.bytes = result; + packet.b_o_s = false; + packet.e_o_s = eos; + packet.granulepos = encoder->granulepos; + packet.packetno = encoder->packetno++; + encoder->stream.PacketIn(packet); + + encoder->buffer_position = 0; + + return true; +} + +static bool +opus_encoder_end(Encoder *_encoder, GError **error_r) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + encoder->stream.Flush(); + + memset(encoder->buffer + encoder->buffer_position, 0, + encoder->buffer_size - encoder->buffer_position); + encoder->buffer_position = encoder->buffer_size; + + return opus_encoder_do_encode(encoder, true, error_r); +} + +static bool +opus_encoder_flush(Encoder *_encoder, G_GNUC_UNUSED GError **error) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + encoder->stream.Flush(); + return true; +} + +static bool +opus_encoder_write_silence(struct opus_encoder *encoder, unsigned fill_frames, + GError **error_r) +{ + size_t fill_bytes = fill_frames * encoder->frame_size; + + while (fill_bytes > 0) { + size_t nbytes = + encoder->buffer_size - encoder->buffer_position; + if (nbytes > fill_bytes) + nbytes = fill_bytes; + + memset(encoder->buffer + encoder->buffer_position, + 0, nbytes); + encoder->buffer_position += nbytes; + fill_bytes -= nbytes; + + if (encoder->buffer_position == encoder->buffer_size && + !opus_encoder_do_encode(encoder, false, error_r)) + return false; + } + + return true; +} + +static bool +opus_encoder_write(Encoder *_encoder, + const void *_data, size_t length, + GError **error_r) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + const uint8_t *data = (const uint8_t *)_data; + + if (encoder->lookahead > 0) { + /* generate some silence at the beginning of the + stream */ + + assert(encoder->buffer_position == 0); + + if (!opus_encoder_write_silence(encoder, encoder->lookahead, + error_r)) + return false; + + encoder->lookahead = 0; + } + + while (length > 0) { + size_t nbytes = + encoder->buffer_size - encoder->buffer_position; + if (nbytes > length) + nbytes = length; + + memcpy(encoder->buffer + encoder->buffer_position, + data, nbytes); + data += nbytes; + length -= nbytes; + encoder->buffer_position += nbytes; + + if (encoder->buffer_position == encoder->buffer_size && + !opus_encoder_do_encode(encoder, false, error_r)) + return false; + } + + return true; +} + +static void +opus_encoder_generate_head(struct opus_encoder *encoder) +{ + unsigned char header[19]; + memcpy(header, "OpusHead", 8); + header[8] = 1; + header[9] = encoder->audio_format.channels; + *(uint16_t *)(header + 10) = GUINT16_TO_LE(encoder->lookahead); + *(uint32_t *)(header + 12) = + GUINT32_TO_LE(encoder->audio_format.sample_rate); + header[16] = 0; + header[17] = 0; + header[18] = 0; + + ogg_packet packet; + packet.packet = header; + packet.bytes = 19; + packet.b_o_s = true; + packet.e_o_s = false; + packet.granulepos = 0; + packet.packetno = encoder->packetno++; + encoder->stream.PacketIn(packet); + encoder->stream.Flush(); +} + +static void +opus_encoder_generate_tags(struct opus_encoder *encoder) +{ + const char *version = opus_get_version_string(); + size_t version_length = strlen(version); + + size_t comments_size = 8 + 4 + version_length + 4; + unsigned char *comments = (unsigned char *)g_malloc(comments_size); + memcpy(comments, "OpusTags", 8); + *(uint32_t *)(comments + 8) = GUINT32_TO_LE(version_length); + memcpy(comments + 12, version, version_length); + *(uint32_t *)(comments + 12 + version_length) = GUINT32_TO_LE(0); + + ogg_packet packet; + packet.packet = comments; + packet.bytes = comments_size; + packet.b_o_s = false; + packet.e_o_s = false; + packet.granulepos = 0; + packet.packetno = encoder->packetno++; + encoder->stream.PacketIn(packet); + encoder->stream.Flush(); + + g_free(comments); +} + +static size_t +opus_encoder_read(Encoder *_encoder, void *dest, size_t length) +{ + struct opus_encoder *encoder = (struct opus_encoder *)_encoder; + + if (encoder->packetno == 0) + opus_encoder_generate_head(encoder); + else if (encoder->packetno == 1) + opus_encoder_generate_tags(encoder); + + return encoder->stream.PageOut(dest, length); +} + +static const char * +opus_encoder_get_mime_type(G_GNUC_UNUSED Encoder *_encoder) +{ + return "audio/ogg"; +} + +const EncoderPlugin opus_encoder_plugin = { + "opus", + opus_encoder_init, + opus_encoder_finish, + opus_encoder_open, + opus_encoder_close, + opus_encoder_end, + opus_encoder_flush, + nullptr, + nullptr, + opus_encoder_write, + opus_encoder_read, + opus_encoder_get_mime_type, +}; diff --git a/src/encoder/OpusEncoderPlugin.hxx b/src/encoder/OpusEncoderPlugin.hxx new file mode 100644 index 000000000..d6da0e960 --- /dev/null +++ b/src/encoder/OpusEncoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2012 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_OPUS_H +#define MPD_ENCODER_OPUS_H + +extern const struct EncoderPlugin opus_encoder_plugin; + +#endif diff --git a/src/encoder/twolame_encoder.c b/src/encoder/TwolameEncoderPlugin.cxx index 934b2ab24..cd85e43b6 100644 --- a/src/encoder/twolame_encoder.c +++ b/src/encoder/TwolameEncoderPlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,18 +18,21 @@ */ #include "config.h" -#include "encoder_api.h" -#include "encoder_plugin.h" -#include "audio_format.h" +#include "TwolameEncoderPlugin.hxx" +#include "EncoderAPI.hxx" +#include "AudioFormat.hxx" #include <twolame.h> + +#include <glib.h> + #include <assert.h> #include <string.h> -struct twolame_encoder { - struct encoder encoder; +struct TwolameEncoder final { + Encoder encoder; - struct audio_format audio_format; + AudioFormat audio_format; float quality; int bitrate; @@ -42,9 +45,11 @@ struct twolame_encoder { * Call libtwolame's flush function when the buffer is empty? */ bool flush; -}; -extern const struct encoder_plugin twolame_encoder_plugin; + TwolameEncoder():encoder(twolame_encoder_plugin) {} + + bool Configure(const config_param ¶m, GError **error); +}; static inline GQuark twolame_encoder_quark(void) @@ -52,54 +57,52 @@ 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) +bool +TwolameEncoder::Configure(const config_param ¶m, GError **error) { const char *value; char *endptr; - value = config_get_block_string(param, "quality", NULL); - if (value != NULL) { + value = param.GetBlockValue("quality"); + if (value != nullptr) { /* a quality was configured (VBR) */ - encoder->quality = g_ascii_strtod(value, &endptr); + quality = g_ascii_strtod(value, &endptr); - if (*endptr != '\0' || encoder->quality < -1.0 || - encoder->quality > 10.0) { + 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); + value, param.line); return false; } - if (config_get_block_string(param, "bitrate", NULL) != NULL) { + if (param.GetBlockValue("bitrate") != nullptr) { g_set_error(error, twolame_encoder_quark(), 0, "quality and bitrate are " "both defined (line %i)", - param->line); + param.line); return false; } } else { /* a bit rate was configured */ - value = config_get_block_string(param, "bitrate", NULL); - if (value == NULL) { + value = param.GetBlockValue("bitrate"); + if (value == nullptr) { g_set_error(error, twolame_encoder_quark(), 0, "neither bitrate nor quality defined " "at line %i", - param->line); + param.line); return false; } - encoder->quality = -2.0; - encoder->bitrate = g_ascii_strtoll(value, &endptr, 10); + quality = -2.0; + bitrate = g_ascii_strtoll(value, &endptr, 10); - if (*endptr != '\0' || encoder->bitrate <= 0) { + if (*endptr != '\0' || bitrate <= 0) { g_set_error(error, twolame_encoder_quark(), 0, "bitrate at line %i should be a positive integer", - param->line); + param.line); return false; } } @@ -107,38 +110,35 @@ twolame_encoder_configure(struct twolame_encoder *encoder, return true; } -static struct encoder * -twolame_encoder_init(const struct config_param *param, GError **error) +static Encoder * +twolame_encoder_init(const config_param ¶m, GError **error_r) { - 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); + TwolameEncoder *encoder = new TwolameEncoder(); /* load configuration from "param" */ - if (!twolame_encoder_configure(encoder, param, error)) { + if (!encoder->Configure(param, error_r)) { /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; + delete encoder; + return nullptr; } return &encoder->encoder; } static void -twolame_encoder_finish(struct encoder *_encoder) +twolame_encoder_finish(Encoder *_encoder) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; /* the real libtwolame cleanup was already performed by twolame_encoder_close(), so no real work here */ - g_free(encoder); + delete encoder; } static bool -twolame_encoder_setup(struct twolame_encoder *encoder, GError **error) +twolame_encoder_setup(TwolameEncoder *encoder, GError **error) { if (encoder->quality >= -1.0) { /* a quality was configured (VBR) */ @@ -187,18 +187,18 @@ twolame_encoder_setup(struct twolame_encoder *encoder, GError **error) } static bool -twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, +twolame_encoder_open(Encoder *_encoder, AudioFormat &audio_format, GError **error) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; - audio_format->format = SAMPLE_FORMAT_S16; - audio_format->channels = 2; + audio_format.format = SampleFormat::S16; + audio_format.channels = 2; - encoder->audio_format = *audio_format; + encoder->audio_format = audio_format; encoder->options = twolame_init(); - if (encoder->options == NULL) { + if (encoder->options == nullptr) { g_set_error(error, twolame_encoder_quark(), 0, "twolame_init() failed"); return false; @@ -216,41 +216,39 @@ twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format } static void -twolame_encoder_close(struct encoder *_encoder) +twolame_encoder_close(Encoder *_encoder) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; twolame_close(&encoder->options); } static bool -twolame_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error) +twolame_encoder_flush(Encoder *_encoder, gcc_unused GError **error) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; encoder->flush = true; return true; } static bool -twolame_encoder_write(struct encoder *_encoder, +twolame_encoder_write(Encoder *_encoder, const void *data, size_t length, - G_GNUC_UNUSED GError **error) + gcc_unused GError **error) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; - unsigned num_frames; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; 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); + const unsigned num_frames = + length / encoder->audio_format.GetFrameSize(); - bytes_out = twolame_encode_buffer_interleaved(encoder->options, - src, num_frames, - encoder->buffer, - sizeof(encoder->buffer)); + 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"); @@ -262,9 +260,9 @@ twolame_encoder_write(struct encoder *_encoder, } static size_t -twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length) +twolame_encoder_read(Encoder *_encoder, void *dest, size_t length) { - struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder; + TwolameEncoder *encoder = (TwolameEncoder *)_encoder; if (encoder->buffer_length == 0 && encoder->flush) { int ret = twolame_encode_flush(encoder->options, @@ -289,20 +287,22 @@ twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length) } static const char * -twolame_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder) +twolame_encoder_get_mime_type(gcc_unused 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, +const EncoderPlugin 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..dd8a536f6 --- /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 EncoderPlugin twolame_encoder_plugin; + +#endif diff --git a/src/encoder/vorbis_encoder.c b/src/encoder/VorbisEncoderPlugin.cxx index 468cf38ee..9d0ba9461 100644 --- a/src/encoder/vorbis_encoder.c +++ b/src/encoder/VorbisEncoderPlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * Copyright (C) 2003-2012 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -18,10 +18,11 @@ */ #include "config.h" -#include "encoder_api.h" -#include "encoder_plugin.h" -#include "tag.h" -#include "audio_format.h" +#include "VorbisEncoderPlugin.hxx" +#include "OggStream.hxx" +#include "EncoderAPI.hxx" +#include "Tag.hxx" +#include "AudioFormat.hxx" #include "mpd_error.h" #include <vorbis/vorbisenc.h> @@ -33,7 +34,7 @@ struct vorbis_encoder { /** the base class */ - struct encoder encoder; + Encoder encoder; /* configuration */ @@ -42,18 +43,16 @@ struct vorbis_encoder { /* runtime information */ - struct audio_format audio_format; - - ogg_stream_state os; + AudioFormat audio_format; vorbis_dsp_state vd; vorbis_block vb; vorbis_info vi; - bool flush; -}; + OggStream stream; -extern const struct encoder_plugin vorbis_encoder_plugin; + vorbis_encoder():encoder(vorbis_encoder_plugin) {} +}; static inline GQuark vorbis_encoder_quark(void) @@ -63,10 +62,10 @@ vorbis_encoder_quark(void) static bool vorbis_encoder_configure(struct vorbis_encoder *encoder, - const struct config_param *param, GError **error) + const config_param ¶m, GError **error) { - const char *value = config_get_block_string(param, "quality", NULL); - if (value != NULL) { + const char *value = param.GetBlockValue("quality"); + if (value != nullptr) { /* a quality was configured (VBR) */ char *endptr; @@ -77,26 +76,26 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder, g_set_error(error, vorbis_encoder_quark(), 0, "quality \"%s\" is not a number in the " "range -1 to 10, line %i", - value, param->line); + value, param.line); return false; } - if (config_get_block_string(param, "bitrate", NULL) != NULL) { + if (param.GetBlockValue("bitrate") != nullptr) { g_set_error(error, vorbis_encoder_quark(), 0, "quality and bitrate are " "both defined (line %i)", - param->line); + param.line); return false; } } else { /* a bit rate was configured */ - value = config_get_block_string(param, "bitrate", NULL); - if (value == NULL) { + value = param.GetBlockValue("bitrate"); + if (value == nullptr) { g_set_error(error, vorbis_encoder_quark(), 0, "neither bitrate nor quality defined " "at line %i", - param->line); + param.line); return false; } @@ -107,7 +106,7 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder, if (*endptr != '\0' || encoder->bitrate <= 0) { g_set_error(error, vorbis_encoder_quark(), 0, "bitrate at line %i should be a positive integer", - param->line); + param.line); return false; } } @@ -115,30 +114,29 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder, return true; } -static struct encoder * -vorbis_encoder_init(const struct config_param *param, GError **error) +static Encoder * +vorbis_encoder_init(const config_param ¶m, GError **error) { - struct vorbis_encoder *encoder = g_new(struct vorbis_encoder, 1); - encoder_struct_init(&encoder->encoder, &vorbis_encoder_plugin); + vorbis_encoder *encoder = new vorbis_encoder(); /* load configuration from "param" */ if (!vorbis_encoder_configure(encoder, param, error)) { /* configuration has failed, roll back and return error */ - g_free(encoder); - return NULL; + delete encoder; + return nullptr; } return &encoder->encoder; } static void -vorbis_encoder_finish(struct encoder *_encoder) +vorbis_encoder_finish(Encoder *_encoder) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; /* the real libvorbis/libogg cleanup was already performed by vorbis_encoder_close(), so no real work here */ - g_free(encoder); + delete encoder; } static bool @@ -174,7 +172,7 @@ vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error) vorbis_analysis_init(&encoder->vd, &encoder->vi); vorbis_block_init(&encoder->vd, &encoder->vb); - ogg_stream_init(&encoder->os, g_random_int()); + encoder->stream.Initialize(g_random_int()); return true; } @@ -187,9 +185,9 @@ vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc) vorbis_analysis_headerout(&encoder->vd, vc, &packet, &comments, &codebooks); - ogg_stream_packetin(&encoder->os, &packet); - ogg_stream_packetin(&encoder->os, &comments); - ogg_stream_packetin(&encoder->os, &codebooks); + encoder->stream.PacketIn(packet); + encoder->stream.PacketIn(comments); + encoder->stream.PacketIn(codebooks); } static void @@ -203,39 +201,35 @@ vorbis_encoder_send_header(struct vorbis_encoder *encoder) } static bool -vorbis_encoder_open(struct encoder *_encoder, - struct audio_format *audio_format, +vorbis_encoder_open(Encoder *_encoder, + AudioFormat &audio_format, GError **error) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; - audio_format->format = SAMPLE_FORMAT_S16; + audio_format.format = SampleFormat::FLOAT; - encoder->audio_format = *audio_format; + encoder->audio_format = audio_format; if (!vorbis_encoder_reinit(encoder, error)) return false; vorbis_encoder_send_header(encoder); - /* set "flush" to true, so the caller gets the full headers on - the first read() */ - encoder->flush = true; - return true; } static void vorbis_encoder_clear(struct vorbis_encoder *encoder) { - ogg_stream_clear(&encoder->os); + encoder->stream.Deinitialize(); vorbis_block_clear(&encoder->vb); vorbis_dsp_clear(&encoder->vd); vorbis_info_clear(&encoder->vi); } static void -vorbis_encoder_close(struct encoder *_encoder) +vorbis_encoder_close(Encoder *_encoder) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; @@ -246,26 +240,26 @@ static void vorbis_encoder_blockout(struct vorbis_encoder *encoder) { while (vorbis_analysis_blockout(&encoder->vd, &encoder->vb) == 1) { - vorbis_analysis(&encoder->vb, NULL); + vorbis_analysis(&encoder->vb, nullptr); vorbis_bitrate_addblock(&encoder->vb); ogg_packet packet; while (vorbis_bitrate_flushpacket(&encoder->vd, &packet)) - ogg_stream_packetin(&encoder->os, &packet); + encoder->stream.PacketIn(packet); } } static bool -vorbis_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error) +vorbis_encoder_flush(Encoder *_encoder, G_GNUC_UNUSED GError **error) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; - encoder->flush = true; + encoder->stream.Flush(); return true; } static bool -vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error) +vorbis_encoder_pre_tag(Encoder *_encoder, G_GNUC_UNUSED GError **error) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; @@ -279,23 +273,23 @@ vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error) vorbis_analysis_init(&encoder->vd, &encoder->vi); vorbis_block_init(&encoder->vd, &encoder->vb); - encoder->flush = true; + encoder->stream.Flush(); return true; } static void -copy_tag_to_vorbis_comment(vorbis_comment *vc, const struct tag *tag) +copy_tag_to_vorbis_comment(vorbis_comment *vc, const Tag *tag) { for (unsigned i = 0; i < tag->num_items; i++) { - struct tag_item *item = tag->items[i]; - char *name = g_ascii_strup(tag_item_names[item->type], -1); - vorbis_comment_add_tag(vc, name, item->value); + const TagItem &item = *tag->items[i]; + char *name = g_ascii_strup(tag_item_names[item.type], -1); + vorbis_comment_add_tag(vc, name, item.value); g_free(name); } } static bool -vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag, +vorbis_encoder_tag(Encoder *_encoder, const Tag *tag, G_GNUC_UNUSED GError **error) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; @@ -308,46 +302,41 @@ vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag, /* reset ogg_stream_state and begin a new stream */ - ogg_stream_reset_serialno(&encoder->os, g_random_int()); + encoder->stream.Reinitialize(g_random_int()); /* send that vorbis_comment to the ogg_stream_state */ vorbis_encoder_headerout(encoder, &comment); vorbis_comment_clear(&comment); - /* the next vorbis_encoder_read() call should flush the - ogg_stream_state */ - - encoder->flush = true; - return true; } static void -pcm16_to_vorbis_buffer(float **dest, const int16_t *src, - unsigned num_frames, unsigned num_channels) +interleaved_to_vorbis_buffer(float **dest, const float *src, + unsigned num_frames, unsigned num_channels) { for (unsigned i = 0; i < num_frames; i++) for (unsigned j = 0; j < num_channels; j++) - dest[j][i] = *src++ / 32768.0; + dest[j][i] = *src++; } static bool -vorbis_encoder_write(struct encoder *_encoder, +vorbis_encoder_write(Encoder *_encoder, const void *data, size_t length, G_GNUC_UNUSED GError **error) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; - unsigned num_frames = length - / audio_format_frame_size(&encoder->audio_format); + unsigned num_frames = length / encoder->audio_format.GetFrameSize(); /* this is for only 16-bit audio */ - pcm16_to_vorbis_buffer(vorbis_analysis_buffer(&encoder->vd, - num_frames), - (const int16_t *)data, - num_frames, encoder->audio_format.channels); + interleaved_to_vorbis_buffer(vorbis_analysis_buffer(&encoder->vd, + num_frames), + (const float *)data, + num_frames, + encoder->audio_format.channels); vorbis_analysis_wrote(&encoder->vd, num_frames); vorbis_encoder_blockout(encoder); @@ -355,53 +344,30 @@ vorbis_encoder_write(struct encoder *_encoder, } static size_t -vorbis_encoder_read(struct encoder *_encoder, void *_dest, size_t length) +vorbis_encoder_read(Encoder *_encoder, void *dest, size_t length) { struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; - unsigned char *dest = _dest; - - ogg_page page; - int ret = ogg_stream_pageout(&encoder->os, &page); - if (ret == 0 && encoder->flush) { - encoder->flush = false; - ret = ogg_stream_flush(&encoder->os, &page); - - } - - if (ret == 0) - return 0; - - assert(page.header_len > 0 || page.body_len > 0); - - size_t nbytes = (size_t)page.header_len + (size_t)page.body_len; - - if (nbytes > length) - /* XXX better error handling */ - MPD_ERROR("buffer too small"); - - memcpy(dest, page.header, page.header_len); - memcpy(dest + page.header_len, page.body, page.body_len); - return nbytes; + return encoder->stream.PageOut(dest, length); } static const char * -vorbis_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder) +vorbis_encoder_get_mime_type(G_GNUC_UNUSED Encoder *_encoder) { return "audio/ogg"; } -const struct encoder_plugin vorbis_encoder_plugin = { - .name = "vorbis", - .init = vorbis_encoder_init, - .finish = vorbis_encoder_finish, - .open = vorbis_encoder_open, - .close = vorbis_encoder_close, - .end = vorbis_encoder_pre_tag, - .flush = vorbis_encoder_flush, - .pre_tag = vorbis_encoder_pre_tag, - .tag = vorbis_encoder_tag, - .write = vorbis_encoder_write, - .read = vorbis_encoder_read, - .get_mime_type = vorbis_encoder_get_mime_type, +const EncoderPlugin vorbis_encoder_plugin = { + "vorbis", + vorbis_encoder_init, + vorbis_encoder_finish, + vorbis_encoder_open, + vorbis_encoder_close, + vorbis_encoder_pre_tag, + vorbis_encoder_flush, + vorbis_encoder_pre_tag, + vorbis_encoder_tag, + vorbis_encoder_write, + vorbis_encoder_read, + vorbis_encoder_get_mime_type, }; diff --git a/src/encoder/VorbisEncoderPlugin.hxx b/src/encoder/VorbisEncoderPlugin.hxx new file mode 100644 index 000000000..72cc44f5c --- /dev/null +++ b/src/encoder/VorbisEncoderPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2012 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_VORBIS_H +#define MPD_ENCODER_VORBIS_H + +extern const struct EncoderPlugin vorbis_encoder_plugin; + +#endif diff --git a/src/encoder/wave_encoder.c b/src/encoder/WaveEncoderPlugin.cxx index 9eeb4d513..17560dfea 100644 --- a/src/encoder/wave_encoder.c +++ b/src/encoder/WaveEncoderPlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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 @@ -18,19 +18,25 @@ */ #include "config.h" -#include "encoder_api.h" -#include "encoder_plugin.h" -#include "fifo_buffer.h" -#include "growing_fifo.h" +#include "WaveEncoderPlugin.hxx" +#include "EncoderAPI.hxx" +#include "util/fifo_buffer.h" +extern "C" { +#include "util/growing_fifo.h" +} + +#include <glib.h> #include <assert.h> #include <string.h> -struct wave_encoder { - struct encoder encoder; +struct WaveEncoder { + Encoder encoder; unsigned bits; struct fifo_buffer *buffer; + + WaveEncoder():encoder(wave_encoder_plugin) {} }; struct wave_header { @@ -49,14 +55,6 @@ struct wave_header { uint32_t data_size; }; -extern const struct encoder_plugin wave_encoder_plugin; - -static inline GQuark -wave_encoder_quark(void) -{ - return g_quark_from_static_string("wave_encoder"); -} - static void fill_wave_header(struct wave_header *header, int channels, int bits, int freq, int block_size) @@ -84,77 +82,73 @@ fill_wave_header(struct wave_header *header, int channels, int bits, (8 + data_size)); } -static struct encoder * -wave_encoder_init(G_GNUC_UNUSED const struct config_param *param, - G_GNUC_UNUSED GError **error) +static Encoder * +wave_encoder_init(gcc_unused const config_param ¶m, + gcc_unused GError **error) { - struct wave_encoder *encoder; - - encoder = g_new(struct wave_encoder, 1); - encoder_struct_init(&encoder->encoder, &wave_encoder_plugin); - + WaveEncoder *encoder = new WaveEncoder(); return &encoder->encoder; } static void -wave_encoder_finish(struct encoder *_encoder) +wave_encoder_finish(Encoder *_encoder) { - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + WaveEncoder *encoder = (WaveEncoder *)_encoder; g_free(encoder); } static bool -wave_encoder_open(struct encoder *_encoder, - G_GNUC_UNUSED struct audio_format *audio_format, - G_GNUC_UNUSED GError **error) +wave_encoder_open(Encoder *_encoder, + AudioFormat &audio_format, + gcc_unused GError **error) { - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + WaveEncoder *encoder = (WaveEncoder *)_encoder; - assert(audio_format_valid(audio_format)); + assert(audio_format.IsValid()); - switch (audio_format->format) { - case SAMPLE_FORMAT_S8: + switch (audio_format.format) { + case SampleFormat::S8: encoder->bits = 8; break; - case SAMPLE_FORMAT_S16: + case SampleFormat::S16: encoder->bits = 16; break; - case SAMPLE_FORMAT_S24_P32: + case SampleFormat::S24_P32: encoder->bits = 24; break; - case SAMPLE_FORMAT_S32: + case SampleFormat::S32: encoder->bits = 32; break; default: - audio_format->format = SAMPLE_FORMAT_S16; + audio_format.format = SampleFormat::S16; encoder->bits = 16; break; } encoder->buffer = growing_fifo_new(); - struct wave_header *header = + wave_header *header = (wave_header *) growing_fifo_write(&encoder->buffer, sizeof(*header)); /* create PCM wave header in initial buffer */ fill_wave_header(header, - audio_format->channels, + audio_format.channels, encoder->bits, - audio_format->sample_rate, - (encoder->bits / 8) * audio_format->channels ); + audio_format.sample_rate, + (encoder->bits / 8) * audio_format.channels); fifo_buffer_append(encoder->buffer, sizeof(*header)); return true; } static void -wave_encoder_close(struct encoder *_encoder) +wave_encoder_close(Encoder *_encoder) { - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + WaveEncoder *encoder = (WaveEncoder *)_encoder; fifo_buffer_free(encoder->buffer); } @@ -200,13 +194,13 @@ pcm24_to_wave(uint8_t *dst8, const uint32_t *src32, size_t length) } static bool -wave_encoder_write(struct encoder *_encoder, +wave_encoder_write(Encoder *_encoder, const void *src, size_t length, - G_GNUC_UNUSED GError **error) + gcc_unused GError **error) { - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + WaveEncoder *encoder = (WaveEncoder *)_encoder; - void *dst = growing_fifo_write(&encoder->buffer, length); + uint8_t *dst = (uint8_t *)growing_fifo_write(&encoder->buffer, length); #if (G_BYTE_ORDER == G_LITTLE_ENDIAN) switch (encoder->bits) { @@ -216,7 +210,7 @@ wave_encoder_write(struct encoder *_encoder, memcpy(dst, src, length); break; case 24: - length = pcm24_to_wave(dst, src, length); + length = pcm24_to_wave(dst, (const uint32_t *)src, length); break; } #elif (G_BYTE_ORDER == G_BIG_ENDIAN) @@ -225,13 +219,13 @@ wave_encoder_write(struct encoder *_encoder, memcpy(dst, src, length); break; case 16: - length = pcm16_to_wave(dst, src, length); + length = pcm16_to_wave(dst, (const uint16_t *)src, length); break; case 24: - length = pcm24_to_wave(dst, src, length); + length = pcm24_to_wave(dst, (const uint32_t *)src, length); break; case 32: - length = pcm32_to_wave(dst, src, length); + length = pcm32_to_wave(dst, (const uint32_t *)src, length); break; } #else @@ -243,9 +237,9 @@ wave_encoder_write(struct encoder *_encoder, } static size_t -wave_encoder_read(struct encoder *_encoder, void *dest, size_t length) +wave_encoder_read(Encoder *_encoder, void *dest, size_t length) { - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + WaveEncoder *encoder = (WaveEncoder *)_encoder; size_t max_length; const void *src = fifo_buffer_read(encoder->buffer, &max_length); @@ -261,18 +255,22 @@ wave_encoder_read(struct encoder *_encoder, void *dest, size_t length) } static const char * -wave_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder) +wave_encoder_get_mime_type(gcc_unused Encoder *_encoder) { return "audio/wav"; } -const struct encoder_plugin wave_encoder_plugin = { - .name = "wave", - .init = wave_encoder_init, - .finish = wave_encoder_finish, - .open = wave_encoder_open, - .close = wave_encoder_close, - .write = wave_encoder_write, - .read = wave_encoder_read, - .get_mime_type = wave_encoder_get_mime_type, +const EncoderPlugin wave_encoder_plugin = { + "wave", + wave_encoder_init, + wave_encoder_finish, + wave_encoder_open, + wave_encoder_close, + nullptr, + nullptr, + nullptr, + nullptr, + wave_encoder_write, + wave_encoder_read, + wave_encoder_get_mime_type, }; diff --git a/src/encoder/WaveEncoderPlugin.hxx b/src/encoder/WaveEncoderPlugin.hxx new file mode 100644 index 000000000..190ee131e --- /dev/null +++ b/src/encoder/WaveEncoderPlugin.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_WAVE_HXX +#define MPD_ENCODER_WAVE_HXX + +extern const struct EncoderPlugin wave_encoder_plugin; + +#endif diff --git a/src/encoder/null_encoder.c b/src/encoder/null_encoder.c deleted file mode 100644 index 48cdf139b..000000000 --- a/src/encoder/null_encoder.c +++ /dev/null @@ -1,120 +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 "fifo_buffer.h" -#include "growing_fifo.h" - -#include <assert.h> -#include <string.h> - -struct null_encoder { - struct encoder encoder; - - struct fifo_buffer *buffer; -}; - -extern const struct encoder_plugin null_encoder_plugin; - -static inline GQuark -null_encoder_quark(void) -{ - return g_quark_from_static_string("null_encoder"); -} - -static struct encoder * -null_encoder_init(G_GNUC_UNUSED const struct config_param *param, - G_GNUC_UNUSED GError **error) -{ - struct null_encoder *encoder; - - encoder = g_new(struct null_encoder, 1); - encoder_struct_init(&encoder->encoder, &null_encoder_plugin); - - return &encoder->encoder; -} - -static void -null_encoder_finish(struct encoder *_encoder) -{ - struct null_encoder *encoder = (struct null_encoder *)_encoder; - - g_free(encoder); -} - -static void -null_encoder_close(struct encoder *_encoder) -{ - struct null_encoder *encoder = (struct null_encoder *)_encoder; - - fifo_buffer_free(encoder->buffer); -} - - -static bool -null_encoder_open(struct encoder *_encoder, - G_GNUC_UNUSED struct audio_format *audio_format, - G_GNUC_UNUSED GError **error) -{ - struct null_encoder *encoder = (struct null_encoder *)_encoder; - - encoder->buffer = growing_fifo_new(); - return true; -} - -static bool -null_encoder_write(struct encoder *_encoder, - const void *data, size_t length, - G_GNUC_UNUSED GError **error) -{ - struct null_encoder *encoder = (struct null_encoder *)_encoder; - - growing_fifo_append(&encoder->buffer, data, length); - return length; -} - -static size_t -null_encoder_read(struct encoder *_encoder, void *dest, size_t length) -{ - struct null_encoder *encoder = (struct null_encoder *)_encoder; - - size_t max_length; - const void *src = fifo_buffer_read(encoder->buffer, &max_length); - if (src == NULL) - return 0; - - if (length > max_length) - length = max_length; - - memcpy(dest, src, length); - fifo_buffer_consume(encoder->buffer, length); - return length; -} - -const struct encoder_plugin null_encoder_plugin = { - .name = "null", - .init = null_encoder_init, - .finish = null_encoder_finish, - .open = null_encoder_open, - .close = null_encoder_close, - .write = null_encoder_write, - .read = null_encoder_read, -}; |