aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoder')
-rw-r--r--src/encoder/FlacEncoderPlugin.cxx (renamed from src/encoder/flac_encoder.c)235
-rw-r--r--src/encoder/FlacEncoderPlugin.hxx25
-rw-r--r--src/encoder/LameEncoderPlugin.cxx294
-rw-r--r--src/encoder/LameEncoderPlugin.hxx25
-rw-r--r--src/encoder/NullEncoderPlugin.cxx117
-rw-r--r--src/encoder/NullEncoderPlugin.hxx25
-rw-r--r--src/encoder/OggStream.hxx128
-rw-r--r--src/encoder/OpusEncoderPlugin.cxx417
-rw-r--r--src/encoder/OpusEncoderPlugin.hxx25
-rw-r--r--src/encoder/TwolameEncoderPlugin.cxx315
-rw-r--r--src/encoder/TwolameEncoderPlugin.hxx25
-rw-r--r--src/encoder/VorbisEncoderPlugin.cxx (renamed from src/encoder/vorbis_encoder.c)234
-rw-r--r--src/encoder/VorbisEncoderPlugin.hxx25
-rw-r--r--src/encoder/WaveEncoderPlugin.cxx (renamed from src/encoder/wave_encoder.c)122
-rw-r--r--src/encoder/WaveEncoderPlugin.hxx25
-rw-r--r--src/encoder/lame_encoder.c300
-rw-r--r--src/encoder/null_encoder.c120
-rw-r--r--src/encoder/twolame_encoder.c308
18 files changed, 1710 insertions, 1055 deletions
diff --git a/src/encoder/flac_encoder.c b/src/encoder/FlacEncoderPlugin.cxx
index e32588e29..5a77e24a7 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,130 +18,127 @@
*/
#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 "ConfigError.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+#include "util/fifo_buffer.h"
+
+extern "C" {
+#include "util/growing_fifo.h"
+}
+
+#include <glib.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)
-{
- return g_quark_from_static_string("flac_encoder");
-}
+static constexpr Domain flac_encoder_domain("vorbis_encoder");
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 &param,
+ gcc_unused Error &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 &param, Error &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)
+ Error &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,
- "error setting flac compression to %d",
- encoder->compression);
+ error.Format(config_domain,
+ "error setting flac compression to %d",
+ 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,
- "error setting flac channels num to %d",
- encoder->audio_format.channels);
+ error.Format(config_domain,
+ "error setting flac channels num to %d",
+ encoder->audio_format.channels);
return false;
}
if ( !FLAC__stream_encoder_set_bits_per_sample(encoder->fse,
bits_per_sample)) {
- g_set_error(error, flac_encoder_quark(), 0,
- "error setting flac bit format to %d",
- bits_per_sample);
+ error.Format(config_domain,
+ "error setting flac bit format to %d",
+ bits_per_sample);
return false;
}
if ( !FLAC__stream_encoder_set_sample_rate(encoder->fse,
encoder->audio_format.sample_rate)) {
- g_set_error(error, flac_encoder_quark(), 0,
- "error setting flac sample rate to %d",
- encoder->audio_format.sample_rate);
+ error.Format(config_domain,
+ "error setting flac sample rate to %d",
+ encoder->audio_format.sample_rate);
return false;
}
return true;
}
static FLAC__StreamEncoderWriteStatus
-flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse,
+flac_write_callback(gcc_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)
+ gcc_unused unsigned samples,
+ gcc_unused unsigned current_frame, void *client_data)
{
struct flac_encoder *encoder = (struct flac_encoder *) client_data;
@@ -152,49 +149,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,
- GError **error)
+flac_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &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) {
- g_set_error(error, flac_encoder_quark(), 0,
- "flac_new() failed");
+ if (encoder->fse == nullptr) {
+ error.Set(flac_encoder_domain, "flac_new() failed");
return false;
}
@@ -203,53 +198,32 @@ 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,
- "failed to initialize encoder: %s\n",
- FLAC__StreamEncoderInitStatusString[init_status]);
+ error.Format(flac_encoder_domain,
+ "failed to initialize encoder: %s\n",
+ FLAC__StreamEncoderInitStatusString[init_status]);
flac_encoder_close(_encoder);
return false;
}
}
-#endif
return true;
}
static bool
-flac_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+flac_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
{
struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
@@ -276,47 +250,52 @@ pcm16_to_flac(int32_t *out, const int16_t *in, unsigned num_samples)
}
static bool
-flac_encoder_write(struct encoder *_encoder,
- const void *data, size_t length,
- G_GNUC_UNUSED GError **error)
+flac_encoder_write(Encoder *_encoder,
+ const void *data, size_t length,
+ gcc_unused Error &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)) {
- g_set_error(error, flac_encoder_quark(), 0,
- "flac encoder process failed");
+ if (!FLAC__stream_encoder_process_interleaved(encoder->fse,
+ (const FLAC__int32 *)buffer,
+ num_frames)) {
+ error.Set(flac_encoder_domain, "flac encoder process failed");
return false;
}
@@ -324,14 +303,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 +322,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(gcc_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/LameEncoderPlugin.cxx b/src/encoder/LameEncoderPlugin.cxx
new file mode 100644
index 000000000..a5b7be483
--- /dev/null
+++ b/src/encoder/LameEncoderPlugin.cxx
@@ -0,0 +1,294 @@
+/*
+ * 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 "EncoderAPI.hxx"
+#include "AudioFormat.hxx"
+#include "ConfigError.hxx"
+#include "util/ReusableArray.hxx"
+#include "util/Manual.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+#include <lame/lame.h>
+
+#include <glib.h>
+
+#include <assert.h>
+#include <string.h>
+
+struct LameEncoder final {
+ Encoder encoder;
+
+ AudioFormat audio_format;
+ float quality;
+ int bitrate;
+
+ lame_global_flags *gfp;
+
+ Manual<ReusableArray<unsigned char, 32768>> output_buffer;
+ unsigned char *output_begin, *output_end;
+
+ LameEncoder():encoder(lame_encoder_plugin) {}
+
+ bool Configure(const config_param &param, Error &error);
+};
+
+static constexpr Domain lame_encoder_domain("lame_encoder");
+
+bool
+LameEncoder::Configure(const config_param &param, Error &error)
+{
+ const char *value;
+ char *endptr;
+
+ value = param.GetBlockValue("quality");
+ if (value != nullptr) {
+ /* a quality was configured (VBR) */
+
+ quality = g_ascii_strtod(value, &endptr);
+
+ if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
+ error.Format(config_domain,
+ "quality \"%s\" is not a number in the "
+ "range -1 to 10",
+ value);
+ return false;
+ }
+
+ if (param.GetBlockValue("bitrate") != nullptr) {
+ error.Set(config_domain,
+ "quality and bitrate are both defined");
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ value = param.GetBlockValue("bitrate");
+ if (value == nullptr) {
+ error.Set(config_domain,
+ "neither bitrate nor quality defined");
+ return false;
+ }
+
+ quality = -2.0;
+ bitrate = g_ascii_strtoll(value, &endptr, 10);
+
+ if (*endptr != '\0' || bitrate <= 0) {
+ error.Set(config_domain,
+ "bitrate should be a positive integer");
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static Encoder *
+lame_encoder_init(const config_param &param, Error &error)
+{
+ LameEncoder *encoder = new LameEncoder();
+
+ /* load configuration from "param" */
+ if (!encoder->Configure(param, error)) {
+ /* configuration has failed, roll back and return error */
+ delete encoder;
+ return nullptr;
+ }
+
+ return &encoder->encoder;
+}
+
+static void
+lame_encoder_finish(Encoder *_encoder)
+{
+ LameEncoder *encoder = (LameEncoder *)_encoder;
+
+ /* the real liblame cleanup was already performed by
+ lame_encoder_close(), so no real work here */
+ delete encoder;
+}
+
+static bool
+lame_encoder_setup(LameEncoder *encoder, Error &error)
+{
+ if (encoder->quality >= -1.0) {
+ /* a quality was configured (VBR) */
+
+ if (0 != lame_set_VBR(encoder->gfp, vbr_rh)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame VBR mode");
+ return false;
+ }
+ if (0 != lame_set_VBR_q(encoder->gfp, encoder->quality)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame VBR quality");
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ if (0 != lame_set_brate(encoder->gfp, encoder->bitrate)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame bitrate");
+ return false;
+ }
+ }
+
+ if (0 != lame_set_num_channels(encoder->gfp,
+ encoder->audio_format.channels)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame num channels");
+ return false;
+ }
+
+ if (0 != lame_set_in_samplerate(encoder->gfp,
+ encoder->audio_format.sample_rate)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame sample rate");
+ return false;
+ }
+
+ if (0 != lame_set_out_samplerate(encoder->gfp,
+ encoder->audio_format.sample_rate)) {
+ error.Set(lame_encoder_domain,
+ "error setting lame out sample rate");
+ return false;
+ }
+
+ if (0 > lame_init_params(encoder->gfp)) {
+ error.Set(lame_encoder_domain,
+ "error initializing lame params");
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+lame_encoder_open(Encoder *_encoder, AudioFormat &audio_format, Error &error)
+{
+ LameEncoder *encoder = (LameEncoder *)_encoder;
+
+ audio_format.format = SampleFormat::S16;
+ audio_format.channels = 2;
+
+ encoder->audio_format = audio_format;
+
+ encoder->gfp = lame_init();
+ if (encoder->gfp == nullptr) {
+ error.Set(lame_encoder_domain, "lame_init() failed");
+ return false;
+ }
+
+ if (!lame_encoder_setup(encoder, error)) {
+ lame_close(encoder->gfp);
+ return false;
+ }
+
+ encoder->output_buffer.Construct();
+ encoder->output_begin = encoder->output_end = nullptr;
+
+ return true;
+}
+
+static void
+lame_encoder_close(Encoder *_encoder)
+{
+ LameEncoder *encoder = (LameEncoder *)_encoder;
+
+ lame_close(encoder->gfp);
+ encoder->output_buffer.Destruct();
+}
+
+static bool
+lame_encoder_write(Encoder *_encoder,
+ const void *data, size_t length,
+ gcc_unused Error &error)
+{
+ LameEncoder *encoder = (LameEncoder *)_encoder;
+ const int16_t *src = (const int16_t*)data;
+
+ assert(encoder->output_begin == encoder->output_end);
+
+ const unsigned num_frames =
+ length / encoder->audio_format.GetFrameSize();
+ const unsigned num_samples =
+ length / encoder->audio_format.GetSampleSize();
+
+ /* worst-case formula according to LAME documentation */
+ const size_t output_buffer_size = 5 * num_samples / 4 + 7200;
+ const auto output_buffer = encoder->output_buffer->Get(output_buffer_size);
+
+ /* this is for only 16-bit audio */
+
+ int bytes_out = lame_encode_buffer_interleaved(encoder->gfp,
+ const_cast<short *>(src),
+ num_frames,
+ output_buffer,
+ output_buffer_size);
+
+ if (bytes_out < 0) {
+ error.Set(lame_encoder_domain, "lame encoder failed");
+ return false;
+ }
+
+ encoder->output_begin = output_buffer;
+ encoder->output_end = output_buffer + bytes_out;
+ return true;
+}
+
+static size_t
+lame_encoder_read(Encoder *_encoder, void *dest, size_t length)
+{
+ LameEncoder *encoder = (LameEncoder *)_encoder;
+
+ const auto begin = encoder->output_begin;
+ assert(begin <= encoder->output_end);
+ const size_t remainning = encoder->output_end - begin;
+ if (length > remainning)
+ length = remainning;
+
+ memcpy(dest, begin, length);
+
+ encoder->output_begin = begin + length;
+ return length;
+}
+
+static const char *
+lame_encoder_get_mime_type(gcc_unused Encoder *_encoder)
+{
+ return "audio/mpeg";
+}
+
+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..38bc5cbe3
--- /dev/null
+++ b/src/encoder/NullEncoderPlugin.cxx
@@ -0,0 +1,117 @@
+/*
+ * 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 <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 &param,
+ gcc_unused Error &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 Error &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 Error &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..f3803e2ec
--- /dev/null
+++ b/src/encoder/OpusEncoderPlugin.cxx
@@ -0,0 +1,417 @@
+/*
+ * 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 "ConfigError.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+#include <opus.h>
+#include <ogg/ogg.h>
+
+#include <glib.h>
+
+#include <assert.h>
+
+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) {}
+};
+
+static constexpr Domain opus_encoder_domain("opus_encoder");
+
+static bool
+opus_encoder_configure(struct opus_encoder *encoder,
+ const config_param &param, Error &error)
+{
+ 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) {
+ error.Set(config_domain, "Invalid bit rate");
+ return false;
+ }
+ }
+
+ encoder->complexity = param.GetBlockValue("complexity", 10u);
+ if (encoder->complexity > 10) {
+ error.Format(config_domain, "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 {
+ error.Format(config_domain, "Invalid signal");
+ return false;
+ }
+
+ return true;
+}
+
+static Encoder *
+opus_encoder_init(const config_param &param, Error &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,
+ Error &error)
+{
+ 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_code;
+ encoder->enc = opus_encoder_create(audio_format.sample_rate,
+ audio_format.channels,
+ OPUS_APPLICATION_AUDIO,
+ &error_code);
+ if (encoder->enc == nullptr) {
+ error.Set(opus_encoder_domain, error_code,
+ opus_strerror(error_code));
+ 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,
+ Error &error)
+{
+ 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) {
+ error.Set(opus_encoder_domain, "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, Error &error)
+{
+ 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);
+}
+
+static bool
+opus_encoder_flush(Encoder *_encoder, gcc_unused Error &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,
+ Error &error)
+{
+ 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))
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+opus_encoder_write(Encoder *_encoder,
+ const void *_data, size_t length,
+ Error &error)
+{
+ 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))
+ 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))
+ 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(gcc_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/TwolameEncoderPlugin.cxx b/src/encoder/TwolameEncoderPlugin.cxx
new file mode 100644
index 000000000..6862173f7
--- /dev/null
+++ b/src/encoder/TwolameEncoderPlugin.cxx
@@ -0,0 +1,315 @@
+/*
+ * 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 "EncoderAPI.hxx"
+#include "AudioFormat.hxx"
+#include "ConfigError.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+#include "Log.hxx"
+
+#include <twolame.h>
+
+#include <glib.h>
+
+#include <assert.h>
+#include <string.h>
+
+struct TwolameEncoder final {
+ Encoder encoder;
+
+ AudioFormat audio_format;
+ float quality;
+ int bitrate;
+
+ twolame_options *options;
+
+ unsigned char output_buffer[32768];
+ size_t output_buffer_length;
+ size_t output_buffer_position;
+
+ /**
+ * Call libtwolame's flush function when the output_buffer is
+ * empty?
+ */
+ bool flush;
+
+ TwolameEncoder():encoder(twolame_encoder_plugin) {}
+
+ bool Configure(const config_param &param, Error &error);
+};
+
+static constexpr Domain twolame_encoder_domain("twolame_encoder");
+
+bool
+TwolameEncoder::Configure(const config_param &param, Error &error)
+{
+ const char *value;
+ char *endptr;
+
+ value = param.GetBlockValue("quality");
+ if (value != nullptr) {
+ /* a quality was configured (VBR) */
+
+ quality = g_ascii_strtod(value, &endptr);
+
+ if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
+ error.Format(config_domain,
+ "quality \"%s\" is not a number in the "
+ "range -1 to 10",
+ value);
+ return false;
+ }
+
+ if (param.GetBlockValue("bitrate") != nullptr) {
+ error.Set(config_domain,
+ "quality and bitrate are both defined");
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ value = param.GetBlockValue("bitrate");
+ if (value == nullptr) {
+ error.Set(config_domain,
+ "neither bitrate nor quality defined");
+ return false;
+ }
+
+ quality = -2.0;
+ bitrate = g_ascii_strtoll(value, &endptr, 10);
+
+ if (*endptr != '\0' || bitrate <= 0) {
+ error.Set(config_domain,
+ "bitrate should be a positive integer");
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static Encoder *
+twolame_encoder_init(const config_param &param, Error &error_r)
+{
+ FormatDebug(twolame_encoder_domain,
+ "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(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, Error &error)
+{
+ if (encoder->quality >= -1.0) {
+ /* a quality was configured (VBR) */
+
+ if (0 != twolame_set_VBR(encoder->options, true)) {
+ error.Set(twolame_encoder_domain,
+ "error setting twolame VBR mode");
+ return false;
+ }
+ if (0 != twolame_set_VBR_q(encoder->options, encoder->quality)) {
+ error.Set(twolame_encoder_domain,
+ "error setting twolame VBR quality");
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) {
+ error.Set(twolame_encoder_domain,
+ "error setting twolame bitrate");
+ return false;
+ }
+ }
+
+ if (0 != twolame_set_num_channels(encoder->options,
+ encoder->audio_format.channels)) {
+ error.Set(twolame_encoder_domain,
+ "error setting twolame num channels");
+ return false;
+ }
+
+ if (0 != twolame_set_in_samplerate(encoder->options,
+ encoder->audio_format.sample_rate)) {
+ error.Set(twolame_encoder_domain,
+ "error setting twolame sample rate");
+ return false;
+ }
+
+ if (0 > twolame_init_params(encoder->options)) {
+ error.Set(twolame_encoder_domain,
+ "error initializing twolame params");
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+twolame_encoder_open(Encoder *_encoder, AudioFormat &audio_format,
+ Error &error)
+{
+ TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
+
+ audio_format.format = SampleFormat::S16;
+ audio_format.channels = 2;
+
+ encoder->audio_format = audio_format;
+
+ encoder->options = twolame_init();
+ if (encoder->options == nullptr) {
+ error.Set(twolame_encoder_domain, "twolame_init() failed");
+ return false;
+ }
+
+ if (!twolame_encoder_setup(encoder, error)) {
+ twolame_close(&encoder->options);
+ return false;
+ }
+
+ encoder->output_buffer_length = 0;
+ encoder->output_buffer_position = 0;
+ encoder->flush = false;
+
+ return true;
+}
+
+static void
+twolame_encoder_close(Encoder *_encoder)
+{
+ TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
+
+ twolame_close(&encoder->options);
+}
+
+static bool
+twolame_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
+{
+ TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
+
+ encoder->flush = true;
+ return true;
+}
+
+static bool
+twolame_encoder_write(Encoder *_encoder,
+ const void *data, size_t length,
+ gcc_unused Error &error)
+{
+ TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
+ const int16_t *src = (const int16_t*)data;
+
+ assert(encoder->output_buffer_position ==
+ encoder->output_buffer_length);
+
+ const unsigned num_frames =
+ length / encoder->audio_format.GetFrameSize();
+
+ int bytes_out = twolame_encode_buffer_interleaved(encoder->options,
+ src, num_frames,
+ encoder->output_buffer,
+ sizeof(encoder->output_buffer));
+ if (bytes_out < 0) {
+ error.Set(twolame_encoder_domain, "twolame encoder failed");
+ return false;
+ }
+
+ encoder->output_buffer_length = (size_t)bytes_out;
+ encoder->output_buffer_position = 0;
+ return true;
+}
+
+static size_t
+twolame_encoder_read(Encoder *_encoder, void *dest, size_t length)
+{
+ TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
+
+ assert(encoder->output_buffer_position <=
+ encoder->output_buffer_length);
+
+ if (encoder->output_buffer_position == encoder->output_buffer_length &&
+ encoder->flush) {
+ int ret = twolame_encode_flush(encoder->options,
+ encoder->output_buffer,
+ sizeof(encoder->output_buffer));
+ if (ret > 0) {
+ encoder->output_buffer_length = (size_t)ret;
+ encoder->output_buffer_position = 0;
+ }
+
+ encoder->flush = false;
+ }
+
+
+ const size_t remainning = encoder->output_buffer_length
+ - encoder->output_buffer_position;
+ if (length > remainning)
+ length = remainning;
+
+ memcpy(dest, encoder->output_buffer + encoder->output_buffer_position,
+ length);
+
+ encoder->output_buffer_position += length;
+
+ return length;
+}
+
+static const char *
+twolame_encoder_get_mime_type(gcc_unused Encoder *_encoder)
+{
+ return "audio/mpeg";
+}
+
+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..84b4cac28 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,22 +18,24 @@
*/
#include "config.h"
-#include "encoder_api.h"
-#include "encoder_plugin.h"
-#include "tag.h"
-#include "audio_format.h"
-#include "mpd_error.h"
+#include "VorbisEncoderPlugin.hxx"
+#include "OggStream.hxx"
+#include "EncoderAPI.hxx"
+#include "tag/Tag.hxx"
+#include "AudioFormat.hxx"
+#include "ConfigError.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
#include <vorbis/vorbisenc.h>
-#include <assert.h>
+#include <glib.h>
-#undef G_LOG_DOMAIN
-#define G_LOG_DOMAIN "vorbis_encoder"
+#include <assert.h>
struct vorbis_encoder {
/** the base class */
- struct encoder encoder;
+ Encoder encoder;
/* configuration */
@@ -42,31 +44,25 @@ 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;
+
+ vorbis_encoder():encoder(vorbis_encoder_plugin) {}
};
-extern const struct encoder_plugin vorbis_encoder_plugin;
-
-static inline GQuark
-vorbis_encoder_quark(void)
-{
- return g_quark_from_static_string("vorbis_encoder");
-}
+static constexpr Domain vorbis_encoder_domain("vorbis_encoder");
static bool
vorbis_encoder_configure(struct vorbis_encoder *encoder,
- const struct config_param *param, GError **error)
+ const config_param &param, Error &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;
@@ -74,29 +70,25 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder,
if (*endptr != '\0' || encoder->quality < -1.0 ||
encoder->quality > 10.0) {
- 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);
+ error.Format(config_domain,
+ "quality \"%s\" is not a number in the "
+ "range -1 to 10",
+ value);
return false;
}
- if (config_get_block_string(param, "bitrate", NULL) != NULL) {
- g_set_error(error, vorbis_encoder_quark(), 0,
- "quality and bitrate are "
- "both defined (line %i)",
- param->line);
+ if (param.GetBlockValue("bitrate") != nullptr) {
+ error.Set(config_domain,
+ "quality and bitrate are both defined");
return false;
}
} else {
/* a bit rate was configured */
- value = config_get_block_string(param, "bitrate", NULL);
- if (value == NULL) {
- g_set_error(error, vorbis_encoder_quark(), 0,
- "neither bitrate nor quality defined "
- "at line %i",
- param->line);
+ value = param.GetBlockValue("bitrate");
+ if (value == nullptr) {
+ error.Set(config_domain,
+ "neither bitrate nor quality defined");
return false;
}
@@ -105,9 +97,8 @@ vorbis_encoder_configure(struct vorbis_encoder *encoder,
char *endptr;
encoder->bitrate = g_ascii_strtoll(value, &endptr, 10);
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);
+ error.Set(config_domain,
+ "bitrate should be a positive integer");
return false;
}
}
@@ -115,34 +106,33 @@ 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 &param, Error &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
-vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
+vorbis_encoder_reinit(struct vorbis_encoder *encoder, Error &error)
{
vorbis_info_init(&encoder->vi);
@@ -153,8 +143,8 @@ vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
encoder->audio_format.channels,
encoder->audio_format.sample_rate,
encoder->quality * 0.1)) {
- g_set_error(error, vorbis_encoder_quark(), 0,
- "error initializing vorbis vbr");
+ error.Set(vorbis_encoder_domain,
+ "error initializing vorbis vbr");
vorbis_info_clear(&encoder->vi);
return false;
}
@@ -165,8 +155,8 @@ vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
encoder->audio_format.channels,
encoder->audio_format.sample_rate, -1.0,
encoder->bitrate * 1000, -1.0)) {
- g_set_error(error, vorbis_encoder_quark(), 0,
- "error initializing vorbis encoder");
+ error.Set(vorbis_encoder_domain,
+ "error initializing vorbis encoder");
vorbis_info_clear(&encoder->vi);
return false;
}
@@ -174,7 +164,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 +177,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 +193,35 @@ vorbis_encoder_send_header(struct vorbis_encoder *encoder)
}
static bool
-vorbis_encoder_open(struct encoder *_encoder,
- struct audio_format *audio_format,
- GError **error)
+vorbis_encoder_open(Encoder *_encoder,
+ AudioFormat &audio_format,
+ Error &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 +232,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, gcc_unused Error &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, gcc_unused Error &error)
{
struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
@@ -279,24 +265,24 @@ 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,
- G_GNUC_UNUSED GError **error)
+vorbis_encoder_tag(Encoder *_encoder, const Tag *tag,
+ gcc_unused Error &error)
{
struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
vorbis_comment comment;
@@ -308,46 +294,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)
+ gcc_unused Error &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 +336,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(gcc_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..493b07b61 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 &param,
+ gcc_unused Error &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 Error &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 Error &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/lame_encoder.c b/src/encoder/lame_encoder.c
deleted file mode 100644
index 3bb99ea28..000000000
--- a/src/encoder/lame_encoder.c
+++ /dev/null
@@ -1,300 +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 <lame/lame.h>
-#include <assert.h>
-#include <string.h>
-
-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,
- G_GNUC_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(G_GNUC_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/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,
-};
diff --git a/src/encoder/twolame_encoder.c b/src/encoder/twolame_encoder.c
deleted file mode 100644
index 934b2ab24..000000000
--- a/src/encoder/twolame_encoder.c
+++ /dev/null
@@ -1,308 +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 <twolame.h>
-#include <assert.h>
-#include <string.h>
-
-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, G_GNUC_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,
- G_GNUC_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(G_GNUC_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,
-};