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

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

#include <lame/lame.h>
32

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

36 37
class LameEncoder final : public Encoder {
	const AudioFormat audio_format;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	lame_global_flags *const gfp;

	ReusableArray<unsigned char, 32768> output_buffer;
	unsigned char *output_begin = nullptr, *output_end = nullptr;

public:
	LameEncoder(const AudioFormat _audio_format,
		    lame_global_flags *_gfp)
		:Encoder(false),
		 audio_format(_audio_format), gfp(_gfp) {}

	~LameEncoder() override;

	/* virtual methods from class Encoder */
	bool Write(const void *data, size_t length, Error &) override;
	size_t Read(void *dest, size_t length) override;
};
56

57
class PreparedLameEncoder final : public PreparedEncoder {
58 59
	float quality;
	int bitrate;
60

61
public:
62
	PreparedLameEncoder(const ConfigBlock &block);
63 64 65 66 67 68 69

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

	const char *GetMimeType() const override {
		return "audio/mpeg";
	}
70
};
71

72
static constexpr Domain lame_encoder_domain("lame_encoder");
73

74
PreparedLameEncoder::PreparedLameEncoder(const ConfigBlock &block)
75 76 77 78
{
	const char *value;
	char *endptr;

79
	value = block.GetBlockValue("quality");
80
	if (value != nullptr) {
81 82
		/* a quality was configured (VBR) */

83
		quality = ParseDouble(value, &endptr);
84

85 86 87 88
		if (*endptr != '\0' || quality < -1.0 || quality > 10.0)
			throw FormatRuntimeError("quality \"%s\" is not a number in the "
						 "range -1 to 10",
						 value);
89

90 91
		if (block.GetBlockValue("bitrate") != nullptr)
			throw std::runtime_error("quality and bitrate are both defined");
92 93 94
	} else {
		/* a bit rate was configured */

95
		value = block.GetBlockValue("bitrate");
96 97
		if (value == nullptr)
			throw std::runtime_error("neither bitrate nor quality defined");
98

99
		quality = -2.0;
100
		bitrate = ParseInt(value, &endptr);
101

102 103
		if (*endptr != '\0' || bitrate <= 0)
			throw std::runtime_error("bitrate should be a positive integer");
104 105 106
	}
}

107
static PreparedEncoder *
108
lame_encoder_init(const ConfigBlock &block)
109
{
110
	return new PreparedLameEncoder(block);
111 112 113
}

static bool
114 115
lame_encoder_setup(lame_global_flags *gfp, float quality, int bitrate,
		   const AudioFormat &audio_format, Error &error)
116
{
117
	if (quality >= -1.0) {
118 119
		/* a quality was configured (VBR) */

120
		if (0 != lame_set_VBR(gfp, vbr_rh)) {
121 122
			error.Set(lame_encoder_domain,
				  "error setting lame VBR mode");
123 124
			return false;
		}
125
		if (0 != lame_set_VBR_q(gfp, quality)) {
126 127
			error.Set(lame_encoder_domain,
				  "error setting lame VBR quality");
128 129 130 131 132
			return false;
		}
	} else {
		/* a bit rate was configured */

133
		if (0 != lame_set_brate(gfp, bitrate)) {
134 135
			error.Set(lame_encoder_domain,
				  "error setting lame bitrate");
136 137 138 139
			return false;
		}
	}

140
	if (0 != lame_set_num_channels(gfp, audio_format.channels)) {
141 142
		error.Set(lame_encoder_domain,
			  "error setting lame num channels");
143 144 145
		return false;
	}

146
	if (0 != lame_set_in_samplerate(gfp, audio_format.sample_rate)) {
147 148
		error.Set(lame_encoder_domain,
			  "error setting lame sample rate");
149 150 151
		return false;
	}

152
	if (0 != lame_set_out_samplerate(gfp, audio_format.sample_rate)) {
153 154
		error.Set(lame_encoder_domain,
			  "error setting lame out sample rate");
155 156 157
		return false;
	}

158
	if (0 > lame_init_params(gfp)) {
159 160
		error.Set(lame_encoder_domain,
			  "error initializing lame params");
161 162 163 164 165 166
		return false;
	}

	return true;
}

167 168
Encoder *
PreparedLameEncoder::Open(AudioFormat &audio_format, Error &error)
169
{
170 171
	audio_format.format = SampleFormat::S16;
	audio_format.channels = 2;
172

173 174
	auto gfp = lame_init();
	if (gfp == nullptr) {
175
		error.Set(lame_encoder_domain, "lame_init() failed");
176
		return nullptr;
177 178
	}

179
	if (!lame_encoder_setup(gfp, quality, bitrate,
180 181 182
				audio_format, error)) {
		lame_close(gfp);
		return nullptr;
183 184
	}

185
	return new LameEncoder(audio_format, gfp);
186 187
}

188
LameEncoder::~LameEncoder()
189
{
190
	lame_close(gfp);
191 192
}

193 194
bool
LameEncoder::Write(const void *data, size_t length,
195
		   gcc_unused Error &error)
196 197 198
{
	const int16_t *src = (const int16_t*)data;

199
	assert(output_begin == output_end);
200

201 202
	const unsigned num_frames = length / audio_format.GetFrameSize();
	const unsigned num_samples = length / audio_format.GetSampleSize();
203 204 205

	/* worst-case formula according to LAME documentation */
	const size_t output_buffer_size = 5 * num_samples / 4 + 7200;
206
	const auto dest = output_buffer.Get(output_buffer_size);
207 208 209

	/* this is for only 16-bit audio */

210
	int bytes_out = lame_encode_buffer_interleaved(gfp,
211 212
						       const_cast<short *>(src),
						       num_frames,
213
						       dest, output_buffer_size);
214 215

	if (bytes_out < 0) {
216
		error.Set(lame_encoder_domain, "lame encoder failed");
217 218 219
		return false;
	}

220 221
	output_begin = dest;
	output_end = dest + bytes_out;
222 223 224
	return true;
}

225 226
size_t
LameEncoder::Read(void *dest, size_t length)
227
{
228 229 230
	const auto begin = output_begin;
	assert(begin <= output_end);
	const size_t remainning = output_end - begin;
231 232
	if (length > remainning)
		length = remainning;
233

234
	memcpy(dest, begin, length);
235

236
	output_begin = begin + length;
237 238 239
	return length;
}

240
const EncoderPlugin lame_encoder_plugin = {
241 242
	"lame",
	lame_encoder_init,
243
};