ismindex.c 29 KB

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