You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

818 lines
28KB

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