2 * MPEG-DASH ISO BMFF segmenter
3 * Copyright (c) 2014 Martin Storsjo
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/avutil.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/time_internal.h"
36 #include "avio_internal.h"
39 #include "os_support.h"
42 // See ISO/IEC 23009-1:2014 5.3.9.4.4
44 DASH_TMPL_ID_UNDEFINED = -1,
48 DASH_TMPL_ID_BANDWIDTH,
52 typedef struct Segment {
55 int range_length, index_length;
61 typedef struct AdaptationSet {
63 enum AVMediaType media_type;
64 AVDictionary *metadata;
67 typedef struct OutputStream {
69 int ctx_inited, as_idx;
74 int64_t init_start_pos, pos;
75 int init_range_length;
76 int nb_segments, segments_size, segment_index;
78 int64_t first_pts, start_pts, max_pts;
81 char bandwidth_str[64];
86 typedef struct DASHContext {
87 const AVClass *class; /* Class for private options. */
88 char *adaptation_sets;
92 int extra_window_size;
98 OutputStream *streams;
100 int64_t last_duration;
101 int64_t total_duration;
102 char availability_start_time[100];
104 const char *single_file_name;
105 const char *init_seg_name;
106 const char *media_seg_name;
107 const char *utc_timing_url;
110 static struct codec_string {
114 { AV_CODEC_ID_VP8, "vp8" },
115 { AV_CODEC_ID_VP9, "vp9" },
116 { AV_CODEC_ID_VORBIS, "vorbis" },
117 { AV_CODEC_ID_OPUS, "opus" },
121 static void set_codec_str(AVFormatContext *s, AVCodecParameters *par,
124 const AVCodecTag *tags[2] = { NULL, NULL };
128 // common Webm codecs are not part of RFC 6381
129 for (i = 0; codecs[i].id; i++)
130 if (codecs[i].id == par->codec_id) {
131 av_strlcpy(str, codecs[i].str, size);
135 // for codecs part of RFC 6381
136 if (par->codec_type == AVMEDIA_TYPE_VIDEO)
137 tags[0] = ff_codec_movvideo_tags;
138 else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
139 tags[0] = ff_codec_movaudio_tags;
143 tag = av_codec_get_tag(tags, par->codec_id);
151 if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
153 tags[0] = ff_mp4_obj_type;
154 oti = av_codec_get_tag(tags, par->codec_id);
156 av_strlcatf(str, size, ".%02"SCNx32, oti);
160 if (tag == MKTAG('m', 'p', '4', 'a')) {
161 if (par->extradata_size >= 2) {
162 int aot = par->extradata[0] >> 3;
164 aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
165 av_strlcatf(str, size, ".%d", aot);
167 } else if (tag == MKTAG('m', 'p', '4', 'v')) {
168 // Unimplemented, should output ProfileLevelIndication as a decimal number
169 av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
171 } else if (!strcmp(str, "avc1")) {
172 uint8_t *tmpbuf = NULL;
173 uint8_t *extradata = par->extradata;
174 int extradata_size = par->extradata_size;
177 if (extradata[0] != 1) {
179 if (avio_open_dyn_buf(&pb) < 0)
181 if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
182 ffio_free_dyn_buf(&pb);
185 extradata_size = avio_close_dyn_buf(pb, &extradata);
189 if (extradata_size >= 4)
190 av_strlcatf(str, size, ".%02x%02x%02x",
191 extradata[1], extradata[2], extradata[3]);
196 static int flush_dynbuf(OutputStream *os, int *range_length)
201 return AVERROR(EINVAL);
205 av_write_frame(os->ctx, NULL);
206 avio_flush(os->ctx->pb);
209 *range_length = avio_close_dyn_buf(os->ctx->pb, &buffer);
211 avio_write(os->out, buffer, *range_length);
215 return avio_open_dyn_buf(&os->ctx->pb);
218 static int flush_init_segment(AVFormatContext *s, OutputStream *os)
220 DASHContext *c = s->priv_data;
221 int ret, range_length;
223 ret = flush_dynbuf(os, &range_length);
227 os->pos = os->init_range_length = range_length;
229 ff_format_io_close(s, &os->out);
233 static void dash_free(AVFormatContext *s)
235 DASHContext *c = s->priv_data;
239 for (i = 0; i < c->nb_as; i++)
240 av_dict_free(&c->as[i].metadata);
247 for (i = 0; i < s->nb_streams; i++) {
248 OutputStream *os = &c->streams[i];
249 if (os->ctx && os->ctx_inited)
250 av_write_trailer(os->ctx);
251 if (os->ctx && os->ctx->pb)
252 ffio_free_dyn_buf(&os->ctx->pb);
253 ff_format_io_close(s, &os->out);
255 avformat_free_context(os->ctx);
256 for (j = 0; j < os->nb_segments; j++)
257 av_free(os->segments[j]);
258 av_free(os->segments);
260 av_freep(&c->streams);
263 static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
265 int i, start_index = 0, start_number = 1;
266 if (c->window_size) {
267 start_index = FFMAX(os->nb_segments - c->window_size, 0);
268 start_number = FFMAX(os->segment_index - c->window_size, 1);
271 if (c->use_template) {
272 int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE;
273 avio_printf(out, "\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
274 if (!c->use_timeline)
275 avio_printf(out, "duration=\"%"PRId64"\" ", c->last_duration);
276 avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\">\n", c->init_seg_name, c->media_seg_name, c->use_timeline ? start_number : 1);
277 if (c->use_timeline) {
278 int64_t cur_time = 0;
279 avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
280 for (i = start_index; i < os->nb_segments; ) {
281 Segment *seg = os->segments[i];
283 avio_printf(out, "\t\t\t\t\t\t<S ");
284 if (i == start_index || seg->time != cur_time) {
285 cur_time = seg->time;
286 avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
288 avio_printf(out, "d=\"%d\" ", seg->duration);
289 while (i + repeat + 1 < os->nb_segments &&
290 os->segments[i + repeat + 1]->duration == seg->duration &&
291 os->segments[i + repeat + 1]->time == os->segments[i + repeat]->time + os->segments[i + repeat]->duration)
294 avio_printf(out, "r=\"%d\" ", repeat);
295 avio_printf(out, "/>\n");
297 cur_time += (1 + repeat) * seg->duration;
299 avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
301 avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
302 } else if (c->single_file) {
303 avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
304 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
305 avio_printf(out, "\t\t\t\t\t<Initialization range=\"%"PRId64"-%"PRId64"\" />\n", os->init_start_pos, os->init_start_pos + os->init_range_length - 1);
306 for (i = start_index; i < os->nb_segments; i++) {
307 Segment *seg = os->segments[i];
308 avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
309 if (seg->index_length)
310 avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
311 avio_printf(out, "/>\n");
313 avio_printf(out, "\t\t\t\t</SegmentList>\n");
315 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
316 avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
317 for (i = start_index; i < os->nb_segments; i++) {
318 Segment *seg = os->segments[i];
319 avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
321 avio_printf(out, "\t\t\t\t</SegmentList>\n");
325 static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
326 size_t format_tag_size, const char **ptr) {
327 const char *next_ptr;
328 DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
330 if (av_strstart(identifier, "$$", &next_ptr)) {
331 id_type = DASH_TMPL_ID_ESCAPE;
333 } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
334 id_type = DASH_TMPL_ID_REP_ID;
335 // default to basic format, as $RepresentationID$ identifiers
336 // are not allowed to have custom format-tags.
337 av_strlcpy(format_tag, "%d", format_tag_size);
339 } else { // the following identifiers may have an explicit format_tag
340 if (av_strstart(identifier, "$Number", &next_ptr))
341 id_type = DASH_TMPL_ID_NUMBER;
342 else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
343 id_type = DASH_TMPL_ID_BANDWIDTH;
344 else if (av_strstart(identifier, "$Time", &next_ptr))
345 id_type = DASH_TMPL_ID_TIME;
347 id_type = DASH_TMPL_ID_UNDEFINED;
349 // next parse the dash format-tag and generate a c-string format tag
350 // (next_ptr now points at the first '%' at the beginning of the format-tag)
351 if (id_type != DASH_TMPL_ID_UNDEFINED) {
352 const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
353 if (next_ptr[0] == '$') { // no dash format-tag
354 snprintf(format_tag, format_tag_size, "%%%s", number_format);
357 const char *width_ptr;
358 // only tolerate single-digit width-field (i.e. up to 9-digit width)
359 if (av_strstart(next_ptr, "%0", &width_ptr) &&
360 av_isdigit(width_ptr[0]) &&
361 av_strstart(&width_ptr[1], "d$", &next_ptr)) {
362 // yes, we're using a format tag to build format_tag.
363 snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
366 av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
367 "closing '$' character or a format-string like '%%0[width]d', "
368 "where width must be a single digit\n", next_ptr);
369 id_type = DASH_TMPL_ID_UNDEFINED;
377 static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
378 const char *template, int rep_id,
379 int number, int bit_rate,
382 const char *t_cur = template;
383 while (dst_pos < buffer_size - 1 && *t_cur) {
384 char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
387 const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
389 int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
390 av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
392 dst_pos += num_copy_bytes;
394 } else { // no more DASH identifiers to substitute - just copy the rest over and break
395 av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
399 if (dst_pos >= buffer_size - 1 || !*t_cur)
402 // t_cur is now pointing to a '$' character
403 id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
405 case DASH_TMPL_ID_ESCAPE:
406 av_strlcpy(&dst[dst_pos], "$", 2);
409 case DASH_TMPL_ID_REP_ID:
410 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
412 case DASH_TMPL_ID_NUMBER:
413 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
415 case DASH_TMPL_ID_BANDWIDTH:
416 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
418 case DASH_TMPL_ID_TIME:
419 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
421 case DASH_TMPL_ID_UNDEFINED:
422 // copy over one byte and advance
423 av_strlcpy(&dst[dst_pos], t_cur, 2);
428 // t_next points just past the processed identifier
429 // n is the number of bytes that were attempted to be written to dst
430 // (may have failed to write all because buffer_size).
433 dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
438 static char *xmlescape(const char *str) {
439 int outlen = strlen(str)*3/2 + 6;
440 char *out = av_realloc(NULL, outlen + 1);
444 for (; *str; str++) {
445 if (pos + 6 > outlen) {
447 outlen = 2 * outlen + 6;
448 tmp = av_realloc(out, outlen + 1);
456 memcpy(&out[pos], "&", 5);
458 } else if (*str == '<') {
459 memcpy(&out[pos], "<", 4);
461 } else if (*str == '>') {
462 memcpy(&out[pos], ">", 4);
464 } else if (*str == '\'') {
465 memcpy(&out[pos], "'", 6);
467 } else if (*str == '\"') {
468 memcpy(&out[pos], """, 6);
478 static void write_time(AVIOContext *out, int64_t time)
480 int seconds = time / AV_TIME_BASE;
481 int fractions = time % AV_TIME_BASE;
482 int minutes = seconds / 60;
483 int hours = minutes / 60;
486 avio_printf(out, "PT");
488 avio_printf(out, "%dH", hours);
489 if (hours || minutes)
490 avio_printf(out, "%dM", minutes);
491 avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
494 static void format_date_now(char *buf, int size)
496 time_t t = time(NULL);
497 struct tm *ptm, tmbuf;
498 ptm = gmtime_r(&t, &tmbuf);
500 if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", ptm))
505 static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index)
507 DASHContext *c = s->priv_data;
508 AdaptationSet *as = &c->as[as_index];
509 AVDictionaryEntry *lang, *role;
512 avio_printf(out, "\t\t<AdaptationSet id=\"%s\" contentType=\"%s\" segmentAlignment=\"true\" bitstreamSwitching=\"true\"",
513 as->id, as->media_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio");
514 lang = av_dict_get(as->metadata, "language", NULL, 0);
516 avio_printf(out, " lang=\"%s\"", lang->value);
517 avio_printf(out, ">\n");
519 role = av_dict_get(as->metadata, "role", NULL, 0);
521 avio_printf(out, "\t\t\t<Role schemeIdUri=\"urn:mpeg:dash:role:2011\" value=\"%s\"/>\n", role->value);
523 for (i = 0; i < s->nb_streams; i++) {
524 OutputStream *os = &c->streams[i];
526 if (os->as_idx - 1 != as_index)
529 if (as->media_type == AVMEDIA_TYPE_VIDEO) {
530 avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/%s\" codecs=\"%s\"%s width=\"%d\" height=\"%d\">\n",
531 i, os->format_name, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height);
533 avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/%s\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n",
534 i, os->format_name, os->codec_str, os->bandwidth_str, s->streams[i]->codecpar->sample_rate);
535 avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
536 s->streams[i]->codecpar->channels);
538 output_segment_list(os, out, c);
539 avio_printf(out, "\t\t\t</Representation>\n");
541 avio_printf(out, "\t\t</AdaptationSet>\n");
546 static int add_adaptation_set(AVFormatContext *s, AdaptationSet **as, enum AVMediaType type)
548 DASHContext *c = s->priv_data;
550 void *mem = av_realloc(c->as, sizeof(*c->as) * (c->nb_as + 1));
552 return AVERROR(ENOMEM);
556 *as = &c->as[c->nb_as - 1];
557 memset(*as, 0, sizeof(**as));
558 (*as)->media_type = type;
563 static int adaptation_set_add_stream(AVFormatContext *s, int as_idx, int i)
565 DASHContext *c = s->priv_data;
566 AdaptationSet *as = &c->as[as_idx - 1];
567 OutputStream *os = &c->streams[i];
569 if (as->media_type != s->streams[i]->codecpar->codec_type) {
570 av_log(s, AV_LOG_ERROR, "Codec type of stream %d doesn't match AdaptationSet's media type\n", i);
571 return AVERROR(EINVAL);
572 } else if (os->as_idx) {
573 av_log(s, AV_LOG_ERROR, "Stream %d is already assigned to an AdaptationSet\n", i);
574 return AVERROR(EINVAL);
581 static int parse_adaptation_sets(AVFormatContext *s)
583 DASHContext *c = s->priv_data;
584 const char *p = c->adaptation_sets;
585 enum { new_set, parse_id, parsing_streams } state;
589 // default: one AdaptationSet for each stream
591 for (i = 0; i < s->nb_streams; i++) {
592 if ((ret = add_adaptation_set(s, &as, s->streams[i]->codecpar->codec_type)) < 0)
594 snprintf(as->id, sizeof(as->id), "%d", i);
596 c->streams[i].as_idx = c->nb_as;
601 // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on
607 } else if (state == new_set && av_strstart(p, "id=", &p)) {
609 if ((ret = add_adaptation_set(s, &as, AVMEDIA_TYPE_UNKNOWN)) < 0)
613 snprintf(as->id, sizeof(as->id), "%.*s", n, p);
619 } else if (state == parse_id && av_strstart(p, "streams=", &p)) {
620 state = parsing_streams;
621 } else if (state == parsing_streams) {
622 AdaptationSet *as = &c->as[c->nb_as - 1];
623 char idx_str[8], *end_str;
625 n = strcspn(p, " ,");
626 snprintf(idx_str, sizeof(idx_str), "%.*s", n, p);
629 // if value is "a" or "v", map all streams of that type
630 if (as->media_type == AVMEDIA_TYPE_UNKNOWN && (idx_str[0] == 'v' || idx_str[0] == 'a')) {
631 enum AVMediaType type = (idx_str[0] == 'v') ? AVMEDIA_TYPE_VIDEO : AVMEDIA_TYPE_AUDIO;
632 av_log(s, AV_LOG_DEBUG, "Map all streams of type %s\n", idx_str);
634 for (i = 0; i < s->nb_streams; i++) {
635 if (s->streams[i]->codecpar->codec_type != type)
638 as->media_type = s->streams[i]->codecpar->codec_type;
640 if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
643 } else { // select single stream
644 i = strtol(idx_str, &end_str, 10);
645 if (idx_str == end_str || i < 0 || i >= s->nb_streams) {
646 av_log(s, AV_LOG_ERROR, "Selected stream \"%s\" not found!\n", idx_str);
647 return AVERROR(EINVAL);
649 av_log(s, AV_LOG_DEBUG, "Map stream %d\n", i);
651 if (as->media_type == AVMEDIA_TYPE_UNKNOWN) {
652 as->media_type = s->streams[i]->codecpar->codec_type;
655 if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
664 return AVERROR(EINVAL);
669 // check for unassigned streams
670 for (i = 0; i < s->nb_streams; i++) {
671 OutputStream *os = &c->streams[i];
673 av_log(s, AV_LOG_ERROR, "Stream %d is not mapped to an AdaptationSet\n", i);
674 return AVERROR(EINVAL);
680 static int write_manifest(AVFormatContext *s, int final)
682 DASHContext *c = s->priv_data;
684 char temp_filename[1024];
686 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
688 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
689 ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
691 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
694 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
695 avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
696 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
697 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
698 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
699 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
700 "\ttype=\"%s\"\n", final ? "static" : "dynamic");
702 avio_printf(out, "\tmediaPresentationDuration=\"");
703 write_time(out, c->total_duration);
704 avio_printf(out, "\"\n");
706 int64_t update_period = c->last_duration / AV_TIME_BASE;
708 if (c->use_template && !c->use_timeline)
710 avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
711 avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
712 if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
713 format_date_now(c->availability_start_time, sizeof(c->availability_start_time));
715 if (c->availability_start_time[0])
716 avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
717 format_date_now(now_str, sizeof(now_str));
719 avio_printf(out, "\tpublishTime=\"%s\"\n", now_str);
720 if (c->window_size && c->use_template) {
721 avio_printf(out, "\ttimeShiftBufferDepth=\"");
722 write_time(out, c->last_duration * c->window_size);
723 avio_printf(out, "\"\n");
726 avio_printf(out, "\tminBufferTime=\"");
727 write_time(out, c->last_duration * 2);
728 avio_printf(out, "\">\n");
729 avio_printf(out, "\t<ProgramInformation>\n");
731 char *escaped = xmlescape(title->value);
732 avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
735 avio_printf(out, "\t</ProgramInformation>\n");
736 if (c->utc_timing_url)
737 avio_printf(out, "\t<UTCTiming schemeIdUri=\"urn:mpeg:dash:utc:http-xsdate:2014\" value=\"%s\"/>\n", c->utc_timing_url);
739 if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
740 OutputStream *os = &c->streams[0];
741 int start_index = FFMAX(os->nb_segments - c->window_size, 0);
742 int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
743 avio_printf(out, "\t<Period id=\"0\" start=\"");
744 write_time(out, start_time);
745 avio_printf(out, "\">\n");
747 avio_printf(out, "\t<Period id=\"0\" start=\"PT0.0S\">\n");
750 for (i = 0; i < c->nb_as; i++) {
751 if ((ret = write_adaptation_set(s, out, i)) < 0)
754 avio_printf(out, "\t</Period>\n");
755 avio_printf(out, "</MPD>\n");
757 ff_format_io_close(s, &out);
758 return ff_rename(temp_filename, s->filename);
761 static int dict_copy_entry(AVDictionary **dst, const AVDictionary *src, const char *key)
763 AVDictionaryEntry *entry = av_dict_get(src, key, NULL, 0);
765 av_dict_set(dst, key, entry->value, AV_DICT_DONT_OVERWRITE);
769 static int dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
772 snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
773 flags &= ~AV_DICT_DONT_STRDUP_VAL;
774 return av_dict_set(pm, key, valuestr, flags);
777 static int dash_write_header(AVFormatContext *s)
779 DASHContext *c = s->priv_data;
784 if (c->single_file_name)
789 av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
790 ptr = strrchr(c->dirname, '/');
792 av_strlcpy(basename, &ptr[1], sizeof(basename));
795 c->dirname[0] = '\0';
796 av_strlcpy(basename, s->filename, sizeof(basename));
799 ptr = strrchr(basename, '.');
803 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
805 ret = AVERROR(ENOMEM);
809 if ((ret = parse_adaptation_sets(s)) < 0)
812 for (i = 0; i < s->nb_streams; i++) {
813 OutputStream *os = &c->streams[i];
814 AdaptationSet *as = &c->as[os->as_idx - 1];
815 AVFormatContext *ctx;
817 AVDictionary *opts = NULL;
820 os->bit_rate = s->streams[i]->codecpar->bit_rate;
822 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
823 " bandwidth=\"%d\"", os->bit_rate);
825 int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
826 AV_LOG_ERROR : AV_LOG_WARNING;
827 av_log(s, level, "No bit rate set for stream %d\n", i);
828 if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
829 ret = AVERROR(EINVAL);
834 // copy AdaptationSet language and role from stream metadata
835 dict_copy_entry(&as->metadata, s->streams[i]->metadata, "language");
836 dict_copy_entry(&as->metadata, s->streams[i]->metadata, "role");
838 ctx = avformat_alloc_context();
840 ret = AVERROR(ENOMEM);
844 // choose muxer based on codec: webm for VP8/9 and opus, mp4 otherwise
845 // note: os->format_name is also used as part of the mimetype of the
846 // representation, e.g. video/<format_name>
847 if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VP8 ||
848 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VP9 ||
849 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_OPUS ||
850 s->streams[i]->codecpar->codec_id == AV_CODEC_ID_VORBIS) {
851 snprintf(os->format_name, sizeof(os->format_name), "webm");
853 snprintf(os->format_name, sizeof(os->format_name), "mp4");
855 ctx->oformat = av_guess_format(os->format_name, NULL, NULL);
857 ret = AVERROR_MUXER_NOT_FOUND;
861 ctx->interrupt_callback = s->interrupt_callback;
862 ctx->opaque = s->opaque;
863 ctx->io_close = s->io_close;
864 ctx->io_open = s->io_open;
866 if (!(st = avformat_new_stream(ctx, NULL))) {
867 ret = AVERROR(ENOMEM);
870 avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
871 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
872 st->time_base = s->streams[i]->time_base;
873 ctx->avoid_negative_ts = s->avoid_negative_ts;
875 if ((ret = avio_open_dyn_buf(&ctx->pb)) < 0)
878 if (c->single_file) {
879 if (c->single_file_name)
880 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
882 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
884 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
886 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
887 ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
890 os->init_start_pos = 0;
892 if (!strcmp(os->format_name, "mp4")) {
893 av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
895 dict_set_int(&opts, "cluster_time_limit", c->min_seg_duration / 1000, 0);
896 dict_set_int(&opts, "cluster_size_limit", 5 * 1024 * 1024, 0); // set a large cluster size limit
898 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
905 av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
907 // Flush init segment
908 // except for mp4, since delay_moov is set and the init segment
909 // is then flushed after the first packets
910 if (strcmp(os->format_name, "mp4")) {
911 flush_init_segment(s, os);
914 s->streams[i]->time_base = st->time_base;
915 // If the muxer wants to shift timestamps, request to have them shifted
916 // already before being handed to this muxer, so we don't have mismatches
917 // between the MPD and the actual segments.
918 s->avoid_negative_ts = ctx->avoid_negative_ts;
919 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
922 set_codec_str(s, st->codecpar, os->codec_str, sizeof(os->codec_str));
923 os->first_pts = AV_NOPTS_VALUE;
924 os->max_pts = AV_NOPTS_VALUE;
925 os->last_dts = AV_NOPTS_VALUE;
926 os->segment_index = 1;
929 if (!c->has_video && c->min_seg_duration <= 0) {
930 av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
931 ret = AVERROR(EINVAL);
933 ret = write_manifest(s, 0);
935 av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
943 static int add_segment(OutputStream *os, const char *file,
944 int64_t time, int duration,
945 int64_t start_pos, int64_t range_length,
946 int64_t index_length)
950 if (os->nb_segments >= os->segments_size) {
951 os->segments_size = (os->segments_size + 1) * 2;
952 if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
953 os->segments_size)) < 0) {
954 os->segments_size = 0;
959 seg = av_mallocz(sizeof(*seg));
961 return AVERROR(ENOMEM);
962 av_strlcpy(seg->file, file, sizeof(seg->file));
964 seg->duration = duration;
965 if (seg->time < 0) { // If pts<0, it is expected to be cut away with an edit list
966 seg->duration += seg->time;
969 seg->start_pos = start_pos;
970 seg->range_length = range_length;
971 seg->index_length = index_length;
972 os->segments[os->nb_segments++] = seg;
977 static void write_styp(AVIOContext *pb)
980 ffio_wfourcc(pb, "styp");
981 ffio_wfourcc(pb, "msdh");
982 avio_wb32(pb, 0); /* minor */
983 ffio_wfourcc(pb, "msdh");
984 ffio_wfourcc(pb, "msix");
987 static void find_index_range(AVFormatContext *s, const char *full_path,
988 int64_t pos, int *index_length)
994 ret = s->io_open(s, &pb, full_path, AVIO_FLAG_READ, NULL);
997 if (avio_seek(pb, pos, SEEK_SET) != pos) {
998 ff_format_io_close(s, &pb);
1001 ret = avio_read(pb, buf, 8);
1002 ff_format_io_close(s, &pb);
1005 if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
1007 *index_length = AV_RB32(&buf[0]);
1010 static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
1011 AVCodecParameters *par)
1015 if (os->ctx->streams[0]->codecpar->extradata_size || !par->extradata_size)
1018 extradata = av_malloc(par->extradata_size);
1021 return AVERROR(ENOMEM);
1023 memcpy(extradata, par->extradata, par->extradata_size);
1025 os->ctx->streams[0]->codecpar->extradata = extradata;
1026 os->ctx->streams[0]->codecpar->extradata_size = par->extradata_size;
1028 set_codec_str(s, par, os->codec_str, sizeof(os->codec_str));
1033 static int dash_flush(AVFormatContext *s, int final, int stream)
1035 DASHContext *c = s->priv_data;
1037 int cur_flush_segment_index = 0;
1039 cur_flush_segment_index = c->streams[stream].segment_index;
1041 for (i = 0; i < s->nb_streams; i++) {
1042 OutputStream *os = &c->streams[i];
1043 char filename[1024] = "", full_path[1024], temp_path[1024];
1044 int range_length, index_length = 0;
1046 if (!os->packets_written)
1049 // Flush the single stream that got a keyframe right now.
1050 // Flush all audio streams as well, in sync with video keyframes,
1051 // but not the other video streams.
1052 if (stream >= 0 && i != stream) {
1053 if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
1055 // Make sure we don't flush audio streams multiple times, when
1056 // all video streams are flushed one at a time.
1057 if (c->has_video && os->segment_index > cur_flush_segment_index)
1061 if (!os->init_range_length) {
1062 flush_init_segment(s, os);
1065 if (!c->single_file) {
1066 dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
1067 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
1068 snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
1069 ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);
1072 if (!strcmp(os->format_name, "mp4"))
1073 write_styp(os->ctx->pb);
1075 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
1078 ret = flush_dynbuf(os, &range_length);
1081 os->packets_written = 0;
1083 if (c->single_file) {
1084 find_index_range(s, full_path, os->pos, &index_length);
1086 ff_format_io_close(s, &os->out);
1087 ret = ff_rename(temp_path, full_path);
1092 if (!os->bit_rate) {
1093 // calculate average bitrate of first segment
1094 int64_t bitrate = (int64_t) range_length * 8 * AV_TIME_BASE / (os->max_pts - os->start_pts);
1096 os->bit_rate = bitrate;
1097 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
1098 " bandwidth=\"%d\"", os->bit_rate);
1101 add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length);
1102 av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
1104 os->pos += range_length;
1107 if (c->window_size || (final && c->remove_at_exit)) {
1108 for (i = 0; i < s->nb_streams; i++) {
1109 OutputStream *os = &c->streams[i];
1111 int remove = os->nb_segments - c->window_size - c->extra_window_size;
1112 if (final && c->remove_at_exit)
1113 remove = os->nb_segments;
1115 for (j = 0; j < remove; j++) {
1116 char filename[1024];
1117 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
1119 av_free(os->segments[j]);
1121 os->nb_segments -= remove;
1122 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
1128 ret = write_manifest(s, final);
1132 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
1134 DASHContext *c = s->priv_data;
1135 AVStream *st = s->streams[pkt->stream_index];
1136 OutputStream *os = &c->streams[pkt->stream_index];
1139 ret = update_stream_extradata(s, os, st->codecpar);
1143 // Fill in a heuristic guess of the packet duration, if none is available.
1144 // The mp4 muxer will do something similar (for the last packet in a fragment)
1145 // if nothing is set (setting it for the other packets doesn't hurt).
1146 // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
1147 // invoke its heuristic (this doesn't have to be identical to that algorithm),
1148 // so that we know the exact timestamps of fragments.
1149 if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
1150 pkt->duration = pkt->dts - os->last_dts;
1151 os->last_dts = pkt->dts;
1153 // If forcing the stream to start at 0, the mp4 muxer will set the start
1154 // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
1155 if (os->first_pts == AV_NOPTS_VALUE &&
1156 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
1157 pkt->pts -= pkt->dts;
1161 if (os->first_pts == AV_NOPTS_VALUE)
1162 os->first_pts = pkt->pts;
1164 if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
1165 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
1166 av_compare_ts(pkt->pts - os->start_pts, st->time_base,
1167 c->min_seg_duration, AV_TIME_BASE_Q) >= 0) {
1168 int64_t prev_duration = c->last_duration;
1170 c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
1173 c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
1177 if ((!c->use_timeline || !c->use_template) && prev_duration) {
1178 if (c->last_duration < prev_duration*9/10 ||
1179 c->last_duration > prev_duration*11/10) {
1180 av_log(s, AV_LOG_WARNING,
1181 "Segment durations differ too much, enable use_timeline "
1182 "and use_template, or keep a stricter keyframe interval\n");
1186 if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
1190 if (!os->packets_written) {
1191 // If we wrote a previous segment, adjust the start time of the segment
1192 // to the end of the previous one (which is the same as the mp4 muxer
1193 // does). This avoids gaps in the timeline.
1194 if (os->max_pts != AV_NOPTS_VALUE)
1195 os->start_pts = os->max_pts;
1197 os->start_pts = pkt->pts;
1199 if (os->max_pts == AV_NOPTS_VALUE)
1200 os->max_pts = pkt->pts + pkt->duration;
1202 os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
1203 os->packets_written++;
1204 return ff_write_chained(os->ctx, 0, pkt, s);
1207 static int dash_write_trailer(AVFormatContext *s)
1209 DASHContext *c = s->priv_data;
1211 if (s->nb_streams > 0) {
1212 OutputStream *os = &c->streams[0];
1213 // If no segments have been written so far, try to do a crude
1214 // guess of the segment duration
1215 if (!c->last_duration)
1216 c->last_duration = av_rescale_q(os->max_pts - os->start_pts,
1217 s->streams[0]->time_base,
1219 c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
1220 s->streams[0]->time_base,
1223 dash_flush(s, 1, -1);
1225 if (c->remove_at_exit) {
1226 char filename[1024];
1228 for (i = 0; i < s->nb_streams; i++) {
1229 OutputStream *os = &c->streams[i];
1230 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
1233 unlink(s->filename);
1240 #define OFFSET(x) offsetof(DASHContext, x)
1241 #define E AV_OPT_FLAG_ENCODING_PARAM
1242 static const AVOption options[] = {
1243 { "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 },
1244 { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
1245 { "extra_window_size", "number of segments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E },
1246 { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
1247 { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
1248 { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
1249 { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
1250 { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
1251 { "single_file_name", "DASH-templated name to be used for baseURL. Implies storing all segments in one file, accessed using byte ranges", OFFSET(single_file_name), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
1252 { "init_seg_name", "DASH-templated name to used for the initialization segment", OFFSET(init_seg_name), AV_OPT_TYPE_STRING, {.str = "init-stream$RepresentationID$.m4s"}, 0, 0, E },
1253 { "media_seg_name", "DASH-templated name to used for the media segments", OFFSET(media_seg_name), AV_OPT_TYPE_STRING, {.str = "chunk-stream$RepresentationID$-$Number%05d$.m4s"}, 0, 0, E },
1254 { "utc_timing_url", "URL of the page that will return the UTC timestamp in ISO format", OFFSET(utc_timing_url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
1258 static const AVClass dash_class = {
1259 .class_name = "dash muxer",
1260 .item_name = av_default_item_name,
1262 .version = LIBAVUTIL_VERSION_INT,
1265 AVOutputFormat ff_dash_muxer = {
1267 .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
1268 .priv_data_size = sizeof(DASHContext),
1269 .audio_codec = AV_CODEC_ID_AAC,
1270 .video_codec = AV_CODEC_ID_H264,
1271 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
1272 .write_header = dash_write_header,
1273 .write_packet = dash_write_packet,
1274 .write_trailer = dash_write_trailer,
1275 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1276 .priv_class = &dash_class,