ismindex.c 24 KB

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