aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoder
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/encoder/OggStream.hxx128
-rw-r--r--src/encoder/OpusEncoderPlugin.cxx429
-rw-r--r--src/encoder/OpusEncoderPlugin.hxx25
-rw-r--r--src/encoder/VorbisEncoderPlugin.cxx377
-rw-r--r--src/encoder/VorbisEncoderPlugin.hxx25
-rw-r--r--src/encoder/flac_encoder.c35
-rw-r--r--src/encoder/lame_encoder.c7
-rw-r--r--src/encoder/null_encoder.c23
-rw-r--r--src/encoder/twolame_encoder.c9
-rw-r--r--src/encoder/vorbis_encoder.c407
-rw-r--r--src/encoder/wave_encoder.c24
-rw-r--r--src/encoder_list.c8
-rw-r--r--src/encoder_list.h8
-rw-r--r--src/encoder_plugin.h2
14 files changed, 1037 insertions, 470 deletions
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..8d2c0974b
--- /dev/null
+++ b/src/encoder/OpusEncoderPlugin.cxx
@@ -0,0 +1,429 @@
+/*
+ * 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 "encoder_api.h"
+#include "encoder_plugin.h"
+#include "audio_format.h"
+#include "mpd_error.h"
+
+#include <opus.h>
+#include <ogg/ogg.h>
+
+#include <assert.h>
+
+#undef G_LOG_DOMAIN
+#define G_LOG_DOMAIN "opus_encoder"
+
+struct opus_encoder {
+ /** the base class */
+ struct encoder encoder;
+
+ /* configuration */
+
+ opus_int32 bitrate;
+ int complexity;
+ int signal;
+
+ /* runtime information */
+
+ struct audio_format 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_struct_init(&encoder, &opus_encoder_plugin);
+ }
+};
+
+gcc_const
+static inline GQuark
+opus_encoder_quark(void)
+{
+ return g_quark_from_static_string("opus_encoder");
+}
+
+static bool
+opus_encoder_configure(struct opus_encoder *encoder,
+ const struct config_param *param, GError **error_r)
+{
+ const char *value = config_get_block_string(param, "bitrate", "auto");
+ if (strcmp(value, "auto") == 0)
+ encoder->bitrate = OPUS_AUTO;
+ else if (strcmp(value, "max") == 0)
+ encoder->bitrate = OPUS_BITRATE_MAX;
+ else {
+ char *endptr;
+ encoder->bitrate = strtoul(value, &endptr, 10);
+ if (endptr == value || *endptr != 0 ||
+ encoder->bitrate < 500 || encoder->bitrate > 512000) {
+ g_set_error(error_r, opus_encoder_quark(), 0,
+ "Invalid bit rate");
+ return false;
+ }
+ }
+
+ encoder->complexity = config_get_block_unsigned(param, "complexity",
+ 10);
+ if (encoder->complexity > 10) {
+ g_set_error(error_r, opus_encoder_quark(), 0,
+ "Invalid complexity");
+ return false;
+ }
+
+ value = config_get_block_string(param, "signal", "auto");
+ if (strcmp(value, "auto") == 0)
+ encoder->bitrate = OPUS_AUTO;
+ else if (strcmp(value, "voice") == 0)
+ encoder->bitrate = OPUS_SIGNAL_VOICE;
+ else if (strcmp(value, "music") == 0)
+ encoder->bitrate = OPUS_SIGNAL_MUSIC;
+ else {
+ g_set_error(error_r, opus_encoder_quark(), 0,
+ "Invalid signal");
+ return false;
+ }
+
+ return true;
+}
+
+static struct encoder *
+opus_encoder_init(const struct config_param *param, GError **error)
+{
+ opus_encoder *encoder = new opus_encoder();
+
+ /* load configuration from "param" */
+ if (!opus_encoder_configure(encoder, param, error)) {
+ /* configuration has failed, roll back and return error */
+ delete encoder;
+ return NULL;
+ }
+
+ return &encoder->encoder;
+}
+
+static void
+opus_encoder_finish(struct 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(struct encoder *_encoder,
+ struct audio_format *audio_format,
+ GError **error_r)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+
+ /* libopus supports only 48 kHz */
+ audio_format->sample_rate = 48000;
+
+ if (audio_format->channels > 2)
+ audio_format->channels = 1;
+
+ switch ((enum sample_format)audio_format->format) {
+ case SAMPLE_FORMAT_S16:
+ case SAMPLE_FORMAT_FLOAT:
+ break;
+
+ case SAMPLE_FORMAT_S8:
+ audio_format->format = SAMPLE_FORMAT_S16;
+ break;
+
+ default:
+ audio_format->format = SAMPLE_FORMAT_FLOAT;
+ break;
+ }
+
+ encoder->audio_format = *audio_format;
+ encoder->frame_size = audio_format_frame_size(audio_format);
+
+ int error;
+ encoder->enc = opus_encoder_create(audio_format->sample_rate,
+ audio_format->channels,
+ OPUS_APPLICATION_AUDIO,
+ &error);
+ if (encoder->enc == nullptr) {
+ g_set_error_literal(error_r, opus_encoder_quark(), error,
+ opus_strerror(error));
+ return false;
+ }
+
+ opus_encoder_ctl(encoder->enc, OPUS_SET_BITRATE(encoder->bitrate));
+ opus_encoder_ctl(encoder->enc,
+ OPUS_SET_COMPLEXITY(encoder->complexity));
+ opus_encoder_ctl(encoder->enc, OPUS_SET_SIGNAL(encoder->signal));
+
+ opus_encoder_ctl(encoder->enc, OPUS_GET_LOOKAHEAD(&encoder->lookahead));
+
+ encoder->buffer_frames = audio_format->sample_rate / 50;
+ encoder->buffer_size = encoder->frame_size * encoder->buffer_frames;
+ encoder->buffer_position = 0;
+ encoder->buffer = (unsigned char *)g_malloc(encoder->buffer_size);
+
+ encoder->stream.Initialize(g_random_int());
+ encoder->packetno = 0;
+
+ return true;
+}
+
+static void
+opus_encoder_close(struct encoder *_encoder)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+
+ encoder->stream.Deinitialize();
+ g_free(encoder->buffer);
+ opus_encoder_destroy(encoder->enc);
+}
+
+static bool
+opus_encoder_do_encode(struct opus_encoder *encoder, bool eos,
+ GError **error_r)
+{
+ assert(encoder->buffer_position == encoder->buffer_size);
+
+ opus_int32 result =
+ encoder->audio_format.format == SAMPLE_FORMAT_S16
+ ? opus_encode(encoder->enc,
+ (const opus_int16 *)encoder->buffer,
+ encoder->buffer_frames,
+ encoder->buffer2,
+ sizeof(encoder->buffer2))
+ : opus_encode_float(encoder->enc,
+ (const float *)encoder->buffer,
+ encoder->buffer_frames,
+ encoder->buffer2,
+ sizeof(encoder->buffer2));
+ if (result < 0) {
+ g_set_error_literal(error_r, opus_encoder_quark(), 0,
+ "Opus encoder error");
+ return false;
+ }
+
+ encoder->granulepos += encoder->buffer_frames;
+
+ ogg_packet packet;
+ packet.packet = encoder->buffer2;
+ packet.bytes = result;
+ packet.b_o_s = false;
+ packet.e_o_s = eos;
+ packet.granulepos = encoder->granulepos;
+ packet.packetno = encoder->packetno++;
+ encoder->stream.PacketIn(packet);
+
+ encoder->buffer_position = 0;
+
+ return true;
+}
+
+static bool
+opus_encoder_end(struct encoder *_encoder, GError **error_r)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+
+ encoder->stream.Flush();
+
+ memset(encoder->buffer + encoder->buffer_position, 0,
+ encoder->buffer_size - encoder->buffer_position);
+ encoder->buffer_position = encoder->buffer_size;
+
+ return opus_encoder_do_encode(encoder, true, error_r);
+}
+
+static bool
+opus_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+
+ encoder->stream.Flush();
+ return true;
+}
+
+static bool
+opus_encoder_write_silence(struct opus_encoder *encoder, unsigned fill_frames,
+ GError **error_r)
+{
+ size_t fill_bytes = fill_frames * encoder->frame_size;
+
+ while (fill_bytes > 0) {
+ size_t nbytes =
+ encoder->buffer_size - encoder->buffer_position;
+ if (nbytes > fill_bytes)
+ nbytes = fill_bytes;
+
+ memset(encoder->buffer + encoder->buffer_position,
+ 0, nbytes);
+ encoder->buffer_position += nbytes;
+ fill_bytes -= nbytes;
+
+ if (encoder->buffer_position == encoder->buffer_size &&
+ !opus_encoder_do_encode(encoder, false, error_r))
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+opus_encoder_write(struct encoder *_encoder,
+ const void *_data, size_t length,
+ GError **error_r)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+ const uint8_t *data = (const uint8_t *)_data;
+
+ if (encoder->lookahead > 0) {
+ /* generate some silence at the beginning of the
+ stream */
+
+ assert(encoder->buffer_position == 0);
+
+ if (!opus_encoder_write_silence(encoder, encoder->lookahead,
+ error_r))
+ return false;
+
+ encoder->lookahead = 0;
+ }
+
+ while (length > 0) {
+ size_t nbytes =
+ encoder->buffer_size - encoder->buffer_position;
+ if (nbytes > length)
+ nbytes = length;
+
+ memcpy(encoder->buffer + encoder->buffer_position,
+ data, nbytes);
+ data += nbytes;
+ length -= nbytes;
+ encoder->buffer_position += nbytes;
+
+ if (encoder->buffer_position == encoder->buffer_size &&
+ !opus_encoder_do_encode(encoder, false, error_r))
+ return false;
+ }
+
+ return true;
+}
+
+static void
+opus_encoder_generate_head(struct opus_encoder *encoder)
+{
+ unsigned char header[19];
+ memcpy(header, "OpusHead", 8);
+ header[8] = 1;
+ header[9] = encoder->audio_format.channels;
+ *(uint16_t *)(header + 10) = GUINT16_TO_LE(encoder->lookahead);
+ *(uint32_t *)(header + 12) =
+ GUINT32_TO_LE(encoder->audio_format.sample_rate);
+ header[16] = 0;
+ header[17] = 0;
+ header[18] = 0;
+
+ ogg_packet packet;
+ packet.packet = header;
+ packet.bytes = 19;
+ packet.b_o_s = true;
+ packet.e_o_s = false;
+ packet.granulepos = 0;
+ packet.packetno = encoder->packetno++;
+ encoder->stream.PacketIn(packet);
+ encoder->stream.Flush();
+}
+
+static void
+opus_encoder_generate_tags(struct opus_encoder *encoder)
+{
+ const char *version = opus_get_version_string();
+ size_t version_length = strlen(version);
+
+ size_t comments_size = 8 + 4 + version_length + 4;
+ unsigned char *comments = (unsigned char *)g_malloc(comments_size);
+ memcpy(comments, "OpusTags", 8);
+ *(uint32_t *)(comments + 8) = GUINT32_TO_LE(version_length);
+ memcpy(comments + 12, version, version_length);
+ *(uint32_t *)(comments + 12 + version_length) = GUINT32_TO_LE(0);
+
+ ogg_packet packet;
+ packet.packet = comments;
+ packet.bytes = comments_size;
+ packet.b_o_s = false;
+ packet.e_o_s = false;
+ packet.granulepos = 0;
+ packet.packetno = encoder->packetno++;
+ encoder->stream.PacketIn(packet);
+ encoder->stream.Flush();
+
+ g_free(comments);
+}
+
+static size_t
+opus_encoder_read(struct encoder *_encoder, void *dest, size_t length)
+{
+ struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
+
+ if (encoder->packetno == 0)
+ opus_encoder_generate_head(encoder);
+ else if (encoder->packetno == 1)
+ opus_encoder_generate_tags(encoder);
+
+ return encoder->stream.PageOut(dest, length);
+}
+
+static const char *
+opus_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
+{
+ return "audio/ogg";
+}
+
+const struct encoder_plugin 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..f54377202
--- /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 encoder_plugin opus_encoder_plugin;
+
+#endif
diff --git a/src/encoder/VorbisEncoderPlugin.cxx b/src/encoder/VorbisEncoderPlugin.cxx
new file mode 100644
index 000000000..dc7ef0d5e
--- /dev/null
+++ b/src/encoder/VorbisEncoderPlugin.cxx
@@ -0,0 +1,377 @@
+/*
+ * 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 "VorbisEncoderPlugin.hxx"
+#include "OggStream.hxx"
+#include "encoder_api.h"
+#include "encoder_plugin.h"
+#include "tag.h"
+#include "audio_format.h"
+#include "mpd_error.h"
+
+#include <vorbis/vorbisenc.h>
+
+#include <assert.h>
+
+#undef G_LOG_DOMAIN
+#define G_LOG_DOMAIN "vorbis_encoder"
+
+struct vorbis_encoder {
+ /** the base class */
+ struct encoder encoder;
+
+ /* configuration */
+
+ float quality;
+ int bitrate;
+
+ /* runtime information */
+
+ struct audio_format audio_format;
+
+ vorbis_dsp_state vd;
+ vorbis_block vb;
+ vorbis_info vi;
+
+ OggStream stream;
+
+ vorbis_encoder() {
+ encoder_struct_init(&encoder, &vorbis_encoder_plugin);
+ }
+};
+
+static inline GQuark
+vorbis_encoder_quark(void)
+{
+ return g_quark_from_static_string("vorbis_encoder");
+}
+
+static bool
+vorbis_encoder_configure(struct vorbis_encoder *encoder,
+ const struct config_param *param, GError **error)
+{
+ const char *value = config_get_block_string(param, "quality", nullptr);
+ if (value != nullptr) {
+ /* a quality was configured (VBR) */
+
+ char *endptr;
+ encoder->quality = g_ascii_strtod(value, &endptr);
+
+ 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);
+ return false;
+ }
+
+ if (config_get_block_string(param, "bitrate", nullptr) != nullptr) {
+ g_set_error(error, vorbis_encoder_quark(), 0,
+ "quality and bitrate are "
+ "both defined (line %i)",
+ param->line);
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ value = config_get_block_string(param, "bitrate", nullptr);
+ if (value == nullptr) {
+ g_set_error(error, vorbis_encoder_quark(), 0,
+ "neither bitrate nor quality defined "
+ "at line %i",
+ param->line);
+ return false;
+ }
+
+ encoder->quality = -2.0;
+
+ 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);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static struct encoder *
+vorbis_encoder_init(const struct config_param *param, GError **error)
+{
+ 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 */
+ delete encoder;
+ return nullptr;
+ }
+
+ return &encoder->encoder;
+}
+
+static void
+vorbis_encoder_finish(struct 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 */
+ delete encoder;
+}
+
+static bool
+vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
+{
+ vorbis_info_init(&encoder->vi);
+
+ if (encoder->quality >= -1.0) {
+ /* a quality was configured (VBR) */
+
+ if (0 != vorbis_encode_init_vbr(&encoder->vi,
+ 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");
+ vorbis_info_clear(&encoder->vi);
+ return false;
+ }
+ } else {
+ /* a bit rate was configured */
+
+ if (0 != vorbis_encode_init(&encoder->vi,
+ 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");
+ vorbis_info_clear(&encoder->vi);
+ return false;
+ }
+ }
+
+ vorbis_analysis_init(&encoder->vd, &encoder->vi);
+ vorbis_block_init(&encoder->vd, &encoder->vb);
+ encoder->stream.Initialize(g_random_int());
+
+ return true;
+}
+
+static void
+vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc)
+{
+ ogg_packet packet, comments, codebooks;
+
+ vorbis_analysis_headerout(&encoder->vd, vc,
+ &packet, &comments, &codebooks);
+
+ encoder->stream.PacketIn(packet);
+ encoder->stream.PacketIn(comments);
+ encoder->stream.PacketIn(codebooks);
+}
+
+static void
+vorbis_encoder_send_header(struct vorbis_encoder *encoder)
+{
+ vorbis_comment vc;
+
+ vorbis_comment_init(&vc);
+ vorbis_encoder_headerout(encoder, &vc);
+ vorbis_comment_clear(&vc);
+}
+
+static bool
+vorbis_encoder_open(struct encoder *_encoder,
+ struct audio_format *audio_format,
+ GError **error)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ audio_format->format = SAMPLE_FORMAT_FLOAT;
+
+ encoder->audio_format = *audio_format;
+
+ if (!vorbis_encoder_reinit(encoder, error))
+ return false;
+
+ vorbis_encoder_send_header(encoder);
+
+ return true;
+}
+
+static void
+vorbis_encoder_clear(struct vorbis_encoder *encoder)
+{
+ 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)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ vorbis_encoder_clear(encoder);
+}
+
+static void
+vorbis_encoder_blockout(struct vorbis_encoder *encoder)
+{
+ while (vorbis_analysis_blockout(&encoder->vd, &encoder->vb) == 1) {
+ vorbis_analysis(&encoder->vb, nullptr);
+ vorbis_bitrate_addblock(&encoder->vb);
+
+ ogg_packet packet;
+ while (vorbis_bitrate_flushpacket(&encoder->vd, &packet))
+ encoder->stream.PacketIn(packet);
+ }
+}
+
+static bool
+vorbis_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ encoder->stream.Flush();
+ return true;
+}
+
+static bool
+vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ vorbis_analysis_wrote(&encoder->vd, 0);
+ vorbis_encoder_blockout(encoder);
+
+ /* reinitialize vorbis_dsp_state and vorbis_block to reset the
+ end-of-stream marker */
+ vorbis_block_clear(&encoder->vb);
+ vorbis_dsp_clear(&encoder->vd);
+ vorbis_analysis_init(&encoder->vd, &encoder->vi);
+ vorbis_block_init(&encoder->vd, &encoder->vb);
+
+ encoder->stream.Flush();
+ return true;
+}
+
+static void
+copy_tag_to_vorbis_comment(vorbis_comment *vc, const struct 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);
+ g_free(name);
+ }
+}
+
+static bool
+vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag,
+ G_GNUC_UNUSED GError **error)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+ vorbis_comment comment;
+
+ /* write the vorbis_comment object */
+
+ vorbis_comment_init(&comment);
+ copy_tag_to_vorbis_comment(&comment, tag);
+
+ /* reset ogg_stream_state and begin a new stream */
+
+ encoder->stream.Reinitialize(g_random_int());
+
+ /* send that vorbis_comment to the ogg_stream_state */
+
+ vorbis_encoder_headerout(encoder, &comment);
+ vorbis_comment_clear(&comment);
+
+ return true;
+}
+
+static void
+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++;
+}
+
+static bool
+vorbis_encoder_write(struct encoder *_encoder,
+ const void *data, size_t length,
+ G_GNUC_UNUSED GError **error)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ unsigned num_frames = length
+ / audio_format_frame_size(&encoder->audio_format);
+
+ /* this is for only 16-bit audio */
+
+ 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);
+ return true;
+}
+
+static size_t
+vorbis_encoder_read(struct encoder *_encoder, void *dest, size_t length)
+{
+ struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
+
+ return encoder->stream.PageOut(dest, length);
+}
+
+static const char *
+vorbis_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
+{
+ return "audio/ogg";
+}
+
+const struct encoder_plugin 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..4cddf1b11
--- /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 encoder_plugin vorbis_encoder_plugin;
+
+#endif
diff --git a/src/encoder/flac_encoder.c b/src/encoder/flac_encoder.c
index e32588e29..db6503fb0 100644
--- a/src/encoder/flac_encoder.c
+++ b/src/encoder/flac_encoder.c
@@ -22,14 +22,18 @@
#include "encoder_plugin.h"
#include "audio_format.h"
#include "pcm_buffer.h"
-#include "fifo_buffer.h"
-#include "growing_fifo.h"
+#include "util/fifo_buffer.h"
+#include "util/growing_fifo.h"
#include <assert.h>
#include <string.h>
#include <FLAC/stream_encoder.h>
+#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
+#error libFLAC is too old
+#endif
+
struct flac_encoder {
struct encoder encoder;
@@ -98,8 +102,6 @@ static bool
flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
GError **error)
{
-#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
-#else
if ( !FLAC__stream_encoder_set_compression_level(encoder->fse,
encoder->compression)) {
g_set_error(error, flac_encoder_quark(), 0,
@@ -107,7 +109,7 @@ flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
encoder->compression);
return false;
}
-#endif
+
if ( !FLAC__stream_encoder_set_channels(encoder->fse,
encoder->audio_format.channels)) {
g_set_error(error, flac_encoder_quark(), 0,
@@ -135,11 +137,7 @@ flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample,
static FLAC__StreamEncoderWriteStatus
flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse,
const FLAC__byte data[],
-#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
- unsigned bytes,
-#else
size_t bytes,
-#endif
G_GNUC_UNUSED unsigned samples,
G_GNUC_UNUSED unsigned current_frame, void *client_data)
{
@@ -209,24 +207,6 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
/* 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;
@@ -242,7 +222,6 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
return false;
}
}
-#endif
return true;
}
diff --git a/src/encoder/lame_encoder.c b/src/encoder/lame_encoder.c
index 3bb99ea28..53594d34e 100644
--- a/src/encoder/lame_encoder.c
+++ b/src/encoder/lame_encoder.c
@@ -23,6 +23,9 @@
#include "audio_format.h"
#include <lame/lame.h>
+
+#include <glib.h>
+
#include <assert.h>
#include <string.h>
@@ -225,7 +228,7 @@ lame_encoder_close(struct encoder *_encoder)
static bool
lame_encoder_write(struct encoder *_encoder,
const void *data, size_t length,
- G_GNUC_UNUSED GError **error)
+ gcc_unused GError **error)
{
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
unsigned num_frames;
@@ -283,7 +286,7 @@ lame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
}
static const char *
-lame_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
+lame_encoder_get_mime_type(gcc_unused struct encoder *_encoder)
{
return "audio/mpeg";
}
diff --git a/src/encoder/null_encoder.c b/src/encoder/null_encoder.c
index 48cdf139b..b973adf76 100644
--- a/src/encoder/null_encoder.c
+++ b/src/encoder/null_encoder.c
@@ -20,8 +20,11 @@
#include "config.h"
#include "encoder_api.h"
#include "encoder_plugin.h"
-#include "fifo_buffer.h"
-#include "growing_fifo.h"
+#include "util/fifo_buffer.h"
+#include "util/growing_fifo.h"
+#include "gcc.h"
+
+#include <glib.h>
#include <assert.h>
#include <string.h>
@@ -34,15 +37,9 @@ struct null_encoder {
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)
+null_encoder_init(gcc_unused const struct config_param *param,
+ gcc_unused GError **error)
{
struct null_encoder *encoder;
@@ -71,8 +68,8 @@ null_encoder_close(struct encoder *_encoder)
static bool
null_encoder_open(struct encoder *_encoder,
- G_GNUC_UNUSED struct audio_format *audio_format,
- G_GNUC_UNUSED GError **error)
+ gcc_unused struct audio_format *audio_format,
+ gcc_unused GError **error)
{
struct null_encoder *encoder = (struct null_encoder *)_encoder;
@@ -83,7 +80,7 @@ null_encoder_open(struct encoder *_encoder,
static bool
null_encoder_write(struct encoder *_encoder,
const void *data, size_t length,
- G_GNUC_UNUSED GError **error)
+ gcc_unused GError **error)
{
struct null_encoder *encoder = (struct null_encoder *)_encoder;
diff --git a/src/encoder/twolame_encoder.c b/src/encoder/twolame_encoder.c
index 934b2ab24..ff184977b 100644
--- a/src/encoder/twolame_encoder.c
+++ b/src/encoder/twolame_encoder.c
@@ -23,6 +23,9 @@
#include "audio_format.h"
#include <twolame.h>
+
+#include <glib.h>
+
#include <assert.h>
#include <string.h>
@@ -224,7 +227,7 @@ twolame_encoder_close(struct encoder *_encoder)
}
static bool
-twolame_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
+twolame_encoder_flush(struct encoder *_encoder, gcc_unused GError **error)
{
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
@@ -235,7 +238,7 @@ twolame_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
static bool
twolame_encoder_write(struct encoder *_encoder,
const void *data, size_t length,
- G_GNUC_UNUSED GError **error)
+ gcc_unused GError **error)
{
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
unsigned num_frames;
@@ -289,7 +292,7 @@ twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
}
static const char *
-twolame_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
+twolame_encoder_get_mime_type(gcc_unused struct encoder *_encoder)
{
return "audio/mpeg";
}
diff --git a/src/encoder/vorbis_encoder.c b/src/encoder/vorbis_encoder.c
deleted file mode 100644
index 468cf38ee..000000000
--- a/src/encoder/vorbis_encoder.c
+++ /dev/null
@@ -1,407 +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 "tag.h"
-#include "audio_format.h"
-#include "mpd_error.h"
-
-#include <vorbis/vorbisenc.h>
-
-#include <assert.h>
-
-#undef G_LOG_DOMAIN
-#define G_LOG_DOMAIN "vorbis_encoder"
-
-struct vorbis_encoder {
- /** the base class */
- struct encoder encoder;
-
- /* configuration */
-
- float quality;
- int bitrate;
-
- /* runtime information */
-
- struct audio_format audio_format;
-
- ogg_stream_state os;
-
- vorbis_dsp_state vd;
- vorbis_block vb;
- vorbis_info vi;
-
- bool flush;
-};
-
-extern const struct encoder_plugin vorbis_encoder_plugin;
-
-static inline GQuark
-vorbis_encoder_quark(void)
-{
- return g_quark_from_static_string("vorbis_encoder");
-}
-
-static bool
-vorbis_encoder_configure(struct vorbis_encoder *encoder,
- const struct config_param *param, GError **error)
-{
- const char *value = config_get_block_string(param, "quality", NULL);
- if (value != NULL) {
- /* a quality was configured (VBR) */
-
- char *endptr;
- encoder->quality = g_ascii_strtod(value, &endptr);
-
- 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);
- 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);
- 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);
- return false;
- }
-
- encoder->quality = -2.0;
-
- 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);
- return false;
- }
- }
-
- return true;
-}
-
-static struct encoder *
-vorbis_encoder_init(const struct config_param *param, GError **error)
-{
- struct vorbis_encoder *encoder = g_new(struct vorbis_encoder, 1);
- encoder_struct_init(&encoder->encoder, &vorbis_encoder_plugin);
-
- /* load configuration from "param" */
- if (!vorbis_encoder_configure(encoder, param, error)) {
- /* configuration has failed, roll back and return error */
- g_free(encoder);
- return NULL;
- }
-
- return &encoder->encoder;
-}
-
-static void
-vorbis_encoder_finish(struct 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);
-}
-
-static bool
-vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
-{
- vorbis_info_init(&encoder->vi);
-
- if (encoder->quality >= -1.0) {
- /* a quality was configured (VBR) */
-
- if (0 != vorbis_encode_init_vbr(&encoder->vi,
- 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");
- vorbis_info_clear(&encoder->vi);
- return false;
- }
- } else {
- /* a bit rate was configured */
-
- if (0 != vorbis_encode_init(&encoder->vi,
- 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");
- vorbis_info_clear(&encoder->vi);
- return false;
- }
- }
-
- vorbis_analysis_init(&encoder->vd, &encoder->vi);
- vorbis_block_init(&encoder->vd, &encoder->vb);
- ogg_stream_init(&encoder->os, g_random_int());
-
- return true;
-}
-
-static void
-vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc)
-{
- ogg_packet packet, comments, codebooks;
-
- 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);
-}
-
-static void
-vorbis_encoder_send_header(struct vorbis_encoder *encoder)
-{
- vorbis_comment vc;
-
- vorbis_comment_init(&vc);
- vorbis_encoder_headerout(encoder, &vc);
- vorbis_comment_clear(&vc);
-}
-
-static bool
-vorbis_encoder_open(struct encoder *_encoder,
- struct audio_format *audio_format,
- GError **error)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
-
- audio_format->format = SAMPLE_FORMAT_S16;
-
- 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);
- vorbis_block_clear(&encoder->vb);
- vorbis_dsp_clear(&encoder->vd);
- vorbis_info_clear(&encoder->vi);
-}
-
-static void
-vorbis_encoder_close(struct encoder *_encoder)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
-
- vorbis_encoder_clear(encoder);
-}
-
-static void
-vorbis_encoder_blockout(struct vorbis_encoder *encoder)
-{
- while (vorbis_analysis_blockout(&encoder->vd, &encoder->vb) == 1) {
- vorbis_analysis(&encoder->vb, NULL);
- vorbis_bitrate_addblock(&encoder->vb);
-
- ogg_packet packet;
- while (vorbis_bitrate_flushpacket(&encoder->vd, &packet))
- ogg_stream_packetin(&encoder->os, &packet);
- }
-}
-
-static bool
-vorbis_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
-
- encoder->flush = true;
- return true;
-}
-
-static bool
-vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
-
- vorbis_analysis_wrote(&encoder->vd, 0);
- vorbis_encoder_blockout(encoder);
-
- /* reinitialize vorbis_dsp_state and vorbis_block to reset the
- end-of-stream marker */
- vorbis_block_clear(&encoder->vb);
- vorbis_dsp_clear(&encoder->vd);
- vorbis_analysis_init(&encoder->vd, &encoder->vi);
- vorbis_block_init(&encoder->vd, &encoder->vb);
-
- encoder->flush = true;
- return true;
-}
-
-static void
-copy_tag_to_vorbis_comment(vorbis_comment *vc, const struct 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);
- g_free(name);
- }
-}
-
-static bool
-vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag,
- G_GNUC_UNUSED GError **error)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
- vorbis_comment comment;
-
- /* write the vorbis_comment object */
-
- vorbis_comment_init(&comment);
- copy_tag_to_vorbis_comment(&comment, tag);
-
- /* reset ogg_stream_state and begin a new stream */
-
- ogg_stream_reset_serialno(&encoder->os, 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)
-{
- for (unsigned i = 0; i < num_frames; i++)
- for (unsigned j = 0; j < num_channels; j++)
- dest[j][i] = *src++ / 32768.0;
-}
-
-static bool
-vorbis_encoder_write(struct encoder *_encoder,
- const void *data, size_t length,
- G_GNUC_UNUSED GError **error)
-{
- struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
-
- unsigned num_frames = length
- / audio_format_frame_size(&encoder->audio_format);
-
- /* 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);
-
- vorbis_analysis_wrote(&encoder->vd, num_frames);
- vorbis_encoder_blockout(encoder);
- return true;
-}
-
-static size_t
-vorbis_encoder_read(struct 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;
-}
-
-static const char *
-vorbis_encoder_get_mime_type(G_GNUC_UNUSED struct 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,
-};
diff --git a/src/encoder/wave_encoder.c b/src/encoder/wave_encoder.c
index 9eeb4d513..9b3c21fca 100644
--- a/src/encoder/wave_encoder.c
+++ b/src/encoder/wave_encoder.c
@@ -20,8 +20,10 @@
#include "config.h"
#include "encoder_api.h"
#include "encoder_plugin.h"
-#include "fifo_buffer.h"
-#include "growing_fifo.h"
+#include "util/fifo_buffer.h"
+#include "util/growing_fifo.h"
+
+#include <glib.h>
#include <assert.h>
#include <string.h>
@@ -51,12 +53,6 @@ struct wave_header {
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)
@@ -85,8 +81,8 @@ fill_wave_header(struct wave_header *header, int channels, int bits,
}
static struct encoder *
-wave_encoder_init(G_GNUC_UNUSED const struct config_param *param,
- G_GNUC_UNUSED GError **error)
+wave_encoder_init(gcc_unused const struct config_param *param,
+ gcc_unused GError **error)
{
struct wave_encoder *encoder;
@@ -106,8 +102,8 @@ wave_encoder_finish(struct encoder *_encoder)
static bool
wave_encoder_open(struct encoder *_encoder,
- G_GNUC_UNUSED struct audio_format *audio_format,
- G_GNUC_UNUSED GError **error)
+ gcc_unused struct audio_format *audio_format,
+ gcc_unused GError **error)
{
struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
@@ -202,7 +198,7 @@ pcm24_to_wave(uint8_t *dst8, const uint32_t *src32, size_t length)
static bool
wave_encoder_write(struct encoder *_encoder,
const void *src, size_t length,
- G_GNUC_UNUSED GError **error)
+ gcc_unused GError **error)
{
struct wave_encoder *encoder = (struct wave_encoder *)_encoder;
@@ -261,7 +257,7 @@ 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 struct encoder *_encoder)
{
return "audio/wav";
}
diff --git a/src/encoder_list.c b/src/encoder_list.c
index 2326c1099..029b4be34 100644
--- a/src/encoder_list.c
+++ b/src/encoder_list.c
@@ -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
@@ -20,11 +20,12 @@
#include "config.h"
#include "encoder_list.h"
#include "encoder_plugin.h"
+#include "encoder/VorbisEncoderPlugin.hxx"
+#include "encoder/OpusEncoderPlugin.hxx"
#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;
@@ -35,6 +36,9 @@ const struct encoder_plugin *const encoder_plugins[] = {
#ifdef ENABLE_VORBIS_ENCODER
&vorbis_encoder_plugin,
#endif
+#ifdef HAVE_OPUS
+ &opus_encoder_plugin,
+#endif
#ifdef ENABLE_LAME_ENCODER
&lame_encoder_plugin,
#endif
diff --git a/src/encoder_list.h b/src/encoder_list.h
index fb1c9bf9c..31663c751 100644
--- a/src/encoder_list.h
+++ b/src/encoder_list.h
@@ -30,6 +30,10 @@ extern const struct encoder_plugin *const encoder_plugins[];
(plugin = *encoder_plugin_iterator) != NULL; \
++encoder_plugin_iterator)
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/**
* Looks up an encoder plugin by its name.
*
@@ -40,4 +44,8 @@ extern const struct encoder_plugin *const encoder_plugins[];
const struct encoder_plugin *
encoder_plugin_get(const char *name);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/encoder_plugin.h b/src/encoder_plugin.h
index 3a42d79f4..e0748a136 100644
--- a/src/encoder_plugin.h
+++ b/src/encoder_plugin.h
@@ -20,7 +20,7 @@
#ifndef MPD_ENCODER_PLUGIN_H
#define MPD_ENCODER_PLUGIN_H
-#include <glib.h>
+#include "gerror.h"
#include <assert.h>
#include <stdbool.h>