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.

742 lines
26KB

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