diff options
Diffstat (limited to '')
25 files changed, 2431 insertions, 2249 deletions
diff --git a/src/encoder/FlacEncoderPlugin.cxx b/src/encoder/FlacEncoderPlugin.cxx new file mode 100644 index 000000000..5a77e24a7 --- /dev/null +++ b/src/encoder/FlacEncoderPlugin.cxx @@ -0,0 +1,344 @@ +/* + * 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 "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 { + Encoder encoder; + + AudioFormat audio_format; + unsigned compression; + + FLAC__StreamEncoder *fse; + + 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; + + flac_encoder():encoder(flac_encoder_plugin) {} +}; + +static constexpr Domain flac_encoder_domain("vorbis_encoder"); + +static bool +flac_encoder_configure(struct flac_encoder *encoder, const config_param ¶m, + gcc_unused Error &error) +{ + encoder->compression = param.GetBlockValue("compression", 5u); + + return true; +} + +static Encoder * +flac_encoder_init(const config_param ¶m, Error &error) +{ + 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 */ + delete encoder; + return nullptr; + } + + return &encoder->encoder; +} + +static void +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 */ + delete encoder; +} + +static bool +flac_encoder_setup(struct flac_encoder *encoder, unsigned bits_per_sample, + Error &error) +{ + if ( !FLAC__stream_encoder_set_compression_level(encoder->fse, + encoder->compression)) { + error.Format(config_domain, + "error setting flac compression to %d", + encoder->compression); + return false; + } + + if ( !FLAC__stream_encoder_set_channels(encoder->fse, + 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)) { + 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)) { + 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(gcc_unused const FLAC__StreamEncoder *fse, + const FLAC__byte data[], + size_t bytes, + gcc_unused unsigned samples, + gcc_unused unsigned current_frame, void *client_data) +{ + struct flac_encoder *encoder = (struct flac_encoder *) client_data; + + //transfer data to buffer + growing_fifo_append(&encoder->output_buffer, data, bytes); + + return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; +} + +static void +flac_encoder_close(Encoder *_encoder) +{ + struct flac_encoder *encoder = (struct flac_encoder *)_encoder; + + FLAC__stream_encoder_delete(encoder->fse); + + encoder->expand_buffer.Clear(); + fifo_buffer_free(encoder->output_buffer); +} + +static bool +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; + + /* FIXME: flac should support 32bit as well */ + switch (audio_format.format) { + case SampleFormat::S8: + bits_per_sample = 8; + break; + + case SampleFormat::S16: + bits_per_sample = 16; + break; + + case SampleFormat::S24_P32: + bits_per_sample = 24; + break; + + default: + bits_per_sample = 24; + audio_format.format = SampleFormat::S24_P32; + } + + /* allocate the encoder */ + encoder->fse = FLAC__stream_encoder_new(); + if (encoder->fse == nullptr) { + error.Set(flac_encoder_domain, "flac_new() failed"); + return false; + } + + if (!flac_encoder_setup(encoder, bits_per_sample, error)) { + FLAC__stream_encoder_delete(encoder->fse); + return false; + } + + encoder->output_buffer = growing_fifo_new(); + + /* this immediately outputs data through callback */ + + { + FLAC__StreamEncoderInitStatus init_status; + + init_status = FLAC__stream_encoder_init_stream(encoder->fse, + flac_write_callback, + nullptr, nullptr, nullptr, encoder); + + if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { + error.Format(flac_encoder_domain, + "failed to initialize encoder: %s\n", + FLAC__StreamEncoderInitStatusString[init_status]); + flac_encoder_close(_encoder); + return false; + } + } + + return true; +} + + +static bool +flac_encoder_flush(Encoder *_encoder, gcc_unused Error &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(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 = nullptr; + + /* format conversion */ + + num_frames = length / encoder->audio_format.GetFrameSize(); + num_samples = num_frames * encoder->audio_format.channels; + + switch (encoder->audio_format.format) { + 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 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 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, + (const FLAC__int32 *)buffer, + num_frames)) { + error.Set(flac_encoder_domain, "flac encoder process failed"); + return false; + } + + return true; +} + +static size_t +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 = (const char *) + fifo_buffer_read(encoder->output_buffer, &max_length); + if (src == nullptr) + return 0; + + if (length > max_length) + length = max_length; + + memcpy(dest, src, length); + fifo_buffer_consume(encoder->output_buffer, length); + return length; +} + +static const char * +flac_encoder_get_mime_type(gcc_unused Encoder *_encoder) +{ + return "audio/flac"; +} + +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 ¶m, Error &error); +}; + +static constexpr Domain lame_encoder_domain("lame_encoder"); + +bool +LameEncoder::Configure(const config_param ¶m, 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 ¶m, 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 ¶m, + 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 ¶m, 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 ¶m, 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 ¶m, Error &error); +}; + +static constexpr Domain twolame_encoder_domain("twolame_encoder"); + +bool +TwolameEncoder::Configure(const config_param ¶m, 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 ¶m, 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/VorbisEncoderPlugin.cxx b/src/encoder/VorbisEncoderPlugin.cxx new file mode 100644 index 000000000..84b4cac28 --- /dev/null +++ b/src/encoder/VorbisEncoderPlugin.cxx @@ -0,0 +1,365 @@ +/* + * 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 "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 <glib.h> + +#include <assert.h> + +struct vorbis_encoder { + /** the base class */ + Encoder encoder; + + /* configuration */ + + float quality; + int bitrate; + + /* runtime information */ + + AudioFormat audio_format; + + vorbis_dsp_state vd; + vorbis_block vb; + vorbis_info vi; + + OggStream stream; + + vorbis_encoder():encoder(vorbis_encoder_plugin) {} +}; + +static constexpr Domain vorbis_encoder_domain("vorbis_encoder"); + +static bool +vorbis_encoder_configure(struct vorbis_encoder *encoder, + const config_param ¶m, Error &error) +{ + const char *value = param.GetBlockValue("quality"); + 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) { + 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; + } + + encoder->quality = -2.0; + + char *endptr; + encoder->bitrate = g_ascii_strtoll(value, &endptr, 10); + if (*endptr != '\0' || encoder->bitrate <= 0) { + error.Set(config_domain, + "bitrate should be a positive integer"); + return false; + } + } + + return true; +} + +static Encoder * +vorbis_encoder_init(const config_param ¶m, Error &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(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, Error &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)) { + error.Set(vorbis_encoder_domain, + "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)) { + error.Set(vorbis_encoder_domain, + "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(Encoder *_encoder, + AudioFormat &audio_format, + Error &error) +{ + struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; + + audio_format.format = SampleFormat::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(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(Encoder *_encoder, gcc_unused Error &error) +{ + struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; + + encoder->stream.Flush(); + return true; +} + +static bool +vorbis_encoder_pre_tag(Encoder *_encoder, gcc_unused Error &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 Tag *tag) +{ + for (unsigned i = 0; i < tag->num_items; i++) { + 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(Encoder *_encoder, const Tag *tag, + gcc_unused Error &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(Encoder *_encoder, + const void *data, size_t length, + gcc_unused Error &error) +{ + struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder; + + unsigned num_frames = length / encoder->audio_format.GetFrameSize(); + + /* 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(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(gcc_unused Encoder *_encoder) +{ + return "audio/ogg"; +} + +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/WaveEncoderPlugin.cxx b/src/encoder/WaveEncoderPlugin.cxx new file mode 100644 index 000000000..493b07b61 --- /dev/null +++ b/src/encoder/WaveEncoderPlugin.cxx @@ -0,0 +1,276 @@ +/* + * 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 "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 WaveEncoder { + Encoder encoder; + unsigned bits; + + struct fifo_buffer *buffer; + + WaveEncoder():encoder(wave_encoder_plugin) {} +}; + +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; +}; + +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 Encoder * +wave_encoder_init(gcc_unused const config_param ¶m, + gcc_unused Error &error) +{ + WaveEncoder *encoder = new WaveEncoder(); + return &encoder->encoder; +} + +static void +wave_encoder_finish(Encoder *_encoder) +{ + WaveEncoder *encoder = (WaveEncoder *)_encoder; + + g_free(encoder); +} + +static bool +wave_encoder_open(Encoder *_encoder, + AudioFormat &audio_format, + gcc_unused Error &error) +{ + WaveEncoder *encoder = (WaveEncoder *)_encoder; + + assert(audio_format.IsValid()); + + switch (audio_format.format) { + case SampleFormat::S8: + encoder->bits = 8; + break; + + case SampleFormat::S16: + encoder->bits = 16; + break; + + case SampleFormat::S24_P32: + encoder->bits = 24; + break; + + case SampleFormat::S32: + encoder->bits = 32; + break; + + default: + audio_format.format = SampleFormat::S16; + encoder->bits = 16; + break; + } + + encoder->buffer = growing_fifo_new(); + 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, + encoder->bits, + audio_format.sample_rate, + (encoder->bits / 8) * audio_format.channels); + fifo_buffer_append(encoder->buffer, sizeof(*header)); + + return true; +} + +static void +wave_encoder_close(Encoder *_encoder) +{ + WaveEncoder *encoder = (WaveEncoder *)_encoder; + + fifo_buffer_free(encoder->buffer); +} + +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(Encoder *_encoder, + const void *src, size_t length, + gcc_unused Error &error) +{ + WaveEncoder *encoder = (WaveEncoder *)_encoder; + + uint8_t *dst = (uint8_t *)growing_fifo_write(&encoder->buffer, 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, (const uint32_t *)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, (const uint16_t *)src, length); + break; + case 24: + length = pcm24_to_wave(dst, (const uint32_t *)src, length); + break; + case 32: + length = pcm32_to_wave(dst, (const uint32_t *)src, length); + break; + } +#else +#error G_BYTE_ORDER set to G_PDP_ENDIAN is not supported by wave_encoder +#endif + + fifo_buffer_append(encoder->buffer, length); + return true; +} + +static size_t +wave_encoder_read(Encoder *_encoder, void *dest, size_t length) +{ + WaveEncoder *encoder = (WaveEncoder *)_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; +} + +static const char * +wave_encoder_get_mime_type(gcc_unused Encoder *_encoder) +{ + return "audio/wav"; +} + +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/flac_encoder.c b/src/encoder/flac_encoder.c deleted file mode 100644 index e32588e29..000000000 --- a/src/encoder/flac_encoder.c +++ /dev/null @@ -1,363 +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 "pcm_buffer.h" -#include "fifo_buffer.h" -#include "growing_fifo.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; - - /** - * 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; - - -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; - - //transfer data to buffer - growing_fifo_append(&encoder->output_buffer, data, 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->expand_buffer); - fifo_buffer_free(encoder->output_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; - } - - 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); - - 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; - - size_t max_length; - const char *src = fifo_buffer_read(encoder->output_buffer, - &max_length); - if (src == NULL) - return 0; - - if (length > max_length) - length = max_length; - - memcpy(dest, src, length); - fifo_buffer_consume(encoder->output_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, - .end = flac_encoder_flush, - .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 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, -}; 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 deleted file mode 100644 index 9eeb4d513..000000000 --- a/src/encoder/wave_encoder.c +++ /dev/null @@ -1,278 +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 wave_encoder { - struct encoder encoder; - unsigned bits; - - struct fifo_buffer *buffer; -}; - -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); - - return &encoder->encoder; -} - -static void -wave_encoder_finish(struct encoder *_encoder) -{ - struct wave_encoder *encoder = (struct wave_encoder *)_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) -{ - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - - 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; - } - - encoder->buffer = growing_fifo_new(); - struct wave_header *header = - growing_fifo_write(&encoder->buffer, sizeof(*header)); - - /* create PCM wave header in initial buffer */ - fill_wave_header(header, - audio_format->channels, - encoder->bits, - 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) -{ - struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - - fifo_buffer_free(encoder->buffer); -} - -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 = growing_fifo_write(&encoder->buffer, 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 - - fifo_buffer_append(encoder->buffer, 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; - - 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; -} - -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, - .close = wave_encoder_close, - .write = wave_encoder_write, - .read = wave_encoder_read, - .get_mime_type = wave_encoder_get_mime_type, -}; diff --git a/src/encoder_api.h b/src/encoder_api.h deleted file mode 100644 index 46c8d10c8..000000000 --- a/src/encoder_api.h +++ /dev/null @@ -1,33 +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. - */ - -/* - * This header is included by encoder plugins. - * - */ - -#ifndef MPD_ENCODER_API_H -#define MPD_ENCODER_API_H - -#include "encoder_plugin.h" -#include "audio_format.h" -#include "tag.h" -#include "conf.h" - -#endif diff --git a/src/encoder_list.c b/src/encoder_list.c deleted file mode 100644 index 2326c1099..000000000 --- a/src/encoder_list.c +++ /dev/null @@ -1,61 +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_list.h" -#include "encoder_plugin.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; - -const struct encoder_plugin *const 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 -}; - -const struct encoder_plugin * -encoder_plugin_get(const char *name) -{ - encoder_plugins_for_each(plugin) - if (strcmp(plugin->name, name) == 0) - return plugin; - - return NULL; -} diff --git a/src/encoder_list.h b/src/encoder_list.h deleted file mode 100644 index fb1c9bf9c..000000000 --- a/src/encoder_list.h +++ /dev/null @@ -1,43 +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. - */ - -#ifndef MPD_ENCODER_LIST_H -#define MPD_ENCODER_LIST_H - -struct encoder_plugin; - -extern const struct encoder_plugin *const encoder_plugins[]; - -#define encoder_plugins_for_each(plugin) \ - for (const struct encoder_plugin *plugin, \ - *const*encoder_plugin_iterator = &encoder_plugins[0]; \ - (plugin = *encoder_plugin_iterator) != NULL; \ - ++encoder_plugin_iterator) - -/** - * Looks up an encoder plugin by its name. - * - * @param name the encoder name to look for - * @return the encoder plugin with the specified name, or NULL if none - * was found - */ -const struct encoder_plugin * -encoder_plugin_get(const char *name); - -#endif diff --git a/src/encoder_plugin.h b/src/encoder_plugin.h deleted file mode 100644 index 3a42d79f4..000000000 --- a/src/encoder_plugin.h +++ /dev/null @@ -1,336 +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. - */ - -#ifndef MPD_ENCODER_PLUGIN_H -#define MPD_ENCODER_PLUGIN_H - -#include <glib.h> - -#include <assert.h> -#include <stdbool.h> -#include <stddef.h> - -struct encoder_plugin; -struct audio_format; -struct config_param; -struct tag; - -struct encoder { - const struct encoder_plugin *plugin; - -#ifndef NDEBUG - bool open, pre_tag, tag, end; -#endif -}; - -struct encoder_plugin { - const char *name; - - struct encoder *(*init)(const struct config_param *param, - GError **error); - - void (*finish)(struct encoder *encoder); - - bool (*open)(struct encoder *encoder, - struct audio_format *audio_format, - GError **error); - - void (*close)(struct encoder *encoder); - - bool (*end)(struct encoder *encoder, GError **error); - - bool (*flush)(struct encoder *encoder, GError **error); - - bool (*pre_tag)(struct encoder *encoder, GError **error); - - bool (*tag)(struct encoder *encoder, const struct tag *tag, - GError **error); - - bool (*write)(struct encoder *encoder, - const void *data, size_t length, - GError **error); - - size_t (*read)(struct encoder *encoder, void *dest, size_t length); - - const char *(*get_mime_type)(struct encoder *encoder); -}; - -/** - * Initializes an encoder object. This should be used by encoder - * plugins to initialize their base class. - */ -static inline void -encoder_struct_init(struct encoder *encoder, - const struct encoder_plugin *plugin) -{ - encoder->plugin = plugin; - -#ifndef NDEBUG - encoder->open = false; -#endif -} - -/** - * Creates a new encoder object. - * - * @param plugin the encoder plugin - * @param param optional configuration - * @param error location to store the error occurring, or NULL to ignore errors. - * @return an encoder object on success, NULL on failure - */ -static inline struct encoder * -encoder_init(const struct encoder_plugin *plugin, - const struct config_param *param, GError **error) -{ - return plugin->init(param, error); -} - -/** - * Frees an encoder object. - * - * @param encoder the encoder - */ -static inline void -encoder_finish(struct encoder *encoder) -{ - assert(!encoder->open); - - encoder->plugin->finish(encoder); -} - -/** - * Opens an encoder object. You must call this prior to using it. - * Before you free it, you must call encoder_close(). You may open - * and close (reuse) one encoder any number of times. - * - * After this function returns successfully and before the first - * encoder_write() call, you should invoke encoder_read() to obtain - * the file header. - * - * @param encoder the encoder - * @param audio_format the encoder's input audio format; the plugin - * may modify the struct to adapt it to its abilities - * @param error location to store the error occurring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_open(struct encoder *encoder, struct audio_format *audio_format, - GError **error) -{ - assert(!encoder->open); - - bool success = encoder->plugin->open(encoder, audio_format, error); -#ifndef NDEBUG - encoder->open = success; - encoder->pre_tag = encoder->tag = encoder->end = false; -#endif - return success; -} - -/** - * Closes an encoder object. This disables the encoder, and readies - * it for reusal by calling encoder_open() again. - * - * @param encoder the encoder - */ -static inline void -encoder_close(struct encoder *encoder) -{ - assert(encoder->open); - - if (encoder->plugin->close != NULL) - encoder->plugin->close(encoder); - -#ifndef NDEBUG - encoder->open = false; -#endif -} - -/** - * Ends the stream: flushes the encoder object, generate an - * end-of-stream marker (if applicable), make everything which might - * currently be buffered available by encoder_read(). - * - * After this function has been called, the encoder may not be usable - * for more data, and only encoder_read() and encoder_close() can be - * called. - * - * @param encoder the encoder - * @param error location to store the error occuring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_end(struct encoder *encoder, GError **error) -{ - assert(encoder->open); - assert(!encoder->end); - -#ifndef NDEBUG - encoder->end = true; -#endif - - /* this method is optional */ - return encoder->plugin->end != NULL - ? encoder->plugin->end(encoder, error) - : true; -} - -/** - * Flushes an encoder object, make everything which might currently be - * buffered available by encoder_read(). - * - * @param encoder the encoder - * @param error location to store the error occurring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_flush(struct encoder *encoder, GError **error) -{ - assert(encoder->open); - assert(!encoder->pre_tag); - assert(!encoder->tag); - assert(!encoder->end); - - /* this method is optional */ - return encoder->plugin->flush != NULL - ? encoder->plugin->flush(encoder, error) - : true; -} - -/** - * Prepare for sending a tag to the encoder. This is used by some - * encoders to flush the previous sub-stream, in preparation to begin - * a new one. - * - * @param encoder the encoder - * @param tag the tag object - * @param error location to store the error occuring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_pre_tag(struct encoder *encoder, GError **error) -{ - assert(encoder->open); - assert(!encoder->pre_tag); - assert(!encoder->tag); - assert(!encoder->end); - - /* this method is optional */ - bool success = encoder->plugin->pre_tag != NULL - ? encoder->plugin->pre_tag(encoder, error) - : true; - -#ifndef NDEBUG - encoder->pre_tag = success; -#endif - return success; -} - -/** - * Sends a tag to the encoder. - * - * Instructions: call encoder_pre_tag(); then obtain flushed data with - * encoder_read(); finally call encoder_tag(). - * - * @param encoder the encoder - * @param tag the tag object - * @param error location to store the error occurring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_tag(struct encoder *encoder, const struct tag *tag, GError **error) -{ - assert(encoder->open); - assert(!encoder->pre_tag); - assert(encoder->tag); - assert(!encoder->end); - -#ifndef NDEBUG - encoder->tag = false; -#endif - - /* this method is optional */ - return encoder->plugin->tag != NULL - ? encoder->plugin->tag(encoder, tag, error) - : true; -} - -/** - * Writes raw PCM data to the encoder. - * - * @param encoder the encoder - * @param data the buffer containing PCM samples - * @param length the length of the buffer in bytes - * @param error location to store the error occurring, or NULL to ignore errors. - * @return true on success - */ -static inline bool -encoder_write(struct encoder *encoder, const void *data, size_t length, - GError **error) -{ - assert(encoder->open); - assert(!encoder->pre_tag); - assert(!encoder->tag); - assert(!encoder->end); - - return encoder->plugin->write(encoder, data, length, error); -} - -/** - * Reads encoded data from the encoder. - * - * Call this repeatedly until no more data is returned. - * - * @param encoder the encoder - * @param dest the destination buffer to copy to - * @param length the maximum length of the destination buffer - * @return the number of bytes written to #dest - */ -static inline size_t -encoder_read(struct encoder *encoder, void *dest, size_t length) -{ - assert(encoder->open); - assert(!encoder->pre_tag || !encoder->tag); - -#ifndef NDEBUG - if (encoder->pre_tag) { - encoder->pre_tag = false; - encoder->tag = true; - } -#endif - - 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 |