ismindex: produce .ismf file
[ffmpeg.git] / tools / ismindex.c
1 /*
2  * Copyright (c) 2012 Martin Storsjo
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /*
22  * To create a simple file for smooth streaming:
23  * avconv <normal input/transcoding options> -movflags frag_keyframe foo.ismv
24  * ismindex -n foo foo.ismv
25  * This step creates foo.ism and foo.ismc that is required by IIS for
26  * serving it.
27  *
28  * With -ismf, it also creates foo.ismf, which maps fragment names to
29  * start-end offsets in the ismv, for use in your own streaming server.
30  *
31  * By adding -path-prefix path/, the produced foo.ism will refer to the
32  * files foo.ismv as "path/foo.ismv" - the prefix for the generated ismc
33  * file can be set with the -ismc-prefix option similarly.
34  *
35  * To pre-split files for serving as static files by a web server without
36  * any extra server support, create the ismv file as above, and split it:
37  * ismindex -split foo.ismv
38  * This step creates a file Manifest and directories QualityLevel(...),
39  * that can be read directly by a smooth streaming player.
40  *
41  * The -output dir option can be used to request that output files
42  * (both .ism/.ismc, or Manifest/QualityLevels* when splitting)
43  * should be written to this directory instead of in the current directory.
44  * (The directory itself isn't created if it doesn't already exist.)
45  */
46
47 #include <stdio.h>
48 #include <string.h>
49
50 #include "libavformat/avformat.h"
51 #include "libavformat/os_support.h"
52 #include "libavutil/intreadwrite.h"
53 #include "libavutil/mathematics.h"
54
55 static int usage(const char *argv0, int ret)
56 {
57     fprintf(stderr, "%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
58                     "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
59     return ret;
60 }
61
62 struct MoofOffset {
63     int64_t time;
64     int64_t offset;
65     int64_t duration;
66 };
67
68 struct Track {
69     const char *name;
70     int64_t duration;
71     int bitrate;
72     int track_id;
73     int is_audio, is_video;
74     int width, height;
75     int chunks;
76     int sample_rate, channels;
77     uint8_t *codec_private;
78     int codec_private_size;
79     struct MoofOffset *offsets;
80     int timescale;
81     const char *fourcc;
82     int blocksize;
83     int tag;
84 };
85
86 struct Tracks {
87     int nb_tracks;
88     int64_t duration;
89     struct Track **tracks;
90     int video_track, audio_track;
91     int nb_video_tracks, nb_audio_tracks;
92 };
93
94 static int expect_tag(int32_t got_tag, int32_t expected_tag) {
95     if (got_tag != expected_tag) {
96         char got_tag_str[4], expected_tag_str[4];
97         AV_WB32(got_tag_str, got_tag);
98         AV_WB32(expected_tag_str, expected_tag);
99         fprintf(stderr, "wanted tag %.4s, got %.4s\n", expected_tag_str,
100                 got_tag_str);
101         return -1;
102     }
103     return 0;
104 }
105
106 static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
107 {
108     int32_t size, tag;
109
110     size = avio_rb32(in);
111     tag  = avio_rb32(in);
112     avio_wb32(out, size);
113     avio_wb32(out, tag);
114     if (expect_tag(tag, tag_name) != 0)
115         return -1;
116     size -= 8;
117     while (size > 0) {
118         char buf[1024];
119         int len = FFMIN(sizeof(buf), size);
120         int got;
121         if ((got = avio_read(in, buf, len)) != len) {
122             fprintf(stderr, "short read, wanted %d, got %d\n", len, got);
123             break;
124         }
125         avio_write(out, buf, len);
126         size -= len;
127     }
128     return 0;
129 }
130
131 static int skip_tag(AVIOContext *in, int32_t tag_name)
132 {
133     int64_t pos = avio_tell(in);
134     int32_t size, tag;
135
136     size = avio_rb32(in);
137     tag  = avio_rb32(in);
138     if (expect_tag(tag, tag_name) != 0)
139         return -1;
140     avio_seek(in, pos + size, SEEK_SET);
141     return 0;
142 }
143
144 static int write_fragment(const char *filename, AVIOContext *in)
145 {
146     AVIOContext *out = NULL;
147     int ret;
148
149     if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0) {
150         char errbuf[100];
151         av_strerror(ret, errbuf, sizeof(errbuf));
152         fprintf(stderr, "Unable to open %s: %s\n", filename, errbuf);
153         return ret;
154     }
155     ret = copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
156     if (!ret)
157         ret = copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
158
159     avio_flush(out);
160     avio_close(out);
161
162     return ret;
163 }
164
165 static int skip_fragment(AVIOContext *in)
166 {
167     int ret;
168     ret = skip_tag(in, MKBETAG('m', 'o', 'o', 'f'));
169     if (!ret)
170         ret = skip_tag(in, MKBETAG('m', 'd', 'a', 't'));
171     return ret;
172 }
173
174 static int write_fragments(struct Tracks *tracks, int start_index,
175                            AVIOContext *in, const char *basename,
176                            int split, int ismf, const char* output_prefix)
177 {
178     char dirname[2048], filename[2048], idxname[2048];
179     int i, j, ret = 0, fragment_ret;
180     FILE* out = NULL;
181
182     if (ismf) {
183         snprintf(idxname, sizeof(idxname), "%s%s.ismf", output_prefix, basename);
184         out = fopen(idxname, "w");
185         if (!out) {
186             ret = AVERROR(errno);
187             perror(idxname);
188             goto fail;
189         }
190     }
191     for (i = start_index; i < tracks->nb_tracks; i++) {
192         struct Track *track = tracks->tracks[i];
193         const char *type    = track->is_video ? "video" : "audio";
194         snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
195         if (split) {
196             if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
197                 ret = AVERROR(errno);
198                 perror(dirname);
199                 goto fail;
200             }
201         }
202         for (j = 0; j < track->chunks; j++) {
203             snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
204                      dirname, type, track->offsets[j].time);
205             avio_seek(in, track->offsets[j].offset, SEEK_SET);
206             if (ismf)
207                 fprintf(out, "%s %"PRId64, filename, avio_tell(in));
208             if (split)
209                 fragment_ret = write_fragment(filename, in);
210             else
211                 fragment_ret = skip_fragment(in);
212             if (ismf)
213                 fprintf(out, " %"PRId64"\n", avio_tell(in));
214             if (fragment_ret != 0) {
215                 fprintf(stderr, "failed fragment %d in track %d (%s)\n", j,
216                         track->track_id, track->name);
217                 ret = fragment_ret;
218             }
219         }
220     }
221 fail:
222     if (out)
223         fclose(out);
224     return ret;
225 }
226
227 static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
228 {
229     int ret = AVERROR_EOF, track_id;
230     int version, fieldlength, i, j;
231     int64_t pos   = avio_tell(f);
232     uint32_t size = avio_rb32(f);
233     struct Track *track = NULL;
234
235     if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
236         goto fail;
237     version = avio_r8(f);
238     avio_rb24(f);
239     track_id = avio_rb32(f); /* track id */
240     for (i = start_index; i < tracks->nb_tracks && !track; i++)
241         if (tracks->tracks[i]->track_id == track_id)
242             track = tracks->tracks[i];
243     if (!track) {
244         /* Ok, continue parsing the next atom */
245         ret = 0;
246         goto fail;
247     }
248     fieldlength = avio_rb32(f);
249     track->chunks  = avio_rb32(f);
250     track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
251     if (!track->offsets) {
252         ret = AVERROR(ENOMEM);
253         goto fail;
254     }
255     for (i = 0; i < track->chunks; i++) {
256         if (version == 1) {
257             track->offsets[i].time   = avio_rb64(f);
258             track->offsets[i].offset = avio_rb64(f);
259         } else {
260             track->offsets[i].time   = avio_rb32(f);
261             track->offsets[i].offset = avio_rb32(f);
262         }
263         for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
264             avio_r8(f);
265         for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
266             avio_r8(f);
267         for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
268             avio_r8(f);
269         if (i > 0)
270             track->offsets[i - 1].duration = track->offsets[i].time -
271                                              track->offsets[i - 1].time;
272     }
273     if (track->chunks > 0)
274         track->offsets[track->chunks - 1].duration = track->duration -
275                                                      track->offsets[track->chunks - 1].time;
276     ret = 0;
277
278 fail:
279     avio_seek(f, pos + size, SEEK_SET);
280     return ret;
281 }
282
283 static int read_mfra(struct Tracks *tracks, int start_index,
284                      const char *file, int split, int ismf,
285                      const char *basename, const char* output_prefix)
286 {
287     int err = 0;
288     const char* err_str = "";
289     AVIOContext *f = NULL;
290     int32_t mfra_size;
291
292     if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
293         goto fail;
294     avio_seek(f, avio_size(f) - 4, SEEK_SET);
295     mfra_size = avio_rb32(f);
296     avio_seek(f, -mfra_size, SEEK_CUR);
297     if (avio_rb32(f) != mfra_size) {
298         err = AVERROR_INVALIDDATA;
299         err_str = "mfra size mismatch";
300         goto fail;
301     }
302     if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
303         err = AVERROR_INVALIDDATA;
304         err_str = "mfra tag mismatch";
305         goto fail;
306     }
307     while (!read_tfra(tracks, start_index, f)) {
308         /* Empty */
309     }
310
311     if (split || ismf)
312         err = write_fragments(tracks, start_index, f, basename, split, ismf,
313                               output_prefix);
314     err_str = "error in write_fragments";
315
316 fail:
317     if (f)
318         avio_close(f);
319     if (err)
320         fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
321     return err;
322 }
323
324 static int get_private_data(struct Track *track, AVCodecContext *codec)
325 {
326     track->codec_private_size = codec->extradata_size;
327     track->codec_private      = av_mallocz(codec->extradata_size);
328     if (!track->codec_private)
329         return AVERROR(ENOMEM);
330     memcpy(track->codec_private, codec->extradata, codec->extradata_size);
331     return 0;
332 }
333
334 static int get_video_private_data(struct Track *track, AVCodecContext *codec)
335 {
336     AVIOContext *io = NULL;
337     uint16_t sps_size, pps_size;
338     int err;
339
340     if (codec->codec_id == AV_CODEC_ID_VC1)
341         return get_private_data(track, codec);
342
343     if ((err = avio_open_dyn_buf(&io)) < 0)
344         goto fail;
345     err = AVERROR(EINVAL);
346     if (codec->extradata_size < 11 || codec->extradata[0] != 1)
347         goto fail;
348     sps_size = AV_RB16(&codec->extradata[6]);
349     if (11 + sps_size > codec->extradata_size)
350         goto fail;
351     avio_wb32(io, 0x00000001);
352     avio_write(io, &codec->extradata[8], sps_size);
353     pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
354     if (11 + sps_size + pps_size > codec->extradata_size)
355         goto fail;
356     avio_wb32(io, 0x00000001);
357     avio_write(io, &codec->extradata[11 + sps_size], pps_size);
358     err = 0;
359
360 fail:
361     track->codec_private_size = avio_close_dyn_buf(io, &track->codec_private);
362     return err;
363 }
364
365 static int handle_file(struct Tracks *tracks, const char *file, int split,
366                        int ismf, const char *basename,
367                        const char* output_prefix)
368 {
369     AVFormatContext *ctx = NULL;
370     int err = 0, i, orig_tracks = tracks->nb_tracks;
371     char errbuf[50], *ptr;
372     struct Track *track;
373
374     err = avformat_open_input(&ctx, file, NULL, NULL);
375     if (err < 0) {
376         av_strerror(err, errbuf, sizeof(errbuf));
377         fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
378         return 1;
379     }
380
381     err = avformat_find_stream_info(ctx, NULL);
382     if (err < 0) {
383         av_strerror(err, errbuf, sizeof(errbuf));
384         fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
385         goto fail;
386     }
387
388     if (ctx->nb_streams < 1) {
389         fprintf(stderr, "No streams found in %s\n", file);
390         goto fail;
391     }
392
393     for (i = 0; i < ctx->nb_streams; i++) {
394         struct Track **temp;
395         AVStream *st = ctx->streams[i];
396
397         if (st->codec->bit_rate == 0) {
398             fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
399                     st->id, file);
400             continue;
401         }
402
403         track = av_mallocz(sizeof(*track));
404         if (!track) {
405             err = AVERROR(ENOMEM);
406             goto fail;
407         }
408         temp = av_realloc(tracks->tracks,
409                           sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
410         if (!temp) {
411             av_free(track);
412             err = AVERROR(ENOMEM);
413             goto fail;
414         }
415         tracks->tracks = temp;
416         tracks->tracks[tracks->nb_tracks] = track;
417
418         track->name = file;
419         if ((ptr = strrchr(file, '/')))
420             track->name = ptr + 1;
421
422         track->bitrate   = st->codec->bit_rate;
423         track->track_id  = st->id;
424         track->timescale = st->time_base.den;
425         track->duration  = st->duration;
426         track->is_audio  = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
427         track->is_video  = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
428
429         if (!track->is_audio && !track->is_video) {
430             fprintf(stderr,
431                     "Track %d in %s is neither video nor audio, skipping\n",
432                     track->track_id, file);
433             av_freep(&tracks->tracks[tracks->nb_tracks]);
434             continue;
435         }
436
437         tracks->duration = FFMAX(tracks->duration,
438                                  av_rescale_rnd(track->duration, AV_TIME_BASE,
439                                                 track->timescale, AV_ROUND_UP));
440
441         if (track->is_audio) {
442             if (tracks->audio_track < 0)
443                 tracks->audio_track = tracks->nb_tracks;
444             tracks->nb_audio_tracks++;
445             track->channels    = st->codec->channels;
446             track->sample_rate = st->codec->sample_rate;
447             if (st->codec->codec_id == AV_CODEC_ID_AAC) {
448                 track->fourcc    = "AACL";
449                 track->tag       = 255;
450                 track->blocksize = 4;
451             } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
452                 track->fourcc    = "WMAP";
453                 track->tag       = st->codec->codec_tag;
454                 track->blocksize = st->codec->block_align;
455             }
456             get_private_data(track, st->codec);
457         }
458         if (track->is_video) {
459             if (tracks->video_track < 0)
460                 tracks->video_track = tracks->nb_tracks;
461             tracks->nb_video_tracks++;
462             track->width  = st->codec->width;
463             track->height = st->codec->height;
464             if (st->codec->codec_id == AV_CODEC_ID_H264)
465                 track->fourcc = "H264";
466             else if (st->codec->codec_id == AV_CODEC_ID_VC1)
467                 track->fourcc = "WVC1";
468             get_video_private_data(track, st->codec);
469         }
470
471         tracks->nb_tracks++;
472     }
473
474     avformat_close_input(&ctx);
475
476     err = read_mfra(tracks, orig_tracks, file, split, ismf, basename,
477                     output_prefix);
478
479 fail:
480     if (ctx)
481         avformat_close_input(&ctx);
482     return err;
483 }
484
485 static void output_server_manifest(struct Tracks *tracks, const char *basename,
486                                    const char *output_prefix,
487                                    const char *path_prefix,
488                                    const char *ismc_prefix)
489 {
490     char filename[1000];
491     FILE *out;
492     int i;
493
494     snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
495     out = fopen(filename, "w");
496     if (!out) {
497         perror(filename);
498         return;
499     }
500     fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
501     fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
502     fprintf(out, "\t<head>\n");
503     fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
504                  "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
505     fprintf(out, "\t</head>\n");
506     fprintf(out, "\t<body>\n");
507     fprintf(out, "\t\t<switch>\n");
508     for (i = 0; i < tracks->nb_tracks; i++) {
509         struct Track *track = tracks->tracks[i];
510         const char *type    = track->is_video ? "video" : "audio";
511         fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
512                 type, path_prefix, track->name, track->bitrate);
513         fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
514                      "valueType=\"data\" />\n", track->track_id);
515         fprintf(out, "\t\t\t</%s>\n", type);
516     }
517     fprintf(out, "\t\t</switch>\n");
518     fprintf(out, "\t</body>\n");
519     fprintf(out, "</smil>\n");
520     fclose(out);
521 }
522
523 static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
524                                const char *type)
525 {
526     int i, j;
527     struct Track *track = tracks->tracks[main];
528     int should_print_time_mismatch = 1;
529
530     for (i = 0; i < track->chunks; i++) {
531         for (j = main + 1; j < tracks->nb_tracks; j++) {
532             if (tracks->tracks[j]->is_audio == track->is_audio) {
533                 if (track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration) {
534                     fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
535                             type, i, track->name, main, tracks->tracks[j]->name, j);
536                     should_print_time_mismatch = 1;
537                 }
538                 if (track->offsets[i].time != tracks->tracks[j]->offsets[i].time) {
539                     if (should_print_time_mismatch)
540                         fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
541                                 type, i, track->name, main, tracks->tracks[j]->name, j);
542                     should_print_time_mismatch = 0;
543                 }
544             }
545         }
546         fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" />\n",
547                 i, track->offsets[i].duration);
548     }
549 }
550
551 static void output_client_manifest(struct Tracks *tracks, const char *basename,
552                                    const char *output_prefix, int split)
553 {
554     char filename[1000];
555     FILE *out;
556     int i, j;
557
558     if (split)
559         snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
560     else
561         snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
562     out = fopen(filename, "w");
563     if (!out) {
564         perror(filename);
565         return;
566     }
567     fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
568     fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
569                  "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
570     if (tracks->video_track >= 0) {
571         struct Track *track = tracks->tracks[tracks->video_track];
572         struct Track *first_track = track;
573         int index = 0;
574         fprintf(out,
575                 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
576                 "Chunks=\"%d\" "
577                 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
578                 tracks->nb_video_tracks, track->chunks);
579         for (i = 0; i < tracks->nb_tracks; i++) {
580             track = tracks->tracks[i];
581             if (!track->is_video)
582                 continue;
583             fprintf(out,
584                     "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
585                     "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
586                     "CodecPrivateData=\"",
587                     index, track->bitrate, track->fourcc, track->width, track->height);
588             for (j = 0; j < track->codec_private_size; j++)
589                 fprintf(out, "%02X", track->codec_private[j]);
590             fprintf(out, "\" />\n");
591             index++;
592             if (track->chunks != first_track->chunks)
593                 fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
594                         track->name, track->track_id, track->chunks, first_track->name, first_track->track_id, first_track->chunks);
595         }
596         print_track_chunks(out, tracks, tracks->video_track, "video");
597         fprintf(out, "\t</StreamIndex>\n");
598     }
599     if (tracks->audio_track >= 0) {
600         struct Track *track = tracks->tracks[tracks->audio_track];
601         struct Track *first_track = track;
602         int index = 0;
603         fprintf(out,
604                 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
605                 "Chunks=\"%d\" "
606                 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
607                 tracks->nb_audio_tracks, track->chunks);
608         for (i = 0; i < tracks->nb_tracks; i++) {
609             track = tracks->tracks[i];
610             if (!track->is_audio)
611                 continue;
612             fprintf(out,
613                     "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
614                     "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
615                     "BitsPerSample=\"16\" PacketSize=\"%d\" "
616                     "AudioTag=\"%d\" CodecPrivateData=\"",
617                     index, track->bitrate, track->fourcc, track->sample_rate,
618                     track->channels, track->blocksize, track->tag);
619             for (j = 0; j < track->codec_private_size; j++)
620                 fprintf(out, "%02X", track->codec_private[j]);
621             fprintf(out, "\" />\n");
622             index++;
623             if (track->chunks != first_track->chunks)
624                 fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
625                         track->name, first_track->name);
626         }
627         print_track_chunks(out, tracks, tracks->audio_track, "audio");
628         fprintf(out, "\t</StreamIndex>\n");
629     }
630     fprintf(out, "</SmoothStreamingMedia>\n");
631     fclose(out);
632 }
633
634 static void clean_tracks(struct Tracks *tracks)
635 {
636     int i;
637     for (i = 0; i < tracks->nb_tracks; i++) {
638         av_freep(&tracks->tracks[i]->codec_private);
639         av_freep(&tracks->tracks[i]->offsets);
640         av_freep(&tracks->tracks[i]);
641     }
642     av_freep(&tracks->tracks);
643     tracks->nb_tracks = 0;
644 }
645
646 int main(int argc, char **argv)
647 {
648     const char *basename = NULL;
649     const char *path_prefix = "", *ismc_prefix = "";
650     const char *output_prefix = "";
651     char output_prefix_buf[2048];
652     int split = 0, ismf = 0, i;
653     struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
654
655     av_register_all();
656
657     for (i = 1; i < argc; i++) {
658         if (!strcmp(argv[i], "-n")) {
659             basename = argv[i + 1];
660             i++;
661         } else if (!strcmp(argv[i], "-path-prefix")) {
662             path_prefix = argv[i + 1];
663             i++;
664         } else if (!strcmp(argv[i], "-ismc-prefix")) {
665             ismc_prefix = argv[i + 1];
666             i++;
667         } else if (!strcmp(argv[i], "-output")) {
668             output_prefix = argv[i + 1];
669             i++;
670             if (output_prefix[strlen(output_prefix) - 1] != '/') {
671                 snprintf(output_prefix_buf, sizeof(output_prefix_buf),
672                          "%s/", output_prefix);
673                 output_prefix = output_prefix_buf;
674             }
675         } else if (!strcmp(argv[i], "-split")) {
676             split = 1;
677         } else if (!strcmp(argv[i], "-ismf")) {
678             ismf = 1;
679         } else if (argv[i][0] == '-') {
680             return usage(argv[0], 1);
681         } else {
682             if (handle_file(&tracks, argv[i], split, ismf,
683                             basename, output_prefix))
684                 return 1;
685         }
686     }
687     if (!tracks.nb_tracks || (!basename && !split))
688         return usage(argv[0], 1);
689
690     if (!split)
691         output_server_manifest(&tracks, basename, output_prefix,
692                                path_prefix, ismc_prefix);
693     output_client_manifest(&tracks, basename, output_prefix, split);
694
695     clean_tracks(&tracks);
696
697     return 0;
698 }