FFmpeg
 All Data Structures 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 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  * 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  * To pre-split files for serving as static files by a web server without
29  * any extra server support, create the ismv file as above, and split it:
30  * ismindex -split foo.ismv
31  * This step creates a file Manifest and directories QualityLevel(...),
32  * that can be read directly by a smooth streaming player.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #ifdef _WIN32
39 #include <direct.h>
40 #define mkdir(a, b) _mkdir(a)
41 #endif
42 
43 #include "cmdutils.h"
44 
45 #include "libavformat/avformat.h"
46 #include "libavutil/intreadwrite.h"
47 #include "libavutil/mathematics.h"
48 
49 static int usage(const char *argv0, int ret)
50 {
51  fprintf(stderr, "%s [-split] [-n basename] file1 [file2] ...\n", argv0);
52  return ret;
53 }
54 
55 struct MoofOffset {
56  int64_t time;
57  int64_t offset;
58  int duration;
59 };
60 
61 struct VideoFile {
62  const char *name;
63  int64_t duration;
64  int bitrate;
65  int track_id;
67  int width, height;
68  int chunks;
73  int timescale;
74  const char *fourcc;
75  int blocksize;
76  int tag;
77 };
78 
79 struct VideoFiles {
80  int nb_files;
81  int64_t duration;
82  struct VideoFile **files;
85 };
86 
87 static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
88 {
89  int32_t size, tag;
90 
91  size = avio_rb32(in);
92  tag = avio_rb32(in);
93  avio_wb32(out, size);
94  avio_wb32(out, tag);
95  if (tag != tag_name)
96  return -1;
97  size -= 8;
98  while (size > 0) {
99  char buf[1024];
100  int len = FFMIN(sizeof(buf), size);
101  if (avio_read(in, buf, len) != len)
102  break;
103  avio_write(out, buf, len);
104  size -= len;
105  }
106  return 0;
107 }
108 
109 static int write_fragment(const char *filename, AVIOContext *in)
110 {
111  AVIOContext *out = NULL;
112  int ret;
113 
114  if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0)
115  return ret;
116  copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
117  copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
118 
119  avio_flush(out);
120  avio_close(out);
121 
122  return ret;
123 }
124 
125 static int write_fragments(struct VideoFiles *files, int start_index,
126  AVIOContext *in)
127 {
128  char dirname[100], filename[500];
129  int i, j;
130 
131  for (i = start_index; i < files->nb_files; i++) {
132  struct VideoFile *vf = files->files[i];
133  const char *type = vf->is_video ? "video" : "audio";
134  snprintf(dirname, sizeof(dirname), "QualityLevels(%d)", vf->bitrate);
135  mkdir(dirname, 0777);
136  for (j = 0; j < vf->chunks; j++) {
137  snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
138  dirname, type, vf->offsets[j].time);
139  avio_seek(in, vf->offsets[j].offset, SEEK_SET);
140  write_fragment(filename, in);
141  }
142  }
143  return 0;
144 }
145 
146 static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f)
147 {
148  int ret = AVERROR_EOF, track_id;
149  int version, fieldlength, i, j;
150  int64_t pos = avio_tell(f);
151  uint32_t size = avio_rb32(f);
152  struct VideoFile *vf = NULL;
153 
154  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
155  goto fail;
156  version = avio_r8(f);
157  avio_rb24(f);
158  track_id = avio_rb32(f); /* track id */
159  for (i = start_index; i < files->nb_files && !vf; i++)
160  if (files->files[i]->track_id == track_id)
161  vf = files->files[i];
162  if (!vf) {
163  /* Ok, continue parsing the next atom */
164  ret = 0;
165  goto fail;
166  }
167  fieldlength = avio_rb32(f);
168  vf->chunks = avio_rb32(f);
169  vf->offsets = av_mallocz(sizeof(*vf->offsets) * vf->chunks);
170  if (!vf->offsets) {
171  ret = AVERROR(ENOMEM);
172  goto fail;
173  }
174  for (i = 0; i < vf->chunks; i++) {
175  if (version == 1) {
176  vf->offsets[i].time = avio_rb64(f);
177  vf->offsets[i].offset = avio_rb64(f);
178  } else {
179  vf->offsets[i].time = avio_rb32(f);
180  vf->offsets[i].offset = avio_rb32(f);
181  }
182  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
183  avio_r8(f);
184  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
185  avio_r8(f);
186  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
187  avio_r8(f);
188  if (i > 0)
189  vf->offsets[i - 1].duration = vf->offsets[i].time -
190  vf->offsets[i - 1].time;
191  }
192  if (vf->chunks > 0)
193  vf->offsets[vf->chunks - 1].duration = vf->duration -
194  vf->offsets[vf->chunks - 1].time;
195  ret = 0;
196 
197 fail:
198  avio_seek(f, pos + size, SEEK_SET);
199  return ret;
200 }
201 
202 static int read_mfra(struct VideoFiles *files, int start_index,
203  const char *file, int split)
204 {
205  int err = 0;
206  AVIOContext *f = NULL;
207  int32_t mfra_size;
208 
209  if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
210  goto fail;
211  avio_seek(f, avio_size(f) - 4, SEEK_SET);
212  mfra_size = avio_rb32(f);
213  avio_seek(f, -mfra_size, SEEK_CUR);
214  if (avio_rb32(f) != mfra_size) {
215  err = AVERROR_INVALIDDATA;
216  goto fail;
217  }
218  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
219  err = AVERROR_INVALIDDATA;
220  goto fail;
221  }
222  while (!read_tfra(files, start_index, f)) {
223  /* Empty */
224  }
225 
226  if (split)
227  write_fragments(files, start_index, f);
228 
229 fail:
230  if (f)
231  avio_close(f);
232  if (err)
233  fprintf(stderr, "Unable to read the MFRA atom in %s\n", file);
234  return err;
235 }
236 
237 static int get_private_data(struct VideoFile *vf, AVCodecContext *codec)
238 {
239  vf->codec_private_size = codec->extradata_size;
241  if (!vf->codec_private)
242  return AVERROR(ENOMEM);
243  memcpy(vf->codec_private, codec->extradata, codec->extradata_size);
244  return 0;
245 }
246 
247 static int get_video_private_data(struct VideoFile *vf, AVCodecContext *codec)
248 {
249  AVIOContext *io = NULL;
250  uint16_t sps_size, pps_size;
251  int err = AVERROR(EINVAL);
252 
253  if (codec->codec_id == AV_CODEC_ID_VC1)
254  return get_private_data(vf, codec);
255 
256  if (avio_open_dyn_buf(&io) < 0) {
257  err = AVERROR(ENOMEM);
258  goto fail;
259  }
260  if (codec->extradata_size < 11 || codec->extradata[0] != 1)
261  goto fail;
262  sps_size = AV_RB16(&codec->extradata[6]);
263  if (11 + sps_size > codec->extradata_size)
264  goto fail;
265  avio_wb32(io, 0x00000001);
266  avio_write(io, &codec->extradata[8], sps_size);
267  pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
268  if (11 + sps_size + pps_size > codec->extradata_size)
269  goto fail;
270  avio_wb32(io, 0x00000001);
271  avio_write(io, &codec->extradata[11 + sps_size], pps_size);
272  err = 0;
273 
274 fail:
276  return err;
277 }
278 
279 static int handle_file(struct VideoFiles *files, const char *file, int split)
280 {
281  AVFormatContext *ctx = NULL;
282  int err = 0, i, orig_files = files->nb_files;
283  char errbuf[50], *ptr;
284  struct VideoFile *vf;
285 
286  err = avformat_open_input(&ctx, file, NULL, NULL);
287  if (err < 0) {
288  av_strerror(err, errbuf, sizeof(errbuf));
289  fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
290  return 1;
291  }
292 
293  err = avformat_find_stream_info(ctx, NULL);
294  if (err < 0) {
295  av_strerror(err, errbuf, sizeof(errbuf));
296  fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
297  goto fail;
298  }
299 
300  if (ctx->nb_streams < 1) {
301  fprintf(stderr, "No streams found in %s\n", file);
302  goto fail;
303  }
304  if (!files->duration)
305  files->duration = ctx->duration;
306 
307  for (i = 0; i < ctx->nb_streams; i++) {
308  AVStream *st = ctx->streams[i];
309  vf = av_mallocz(sizeof(*vf));
310  files->files = av_realloc(files->files,
311  sizeof(*files->files) * (files->nb_files + 1));
312  files->files[files->nb_files] = vf;
313 
314  vf->name = file;
315  if ((ptr = strrchr(file, '/')) != NULL)
316  vf->name = ptr + 1;
317 
318  vf->bitrate = st->codec->bit_rate;
319  vf->track_id = st->id;
320  vf->timescale = st->time_base.den;
321  vf->duration = av_rescale_rnd(ctx->duration, vf->timescale,
325 
326  if (!vf->is_audio && !vf->is_video) {
327  fprintf(stderr,
328  "Track %d in %s is neither video nor audio, skipping\n",
329  vf->track_id, file);
330  av_freep(&files->files[files->nb_files]);
331  continue;
332  }
333 
334  if (vf->is_audio) {
335  if (files->audio_file < 0)
336  files->audio_file = files->nb_files;
337  files->nb_audio_files++;
338  vf->channels = st->codec->channels;
339  vf->sample_rate = st->codec->sample_rate;
340  if (st->codec->codec_id == AV_CODEC_ID_AAC) {
341  vf->fourcc = "AACL";
342  vf->tag = 255;
343  vf->blocksize = 4;
344  } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
345  vf->fourcc = "WMAP";
346  vf->tag = st->codec->codec_tag;
347  vf->blocksize = st->codec->block_align;
348  }
349  get_private_data(vf, st->codec);
350  }
351  if (vf->is_video) {
352  if (files->video_file < 0)
353  files->video_file = files->nb_files;
354  files->nb_video_files++;
355  vf->width = st->codec->width;
356  vf->height = st->codec->height;
357  if (st->codec->codec_id == AV_CODEC_ID_H264)
358  vf->fourcc = "H264";
359  else if (st->codec->codec_id == AV_CODEC_ID_VC1)
360  vf->fourcc = "WVC1";
361  get_video_private_data(vf, st->codec);
362  }
363 
364  files->nb_files++;
365  }
366 
367  avformat_close_input(&ctx);
368 
369  err = read_mfra(files, orig_files, file, split);
370 
371 fail:
372  if (ctx)
373  avformat_close_input(&ctx);
374  return err;
375 }
376 
377 static void output_server_manifest(struct VideoFiles *files,
378  const char *basename)
379 {
380  char filename[1000];
381  FILE *out;
382  int i;
383 
384  snprintf(filename, sizeof(filename), "%s.ism", basename);
385  out = fopen(filename, "w");
386  if (!out) {
387  perror(filename);
388  return;
389  }
390  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
391  fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
392  fprintf(out, "\t<head>\n");
393  fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
394  "content=\"%s.ismc\" />\n", basename);
395  fprintf(out, "\t</head>\n");
396  fprintf(out, "\t<body>\n");
397  fprintf(out, "\t\t<switch>\n");
398  for (i = 0; i < files->nb_files; i++) {
399  struct VideoFile *vf = files->files[i];
400  const char *type = vf->is_video ? "video" : "audio";
401  fprintf(out, "\t\t\t<%s src=\"%s\" systemBitrate=\"%d\">\n",
402  type, vf->name, vf->bitrate);
403  fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
404  "valueType=\"data\" />\n", vf->track_id);
405  fprintf(out, "\t\t\t</%s>\n", type);
406  }
407  fprintf(out, "\t\t</switch>\n");
408  fprintf(out, "\t</body>\n");
409  fprintf(out, "</smil>\n");
410  fclose(out);
411 }
412 
413 static void output_client_manifest(struct VideoFiles *files,
414  const char *basename, int split)
415 {
416  char filename[1000];
417  FILE *out;
418  int i, j;
419 
420  if (split)
421  snprintf(filename, sizeof(filename), "Manifest");
422  else
423  snprintf(filename, sizeof(filename), "%s.ismc", basename);
424  out = fopen(filename, "w");
425  if (!out) {
426  perror(filename);
427  return;
428  }
429  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
430  fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
431  "Duration=\"%"PRId64 "\">\n", files->duration * 10);
432  if (files->video_file >= 0) {
433  struct VideoFile *vf = files->files[files->video_file];
434  struct VideoFile *first_vf = vf;
435  int index = 0;
436  fprintf(out,
437  "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
438  "Chunks=\"%d\" "
439  "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
440  files->nb_video_files, vf->chunks);
441  for (i = 0; i < files->nb_files; i++) {
442  vf = files->files[i];
443  if (!vf->is_video)
444  continue;
445  fprintf(out,
446  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
447  "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
448  "CodecPrivateData=\"",
449  index, vf->bitrate, vf->fourcc, vf->width, vf->height);
450  for (j = 0; j < vf->codec_private_size; j++)
451  fprintf(out, "%02X", vf->codec_private[j]);
452  fprintf(out, "\" />\n");
453  index++;
454  if (vf->chunks != first_vf->chunks)
455  fprintf(stderr, "Mismatched number of video chunks in %s and %s\n",
456  vf->name, first_vf->name);
457  }
458  vf = first_vf;
459  for (i = 0; i < vf->chunks; i++) {
460  for (j = files->video_file + 1; j < files->nb_files; j++) {
461  if (files->files[j]->is_video &&
462  vf->offsets[i].duration != files->files[j]->offsets[i].duration)
463  fprintf(stderr, "Mismatched duration of video chunk %d in %s and %s\n",
464  i, vf->name, files->files[j]->name);
465  }
466  fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n", i,
467  vf->offsets[i].duration);
468  }
469  fprintf(out, "\t</StreamIndex>\n");
470  }
471  if (files->audio_file >= 0) {
472  struct VideoFile *vf = files->files[files->audio_file];
473  struct VideoFile *first_vf = vf;
474  int index = 0;
475  fprintf(out,
476  "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
477  "Chunks=\"%d\" "
478  "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
479  files->nb_audio_files, vf->chunks);
480  for (i = 0; i < files->nb_files; i++) {
481  vf = files->files[i];
482  if (!vf->is_audio)
483  continue;
484  fprintf(out,
485  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
486  "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
487  "BitsPerSample=\"16\" PacketSize=\"%d\" "
488  "AudioTag=\"%d\" CodecPrivateData=\"",
489  index, vf->bitrate, vf->fourcc, vf->sample_rate,
490  vf->channels, vf->blocksize, vf->tag);
491  for (j = 0; j < vf->codec_private_size; j++)
492  fprintf(out, "%02X", vf->codec_private[j]);
493  fprintf(out, "\" />\n");
494  index++;
495  if (vf->chunks != first_vf->chunks)
496  fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
497  vf->name, first_vf->name);
498  }
499  vf = first_vf;
500  for (i = 0; i < vf->chunks; i++) {
501  for (j = files->audio_file + 1; j < files->nb_files; j++) {
502  if (files->files[j]->is_audio &&
503  vf->offsets[i].duration != files->files[j]->offsets[i].duration)
504  fprintf(stderr, "Mismatched duration of audio chunk %d in %s and %s\n",
505  i, vf->name, files->files[j]->name);
506  }
507  fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n",
508  i, vf->offsets[i].duration);
509  }
510  fprintf(out, "\t</StreamIndex>\n");
511  }
512  fprintf(out, "</SmoothStreamingMedia>\n");
513  fclose(out);
514 }
515 
516 static void clean_files(struct VideoFiles *files)
517 {
518  int i;
519  for (i = 0; i < files->nb_files; i++) {
520  av_freep(&files->files[i]->codec_private);
521  av_freep(&files->files[i]->offsets);
522  av_freep(&files->files[i]);
523  }
524  av_freep(&files->files);
525  files->nb_files = 0;
526 }
527 
528 int main(int argc, char **argv)
529 {
530  const char *basename = NULL;
531  int split = 0, i;
532  struct VideoFiles vf = { 0, .video_file = -1, .audio_file = -1 };
533 
534  av_register_all();
535 
536  for (i = 1; i < argc; i++) {
537  if (!strcmp(argv[i], "-n")) {
538  basename = argv[i + 1];
539  i++;
540  } else if (!strcmp(argv[i], "-split")) {
541  split = 1;
542  } else if (argv[i][0] == '-') {
543  return usage(argv[0], 1);
544  } else {
545  if (handle_file(&vf, argv[i], split))
546  return 1;
547  }
548  }
549  if (!vf.nb_files || (!basename && !split))
550  return usage(argv[0], 1);
551 
552  if (!split)
553  output_server_manifest(&vf, basename);
554  output_client_manifest(&vf, basename, split);
555 
556  clean_files(&vf);
557 
558  return 0;
559 }