ismindex: Calculate the file duration among the included tracks
[ffmpeg.git] / tools / ismindex.c
index 0a01c90..d5cf138 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
-#include <sys/stat.h>
-#ifdef _WIN32
-#include <direct.h>
-#define mkdir(a, b) _mkdir(a)
-#endif
 
 #include "libavformat/avformat.h"
+#include "libavformat/os_support.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 
@@ -296,14 +292,23 @@ static int handle_file(struct Tracks *tracks, const char *file, int split)
         fprintf(stderr, "No streams found in %s\n", file);
         goto fail;
     }
-    if (!tracks->duration)
-        tracks->duration = ctx->duration;
 
     for (i = 0; i < ctx->nb_streams; i++) {
+        struct Track **temp;
         AVStream *st = ctx->streams[i];
         track = av_mallocz(sizeof(*track));
-        tracks->tracks = av_realloc(tracks->tracks,
-                                    sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
+        if (!track) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        temp = av_realloc(tracks->tracks,
+                          sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
+        if (!temp) {
+            av_free(track);
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        tracks->tracks = temp;
         tracks->tracks[tracks->nb_tracks] = track;
 
         track->name = file;
@@ -313,8 +318,7 @@ static int handle_file(struct Tracks *tracks, const char *file, int split)
         track->bitrate   = st->codec->bit_rate;
         track->track_id  = st->id;
         track->timescale = st->time_base.den;
-        track->duration  = av_rescale_rnd(ctx->duration, track->timescale,
-                                          AV_TIME_BASE, AV_ROUND_UP);
+        track->duration  = st->duration;
         track->is_audio  = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
         track->is_video  = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
 
@@ -326,6 +330,10 @@ static int handle_file(struct Tracks *tracks, const char *file, int split)
             continue;
         }
 
+        tracks->duration = FFMAX(tracks->duration,
+                                 av_rescale_rnd(track->duration, AV_TIME_BASE,
+                                                track->timescale, AV_ROUND_UP));
+
         if (track->is_audio) {
             if (tracks->audio_track < 0)
                 tracks->audio_track = tracks->nb_tracks;
@@ -405,6 +413,23 @@ static void output_server_manifest(struct Tracks *tracks,
     fclose(out);
 }
 
+static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
+                               const char *type)
+{
+    int i, j;
+    struct Track *track = tracks->tracks[main];
+    for (i = 0; i < track->chunks; i++) {
+        for (j = main + 1; j < tracks->nb_tracks; j++) {
+            if (tracks->tracks[j]->is_audio == track->is_audio &&
+                track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration)
+                fprintf(stderr, "Mismatched duration of %s chunk %d in %s and %s\n",
+                        type, i, track->name, tracks->tracks[j]->name);
+        }
+        fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n",
+                i, track->offsets[i].duration);
+    }
+}
+
 static void output_client_manifest(struct Tracks *tracks,
                                    const char *basename, int split)
 {
@@ -450,17 +475,7 @@ static void output_client_manifest(struct Tracks *tracks,
                 fprintf(stderr, "Mismatched number of video chunks in %s and %s\n",
                         track->name, first_track->name);
         }
-        track = first_track;
-        for (i = 0; i < track->chunks; i++) {
-            for (j = tracks->video_track + 1; j < tracks->nb_tracks; j++) {
-                if (tracks->tracks[j]->is_video &&
-                    track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration)
-                    fprintf(stderr, "Mismatched duration of video chunk %d in %s and %s\n",
-                            i, track->name, tracks->tracks[j]->name);
-            }
-            fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n", i,
-                    track->offsets[i].duration);
-        }
+        print_track_chunks(out, tracks, tracks->video_track, "video");
         fprintf(out, "\t</StreamIndex>\n");
     }
     if (tracks->audio_track >= 0) {
@@ -491,17 +506,7 @@ static void output_client_manifest(struct Tracks *tracks,
                 fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
                         track->name, first_track->name);
         }
-        track = first_track;
-        for (i = 0; i < track->chunks; i++) {
-            for (j = tracks->audio_track + 1; j < tracks->nb_tracks; j++) {
-                if (tracks->tracks[j]->is_audio &&
-                    track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration)
-                    fprintf(stderr, "Mismatched duration of audio chunk %d in %s and %s\n",
-                            i, track->name, tracks->tracks[j]->name);
-            }
-            fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n",
-                    i, track->offsets[i].duration);
-        }
+        print_track_chunks(out, tracks, tracks->audio_track, "audio");
         fprintf(out, "\t</StreamIndex>\n");
     }
     fprintf(out, "</SmoothStreamingMedia>\n");