sidxindex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2014 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. #include <stdio.h>
  21. #include <string.h>
  22. #include "libavformat/avformat.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/mathematics.h"
  26. static int usage(const char *argv0, int ret)
  27. {
  28. fprintf(stderr, "%s -out foo.mpd file1\n", argv0);
  29. return ret;
  30. }
  31. struct Track {
  32. const char *name;
  33. int64_t duration;
  34. int bitrate;
  35. int track_id;
  36. int is_audio, is_video;
  37. int width, height;
  38. int sample_rate, channels;
  39. int timescale;
  40. char codec_str[30];
  41. int64_t sidx_start, sidx_length;
  42. int64_t earliest_presentation;
  43. uint32_t earliest_presentation_timescale;
  44. };
  45. struct Tracks {
  46. int nb_tracks;
  47. int64_t duration;
  48. struct Track **tracks;
  49. int multiple_tracks_per_file;
  50. };
  51. static void set_codec_str(AVCodecContext *codec, char *str, int size)
  52. {
  53. switch (codec->codec_id) {
  54. case AV_CODEC_ID_H264:
  55. snprintf(str, size, "avc1");
  56. if (codec->extradata_size >= 4 && codec->extradata[0] == 1) {
  57. av_strlcatf(str, size, ".%02x%02x%02x",
  58. codec->extradata[1], codec->extradata[2], codec->extradata[3]);
  59. }
  60. break;
  61. case AV_CODEC_ID_AAC:
  62. snprintf(str, size, "mp4a.40"); // 0x40 is the mp4 object type for AAC
  63. if (codec->extradata_size >= 2) {
  64. int aot = codec->extradata[0] >> 3;
  65. if (aot == 31)
  66. aot = ((AV_RB16(codec->extradata) >> 5) & 0x3f) + 32;
  67. av_strlcatf(str, size, ".%d", aot);
  68. }
  69. break;
  70. }
  71. }
  72. static int find_sidx(struct Tracks *tracks, int start_index,
  73. const char *file)
  74. {
  75. int err = 0;
  76. AVIOContext *f = NULL;
  77. int i;
  78. if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
  79. goto fail;
  80. while (!f->eof_reached) {
  81. int64_t pos = avio_tell(f);
  82. int32_t size, tag;
  83. size = avio_rb32(f);
  84. tag = avio_rb32(f);
  85. if (size < 8)
  86. break;
  87. if (tag == MKBETAG('s', 'i', 'd', 'x')) {
  88. int version, track_id;
  89. uint32_t timescale;
  90. int64_t earliest_presentation;
  91. version = avio_r8(f);
  92. avio_rb24(f); /* flags */
  93. track_id = avio_rb32(f);
  94. timescale = avio_rb32(f);
  95. earliest_presentation = version ? avio_rb64(f) : avio_rb32(f);
  96. for (i = start_index; i < tracks->nb_tracks; i++) {
  97. struct Track *track = tracks->tracks[i];
  98. if (!track->sidx_start) {
  99. track->sidx_start = pos;
  100. track->sidx_length = size;
  101. } else if (pos == track->sidx_start + track->sidx_length) {
  102. track->sidx_length = pos + size - track->sidx_start;
  103. }
  104. if (track->track_id == track_id) {
  105. track->earliest_presentation = earliest_presentation;
  106. track->earliest_presentation_timescale = timescale;
  107. }
  108. }
  109. }
  110. if (avio_seek(f, pos + size, SEEK_SET) != pos + size)
  111. break;
  112. }
  113. fail:
  114. if (f)
  115. avio_close(f);
  116. return err;
  117. }
  118. static int handle_file(struct Tracks *tracks, const char *file)
  119. {
  120. AVFormatContext *ctx = NULL;
  121. int err = 0, i, orig_tracks = tracks->nb_tracks;
  122. char errbuf[50], *ptr;
  123. struct Track *track;
  124. err = avformat_open_input(&ctx, file, NULL, NULL);
  125. if (err < 0) {
  126. av_strerror(err, errbuf, sizeof(errbuf));
  127. fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
  128. return 1;
  129. }
  130. err = avformat_find_stream_info(ctx, NULL);
  131. if (err < 0) {
  132. av_strerror(err, errbuf, sizeof(errbuf));
  133. fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
  134. goto fail;
  135. }
  136. if (ctx->nb_streams < 1) {
  137. fprintf(stderr, "No streams found in %s\n", file);
  138. goto fail;
  139. }
  140. if (ctx->nb_streams > 1)
  141. tracks->multiple_tracks_per_file = 1;
  142. for (i = 0; i < ctx->nb_streams; i++) {
  143. struct Track **temp;
  144. AVStream *st = ctx->streams[i];
  145. if (st->codec->bit_rate == 0) {
  146. fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
  147. st->id, file);
  148. continue;
  149. }
  150. track = av_mallocz(sizeof(*track));
  151. if (!track) {
  152. err = AVERROR(ENOMEM);
  153. goto fail;
  154. }
  155. temp = av_realloc(tracks->tracks,
  156. sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
  157. if (!temp) {
  158. av_free(track);
  159. err = AVERROR(ENOMEM);
  160. goto fail;
  161. }
  162. tracks->tracks = temp;
  163. tracks->tracks[tracks->nb_tracks] = track;
  164. track->name = file;
  165. if ((ptr = strrchr(file, '/')))
  166. track->name = ptr + 1;
  167. track->bitrate = st->codec->bit_rate;
  168. track->track_id = st->id;
  169. track->timescale = st->time_base.den;
  170. track->duration = st->duration;
  171. track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
  172. track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  173. if (!track->is_audio && !track->is_video) {
  174. fprintf(stderr,
  175. "Track %d in %s is neither video nor audio, skipping\n",
  176. track->track_id, file);
  177. av_freep(&tracks->tracks[tracks->nb_tracks]);
  178. continue;
  179. }
  180. tracks->duration = FFMAX(tracks->duration,
  181. av_rescale_rnd(track->duration, AV_TIME_BASE,
  182. track->timescale, AV_ROUND_UP));
  183. if (track->is_audio) {
  184. track->channels = st->codec->channels;
  185. track->sample_rate = st->codec->sample_rate;
  186. }
  187. if (track->is_video) {
  188. track->width = st->codec->width;
  189. track->height = st->codec->height;
  190. }
  191. set_codec_str(st->codec, track->codec_str, sizeof(track->codec_str));
  192. tracks->nb_tracks++;
  193. }
  194. avformat_close_input(&ctx);
  195. err = find_sidx(tracks, orig_tracks, file);
  196. fail:
  197. if (ctx)
  198. avformat_close_input(&ctx);
  199. return err;
  200. }
  201. static void write_time(FILE *out, int64_t time, int decimals, enum AVRounding round)
  202. {
  203. int seconds = time / AV_TIME_BASE;
  204. int fractions = time % AV_TIME_BASE;
  205. int minutes = seconds / 60;
  206. int hours = minutes / 60;
  207. fractions = av_rescale_rnd(fractions, pow(10, decimals), AV_TIME_BASE, round);
  208. seconds %= 60;
  209. minutes %= 60;
  210. fprintf(out, "PT");
  211. if (hours)
  212. fprintf(out, "%dH", hours);
  213. if (hours || minutes)
  214. fprintf(out, "%dM", minutes);
  215. fprintf(out, "%d.%0*dS", seconds, decimals, fractions);
  216. }
  217. static int output_mpd(struct Tracks *tracks, const char *filename)
  218. {
  219. FILE *out;
  220. int i, j, ret = 0;
  221. struct Track **adaptation_sets_buf[2] = { NULL };
  222. struct Track ***adaptation_sets;
  223. int nb_tracks_buf[2] = { 0 };
  224. int *nb_tracks;
  225. int set, nb_sets;
  226. int64_t latest_start = 0;
  227. if (!tracks->multiple_tracks_per_file) {
  228. adaptation_sets = adaptation_sets_buf;
  229. nb_tracks = nb_tracks_buf;
  230. nb_sets = 2;
  231. for (i = 0; i < 2; i++) {
  232. adaptation_sets[i] = av_malloc(sizeof(*adaptation_sets[i]) * tracks->nb_tracks);
  233. if (!adaptation_sets[i]) {
  234. ret = AVERROR(ENOMEM);
  235. goto err;
  236. }
  237. }
  238. for (i = 0; i < tracks->nb_tracks; i++) {
  239. int set_index = -1;
  240. if (tracks->tracks[i]->is_video)
  241. set_index = 0;
  242. else if (tracks->tracks[i]->is_audio)
  243. set_index = 1;
  244. else
  245. continue;
  246. adaptation_sets[set_index][nb_tracks[set_index]++] = tracks->tracks[i];
  247. }
  248. } else {
  249. adaptation_sets = &tracks->tracks;
  250. nb_tracks = &tracks->nb_tracks;
  251. nb_sets = 1;
  252. }
  253. out = fopen(filename, "w");
  254. if (!out) {
  255. ret = AVERROR(errno);
  256. perror(filename);
  257. return ret;
  258. }
  259. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  260. fprintf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
  261. "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
  262. "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
  263. "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
  264. "\tprofiles=\"urn:mpeg:dash:profile:isoff-on-demand:2011\"\n"
  265. "\ttype=\"static\"\n");
  266. fprintf(out, "\tmediaPresentationDuration=\"");
  267. write_time(out, tracks->duration, 1, AV_ROUND_DOWN);
  268. fprintf(out, "\"\n");
  269. fprintf(out, "\tminBufferTime=\"PT5S\">\n");
  270. for (i = 0; i < tracks->nb_tracks; i++) {
  271. int64_t start = av_rescale_rnd(tracks->tracks[i]->earliest_presentation,
  272. AV_TIME_BASE,
  273. tracks->tracks[i]->earliest_presentation_timescale,
  274. AV_ROUND_UP);
  275. latest_start = FFMAX(start, latest_start);
  276. }
  277. fprintf(out, "\t<Period start=\"");
  278. write_time(out, latest_start, 3, AV_ROUND_UP);
  279. fprintf(out, "\">\n");
  280. for (set = 0; set < nb_sets; set++) {
  281. if (nb_tracks[set] == 0)
  282. continue;
  283. fprintf(out, "\t\t<AdaptationSet segmentAlignment=\"true\">\n");
  284. if (nb_sets == 1) {
  285. for (i = 0; i < nb_tracks[set]; i++) {
  286. struct Track *track = adaptation_sets[set][i];
  287. if (strcmp(track->name, adaptation_sets[set][0]->name))
  288. break;
  289. fprintf(out, "\t\t\t<ContentComponent id=\"%d\" contentType=\"%s\" />\n", track->track_id, track->is_audio ? "audio" : "video");
  290. }
  291. }
  292. for (i = 0; i < nb_tracks[set]; ) {
  293. struct Track *first_track = adaptation_sets[set][i];
  294. int width = 0, height = 0, sample_rate = 0, channels = 0, bitrate = 0;
  295. fprintf(out, "\t\t\t<Representation id=\"%d\" codecs=\"", i);
  296. for (j = i; j < nb_tracks[set]; j++) {
  297. struct Track *track = adaptation_sets[set][j];
  298. if (strcmp(track->name, first_track->name))
  299. break;
  300. if (track->is_audio) {
  301. sample_rate = track->sample_rate;
  302. channels = track->channels;
  303. }
  304. if (track->is_video) {
  305. width = track->width;
  306. height = track->height;
  307. }
  308. bitrate += track->bitrate;
  309. if (j > i)
  310. fprintf(out, ",");
  311. fprintf(out, "%s", track->codec_str);
  312. }
  313. fprintf(out, "\" mimeType=\"%s/mp4\" bandwidth=\"%d\"",
  314. width ? "video" : "audio", bitrate);
  315. if (width > 0 && height > 0)
  316. fprintf(out, " width=\"%d\" height=\"%d\"", width, height);
  317. if (sample_rate > 0)
  318. fprintf(out, " audioSamplingRate=\"%d\"", sample_rate);
  319. fprintf(out, ">\n");
  320. if (channels > 0)
  321. fprintf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", channels);
  322. fprintf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", first_track->name);
  323. fprintf(out, "\t\t\t\t<SegmentBase indexRange=\"%"PRId64"-%"PRId64"\" />\n", first_track->sidx_start, first_track->sidx_start + first_track->sidx_length - 1);
  324. fprintf(out, "\t\t\t</Representation>\n");
  325. i = j;
  326. }
  327. fprintf(out, "\t\t</AdaptationSet>\n");
  328. }
  329. fprintf(out, "\t</Period>\n");
  330. fprintf(out, "</MPD>\n");
  331. fclose(out);
  332. err:
  333. for (i = 0; i < 2; i++)
  334. av_free(adaptation_sets_buf[i]);
  335. return ret;
  336. }
  337. static void clean_tracks(struct Tracks *tracks)
  338. {
  339. int i;
  340. for (i = 0; i < tracks->nb_tracks; i++) {
  341. av_freep(&tracks->tracks[i]);
  342. }
  343. av_freep(&tracks->tracks);
  344. tracks->nb_tracks = 0;
  345. }
  346. int main(int argc, char **argv)
  347. {
  348. const char *out = NULL;
  349. struct Tracks tracks = { 0 };
  350. int i;
  351. av_register_all();
  352. for (i = 1; i < argc; i++) {
  353. if (!strcmp(argv[i], "-out")) {
  354. out = argv[i + 1];
  355. i++;
  356. } else if (argv[i][0] == '-') {
  357. return usage(argv[0], 1);
  358. } else {
  359. if (handle_file(&tracks, argv[i]))
  360. return 1;
  361. }
  362. }
  363. if (!tracks.nb_tracks || !out)
  364. return usage(argv[0], 1);
  365. output_mpd(&tracks, out);
  366. clean_tracks(&tracks);
  367. return 0;
  368. }