]> git.sesse.net Git - ffmpeg/blob - libavformat/dashenc.c
Merge commit '2ef87815fec059504370ae3050cc243a53553915'
[ffmpeg] / libavformat / dashenc.c
1 /*
2  * MPEG-DASH ISO BMFF segmenter
3  * Copyright (c) 2014 Martin Storsjo
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 #include "config.h"
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/rational.h"
33 #include "libavutil/time_internal.h"
34
35 #include "avc.h"
36 #include "avformat.h"
37 #include "avio_internal.h"
38 #include "internal.h"
39 #include "isom.h"
40 #include "os_support.h"
41 #include "url.h"
42
43 // See ISO/IEC 23009-1:2014 5.3.9.4.4
44 typedef enum {
45     DASH_TMPL_ID_UNDEFINED = -1,
46     DASH_TMPL_ID_ESCAPE,
47     DASH_TMPL_ID_REP_ID,
48     DASH_TMPL_ID_NUMBER,
49     DASH_TMPL_ID_BANDWIDTH,
50     DASH_TMPL_ID_TIME,
51 } DASHTmplId;
52
53 typedef struct Segment {
54     char file[1024];
55     int64_t start_pos;
56     int range_length, index_length;
57     int64_t time;
58     int duration;
59     int n;
60 } Segment;
61
62 typedef struct OutputStream {
63     AVFormatContext *ctx;
64     int ctx_inited;
65     uint8_t iobuf[32768];
66     AVIOContext *out;
67     int packets_written;
68     char initfile[1024];
69     int64_t init_start_pos;
70     int init_range_length;
71     int nb_segments, segments_size, segment_index;
72     Segment **segments;
73     int64_t first_pts, start_pts, max_pts;
74     int64_t last_dts;
75     int bit_rate;
76     char bandwidth_str[64];
77
78     char codec_str[100];
79 } OutputStream;
80
81 typedef struct DASHContext {
82     const AVClass *class;  /* Class for private options. */
83     int window_size;
84     int extra_window_size;
85     int min_seg_duration;
86     int remove_at_exit;
87     int use_template;
88     int use_timeline;
89     int single_file;
90     OutputStream *streams;
91     int has_video, has_audio;
92     int64_t last_duration;
93     int64_t total_duration;
94     char availability_start_time[100];
95     char dirname[1024];
96     const char *single_file_name;
97     const char *init_seg_name;
98     const char *media_seg_name;
99     AVRational min_frame_rate, max_frame_rate;
100     int ambiguous_frame_rate;
101 } DASHContext;
102
103 static int dash_write(void *opaque, uint8_t *buf, int buf_size)
104 {
105     OutputStream *os = opaque;
106     if (os->out)
107         avio_write(os->out, buf, buf_size);
108     return buf_size;
109 }
110
111 // RFC 6381
112 static void set_codec_str(AVFormatContext *s, AVCodecParameters *par,
113                           char *str, int size)
114 {
115     const AVCodecTag *tags[2] = { NULL, NULL };
116     uint32_t tag;
117     if (par->codec_type == AVMEDIA_TYPE_VIDEO)
118         tags[0] = ff_codec_movvideo_tags;
119     else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
120         tags[0] = ff_codec_movaudio_tags;
121     else
122         return;
123
124     tag = av_codec_get_tag(tags, par->codec_id);
125     if (!tag)
126         return;
127     if (size < 5)
128         return;
129
130     AV_WL32(str, tag);
131     str[4] = '\0';
132     if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
133         uint32_t oti;
134         tags[0] = ff_mp4_obj_type;
135         oti = av_codec_get_tag(tags, par->codec_id);
136         if (oti)
137             av_strlcatf(str, size, ".%02x", oti);
138         else
139             return;
140
141         if (tag == MKTAG('m', 'p', '4', 'a')) {
142             if (par->extradata_size >= 2) {
143                 int aot = par->extradata[0] >> 3;
144                 if (aot == 31)
145                     aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
146                 av_strlcatf(str, size, ".%d", aot);
147             }
148         } else if (tag == MKTAG('m', 'p', '4', 'v')) {
149             // Unimplemented, should output ProfileLevelIndication as a decimal number
150             av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
151         }
152     } else if (!strcmp(str, "avc1")) {
153         uint8_t *tmpbuf = NULL;
154         uint8_t *extradata = par->extradata;
155         int extradata_size = par->extradata_size;
156         if (!extradata_size)
157             return;
158         if (extradata[0] != 1) {
159             AVIOContext *pb;
160             if (avio_open_dyn_buf(&pb) < 0)
161                 return;
162             if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
163                 ffio_free_dyn_buf(&pb);
164                 return;
165             }
166             extradata_size = avio_close_dyn_buf(pb, &extradata);
167             tmpbuf = extradata;
168         }
169
170         if (extradata_size >= 4)
171             av_strlcatf(str, size, ".%02x%02x%02x",
172                         extradata[1], extradata[2], extradata[3]);
173         av_free(tmpbuf);
174     }
175 }
176
177 static void dash_free(AVFormatContext *s)
178 {
179     DASHContext *c = s->priv_data;
180     int i, j;
181     if (!c->streams)
182         return;
183     for (i = 0; i < s->nb_streams; i++) {
184         OutputStream *os = &c->streams[i];
185         if (os->ctx && os->ctx_inited)
186             av_write_trailer(os->ctx);
187         if (os->ctx && os->ctx->pb)
188             av_free(os->ctx->pb);
189         ff_format_io_close(s, &os->out);
190         if (os->ctx)
191             avformat_free_context(os->ctx);
192         for (j = 0; j < os->nb_segments; j++)
193             av_free(os->segments[j]);
194         av_free(os->segments);
195     }
196     av_freep(&c->streams);
197 }
198
199 static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
200 {
201     int i, start_index = 0, start_number = 1;
202     if (c->window_size) {
203         start_index  = FFMAX(os->nb_segments   - c->window_size, 0);
204         start_number = FFMAX(os->segment_index - c->window_size, 1);
205     }
206
207     if (c->use_template) {
208         int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE;
209         avio_printf(out, "\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
210         if (!c->use_timeline)
211             avio_printf(out, "duration=\"%"PRId64"\" ", c->last_duration);
212         avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\">\n", c->init_seg_name, c->media_seg_name, c->use_timeline ? start_number : 1);
213         if (c->use_timeline) {
214             int64_t cur_time = 0;
215             avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
216             for (i = start_index; i < os->nb_segments; ) {
217                 Segment *seg = os->segments[i];
218                 int repeat = 0;
219                 avio_printf(out, "\t\t\t\t\t\t<S ");
220                 if (i == start_index || seg->time != cur_time) {
221                     cur_time = seg->time;
222                     avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
223                 }
224                 avio_printf(out, "d=\"%d\" ", seg->duration);
225                 while (i + repeat + 1 < os->nb_segments &&
226                        os->segments[i + repeat + 1]->duration == seg->duration &&
227                        os->segments[i + repeat + 1]->time == os->segments[i + repeat]->time + os->segments[i + repeat]->duration)
228                     repeat++;
229                 if (repeat > 0)
230                     avio_printf(out, "r=\"%d\" ", repeat);
231                 avio_printf(out, "/>\n");
232                 i += 1 + repeat;
233                 cur_time += (1 + repeat) * seg->duration;
234             }
235             avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
236         }
237         avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
238     } else if (c->single_file) {
239         avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
240         avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
241         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);
242         for (i = start_index; i < os->nb_segments; i++) {
243             Segment *seg = os->segments[i];
244             avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
245             if (seg->index_length)
246                 avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
247             avio_printf(out, "/>\n");
248         }
249         avio_printf(out, "\t\t\t\t</SegmentList>\n");
250     } else {
251         avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
252         avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
253         for (i = start_index; i < os->nb_segments; i++) {
254             Segment *seg = os->segments[i];
255             avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
256         }
257         avio_printf(out, "\t\t\t\t</SegmentList>\n");
258     }
259 }
260
261 static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
262                                     size_t format_tag_size, const char **ptr) {
263     const char *next_ptr;
264     DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
265
266     if (av_strstart(identifier, "$$", &next_ptr)) {
267         id_type = DASH_TMPL_ID_ESCAPE;
268         *ptr = next_ptr;
269     } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
270         id_type = DASH_TMPL_ID_REP_ID;
271         // default to basic format, as $RepresentationID$ identifiers
272         // are not allowed to have custom format-tags.
273         av_strlcpy(format_tag, "%d", format_tag_size);
274         *ptr = next_ptr;
275     } else { // the following identifiers may have an explicit format_tag
276         if (av_strstart(identifier, "$Number", &next_ptr))
277             id_type = DASH_TMPL_ID_NUMBER;
278         else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
279             id_type = DASH_TMPL_ID_BANDWIDTH;
280         else if (av_strstart(identifier, "$Time", &next_ptr))
281             id_type = DASH_TMPL_ID_TIME;
282         else
283             id_type = DASH_TMPL_ID_UNDEFINED;
284
285         // next parse the dash format-tag and generate a c-string format tag
286         // (next_ptr now points at the first '%' at the beginning of the format-tag)
287         if (id_type != DASH_TMPL_ID_UNDEFINED) {
288             const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
289             if (next_ptr[0] == '$') { // no dash format-tag
290                 snprintf(format_tag, format_tag_size, "%%%s", number_format);
291                 *ptr = &next_ptr[1];
292             } else {
293                 const char *width_ptr;
294                 // only tolerate single-digit width-field (i.e. up to 9-digit width)
295                 if (av_strstart(next_ptr, "%0", &width_ptr) &&
296                     av_isdigit(width_ptr[0]) &&
297                     av_strstart(&width_ptr[1], "d$", &next_ptr)) {
298                     // yes, we're using a format tag to build format_tag.
299                     snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
300                     *ptr = next_ptr;
301                 } else {
302                     av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
303                                                  "closing '$' character or a format-string like '%%0[width]d', "
304                                                  "where width must be a single digit\n", next_ptr);
305                     id_type = DASH_TMPL_ID_UNDEFINED;
306                 }
307             }
308         }
309     }
310     return id_type;
311 }
312
313 static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
314                                   const char *template, int rep_id,
315                                   int number, int bit_rate,
316                                   int64_t time) {
317     int dst_pos = 0;
318     const char *t_cur = template;
319     while (dst_pos < buffer_size - 1 && *t_cur) {
320         char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
321         int n = 0;
322         DASHTmplId id_type;
323         const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
324         if (t_next) {
325             int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
326             av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
327             // advance
328             dst_pos += num_copy_bytes;
329             t_cur = t_next;
330         } else { // no more DASH identifiers to substitute - just copy the rest over and break
331             av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
332             break;
333         }
334
335         if (dst_pos >= buffer_size - 1 || !*t_cur)
336             break;
337
338         // t_cur is now pointing to a '$' character
339         id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
340         switch (id_type) {
341         case DASH_TMPL_ID_ESCAPE:
342             av_strlcpy(&dst[dst_pos], "$", 2);
343             n = 1;
344             break;
345         case DASH_TMPL_ID_REP_ID:
346             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
347             break;
348         case DASH_TMPL_ID_NUMBER:
349             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
350             break;
351         case DASH_TMPL_ID_BANDWIDTH:
352             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
353             break;
354         case DASH_TMPL_ID_TIME:
355             n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
356             break;
357         case DASH_TMPL_ID_UNDEFINED:
358             // copy over one byte and advance
359             av_strlcpy(&dst[dst_pos], t_cur, 2);
360             n = 1;
361             t_next = &t_cur[1];
362             break;
363         }
364         // t_next points just past the processed identifier
365         // n is the number of bytes that were attempted to be written to dst
366         // (may have failed to write all because buffer_size).
367
368         // advance
369         dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
370         t_cur = t_next;
371     }
372 }
373
374 static char *xmlescape(const char *str) {
375     int outlen = strlen(str)*3/2 + 6;
376     char *out = av_realloc(NULL, outlen + 1);
377     int pos = 0;
378     if (!out)
379         return NULL;
380     for (; *str; str++) {
381         if (pos + 6 > outlen) {
382             char *tmp;
383             outlen = 2 * outlen + 6;
384             tmp = av_realloc(out, outlen + 1);
385             if (!tmp) {
386                 av_free(out);
387                 return NULL;
388             }
389             out = tmp;
390         }
391         if (*str == '&') {
392             memcpy(&out[pos], "&amp;", 5);
393             pos += 5;
394         } else if (*str == '<') {
395             memcpy(&out[pos], "&lt;", 4);
396             pos += 4;
397         } else if (*str == '>') {
398             memcpy(&out[pos], "&gt;", 4);
399             pos += 4;
400         } else if (*str == '\'') {
401             memcpy(&out[pos], "&apos;", 6);
402             pos += 6;
403         } else if (*str == '\"') {
404             memcpy(&out[pos], "&quot;", 6);
405             pos += 6;
406         } else {
407             out[pos++] = *str;
408         }
409     }
410     out[pos] = '\0';
411     return out;
412 }
413
414 static void write_time(AVIOContext *out, int64_t time)
415 {
416     int seconds = time / AV_TIME_BASE;
417     int fractions = time % AV_TIME_BASE;
418     int minutes = seconds / 60;
419     int hours = minutes / 60;
420     seconds %= 60;
421     minutes %= 60;
422     avio_printf(out, "PT");
423     if (hours)
424         avio_printf(out, "%dH", hours);
425     if (hours || minutes)
426         avio_printf(out, "%dM", minutes);
427     avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
428 }
429
430 static void format_date_now(char *buf, int size)
431 {
432     time_t t = time(NULL);
433     struct tm *ptm, tmbuf;
434     ptm = gmtime_r(&t, &tmbuf);
435     if (ptm) {
436         if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%S", ptm))
437             buf[0] = '\0';
438     }
439 }
440
441 static int write_manifest(AVFormatContext *s, int final)
442 {
443     DASHContext *c = s->priv_data;
444     AVIOContext *out;
445     char temp_filename[1024];
446     int ret, i;
447     AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
448
449     snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
450     ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
451     if (ret < 0) {
452         av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
453         return ret;
454     }
455     avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
456     avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
457                 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
458                 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
459                 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
460                 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
461                 "\ttype=\"%s\"\n", final ? "static" : "dynamic");
462     if (final) {
463         avio_printf(out, "\tmediaPresentationDuration=\"");
464         write_time(out, c->total_duration);
465         avio_printf(out, "\"\n");
466     } else {
467         int64_t update_period = c->last_duration / AV_TIME_BASE;
468         char now_str[100];
469         if (c->use_template && !c->use_timeline)
470             update_period = 500;
471         avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
472         avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
473         if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
474             format_date_now(c->availability_start_time, sizeof(c->availability_start_time));
475         }
476         if (c->availability_start_time[0])
477             avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
478         format_date_now(now_str, sizeof(now_str));
479         if (now_str[0])
480             avio_printf(out, "\tpublishTime=\"%s\"\n", now_str);
481         if (c->window_size && c->use_template) {
482             avio_printf(out, "\ttimeShiftBufferDepth=\"");
483             write_time(out, c->last_duration * c->window_size);
484             avio_printf(out, "\"\n");
485         }
486     }
487     avio_printf(out, "\tminBufferTime=\"");
488     write_time(out, c->last_duration);
489     avio_printf(out, "\">\n");
490     avio_printf(out, "\t<ProgramInformation>\n");
491     if (title) {
492         char *escaped = xmlescape(title->value);
493         avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
494         av_free(escaped);
495     }
496     avio_printf(out, "\t</ProgramInformation>\n");
497     if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
498         OutputStream *os = &c->streams[0];
499         int start_index = FFMAX(os->nb_segments - c->window_size, 0);
500         int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
501         avio_printf(out, "\t<Period start=\"");
502         write_time(out, start_time);
503         avio_printf(out, "\">\n");
504     } else {
505         avio_printf(out, "\t<Period start=\"PT0.0S\">\n");
506     }
507
508     if (c->has_video) {
509         avio_printf(out, "\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\"");
510         if (c->max_frame_rate.num && !c->ambiguous_frame_rate)
511             avio_printf(out, " %s=\"%d/%d\"", (av_cmp_q(c->min_frame_rate, c->max_frame_rate) < 0) ? "maxFrameRate" : "frameRate", c->max_frame_rate.num, c->max_frame_rate.den);
512         avio_printf(out, ">\n");
513
514         for (i = 0; i < s->nb_streams; i++) {
515             AVStream *st = s->streams[i];
516             OutputStream *os = &c->streams[i];
517
518             if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
519                 continue;
520
521             avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"", i, os->codec_str, os->bandwidth_str, st->codecpar->width, st->codecpar->height);
522             if (st->avg_frame_rate.num)
523                 avio_printf(out, " frameRate=\"%d/%d\"", st->avg_frame_rate.num, st->avg_frame_rate.den);
524             avio_printf(out, ">\n");
525
526             output_segment_list(&c->streams[i], out, c);
527             avio_printf(out, "\t\t\t</Representation>\n");
528         }
529         avio_printf(out, "\t\t</AdaptationSet>\n");
530     }
531     if (c->has_audio) {
532         avio_printf(out, "\t\t<AdaptationSet contentType=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
533         for (i = 0; i < s->nb_streams; i++) {
534             AVStream *st = s->streams[i];
535             OutputStream *os = &c->streams[i];
536
537             if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
538                 continue;
539
540             avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/mp4\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n", i, os->codec_str, os->bandwidth_str, st->codecpar->sample_rate);
541             avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codecpar->channels);
542             output_segment_list(&c->streams[i], out, c);
543             avio_printf(out, "\t\t\t</Representation>\n");
544         }
545         avio_printf(out, "\t\t</AdaptationSet>\n");
546     }
547     avio_printf(out, "\t</Period>\n");
548     avio_printf(out, "</MPD>\n");
549     avio_flush(out);
550     ff_format_io_close(s, &out);
551     return avpriv_io_move(temp_filename, s->filename);
552 }
553
554 static int dash_init(AVFormatContext *s)
555 {
556     DASHContext *c = s->priv_data;
557     int ret = 0, i;
558     AVOutputFormat *oformat;
559     char *ptr;
560     char basename[1024];
561
562     if (c->single_file_name)
563         c->single_file = 1;
564     if (c->single_file)
565         c->use_template = 0;
566     c->ambiguous_frame_rate = 0;
567
568     av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
569     ptr = strrchr(c->dirname, '/');
570     if (ptr) {
571         av_strlcpy(basename, &ptr[1], sizeof(basename));
572         ptr[1] = '\0';
573     } else {
574         c->dirname[0] = '\0';
575         av_strlcpy(basename, s->filename, sizeof(basename));
576     }
577
578     ptr = strrchr(basename, '.');
579     if (ptr)
580         *ptr = '\0';
581
582     oformat = av_guess_format("mp4", NULL, NULL);
583     if (!oformat)
584         return AVERROR_MUXER_NOT_FOUND;
585
586     c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
587     if (!c->streams)
588         return AVERROR(ENOMEM);
589
590     for (i = 0; i < s->nb_streams; i++) {
591         OutputStream *os = &c->streams[i];
592         AVFormatContext *ctx;
593         AVStream *st;
594         AVDictionary *opts = NULL;
595         char filename[1024];
596
597         os->bit_rate = s->streams[i]->codecpar->bit_rate;
598         if (os->bit_rate) {
599             snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
600                      " bandwidth=\"%d\"", os->bit_rate);
601         } else {
602             int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
603                         AV_LOG_ERROR : AV_LOG_WARNING;
604             av_log(s, level, "No bit rate set for stream %d\n", i);
605             if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT)
606                 return AVERROR(EINVAL);
607         }
608
609         ctx = avformat_alloc_context();
610         if (!ctx)
611             return AVERROR(ENOMEM);
612         os->ctx = ctx;
613         ctx->oformat = oformat;
614         ctx->interrupt_callback = s->interrupt_callback;
615         ctx->opaque             = s->opaque;
616         ctx->io_close           = s->io_close;
617         ctx->io_open            = s->io_open;
618
619         if (!(st = avformat_new_stream(ctx, NULL)))
620             return AVERROR(ENOMEM);
621         avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
622         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
623         st->time_base = s->streams[i]->time_base;
624         ctx->avoid_negative_ts = s->avoid_negative_ts;
625         ctx->flags = s->flags;
626
627         ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
628         if (!ctx->pb)
629             return AVERROR(ENOMEM);
630
631         if (c->single_file) {
632             if (c->single_file_name)
633                 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
634             else
635                 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
636         } else {
637             dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
638         }
639         snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
640         ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL);
641         if (ret < 0)
642             return ret;
643         os->init_start_pos = 0;
644
645         av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
646         if ((ret = avformat_init_output(ctx, &opts)) < 0)
647             return ret;
648         os->ctx_inited = 1;
649         avio_flush(ctx->pb);
650         av_dict_free(&opts);
651
652         av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
653
654         s->streams[i]->time_base = st->time_base;
655         // If the muxer wants to shift timestamps, request to have them shifted
656         // already before being handed to this muxer, so we don't have mismatches
657         // between the MPD and the actual segments.
658         s->avoid_negative_ts = ctx->avoid_negative_ts;
659         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
660             AVRational avg_frame_rate = s->streams[i]->avg_frame_rate;
661             if (avg_frame_rate.num > 0) {
662                 if (av_cmp_q(avg_frame_rate, c->min_frame_rate) < 0)
663                     c->min_frame_rate = avg_frame_rate;
664                 if (av_cmp_q(c->max_frame_rate, avg_frame_rate) < 0)
665                     c->max_frame_rate = avg_frame_rate;
666             } else {
667                 c->ambiguous_frame_rate = 1;
668             }
669             c->has_video = 1;
670         } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
671             c->has_audio = 1;
672         }
673
674         set_codec_str(s, st->codecpar, os->codec_str, sizeof(os->codec_str));
675         os->first_pts = AV_NOPTS_VALUE;
676         os->max_pts = AV_NOPTS_VALUE;
677         os->last_dts = AV_NOPTS_VALUE;
678         os->segment_index = 1;
679     }
680
681     if (!c->has_video && c->min_seg_duration <= 0) {
682         av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
683         return AVERROR(EINVAL);
684     }
685     return 0;
686 }
687
688 static int dash_write_header(AVFormatContext *s)
689 {
690     DASHContext *c = s->priv_data;
691     int i, ret;
692     for (i = 0; i < s->nb_streams; i++) {
693         OutputStream *os = &c->streams[i];
694         if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
695             dash_free(s);
696             return ret;
697         }
698     }
699     ret = write_manifest(s, 0);
700     if (!ret)
701         av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
702     return ret;
703 }
704
705 static int add_segment(OutputStream *os, const char *file,
706                        int64_t time, int duration,
707                        int64_t start_pos, int64_t range_length,
708                        int64_t index_length)
709 {
710     int err;
711     Segment *seg;
712     if (os->nb_segments >= os->segments_size) {
713         os->segments_size = (os->segments_size + 1) * 2;
714         if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
715                                os->segments_size)) < 0) {
716             os->segments_size = 0;
717             os->nb_segments = 0;
718             return err;
719         }
720     }
721     seg = av_mallocz(sizeof(*seg));
722     if (!seg)
723         return AVERROR(ENOMEM);
724     av_strlcpy(seg->file, file, sizeof(seg->file));
725     seg->time = time;
726     seg->duration = duration;
727     if (seg->time < 0) { // If pts<0, it is expected to be cut away with an edit list
728         seg->duration += seg->time;
729         seg->time = 0;
730     }
731     seg->start_pos = start_pos;
732     seg->range_length = range_length;
733     seg->index_length = index_length;
734     os->segments[os->nb_segments++] = seg;
735     os->segment_index++;
736     return 0;
737 }
738
739 static void write_styp(AVIOContext *pb)
740 {
741     avio_wb32(pb, 24);
742     ffio_wfourcc(pb, "styp");
743     ffio_wfourcc(pb, "msdh");
744     avio_wb32(pb, 0); /* minor */
745     ffio_wfourcc(pb, "msdh");
746     ffio_wfourcc(pb, "msix");
747 }
748
749 static void find_index_range(AVFormatContext *s, const char *full_path,
750                              int64_t pos, int *index_length)
751 {
752     uint8_t buf[8];
753     AVIOContext *pb;
754     int ret;
755
756     ret = s->io_open(s, &pb, full_path, AVIO_FLAG_READ, NULL);
757     if (ret < 0)
758         return;
759     if (avio_seek(pb, pos, SEEK_SET) != pos) {
760         ff_format_io_close(s, &pb);
761         return;
762     }
763     ret = avio_read(pb, buf, 8);
764     ff_format_io_close(s, &pb);
765     if (ret < 8)
766         return;
767     if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
768         return;
769     *index_length = AV_RB32(&buf[0]);
770 }
771
772 static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
773                                    AVCodecParameters *par)
774 {
775     uint8_t *extradata;
776
777     if (os->ctx->streams[0]->codecpar->extradata_size || !par->extradata_size)
778         return 0;
779
780     extradata = av_malloc(par->extradata_size);
781
782     if (!extradata)
783         return AVERROR(ENOMEM);
784
785     memcpy(extradata, par->extradata, par->extradata_size);
786
787     os->ctx->streams[0]->codecpar->extradata = extradata;
788     os->ctx->streams[0]->codecpar->extradata_size = par->extradata_size;
789
790     set_codec_str(s, par, os->codec_str, sizeof(os->codec_str));
791
792     return 0;
793 }
794
795 static int dash_flush(AVFormatContext *s, int final, int stream)
796 {
797     DASHContext *c = s->priv_data;
798     int i, ret = 0;
799     int cur_flush_segment_index = 0;
800     if (stream >= 0)
801         cur_flush_segment_index = c->streams[stream].segment_index;
802
803     for (i = 0; i < s->nb_streams; i++) {
804         OutputStream *os = &c->streams[i];
805         char filename[1024] = "", full_path[1024], temp_path[1024];
806         int64_t start_pos;
807         int range_length, index_length = 0;
808
809         if (!os->packets_written)
810             continue;
811
812         // Flush the single stream that got a keyframe right now.
813         // Flush all audio streams as well, in sync with video keyframes,
814         // but not the other video streams.
815         if (stream >= 0 && i != stream) {
816             if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
817                 continue;
818             // Make sure we don't flush audio streams multiple times, when
819             // all video streams are flushed one at a time.
820             if (c->has_video && os->segment_index > cur_flush_segment_index)
821                 continue;
822         }
823
824         if (!os->init_range_length) {
825             av_write_frame(os->ctx, NULL);
826             os->init_range_length = avio_tell(os->ctx->pb);
827             if (!c->single_file)
828                 ff_format_io_close(s, &os->out);
829         }
830
831         start_pos = avio_tell(os->ctx->pb);
832
833         if (!c->single_file) {
834             dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
835             snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
836             snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
837             ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL);
838             if (ret < 0)
839                 break;
840             write_styp(os->ctx->pb);
841         } else {
842             snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
843         }
844
845         av_write_frame(os->ctx, NULL);
846         avio_flush(os->ctx->pb);
847         os->packets_written = 0;
848
849         range_length = avio_tell(os->ctx->pb) - start_pos;
850         if (c->single_file) {
851             find_index_range(s, full_path, start_pos, &index_length);
852         } else {
853             ff_format_io_close(s, &os->out);
854             ret = avpriv_io_move(temp_path, full_path);
855             if (ret < 0)
856                 break;
857         }
858         add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length);
859         av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
860     }
861
862     if (c->window_size || (final && c->remove_at_exit)) {
863         for (i = 0; i < s->nb_streams; i++) {
864             OutputStream *os = &c->streams[i];
865             int j;
866             int remove = os->nb_segments - c->window_size - c->extra_window_size;
867             if (final && c->remove_at_exit)
868                 remove = os->nb_segments;
869             if (remove > 0) {
870                 for (j = 0; j < remove; j++) {
871                     char filename[1024];
872                     snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
873                     unlink(filename);
874                     av_free(os->segments[j]);
875                 }
876                 os->nb_segments -= remove;
877                 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
878             }
879         }
880     }
881
882     if (ret >= 0)
883         ret = write_manifest(s, final);
884     return ret;
885 }
886
887 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
888 {
889     DASHContext *c = s->priv_data;
890     AVStream *st = s->streams[pkt->stream_index];
891     OutputStream *os = &c->streams[pkt->stream_index];
892     int64_t seg_end_duration = (os->segment_index) * (int64_t) c->min_seg_duration;
893     int ret;
894
895     ret = update_stream_extradata(s, os, st->codecpar);
896     if (ret < 0)
897         return ret;
898
899     // Fill in a heuristic guess of the packet duration, if none is available.
900     // The mp4 muxer will do something similar (for the last packet in a fragment)
901     // if nothing is set (setting it for the other packets doesn't hurt).
902     // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
903     // invoke its heuristic (this doesn't have to be identical to that algorithm),
904     // so that we know the exact timestamps of fragments.
905     if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
906         pkt->duration = pkt->dts - os->last_dts;
907     os->last_dts = pkt->dts;
908
909     // If forcing the stream to start at 0, the mp4 muxer will set the start
910     // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
911     if (os->first_pts == AV_NOPTS_VALUE &&
912         s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
913         pkt->pts -= pkt->dts;
914         pkt->dts  = 0;
915     }
916
917     if (os->first_pts == AV_NOPTS_VALUE)
918         os->first_pts = pkt->pts;
919
920     if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
921         pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
922         av_compare_ts(pkt->pts - os->first_pts, st->time_base,
923                       seg_end_duration, AV_TIME_BASE_Q) >= 0) {
924         int64_t prev_duration = c->last_duration;
925
926         c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
927                                         st->time_base,
928                                         AV_TIME_BASE_Q);
929         c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
930                                          st->time_base,
931                                          AV_TIME_BASE_Q);
932
933         if ((!c->use_timeline || !c->use_template) && prev_duration) {
934             if (c->last_duration < prev_duration*9/10 ||
935                 c->last_duration > prev_duration*11/10) {
936                 av_log(s, AV_LOG_WARNING,
937                        "Segment durations differ too much, enable use_timeline "
938                        "and use_template, or keep a stricter keyframe interval\n");
939             }
940         }
941
942         if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
943             return ret;
944     }
945
946     if (!os->packets_written) {
947         // If we wrote a previous segment, adjust the start time of the segment
948         // to the end of the previous one (which is the same as the mp4 muxer
949         // does). This avoids gaps in the timeline.
950         if (os->max_pts != AV_NOPTS_VALUE)
951             os->start_pts = os->max_pts;
952         else
953             os->start_pts = pkt->pts;
954     }
955     if (os->max_pts == AV_NOPTS_VALUE)
956         os->max_pts = pkt->pts + pkt->duration;
957     else
958         os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
959     os->packets_written++;
960     return ff_write_chained(os->ctx, 0, pkt, s, 0);
961 }
962
963 static int dash_write_trailer(AVFormatContext *s)
964 {
965     DASHContext *c = s->priv_data;
966
967     if (s->nb_streams > 0) {
968         OutputStream *os = &c->streams[0];
969         // If no segments have been written so far, try to do a crude
970         // guess of the segment duration
971         if (!c->last_duration)
972             c->last_duration = av_rescale_q(os->max_pts - os->start_pts,
973                                             s->streams[0]->time_base,
974                                             AV_TIME_BASE_Q);
975         c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
976                                          s->streams[0]->time_base,
977                                          AV_TIME_BASE_Q);
978     }
979     dash_flush(s, 1, -1);
980
981     if (c->remove_at_exit) {
982         char filename[1024];
983         int i;
984         for (i = 0; i < s->nb_streams; i++) {
985             OutputStream *os = &c->streams[i];
986             snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
987             unlink(filename);
988         }
989         unlink(s->filename);
990     }
991
992     return 0;
993 }
994
995 static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt)
996 {
997     DASHContext *c = s->priv_data;
998     OutputStream *os = &c->streams[avpkt->stream_index];
999     AVFormatContext *oc = os->ctx;
1000     if (oc->oformat->check_bitstream) {
1001         int ret;
1002         AVPacket pkt = *avpkt;
1003         pkt.stream_index = 0;
1004         ret = oc->oformat->check_bitstream(oc, &pkt);
1005         if (ret == 1) {
1006             AVStream *st = s->streams[avpkt->stream_index];
1007             AVStream *ost = oc->streams[0];
1008             st->internal->bsfcs = ost->internal->bsfcs;
1009             st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
1010             ost->internal->bsfcs = NULL;
1011             ost->internal->nb_bsfcs = 0;
1012         }
1013         return ret;
1014     }
1015     return 1;
1016 }
1017
1018 #define OFFSET(x) offsetof(DASHContext, x)
1019 #define E AV_OPT_FLAG_ENCODING_PARAM
1020 static const AVOption options[] = {
1021     { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
1022     { "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 },
1023     { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
1024     { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
1025     { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
1026     { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
1027     { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
1028     { "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 },
1029     { "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 },
1030     { "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 },
1031     { NULL },
1032 };
1033
1034 static const AVClass dash_class = {
1035     .class_name = "dash muxer",
1036     .item_name  = av_default_item_name,
1037     .option     = options,
1038     .version    = LIBAVUTIL_VERSION_INT,
1039 };
1040
1041 AVOutputFormat ff_dash_muxer = {
1042     .name           = "dash",
1043     .long_name      = NULL_IF_CONFIG_SMALL("DASH Muxer"),
1044     .priv_data_size = sizeof(DASHContext),
1045     .audio_codec    = AV_CODEC_ID_AAC,
1046     .video_codec    = AV_CODEC_ID_H264,
1047     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
1048     .init           = dash_init,
1049     .write_header   = dash_write_header,
1050     .write_packet   = dash_write_packet,
1051     .write_trailer  = dash_write_trailer,
1052     .deinit         = dash_free,
1053     .codec_tag      = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1054     .check_bitstream = dash_check_bitstream,
1055     .priv_class     = &dash_class,
1056 };