dashenc: replace attribute id with contentType for the AdaptationSet element
[libav.git] / libavformat / dashenc.c
CommitLineData
fe5e6e34
MS
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
a9d8d35e
BH
41// See ISO/IEC 23009-1:2014 5.3.9.4.4
42typedef 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
fe5e6e34
MS
51typedef 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
60typedef 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;
7a1a63e3 71 int64_t first_pts, start_pts, max_pts;
30411836 72 int64_t last_dts;
a9d8d35e 73 int bit_rate;
f856d9c2 74 char bandwidth_str[64];
fe5e6e34
MS
75
76 char codec_str[100];
77} OutputStream;
78
79typedef struct DASHContext {
80 const AVClass *class; /* Class for private options. */
81 int window_size;
82 int extra_window_size;
83 int min_seg_duration;
84 int remove_at_exit;
85 int use_template;
86 int use_timeline;
87 int single_file;
88 OutputStream *streams;
89 int has_video, has_audio;
e737a4aa
MS
90 int64_t last_duration;
91 int64_t total_duration;
fe5e6e34
MS
92 char availability_start_time[100];
93 char dirname[1024];
a9d8d35e
BH
94 const char *single_file_name;
95 const char *init_seg_name;
96 const char *media_seg_name;
fe5e6e34
MS
97} DASHContext;
98
99static int dash_write(void *opaque, uint8_t *buf, int buf_size)
100{
101 OutputStream *os = opaque;
102 if (os->out)
103 ffurl_write(os->out, buf, buf_size);
104 return buf_size;
105}
106
107// RFC 6381
108static void set_codec_str(AVFormatContext *s, AVCodecContext *codec,
109 char *str, int size)
110{
111 const AVCodecTag *tags[2] = { NULL, NULL };
112 uint32_t tag;
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;
117 else
118 return;
119
120 tag = av_codec_get_tag(tags, codec->codec_id);
121 if (!tag)
122 return;
123 if (size < 5)
124 return;
125
126 AV_WL32(str, tag);
127 str[4] = '\0';
128 if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
129 uint32_t oti;
130 tags[0] = ff_mp4_obj_type;
131 oti = av_codec_get_tag(tags, codec->codec_id);
132 if (oti)
133 av_strlcatf(str, size, ".%02x", oti);
134 else
135 return;
136
137 if (tag == MKTAG('m', 'p', '4', 'a')) {
138 if (codec->extradata_size >= 2) {
139 int aot = codec->extradata[0] >> 3;
140 if (aot == 31)
141 aot = ((AV_RB16(codec->extradata) >> 5) & 0x3f) + 32;
142 av_strlcatf(str, size, ".%d", aot);
143 }
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");
147 }
148 } else if (!strcmp(str, "avc1")) {
149 uint8_t *tmpbuf = NULL;
150 uint8_t *extradata = codec->extradata;
151 int extradata_size = codec->extradata_size;
152 if (!extradata_size)
153 return;
154 if (extradata[0] != 1) {
155 AVIOContext *pb;
156 if (avio_open_dyn_buf(&pb) < 0)
157 return;
158 if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
8e32b1f0 159 ffio_free_dyn_buf(&pb);
fe5e6e34
MS
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
173static 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
196static 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)
e737a4aa 208 avio_printf(out, "duration=\"%"PRId64"\" ", c->last_duration);
a9d8d35e 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);
fe5e6e34 210 if (c->use_timeline) {
2f628d59 211 int64_t cur_time = 0;
fe5e6e34
MS
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];
215 int repeat = 0;
216 avio_printf(out, "\t\t\t\t\t\t<S ");
b91a5757
MS
217 if (i == start_index || seg->time != cur_time) {
218 cur_time = seg->time;
fe5e6e34 219 avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
b91a5757 220 }
fe5e6e34 221 avio_printf(out, "d=\"%d\" ", seg->duration);
2f628d59
MS
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)
fe5e6e34
MS
225 repeat++;
226 if (repeat > 0)
227 avio_printf(out, "r=\"%d\" ", repeat);
228 avio_printf(out, "/>\n");
229 i += 1 + repeat;
2f628d59 230 cur_time += (1 + repeat) * seg->duration;
fe5e6e34
MS
231 }
232 avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
233 }
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);
e737a4aa 237 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
fe5e6e34
MS
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");
245 }
246 avio_printf(out, "\t\t\t\t</SegmentList>\n");
247 } else {
e737a4aa 248 avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
fe5e6e34
MS
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);
253 }
254 avio_printf(out, "\t\t\t\t</SegmentList>\n");
255 }
256}
257
a9d8d35e
BH
258static 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;
262
263 if (av_strstart(identifier, "$$", &next_ptr)) {
264 id_type = DASH_TMPL_ID_ESCAPE;
265 *ptr = next_ptr;
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);
271 *ptr = next_ptr;
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;
279 else
280 id_type = DASH_TMPL_ID_UNDEFINED;
281
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) {
3a724a7f 285 const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
a9d8d35e
BH
286 if (next_ptr[0] == '$') { // no dash format-tag
287 snprintf(format_tag, format_tag_size, "%%%s", number_format);
288 *ptr = &next_ptr[1];
289 } else {
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);
297 *ptr = next_ptr;
298 } else {
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;
303 }
304 }
305 }
306 }
307 return id_type;
308}
309
310static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
311 const char *template, int rep_id,
312 int number, int bit_rate,
313 int64_t time) {
314 int dst_pos = 0;
315 const char *t_cur = template;
316 while (dst_pos < buffer_size - 1 && *t_cur) {
fcae9f21 317 char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
a9d8d35e
BH
318 int n = 0;
319 DASHTmplId id_type;
320 const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
321 if (t_next) {
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);
324 // advance
325 dst_pos += num_copy_bytes;
326 t_cur = t_next;
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);
329 break;
330 }
331
332 if (dst_pos >= buffer_size - 1 || !*t_cur)
333 break;
334
335 // t_cur is now pointing to a '$' character
fcae9f21 336 id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
a9d8d35e
BH
337 switch (id_type) {
338 case DASH_TMPL_ID_ESCAPE:
339 av_strlcpy(&dst[dst_pos], "$", 2);
340 n = 1;
341 break;
342 case DASH_TMPL_ID_REP_ID:
343 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
344 break;
345 case DASH_TMPL_ID_NUMBER:
346 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
347 break;
348 case DASH_TMPL_ID_BANDWIDTH:
349 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
350 break;
351 case DASH_TMPL_ID_TIME:
352 n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
353 break;
354 case DASH_TMPL_ID_UNDEFINED:
355 // copy over one byte and advance
356 av_strlcpy(&dst[dst_pos], t_cur, 2);
357 n = 1;
358 t_next = &t_cur[1];
359 break;
360 }
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).
364
365 // advance
366 dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
367 t_cur = t_next;
368 }
369}
370
fe5e6e34
MS
371static char *xmlescape(const char *str) {
372 int outlen = strlen(str)*3/2 + 6;
373 char *out = av_realloc(NULL, outlen + 1);
374 int pos = 0;
375 if (!out)
376 return NULL;
377 for (; *str; str++) {
378 if (pos + 6 > outlen) {
379 char *tmp;
380 outlen = 2 * outlen + 6;
381 tmp = av_realloc(out, outlen + 1);
382 if (!tmp) {
383 av_free(out);
384 return NULL;
385 }
386 out = tmp;
387 }
388 if (*str == '&') {
389 memcpy(&out[pos], "&amp;", 5);
390 pos += 5;
391 } else if (*str == '<') {
392 memcpy(&out[pos], "&lt;", 4);
393 pos += 4;
394 } else if (*str == '>') {
395 memcpy(&out[pos], "&gt;", 4);
396 pos += 4;
397 } else if (*str == '\'') {
398 memcpy(&out[pos], "&apos;", 6);
399 pos += 6;
400 } else if (*str == '\"') {
401 memcpy(&out[pos], "&quot;", 6);
402 pos += 6;
403 } else {
404 out[pos++] = *str;
405 }
406 }
407 out[pos] = '\0';
408 return out;
409}
410
411static void write_time(AVIOContext *out, int64_t time)
412{
413 int seconds = time / AV_TIME_BASE;
414 int fractions = time % AV_TIME_BASE;
415 int minutes = seconds / 60;
416 int hours = minutes / 60;
417 seconds %= 60;
418 minutes %= 60;
419 avio_printf(out, "PT");
420 if (hours)
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));
425}
426
2cc39365
MS
427static void format_date_now(char *buf, int size)
428{
429 time_t t = time(NULL);
430 struct tm *ptm, tmbuf;
431 ptm = gmtime_r(&t, &tmbuf);
432 if (ptm) {
433 if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%S", ptm))
434 buf[0] = '\0';
435 }
436}
437
fe5e6e34
MS
438static int write_manifest(AVFormatContext *s, int final)
439{
440 DASHContext *c = s->priv_data;
441 AVIOContext *out;
442 char temp_filename[1024];
443 int ret, i;
444 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
445
446 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
675ac56b 447 ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
fe5e6e34 448 if (ret < 0) {
675ac56b 449 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
fe5e6e34
MS
450 return ret;
451 }
452 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
453 avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
454 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
455 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
456 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
457 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
458 "\ttype=\"%s\"\n", final ? "static" : "dynamic");
459 if (final) {
460 avio_printf(out, "\tmediaPresentationDuration=\"");
461 write_time(out, c->total_duration);
462 avio_printf(out, "\"\n");
463 } else {
e737a4aa 464 int64_t update_period = c->last_duration / AV_TIME_BASE;
2cc39365 465 char now_str[100];
fe5e6e34
MS
466 if (c->use_template && !c->use_timeline)
467 update_period = 500;
e737a4aa
MS
468 avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
469 avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
fe5e6e34 470 if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
2cc39365 471 format_date_now(c->availability_start_time, sizeof(c->availability_start_time));
fe5e6e34
MS
472 }
473 if (c->availability_start_time[0])
474 avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
2cc39365
MS
475 format_date_now(now_str, sizeof(now_str));
476 if (now_str[0])
477 avio_printf(out, "\tpublishTime=\"%s\"\n", now_str);
fe5e6e34
MS
478 if (c->window_size && c->use_template) {
479 avio_printf(out, "\ttimeShiftBufferDepth=\"");
480 write_time(out, c->last_duration * c->window_size);
481 avio_printf(out, "\"\n");
482 }
483 }
484 avio_printf(out, "\tminBufferTime=\"");
485 write_time(out, c->last_duration);
486 avio_printf(out, "\">\n");
487 avio_printf(out, "\t<ProgramInformation>\n");
488 if (title) {
489 char *escaped = xmlescape(title->value);
490 avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
491 av_free(escaped);
492 }
493 avio_printf(out, "\t</ProgramInformation>\n");
494 if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
495 OutputStream *os = &c->streams[0];
496 int start_index = FFMAX(os->nb_segments - c->window_size, 0);
497 int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
498 avio_printf(out, "\t<Period start=\"");
499 write_time(out, start_time);
500 avio_printf(out, "\">\n");
501 } else {
502 avio_printf(out, "\t<Period start=\"PT0.0S\">\n");
503 }
504
505 if (c->has_video) {
ac1a1cb9 506 avio_printf(out, "\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
fe5e6e34
MS
507 for (i = 0; i < s->nb_streams; i++) {
508 AVStream *st = s->streams[i];
509 OutputStream *os = &c->streams[i];
33d412eb 510 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO)
fe5e6e34 511 continue;
f856d9c2 512 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);
fe5e6e34
MS
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 if (c->has_audio) {
ac1a1cb9 519 avio_printf(out, "\t\t<AdaptationSet contentType=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
fe5e6e34
MS
520 for (i = 0; i < s->nb_streams; i++) {
521 AVStream *st = s->streams[i];
522 OutputStream *os = &c->streams[i];
33d412eb 523 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
fe5e6e34 524 continue;
f856d9c2 525 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);
fe5e6e34
MS
526 avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codec->channels);
527 output_segment_list(&c->streams[i], out, c);
528 avio_printf(out, "\t\t\t</Representation>\n");
529 }
530 avio_printf(out, "\t\t</AdaptationSet>\n");
531 }
532 avio_printf(out, "\t</Period>\n");
533 avio_printf(out, "</MPD>\n");
534 avio_flush(out);
535 avio_close(out);
675ac56b 536 return ff_rename(temp_filename, s->filename);
fe5e6e34
MS
537}
538
539static int dash_write_header(AVFormatContext *s)
540{
541 DASHContext *c = s->priv_data;
542 int ret = 0, i;
543 AVOutputFormat *oformat;
544 char *ptr;
545 char basename[1024];
546
a9d8d35e
BH
547 if (c->single_file_name)
548 c->single_file = 1;
fe5e6e34
MS
549 if (c->single_file)
550 c->use_template = 0;
551
552 av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
553 ptr = strrchr(c->dirname, '/');
554 if (ptr) {
555 av_strlcpy(basename, &ptr[1], sizeof(basename));
556 ptr[1] = '\0';
557 } else {
558 c->dirname[0] = '\0';
559 av_strlcpy(basename, s->filename, sizeof(basename));
560 }
561
562 ptr = strrchr(basename, '.');
563 if (ptr)
564 *ptr = '\0';
565
566 oformat = av_guess_format("mp4", NULL, NULL);
567 if (!oformat) {
568 ret = AVERROR_MUXER_NOT_FOUND;
569 goto fail;
570 }
571
572 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
573 if (!c->streams) {
574 ret = AVERROR(ENOMEM);
575 goto fail;
576 }
577
578 for (i = 0; i < s->nb_streams; i++) {
579 OutputStream *os = &c->streams[i];
580 AVFormatContext *ctx;
581 AVStream *st;
582 AVDictionary *opts = NULL;
583 char filename[1024];
584
a9d8d35e
BH
585 os->bit_rate = s->streams[i]->codec->bit_rate;
586 if (os->bit_rate) {
f856d9c2 587 snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
a9d8d35e 588 " bandwidth=\"%d\"", os->bit_rate);
f856d9c2
MS
589 } else {
590 int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ?
591 AV_LOG_ERROR : AV_LOG_WARNING;
592 av_log(s, level, "No bit rate set for stream %d\n", i);
593 if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
594 ret = AVERROR(EINVAL);
595 goto fail;
596 }
fe5e6e34
MS
597 }
598
599 ctx = avformat_alloc_context();
600 if (!ctx) {
601 ret = AVERROR(ENOMEM);
602 goto fail;
603 }
604 os->ctx = ctx;
605 ctx->oformat = oformat;
606 ctx->interrupt_callback = s->interrupt_callback;
607
608 if (!(st = avformat_new_stream(ctx, NULL))) {
609 ret = AVERROR(ENOMEM);
610 goto fail;
611 }
612 avcodec_copy_context(st->codec, s->streams[i]->codec);
613 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
614 st->time_base = s->streams[i]->time_base;
615 ctx->avoid_negative_ts = s->avoid_negative_ts;
616
617 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
618 if (!ctx->pb) {
619 ret = AVERROR(ENOMEM);
620 goto fail;
621 }
622
a9d8d35e
BH
623 if (c->single_file) {
624 if (c->single_file_name)
625 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
626 else
627 snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
628 } else {
629 dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
630 }
fe5e6e34
MS
631 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
632 ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
633 if (ret < 0)
634 goto fail;
635 os->init_start_pos = 0;
636
c5e7ea13 637 av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
fe5e6e34
MS
638 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
639 goto fail;
640 }
641 os->ctx_inited = 1;
642 avio_flush(ctx->pb);
643 av_dict_free(&opts);
644
c5e7ea13 645 av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
fe5e6e34
MS
646
647 s->streams[i]->time_base = st->time_base;
648 // If the muxer wants to shift timestamps, request to have them shifted
649 // already before being handed to this muxer, so we don't have mismatches
650 // between the MPD and the actual segments.
651 s->avoid_negative_ts = ctx->avoid_negative_ts;
652 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
653 c->has_video = 1;
654 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
655 c->has_audio = 1;
656
6cf7f306 657 set_codec_str(s, st->codec, os->codec_str, sizeof(os->codec_str));
7a1a63e3
MS
658 os->first_pts = AV_NOPTS_VALUE;
659 os->max_pts = AV_NOPTS_VALUE;
30411836 660 os->last_dts = AV_NOPTS_VALUE;
fe5e6e34
MS
661 os->segment_index = 1;
662 }
663
664 if (!c->has_video && c->min_seg_duration <= 0) {
665 av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
666 ret = AVERROR(EINVAL);
667 }
668 ret = write_manifest(s, 0);
fa8934d6
BH
669 if (!ret)
670 av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
fe5e6e34
MS
671
672fail:
673 if (ret)
674 dash_free(s);
675 return ret;
676}
677
678static int add_segment(OutputStream *os, const char *file,
679 int64_t time, int duration,
680 int64_t start_pos, int64_t range_length,
681 int64_t index_length)
682{
683 int err;
684 Segment *seg;
685 if (os->nb_segments >= os->segments_size) {
686 os->segments_size = (os->segments_size + 1) * 2;
687 if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
688 os->segments_size)) < 0) {
689 os->segments_size = 0;
690 os->nb_segments = 0;
691 return err;
692 }
693 }
694 seg = av_mallocz(sizeof(*seg));
695 if (!seg)
696 return AVERROR(ENOMEM);
697 av_strlcpy(seg->file, file, sizeof(seg->file));
698 seg->time = time;
7a1a63e3
MS
699 if (seg->time < 0) // If pts<0, it is expected to be cut away with an edit list
700 seg->time = 0;
fe5e6e34
MS
701 seg->duration = duration;
702 seg->start_pos = start_pos;
703 seg->range_length = range_length;
704 seg->index_length = index_length;
705 os->segments[os->nb_segments++] = seg;
706 os->segment_index++;
707 return 0;
708}
709
710static void write_styp(AVIOContext *pb)
711{
712 avio_wb32(pb, 24);
713 ffio_wfourcc(pb, "styp");
714 ffio_wfourcc(pb, "msdh");
715 avio_wb32(pb, 0); /* minor */
716 ffio_wfourcc(pb, "msdh");
717 ffio_wfourcc(pb, "msix");
718}
719
fa8934d6
BH
720static void find_index_range(AVFormatContext *s, const char *full_path,
721 int64_t pos, int *index_length)
fe5e6e34 722{
fe5e6e34
MS
723 uint8_t buf[8];
724 URLContext *fd;
725 int ret;
726
fe5e6e34
MS
727 ret = ffurl_open(&fd, full_path, AVIO_FLAG_READ, &s->interrupt_callback, NULL);
728 if (ret < 0)
729 return;
730 if (ffurl_seek(fd, pos, SEEK_SET) != pos) {
731 ffurl_close(fd);
732 return;
733 }
734 ret = ffurl_read(fd, buf, 8);
735 ffurl_close(fd);
736 if (ret < 8)
737 return;
738 if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
739 return;
740 *index_length = AV_RB32(&buf[0]);
741}
742
5aef535a
TR
743static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
744 AVCodecContext *codec)
745{
746 uint8_t *extradata;
747
748 if (os->ctx->streams[0]->codec->extradata_size || !codec->extradata_size)
749 return 0;
750
751 extradata = av_malloc(codec->extradata_size);
752
753 if (!extradata)
754 return AVERROR(ENOMEM);
755
756 memcpy(extradata, codec->extradata, codec->extradata_size);
757
758 os->ctx->streams[0]->codec->extradata = extradata;
759 os->ctx->streams[0]->codec->extradata_size = codec->extradata_size;
760
761 set_codec_str(s, codec, os->codec_str, sizeof(os->codec_str));
762
763 return 0;
764}
765
fe42f94c 766static int dash_flush(AVFormatContext *s, int final, int stream)
fe5e6e34
MS
767{
768 DASHContext *c = s->priv_data;
769 int i, ret = 0;
fe42f94c
MS
770 int cur_flush_segment_index = 0;
771 if (stream >= 0)
772 cur_flush_segment_index = c->streams[stream].segment_index;
fe5e6e34
MS
773
774 for (i = 0; i < s->nb_streams; i++) {
775 OutputStream *os = &c->streams[i];
776 char filename[1024] = "", full_path[1024], temp_path[1024];
c5e7ea13 777 int64_t start_pos;
fe5e6e34
MS
778 int range_length, index_length = 0;
779
780 if (!os->packets_written)
781 continue;
782
fe42f94c
MS
783 // Flush the single stream that got a keyframe right now.
784 // Flush all audio streams as well, in sync with video keyframes,
785 // but not the other video streams.
786 if (stream >= 0 && i != stream) {
787 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
788 continue;
789 // Make sure we don't flush audio streams multiple times, when
790 // all video streams are flushed one at a time.
791 if (c->has_video && os->segment_index > cur_flush_segment_index)
792 continue;
793 }
794
c5e7ea13
MS
795 if (!os->init_range_length) {
796 av_write_frame(os->ctx, NULL);
797 os->init_range_length = avio_tell(os->ctx->pb);
798 if (!c->single_file) {
799 ffurl_close(os->out);
800 os->out = NULL;
801 }
802 }
803
804 start_pos = avio_tell(os->ctx->pb);
805
fe5e6e34 806 if (!c->single_file) {
7a1a63e3 807 dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
fe5e6e34
MS
808 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
809 snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
675ac56b 810 ret = ffurl_open(&os->out, temp_path, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
fe5e6e34
MS
811 if (ret < 0)
812 break;
813 write_styp(os->ctx->pb);
fa8934d6
BH
814 } else {
815 snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
fe5e6e34 816 }
fa8934d6 817
fe5e6e34
MS
818 av_write_frame(os->ctx, NULL);
819 avio_flush(os->ctx->pb);
820 os->packets_written = 0;
821
822 range_length = avio_tell(os->ctx->pb) - start_pos;
823 if (c->single_file) {
fa8934d6 824 find_index_range(s, full_path, start_pos, &index_length);
fe5e6e34
MS
825 } else {
826 ffurl_close(os->out);
827 os->out = NULL;
675ac56b 828 ret = ff_rename(temp_path, full_path);
fe5e6e34
MS
829 if (ret < 0)
830 break;
831 }
7a1a63e3 832 add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length);
fa8934d6 833 av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
fe5e6e34
MS
834 }
835
836 if (c->window_size || (final && c->remove_at_exit)) {
837 for (i = 0; i < s->nb_streams; i++) {
838 OutputStream *os = &c->streams[i];
839 int j;
840 int remove = os->nb_segments - c->window_size - c->extra_window_size;
841 if (final && c->remove_at_exit)
842 remove = os->nb_segments;
843 if (remove > 0) {
844 for (j = 0; j < remove; j++) {
845 char filename[1024];
846 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
847 unlink(filename);
848 av_free(os->segments[j]);
849 }
850 os->nb_segments -= remove;
851 memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
852 }
853 }
854 }
855
856 if (ret >= 0)
857 ret = write_manifest(s, final);
858 return ret;
859}
860
861static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
862{
863 DASHContext *c = s->priv_data;
864 AVStream *st = s->streams[pkt->stream_index];
865 OutputStream *os = &c->streams[pkt->stream_index];
fe42f94c 866 int64_t seg_end_duration = (os->segment_index) * (int64_t) c->min_seg_duration;
fe5e6e34
MS
867 int ret;
868
5aef535a
TR
869 ret = update_stream_extradata(s, os, st->codec);
870 if (ret < 0)
871 return ret;
872
30411836
MS
873 // Fill in a heuristic guess of the packet duration, if none is available.
874 // The mp4 muxer will do something similar (for the last packet in a fragment)
875 // if nothing is set (setting it for the other packets doesn't hurt).
876 // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
877 // invoke its heuristic (this doesn't have to be identical to that algorithm),
878 // so that we know the exact timestamps of fragments.
879 if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
880 pkt->duration = pkt->dts - os->last_dts;
881 os->last_dts = pkt->dts;
882
fe5e6e34
MS
883 // If forcing the stream to start at 0, the mp4 muxer will set the start
884 // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
7a1a63e3 885 if (os->first_pts == AV_NOPTS_VALUE &&
fe5e6e34
MS
886 s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
887 pkt->pts -= pkt->dts;
888 pkt->dts = 0;
889 }
890
7a1a63e3
MS
891 if (os->first_pts == AV_NOPTS_VALUE)
892 os->first_pts = pkt->pts;
fe5e6e34
MS
893
894 if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
895 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
7a1a63e3 896 av_compare_ts(pkt->pts - os->first_pts, st->time_base,
fe5e6e34
MS
897 seg_end_duration, AV_TIME_BASE_Q) >= 0) {
898 int64_t prev_duration = c->last_duration;
899
7a1a63e3 900 c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
fe5e6e34
MS
901 st->time_base,
902 AV_TIME_BASE_Q);
7a1a63e3 903 c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
fe5e6e34
MS
904 st->time_base,
905 AV_TIME_BASE_Q);
906
907 if ((!c->use_timeline || !c->use_template) && prev_duration) {
908 if (c->last_duration < prev_duration*9/10 ||
909 c->last_duration > prev_duration*11/10) {
910 av_log(s, AV_LOG_WARNING,
911 "Segment durations differ too much, enable use_timeline "
912 "and use_template, or keep a stricter keyframe interval\n");
913 }
914 }
915
fe42f94c 916 if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
fe5e6e34 917 return ret;
fe5e6e34
MS
918 }
919
456e93bf
MS
920 if (!os->packets_written) {
921 // If we wrote a previous segment, adjust the start time of the segment
922 // to the end of the previous one (which is the same as the mp4 muxer
923 // does). This avoids gaps in the timeline.
7a1a63e3
MS
924 if (os->max_pts != AV_NOPTS_VALUE)
925 os->start_pts = os->max_pts;
456e93bf 926 else
7a1a63e3 927 os->start_pts = pkt->pts;
456e93bf 928 }
7a1a63e3
MS
929 if (os->max_pts == AV_NOPTS_VALUE)
930 os->max_pts = pkt->pts + pkt->duration;
931 else
932 os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
fe5e6e34
MS
933 os->packets_written++;
934 return ff_write_chained(os->ctx, 0, pkt, s);
935}
936
937static int dash_write_trailer(AVFormatContext *s)
938{
939 DASHContext *c = s->priv_data;
940
941 if (s->nb_streams > 0) {
942 OutputStream *os = &c->streams[0];
943 // If no segments have been written so far, try to do a crude
944 // guess of the segment duration
945 if (!c->last_duration)
7a1a63e3 946 c->last_duration = av_rescale_q(os->max_pts - os->start_pts,
fe5e6e34
MS
947 s->streams[0]->time_base,
948 AV_TIME_BASE_Q);
7a1a63e3 949 c->total_duration = av_rescale_q(os->max_pts - os->first_pts,
fe5e6e34
MS
950 s->streams[0]->time_base,
951 AV_TIME_BASE_Q);
952 }
fe42f94c 953 dash_flush(s, 1, -1);
fe5e6e34
MS
954
955 if (c->remove_at_exit) {
956 char filename[1024];
957 int i;
958 for (i = 0; i < s->nb_streams; i++) {
959 OutputStream *os = &c->streams[i];
960 snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
961 unlink(filename);
962 }
963 unlink(s->filename);
964 }
965
966 dash_free(s);
967 return 0;
968}
969
970#define OFFSET(x) offsetof(DASHContext, x)
971#define E AV_OPT_FLAG_ENCODING_PARAM
972static const AVOption options[] = {
973 { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
974 { "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 },
975 { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
976 { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
977 { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
978 { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
979 { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
a9d8d35e 980 { "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 },
8d54bacb
MS
981 { "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 },
982 { "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 },
fe5e6e34
MS
983 { NULL },
984};
985
986static const AVClass dash_class = {
987 .class_name = "dash muxer",
988 .item_name = av_default_item_name,
989 .option = options,
990 .version = LIBAVUTIL_VERSION_INT,
991};
992
993AVOutputFormat ff_dash_muxer = {
994 .name = "dash",
995 .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
996 .priv_data_size = sizeof(DASHContext),
997 .audio_codec = AV_CODEC_ID_AAC,
998 .video_codec = AV_CODEC_ID_H264,
999 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE,
1000 .write_header = dash_write_header,
1001 .write_packet = dash_write_packet,
1002 .write_trailer = dash_write_trailer,
1003 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1004 .priv_class = &dash_class,
1005};