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 typedef struct Segment {
44 int range_length, index_length;
50 typedef struct OutputStream {
57 int64_t init_start_pos;
58 int init_range_length;
59 int nb_segments, segments_size, segment_index;
61 int64_t first_dts, start_dts, end_dts;
62 char bandwidth_str[64];
67 typedef struct DASHContext {
68 const AVClass *class; /* Class for private options. */
70 int extra_window_size;
76 OutputStream *streams;
77 int has_video, has_audio;
81 char availability_start_time[100];
85 static int dash_write(void *opaque, uint8_t *buf, int buf_size)
87 OutputStream *os = opaque;
89 ffurl_write(os->out, buf, buf_size);
94 static void set_codec_str(AVFormatContext *s, AVCodecContext *codec,
97 const AVCodecTag *tags[2] = { NULL, NULL };
99 if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
100 tags[0] = ff_codec_movvideo_tags;
101 else if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
102 tags[0] = ff_codec_movaudio_tags;
106 tag = av_codec_get_tag(tags, codec->codec_id);
114 if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
116 tags[0] = ff_mp4_obj_type;
117 oti = av_codec_get_tag(tags, codec->codec_id);
119 av_strlcatf(str, size, ".%02x", oti);
123 if (tag == MKTAG('m', 'p', '4', 'a')) {
124 if (codec->extradata_size >= 2) {
125 int aot = codec->extradata[0] >> 3;
127 aot = ((AV_RB16(codec->extradata) >> 5) & 0x3f) + 32;
128 av_strlcatf(str, size, ".%d", aot);
130 } else if (tag == MKTAG('m', 'p', '4', 'v')) {
131 // Unimplemented, should output ProfileLevelIndication as a decimal number
132 av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
134 } else if (!strcmp(str, "avc1")) {
135 uint8_t *tmpbuf = NULL;
136 uint8_t *extradata = codec->extradata;
137 int extradata_size = codec->extradata_size;
140 if (extradata[0] != 1) {
142 if (avio_open_dyn_buf(&pb) < 0)
144 if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
145 avio_close_dyn_buf(pb, &tmpbuf);
149 extradata_size = avio_close_dyn_buf(pb, &extradata);
153 if (extradata_size >= 4)
154 av_strlcatf(str, size, ".%02x%02x%02x",
155 extradata[1], extradata[2], extradata[3]);
160 static void dash_free(AVFormatContext *s)
162 DASHContext *c = s->priv_data;
166 for (i = 0; i < s->nb_streams; i++) {
167 OutputStream *os = &c->streams[i];
168 if (os->ctx && os->ctx_inited)
169 av_write_trailer(os->ctx);
170 if (os->ctx && os->ctx->pb)
171 av_free(os->ctx->pb);
172 ffurl_close(os->out);
175 avformat_free_context(os->ctx);
176 for (j = 0; j < os->nb_segments; j++)
177 av_free(os->segments[j]);
178 av_free(os->segments);
180 av_freep(&c->streams);
183 static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
185 int i, start_index = 0, start_number = 1;
186 if (c->window_size) {
187 start_index = FFMAX(os->nb_segments - c->window_size, 0);
188 start_number = FFMAX(os->segment_index - c->window_size, 1);
191 if (c->use_template) {
192 int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE;
193 avio_printf(out, "\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
194 if (!c->use_timeline)
195 avio_printf(out, "duration=\"%d\" ", c->last_duration);
196 avio_printf(out, "initialization=\"init-stream$RepresentationID$.m4s\" media=\"chunk-stream$RepresentationID$-$Number%%05d$.m4s\" startNumber=\"%d\">\n", c->use_timeline ? start_number : 1);
197 if (c->use_timeline) {
198 avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
199 for (i = start_index; i < os->nb_segments; ) {
200 Segment *seg = os->segments[i];
202 avio_printf(out, "\t\t\t\t\t\t<S ");
203 if (i == start_index)
204 avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
205 avio_printf(out, "d=\"%d\" ", seg->duration);
206 while (i + repeat + 1 < os->nb_segments && os->segments[i + repeat + 1]->duration == seg->duration)
209 avio_printf(out, "r=\"%d\" ", repeat);
210 avio_printf(out, "/>\n");
213 avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
215 avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
216 } else if (c->single_file) {
217 avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
218 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%d\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
219 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);
220 for (i = start_index; i < os->nb_segments; i++) {
221 Segment *seg = os->segments[i];
222 avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
223 if (seg->index_length)
224 avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
225 avio_printf(out, "/>\n");
227 avio_printf(out, "\t\t\t\t</SegmentList>\n");
229 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%d\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
230 avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
231 for (i = start_index; i < os->nb_segments; i++) {
232 Segment *seg = os->segments[i];
233 avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
235 avio_printf(out, "\t\t\t\t</SegmentList>\n");
239 static char *xmlescape(const char *str) {
240 int outlen = strlen(str)*3/2 + 6;
241 char *out = av_realloc(NULL, outlen + 1);
245 for (; *str; str++) {
246 if (pos + 6 > outlen) {
248 outlen = 2 * outlen + 6;
249 tmp = av_realloc(out, outlen + 1);
257 memcpy(&out[pos], "&", 5);
259 } else if (*str == '<') {
260 memcpy(&out[pos], "<", 4);
262 } else if (*str == '>') {
263 memcpy(&out[pos], ">", 4);
265 } else if (*str == '\'') {
266 memcpy(&out[pos], "'", 6);
268 } else if (*str == '\"') {
269 memcpy(&out[pos], """, 6);
279 static void write_time(AVIOContext *out, int64_t time)
281 int seconds = time / AV_TIME_BASE;
282 int fractions = time % AV_TIME_BASE;
283 int minutes = seconds / 60;
284 int hours = minutes / 60;
287 avio_printf(out, "PT");
289 avio_printf(out, "%dH", hours);
290 if (hours || minutes)
291 avio_printf(out, "%dM", minutes);
292 avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
295 static int write_manifest(AVFormatContext *s, int final)
297 DASHContext *c = s->priv_data;
299 char temp_filename[1024];
301 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
303 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
304 ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
306 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
309 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
310 avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
311 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
312 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
313 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
314 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
315 "\ttype=\"%s\"\n", final ? "static" : "dynamic");
317 avio_printf(out, "\tmediaPresentationDuration=\"");
318 write_time(out, c->total_duration);
319 avio_printf(out, "\"\n");
321 int update_period = c->last_duration / AV_TIME_BASE;
322 if (c->use_template && !c->use_timeline)
324 avio_printf(out, "\tminimumUpdatePeriod=\"PT%dS\"\n", update_period);
325 avio_printf(out, "\tsuggestedPresentationDelay=\"PT%dS\"\n", c->last_duration / AV_TIME_BASE);
326 if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
327 time_t t = time(NULL);
328 struct tm *ptm, tmbuf;
329 ptm = gmtime_r(&t, &tmbuf);
331 if (!strftime(c->availability_start_time, sizeof(c->availability_start_time),
332 "%Y-%m-%dT%H:%M:%S", ptm))
333 c->availability_start_time[0] = '\0';
336 if (c->availability_start_time[0])
337 avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
338 if (c->window_size && c->use_template) {
339 avio_printf(out, "\ttimeShiftBufferDepth=\"");
340 write_time(out, c->last_duration * c->window_size);
341 avio_printf(out, "\"\n");
344 avio_printf(out, "\tminBufferTime=\"");
345 write_time(out, c->last_duration);
346 avio_printf(out, "\">\n");
347 avio_printf(out, "\t<ProgramInformation>\n");
349 char *escaped = xmlescape(title->value);
350 avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
353 avio_printf(out, "\t</ProgramInformation>\n");
354 if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
355 OutputStream *os = &c->streams[0];
356 int start_index = FFMAX(os->nb_segments - c->window_size, 0);
357 int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
358 avio_printf(out, "\t<Period start=\"");
359 write_time(out, start_time);
360 avio_printf(out, "\">\n");
362 avio_printf(out, "\t<Period start=\"PT0.0S\">\n");
366 avio_printf(out, "\t\t<AdaptationSet id=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
367 for (i = 0; i < s->nb_streams; i++) {
368 AVStream *st = s->streams[i];
369 OutputStream *os = &c->streams[i];
370 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
372 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);
373 output_segment_list(&c->streams[i], out, c);
374 avio_printf(out, "\t\t\t</Representation>\n");
376 avio_printf(out, "\t\t</AdaptationSet>\n");
379 avio_printf(out, "\t\t<AdaptationSet id=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
380 for (i = 0; i < s->nb_streams; i++) {
381 AVStream *st = s->streams[i];
382 OutputStream *os = &c->streams[i];
383 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
385 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);
386 avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codec->channels);
387 output_segment_list(&c->streams[i], out, c);
388 avio_printf(out, "\t\t\t</Representation>\n");
390 avio_printf(out, "\t\t</AdaptationSet>\n");
392 avio_printf(out, "\t</Period>\n");
393 avio_printf(out, "</MPD>\n");
396 return ff_rename(temp_filename, s->filename, s);
399 static int dash_write_header(AVFormatContext *s)
401 DASHContext *c = s->priv_data;
403 AVOutputFormat *oformat;
410 av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
411 ptr = strrchr(c->dirname, '/');
413 av_strlcpy(basename, &ptr[1], sizeof(basename));
416 c->dirname[0] = '\0';
417 av_strlcpy(basename, s->filename, sizeof(basename));
420 ptr = strrchr(basename, '.');
424 oformat = av_guess_format("mp4", NULL, NULL);
426 ret = AVERROR_MUXER_NOT_FOUND;
430 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
432 ret = AVERROR(ENOMEM);
436 for (i = 0; i < s->nb_streams; i++) {
437 OutputStream *os = &c->streams[i];
438 AVFormatContext *ctx;
440 AVDictionary *opts = NULL;
443 int bit_rate = s->streams[i]->codec->bit_rate ?
444 s->streams[i]->codec->bit_rate :
445 s->streams[i]->codec->rc_max_rate;
447 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
448 " bandwidth=\"%d\"", bit_rate);
450 int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
451 AV_LOG_ERROR : AV_LOG_WARNING;
452 av_log(s, level, "No bit rate set for stream %d\n", i);
453 if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
454 ret = AVERROR(EINVAL);
459 ctx = avformat_alloc_context();
461 ret = AVERROR(ENOMEM);
465 ctx->oformat = oformat;
466 ctx->interrupt_callback = s->interrupt_callback;
468 if (!(st = avformat_new_stream(ctx, NULL))) {
469 ret = AVERROR(ENOMEM);
472 avcodec_copy_context(st->codec, s->streams[i]->codec);
473 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
474 st->time_base = s->streams[i]->time_base;
475 ctx->avoid_negative_ts = s->avoid_negative_ts;
477 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
479 ret = AVERROR(ENOMEM);
484 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
486 snprintf(os->initfile, sizeof(os->initfile), "init-stream%d.m4s", i);
487 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
488 ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
491 os->init_start_pos = 0;
493 av_dict_set(&opts, "movflags", "frag_custom+dash", 0);
494 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
501 if (c->single_file) {
502 os->init_range_length = avio_tell(ctx->pb);
504 ffurl_close(os->out);
508 s->streams[i]->time_base = st->time_base;
509 // If the muxer wants to shift timestamps, request to have them shifted
510 // already before being handed to this muxer, so we don't have mismatches
511 // between the MPD and the actual segments.
512 s->avoid_negative_ts = ctx->avoid_negative_ts;
513 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
515 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
518 set_codec_str(s, os->ctx->streams[0]->codec, os->codec_str, sizeof(os->codec_str));
519 os->first_dts = AV_NOPTS_VALUE;
520 os->segment_index = 1;
523 if (!c->has_video && c->min_seg_duration <= 0) {
524 av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
525 ret = AVERROR(EINVAL);
527 ret = write_manifest(s, 0);
535 static int add_segment(OutputStream *os, const char *file,
536 int64_t time, int duration,
537 int64_t start_pos, int64_t range_length,
538 int64_t index_length)
542 if (os->nb_segments >= os->segments_size) {
543 os->segments_size = (os->segments_size + 1) * 2;
544 if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
545 os->segments_size)) < 0) {
546 os->segments_size = 0;
551 seg = av_mallocz(sizeof(*seg));
553 return AVERROR(ENOMEM);
554 av_strlcpy(seg->file, file, sizeof(seg->file));
556 seg->duration = duration;
557 seg->start_pos = start_pos;
558 seg->range_length = range_length;
559 seg->index_length = index_length;
560 os->segments[os->nb_segments++] = seg;
565 static void write_styp(AVIOContext *pb)
568 ffio_wfourcc(pb, "styp");
569 ffio_wfourcc(pb, "msdh");
570 avio_wb32(pb, 0); /* minor */
571 ffio_wfourcc(pb, "msdh");
572 ffio_wfourcc(pb, "msix");
575 static void find_index_range(AVFormatContext *s, const char *dirname,
576 const char *filename, int64_t pos,
579 char full_path[1024];
584 snprintf(full_path, sizeof(full_path), "%s%s", dirname, filename);
585 ret = ffurl_open(&fd, full_path, AVIO_FLAG_READ, &s->interrupt_callback, NULL);
588 if (ffurl_seek(fd, pos, SEEK_SET) != pos) {
592 ret = ffurl_read(fd, buf, 8);
596 if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
598 *index_length = AV_RB32(&buf[0]);
601 static int dash_flush(AVFormatContext *s, int final)
603 DASHContext *c = s->priv_data;
606 for (i = 0; i < s->nb_streams; i++) {
607 OutputStream *os = &c->streams[i];
608 char filename[1024] = "", full_path[1024], temp_path[1024];
609 int64_t start_pos = avio_tell(os->ctx->pb);
610 int range_length, index_length = 0;
612 if (!os->packets_written)
615 if (!c->single_file) {
616 snprintf(filename, sizeof(filename), "chunk-stream%d-%05d.m4s", i, os->segment_index);
617 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
618 snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
619 ret = ffurl_open(&os->out, temp_path, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
622 write_styp(os->ctx->pb);
624 av_write_frame(os->ctx, NULL);
625 avio_flush(os->ctx->pb);
626 os->packets_written = 0;
628 range_length = avio_tell(os->ctx->pb) - start_pos;
629 if (c->single_file) {
630 find_index_range(s, c->dirname, os->initfile, start_pos, &index_length);
632 ffurl_close(os->out);
634 ret = ff_rename(temp_path, full_path, s);
638 add_segment(os, filename, os->start_dts, os->end_dts - os->start_dts, start_pos, range_length, index_length);
641 if (c->window_size || (final && c->remove_at_exit)) {
642 for (i = 0; i < s->nb_streams; i++) {
643 OutputStream *os = &c->streams[i];
645 int remove = os->nb_segments - c->window_size - c->extra_window_size;
646 if (final && c->remove_at_exit)
647 remove = os->nb_segments;
649 for (j = 0; j < remove; j++) {
651 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
653 av_free(os->segments[j]);
655 os->nb_segments -= remove;
656 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
662 ret = write_manifest(s, final);
666 static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
668 DASHContext *c = s->priv_data;
669 AVStream *st = s->streams[pkt->stream_index];
670 OutputStream *os = &c->streams[pkt->stream_index];
671 int64_t seg_end_duration = (c->nb_segments + 1) * (int64_t) c->min_seg_duration;
674 // If forcing the stream to start at 0, the mp4 muxer will set the start
675 // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
676 if (os->first_dts == AV_NOPTS_VALUE &&
677 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
678 pkt->pts -= pkt->dts;
682 if (os->first_dts == AV_NOPTS_VALUE)
683 os->first_dts = pkt->dts;
685 if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
686 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
687 av_compare_ts(pkt->dts - os->first_dts, st->time_base,
688 seg_end_duration, AV_TIME_BASE_Q) >= 0) {
689 int64_t prev_duration = c->last_duration;
691 c->last_duration = av_rescale_q(pkt->dts - os->start_dts,
694 c->total_duration = av_rescale_q(pkt->dts - os->first_dts,
698 if ((!c->use_timeline || !c->use_template) && prev_duration) {
699 if (c->last_duration < prev_duration*9/10 ||
700 c->last_duration > prev_duration*11/10) {
701 av_log(s, AV_LOG_WARNING,
702 "Segment durations differ too much, enable use_timeline "
703 "and use_template, or keep a stricter keyframe interval\n");
707 if ((ret = dash_flush(s, 0)) < 0)
712 if (!os->packets_written)
713 os->start_dts = pkt->dts;
714 os->end_dts = pkt->dts + pkt->duration;
715 os->packets_written++;
716 return ff_write_chained(os->ctx, 0, pkt, s, 0);
719 static int dash_write_trailer(AVFormatContext *s)
721 DASHContext *c = s->priv_data;
723 if (s->nb_streams > 0) {
724 OutputStream *os = &c->streams[0];
725 // If no segments have been written so far, try to do a crude
726 // guess of the segment duration
727 if (!c->last_duration)
728 c->last_duration = av_rescale_q(os->end_dts - os->start_dts,
729 s->streams[0]->time_base,
731 c->total_duration = av_rescale_q(os->end_dts - os->first_dts,
732 s->streams[0]->time_base,
737 if (c->remove_at_exit) {
740 for (i = 0; i < s->nb_streams; i++) {
741 OutputStream *os = &c->streams[i];
742 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
752 #define OFFSET(x) offsetof(DASHContext, x)
753 #define E AV_OPT_FLAG_ENCODING_PARAM
754 static const AVOption options[] = {
755 { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
756 { "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 },
757 { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
758 { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
759 { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
760 { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
761 { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
765 static const AVClass dash_class = {
766 .class_name = "dash muxer",
767 .item_name = av_default_item_name,
769 .version = LIBAVUTIL_VERSION_INT,
772 AVOutputFormat ff_dash_muxer = {
774 .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
775 .priv_data_size = sizeof(DASHContext),
776 .audio_codec = AV_CODEC_ID_AAC,
777 .video_codec = AV_CODEC_ID_H264,
778 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
779 .write_header = dash_write_header,
780 .write_packet = dash_write_packet,
781 .write_trailer = dash_write_trailer,
782 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
783 .priv_class = &dash_class,