TwolameEncoderPlugin.cxx 6.46 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "TwolameEncoderPlugin.hxx"
22
#include "../EncoderAPI.hxx"
23
#include "AudioFormat.hxx"
24
#include "config/ConfigError.hxx"
25
#include "util/NumberParser.hxx"
26
#include "util/RuntimeError.hxx"
27 28
#include "util/Error.hxx"
#include "util/Domain.hxx"
29
#include "Log.hxx"
30 31

#include <twolame.h>
32

33 34 35
#include <assert.h>
#include <string.h>

36 37
class TwolameEncoder final : public Encoder {
	const AudioFormat audio_format;
38 39 40

	twolame_options *options;

41
	unsigned char output_buffer[32768];
42 43
	size_t output_buffer_length = 0;
	size_t output_buffer_position = 0;
44 45

	/**
46 47
	 * Call libtwolame's flush function when the output_buffer is
	 * empty?
48
	 */
49
	bool flush = false;
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
public:
	TwolameEncoder(const AudioFormat _audio_format,
		       twolame_options *_options)
		:Encoder(false),
		 audio_format(_audio_format), options(_options) {}
	~TwolameEncoder() override;

	bool Configure(const ConfigBlock &block, Error &error);

	/* virtual methods from class Encoder */

	bool End(Error &) override {
		flush = true;
		return true;
	}

	bool Flush(Error &) override {
		flush = true;
		return true;
	}

	bool Write(const void *data, size_t length, Error &) override;
	size_t Read(void *dest, size_t length) override;
};

76
class PreparedTwolameEncoder final : public PreparedEncoder {
77 78 79
	float quality;
	int bitrate;

80
public:
81
	PreparedTwolameEncoder(const ConfigBlock &block);
82 83 84 85 86 87 88

	/* virtual methods from class PreparedEncoder */
	Encoder *Open(AudioFormat &audio_format, Error &) override;

	const char *GetMimeType() const override {
		return  "audio/mpeg";
	}
89
};
90

91
static constexpr Domain twolame_encoder_domain("twolame_encoder");
92

93
PreparedTwolameEncoder::PreparedTwolameEncoder(const ConfigBlock &block)
94 95 96 97
{
	const char *value;
	char *endptr;

98
	value = block.GetBlockValue("quality");
99
	if (value != nullptr) {
100 101
		/* a quality was configured (VBR) */

102
		quality = ParseDouble(value, &endptr);
103

104 105 106 107 108 109 110
		if (*endptr != '\0' || quality < -1.0 || quality > 10.0)
			throw FormatRuntimeError("quality \"%s\" is not a number in the "
						 "range -1 to 10",
						 value);

		if (block.GetBlockValue("bitrate") != nullptr)
			throw std::runtime_error("quality and bitrate are both defined");
111 112 113
	} else {
		/* a bit rate was configured */

114
		value = block.GetBlockValue("bitrate");
115 116
		if (value == nullptr)
			throw std::runtime_error("neither bitrate nor quality defined");
117

118
		quality = -2.0;
119
		bitrate = ParseInt(value, &endptr);
120

121 122
		if (*endptr != '\0' || bitrate <= 0)
			throw std::runtime_error("bitrate should be a positive integer");
123 124 125
	}
}

126
static PreparedEncoder *
127
twolame_encoder_init(const ConfigBlock &block)
128
{
129 130
	FormatDebug(twolame_encoder_domain,
		    "libtwolame version %s", get_twolame_version());
131

132
	return new PreparedTwolameEncoder(block);
133 134 135
}

static bool
136 137
twolame_encoder_setup(twolame_options *options, float quality, int bitrate,
		      const AudioFormat &audio_format, Error &error)
138
{
139
	if (quality >= -1.0) {
140 141
		/* a quality was configured (VBR) */

142
		if (0 != twolame_set_VBR(options, true)) {
143 144
			error.Set(twolame_encoder_domain,
				  "error setting twolame VBR mode");
145 146
			return false;
		}
147
		if (0 != twolame_set_VBR_q(options, quality)) {
148 149
			error.Set(twolame_encoder_domain,
				  "error setting twolame VBR quality");
150 151 152 153 154
			return false;
		}
	} else {
		/* a bit rate was configured */

155
		if (0 != twolame_set_brate(options, bitrate)) {
156 157
			error.Set(twolame_encoder_domain,
				  "error setting twolame bitrate");
158 159 160 161
			return false;
		}
	}

162
	if (0 != twolame_set_num_channels(options, audio_format.channels)) {
163 164
		error.Set(twolame_encoder_domain,
			  "error setting twolame num channels");
165 166 167
		return false;
	}

168 169
	if (0 != twolame_set_in_samplerate(options,
					   audio_format.sample_rate)) {
170 171
		error.Set(twolame_encoder_domain,
			  "error setting twolame sample rate");
172 173 174
		return false;
	}

175
	if (0 > twolame_init_params(options)) {
176 177
		error.Set(twolame_encoder_domain,
			  "error initializing twolame params");
178 179 180 181 182 183
		return false;
	}

	return true;
}

184 185
Encoder *
PreparedTwolameEncoder::Open(AudioFormat &audio_format, Error &error)
186
{
187 188
	audio_format.format = SampleFormat::S16;
	audio_format.channels = 2;
189

190 191
	auto options = twolame_init();
	if (options == nullptr) {
192
		error.Set(twolame_encoder_domain, "twolame_init() failed");
193
		return nullptr;
194 195
	}

196
	if (!twolame_encoder_setup(options, quality, bitrate,
197 198 199
				   audio_format, error)) {
		twolame_close(&options);
		return nullptr;
200 201
	}

202
	return new TwolameEncoder(audio_format, options);
203 204
}

205
TwolameEncoder::~TwolameEncoder()
206
{
207
	twolame_close(&options);
208 209
}

210 211
bool
TwolameEncoder::Write(const void *data, size_t length,
212
		      gcc_unused Error &error)
213 214 215
{
	const int16_t *src = (const int16_t*)data;

216
	assert(output_buffer_position == output_buffer_length);
217

218
	const unsigned num_frames = length / audio_format.GetFrameSize();
219

220
	int bytes_out = twolame_encode_buffer_interleaved(options,
221
							  src, num_frames,
222 223
							  output_buffer,
							  sizeof(output_buffer));
224
	if (bytes_out < 0) {
225
		error.Set(twolame_encoder_domain, "twolame encoder failed");
226 227 228
		return false;
	}

229 230
	output_buffer_length = (size_t)bytes_out;
	output_buffer_position = 0;
231 232 233
	return true;
}

234 235
size_t
TwolameEncoder::Read(void *dest, size_t length)
236
{
237
	assert(output_buffer_position <= output_buffer_length);
238

239 240 241
	if (output_buffer_position == output_buffer_length && flush) {
		int ret = twolame_encode_flush(options, output_buffer,
					       sizeof(output_buffer));
242
		if (ret > 0) {
243 244
			output_buffer_length = (size_t)ret;
			output_buffer_position = 0;
245
		}
246

247
		flush = false;
248 249 250
	}


251
	const size_t remainning = output_buffer_length - output_buffer_position;
252 253 254
	if (length > remainning)
		length = remainning;

255
	memcpy(dest, output_buffer + output_buffer_position, length);
256

257
	output_buffer_position += length;
258 259 260 261

	return length;
}

262
const EncoderPlugin twolame_encoder_plugin = {
263 264
	"twolame",
	twolame_encoder_init,
265
};