ismindex.c 29 KB

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