FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ismindex.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Martin Storsjo
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  * ffmpeg <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 "cmdutils.h"
51 
52 #include "libavformat/avformat.h"
53 #include "libavformat/isom.h"
54 #include "libavformat/os_support.h"
55 #include "libavutil/intreadwrite.h"
56 #include "libavutil/mathematics.h"
57 
58 static int usage(const char *argv0, int ret)
59 {
60  fprintf(stderr, "%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
61  "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
62  return ret;
63 }
64 
65 struct MoofOffset {
66  int64_t time;
67  int64_t offset;
68  int64_t duration;
69 };
70 
71 struct Track {
72  const char *name;
73  int64_t duration;
74  int bitrate;
75  int track_id;
77  int width, height;
78  int chunks;
83  int timescale;
84  const char *fourcc;
85  int blocksize;
86  int tag;
87 };
88 
89 struct Tracks {
90  int nb_tracks;
91  int64_t duration;
92  struct Track **tracks;
95 };
96 
97 static int expect_tag(int32_t got_tag, int32_t expected_tag) {
98  if (got_tag != expected_tag) {
99  char got_tag_str[4], expected_tag_str[4];
100  AV_WB32(got_tag_str, got_tag);
101  AV_WB32(expected_tag_str, expected_tag);
102  fprintf(stderr, "wanted tag %.4s, got %.4s\n", expected_tag_str,
103  got_tag_str);
104  return -1;
105  }
106  return 0;
107 }
108 
109 static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
110 {
111  int32_t size, tag;
112 
113  size = avio_rb32(in);
114  tag = avio_rb32(in);
115  avio_wb32(out, size);
116  avio_wb32(out, tag);
117  if (expect_tag(tag, tag_name) != 0)
118  return -1;
119  size -= 8;
120  while (size > 0) {
121  char buf[1024];
122  int len = FFMIN(sizeof(buf), size);
123  int got;
124  if ((got = avio_read(in, buf, len)) != len) {
125  fprintf(stderr, "short read, wanted %d, got %d\n", len, got);
126  break;
127  }
128  avio_write(out, buf, len);
129  size -= len;
130  }
131  return 0;
132 }
133 
134 static int skip_tag(AVIOContext *in, int32_t tag_name)
135 {
136  int64_t pos = avio_tell(in);
137  int32_t size, tag;
138 
139  size = avio_rb32(in);
140  tag = avio_rb32(in);
141  if (expect_tag(tag, tag_name) != 0)
142  return -1;
143  avio_seek(in, pos + size, SEEK_SET);
144  return 0;
145 }
146 
147 static int write_fragment(const char *filename, AVIOContext *in)
148 {
149  AVIOContext *out = NULL;
150  int ret;
151 
152  if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0) {
153  char errbuf[100];
154  av_strerror(ret, errbuf, sizeof(errbuf));
155  fprintf(stderr, "Unable to open %s: %s\n", filename, errbuf);
156  return ret;
157  }
158  ret = copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
159  if (!ret)
160  ret = copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
161 
162  avio_flush(out);
163  avio_close(out);
164 
165  return ret;
166 }
167 
169 {
170  int ret;
171  ret = skip_tag(in, MKBETAG('m', 'o', 'o', 'f'));
172  if (!ret)
173  ret = skip_tag(in, MKBETAG('m', 'd', 'a', 't'));
174  return ret;
175 }
176 
177 static int write_fragments(struct Tracks *tracks, int start_index,
178  AVIOContext *in, const char *basename,
179  int split, int ismf, const char* output_prefix)
180 {
181  char dirname[2048], filename[2048], idxname[2048];
182  int i, j, ret = 0, fragment_ret;
183  FILE* out = NULL;
184 
185  if (ismf) {
186  snprintf(idxname, sizeof(idxname), "%s%s.ismf", output_prefix, basename);
187  out = fopen(idxname, "w");
188  if (!out) {
189  ret = AVERROR(errno);
190  perror(idxname);
191  goto fail;
192  }
193  }
194  for (i = start_index; i < tracks->nb_tracks; i++) {
195  struct Track *track = tracks->tracks[i];
196  const char *type = track->is_video ? "video" : "audio";
197  snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
198  if (split) {
199  if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
200  ret = AVERROR(errno);
201  perror(dirname);
202  goto fail;
203  }
204  }
205  for (j = 0; j < track->chunks; j++) {
206  snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
207  dirname, type, track->offsets[j].time);
208  avio_seek(in, track->offsets[j].offset, SEEK_SET);
209  if (ismf)
210  fprintf(out, "%s %"PRId64, filename, avio_tell(in));
211  if (split)
212  fragment_ret = write_fragment(filename, in);
213  else
214  fragment_ret = skip_fragment(in);
215  if (ismf)
216  fprintf(out, " %"PRId64"\n", avio_tell(in));
217  if (fragment_ret != 0) {
218  fprintf(stderr, "failed fragment %d in track %d (%s)\n", j,
219  track->track_id, track->name);
220  ret = fragment_ret;
221  }
222  }
223  }
224 fail:
225  if (out)
226  fclose(out);
227  return ret;
228 }
229 
230 static int64_t read_trun_duration(AVIOContext *in, int default_duration,
231  int64_t end)
232 {
233  int64_t ret = 0;
234  int64_t pos;
235  int flags, i;
236  int entries;
237  avio_r8(in); /* version */
238  flags = avio_rb24(in);
239  if (default_duration <= 0 && !(flags & MOV_TRUN_SAMPLE_DURATION)) {
240  fprintf(stderr, "No sample duration in trun flags\n");
241  return -1;
242  }
243  entries = avio_rb32(in);
244 
245  if (flags & MOV_TRUN_DATA_OFFSET) avio_rb32(in);
246  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) avio_rb32(in);
247 
248  pos = avio_tell(in);
249  for (i = 0; i < entries && pos < end; i++) {
250  int sample_duration = default_duration;
251  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(in);
252  if (flags & MOV_TRUN_SAMPLE_SIZE) avio_rb32(in);
253  if (flags & MOV_TRUN_SAMPLE_FLAGS) avio_rb32(in);
254  if (flags & MOV_TRUN_SAMPLE_CTS) avio_rb32(in);
255  if (sample_duration < 0) {
256  fprintf(stderr, "Negative sample duration %d\n", sample_duration);
257  return -1;
258  }
259  ret += sample_duration;
260  pos = avio_tell(in);
261  }
262 
263  return ret;
264 }
265 
266 static int64_t read_moof_duration(AVIOContext *in, int64_t offset)
267 {
268  int64_t ret = -1;
269  int32_t moof_size, size, tag;
270  int64_t pos = 0;
271  int default_duration = 0;
272 
273  avio_seek(in, offset, SEEK_SET);
274  moof_size = avio_rb32(in);
275  tag = avio_rb32(in);
276  if (expect_tag(tag, MKBETAG('m', 'o', 'o', 'f')) != 0)
277  goto fail;
278  while (pos < offset + moof_size) {
279  pos = avio_tell(in);
280  size = avio_rb32(in);
281  tag = avio_rb32(in);
282  if (tag == MKBETAG('t', 'r', 'a', 'f')) {
283  int64_t traf_pos = pos;
284  int64_t traf_size = size;
285  while (pos < traf_pos + traf_size) {
286  pos = avio_tell(in);
287  size = avio_rb32(in);
288  tag = avio_rb32(in);
289  if (tag == MKBETAG('t', 'f', 'h', 'd')) {
290  int flags = 0;
291  avio_r8(in); /* version */
292  flags = avio_rb24(in);
293  avio_rb32(in); /* track_id */
294  if (flags & MOV_TFHD_BASE_DATA_OFFSET)
295  avio_rb64(in);
296  if (flags & MOV_TFHD_STSD_ID)
297  avio_rb32(in);
298  if (flags & MOV_TFHD_DEFAULT_DURATION)
299  default_duration = avio_rb32(in);
300  }
301  if (tag == MKBETAG('t', 'r', 'u', 'n')) {
302  return read_trun_duration(in, default_duration,
303  pos + size);
304  }
305  avio_seek(in, pos + size, SEEK_SET);
306  }
307  fprintf(stderr, "Couldn't find trun\n");
308  goto fail;
309  }
310  avio_seek(in, pos + size, SEEK_SET);
311  }
312  fprintf(stderr, "Couldn't find traf\n");
313 
314 fail:
315  return ret;
316 }
317 
318 static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
319 {
320  int ret = AVERROR_EOF, track_id;
321  int version, fieldlength, i, j;
322  int64_t pos = avio_tell(f);
323  uint32_t size = avio_rb32(f);
324  struct Track *track = NULL;
325 
326  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
327  goto fail;
328  version = avio_r8(f);
329  avio_rb24(f);
330  track_id = avio_rb32(f); /* track id */
331  for (i = start_index; i < tracks->nb_tracks && !track; i++)
332  if (tracks->tracks[i]->track_id == track_id)
333  track = tracks->tracks[i];
334  if (!track) {
335  /* Ok, continue parsing the next atom */
336  ret = 0;
337  goto fail;
338  }
339  fieldlength = avio_rb32(f);
340  track->chunks = avio_rb32(f);
341  track->offsets = av_mallocz_array(track->chunks, sizeof(*track->offsets));
342  if (!track->offsets) {
343  ret = AVERROR(ENOMEM);
344  goto fail;
345  }
346  // The duration here is always the difference between consecutive
347  // start times.
348  for (i = 0; i < track->chunks; i++) {
349  if (version == 1) {
350  track->offsets[i].time = avio_rb64(f);
351  track->offsets[i].offset = avio_rb64(f);
352  } else {
353  track->offsets[i].time = avio_rb32(f);
354  track->offsets[i].offset = avio_rb32(f);
355  }
356  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
357  avio_r8(f);
358  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
359  avio_r8(f);
360  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
361  avio_r8(f);
362  if (i > 0)
363  track->offsets[i - 1].duration = track->offsets[i].time -
364  track->offsets[i - 1].time;
365  }
366  if (track->chunks > 0) {
367  track->offsets[track->chunks - 1].duration = track->offsets[0].time +
368  track->duration -
369  track->offsets[track->chunks - 1].time;
370  }
371  // Now try and read the actual durations from the trun sample data.
372  for (i = 0; i < track->chunks; i++) {
373  int64_t duration = read_moof_duration(f, track->offsets[i].offset);
374  if (duration > 0 && abs(duration - track->offsets[i].duration) > 3) {
375  // 3 allows for integer duration to drift a few units,
376  // e.g., for 1/3 durations
377  track->offsets[i].duration = duration;
378  }
379  }
380  if (track->chunks > 0) {
381  if (track->offsets[track->chunks - 1].duration <= 0) {
382  fprintf(stderr, "Calculated last chunk duration for track %d "
383  "was non-positive (%"PRId64"), probably due to missing "
384  "fragments ", track->track_id,
385  track->offsets[track->chunks - 1].duration);
386  if (track->chunks > 1) {
387  track->offsets[track->chunks - 1].duration =
388  track->offsets[track->chunks - 2].duration;
389  } else {
390  track->offsets[track->chunks - 1].duration = 1;
391  }
392  fprintf(stderr, "corrected to %"PRId64"\n",
393  track->offsets[track->chunks - 1].duration);
394  track->duration = track->offsets[track->chunks - 1].time +
395  track->offsets[track->chunks - 1].duration -
396  track->offsets[0].time;
397  fprintf(stderr, "Track duration corrected to %"PRId64"\n",
398  track->duration);
399  }
400  }
401  ret = 0;
402 
403 fail:
404  avio_seek(f, pos + size, SEEK_SET);
405  return ret;
406 }
407 
408 static int read_mfra(struct Tracks *tracks, int start_index,
409  const char *file, int split, int ismf,
410  const char *basename, const char* output_prefix)
411 {
412  int err = 0;
413  const char* err_str = "";
414  AVIOContext *f = NULL;
415  int32_t mfra_size;
416 
417  if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
418  goto fail;
419  avio_seek(f, avio_size(f) - 4, SEEK_SET);
420  mfra_size = avio_rb32(f);
421  avio_seek(f, -mfra_size, SEEK_CUR);
422  if (avio_rb32(f) != mfra_size) {
423  err = AVERROR_INVALIDDATA;
424  err_str = "mfra size mismatch";
425  goto fail;
426  }
427  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
428  err = AVERROR_INVALIDDATA;
429  err_str = "mfra tag mismatch";
430  goto fail;
431  }
432  while (!read_tfra(tracks, start_index, f)) {
433  /* Empty */
434  }
435 
436  if (split || ismf)
437  err = write_fragments(tracks, start_index, f, basename, split, ismf,
438  output_prefix);
439  err_str = "error in write_fragments";
440 
441 fail:
442  if (f)
443  avio_close(f);
444  if (err)
445  fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
446  return err;
447 }
448 
449 static int get_private_data(struct Track *track, AVCodecContext *codec)
450 {
451  track->codec_private_size = codec->extradata_size;
452  track->codec_private = av_mallocz(codec->extradata_size);
453  if (!track->codec_private)
454  return AVERROR(ENOMEM);
455  memcpy(track->codec_private, codec->extradata, codec->extradata_size);
456  return 0;
457 }
458 
459 static int get_video_private_data(struct Track *track, AVCodecContext *codec)
460 {
461  AVIOContext *io = NULL;
462  uint16_t sps_size, pps_size;
463  int err;
464 
465  if (codec->codec_id == AV_CODEC_ID_VC1)
466  return get_private_data(track, codec);
467 
468  if ((err = avio_open_dyn_buf(&io)) < 0)
469  goto fail;
470  err = AVERROR(EINVAL);
471  if (codec->extradata_size < 11 || codec->extradata[0] != 1)
472  goto fail;
473  sps_size = AV_RB16(&codec->extradata[6]);
474  if (11 + sps_size > codec->extradata_size)
475  goto fail;
476  avio_wb32(io, 0x00000001);
477  avio_write(io, &codec->extradata[8], sps_size);
478  pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
479  if (11 + sps_size + pps_size > codec->extradata_size)
480  goto fail;
481  avio_wb32(io, 0x00000001);
482  avio_write(io, &codec->extradata[11 + sps_size], pps_size);
483  err = 0;
484 
485 fail:
487  return err;
488 }
489 
490 static int handle_file(struct Tracks *tracks, const char *file, int split,
491  int ismf, const char *basename,
492  const char* output_prefix)
493 {
494  AVFormatContext *ctx = NULL;
495  int err = 0, i, orig_tracks = tracks->nb_tracks;
496  char errbuf[50], *ptr;
497  struct Track *track;
498 
499  err = avformat_open_input(&ctx, file, NULL, NULL);
500  if (err < 0) {
501  av_strerror(err, errbuf, sizeof(errbuf));
502  fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
503  return 1;
504  }
505 
506  err = avformat_find_stream_info(ctx, NULL);
507  if (err < 0) {
508  av_strerror(err, errbuf, sizeof(errbuf));
509  fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
510  goto fail;
511  }
512 
513  if (ctx->nb_streams < 1) {
514  fprintf(stderr, "No streams found in %s\n", file);
515  goto fail;
516  }
517 
518  for (i = 0; i < ctx->nb_streams; i++) {
519  struct Track **temp;
520  AVStream *st = ctx->streams[i];
521 
522  if (st->codec->bit_rate == 0) {
523  fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
524  st->id, file);
525  continue;
526  }
527 
528  track = av_mallocz(sizeof(*track));
529  if (!track) {
530  err = AVERROR(ENOMEM);
531  goto fail;
532  }
533  temp = av_realloc(tracks->tracks,
534  sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
535  if (!temp) {
536  av_free(track);
537  err = AVERROR(ENOMEM);
538  goto fail;
539  }
540  tracks->tracks = temp;
541  tracks->tracks[tracks->nb_tracks] = track;
542 
543  track->name = file;
544  if ((ptr = strrchr(file, '/')))
545  track->name = ptr + 1;
546 
547  track->bitrate = st->codec->bit_rate;
548  track->track_id = st->id;
549  track->timescale = st->time_base.den;
550  track->duration = st->duration;
551  track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
552  track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
553 
554  if (!track->is_audio && !track->is_video) {
555  fprintf(stderr,
556  "Track %d in %s is neither video nor audio, skipping\n",
557  track->track_id, file);
558  av_freep(&tracks->tracks[tracks->nb_tracks]);
559  continue;
560  }
561 
562  tracks->duration = FFMAX(tracks->duration,
564  track->timescale, AV_ROUND_UP));
565 
566  if (track->is_audio) {
567  if (tracks->audio_track < 0)
568  tracks->audio_track = tracks->nb_tracks;
569  tracks->nb_audio_tracks++;
570  track->channels = st->codec->channels;
571  track->sample_rate = st->codec->sample_rate;
572  if (st->codec->codec_id == AV_CODEC_ID_AAC) {
573  track->fourcc = "AACL";
574  track->tag = 255;
575  track->blocksize = 4;
576  } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
577  track->fourcc = "WMAP";
578  track->tag = st->codec->codec_tag;
579  track->blocksize = st->codec->block_align;
580  }
581  get_private_data(track, st->codec);
582  }
583  if (track->is_video) {
584  if (tracks->video_track < 0)
585  tracks->video_track = tracks->nb_tracks;
586  tracks->nb_video_tracks++;
587  track->width = st->codec->width;
588  track->height = st->codec->height;
589  if (st->codec->codec_id == AV_CODEC_ID_H264)
590  track->fourcc = "H264";
591  else if (st->codec->codec_id == AV_CODEC_ID_VC1)
592  track->fourcc = "WVC1";
593  get_video_private_data(track, st->codec);
594  }
595 
596  tracks->nb_tracks++;
597  }
598 
599  avformat_close_input(&ctx);
600 
601  err = read_mfra(tracks, orig_tracks, file, split, ismf, basename,
602  output_prefix);
603 
604 fail:
605  if (ctx)
606  avformat_close_input(&ctx);
607  return err;
608 }
609 
610 static void output_server_manifest(struct Tracks *tracks, const char *basename,
611  const char *output_prefix,
612  const char *path_prefix,
613  const char *ismc_prefix)
614 {
615  char filename[1000];
616  FILE *out;
617  int i;
618 
619  snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
620  out = fopen(filename, "w");
621  if (!out) {
622  perror(filename);
623  return;
624  }
625  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
626  fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
627  fprintf(out, "\t<head>\n");
628  fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
629  "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
630  fprintf(out, "\t</head>\n");
631  fprintf(out, "\t<body>\n");
632  fprintf(out, "\t\t<switch>\n");
633  for (i = 0; i < tracks->nb_tracks; i++) {
634  struct Track *track = tracks->tracks[i];
635  const char *type = track->is_video ? "video" : "audio";
636  fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
637  type, path_prefix, track->name, track->bitrate);
638  fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
639  "valueType=\"data\" />\n", track->track_id);
640  fprintf(out, "\t\t\t</%s>\n", type);
641  }
642  fprintf(out, "\t\t</switch>\n");
643  fprintf(out, "\t</body>\n");
644  fprintf(out, "</smil>\n");
645  fclose(out);
646 }
647 
648 static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
649  const char *type)
650 {
651  int i, j;
652  int64_t pos = 0;
653  struct Track *track = tracks->tracks[main];
654  int should_print_time_mismatch = 1;
655 
656  for (i = 0; i < track->chunks; i++) {
657  for (j = main + 1; j < tracks->nb_tracks; j++) {
658  if (tracks->tracks[j]->is_audio == track->is_audio) {
659  if (track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration) {
660  fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
661  type, i, track->name, main, tracks->tracks[j]->name, j);
662  should_print_time_mismatch = 1;
663  }
664  if (track->offsets[i].time != tracks->tracks[j]->offsets[i].time) {
665  if (should_print_time_mismatch)
666  fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
667  type, i, track->name, main, tracks->tracks[j]->name, j);
668  should_print_time_mismatch = 0;
669  }
670  }
671  }
672  fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" ",
673  i, track->offsets[i].duration);
674  if (pos != track->offsets[i].time) {
675  fprintf(out, "t=\"%"PRId64"\" ", track->offsets[i].time);
676  pos = track->offsets[i].time;
677  }
678  pos += track->offsets[i].duration;
679  fprintf(out, "/>\n");
680  }
681 }
682 
683 static void output_client_manifest(struct Tracks *tracks, const char *basename,
684  const char *output_prefix, int split)
685 {
686  char filename[1000];
687  FILE *out;
688  int i, j;
689 
690  if (split)
691  snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
692  else
693  snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
694  out = fopen(filename, "w");
695  if (!out) {
696  perror(filename);
697  return;
698  }
699  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
700  fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
701  "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
702  if (tracks->video_track >= 0) {
703  struct Track *track = tracks->tracks[tracks->video_track];
704  struct Track *first_track = track;
705  int index = 0;
706  fprintf(out,
707  "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
708  "Chunks=\"%d\" "
709  "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
710  tracks->nb_video_tracks, track->chunks);
711  for (i = 0; i < tracks->nb_tracks; i++) {
712  track = tracks->tracks[i];
713  if (!track->is_video)
714  continue;
715  fprintf(out,
716  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
717  "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
718  "CodecPrivateData=\"",
719  index, track->bitrate, track->fourcc, track->width, track->height);
720  for (j = 0; j < track->codec_private_size; j++)
721  fprintf(out, "%02X", track->codec_private[j]);
722  fprintf(out, "\" />\n");
723  index++;
724  if (track->chunks != first_track->chunks)
725  fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
726  track->name, track->track_id, track->chunks, first_track->name, first_track->track_id, first_track->chunks);
727  }
728  print_track_chunks(out, tracks, tracks->video_track, "video");
729  fprintf(out, "\t</StreamIndex>\n");
730  }
731  if (tracks->audio_track >= 0) {
732  struct Track *track = tracks->tracks[tracks->audio_track];
733  struct Track *first_track = track;
734  int index = 0;
735  fprintf(out,
736  "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
737  "Chunks=\"%d\" "
738  "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
739  tracks->nb_audio_tracks, track->chunks);
740  for (i = 0; i < tracks->nb_tracks; i++) {
741  track = tracks->tracks[i];
742  if (!track->is_audio)
743  continue;
744  fprintf(out,
745  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
746  "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
747  "BitsPerSample=\"16\" PacketSize=\"%d\" "
748  "AudioTag=\"%d\" CodecPrivateData=\"",
749  index, track->bitrate, track->fourcc, track->sample_rate,
750  track->channels, track->blocksize, track->tag);
751  for (j = 0; j < track->codec_private_size; j++)
752  fprintf(out, "%02X", track->codec_private[j]);
753  fprintf(out, "\" />\n");
754  index++;
755  if (track->chunks != first_track->chunks)
756  fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
757  track->name, first_track->name);
758  }
759  print_track_chunks(out, tracks, tracks->audio_track, "audio");
760  fprintf(out, "\t</StreamIndex>\n");
761  }
762  fprintf(out, "</SmoothStreamingMedia>\n");
763  fclose(out);
764 }
765 
766 static void clean_tracks(struct Tracks *tracks)
767 {
768  int i;
769  for (i = 0; i < tracks->nb_tracks; i++) {
770  av_freep(&tracks->tracks[i]->codec_private);
771  av_freep(&tracks->tracks[i]->offsets);
772  av_freep(&tracks->tracks[i]);
773  }
774  av_freep(&tracks->tracks);
775  tracks->nb_tracks = 0;
776 }
777 
778 int main(int argc, char **argv)
779 {
780  const char *basename = NULL;
781  const char *path_prefix = "", *ismc_prefix = "";
782  const char *output_prefix = "";
783  char output_prefix_buf[2048];
784  int split = 0, ismf = 0, i;
785  struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
786 
787  av_register_all();
788 
789  for (i = 1; i < argc; i++) {
790  if (!strcmp(argv[i], "-n")) {
791  basename = argv[i + 1];
792  i++;
793  } else if (!strcmp(argv[i], "-path-prefix")) {
794  path_prefix = argv[i + 1];
795  i++;
796  } else if (!strcmp(argv[i], "-ismc-prefix")) {
797  ismc_prefix = argv[i + 1];
798  i++;
799  } else if (!strcmp(argv[i], "-output")) {
800  output_prefix = argv[i + 1];
801  i++;
802  if (output_prefix[strlen(output_prefix) - 1] != '/') {
803  snprintf(output_prefix_buf, sizeof(output_prefix_buf),
804  "%s/", output_prefix);
805  output_prefix = output_prefix_buf;
806  }
807  } else if (!strcmp(argv[i], "-split")) {
808  split = 1;
809  } else if (!strcmp(argv[i], "-ismf")) {
810  ismf = 1;
811  } else if (argv[i][0] == '-') {
812  return usage(argv[0], 1);
813  } else {
814  if (!basename)
815  ismf = 0;
816  if (handle_file(&tracks, argv[i], split, ismf,
817  basename, output_prefix))
818  return 1;
819  }
820  }
821  if (!tracks.nb_tracks || (!basename && !split))
822  return usage(argv[0], 1);
823 
824  if (!split)
825  output_server_manifest(&tracks, basename, output_prefix,
826  path_prefix, ismc_prefix);
827  output_client_manifest(&tracks, basename, output_prefix, split);
828 
829  clean_tracks(&tracks);
830 
831  return 0;
832 }