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.

816 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, int64_t end)
  209. {
  210. int64_t ret = 0;
  211. int64_t pos;
  212. int flags, i;
  213. int entries;
  214. avio_r8(in); /* version */
  215. flags = avio_rb24(in);
  216. if (!(flags & MOV_TRUN_SAMPLE_DURATION)) {
  217. fprintf(stderr, "No sample duration in trun flags\n");
  218. return -1;
  219. }
  220. entries = avio_rb32(in);
  221. if (flags & MOV_TRUN_DATA_OFFSET) avio_rb32(in);
  222. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) avio_rb32(in);
  223. pos = avio_tell(in);
  224. for (i = 0; i < entries && pos < end; i++) {
  225. int sample_duration = 0;
  226. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(in);
  227. if (flags & MOV_TRUN_SAMPLE_SIZE) avio_rb32(in);
  228. if (flags & MOV_TRUN_SAMPLE_FLAGS) avio_rb32(in);
  229. if (flags & MOV_TRUN_SAMPLE_CTS) avio_rb32(in);
  230. if (sample_duration < 0) {
  231. fprintf(stderr, "Negative sample duration %d\n", sample_duration);
  232. return -1;
  233. }
  234. ret += sample_duration;
  235. pos = avio_tell(in);
  236. }
  237. return ret;
  238. }
  239. static int64_t read_moof_duration(AVIOContext *in, int64_t offset)
  240. {
  241. int64_t ret = -1;
  242. int32_t moof_size, size, tag;
  243. int64_t pos = 0;
  244. avio_seek(in, offset, SEEK_SET);
  245. moof_size = avio_rb32(in);
  246. tag = avio_rb32(in);
  247. if (expect_tag(tag, MKBETAG('m', 'o', 'o', 'f')) != 0)
  248. goto fail;
  249. while (pos < offset + moof_size) {
  250. pos = avio_tell(in);
  251. size = avio_rb32(in);
  252. tag = avio_rb32(in);
  253. if (tag == MKBETAG('t', 'r', 'a', 'f')) {
  254. int64_t traf_pos = pos;
  255. int64_t traf_size = size;
  256. while (pos < traf_pos + traf_size) {
  257. pos = avio_tell(in);
  258. size = avio_rb32(in);
  259. tag = avio_rb32(in);
  260. if (tag == MKBETAG('t', 'r', 'u', 'n')) {
  261. return read_trun_duration(in, pos + size);
  262. }
  263. avio_seek(in, pos + size, SEEK_SET);
  264. }
  265. fprintf(stderr, "Couldn't find trun\n");
  266. goto fail;
  267. }
  268. avio_seek(in, pos + size, SEEK_SET);
  269. }
  270. fprintf(stderr, "Couldn't find traf\n");
  271. fail:
  272. return ret;
  273. }
  274. static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
  275. {
  276. int ret = AVERROR_EOF, track_id;
  277. int version, fieldlength, i, j;
  278. int64_t pos = avio_tell(f);
  279. uint32_t size = avio_rb32(f);
  280. struct Track *track = NULL;
  281. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
  282. goto fail;
  283. version = avio_r8(f);
  284. avio_rb24(f);
  285. track_id = avio_rb32(f); /* track id */
  286. for (i = start_index; i < tracks->nb_tracks && !track; i++)
  287. if (tracks->tracks[i]->track_id == track_id)
  288. track = tracks->tracks[i];
  289. if (!track) {
  290. /* Ok, continue parsing the next atom */
  291. ret = 0;
  292. goto fail;
  293. }
  294. fieldlength = avio_rb32(f);
  295. track->chunks = avio_rb32(f);
  296. track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
  297. if (!track->offsets) {
  298. ret = AVERROR(ENOMEM);
  299. goto fail;
  300. }
  301. // The duration here is always the difference between consecutive
  302. // start times.
  303. for (i = 0; i < track->chunks; i++) {
  304. if (version == 1) {
  305. track->offsets[i].time = avio_rb64(f);
  306. track->offsets[i].offset = avio_rb64(f);
  307. } else {
  308. track->offsets[i].time = avio_rb32(f);
  309. track->offsets[i].offset = avio_rb32(f);
  310. }
  311. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  312. avio_r8(f);
  313. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  314. avio_r8(f);
  315. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  316. avio_r8(f);
  317. if (i > 0)
  318. track->offsets[i - 1].duration = track->offsets[i].time -
  319. track->offsets[i - 1].time;
  320. }
  321. if (track->chunks > 0) {
  322. track->offsets[track->chunks - 1].duration = track->offsets[0].time +
  323. track->duration -
  324. track->offsets[track->chunks - 1].time;
  325. }
  326. // Now try and read the actual durations from the trun sample data.
  327. for (i = 0; i < track->chunks; i++) {
  328. int64_t duration = read_moof_duration(f, track->offsets[i].offset);
  329. if (duration > 0 && abs(duration - track->offsets[i].duration) > 3) {
  330. // 3 allows for integer duration to drift a few units,
  331. // e.g., for 1/3 durations
  332. track->offsets[i].duration = duration;
  333. }
  334. }
  335. if (track->chunks > 0) {
  336. if (track->offsets[track->chunks - 1].duration <= 0) {
  337. fprintf(stderr, "Calculated last chunk duration for track %d "
  338. "was non-positive (%"PRId64"), probably due to missing "
  339. "fragments ", track->track_id,
  340. track->offsets[track->chunks - 1].duration);
  341. if (track->chunks > 1) {
  342. track->offsets[track->chunks - 1].duration =
  343. track->offsets[track->chunks - 2].duration;
  344. } else {
  345. track->offsets[track->chunks - 1].duration = 1;
  346. }
  347. fprintf(stderr, "corrected to %"PRId64"\n",
  348. track->offsets[track->chunks - 1].duration);
  349. track->duration = track->offsets[track->chunks - 1].time +
  350. track->offsets[track->chunks - 1].duration -
  351. track->offsets[0].time;
  352. fprintf(stderr, "Track duration corrected to %"PRId64"\n",
  353. track->duration);
  354. }
  355. }
  356. ret = 0;
  357. fail:
  358. avio_seek(f, pos + size, SEEK_SET);
  359. return ret;
  360. }
  361. static int read_mfra(struct Tracks *tracks, int start_index,
  362. const char *file, int split, int ismf,
  363. const char *basename, const char* output_prefix)
  364. {
  365. int err = 0;
  366. const char* err_str = "";
  367. AVIOContext *f = NULL;
  368. int32_t mfra_size;
  369. if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
  370. goto fail;
  371. avio_seek(f, avio_size(f) - 4, SEEK_SET);
  372. mfra_size = avio_rb32(f);
  373. avio_seek(f, -mfra_size, SEEK_CUR);
  374. if (avio_rb32(f) != mfra_size) {
  375. err = AVERROR_INVALIDDATA;
  376. err_str = "mfra size mismatch";
  377. goto fail;
  378. }
  379. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  380. err = AVERROR_INVALIDDATA;
  381. err_str = "mfra tag mismatch";
  382. goto fail;
  383. }
  384. while (!read_tfra(tracks, start_index, f)) {
  385. /* Empty */
  386. }
  387. if (split || ismf)
  388. err = write_fragments(tracks, start_index, f, basename, split, ismf,
  389. output_prefix);
  390. err_str = "error in write_fragments";
  391. fail:
  392. if (f)
  393. avio_close(f);
  394. if (err)
  395. fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
  396. return err;
  397. }
  398. static int get_private_data(struct Track *track, AVCodecContext *codec)
  399. {
  400. track->codec_private_size = codec->extradata_size;
  401. track->codec_private = av_mallocz(codec->extradata_size);
  402. if (!track->codec_private)
  403. return AVERROR(ENOMEM);
  404. memcpy(track->codec_private, codec->extradata, codec->extradata_size);
  405. return 0;
  406. }
  407. static int get_video_private_data(struct Track *track, AVCodecContext *codec)
  408. {
  409. AVIOContext *io = NULL;
  410. uint16_t sps_size, pps_size;
  411. int err;
  412. if (codec->codec_id == AV_CODEC_ID_VC1)
  413. return get_private_data(track, codec);
  414. if ((err = avio_open_dyn_buf(&io)) < 0)
  415. goto fail;
  416. err = AVERROR(EINVAL);
  417. if (codec->extradata_size < 11 || codec->extradata[0] != 1)
  418. goto fail;
  419. sps_size = AV_RB16(&codec->extradata[6]);
  420. if (11 + sps_size > codec->extradata_size)
  421. goto fail;
  422. avio_wb32(io, 0x00000001);
  423. avio_write(io, &codec->extradata[8], sps_size);
  424. pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
  425. if (11 + sps_size + pps_size > codec->extradata_size)
  426. goto fail;
  427. avio_wb32(io, 0x00000001);
  428. avio_write(io, &codec->extradata[11 + sps_size], pps_size);
  429. err = 0;
  430. fail:
  431. track->codec_private_size = avio_close_dyn_buf(io, &track->codec_private);
  432. return err;
  433. }
  434. static int handle_file(struct Tracks *tracks, const char *file, int split,
  435. int ismf, const char *basename,
  436. const char* output_prefix)
  437. {
  438. AVFormatContext *ctx = NULL;
  439. int err = 0, i, orig_tracks = tracks->nb_tracks;
  440. char errbuf[50], *ptr;
  441. struct Track *track;
  442. err = avformat_open_input(&ctx, file, NULL, NULL);
  443. if (err < 0) {
  444. av_strerror(err, errbuf, sizeof(errbuf));
  445. fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
  446. return 1;
  447. }
  448. err = avformat_find_stream_info(ctx, NULL);
  449. if (err < 0) {
  450. av_strerror(err, errbuf, sizeof(errbuf));
  451. fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
  452. goto fail;
  453. }
  454. if (ctx->nb_streams < 1) {
  455. fprintf(stderr, "No streams found in %s\n", file);
  456. goto fail;
  457. }
  458. for (i = 0; i < ctx->nb_streams; i++) {
  459. struct Track **temp;
  460. AVStream *st = ctx->streams[i];
  461. if (st->codec->bit_rate == 0) {
  462. fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
  463. st->id, file);
  464. continue;
  465. }
  466. track = av_mallocz(sizeof(*track));
  467. if (!track) {
  468. err = AVERROR(ENOMEM);
  469. goto fail;
  470. }
  471. temp = av_realloc(tracks->tracks,
  472. sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
  473. if (!temp) {
  474. av_free(track);
  475. err = AVERROR(ENOMEM);
  476. goto fail;
  477. }
  478. tracks->tracks = temp;
  479. tracks->tracks[tracks->nb_tracks] = track;
  480. track->name = file;
  481. if ((ptr = strrchr(file, '/')))
  482. track->name = ptr + 1;
  483. track->bitrate = st->codec->bit_rate;
  484. track->track_id = st->id;
  485. track->timescale = st->time_base.den;
  486. track->duration = st->duration;
  487. track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
  488. track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  489. if (!track->is_audio && !track->is_video) {
  490. fprintf(stderr,
  491. "Track %d in %s is neither video nor audio, skipping\n",
  492. track->track_id, file);
  493. av_freep(&tracks->tracks[tracks->nb_tracks]);
  494. continue;
  495. }
  496. tracks->duration = FFMAX(tracks->duration,
  497. av_rescale_rnd(track->duration, AV_TIME_BASE,
  498. track->timescale, AV_ROUND_UP));
  499. if (track->is_audio) {
  500. if (tracks->audio_track < 0)
  501. tracks->audio_track = tracks->nb_tracks;
  502. tracks->nb_audio_tracks++;
  503. track->channels = st->codec->channels;
  504. track->sample_rate = st->codec->sample_rate;
  505. if (st->codec->codec_id == AV_CODEC_ID_AAC) {
  506. track->fourcc = "AACL";
  507. track->tag = 255;
  508. track->blocksize = 4;
  509. } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
  510. track->fourcc = "WMAP";
  511. track->tag = st->codec->codec_tag;
  512. track->blocksize = st->codec->block_align;
  513. }
  514. get_private_data(track, st->codec);
  515. }
  516. if (track->is_video) {
  517. if (tracks->video_track < 0)
  518. tracks->video_track = tracks->nb_tracks;
  519. tracks->nb_video_tracks++;
  520. track->width = st->codec->width;
  521. track->height = st->codec->height;
  522. if (st->codec->codec_id == AV_CODEC_ID_H264)
  523. track->fourcc = "H264";
  524. else if (st->codec->codec_id == AV_CODEC_ID_VC1)
  525. track->fourcc = "WVC1";
  526. get_video_private_data(track, st->codec);
  527. }
  528. tracks->nb_tracks++;
  529. }
  530. avformat_close_input(&ctx);
  531. err = read_mfra(tracks, orig_tracks, file, split, ismf, basename,
  532. output_prefix);
  533. fail:
  534. if (ctx)
  535. avformat_close_input(&ctx);
  536. return err;
  537. }
  538. static void output_server_manifest(struct Tracks *tracks, const char *basename,
  539. const char *output_prefix,
  540. const char *path_prefix,
  541. const char *ismc_prefix)
  542. {
  543. char filename[1000];
  544. FILE *out;
  545. int i;
  546. snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
  547. out = fopen(filename, "w");
  548. if (!out) {
  549. perror(filename);
  550. return;
  551. }
  552. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  553. fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  554. fprintf(out, "\t<head>\n");
  555. fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
  556. "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
  557. fprintf(out, "\t</head>\n");
  558. fprintf(out, "\t<body>\n");
  559. fprintf(out, "\t\t<switch>\n");
  560. for (i = 0; i < tracks->nb_tracks; i++) {
  561. struct Track *track = tracks->tracks[i];
  562. const char *type = track->is_video ? "video" : "audio";
  563. fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
  564. type, path_prefix, track->name, track->bitrate);
  565. fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
  566. "valueType=\"data\" />\n", track->track_id);
  567. fprintf(out, "\t\t\t</%s>\n", type);
  568. }
  569. fprintf(out, "\t\t</switch>\n");
  570. fprintf(out, "\t</body>\n");
  571. fprintf(out, "</smil>\n");
  572. fclose(out);
  573. }
  574. static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
  575. const char *type)
  576. {
  577. int i, j;
  578. int64_t pos = 0;
  579. struct Track *track = tracks->tracks[main];
  580. int should_print_time_mismatch = 1;
  581. for (i = 0; i < track->chunks; i++) {
  582. for (j = main + 1; j < tracks->nb_tracks; j++) {
  583. if (tracks->tracks[j]->is_audio == track->is_audio) {
  584. if (track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration) {
  585. fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
  586. type, i, track->name, main, tracks->tracks[j]->name, j);
  587. should_print_time_mismatch = 1;
  588. }
  589. if (track->offsets[i].time != tracks->tracks[j]->offsets[i].time) {
  590. if (should_print_time_mismatch)
  591. fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
  592. type, i, track->name, main, tracks->tracks[j]->name, j);
  593. should_print_time_mismatch = 0;
  594. }
  595. }
  596. }
  597. fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" ",
  598. i, track->offsets[i].duration);
  599. if (pos != track->offsets[i].time) {
  600. fprintf(out, "t=\"%"PRId64"\" ", track->offsets[i].time);
  601. pos = track->offsets[i].time;
  602. }
  603. pos += track->offsets[i].duration;
  604. fprintf(out, "/>\n");
  605. }
  606. }
  607. static void output_client_manifest(struct Tracks *tracks, const char *basename,
  608. const char *output_prefix, int split)
  609. {
  610. char filename[1000];
  611. FILE *out;
  612. int i, j;
  613. if (split)
  614. snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
  615. else
  616. snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
  617. out = fopen(filename, "w");
  618. if (!out) {
  619. perror(filename);
  620. return;
  621. }
  622. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  623. fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
  624. "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
  625. if (tracks->video_track >= 0) {
  626. struct Track *track = tracks->tracks[tracks->video_track];
  627. struct Track *first_track = track;
  628. int index = 0;
  629. fprintf(out,
  630. "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
  631. "Chunks=\"%d\" "
  632. "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
  633. tracks->nb_video_tracks, track->chunks);
  634. for (i = 0; i < tracks->nb_tracks; i++) {
  635. track = tracks->tracks[i];
  636. if (!track->is_video)
  637. continue;
  638. fprintf(out,
  639. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  640. "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
  641. "CodecPrivateData=\"",
  642. index, track->bitrate, track->fourcc, track->width, track->height);
  643. for (j = 0; j < track->codec_private_size; j++)
  644. fprintf(out, "%02X", track->codec_private[j]);
  645. fprintf(out, "\" />\n");
  646. index++;
  647. if (track->chunks != first_track->chunks)
  648. fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
  649. track->name, track->track_id, track->chunks, first_track->name, first_track->track_id, first_track->chunks);
  650. }
  651. print_track_chunks(out, tracks, tracks->video_track, "video");
  652. fprintf(out, "\t</StreamIndex>\n");
  653. }
  654. if (tracks->audio_track >= 0) {
  655. struct Track *track = tracks->tracks[tracks->audio_track];
  656. struct Track *first_track = track;
  657. int index = 0;
  658. fprintf(out,
  659. "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
  660. "Chunks=\"%d\" "
  661. "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
  662. tracks->nb_audio_tracks, track->chunks);
  663. for (i = 0; i < tracks->nb_tracks; i++) {
  664. track = tracks->tracks[i];
  665. if (!track->is_audio)
  666. continue;
  667. fprintf(out,
  668. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  669. "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
  670. "BitsPerSample=\"16\" PacketSize=\"%d\" "
  671. "AudioTag=\"%d\" CodecPrivateData=\"",
  672. index, track->bitrate, track->fourcc, track->sample_rate,
  673. track->channels, track->blocksize, track->tag);
  674. for (j = 0; j < track->codec_private_size; j++)
  675. fprintf(out, "%02X", track->codec_private[j]);
  676. fprintf(out, "\" />\n");
  677. index++;
  678. if (track->chunks != first_track->chunks)
  679. fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
  680. track->name, first_track->name);
  681. }
  682. print_track_chunks(out, tracks, tracks->audio_track, "audio");
  683. fprintf(out, "\t</StreamIndex>\n");
  684. }
  685. fprintf(out, "</SmoothStreamingMedia>\n");
  686. fclose(out);
  687. }
  688. static void clean_tracks(struct Tracks *tracks)
  689. {
  690. int i;
  691. for (i = 0; i < tracks->nb_tracks; i++) {
  692. av_freep(&tracks->tracks[i]->codec_private);
  693. av_freep(&tracks->tracks[i]->offsets);
  694. av_freep(&tracks->tracks[i]);
  695. }
  696. av_freep(&tracks->tracks);
  697. tracks->nb_tracks = 0;
  698. }
  699. int main(int argc, char **argv)
  700. {
  701. const char *basename = NULL;
  702. const char *path_prefix = "", *ismc_prefix = "";
  703. const char *output_prefix = "";
  704. char output_prefix_buf[2048];
  705. int split = 0, ismf = 0, i;
  706. struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
  707. av_register_all();
  708. for (i = 1; i < argc; i++) {
  709. if (!strcmp(argv[i], "-n")) {
  710. basename = argv[i + 1];
  711. i++;
  712. } else if (!strcmp(argv[i], "-path-prefix")) {
  713. path_prefix = argv[i + 1];
  714. i++;
  715. } else if (!strcmp(argv[i], "-ismc-prefix")) {
  716. ismc_prefix = argv[i + 1];
  717. i++;
  718. } else if (!strcmp(argv[i], "-output")) {
  719. output_prefix = argv[i + 1];
  720. i++;
  721. if (output_prefix[strlen(output_prefix) - 1] != '/') {
  722. snprintf(output_prefix_buf, sizeof(output_prefix_buf),
  723. "%s/", output_prefix);
  724. output_prefix = output_prefix_buf;
  725. }
  726. } else if (!strcmp(argv[i], "-split")) {
  727. split = 1;
  728. } else if (!strcmp(argv[i], "-ismf")) {
  729. ismf = 1;
  730. } else if (argv[i][0] == '-') {
  731. return usage(argv[0], 1);
  732. } else {
  733. if (!basename)
  734. ismf = 0;
  735. if (handle_file(&tracks, argv[i], split, ismf,
  736. basename, output_prefix))
  737. return 1;
  738. }
  739. }
  740. if (!tracks.nb_tracks || (!basename && !split))
  741. return usage(argv[0], 1);
  742. if (!split)
  743. output_server_manifest(&tracks, basename, output_prefix,
  744. path_prefix, ismc_prefix);
  745. output_client_manifest(&tracks, basename, output_prefix, split);
  746. clean_tracks(&tracks);
  747. return 0;
  748. }