[FFmpeg-devel] [PATCH 20/37] avformat/matroskadec: Refactor some functions

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri May 17 01:30:04 EEST 2019


Since the changes to the parsing of SimpleBlocks, both ebml_parse_id and
ebml_parse_elem are only called from one place, so that it is possible
to inline these two function calls. This is done, but not completely:
ebml_parse_id still exists in a modified form. This is done in
preparation for a further patch regarding the handling of
unknown-length elements.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavformat/matroskadec.c | 59 +++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 2084b72748..deb01578d5 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -1052,40 +1052,17 @@ static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
     return res;
 }
 
-static int ebml_parse_elem(MatroskaDemuxContext *matroska,
-                           EbmlSyntax *syntax, void *data);
+static int ebml_parse(MatroskaDemuxContext *matroska,
+                      EbmlSyntax *syntax, void *data);
 
-static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
-                         uint32_t id, void *data)
+static EbmlSyntax *ebml_parse_id(EbmlSyntax *syntax, uint32_t id)
 {
     int i;
     for (i = 0; syntax[i].id; i++)
         if (id == syntax[i].id)
             break;
-    if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
-        matroska->num_levels > 0                   &&
-        matroska->levels[matroska->num_levels - 1].length == EBML_UNKNOWN_LENGTH)
-        return 0;  // we reached the end of an unknown size cluster
-    if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
-        av_log(matroska->ctx, AV_LOG_DEBUG, "Unknown entry 0x%"PRIX32"\n", id);
-    }
-    return ebml_parse_elem(matroska, &syntax[i], data);
-}
 
-static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
-                      void *data)
-{
-    if (!matroska->current_id) {
-        uint64_t id;
-        int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id, 0);
-        if (res < 0) {
-            // in live mode, finish parsing if EOF is reached.
-            return (matroska->is_live && matroska->ctx->pb->eof_reached &&
-                    res == AVERROR_EOF) ? 1 : res;
-        }
-        matroska->current_id = id | 1 << 7 * res;
-    }
-    return ebml_parse_id(matroska, syntax, matroska->current_id, data);
+    return &syntax[i];
 }
 
 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
@@ -1170,8 +1147,8 @@ static MatroskaLevel1Element *matroska_find_level1_elem(MatroskaDemuxContext *ma
     return elem;
 }
 
-static int ebml_parse_elem(MatroskaDemuxContext *matroska,
-                           EbmlSyntax *syntax, void *data)
+static int ebml_parse(MatroskaDemuxContext *matroska,
+                      EbmlSyntax *syntax, void *data)
 {
     static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
         [EBML_UINT]  = 8,
@@ -1185,12 +1162,34 @@ static int ebml_parse_elem(MatroskaDemuxContext *matroska,
         // no limits for anything else
     };
     AVIOContext *pb = matroska->ctx->pb;
-    uint32_t id = syntax->id;
+    uint32_t id;
     uint64_t length;
     int res;
     void *newelem;
     MatroskaLevel1Element *level1_elem;
 
+    if (!matroska->current_id) {
+        uint64_t id;
+        res = ebml_read_num(matroska, pb, 4, &id, 0);
+        if (res < 0) {
+            // in live mode, finish parsing if EOF is reached.
+            return (matroska->is_live && pb->eof_reached &&
+                    res == AVERROR_EOF) ? 1 : res;
+        }
+        matroska->current_id = id | 1 << 7 * res;
+    }
+
+    id = matroska->current_id;
+
+    syntax = ebml_parse_id(syntax, id);
+    if (!syntax->id && id == MATROSKA_ID_CLUSTER &&
+        matroska->num_levels > 0                 &&
+        matroska->levels[matroska->num_levels - 1].length == EBML_UNKNOWN_LENGTH)
+        return 0;  // we reached the end of an unknown size cluster
+    if (!syntax->id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
+        av_log(matroska->ctx, AV_LOG_DEBUG, "Unknown entry 0x%"PRIX32"\n", id);
+    }
+
     data = (char *) data + syntax->data_offset;
     if (syntax->list_elem_size) {
         EbmlList *list = data;
-- 
2.21.0



More information about the ffmpeg-devel mailing list