Libav
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 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"
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 {
49 } DASHTmplId;
50 
51 typedef struct Segment {
52  char file[1024];
53  int64_t start_pos;
55  int64_t time;
56  int duration;
57  int n;
58 } Segment;
59 
60 typedef struct OutputStream {
63  uint8_t iobuf[32768];
66  char initfile[1024];
67  int64_t init_start_pos;
69  int nb_segments, segments_size, segment_index;
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. */
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
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)
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 
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;
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;
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;
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) {
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];
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];
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 
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) {
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];
580  AVStream *st;
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 {
591  av_log(s, level, "No bit rate set for stream %d\n", i);
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;
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  }
616  st->time_base = s->streams[i]->time_base;
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.
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));
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 
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;
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) {
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 
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 &&
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,
904  c->total_duration = av_rescale_q(pkt->pts - os->first_pts,
905  st->time_base,
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) {
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 
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)
948  s->streams[0]->time_base,
951  s->streams[0]->time_base,
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 
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,
1004  .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
1005  .priv_class = &dash_class,
1006 };
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:104
int use_timeline
Definition: dashenc.c:86
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:157
int size
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1155
AVOption.
Definition: opt.h:234
int min_seg_duration
Definition: dashenc.c:83
static const AVOption options[]
Definition: dashenc.c:973
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:252
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:470
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int range_length
Definition: dashenc.c:54
int nb_segments
Definition: dashenc.c:69
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
int n
Definition: dashenc.c:57
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
char codec_str[100]
Definition: dashenc.c:76
AVFormatContext * ctx
Definition: dashenc.c:61
int single_file
Definition: dashenc.c:87
char availability_start_time[100]
Definition: dashenc.c:92
static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
Definition: dashenc.c:195
int packets_written
Definition: dashenc.c:65
int64_t start_pts
Definition: dashenc.c:71
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1143
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1211
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
int64_t last_dts
Definition: dashenc.c:72
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
int64_t init_start_pos
Definition: dashenc.c:67
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:39
uint8_t
int width
Video only.
Definition: avcodec.h:3525
AVOptions.
miscellaneous OS support macros and functions.
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:70
#define AV_RB32
Definition: intreadwrite.h:130
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
int extra_window_size
Definition: dashenc.c:82
int ctx_inited
Definition: dashenc.c:62
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
uint8_t iobuf[32768]
Definition: dashenc.c:63
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:38
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int flags
Definition: log.c:50
uint32_t tag
Definition: movenc.c:854
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
char initfile[1024]
Definition: dashenc.c:66
static void format_date_now(char *buf, int size)
Definition: dashenc.c:426
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:959
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
static void write_time(AVIOContext *out, int64_t time)
Definition: dashenc.c:410
static int ff_rename(const char *oldpath, const char *newpath)
Wrap errno on rename() error.
Definition: internal.h:431
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
static int write_manifest(AVFormatContext *s, int final)
Definition: dashenc.c:437
uint8_t iobuf[32768]
Definition: movenc.c:49
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:151
#define E
Definition: dashenc.c:972
static int64_t start_time
Definition: avplay.c:245
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int remove_at_exit
Definition: dashenc.c:84
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
int64_t last_duration
Definition: dashenc.c:90
static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dashenc.c:862
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int duration
Definition: dashenc.c:56
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
int64_t start_pos
Definition: dashenc.c:53
#define FFMAX(a, b)
Definition: common.h:64
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:81
#define fail()
Definition: checkasm.h:80
char bandwidth_str[64]
Definition: dashenc.c:74
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:1352
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:104
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
void * opaque
Arbitrary user data set by the caller.
Definition: avformat.h:1248
AVOutputFormat ff_dash_muxer
Definition: dashenc.c:994
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
AVDictionary * opts
Definition: movenc.c:50
const char * media_seg_name
Definition: dashenc.c:96
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:236
char filename[1024]
input or output filename
Definition: avformat.h:1016
static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag, size_t format_tag_size, const char **ptr)
Definition: dashenc.c:257
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
#define FFMIN(a, b)
Definition: common.h:66
int segment_index
Definition: dashenc.c:69
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:419
const char * name
Definition: avformat.h:450
AVFormatContext * ctx
Definition: movenc.c:48
char dirname[1024]
Definition: dashenc.c:93
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1234
#define AV_RL32
Definition: intreadwrite.h:146
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:104
static int dash_write(void *opaque, uint8_t *buf, int buf_size)
Definition: dashenc.c:99
DASHTmplId
Definition: dashenc.c:42
int segments_size
Definition: dashenc.c:69
#define OFFSET(x)
Definition: dashenc.c:971
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1183
Stream structure.
Definition: avformat.h:705
int64_t time
Definition: dashenc.c:55
Segment ** segments
Definition: dashenc.c:70
NULL
Definition: eval.c:55
int index_length
Definition: dashenc.c:54
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:271
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
AVIOContext * pb
I/O context.
Definition: avformat.h:982
int bit_rate
Definition: dashenc.c:73
av_default_item_name
Definition: dnxhdenc.c:55
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
int use_template
Definition: dashenc.c:85
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1276
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:68
static void set_codec_str(AVFormatContext *s, AVCodecParameters *par, char *str, int size)
Definition: dashenc.c:108
Describe the class of an AVClass context structure.
Definition: log.h:34
const char * init_seg_name
Definition: dashenc.c:95
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:2607
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
int64_t total_duration
Definition: dashenc.c:91
static void find_index_range(AVFormatContext *s, const char *full_path, int64_t pos, int *index_length)
Definition: dashenc.c:724
char file[1024]
Definition: dashenc.c:52
uint8_t level
Definition: svq3.c:204
OutputStream * streams
Definition: dashenc.c:88
int init_range_length
Definition: dashenc.c:68
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO
Shift timestamps so that they start at 0.
Definition: avformat.h:1237
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:32
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:412
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
const char * single_file_name
Definition: dashenc.c:94
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:755
int den
denominator
Definition: rational.h:45
static void write_styp(AVIOContext *pb)
Definition: dashenc.c:714
static int dash_write_header(AVFormatContext *s)
Definition: dashenc.c:538
char * value
Definition: dict.h:74
static uint8_t tmp[8]
Definition: des.c:38
void * priv_data
Format private data.
Definition: avformat.h:968
static int dash_write_trailer(AVFormatContext *s)
Definition: dashenc.c:938
int window_size
Definition: dashenc.c:81
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
static void dash_free(AVFormatContext *s)
Definition: dashenc.c:173
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int has_video
Definition: dashenc.c:89
int channels
Audio only.
Definition: avcodec.h:3560
int64_t max_pts
Definition: dashenc.c:71
#define AV_WL32(p, val)
Definition: intreadwrite.h:263
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:714
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
FILE * out
Definition: movenc.c:54
static int update_stream_extradata(AVFormatContext *s, OutputStream *os, AVCodecParameters *par)
Definition: dashenc.c:747
int has_audio
Definition: dashenc.c:89
unbuffered private I/O API
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:55
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:309
AVCodecParameters * codecpar
Definition: avformat.h:831
static char * xmlescape(const char *str)
Definition: dashenc.c:370
AVIOContext * out
Definition: dashenc.c:64
static int dash_flush(AVFormatContext *s, int final, int stream)
Definition: dashenc.c:770
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
int64_t first_pts
Definition: avconv.h:356
#define MKTAG(a, b, c, d)
Definition: common.h:256
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:435
This structure stores compressed data.
Definition: avcodec.h:1323
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:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
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:987
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
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:680