]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
mp4: Reduce atom parsing to the bare minimum.
authorAndre Noll <maan@tuebingen.mpg.de>
Thu, 19 Aug 2021 18:07:13 +0000 (20:07 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
This replaces need_parse_when_meta_only() by need_atom() which is
called from parse_sub_atoms() for both regular opens and meta-only
opens to decide if the detected atom needs to be parsed.

After this patch we skip more atoms than we used to do, speeding up
the operation for both kinds of opens.

mp4.c

diff --git a/mp4.c b/mp4.c
index e5ec9dd13ecd6ab71ef00e3657a7eb5a598a60eb..a110e9146f88cb45e9a910652385415807426f3d 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -408,27 +408,6 @@ static int64_t get_position(const struct mp4 *f)
        return f->current_position;
 }
 
-static int need_parse_when_meta_only(uint8_t atom_type)
-{
-       switch (atom_type) {
-       case ATOM_EDTS:
-       case ATOM_DRMS:
-       case ATOM_SINF:
-       case ATOM_SCHI:
-       case ATOM_STTS:
-       case ATOM_STSZ:
-       case ATOM_STZ2:
-       case ATOM_STCO:
-       case ATOM_STSC:
-       case ATOM_FRMA:
-       case ATOM_IVIV:
-       case ATOM_PRIV:
-               return 0;
-       default:
-               return 1;
-       }
-}
-
 static int32_t set_position(struct mp4 *f, int64_t position)
 {
        f->cb->seek(f->cb->user_data, position);
@@ -864,6 +843,34 @@ static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
        return ret;
 }
 
+static bool need_atom(uint8_t atom_type, bool meta_only)
+{
+       /* these are needed in any case */
+       switch (atom_type) {
+       case ATOM_STSD:
+       case ATOM_META:
+       case ATOM_TRAK:
+       case ATOM_MDIA:
+       case ATOM_MINF:
+       case ATOM_STBL:
+               return true;
+       }
+       /* meta-only opens don't need anything else */
+       if (meta_only)
+               return false;
+       /* these are only required for regular opens */
+       switch (atom_type) {
+       case ATOM_STTS:
+       case ATOM_STSZ:
+       case ATOM_STCO:
+       case ATOM_STSC:
+       case ATOM_MDHD:
+       case ATOM_UDTA:
+               return true;
+       }
+       return false;
+}
+
 /* parse atoms that are sub atoms of other atoms */
 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
 {
@@ -887,18 +894,16 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
                        f->track[f->total_tracks - 1] = para_calloc(
                                sizeof(struct mp4_track));
                }
-               /* parse subatoms */
-               if (meta_only && !need_parse_when_meta_only(atom_type)) {
+               if (!need_atom(atom_type, meta_only)) {
                        set_position(f, get_position(f) + size - header_size);
-               } else if (atom_type < SUBATOMIC) {
+                       continue;
+               }
+               if (atom_type < SUBATOMIC)
                        ret = parse_sub_atoms(f, size - header_size, meta_only);
-                       if (ret <= 0)
-                               return ret;
-               } else {
+               else
                        ret = atom_read(f, size, atom_type);
-                       if (ret <= 0)
-                               return ret;
-               }
+               if (ret <= 0)
+                       return ret;
        }
        return 1;
 }