VorbisEncoderPlugin.cxx 9.31 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2012 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 24

extern "C" {
25
#include "encoder_api.h"
26 27
}

28 29 30
#include "encoder_plugin.h"
#include "tag.h"
#include "audio_format.h"
31
#include "mpd_error.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

#include <vorbis/vorbisenc.h>

#include <assert.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "vorbis_encoder"

struct vorbis_encoder {
	/** the base class */
	struct encoder encoder;

	/* configuration */

	float quality;
	int bitrate;

	/* runtime information */

	struct audio_format audio_format;

	vorbis_dsp_state vd;
	vorbis_block vb;
	vorbis_info vi;
56

57
	OggStream stream;
58 59 60 61 62 63 64 65 66 67 68 69
};

static inline GQuark
vorbis_encoder_quark(void)
{
	return g_quark_from_static_string("vorbis_encoder");
}

static bool
vorbis_encoder_configure(struct vorbis_encoder *encoder,
			 const struct config_param *param, GError **error)
{
70 71
	const char *value = config_get_block_string(param, "quality", nullptr);
	if (value != nullptr) {
72 73
		/* a quality was configured (VBR) */

74
		char *endptr;
75 76 77 78 79 80 81 82 83 84 85
		encoder->quality = g_ascii_strtod(value, &endptr);

		if (*endptr != '\0' || encoder->quality < -1.0 ||
		    encoder->quality > 10.0) {
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "quality \"%s\" is not a number in the "
				    "range -1 to 10, line %i",
				    value, param->line);
			return false;
		}

86
		if (config_get_block_string(param, "bitrate", nullptr) != nullptr) {
87 88 89 90 91 92 93 94 95
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "quality and bitrate are "
				    "both defined (line %i)",
				    param->line);
			return false;
		}
	} else {
		/* a bit rate was configured */

96 97
		value = config_get_block_string(param, "bitrate", nullptr);
		if (value == nullptr) {
98 99 100 101 102 103 104 105 106
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "neither bitrate nor quality defined "
				    "at line %i",
				    param->line);
			return false;
		}

		encoder->quality = -2.0;

107 108
		char *endptr;
		encoder->bitrate = g_ascii_strtoll(value, &endptr, 10);
109 110 111 112 113 114 115 116 117 118 119 120 121 122
		if (*endptr != '\0' || encoder->bitrate <= 0) {
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "bitrate at line %i should be a positive integer",
				    param->line);
			return false;
		}
	}

	return true;
}

static struct encoder *
vorbis_encoder_init(const struct config_param *param, GError **error)
{
123
	struct vorbis_encoder *encoder = g_new(struct vorbis_encoder, 1);
124 125 126 127 128 129
	encoder_struct_init(&encoder->encoder, &vorbis_encoder_plugin);

	/* load configuration from "param" */
	if (!vorbis_encoder_configure(encoder, param, error)) {
		/* configuration has failed, roll back and return error */
		g_free(encoder);
130
		return nullptr;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	}

	return &encoder->encoder;
}

static void
vorbis_encoder_finish(struct encoder *_encoder)
{
	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 */
	g_free(encoder);
}

static bool
vorbis_encoder_reinit(struct vorbis_encoder *encoder, GError **error)
{
	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)) {
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "error initializing vorbis vbr");
			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)) {
			g_set_error(error, vorbis_encoder_quark(), 0,
				    "error initializing vorbis encoder");
			vorbis_info_clear(&encoder->vi);
			return false;
		}
	}

	vorbis_analysis_init(&encoder->vd, &encoder->vi);
	vorbis_block_init(&encoder->vd, &encoder->vb);
179
	encoder->stream.Initialize(g_random_int());
180 181 182 183 184

	return true;
}

static void
185
vorbis_encoder_headerout(struct vorbis_encoder *encoder, vorbis_comment *vc)
186 187 188
{
	ogg_packet packet, comments, codebooks;

189
	vorbis_analysis_headerout(&encoder->vd, vc,
190 191
				  &packet, &comments, &codebooks);

192 193 194
	encoder->stream.PacketIn(packet);
	encoder->stream.PacketIn(comments);
	encoder->stream.PacketIn(codebooks);
195
}
196

197 198 199 200 201 202 203
static void
vorbis_encoder_send_header(struct vorbis_encoder *encoder)
{
	vorbis_comment vc;

	vorbis_comment_init(&vc);
	vorbis_encoder_headerout(encoder, &vc);
204
	vorbis_comment_clear(&vc);
205 206 207 208 209 210 211 212 213
}

static bool
vorbis_encoder_open(struct encoder *_encoder,
		    struct audio_format *audio_format,
		    GError **error)
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

214
	audio_format->format = SAMPLE_FORMAT_FLOAT;
215 216 217

	encoder->audio_format = *audio_format;

218
	if (!vorbis_encoder_reinit(encoder, error))
219 220 221
		return false;

	vorbis_encoder_send_header(encoder);
222

223 224 225
	return true;
}

226
static void
227 228
vorbis_encoder_clear(struct vorbis_encoder *encoder)
{
229
	encoder->stream.Deinitialize();
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	vorbis_block_clear(&encoder->vb);
	vorbis_dsp_clear(&encoder->vd);
	vorbis_info_clear(&encoder->vi);
}

static void
vorbis_encoder_close(struct encoder *_encoder)
{
	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) {
247
		vorbis_analysis(&encoder->vb, nullptr);
248 249
		vorbis_bitrate_addblock(&encoder->vb);

250
		ogg_packet packet;
251
		while (vorbis_bitrate_flushpacket(&encoder->vd, &packet))
252
			encoder->stream.PacketIn(packet);
253 254 255 256 257 258 259 260
	}
}

static bool
vorbis_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

261
	encoder->stream.Flush();
262 263 264 265 266 267 268 269
	return true;
}

static bool
vorbis_encoder_pre_tag(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

270 271 272
	vorbis_analysis_wrote(&encoder->vd, 0);
	vorbis_encoder_blockout(encoder);

273 274 275 276 277 278 279
	/* 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);

280
	encoder->stream.Flush();
281 282 283 284
	return true;
}

static void
285
copy_tag_to_vorbis_comment(vorbis_comment *vc, const struct tag *tag)
286
{
Max Kellermann's avatar
Max Kellermann committed
287
	for (unsigned i = 0; i < tag->num_items; i++) {
288 289 290 291
		struct tag_item *item = tag->items[i];
		char *name = g_ascii_strup(tag_item_names[item->type], -1);
		vorbis_comment_add_tag(vc, name, item->value);
		g_free(name);
292 293 294 295 296
	}
}

static bool
vorbis_encoder_tag(struct encoder *_encoder, const struct tag *tag,
297
		   G_GNUC_UNUSED GError **error)
298 299
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;
300
	vorbis_comment comment;
301

302
	/* write the vorbis_comment object */
303

304 305
	vorbis_comment_init(&comment);
	copy_tag_to_vorbis_comment(&comment, tag);
306

307
	/* reset ogg_stream_state and begin a new stream */
308

309
	encoder->stream.Reinitialize(g_random_int());
310 311 312 313

	/* send that vorbis_comment to the ogg_stream_state */

	vorbis_encoder_headerout(encoder, &comment);
314 315
	vorbis_comment_clear(&comment);

316 317 318 319
	return true;
}

static void
320 321
interleaved_to_vorbis_buffer(float **dest, const float *src,
			     unsigned num_frames, unsigned num_channels)
322 323 324
{
	for (unsigned i = 0; i < num_frames; i++)
		for (unsigned j = 0; j < num_channels; j++)
325
			dest[j][i] = *src++;
326 327 328 329 330 331 332 333 334
}

static bool
vorbis_encoder_write(struct encoder *_encoder,
		     const void *data, size_t length,
		     G_GNUC_UNUSED GError **error)
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

335 336
	unsigned num_frames = length
		/ audio_format_frame_size(&encoder->audio_format);
337 338 339

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

340 341 342 343 344
	interleaved_to_vorbis_buffer(vorbis_analysis_buffer(&encoder->vd,
							    num_frames),
				     (const float *)data,
				     num_frames,
				     encoder->audio_format.channels);
345 346 347 348 349 350 351

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

static size_t
352
vorbis_encoder_read(struct encoder *_encoder, void *dest, size_t length)
353 354 355
{
	struct vorbis_encoder *encoder = (struct vorbis_encoder *)_encoder;

356
	return encoder->stream.PageOut(dest, length);
357 358
}

359 360 361
static const char *
vorbis_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
{
362
	return  "audio/ogg";
363 364
}

365
const struct encoder_plugin vorbis_encoder_plugin = {
366 367 368 369 370 371 372 373 374 375 376 377
	"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,
378
};