OpusEncoderPlugin.cxx 9.1 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "OpusEncoderPlugin.hxx"
21
#include "OggEncoder.hxx"
22
#include "AudioFormat.hxx"
23
#include "config/Domain.hxx"
24
#include "util/Alloc.hxx"
25
#include "system/ByteOrder.hxx"
26
#include "util/StringUtil.hxx"
27 28 29 30

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

31 32
#include <stdexcept>

33
#include <assert.h>
34
#include <stdlib.h>
35

36
namespace {
37

38
class OpusEncoder final : public OggEncoder {
39
	const AudioFormat audio_format;
40

41
	const size_t frame_size;
42

43 44 45
	const size_t buffer_frames, buffer_size;
	size_t buffer_position = 0;
	uint8_t *const buffer;
46

47
	::OpusEncoder *const enc;
48 49 50

	unsigned char buffer2[1275 * 3 + 7];

51 52
	int lookahead;

53
	ogg_int64_t packetno = 0;
54

55
	ogg_int64_t granulepos = 0;
56

57
public:
58
	OpusEncoder(AudioFormat &_audio_format, ::OpusEncoder *_enc, bool _chaining);
59
	~OpusEncoder() override;
60

61
	/* virtual methods from class Encoder */
62 63
	void End() override;
	void Write(const void *data, size_t length) override;
64

65 66
	void PreTag() override;
	void SendTag(const Tag &tag) override;
67

68
private:
69 70
	void DoEncode(bool eos);
	void WriteSilence(unsigned fill_frames);
71

72
	void GenerateHeaders(const Tag *tag);
73
	void GenerateHead();
74
	void GenerateTags(const Tag *tag);
75 76
};

77
class PreparedOpusEncoder final : public PreparedEncoder {
78 79 80
	opus_int32 bitrate;
	int complexity;
	int signal;
81
	const bool chaining;
82

83
public:
84
	PreparedOpusEncoder(const ConfigBlock &block);
85 86

	/* virtual methods from class PreparedEncoder */
87
	Encoder *Open(AudioFormat &audio_format) override;
88 89 90 91

	const char *GetMimeType() const override {
		return "audio/ogg";
	}
92 93
};

94
PreparedOpusEncoder::PreparedOpusEncoder(const ConfigBlock &block)
95
	:chaining(block.GetBlockValue("opustags", false))
96
{
97
	const char *value = block.GetBlockValue("bitrate", "auto");
98
	if (strcmp(value, "auto") == 0)
99
		bitrate = OPUS_AUTO;
100
	else if (strcmp(value, "max") == 0)
101
		bitrate = OPUS_BITRATE_MAX;
102 103
	else {
		char *endptr;
104
		bitrate = strtoul(value, &endptr, 10);
105
		if (endptr == value || *endptr != 0 ||
106 107
		    bitrate < 500 || bitrate > 512000)
			throw std::runtime_error("Invalid bit rate");
108 109
	}

110
	complexity = block.GetBlockValue("complexity", 10u);
111 112
	if (complexity > 10)
		throw std::runtime_error("Invalid complexity");
113

114
	value = block.GetBlockValue("signal", "auto");
115
	if (strcmp(value, "auto") == 0)
116
		signal = OPUS_AUTO;
117
	else if (strcmp(value, "voice") == 0)
118
		signal = OPUS_SIGNAL_VOICE;
119
	else if (strcmp(value, "music") == 0)
120
		signal = OPUS_SIGNAL_MUSIC;
121 122
	else
		throw std::runtime_error("Invalid signal");
123 124
}

125
static PreparedEncoder *
126
opus_encoder_init(const ConfigBlock &block)
127
{
128
	return new PreparedOpusEncoder(block);
129 130
}

131 132
OpusEncoder::OpusEncoder(AudioFormat &_audio_format, ::OpusEncoder *_enc, bool _chaining)
	:OggEncoder(_chaining),
133 134 135 136 137 138 139 140
	 audio_format(_audio_format),
	 frame_size(_audio_format.GetFrameSize()),
	 buffer_frames(_audio_format.sample_rate / 50),
	 buffer_size(frame_size * buffer_frames),
	 buffer((unsigned char *)xalloc(buffer_size)),
	 enc(_enc)
{
	opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&lookahead));
141
	GenerateHeaders(nullptr);
142 143 144
}

Encoder *
145
PreparedOpusEncoder::Open(AudioFormat &audio_format)
146 147
{
	/* libopus supports only 48 kHz */
148
	audio_format.sample_rate = 48000;
149

150 151
	if (audio_format.channels > 2)
		audio_format.channels = 1;
152

153
	switch (audio_format.format) {
154 155
	case SampleFormat::S16:
	case SampleFormat::FLOAT:
156 157
		break;

158
	case SampleFormat::S8:
159
		audio_format.format = SampleFormat::S16;
160 161 162
		break;

	default:
163
		audio_format.format = SampleFormat::FLOAT;
164 165 166
		break;
	}

167
	int error_code;
168 169 170 171
	auto *enc = opus_encoder_create(audio_format.sample_rate,
					audio_format.channels,
					OPUS_APPLICATION_AUDIO,
					&error_code);
172 173
	if (enc == nullptr)
		throw std::runtime_error(opus_strerror(error_code));
174

175 176 177
	opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
	opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
	opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal));
178

179
	return new OpusEncoder(audio_format, enc, chaining);
180 181
}

182
OpusEncoder::~OpusEncoder()
183 184 185 186 187
{
	free(buffer);
	opus_encoder_destroy(enc);
}

188 189
void
OpusEncoder::DoEncode(bool eos)
190
{
191
	assert(buffer_position == buffer_size || eos);
192 193

	opus_int32 result =
194 195
		audio_format.format == SampleFormat::S16
		? opus_encode(enc,
196 197 198 199
		              (const opus_int16 *)buffer,
		              buffer_frames,
		              buffer2,
		              sizeof(buffer2))
200
		: opus_encode_float(enc,
201 202 203 204
		                    (const float *)buffer,
		                    buffer_frames,
		                    buffer2,
		                    sizeof(buffer2));
205 206
	if (result < 0)
		throw std::runtime_error("Opus encoder error");
207

208
	granulepos += buffer_position / frame_size;
209

210
	ogg_packet packet;
211
	packet.packet = buffer2;
212 213 214
	packet.bytes = result;
	packet.b_o_s = false;
	packet.e_o_s = eos;
215 216 217
	packet.granulepos = granulepos;
	packet.packetno = packetno++;
	stream.PacketIn(packet);
218

219
	buffer_position = 0;
220 221
}

222 223
void
OpusEncoder::End()
224
{
225 226
	memset(buffer + buffer_position, 0,
	       buffer_size - buffer_position);
227
	DoEncode(true);
228
	Flush();
229 230
}

231 232
void
OpusEncoder::WriteSilence(unsigned fill_frames)
233 234
{
	size_t fill_bytes = fill_frames * frame_size;
235 236

	while (fill_bytes > 0) {
237
		size_t nbytes = buffer_size - buffer_position;
238 239 240
		if (nbytes > fill_bytes)
			nbytes = fill_bytes;

241 242
		memset(buffer + buffer_position, 0, nbytes);
		buffer_position += nbytes;
243 244
		fill_bytes -= nbytes;

245 246
		if (buffer_position == buffer_size)
			DoEncode(false);
247 248 249
	}
}

250 251
void
OpusEncoder::Write(const void *_data, size_t length)
252 253 254
{
	const uint8_t *data = (const uint8_t *)_data;

255
	if (lookahead > 0) {
256 257 258
		/* generate some silence at the beginning of the
		   stream */

259
		assert(buffer_position == 0);
260

261
		WriteSilence(lookahead);
262
		lookahead = 0;
263 264
	}

265
	while (length > 0) {
266
		size_t nbytes = buffer_size - buffer_position;
267 268 269
		if (nbytes > length)
			nbytes = length;

270
		memcpy(buffer + buffer_position, data, nbytes);
271 272
		data += nbytes;
		length -= nbytes;
273
		buffer_position += nbytes;
274

275 276
		if (buffer_position == buffer_size)
			DoEncode(false);
277 278 279
	}
}

280 281 282 283 284 285 286
void
OpusEncoder::GenerateHeaders(const Tag *tag)
{
	GenerateHead();
	GenerateTags(tag);
}

287 288
void
OpusEncoder::GenerateHead()
289 290 291 292
{
	unsigned char header[19];
	memcpy(header, "OpusHead", 8);
	header[8] = 1;
293 294 295
	header[9] = audio_format.channels;
	*(uint16_t *)(header + 10) = ToLE16(lookahead);
	*(uint32_t *)(header + 12) = ToLE32(audio_format.sample_rate);
296 297 298 299 300 301
	header[16] = 0;
	header[17] = 0;
	header[18] = 0;

	ogg_packet packet;
	packet.packet = header;
302
	packet.bytes = sizeof(header);
303 304 305
	packet.b_o_s = true;
	packet.e_o_s = false;
	packet.granulepos = 0;
306 307
	packet.packetno = packetno++;
	stream.PacketIn(packet);
308
	// flush not needed because libogg autoflushes on b_o_s flag
309 310
}

311
void
312
OpusEncoder::GenerateTags(const Tag *tag)
313 314 315 316
{
	const char *version = opus_get_version_string();
	size_t version_length = strlen(version);

317
	// len("OpusTags") + 4 byte version length + len(version) + 4 byte tag count
318
	size_t comments_size = 8 + 4 + version_length + 4;
319 320 321 322 323 324 325 326 327
	uint32_t tag_count = 0;
	if (tag) {
		for (const auto &item: *tag) {
			++tag_count;
			// 4 byte length + len(tagname) + len('=') + len(value)
			comments_size += 4 + strlen(tag_item_names[item.type]) + 1 + strlen(item.value);
		}
	}

328
	unsigned char *comments = (unsigned char *)xalloc(comments_size);
329 330
	unsigned char *p = comments;

331
	memcpy(comments, "OpusTags", 8);
332
	*(uint32_t *)(comments + 8) = ToLE32(version_length);
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
	p += 12;

	memcpy(p, version, version_length);
	p += version_length;

	tag_count = ToLE32(tag_count);
	memcpy(p, &tag_count, 4);
	p += 4;

	if (tag) {
		for (const auto &item: *tag) {
			size_t tag_name_len = strlen(tag_item_names[item.type]);
			size_t tag_val_len = strlen(item.value);
			uint32_t tag_len_le = ToLE32(tag_name_len + 1 + tag_val_len);

			memcpy(p, &tag_len_le, 4);
			p += 4;

			ToUpperASCII((char *)p, tag_item_names[item.type], tag_name_len + 1);
			p += tag_name_len;

			*p++ = '=';

			memcpy(p, item.value, tag_val_len);
			p += tag_val_len;
		}
	}
	assert(comments + comments_size == p);
361 362 363 364 365 366 367

	ogg_packet packet;
	packet.packet = comments;
	packet.bytes = comments_size;
	packet.b_o_s = false;
	packet.e_o_s = false;
	packet.granulepos = 0;
368 369
	packet.packetno = packetno++;
	stream.PacketIn(packet);
370
	Flush();
371

372
	free(comments);
373 374
}

375 376
void
OpusEncoder::PreTag()
377
{
378 379 380 381 382
	End();
	packetno = 0;
	granulepos = 0; // not really required, but useful to prevent wraparound
	opus_encoder_ctl(enc, OPUS_RESET_STATE);
}
383

384 385 386 387 388 389
void
OpusEncoder::SendTag(const Tag &tag)
{
	stream.Reinitialize(GenerateOggSerial());
	opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&lookahead));
	GenerateHeaders(&tag);
390
}
391

392 393
}

394
const EncoderPlugin opus_encoder_plugin = {
395 396 397
	"opus",
	opus_encoder_init,
};