LameEncoderPlugin.cxx 5.51 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 <lame/lame.h>
30

31 32
#include <stdexcept>

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
	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 */
53
	void Write(const void *data, size_t length) override;
54 55
	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

	/* virtual methods from class PreparedEncoder */
65
	Encoder *Open(AudioFormat &audio_format) override;
66 67 68 69

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

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

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

81
		quality = ParseDouble(value, &endptr);
82

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

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

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

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

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

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

111
static void
112
lame_encoder_setup(lame_global_flags *gfp, float quality, int bitrate,
113
		   const AudioFormat &audio_format)
114
{
115
	if (quality >= -1.0) {
116 117
		/* a quality was configured (VBR) */

118 119 120 121 122
		if (0 != lame_set_VBR(gfp, vbr_rh))
			throw std::runtime_error("error setting lame VBR mode");

		if (0 != lame_set_VBR_q(gfp, quality))
			throw std::runtime_error("error setting lame VBR quality");
123 124 125
	} else {
		/* a bit rate was configured */

126 127
		if (0 != lame_set_brate(gfp, bitrate))
			throw std::runtime_error("error setting lame bitrate");
128 129
	}

130 131
	if (0 != lame_set_num_channels(gfp, audio_format.channels))
		throw std::runtime_error("error setting lame num channels");
132

133 134
	if (0 != lame_set_in_samplerate(gfp, audio_format.sample_rate))
		throw std::runtime_error("error setting lame sample rate");
135

136 137
	if (0 != lame_set_out_samplerate(gfp, audio_format.sample_rate))
		throw std::runtime_error("error setting lame out sample rate");
138

139 140
	if (0 > lame_init_params(gfp))
		throw std::runtime_error("error initializing lame params");
141 142
}

143
Encoder *
144
PreparedLameEncoder::Open(AudioFormat &audio_format)
145
{
146 147
	audio_format.format = SampleFormat::S16;
	audio_format.channels = 2;
148

149
	auto gfp = lame_init();
150 151
	if (gfp == nullptr)
		throw std::runtime_error("lame_init() failed");
152

153 154 155
	try {
		lame_encoder_setup(gfp, quality, bitrate, audio_format);
	} catch (...) {
156
		lame_close(gfp);
157
		throw;
158 159
	}

160
	return new LameEncoder(audio_format, gfp);
161 162
}

163
LameEncoder::~LameEncoder()
164
{
165
	lame_close(gfp);
166 167
}

168 169
void
LameEncoder::Write(const void *data, size_t length)
170 171 172
{
	const int16_t *src = (const int16_t*)data;

173
	assert(output_begin == output_end);
174

175 176
	const unsigned num_frames = length / audio_format.GetFrameSize();
	const unsigned num_samples = length / audio_format.GetSampleSize();
177 178 179

	/* worst-case formula according to LAME documentation */
	const size_t output_buffer_size = 5 * num_samples / 4 + 7200;
180
	const auto dest = output_buffer.Get(output_buffer_size);
181 182 183

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

184
	int bytes_out = lame_encode_buffer_interleaved(gfp,
185 186
						       const_cast<short *>(src),
						       num_frames,
187
						       dest, output_buffer_size);
188

189 190
	if (bytes_out < 0)
		throw std::runtime_error("lame encoder failed");
191

192 193
	output_begin = dest;
	output_end = dest + bytes_out;
194 195
}

196 197
size_t
LameEncoder::Read(void *dest, size_t length)
198
{
199 200 201
	const auto begin = output_begin;
	assert(begin <= output_end);
	const size_t remainning = output_end - begin;
202 203
	if (length > remainning)
		length = remainning;
204

205
	memcpy(dest, begin, length);
206

207
	output_begin = begin + length;
208 209 210
	return length;
}

211
const EncoderPlugin lame_encoder_plugin = {
212 213
	"lame",
	lame_encoder_init,
214
};