encoder/lame,twolame: convert to C++
This commit is contained in:
parent
da1f4b3ede
commit
7a3aac1843
|
@ -662,11 +662,15 @@ libencoder_plugins_a_SOURCES += \
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if ENABLE_LAME_ENCODER
|
if ENABLE_LAME_ENCODER
|
||||||
libencoder_plugins_a_SOURCES += src/encoder/lame_encoder.c
|
libencoder_plugins_a_SOURCES += \
|
||||||
|
src/encoder/LameEncoderPlugin.cxx \
|
||||||
|
src/encoder/LameEncoderPlugin.hxx
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if ENABLE_TWOLAME_ENCODER
|
if ENABLE_TWOLAME_ENCODER
|
||||||
libencoder_plugins_a_SOURCES += src/encoder/twolame_encoder.c
|
libencoder_plugins_a_SOURCES += \
|
||||||
|
src/encoder/TwolameEncoderPlugin.cxx \
|
||||||
|
src/encoder/TwolameEncoderPlugin.hxx
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if ENABLE_FLAC_ENCODER
|
if ENABLE_FLAC_ENCODER
|
||||||
|
|
|
@ -25,12 +25,11 @@
|
||||||
#include "encoder/VorbisEncoderPlugin.hxx"
|
#include "encoder/VorbisEncoderPlugin.hxx"
|
||||||
#include "encoder/OpusEncoderPlugin.hxx"
|
#include "encoder/OpusEncoderPlugin.hxx"
|
||||||
#include "encoder/FlacEncoderPlugin.hxx"
|
#include "encoder/FlacEncoderPlugin.hxx"
|
||||||
|
#include "encoder/LameEncoderPlugin.hxx"
|
||||||
|
#include "encoder/TwolameEncoderPlugin.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
extern const struct encoder_plugin lame_encoder_plugin;
|
|
||||||
extern const struct encoder_plugin twolame_encoder_plugin;
|
|
||||||
|
|
||||||
const struct encoder_plugin *const encoder_plugins[] = {
|
const struct encoder_plugin *const encoder_plugins[] = {
|
||||||
&null_encoder_plugin,
|
&null_encoder_plugin,
|
||||||
#ifdef ENABLE_VORBIS_ENCODER
|
#ifdef ENABLE_VORBIS_ENCODER
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
* http://www.musicpd.org
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "LameEncoderPlugin.hxx"
|
||||||
#include "encoder_api.h"
|
#include "encoder_api.h"
|
||||||
#include "encoder_plugin.h"
|
#include "encoder_plugin.h"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
|
@ -29,7 +30,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct lame_encoder {
|
struct LameEncoder final {
|
||||||
struct encoder encoder;
|
struct encoder encoder;
|
||||||
|
|
||||||
struct audio_format audio_format;
|
struct audio_format audio_format;
|
||||||
|
@ -40,9 +41,13 @@ struct lame_encoder {
|
||||||
|
|
||||||
unsigned char buffer[32768];
|
unsigned char buffer[32768];
|
||||||
size_t buffer_length;
|
size_t buffer_length;
|
||||||
};
|
|
||||||
|
|
||||||
extern const struct encoder_plugin lame_encoder_plugin;
|
LameEncoder() {
|
||||||
|
encoder_struct_init(&encoder, &lame_encoder_plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Configure(const config_param *param, GError **error);
|
||||||
|
};
|
||||||
|
|
||||||
static inline GQuark
|
static inline GQuark
|
||||||
lame_encoder_quark(void)
|
lame_encoder_quark(void)
|
||||||
|
@ -50,21 +55,19 @@ lame_encoder_quark(void)
|
||||||
return g_quark_from_static_string("lame_encoder");
|
return g_quark_from_static_string("lame_encoder");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
bool
|
||||||
lame_encoder_configure(struct lame_encoder *encoder,
|
LameEncoder::Configure(const config_param *param, GError **error)
|
||||||
const struct config_param *param, GError **error)
|
|
||||||
{
|
{
|
||||||
const char *value;
|
const char *value;
|
||||||
char *endptr;
|
char *endptr;
|
||||||
|
|
||||||
value = config_get_block_string(param, "quality", NULL);
|
value = config_get_block_string(param, "quality", nullptr);
|
||||||
if (value != NULL) {
|
if (value != nullptr) {
|
||||||
/* a quality was configured (VBR) */
|
/* a quality was configured (VBR) */
|
||||||
|
|
||||||
encoder->quality = g_ascii_strtod(value, &endptr);
|
quality = g_ascii_strtod(value, &endptr);
|
||||||
|
|
||||||
if (*endptr != '\0' || encoder->quality < -1.0 ||
|
if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
|
||||||
encoder->quality > 10.0) {
|
|
||||||
g_set_error(error, lame_encoder_quark(), 0,
|
g_set_error(error, lame_encoder_quark(), 0,
|
||||||
"quality \"%s\" is not a number in the "
|
"quality \"%s\" is not a number in the "
|
||||||
"range -1 to 10, line %i",
|
"range -1 to 10, line %i",
|
||||||
|
@ -72,7 +75,7 @@ lame_encoder_configure(struct lame_encoder *encoder,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config_get_block_string(param, "bitrate", NULL) != NULL) {
|
if (config_get_block_string(param, "bitrate", nullptr) != nullptr) {
|
||||||
g_set_error(error, lame_encoder_quark(), 0,
|
g_set_error(error, lame_encoder_quark(), 0,
|
||||||
"quality and bitrate are "
|
"quality and bitrate are "
|
||||||
"both defined (line %i)",
|
"both defined (line %i)",
|
||||||
|
@ -82,8 +85,8 @@ lame_encoder_configure(struct lame_encoder *encoder,
|
||||||
} else {
|
} else {
|
||||||
/* a bit rate was configured */
|
/* a bit rate was configured */
|
||||||
|
|
||||||
value = config_get_block_string(param, "bitrate", NULL);
|
value = config_get_block_string(param, "bitrate", nullptr);
|
||||||
if (value == NULL) {
|
if (value == nullptr) {
|
||||||
g_set_error(error, lame_encoder_quark(), 0,
|
g_set_error(error, lame_encoder_quark(), 0,
|
||||||
"neither bitrate nor quality defined "
|
"neither bitrate nor quality defined "
|
||||||
"at line %i",
|
"at line %i",
|
||||||
|
@ -91,10 +94,10 @@ lame_encoder_configure(struct lame_encoder *encoder,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder->quality = -2.0;
|
quality = -2.0;
|
||||||
encoder->bitrate = g_ascii_strtoll(value, &endptr, 10);
|
bitrate = g_ascii_strtoll(value, &endptr, 10);
|
||||||
|
|
||||||
if (*endptr != '\0' || encoder->bitrate <= 0) {
|
if (*endptr != '\0' || bitrate <= 0) {
|
||||||
g_set_error(error, lame_encoder_quark(), 0,
|
g_set_error(error, lame_encoder_quark(), 0,
|
||||||
"bitrate at line %i should be a positive integer",
|
"bitrate at line %i should be a positive integer",
|
||||||
param->line);
|
param->line);
|
||||||
|
@ -106,18 +109,15 @@ lame_encoder_configure(struct lame_encoder *encoder,
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct encoder *
|
static struct encoder *
|
||||||
lame_encoder_init(const struct config_param *param, GError **error)
|
lame_encoder_init(const struct config_param *param, GError **error_r)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder;
|
LameEncoder *encoder = new LameEncoder();
|
||||||
|
|
||||||
encoder = g_new(struct lame_encoder, 1);
|
|
||||||
encoder_struct_init(&encoder->encoder, &lame_encoder_plugin);
|
|
||||||
|
|
||||||
/* load configuration from "param" */
|
/* load configuration from "param" */
|
||||||
if (!lame_encoder_configure(encoder, param, error)) {
|
if (!encoder->Configure(param, error_r)) {
|
||||||
/* configuration has failed, roll back and return error */
|
/* configuration has failed, roll back and return error */
|
||||||
g_free(encoder);
|
delete encoder;
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return &encoder->encoder;
|
return &encoder->encoder;
|
||||||
|
@ -126,7 +126,7 @@ lame_encoder_init(const struct config_param *param, GError **error)
|
||||||
static void
|
static void
|
||||||
lame_encoder_finish(struct encoder *_encoder)
|
lame_encoder_finish(struct encoder *_encoder)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
|
LameEncoder *encoder = (LameEncoder *)_encoder;
|
||||||
|
|
||||||
/* the real liblame cleanup was already performed by
|
/* the real liblame cleanup was already performed by
|
||||||
lame_encoder_close(), so no real work here */
|
lame_encoder_close(), so no real work here */
|
||||||
|
@ -134,7 +134,7 @@ lame_encoder_finish(struct encoder *_encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
lame_encoder_setup(struct lame_encoder *encoder, GError **error)
|
lame_encoder_setup(LameEncoder *encoder, GError **error)
|
||||||
{
|
{
|
||||||
if (encoder->quality >= -1.0) {
|
if (encoder->quality >= -1.0) {
|
||||||
/* a quality was configured (VBR) */
|
/* a quality was configured (VBR) */
|
||||||
|
@ -193,7 +193,7 @@ static bool
|
||||||
lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
|
LameEncoder *encoder = (LameEncoder *)_encoder;
|
||||||
|
|
||||||
audio_format->format = SAMPLE_FORMAT_S16;
|
audio_format->format = SAMPLE_FORMAT_S16;
|
||||||
audio_format->channels = 2;
|
audio_format->channels = 2;
|
||||||
|
@ -201,7 +201,7 @@ lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
||||||
encoder->audio_format = *audio_format;
|
encoder->audio_format = *audio_format;
|
||||||
|
|
||||||
encoder->gfp = lame_init();
|
encoder->gfp = lame_init();
|
||||||
if (encoder->gfp == NULL) {
|
if (encoder->gfp == nullptr) {
|
||||||
g_set_error(error, lame_encoder_quark(), 0,
|
g_set_error(error, lame_encoder_quark(), 0,
|
||||||
"lame_init() failed");
|
"lame_init() failed");
|
||||||
return false;
|
return false;
|
||||||
|
@ -220,7 +220,7 @@ lame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
||||||
static void
|
static void
|
||||||
lame_encoder_close(struct encoder *_encoder)
|
lame_encoder_close(struct encoder *_encoder)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
|
LameEncoder *encoder = (LameEncoder *)_encoder;
|
||||||
|
|
||||||
lame_close(encoder->gfp);
|
lame_close(encoder->gfp);
|
||||||
}
|
}
|
||||||
|
@ -230,30 +230,26 @@ lame_encoder_write(struct encoder *_encoder,
|
||||||
const void *data, size_t length,
|
const void *data, size_t length,
|
||||||
gcc_unused GError **error)
|
gcc_unused GError **error)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
|
LameEncoder *encoder = (LameEncoder *)_encoder;
|
||||||
unsigned num_frames;
|
|
||||||
float *left, *right;
|
|
||||||
const int16_t *src = (const int16_t*)data;
|
const int16_t *src = (const int16_t*)data;
|
||||||
unsigned int i;
|
|
||||||
int bytes_out;
|
|
||||||
|
|
||||||
assert(encoder->buffer_length == 0);
|
assert(encoder->buffer_length == 0);
|
||||||
|
|
||||||
num_frames =
|
const unsigned num_frames =
|
||||||
length / audio_format_frame_size(&encoder->audio_format);
|
length / audio_format_frame_size(&encoder->audio_format);
|
||||||
left = g_malloc(sizeof(left[0]) * num_frames);
|
float *left = g_new(float, num_frames);
|
||||||
right = g_malloc(sizeof(right[0]) * num_frames);
|
float *right = g_new(float, num_frames);
|
||||||
|
|
||||||
/* this is for only 16-bit audio */
|
/* this is for only 16-bit audio */
|
||||||
|
|
||||||
for (i = 0; i < num_frames; i++) {
|
for (unsigned i = 0; i < num_frames; i++) {
|
||||||
left[i] = *src++;
|
left[i] = *src++;
|
||||||
right[i] = *src++;
|
right[i] = *src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_out = lame_encode_buffer_float(encoder->gfp, left, right,
|
int bytes_out = lame_encode_buffer_float(encoder->gfp, left, right,
|
||||||
num_frames, encoder->buffer,
|
num_frames, encoder->buffer,
|
||||||
sizeof(encoder->buffer));
|
sizeof(encoder->buffer));
|
||||||
|
|
||||||
g_free(left);
|
g_free(left);
|
||||||
g_free(right);
|
g_free(right);
|
||||||
|
@ -271,7 +267,7 @@ lame_encoder_write(struct encoder *_encoder,
|
||||||
static size_t
|
static size_t
|
||||||
lame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
lame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
||||||
{
|
{
|
||||||
struct lame_encoder *encoder = (struct lame_encoder *)_encoder;
|
LameEncoder *encoder = (LameEncoder *)_encoder;
|
||||||
|
|
||||||
if (length > encoder->buffer_length)
|
if (length > encoder->buffer_length)
|
||||||
length = encoder->buffer_length;
|
length = encoder->buffer_length;
|
||||||
|
@ -292,12 +288,16 @@ lame_encoder_get_mime_type(gcc_unused struct encoder *_encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct encoder_plugin lame_encoder_plugin = {
|
const struct encoder_plugin lame_encoder_plugin = {
|
||||||
.name = "lame",
|
"lame",
|
||||||
.init = lame_encoder_init,
|
lame_encoder_init,
|
||||||
.finish = lame_encoder_finish,
|
lame_encoder_finish,
|
||||||
.open = lame_encoder_open,
|
lame_encoder_open,
|
||||||
.close = lame_encoder_close,
|
lame_encoder_close,
|
||||||
.write = lame_encoder_write,
|
nullptr,
|
||||||
.read = lame_encoder_read,
|
nullptr,
|
||||||
.get_mime_type = lame_encoder_get_mime_type,
|
nullptr,
|
||||||
|
nullptr,
|
||||||
|
lame_encoder_write,
|
||||||
|
lame_encoder_read,
|
||||||
|
lame_encoder_get_mime_type,
|
||||||
};
|
};
|
|
@ -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 encoder_plugin lame_encoder_plugin;
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
* http://www.musicpd.org
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "TwolameEncoderPlugin.hxx"
|
||||||
#include "encoder_api.h"
|
#include "encoder_api.h"
|
||||||
#include "encoder_plugin.h"
|
#include "encoder_plugin.h"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
|
@ -29,7 +30,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct twolame_encoder {
|
struct TwolameEncoder final {
|
||||||
struct encoder encoder;
|
struct encoder encoder;
|
||||||
|
|
||||||
struct audio_format audio_format;
|
struct audio_format audio_format;
|
||||||
|
@ -45,9 +46,13 @@ struct twolame_encoder {
|
||||||
* Call libtwolame's flush function when the buffer is empty?
|
* Call libtwolame's flush function when the buffer is empty?
|
||||||
*/
|
*/
|
||||||
bool flush;
|
bool flush;
|
||||||
};
|
|
||||||
|
|
||||||
extern const struct encoder_plugin twolame_encoder_plugin;
|
TwolameEncoder() {
|
||||||
|
encoder_struct_init(&encoder, &twolame_encoder_plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Configure(const config_param *param, GError **error);
|
||||||
|
};
|
||||||
|
|
||||||
static inline GQuark
|
static inline GQuark
|
||||||
twolame_encoder_quark(void)
|
twolame_encoder_quark(void)
|
||||||
|
@ -55,21 +60,19 @@ twolame_encoder_quark(void)
|
||||||
return g_quark_from_static_string("twolame_encoder");
|
return g_quark_from_static_string("twolame_encoder");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
bool
|
||||||
twolame_encoder_configure(struct twolame_encoder *encoder,
|
TwolameEncoder::Configure(const config_param *param, GError **error)
|
||||||
const struct config_param *param, GError **error)
|
|
||||||
{
|
{
|
||||||
const char *value;
|
const char *value;
|
||||||
char *endptr;
|
char *endptr;
|
||||||
|
|
||||||
value = config_get_block_string(param, "quality", NULL);
|
value = config_get_block_string(param, "quality", nullptr);
|
||||||
if (value != NULL) {
|
if (value != nullptr) {
|
||||||
/* a quality was configured (VBR) */
|
/* a quality was configured (VBR) */
|
||||||
|
|
||||||
encoder->quality = g_ascii_strtod(value, &endptr);
|
quality = g_ascii_strtod(value, &endptr);
|
||||||
|
|
||||||
if (*endptr != '\0' || encoder->quality < -1.0 ||
|
if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
|
||||||
encoder->quality > 10.0) {
|
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"quality \"%s\" is not a number in the "
|
"quality \"%s\" is not a number in the "
|
||||||
"range -1 to 10, line %i",
|
"range -1 to 10, line %i",
|
||||||
|
@ -77,7 +80,7 @@ twolame_encoder_configure(struct twolame_encoder *encoder,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config_get_block_string(param, "bitrate", NULL) != NULL) {
|
if (config_get_block_string(param, "bitrate", nullptr) != nullptr) {
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"quality and bitrate are "
|
"quality and bitrate are "
|
||||||
"both defined (line %i)",
|
"both defined (line %i)",
|
||||||
|
@ -87,8 +90,8 @@ twolame_encoder_configure(struct twolame_encoder *encoder,
|
||||||
} else {
|
} else {
|
||||||
/* a bit rate was configured */
|
/* a bit rate was configured */
|
||||||
|
|
||||||
value = config_get_block_string(param, "bitrate", NULL);
|
value = config_get_block_string(param, "bitrate", nullptr);
|
||||||
if (value == NULL) {
|
if (value == nullptr) {
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"neither bitrate nor quality defined "
|
"neither bitrate nor quality defined "
|
||||||
"at line %i",
|
"at line %i",
|
||||||
|
@ -96,10 +99,10 @@ twolame_encoder_configure(struct twolame_encoder *encoder,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder->quality = -2.0;
|
quality = -2.0;
|
||||||
encoder->bitrate = g_ascii_strtoll(value, &endptr, 10);
|
bitrate = g_ascii_strtoll(value, &endptr, 10);
|
||||||
|
|
||||||
if (*endptr != '\0' || encoder->bitrate <= 0) {
|
if (*endptr != '\0' || bitrate <= 0) {
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"bitrate at line %i should be a positive integer",
|
"bitrate at line %i should be a positive integer",
|
||||||
param->line);
|
param->line);
|
||||||
|
@ -111,20 +114,17 @@ twolame_encoder_configure(struct twolame_encoder *encoder,
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct encoder *
|
static struct encoder *
|
||||||
twolame_encoder_init(const struct config_param *param, GError **error)
|
twolame_encoder_init(const struct config_param *param, GError **error_r)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder;
|
|
||||||
|
|
||||||
g_debug("libtwolame version %s", get_twolame_version());
|
g_debug("libtwolame version %s", get_twolame_version());
|
||||||
|
|
||||||
encoder = g_new(struct twolame_encoder, 1);
|
TwolameEncoder *encoder = new TwolameEncoder();
|
||||||
encoder_struct_init(&encoder->encoder, &twolame_encoder_plugin);
|
|
||||||
|
|
||||||
/* load configuration from "param" */
|
/* load configuration from "param" */
|
||||||
if (!twolame_encoder_configure(encoder, param, error)) {
|
if (!encoder->Configure(param, error_r)) {
|
||||||
/* configuration has failed, roll back and return error */
|
/* configuration has failed, roll back and return error */
|
||||||
g_free(encoder);
|
delete encoder;
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return &encoder->encoder;
|
return &encoder->encoder;
|
||||||
|
@ -133,15 +133,15 @@ twolame_encoder_init(const struct config_param *param, GError **error)
|
||||||
static void
|
static void
|
||||||
twolame_encoder_finish(struct encoder *_encoder)
|
twolame_encoder_finish(struct encoder *_encoder)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
|
|
||||||
/* the real libtwolame cleanup was already performed by
|
/* the real libtwolame cleanup was already performed by
|
||||||
twolame_encoder_close(), so no real work here */
|
twolame_encoder_close(), so no real work here */
|
||||||
g_free(encoder);
|
delete encoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
twolame_encoder_setup(struct twolame_encoder *encoder, GError **error)
|
twolame_encoder_setup(TwolameEncoder *encoder, GError **error)
|
||||||
{
|
{
|
||||||
if (encoder->quality >= -1.0) {
|
if (encoder->quality >= -1.0) {
|
||||||
/* a quality was configured (VBR) */
|
/* a quality was configured (VBR) */
|
||||||
|
@ -193,7 +193,7 @@ static bool
|
||||||
twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
|
|
||||||
audio_format->format = SAMPLE_FORMAT_S16;
|
audio_format->format = SAMPLE_FORMAT_S16;
|
||||||
audio_format->channels = 2;
|
audio_format->channels = 2;
|
||||||
|
@ -201,7 +201,7 @@ twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format
|
||||||
encoder->audio_format = *audio_format;
|
encoder->audio_format = *audio_format;
|
||||||
|
|
||||||
encoder->options = twolame_init();
|
encoder->options = twolame_init();
|
||||||
if (encoder->options == NULL) {
|
if (encoder->options == nullptr) {
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"twolame_init() failed");
|
"twolame_init() failed");
|
||||||
return false;
|
return false;
|
||||||
|
@ -221,7 +221,7 @@ twolame_encoder_open(struct encoder *_encoder, struct audio_format *audio_format
|
||||||
static void
|
static void
|
||||||
twolame_encoder_close(struct encoder *_encoder)
|
twolame_encoder_close(struct encoder *_encoder)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
|
|
||||||
twolame_close(&encoder->options);
|
twolame_close(&encoder->options);
|
||||||
}
|
}
|
||||||
|
@ -229,7 +229,7 @@ twolame_encoder_close(struct encoder *_encoder)
|
||||||
static bool
|
static bool
|
||||||
twolame_encoder_flush(struct encoder *_encoder, gcc_unused GError **error)
|
twolame_encoder_flush(struct encoder *_encoder, gcc_unused GError **error)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
|
|
||||||
encoder->flush = true;
|
encoder->flush = true;
|
||||||
return true;
|
return true;
|
||||||
|
@ -240,20 +240,18 @@ twolame_encoder_write(struct encoder *_encoder,
|
||||||
const void *data, size_t length,
|
const void *data, size_t length,
|
||||||
gcc_unused GError **error)
|
gcc_unused GError **error)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
unsigned num_frames;
|
|
||||||
const int16_t *src = (const int16_t*)data;
|
const int16_t *src = (const int16_t*)data;
|
||||||
int bytes_out;
|
|
||||||
|
|
||||||
assert(encoder->buffer_length == 0);
|
assert(encoder->buffer_length == 0);
|
||||||
|
|
||||||
num_frames =
|
const unsigned num_frames =
|
||||||
length / audio_format_frame_size(&encoder->audio_format);
|
length / audio_format_frame_size(&encoder->audio_format);
|
||||||
|
|
||||||
bytes_out = twolame_encode_buffer_interleaved(encoder->options,
|
int bytes_out = twolame_encode_buffer_interleaved(encoder->options,
|
||||||
src, num_frames,
|
src, num_frames,
|
||||||
encoder->buffer,
|
encoder->buffer,
|
||||||
sizeof(encoder->buffer));
|
sizeof(encoder->buffer));
|
||||||
if (bytes_out < 0) {
|
if (bytes_out < 0) {
|
||||||
g_set_error(error, twolame_encoder_quark(), 0,
|
g_set_error(error, twolame_encoder_quark(), 0,
|
||||||
"twolame encoder failed");
|
"twolame encoder failed");
|
||||||
|
@ -267,7 +265,7 @@ twolame_encoder_write(struct encoder *_encoder,
|
||||||
static size_t
|
static size_t
|
||||||
twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
twolame_encoder_read(struct encoder *_encoder, void *dest, size_t length)
|
||||||
{
|
{
|
||||||
struct twolame_encoder *encoder = (struct twolame_encoder *)_encoder;
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
||||||
|
|
||||||
if (encoder->buffer_length == 0 && encoder->flush) {
|
if (encoder->buffer_length == 0 && encoder->flush) {
|
||||||
int ret = twolame_encode_flush(encoder->options,
|
int ret = twolame_encode_flush(encoder->options,
|
||||||
|
@ -298,14 +296,16 @@ twolame_encoder_get_mime_type(gcc_unused struct encoder *_encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct encoder_plugin twolame_encoder_plugin = {
|
const struct encoder_plugin twolame_encoder_plugin = {
|
||||||
.name = "twolame",
|
"twolame",
|
||||||
.init = twolame_encoder_init,
|
twolame_encoder_init,
|
||||||
.finish = twolame_encoder_finish,
|
twolame_encoder_finish,
|
||||||
.open = twolame_encoder_open,
|
twolame_encoder_open,
|
||||||
.close = twolame_encoder_close,
|
twolame_encoder_close,
|
||||||
.end = twolame_encoder_flush,
|
twolame_encoder_flush,
|
||||||
.flush = twolame_encoder_flush,
|
twolame_encoder_flush,
|
||||||
.write = twolame_encoder_write,
|
nullptr,
|
||||||
.read = twolame_encoder_read,
|
nullptr,
|
||||||
.get_mime_type = twolame_encoder_get_mime_type,
|
twolame_encoder_write,
|
||||||
|
twolame_encoder_read,
|
||||||
|
twolame_encoder_get_mime_type,
|
||||||
};
|
};
|
|
@ -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 encoder_plugin twolame_encoder_plugin;
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue