]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
mp4: Reduce atom parsing to the bare minimum.
[paraslash.git] / 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;
 }