ismindex.c 29 KB

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