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
51
57
59 {
60 fprintf(stderr, "%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
61 "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
63 }
64
69 };
70
87 };
88
95 };
96
98 if (got_tag != expected_tag) {
99 char got_tag_str[4], expected_tag_str[4];
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
110 {
112
118 return -1;
119 size -= 8;
120 while (size > 0) {
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 }
130 }
131 return 0;
132 }
133
135 {
138
142 return -1;
144 return 0;
145 }
146
148 {
151
153 char errbuf[100];
155 fprintf(stderr, "Unable to open %s: %s\n", filename, errbuf);
157 }
159 if (!ret)
161
164
166 }
167
169 {
172 if (!ret)
175 }
176
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;
184
185 if (ismf) {
186 snprintf(idxname,
sizeof(idxname),
"%s%s.ismf", output_prefix, basename);
187 out = fopen(idxname, "w");
188 if (!out) {
190 perror(idxname);
191 goto fail;
192 }
193 }
194 for (i = start_index; i < tracks->
nb_tracks; i++) {
197 snprintf(dirname,
sizeof(dirname),
"%sQualityLevels(%d)", output_prefix, track->
bitrate);
198 if (split) {
199 if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
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
")",
209 if (ismf)
210 fprintf(out,
"%s %"PRId64, filename,
avio_tell(in));
211 if (split)
213 else
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,
220 ret = fragment_ret;
221 }
222 }
223 }
224 fail:
225 if (out)
226 fclose(out);
228 }
229
232 {
234 int64_t pos;
236 int entries;
240 fprintf(stderr, "No sample duration in trun flags\n");
241 return -1;
242 }
244
247
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);
255 if (sample_duration < 0) {
256 fprintf(stderr, "Negative sample duration %d\n", sample_duration);
257 return -1;
258 }
259 ret += sample_duration;
261 }
262
264 }
265
267 {
270 int64_t pos = 0;
271 int default_duration = 0;
272
277 goto fail;
278 while (pos < offset + moof_size) {
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) {
289 if (tag ==
MKBETAG(
't',
'f',
'h',
'd')) {
300 }
301 if (tag ==
MKBETAG(
't',
'r',
'u',
'n')) {
303 pos + size);
304 }
306 }
307 fprintf(stderr, "Couldn't find trun\n");
308 goto fail;
309 }
311 }
312 fprintf(stderr, "Couldn't find traf\n");
313
314 fail:
316 }
317
319 {
321 int version, fieldlength, i, j;
325
327 goto fail;
331 for (i = start_index; i < tracks->
nb_tracks && !track; i++)
333 track = tracks->
tracks[i];
334 if (!track) {
335 /* Ok, continue parsing the next atom */
336 ret = 0;
337 goto fail;
338 }
345 goto fail;
346 }
347 // The duration here is always the difference between consecutive
348 // start times.
349 for (i = 0; i < track->
chunks; i++) {
350 if (version == 1) {
353 } else {
356 }
357 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
359 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
361 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
363 if (i > 0)
366 }
371 }
372 // Now try and read the actual durations from the trun sample data.
373 for (i = 0; i < track->
chunks; i++) {
376 // 3 allows for integer duration to drift a few units,
377 // e.g., for 1/3 durations
379 }
380 }
383 fprintf(stderr, "Calculated last chunk duration for track %d "
384 "was non-positive (%"PRId64"), probably due to missing "
390 } else {
392 }
393 fprintf(stderr, "corrected to %"PRId64"\n",
398 fprintf(stderr, "Track duration corrected to %"PRId64"\n",
400 }
401 }
402 ret = 0;
403
404 fail:
407 }
408
410 const char *file,
int split,
int ismf,
411 const char *basename, const char* output_prefix)
412 {
413 int err = 0;
414 const char* err_str = "";
417
419 goto fail;
425 err_str = "mfra size mismatch";
426 goto fail;
427 }
430 err_str = "mfra tag mismatch";
431 goto fail;
432 }
433 while (!
read_tfra(tracks, start_index, f)) {
434 /* Empty */
435 }
436
437 if (split || ismf)
439 output_prefix);
440 err_str = "error in write_fragments";
441
442 fail:
443 if (f)
445 if (err)
446 fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
447 return err;
448 }
449
451 {
458 return 0;
459 }
460
462 {
464 uint16_t sps_size, pps_size;
465 int err;
466
469
471 goto fail;
474 goto fail;
477 goto fail;
482 goto fail;
485 err = 0;
486
487 fail:
489 return err;
490 }
491
493 int ismf, const char *basename,
494 const char* output_prefix)
495 {
497 int err = 0, i, orig_tracks = tracks->
nb_tracks;
498 char errbuf[50], *ptr;
500
502 if (err < 0) {
504 fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
505 return 1;
506 }
507
509 if (err < 0) {
511 fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
512 goto fail;
513 }
514
516 fprintf(stderr, "No streams found in %s\n", file);
517 goto fail;
518 }
519
523
525 fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
527 continue;
528 }
529
531 if (!track) {
533 goto fail;
534 }
538 if (!temp) {
541 goto fail;
542 }
545
547 if ((ptr = strrchr(file, '/')))
548 track->
name = ptr + 1;
549
556
558 fprintf(stderr,
559 "Track %d in %s is neither video nor audio, skipping\n",
562 continue;
563 }
564
568
583 }
585 }
597 }
598
600 }
601
603
604 err =
read_mfra(tracks, orig_tracks, file, split, ismf, basename,
605 output_prefix);
606
607 fail:
608 if (ctx)
610 return err;
611 }
612
614 const char *output_prefix,
615 const char *path_prefix,
616 const char *ismc_prefix)
617 {
618 char filename[1000];
620 int i;
621
622 snprintf(filename,
sizeof(filename),
"%s%s.ism", output_prefix, basename);
623 out = fopen(filename, "w");
624 if (!out) {
625 perror(filename);
626 return;
627 }
628 fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
629 fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
630 fprintf(out, "\t<head>\n");
631 fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
632 "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
633 fprintf(out, "\t</head>\n");
634 fprintf(out, "\t<body>\n");
635 fprintf(out, "\t\t<switch>\n");
636 for (i = 0; i < tracks->
nb_tracks; i++) {
639 fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
641 fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
642 "valueType=\"data\" />\n", track->
track_id);
643 fprintf(out, "\t\t\t</%s>\n", type);
644 }
645 fprintf(out, "\t\t</switch>\n");
646 fprintf(out, "\t</body>\n");
647 fprintf(out, "</smil>\n");
648 fclose(out);
649 }
650
653 {
654 int i, j;
655 int64_t pos = 0;
657 int should_print_time_mismatch = 1;
658
659 for (i = 0; i < track->
chunks; i++) {
660 for (j = main + 1; j < tracks->
nb_tracks; j++) {
663 fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
665 should_print_time_mismatch = 1;
666 }
668 if (should_print_time_mismatch)
669 fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
671 should_print_time_mismatch = 0;
672 }
673 }
674 }
675 fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" ",
678 fprintf(out,
"t=\"%"PRId64
"\" ", track->
offsets[i].
time);
680 }
682 fprintf(out, "/>\n");
683 }
684 }
685
687 const char *output_prefix,
int split)
688 {
689 char filename[1000];
691 int i, j;
692
693 if (split)
694 snprintf(filename,
sizeof(filename),
"%sManifest", output_prefix);
695 else
696 snprintf(filename,
sizeof(filename),
"%s%s.ismc", output_prefix, basename);
697 out = fopen(filename, "w");
698 if (!out) {
699 perror(filename);
700 return;
701 }
702 fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
703 fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
704 "Duration=\"%"PRId64
"\">\n", tracks->
duration * 10);
707 struct Track *first_track = track;
709 fprintf(out,
710 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
711 "Chunks=\"%d\" "
712 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
714 for (i = 0; i < tracks->
nb_tracks; i++) {
715 track = tracks->
tracks[i];
717 continue;
718 fprintf(out,
719 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
720 "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
721 "CodecPrivateData=\"",
725 fprintf(out, "\" />\n");
726 index++;
728 fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
730 }
732 fprintf(out, "\t</StreamIndex>\n");
733 }
736 struct Track *first_track = track;
738 fprintf(out,
739 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
740 "Chunks=\"%d\" "
741 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
743 for (i = 0; i < tracks->
nb_tracks; i++) {
744 track = tracks->
tracks[i];
746 continue;
747 fprintf(out,
748 "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
749 "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
750 "BitsPerSample=\"16\" PacketSize=\"%d\" "
751 "AudioTag=\"%d\" CodecPrivateData=\"",
756 fprintf(out, "\" />\n");
757 index++;
759 fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
761 }
763 fprintf(out, "\t</StreamIndex>\n");
764 }
765 fprintf(out, "</SmoothStreamingMedia>\n");
766 fclose(out);
767 }
768
770 {
771 int i;
772 for (i = 0; i < tracks->
nb_tracks; i++) {
776 }
779 }
780
781 int main(
int argc,
char **argv)
782 {
783 const char *basename =
NULL;
784 const char *path_prefix = "", *ismc_prefix = "";
785 const char *output_prefix = "";
786 char output_prefix_buf[2048];
787 int split = 0, ismf = 0, i;
789
791
792 for (i = 1; i < argc; i++) {
793 if (!strcmp(argv[i], "-n")) {
794 basename = argv[i + 1];
795 i++;
796 } else if (!strcmp(argv[i], "-path-prefix")) {
797 path_prefix = argv[i + 1];
798 i++;
799 } else if (!strcmp(argv[i], "-ismc-prefix")) {
800 ismc_prefix = argv[i + 1];
801 i++;
802 } else if (!strcmp(argv[i], "-output")) {
803 output_prefix = argv[i + 1];
804 i++;
805 if (output_prefix[strlen(output_prefix) - 1] != '/') {
806 snprintf(output_prefix_buf,
sizeof(output_prefix_buf),
807 "%s/", output_prefix);
808 output_prefix = output_prefix_buf;
809 }
810 } else if (!strcmp(argv[i], "-split")) {
811 split = 1;
812 } else if (!strcmp(argv[i], "-ismf")) {
813 ismf = 1;
814 } else if (argv[i][0] == '-') {
815 return usage(argv[0], 1);
816 } else {
817 if (!basename)
818 ismf = 0;
820 basename, output_prefix))
821 return 1;
822 }
823 }
824 if (!tracks.
nb_tracks || (!basename && !split))
825 return usage(argv[0], 1);
826
827 if (!split)
829 path_prefix, ismc_prefix);
831
833
834 return 0;
835 }