]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
mp4: Introduce mp4ff_get_duration().
[paraslash.git] / mp4.c
diff --git a/mp4.c b/mp4.c
index b6fbabcd39e45c5fb2a5df9d4dcb45e0db8fe205..46aec2db6cf42ab50ecb2d99aef2bd0c7b4181f8 100644 (file)
--- a/mp4.c
+++ b/mp4.c
 #include "string.h"
 #include "mp4.h"
 
+struct mp4ff_track {
+       int32_t type;
+       int32_t channelCount;
+       int32_t sampleSize;
+       uint16_t sampleRate;
+       int32_t audioType;
+
+       /* stsd */
+       int32_t stsd_entry_count;
+
+       /* stsz */
+       int32_t stsz_sample_size;
+       int32_t stsz_sample_count;
+       int32_t *stsz_table;
+
+       /* stts */
+       int32_t stts_entry_count;
+       int32_t *stts_sample_count;
+       int32_t *stts_sample_delta;
+
+       /* stsc */
+       int32_t stsc_entry_count;
+       int32_t *stsc_first_chunk;
+       int32_t *stsc_samples_per_chunk;
+       int32_t *stsc_sample_desc_index;
+
+       /* stsc */
+       int32_t stco_entry_count;
+       int32_t *stco_chunk_offset;
+
+       /* ctts */
+       int32_t ctts_entry_count;
+       int32_t *ctts_sample_count;
+       int32_t *ctts_sample_offset;
+
+       /* esde */
+       uint8_t *decoderConfig;
+       int32_t decoderConfigLen;
+
+       uint32_t maxBitrate;
+       uint32_t avgBitrate;
+
+       uint32_t timeScale;
+       uint64_t duration;
+};
+
+#define MAX_TRACKS 1024
+
+struct mp4ff {
+       /* stream to read from */
+       struct mp4ff_callback *stream;
+       int64_t current_position;
+
+       uint64_t moov_offset;
+       uint64_t moov_size;
+       uint8_t last_atom;
+       uint64_t file_size;
+       uint32_t error;
+
+       /* mvhd */
+       int32_t time_scale;
+       int32_t duration;
+
+       /* incremental track index while reading the file */
+       int32_t total_tracks;
+
+       /* track data */
+       struct mp4ff_track *track[MAX_TRACKS];
+
+       /* metadata */
+       struct mp4ff_metadata tags;
+};
+
 int32_t mp4ff_total_tracks(const struct mp4ff *f)
 {
        return f->total_tracks;
@@ -40,10 +113,8 @@ static uint64_t read_int64(struct mp4ff *f)
 }
 
 /* comnapre 2 atom names, returns 1 for equal, 0 for unequal */
-static int32_t atom_compare(const int8_t a1, const int8_t b1,
-                                 const int8_t c1, const int8_t d1,
-                                 const int8_t a2, const int8_t b2,
-                                 const int8_t c2, const int8_t d2)
+static int32_t 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)
 {
        if (a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2)
                return 1;
@@ -142,8 +213,7 @@ enum atoms {
 
 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
 
-static uint8_t atom_name_to_type(const int8_t a, const int8_t b,
-               const int8_t c, const int8_t d)
+static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
 {
        if (a == 'm') {
                if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
@@ -348,7 +418,7 @@ static int need_parse_when_meta_only(uint8_t atom_type)
        }
 }
 
-static int32_t set_position(struct mp4ff *f, const int64_t position)
+static int32_t set_position(struct mp4ff *f, int64_t position)
 {
        f->stream->seek(f->stream->user_data, position);
        f->current_position = position;
@@ -693,29 +763,21 @@ static int32_t read_mvhd(struct mp4ff *f)
 static int32_t tag_add_field(struct mp4ff_metadata *tags, const char *item,
                const char *value, int32_t len)
 {
-       void *backup = (void *) tags->tags;
-
        if (!item || (item && !*item) || !value)
                return 0;
-
-       tags->tags = (struct mp4ff_tag *)realloc(tags->tags,
+       tags->tags = para_realloc(tags->tags,
                (tags->count + 1) * sizeof(struct mp4ff_tag));
-       if (!tags->tags) {
-               free(backup);
-               return 0;
+       tags->tags[tags->count].item = para_strdup(item);
+       tags->tags[tags->count].len = len;
+       if (len >= 0) {
+               tags->tags[tags->count].value = para_malloc(len + 1);
+               memcpy(tags->tags[tags->count].value, value, len);
+               tags->tags[tags->count].value[len] = 0;
        } else {
-               tags->tags[tags->count].item = para_strdup(item);
-               tags->tags[tags->count].len = len;
-               if (len >= 0) {
-                       tags->tags[tags->count].value = para_malloc(len + 1);
-                       memcpy(tags->tags[tags->count].value, value, len);
-                       tags->tags[tags->count].value[len] = 0;
-               } else {
-                       tags->tags[tags->count].value = para_strdup(value);
-               }
-               tags->count++;
-               return 1;
+               tags->tags[tags->count].value = para_strdup(value);
        }
+       tags->count++;
+       return 1;
 }
 
 static const char *ID3v1GenreList[] = {
@@ -769,7 +831,7 @@ static char *read_string(struct mp4ff *f, uint32_t length)
        return str;
 }
 
-static int32_t set_metadata_name(const uint8_t atom_type, char **name)
+static int32_t set_metadata_name(uint8_t atom_type, char **name)
 {
        static char *tag_names[] = {
                "unknown", "title", "artist", "writer", "album",
@@ -883,7 +945,7 @@ static int32_t set_metadata_name(const uint8_t atom_type, char **name)
        return 0;
 }
 
-static uint32_t min_body_size(const uint8_t atom_type)
+static uint32_t min_body_size(uint8_t atom_type)
 {
        switch(atom_type) {
        case ATOM_GENRE2:
@@ -906,8 +968,7 @@ static uint32_t min_body_size(const uint8_t atom_type)
        }
 }
 
-static int32_t parse_tag(struct mp4ff *f, const uint8_t parent,
-               const int32_t size)
+static int32_t parse_tag(struct mp4ff *f, uint8_t parent, int32_t size)
 {
        uint8_t atom_type;
        uint8_t header_size = 0;
@@ -1029,7 +1090,7 @@ static int32_t read_mdhd(struct mp4ff *f)
        return 1;
 }
 
-static int32_t parse_metadata(struct mp4ff *f, const int32_t size)
+static int32_t parse_metadata(struct mp4ff *f, int32_t size)
 {
        uint64_t subsize, sumsize = 0;
        uint8_t atom_type;
@@ -1046,7 +1107,7 @@ static int32_t parse_metadata(struct mp4ff *f, const int32_t size)
        return 0;
 }
 
-static int32_t read_meta(struct mp4ff *f, const uint64_t size)
+static int32_t read_meta(struct mp4ff *f, uint64_t size)
 {
        uint64_t subsize, sumsize = 0;
        uint8_t atom_type;
@@ -1070,8 +1131,7 @@ static int32_t read_meta(struct mp4ff *f, const uint64_t size)
        return 0;
 }
 
-static int32_t atom_read(struct mp4ff *f, const int32_t size,
-               const uint8_t atom_type)
+static int32_t atom_read(struct mp4ff *f, int32_t size, uint8_t atom_type)
 {
        uint64_t dest_position = get_position(f) + size - 8;
        if (atom_type == ATOM_STSZ) {
@@ -1108,7 +1168,7 @@ static int32_t atom_read(struct mp4ff *f, const int32_t size,
 }
 
 /* parse atoms that are sub atoms of other atoms */
-static int32_t parse_sub_atoms(struct mp4ff *f, const uint64_t total_size, int meta_only)
+static int32_t parse_sub_atoms(struct mp4ff *f, uint64_t total_size, int meta_only)
 {
        uint64_t size;
        uint8_t atom_type = 0;
@@ -1175,8 +1235,8 @@ static int32_t parse_atoms(struct mp4ff *f, int meta_only)
        return 0;
 }
 
-void mp4ff_get_decoder_config(const struct mp4ff *f, const int track,
-                                unsigned char **ppBuf, unsigned int *pBufSize)
+void mp4ff_get_decoder_config(const struct mp4ff *f, int track,
+               unsigned char **ppBuf, unsigned int *pBufSize)
 {
        if (track >= f->total_tracks) {
                *ppBuf = NULL;
@@ -1251,8 +1311,8 @@ void mp4ff_close(struct mp4ff *ff)
        free(ff);
 }
 
-static int32_t chunk_of_sample(const struct mp4ff *f, const int32_t track,
-               const int32_t sample, int32_t * chunk_sample, int32_t * chunk)
+static int32_t chunk_of_sample(const struct mp4ff *f, int32_t track,
+               int32_t sample, int32_t *chunk_sample, int32_t *chunk)
 {
        int32_t total_entries = 0;
        int32_t chunk2entry;
@@ -1297,8 +1357,8 @@ static int32_t chunk_of_sample(const struct mp4ff *f, const int32_t track,
        return 0;
 }
 
-static int32_t chunk_to_offset(const struct mp4ff *f, const int32_t track,
-               const int32_t chunk)
+static int32_t chunk_to_offset(const struct mp4ff *f, int32_t track,
+               int32_t chunk)
 {
        const struct mp4ff_track *p_track = f->track[track];
 
@@ -1314,8 +1374,8 @@ static int32_t chunk_to_offset(const struct mp4ff *f, const int32_t track,
        return 0;
 }
 
-static int32_t sample_range_size(const struct mp4ff *f, const int32_t track,
-               const int32_t chunk_sample, const int32_t sample)
+static int32_t sample_range_size(const struct mp4ff *f, int32_t track,
+               int32_t chunk_sample, int32_t sample)
 {
        int32_t i, total;
        const struct mp4ff_track *p_track = f->track[track];
@@ -1334,8 +1394,8 @@ static int32_t sample_range_size(const struct mp4ff *f, const int32_t track,
        return total;
 }
 
-static int32_t sample_to_offset(const struct mp4ff *f, const int32_t track,
-               const int32_t sample)
+static int32_t sample_to_offset(const struct mp4ff *f, int32_t track,
+               int32_t sample)
 {
        int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
 
@@ -1347,8 +1407,25 @@ static int32_t sample_to_offset(const struct mp4ff *f, const int32_t track,
        return chunk_offset2;
 }
 
-void mp4ff_set_sample_position(struct mp4ff *f, const int32_t track,
-               const int32_t sample)
+/**
+ * Return the number of milliseconds of the given track.
+ *
+ * \param f As returned by \ref mp4ff_open_read(), must not be NULL.
+ * \param track Between zero and the value returned by \ref mp4ff_total_tracks().
+ *
+ * The function returns zero if the audio file is of zero length or contains a
+ * corrupt track header.
+ */
+uint64_t mp4ff_get_duration(const struct mp4ff *f, int32_t track)
+{
+       const struct mp4ff_track *t = f->track[track];
+
+       if (t->timeScale == 0)
+               return 0;
+       return t->duration * 1000 / t->timeScale;
+}
+
+void mp4ff_set_sample_position(struct mp4ff *f, int32_t track, int32_t sample)
 {
        int32_t offset = sample_to_offset(f, track, sample);
        set_position(f, offset);
@@ -1363,17 +1440,17 @@ int32_t mp4ff_get_sample_size(const struct mp4ff *f, int track, int sample)
        return t->stsz_table[sample];
 }
 
-uint32_t mp4ff_get_sample_rate(const struct mp4ff *f, const int32_t track)
+uint32_t mp4ff_get_sample_rate(const struct mp4ff *f, int32_t track)
 {
        return f->track[track]->sampleRate;
 }
 
-uint32_t mp4ff_get_channel_count(const struct mp4ff *f, const int32_t track)
+uint32_t mp4ff_get_channel_count(const struct mp4ff *f, int32_t track)
 {
        return f->track[track]->channelCount;
 }
 
-int32_t mp4ff_num_samples(const struct mp4ff *f, const int32_t track)
+int32_t mp4ff_num_samples(const struct mp4ff *f, int32_t track)
 {
        int32_t i;
        int32_t total = 0;
@@ -1491,8 +1568,6 @@ struct membuffer {
        unsigned error;
 };
 
-#define stricmp strcasecmp
-
 static struct membuffer *membuffer_create(void)
 {
        const unsigned initial_size = 256;
@@ -1516,17 +1591,7 @@ static unsigned membuffer_write(struct membuffer *buf, const void *ptr, unsigned
                do {
                        buf->allocated <<= 1;
                } while (dest_size > buf->allocated);
-
-               {
-                       void *newptr = realloc(buf->data, buf->allocated);
-                       if (newptr == 0) {
-                               free(buf->data);
-                               buf->data = 0;
-                               buf->error = 1;
-                               return 0;
-                       }
-                       buf->data = newptr;
-               }
+               buf->data = para_realloc(buf->data, buf->allocated);
        }
 
        if (ptr)
@@ -1599,7 +1664,7 @@ static uint32_t meta_genre_to_index(const char *genrestr)
 {
        unsigned n;
        for (n = 0; n < sizeof (ID3v1GenreList) / sizeof (ID3v1GenreList[0]); n++) {
-               if (!stricmp(genrestr, ID3v1GenreList[n]))
+               if (!strcasecmp(genrestr, ID3v1GenreList[n]))
                        return n + 1;
        }
        return 0;
@@ -1627,7 +1692,7 @@ static const char *find_standard_meta(const char *name)   //returns atom name if f
 {
        unsigned n;
        for (n = 0; n < sizeof (stdmetas) / sizeof (stdmetas[0]); n++) {
-               if (!stricmp(name, stdmetas[n].name))
+               if (!strcasecmp(name, stdmetas[n].name))
                        return stdmetas[n].atom;
        }
        return 0;
@@ -1704,15 +1769,9 @@ static void *membuffer_detach(struct membuffer *buf)
 
        if (buf->error)
                return 0;
-
-       ret = realloc(buf->data, buf->written);
-
-       if (ret == 0)
-               free(buf->data);
-
+       ret = para_realloc(buf->data, buf->written);
        buf->data = 0;
        buf->error = 1;
-
        return ret;
 }
 
@@ -1722,78 +1781,70 @@ static uint32_t create_ilst(const struct mp4ff_metadata *data, void **out_buffer
        struct membuffer *buf = membuffer_create();
        unsigned metaptr;
        char *mask = para_calloc(data->count);
-       {
-               const char *tracknumber_ptr = 0, *totaltracks_ptr = 0;
-               const char *discnumber_ptr = 0, *totaldiscs_ptr = 0;
-               const char *genre_ptr = 0, *tempo_ptr = 0;
-               for (metaptr = 0; metaptr < data->count; metaptr++) {
-                       struct mp4ff_tag *tag = &data->tags[metaptr];
-                       if (!stricmp(tag->item, "tracknumber") || !stricmp(tag->item, "track")) {
-                               if (tracknumber_ptr == 0)
-                                       tracknumber_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       } else if (!stricmp(tag->item, "totaltracks")) {
-                               if (totaltracks_ptr == 0)
-                                       totaltracks_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       } else if (!stricmp(tag->item, "discnumber")
-                                       || !stricmp(tag->item, "disc")) {
-                               if (discnumber_ptr == 0)
-                                       discnumber_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       } else if (!stricmp(tag->item, "totaldiscs")) {
-                               if (totaldiscs_ptr == 0)
-                                       totaldiscs_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       } else if (!stricmp(tag->item, "genre")) {
-                               if (genre_ptr == 0)
-                                       genre_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       } else if (!stricmp(tag->item, "tempo")) {
-                               if (tempo_ptr == 0)
-                                       tempo_ptr = tag->value;
-                               mask[metaptr] = 1;
-                       }
-
-               }
+       const char *tracknumber_ptr = 0, *totaltracks_ptr = 0;
+       const char *discnumber_ptr = 0, *totaldiscs_ptr = 0;
+       const char *genre_ptr = 0, *tempo_ptr = 0;
 
-               if (tracknumber_ptr)
-                       membuffer_write_track_tag(buf, "trkn",
-                                                 myatoi(tracknumber_ptr),
-                                                 myatoi(totaltracks_ptr));
-               if (discnumber_ptr)
-                       membuffer_write_track_tag(buf, "disk",
-                                                 myatoi(discnumber_ptr),
-                                                 myatoi(totaldiscs_ptr));
-               if (tempo_ptr)
-                       membuffer_write_int16_tag(buf, "tmpo",
-                                                 (uint16_t) myatoi(tempo_ptr));
-
-               if (genre_ptr) {
-                       uint32_t index = meta_genre_to_index(genre_ptr);
-                       if (index == 0)
-                               membuffer_write_std_tag(buf, "©gen",
-                                                       genre_ptr);
-                       else
-                               membuffer_write_int16_tag(buf, "gnre",
-                                                         (uint16_t) index);
+       for (metaptr = 0; metaptr < data->count; metaptr++) {
+               struct mp4ff_tag *tag = &data->tags[metaptr];
+               if (!strcasecmp(tag->item, "tracknumber")
+                               || !strcasecmp(tag->item, "track")) {
+                       if (tracknumber_ptr == 0)
+                               tracknumber_ptr = tag->value;
+                       mask[metaptr] = 1;
+               } else if (!strcasecmp(tag->item, "totaltracks")) {
+                       if (totaltracks_ptr == 0)
+                               totaltracks_ptr = tag->value;
+                       mask[metaptr] = 1;
+               } else if (!strcasecmp(tag->item, "discnumber")
+                               || !strcasecmp(tag->item, "disc")) {
+                       if (discnumber_ptr == 0)
+                               discnumber_ptr = tag->value;
+                       mask[metaptr] = 1;
+               } else if (!strcasecmp(tag->item, "totaldiscs")) {
+                       if (totaldiscs_ptr == 0)
+                               totaldiscs_ptr = tag->value;
+                       mask[metaptr] = 1;
+               } else if (!strcasecmp(tag->item, "genre")) {
+                       if (genre_ptr == 0)
+                               genre_ptr = tag->value;
+                       mask[metaptr] = 1;
+               } else if (!strcasecmp(tag->item, "tempo")) {
+                       if (tempo_ptr == 0)
+                               tempo_ptr = tag->value;
+                       mask[metaptr] = 1;
                }
        }
 
-       for (metaptr = 0; metaptr < data->count; metaptr++) {
-               if (!mask[metaptr]) {
-                       struct mp4ff_tag *tag = &data->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);
-                       } else {
-                               membuffer_write_custom_tag(buf, tag->item,
-                                       tag->value);
-                       }
-               }
+       if (tracknumber_ptr)
+               membuffer_write_track_tag(buf, "trkn", myatoi(tracknumber_ptr),
+                        myatoi(totaltracks_ptr));
+       if (discnumber_ptr)
+               membuffer_write_track_tag(buf, "disk", myatoi(discnumber_ptr),
+                        myatoi(totaldiscs_ptr));
+       if (tempo_ptr)
+               membuffer_write_int16_tag(buf, "tmpo", myatoi(tempo_ptr));
+
+       if (genre_ptr) {
+               uint32_t index = meta_genre_to_index(genre_ptr);
+               if (index == 0)
+                       membuffer_write_std_tag(buf, "©gen", genre_ptr);
+               else
+                       membuffer_write_int16_tag(buf, "gnre", index);
        }
+       for (metaptr = 0; metaptr < data->count; metaptr++) {
+               struct mp4ff_tag *tag;
+               const char *std_meta_atom;
 
+               if (mask[metaptr])
+                       continue;
+               tag = &data->tags[metaptr];
+               std_meta_atom = find_standard_meta(tag->item);
+               if (std_meta_atom)
+                       membuffer_write_std_tag(buf, std_meta_atom, tag->value);
+               else
+                       membuffer_write_custom_tag(buf, tag->item, tag->value);
+       }
        free(mask);
 
        if (membuffer_error(buf)) {
@@ -2023,7 +2074,7 @@ static int32_t write_data(struct mp4ff *f, void *data, uint32_t size)
        return result;
 }
 
-static int32_t write_int32(struct mp4ff *f, const uint32_t data)
+static int32_t write_int32(struct mp4ff *f, uint32_t data)
 {
        int8_t temp[4];
        write_u32_be(temp, data);
@@ -2084,7 +2135,7 @@ static int32_t meta_find_by_name(const struct mp4ff *f, const char *item,
        uint32_t i;
 
        for (i = 0; i < f->tags.count; i++) {
-               if (!stricmp(f->tags.tags[i].item, item)) {
+               if (!strcasecmp(f->tags.tags[i].item, item)) {
                        *value = para_strdup(f->tags.tags[i].value);
                        return 1;
                }