diff options
Diffstat (limited to 'src/encoder')
-rw-r--r-- | src/encoder/null_encoder.c | 115 | ||||
-rw-r--r-- | src/encoder/twolame_encoder.c | 299 | ||||
-rw-r--r-- | src/encoder/wave_encoder.c | 241 |
3 files changed, 655 insertions, 0 deletions
diff --git a/src/encoder/null_encoder.c b/src/encoder/null_encoder.c new file mode 100644 index 000000000..92052b095 --- /dev/null +++ b/src/encoder/null_encoder.c @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "encoder_api.h" +#include "encoder_plugin.h" + +#include <assert.h> +#include <string.h> + +#define MAX_BUFFER 32768 + +struct null_encoder { + struct encoder encoder; + + unsigned char buffer[MAX_BUFFER]; + size_t buffer_length; +}; + +extern const struct encoder_plugin null_encoder_plugin; + +static inline GQuark +null_encoder_quark(void) +{ + return g_quark_from_static_string("null_encoder"); +} + +static struct encoder * +null_encoder_init(G_GNUC_UNUSED const struct config_param *param, + G_GNUC_UNUSED GError **error) +{ + struct null_encoder *encoder; + + encoder = g_new(struct null_encoder, 1); + encoder_struct_init(&encoder->encoder, &null_encoder_plugin); + + return &encoder->encoder; +} + +static void +null_encoder_finish(struct encoder *_encoder) +{ + struct null_encoder *encoder = (struct null_encoder *)_encoder; + + g_free(encoder); +} + +static bool +null_encoder_open(struct encoder *_encoder, + G_GNUC_UNUSED struct audio_format *audio_format, + G_GNUC_UNUSED GError **error) +{ + struct null_encoder *encoder = (struct null_encoder *)_encoder; + + encoder->buffer_length = 0; + + 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; + + assert(length + encoder->buffer_length < MAX_BUFFER); + + memcpy(encoder->buffer+encoder->buffer_length, + data, length); + + encoder->buffer_length += length; + return true; +} + +static size_t +null_encoder_read(struct encoder *_encoder, void *dest, size_t length) +{ + struct null_encoder *encoder = (struct null_encoder *)_encoder; + + 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; +} + +const struct encoder_plugin null_encoder_plugin = { + .name = "null", + .init = null_encoder_init, + .finish = null_encoder_finish, + .open = null_encoder_open, + .write = null_encoder_write, + .read = null_encoder_read, +}; diff --git a/src/encoder/twolame_encoder.c b/src/encoder/twolame_encoder.c new file mode 100644 index 000000000..5a8a82d81 --- /dev/null +++ b/src/encoder/twolame_encoder.c @@ -0,0 +1,299 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "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->bits = 16; + 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; +} + +const struct encoder_plugin twolame_encoder_plugin = { + .name = "twolame", + .init = twolame_encoder_init, + .finish = twolame_encoder_finish, + .open = twolame_encoder_open, + .close = twolame_encoder_close, + .flush = twolame_encoder_flush, + .write = twolame_encoder_write, + .read = twolame_encoder_read, +}; diff --git a/src/encoder/wave_encoder.c b/src/encoder/wave_encoder.c new file mode 100644 index 000000000..f06dea307 --- /dev/null +++ b/src/encoder/wave_encoder.c @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2003-2009 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "encoder_api.h" +#include "encoder_plugin.h" + +#include <assert.h> +#include <string.h> + +#define MAX_BUFFER 32768 + +struct wave_encoder { + struct encoder encoder; + unsigned bits; + + uint8_t buffer[MAX_BUFFER]; + size_t buffer_length; +}; + +struct wave_header { + uint32_t id_riff; + uint32_t riff_size; + uint32_t id_wave; + uint32_t id_fmt; + uint32_t fmt_size; + uint16_t format; + uint16_t channels; + uint32_t freq; + uint32_t byterate; + uint16_t blocksize; + uint16_t bits; + uint32_t id_data; + uint32_t data_size; +}; + +extern const struct encoder_plugin wave_encoder_plugin; + +static inline GQuark +wave_encoder_quark(void) +{ + return g_quark_from_static_string("wave_encoder"); +} + +static void +fill_wave_header(struct wave_header *header, int channels, int bits, + int freq, int block_size) +{ + int data_size = 0x0FFFFFFF; + + /* constants */ + header->id_riff = GUINT32_TO_LE(0x46464952); + header->id_wave = GUINT32_TO_LE(0x45564157); + header->id_fmt = GUINT32_TO_LE(0x20746d66); + header->id_data = GUINT32_TO_LE(0x61746164); + + /* wave format */ + header->format = GUINT16_TO_LE(1); // PCM_FORMAT + header->channels = GUINT16_TO_LE(channels); + header->bits = GUINT16_TO_LE(bits); + header->freq = GUINT32_TO_LE(freq); + header->blocksize = GUINT16_TO_LE(block_size); + header->byterate = GUINT32_TO_LE(freq * block_size); + + /* chunk sizes (fake data length) */ + header->fmt_size = GUINT32_TO_LE(16); + header->data_size = GUINT32_TO_LE(data_size); + header->riff_size = GUINT32_TO_LE(4 + (8 + 16) + + (8 + data_size)); +} + +static struct encoder * +wave_encoder_init(G_GNUC_UNUSED const struct config_param *param, + G_GNUC_UNUSED GError **error) +{ + struct wave_encoder *encoder; + + encoder = g_new(struct wave_encoder, 1); + encoder_struct_init(&encoder->encoder, &wave_encoder_plugin); + + 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; + + encoder->bits = audio_format->bits; + + /* create PCM wave header in initial buffer */ + fill_wave_header((struct wave_header *) encoder->buffer, + audio_format->channels, + audio_format->bits, + audio_format->sample_rate, + (audio_format->bits / 8) * audio_format->channels ); + + encoder->buffer_length = sizeof(struct wave_header); + return true; +} + +static inline size_t +pcm16_to_wave(void *dst, const void *src, size_t length) +{ + uint32_t scnt; + const uint16_t *src16 = (const uint16_t *) src; + uint16_t *dst16 = (uint16_t *) dst; + + for(scnt = length >> 1; scnt>0; scnt--){ + *dst16++ = GUINT16_TO_LE(*src16++); + } + return length; +} + +static inline size_t +pcm32_to_wave(void *dst, const void *src, size_t length) +{ + uint32_t scnt; + const uint32_t *src32 = (const uint32_t *) src; + uint32_t *dst32 = (uint32_t *) dst; + + for(scnt = length >> 2; scnt>0; scnt--){ + *dst32++ = GUINT32_TO_LE(*src32++); + } + return length; +} + +static inline size_t +pcm24_to_wave(void *dst, const void *src, size_t length) +{ + uint32_t scnt, value; + const uint32_t *src32 = (const uint32_t *) src; + uint8_t *dst8 = (uint8_t *) dst; + + for(scnt = length >> 2; scnt>0; scnt--){ + value = *src32++; + *dst8++ = (value) & 0xFF; + *dst8++ = (value >> 8) & 0xFF; + *dst8++ = (value >> 16) & 0xFF; + } + //correct buffer length + length = dst8 - ((uint8_t *)dst); + return length; +} + +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; + + assert(length + encoder->buffer_length < MAX_BUFFER); + + dst = encoder->buffer + 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 + + encoder->buffer_length += length; + return true; +} + +static size_t +wave_encoder_read(struct encoder *_encoder, void *dest, size_t length) +{ + struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + + 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; +} + +const struct encoder_plugin wave_encoder_plugin = { + .name = "wave", + .init = wave_encoder_init, + .finish = wave_encoder_finish, + .open = wave_encoder_open, + .write = wave_encoder_write, + .read = wave_encoder_read, +}; |