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