VorbisEncoderPlugin.cxx 6.52 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 "VorbisEncoderPlugin.hxx"
22
#include "OggEncoder.hxx"
23
#include "lib/xiph/VorbisComment.hxx"
24
#include "AudioFormat.hxx"
25
#include "config/ConfigError.hxx"
26
#include "util/StringUtil.hxx"
27
#include "util/NumberParser.hxx"
28
#include "util/RuntimeError.hxx"
29 30
#include "util/Error.hxx"
#include "util/Domain.hxx"
31 32 33

#include <vorbis/vorbisenc.h>

34
class VorbisEncoder final : public OggEncoder {
35
	AudioFormat audio_format;
36 37 38 39

	vorbis_dsp_state vd;
	vorbis_block vb;
	vorbis_info vi;
40

41 42
public:
	VorbisEncoder()
43 44 45
		:OggEncoder(true) {
		vorbis_info_init(&vi);
	}
46

47
	virtual ~VorbisEncoder() {
48 49 50
		vorbis_block_clear(&vb);
		vorbis_dsp_clear(&vd);
		vorbis_info_clear(&vi);
51
	}
52

53 54 55 56 57 58 59
	bool Open(float quality, int bitrate, AudioFormat &audio_format,
		  Error &error);

	/* virtual methods from class Encoder */
	bool End(Error &error) override {
		return PreTag(error);
	}
60

61 62 63 64 65 66
	bool PreTag(Error &error) override;
	bool SendTag(const Tag &tag, Error &error) override;

	bool Write(const void *data, size_t length, Error &) override;

private:
67 68 69
	void HeaderOut(vorbis_comment &vc);
	void SendHeader();
	void BlockOut();
70 71
};

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

76
public:
77
	PreparedVorbisEncoder(const ConfigBlock &block);
78 79 80 81 82 83 84

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

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

87
static constexpr Domain vorbis_encoder_domain("vorbis_encoder");
88

89
PreparedVorbisEncoder::PreparedVorbisEncoder(const ConfigBlock &block)
90
{
91
	const char *value = block.GetBlockValue("quality");
92
	if (value != nullptr) {
93 94
		/* a quality was configured (VBR) */

95
		char *endptr;
96
		quality = ParseDouble(value, &endptr);
97

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

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

112
		quality = -2.0;
113

114
		char *endptr;
115
		bitrate = ParseInt(value, &endptr);
116 117
		if (*endptr != '\0' || bitrate <= 0)
			throw std::runtime_error("bitrate should be a positive integer");
118 119 120
	}
}

121
static PreparedEncoder *
122
vorbis_encoder_init(const ConfigBlock &block)
123
{
124
	return new PreparedVorbisEncoder(block);
125 126
}

127
bool
128 129
VorbisEncoder::Open(float quality, int bitrate, AudioFormat &_audio_format,
		    Error &error)
130
{
131 132 133
	_audio_format.format = SampleFormat::FLOAT;
	audio_format = _audio_format;

134
	if (quality >= -1.0) {
135 136
		/* a quality was configured (VBR) */

137 138 139 140
		if (0 != vorbis_encode_init_vbr(&vi,
						audio_format.channels,
						audio_format.sample_rate,
						quality * 0.1)) {
141 142
			error.Set(vorbis_encoder_domain,
				  "error initializing vorbis vbr");
143 144 145 146 147
			return false;
		}
	} else {
		/* a bit rate was configured */

148 149 150 151
		if (0 != vorbis_encode_init(&vi,
					    audio_format.channels,
					    audio_format.sample_rate, -1.0,
					    bitrate * 1000, -1.0)) {
152 153
			error.Set(vorbis_encoder_domain,
				  "error initializing vorbis encoder");
154 155 156 157
			return false;
		}
	}

158 159
	vorbis_analysis_init(&vd, &vi);
	vorbis_block_init(&vd, &vb);
160

161 162
	SendHeader();

163 164 165
	return true;
}

166 167
void
VorbisEncoder::HeaderOut(vorbis_comment &vc)
168 169 170
{
	ogg_packet packet, comments, codebooks;

171
	vorbis_analysis_headerout(&vd, &vc,
172 173
				  &packet, &comments, &codebooks);

174 175 176
	stream.PacketIn(packet);
	stream.PacketIn(comments);
	stream.PacketIn(codebooks);
177
}
178

179 180
void
VorbisEncoder::SendHeader()
181
{
182
	VorbisComment vc;
183
	HeaderOut(vc);
184 185
}

186 187
Encoder *
PreparedVorbisEncoder::Open(AudioFormat &audio_format, Error &error)
188
{
189
	auto *e = new VorbisEncoder();
190
	if (!e->Open(quality, bitrate, audio_format, error)) {
191 192 193
		delete e;
		return nullptr;
	}
194

195
	return e;
196 197
}

198 199
void
VorbisEncoder::BlockOut()
200
{
201 202 203
	while (vorbis_analysis_blockout(&vd, &vb) == 1) {
		vorbis_analysis(&vb, nullptr);
		vorbis_bitrate_addblock(&vb);
204

205
		ogg_packet packet;
206 207
		while (vorbis_bitrate_flushpacket(&vd, &packet))
			stream.PacketIn(packet);
208 209 210
	}
}

211 212
bool
VorbisEncoder::PreTag(gcc_unused Error &error)
213
{
214 215
	vorbis_analysis_wrote(&vd, 0);
	BlockOut();
216

217 218
	/* reinitialize vorbis_dsp_state and vorbis_block to reset the
	   end-of-stream marker */
219 220 221 222
	vorbis_block_clear(&vb);
	vorbis_dsp_clear(&vd);
	vorbis_analysis_init(&vd, &vi);
	vorbis_block_init(&vd, &vb);
223

224
	Flush();
225 226 227 228
	return true;
}

static void
229
copy_tag_to_vorbis_comment(VorbisComment &vc, const Tag &tag)
230
{
231
	for (const auto &item : tag) {
232 233
		char name[64];
		ToUpperASCII(name, tag_item_names[item.type], sizeof(name));
234
		vc.AddTag(name, item.value);
235 236 237
	}
}

238 239
bool
VorbisEncoder::SendTag(const Tag &tag, gcc_unused Error &error)
240
{
241
	/* write the vorbis_comment object */
242

243 244
	VorbisComment comment;
	copy_tag_to_vorbis_comment(comment, tag);
245

246
	/* reset ogg_stream_state and begin a new stream */
247

248
	stream.Reinitialize(GenerateOggSerial());
249 250 251

	/* send that vorbis_comment to the ogg_stream_state */

252
	HeaderOut(comment);
253

254 255 256 257
	return true;
}

static void
258 259
interleaved_to_vorbis_buffer(float **dest, const float *src,
			     unsigned num_frames, unsigned num_channels)
260 261 262
{
	for (unsigned i = 0; i < num_frames; i++)
		for (unsigned j = 0; j < num_channels; j++)
263
			dest[j][i] = *src++;
264 265
}

266 267
bool
VorbisEncoder::Write(const void *data, size_t length, gcc_unused Error &error)
268
{
269
	unsigned num_frames = length / audio_format.GetFrameSize();
270 271 272

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

273
	interleaved_to_vorbis_buffer(vorbis_analysis_buffer(&vd, num_frames),
274 275
				     (const float *)data,
				     num_frames,
276
				     audio_format.channels);
277

278 279
	vorbis_analysis_wrote(&vd, num_frames);
	BlockOut();
280 281 282
	return true;
}

283
const EncoderPlugin vorbis_encoder_plugin = {
284 285
	"vorbis",
	vorbis_encoder_init,
286
};