2009-07-14 23:07:41 +02:00
|
|
|
/*
|
2013-07-30 08:52:47 +02:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-07-14 23:07:41 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-07-30 08:52:47 +02:00
|
|
|
#include "TwolameEncoderPlugin.hxx"
|
2013-07-30 09:04:05 +02:00
|
|
|
#include "EncoderAPI.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "ConfigError.hxx"
|
2013-10-21 09:48:31 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
#include <twolame.h>
|
2013-01-30 21:40:53 +01:00
|
|
|
|
2009-07-14 23:07:41 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
struct TwolameEncoder final {
|
2013-07-30 09:04:05 +02:00
|
|
|
Encoder encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2009-07-14 23:07:41 +02:00
|
|
|
float quality;
|
|
|
|
int bitrate;
|
|
|
|
|
|
|
|
twolame_options *options;
|
|
|
|
|
2013-08-06 08:47:51 +02:00
|
|
|
unsigned char output_buffer[32768];
|
|
|
|
size_t output_buffer_length;
|
2013-08-06 08:47:30 +02:00
|
|
|
size_t output_buffer_position;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-06 08:47:51 +02:00
|
|
|
* Call libtwolame's flush function when the output_buffer is
|
|
|
|
* empty?
|
2009-07-14 23:07:41 +02:00
|
|
|
*/
|
|
|
|
bool flush;
|
|
|
|
|
2013-07-30 09:04:05 +02:00
|
|
|
TwolameEncoder():encoder(twolame_encoder_plugin) {}
|
2013-07-30 08:52:47 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
bool Configure(const config_param ¶m, Error &error);
|
2013-07-30 08:52:47 +02:00
|
|
|
};
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain twolame_encoder_domain("twolame_encoder");
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
TwolameEncoder::Configure(const config_param ¶m, Error &error)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
char *endptr;
|
|
|
|
|
2013-08-04 12:24:36 +02:00
|
|
|
value = param.GetBlockValue("quality");
|
2013-07-30 08:52:47 +02:00
|
|
|
if (value != nullptr) {
|
2009-07-14 23:07:41 +02:00
|
|
|
/* a quality was configured (VBR) */
|
|
|
|
|
2013-10-21 09:48:31 +02:00
|
|
|
quality = ParseDouble(value, &endptr);
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(config_domain,
|
|
|
|
"quality \"%s\" is not a number in the "
|
2013-09-26 20:59:11 +02:00
|
|
|
"range -1 to 10",
|
|
|
|
value);
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-04 12:24:36 +02:00
|
|
|
if (param.GetBlockValue("bitrate") != nullptr) {
|
2013-09-26 20:59:11 +02:00
|
|
|
error.Set(config_domain,
|
|
|
|
"quality and bitrate are both defined");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* a bit rate was configured */
|
|
|
|
|
2013-08-04 12:24:36 +02:00
|
|
|
value = param.GetBlockValue("bitrate");
|
2013-07-30 08:52:47 +02:00
|
|
|
if (value == nullptr) {
|
2013-09-26 20:59:11 +02:00
|
|
|
error.Set(config_domain,
|
|
|
|
"neither bitrate nor quality defined");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
quality = -2.0;
|
2013-10-21 09:48:31 +02:00
|
|
|
bitrate = ParseInt(value, &endptr);
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
if (*endptr != '\0' || bitrate <= 0) {
|
2013-09-26 20:59:11 +02:00
|
|
|
error.Set(config_domain,
|
|
|
|
"bitrate should be a positive integer");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-30 09:04:05 +02:00
|
|
|
static Encoder *
|
2013-08-10 18:02:44 +02:00
|
|
|
twolame_encoder_init(const config_param ¶m, Error &error_r)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(twolame_encoder_domain,
|
|
|
|
"libtwolame version %s", get_twolame_version());
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = new TwolameEncoder();
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
/* load configuration from "param" */
|
2013-07-30 08:52:47 +02:00
|
|
|
if (!encoder->Configure(param, error_r)) {
|
2009-07-14 23:07:41 +02:00
|
|
|
/* configuration has failed, roll back and return error */
|
2013-07-30 08:52:47 +02:00
|
|
|
delete encoder;
|
|
|
|
return nullptr;
|
2009-07-14 23:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &encoder->encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-07-30 09:04:05 +02:00
|
|
|
twolame_encoder_finish(Encoder *_encoder)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
/* the real libtwolame cleanup was already performed by
|
|
|
|
twolame_encoder_close(), so no real work here */
|
2013-07-30 08:52:47 +02:00
|
|
|
delete encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
twolame_encoder_setup(TwolameEncoder *encoder, Error &error)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
|
|
|
if (encoder->quality >= -1.0) {
|
|
|
|
/* a quality was configured (VBR) */
|
|
|
|
|
|
|
|
if (0 != twolame_set_VBR(encoder->options, true)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error setting twolame VBR mode");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (0 != twolame_set_VBR_q(encoder->options, encoder->quality)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error setting twolame VBR quality");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* a bit rate was configured */
|
|
|
|
|
|
|
|
if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error setting twolame bitrate");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != twolame_set_num_channels(encoder->options,
|
|
|
|
encoder->audio_format.channels)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error setting twolame num channels");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != twolame_set_in_samplerate(encoder->options,
|
|
|
|
encoder->audio_format.sample_rate)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error setting twolame sample rate");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 > twolame_init_params(encoder->options)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain,
|
|
|
|
"error initializing twolame params");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-03 21:00:50 +02:00
|
|
|
twolame_encoder_open(Encoder *_encoder, AudioFormat &audio_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
|
|
|
audio_format.channels = 2;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
encoder->audio_format = audio_format;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
encoder->options = twolame_init();
|
2013-07-30 08:52:47 +02:00
|
|
|
if (encoder->options == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain, "twolame_init() failed");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!twolame_encoder_setup(encoder, error)) {
|
|
|
|
twolame_close(&encoder->options);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:47:51 +02:00
|
|
|
encoder->output_buffer_length = 0;
|
2013-08-06 08:47:30 +02:00
|
|
|
encoder->output_buffer_position = 0;
|
2009-07-14 23:07:41 +02:00
|
|
|
encoder->flush = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-07-30 09:04:05 +02:00
|
|
|
twolame_encoder_close(Encoder *_encoder)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
twolame_close(&encoder->options);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
twolame_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
encoder->flush = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-07-30 09:04:05 +02:00
|
|
|
twolame_encoder_write(Encoder *_encoder,
|
2009-07-14 23:07:41 +02:00
|
|
|
const void *data, size_t length,
|
2013-08-10 18:02:44 +02:00
|
|
|
gcc_unused Error &error)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
const int16_t *src = (const int16_t*)data;
|
|
|
|
|
2013-08-06 08:47:30 +02:00
|
|
|
assert(encoder->output_buffer_position ==
|
|
|
|
encoder->output_buffer_length);
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
const unsigned num_frames =
|
2013-08-03 21:00:50 +02:00
|
|
|
length / encoder->audio_format.GetFrameSize();
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-07-30 08:52:47 +02:00
|
|
|
int bytes_out = twolame_encode_buffer_interleaved(encoder->options,
|
|
|
|
src, num_frames,
|
2013-08-06 08:47:51 +02:00
|
|
|
encoder->output_buffer,
|
|
|
|
sizeof(encoder->output_buffer));
|
2009-07-14 23:07:41 +02:00
|
|
|
if (bytes_out < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(twolame_encoder_domain, "twolame encoder failed");
|
2009-07-14 23:07:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:47:51 +02:00
|
|
|
encoder->output_buffer_length = (size_t)bytes_out;
|
2013-08-06 08:47:30 +02:00
|
|
|
encoder->output_buffer_position = 0;
|
2009-07-14 23:07:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2013-07-30 09:04:05 +02:00
|
|
|
twolame_encoder_read(Encoder *_encoder, void *dest, size_t length)
|
2009-07-14 23:07:41 +02:00
|
|
|
{
|
2013-07-30 08:52:47 +02:00
|
|
|
TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-06 08:47:30 +02:00
|
|
|
assert(encoder->output_buffer_position <=
|
|
|
|
encoder->output_buffer_length);
|
|
|
|
|
|
|
|
if (encoder->output_buffer_position == encoder->output_buffer_length &&
|
|
|
|
encoder->flush) {
|
2009-07-14 23:07:41 +02:00
|
|
|
int ret = twolame_encode_flush(encoder->options,
|
2013-08-06 08:47:51 +02:00
|
|
|
encoder->output_buffer,
|
|
|
|
sizeof(encoder->output_buffer));
|
2013-08-06 08:47:30 +02:00
|
|
|
if (ret > 0) {
|
2013-08-06 08:47:51 +02:00
|
|
|
encoder->output_buffer_length = (size_t)ret;
|
2013-08-06 08:47:30 +02:00
|
|
|
encoder->output_buffer_position = 0;
|
|
|
|
}
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
encoder->flush = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 08:47:30 +02:00
|
|
|
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);
|
2009-07-14 23:07:41 +02:00
|
|
|
|
2013-08-06 08:47:30 +02:00
|
|
|
encoder->output_buffer_position += length;
|
2009-07-14 23:07:41 +02:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-12-03 20:11:32 +01:00
|
|
|
static const char *
|
2013-07-30 09:04:05 +02:00
|
|
|
twolame_encoder_get_mime_type(gcc_unused Encoder *_encoder)
|
2009-12-03 20:11:32 +01:00
|
|
|
{
|
2009-12-03 20:43:13 +01:00
|
|
|
return "audio/mpeg";
|
2009-12-03 20:11:32 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 09:04:05 +02:00
|
|
|
const EncoderPlugin twolame_encoder_plugin = {
|
2013-07-30 08:52:47 +02:00
|
|
|
"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,
|
2009-07-14 23:07:41 +02:00
|
|
|
};
|