aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder/OpusDecoderPlugin.cxx
blob: c757d66ef57d0f352ac3f21814769b657ca4f177 (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
/*
 * 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" /* must be first for large file support */
#include "OpusDecoderPlugin.h"
#include "OpusHead.hxx"
#include "OpusTags.hxx"
#include "OggUtil.hxx"
#include "OggFind.hxx"
#include "OggSyncState.hxx"
#include "DecoderAPI.hxx"
#include "OggCodec.hxx"
#include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx"
#include "tag/TagBuilder.hxx"
#include "InputStream.hxx"
#include "util/Error.hxx"

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

#include <glib.h>

#include <stdio.h>
#include <string.h>

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "opus"

static const opus_int32 opus_sample_rate = 48000;

gcc_pure
static bool
IsOpusHead(const ogg_packet &packet)
{
	return packet.bytes >= 8 && memcmp(packet.packet, "OpusHead", 8) == 0;
}

gcc_pure
static bool
IsOpusTags(const ogg_packet &packet)
{
	return packet.bytes >= 8 && memcmp(packet.packet, "OpusTags", 8) == 0;
}

static bool
mpd_opus_init(gcc_unused const config_param &param)
{
	g_debug("%s", opus_get_version_string());

	return true;
}

class MPDOpusDecoder {
	struct decoder *decoder;
	struct input_stream *input_stream;

	ogg_stream_state os;

	OpusDecoder *opus_decoder;
	opus_int16 *output_buffer;
	unsigned output_size;

	bool os_initialized;
	bool found_opus;

	int opus_serialno;

	size_t frame_size;

public:
	MPDOpusDecoder(struct decoder *_decoder,
		       struct input_stream *_input_stream)
		:decoder(_decoder), input_stream(_input_stream),
		 opus_decoder(nullptr),
		 output_buffer(nullptr), output_size(0),
		 os_initialized(false), found_opus(false) {}
	~MPDOpusDecoder();

	bool ReadFirstPage(OggSyncState &oy);
	bool ReadNextPage(OggSyncState &oy);

	enum decoder_command HandlePackets();
	enum decoder_command HandlePacket(const ogg_packet &packet);
	enum decoder_command HandleBOS(const ogg_packet &packet);
	enum decoder_command HandleTags(const ogg_packet &packet);
	enum decoder_command HandleAudio(const ogg_packet &packet);
};

MPDOpusDecoder::~MPDOpusDecoder()
{
	g_free(output_buffer);

	if (opus_decoder != nullptr)
		opus_decoder_destroy(opus_decoder);

	if (os_initialized)
		ogg_stream_clear(&os);
}

inline bool
MPDOpusDecoder::ReadFirstPage(OggSyncState &oy)
{
	assert(!os_initialized);

	if (!oy.ExpectFirstPage(os))
		return false;

	os_initialized = true;
	return true;
}

inline bool
MPDOpusDecoder::ReadNextPage(OggSyncState &oy)
{
	assert(os_initialized);

	ogg_page page;
	if (!oy.ExpectPage(page))
		return false;

	const auto page_serialno = ogg_page_serialno(&page);
	if (page_serialno != os.serialno)
		ogg_stream_reset_serialno(&os, page_serialno);

	ogg_stream_pagein(&os, &page);
	return true;
}

inline enum decoder_command
MPDOpusDecoder::HandlePackets()
{
	ogg_packet packet;
	while (ogg_stream_packetout(&os, &packet) == 1) {
		enum decoder_command cmd = HandlePacket(packet);
		if (cmd != DECODE_COMMAND_NONE)
			return cmd;
	}

	return DECODE_COMMAND_NONE;
}

inline enum decoder_command
MPDOpusDecoder::HandlePacket(const ogg_packet &packet)
{
	if (packet.e_o_s)
		return DECODE_COMMAND_STOP;

	if (packet.b_o_s)
		return HandleBOS(packet);
	else if (!found_opus)
		return DECODE_COMMAND_STOP;

	if (IsOpusTags(packet))
		return HandleTags(packet);

	return HandleAudio(packet);
}

inline enum decoder_command
MPDOpusDecoder::HandleBOS(const ogg_packet &packet)
{
	assert(packet.b_o_s);

	if (found_opus || !IsOpusHead(packet))
		return DECODE_COMMAND_STOP;

	unsigned channels;
	if (!ScanOpusHeader(packet.packet, packet.bytes, channels) ||
	    !audio_valid_channel_count(channels))
		return DECODE_COMMAND_STOP;

	assert(opus_decoder == nullptr);
	assert(output_buffer == nullptr);

	opus_serialno = os.serialno;
	found_opus = true;

	/* TODO: parse attributes from the OpusHead (sample rate,
	   channels, ...) */

	int opus_error;
	opus_decoder = opus_decoder_create(opus_sample_rate, channels,
					   &opus_error);
	if (opus_decoder == nullptr) {
		g_warning("libopus error: %s",
			  opus_strerror(opus_error));
		return DECODE_COMMAND_STOP;
	}

	const AudioFormat audio_format(opus_sample_rate,
				       SampleFormat::S16, channels);
	decoder_initialized(decoder, audio_format, false, -1);
	frame_size = audio_format.GetFrameSize();

	/* allocate an output buffer for 16 bit PCM samples big enough
	   to hold a quarter second, larger than 120ms required by
	   libopus */
	output_size = audio_format.sample_rate / 4;
	output_buffer = (opus_int16 *)
		g_malloc(sizeof(*output_buffer) * output_size *
			 audio_format.channels);

	return decoder_get_command(decoder);
}

inline enum decoder_command
MPDOpusDecoder::HandleTags(const ogg_packet &packet)
{
	TagBuilder tag_builder;

	enum decoder_command cmd;
	if (ScanOpusTags(packet.packet, packet.bytes,
			 &add_tag_handler, &tag_builder) &&
	    !tag_builder.IsEmpty()) {
		Tag tag;
		tag_builder.Commit(tag);
		cmd = decoder_tag(decoder, input_stream, std::move(tag));
	} else
		cmd = decoder_get_command(decoder);

	return cmd;
}

inline enum decoder_command
MPDOpusDecoder::HandleAudio(const ogg_packet &packet)
{
	assert(opus_decoder != nullptr);

	int nframes = opus_decode(opus_decoder,
				  (const unsigned char*)packet.packet,
				  packet.bytes,
				  output_buffer, output_size,
				  0);
	if (nframes < 0) {
		g_warning("%s", opus_strerror(nframes));
		return DECODE_COMMAND_STOP;
	}

	if (nframes > 0) {
		const size_t nbytes = nframes * frame_size;
		enum decoder_command cmd =
			decoder_data(decoder, input_stream,
				     output_buffer, nbytes,
				     0);
		if (cmd != DECODE_COMMAND_NONE)
			return cmd;
	}

	return DECODE_COMMAND_NONE;
}

static void
mpd_opus_stream_decode(struct decoder *decoder,
		       struct input_stream *input_stream)
{
	if (ogg_codec_detect(decoder, input_stream) != OGG_CODEC_OPUS)
		return;

	/* rewind the stream, because ogg_codec_detect() has
	   moved it */
	input_stream->LockSeek(0, SEEK_SET, IgnoreError());

	MPDOpusDecoder d(decoder, input_stream);
	OggSyncState oy(*input_stream, decoder);

	if (!d.ReadFirstPage(oy))
		return;

	while (true) {
		enum decoder_command cmd = d.HandlePackets();
		if (cmd != DECODE_COMMAND_NONE)
			break;

		if (!d.ReadNextPage(oy))
			break;

	}
}

static bool
SeekFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet,
	    input_stream *is)
{
	if (is->size > 0 && is->size - is->offset < 65536)
		return OggFindEOS(oy, os, packet);

	if (!is->CheapSeeking())
		return false;

	oy.Reset();

	Error error;
	return is->LockSeek(-65536, SEEK_END, error) &&
		oy.ExpectPageSeekIn(os) &&
		OggFindEOS(oy, os, packet);
}

static bool
mpd_opus_scan_stream(struct input_stream *is,
		     const struct tag_handler *handler, void *handler_ctx)
{
	OggSyncState oy(*is);

	ogg_stream_state os;
	if (!oy.ExpectFirstPage(os))
		return false;

	/* read at most two more pages */
	unsigned remaining_pages = 2;

	bool result = false;

	ogg_packet packet;
	while (true) {
		int r = ogg_stream_packetout(&os, &packet);
		if (r < 0) {
			result = false;
			break;
		}

		if (r == 0) {
			if (remaining_pages-- == 0)
				break;

			if (!oy.ExpectPageIn(os)) {
				result = false;
				break;
			}

			continue;
		}

		if (packet.b_o_s) {
			if (!IsOpusHead(packet))
				break;

			unsigned channels;
			if (!ScanOpusHeader(packet.packet, packet.bytes, channels) ||
			    !audio_valid_channel_count(channels)) {
				result = false;
				break;
			}

			result = true;
		} else if (!result)
			break;
		else if (IsOpusTags(packet)) {
			if (!ScanOpusTags(packet.packet, packet.bytes,
					  handler, handler_ctx))
				result = false;

			break;
		}
	}

	if (packet.e_o_s || SeekFindEOS(oy, os, packet, is))
		tag_handler_invoke_duration(handler, handler_ctx,
					    packet.granulepos / opus_sample_rate);

	ogg_stream_clear(&os);

	return result;
}

static const char *const opus_suffixes[] = {
	"opus",
	"ogg",
	"oga",
	nullptr
};

static const char *const opus_mime_types[] = {
	"audio/opus",
	nullptr
};

const struct decoder_plugin opus_decoder_plugin = {
	"opus",
	mpd_opus_init,
	nullptr,
	mpd_opus_stream_decode,
	nullptr,
	nullptr,
	mpd_opus_scan_stream,
	nullptr,
	opus_suffixes,
	opus_mime_types,
};