TwolameEncoderPlugin.cxx 5.96 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "TwolameEncoderPlugin.hxx"
21
#include "../EncoderAPI.hxx"
22
#include "AudioFormat.hxx"
23
#include "config/Domain.hxx"
24
#include "util/NumberParser.hxx"
25
#include "util/RuntimeError.hxx"
26
#include "util/Domain.hxx"
27
#include "Log.hxx"
28 29

#include <twolame.h>
30

31 32
#include <stdexcept>

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
public:
	TwolameEncoder(const AudioFormat _audio_format,
		       twolame_options *_options)
		:Encoder(false),
		 audio_format(_audio_format), options(_options) {}
	~TwolameEncoder() override;

	/* virtual methods from class Encoder */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

128
	return new PreparedTwolameEncoder(block);
129 130
}

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

138 139 140 141 142
		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");
143 144 145
	} else {
		/* a bit rate was configured */

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

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

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

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

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

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

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

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

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

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

192
	assert(output_buffer_position == output_buffer_length);
193

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

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

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

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

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

220
		flush = false;
221 222 223
	}


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

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

230
	output_buffer_position += length;
231 232 233 234

	return length;
}

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