TwolameEncoderPlugin.cxx 5.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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/Domain.hxx"
25
#include "util/NumberParser.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/Domain.hxx"
28
#include "Log.hxx"
29 30

#include <twolame.h>
31

32 33
#include <stdexcept>

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

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

	twolame_options *options;

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

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

52 53 54 55 56 57 58 59 60
public:
	TwolameEncoder(const AudioFormat _audio_format,
		       twolame_options *_options)
		:Encoder(false),
		 audio_format(_audio_format), options(_options) {}
	~TwolameEncoder() override;

	/* virtual methods from class Encoder */

61
	void End() override {
62 63 64
		flush = true;
	}

65
	void Flush() override {
66 67 68
		flush = true;
	}

69
	void Write(const void *data, size_t length) override;
70 71 72
	size_t Read(void *dest, size_t length) override;
};

73
class PreparedTwolameEncoder final : public PreparedEncoder {
74 75 76
	float quality;
	int bitrate;

77
public:
78
	PreparedTwolameEncoder(const ConfigBlock &block);
79 80

	/* virtual methods from class PreparedEncoder */
81
	Encoder *Open(AudioFormat &audio_format) override;
82 83 84 85

	const char *GetMimeType() const override {
		return  "audio/mpeg";
	}
86
};
87

88
static constexpr Domain twolame_encoder_domain("twolame_encoder");
89

90
PreparedTwolameEncoder::PreparedTwolameEncoder(const ConfigBlock &block)
91 92 93 94
{
	const char *value;
	char *endptr;

95
	value = block.GetBlockValue("quality");
96
	if (value != nullptr) {
97 98
		/* a quality was configured (VBR) */

99
		quality = ParseDouble(value, &endptr);
100

101 102 103 104 105 106 107
		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");
108 109 110
	} else {
		/* a bit rate was configured */

111
		value = block.GetBlockValue("bitrate");
112 113
		if (value == nullptr)
			throw std::runtime_error("neither bitrate nor quality defined");
114

115
		quality = -2.0;
116
		bitrate = ParseInt(value, &endptr);
117

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

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

129
	return new PreparedTwolameEncoder(block);
130 131
}

132
static void
133
twolame_encoder_setup(twolame_options *options, float quality, int bitrate,
134
		      const AudioFormat &audio_format)
135
{
136
	if (quality >= -1.0) {
137 138
		/* a quality was configured (VBR) */

139 140 141 142 143
		if (0 != twolame_set_VBR(options, true))
			throw std::runtime_error("error setting twolame VBR mode");

		if (0 != twolame_set_VBR_q(options, quality))
			throw std::runtime_error("error setting twolame VBR quality");
144 145 146
	} else {
		/* a bit rate was configured */

147 148
		if (0 != twolame_set_brate(options, bitrate))
			throw std::runtime_error("error setting twolame bitrate");
149 150
	}

151 152
	if (0 != twolame_set_num_channels(options, audio_format.channels))
		throw std::runtime_error("error setting twolame num channels");
153

154
	if (0 != twolame_set_in_samplerate(options,
155 156
					   audio_format.sample_rate))
		throw std::runtime_error("error setting twolame sample rate");
157

158 159
	if (0 > twolame_init_params(options))
		throw std::runtime_error("error initializing twolame params");
160 161
}

162
Encoder *
163
PreparedTwolameEncoder::Open(AudioFormat &audio_format)
164
{
165 166
	audio_format.format = SampleFormat::S16;
	audio_format.channels = 2;
167

168
	auto options = twolame_init();
169 170
	if (options == nullptr)
		throw std::runtime_error("twolame_init() failed");
171

172 173 174 175
	try {
		twolame_encoder_setup(options, quality, bitrate,
				      audio_format);
	} catch (...) {
176
		twolame_close(&options);
177
		throw;
178 179
	}

180
	return new TwolameEncoder(audio_format, options);
181 182
}

183
TwolameEncoder::~TwolameEncoder()
184
{
185
	twolame_close(&options);
186 187
}

188 189
void
TwolameEncoder::Write(const void *data, size_t length)
190 191 192
{
	const int16_t *src = (const int16_t*)data;

193
	assert(output_buffer_position == output_buffer_length);
194

195
	const unsigned num_frames = length / audio_format.GetFrameSize();
196

197
	int bytes_out = twolame_encode_buffer_interleaved(options,
198
							  src, num_frames,
199 200
							  output_buffer,
							  sizeof(output_buffer));
201 202
	if (bytes_out < 0)
		throw std::runtime_error("twolame encoder failed");
203

204 205
	output_buffer_length = (size_t)bytes_out;
	output_buffer_position = 0;
206 207
}

208 209
size_t
TwolameEncoder::Read(void *dest, size_t length)
210
{
211
	assert(output_buffer_position <= output_buffer_length);
212

213 214 215
	if (output_buffer_position == output_buffer_length && flush) {
		int ret = twolame_encode_flush(options, output_buffer,
					       sizeof(output_buffer));
216
		if (ret > 0) {
217 218
			output_buffer_length = (size_t)ret;
			output_buffer_position = 0;
219
		}
220

221
		flush = false;
222 223 224
	}


225
	const size_t remainning = output_buffer_length - output_buffer_position;
226 227 228
	if (length > remainning)
		length = remainning;

229
	memcpy(dest, output_buffer + output_buffer_position, length);
230

231
	output_buffer_position += length;
232 233 234 235

	return length;
}

236
const EncoderPlugin twolame_encoder_plugin = {
237 238
	"twolame",
	twolame_encoder_init,
239
};