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