]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
mp4: Eliminate duplication between the two open functions.
[paraslash.git] / mp4.c
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);
 }
 
 /**