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.

831 lines
28KB

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