decoder/Opus: move page reader into the decoder class

This commit is contained in:
Max Kellermann 2013-01-08 01:22:11 +01:00
parent ceb652915f
commit 61e7e531c2

View File

@ -84,7 +84,9 @@ public:
:decoder(_decoder), input_stream(_input_stream) {} :decoder(_decoder), input_stream(_input_stream) {}
~MPDOpusDecoder(); ~MPDOpusDecoder();
enum decoder_command HandlePage(ogg_page &page); bool ReadFirstPage(ogg_sync_state &oy);
bool ReadNextPage(ogg_sync_state &oy);
enum decoder_command HandlePackets(); enum decoder_command HandlePackets();
enum decoder_command HandlePacket(const ogg_packet &packet); enum decoder_command HandlePacket(const ogg_packet &packet);
enum decoder_command HandleBOS(const ogg_packet &packet); enum decoder_command HandleBOS(const ogg_packet &packet);
@ -103,19 +105,33 @@ MPDOpusDecoder::~MPDOpusDecoder()
ogg_stream_clear(&os); ogg_stream_clear(&os);
} }
inline enum decoder_command inline bool
MPDOpusDecoder::HandlePage(ogg_page &page) MPDOpusDecoder::ReadFirstPage(ogg_sync_state &oy)
{ {
assert(!os_initialized);
if (!OggExpectFirstPage(oy, os, decoder, input_stream))
return false;
os_initialized = true;
return true;
}
inline bool
MPDOpusDecoder::ReadNextPage(ogg_sync_state &oy)
{
assert(os_initialized);
ogg_page page;
if (!OggExpectPage(oy, page, decoder, input_stream))
return false;
const auto page_serialno = ogg_page_serialno(&page); const auto page_serialno = ogg_page_serialno(&page);
if (!os_initialized) { if (page_serialno != os.serialno)
os_initialized = true;
ogg_stream_init(&os, page_serialno);
} else if (page_serialno != os.serialno)
ogg_stream_reset_serialno(&os, page_serialno); ogg_stream_reset_serialno(&os, page_serialno);
ogg_stream_pagein(&os, &page); ogg_stream_pagein(&os, &page);
return true;
return HandlePackets();
} }
inline enum decoder_command inline enum decoder_command
@ -256,14 +272,19 @@ mpd_opus_stream_decode(struct decoder *decoder,
ogg_sync_state oy; ogg_sync_state oy;
ogg_sync_init(&oy); ogg_sync_init(&oy);
while (true) { if (!d.ReadFirstPage(oy)) {
ogg_page page; ogg_sync_clear(&oy);
if (!OggExpectPage(oy, page, decoder, input_stream)) return;
break; }
enum decoder_command cmd = d.HandlePage(page); while (true) {
enum decoder_command cmd = d.HandlePackets();
if (cmd != DECODE_COMMAND_NONE) if (cmd != DECODE_COMMAND_NONE)
break; break;
if (!d.ReadNextPage(oy))
break;
} }
ogg_sync_clear(&oy); ogg_sync_clear(&oy);