aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoder
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/encoder/flac_encoder.c358
-rw-r--r--src/encoder/lame_encoder.c10
-rw-r--r--src/encoder/null_encoder.c124
-rw-r--r--src/encoder/twolame_encoder.c307
-rw-r--r--src/encoder/vorbis_encoder.c10
-rw-r--r--src/encoder/wave_encoder.c270
-rw-r--r--src/encoder_list.c26
-rw-r--r--src/encoder_list.h5
-rw-r--r--src/encoder_plugin.h17
9 files changed, 1124 insertions, 3 deletions
diff --git a/src/encoder/flac_encoder.c b/src/encoder/flac_encoder.c
new file mode 100644
index 000000000..395af5788
--- /dev/null
+++ b/src/encoder/flac_encoder.c
@@ -0,0 +1,358 @@
+/*
+ * Copyright (C) 2003-2009 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 "pcm_buffer.h"
+
+#include <assert.h>
+#include <string.h>
+
+#include <FLAC/stream_encoder.h>
+
+struct flac_encoder {
+ struct encoder encoder;
+
+ struct audio_format audio_format;
+ unsigned compression;
+
+ FLAC__StreamEncoder *fse;
+
+ struct pcm_buffer expand_buffer;
+
+ struct pcm_buffer buffer;
+ size_t buffer_length;
+};
+
+extern const struct encoder_plugin flac_encoder_plugin;
+
+
+static inline GQuark
+flac_encoder_quark(void)
+{
+ return g_quark_from_static_string("flac_encoder");
+}
+
+static bool
+flac_encoder_configure(struct flac_encoder *encoder,
+ const struct config_param *param, G_GNUC_UNUSED GError **error)
+{
+ encoder->compression = config_get_block_unsigned(param,
+ "compression", 5);
+
+ return true;
+}
+
+static struct encoder *
+flac_encoder_init(const struct config_param *param, GError **error)
+{
+ struct flac_encoder *encoder;
+
+ encoder = g_new(struct flac_encoder, 1);
+ encoder_struct_init(&encoder->encoder, &flac_encoder_plugin);
+
+ /* load configuration from "param" */
+ if (!flac_encoder_configure(encoder, param, error)) {
+ /* configuration has failed, roll back and return error */
+ g_free(encoder);
+ return NULL;
+ }
+
+ return &encoder->encoder;
+}
+
+static void
+flac_encoder_finish(struct 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);
+}
+
+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,
+ "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);
+ 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);
+ 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);
+ return false;
+ }
+ return true;
+}
+
+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)
+{
+ struct flac_encoder *encoder = (struct flac_encoder *) client_data;
+
+ char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + bytes);
+
+ //transfer data to buffer
+ memcpy( buffer + encoder->buffer_length, data, bytes);
+ encoder->buffer_length += bytes;
+
+ return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
+}
+
+static void
+flac_encoder_close(struct encoder *_encoder)
+{
+ struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
+
+ FLAC__stream_encoder_delete(encoder->fse);
+
+ pcm_buffer_deinit(&encoder->buffer);
+ pcm_buffer_deinit(&encoder->expand_buffer);
+}
+
+static bool
+flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
+ GError **error)
+{
+ struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
+ unsigned bits_per_sample;
+
+ encoder->audio_format = *audio_format;
+
+ /* FIXME: flac should support 32bit as well */
+ switch (audio_format->format) {
+ case SAMPLE_FORMAT_S8:
+ bits_per_sample = 8;
+ break;
+
+ case SAMPLE_FORMAT_S16:
+ bits_per_sample = 16;
+ break;
+
+ case SAMPLE_FORMAT_S24_P32:
+ bits_per_sample = 24;
+ break;
+
+ default:
+ bits_per_sample = 24;
+ audio_format->format = SAMPLE_FORMAT_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");
+ return false;
+ }
+
+ if (!flac_encoder_setup(encoder, bits_per_sample, error)) {
+ FLAC__stream_encoder_delete(encoder->fse);
+ return false;
+ }
+
+ encoder->buffer_length = 0;
+ pcm_buffer_init(&encoder->buffer);
+ pcm_buffer_init(&encoder->expand_buffer);
+
+ /* this immediatelly outputs data throught 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);
+
+ 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]);
+ flac_encoder_close(_encoder);
+ return false;
+ }
+ }
+#endif
+
+ return true;
+}
+
+
+static bool
+flac_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+{
+ struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
+
+ (void) FLAC__stream_encoder_finish(encoder->fse);
+ return true;
+}
+
+static inline void
+pcm8_to_flac(int32_t *out, const int8_t *in, unsigned num_samples)
+{
+ while (num_samples > 0) {
+ *out++ = *in++;
+ --num_samples;
+ }
+}
+
+static inline void
+pcm16_to_flac(int32_t *out, const int16_t *in, unsigned num_samples)
+{
+ while (num_samples > 0) {
+ *out++ = *in++;
+ --num_samples;
+ }
+}
+
+static bool
+flac_encoder_write(struct 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;
+
+ /* format conversion */
+
+ num_frames = length / audio_format_frame_size(&encoder->audio_format);
+ 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);
+ buffer = exbuffer;
+ break;
+
+ case SAMPLE_FORMAT_S16:
+ exbuffer = pcm_buffer_get(&encoder->expand_buffer, length*2);
+ pcm16_to_flac(exbuffer, data, num_samples);
+ buffer = exbuffer;
+ break;
+
+ case SAMPLE_FORMAT_S24_P32:
+ case SAMPLE_FORMAT_S32:
+ /* nothing need to be done; format is the same for
+ both mpd and libFLAC */
+ buffer = data;
+ break;
+ }
+
+ /* 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");
+ return false;
+ }
+
+ return true;
+}
+
+static size_t
+flac_encoder_read(struct encoder *_encoder, void *dest, size_t length)
+{
+ struct flac_encoder *encoder = (struct flac_encoder *)_encoder;
+ char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length);
+
+ if (length > encoder->buffer_length)
+ length = encoder->buffer_length;
+
+ memcpy(dest, buffer, length);
+
+ encoder->buffer_length -= length;
+ memmove(buffer, buffer + length, encoder->buffer_length);
+
+ return length;
+}
+
+static const char *
+flac_encoder_get_mime_type(G_GNUC_UNUSED struct 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,
+ .flush = flac_encoder_flush,
+ .write = flac_encoder_write,
+ .read = flac_encoder_read,
+ .get_mime_type = flac_encoder_get_mime_type,
+};
+
diff --git a/src/encoder/lame_encoder.c b/src/encoder/lame_encoder.c
index acaf4470f..279a676d9 100644
--- a/src/encoder/lame_encoder.c
+++ b/src/encoder/lame_encoder.c
@@ -17,6 +17,7 @@
* 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"
@@ -184,7 +185,7 @@ lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
{
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
- audio_format->bits = 16;
+ audio_format->format = SAMPLE_FORMAT_S16;
audio_format->channels = 2;
encoder->audio_format = *audio_format;
@@ -274,6 +275,12 @@ lame_encoder_read(struct encoder *_encoder, void *dest, size_t 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,
@@ -282,4 +289,5 @@ const struct encoder_plugin lame_encoder_plugin = {
.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
new file mode 100644
index 000000000..3a73e3987
--- /dev/null
+++ b/src/encoder/null_encoder.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2003-2009 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 "pcm_buffer.h"
+
+#include <assert.h>
+#include <string.h>
+
+struct null_encoder {
+ struct encoder encoder;
+
+ struct pcm_buffer buffer;
+ size_t buffer_length;
+};
+
+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;
+
+ pcm_buffer_deinit(&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_length = 0;
+ pcm_buffer_init(&encoder->buffer);
+
+ 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;
+ char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + length);
+
+ memcpy(buffer+encoder->buffer_length, data, length);
+
+ encoder->buffer_length += length;
+ return true;
+}
+
+static size_t
+null_encoder_read(struct encoder *_encoder, void *dest, size_t length)
+{
+ struct null_encoder *encoder = (struct null_encoder *)_encoder;
+ char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length);
+
+ if (length > encoder->buffer_length)
+ length = encoder->buffer_length;
+
+ memcpy(dest, buffer, length);
+
+ encoder->buffer_length -= length;
+ memmove(buffer, buffer + length, 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
new file mode 100644
index 000000000..5081db9fb
--- /dev/null
+++ b/src/encoder/twolame_encoder.c
@@ -0,0 +1,307 @@
+/*
+ * Copyright (C) 2003-2009 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,
+ .flush = twolame_encoder_flush,
+ .write = twolame_encoder_write,
+ .read = twolame_encoder_read,
+ .get_mime_type = twolame_encoder_get_mime_type,
+};
diff --git a/src/encoder/vorbis_encoder.c b/src/encoder/vorbis_encoder.c
index a5f6387f6..8e118e90a 100644
--- a/src/encoder/vorbis_encoder.c
+++ b/src/encoder/vorbis_encoder.c
@@ -17,6 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "config.h"
#include "encoder_api.h"
#include "encoder_plugin.h"
#include "tag.h"
@@ -211,7 +212,7 @@ vorbis_encoder_open(struct encoder *_encoder,
struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
bool ret;
- audio_format->bits = 16;
+ audio_format->format = SAMPLE_FORMAT_S16;
encoder->audio_format = *audio_format;
@@ -381,6 +382,12 @@ vorbis_encoder_read(struct encoder *_encoder, void *_dest, size_t length)
return nbytes;
}
+static const char *
+vorbis_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
+{
+ return "application/x-ogg";
+}
+
const struct encoder_plugin vorbis_encoder_plugin = {
.name = "vorbis",
.init = vorbis_encoder_init,
@@ -391,4 +398,5 @@ const struct encoder_plugin vorbis_encoder_plugin = {
.tag = vorbis_encoder_tag,
.write = vorbis_encoder_write,
.read = vorbis_encoder_read,
+ .get_mime_type = vorbis_encoder_get_mime_type,
};
diff --git a/src/encoder/wave_encoder.c b/src/encoder/wave_encoder.c
new file mode 100644
index 000000000..3f6e21845
--- /dev/null
+++ b/src/encoder/wave_encoder.c
@@ -0,0 +1,270 @@
+/*
+ * Copyright (C) 2003-2009 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 "pcm_buffer.h"
+
+#include <assert.h>
+#include <string.h>
+
+struct wave_encoder {
+ struct encoder encoder;
+ unsigned bits;
+
+ struct pcm_buffer buffer;
+ size_t buffer_length;
+};
+
+struct wave_header {
+ uint32_t id_riff;
+ uint32_t riff_size;
+ uint32_t id_wave;
+ uint32_t id_fmt;
+ uint32_t fmt_size;
+ uint16_t format;
+ uint16_t channels;
+ uint32_t freq;
+ uint32_t byterate;
+ uint16_t blocksize;
+ uint16_t bits;
+ uint32_t id_data;
+ 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)
+{
+ int data_size = 0x0FFFFFFF;
+
+ /* constants */
+ header->id_riff = GUINT32_TO_LE(0x46464952);
+ header->id_wave = GUINT32_TO_LE(0x45564157);
+ header->id_fmt = GUINT32_TO_LE(0x20746d66);
+ header->id_data = GUINT32_TO_LE(0x61746164);
+
+ /* wave format */
+ header->format = GUINT16_TO_LE(1); // PCM_FORMAT
+ header->channels = GUINT16_TO_LE(channels);
+ header->bits = GUINT16_TO_LE(bits);
+ header->freq = GUINT32_TO_LE(freq);
+ header->blocksize = GUINT16_TO_LE(block_size);
+ header->byterate = GUINT32_TO_LE(freq * block_size);
+
+ /* chunk sizes (fake data length) */
+ header->fmt_size = GUINT32_TO_LE(16);
+ header->data_size = GUINT32_TO_LE(data_size);
+ header->riff_size = GUINT32_TO_LE(4 + (8 + 16) +
+ (8 + data_size));
+}
+
+static struct encoder *
+wave_encoder_init(G_GNUC_UNUSED const struct config_param *param,
+ G_GNUC_UNUSED GError **error)
+{
+ struct wave_encoder *encoder;
+
+ encoder = g_new(struct wave_encoder, 1);
+ encoder_struct_init(&encoder->encoder, &wave_encoder_plugin);
+ pcm_buffer_init(&encoder->buffer);
+
+ return &encoder->encoder;
+}
+
+static void
+wave_encoder_finish(struct encoder *_encoder)
+{
+ struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
+
+ pcm_buffer_deinit(&encoder->buffer);
+ g_free(encoder);
+}
+
+static bool
+wave_encoder_open(struct encoder *_encoder,
+ G_GNUC_UNUSED struct audio_format *audio_format,
+ G_GNUC_UNUSED GError **error)
+{
+ struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
+ void *buffer;
+
+ assert(audio_format_valid(audio_format));
+
+ switch (audio_format->format) {
+ case SAMPLE_FORMAT_S8:
+ encoder->bits = 8;
+ break;
+
+ case SAMPLE_FORMAT_S16:
+ encoder->bits = 16;
+ break;
+
+ case SAMPLE_FORMAT_S24_P32:
+ encoder->bits = 24;
+ break;
+
+ case SAMPLE_FORMAT_S32:
+ encoder->bits = 32;
+ break;
+
+ default:
+ audio_format->format = SAMPLE_FORMAT_S16;
+ encoder->bits = 16;
+ break;
+ }
+
+ buffer = pcm_buffer_get(&encoder->buffer, sizeof(struct wave_header) );
+
+ /* create PCM wave header in initial buffer */
+ fill_wave_header((struct wave_header *) buffer,
+ audio_format->channels,
+ encoder->bits,
+ audio_format->sample_rate,
+ (encoder->bits / 8) * audio_format->channels );
+
+ encoder->buffer_length = sizeof(struct wave_header);
+ return true;
+}
+
+static inline size_t
+pcm16_to_wave(uint16_t *dst16, const uint16_t *src16, size_t length)
+{
+ size_t cnt = length >> 1;
+ while (cnt > 0) {
+ *dst16++ = GUINT16_TO_LE(*src16++);
+ cnt--;
+ }
+ return length;
+}
+
+static inline size_t
+pcm32_to_wave(uint32_t *dst32, const uint32_t *src32, size_t length)
+{
+ size_t cnt = length >> 2;
+ while (cnt > 0){
+ *dst32++ = GUINT32_TO_LE(*src32++);
+ cnt--;
+ }
+ return length;
+}
+
+static inline size_t
+pcm24_to_wave(uint8_t *dst8, const uint32_t *src32, size_t length)
+{
+ uint32_t value;
+ uint8_t *dst_old = dst8;
+
+ length = length >> 2;
+ while (length > 0){
+ value = *src32++;
+ *dst8++ = (value) & 0xFF;
+ *dst8++ = (value >> 8) & 0xFF;
+ *dst8++ = (value >> 16) & 0xFF;
+ length--;
+ }
+ //correct buffer length
+ return (dst8 - dst_old);
+}
+
+static bool
+wave_encoder_write(struct encoder *_encoder,
+ const void *src, size_t length,
+ G_GNUC_UNUSED GError **error)
+{
+ struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
+ void *dst;
+
+ dst = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + length);
+
+#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
+ switch (encoder->bits) {
+ case 8:
+ case 16:
+ case 32:// optimized cases
+ memcpy(dst, src, length);
+ break;
+ case 24:
+ length = pcm24_to_wave(dst, src, length);
+ break;
+ }
+#elif (G_BYTE_ORDER == G_BIG_ENDIAN)
+ switch (encoder->bits) {
+ case 8:
+ memcpy(dst, src, length);
+ break;
+ case 16:
+ length = pcm16_to_wave(dst, src, length);
+ break;
+ case 24:
+ length = pcm24_to_wave(dst, src, length);
+ break;
+ case 32:
+ length = pcm32_to_wave(dst, src, length);
+ break;
+ }
+#else
+#error G_BYTE_ORDER set to G_PDP_ENDIAN is not supported by wave_encoder
+#endif
+
+ encoder->buffer_length += length;
+ return true;
+}
+
+static size_t
+wave_encoder_read(struct encoder *_encoder, void *dest, size_t length)
+{
+ struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
+ uint8_t *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length );
+
+ if (length > encoder->buffer_length)
+ length = encoder->buffer_length;
+
+ memcpy(dest, buffer, length);
+
+ encoder->buffer_length -= length;
+ memmove(buffer, buffer + length, encoder->buffer_length);
+
+ return length;
+}
+
+static const char *
+wave_encoder_get_mime_type(G_GNUC_UNUSED struct 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,
+ .write = wave_encoder_write,
+ .read = wave_encoder_read,
+ .get_mime_type = wave_encoder_get_mime_type,
+};
diff --git a/src/encoder_list.c b/src/encoder_list.c
index d563b6bc8..d86753d50 100644
--- a/src/encoder_list.c
+++ b/src/encoder_list.c
@@ -17,22 +17,36 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "config.h"
#include "encoder_list.h"
#include "encoder_plugin.h"
-#include "config.h"
#include <string.h>
+extern const struct encoder_plugin null_encoder_plugin;
extern const struct encoder_plugin vorbis_encoder_plugin;
extern const struct encoder_plugin lame_encoder_plugin;
+extern const struct encoder_plugin twolame_encoder_plugin;
+extern const struct encoder_plugin wave_encoder_plugin;
+extern const struct encoder_plugin flac_encoder_plugin;
static const struct encoder_plugin *encoder_plugins[] = {
+ &null_encoder_plugin,
#ifdef ENABLE_VORBIS_ENCODER
&vorbis_encoder_plugin,
#endif
#ifdef ENABLE_LAME_ENCODER
&lame_encoder_plugin,
#endif
+#ifdef ENABLE_TWOLAME_ENCODER
+ &twolame_encoder_plugin,
+#endif
+#ifdef ENABLE_WAVE_ENCODER
+ &wave_encoder_plugin,
+#endif
+#ifdef ENABLE_FLAC_ENCODER
+ &flac_encoder_plugin,
+#endif
NULL
};
@@ -45,3 +59,13 @@ encoder_plugin_get(const char *name)
return NULL;
}
+
+void
+encoder_plugin_print_all_types(FILE * fp)
+{
+ for (unsigned i = 0; encoder_plugins[i] != NULL; ++i)
+ fprintf(fp, "%s ", encoder_plugins[i]->name);
+
+ fprintf(fp, "\n");
+ fflush(fp);
+}
diff --git a/src/encoder_list.h b/src/encoder_list.h
index bc20ad8c5..26caab242 100644
--- a/src/encoder_list.h
+++ b/src/encoder_list.h
@@ -20,6 +20,8 @@
#ifndef MPD_ENCODER_LIST_H
#define MPD_ENCODER_LIST_H
+#include <stdio.h>
+
struct encoder_plugin;
/**
@@ -32,4 +34,7 @@ struct encoder_plugin;
const struct encoder_plugin *
encoder_plugin_get(const char *name);
+void
+encoder_plugin_print_all_types(FILE * fp);
+
#endif
diff --git a/src/encoder_plugin.h b/src/encoder_plugin.h
index 958fe97cf..e8f2e4527 100644
--- a/src/encoder_plugin.h
+++ b/src/encoder_plugin.h
@@ -58,6 +58,8 @@ struct encoder_plugin {
GError **error);
size_t (*read)(struct encoder *encoder, void *dest, size_t length);
+
+ const char *(*get_mime_type)(struct encoder *encoder);
};
/**
@@ -192,4 +194,19 @@ encoder_read(struct encoder *encoder, void *dest, size_t length)
return encoder->plugin->read(encoder, dest, length);
}
+/**
+ * Get mime type of encoded content.
+ *
+ * @param plugin the encoder plugin
+ * @return an constant string, NULL on failure
+ */
+static inline const char *
+encoder_get_mime_type(struct encoder *encoder)
+{
+ /* this method is optional */
+ return encoder->plugin->get_mime_type != NULL
+ ? encoder->plugin->get_mime_type(encoder)
+ : NULL;
+}
+
#endif