2012-09-04 09:26:18 +02:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2012-09-04 09:26:18 +02:00
|
|
|
* 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"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "OpusDomain.hxx"
|
2012-09-04 09:26:18 +02:00
|
|
|
#include "OpusHead.hxx"
|
|
|
|
#include "OpusTags.hxx"
|
2013-01-07 23:06:02 +01:00
|
|
|
#include "OggFind.hxx"
|
2016-05-09 13:26:01 +02:00
|
|
|
#include "lib/xiph/OggStreamState.hxx"
|
2016-05-02 23:11:07 +02:00
|
|
|
#include "lib/xiph/OggSyncState.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2016-05-02 23:46:48 +02:00
|
|
|
#include "decoder/Reader.hxx"
|
|
|
|
#include "input/Reader.hxx"
|
2013-01-07 22:12:09 +01:00
|
|
|
#include "OggCodec.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-09-05 19:11:50 +02:00
|
|
|
#include "tag/TagBuilder.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2016-05-13 13:18:19 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
#include <opus.h>
|
|
|
|
#include <ogg/ogg.h>
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-11 17:25:43 +02:00
|
|
|
namespace {
|
|
|
|
|
2013-10-24 20:14:47 +02:00
|
|
|
static constexpr opus_int32 opus_sample_rate = 48000;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2014-11-11 07:45:31 +01:00
|
|
|
/**
|
|
|
|
* Allocate an output buffer for 16 bit PCM samples big enough to hold
|
|
|
|
* a quarter second, larger than 120ms required by libopus.
|
|
|
|
*/
|
|
|
|
static constexpr unsigned opus_output_buffer_frames = opus_sample_rate / 4;
|
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
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
|
2015-01-21 22:13:44 +01:00
|
|
|
mpd_opus_init(gcc_unused const ConfigBlock &block)
|
2012-09-04 09:26:18 +02:00
|
|
|
{
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(opus_domain, opus_get_version_string());
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MPDOpusDecoder {
|
2013-10-21 21:12:37 +02:00
|
|
|
Decoder &decoder;
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &input_stream;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-13 12:25:30 +02:00
|
|
|
OggSyncState oy;
|
2016-05-09 15:32:40 +02:00
|
|
|
OggStreamState os;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-09 15:34:39 +02:00
|
|
|
OpusDecoder *opus_decoder = nullptr;
|
|
|
|
opus_int16 *output_buffer = nullptr;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2014-11-11 07:41:01 +01:00
|
|
|
/**
|
|
|
|
* If non-zero, then a previous Opus stream has been found
|
|
|
|
* already with this number of channels. If opus_decoder is
|
|
|
|
* nullptr, then its end-of-stream packet has been found
|
|
|
|
* already.
|
|
|
|
*/
|
2016-05-09 15:34:39 +02:00
|
|
|
unsigned previous_channels = 0;
|
2014-11-11 07:41:01 +01:00
|
|
|
|
2013-01-07 23:05:33 +01:00
|
|
|
ogg_int64_t eos_granulepos;
|
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
size_t frame_size;
|
|
|
|
|
|
|
|
public:
|
2016-05-13 12:16:22 +02:00
|
|
|
MPDOpusDecoder(DecoderReader &reader)
|
|
|
|
:decoder(reader.GetDecoder()),
|
|
|
|
input_stream(reader.GetInputStream()),
|
2016-05-13 12:25:30 +02:00
|
|
|
oy(reader),
|
2016-05-11 17:57:51 +02:00
|
|
|
os(0) {}
|
2016-05-09 15:32:40 +02:00
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
~MPDOpusDecoder();
|
|
|
|
|
2016-05-11 17:57:51 +02:00
|
|
|
/**
|
|
|
|
* Has the OggStreamState been initialized with the first
|
|
|
|
* serial?
|
|
|
|
*/
|
|
|
|
bool HasSerial() const {
|
|
|
|
return os.GetSerialNo() != 0;
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:24:48 +02:00
|
|
|
/**
|
|
|
|
* Has decoder_initialized() been called yet?
|
|
|
|
*/
|
|
|
|
bool IsInitialized() const {
|
|
|
|
return previous_channels != 0;
|
|
|
|
}
|
|
|
|
|
2016-05-13 12:25:30 +02:00
|
|
|
bool ReadNextPage();
|
2013-01-08 01:22:11 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand HandlePackets();
|
2016-05-11 17:27:02 +02:00
|
|
|
|
2016-05-13 12:25:30 +02:00
|
|
|
bool Seek(uint64_t where_frame);
|
2016-05-11 17:27:02 +02:00
|
|
|
|
|
|
|
private:
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand HandlePacket(const ogg_packet &packet);
|
|
|
|
DecoderCommand HandleBOS(const ogg_packet &packet);
|
2014-11-11 08:36:22 +01:00
|
|
|
DecoderCommand HandleEOS();
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand HandleTags(const ogg_packet &packet);
|
|
|
|
DecoderCommand HandleAudio(const ogg_packet &packet);
|
2012-09-04 09:26:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
MPDOpusDecoder::~MPDOpusDecoder()
|
|
|
|
{
|
2014-02-22 13:37:11 +01:00
|
|
|
delete[] output_buffer;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
if (opus_decoder != nullptr)
|
|
|
|
opus_decoder_destroy(opus_decoder);
|
2013-01-08 01:22:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2016-05-13 12:25:30 +02:00
|
|
|
MPDOpusDecoder::ReadNextPage()
|
2013-01-08 01:22:11 +01:00
|
|
|
{
|
|
|
|
ogg_page page;
|
2013-01-08 11:04:14 +01:00
|
|
|
if (!oy.ExpectPage(page))
|
2013-01-08 01:22:11 +01:00
|
|
|
return false;
|
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
const auto page_serialno = ogg_page_serialno(&page);
|
2016-05-09 15:32:40 +02:00
|
|
|
if (page_serialno != os.GetSerialNo())
|
|
|
|
os.Reinitialize(page_serialno);
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-09 15:32:40 +02:00
|
|
|
os.PageIn(page);
|
2013-01-08 01:22:11 +01:00
|
|
|
return true;
|
2013-01-08 01:18:26 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
inline DecoderCommand
|
2013-01-08 01:18:26 +01:00
|
|
|
MPDOpusDecoder::HandlePackets()
|
|
|
|
{
|
2012-09-04 09:26:18 +02:00
|
|
|
ogg_packet packet;
|
2016-05-09 15:32:40 +02:00
|
|
|
while (os.PacketOut(packet) == 1) {
|
2013-09-27 12:11:37 +02:00
|
|
|
auto cmd = HandlePacket(packet);
|
|
|
|
if (cmd != DecoderCommand::NONE)
|
2012-09-04 09:26:18 +02:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::NONE;
|
2012-09-04 09:26:18 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
inline DecoderCommand
|
2012-09-04 09:26:18 +02:00
|
|
|
MPDOpusDecoder::HandlePacket(const ogg_packet &packet)
|
|
|
|
{
|
|
|
|
if (packet.e_o_s)
|
2014-11-11 08:36:22 +01:00
|
|
|
return HandleEOS();
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
if (packet.b_o_s)
|
|
|
|
return HandleBOS(packet);
|
2016-05-13 13:18:19 +02:00
|
|
|
else if (opus_decoder == nullptr)
|
|
|
|
throw std::runtime_error("BOS packet expected");
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
if (IsOpusTags(packet))
|
|
|
|
return HandleTags(packet);
|
|
|
|
|
|
|
|
return HandleAudio(packet);
|
|
|
|
}
|
|
|
|
|
2013-10-24 20:23:26 +02:00
|
|
|
/**
|
|
|
|
* Load the end-of-stream packet and restore the previous file
|
|
|
|
* position.
|
|
|
|
*/
|
|
|
|
static bool
|
2016-05-13 10:39:36 +02:00
|
|
|
LoadEOSPacket(InputStream &is, Decoder &decoder, int serialno,
|
2013-10-24 20:23:26 +02:00
|
|
|
ogg_packet &packet)
|
|
|
|
{
|
|
|
|
if (!is.CheapSeeking())
|
|
|
|
/* we do this for local files only, because seeking
|
|
|
|
around remote files is expensive and not worth the
|
2016-05-09 15:00:04 +02:00
|
|
|
trouble */
|
2014-11-11 11:18:51 +01:00
|
|
|
return false;
|
2013-10-24 20:23:26 +02:00
|
|
|
|
2014-05-11 18:34:09 +02:00
|
|
|
const auto old_offset = is.GetOffset();
|
2013-10-24 20:23:26 +02:00
|
|
|
|
|
|
|
/* create temporary Ogg objects for seeking and parsing the
|
|
|
|
EOS packet */
|
|
|
|
|
2016-05-09 13:26:01 +02:00
|
|
|
bool result;
|
|
|
|
|
|
|
|
{
|
2016-05-09 15:30:29 +02:00
|
|
|
DecoderReader reader(decoder, is);
|
|
|
|
OggSyncState oy(reader);
|
2016-05-09 13:26:01 +02:00
|
|
|
OggStreamState os(serialno);
|
|
|
|
result = OggSeekFindEOS(oy, os, packet, is);
|
|
|
|
}
|
2013-10-24 20:23:26 +02:00
|
|
|
|
|
|
|
/* restore the previous file position */
|
2014-11-24 08:54:30 +01:00
|
|
|
is.LockSeek(old_offset, IgnoreError());
|
2013-10-24 20:23:26 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the end-of-stream granulepos and restore the previous file
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* @return -1 on error
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static ogg_int64_t
|
2016-05-13 10:39:36 +02:00
|
|
|
LoadEOSGranulePos(InputStream &is, Decoder &decoder, int serialno)
|
2013-10-24 20:23:26 +02:00
|
|
|
{
|
|
|
|
ogg_packet packet;
|
|
|
|
if (!LoadEOSPacket(is, decoder, serialno, packet))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return packet.granulepos;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
inline DecoderCommand
|
2012-09-04 09:26:18 +02:00
|
|
|
MPDOpusDecoder::HandleBOS(const ogg_packet &packet)
|
|
|
|
{
|
|
|
|
assert(packet.b_o_s);
|
|
|
|
|
2016-05-13 13:18:19 +02:00
|
|
|
if (opus_decoder != nullptr || !IsOpusHead(packet))
|
|
|
|
throw std::runtime_error("BOS packet must be OpusHead");
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
unsigned channels;
|
|
|
|
if (!ScanOpusHeader(packet.packet, packet.bytes, channels) ||
|
2016-05-13 13:18:19 +02:00
|
|
|
!audio_valid_channel_count(channels))
|
|
|
|
throw std::runtime_error("Malformed BOS packet");
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
assert(opus_decoder == nullptr);
|
2016-05-11 17:24:48 +02:00
|
|
|
assert(IsInitialized() == (output_buffer != nullptr));
|
2014-11-11 07:41:01 +01:00
|
|
|
|
2016-05-13 13:18:19 +02:00
|
|
|
if (IsInitialized() && channels != previous_channels)
|
|
|
|
throw FormatRuntimeError("Next stream has different channels (%u -> %u)",
|
|
|
|
previous_channels, channels);
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-09 15:43:15 +02:00
|
|
|
const auto opus_serialno = os.GetSerialNo();
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
/* TODO: parse attributes from the OpusHead (sample rate,
|
|
|
|
channels, ...) */
|
|
|
|
|
|
|
|
int opus_error;
|
|
|
|
opus_decoder = opus_decoder_create(opus_sample_rate, channels,
|
|
|
|
&opus_error);
|
2016-05-13 13:18:19 +02:00
|
|
|
if (opus_decoder == nullptr)
|
|
|
|
throw FormatRuntimeError("libopus error: %s",
|
|
|
|
opus_strerror(opus_error));
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-11 17:24:48 +02:00
|
|
|
if (IsInitialized()) {
|
2014-11-11 07:41:01 +01:00
|
|
|
/* decoder was already initialized by the previous
|
|
|
|
stream; skip the rest of this method */
|
|
|
|
LogDebug(opus_domain, "Found another stream");
|
|
|
|
return decoder_get_command(decoder);
|
|
|
|
}
|
|
|
|
|
2016-05-13 10:39:36 +02:00
|
|
|
eos_granulepos = LoadEOSGranulePos(input_stream, decoder,
|
2013-01-07 23:05:33 +01:00
|
|
|
opus_serialno);
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto duration = eos_granulepos >= 0
|
|
|
|
? SignedSongTime::FromScale<uint64_t>(eos_granulepos,
|
|
|
|
opus_sample_rate)
|
|
|
|
: SignedSongTime::Negative();
|
2013-10-24 20:23:26 +02:00
|
|
|
|
2014-11-11 07:41:01 +01:00
|
|
|
previous_channels = channels;
|
2013-08-03 21:00:50 +02:00
|
|
|
const AudioFormat audio_format(opus_sample_rate,
|
|
|
|
SampleFormat::S16, channels);
|
2013-01-07 23:05:33 +01:00
|
|
|
decoder_initialized(decoder, audio_format,
|
|
|
|
eos_granulepos > 0, duration);
|
2013-08-03 21:00:50 +02:00
|
|
|
frame_size = audio_format.GetFrameSize();
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2014-11-11 07:45:31 +01:00
|
|
|
output_buffer = new opus_int16[opus_output_buffer_frames
|
|
|
|
* audio_format.channels];
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
return decoder_get_command(decoder);
|
|
|
|
}
|
|
|
|
|
2014-11-11 08:36:22 +01:00
|
|
|
inline DecoderCommand
|
|
|
|
MPDOpusDecoder::HandleEOS()
|
|
|
|
{
|
2016-05-11 17:24:48 +02:00
|
|
|
if (eos_granulepos < 0 && IsInitialized()) {
|
2014-11-11 07:41:01 +01:00
|
|
|
/* allow chaining of (unseekable) streams */
|
|
|
|
assert(opus_decoder != nullptr);
|
|
|
|
assert(output_buffer != nullptr);
|
|
|
|
|
|
|
|
opus_decoder_destroy(opus_decoder);
|
|
|
|
opus_decoder = nullptr;
|
|
|
|
|
|
|
|
return decoder_get_command(decoder);
|
|
|
|
}
|
|
|
|
|
2016-05-13 13:12:21 +02:00
|
|
|
throw StopDecoder();
|
2014-11-11 08:36:22 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
inline DecoderCommand
|
2012-09-04 09:26:18 +02:00
|
|
|
MPDOpusDecoder::HandleTags(const ogg_packet &packet)
|
|
|
|
{
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainInfo rgi;
|
2013-10-25 19:05:49 +02:00
|
|
|
rgi.Clear();
|
2013-10-24 23:56:06 +02:00
|
|
|
|
2013-09-05 19:11:50 +02:00
|
|
|
TagBuilder tag_builder;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2013-07-30 20:11:57 +02:00
|
|
|
if (ScanOpusTags(packet.packet, packet.bytes,
|
2013-10-24 23:56:06 +02:00
|
|
|
&rgi,
|
2016-02-23 10:10:13 +01:00
|
|
|
add_tag_handler, &tag_builder) &&
|
2013-09-05 19:11:50 +02:00
|
|
|
!tag_builder.IsEmpty()) {
|
2013-10-24 23:56:06 +02:00
|
|
|
decoder_replay_gain(decoder, &rgi);
|
|
|
|
|
2014-01-08 19:42:04 +01:00
|
|
|
Tag tag = tag_builder.Commit();
|
2013-07-31 00:34:22 +02:00
|
|
|
cmd = decoder_tag(decoder, input_stream, std::move(tag));
|
2013-09-05 19:11:50 +02:00
|
|
|
} else
|
2012-09-04 09:26:18 +02:00
|
|
|
cmd = decoder_get_command(decoder);
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
inline DecoderCommand
|
2012-09-04 09:26:18 +02:00
|
|
|
MPDOpusDecoder::HandleAudio(const ogg_packet &packet)
|
|
|
|
{
|
|
|
|
assert(opus_decoder != nullptr);
|
|
|
|
|
|
|
|
int nframes = opus_decode(opus_decoder,
|
|
|
|
(const unsigned char*)packet.packet,
|
|
|
|
packet.bytes,
|
2014-11-11 07:45:31 +01:00
|
|
|
output_buffer, opus_output_buffer_frames,
|
2012-09-04 09:26:18 +02:00
|
|
|
0);
|
2016-05-13 13:18:19 +02:00
|
|
|
if (nframes < 0)
|
|
|
|
throw FormatRuntimeError("libopus error: %s",
|
|
|
|
opus_strerror(nframes));
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
if (nframes > 0) {
|
|
|
|
const size_t nbytes = nframes * frame_size;
|
2013-09-27 12:11:37 +02:00
|
|
|
auto cmd = decoder_data(decoder, input_stream,
|
|
|
|
output_buffer, nbytes,
|
|
|
|
0);
|
|
|
|
if (cmd != DecoderCommand::NONE)
|
2012-09-04 09:26:18 +02:00
|
|
|
return cmd;
|
2013-10-24 22:07:35 +02:00
|
|
|
|
|
|
|
if (packet.granulepos > 0)
|
|
|
|
decoder_timestamp(decoder,
|
|
|
|
double(packet.granulepos)
|
|
|
|
/ opus_sample_rate);
|
2012-09-04 09:26:18 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::NONE;
|
2012-09-04 09:26:18 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 23:05:33 +01:00
|
|
|
bool
|
2016-05-13 12:25:30 +02:00
|
|
|
MPDOpusDecoder::Seek(uint64_t where_frame)
|
2013-01-07 23:05:33 +01:00
|
|
|
{
|
|
|
|
assert(eos_granulepos > 0);
|
2014-05-11 18:34:09 +02:00
|
|
|
assert(input_stream.IsSeekable());
|
|
|
|
assert(input_stream.KnownSize());
|
2013-01-07 23:05:33 +01:00
|
|
|
|
2014-08-26 11:33:45 +02:00
|
|
|
const ogg_int64_t where_granulepos(where_frame);
|
2013-01-07 23:05:33 +01:00
|
|
|
|
|
|
|
/* interpolate the file offset where we expect to find the
|
|
|
|
given granule position */
|
|
|
|
/* TODO: implement binary search */
|
2014-08-19 22:29:52 +02:00
|
|
|
offset_type offset(where_granulepos * input_stream.GetSize()
|
|
|
|
/ eos_granulepos);
|
2013-01-07 23:05:33 +01:00
|
|
|
|
2014-08-26 11:34:24 +02:00
|
|
|
return OggSeekPageAtOffset(oy, os, input_stream, offset);
|
2013-01-07 23:05:33 +01:00
|
|
|
}
|
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
static void
|
2013-10-21 21:12:37 +02:00
|
|
|
mpd_opus_stream_decode(Decoder &decoder,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &input_stream)
|
2012-09-04 09:26:18 +02:00
|
|
|
{
|
2013-10-21 21:12:37 +02:00
|
|
|
if (ogg_codec_detect(&decoder, input_stream) != OGG_CODEC_OPUS)
|
2012-09-04 09:26:18 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* rewind the stream, because ogg_codec_detect() has
|
|
|
|
moved it */
|
2013-10-23 22:08:59 +02:00
|
|
|
input_stream.LockRewind(IgnoreError());
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-02 23:46:48 +02:00
|
|
|
DecoderReader reader(decoder, input_stream);
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-13 12:16:22 +02:00
|
|
|
MPDOpusDecoder d(reader);
|
2016-05-09 15:32:40 +02:00
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
while (true) {
|
2013-09-27 12:11:37 +02:00
|
|
|
auto cmd = d.HandlePackets();
|
2013-01-07 23:05:33 +01:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2016-05-13 12:25:30 +02:00
|
|
|
if (d.Seek(decoder_seek_where_frame(decoder)))
|
2013-01-07 23:05:33 +01:00
|
|
|
decoder_command_finished(decoder);
|
|
|
|
else
|
|
|
|
decoder_seek_error(decoder);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd != DecoderCommand::NONE)
|
2012-09-04 09:26:18 +02:00
|
|
|
break;
|
|
|
|
|
2016-05-13 12:25:30 +02:00
|
|
|
if (!d.ReadNextPage())
|
2013-01-08 01:05:59 +01:00
|
|
|
break;
|
2012-09-04 09:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
mpd_opus_scan_stream(InputStream &is,
|
2016-02-23 10:10:13 +01:00
|
|
|
const TagHandler &handler, void *handler_ctx)
|
2012-09-04 09:26:18 +02:00
|
|
|
{
|
2016-05-02 23:46:48 +02:00
|
|
|
InputStreamReader reader(is);
|
|
|
|
OggSyncState oy(reader);
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-05-09 15:28:41 +02:00
|
|
|
ogg_page first_page;
|
|
|
|
if (!oy.ExpectPage(first_page))
|
2012-09-04 09:26:18 +02:00
|
|
|
return false;
|
|
|
|
|
2016-05-09 15:28:41 +02:00
|
|
|
OggStreamState os(first_page);
|
|
|
|
|
2016-04-19 13:05:42 +02:00
|
|
|
/* read at most 64 more pages */
|
|
|
|
unsigned remaining_pages = 64;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
2016-04-19 13:07:28 +02:00
|
|
|
unsigned remaining_packets = 4;
|
2012-09-04 09:26:18 +02:00
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
ogg_packet packet;
|
2016-04-19 13:07:28 +02:00
|
|
|
while (remaining_packets > 0) {
|
2016-05-09 15:28:41 +02:00
|
|
|
int r = os.PacketOut(packet);
|
2012-09-04 09:26:18 +02:00
|
|
|
if (r < 0) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
if (remaining_pages-- == 0)
|
|
|
|
break;
|
|
|
|
|
2013-01-08 11:04:14 +01:00
|
|
|
if (!oy.ExpectPageIn(os)) {
|
2012-09-04 09:26:18 +02:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-19 13:07:28 +02:00
|
|
|
--remaining_packets;
|
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
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,
|
2013-10-24 23:56:06 +02:00
|
|
|
nullptr,
|
2012-09-04 09:26:18 +02:00
|
|
|
handler, handler_ctx))
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
if (packet.e_o_s || OggSeekFindEOS(oy, os, packet, is)) {
|
|
|
|
const auto duration =
|
|
|
|
SongTime::FromScale<uint64_t>(packet.granulepos,
|
|
|
|
opus_sample_rate);
|
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, duration);
|
|
|
|
}
|
2013-01-07 23:06:02 +01:00
|
|
|
|
2012-09-04 09:26:18 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const opus_suffixes[] = {
|
|
|
|
"opus",
|
|
|
|
"ogg",
|
|
|
|
"oga",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const opus_mime_types[] = {
|
2014-11-12 15:14:34 +01:00
|
|
|
/* the official MIME type (RFC 5334) */
|
|
|
|
"audio/ogg",
|
|
|
|
|
|
|
|
/* deprecated (RFC 5334) */
|
|
|
|
"application/ogg",
|
|
|
|
|
|
|
|
/* deprecated; from an early draft */
|
2012-09-04 09:26:18 +02:00
|
|
|
"audio/opus",
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2016-05-11 17:25:43 +02:00
|
|
|
} /* anonymous namespace */
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin opus_decoder_plugin = {
|
2012-09-04 09:26:18 +02:00
|
|
|
"opus",
|
|
|
|
mpd_opus_init,
|
|
|
|
nullptr,
|
|
|
|
mpd_opus_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mpd_opus_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
opus_suffixes,
|
|
|
|
opus_mime_types,
|
|
|
|
};
|