sidxindex.c 12 KB

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