]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
paraslash 0.7.3
[paraslash.git] / mp4.c
diff --git a/mp4.c b/mp4.c
index 60969caeb7a73dc5cc5ceb1e5ac5eedd1c216575..5ca1307f680d366c155b2e98257ba807c5a4fb92 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -5,17 +5,43 @@
  * See file COPYING.
  */
 
+/** \file mp4.c Paraslash's internal mp4 parser. */
+
+/*
+ * This is a stripped down version of the former mp4ff library which used to be
+ * part of the faad decoder project but was removed from the faad code base in
+ * 2017. The original code has been cleaned up substantially and the public API
+ * has been documented. See the git commit log for details.
+ */
+
 #include <regex.h>
 
 #include "para.h"
+#include "error.h"
 #include "portable_io.h"
 #include "string.h"
 #include "mp4.h"
 
+/**
+ * The three states of the mp4 parser. The parser only loads the audio specific
+ * values and tables when it is in the second state.
+ */
+enum audio_track_state {
+       /** We haven't encountered an mp4a atom so far. */
+       ATS_INITIAL,
+       /** We have seen an mp4a atom but no subsequent trak atom yet. */
+       ATS_SEEN_MP4A,
+       /** A trak atom was seen *after* the mp4a atom. */
+       ATS_TRACK_CHANGE,
+};
+
 struct mp4_track {
-       bool is_audio;
-       uint16_t channelCount;
-       uint16_t sampleRate;
+       /* determines which atoms we still need to parse. */
+       enum audio_track_state state;
+
+       /* mp4a */
+       uint16_t channel_count;
+       uint16_t sample_rate;
 
        /* stsz */
        uint32_t stsz_sample_size;
@@ -25,605 +51,309 @@ struct mp4_track {
        /* stts */
        uint32_t stts_entry_count;
        uint32_t *stts_sample_count;
-       uint32_t *stts_sample_delta;
 
        /* stsc */
        uint32_t stsc_entry_count;
        uint32_t *stsc_first_chunk;
        uint32_t *stsc_samples_per_chunk;
-       uint32_t *stsc_sample_desc_index;
 
        /* stsc */
        uint32_t stco_entry_count;
        uint32_t *stco_chunk_offset;
 
-       uint32_t timeScale;
+       /* mdhd */
+       uint32_t time_scale;
        uint64_t duration;
 };
 
-#define MAX_TRACKS 1024
-
 struct mp4 {
        const struct mp4_callback *cb;
-       int64_t current_position;
 
        uint64_t moov_offset;
        uint64_t moov_size;
-       uint8_t last_atom;
-       uint64_t file_size;
-
-       uint32_t read_error;
-       uint32_t error;
-
-       /* incremental track index while reading the file */
-       int32_t total_tracks;
-
-       /* track data */
-       struct mp4_track *track[MAX_TRACKS];
+       uint64_t meta_offset;
+       uint32_t meta_size;
+       uint64_t ilst_offset;
+       uint32_t ilst_size;
+       uint64_t udta_offset;
+       uint32_t udta_size;
 
-       /* metadata */
+       uint8_t last_atom;
+       struct mp4_track track;
        struct mp4_metadata meta;
 };
 
-int32_t mp4_total_tracks(const struct mp4 *f)
-{
-       return f->total_tracks;
-}
-
-static int32_t read_data(struct mp4 *f, void *data, uint32_t size)
+/*
+ * Returns -E_MP4_READ, 0, or 1 on errors/EOF/success. Partial reads followed
+ * by EOF or read errors are treated as errors.
+ */
+static int read_data(struct mp4 *f, void *data, size_t size)
 {
-       int32_t result = 1;
-
-       result = f->cb->read(f->cb->user_data, data, size);
-
-       if (result < size)
-               f->read_error++;
-
-       f->current_position += size;
-
-       return result;
+       while (size > 0) {
+               ssize_t ret = f->cb->read(f->cb->user_data, data, size);
+               if (ret < 0 && errno == EINTR)
+                       continue;
+               /* regard EAGAIN as an error as reads should be blocking. */
+               if (ret <= 0)
+                       return ret < 0? -E_MP4_READ : 0;
+               size -= ret;
+       }
+       return 1;
 }
 
-static uint64_t read_int64(struct mp4 *f)
+static int read_int64(struct mp4 *f, uint64_t *result)
 {
        uint8_t data[8];
+       int ret = read_data(f, data, 8);
 
-       read_data(f, data, 8);
-       return read_u64_be(data);
+       if (ret > 0)
+               *result = read_u64_be(data);
+       return ret;
 }
 
-static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
-               int8_t a2, int8_t b2, int8_t c2, int8_t d2)
+static int read_int32(struct mp4 *f, uint32_t *result)
 {
-       return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
-}
-enum atoms {
-       /* atoms with subatoms */
-       ATOM_MOOV = 1,
-       ATOM_TRAK = 2,
-       ATOM_EDTS = 3,
-       ATOM_MDIA = 4,
-       ATOM_MINF = 5,
-       ATOM_STBL = 6,
-       ATOM_UDTA = 7,
-       ATOM_ILST = 8, /* iTunes Metadata list */
-       ATOM_TITLE = 9,
-       ATOM_ARTIST = 10,
-       ATOM_WRITER = 11,
-       ATOM_ALBUM = 12,
-       ATOM_DATE = 13,
-       ATOM_TOOL = 14,
-       ATOM_COMMENT = 15,
-       ATOM_GENRE1 = 16,
-       ATOM_TRACK = 17,
-       ATOM_DISC = 18,
-       ATOM_COMPILATION = 19,
-       ATOM_GENRE2 = 20,
-       ATOM_TEMPO = 21,
-       ATOM_COVER = 22,
-       ATOM_DRMS = 23,
-       ATOM_SINF = 24,
-       ATOM_SCHI = 25,
-
-       SUBATOMIC = 128,
-
-       /* atoms without subatoms */
-       ATOM_FTYP = 129,
-       ATOM_MDAT = 130,
-       ATOM_MVHD = 131,
-       ATOM_TKHD = 132,
-       ATOM_TREF = 133,
-       ATOM_MDHD = 134,
-       ATOM_VMHD = 135,
-       ATOM_SMHD = 136,
-       ATOM_HMHD = 137,
-       ATOM_STSD = 138,
-       ATOM_STTS = 139,
-       ATOM_STSZ = 140,
-       ATOM_STZ2 = 141,
-       ATOM_STCO = 142,
-       ATOM_STSC = 143,
-       ATOM_MP4A = 144,
-       ATOM_MP4V = 145,
-       ATOM_MP4S = 146,
-       ATOM_ESDS = 147,
-       ATOM_META = 148, /* iTunes Metadata box */
-       ATOM_NAME = 149, /* iTunes Metadata name box */
-       ATOM_DATA = 150, /* iTunes Metadata data box */
-       ATOM_CTTS = 151,
-       ATOM_FRMA = 152,
-       ATOM_IVIV = 153,
-       ATOM_PRIV = 154,
-       ATOM_USER = 155,
-       ATOM_KEY = 156,
-       ATOM_ALBUM_ARTIST = 157,
-       ATOM_CONTENTGROUP = 158,
-       ATOM_LYRICS = 159,
-       ATOM_DESCRIPTION = 160,
-       ATOM_NETWORK = 161,
-       ATOM_SHOW = 162,
-       ATOM_EPISODENAME = 163,
-       ATOM_SORTTITLE = 164,
-       ATOM_SORTALBUM = 165,
-       ATOM_SORTARTIST = 166,
-       ATOM_SORTALBUMARTIST = 167,
-       ATOM_SORTWRITER = 168,
-       ATOM_SORTSHOW = 169,
-       ATOM_SEASON = 170,
-       ATOM_EPISODE = 171,
-       ATOM_PODCAST = 172,
-
-       ATOM_UNKNOWN = 255
-};
+       uint8_t data[4];
+       int ret = read_data(f, data, 4);
 
-#define ATOM_FREE ATOM_UNKNOWN
-#define ATOM_SKIP ATOM_UNKNOWN
-
-#define COPYRIGHT_SYMBOL ((int8_t)0xA9)
+       if (ret > 0)
+               *result = read_u32_be(data);
+       return ret;
+}
 
-static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
+static int read_int16(struct mp4 *f, uint16_t *result)
 {
-       if (a == 'm') {
-               if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
-                       return ATOM_MOOV;
-               else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
-                       return ATOM_MINF;
-               else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
-                       return ATOM_MDIA;
-               else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
-                       return ATOM_MDAT;
-               else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
-                       return ATOM_MDHD;
-               else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
-                       return ATOM_MVHD;
-               else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
-                       return ATOM_MP4A;
-               else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
-                       return ATOM_MP4V;
-               else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
-                       return ATOM_MP4S;
-               else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
-                       return ATOM_META;
-       } else if (a == 't') {
-               if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
-                       return ATOM_TRAK;
-               else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
-                       return ATOM_TKHD;
-               else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
-                       return ATOM_TREF;
-               else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
-                       return ATOM_TRACK;
-               else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
-                       return ATOM_TEMPO;
-               else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
-                       return ATOM_NETWORK;
-               else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
-                       return ATOM_SHOW;
-               else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
-                       return ATOM_EPISODENAME;
-               else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
-                       return ATOM_SEASON;
-               else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
-                       return ATOM_EPISODE;
-       } else if (a == 's') {
-               if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
-                       return ATOM_STBL;
-               else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
-                       return ATOM_SMHD;
-               else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
-                       return ATOM_STSD;
-               else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
-                       return ATOM_STTS;
-               else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
-                       return ATOM_STCO;
-               else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
-                       return ATOM_STSC;
-               else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
-                       return ATOM_STSZ;
-               else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
-                       return ATOM_STZ2;
-               else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
-                       return ATOM_SKIP;
-               else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
-                       return ATOM_SINF;
-               else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
-                       return ATOM_SCHI;
-               else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
-                       return ATOM_SORTTITLE;
-               else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
-                       return ATOM_SORTALBUM;
-               else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
-                       return ATOM_SORTARTIST;
-               else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
-                       return ATOM_SORTALBUMARTIST;
-               else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
-                       return ATOM_SORTWRITER;
-               else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
-                       return ATOM_SORTSHOW;
-       } else if (a == COPYRIGHT_SYMBOL) {
-               if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
-                       return ATOM_TITLE;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
-                       return ATOM_ARTIST;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
-                       return ATOM_WRITER;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
-                       return ATOM_ALBUM;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
-                       return ATOM_DATE;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
-                       return ATOM_TOOL;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
-                       return ATOM_COMMENT;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
-                       return ATOM_GENRE1;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
-                       return ATOM_CONTENTGROUP;
-               else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
-                       return ATOM_LYRICS;
-       }
+       uint8_t data[2];
+       int ret = read_data(f, data, 2);
 
-       if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
-               return ATOM_EDTS;
-       else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
-               return ATOM_ESDS;
-       else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
-               return ATOM_FTYP;
-       else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
-               return ATOM_FREE;
-       else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
-               return ATOM_HMHD;
-       else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
-               return ATOM_VMHD;
-       else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
-               return ATOM_UDTA;
-       else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
-               return ATOM_ILST;
-       else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
-               return ATOM_NAME;
-       else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
-               return ATOM_DATA;
-       else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
-               return ATOM_DISC;
-       else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
-               return ATOM_GENRE2;
-       else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
-               return ATOM_COVER;
-       else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
-               return ATOM_COMPILATION;
-       else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
-               return ATOM_CTTS;
-       else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
-               return ATOM_DRMS;
-       else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
-               return ATOM_FRMA;
-       else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
-               return ATOM_PRIV;
-       else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
-               return ATOM_IVIV;
-       else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
-               return ATOM_USER;
-       else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
-               return ATOM_KEY;
-       else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
-               return ATOM_ALBUM_ARTIST;
-       else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
-               return ATOM_DESCRIPTION;
-       else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
-               return ATOM_PODCAST;
-       else
-               return ATOM_UNKNOWN;
+       if (ret > 0)
+               *result = read_u16_be(data);
+       return ret;
 }
 
-/* read atom header, return atom size, atom size is with header included */
-static uint64_t atom_read_header(struct mp4 *f, uint8_t * atom_type,
-                               uint8_t * header_size)
-{
-       uint64_t size;
-       int32_t ret;
-       int8_t atom_header[8];
+/** A macro defining the atoms we care about. It gets expanded twice. */
+#define ATOM_ITEMS \
+       ATOM_ITEM(MOOV, 'm', 'o', 'o', 'v') /* movie (top-level container) */ \
+       ATOM_ITEM(TRAK, 't', 'r', 'a', 'k') /* container for a single track */ \
+       ATOM_ITEM(MDIA, 'm', 'd', 'i', 'a') /* media information */ \
+       ATOM_ITEM(MINF, 'm', 'i', 'n', 'f') /* extends mdia */ \
+       ATOM_ITEM(STBL, 's', 't', 'b', 'l') /* sample table container */ \
+       ATOM_ITEM(UDTA, 'u', 'd', 't', 'a') /* user data */ \
+       ATOM_ITEM(ILST, 'i', 'l', 's', 't') /* iTunes Metadata list */ \
+       ATOM_ITEM(ARTIST, 0xa9, 'A', 'R', 'T') /* artist */ \
+       ATOM_ITEM(TITLE, 0xa9, 'n', 'a', 'm') /* title */ \
+       ATOM_ITEM(ALBUM, 0xa9, 'a', 'l', 'b') /* album */ \
+       ATOM_ITEM(DATE, 0xa9, 'd', 'a', 'y') /* date */ \
+       ATOM_ITEM(COMMENT, 0xa9, 'c', 'm', 't') /* comment */ \
+       ATOM_ITEM(MDHD, 'm', 'd', 'h', 'd') /* track header */ \
+       ATOM_ITEM(STSD, 's', 't', 's', 'd') /* sample description box */ \
+       ATOM_ITEM(STTS, 's', 't', 't', 's') /* time to sample box */ \
+       ATOM_ITEM(STSZ, 's', 't', 's', 'z') /* sample size box */ \
+       ATOM_ITEM(STCO, 's', 't', 'c', 'o') /* chunk offset box */ \
+       ATOM_ITEM(STSC, 's', 't', 's', 'c') /* sample to chunk box */ \
+       ATOM_ITEM(MP4A, 'm', 'p', '4', 'a') /* mp4 audio */ \
+       ATOM_ITEM(META, 'm', 'e', 't', 'a') /* iTunes Metadata box */ \
+       ATOM_ITEM(DATA, 'd', 'a', 't', 'a') /* iTunes Metadata data box */ \
+
+/** For the C enumeration we concatenate ATOM_ with the first argument. */
+#define ATOM_ITEM(_name, a, b, c, d) ATOM_ ## _name,
+/** The enumeration of interesting atoms. */
+enum atom {ATOM_ITEMS};
+#undef ATOM_ITEM
+
+/** A cpp version of read_u32_be(). */
+#define ATOM_VALUE(a, b, c, d) ((a << 24) + (b << 16) + (c << 8) + d)
+
+static uint8_t atom_name_to_type(uint8_t *p)
+{
+       /** Expands to an instance of the following unnamed structure. */
+       #define ATOM_ITEM(_name, a, b, c, d) \
+               {.name = # _name, .val = ATOM_VALUE(a, b, c, d)},
+       static const struct {
+               const char *name;
+               uint32_t val;
+       } atom_table[] = {ATOM_ITEMS};
+       #undef ATOM_ITEM
+       uint32_t val = read_u32_be(p);
+
+       for (uint8_t n = 0; n < ARRAY_SIZE(atom_table); n++)
+               if (val == atom_table[n].val)
+                       return n;
+       return 255;
+}
+
+/* read atom header, atom size is returned with header included. */
+static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
+               uint8_t *header_size, uint64_t *atom_size)
+{
+       uint32_t size;
+       int ret;
+       uint8_t atom_header[8];
 
        ret = read_data(f, atom_header, 8);
-       if (ret != 8)
-               return 0;
-
+       if (ret <= 0)
+               return ret;
        size = read_u32_be(atom_header);
-       *header_size = 8;
-
-       /* check for 64 bit atom size */
-       if (size == 1) {
-               *header_size = 16;
-               size = read_int64(f);
-       }
-       *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
-               atom_header[6], atom_header[7]);
-       return size;
-}
-
-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;
+       if (size == 1) { /* 64 bit atom size */
+               if (header_size)
+                       *header_size = 16;
+               ret = read_int64(f, atom_size);
+               if (ret <= 0)
+                       return ret;
+       } else {
+               if (header_size)
+                       *header_size = 8;
+               *atom_size = size;
        }
+       *atom_type = atom_name_to_type(atom_header + 4);
+       return 1;
 }
 
-static int32_t set_position(struct mp4 *f, int64_t position)
-{
-       f->cb->seek(f->cb->user_data, position);
-       f->current_position = position;
-
-       return 0;
-}
-
-static void track_add(struct mp4 *f)
+static off_t get_position(const struct mp4 *f)
 {
-       f->total_tracks++;
-
-       if (f->total_tracks > MAX_TRACKS) {
-               f->total_tracks = 0;
-               f->error++;
-               return;
-       }
-       f->track[f->total_tracks - 1] = para_calloc(sizeof(struct mp4_track));
+       return f->cb->seek(f->cb->user_data, 0, SEEK_CUR);
 }
 
-static uint8_t read_int8(struct mp4 *f)
+static void set_position(struct mp4 *f, off_t position)
 {
-       uint8_t output;
-       read_data(f, &output, 1);
-       return output;
+       f->cb->seek(f->cb->user_data, position, SEEK_SET);
 }
 
-static uint32_t read_int24(struct mp4 *f)
+static void skip_bytes(struct mp4 *f, off_t num_skip)
 {
-       int8_t data[4];
-
-       read_data(f, data, 3);
-       return read_u24_be(data);
+       f->cb->seek(f->cb->user_data, num_skip, SEEK_CUR);
 }
 
-static uint32_t read_int32(struct mp4 *f)
+static int read_stsz(struct mp4 *f)
 {
-       int8_t data[4];
+       int ret;
+       struct mp4_track *t = &f->track;
 
-       read_data(f, data, 4);
-       return read_u32_be(data);
-}
-
-static int32_t read_stsz(struct mp4 *f)
-{
-       int32_t i;
-       struct mp4_track *t;
-
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-       t->stsz_sample_size = read_int32(f);
-       t->stsz_sample_count = read_int32(f);
+       if (t->state != ATS_SEEN_MP4A || t->stsz_table)
+               return 1;
+       skip_bytes(f, 4); /* version (1), flags (3) */
+       ret = read_int32(f, &t->stsz_sample_size);
+       if (ret <= 0)
+               return ret;
+       ret = read_int32(f, &t->stsz_sample_count);
+       if (ret <= 0)
+               return ret;
        if (t->stsz_sample_size != 0)
-               return 0;
-       t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
-       for (i = 0; i < t->stsz_sample_count && !f->read_error; i++)
-               t->stsz_table[i] = read_int32(f);
-       return 0;
-}
-
-static int32_t read_stts(struct mp4 *f)
-{
-       int32_t i;
-       struct mp4_track *t;
-
-       /* CVE-2017-9223 */
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
-       if (t->stts_entry_count)
-               return 0;
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-       t->stts_entry_count = read_int32(f);
-
-       t->stts_sample_count = para_malloc(t->stts_entry_count
-               * sizeof(int32_t));
-       t->stts_sample_delta = para_malloc(t->stts_entry_count
-               * sizeof (int32_t));
-       /* CVE-2017-9254 */
-       for (i = 0; i < t->stts_entry_count && !f->read_error; i++) {
-               t->stts_sample_count[i] = read_int32(f);
-               t->stts_sample_delta[i] = read_int32(f);
+               return 1;
+       t->stsz_table = arr_alloc(t->stsz_sample_count, sizeof(int32_t));
+       for (uint32_t n = 0; n < t->stsz_sample_count; n++) {
+               ret = read_int32(f, &t->stsz_table[n]);
+               if (ret <= 0)
+                       return ret;
        }
        return 1;
 }
 
-static int32_t read_stsc(struct mp4 *f)
+static int read_stts(struct mp4 *f)
 {
-       int32_t i;
-       struct mp4_track *t;
-
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
-
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-       t->stsc_entry_count = read_int32(f);
-       t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
-       t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
-               * sizeof (int32_t));
-       t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
-               sizeof (int32_t));
+       int ret;
+       struct mp4_track *t = &f->track;
 
-       /* CVE-2017-9255 */
-       for (i = 0; i < t->stsc_entry_count && !f->read_error; i++) {
-               t->stsc_first_chunk[i] = read_int32(f);
-               t->stsc_samples_per_chunk[i] = read_int32(f);
-               t->stsc_sample_desc_index[i] = read_int32(f);
+       if (t->state != ATS_SEEN_MP4A || t->stts_sample_count)
+               return 1;
+       skip_bytes(f, 4); /* version (1), flags (3) */
+       ret = read_int32(f, &t->stts_entry_count);
+       if (ret <= 0)
+               return ret;
+       t->stts_sample_count = arr_alloc(t->stts_entry_count, sizeof(int32_t));
+       for (uint32_t n = 0; n < t->stts_entry_count; n++) {
+               ret = read_int32(f, &t->stts_sample_count[n]);
+               if (ret <= 0)
+                       return ret;
+               skip_bytes(f, 4); /* sample delta */
        }
-       return 0;
-}
-
-static int32_t read_stco(struct mp4 *f)
-{
-       int32_t i;
-       struct mp4_track *t;
-
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
-
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-       t->stco_entry_count = read_int32(f);
-       t->stco_chunk_offset = para_malloc(t->stco_entry_count
-               * sizeof(int32_t));
-       /* CVE-2017-9256 */
-       for (i = 0; i < t->stco_entry_count && !f->read_error; i++)
-               t->stco_chunk_offset[i] = read_int32(f);
-       return 0;
+       return 1;
 }
 
-static uint16_t read_int16(struct mp4 *f)
+static int read_stsc(struct mp4 *f)
 {
-       int8_t data[2];
+       int ret;
+       struct mp4_track *t = &f->track;
 
-       read_data(f, data, 2);
-       return read_u16_be(data);
+       if (t->state != ATS_SEEN_MP4A)
+               return 1;
+       if (t->stsc_first_chunk || t->stsc_samples_per_chunk)
+               return 1;
+       skip_bytes(f, 4); /* version (1), flags (3) */
+       ret = read_int32(f, &t->stsc_entry_count);
+       if (ret <= 0)
+               return ret;
+       t->stsc_first_chunk = arr_alloc(t->stsc_entry_count, sizeof(int32_t));
+       t->stsc_samples_per_chunk = arr_alloc(t->stsc_entry_count,
+               sizeof (int32_t));
+       for (uint32_t n = 0; n < t->stsc_entry_count; n++) {
+               ret = read_int32(f, &t->stsc_first_chunk[n]);
+               if (ret <= 0)
+                       return ret;
+               ret = read_int32(f, &t->stsc_samples_per_chunk[n]);
+               if (ret <= 0)
+                       return ret;
+               skip_bytes(f, 4); /* sample desc index */
+       }
+       return 1;
 }
 
-static int32_t read_mp4a(struct mp4 *f)
+static int read_stco(struct mp4 *f)
 {
-       int32_t i;
-       uint8_t atom_type = 0;
-       uint8_t header_size = 0;
-       struct mp4_track *t;
-
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
+       int ret;
+       struct mp4_track *t = &f->track;
 
-       for (i = 0; i < 6; i++)
-               read_int8(f); /* reserved */
-       /* data_reference_index */ read_int16(f);
-
-       read_int32(f);  /* reserved */
-       read_int32(f);  /* reserved */
-
-       t->channelCount = read_int16(f);
-       read_int16(f);
-
-       read_int16(f);
-       read_int16(f);
-
-       t->sampleRate = read_int16(f);
-
-       read_int16(f);
-
-       atom_read_header(f, &atom_type, &header_size);
-       return 0;
+       if (t->state != ATS_SEEN_MP4A || t->stco_chunk_offset)
+               return 1;
+       skip_bytes(f, 4); /* version (1), flags (3) */
+       ret = read_int32(f, &t->stco_entry_count);
+       if (ret <= 0)
+               return ret;
+       t->stco_chunk_offset = arr_alloc(t->stco_entry_count, sizeof(int32_t));
+       for (uint32_t n = 0; n < t->stco_entry_count; n++) {
+               ret = read_int32(f, &t->stco_chunk_offset[n]);
+               if (ret <= 0)
+                       return ret;
+       }
+       return 1;
 }
 
-static int32_t read_stsd(struct mp4 *f)
+static int read_stsd(struct mp4 *f)
 {
-       int32_t i, entry_count;
-       uint8_t header_size = 0;
-       struct mp4_track *t;
-
-       /* CVE-2017-9218 */
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
+       int ret;
+       uint32_t entry_count;
 
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-
-       entry_count = read_int32(f);
-
-       /* CVE-2017-9253 */
-       for (i = 0; i < entry_count && !f->read_error; i++) {
+       if (f->track.state != ATS_INITIAL)
+               return 1;
+       skip_bytes(f, 4); /* version (1), flags (3) */
+       ret = read_int32(f, &entry_count);
+       if (ret <= 0)
+               return ret;
+       for (uint32_t n = 0; n < entry_count; n++) {
                uint64_t skip = get_position(f);
                uint64_t size;
                uint8_t atom_type = 0;
-               size = atom_read_header(f, &atom_type, &header_size);
+               ret = atom_read_header(f, &atom_type, NULL, &size);
+               if (ret <= 0)
+                       return ret;
                skip += size;
-               t->is_audio = atom_type == ATOM_MP4A;
-               if (t->is_audio)
-                       read_mp4a(f);
+               if (atom_type == ATOM_MP4A) {
+                       f->track.state = ATS_SEEN_MP4A;
+                       /* reserved (6), data reference index (2), reserved (8) */
+                       skip_bytes(f, 16);
+                       ret = read_int16(f, &f->track.channel_count);
+                       if (ret <= 0)
+                               return ret;
+                       skip_bytes(f, 6);
+                       ret = read_int16(f, &f->track.sample_rate);
+                       if (ret <= 0)
+                               return ret;
+               }
                set_position(f, skip);
        }
-
-       return 0;
-}
-
-static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
-               const char *value, int32_t len)
-{
-       meta->tags = para_realloc(meta->tags,
-               (meta->count + 1) * sizeof(struct mp4_tag));
-       meta->tags[meta->count].item = para_strdup(item);
-       meta->tags[meta->count].len = len;
-       if (len >= 0) {
-               meta->tags[meta->count].value = para_malloc(len + 1);
-               memcpy(meta->tags[meta->count].value, value, len);
-               meta->tags[meta->count].value[len] = 0;
-       } else {
-               meta->tags[meta->count].value = para_strdup(value);
-       }
-       meta->count++;
        return 1;
 }
 
-static char *read_string(struct mp4 *f, uint32_t length)
-{
-       char *str = para_malloc(length + 1);
-       if ((uint32_t)read_data(f, str, length) != length) {
-               free(str);
-               str = NULL;
-       } else
-               str[length] = 0;
-       return str;
-}
-
 static const char *get_metadata_name(uint8_t atom_type)
 {
        switch (atom_type) {
@@ -636,79 +366,102 @@ static const char *get_metadata_name(uint8_t atom_type)
        }
 }
 
-static void parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
+static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
 {
+       int ret;
        uint64_t subsize, sumsize;
-       char *data = NULL;
+       char *value = NULL;
        uint32_t len = 0;
        uint64_t destpos;
+       struct mp4_tag *tag;
 
        for (
                sumsize = 0;
-               sumsize < size && !f->read_error; /* CVE-2017-9222 */
+               sumsize < size;
                set_position(f, destpos), sumsize += subsize
        ) {
                uint8_t atom_type;
                uint8_t header_size = 0;
-               subsize = atom_read_header(f, &atom_type, &header_size);
+               ret = atom_read_header(f, &atom_type, &header_size, &subsize);
+               if (ret <= 0)
+                       goto fail;
                destpos = get_position(f) + subsize - header_size;
                if (atom_type != ATOM_DATA)
                        continue;
-               read_int8(f); /* version */
-               read_int24(f);  /* flags */
-               read_int32(f);  /* reserved */
-               free(data);
-               data = read_string(f, subsize - (header_size + 8));
+               skip_bytes(f, 8); /* version (1), flags (3), reserved (4) */
+               ret = -E_MP4_CORRUPT;
+               if (subsize < header_size + 8 || subsize > UINT_MAX)
+                       goto fail;
                len = subsize - (header_size + 8);
+               free(value);
+               value = alloc(len + 1);
+               ret = read_data(f, value, len);
+               if (ret <= 0)
+                       goto fail;
+               value[len] = '\0';
        }
-       if (!data)
-               return;
-       tag_add_field(&f->meta, get_metadata_name(parent), data, len);
-       free(data);
+       if (!value)
+               return -E_MP4_CORRUPT;
+       f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
+               * sizeof(struct mp4_tag));
+       tag = f->meta.tags + f->meta.count;
+       tag->item = para_strdup(get_metadata_name(parent));
+       tag->value = value;
+       f->meta.count++;
+       return 1;
+fail:
+       free(value);
+       return ret;
 }
 
-static int32_t read_mdhd(struct mp4 *f)
+static int read_mdhd(struct mp4 *f)
 {
+       int ret;
        uint32_t version;
-       struct mp4_track *t;
-
-       /* CVE-2017-9221 */
-       if (f->total_tracks == 0)
-               return f->error++;
-       t = f->track[f->total_tracks - 1];
+       struct mp4_track *t = &f->track;
 
-       version = read_int32(f);
+       if (t->state != ATS_INITIAL)
+               return 1;
+       ret = read_int32(f, &version);
+       if (ret <= 0)
+               return ret;
        if (version == 1) {
-               read_int64(f); //creation-time
-               read_int64(f); //modification-time
-               t->timeScale = read_int32(f); //timescale
-               t->duration = read_int64(f); //duration
-       } else { //version == 0
+               skip_bytes(f, 16); /* creation time (8), modification time (8) */
+               ret = read_int32(f, &t->time_scale);
+               if (ret <= 0)
+                       return ret;
+               ret = read_int64(f, &t->duration);
+               if (ret <= 0)
+                       return ret;
+       } else { /* version == 0 */
                uint32_t temp;
 
-               read_int32(f);  //creation-time
-               read_int32(f);  //modification-time
-               t->timeScale = read_int32(f); //timescale
-               temp = read_int32(f);
+               skip_bytes(f, 8); /* creation time (4), modification time (4) */
+               ret = read_int32(f, &t->time_scale);
+               if (ret <= 0)
+                       return ret;
+               ret = read_int32(f, &temp);
+               if (ret <= 0)
+                       return ret;
                t->duration = (temp == (uint32_t) (-1))?
                        (uint64_t) (-1) : (uint64_t) (temp);
        }
-       read_int16(f);
-       read_int16(f);
+       skip_bytes(f, 4);
        return 1;
 }
 
-static int32_t read_ilst(struct mp4 *f, int32_t size)
+static int read_ilst(struct mp4 *f, int32_t size)
 {
+       int ret;
        uint64_t sumsize = 0;
 
        while (sumsize < size) {
                uint8_t atom_type;
                uint64_t subsize, destpos;
                uint8_t header_size = 0;
-               subsize = atom_read_header(f, &atom_type, &header_size);
-               if (subsize == 0)
-                       break;
+               ret = atom_read_header(f, &atom_type, &header_size, &subsize);
+               if (ret <= 0)
+                       return ret;
                destpos = get_position(f) + subsize - header_size;
                switch (atom_type) {
                case ATOM_ARTIST:
@@ -716,748 +469,539 @@ static int32_t read_ilst(struct mp4 *f, int32_t size)
                case ATOM_ALBUM:
                case ATOM_COMMENT:
                case ATOM_DATE:
-                       parse_tag(f, atom_type, subsize - header_size);
+                       ret = parse_tag(f, atom_type, subsize - header_size);
+                       if (ret <= 0)
+                               return ret;
                }
                set_position(f, destpos);
                sumsize += subsize;
        }
-
-       return 0;
+       return 1;
 }
 
-static int32_t read_meta(struct mp4 *f, uint64_t size)
+static int read_meta(struct mp4 *f, uint64_t size)
 {
+       int ret;
        uint64_t subsize, sumsize = 0;
        uint8_t atom_type;
        uint8_t header_size = 0;
 
-       read_int8(f); /* version */
-       read_int24(f);  /* flags */
-
+       skip_bytes(f, 4); /* version (1), flags (3) */
        while (sumsize < (size - (header_size + 4))) {
-               subsize = atom_read_header(f, &atom_type, &header_size);
+               ret = atom_read_header(f, &atom_type, &header_size, &subsize);
+               if (ret <= 0)
+                       return ret;
                if (subsize <= header_size + 4)
                        return 1;
-               if (atom_type == ATOM_ILST)
-                       read_ilst(f, subsize - (header_size + 4));
-               else
+               if (atom_type == ATOM_ILST) {
+                       f->ilst_offset = get_position(f) - header_size;
+                       f->ilst_size = subsize;
+                       ret = read_ilst(f, subsize - (header_size + 4));
+                       if (ret <= 0)
+                               return ret;
+               } else
                        set_position(f, get_position(f) + subsize - header_size);
                sumsize += subsize;
        }
-
-       return 0;
+       return 1;
 }
 
-static int32_t atom_read(struct mp4 *f, int32_t size, uint8_t atom_type)
+static bool need_atom(uint8_t atom_type, bool meta_only)
 {
-       uint64_t dest_position = get_position(f) + size - 8;
-       if (atom_type == ATOM_STSZ) {
-               /* sample size box */
-               read_stsz(f);
-       } else if (atom_type == ATOM_STTS) {
-               /* time to sample box */
-               read_stts(f);
-       } else if (atom_type == ATOM_STSC) {
-               /* sample to chunk box */
-               read_stsc(f);
-       } else if (atom_type == ATOM_STCO) {
-               /* chunk offset box */
-               read_stco(f);
-       } else if (atom_type == ATOM_STSD) {
-               /* sample description box */
-               read_stsd(f);
-       } else if (atom_type == ATOM_MDHD) {
-               /* track header */
-               read_mdhd(f);
-       } else if (atom_type == ATOM_META) {
-               /* iTunes Metadata box */
-               read_meta(f, size);
+       /* 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:
+       case ATOM_UDTA:
+               return true;
        }
-
-       set_position(f, dest_position);
-       return 0;
+       /* 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:
+               return true;
+       }
+       return false;
 }
 
 /* parse atoms that are sub atoms of other atoms */
-static int32_t parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
+static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
 {
-       uint64_t size;
-       uint8_t atom_type = 0;
-       uint64_t counted_size = 0;
-       uint8_t header_size = 0;
-
-       while (counted_size < total_size) {
-               size = atom_read_header(f, &atom_type, &header_size);
-               counted_size += size;
+       int ret;
+       uint64_t dest, size, end = get_position(f) + total_size;
 
-               /* check for end of file */
+       for (dest = get_position(f); dest < end; set_position(f, dest)) {
+               uint8_t header_size, atom_type;
+               ret = atom_read_header(f, &atom_type, &header_size, &size);
+               if (ret <= 0)
+                       return ret;
                if (size == 0)
-                       break;
-
-               /* we're starting to read a new track, update index,
-                * so that all data and tables get written in the right place
-                */
-               if (atom_type == ATOM_TRAK)
-                       track_add(f);
-               /* parse subatoms */
-               if (meta_only && !need_parse_when_meta_only(atom_type)) {
-                       set_position(f, get_position(f) + size - header_size);
-               } else if (atom_type < SUBATOMIC) {
-                       parse_sub_atoms(f, size - header_size, meta_only);
-               } else {
-                       atom_read(f, (uint32_t) size, atom_type);
+                       return -E_MP4_CORRUPT;
+               dest = get_position(f) + size - header_size;
+               if (atom_type == ATOM_TRAK && f->track.state == ATS_SEEN_MP4A) {
+                       f->track.state = ATS_TRACK_CHANGE;
+                       continue;
                }
-       }
-
-       return 0;
-}
-
-/* parse root atoms */
-static int32_t parse_atoms(struct mp4 *f, int meta_only)
-{
-       uint64_t size;
-       uint8_t atom_type = 0;
-       uint8_t header_size = 0;
-
-       f->file_size = 0;
-       f->read_error = 0;
-
-       while ((size =
-               atom_read_header(f, &atom_type, &header_size)) != 0) {
-               f->file_size += size;
-               f->last_atom = atom_type;
-
-               if (atom_type == ATOM_MOOV && size > header_size) {
-                       f->moov_offset = get_position(f) - header_size;
-                       f->moov_size = size;
+               if (atom_type == ATOM_UDTA) {
+                       f->udta_offset = get_position(f) - header_size;
+                       f->udta_size = size;
                }
-
-               /* parse subatoms */
-               if (meta_only && !need_parse_when_meta_only(atom_type)) {
-                       set_position(f, get_position(f) + size - header_size);
-               } else if (atom_type < SUBATOMIC) {
-                       parse_sub_atoms(f, size - header_size, meta_only);
-               } else {
-                       /* skip this atom */
-                       set_position(f, get_position(f) + size - header_size);
+               if (!need_atom(atom_type, meta_only))
+                       continue;
+               switch (atom_type) {
+               case ATOM_STSZ: ret = read_stsz(f); break;
+               case ATOM_STTS: ret = read_stts(f); break;
+               case ATOM_STSC: ret = read_stsc(f); break;
+               case ATOM_STCO: ret = read_stco(f); break;
+               case ATOM_STSD: ret = read_stsd(f); break;
+               case ATOM_MDHD: ret = read_mdhd(f); break;
+               case ATOM_META:
+                       f->meta_offset = get_position(f) - header_size;
+                       f->meta_size = size;
+                       ret = read_meta(f, size);
+                       break;
+               default:
+                       ret = parse_sub_atoms(f, size - header_size, meta_only);
                }
+               if (ret <= 0)
+                       return ret;
        }
-
-       return 0;
-}
-
-struct mp4 *mp4_open_read(const struct mp4_callback *cb)
-{
-       struct mp4 *f = para_calloc(sizeof(struct mp4));
-
-       f->cb = cb;
-       parse_atoms(f, 0);
-       if (f->error) {
-               free(f);
-               f = NULL;
-       }
-       return f;
+       return 1;
 }
 
+/**
+ * Deallocate all resources associated with an mp4 file handle.
+ *
+ * \param f File handle returned by \ref mp4_open() or \ref mp4_open_meta().
+ *
+ * This frees the metadata items and various tables which were allocated when
+ * the file was opened. The given file handle must not be NULL.
+ */
 void mp4_close(struct mp4 *f)
 {
-       int32_t i;
-
-       for (i = 0; i < f->total_tracks; i++) {
-               if (f->track[i]) {
-                       free(f->track[i]->stsz_table);
-                       free(f->track[i]->stts_sample_count);
-                       free(f->track[i]->stts_sample_delta);
-                       free(f->track[i]->stsc_first_chunk);
-                       free(f->track[i]->stsc_samples_per_chunk);
-                       free(f->track[i]->stsc_sample_desc_index);
-                       free(f->track[i]->stco_chunk_offset);
-                       free(f->track[i]);
-               }
-       }
-       for (i = 0; i < f->meta.count; i++) {
-               free(f->meta.tags[i].item);
-               free(f->meta.tags[i].value);
+       free(f->track.stsz_table);
+       free(f->track.stts_sample_count);
+       free(f->track.stsc_first_chunk);
+       free(f->track.stsc_samples_per_chunk);
+       free(f->track.stco_chunk_offset);
+       for (uint32_t n = 0; n < f->meta.count; n++) {
+               free(f->meta.tags[n].item);
+               free(f->meta.tags[n].value);
        }
        free(f->meta.tags);
        free(f);
 }
 
-static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
-               int32_t sample, int32_t *chunk_sample, int32_t *chunk)
+static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
 {
-       int32_t total_entries = 0;
-       int32_t chunk2entry;
-       int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
-
-       *chunk_sample = 0;
-       *chunk = 1;
-       if (f->track[track] == NULL) {
-               return -1;
-       }
-
-       total_entries = f->track[track]->stsc_entry_count;
-
-       chunk1 = 1;
-       chunk1samples = 0;
-       chunk2entry = 0;
-
-       do {
-               chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
-               *chunk = chunk2 - chunk1;
-               range_samples = *chunk * chunk1samples;
-
-               if (sample < total + range_samples)
-                       break;
-
-               chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
-               chunk1 = chunk2;
+       int ret;
+       uint64_t size;
+       uint8_t atom_type, header_size;
+       struct mp4 *f = zalloc(sizeof(*f));
 
-               if (chunk2entry < total_entries) {
-                       chunk2entry++;
-                       total += range_samples;
+       f->cb = cb;
+       while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
+               f->last_atom = atom_type;
+               if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
+                       set_position(f, get_position(f) + size - header_size);
+                       continue;
                }
-       } while (chunk2entry < total_entries);
-
-       if (chunk1samples)
-               *chunk = (sample - total) / chunk1samples + chunk1;
-       else
-               *chunk = 1;
-
-       *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
-
-       return 0;
-}
-
-static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
-               int32_t chunk)
-{
-       const struct mp4_track *p_track = f->track[track];
-
-       if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count)) {
-               return p_track->stco_chunk_offset[p_track->stco_entry_count -
-                                                 1];
-       } else if (p_track->stco_entry_count) {
-               return p_track->stco_chunk_offset[chunk - 1];
-       } else {
-               return 8;
+               f->moov_offset = get_position(f) - header_size;
+               f->moov_size = size;
+               ret = parse_sub_atoms(f, size - header_size, meta_only);
+               if (ret <= 0)
+                       break;
        }
-
-       return 0;
+       if (ret < 0)
+               goto fail;
+       ret = -E_MP4_TRACK;
+       if (f->track.channel_count == 0)
+               goto fail;
+       ret = -E_MP4_BAD_SAMPLERATE;
+       if (f->track.sample_rate == 0)
+               goto fail;
+       ret = -E_MP4_MISSING_ATOM;
+       if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0)
+               goto fail;
+       *result = f;
+       return 1;
+fail:
+       *result = NULL;
+       mp4_close(f);
+       return ret;
 }
 
-static int32_t sample_range_size(const struct mp4 *f, int32_t track,
-               int32_t chunk_sample, int32_t sample)
-{
-       int32_t i, total;
-       const struct mp4_track *p_track = f->track[track];
-
-       if (p_track->stsz_sample_size) {
-               return (sample - chunk_sample) * p_track->stsz_sample_size;
-       } else {
-               if (sample >= p_track->stsz_sample_count)
-                       return 0;       //error
-
-               for (i = chunk_sample, total = 0; i < sample; i++) {
-                       total += p_track->stsz_table[i];
-               }
-       }
-
-       return total;
+/**
+ * Read the audio track and the metadata of an mp4 file.
+ *
+ * \param cb Only the ->read() and ->seek() methods need to be supplied.
+ * \param result Initialized to a non-NULL pointer iff the function succeeds.
+ *
+ * This detects and parses the first audio track and the metadata information
+ * of the mp4 file. Various error checks are performed after the mp4 atoms have
+ * been parsed successfully.
+ *
+ * This function does not modify the file. However, if the caller intents to
+ * update the metadata later, the ->write() and ->truncate() methods must be
+ * supplied in the callback structure.
+ *
+ * \return Standard. Several errors are possible.
+ *
+ * \sa \ref mp4_open_meta().
+ */
+int mp4_open(const struct mp4_callback *cb, struct mp4 **result)
+{
+       struct mp4 *f;
+       int ret;
+
+       *result = NULL;
+       ret = open_file(cb, false, &f);
+       if (ret < 0)
+               return ret;
+       ret = -E_MP4_BAD_SAMPLE_COUNT;
+       if (f->track.stsz_sample_count == 0)
+               goto fail;
+       ret = -E_MP4_CORRUPT;
+       if (f->track.time_scale == 0)
+               goto fail;
+       *result = f;
+       return 1;
+fail:
+       mp4_close(f);
+       return ret;
 }
 
-static int32_t sample_to_offset(const struct mp4 *f, int32_t track,
-               int32_t sample)
+static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
+               int32_t *chunk)
 {
-       int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
+       const struct mp4_track *t = &f->track;
+       uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
+       uint32_t chunk1, chunk1samples, n, total, k;
 
-       chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
-
-       chunk_offset1 = chunk_to_offset(f, track, chunk);
-       chunk_offset2 = chunk_offset1 + sample_range_size(f,
-               track, chunk_sample, sample);
-       return chunk_offset2;
+       for (k = 1, total = 0; k < t->stsc_entry_count; k++, total += n) {
+               n = (fc[k] - fc[k - 1]) * spc[k - 1]; /* number of samples */
+               if (sample < total + n)
+                       break;
+       }
+       chunk1 = fc[k - 1];
+       chunk1samples = spc[k - 1];
+       if (chunk1samples != 0)
+               *chunk = (sample - total) / chunk1samples + chunk1;
+       else
+               *chunk = 1;
+       return total + (*chunk - chunk1) * chunk1samples;
 }
 
 /**
- * Return the number of milliseconds of the given track.
+ * Compute the duration of an mp4 file.
  *
- * \param f As returned by \ref mp4_open_read(), must not be NULL.
- * \param track Between zero and the value returned by \ref mp4_total_tracks().
+ * \param f See \ref mp4_close().
  *
- * The function returns zero if the audio file is of zero length or contains a
- * corrupt track header.
+ * \return The number of milliseconds of the audio track. This function never
+ * fails.
  */
-uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
+uint64_t mp4_get_duration(const struct mp4 *f)
 {
-       const struct mp4_track *t = f->track[track];
+       const struct mp4_track *t = &f->track;
 
-       if (t->timeScale == 0)
-               return 0;
-       return t->duration * 1000 / t->timeScale;
+       return t->duration * 1000 / t->time_scale;
 }
 
 /**
- * Check whether the given track number corresponds to an audio track.
+ * Reposition the read/write file offset.
  *
- * \param f See \ref mp4_get_duration().
- * \param track See \ref mp4_get_duration().
+ * \param f See \ref mp4_close().
+ * \param sample The number of the sample to reposition to.
  *
- * Besides audio tracks, an mp4 file may contain video and system tracks. For
- * those the function returns false.
+ * The given sample number must be within range, i.e., strictly less than the
+ * value returned by \ref mp4_num_samples().
+ *
+ * \return Standard. The only possible error is an invalid sample number.
  */
-bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
-{
-       return f->track[track]->is_audio;
-}
-
-void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
-{
-       int32_t offset = sample_to_offset(f, track, sample);
-       set_position(f, offset);
+int mp4_set_sample_position(struct mp4 *f, uint32_t sample)
+{
+       const struct mp4_track *t = &f->track;
+       int32_t offset, chunk, chunk_sample;
+       uint32_t n, srs; /* sample range size */
+
+       if (sample >= t->stsz_sample_count)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       chunk_sample = chunk_of_sample(f, sample, &chunk);
+       if (t->stsz_sample_size > 0)
+               srs = (sample - chunk_sample) * t->stsz_sample_size;
+       else {
+               for (srs = 0, n = chunk_sample; n < sample; n++)
+                       srs += t->stsz_table[n];
+       }
+       if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
+               offset = t->stco_chunk_offset[t->stco_entry_count - 1];
+       else if (t->stco_entry_count > 0)
+               offset = t->stco_chunk_offset[chunk - 1];
+       else
+               offset = 8;
+       set_position(f, offset + srs);
+       return 1;
 }
 
-int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
+/**
+ * Look up and return the size of the given sample in the stsz table.
+ *
+ * \param f See \ref mp4_close().
+ * \param sample The sample number of interest.
+ * \param result Sample size is returned here.
+ *
+ * For the sample argument the restriction mentioned in the documentation of
+ * \ref mp4_set_sample_position() applies as well.
+ *
+ * \return Standard. Like for \ref mp4_set_sample_position(), EINVAL is the
+ * only possible error.
+ */
+int mp4_get_sample_size(const struct mp4 *f, uint32_t sample, uint32_t *result)
 {
-       const struct mp4_track *t = f->track[track];
+       const struct mp4_track *t = &f->track;
 
+       if (sample >= t->stsz_sample_count)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (t->stsz_sample_size != 0)
-               return t->stsz_sample_size;
-       return t->stsz_table[sample];
+               *result = t->stsz_sample_size;
+       else
+               *result = t->stsz_table[sample];
+       return 1;
 }
 
-uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
+/**
+ * Return the sample rate stored in the stsd atom.
+ *
+ * \param f See \ref mp4_close().
+ *
+ * The sample rate is a property of the audio track of the mp4 file and is thus
+ * independent of the sample number.
+ *
+ * \return The function always returns a positive value because the open
+ * operation fails if the sample rate happens to be zero. A typical value is
+ * 44100.
+ */
+uint16_t mp4_get_sample_rate(const struct mp4 *f)
 {
-       return f->track[track]->sampleRate;
+       return f->track.sample_rate;
 }
 
-uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
+/**
+ * Return the number of channels of the audio track.
+ *
+ * \param f See \ref mp4_close().
+ *
+ * \return The returned channel count is guaranteed to be positive because the
+ * open operation fails if the mp4a atom is missing or contains a zero channel
+ * count.
+ */
+uint16_t mp4_get_channel_count(const struct mp4 *f)
 {
-       return f->track[track]->channelCount;
+       return f->track.channel_count;
 }
 
-int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
+/**
+ * Return the number of samples of the audio track.
+ *
+ * \param f See \ref mp4_close().
+ *
+ * \return The sample count is read from the stsz atom during open.
+ */
+uint32_t mp4_num_samples(const struct mp4 *f)
 {
-       int32_t i;
-       int32_t total = 0;
-
-       for (i = 0; i < f->track[track]->stts_entry_count; i++) {
-               total += f->track[track]->stts_sample_count[i];
-       }
-       return total;
+       return f->track.stsz_sample_count;
 }
 
-struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
+/**
+ * Open an mp4 file in metadata-only mode.
+ *
+ * \param cb See \ref mp4_open().
+ * \param result See \ref mp4_open().
+ *
+ * This is similar to \ref mp4_open() but is cheaper because it only parses the
+ * metadata of the mp4 file. The only functions that can subsequently be called
+ * with the file handle returned here are \ref mp4_get_meta() and \ref
+ * mp4_update_meta().
+ *
+ * \return Standard.
+ *
+ * \sa \ref mp4_open(). The comment about ->write() and ->truncate() applies to
+ * this function as well.
+ */
+int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
 {
-       struct mp4 *f = para_calloc(sizeof(struct mp4));
+       struct mp4 *f;
+       int ret = open_file(cb, true, &f);
 
-       f->cb = cb;
-       parse_atoms(f, 1);
-       if (f->error) {
-               free(f);
-               f = NULL;
-       }
-       return f;
+       if (ret < 0)
+               return ret;
+       *result = f;
+       return 1;
 }
 
 /**
  * Return the metadata of an mp4 file.
  *
- * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
+ * \param f See \ref mp4_close().
  *
  * The caller is allowed to add, delete or modify the entries of the returned
- * structure in order to pass the modified version to \ref mp4_meta_update().
+ * structure with the intention to pass the modified version to \ref
+ * mp4_update_meta().
+ *
+ * \return This never returns NULL, even if the file contains no metadata tag
+ * items. However, the meta count will be zero and the ->tags pointer NULL in
+ * this case.
  */
 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
 {
        return &f->meta;
 }
 
-static uint32_t find_atom(struct mp4 *f, uint64_t base, uint32_t size,
-                         const char *name)
-{
-       uint32_t remaining = size;
-       uint64_t atom_offset = base;
-       for (;;) {
-               char atom_name[4];
-               uint32_t atom_size;
-
-               set_position(f, atom_offset);
-
-               if (remaining < 8)
-                       break;
-               atom_size = read_int32(f);
-               if (atom_size > remaining || atom_size < 8)
-                       break;
-               read_data(f, atom_name, 4);
-
-               if (!memcmp(atom_name, name, 4)) {
-                       set_position(f, atom_offset);
-                       return 1;
-               }
-
-               remaining -= atom_size;
-               atom_offset += atom_size;
-       }
-       return 0;
-}
-
-static uint32_t find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
-               const char *name, uint32_t extraheaders, const char *name_inside)
-{
-       uint64_t first_base = (uint64_t) (-1);
-       while (find_atom(f, base, size, name))  //try to find atom <name> with atom <name_inside> in it
-       {
-               uint64_t mybase = get_position(f);
-               uint32_t mysize = read_int32(f);
-
-               if (first_base == (uint64_t) (-1))
-                       first_base = mybase;
-
-               if (mysize < 8 + extraheaders)
-                       break;
-
-               if (find_atom (f, mybase + (8 + extraheaders),
-                               mysize - (8 + extraheaders), name_inside)) {
-                       set_position(f, mybase);
-                       return 2;
-               }
-               base += mysize;
-               if (size <= mysize) {
-                       size = 0;
-                       break;
-               }
-               size -= mysize;
-       }
-
-       if (first_base != (uint64_t) (-1))      //wanted atom inside not found
-       {
-               set_position(f, first_base);
-               return 1;
-       } else
-               return 0;
-}
-
-struct membuffer {
-       void *data;
-       unsigned written;
-       unsigned allocated;
-};
-
-static struct membuffer *membuffer_create(void)
-{
-       struct membuffer *buf = para_calloc(sizeof(*buf));
-
-       buf->allocated = 256;
-       buf->data = para_malloc(buf->allocated);
-       return buf;
-}
-
-static void membuffer_write(struct membuffer *buf, const void *ptr,
-               unsigned bytes)
-{
-       unsigned dest_size = buf->written + bytes;
-
-       if (dest_size > buf->allocated) {
-               do {
-                       buf->allocated <<= 1;
-               } while (dest_size > buf->allocated);
-               buf->data = para_realloc(buf->data, buf->allocated);
-       }
-
-       if (ptr)
-               memcpy((char *) buf->data + buf->written, ptr, bytes);
-       buf->written += bytes;
-}
-
-static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
-{
-       membuffer_write(buf, data, 4);
-}
-
-static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
-{
-       uint8_t temp[4];
-       write_u32_be(temp, data);
-       membuffer_write(buf, temp, 4);
-}
-
-static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
-               const char *value)
-{
-       uint32_t len = strlen(value);
-       membuffer_write_int32(buf, 8 /* atom header */
-               + 8 /* data atom header */
-               + 8 /* flags + reserved */
-               + len);
-       membuffer_write_atom_name(buf, name);
-       membuffer_write_int32(buf, 8 /* data atom header */
-               + 8 /* flags + reserved */
-               + len);
-       membuffer_write_atom_name(buf, "data");
-       membuffer_write_int32(buf, 1);  /* flags */
-       membuffer_write_int32(buf, 0);  /* reserved */
-       membuffer_write(buf, value, len);
-}
-
-static unsigned membuffer_get_size(const struct membuffer *buf)
-{
-       return buf->written;
-}
-
-static void *membuffer_detach(struct membuffer *buf)
-{
-       void *ret = para_realloc(buf->data, buf->written);
-       free(buf);
-       return ret;
-}
-
-struct stdmeta_entry {
-       const char *atom;
-       const char *name;
-};
-
-static const char *find_standard_meta(const char *name)
-{
-       const struct stdmeta_entry stdmetas[] = {
-               {"\xA9" "nam", "title"},
-               {"\xA9" "ART", "artist"},
-               {"\xA9" "alb", "album"},
-               {"\xA9" "day", "date"},
-               {"\xA9" "cmt", "comment"},
-       };
-
-       for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
-               if (!strcasecmp(name, stdmetas[n].name))
-                       return stdmetas[n].atom;
-       return NULL;
-}
-
-static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
-               uint32_t * out_size)
-{
-       struct membuffer *buf = membuffer_create();
-       unsigned metaptr;
-
-       for (metaptr = 0; metaptr < meta->count; metaptr++) {
-               struct mp4_tag *tag = meta->tags + metaptr;
-               const char *std_meta_atom = find_standard_meta(tag->item);
-               if (std_meta_atom)
-                       membuffer_write_std_tag(buf, std_meta_atom, tag->value);
+/** Total length of an on-disk metadata tag. */
+#define TAG_LEN(_len) (24 + (_len))
+static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
+{
+       for (unsigned n = 0; n < meta->count; n++) {
+               struct mp4_tag *tag = meta->tags + n;
+               unsigned len = strlen(tag->value);
+               const char *atom_name;
+
+               if (!strcasecmp(tag->item, "title"))
+                       atom_name = "\xA9" "nam";
+               else if (!strcasecmp(tag->item, "artist"))
+                       atom_name = "\xA9" "ART";
+               else if (!strcasecmp(tag->item, "album"))
+                       atom_name = "\xA9" "alb";
+               else if (!strcasecmp(tag->item, "date"))
+                       atom_name = "\xA9" "day";
+               else if (!strcasecmp(tag->item, "comment"))
+                       atom_name = "\xA9" "cmt";
                else
-                       PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
+                       assert(false);
+               write_u32_be(out, TAG_LEN(len));
+               memcpy(out + 4, atom_name, 4);
+               write_u32_be(out + 8, 8 /* data atom header */
+                       + 8 /* flags + reserved */
+                       + len);
+               memcpy(out + 12, "data", 4);
+               write_u32_be(out + 16, 1); /* flags */
+               write_u32_be(out + 20, 0); /* reserved */
+               memcpy(out + 24, tag->value, len);
+               out += TAG_LEN(len);
        }
-       *out_size = membuffer_get_size(buf);
-       *out_buffer = membuffer_detach(buf);
-       return 1;
-}
-
-static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
-                         const void *data)
-{
-       membuffer_write_int32(buf, size + 8);
-       membuffer_write_atom_name(buf, name);
-       membuffer_write(buf, data, size);
-}
-
-static void *membuffer_get_ptr(const struct membuffer *buf)
-{
-       return buf->data;
-}
-
-static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
-               unsigned bytes)
-{
-       unsigned oldsize = membuffer_get_size(buf);
-       char *bufptr;
-
-       membuffer_write(buf, 0, bytes);
-       bufptr = membuffer_get_ptr(buf);
-       if (read_data(src, bufptr + oldsize, bytes) != bytes) {
-               free(buf->data);
-               free(buf);
-               return false;
-       }
-       return true;
-}
-
-static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
-               uint32_t * out_size)
-{
-       struct membuffer *buf;
-       uint32_t ilst_size;
-       void *ilst_buffer;
-
-       if (!create_ilst(meta, &ilst_buffer, &ilst_size))
-               return 0;
-
-       buf = membuffer_create();
-
-       membuffer_write_int32(buf, 0);
-       membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
-       free(ilst_buffer);
-
-       *out_size = membuffer_get_size(buf);
-       *out_buffer = membuffer_detach(buf);
-       return 1;
-}
-
-static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
-uint32_t * out_size)
-{
-       struct membuffer *buf;
-       uint32_t meta_size;
-       void *meta_buffer;
-
-       if (!create_meta(meta, &meta_buffer, &meta_size))
-               return 0;
-
-       buf = membuffer_create();
-
-       membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
-
-       free(meta_buffer);
-
-       *out_size = membuffer_get_size(buf);
-       *out_buffer = membuffer_detach(buf);
-       return 1;
-}
-
-static uint32_t fix_byte_order_32(uint32_t src)
-{
-       return read_u32_be(&src);
 }
 
 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
 {
+       int ret;
        uint64_t total_base = f->moov_offset + 8;
-       uint32_t total_size = (uint32_t) (f->moov_size - 8);
-
-       uint64_t udta_offset, meta_offset, ilst_offset;
-       uint32_t udta_size, meta_size, ilst_size;
-
-       uint32_t new_ilst_size;
-       void *new_ilst_buffer, *out_buffer;
-
+       uint32_t total_size = f->moov_size - 8;
+       uint32_t new_ilst_size = 0;
+       void *out_buffer;
        uint8_t *p_out;
        int32_t size_delta;
+       uint32_t tmp;
 
-       if (!find_atom_v2(f, total_base, total_size, "udta", 0, "meta")) {
-               struct membuffer *buf;
-               void *new_udta_buffer;
-               uint32_t new_udta_size;
-               if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
-                       return NULL;
-
-               buf = membuffer_create();
-               set_position(f, total_base);
-               if (!membuffer_transfer_from_file(buf, f, total_size)) {
-                       free(new_udta_buffer);
-                       return NULL;
-               }
-               membuffer_write_atom(buf, "udta", new_udta_size,
-                       new_udta_buffer);
-
-               free(new_udta_buffer);
-
-               *out_size = membuffer_get_size(buf);
-               return membuffer_detach(buf);
-       }
-       udta_offset = get_position(f);
-       udta_size = read_int32(f);
-       if (!find_atom_v2 (f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst")) {
-               struct membuffer *buf;
-               void *new_meta_buffer;
-               uint32_t new_meta_size;
-
-               if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
-                       return NULL;
-
-               buf = membuffer_create();
-               set_position(f, total_base);
-               if (!membuffer_transfer_from_file(buf, f,
-                               udta_offset - total_base)) {
-                       free(new_meta_buffer);
-                       return NULL;
-               }
-
-               membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
-               membuffer_write_atom_name(buf, "udta");
-               if (!membuffer_transfer_from_file(buf, f, udta_size)) {
-                       free(new_meta_buffer);
-                       return NULL;
-               }
-
-               membuffer_write_atom(buf, "meta", new_meta_size,
-                       new_meta_buffer);
-               free(new_meta_buffer);
-
-               *out_size = membuffer_get_size(buf);
-               return membuffer_detach(buf);
-       }
-       meta_offset = get_position(f);
-       meta_size = read_int32(f);
-       /* shouldn't happen, find_atom_v2 above takes care of it */
-       if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
-               return NULL;
-       ilst_offset = get_position(f);
-       ilst_size = read_int32(f);
-       if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
-               return NULL;
-       size_delta = new_ilst_size - (ilst_size - 8);
+       for (unsigned n = 0; n < f->meta.count; n++)
+               new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
+       size_delta = new_ilst_size - (f->ilst_size - 8);
        *out_size = total_size + size_delta;
-       out_buffer = para_malloc(*out_size);
+       out_buffer = alloc(*out_size);
        p_out = out_buffer;
        set_position(f, total_base);
-       read_data(f, p_out, (uint32_t) (udta_offset - total_base));
-       p_out += (uint32_t) (udta_offset - total_base);
-       *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
+       ret = read_data(f, p_out, f->udta_offset - total_base);
+       if (ret <= 0)
+               return NULL;
+       p_out += f->udta_offset - total_base;
+       ret = read_int32(f, &tmp);
+       if (ret <= 0)
+               return NULL;
+       write_u32_be(p_out, tmp + size_delta);
        p_out += 4;
-       read_data(f, p_out, 4);
+       ret = read_data(f, p_out, 4);
+       if (ret <= 0)
+               return NULL;
        p_out += 4;
-       read_data(f, p_out, (uint32_t) (meta_offset - udta_offset - 8));
-       p_out += (uint32_t) (meta_offset - udta_offset - 8);
-       *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
+       ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
+       if (ret <= 0)
+               return NULL;
+       p_out += f->meta_offset - f->udta_offset - 8;
+       ret = read_int32(f, &tmp);
+       if (ret <= 0)
+               return NULL;
+       write_u32_be(p_out, tmp + size_delta);
        p_out += 4;
-       read_data(f, p_out, 4);
+       ret = read_data(f, p_out, 4);
+       if (ret <= 0)
+               return NULL;
        p_out += 4;
-       read_data(f, p_out, (uint32_t) (ilst_offset - meta_offset - 8));
-       p_out += (uint32_t) (ilst_offset - meta_offset - 8);
-       *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
+       ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
+       if (ret <= 0)
+               return NULL;
+       p_out += f->ilst_offset - f->meta_offset - 8;
+       ret = read_int32(f, &tmp);
+       if (ret <= 0)
+               return NULL;
+       write_u32_be(p_out, tmp + size_delta);
        p_out += 4;
-       read_data(f, p_out, 4);
+       ret = read_data(f, p_out, 4);
+       if (ret <= 0)
+               return NULL;
        p_out += 4;
-       memcpy(p_out, new_ilst_buffer, new_ilst_size);
+       create_ilst(&f->meta, p_out);
        p_out += new_ilst_size;
-       set_position(f, ilst_offset + ilst_size);
-       read_data(f, p_out, (uint32_t) (total_size
-               - (ilst_offset - total_base) - ilst_size));
-       free(new_ilst_buffer);
+       set_position(f, f->ilst_offset + f->ilst_size);
+       ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
+               - f->ilst_size);
+       if (ret <= 0)
+               return NULL;
        return out_buffer;
 }
 
-static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
-{
-       int32_t result = 1;
-
-       result = f->cb->write(f->cb->user_data, data, size);
-
-       f->current_position += size;
-
-       return result;
-}
-
-static int32_t write_int32(struct mp4 *f, uint32_t data)
+static int write_data(struct mp4 *f, void *data, size_t size)
 {
-       int8_t temp[4];
-       write_u32_be(temp, data);
-       return write_data(f, temp, sizeof(temp));
+       while (size > 0) {
+               ssize_t ret = f->cb->write(f->cb->user_data, data, size);
+               if (ret < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       return -ERRNO_TO_PARA_ERROR(errno);
+               }
+               size -= ret;
+       }
+       return 1;
 }
 
-int32_t mp4_meta_update(struct mp4 *f)
+/**
+ * Write back the modified metadata items to the mp4 file.
+ *
+ * This is the only public function which modifies the contents of an mp4 file.
+ * This is achieved by calling the ->write() and ->truncate() methods of the
+ * callback structure passed to \ref mp4_open() or \ref mp4_open_meta().
+ *
+ * \param f See \ref mp4_close().
+ *
+ * The modified metadata structure does not need to be supplied to this
+ * function because it is part of the mp4 structure.
+ *
+ * \return Standard.
+ */
+int mp4_update_meta(struct mp4 *f)
 {
        void *new_moov_data;
        uint32_t new_moov_size;
+       uint8_t buf[8] = "----moov";
+       int ret;
 
        set_position(f, 0);
        new_moov_data = modify_moov(f, &new_moov_size);
@@ -1465,93 +1009,45 @@ int32_t mp4_meta_update(struct mp4 *f)
                mp4_close(f);
                return 0;
        }
-       /* copy moov atom to end of the file */
        if (f->last_atom != ATOM_MOOV) {
-               char *free_data = "free";
-
-               /* rename old moov to free */
                set_position(f, f->moov_offset + 4);
-               write_data(f, free_data, 4);
-
-               set_position(f, f->file_size);
-               write_int32(f, new_moov_size + 8);
-               write_data(f, "moov", 4);
-               write_data(f, new_moov_data, new_moov_size);
-       } else {
+               ret = write_data(f, "free", 4); /* rename old moov to free */
+               if (ret < 0)
+                       goto free_moov;
+               /* write new moov atom at EOF */
+               f->cb->seek(f->cb->user_data, 0, SEEK_END);
+       } else /* overwrite old moov atom */
                set_position(f, f->moov_offset);
-               write_int32(f, new_moov_size + 8);
-               write_data(f, "moov", 4);
-               write_data(f, new_moov_data, new_moov_size);
-       }
+       write_u32_be(buf, new_moov_size + 8);
+       ret = write_data(f, buf, sizeof(buf));
+       if (ret < 0)
+               goto free_moov;
+       ret = write_data(f, new_moov_data, new_moov_size);
+       if (ret < 0)
+               goto free_moov;
+       ret = f->cb->truncate(f->cb->user_data);
+       if (ret < 0)
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+free_moov:
        free(new_moov_data);
-       f->cb->truncate(f->cb->user_data);
-       return 1;
-}
-
-static char *meta_find_by_name(const struct mp4 *f, const char *item)
-{
-       uint32_t i;
-
-       for (i = 0; i < f->meta.count; i++)
-               if (!strcasecmp(f->meta.tags[i].item, item))
-                       return para_strdup(f->meta.tags[i].value);
-       return NULL;
-}
-
-/**
- * Return the value of the artist meta tag of an mp4 file.
- *
- * \param f Must not be NULL.
- *
- * \return If the file does not contain this metadata tag, the function returns
- * NULL. Otherwise, a copy of the tag value is returned. The caller should free
- * this memory when it is no longer needed.
- */
-char *mp4_meta_get_artist(const struct mp4 *f)
-{
-       return meta_find_by_name(f, "artist");
-}
-
-/**
- * Return the value of the title meta tag of an mp4 file.
- *
- * \param f See \ref mp4_meta_get_artist().
- * \return See \ref mp4_meta_get_artist().
- */
-char *mp4_meta_get_title(const struct mp4 *f)
-{
-       return meta_find_by_name(f, "title");
+       return ret;
 }
 
 /**
- * Return the value of the date meta tag of an mp4 file.
+ * Return the value of the given tag item.
  *
- * \param f See \ref mp4_meta_get_artist().
- * \return See \ref mp4_meta_get_artist().
- */
-char *mp4_meta_get_date(const struct mp4 *f)
-{
-       return meta_find_by_name(f, "date");
-}
-
-/**
- * Return the value of the album meta tag of an mp4 file.
+ * \param f See \ref mp4_close().
+ * \param item "artist", "title", "album", "comment", or "date".
  *
- * \param f See \ref mp4_meta_get_artist().
- * \return See \ref mp4_meta_get_artist().
+ * \return The function returns NULL if the given item is not in the above
+ * list. Otherwise, if the file does not contain a tag for the given item, the
+ * function also returns NULL. Otherwise a copy of the tag value is returned
+ * and the caller should free this memory when it is no longer needed.
  */
-char *mp4_meta_get_album(const struct mp4 *f)
+__malloc char *mp4_get_tag_value(const struct mp4 *f, const char *item)
 {
-       return meta_find_by_name(f, "album");
-}
-
-/**
- * Return the value of the comment meta tag of an mp4 file.
- *
- * \param f See \ref mp4_meta_get_artist().
- * \return See \ref mp4_meta_get_artist().
- */
-char *mp4_meta_get_comment(const struct mp4 *f)
-{
-       return meta_find_by_name(f, "comment");
+       for (unsigned n = 0; n < f->meta.count; n++)
+               if (!strcasecmp(f->meta.tags[n].item, item))
+                       return para_strdup(f->meta.tags[n].value);
+       return NULL;
 }