TwolameEncoderPlugin.cxx 6.04 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
#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 61 62
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 */

63
	void End() override {
64 65 66
		flush = true;
	}

67
	void Flush() override {
68 69 70
		flush = true;
	}

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

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

79
public:
80
	PreparedTwolameEncoder(const ConfigBlock &block);
81 82

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

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

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

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

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

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

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

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

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

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

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

131
	return new PreparedTwolameEncoder(block);
132 133
}

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

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

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

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

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

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

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

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

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

182
	return new TwolameEncoder(audio_format, options);
183 184
}

185
TwolameEncoder::~TwolameEncoder()
186
{
187
	twolame_close(&options);
188 189
}

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

195
	assert(output_buffer_position == output_buffer_length);
196

197
	const unsigned num_frames = length / audio_format.GetFrameSize();
198

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

206 207
	output_buffer_length = (size_t)bytes_out;
	output_buffer_position = 0;
208 209
}

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

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

223
		flush = false;
224 225 226
	}


227
	const size_t remainning = output_buffer_length - output_buffer_position;
228 229 230
	if (length > remainning)
		length = remainning;

231
	memcpy(dest, output_buffer + output_buffer_position, length);
232

233
	output_buffer_position += length;
234 235 236 237

	return length;
}

238
const EncoderPlugin twolame_encoder_plugin = {
239 240
	"twolame",
	twolame_encoder_init,
241
};