decoder/opus: move code to class OggVisitor

This commit is contained in:
Max Kellermann
2016-05-11 17:55:42 +02:00
parent 07fa3764ef
commit b5ecfb4c9a
4 changed files with 222 additions and 100 deletions

101
src/lib/xiph/OggVisitor.cxx Normal file
View File

@@ -0,0 +1,101 @@
/*
* 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"
#include "OggVisitor.hxx"
#include <stdexcept>
void
OggVisitor::EndStream()
{
if (!has_stream)
return;
has_stream = false;
OnOggEnd();
}
inline bool
OggVisitor::ReadNextPage()
{
ogg_page page;
if (!sync.ExpectPage(page))
return false;
const auto page_serialno = ogg_page_serialno(&page);
if (page_serialno != stream.GetSerialNo()) {
EndStream();
stream.Reinitialize(page_serialno);
}
stream.PageIn(page);
return true;
}
inline void
OggVisitor::HandlePacket(const ogg_packet &packet)
{
if (packet.b_o_s) {
EndStream();
has_stream = true;
OnOggBeginning(packet);
return;
}
if (!has_stream)
/* fail if BOS is missing */
throw std::runtime_error("BOS packet expected");
if (packet.e_o_s) {
EndStream();
return;
}
OnOggPacket(packet);
}
inline void
OggVisitor::HandlePackets()
{
ogg_packet packet;
while (stream.PacketOut(packet) == 1)
HandlePacket(packet);
}
void
OggVisitor::Visit()
{
do {
HandlePackets();
} while (ReadNextPage());
}
void
OggVisitor::PostSeek()
{
sync.Reset();
/* reset the stream to clear any previous partial packet
data */
stream.Reset();
/* find the next Ogg page and feed it into the stream */
sync.ExpectPageSeekIn(stream);
}

View File

@@ -0,0 +1,70 @@
/*
* 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_VISITOR_HXX
#define MPD_OGG_VISITOR_HXX
#include "check.h"
#include "OggSyncState.hxx"
#include "OggStreamState.hxx"
#include <ogg/ogg.h>
#include <stddef.h>
class Reader;
/**
* Abstract class which iterates over Ogg packets in a #Reader.
* Subclass it and implement the virtual methods.
*/
class OggVisitor {
OggSyncState sync;
OggStreamState stream;
bool has_stream = false;
public:
explicit OggVisitor(Reader &reader)
:sync(reader), stream(0) {}
long GetSerialNo() const {
return stream.GetSerialNo();
}
void Visit();
/**
* Call this method after seeking the #Reader.
*/
void PostSeek();
private:
void EndStream();
bool ReadNextPage();
void HandlePacket(const ogg_packet &packet);
void HandlePackets();
protected:
virtual void OnOggBeginning(const ogg_packet &packet) = 0;
virtual void OnOggPacket(const ogg_packet &packet) = 0;
virtual void OnOggEnd() = 0;
};
#endif