FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dashenc.c
Go to the documentation of this file.
1 /*
2  * MPEG-DASH ISO BMFF segmenter
3  * Copyright (c) 2014 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "config.h"
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/rational.h"
34 
35 #include "avc.h"
36 #include "avformat.h"
37 #include "avio_internal.h"
38 #include "internal.h"
39 #include "isom.h"
40 #include "os_support.h"
41 #include "url.h"
42 
43 // See ISO/IEC 23009-1:2014 5.3.9.4.4
44 typedef enum {
51 } DASHTmplId;
52 
53 typedef struct Segment {
54  char file[1024];
55  int64_t start_pos;
57  int64_t time;
58  int duration;
59  int n;
60 } Segment;
61 
62 typedef struct OutputStream {
65  uint8_t iobuf[32768];
68  char initfile[1024];
69  int64_t init_start_pos;
74  int64_t last_dts;
75  int bit_rate;
76  char bandwidth_str[64];
77 
78  char codec_str[100];
79 } OutputStream;
80 
81 typedef struct DASHContext {
82  const AVClass *class; /* Class for private options. */
92  int64_t last_duration;
93  int64_t total_duration;
95  char dirname[1024];
96  const char *single_file_name;
97  const char *init_seg_name;
98  const char *media_seg_name;
101 } DASHContext;
102 
103 static int dash_write(void *opaque, uint8_t *buf, int buf_size)
104 {
105  OutputStream *os = opaque;
106  if (os->out)
107  ffurl_write(os->out, buf, buf_size);
108  return buf_size;
109 }
110 
111 // RFC 6381
113  char *str, int size)
114 {
115  const AVCodecTag *tags[2] = { NULL, NULL };
116  uint32_t tag;
117  if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
118  tags[0] = ff_codec_movvideo_tags;
119  else if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
120  tags[0] = ff_codec_movaudio_tags;
121  else
122  return;
123 
124  tag = av_codec_get_tag(tags, codec->codec_id);
125  if (!tag)
126  return;
127  if (size < 5)
128  return;
129 
130  AV_WL32(str, tag);
131  str[4] = '\0';
132  if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
133  uint32_t oti;
134  tags[0] = ff_mp4_obj_type;
135  oti = av_codec_get_tag(tags, codec->codec_id);
136  if (oti)
137  av_strlcatf(str, size, ".%02x", oti);
138  else
139  return;
140 
141  if (tag == MKTAG('m', 'p', '4', 'a')) {
142  if (codec->extradata_size >= 2) {
143  int aot = codec->extradata[0] >> 3;
144  if (aot == 31)
145  aot = ((AV_RB16(codec->extradata) >> 5) & 0x3f) + 32;
146  av_strlcatf(str, size, ".%d", aot);
147  }
148  } else if (tag == MKTAG('m', 'p', '4', 'v')) {
149  // Unimplemented, should output ProfileLevelIndication as a decimal number
150  av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
151  }
152  } else if (!strcmp(str, "avc1")) {
153  uint8_t *tmpbuf = NULL;
154  uint8_t *extradata = codec->extradata;
155  int extradata_size = codec->extradata_size;
156  if (!extradata_size)
157  return;
158  if (extradata[0] != 1) {
159  AVIOContext *pb;
160  if (avio_open_dyn_buf(&pb) < 0)
161  return;
162  if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
163  ffio_free_dyn_buf(&pb);
164  return;
165  }
166  extradata_size = avio_close_dyn_buf(pb, &extradata);
167  tmpbuf = extradata;
168  }
169 
170  if (extradata_size >= 4)
171  av_strlcatf(str, size, ".%02x%02x%02x",
172  extradata[1], extradata[2], extradata[3]);
173  av_free(tmpbuf);
174  }
175 }
176 
178 {
179  DASHContext *c = s->priv_data;
180  int i, j;
181  if (!c->streams)
182  return;
183  for (i = 0; i < s->nb_streams; i++) {
184  OutputStream *os = &c->streams[i];
185  if (os->ctx && os->ctx_inited)
186  av_write_trailer(os->ctx);
187  if (os->ctx && os->ctx->pb)
188  av_free(os->ctx->pb);
189  ffurl_close(os->out);
190  os->out = NULL;
191  if (os->ctx)
193  for (j = 0; j < os->nb_segments; j++)
194  av_free(os->segments[j]);
195  av_free(os->segments);
196  }
197  av_freep(&c->streams);
198 }
199 
201 {
202  int i, start_index = 0, start_number = 1;
203  if (c->window_size) {
204  start_index = FFMAX(os->nb_segments - c->window_size, 0);
205  start_number = FFMAX(os->segment_index - c->window_size, 1);
206  }
207 
208  if (c->use_template) {
209  int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE;
210  avio_printf(out, "\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
211  if (!c->use_timeline)
212  avio_printf(out, "duration=\"%"PRId64"\" ", c->last_duration);
213  avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\">\n", c->init_seg_name, c->media_seg_name, c->use_timeline ? start_number : 1);
214  if (c->use_timeline) {
215  int64_t cur_time = 0;
216  avio_printf(out, "\t\t\t\t\t<SegmentTimeline>\n");
217  for (i = start_index; i < os->nb_segments; ) {
218  Segment *seg = os->segments[i];
219  int repeat = 0;
220  avio_printf(out, "\t\t\t\t\t\t<S ");
221  if (i == start_index || seg->time != cur_time) {
222  cur_time = seg->time;
223  avio_printf(out, "t=\"%"PRId64"\" ", seg->time);
224  }
225  avio_printf(out, "d=\"%d\" ", seg->duration);
226  while (i + repeat + 1 < os->nb_segments &&
227  os->segments[i + repeat + 1]->duration == seg->duration &&
228  os->segments[i + repeat + 1]->time == os->segments[i + repeat]->time + os->segments[i + repeat]->duration)
229  repeat++;
230  if (repeat > 0)
231  avio_printf(out, "r=\"%d\" ", repeat);
232  avio_printf(out, "/>\n");
233  i += 1 + repeat;
234  cur_time += (1 + repeat) * seg->duration;
235  }
236  avio_printf(out, "\t\t\t\t\t</SegmentTimeline>\n");
237  }
238  avio_printf(out, "\t\t\t\t</SegmentTemplate>\n");
239  } else if (c->single_file) {
240  avio_printf(out, "\t\t\t\t<BaseURL>%s</BaseURL>\n", os->initfile);
241  avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
242  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);
243  for (i = start_index; i < os->nb_segments; i++) {
244  Segment *seg = os->segments[i];
245  avio_printf(out, "\t\t\t\t\t<SegmentURL mediaRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->range_length - 1);
246  if (seg->index_length)
247  avio_printf(out, "indexRange=\"%"PRId64"-%"PRId64"\" ", seg->start_pos, seg->start_pos + seg->index_length - 1);
248  avio_printf(out, "/>\n");
249  }
250  avio_printf(out, "\t\t\t\t</SegmentList>\n");
251  } else {
252  avio_printf(out, "\t\t\t\t<SegmentList timescale=\"%d\" duration=\"%"PRId64"\" startNumber=\"%d\">\n", AV_TIME_BASE, c->last_duration, start_number);
253  avio_printf(out, "\t\t\t\t\t<Initialization sourceURL=\"%s\" />\n", os->initfile);
254  for (i = start_index; i < os->nb_segments; i++) {
255  Segment *seg = os->segments[i];
256  avio_printf(out, "\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->file);
257  }
258  avio_printf(out, "\t\t\t\t</SegmentList>\n");
259  }
260 }
261 
262 static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag,
263  size_t format_tag_size, const char **ptr) {
264  const char *next_ptr;
266 
267  if (av_strstart(identifier, "$$", &next_ptr)) {
268  id_type = DASH_TMPL_ID_ESCAPE;
269  *ptr = next_ptr;
270  } else if (av_strstart(identifier, "$RepresentationID$", &next_ptr)) {
271  id_type = DASH_TMPL_ID_REP_ID;
272  // default to basic format, as $RepresentationID$ identifiers
273  // are not allowed to have custom format-tags.
274  av_strlcpy(format_tag, "%d", format_tag_size);
275  *ptr = next_ptr;
276  } else { // the following identifiers may have an explicit format_tag
277  if (av_strstart(identifier, "$Number", &next_ptr))
278  id_type = DASH_TMPL_ID_NUMBER;
279  else if (av_strstart(identifier, "$Bandwidth", &next_ptr))
280  id_type = DASH_TMPL_ID_BANDWIDTH;
281  else if (av_strstart(identifier, "$Time", &next_ptr))
282  id_type = DASH_TMPL_ID_TIME;
283  else
284  id_type = DASH_TMPL_ID_UNDEFINED;
285 
286  // next parse the dash format-tag and generate a c-string format tag
287  // (next_ptr now points at the first '%' at the beginning of the format-tag)
288  if (id_type != DASH_TMPL_ID_UNDEFINED) {
289  const char *number_format = (id_type == DASH_TMPL_ID_TIME) ? PRId64 : "d";
290  if (next_ptr[0] == '$') { // no dash format-tag
291  snprintf(format_tag, format_tag_size, "%%%s", number_format);
292  *ptr = &next_ptr[1];
293  } else {
294  const char *width_ptr;
295  // only tolerate single-digit width-field (i.e. up to 9-digit width)
296  if (av_strstart(next_ptr, "%0", &width_ptr) &&
297  av_isdigit(width_ptr[0]) &&
298  av_strstart(&width_ptr[1], "d$", &next_ptr)) {
299  // yes, we're using a format tag to build format_tag.
300  snprintf(format_tag, format_tag_size, "%s%c%s", "%0", width_ptr[0], number_format);
301  *ptr = next_ptr;
302  } else {
303  av_log(NULL, AV_LOG_WARNING, "Failed to parse format-tag beginning with %s. Expected either a "
304  "closing '$' character or a format-string like '%%0[width]d', "
305  "where width must be a single digit\n", next_ptr);
306  id_type = DASH_TMPL_ID_UNDEFINED;
307  }
308  }
309  }
310  }
311  return id_type;
312 }
313 
314 static void dash_fill_tmpl_params(char *dst, size_t buffer_size,
315  const char *template, int rep_id,
316  int number, int bit_rate,
317  int64_t time) {
318  int dst_pos = 0;
319  const char *t_cur = template;
320  while (dst_pos < buffer_size - 1 && *t_cur) {
321  char format_tag[7]; // May be "%d", "%0Xd", or "%0Xlld" (for $Time$), where X is in [0-9]
322  int n = 0;
323  DASHTmplId id_type;
324  const char *t_next = strchr(t_cur, '$'); // copy over everything up to the first '$' character
325  if (t_next) {
326  int num_copy_bytes = FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
327  av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
328  // advance
329  dst_pos += num_copy_bytes;
330  t_cur = t_next;
331  } else { // no more DASH identifiers to substitute - just copy the rest over and break
332  av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
333  break;
334  }
335 
336  if (dst_pos >= buffer_size - 1 || !*t_cur)
337  break;
338 
339  // t_cur is now pointing to a '$' character
340  id_type = dash_read_tmpl_id(t_cur, format_tag, sizeof(format_tag), &t_next);
341  switch (id_type) {
342  case DASH_TMPL_ID_ESCAPE:
343  av_strlcpy(&dst[dst_pos], "$", 2);
344  n = 1;
345  break;
346  case DASH_TMPL_ID_REP_ID:
347  n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
348  break;
349  case DASH_TMPL_ID_NUMBER:
350  n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
351  break;
353  n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
354  break;
355  case DASH_TMPL_ID_TIME:
356  n = snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
357  break;
359  // copy over one byte and advance
360  av_strlcpy(&dst[dst_pos], t_cur, 2);
361  n = 1;
362  t_next = &t_cur[1];
363  break;
364  }
365  // t_next points just past the processed identifier
366  // n is the number of bytes that were attempted to be written to dst
367  // (may have failed to write all because buffer_size).
368 
369  // advance
370  dst_pos += FFMIN(n, buffer_size - dst_pos - 1);
371  t_cur = t_next;
372  }
373 }
374 
375 static char *xmlescape(const char *str) {
376  int outlen = strlen(str)*3/2 + 6;
377  char *out = av_realloc(NULL, outlen + 1);
378  int pos = 0;
379  if (!out)
380  return NULL;
381  for (; *str; str++) {
382  if (pos + 6 > outlen) {
383  char *tmp;
384  outlen = 2 * outlen + 6;
385  tmp = av_realloc(out, outlen + 1);
386  if (!tmp) {
387  av_free(out);
388  return NULL;
389  }
390  out = tmp;
391  }
392  if (*str == '&') {
393  memcpy(&out[pos], "&amp;", 5);
394  pos += 5;
395  } else if (*str == '<') {
396  memcpy(&out[pos], "&lt;", 4);
397  pos += 4;
398  } else if (*str == '>') {
399  memcpy(&out[pos], "&gt;", 4);
400  pos += 4;
401  } else if (*str == '\'') {
402  memcpy(&out[pos], "&apos;", 6);
403  pos += 6;
404  } else if (*str == '\"') {
405  memcpy(&out[pos], "&quot;", 6);
406  pos += 6;
407  } else {
408  out[pos++] = *str;
409  }
410  }
411  out[pos] = '\0';
412  return out;
413 }
414 
415 static void write_time(AVIOContext *out, int64_t time)
416 {
417  int seconds = time / AV_TIME_BASE;
418  int fractions = time % AV_TIME_BASE;
419  int minutes = seconds / 60;
420  int hours = minutes / 60;
421  seconds %= 60;
422  minutes %= 60;
423  avio_printf(out, "PT");
424  if (hours)
425  avio_printf(out, "%dH", hours);
426  if (hours || minutes)
427  avio_printf(out, "%dM", minutes);
428  avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10));
429 }
430 
431 static void format_date_now(char *buf, int size)
432 {
433  time_t t = time(NULL);
434  struct tm *ptm, tmbuf;
435  ptm = gmtime_r(&t, &tmbuf);
436  if (ptm) {
437  if (!strftime(buf, size, "%Y-%m-%dT%H:%M:%S", ptm))
438  buf[0] = '\0';
439  }
440 }
441 
442 static int write_manifest(AVFormatContext *s, int final)
443 {
444  DASHContext *c = s->priv_data;
445  AVIOContext *out;
446  char temp_filename[1024];
447  int ret, i;
448  AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
449 
450  snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
451  ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
452  if (ret < 0) {
453  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
454  return ret;
455  }
456  avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
457  avio_printf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
458  "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
459  "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
460  "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
461  "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
462  "\ttype=\"%s\"\n", final ? "static" : "dynamic");
463  if (final) {
464  avio_printf(out, "\tmediaPresentationDuration=\"");
465  write_time(out, c->total_duration);
466  avio_printf(out, "\"\n");
467  } else {
468  int64_t update_period = c->last_duration / AV_TIME_BASE;
469  char now_str[100];
470  if (c->use_template && !c->use_timeline)
471  update_period = 500;
472  avio_printf(out, "\tminimumUpdatePeriod=\"PT%"PRId64"S\"\n", update_period);
473  avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE);
474  if (!c->availability_start_time[0] && s->nb_streams > 0 && c->streams[0].nb_segments > 0) {
476  }
477  if (c->availability_start_time[0])
478  avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time);
479  format_date_now(now_str, sizeof(now_str));
480  if (now_str[0])
481  avio_printf(out, "\tpublishTime=\"%s\"\n", now_str);
482  if (c->window_size && c->use_template) {
483  avio_printf(out, "\ttimeShiftBufferDepth=\"");
484  write_time(out, c->last_duration * c->window_size);
485  avio_printf(out, "\"\n");
486  }
487  }
488  avio_printf(out, "\tminBufferTime=\"");
489  write_time(out, c->last_duration);
490  avio_printf(out, "\">\n");
491  avio_printf(out, "\t<ProgramInformation>\n");
492  if (title) {
493  char *escaped = xmlescape(title->value);
494  avio_printf(out, "\t\t<Title>%s</Title>\n", escaped);
495  av_free(escaped);
496  }
497  avio_printf(out, "\t</ProgramInformation>\n");
498  if (c->window_size && s->nb_streams > 0 && c->streams[0].nb_segments > 0 && !c->use_template) {
499  OutputStream *os = &c->streams[0];
500  int start_index = FFMAX(os->nb_segments - c->window_size, 0);
501  int64_t start_time = av_rescale_q(os->segments[start_index]->time, s->streams[0]->time_base, AV_TIME_BASE_Q);
502  avio_printf(out, "\t<Period start=\"");
503  write_time(out, start_time);
504  avio_printf(out, "\">\n");
505  } else {
506  avio_printf(out, "\t<Period start=\"PT0.0S\">\n");
507  }
508 
509  if (c->has_video) {
510  avio_printf(out, "\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\"");
512  avio_printf(out, " %s=\"%d/%d\"", (av_cmp_q(c->min_frame_rate, c->max_frame_rate) < 0) ? "maxFrameRate" : "frameRate", c->max_frame_rate.num, c->max_frame_rate.den);
513  avio_printf(out, ">\n");
514 
515  for (i = 0; i < s->nb_streams; i++) {
516  AVStream *st = s->streams[i];
517  OutputStream *os = &c->streams[i];
518 
519  if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO)
520  continue;
521 
522  avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"", i, os->codec_str, os->bandwidth_str, st->codec->width, st->codec->height);
523  if (st->avg_frame_rate.num)
524  avio_printf(out, " frameRate=\"%d/%d\"", st->avg_frame_rate.num, st->avg_frame_rate.den);
525  avio_printf(out, ">\n");
526 
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  if (c->has_audio) {
533  avio_printf(out, "\t\t<AdaptationSet contentType=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
534  for (i = 0; i < s->nb_streams; i++) {
535  AVStream *st = s->streams[i];
536  OutputStream *os = &c->streams[i];
537 
538  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
539  continue;
540 
541  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);
542  avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codec->channels);
543  output_segment_list(&c->streams[i], out, c);
544  avio_printf(out, "\t\t\t</Representation>\n");
545  }
546  avio_printf(out, "\t\t</AdaptationSet>\n");
547  }
548  avio_printf(out, "\t</Period>\n");
549  avio_printf(out, "</MPD>\n");
550  avio_flush(out);
551  ff_format_io_close(s, &out);
552  return ff_rename(temp_filename, s->filename, s);
553 }
554 
556 {
557  DASHContext *c = s->priv_data;
558  int ret = 0, i;
559  AVOutputFormat *oformat;
560  char *ptr;
561  char basename[1024];
562 
563  if (c->single_file_name)
564  c->single_file = 1;
565  if (c->single_file)
566  c->use_template = 0;
567  c->ambiguous_frame_rate = 0;
568 
569  av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
570  ptr = strrchr(c->dirname, '/');
571  if (ptr) {
572  av_strlcpy(basename, &ptr[1], sizeof(basename));
573  ptr[1] = '\0';
574  } else {
575  c->dirname[0] = '\0';
576  av_strlcpy(basename, s->filename, sizeof(basename));
577  }
578 
579  ptr = strrchr(basename, '.');
580  if (ptr)
581  *ptr = '\0';
582 
583  oformat = av_guess_format("mp4", NULL, NULL);
584  if (!oformat) {
586  goto fail;
587  }
588 
589  c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
590  if (!c->streams) {
591  ret = AVERROR(ENOMEM);
592  goto fail;
593  }
594 
595  for (i = 0; i < s->nb_streams; i++) {
596  OutputStream *os = &c->streams[i];
598  AVStream *st;
600  char filename[1024];
601 
602  os->bit_rate = s->streams[i]->codec->bit_rate ?
603  s->streams[i]->codec->bit_rate :
604  s->streams[i]->codec->rc_max_rate;
605  if (os->bit_rate) {
606  snprintf(os->bandwidth_str, sizeof(os->bandwidth_str),
607  " bandwidth=\"%d\"", os->bit_rate);
608  } else {
611  av_log(s, level, "No bit rate set for stream %d\n", i);
613  ret = AVERROR(EINVAL);
614  goto fail;
615  }
616  }
617 
618  ctx = avformat_alloc_context();
619  if (!ctx) {
620  ret = AVERROR(ENOMEM);
621  goto fail;
622  }
623  os->ctx = ctx;
624  ctx->oformat = oformat;
626  ctx->opaque = s->opaque;
627  ctx->io_close = s->io_close;
628  ctx->io_open = s->io_open;
629 
630  if (!(st = avformat_new_stream(ctx, NULL))) {
631  ret = AVERROR(ENOMEM);
632  goto fail;
633  }
636  st->time_base = s->streams[i]->time_base;
638 
639  ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL);
640  if (!ctx->pb) {
641  ret = AVERROR(ENOMEM);
642  goto fail;
643  }
644 
645  if (c->single_file) {
646  if (c->single_file_name)
647  dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->single_file_name, i, 0, os->bit_rate, 0);
648  else
649  snprintf(os->initfile, sizeof(os->initfile), "%s-stream%d.m4s", basename, i);
650  } else {
651  dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), c->init_seg_name, i, 0, os->bit_rate, 0);
652  }
653  snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
655  if (ret < 0)
656  goto fail;
657  os->init_start_pos = 0;
658 
659  av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0);
660  if ((ret = avformat_write_header(ctx, &opts)) < 0) {
661  goto fail;
662  }
663  os->ctx_inited = 1;
664  avio_flush(ctx->pb);
665  av_dict_free(&opts);
666 
667  av_log(s, AV_LOG_VERBOSE, "Representation %d init segment will be written to: %s\n", i, filename);
668 
669  s->streams[i]->time_base = st->time_base;
670  // If the muxer wants to shift timestamps, request to have them shifted
671  // already before being handed to this muxer, so we don't have mismatches
672  // between the MPD and the actual segments.
674  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
675  AVRational avg_frame_rate = s->streams[i]->avg_frame_rate;
676  if (avg_frame_rate.num > 0) {
677  if (av_cmp_q(avg_frame_rate, c->min_frame_rate) < 0)
678  c->min_frame_rate = avg_frame_rate;
679  if (av_cmp_q(c->max_frame_rate, avg_frame_rate) < 0)
680  c->max_frame_rate = avg_frame_rate;
681  } else {
682  c->ambiguous_frame_rate = 1;
683  }
684  c->has_video = 1;
685  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
686  c->has_audio = 1;
687  }
688 
689  set_codec_str(s, st->codec, os->codec_str, sizeof(os->codec_str));
691  os->max_pts = AV_NOPTS_VALUE;
692  os->last_dts = AV_NOPTS_VALUE;
693  os->segment_index = 1;
694  }
695 
696  if (!c->has_video && c->min_seg_duration <= 0) {
697  av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n");
698  ret = AVERROR(EINVAL);
699  }
700  ret = write_manifest(s, 0);
701  if (!ret)
702  av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
703 
704 fail:
705  if (ret)
706  dash_free(s);
707  return ret;
708 }
709 
710 static int add_segment(OutputStream *os, const char *file,
711  int64_t time, int duration,
712  int64_t start_pos, int64_t range_length,
713  int64_t index_length)
714 {
715  int err;
716  Segment *seg;
717  if (os->nb_segments >= os->segments_size) {
718  os->segments_size = (os->segments_size + 1) * 2;
719  if ((err = av_reallocp(&os->segments, sizeof(*os->segments) *
720  os->segments_size)) < 0) {
721  os->segments_size = 0;
722  os->nb_segments = 0;
723  return err;
724  }
725  }
726  seg = av_mallocz(sizeof(*seg));
727  if (!seg)
728  return AVERROR(ENOMEM);
729  av_strlcpy(seg->file, file, sizeof(seg->file));
730  seg->time = time;
731  seg->duration = duration;
732  if (seg->time < 0) { // If pts<0, it is expected to be cut away with an edit list
733  seg->duration += seg->time;
734  seg->time = 0;
735  }
736  seg->start_pos = start_pos;
737  seg->range_length = range_length;
738  seg->index_length = index_length;
739  os->segments[os->nb_segments++] = seg;
740  os->segment_index++;
741  return 0;
742 }
743 
744 static void write_styp(AVIOContext *pb)
745 {
746  avio_wb32(pb, 24);
747  ffio_wfourcc(pb, "styp");
748  ffio_wfourcc(pb, "msdh");
749  avio_wb32(pb, 0); /* minor */
750  ffio_wfourcc(pb, "msdh");
751  ffio_wfourcc(pb, "msix");
752 }
753 
754 static void find_index_range(AVFormatContext *s, const char *full_path,
755  int64_t pos, int *index_length)
756 {
757  uint8_t buf[8];
758  URLContext *fd;
759  int ret;
760 
762  if (ret < 0)
763  return;
764  if (ffurl_seek(fd, pos, SEEK_SET) != pos) {
765  ffurl_close(fd);
766  return;
767  }
768  ret = ffurl_read(fd, buf, 8);
769  ffurl_close(fd);
770  if (ret < 8)
771  return;
772  if (AV_RL32(&buf[4]) != MKTAG('s', 'i', 'd', 'x'))
773  return;
774  *index_length = AV_RB32(&buf[0]);
775 }
776 
778  AVCodecContext *codec)
779 {
780  uint8_t *extradata;
781 
782  if (os->ctx->streams[0]->codec->extradata_size || !codec->extradata_size)
783  return 0;
784 
785  extradata = av_malloc(codec->extradata_size);
786 
787  if (!extradata)
788  return AVERROR(ENOMEM);
789 
790  memcpy(extradata, codec->extradata, codec->extradata_size);
791 
792  os->ctx->streams[0]->codec->extradata = extradata;
793  os->ctx->streams[0]->codec->extradata_size = codec->extradata_size;
794 
795  set_codec_str(s, codec, os->codec_str, sizeof(os->codec_str));
796 
797  return 0;
798 }
799 
800 static int dash_flush(AVFormatContext *s, int final, int stream)
801 {
802  DASHContext *c = s->priv_data;
803  int i, ret = 0;
804  int cur_flush_segment_index = 0;
805  if (stream >= 0)
806  cur_flush_segment_index = c->streams[stream].segment_index;
807 
808  for (i = 0; i < s->nb_streams; i++) {
809  OutputStream *os = &c->streams[i];
810  char filename[1024] = "", full_path[1024], temp_path[1024];
811  int64_t start_pos;
812  int range_length, index_length = 0;
813 
814  if (!os->packets_written)
815  continue;
816 
817  // Flush the single stream that got a keyframe right now.
818  // Flush all audio streams as well, in sync with video keyframes,
819  // but not the other video streams.
820  if (stream >= 0 && i != stream) {
822  continue;
823  // Make sure we don't flush audio streams multiple times, when
824  // all video streams are flushed one at a time.
825  if (c->has_video && os->segment_index > cur_flush_segment_index)
826  continue;
827  }
828 
829  if (!os->init_range_length) {
830  av_write_frame(os->ctx, NULL);
831  os->init_range_length = avio_tell(os->ctx->pb);
832  if (!c->single_file) {
833  ffurl_close(os->out);
834  os->out = NULL;
835  }
836  }
837 
838  start_pos = avio_tell(os->ctx->pb);
839 
840  if (!c->single_file) {
841  dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts);
842  snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename);
843  snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path);
845  if (ret < 0)
846  break;
847  write_styp(os->ctx->pb);
848  } else {
849  snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, os->initfile);
850  }
851 
852  av_write_frame(os->ctx, NULL);
853  avio_flush(os->ctx->pb);
854  os->packets_written = 0;
855 
856  range_length = avio_tell(os->ctx->pb) - start_pos;
857  if (c->single_file) {
858  find_index_range(s, full_path, start_pos, &index_length);
859  } else {
860  ffurl_close(os->out);
861  os->out = NULL;
862  ret = ff_rename(temp_path, full_path, s);
863  if (ret < 0)
864  break;
865  }
866  add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length);
867  av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path);
868  }
869 
870  if (c->window_size || (final && c->remove_at_exit)) {
871  for (i = 0; i < s->nb_streams; i++) {
872  OutputStream *os = &c->streams[i];
873  int j;
874  int remove = os->nb_segments - c->window_size - c->extra_window_size;
875  if (final && c->remove_at_exit)
876  remove = os->nb_segments;
877  if (remove > 0) {
878  for (j = 0; j < remove; j++) {
879  char filename[1024];
880  snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
881  unlink(filename);
882  av_free(os->segments[j]);
883  }
884  os->nb_segments -= remove;
885  memmove(os->segments, os->segments + remove, os->nb_segments * sizeof(*os->segments));
886  }
887  }
888  }
889 
890  if (ret >= 0)
891  ret = write_manifest(s, final);
892  return ret;
893 }
894 
896 {
897  DASHContext *c = s->priv_data;
898  AVStream *st = s->streams[pkt->stream_index];
899  OutputStream *os = &c->streams[pkt->stream_index];
900  int64_t seg_end_duration = (os->segment_index) * (int64_t) c->min_seg_duration;
901  int ret;
902 
903  ret = update_stream_extradata(s, os, st->codec);
904  if (ret < 0)
905  return ret;
906 
907  // Fill in a heuristic guess of the packet duration, if none is available.
908  // The mp4 muxer will do something similar (for the last packet in a fragment)
909  // if nothing is set (setting it for the other packets doesn't hurt).
910  // By setting a nonzero duration here, we can be sure that the mp4 muxer won't
911  // invoke its heuristic (this doesn't have to be identical to that algorithm),
912  // so that we know the exact timestamps of fragments.
913  if (!pkt->duration && os->last_dts != AV_NOPTS_VALUE)
914  pkt->duration = pkt->dts - os->last_dts;
915  os->last_dts = pkt->dts;
916 
917  // If forcing the stream to start at 0, the mp4 muxer will set the start
918  // timestamps to 0. Do the same here, to avoid mismatches in duration/timestamps.
919  if (os->first_pts == AV_NOPTS_VALUE &&
921  pkt->pts -= pkt->dts;
922  pkt->dts = 0;
923  }
924 
925  if (os->first_pts == AV_NOPTS_VALUE)
926  os->first_pts = pkt->pts;
927 
928  if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
929  pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
930  av_compare_ts(pkt->pts - os->first_pts, st->time_base,
931  seg_end_duration, AV_TIME_BASE_Q) >= 0) {
932  int64_t prev_duration = c->last_duration;
933 
934  c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
935  st->time_base,
937  c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
938  st->time_base,
940 
941  if ((!c->use_timeline || !c->use_template) && prev_duration) {
942  if (c->last_duration < prev_duration*9/10 ||
943  c->last_duration > prev_duration*11/10) {
945  "Segment durations differ too much, enable use_timeline "
946  "and use_template, or keep a stricter keyframe interval\n");
947  }
948  }
949 
950  if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0)
951  return ret;
952  }
953 
954  if (!os->packets_written) {
955  // If we wrote a previous segment, adjust the start time of the segment
956  // to the end of the previous one (which is the same as the mp4 muxer
957  // does). This avoids gaps in the timeline.
958  if (os->max_pts != AV_NOPTS_VALUE)
959  os->start_pts = os->max_pts;
960  else
961  os->start_pts = pkt->pts;
962  }
963  if (os->max_pts == AV_NOPTS_VALUE)
964  os->max_pts = pkt->pts + pkt->duration;
965  else
966  os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
967  os->packets_written++;
968  return ff_write_chained(os->ctx, 0, pkt, s, 0);
969 }
970 
972 {
973  DASHContext *c = s->priv_data;
974 
975  if (s->nb_streams > 0) {
976  OutputStream *os = &c->streams[0];
977  // If no segments have been written so far, try to do a crude
978  // guess of the segment duration
979  if (!c->last_duration)
981  s->streams[0]->time_base,
984  s->streams[0]->time_base,
986  }
987  dash_flush(s, 1, -1);
988 
989  if (c->remove_at_exit) {
990  char filename[1024];
991  int i;
992  for (i = 0; i < s->nb_streams; i++) {
993  OutputStream *os = &c->streams[i];
994  snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
995  unlink(filename);
996  }
997  unlink(s->filename);
998  }
999 
1000  dash_free(s);
1001  return 0;
1002 }
1003 
1004 #define OFFSET(x) offsetof(DASHContext, x)
1005 #define E AV_OPT_FLAG_ENCODING_PARAM
1006 static const AVOption options[] = {
1007  { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
1008  { "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 },
1009  { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
1010  { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
1011  { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
1012  { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
1013  { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
1014  { "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 },
1015  { "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 },
1016  { "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 },
1017  { NULL },
1018 };
1019 
1020 static const AVClass dash_class = {
1021  .class_name = "dash muxer",
1022  .item_name = av_default_item_name,
1023  .option = options,
1024  .version = LIBAVUTIL_VERSION_INT,
1025 };
1026 
1028  .name = "dash",
1029  .long_name = NULL_IF_CONFIG_SMALL("DASH Muxer"),
1030  .priv_data_size = sizeof(DASHContext),
1031  .audio_codec = AV_CODEC_ID_AAC,
1032  .video_codec = AV_CODEC_ID_H264,
1037  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1038  .priv_class = &dash_class,
1039 };
AVRational max_frame_rate
Definition: dashenc.c:99
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
int use_timeline
Definition: dashenc.c:88
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:206
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1566
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1168
AVOption.
Definition: opt.h:245
int min_seg_duration
Definition: dashenc.c:85
static const AVOption options[]
Definition: dashenc.c:1006
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:777
AVFormatContext * ctx
Definition: movenc-test.c:48
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
int range_length
Definition: dashenc.c:56
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:433
static int64_t cur_time
Definition: ffserver.c:262
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:1152
int nb_segments
Definition: dashenc.c:71
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:949
int num
numerator
Definition: rational.h:44
#define AVIO_FLAG_READ
read-only
Definition: avio.h:537
int n
Definition: dashenc.c:59
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:538
char codec_str[100]
Definition: dashenc.c:78
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
AVFormatContext * ctx
Definition: dashenc.c:63
int single_file
Definition: dashenc.c:89
static AVPacket pkt
char availability_start_time[100]
Definition: dashenc.c:94
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
Definition: dashenc.c:200
int packets_written
Definition: dashenc.c:67
int64_t start_pts
Definition: dashenc.c:73
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1156
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:182
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1596
int64_t last_dts
Definition: dashenc.c:74
Format I/O context.
Definition: avformat.h:1314
int64_t init_start_pos
Definition: dashenc.c:69
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
static int64_t start_time
Definition: ffplay.c:330
uint8_t
#define av_malloc(s)
AVOptions.
miscellaneous OS support macros and functions.
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:71
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:4736
int extra_window_size
Definition: dashenc.c:84
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
int ctx_inited
Definition: dashenc.c:64
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:132
uint8_t iobuf[32768]
Definition: dashenc.c:65
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
char * protocol_whitelist
',' separated list of allowed protocols.
Definition: avformat.h:1850
uint32_t tag
Definition: movenc.c:1348
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
AVRational min_frame_rate
Definition: dashenc.c:99
static void set_codec_str(AVFormatContext *s, AVCodecContext *codec, char *str, int size)
Definition: dashenc.c:112
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
char initfile[1024]
Definition: dashenc.c:68
static void format_date_now(char *buf, int size)
Definition: dashenc.c:431
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1333
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
static void write_time(AVIOContext *out, int64_t time)
Definition: dashenc.c:415
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
static int write_manifest(AVFormatContext *s, int final)
Definition: dashenc.c:442
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:116
#define E
Definition: dashenc.c:1005
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1528
int remove_at_exit
Definition: dashenc.c:86
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int64_t last_duration
Definition: dashenc.c:92
static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dashenc.c:895
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int duration
Definition: dashenc.c:58
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t start_pos
Definition: dashenc.c:55
URLContext * out
Definition: dashenc.c:66
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:960
#define FFMAX(a, b)
Definition: common.h:94
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:80
char bandwidth_str[64]
Definition: dashenc.c:76
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:147
void * opaque
User data.
Definition: avformat.h:1794
AVOutputFormat ff_dash_muxer
Definition: dashenc.c:1027
int ambiguous_frame_rate
Definition: dashenc.c:100
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
const char * media_seg_name
Definition: dashenc.c:98
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:202
char filename[1024]
input or output filename
Definition: avformat.h:1390
static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag, size_t format_tag_size, const char **ptr)
Definition: dashenc.c:262
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:451
#define FFMIN(a, b)
Definition: common.h:96
int segment_index
Definition: dashenc.c:71
int width
picture width / height.
Definition: avcodec.h:1711
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:484
const char * name
Definition: avformat.h:523
int64_t duration
Definition: movenc-test.c:63
char dirname[1024]
Definition: dashenc.c:95
static int update_stream_extradata(AVFormatContext *s, OutputStream *os, AVCodecContext *codec)
Definition: dashenc.c:777
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1619
int n
Definition: avisynth_c.h:547
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:94
static int dash_write(void *opaque, uint8_t *buf, int buf_size)
Definition: dashenc.c:103
DASHTmplId
Definition: dashenc.c:44
int segments_size
Definition: dashenc.c:71
#define OFFSET(x)
Definition: dashenc.c:1004
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1196
FILE * out
Definition: movenc-test.c:54
Stream structure.
Definition: avformat.h:877
int64_t time
Definition: dashenc.c:57
Segment ** segments
Definition: dashenc.c:72
int index_length
Definition: dashenc.c:56
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:336
enum AVMediaType codec_type
Definition: avcodec.h:1540
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:275
enum AVCodecID codec_id
Definition: avcodec.h:1549
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
int bit_rate
Definition: dashenc.c:75
main external API structure.
Definition: avcodec.h:1532
int use_template
Definition: dashenc.c:87
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
int extradata_size
Definition: avcodec.h:1648
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
static int ff_rename(const char *oldpath, const char *newpath, void *logctx)
Wrap errno on rename() error.
Definition: internal.h:480
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * init_seg_name
Definition: dashenc.c:97
rational number numerator/denominator
Definition: rational.h:43
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:2743
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3741
int64_t total_duration
Definition: dashenc.c:93
static void find_index_range(AVFormatContext *s, const char *full_path, int64_t pos, int *index_length)
Definition: dashenc.c:754
char file[1024]
Definition: dashenc.c:54
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:479
uint8_t level
Definition: svq3.c:150
OutputStream * streams
Definition: dashenc.c:90
int init_range_length
Definition: dashenc.c:70
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1622
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:477
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
Definition: avio.c:446
rational numbers
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
const char * single_file_name
Definition: dashenc.c:96
static double c[64]
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
int den
denominator
Definition: rational.h:45
static void write_styp(AVIOContext *pb)
Definition: dashenc.c:744
static int dash_write_header(AVFormatContext *s)
Definition: dashenc.c:555
#define av_free(p)
char * value
Definition: dict.h:88
int channels
number of audio channels
Definition: avcodec.h:2288
void * priv_data
Format private data.
Definition: avformat.h:1342
AVDictionary * opts
Definition: movenc-test.c:50
static int dash_write_trailer(AVFormatContext *s)
Definition: dashenc.c:971
int window_size
Definition: dashenc.c:83
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
static void dash_free(AVFormatContext *s)
Definition: dashenc.c:177
int has_video
Definition: dashenc.c:91
int64_t max_pts
Definition: dashenc.c:73
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1466
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1083
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:332
#define av_freep(p)
int has_audio
Definition: dashenc.c:91
unbuffered private I/O API
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
static void dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time)
Definition: dashenc.c:314
static char * xmlescape(const char *str)
Definition: dashenc.c:375
static int dash_flush(AVFormatContext *s, int final, int stream)
Definition: dashenc.c:800
int stream_index
Definition: avcodec.h:1469
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:919
int64_t first_pts
Definition: ffmpeg.h:410
#define MKTAG(a, b, c, d)
Definition: common.h:342
unsigned int av_codec_get_tag(const struct AVCodecTag *const *tags, enum AVCodecID id)
Get the codec tag for the given codec id id.
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:500
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
Definition: avformat.h:1872
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1444
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:419
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1878
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
static const AVClass dash_class
Definition: dashenc.c:1020
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2523
static int add_segment(OutputStream *os, const char *file, int64_t time, int duration, int64_t start_pos, int64_t range_length, int64_t index_length)
Definition: dashenc.c:710