VorbisEncoderPlugin.cxx 8.78 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "OggStream.hxx"
23
#include "OggSerial.hxx"
24
#include "../EncoderAPI.hxx"
25
#include "tag/Tag.hxx"
26
#include "AudioFormat.hxx"
27
#include "config/ConfigError.hxx"
28
#include "util/NumberParser.hxx"
29 30
#include "util/Error.hxx"
#include "util/Domain.hxx"
31 32 33

#include <vorbis/vorbisenc.h>

34 35
#include <glib.h>

36 37
struct vorbis_encoder {
	/** the base class */
38
	Encoder encoder;
39 40 41 42 43 44 45 46

	/* configuration */

	float quality;
	int bitrate;

	/* runtime information */

47
	AudioFormat audio_format;
48 49 50 51

	vorbis_dsp_state vd;
	vorbis_block vb;
	vorbis_info vi;
52

53
	OggStream stream;
54

55
	vorbis_encoder():encoder(vorbis_encoder_plugin) {}
56 57
};

58
static constexpr Domain vorbis_encoder_domain("vorbis_encoder");
59 60 61

static bool
vorbis_encoder_configure(struct vorbis_encoder *encoder,
62
			 const config_param &param, Error &error)
63
{
64
	const char *value = param.GetBlockValue("quality");
65
	if (value != nullptr) {
66 67
		/* a quality was configured (VBR) */

68
		char *endptr;
69
		encoder->quality = ParseDouble(value, &endptr);
70 71 72

		if (*endptr != '\0' || encoder->quality < -1.0 ||
		    encoder->quality > 10.0) {
73 74
			error.Format(config_domain,
				     "quality \"%s\" is not a number in the "
75 76
				     "range -1 to 10",
				     value);
77 78 79
			return false;
		}

80
		if (param.GetBlockValue("bitrate") != nullptr) {
81 82
			error.Set(config_domain,
				  "quality and bitrate are both defined");
83 84 85 86 87
			return false;
		}
	} else {
		/* a bit rate was configured */

88
		value = param.GetBlockValue("bitrate");
89
		if (value == nullptr) {
90 91
			error.Set(config_domain,
				  "neither bitrate nor quality defined");
92 93 94 95 96
			return false;
		}

		encoder->quality = -2.0;

97
		char *endptr;
98
		encoder->bitrate = ParseInt(value, &endptr);
99
		if (*endptr != '\0' || encoder->bitrate <= 0) {
100 101
			error.Set(config_domain,
				  "bitrate should be a positive integer");
102 103 104 105 106 107 108
			return false;
		}
	}

	return true;
}

109
static Encoder *
110
vorbis_encoder_init(const config_param &param, Error &error)
111
{
112
	vorbis_encoder *encoder = new vorbis_encoder();
113 114 115 116

	/* load configuration from "param" */
	if (!vorbis_encoder_configure(encoder, param, error)) {
		/* configuration has failed, roll back and return error */
117
		delete encoder;
118
		return nullptr;
119 120 121 122 123 124
	}

	return &encoder->encoder;
}

static void
125
vorbis_encoder_finish(Encoder *_encoder)
126 127 128 129 130
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

	/* the real libvorbis/libogg cleanup was already performed by
	   vorbis_encoder_close(), so no real work here */
131
	delete encoder;
132 133 134
}

static bool
135
vorbis_encoder_reinit(struct vorbis_encoder *encoder, Error &error)
136 137 138 139 140 141 142 143 144 145
{
	vorbis_info_init(&encoder->vi);

	if (encoder->quality >= -1.0) {
		/* a quality was configured (VBR) */

		if (0 != vorbis_encode_init_vbr(&encoder->vi,
						encoder->audio_format.channels,
						encoder->audio_format.sample_rate,
						encoder->quality * 0.1)) {
146 147
			error.Set(vorbis_encoder_domain,
				  "error initializing vorbis vbr");
148 149 150 151 152 153 154 155 156 157
			vorbis_info_clear(&encoder->vi);
			return false;
		}
	} else {
		/* a bit rate was configured */

		if (0 != vorbis_encode_init(&encoder->vi,
					    encoder->audio_format.channels,
					    encoder->audio_format.sample_rate, -1.0,
					    encoder->bitrate * 1000, -1.0)) {
158 159
			error.Set(vorbis_encoder_domain,
				  "error initializing vorbis encoder");
160 161 162 163 164 165 166
			vorbis_info_clear(&encoder->vi);
			return false;
		}
	}

	vorbis_analysis_init(&encoder->vd, &encoder->vi);
	vorbis_block_init(&encoder->vd, &encoder->vb);
167
	encoder->stream.Initialize(GenerateOggSerial());
168 169 170 171 172

	return true;
}

static void
173
vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc)
174 175 176
{
	ogg_packet packet, comments, codebooks;

177
	vorbis_analysis_headerout(&encoder->vd, vc,
178 179
				  &packet, &comments, &codebooks);

180 181 182
	encoder->stream.PacketIn(packet);
	encoder->stream.PacketIn(comments);
	encoder->stream.PacketIn(codebooks);
183
}
184

185 186 187 188 189 190 191
static void
vorbis_encoder_send_header(struct vorbis_encoder *encoder)
{
	vorbis_comment vc;

	vorbis_comment_init(&vc);
	vorbis_encoder_headerout(encoder, &vc);
192
	vorbis_comment_clear(&vc);
193 194 195
}

static bool
196
vorbis_encoder_open(Encoder *_encoder,
197
		    AudioFormat &audio_format,
198
		    Error &error)
199 200 201
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

202
	audio_format.format = SampleFormat::FLOAT;
203

204
	encoder->audio_format = audio_format;
205

206
	if (!vorbis_encoder_reinit(encoder, error))
207 208 209
		return false;

	vorbis_encoder_send_header(encoder);
210

211 212 213
	return true;
}

214
static void
215 216
vorbis_encoder_clear(struct vorbis_encoder *encoder)
{
217
	encoder->stream.Deinitialize();
218 219 220 221 222 223
	vorbis_block_clear(&encoder->vb);
	vorbis_dsp_clear(&encoder->vd);
	vorbis_info_clear(&encoder->vi);
}

static void
224
vorbis_encoder_close(Encoder *_encoder)
225 226 227 228 229 230 231 232 233 234
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

	vorbis_encoder_clear(encoder);
}

static void
vorbis_encoder_blockout(struct vorbis_encoder *encoder)
{
	while (vorbis_analysis_blockout(&encoder->vd, &encoder->vb) == 1) {
235
		vorbis_analysis(&encoder->vb, nullptr);
236 237
		vorbis_bitrate_addblock(&encoder->vb);

238
		ogg_packet packet;
239
		while (vorbis_bitrate_flushpacket(&encoder->vd, &packet))
240
			encoder->stream.PacketIn(packet);
241 242 243 244
	}
}

static bool
245
vorbis_encoder_flush(Encoder *_encoder, gcc_unused Error &error)
246 247 248
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

249
	encoder->stream.Flush();
250 251 252 253
	return true;
}

static bool
254
vorbis_encoder_pre_tag(Encoder *_encoder, gcc_unused Error &error)
255 256 257
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

258 259 260
	vorbis_analysis_wrote(&encoder->vd, 0);
	vorbis_encoder_blockout(encoder);

261 262 263 264 265 266 267
	/* reinitialize vorbis_dsp_state and vorbis_block to reset the
	   end-of-stream marker */
	vorbis_block_clear(&encoder->vb);
	vorbis_dsp_clear(&encoder->vd);
	vorbis_analysis_init(&encoder->vd, &encoder->vi);
	vorbis_block_init(&encoder->vd, &encoder->vb);

268
	encoder->stream.Flush();
269 270 271 272
	return true;
}

static void
Max Kellermann's avatar
Max Kellermann committed
273
copy_tag_to_vorbis_comment(vorbis_comment *vc, const Tag *tag)
274
{
Max Kellermann's avatar
Max Kellermann committed
275
	for (unsigned i = 0; i < tag->num_items; i++) {
Max Kellermann's avatar
Max Kellermann committed
276 277 278
		const TagItem &item = *tag->items[i];
		char *name = g_ascii_strup(tag_item_names[item.type], -1);
		vorbis_comment_add_tag(vc, name, item.value);
279
		g_free(name);
280 281 282 283
	}
}

static bool
Max Kellermann's avatar
Max Kellermann committed
284
vorbis_encoder_tag(Encoder *_encoder, const Tag *tag,
285
		   gcc_unused Error &error)
286 287
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
288
	vorbis_comment comment;
289

290
	/* write the vorbis_comment object */
291

292 293
	vorbis_comment_init(&comment);
	copy_tag_to_vorbis_comment(&comment, tag);
294

295
	/* reset ogg_stream_state and begin a new stream */
296

297
	encoder->stream.Reinitialize(GenerateOggSerial());
298 299 300 301

	/* send that vorbis_comment to the ogg_stream_state */

	vorbis_encoder_headerout(encoder, &comment);
302 303
	vorbis_comment_clear(&comment);

304 305 306 307
	return true;
}

static void
308 309
interleaved_to_vorbis_buffer(float **dest, const float *src,
			     unsigned num_frames, unsigned num_channels)
310 311 312
{
	for (unsigned i = 0; i < num_frames; i++)
		for (unsigned j = 0; j < num_channels; j++)
313
			dest[j][i] = *src++;
314 315 316
}

static bool
317
vorbis_encoder_write(Encoder *_encoder,
318
		     const void *data, size_t length,
319
		     gcc_unused Error &error)
320 321 322
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

323
	unsigned num_frames = length / encoder->audio_format.GetFrameSize();
324 325 326

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

327 328 329 330 331
	interleaved_to_vorbis_buffer(vorbis_analysis_buffer(&encoder->vd,
							    num_frames),
				     (const float *)data,
				     num_frames,
				     encoder->audio_format.channels);
332 333 334 335 336 337 338

	vorbis_analysis_wrote(&encoder->vd, num_frames);
	vorbis_encoder_blockout(encoder);
	return true;
}

static size_t
339
vorbis_encoder_read(Encoder *_encoder, void *dest, size_t length)
340 341 342
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

343
	return encoder->stream.PageOut(dest, length);
344 345
}

346
static const char *
347
vorbis_encoder_get_mime_type(gcc_unused Encoder *_encoder)
348
{
349
	return  "audio/ogg";
350 351
}

352
const EncoderPlugin vorbis_encoder_plugin = {
353 354 355 356 357 358 359 360 361 362 363 364
	"vorbis",
	vorbis_encoder_init,
	vorbis_encoder_finish,
	vorbis_encoder_open,
	vorbis_encoder_close,
	vorbis_encoder_pre_tag,
	vorbis_encoder_flush,
	vorbis_encoder_pre_tag,
	vorbis_encoder_tag,
	vorbis_encoder_write,
	vorbis_encoder_read,
	vorbis_encoder_get_mime_type,
365
};