You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

740 lines
26KB

  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/os_support.h"
  49. #include "libavutil/intreadwrite.h"
  50. #include "libavutil/mathematics.h"
  51. static int usage(const char *argv0, int ret)
  52. {
  53. fprintf(stderr, "%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
  54. "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
  55. return ret;
  56. }
  57. struct MoofOffset {
  58. int64_t time;
  59. int64_t offset;
  60. int64_t duration;
  61. };
  62. struct Track {
  63. const char *name;
  64. int64_t duration;
  65. int bitrate;
  66. int track_id;
  67. int is_audio, is_video;
  68. int width, height;
  69. int chunks;
  70. int sample_rate, channels;
  71. uint8_t *codec_private;
  72. int codec_private_size;
  73. struct MoofOffset *offsets;
  74. int timescale;
  75. const char *fourcc;
  76. int blocksize;
  77. int tag;
  78. };
  79. struct Tracks {
  80. int nb_tracks;
  81. int64_t duration;
  82. struct Track **tracks;
  83. int video_track, audio_track;
  84. int nb_video_tracks, nb_audio_tracks;
  85. };
  86. static int expect_tag(int32_t got_tag, int32_t expected_tag) {
  87. if (got_tag != expected_tag) {
  88. char got_tag_str[4], expected_tag_str[4];
  89. AV_WB32(got_tag_str, got_tag);
  90. AV_WB32(expected_tag_str, expected_tag);
  91. fprintf(stderr, "wanted tag %.4s, got %.4s\n", expected_tag_str,
  92. got_tag_str);
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
  98. {
  99. int32_t size, tag;
  100. size = avio_rb32(in);
  101. tag = avio_rb32(in);
  102. avio_wb32(out, size);
  103. avio_wb32(out, tag);
  104. if (expect_tag(tag, tag_name) != 0)
  105. return -1;
  106. size -= 8;
  107. while (size > 0) {
  108. char buf[1024];
  109. int len = FFMIN(sizeof(buf), size);
  110. int got;
  111. if ((got = avio_read(in, buf, len)) != len) {
  112. fprintf(stderr, "short read, wanted %d, got %d\n", len, got);
  113. break;
  114. }
  115. avio_write(out, buf, len);
  116. size -= len;
  117. }
  118. return 0;
  119. }
  120. static int skip_tag(AVIOContext *in, int32_t tag_name)
  121. {
  122. int64_t pos = avio_tell(in);
  123. int32_t size, tag;
  124. size = avio_rb32(in);
  125. tag = avio_rb32(in);
  126. if (expect_tag(tag, tag_name) != 0)
  127. return -1;
  128. avio_seek(in, pos + size, SEEK_SET);
  129. return 0;
  130. }
  131. static int write_fragment(const char *filename, AVIOContext *in)
  132. {
  133. AVIOContext *out = NULL;
  134. int ret;
  135. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0) {
  136. char errbuf[100];
  137. av_strerror(ret, errbuf, sizeof(errbuf));
  138. fprintf(stderr, "Unable to open %s: %s\n", filename, errbuf);
  139. return ret;
  140. }
  141. ret = copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
  142. if (!ret)
  143. ret = copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
  144. avio_flush(out);
  145. avio_close(out);
  146. return ret;
  147. }
  148. static int skip_fragment(AVIOContext *in)
  149. {
  150. int ret;
  151. ret = skip_tag(in, MKBETAG('m', 'o', 'o', 'f'));
  152. if (!ret)
  153. ret = skip_tag(in, MKBETAG('m', 'd', 'a', 't'));
  154. return ret;
  155. }
  156. static int write_fragments(struct Tracks *tracks, int start_index,
  157. AVIOContext *in, const char *basename,
  158. int split, int ismf, const char* output_prefix)
  159. {
  160. char dirname[2048], filename[2048], idxname[2048];
  161. int i, j, ret = 0, fragment_ret;
  162. FILE* out = NULL;
  163. if (ismf) {
  164. snprintf(idxname, sizeof(idxname), "%s%s.ismf", output_prefix, basename);
  165. out = fopen(idxname, "w");
  166. if (!out) {
  167. ret = AVERROR(errno);
  168. perror(idxname);
  169. goto fail;
  170. }
  171. }
  172. for (i = start_index; i < tracks->nb_tracks; i++) {
  173. struct Track *track = tracks->tracks[i];
  174. const char *type = track->is_video ? "video" : "audio";
  175. snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
  176. if (split) {
  177. if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
  178. ret = AVERROR(errno);
  179. perror(dirname);
  180. goto fail;
  181. }
  182. }
  183. for (j = 0; j < track->chunks; j++) {
  184. snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
  185. dirname, type, track->offsets[j].time);
  186. avio_seek(in, track->offsets[j].offset, SEEK_SET);
  187. if (ismf)
  188. fprintf(out, "%s %"PRId64, filename, avio_tell(in));
  189. if (split)
  190. fragment_ret = write_fragment(filename, in);
  191. else
  192. fragment_ret = skip_fragment(in);
  193. if (ismf)
  194. fprintf(out, " %"PRId64"\n", avio_tell(in));
  195. if (fragment_ret != 0) {
  196. fprintf(stderr, "failed fragment %d in track %d (%s)\n", j,
  197. track->track_id, track->name);
  198. ret = fragment_ret;
  199. }
  200. }
  201. }
  202. fail:
  203. if (out)
  204. fclose(out);
  205. return ret;
  206. }
  207. static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
  208. {
  209. int ret = AVERROR_EOF, track_id;
  210. int version, fieldlength, i, j;
  211. int64_t pos = avio_tell(f);
  212. uint32_t size = avio_rb32(f);
  213. struct Track *track = NULL;
  214. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
  215. goto fail;
  216. version = avio_r8(f);
  217. avio_rb24(f);
  218. track_id = avio_rb32(f); /* track id */
  219. for (i = start_index; i < tracks->nb_tracks && !track; i++)
  220. if (tracks->tracks[i]->track_id == track_id)
  221. track = tracks->tracks[i];
  222. if (!track) {
  223. /* Ok, continue parsing the next atom */
  224. ret = 0;
  225. goto fail;
  226. }
  227. fieldlength = avio_rb32(f);
  228. track->chunks = avio_rb32(f);
  229. track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
  230. if (!track->offsets) {
  231. ret = AVERROR(ENOMEM);
  232. goto fail;
  233. }
  234. // The duration here is always the difference between consecutive
  235. // start times and doesn't even try to read the actual duration of the
  236. // media fragments. This is what other smooth streaming tools tend to
  237. // do too, but cannot express missing fragments, and the start times
  238. // may not match the stream metadata we get from libavformat. Correct
  239. // calculation would require parsing the tfxd atom (if present, it's
  240. // not mandatory) or parsing the full moof atoms separately.
  241. for (i = 0; i < track->chunks; i++) {
  242. if (version == 1) {
  243. track->offsets[i].time = avio_rb64(f);
  244. track->offsets[i].offset = avio_rb64(f);
  245. } else {
  246. track->offsets[i].time = avio_rb32(f);
  247. track->offsets[i].offset = avio_rb32(f);
  248. }
  249. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  250. avio_r8(f);
  251. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  252. avio_r8(f);
  253. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  254. avio_r8(f);
  255. if (i > 0)
  256. track->offsets[i - 1].duration = track->offsets[i].time -
  257. track->offsets[i - 1].time;
  258. }
  259. if (track->chunks > 0) {
  260. track->offsets[track->chunks - 1].duration = track->offsets[0].time +
  261. track->duration -
  262. track->offsets[track->chunks - 1].time;
  263. if (track->offsets[track->chunks - 1].duration <= 0) {
  264. fprintf(stderr, "Calculated last chunk duration for track %d "
  265. "was non-positive (%"PRId64"), probably due to missing "
  266. "fragments ", track->track_id,
  267. track->offsets[track->chunks - 1].duration);
  268. if (track->chunks > 1) {
  269. track->offsets[track->chunks - 1].duration =
  270. track->offsets[track->chunks - 2].duration;
  271. } else {
  272. track->offsets[track->chunks - 1].duration = 1;
  273. }
  274. fprintf(stderr, "corrected to %"PRId64"\n",
  275. track->offsets[track->chunks - 1].duration);
  276. track->duration = track->offsets[track->chunks - 1].time +
  277. track->offsets[track->chunks - 1].duration -
  278. track->offsets[0].time;
  279. fprintf(stderr, "Track duration corrected to %"PRId64"\n",
  280. track->duration);
  281. }
  282. }
  283. ret = 0;
  284. fail:
  285. avio_seek(f, pos + size, SEEK_SET);
  286. return ret;
  287. }
  288. static int read_mfra(struct Tracks *tracks, int start_index,
  289. const char *file, int split, int ismf,
  290. const char *basename, const char* output_prefix)
  291. {
  292. int err = 0;
  293. const char* err_str = "";
  294. AVIOContext *f = NULL;
  295. int32_t mfra_size;
  296. if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
  297. goto fail;
  298. avio_seek(f, avio_size(f) - 4, SEEK_SET);
  299. mfra_size = avio_rb32(f);
  300. avio_seek(f, -mfra_size, SEEK_CUR);
  301. if (avio_rb32(f) != mfra_size) {
  302. err = AVERROR_INVALIDDATA;
  303. err_str = "mfra size mismatch";
  304. goto fail;
  305. }
  306. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  307. err = AVERROR_INVALIDDATA;
  308. err_str = "mfra tag mismatch";
  309. goto fail;
  310. }
  311. while (!read_tfra(tracks, start_index, f)) {
  312. /* Empty */
  313. }
  314. if (split || ismf)
  315. err = write_fragments(tracks, start_index, f, basename, split, ismf,
  316. output_prefix);
  317. err_str = "error in write_fragments";
  318. fail:
  319. if (f)
  320. avio_close(f);
  321. if (err)
  322. fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
  323. return err;
  324. }
  325. static int get_private_data(struct Track *track, AVCodecContext *codec)
  326. {
  327. track->codec_private_size = codec->extradata_size;
  328. track->codec_private = av_mallocz(codec->extradata_size);
  329. if (!track->codec_private)
  330. return AVERROR(ENOMEM);
  331. memcpy(track->codec_private, codec->extradata, codec->extradata_size);
  332. return 0;
  333. }
  334. static int get_video_private_data(struct Track *track, AVCodecContext *codec)
  335. {
  336. AVIOContext *io = NULL;
  337. uint16_t sps_size, pps_size;
  338. int err;
  339. if (codec->codec_id == AV_CODEC_ID_VC1)
  340. return get_private_data(track, codec);
  341. if ((err = avio_open_dyn_buf(&io)) < 0)
  342. goto fail;
  343. err = AVERROR(EINVAL);
  344. if (codec->extradata_size < 11 || codec->extradata[0] != 1)
  345. goto fail;
  346. sps_size = AV_RB16(&codec->extradata[6]);
  347. if (11 + sps_size > codec->extradata_size)
  348. goto fail;
  349. avio_wb32(io, 0x00000001);
  350. avio_write(io, &codec->extradata[8], sps_size);
  351. pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
  352. if (11 + sps_size + pps_size > codec->extradata_size)
  353. goto fail;
  354. avio_wb32(io, 0x00000001);
  355. avio_write(io, &codec->extradata[11 + sps_size], pps_size);
  356. err = 0;
  357. fail:
  358. track->codec_private_size = avio_close_dyn_buf(io, &track->codec_private);
  359. return err;
  360. }
  361. static int handle_file(struct Tracks *tracks, const char *file, int split,
  362. int ismf, const char *basename,
  363. const char* output_prefix)
  364. {
  365. AVFormatContext *ctx = NULL;
  366. int err = 0, i, orig_tracks = tracks->nb_tracks;
  367. char errbuf[50], *ptr;
  368. struct Track *track;
  369. err = avformat_open_input(&ctx, file, NULL, NULL);
  370. if (err < 0) {
  371. av_strerror(err, errbuf, sizeof(errbuf));
  372. fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
  373. return 1;
  374. }
  375. err = avformat_find_stream_info(ctx, NULL);
  376. if (err < 0) {
  377. av_strerror(err, errbuf, sizeof(errbuf));
  378. fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
  379. goto fail;
  380. }
  381. if (ctx->nb_streams < 1) {
  382. fprintf(stderr, "No streams found in %s\n", file);
  383. goto fail;
  384. }
  385. for (i = 0; i < ctx->nb_streams; i++) {
  386. struct Track **temp;
  387. AVStream *st = ctx->streams[i];
  388. if (st->codec->bit_rate == 0) {
  389. fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
  390. st->id, file);
  391. continue;
  392. }
  393. track = av_mallocz(sizeof(*track));
  394. if (!track) {
  395. err = AVERROR(ENOMEM);
  396. goto fail;
  397. }
  398. temp = av_realloc(tracks->tracks,
  399. sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
  400. if (!temp) {
  401. av_free(track);
  402. err = AVERROR(ENOMEM);
  403. goto fail;
  404. }
  405. tracks->tracks = temp;
  406. tracks->tracks[tracks->nb_tracks] = track;
  407. track->name = file;
  408. if ((ptr = strrchr(file, '/')))
  409. track->name = ptr + 1;
  410. track->bitrate = st->codec->bit_rate;
  411. track->track_id = st->id;
  412. track->timescale = st->time_base.den;
  413. track->duration = st->duration;
  414. track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
  415. track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  416. if (!track->is_audio && !track->is_video) {
  417. fprintf(stderr,
  418. "Track %d in %s is neither video nor audio, skipping\n",
  419. track->track_id, file);
  420. av_freep(&tracks->tracks[tracks->nb_tracks]);
  421. continue;
  422. }
  423. tracks->duration = FFMAX(tracks->duration,
  424. av_rescale_rnd(track->duration, AV_TIME_BASE,
  425. track->timescale, AV_ROUND_UP));
  426. if (track->is_audio) {
  427. if (tracks->audio_track < 0)
  428. tracks->audio_track = tracks->nb_tracks;
  429. tracks->nb_audio_tracks++;
  430. track->channels = st->codec->channels;
  431. track->sample_rate = st->codec->sample_rate;
  432. if (st->codec->codec_id == AV_CODEC_ID_AAC) {
  433. track->fourcc = "AACL";
  434. track->tag = 255;
  435. track->blocksize = 4;
  436. } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
  437. track->fourcc = "WMAP";
  438. track->tag = st->codec->codec_tag;
  439. track->blocksize = st->codec->block_align;
  440. }
  441. get_private_data(track, st->codec);
  442. }
  443. if (track->is_video) {
  444. if (tracks->video_track < 0)
  445. tracks->video_track = tracks->nb_tracks;
  446. tracks->nb_video_tracks++;
  447. track->width = st->codec->width;
  448. track->height = st->codec->height;
  449. if (st->codec->codec_id == AV_CODEC_ID_H264)
  450. track->fourcc = "H264";
  451. else if (st->codec->codec_id == AV_CODEC_ID_VC1)
  452. track->fourcc = "WVC1";
  453. get_video_private_data(track, st->codec);
  454. }
  455. tracks->nb_tracks++;
  456. }
  457. avformat_close_input(&ctx);
  458. err = read_mfra(tracks, orig_tracks, file, split, ismf, basename,
  459. output_prefix);
  460. fail:
  461. if (ctx)
  462. avformat_close_input(&ctx);
  463. return err;
  464. }
  465. static void output_server_manifest(struct Tracks *tracks, const char *basename,
  466. const char *output_prefix,
  467. const char *path_prefix,
  468. const char *ismc_prefix)
  469. {
  470. char filename[1000];
  471. FILE *out;
  472. int i;
  473. snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
  474. out = fopen(filename, "w");
  475. if (!out) {
  476. perror(filename);
  477. return;
  478. }
  479. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  480. fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  481. fprintf(out, "\t<head>\n");
  482. fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
  483. "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
  484. fprintf(out, "\t</head>\n");
  485. fprintf(out, "\t<body>\n");
  486. fprintf(out, "\t\t<switch>\n");
  487. for (i = 0; i < tracks->nb_tracks; i++) {
  488. struct Track *track = tracks->tracks[i];
  489. const char *type = track->is_video ? "video" : "audio";
  490. fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
  491. type, path_prefix, track->name, track->bitrate);
  492. fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
  493. "valueType=\"data\" />\n", track->track_id);
  494. fprintf(out, "\t\t\t</%s>\n", type);
  495. }
  496. fprintf(out, "\t\t</switch>\n");
  497. fprintf(out, "\t</body>\n");
  498. fprintf(out, "</smil>\n");
  499. fclose(out);
  500. }
  501. static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
  502. const char *type)
  503. {
  504. int i, j;
  505. int64_t pos = 0;
  506. struct Track *track = tracks->tracks[main];
  507. int should_print_time_mismatch = 1;
  508. for (i = 0; i < track->chunks; i++) {
  509. for (j = main + 1; j < tracks->nb_tracks; j++) {
  510. if (tracks->tracks[j]->is_audio == track->is_audio) {
  511. if (track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration) {
  512. fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
  513. type, i, track->name, main, tracks->tracks[j]->name, j);
  514. should_print_time_mismatch = 1;
  515. }
  516. if (track->offsets[i].time != tracks->tracks[j]->offsets[i].time) {
  517. if (should_print_time_mismatch)
  518. fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
  519. type, i, track->name, main, tracks->tracks[j]->name, j);
  520. should_print_time_mismatch = 0;
  521. }
  522. }
  523. }
  524. fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" ",
  525. i, track->offsets[i].duration);
  526. if (pos != track->offsets[i].time) {
  527. // With the current logic for calculation of durations from
  528. // chunk start times, this branch can only be hit on the first
  529. // chunk - but that's still useful and this will keep working
  530. // if the duration calculation is improved.
  531. fprintf(out, "t=\"%"PRId64"\" ", track->offsets[i].time);
  532. pos = track->offsets[i].time;
  533. }
  534. pos += track->offsets[i].duration;
  535. fprintf(out, "/>\n");
  536. }
  537. }
  538. static void output_client_manifest(struct Tracks *tracks, const char *basename,
  539. const char *output_prefix, int split)
  540. {
  541. char filename[1000];
  542. FILE *out;
  543. int i, j;
  544. if (split)
  545. snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
  546. else
  547. snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
  548. out = fopen(filename, "w");
  549. if (!out) {
  550. perror(filename);
  551. return;
  552. }
  553. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  554. fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
  555. "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
  556. if (tracks->video_track >= 0) {
  557. struct Track *track = tracks->tracks[tracks->video_track];
  558. struct Track *first_track = track;
  559. int index = 0;
  560. fprintf(out,
  561. "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
  562. "Chunks=\"%d\" "
  563. "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
  564. tracks->nb_video_tracks, track->chunks);
  565. for (i = 0; i < tracks->nb_tracks; i++) {
  566. track = tracks->tracks[i];
  567. if (!track->is_video)
  568. continue;
  569. fprintf(out,
  570. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  571. "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
  572. "CodecPrivateData=\"",
  573. index, track->bitrate, track->fourcc, track->width, track->height);
  574. for (j = 0; j < track->codec_private_size; j++)
  575. fprintf(out, "%02X", track->codec_private[j]);
  576. fprintf(out, "\" />\n");
  577. index++;
  578. if (track->chunks != first_track->chunks)
  579. fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
  580. track->name, track->track_id, track->chunks, first_track->name, first_track->track_id, first_track->chunks);
  581. }
  582. print_track_chunks(out, tracks, tracks->video_track, "video");
  583. fprintf(out, "\t</StreamIndex>\n");
  584. }
  585. if (tracks->audio_track >= 0) {
  586. struct Track *track = tracks->tracks[tracks->audio_track];
  587. struct Track *first_track = track;
  588. int index = 0;
  589. fprintf(out,
  590. "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
  591. "Chunks=\"%d\" "
  592. "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
  593. tracks->nb_audio_tracks, track->chunks);
  594. for (i = 0; i < tracks->nb_tracks; i++) {
  595. track = tracks->tracks[i];
  596. if (!track->is_audio)
  597. continue;
  598. fprintf(out,
  599. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  600. "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
  601. "BitsPerSample=\"16\" PacketSize=\"%d\" "
  602. "AudioTag=\"%d\" CodecPrivateData=\"",
  603. index, track->bitrate, track->fourcc, track->sample_rate,
  604. track->channels, track->blocksize, track->tag);
  605. for (j = 0; j < track->codec_private_size; j++)
  606. fprintf(out, "%02X", track->codec_private[j]);
  607. fprintf(out, "\" />\n");
  608. index++;
  609. if (track->chunks != first_track->chunks)
  610. fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
  611. track->name, first_track->name);
  612. }
  613. print_track_chunks(out, tracks, tracks->audio_track, "audio");
  614. fprintf(out, "\t</StreamIndex>\n");
  615. }
  616. fprintf(out, "</SmoothStreamingMedia>\n");
  617. fclose(out);
  618. }
  619. static void clean_tracks(struct Tracks *tracks)
  620. {
  621. int i;
  622. for (i = 0; i < tracks->nb_tracks; i++) {
  623. av_freep(&tracks->tracks[i]->codec_private);
  624. av_freep(&tracks->tracks[i]->offsets);
  625. av_freep(&tracks->tracks[i]);
  626. }
  627. av_freep(&tracks->tracks);
  628. tracks->nb_tracks = 0;
  629. }
  630. int main(int argc, char **argv)
  631. {
  632. const char *basename = NULL;
  633. const char *path_prefix = "", *ismc_prefix = "";
  634. const char *output_prefix = "";
  635. char output_prefix_buf[2048];
  636. int split = 0, ismf = 0, i;
  637. struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
  638. av_register_all();
  639. for (i = 1; i < argc; i++) {
  640. if (!strcmp(argv[i], "-n")) {
  641. basename = argv[i + 1];
  642. i++;
  643. } else if (!strcmp(argv[i], "-path-prefix")) {
  644. path_prefix = argv[i + 1];
  645. i++;
  646. } else if (!strcmp(argv[i], "-ismc-prefix")) {
  647. ismc_prefix = argv[i + 1];
  648. i++;
  649. } else if (!strcmp(argv[i], "-output")) {
  650. output_prefix = argv[i + 1];
  651. i++;
  652. if (output_prefix[strlen(output_prefix) - 1] != '/') {
  653. snprintf(output_prefix_buf, sizeof(output_prefix_buf),
  654. "%s/", output_prefix);
  655. output_prefix = output_prefix_buf;
  656. }
  657. } else if (!strcmp(argv[i], "-split")) {
  658. split = 1;
  659. } else if (!strcmp(argv[i], "-ismf")) {
  660. ismf = 1;
  661. } else if (argv[i][0] == '-') {
  662. return usage(argv[0], 1);
  663. } else {
  664. if (!basename)
  665. ismf = 0;
  666. if (handle_file(&tracks, argv[i], split, ismf,
  667. basename, output_prefix))
  668. return 1;
  669. }
  670. }
  671. if (!tracks.nb_tracks || (!basename && !split))
  672. return usage(argv[0], 1);
  673. if (!split)
  674. output_server_manifest(&tracks, basename, output_prefix,
  675. path_prefix, ismc_prefix);
  676. output_client_manifest(&tracks, basename, output_prefix, split);
  677. clean_tracks(&tracks);
  678. return 0;
  679. }