mp4: Eliminate duplication between the two open functions.
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 21 Aug 2021 15:56:17 +0000 (17:56 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:36 +0000 (21:37 +0200)
The only difference between mp4_open_read() and mp4_open_meta()
is that they pass different values for the meta_only flag to
parse_root_atoms(). We can avoid some duplication by moving the
common code to parse_root_atoms(). Rename that function to open_file()
because it now does more than just parsing atoms.

The patch also changes the prototype of both public open functions
to return an integer error code in addition to the pointer to an mp4
structure. This allows us to gradually improve the error diagnostics.

aac_afh.c
mp4.c
mp4.h

index 42cba27a3670ddac4bde28f9d683c9c31a86e924..f3a06c4c213c80fad95158d8c33b18fafa43e33a 100644 (file)
--- a/aac_afh.c
+++ b/aac_afh.c
@@ -60,9 +60,8 @@ static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
        c->cb.seek = aac_afh_seek_cb;
        c->cb.user_data = c;
 
-       ret = -E_MP4_OPEN;
-       c->mp4 = mp4_open_read(&c->cb);
-       if (!c->mp4)
+       ret = mp4_open_read(&c->cb, &c->mp4);
+       if (ret < 0)
                goto free_ctx;
        *afh_context = c;
        return 0;
@@ -229,9 +228,9 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize,
                return ret;
        lseek(fd, 0, SEEK_SET);
 
-       mp4 = mp4_open_meta(&cb);
-       if (!mp4)
-               return -E_MP4_OPEN;
+       ret = mp4_open_meta(&cb, &mp4);
+       if (ret < 0)
+               return ret;
        metadata = mp4_get_meta(mp4);
        PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count);
        replace_or_add_tag("artist", tags->artist, metadata);
diff --git a/mp4.c b/mp4.c
index ca70bd20b8468f0d5a170fc9e67292e681617ceb..91b5195675dd5b9325972fb6cfb561f499f9a82f 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -901,15 +901,14 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
        return 1;
 }
 
-static int parse_root_atoms(struct mp4 *f, bool meta_only)
+static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
 {
        int ret;
        uint64_t size;
-       uint8_t atom_type = 0;
-       uint8_t header_size = 0;
-
-       f->file_size = 0;
+       uint8_t atom_type, header_size;
+       struct mp4 *f = para_calloc(sizeof(*f));
 
+       f->cb = cb;
        while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
                f->file_size += size;
                f->last_atom = atom_type;
@@ -923,25 +922,24 @@ static int parse_root_atoms(struct mp4 *f, bool meta_only)
                if (ret <= 0)
                        break;
        }
-       if (ret < 0)
-               return ret;
+       if (ret < 0) {
+               ret = -E_MP4_OPEN;
+               goto fail;
+       }
+       ret = -E_MP4_TRACK;
        if (!f->audio_track)
-               return -E_MP4_TRACK;
+               goto fail;
+       *result = f;
+       return 1;
+fail:
+       *result = NULL;
+       free(f);
        return ret;
 }
 
-struct mp4 *mp4_open_read(const struct mp4_callback *cb)
+int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
 {
-       int ret;
-       struct mp4 *f = para_calloc(sizeof(struct mp4));
-
-       f->cb = cb;
-       ret = parse_root_atoms(f, false);
-       if (ret < 0) {
-               free(f);
-               return NULL;
-       }
-       return f;
+       return open_file(cb, false, result);
 }
 
 void mp4_close(struct mp4 *f)
@@ -1056,18 +1054,9 @@ int32_t mp4_num_samples(const struct mp4 *f)
        return total;
 }
 
-struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
+int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
 {
-       int ret;
-       struct mp4 *f = para_calloc(sizeof(struct mp4));
-
-       f->cb = cb;
-       ret = parse_root_atoms(f, true);
-       if (ret < 0) {
-               free(f);
-               return NULL;
-       }
-       return f;
+       return open_file(cb, true, result);
 }
 
 /**
diff --git a/mp4.h b/mp4.h
index 1142925d5dbf1c28e63f0275a99bf5f7a94cd6ae..783de19d631d4a09bb3c364d96451003eb253d00 100644 (file)
--- a/mp4.h
+++ b/mp4.h
@@ -20,15 +20,14 @@ struct mp4_metadata {
 struct mp4; /* opaque */
 
 int mp4_set_sample_position(struct mp4 *f, int32_t sample);
-struct mp4 *mp4_open_read(const struct mp4_callback *cb);
+int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result);
 void mp4_close(struct mp4 *f);
 int32_t mp4_get_sample_size(const struct mp4 *f, int sample);
 uint32_t mp4_get_sample_rate(const struct mp4 *f);
 uint32_t mp4_get_channel_count(const struct mp4 * f);
 int32_t mp4_num_samples(const struct mp4 *f);
 uint64_t mp4_get_duration(const struct mp4 *f);
-struct mp4 *mp4_open_meta(const struct mp4_callback *cb);
-
+int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result);
 struct mp4_metadata *mp4_get_meta(struct mp4 *f);
 int32_t mp4_meta_update(struct mp4 *f);
 char *mp4_meta_get_artist(const struct mp4 *f);