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