2 * MPEG-DASH ISO BMFF segmenter
3 * Copyright (c) 2014 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
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"
35 #include "avio_internal.h"
38 #include "os_support.h"
41 // See ISO/IEC 23009-1:2014 5.3.9.4.4
43 DASH_TMPL_ID_UNDEFINED = -1,
47 DASH_TMPL_ID_BANDWIDTH,
51 typedef struct Segment {
54 int range_length, index_length;
60 typedef struct OutputStream {
67 int64_t init_start_pos;
68 int init_range_length;
69 int nb_segments, segments_size, segment_index;
71 int64_t first_pts, start_pts, max_pts;
74 char bandwidth_str[64];
79 typedef struct DASHContext {
80 const AVClass *class; /* Class for private options. */
82 int extra_window_size;
88 OutputStream *streams;
89 int has_video, has_audio;
90 int64_t last_duration;
91 int64_t total_duration;
92 char availability_start_time[100];
94 const char *single_file_name;
95 const char *init_seg_name;
96 const char *media_seg_name;
99 static int dash_write(void *opaque, uint8_t *buf, int buf_size)
101 OutputStream *os = opaque;
103 ffurl_write(os->out, buf, buf_size);
108 static void set_codec_str(AVFormatContext *s, AVCodecContext *codec,
111 const AVCodecTag *tags[2] = { NULL, NULL };
113 if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
114 tags[0] = ff_codec_movvideo_tags;
115 else if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
116 tags[0] = ff_codec_movaudio_tags;
120 tag = av_codec_get_tag(tags, codec->codec_id);
128 if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
130 tags[0] = ff_mp4_obj_type;
131 oti = av_codec_get_tag(tags, codec->codec_id);
133 av_strlcatf(str, size, ".%02x", oti);
137 if (tag == MKTAG('m', 'p', '4', 'a')) {
138 if (codec->extradata_size >= 2) {
139 int aot = codec->extradata[0] >> 3;
141 aot = ((AV_RB16(codec->extradata) >> 5) & 0x3f) + 32;
142 av_strlcatf(str, size, ".%d", aot);
144 } else if (tag == MKTAG('m', 'p', '4', 'v')) {
145 // Unimplemented, should output ProfileLevelIndication as a decimal number
146 av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
148 } else if (!strcmp(str, "avc1")) {
149 uint8_t *tmpbuf = NULL;
150 uint8_t *extradata = codec->extradata;
151 int extradata_size = codec->extradata_size;
154 if (extradata[0] != 1) {
156 if (avio_open_dyn_buf(&pb) < 0)
158 if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
159 ffio_free_dyn_buf(&pb);
162 extradata_size = avio_close_dyn_buf(pb, &extradata);
166 if (extradata_size >= 4)
167 av_strlcatf(str, size, ".%02x%02x%02x",
168 extradata[1], extradata[2], extradata[3]);
173 static void dash_free(AVFormatContext *s)
175 DASHContext *c = s->priv_data;
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);
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);
193 av_freep(&c->streams);
196 static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
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);
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=\"%"PRId64"\" ", 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 int64_t cur_time = 0;
212 avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
213 for (i = start_index; i < os->nb_segments; ) {
214 Segment *seg = os->segments[i];
216 avio_printf(out, "\t\t\t\t\t\t<S ");
217 if (i == start_index || seg->time != cur_time) {
218 cur_time = seg->time;
219 avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
221 avio_printf(out, "d=\"%d\" ", seg->duration);
222 while (i + repeat + 1 < os->nb_segments &&
223 os->segments[i + repeat + 1]->duration == seg->duration &&
224 os->segments[i + repeat + 1]->time == os->segments[i + repeat]->time + os->segments[i + repeat]->duration)
227 avio_printf(out, "r=\"%d\" ", repeat);
228 avio_printf(out, "/>\n");
230 cur_time += (1 + repeat) * seg->duration;
232 avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
234 avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
235 } else if (c->single_file) {
236 avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
237 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
238 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);
239 for (i = start_index; i < os->nb_segments; i++) {
240 Segment *seg = os->segments[i];
241 avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
242 if (seg->index_length)
243 avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
244 avio_printf(out, "/>\n");
246 avio_printf(out, "\t\t\t\t</SegmentList>\n");
248 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
249 avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
250 for (i = start_index; i < os->nb_segments; i++) {
251 Segment *seg = os->segments[i];
252 avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
254 avio_printf(out, "\t\t\t\t</SegmentList>\n");
258 static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
259 size_t format_tag_size, const char **ptr) {
260 const char *next_ptr;
261 DASHTmplId id_type = DASH_TMPL_ID_UNDEFINED;
263 if (av_strstart(identifier, "$$", &next_ptr)) {
264 id_type = DASH_TMPL_ID_ESCAPE;
266 } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
267 id_type = DASH_TMPL_ID_REP_ID;
268 // default to basic format, as $RepresentationID$ identifiers
269 // are not allowed to have custom format-tags.
270 av_strlcpy(format_tag, "%d", format_tag_size);
272 } else { // the following identifiers may have an explicit format_tag
273 if (av_strstart(identifier, "$Number", &next_ptr))
274 id_type = DASH_TMPL_ID_NUMBER;
275 else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
276 id_type = DASH_TMPL_ID_BANDWIDTH;
277 else if (av_strstart(identifier, "$Time", &next_ptr))
278 id_type = DASH_TMPL_ID_TIME;
280 id_type = DASH_TMPL_ID_UNDEFINED;
282 // next parse the dash format-tag and generate a c-string format tag
283 // (next_ptr now points at the first '%' at the beginning of the format-tag)
284 if (id_type != DASH_TMPL_ID_UNDEFINED) {
285 const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
286 if (next_ptr[0] == '$') { // no dash format-tag
287 snprintf(format_tag, format_tag_size, "%%%s", number_format);
290 const char *width_ptr;
291 // only tolerate single-digit width-field (i.e. up to 9-digit width)
292 if (av_strstart(next_ptr, "%0", &width_ptr) &&
293 av_isdigit(width_ptr[0]) &&
294 av_strstart(&width_ptr[1], "d$", &next_ptr)) {
295 // yes, we're using a format tag to build format_tag.
296 snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
299 av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
300 "closing '$' character or a format-string like '%%0[width]d', "
301 "where width must be a single digit\n", next_ptr);
302 id_type = DASH_TMPL_ID_UNDEFINED;
310 static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
311 const char *template, int rep_id,
312 int number, int bit_rate,
315 const char *t_cur = template;
316 while (dst_pos < buffer_size - 1 && *t_cur) {
317 char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
320 const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
322 int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
323 av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
325 dst_pos += num_copy_bytes;
327 } else { // no more DASH identifiers to substitute - just copy the rest over and break
328 av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
332 if (dst_pos >= buffer_size - 1 || !*t_cur)
335 // t_cur is now pointing to a '$' character
336 id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
338 case DASH_TMPL_ID_ESCAPE:
339 av_strlcpy(&dst[dst_pos], "$", 2);
342 case DASH_TMPL_ID_REP_ID:
343 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
345 case DASH_TMPL_ID_NUMBER:
346 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
348 case DASH_TMPL_ID_BANDWIDTH:
349 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
351 case DASH_TMPL_ID_TIME:
352 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
354 case DASH_TMPL_ID_UNDEFINED:
355 // copy over one byte and advance
356 av_strlcpy(&dst[dst_pos], t_cur, 2);
361 // t_next points just past the processed identifier
362 // n is the number of bytes that were attempted to be written to dst
363 // (may have failed to write all because buffer_size).
366 dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
371 static char *xmlescape(const char *str) {
372 int outlen = strlen(str)*3/2 + 6;
373 char *out = av_realloc(NULL, outlen + 1);
377 for (; *str; str++) {
378 if (pos + 6 > outlen) {
380 outlen = 2 * outlen + 6;
381 tmp = av_realloc(out, outlen + 1);
389 memcpy(&out[pos], "&", 5);
391 } else if (*str == '<') {
392 memcpy(&out[pos], "<", 4);
394 } else if (*str == '>') {
395 memcpy(&out[pos], ">", 4);
397 } else if (*str == '\'') {
398 memcpy(&out[pos], "'", 6);
400 } else if (*str == '\"') {
401 memcpy(&out[pos], """, 6);
411 static void write_time(AVIOContext *out, int64_t time)
413 int seconds = time / AV_TIME_BASE;
414 int fractions = time % AV_TIME_BASE;
415 int minutes = seconds / 60;
416 int hours = minutes / 60;
419 avio_printf(out, "PT");
421 avio_printf(out, "%dH", hours);
422 if (hours || minutes)
423 avio_printf(out, "%dM", minutes);
424 avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
427 static int write_manifest(AVFormatContext *s, int final)
429 DASHContext *c = s->priv_data;
431 char temp_filename[1024];
433 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
435 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
436 ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
438 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
441 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
442 avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
443 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
444 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
445 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
446 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
447 "\ttype=\"%s\"\n", final ? "static" : "dynamic");
449 avio_printf(out, "\tmediaPresentationDuration=\"");
450 write_time(out, c->total_duration);
451 avio_printf(out, "\"\n");
453 int64_t update_period = c->last_duration / AV_TIME_BASE;
454 if (c->use_template && !c->use_timeline)
456 avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
457 avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
458 if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
459 time_t t = time(NULL);
460 struct tm *ptm, tmbuf;
461 ptm = gmtime_r(&t, &tmbuf);
463 if (!strftime(c->availability_start_time, sizeof(c->availability_start_time),
464 "%Y-%m-%dT%H:%M:%S", ptm))
465 c->availability_start_time[0] = '\0';
468 if (c->availability_start_time[0])
469 avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
470 if (c->window_size && c->use_template) {
471 avio_printf(out, "\ttimeShiftBufferDepth=\"");
472 write_time(out, c->last_duration * c->window_size);
473 avio_printf(out, "\"\n");
476 avio_printf(out, "\tminBufferTime=\"");
477 write_time(out, c->last_duration);
478 avio_printf(out, "\">\n");
479 avio_printf(out, "\t<ProgramInformation>\n");
481 char *escaped = xmlescape(title->value);
482 avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
485 avio_printf(out, "\t</ProgramInformation>\n");
486 if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
487 OutputStream *os = &c->streams[0];
488 int start_index = FFMAX(os->nb_segments - c->window_size, 0);
489 int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
490 avio_printf(out, "\t<Period start=\"");
491 write_time(out, start_time);
492 avio_printf(out, "\">\n");
494 avio_printf(out, "\t<Period start=\"PT0.0S\">\n");
498 avio_printf(out, "\t\t<AdaptationSet id=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
499 for (i = 0; i < s->nb_streams; i++) {
500 AVStream *st = s->streams[i];
501 OutputStream *os = &c->streams[i];
503 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO)
506 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);
507 output_segment_list(&c->streams[i], out, c);
508 avio_printf(out, "\t\t\t</Representation>\n");
510 avio_printf(out, "\t\t</AdaptationSet>\n");
513 avio_printf(out, "\t\t<AdaptationSet id=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
514 for (i = 0; i < s->nb_streams; i++) {
515 AVStream *st = s->streams[i];
516 OutputStream *os = &c->streams[i];
518 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
521 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);
522 avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codec->channels);
523 output_segment_list(&c->streams[i], out, c);
524 avio_printf(out, "\t\t\t</Representation>\n");
526 avio_printf(out, "\t\t</AdaptationSet>\n");
528 avio_printf(out, "\t</Period>\n");
529 avio_printf(out, "</MPD>\n");
532 return ff_rename(temp_filename, s->filename, s);
535 static int dash_write_header(AVFormatContext *s)
537 DASHContext *c = s->priv_data;
539 AVOutputFormat *oformat;
543 if (c->single_file_name)
548 av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
549 ptr = strrchr(c->dirname, '/');
551 av_strlcpy(basename, &ptr[1], sizeof(basename));
554 c->dirname[0] = '\0';
555 av_strlcpy(basename, s->filename, sizeof(basename));
558 ptr = strrchr(basename, '.');
562 oformat = av_guess_format("mp4", NULL, NULL);
564 ret = AVERROR_MUXER_NOT_FOUND;
568 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
570 ret = AVERROR(ENOMEM);
574 for (i = 0; i < s->nb_streams; i++) {
575 OutputStream *os = &c->streams[i];
576 AVFormatContext *ctx;
578 AVDictionary *opts = NULL;
581 os->bit_rate = s->streams[i]->codec->bit_rate ?
582 s->streams[i]->codec->bit_rate :
583 s->streams[i]->codec->rc_max_rate;
585 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
586 " bandwidth=\"%d\"", os->bit_rate);
588 int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
589 AV_LOG_ERROR : AV_LOG_WARNING;
590 av_log(s, level, "No bit rate set for stream %d\n", i);
591 if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
592 ret = AVERROR(EINVAL);
597 ctx = avformat_alloc_context();
599 ret = AVERROR(ENOMEM);
603 ctx->oformat = oformat;
604 ctx->interrupt_callback = s->interrupt_callback;
606 if (!(st = avformat_new_stream(ctx, NULL))) {
607 ret = AVERROR(ENOMEM);
610 avcodec_copy_context(st->codec, s->streams[i]->codec);
611 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
612 st->time_base = s->streams[i]->time_base;
613 ctx->avoid_negative_ts = s->avoid_negative_ts;
615 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
617 ret = AVERROR(ENOMEM);
621 if (c->single_file) {
622 if (c->single_file_name)
623 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
625 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
627 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
629 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
630 ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
633 os->init_start_pos = 0;
635 av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
636 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
643 av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
645 s->streams[i]->time_base = st->time_base;
646 // If the muxer wants to shift timestamps, request to have them shifted
647 // already before being handed to this muxer, so we don't have mismatches
648 // between the MPD and the actual segments.
649 s->avoid_negative_ts = ctx->avoid_negative_ts;
650 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
652 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
655 set_codec_str(s, st->codec, os->codec_str, sizeof(os->codec_str));
656 os->first_pts = AV_NOPTS_VALUE;
657 os->max_pts = AV_NOPTS_VALUE;
658 os->last_dts = AV_NOPTS_VALUE;
659 os->segment_index = 1;
662 if (!c->has_video && c->min_seg_duration <= 0) {
663 av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
664 ret = AVERROR(EINVAL);
666 ret = write_manifest(s, 0);
668 av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
676 static int add_segment(OutputStream *os, const char *file,
677 int64_t time, int duration,
678 int64_t start_pos, int64_t range_length,
679 int64_t index_length)
683 if (os->nb_segments >= os->segments_size) {
684 os->segments_size = (os->segments_size + 1) * 2;
685 if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
686 os->segments_size)) < 0) {
687 os->segments_size = 0;
692 seg = av_mallocz(sizeof(*seg));
694 return AVERROR(ENOMEM);
695 av_strlcpy(seg->file, file, sizeof(seg->file));
697 if (seg->time < 0) // If pts<0, it is expected to be cut away with an edit list
699 seg->duration = duration;
700 seg->start_pos = start_pos;
701 seg->range_length = range_length;
702 seg->index_length = index_length;
703 os->segments[os->nb_segments++] = seg;
708 static void write_styp(AVIOContext *pb)
711 ffio_wfourcc(pb, "styp");
712 ffio_wfourcc(pb, "msdh");
713 avio_wb32(pb, 0); /* minor */
714 ffio_wfourcc(pb, "msdh");
715 ffio_wfourcc(pb, "msix");
718 static void find_index_range(AVFormatContext *s, const char *full_path,
719 int64_t pos, int *index_length)
725 ret = ffurl_open(&fd, full_path, AVIO_FLAG_READ, &s->interrupt_callback, NULL);
728 if (ffurl_seek(fd, pos, SEEK_SET) != pos) {
732 ret = ffurl_read(fd, buf, 8);
736 if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
738 *index_length = AV_RB32(&buf[0]);
741 static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
742 AVCodecContext *codec)
746 if (os->ctx->streams[0]->codec->extradata_size || !codec->extradata_size)
749 extradata = av_malloc(codec->extradata_size);
752 return AVERROR(ENOMEM);
754 memcpy(extradata, codec->extradata, codec->extradata_size);
756 os->ctx->streams[0]->codec->extradata = extradata;
757 os->ctx->streams[0]->codec->extradata_size = codec->extradata_size;
759 set_codec_str(s, codec, os->codec_str, sizeof(os->codec_str));
764 static int dash_flush(AVFormatContext *s, int final, int stream)
766 DASHContext *c = s->priv_data;
768 int cur_flush_segment_index = 0;
770 cur_flush_segment_index = c->streams[stream].segment_index;
772 for (i = 0; i < s->nb_streams; i++) {
773 OutputStream *os = &c->streams[i];
774 char filename[1024] = "", full_path[1024], temp_path[1024];
776 int range_length, index_length = 0;
778 if (!os->packets_written)
781 // Flush the single stream that got a keyframe right now.
782 // Flush all audio streams as well, in sync with video keyframes,
783 // but not the other video streams.
784 if (stream >= 0 && i != stream) {
785 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
787 // Make sure we don't flush audio streams multiple times, when
788 // all video streams are flushed one at a time.
789 if (c->has_video && os->segment_index > cur_flush_segment_index)
793 if (!os->init_range_length) {
794 av_write_frame(os->ctx, NULL);
795 os->init_range_length = avio_tell(os->ctx->pb);
796 if (!c->single_file) {
797 ffurl_close(os->out);
802 start_pos = avio_tell(os->ctx->pb);
804 if (!c->single_file) {
805 dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
806 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
807 snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
808 ret = ffurl_open(&os->out, temp_path, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
811 write_styp(os->ctx->pb);
813 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
816 av_write_frame(os->ctx, NULL);
817 avio_flush(os->ctx->pb);
818 os->packets_written = 0;
820 range_length = avio_tell(os->ctx->pb) - start_pos;
821 if (c->single_file) {
822 find_index_range(s, full_path, start_pos, &index_length);
824 ffurl_close(os->out);
826 ret = ff_rename(temp_path, full_path, s);
830 add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length);
831 av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
834 if (c->window_size || (final && c->remove_at_exit)) {
835 for (i = 0; i < s->nb_streams; i++) {
836 OutputStream *os = &c->streams[i];
838 int remove = os->nb_segments - c->window_size - c->extra_window_size;
839 if (final && c->remove_at_exit)
840 remove = os->nb_segments;
842 for (j = 0; j < remove; j++) {
844 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
846 av_free(os->segments[j]);
848 os->nb_segments -= remove;
849 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
855 ret = write_manifest(s, final);
859 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
861 DASHContext *c = s->priv_data;
862 AVStream *st = s->streams[pkt->stream_index];
863 OutputStream *os = &c->streams[pkt->stream_index];
864 int64_t seg_end_duration = (os->segment_index) * (int64_t) c->min_seg_duration;
867 ret = update_stream_extradata(s, os, st->codec);
871 // Fill in a heuristic guess of the packet duration, if none is available.
872 // The mp4 muxer will do something similar (for the last packet in a fragment)
873 // if nothing is set (setting it for the other packets doesn't hurt).
874 // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
875 // invoke its heuristic (this doesn't have to be identical to that algorithm),
876 // so that we know the exact timestamps of fragments.
877 if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
878 pkt->duration = pkt->dts - os->last_dts;
879 os->last_dts = pkt->dts;
881 // If forcing the stream to start at 0, the mp4 muxer will set the start
882 // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
883 if (os->first_pts == AV_NOPTS_VALUE &&
884 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
885 pkt->pts -= pkt->dts;
889 if (os->first_pts == AV_NOPTS_VALUE)
890 os->first_pts = pkt->pts;
892 if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
893 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
894 av_compare_ts(pkt->pts - os->first_pts, st->time_base,
895 seg_end_duration, AV_TIME_BASE_Q) >= 0) {
896 int64_t prev_duration = c->last_duration;
898 c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
901 c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
905 if ((!c->use_timeline || !c->use_template) && prev_duration) {
906 if (c->last_duration < prev_duration*9/10 ||
907 c->last_duration > prev_duration*11/10) {
908 av_log(s, AV_LOG_WARNING,
909 "Segment durations differ too much, enable use_timeline "
910 "and use_template, or keep a stricter keyframe interval\n");
914 if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
918 if (!os->packets_written) {
919 // If we wrote a previous segment, adjust the start time of the segment
920 // to the end of the previous one (which is the same as the mp4 muxer
921 // does). This avoids gaps in the timeline.
922 if (os->max_pts != AV_NOPTS_VALUE)
923 os->start_pts = os->max_pts;
925 os->start_pts = pkt->pts;
927 if (os->max_pts == AV_NOPTS_VALUE)
928 os->max_pts = pkt->pts + pkt->duration;
930 os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
931 os->packets_written++;
932 return ff_write_chained(os->ctx, 0, pkt, s, 0);
935 static int dash_write_trailer(AVFormatContext *s)
937 DASHContext *c = s->priv_data;
939 if (s->nb_streams > 0) {
940 OutputStream *os = &c->streams[0];
941 // If no segments have been written so far, try to do a crude
942 // guess of the segment duration
943 if (!c->last_duration)
944 c->last_duration = av_rescale_q(os->max_pts - os->start_pts,
945 s->streams[0]->time_base,
947 c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
948 s->streams[0]->time_base,
951 dash_flush(s, 1, -1);
953 if (c->remove_at_exit) {
956 for (i = 0; i < s->nb_streams; i++) {
957 OutputStream *os = &c->streams[i];
958 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
968 #define OFFSET(x) offsetof(DASHContext, x)
969 #define E AV_OPT_FLAG_ENCODING_PARAM
970 static const AVOption options[] = {
971 { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
972 { "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 },
973 { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
974 { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
975 { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
976 { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
977 { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
978 { "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 },
979 { "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 },
980 { "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 },
984 static const AVClass dash_class = {
985 .class_name = "dash muxer",
986 .item_name = av_default_item_name,
988 .version = LIBAVUTIL_VERSION_INT,
991 AVOutputFormat ff_dash_muxer = {
993 .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
994 .priv_data_size = sizeof(DASHContext),
995 .audio_codec = AV_CODEC_ID_AAC,
996 .video_codec = AV_CODEC_ID_H264,
997 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
998 .write_header = dash_write_header,
999 .write_packet = dash_write_packet,
1000 .write_trailer = dash_write_trailer,
1001 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1002 .priv_class = &dash_class,