From b8f2518ef57e216a6db05a7a4eb57f723f6ea69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= Date: Sun, 17 Jan 2021 11:51:47 +0100 Subject: [PATCH] Move the page counting logic in the Ogg reader --- src/cli.cc | 10 ++++------ src/ogg.cc | 1 + src/opustags.h | 6 ++++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/cli.cc b/src/cli.cc index 7b5685d..510c6f0 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -357,16 +357,14 @@ static void process(ot::ogg_reader& reader, ot::ogg_writer* writer, const ot::op { bool focused = false; /*< the stream on which we operate is defined */ int focused_serialno; /*< when focused, the serialno of the focused stream */ - int absolute_page_no = -1; /*< page number in the physical stream, not logical */ for (;;) { ot::status rc = reader.next_page(); if (rc == ot::st::end_of_stream) break; - else if (rc == ot::st::bad_stream && absolute_page_no == -1) + else if (rc == ot::st::bad_stream && reader.absolute_page_no == (size_t) -1) throw ot::status {ot::st::bad_stream, "Input is not a valid Ogg file."}; else if (rc != ot::st::ok) throw rc; - ++absolute_page_no; auto serialno = ogg_page_serialno(&reader.page); auto pageno = ogg_page_pageno(&reader.page); if (!focused) { @@ -376,7 +374,7 @@ static void process(ot::ogg_reader& reader, ot::ogg_writer* writer, const ot::op /** \todo Support mixed streams. */ throw ot::status {ot::st::error, "Muxed streams are not supported yet."}; } - if (absolute_page_no == 0) { // Identification header + if (reader.absolute_page_no == 0) { // Identification header if (!ot::is_opus_stream(reader.page)) throw ot::status {ot::st::error, "Not an Opus stream."}; if (writer) { @@ -384,7 +382,7 @@ static void process(ot::ogg_reader& reader, ot::ogg_writer* writer, const ot::op if (rc != ot::st::ok) throw rc; } - } else if (absolute_page_no == 1) { // Comment header + } else if (reader.absolute_page_no == 1) { // Comment header ot::opus_tags tags; rc = reader.process_header_packet( [&tags](ogg_packet& p) { return ot::parse_tags(p, tags); }); @@ -409,7 +407,7 @@ static void process(ot::ogg_reader& reader, ot::ogg_writer* writer, const ot::op throw rc; } } - if (absolute_page_no < 1) + if (reader.absolute_page_no < 1) throw ot::status {ot::st::error, "Expected at least 2 Ogg pages."}; } diff --git a/src/ogg.cc b/src/ogg.cc index 84080fa..5ce260f 100644 --- a/src/ogg.cc +++ b/src/ogg.cc @@ -46,6 +46,7 @@ ot::status ot::ogg_reader::next_page() if (ogg_sync_wrote(&sync, len) != 0) return {st::libogg_error, "ogg_sync_wrote failed."}; } + ++absolute_page_no; return st::ok; } diff --git a/src/opustags.h b/src/opustags.h index 08d8940..595b05a 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -258,6 +258,12 @@ struct ogg_reader { * to ogg_sync_pageout, wrapped by #read_page. */ ogg_page page; + /** + * Page number in the physical stream of the last read page, disregarding multiplexed + * streams. The first page number is 0. When no page has been read, its value is + * (size_t) -1. + */ + size_t absolute_page_no = -1; /** * The file is our source of binary data. It is not integrated to libogg, so we need to * handle it ourselves.