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.

318 lines
11KB

  1. /*
  2. * WebM DASH Manifest XML muxer
  3. * Copyright (c) 2014 Vignesh Venkatasubramanian
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * WebM DASH Specification:
  23. * https://sites.google.com/a/webmproject.org/wiki/adaptive-streaming/webm-dash-specification
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "matroska.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/dict.h"
  32. #include "libavutil/opt.h"
  33. typedef struct AdaptationSet {
  34. char id[10];
  35. int *streams;
  36. int nb_streams;
  37. } AdaptationSet;
  38. typedef struct WebMDashMuxContext {
  39. const AVClass *class;
  40. char *adaptation_sets;
  41. AdaptationSet *as;
  42. int nb_as;
  43. } WebMDashMuxContext;
  44. static const char *get_codec_name(int codec_id)
  45. {
  46. switch (codec_id) {
  47. case AV_CODEC_ID_VP8:
  48. return "vp8";
  49. case AV_CODEC_ID_VP9:
  50. return "vp9";
  51. case AV_CODEC_ID_VORBIS:
  52. return "vorbis";
  53. case AV_CODEC_ID_OPUS:
  54. return "opus";
  55. }
  56. return NULL;
  57. }
  58. static double get_duration(AVFormatContext *s)
  59. {
  60. int i = 0;
  61. double max = 0.0;
  62. for (i = 0; i < s->nb_streams; i++) {
  63. AVDictionaryEntry *duration = av_dict_get(s->streams[i]->metadata,
  64. DURATION, NULL, 0);
  65. if (!duration || atof(duration->value) < 0) continue;
  66. if (atof(duration->value) > max) max = atof(duration->value);
  67. }
  68. return max / 1000;
  69. }
  70. static void write_header(AVFormatContext *s)
  71. {
  72. double min_buffer_time = 1.0;
  73. avio_printf(s->pb, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  74. avio_printf(s->pb, "<MPD\n");
  75. avio_printf(s->pb, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
  76. avio_printf(s->pb, " xmlns=\"urn:mpeg:DASH:schema:MPD:2011\"\n");
  77. avio_printf(s->pb, " xsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011\"\n");
  78. avio_printf(s->pb, " type=\"static\"\n");
  79. avio_printf(s->pb, " mediaPresentationDuration=\"PT%gS\"\n",
  80. get_duration(s));
  81. avio_printf(s->pb, " minBufferTime=\"PT%gS\"\n",
  82. min_buffer_time);
  83. avio_printf(s->pb, " profiles=\"urn:webm:dash:profile:webm-on-demand:2012\"");
  84. avio_printf(s->pb, ">\n");
  85. }
  86. static void write_footer(AVFormatContext *s)
  87. {
  88. avio_printf(s->pb, "</MPD>");
  89. }
  90. static int subsegment_alignment(AVFormatContext *s, AdaptationSet *as) {
  91. int i;
  92. AVDictionaryEntry *gold = av_dict_get(s->streams[as->streams[0]]->metadata,
  93. CUE_TIMESTAMPS, NULL, 0);
  94. if (!gold) return 0;
  95. for (i = 1; i < as->nb_streams; i++) {
  96. AVDictionaryEntry *ts = av_dict_get(s->streams[as->streams[i]]->metadata,
  97. CUE_TIMESTAMPS, NULL, 0);
  98. if (!ts || strncmp(gold->value, ts->value, strlen(gold->value))) return 0;
  99. }
  100. return 1;
  101. }
  102. static int bitstream_switching(AVFormatContext *s, AdaptationSet *as) {
  103. int i;
  104. AVDictionaryEntry *gold_track_num = av_dict_get(s->streams[as->streams[0]]->metadata,
  105. TRACK_NUMBER, NULL, 0);
  106. AVCodecContext *gold_codec = s->streams[as->streams[0]]->codec;
  107. if (!gold_track_num) return 0;
  108. for (i = 1; i < as->nb_streams; i++) {
  109. AVDictionaryEntry *track_num = av_dict_get(s->streams[as->streams[i]]->metadata,
  110. TRACK_NUMBER, NULL, 0);
  111. AVCodecContext *codec = s->streams[as->streams[i]]->codec;
  112. if (!track_num ||
  113. strncmp(gold_track_num->value, track_num->value, strlen(gold_track_num->value)) ||
  114. gold_codec->codec_id != codec->codec_id ||
  115. gold_codec->extradata_size != codec->extradata_size ||
  116. memcmp(gold_codec->extradata, codec->extradata, codec->extradata_size)) {
  117. return 0;
  118. }
  119. }
  120. return 1;
  121. }
  122. /*
  123. * Writes an Adaptation Set. Returns 0 on success and < 0 on failure.
  124. */
  125. static int write_adaptation_set(AVFormatContext *s, int as_index)
  126. {
  127. WebMDashMuxContext *w = s->priv_data;
  128. AdaptationSet *as = &w->as[as_index];
  129. AVCodecContext *codec = s->streams[as->streams[0]]->codec;
  130. int i;
  131. static const char boolean[2][6] = { "false", "true" };
  132. int subsegmentStartsWithSAP = 1;
  133. AVDictionaryEntry *lang;
  134. avio_printf(s->pb, "<AdaptationSet id=\"%s\"", as->id);
  135. avio_printf(s->pb, " mimeType=\"%s/webm\"",
  136. codec->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio");
  137. avio_printf(s->pb, " codecs=\"%s\"", get_codec_name(codec->codec_id));
  138. lang = av_dict_get(s->streams[as->streams[0]]->metadata, "language", NULL, 0);
  139. if (lang) avio_printf(s->pb, " lang=\"%s\"", lang->value);
  140. if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  141. avio_printf(s->pb, " width=\"%d\"", codec->width);
  142. avio_printf(s->pb, " height=\"%d\"", codec->height);
  143. } else {
  144. avio_printf(s->pb, " audioSamplingRate=\"%d\"", codec->sample_rate);
  145. }
  146. avio_printf(s->pb, " bitstreamSwitching=\"%s\"",
  147. boolean[bitstream_switching(s, as)]);
  148. avio_printf(s->pb, " subsegmentAlignment=\"%s\"",
  149. boolean[subsegment_alignment(s, as)]);
  150. for (i = 0; i < as->nb_streams; i++) {
  151. AVDictionaryEntry *kf = av_dict_get(s->streams[as->streams[i]]->metadata,
  152. CLUSTER_KEYFRAME, NULL, 0);
  153. if (!kf || !strncmp(kf->value, "0", 1)) subsegmentStartsWithSAP = 0;
  154. }
  155. avio_printf(s->pb, " subsegmentStartsWithSAP=\"%d\"", subsegmentStartsWithSAP);
  156. avio_printf(s->pb, ">\n");
  157. for (i = 0; i < as->nb_streams; i++) {
  158. AVStream *stream = s->streams[as->streams[i]];
  159. AVDictionaryEntry *irange = av_dict_get(stream->metadata, INITIALIZATION_RANGE, NULL, 0);
  160. AVDictionaryEntry *cues_start = av_dict_get(stream->metadata, CUES_START, NULL, 0);
  161. AVDictionaryEntry *cues_end = av_dict_get(stream->metadata, CUES_END, NULL, 0);
  162. AVDictionaryEntry *filename = av_dict_get(stream->metadata, FILENAME, NULL, 0);
  163. AVDictionaryEntry *bandwidth = av_dict_get(stream->metadata, BANDWIDTH, NULL, 0);
  164. if (!irange || cues_start == NULL || cues_end == NULL || filename == NULL ||
  165. !bandwidth) {
  166. return -1;
  167. }
  168. avio_printf(s->pb, "<Representation id=\"%d\"", i);
  169. avio_printf(s->pb, " bandwidth=\"%s\"", bandwidth->value);
  170. avio_printf(s->pb, ">\n");
  171. avio_printf(s->pb, "<BaseURL>%s</BaseURL>\n", filename->value);
  172. avio_printf(s->pb, "<SegmentBase\n");
  173. avio_printf(s->pb, " indexRange=\"%s-%s\">\n", cues_start->value, cues_end->value);
  174. avio_printf(s->pb, "<Initialization\n");
  175. avio_printf(s->pb, " range=\"0-%s\" />\n", irange->value);
  176. avio_printf(s->pb, "</SegmentBase>\n");
  177. avio_printf(s->pb, "</Representation>\n");
  178. }
  179. avio_printf(s->pb, "</AdaptationSet>\n");
  180. return 0;
  181. }
  182. static int to_integer(char *p, int len)
  183. {
  184. int ret;
  185. char *q = av_malloc(sizeof(char) * len);
  186. if (!q) return -1;
  187. av_strlcpy(q, p, len);
  188. ret = atoi(q);
  189. av_free(q);
  190. return ret;
  191. }
  192. static int parse_adaptation_sets(AVFormatContext *s)
  193. {
  194. WebMDashMuxContext *w = s->priv_data;
  195. char *p = w->adaptation_sets;
  196. char *q;
  197. enum { new_set, parsed_id, parsing_streams } state;
  198. // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on
  199. state = new_set;
  200. while (p < w->adaptation_sets + strlen(w->adaptation_sets)) {
  201. if (*p == ' ')
  202. continue;
  203. else if (state == new_set && !strncmp(p, "id=", 3)) {
  204. w->as = av_realloc(w->as, sizeof(*w->as) * ++w->nb_as);
  205. if (w->as == NULL) return -1;
  206. w->as[w->nb_as - 1].nb_streams = 0;
  207. w->as[w->nb_as - 1].streams = NULL;
  208. p += 3; // consume "id="
  209. q = w->as[w->nb_as - 1].id;
  210. while (*p != ',') *q++ = *p++;
  211. *q = 0;
  212. p++;
  213. state = parsed_id;
  214. } else if (state == parsed_id && !strncmp(p, "streams=", 8)) {
  215. p += 8; // consume "streams="
  216. state = parsing_streams;
  217. } else if (state == parsing_streams) {
  218. struct AdaptationSet *as = &w->as[w->nb_as - 1];
  219. q = p;
  220. while (*q != '\0' && *q != ',' && *q != ' ') q++;
  221. as->streams = av_realloc(as->streams, sizeof(*as->streams) * ++as->nb_streams);
  222. if (as->streams == NULL) return -1;
  223. as->streams[as->nb_streams - 1] = to_integer(p, q - p + 1);
  224. if (as->streams[as->nb_streams - 1] < 0) return -1;
  225. if (*q == '\0') break;
  226. if (*q == ' ') state = new_set;
  227. p = ++q;
  228. } else {
  229. return -1;
  230. }
  231. }
  232. return 0;
  233. }
  234. static int webm_dash_manifest_write_header(AVFormatContext *s)
  235. {
  236. int i;
  237. double start = 0.0;
  238. WebMDashMuxContext *w = s->priv_data;
  239. parse_adaptation_sets(s);
  240. write_header(s);
  241. avio_printf(s->pb, "<Period id=\"0\"");
  242. avio_printf(s->pb, " start=\"PT%gS\"", start);
  243. avio_printf(s->pb, " duration=\"PT%gS\"", get_duration(s));
  244. avio_printf(s->pb, " >\n");
  245. for (i = 0; i < w->nb_as; i++) {
  246. if (write_adaptation_set(s, i) < 0) return -1;
  247. }
  248. avio_printf(s->pb, "</Period>\n");
  249. write_footer(s);
  250. return 0;
  251. }
  252. static int webm_dash_manifest_write_packet(AVFormatContext *s, AVPacket *pkt)
  253. {
  254. return AVERROR_EOF;
  255. }
  256. static int webm_dash_manifest_write_trailer(AVFormatContext *s)
  257. {
  258. WebMDashMuxContext *w = s->priv_data;
  259. int i;
  260. for (i = 0; i < w->nb_as; i++) {
  261. av_freep(&w->as[i].streams);
  262. }
  263. av_freep(&w->as);
  264. return 0;
  265. }
  266. #define OFFSET(x) offsetof(WebMDashMuxContext, x)
  267. static const AVOption options[] = {
  268. { "adaptation_sets", "Adaptation sets. Syntax: id=0,streams=0,1,2 id=1,streams=3,4 and so on", OFFSET(adaptation_sets), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  269. { NULL },
  270. };
  271. #if CONFIG_WEBM_DASH_MANIFEST_MUXER
  272. static const AVClass webm_dash_class = {
  273. .class_name = "WebM DASH Manifest muxer",
  274. .item_name = av_default_item_name,
  275. .option = options,
  276. .version = LIBAVUTIL_VERSION_INT,
  277. };
  278. AVOutputFormat ff_webm_dash_manifest_muxer = {
  279. .name = "webm_dash_manifest",
  280. .long_name = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"),
  281. .mime_type = "application/xml",
  282. .extensions = "xml",
  283. .priv_data_size = sizeof(WebMDashMuxContext),
  284. .write_header = webm_dash_manifest_write_header,
  285. .write_packet = webm_dash_manifest_write_packet,
  286. .write_trailer = webm_dash_manifest_write_trailer,
  287. .priv_class = &webm_dash_class,
  288. };
  289. #endif