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