decoder/opus: move code to new class OggDecoder
Prepare to reuse the same code for a new Vorbis decoder plugin based on libvorbis instead of libvorbisfile.
This commit is contained in:
@@ -1030,6 +1030,7 @@ endif
|
|||||||
|
|
||||||
if HAVE_XIPH
|
if HAVE_XIPH
|
||||||
libdecoder_a_SOURCES += \
|
libdecoder_a_SOURCES += \
|
||||||
|
src/decoder/plugins/OggDecoder.cxx src/decoder/plugins/OggDecoder.hxx \
|
||||||
src/decoder/plugins/OggCodec.cxx src/decoder/plugins/OggCodec.hxx
|
src/decoder/plugins/OggCodec.cxx src/decoder/plugins/OggCodec.hxx
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
87
src/decoder/plugins/OggDecoder.cxx
Normal file
87
src/decoder/plugins/OggDecoder.cxx
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2003-2016 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 "OggDecoder.hxx"
|
||||||
|
#include "lib/xiph/OggFind.hxx"
|
||||||
|
#include "input/InputStream.hxx"
|
||||||
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the end-of-stream packet and restore the previous file
|
||||||
|
* position.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
OggDecoder::LoadEndPacket(ogg_packet &packet) const
|
||||||
|
{
|
||||||
|
if (!input_stream.CheapSeeking())
|
||||||
|
/* we do this for local files only, because seeking
|
||||||
|
around remote files is expensive and not worth the
|
||||||
|
trouble */
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const auto old_offset = input_stream.GetOffset();
|
||||||
|
|
||||||
|
/* create temporary Ogg objects for seeking and parsing the
|
||||||
|
EOS packet */
|
||||||
|
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
{
|
||||||
|
DecoderReader reader(decoder, input_stream);
|
||||||
|
OggSyncState sync2(reader);
|
||||||
|
OggStreamState stream2(GetSerialNo());
|
||||||
|
result = OggSeekFindEOS(sync2, stream2, packet,
|
||||||
|
input_stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* restore the previous file position */
|
||||||
|
input_stream.LockSeek(old_offset, IgnoreError());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
ogg_int64_t
|
||||||
|
OggDecoder::LoadEndGranulePos() const
|
||||||
|
{
|
||||||
|
ogg_packet packet;
|
||||||
|
if (!LoadEndPacket(packet))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return packet.granulepos;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
OggDecoder::SeekGranulePos(ogg_int64_t where_granulepos, Error &error)
|
||||||
|
{
|
||||||
|
assert(IsSeekable());
|
||||||
|
|
||||||
|
/* interpolate the file offset where we expect to find the
|
||||||
|
given granule position */
|
||||||
|
/* TODO: implement binary search */
|
||||||
|
offset_type offset(where_granulepos * input_stream.GetSize()
|
||||||
|
/ end_granulepos);
|
||||||
|
|
||||||
|
if (!input_stream.LockSeek(offset, error))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
PostSeek();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
64
src/decoder/plugins/OggDecoder.hxx
Normal file
64
src/decoder/plugins/OggDecoder.hxx
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2003-2016 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_OGG_DECODER_HXX
|
||||||
|
#define MPD_OGG_DECODER_HXX
|
||||||
|
|
||||||
|
#include "config.h" /* must be first for large file support */
|
||||||
|
#include "lib/xiph/OggVisitor.hxx"
|
||||||
|
#include "decoder/Reader.hxx"
|
||||||
|
|
||||||
|
class Error;
|
||||||
|
|
||||||
|
class OggDecoder : public OggVisitor {
|
||||||
|
ogg_int64_t end_granulepos;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Decoder &decoder;
|
||||||
|
InputStream &input_stream;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit OggDecoder(DecoderReader &reader)
|
||||||
|
:OggVisitor(reader),
|
||||||
|
decoder(reader.GetDecoder()),
|
||||||
|
input_stream(reader.GetInputStream()) {}
|
||||||
|
|
||||||
|
bool Seek(OggSyncState &oy, uint64_t where_frame);
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Load the end-of-stream packet and restore the previous file
|
||||||
|
* position.
|
||||||
|
*/
|
||||||
|
bool LoadEndPacket(ogg_packet &packet) const;
|
||||||
|
ogg_int64_t LoadEndGranulePos() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ogg_int64_t UpdateEndGranulePos() {
|
||||||
|
return end_granulepos = LoadEndGranulePos();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsSeekable() const {
|
||||||
|
return end_granulepos > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SeekGranulePos(ogg_int64_t where_granulepos, Error &error);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@@ -19,12 +19,12 @@
|
|||||||
|
|
||||||
#include "config.h" /* must be first for large file support */
|
#include "config.h" /* must be first for large file support */
|
||||||
#include "OpusDecoderPlugin.h"
|
#include "OpusDecoderPlugin.h"
|
||||||
|
#include "OggDecoder.hxx"
|
||||||
#include "OpusDomain.hxx"
|
#include "OpusDomain.hxx"
|
||||||
#include "OpusHead.hxx"
|
#include "OpusHead.hxx"
|
||||||
#include "OpusTags.hxx"
|
#include "OpusTags.hxx"
|
||||||
#include "lib/xiph/OggPacket.hxx"
|
#include "lib/xiph/OggPacket.hxx"
|
||||||
#include "lib/xiph/OggFind.hxx"
|
#include "lib/xiph/OggFind.hxx"
|
||||||
#include "lib/xiph/OggVisitor.hxx"
|
|
||||||
#include "../DecoderAPI.hxx"
|
#include "../DecoderAPI.hxx"
|
||||||
#include "decoder/Reader.hxx"
|
#include "decoder/Reader.hxx"
|
||||||
#include "input/Reader.hxx"
|
#include "input/Reader.hxx"
|
||||||
@@ -73,10 +73,7 @@ mpd_opus_init(gcc_unused const ConfigBlock &block)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
class MPDOpusDecoder final : public OggVisitor {
|
class MPDOpusDecoder final : public OggDecoder {
|
||||||
Decoder &decoder;
|
|
||||||
InputStream &input_stream;
|
|
||||||
|
|
||||||
OpusDecoder *opus_decoder = nullptr;
|
OpusDecoder *opus_decoder = nullptr;
|
||||||
opus_int16 *output_buffer = nullptr;
|
opus_int16 *output_buffer = nullptr;
|
||||||
|
|
||||||
@@ -88,15 +85,11 @@ class MPDOpusDecoder final : public OggVisitor {
|
|||||||
*/
|
*/
|
||||||
unsigned previous_channels = 0;
|
unsigned previous_channels = 0;
|
||||||
|
|
||||||
ogg_int64_t eos_granulepos;
|
|
||||||
|
|
||||||
size_t frame_size;
|
size_t frame_size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MPDOpusDecoder(DecoderReader &reader)
|
explicit MPDOpusDecoder(DecoderReader &reader)
|
||||||
:OggVisitor(reader),
|
:OggDecoder(reader) {}
|
||||||
decoder(reader.GetDecoder()),
|
|
||||||
input_stream(reader.GetInputStream()) {}
|
|
||||||
|
|
||||||
~MPDOpusDecoder();
|
~MPDOpusDecoder();
|
||||||
|
|
||||||
@@ -107,8 +100,6 @@ public:
|
|||||||
return previous_channels != 0;
|
return previous_channels != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DecoderCommand HandlePackets();
|
|
||||||
|
|
||||||
bool Seek(uint64_t where_frame);
|
bool Seek(uint64_t where_frame);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -139,57 +130,6 @@ MPDOpusDecoder::OnOggPacket(const ogg_packet &packet)
|
|||||||
HandleAudio(packet);
|
HandleAudio(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the end-of-stream packet and restore the previous file
|
|
||||||
* position.
|
|
||||||
*/
|
|
||||||
static bool
|
|
||||||
LoadEOSPacket(InputStream &is, Decoder &decoder, int serialno,
|
|
||||||
ogg_packet &packet)
|
|
||||||
{
|
|
||||||
if (!is.CheapSeeking())
|
|
||||||
/* we do this for local files only, because seeking
|
|
||||||
around remote files is expensive and not worth the
|
|
||||||
trouble */
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const auto old_offset = is.GetOffset();
|
|
||||||
|
|
||||||
/* create temporary Ogg objects for seeking and parsing the
|
|
||||||
EOS packet */
|
|
||||||
|
|
||||||
bool result;
|
|
||||||
|
|
||||||
{
|
|
||||||
DecoderReader reader(decoder, is);
|
|
||||||
OggSyncState oy(reader);
|
|
||||||
OggStreamState os(serialno);
|
|
||||||
result = OggSeekFindEOS(oy, os, packet, is);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* restore the previous file position */
|
|
||||||
is.LockSeek(old_offset, IgnoreError());
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the end-of-stream granulepos and restore the previous file
|
|
||||||
* position.
|
|
||||||
*
|
|
||||||
* @return -1 on error
|
|
||||||
*/
|
|
||||||
gcc_pure
|
|
||||||
static ogg_int64_t
|
|
||||||
LoadEOSGranulePos(InputStream &is, Decoder &decoder, int serialno)
|
|
||||||
{
|
|
||||||
ogg_packet packet;
|
|
||||||
if (!LoadEOSPacket(is, decoder, serialno, packet))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return packet.granulepos;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
MPDOpusDecoder::OnOggBeginning(const ogg_packet &packet)
|
MPDOpusDecoder::OnOggBeginning(const ogg_packet &packet)
|
||||||
{
|
{
|
||||||
@@ -210,8 +150,6 @@ MPDOpusDecoder::OnOggBeginning(const ogg_packet &packet)
|
|||||||
throw FormatRuntimeError("Next stream has different channels (%u -> %u)",
|
throw FormatRuntimeError("Next stream has different channels (%u -> %u)",
|
||||||
previous_channels, channels);
|
previous_channels, channels);
|
||||||
|
|
||||||
const auto opus_serialno = GetSerialNo();
|
|
||||||
|
|
||||||
/* TODO: parse attributes from the OpusHead (sample rate,
|
/* TODO: parse attributes from the OpusHead (sample rate,
|
||||||
channels, ...) */
|
channels, ...) */
|
||||||
|
|
||||||
@@ -229,8 +167,7 @@ MPDOpusDecoder::OnOggBeginning(const ogg_packet &packet)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
eos_granulepos = LoadEOSGranulePos(input_stream, decoder,
|
const auto eos_granulepos = UpdateEndGranulePos();
|
||||||
opus_serialno);
|
|
||||||
const auto duration = eos_granulepos >= 0
|
const auto duration = eos_granulepos >= 0
|
||||||
? SignedSongTime::FromScale<uint64_t>(eos_granulepos,
|
? SignedSongTime::FromScale<uint64_t>(eos_granulepos,
|
||||||
opus_sample_rate)
|
opus_sample_rate)
|
||||||
@@ -254,7 +191,7 @@ MPDOpusDecoder::OnOggBeginning(const ogg_packet &packet)
|
|||||||
void
|
void
|
||||||
MPDOpusDecoder::OnOggEnd()
|
MPDOpusDecoder::OnOggEnd()
|
||||||
{
|
{
|
||||||
if (eos_granulepos < 0 && IsInitialized()) {
|
if (!IsSeekable() && IsInitialized()) {
|
||||||
/* allow chaining of (unseekable) streams */
|
/* allow chaining of (unseekable) streams */
|
||||||
assert(opus_decoder != nullptr);
|
assert(opus_decoder != nullptr);
|
||||||
assert(output_buffer != nullptr);
|
assert(output_buffer != nullptr);
|
||||||
@@ -318,23 +255,13 @@ MPDOpusDecoder::HandleAudio(const ogg_packet &packet)
|
|||||||
bool
|
bool
|
||||||
MPDOpusDecoder::Seek(uint64_t where_frame)
|
MPDOpusDecoder::Seek(uint64_t where_frame)
|
||||||
{
|
{
|
||||||
assert(eos_granulepos > 0);
|
assert(IsSeekable());
|
||||||
assert(input_stream.IsSeekable());
|
assert(input_stream.IsSeekable());
|
||||||
assert(input_stream.KnownSize());
|
assert(input_stream.KnownSize());
|
||||||
|
|
||||||
const ogg_int64_t where_granulepos(where_frame);
|
const ogg_int64_t where_granulepos(where_frame);
|
||||||
|
|
||||||
/* interpolate the file offset where we expect to find the
|
return SeekGranulePos(where_granulepos, IgnoreError());
|
||||||
given granule position */
|
|
||||||
/* TODO: implement binary search */
|
|
||||||
offset_type offset(where_granulepos * input_stream.GetSize()
|
|
||||||
/ eos_granulepos);
|
|
||||||
|
|
||||||
if (!input_stream.LockSeek(offset, IgnoreError()))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
PostSeek();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Reference in New Issue
Block a user