]> 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 40d8bd1bb79d5e5d2806fd4c771bd9e13bc0ac1f..46aec2db6cf42ab50ecb2d99aef2bd0c7b4181f8 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -763,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[] = {
@@ -1415,6 +1407,24 @@ static int32_t sample_to_offset(const struct mp4ff *f, int32_t track,
        return chunk_offset2;
 }
 
+/**
+ * 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);
@@ -1581,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)
@@ -1769,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;
 }