sidxindex.c 12 KB

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