ismindex.c 28 KB

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