2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
#include "config.h" /* must be first for large file support */
21
#include "OpusDecoderPlugin.h"
22
#include "OpusDomain.hxx"
23
#include "OpusHead.hxx"
24
#include "OpusTags.hxx"
25
#include "OggUtil.hxx"
26
#include "OggFind.hxx"
27
#include "OggSyncState.hxx"
28
#include "DecoderAPI.hxx"
29
#include "OggCodec.hxx"
30
#include "CheckAudioFormat.hxx"
31
#include "tag/TagHandler.hxx"
32
#include "tag/TagBuilder.hxx"
33
#include "InputStream.hxx"
34
#include "util/Error.hxx"
44
static constexpr opus_int32 opus_sample_rate = 48000;
48
IsOpusHead(const ogg_packet &packet)
50
return packet.bytes >= 8 && memcmp(packet.packet, "OpusHead", 8) == 0;
55
IsOpusTags(const ogg_packet &packet)
57
return packet.bytes >= 8 && memcmp(packet.packet, "OpusTags", 8) == 0;
61
mpd_opus_init(gcc_unused const config_param ¶m)
63
LogDebug(opus_domain, opus_get_version_string());
68
class MPDOpusDecoder {
70
InputStream &input_stream;
74
OpusDecoder *opus_decoder;
75
opus_int16 *output_buffer;
83
ogg_int64_t eos_granulepos;
88
MPDOpusDecoder(Decoder &_decoder,
89
InputStream &_input_stream)
90
:decoder(_decoder), input_stream(_input_stream),
91
opus_decoder(nullptr),
92
output_buffer(nullptr), output_size(0),
93
os_initialized(false), found_opus(false) {}
96
bool ReadFirstPage(OggSyncState &oy);
97
bool ReadNextPage(OggSyncState &oy);
99
DecoderCommand HandlePackets();
100
DecoderCommand HandlePacket(const ogg_packet &packet);
101
DecoderCommand HandleBOS(const ogg_packet &packet);
102
DecoderCommand HandleTags(const ogg_packet &packet);
103
DecoderCommand HandleAudio(const ogg_packet &packet);
105
bool Seek(OggSyncState &oy, double where);
108
MPDOpusDecoder::~MPDOpusDecoder()
110
g_free(output_buffer);
112
if (opus_decoder != nullptr)
113
opus_decoder_destroy(opus_decoder);
116
ogg_stream_clear(&os);
120
MPDOpusDecoder::ReadFirstPage(OggSyncState &oy)
122
assert(!os_initialized);
124
if (!oy.ExpectFirstPage(os))
127
os_initialized = true;
132
MPDOpusDecoder::ReadNextPage(OggSyncState &oy)
134
assert(os_initialized);
137
if (!oy.ExpectPage(page))
140
const auto page_serialno = ogg_page_serialno(&page);
141
if (page_serialno != os.serialno)
142
ogg_stream_reset_serialno(&os, page_serialno);
144
ogg_stream_pagein(&os, &page);
148
inline DecoderCommand
149
MPDOpusDecoder::HandlePackets()
152
while (ogg_stream_packetout(&os, &packet) == 1) {
153
auto cmd = HandlePacket(packet);
154
if (cmd != DecoderCommand::NONE)
158
return DecoderCommand::NONE;
161
inline DecoderCommand
162
MPDOpusDecoder::HandlePacket(const ogg_packet &packet)
165
return DecoderCommand::STOP;
168
return HandleBOS(packet);
169
else if (!found_opus)
170
return DecoderCommand::STOP;
172
if (IsOpusTags(packet))
173
return HandleTags(packet);
175
return HandleAudio(packet);
179
* Load the end-of-stream packet and restore the previous file
183
LoadEOSPacket(InputStream &is, Decoder *decoder, int serialno,
186
if (!is.CheapSeeking())
187
/* we do this for local files only, because seeking
188
around remote files is expensive and not worth the
192
const auto old_offset = is.offset;
196
/* create temporary Ogg objects for seeking and parsing the
198
OggSyncState oy(is, decoder);
200
ogg_stream_init(&os, serialno);
202
bool result = OggSeekFindEOS(oy, os, packet, is);
203
ogg_stream_clear(&os);
205
/* restore the previous file position */
206
is.Seek(old_offset, SEEK_SET, IgnoreError());
212
* Load the end-of-stream granulepos and restore the previous file
215
* @return -1 on error
219
LoadEOSGranulePos(InputStream &is, Decoder *decoder, int serialno)
222
if (!LoadEOSPacket(is, decoder, serialno, packet))
225
return packet.granulepos;
228
inline DecoderCommand
229
MPDOpusDecoder::HandleBOS(const ogg_packet &packet)
231
assert(packet.b_o_s);
233
if (found_opus || !IsOpusHead(packet))
234
return DecoderCommand::STOP;
237
if (!ScanOpusHeader(packet.packet, packet.bytes, channels) ||
238
!audio_valid_channel_count(channels))
239
return DecoderCommand::STOP;
241
assert(opus_decoder == nullptr);
242
assert(output_buffer == nullptr);
244
opus_serialno = os.serialno;
247
/* TODO: parse attributes from the OpusHead (sample rate,
251
opus_decoder = opus_decoder_create(opus_sample_rate, channels,
253
if (opus_decoder == nullptr) {
254
FormatError(opus_domain, "libopus error: %s",
255
opus_strerror(opus_error));
256
return DecoderCommand::STOP;
259
eos_granulepos = LoadEOSGranulePos(input_stream, &decoder,
261
const double duration = eos_granulepos >= 0
262
? double(eos_granulepos) / opus_sample_rate
265
const AudioFormat audio_format(opus_sample_rate,
266
SampleFormat::S16, channels);
267
decoder_initialized(decoder, audio_format,
268
eos_granulepos > 0, duration);
269
frame_size = audio_format.GetFrameSize();
271
/* allocate an output buffer for 16 bit PCM samples big enough
272
to hold a quarter second, larger than 120ms required by
274
output_size = audio_format.sample_rate / 4;
275
output_buffer = (opus_int16 *)
276
g_malloc(sizeof(*output_buffer) * output_size *
277
audio_format.channels);
279
return decoder_get_command(decoder);
282
inline DecoderCommand
283
MPDOpusDecoder::HandleTags(const ogg_packet &packet)
288
TagBuilder tag_builder;
291
if (ScanOpusTags(packet.packet, packet.bytes,
293
&add_tag_handler, &tag_builder) &&
294
!tag_builder.IsEmpty()) {
295
decoder_replay_gain(decoder, &rgi);
298
tag_builder.Commit(tag);
299
cmd = decoder_tag(decoder, input_stream, std::move(tag));
301
cmd = decoder_get_command(decoder);
306
inline DecoderCommand
307
MPDOpusDecoder::HandleAudio(const ogg_packet &packet)
309
assert(opus_decoder != nullptr);
311
int nframes = opus_decode(opus_decoder,
312
(const unsigned char*)packet.packet,
314
output_buffer, output_size,
317
LogError(opus_domain, opus_strerror(nframes));
318
return DecoderCommand::STOP;
322
const size_t nbytes = nframes * frame_size;
323
auto cmd = decoder_data(decoder, input_stream,
324
output_buffer, nbytes,
326
if (cmd != DecoderCommand::NONE)
329
if (packet.granulepos > 0)
330
decoder_timestamp(decoder,
331
double(packet.granulepos)
335
return DecoderCommand::NONE;
339
MPDOpusDecoder::Seek(OggSyncState &oy, double where_s)
341
assert(eos_granulepos > 0);
342
assert(input_stream.seekable);
343
assert(input_stream.size > 0);
344
assert(input_stream.offset >= 0);
346
const ogg_int64_t where_granulepos(where_s * opus_sample_rate);
348
/* interpolate the file offset where we expect to find the
349
given granule position */
350
/* TODO: implement binary search */
351
InputStream::offset_type offset(where_granulepos * input_stream.size
354
if (!OggSeekPageAtOffset(oy, os, input_stream, offset, SEEK_SET))
357
decoder_timestamp(decoder, where_s);
362
mpd_opus_stream_decode(Decoder &decoder,
363
InputStream &input_stream)
365
if (ogg_codec_detect(&decoder, input_stream) != OGG_CODEC_OPUS)
368
/* rewind the stream, because ogg_codec_detect() has
370
input_stream.LockRewind(IgnoreError());
372
MPDOpusDecoder d(decoder, input_stream);
373
OggSyncState oy(input_stream, &decoder);
375
if (!d.ReadFirstPage(oy))
379
auto cmd = d.HandlePackets();
380
if (cmd == DecoderCommand::SEEK) {
381
if (d.Seek(oy, decoder_seek_where(decoder)))
382
decoder_command_finished(decoder);
384
decoder_seek_error(decoder);
389
if (cmd != DecoderCommand::NONE)
392
if (!d.ReadNextPage(oy))
398
mpd_opus_scan_stream(InputStream &is,
399
const struct tag_handler *handler, void *handler_ctx)
404
if (!oy.ExpectFirstPage(os))
407
/* read at most two more pages */
408
unsigned remaining_pages = 2;
414
int r = ogg_stream_packetout(&os, &packet);
421
if (remaining_pages-- == 0)
424
if (!oy.ExpectPageIn(os)) {
433
if (!IsOpusHead(packet))
437
if (!ScanOpusHeader(packet.packet, packet.bytes, channels) ||
438
!audio_valid_channel_count(channels)) {
446
else if (IsOpusTags(packet)) {
447
if (!ScanOpusTags(packet.packet, packet.bytes,
449
handler, handler_ctx))
456
if (packet.e_o_s || OggSeekFindEOS(oy, os, packet, is))
457
tag_handler_invoke_duration(handler, handler_ctx,
458
packet.granulepos / opus_sample_rate);
460
ogg_stream_clear(&os);
465
static const char *const opus_suffixes[] = {
472
static const char *const opus_mime_types[] = {
477
const struct DecoderPlugin opus_decoder_plugin = {
481
mpd_opus_stream_decode,
484
mpd_opus_scan_stream,