From 0b046b51b2eda96bc109b7faa103a444e4470857 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 19 Aug 2021 20:07:13 +0200 Subject: [PATCH] mp4: Reduce atom parsing to the bare minimum. 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 | 65 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/mp4.c b/mp4.c index e5ec9dd1..a110e914 100644 --- 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; } -- 2.39.2