]> git.sesse.net Git - ffmpeg/blob - libavformat/dashenc.c
vp9_raw_reorder_bsf: Remove a redundant allocation
[ffmpeg] / libavformat / dashenc.c
1 /*
2  * MPEG-DASH ISO BMFF segmenter
3  * Copyright (c) 2014 Martin Storsjo
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
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"
33
34 #include "avc.h"
35 #include "avformat.h"
36 #include "avio_internal.h"
37 #include "internal.h"
38 #include "isom.h"
39 #include "os_support.h"
40 #include "url.h"
41
42 // See ISO/IEC 23009-1:2014 5.3.9.4.4
43 typedef enum {
44     DASH_TMPL_ID_UNDEFINED = -1,
45     DASH_TMPL_ID_ESCAPE,
46     DASH_TMPL_ID_REP_ID,
47     DASH_TMPL_ID_NUMBER,
48     DASH_TMPL_ID_BANDWIDTH,
49     DASH_TMPL_ID_TIME,
50 } DASHTmplId;
51
52 typedef struct Segment {
53     char file[1024];
54     int64_t start_pos;
55     int range_length, index_length;
56     int64_t time;
57     int duration;
58     int n;
59 } Segment;
60
61 typedef struct AdaptationSet {
62     char id[10];
63     enum AVMediaType media_type;
64     AVDictionary *metadata;
65 } AdaptationSet;
66
67 typedef struct OutputStream {
68     AVFormatContext *ctx;
69     int ctx_inited, as_idx;
70     AVIOContext *out;
71     char format_name[8];
72     int packets_written;
73     char initfile[1024];
74     int64_t init_start_pos, pos;
75     int init_range_length;
76     int nb_segments, segments_size, segment_index;
77     Segment **segments;
78     int64_t first_pts, start_pts, max_pts;
79     int64_t last_dts;
80     int bit_rate;
81     char bandwidth_str[64];
82
83     char codec_str[100];
84 } OutputStream;
85
86 typedef struct DASHContext {
87     const AVClass *class;  /* Class for private options. */
88     char *adaptation_sets;
89     AdaptationSet *as;
90     int nb_as;
91     int window_size;
92     int extra_window_size;
93     int min_seg_duration;
94     int remove_at_exit;
95     int use_template;
96     int use_timeline;
97     int single_file;
98     OutputStream *streams;
99     int has_video;
100     int64_t last_duration;
101     int64_t total_duration;
102     char availability_start_time[100];
103     char dirname[1024];
104     const char *single_file_name;
105     const char *init_seg_name;
106     const char *media_seg_name;
107     const char *utc_timing_url;
108 } DASHContext;
109
110 static struct codec_string {
111     int id;
112     const char *str;
113 } codecs[] = {
114     { AV_CODEC_ID_VP8, "vp8" },
115     { AV_CODEC_ID_VP9, "vp9" },
116     { AV_CODEC_ID_VORBIS, "vorbis" },
117     { AV_CODEC_ID_OPUS, "opus" },
118     { 0, NULL }
119 };
120
121 static void set_codec_str(AVFormatContext *s, AVCodecParameters *par,
122                           char *str, int size)
123 {
124     const AVCodecTag *tags[2] = { NULL, NULL };
125     uint32_t tag;
126     int i;
127
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);
132             return;
133         }
134
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;
140     else
141         return;
142
143     tag = av_codec_get_tag(tags, par->codec_id);
144     if (!tag)
145         return;
146     if (size < 5)
147         return;
148
149     AV_WL32(str, tag);
150     str[4] = '\0';
151     if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
152         uint32_t oti;
153         tags[0] = ff_mp4_obj_type;
154         oti = av_codec_get_tag(tags, par->codec_id);
155         if (oti)
156             av_strlcatf(str, size, ".%02"SCNx32, oti);
157         else
158             return;
159
160         if (tag == MKTAG('m', 'p', '4', 'a')) {
161             if (par->extradata_size >= 2) {
162                 int aot = par->extradata[0] >> 3;
163                 if (aot == 31)
164                     aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
165                 av_strlcatf(str, size, ".%d", aot);
166             }
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");
170         }
171     } else if (!strcmp(str, "avc1")) {
172         uint8_t *tmpbuf = NULL;
173         uint8_t *extradata = par->extradata;
174         int extradata_size = par->extradata_size;
175         if (!extradata_size)
176             return;
177         if (extradata[0] != 1) {
178             AVIOContext *pb;
179             if (avio_open_dyn_buf(&pb) < 0)
180                 return;
181             if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
182                 ffio_free_dyn_buf(&pb);
183                 return;
184             }
185             extradata_size = avio_close_dyn_buf(pb, &extradata);
186             tmpbuf = extradata;
187         }
188
189         if (extradata_size >= 4)
190             av_strlcatf(str, size, ".%02x%02x%02x",
191                         extradata[1], extradata[2], extradata[3]);
192         av_free(tmpbuf);
193     }
194 }
195
196 static int flush_dynbuf(OutputStream *os, int *range_length)
197 {
198     uint8_t *buffer;
199
200     if (!os->ctx->pb) {
201         return AVERROR(EINVAL);
202     }
203
204     // flush
205     av_write_frame(os->ctx, NULL);
206     avio_flush(os->ctx->pb);
207
208     // write out to file
209     *range_length = avio_close_dyn_buf(os->ctx->pb, &buffer);
210     os->ctx->pb = NULL;
211     avio_write(os->out, buffer, *range_length);
212     av_free(buffer);
213
214     // re-open buffer
215     return avio_open_dyn_buf(&os->ctx->pb);
216 }
217
218 static int flush_init_segment(AVFormatContext *s, OutputStream *os)
219 {
220     DASHContext *c = s->priv_data;
221     int ret, range_length;
222
223     ret = flush_dynbuf(os, &range_length);
224     if (ret < 0)
225         return ret;
226
227     os->pos = os->init_range_length = range_length;
228     if (!c->single_file)
229         ff_format_io_close(s, &os->out);
230     return 0;
231 }
232
233 static void dash_free(AVFormatContext *s)
234 {
235     DASHContext *c = s->priv_data;
236     int i, j;
237
238     if (c->as) {
239         for (i = 0; i < c->nb_as; i++)
240             av_dict_free(&c->as[i].metadata);
241         av_freep(&c->as);
242         c->nb_as = 0;
243     }
244
245     if (!c->streams)
246         return;
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);
254         if (os->ctx)
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);
259     }
260     av_freep(&c->streams);
261 }
262
263 static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
264 {
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);
269     }
270
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];
282                 int repeat = 0;
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);
287                 }
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)
292                     repeat++;
293                 if (repeat > 0)
294                     avio_printf(out, "r=\"%d\" ", repeat);
295                 avio_printf(out, "/>\n");
296                 i += 1 + repeat;
297                 cur_time += (1 + repeat) * seg->duration;
298             }
299             avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
300         }
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");
312         }
313         avio_printf(out, "\t\t\t\t</SegmentList>\n");
314     } else {
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);
320         }
321         avio_printf(out, "\t\t\t\t</SegmentList>\n");
322     }
323 }
324
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;
329
330     if (av_strstart(identifier, "$$", &next_ptr)) {
331         id_type = DASH_TMPL_ID_ESCAPE;
332         *ptr = next_ptr;
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);
338         *ptr = next_ptr;
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;
346         else
347             id_type = DASH_TMPL_ID_UNDEFINED;
348
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);
355                 *ptr = &next_ptr[1];
356             } else {
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);
364                     *ptr = next_ptr;
365                 } else {
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;
370                 }
371             }
372         }
373     }
374     return id_type;
375 }
376
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,
380                                   int64_t time) {
381     int dst_pos = 0;
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]
385         int n = 0;
386         DASHTmplId id_type;
387         const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
388         if (t_next) {
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);
391             // advance
392             dst_pos += num_copy_bytes;
393             t_cur = t_next;
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);
396             break;
397         }
398
399         if (dst_pos >= buffer_size - 1 || !*t_cur)
400             break;
401
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);
404         switch (id_type) {
405         case DASH_TMPL_ID_ESCAPE:
406             av_strlcpy(&dst[dst_pos], "$", 2);
407             n = 1;
408             break;
409         case DASH_TMPL_ID_REP_ID:
410             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
411             break;
412         case DASH_TMPL_ID_NUMBER:
413             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
414             break;
415         case DASH_TMPL_ID_BANDWIDTH:
416             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
417             break;
418         case DASH_TMPL_ID_TIME:
419             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
420             break;
421         case DASH_TMPL_ID_UNDEFINED:
422             // copy over one byte and advance
423             av_strlcpy(&dst[dst_pos], t_cur, 2);
424             n = 1;
425             t_next = &t_cur[1];
426             break;
427         }
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).
431
432         // advance
433         dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
434         t_cur = t_next;
435     }
436 }
437
438 static char *xmlescape(const char *str) {
439     int outlen = strlen(str)*3/2 + 6;
440     char *out = av_realloc(NULL, outlen + 1);
441     int pos = 0;
442     if (!out)
443         return NULL;
444     for (; *str; str++) {
445         if (pos + 6 > outlen) {
446             char *tmp;
447             outlen = 2 * outlen + 6;
448             tmp = av_realloc(out, outlen + 1);
449             if (!tmp) {
450                 av_free(out);
451                 return NULL;
452             }
453             out = tmp;
454         }
455         if (*str == '&') {
456             memcpy(&out[pos], "&amp;", 5);
457             pos += 5;
458         } else if (*str == '<') {
459             memcpy(&out[pos], "&lt;", 4);
460             pos += 4;
461         } else if (*str == '>') {
462             memcpy(&out[pos], "&gt;", 4);
463             pos += 4;
464         } else if (*str == '\'') {
465             memcpy(&out[pos], "&apos;", 6);
466             pos += 6;
467         } else if (*str == '\"') {
468             memcpy(&out[pos], "&quot;", 6);
469             pos += 6;
470         } else {
471             out[pos++] = *str;
472         }
473     }
474     out[pos] = '\0';
475     return out;
476 }
477
478 static void write_time(AVIOContext *out, int64_t time)
479 {
480     int seconds = time / AV_TIME_BASE;
481     int fractions = time % AV_TIME_BASE;
482     int minutes = seconds / 60;
483     int hours = minutes / 60;
484     seconds %= 60;
485     minutes %= 60;
486     avio_printf(out, "PT");
487     if (hours)
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));
492 }
493
494 static void format_date_now(char *buf, int size)
495 {
496     time_t t = time(NULL);
497     struct tm *ptm, tmbuf;
498     ptm = gmtime_r(&t, &tmbuf);
499     if (ptm) {
500         if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", ptm))
501             buf[0] = '\0';
502     }
503 }
504
505 static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index)
506 {
507     DASHContext *c = s->priv_data;
508     AdaptationSet *as = &c->as[as_index];
509     AVDictionaryEntry *lang, *role;
510     int i;
511
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);
515     if (lang)
516         avio_printf(out, " lang=\"%s\"", lang->value);
517     avio_printf(out, ">\n");
518
519     role = av_dict_get(as->metadata, "role", NULL, 0);
520     if (role)
521         avio_printf(out, "\t\t\t<Role schemeIdUri=\"urn:mpeg:dash:role:2011\" value=\"%s\"/>\n", role->value);
522
523     for (i = 0; i < s->nb_streams; i++) {
524         OutputStream *os = &c->streams[i];
525
526         if (os->as_idx - 1 != as_index)
527             continue;
528
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);
532         } else {
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);
537         }
538         output_segment_list(os, out, c);
539         avio_printf(out, "\t\t\t</Representation>\n");
540     }
541     avio_printf(out, "\t\t</AdaptationSet>\n");
542
543     return 0;
544 }
545
546 static int add_adaptation_set(AVFormatContext *s, AdaptationSet **as, enum AVMediaType type)
547 {
548     DASHContext *c = s->priv_data;
549
550     void *mem = av_realloc(c->as, sizeof(*c->as) * (c->nb_as + 1));
551     if (!mem)
552         return AVERROR(ENOMEM);
553     c->as = mem;
554     ++c->nb_as;
555
556     *as = &c->as[c->nb_as - 1];
557     memset(*as, 0, sizeof(**as));
558     (*as)->media_type = type;
559
560     return 0;
561 }
562
563 static int adaptation_set_add_stream(AVFormatContext *s, int as_idx, int i)
564 {
565     DASHContext *c = s->priv_data;
566     AdaptationSet *as = &c->as[as_idx - 1];
567     OutputStream *os = &c->streams[i];
568
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);
575     }
576     os->as_idx = as_idx;
577
578     return 0;
579 }
580
581 static int parse_adaptation_sets(AVFormatContext *s)
582 {
583     DASHContext *c = s->priv_data;
584     const char *p = c->adaptation_sets;
585     enum { new_set, parse_id, parsing_streams } state;
586     AdaptationSet *as;
587     int i, n, ret;
588
589     // default: one AdaptationSet for each stream
590     if (!p) {
591         for (i = 0; i < s->nb_streams; i++) {
592             if ((ret = add_adaptation_set(s, &as, s->streams[i]->codecpar->codec_type)) < 0)
593                 return ret;
594             snprintf(as->id, sizeof(as->id), "%d", i);
595
596             c->streams[i].as_idx = c->nb_as;
597         }
598         goto end;
599     }
600
601     // syntax id=0,streams=0,1,2 id=1,streams=3,4 and so on
602     state = new_set;
603     while (*p) {
604         if (*p == ' ') {
605             p++;
606             continue;
607         } else if (state == new_set && av_strstart(p, "id=", &p)) {
608
609             if ((ret = add_adaptation_set(s, &as, AVMEDIA_TYPE_UNKNOWN)) < 0)
610                 return ret;
611
612             n = strcspn(p, ",");
613             snprintf(as->id, sizeof(as->id), "%.*s", n, p);
614
615             p += n;
616             if (*p)
617                 p++;
618             state = parse_id;
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;
624
625             n = strcspn(p, " ,");
626             snprintf(idx_str, sizeof(idx_str), "%.*s", n, p);
627             p += n;
628
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);
633
634                 for (i = 0; i < s->nb_streams; i++) {
635                     if (s->streams[i]->codecpar->codec_type != type)
636                         continue;
637
638                     as->media_type = s->streams[i]->codecpar->codec_type;
639
640                     if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
641                         return ret;
642                 }
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);
648                 }
649                 av_log(s, AV_LOG_DEBUG, "Map stream %d\n", i);
650
651                 if (as->media_type == AVMEDIA_TYPE_UNKNOWN) {
652                     as->media_type = s->streams[i]->codecpar->codec_type;
653                 }
654
655                 if ((ret = adaptation_set_add_stream(s, c->nb_as, i)) < 0)
656                     return ret;
657             }
658
659             if (*p == ' ')
660                 state = new_set;
661             if (*p)
662                 p++;
663         } else {
664             return AVERROR(EINVAL);
665         }
666     }
667
668 end:
669     // check for unassigned streams
670     for (i = 0; i < s->nb_streams; i++) {
671         OutputStream *os = &c->streams[i];
672         if (!os->as_idx) {
673             av_log(s, AV_LOG_ERROR, "Stream %d is not mapped to an AdaptationSet\n", i);
674             return AVERROR(EINVAL);
675         }
676     }
677     return 0;
678 }
679
680 static int write_manifest(AVFormatContext *s, int final)
681 {
682     DASHContext *c = s->priv_data;
683     AVIOContext *out;
684     char temp_filename[1024];
685     int ret, i;
686     AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
687
688     snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
689     ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
690     if (ret < 0) {
691         av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
692         return ret;
693     }
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");
701     if (final) {
702         avio_printf(out, "\tmediaPresentationDuration=\"");
703         write_time(out, c->total_duration);
704         avio_printf(out, "\"\n");
705     } else {
706         int64_t update_period = c->last_duration / AV_TIME_BASE;
707         char now_str[100];
708         if (c->use_template && !c->use_timeline)
709             update_period = 500;
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));
714         }
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));
718         if (now_str[0])
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");
724         }
725     }
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");
730     if (title) {
731         char *escaped = xmlescape(title->value);
732         avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
733         av_free(escaped);
734     }
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);
738
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");
746     } else {
747         avio_printf(out, "\t<Period id=\"0\" start=\"PT0.0S\">\n");
748     }
749
750     for (i = 0; i < c->nb_as; i++) {
751         if ((ret = write_adaptation_set(s, out, i)) < 0)
752             return ret;
753     }
754     avio_printf(out, "\t</Period>\n");
755     avio_printf(out, "</MPD>\n");
756     avio_flush(out);
757     ff_format_io_close(s, &out);
758     return ff_rename(temp_filename, s->filename);
759 }
760
761 static int dict_copy_entry(AVDictionary **dst, const AVDictionary *src, const char *key)
762 {
763     AVDictionaryEntry *entry = av_dict_get(src, key, NULL, 0);
764     if (entry)
765         av_dict_set(dst, key, entry->value, AV_DICT_DONT_OVERWRITE);
766     return 0;
767 }
768
769 static int dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
770 {
771     char valuestr[22];
772     snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
773     flags &= ~AV_DICT_DONT_STRDUP_VAL;
774     return av_dict_set(pm, key, valuestr, flags);
775 }
776
777 static int dash_write_header(AVFormatContext *s)
778 {
779     DASHContext *c = s->priv_data;
780     int ret = 0, i;
781     char *ptr;
782     char basename[1024];
783
784     if (c->single_file_name)
785         c->single_file = 1;
786     if (c->single_file)
787         c->use_template = 0;
788
789     av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
790     ptr = strrchr(c->dirname, '/');
791     if (ptr) {
792         av_strlcpy(basename, &ptr[1], sizeof(basename));
793         ptr[1] = '\0';
794     } else {
795         c->dirname[0] = '\0';
796         av_strlcpy(basename, s->filename, sizeof(basename));
797     }
798
799     ptr = strrchr(basename, '.');
800     if (ptr)
801         *ptr = '\0';
802
803     c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
804     if (!c->streams) {
805         ret = AVERROR(ENOMEM);
806         goto fail;
807     }
808
809     if ((ret = parse_adaptation_sets(s)) < 0)
810         goto fail;
811
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;
816         AVStream *st;
817         AVDictionary *opts = NULL;
818         char filename[1024];
819
820         os->bit_rate = s->streams[i]->codecpar->bit_rate;
821         if (os->bit_rate) {
822             snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
823                      " bandwidth=\"%d\"", os->bit_rate);
824         } else {
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);
830                 goto fail;
831             }
832         }
833
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");
837
838         ctx = avformat_alloc_context();
839         if (!ctx) {
840             ret = AVERROR(ENOMEM);
841             goto fail;
842         }
843
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");
852         } else {
853             snprintf(os->format_name, sizeof(os->format_name), "mp4");
854         }
855         ctx->oformat = av_guess_format(os->format_name, NULL, NULL);
856         if (!ctx->oformat) {
857             ret = AVERROR_MUXER_NOT_FOUND;
858             goto fail;
859         }
860         os->ctx = ctx;
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;
865
866         if (!(st = avformat_new_stream(ctx, NULL))) {
867             ret = AVERROR(ENOMEM);
868             goto fail;
869         }
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;
874
875         if ((ret = avio_open_dyn_buf(&ctx->pb)) < 0)
876             goto fail;
877
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);
881             else
882                 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
883         } else {
884             dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
885         }
886         snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
887         ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
888         if (ret < 0)
889             goto fail;
890         os->init_start_pos = 0;
891
892         if (!strcmp(os->format_name, "mp4")) {
893             av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
894         } else {
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
897         }
898         if ((ret = avformat_write_header(ctx, &opts)) < 0) {
899              goto fail;
900         }
901         os->ctx_inited = 1;
902         avio_flush(ctx->pb);
903         av_dict_free(&opts);
904
905         av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
906
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);
912         }
913
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)
920             c->has_video = 1;
921
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;
927     }
928
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);
932     }
933     ret = write_manifest(s, 0);
934     if (!ret)
935         av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
936
937 fail:
938     if (ret)
939         dash_free(s);
940     return ret;
941 }
942
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)
947 {
948     int err;
949     Segment *seg;
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;
955             os->nb_segments = 0;
956             return err;
957         }
958     }
959     seg = av_mallocz(sizeof(*seg));
960     if (!seg)
961         return AVERROR(ENOMEM);
962     av_strlcpy(seg->file, file, sizeof(seg->file));
963     seg->time = time;
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;
967         seg->time = 0;
968     }
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;
973     os->segment_index++;
974     return 0;
975 }
976
977 static void write_styp(AVIOContext *pb)
978 {
979     avio_wb32(pb, 24);
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");
985 }
986
987 static void find_index_range(AVFormatContext *s, const char *full_path,
988                              int64_t pos, int *index_length)
989 {
990     uint8_t buf[8];
991     AVIOContext *pb;
992     int ret;
993
994     ret = s->io_open(s, &pb, full_path, AVIO_FLAG_READ, NULL);
995     if (ret < 0)
996         return;
997     if (avio_seek(pb, pos, SEEK_SET) != pos) {
998         ff_format_io_close(s, &pb);
999         return;
1000     }
1001     ret = avio_read(pb, buf, 8);
1002     ff_format_io_close(s, &pb);
1003     if (ret < 8)
1004         return;
1005     if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
1006         return;
1007     *index_length = AV_RB32(&buf[0]);
1008 }
1009
1010 static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
1011                                    AVCodecParameters *par)
1012 {
1013     uint8_t *extradata;
1014
1015     if (os->ctx->streams[0]->codecpar->extradata_size || !par->extradata_size)
1016         return 0;
1017
1018     extradata = av_malloc(par->extradata_size);
1019
1020     if (!extradata)
1021         return AVERROR(ENOMEM);
1022
1023     memcpy(extradata, par->extradata, par->extradata_size);
1024
1025     os->ctx->streams[0]->codecpar->extradata = extradata;
1026     os->ctx->streams[0]->codecpar->extradata_size = par->extradata_size;
1027
1028     set_codec_str(s, par, os->codec_str, sizeof(os->codec_str));
1029
1030     return 0;
1031 }
1032
1033 static int dash_flush(AVFormatContext *s, int final, int stream)
1034 {
1035     DASHContext *c = s->priv_data;
1036     int i, ret = 0;
1037     int cur_flush_segment_index = 0;
1038     if (stream >= 0)
1039         cur_flush_segment_index = c->streams[stream].segment_index;
1040
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;
1045
1046         if (!os->packets_written)
1047             continue;
1048
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)
1054                 continue;
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)
1058                 continue;
1059         }
1060
1061         if (!os->init_range_length) {
1062             flush_init_segment(s, os);
1063         }
1064
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);
1070             if (ret < 0)
1071                 break;
1072             if (!strcmp(os->format_name, "mp4"))
1073                 write_styp(os->ctx->pb);
1074         } else {
1075             snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
1076         }
1077
1078         ret = flush_dynbuf(os, &range_length);
1079         if (ret < 0)
1080             break;
1081         os->packets_written = 0;
1082
1083         if (c->single_file) {
1084             find_index_range(s, full_path, os->pos, &index_length);
1085         } else {
1086             ff_format_io_close(s, &os->out);
1087             ret = ff_rename(temp_path, full_path);
1088             if (ret < 0)
1089                 break;
1090         }
1091
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);
1095             if (bitrate >= 0) {
1096                 os->bit_rate = bitrate;
1097                 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
1098                      " bandwidth=\"%d\"", os->bit_rate);
1099             }
1100         }
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);
1103
1104         os->pos += range_length;
1105     }
1106
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];
1110             int j;
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;
1114             if (remove > 0) {
1115                 for (j = 0; j < remove; j++) {
1116                     char filename[1024];
1117                     snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
1118                     unlink(filename);
1119                     av_free(os->segments[j]);
1120                 }
1121                 os->nb_segments -= remove;
1122                 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
1123             }
1124         }
1125     }
1126
1127     if (ret >= 0)
1128         ret = write_manifest(s, final);
1129     return ret;
1130 }
1131
1132 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
1133 {
1134     DASHContext *c = s->priv_data;
1135     AVStream *st = s->streams[pkt->stream_index];
1136     OutputStream *os = &c->streams[pkt->stream_index];
1137     int ret;
1138
1139     ret = update_stream_extradata(s, os, st->codecpar);
1140     if (ret < 0)
1141         return ret;
1142
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;
1152
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;
1158         pkt->dts  = 0;
1159     }
1160
1161     if (os->first_pts == AV_NOPTS_VALUE)
1162         os->first_pts = pkt->pts;
1163
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;
1169
1170         c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
1171                                         st->time_base,
1172                                         AV_TIME_BASE_Q);
1173         c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
1174                                          st->time_base,
1175                                          AV_TIME_BASE_Q);
1176
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");
1183             }
1184         }
1185
1186         if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
1187             return ret;
1188     }
1189
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;
1196         else
1197             os->start_pts = pkt->pts;
1198     }
1199     if (os->max_pts == AV_NOPTS_VALUE)
1200         os->max_pts = pkt->pts + pkt->duration;
1201     else
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);
1205 }
1206
1207 static int dash_write_trailer(AVFormatContext *s)
1208 {
1209     DASHContext *c = s->priv_data;
1210
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,
1218                                             AV_TIME_BASE_Q);
1219         c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
1220                                          s->streams[0]->time_base,
1221                                          AV_TIME_BASE_Q);
1222     }
1223     dash_flush(s, 1, -1);
1224
1225     if (c->remove_at_exit) {
1226         char filename[1024];
1227         int i;
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);
1231             unlink(filename);
1232         }
1233         unlink(s->filename);
1234     }
1235
1236     dash_free(s);
1237     return 0;
1238 }
1239
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 },
1255     { NULL },
1256 };
1257
1258 static const AVClass dash_class = {
1259     .class_name = "dash muxer",
1260     .item_name  = av_default_item_name,
1261     .option     = options,
1262     .version    = LIBAVUTIL_VERSION_INT,
1263 };
1264
1265 AVOutputFormat ff_dash_muxer = {
1266     .name           = "dash",
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,
1277 };