diff mbox series

[FFmpeg-devel,3/3] oggdec: use ffio_ensure_seekback() to not require seeking to read the magic

Message ID M6-e-IP--3-2@lynne.ee
State Accepted
Headers show
Series [FFmpeg-devel,1/3] oggdec: eliminate copies and extra buffers | expand

Commit Message

Lynne April 28, 2020, 11:59 a.m. UTC
This just cleans up the code and simplifies it.

Patch attached.

---
 libavformat/oggdec.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index 05cea2528b..88d54dfe12 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -211,30 +211,30 @@  static int ogg_replace_stream(AVFormatContext *s, uint32_t serial, int size)
     struct ogg *ogg = s->priv_data;
     struct ogg_stream *os;
     const struct ogg_codec *codec;
+    uint8_t magic[8];
     int i = 0;
 
-    if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
-        uint8_t magic[8];
-        avio_seek(s->pb, -size, SEEK_CUR);
-        if (avio_read(s->pb, magic, sizeof(magic)) != sizeof(magic))
-            return AVERROR_INVALIDDATA;
-        avio_seek(s->pb, size - sizeof(magic), SEEK_CUR);
-        codec = ogg_find_codec(magic, sizeof(magic));
-        if (!codec) {
-            av_log(s, AV_LOG_ERROR, "Cannot identify new stream\n");
-            return AVERROR_INVALIDDATA;
-        }
-        for (i = 0; i < ogg->nstreams; i++) {
-            if (ogg->streams[i].codec == codec)
-                break;
-        }
-        if (i >= ogg->nstreams)
-            return ogg_new_stream(s, serial);
-    } else if (ogg->nstreams != 1) {
+    if (ogg->nstreams != 1) {
         avpriv_report_missing_feature(s, "Changing stream parameters in multistream ogg");
         return AVERROR_PATCHWELCOME;
     }
 
+    avio_seek(s->pb, -size, SEEK_CUR);
+    if (avio_read(s->pb, magic, sizeof(magic)) != sizeof(magic))
+        return AVERROR_INVALIDDATA;
+    avio_seek(s->pb, size - sizeof(magic), SEEK_CUR);
+    codec = ogg_find_codec(magic, sizeof(magic));
+    if (!codec) {
+        av_log(s, AV_LOG_ERROR, "Cannot identify new stream\n");
+        return AVERROR_INVALIDDATA;
+    }
+    for (i = 0; i < ogg->nstreams; i++) {
+        if (ogg->streams[i].codec == codec)
+            break;
+    }
+    if (i >= ogg->nstreams)
+        return ogg_new_stream(s, serial);
+
     os = &ogg->streams[i];
 
     os->serial  = serial;
@@ -410,6 +410,9 @@  static int ogg_read_page(AVFormatContext *s, int *sid)
         readout_buf = av_malloc(size);
     }
 
+    /* To rewind if checksum is bad/check magic on switches */
+    ffio_ensure_seekback(bc, size);
+
     ret = avio_read(bc, readout_buf, size);
     if (ret < size) {
         if (idx < 0)
-- 
2.26.2