TwolameEncoderPlugin.cxx 7.47 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 27
#include "util/Error.hxx"
#include "util/Domain.hxx"
28
#include "Log.hxx"
29 30

#include <twolame.h>
31

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

35
struct TwolameEncoder final {
36
	Encoder encoder;
37

38
	AudioFormat audio_format;
39 40 41 42 43
	float quality;
	int bitrate;

	twolame_options *options;

44 45
	unsigned char output_buffer[32768];
	size_t output_buffer_length;
46
	size_t output_buffer_position;
47 48

	/**
49 50
	 * Call libtwolame's flush function when the output_buffer is
	 * empty?
51 52 53
	 */
	bool flush;

54
	TwolameEncoder():encoder(twolame_encoder_plugin) {}
55

56
	bool Configure(const ConfigBlock &block, Error &error);
57
};
58

59
static constexpr Domain twolame_encoder_domain("twolame_encoder");
60

61
bool
62
TwolameEncoder::Configure(const ConfigBlock &block, Error &error)
63 64 65 66
{
	const char *value;
	char *endptr;

67
	value = block.GetBlockValue("quality");
68
	if (value != nullptr) {
69 70
		/* a quality was configured (VBR) */

71
		quality = ParseDouble(value, &endptr);
72

73
		if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
74 75
			error.Format(config_domain,
				     "quality \"%s\" is not a number in the "
76 77
				     "range -1 to 10",
				     value);
78 79 80
			return false;
		}

81
		if (block.GetBlockValue("bitrate") != nullptr) {
82 83
			error.Set(config_domain,
				  "quality and bitrate are both defined");
84 85 86 87 88
			return false;
		}
	} else {
		/* a bit rate was configured */

89
		value = block.GetBlockValue("bitrate");
90
		if (value == nullptr) {
91 92
			error.Set(config_domain,
				  "neither bitrate nor quality defined");
93 94 95
			return false;
		}

96
		quality = -2.0;
97
		bitrate = ParseInt(value, &endptr);
98

99
		if (*endptr != '\0' || bitrate <= 0) {
100 101
			error.Set(config_domain,
				  "bitrate should be a positive integer");
102 103 104 105 106 107 108
			return false;
		}
	}

	return true;
}

109
static Encoder *
110
twolame_encoder_init(const ConfigBlock &block, Error &error_r)
111
{
112 113
	FormatDebug(twolame_encoder_domain,
		    "libtwolame version %s", get_twolame_version());
114

115
	TwolameEncoder *encoder = new TwolameEncoder();
116

117 118
	/* load configuration from "block" */
	if (!encoder->Configure(block, error_r)) {
119
		/* configuration has failed, roll back and return error */
120 121
		delete encoder;
		return nullptr;
122 123 124 125 126 127
	}

	return &encoder->encoder;
}

static void
128
twolame_encoder_finish(Encoder *_encoder)
129
{
130
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
131 132 133

	/* the real libtwolame cleanup was already performed by
	   twolame_encoder_close(), so no real work here */
134
	delete encoder;
135 136 137
}

static bool
138
twolame_encoder_setup(TwolameEncoder *encoder, Error &error)
139 140 141 142 143
{
	if (encoder->quality >= -1.0) {
		/* a quality was configured (VBR) */

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

		if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) {
157 158
			error.Set(twolame_encoder_domain,
				  "error setting twolame bitrate");
159 160 161 162 163 164
			return false;
		}
	}

	if (0 != twolame_set_num_channels(encoder->options,
					  encoder->audio_format.channels)) {
165 166
		error.Set(twolame_encoder_domain,
			  "error setting twolame num channels");
167 168 169 170 171
		return false;
	}

	if (0 != twolame_set_in_samplerate(encoder->options,
					   encoder->audio_format.sample_rate)) {
172 173
		error.Set(twolame_encoder_domain,
			  "error setting twolame sample rate");
174 175 176 177
		return false;
	}

	if (0 > twolame_init_params(encoder->options)) {
178 179
		error.Set(twolame_encoder_domain,
			  "error initializing twolame params");
180 181 182 183 184 185 186
		return false;
	}

	return true;
}

static bool
187
twolame_encoder_open(Encoder *_encoder, AudioFormat &audio_format,
188
		     Error &error)
189
{
190
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
191

192 193
	audio_format.format = SampleFormat::S16;
	audio_format.channels = 2;
194

195
	encoder->audio_format = audio_format;
196 197

	encoder->options = twolame_init();
198
	if (encoder->options == nullptr) {
199
		error.Set(twolame_encoder_domain, "twolame_init() failed");
200 201 202 203 204 205 206 207
		return false;
	}

	if (!twolame_encoder_setup(encoder, error)) {
		twolame_close(&encoder->options);
		return false;
	}

208
	encoder->output_buffer_length = 0;
209
	encoder->output_buffer_position = 0;
210 211 212 213 214 215
	encoder->flush = false;

	return true;
}

static void
216
twolame_encoder_close(Encoder *_encoder)
217
{
218
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
219 220 221 222 223

	twolame_close(&encoder->options);
}

static bool
224
twolame_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
225
{
226
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
227 228 229 230 231 232

	encoder->flush = true;
	return true;
}

static bool
233
twolame_encoder_write(Encoder *_encoder,
234
		      const void *data, size_t length,
235
		      gcc_unused Error &error)
236
{
237
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
238 239
	const int16_t *src = (const int16_t*)data;

240 241
	assert(encoder->output_buffer_position ==
	       encoder->output_buffer_length);
242

243
	const unsigned num_frames =
244
		length / encoder->audio_format.GetFrameSize();
245

246 247
	int bytes_out = twolame_encode_buffer_interleaved(encoder->options,
							  src, num_frames,
248 249
							  encoder->output_buffer,
							  sizeof(encoder->output_buffer));
250
	if (bytes_out < 0) {
251
		error.Set(twolame_encoder_domain, "twolame encoder failed");
252 253 254
		return false;
	}

255
	encoder->output_buffer_length = (size_t)bytes_out;
256
	encoder->output_buffer_position = 0;
257 258 259 260
	return true;
}

static size_t
261
twolame_encoder_read(Encoder *_encoder, void *dest, size_t length)
262
{
263
	TwolameEncoder *encoder = (TwolameEncoder *)_encoder;
264

265 266 267 268 269
	assert(encoder->output_buffer_position <=
	       encoder->output_buffer_length);

	if (encoder->output_buffer_position == encoder->output_buffer_length &&
	    encoder->flush) {
270
		int ret = twolame_encode_flush(encoder->options,
271 272
					       encoder->output_buffer,
					       sizeof(encoder->output_buffer));
273
		if (ret > 0) {
274
			encoder->output_buffer_length = (size_t)ret;
275 276
			encoder->output_buffer_position = 0;
		}
277 278 279 280 281

		encoder->flush = false;
	}


282 283 284 285 286 287 288
	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);
289

290
	encoder->output_buffer_position += length;
291 292 293 294

	return length;
}

295
static const char *
296
twolame_encoder_get_mime_type(gcc_unused Encoder *_encoder)
297
{
298
	return "audio/mpeg";
299 300
}

301
const EncoderPlugin twolame_encoder_plugin = {
302 303 304 305 306 307 308 309 310 311 312 313
	"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,
314
};