aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoder/OpusEncoderPlugin.cxx
blob: c967a24a79833f83b74c87d9d928cdd10f86f769 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (C) 2003-2012 The Music Player Daemon Project
 * 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.
 *
 * 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.
 */

#include "config.h"
#include "OpusEncoderPlugin.hxx"

extern "C" {
#include "encoder_api.h"
}

#include "encoder_plugin.h"
#include "audio_format.h"
#include "mpd_error.h"

#include <opus.h>
#include <ogg/ogg.h>

#include <assert.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "opus_encoder"

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

	/* configuration */

	opus_int32 bitrate;
	int complexity;
	int signal;

	/* runtime information */

	struct audio_format audio_format;

	size_t frame_size;

	size_t buffer_frames, buffer_size, buffer_position;
	uint8_t *buffer;

	OpusEncoder *enc;

	unsigned char buffer2[1275 * 3 + 7];

	ogg_stream_state os;

	ogg_int64_t packetno;

	bool flush;
};

gcc_const
static inline GQuark
opus_encoder_quark(void)
{
	return g_quark_from_static_string("opus_encoder");
}

static bool
opus_encoder_configure(struct opus_encoder *encoder,
		       const struct config_param *param, GError **error_r)
{
	const char *value = config_get_block_string(param, "bitrate", "auto");
	if (strcmp(value, "auto") == 0)
		encoder->bitrate = OPUS_AUTO;
	else if (strcmp(value, "max") == 0)
		encoder->bitrate = OPUS_BITRATE_MAX;
	else {
		char *endptr;
		encoder->bitrate = strtoul(value, &endptr, 10);
		if (endptr == value || *endptr != 0 ||
		    encoder->bitrate < 500 || encoder->bitrate > 512000) {
			g_set_error(error_r, opus_encoder_quark(), 0,
				    "Invalid bit rate");
			return false;
		}
	}

	encoder->complexity = config_get_block_unsigned(param, "complexity",
							10);
	if (encoder->complexity > 10) {
		g_set_error(error_r, opus_encoder_quark(), 0,
			    "Invalid complexity");
		return false;
	}

	value = config_get_block_string(param, "signal", "auto");
	if (strcmp(value, "auto") == 0)
		encoder->bitrate = OPUS_AUTO;
	else if (strcmp(value, "voice") == 0)
		encoder->bitrate = OPUS_SIGNAL_VOICE;
	else if (strcmp(value, "music") == 0)
		encoder->bitrate = OPUS_SIGNAL_MUSIC;
	else {
		g_set_error(error_r, opus_encoder_quark(), 0,
			    "Invalid signal");
		return false;
	}

	return true;
}

static struct encoder *
opus_encoder_init(const struct config_param *param, GError **error)
{
	struct opus_encoder *encoder;

	encoder = g_new(struct opus_encoder, 1);
	encoder_struct_init(&encoder->encoder, &opus_encoder_plugin);

	/* load configuration from "param" */
	if (!opus_encoder_configure(encoder, param, error)) {
		/* configuration has failed, roll back and return error */
		g_free(encoder);
		return NULL;
	}

	return &encoder->encoder;
}

static void
opus_encoder_finish(struct encoder *_encoder)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;

	/* the real libopus cleanup was already performed by
	   opus_encoder_close(), so no real work here */
	g_free(encoder);
}

static bool
opus_encoder_open(struct encoder *_encoder,
		  struct audio_format *audio_format,
		  GError **error_r)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;

	/* libopus supports only 48 kHz */
	audio_format->sample_rate = 48000;

	if (audio_format->channels > 2)
		audio_format->channels = 1;

	switch ((enum sample_format)audio_format->format) {
	case SAMPLE_FORMAT_S16:
	case SAMPLE_FORMAT_FLOAT:
		break;

	case SAMPLE_FORMAT_S8:
		audio_format->format = SAMPLE_FORMAT_S16;
		break;

	default:
		audio_format->format = SAMPLE_FORMAT_FLOAT;
		break;
	}

	encoder->audio_format = *audio_format;
	encoder->frame_size = audio_format_frame_size(audio_format);

	int error;
	encoder->enc = opus_encoder_create(audio_format->sample_rate,
					   audio_format->channels,
					   OPUS_APPLICATION_AUDIO,
					   &error);
	if (encoder->enc == nullptr) {
		g_set_error_literal(error_r, opus_encoder_quark(), error,
				    opus_strerror(error));
		return false;
	}

	opus_encoder_ctl(encoder->enc, OPUS_SET_BITRATE(encoder->bitrate));
	opus_encoder_ctl(encoder->enc,
			 OPUS_SET_COMPLEXITY(encoder->complexity));
	opus_encoder_ctl(encoder->enc, OPUS_SET_SIGNAL(encoder->signal));

	encoder->buffer_frames = audio_format->sample_rate / 50;
	encoder->buffer_size = encoder->frame_size * encoder->buffer_frames;
	encoder->buffer_position = 0;
	encoder->buffer = (unsigned char *)g_malloc(encoder->buffer_size);

	ogg_stream_init(&encoder->os, g_random_int());
	encoder->packetno = 0;

	/* set "flush" to true, so the caller gets the full headers on
	   the first read() */
	encoder->flush = true;

	return true;
}

static void
opus_encoder_close(struct encoder *_encoder)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;

	ogg_stream_clear(&encoder->os);
	g_free(encoder->buffer);
	opus_encoder_destroy(encoder->enc);
}

static bool
opus_encoder_do_encode(struct opus_encoder *encoder, bool eos,
		       GError **error_r)
{
	assert(encoder->buffer_position == encoder->buffer_size);

	opus_int32 result =
		encoder->audio_format.format == SAMPLE_FORMAT_S16
		? opus_encode(encoder->enc,
			      (const opus_int16 *)encoder->buffer,
			      encoder->buffer_frames,
			      encoder->buffer2,
			      sizeof(encoder->buffer2))
		: opus_encode_float(encoder->enc,
				    (const float *)encoder->buffer,
				    encoder->buffer_frames,
				    encoder->buffer2,
				    sizeof(encoder->buffer2));
	if (result < 0) {
		g_set_error_literal(error_r, opus_encoder_quark(), 0,
				    "Opus encoder error");
		return false;
	}

	ogg_packet packet;
	packet.packet = encoder->buffer2;
	packet.bytes = result;
	packet.b_o_s = false;
	packet.e_o_s = eos;
	packet.granulepos = 0; // TODO
	packet.packetno = encoder->packetno++;
	ogg_stream_packetin(&encoder->os, &packet);

	encoder->buffer_position = 0;

	return true;
}

static bool
opus_encoder_end(struct encoder *_encoder, GError **error_r)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;

	encoder->flush = true;

	memset(encoder->buffer + encoder->buffer_position, 0,
	       encoder->buffer_size - encoder->buffer_position);
	encoder->buffer_position = encoder->buffer_size;

	return opus_encoder_do_encode(encoder, true, error_r);
}

static bool
opus_encoder_flush(struct encoder *_encoder, G_GNUC_UNUSED GError **error)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;

	encoder->flush = true;
	return true;
}

static bool
opus_encoder_write(struct encoder *_encoder,
		   const void *_data, size_t length,
		   GError **error_r)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
	const uint8_t *data = (const uint8_t *)_data;

	while (length > 0) {
		size_t nbytes =
			encoder->buffer_size - encoder->buffer_position;
		if (nbytes > length)
			nbytes = length;

		memcpy(encoder->buffer + encoder->buffer_position,
		       data, nbytes);
		data += nbytes;
		length -= nbytes;
		encoder->buffer_position += nbytes;

		if (encoder->buffer_position == encoder->buffer_size &&
		    !opus_encoder_do_encode(encoder, false, error_r))
			return false;
	}

	return true;
}

static void
opus_encoder_generate_head(struct opus_encoder *encoder)
{
	unsigned char header[19];
	memcpy(header, "OpusHead", 8);
	header[8] = 1;
	header[9] = encoder->audio_format.channels;
	header[10] = 0;
	header[11] = 0;
	*(uint32_t *)(header + 12) =
		GUINT32_TO_LE(encoder->audio_format.sample_rate);
	header[16] = 0;
	header[17] = 0;
	header[18] = 0;

	ogg_packet packet;
	packet.packet = header;
	packet.bytes = 19;
	packet.b_o_s = true;
	packet.e_o_s = false;
	packet.granulepos = 0;
	packet.packetno = encoder->packetno++;
	ogg_stream_packetin(&encoder->os, &packet);

	encoder->flush = true;
}

static void
opus_encoder_generate_tags(struct opus_encoder *encoder)
{
	const char *version = opus_get_version_string();
	size_t version_length = strlen(version);

	size_t comments_size = 8 + 4 + version_length + 4;
	unsigned char *comments = (unsigned char *)g_malloc(comments_size);
	memcpy(comments, "OpusTags", 8);
	*(uint32_t *)(comments + 8) = GUINT32_TO_LE(version_length);
	memcpy(comments + 12, version, version_length);
	*(uint32_t *)(comments + 12 + version_length) = GUINT32_TO_LE(0);

	ogg_packet packet;
	packet.packet = comments;
	packet.bytes = comments_size;
	packet.b_o_s = false;
	packet.e_o_s = false;
	packet.granulepos = 0;
	packet.packetno = encoder->packetno++;
	ogg_stream_packetin(&encoder->os, &packet);

	g_free(comments);

	encoder->flush = true;
}

static size_t
opus_encoder_read(struct encoder *_encoder, void *_dest, size_t length)
{
	struct opus_encoder *encoder = (struct opus_encoder *)_encoder;
	unsigned char *dest = (unsigned char *)_dest;

	if (encoder->packetno == 0)
		opus_encoder_generate_head(encoder);
	else if (encoder->packetno == 1)
		opus_encoder_generate_tags(encoder);

	ogg_page page;
	int result;
	if (encoder->flush) {
		encoder->flush = false;
		result = ogg_stream_flush(&encoder->os, &page);
	} else
		result = ogg_stream_pageout(&encoder->os, &page);

	if (result == 0)
		return 0;

	assert(page.header_len > 0 || page.body_len > 0);

	size_t nbytes = (size_t)page.header_len + (size_t)page.body_len;

	if (nbytes > length)
		/* XXX better error handling */
		MPD_ERROR("buffer too small");

	memcpy(dest, page.header, page.header_len);
	memcpy(dest + page.header_len, page.body, page.body_len);

	return nbytes;
}

static const char *
opus_encoder_get_mime_type(G_GNUC_UNUSED struct encoder *_encoder)
{
	return  "audio/ogg";
}

const struct encoder_plugin opus_encoder_plugin = {
	"opus",
	opus_encoder_init,
	opus_encoder_finish,
	opus_encoder_open,
	opus_encoder_close,
	opus_encoder_end,
	opus_encoder_flush,
	nullptr,
	nullptr,
	opus_encoder_write,
	opus_encoder_read,
	opus_encoder_get_mime_type,
};