FFmpeg  4.1.11
dashdec.c
Go to the documentation of this file.
1 /*
2  * Dynamic Adaptive Streaming over HTTP demux
3  * Copyright (c) 2017 samsamsam@o2.pl based on HLS demux
4  * Copyright (c) 2017 Steven Liu
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include <libxml/parser.h>
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/parseutils.h"
27 #include "internal.h"
28 #include "avio_internal.h"
29 #include "dash.h"
30 
31 #define INITIAL_BUFFER_SIZE 32768
32 
33 struct fragment {
34  int64_t url_offset;
35  int64_t size;
36  char *url;
37 };
38 
39 /*
40  * reference to : ISO_IEC_23009-1-DASH-2012
41  * Section: 5.3.9.6.2
42  * Table: Table 17 — Semantics of SegmentTimeline element
43  * */
44 struct timeline {
45  /* starttime: Element or Attribute Name
46  * specifies the MPD start time, in @timescale units,
47  * the first Segment in the series starts relative to the beginning of the Period.
48  * The value of this attribute must be equal to or greater than the sum of the previous S
49  * element earliest presentation time and the sum of the contiguous Segment durations.
50  * If the value of the attribute is greater than what is expressed by the previous S element,
51  * it expresses discontinuities in the timeline.
52  * If not present then the value shall be assumed to be zero for the first S element
53  * and for the subsequent S elements, the value shall be assumed to be the sum of
54  * the previous S element's earliest presentation time and contiguous duration
55  * (i.e. previous S@starttime + @duration * (@repeat + 1)).
56  * */
57  int64_t starttime;
58  /* repeat: Element or Attribute Name
59  * specifies the repeat count of the number of following contiguous Segments with
60  * the same duration expressed by the value of @duration. This value is zero-based
61  * (e.g. a value of three means four Segments in the contiguous series).
62  * */
63  int64_t repeat;
64  /* duration: Element or Attribute Name
65  * specifies the Segment duration, in units of the value of the @timescale.
66  * */
67  int64_t duration;
68 };
69 
70 /*
71  * Each playlist has its own demuxer. If it is currently active,
72  * it has an opened AVIOContext too, and potentially an AVPacket
73  * containing the next packet from this stream.
74  */
76  char *url_template;
82  int rep_idx;
83  int rep_count;
85 
87  char id[20];
88  int bandwidth;
90  AVStream *assoc_stream; /* demuxer stream associated with this representation */
91 
93  struct fragment **fragments; /* VOD list of fragment for profile */
94 
96  struct timeline **timelines;
97 
98  int64_t first_seq_no;
99  int64_t last_seq_no;
100  int64_t start_number; /* used in case when we have dynamic list of segment to know which segments are new one*/
101 
104 
106 
107  int64_t cur_seq_no;
108  int64_t cur_seg_offset;
109  int64_t cur_seg_size;
110  struct fragment *cur_seg;
111 
112  /* Currently active Media Initialization Section */
118  int64_t cur_timestamp;
120 };
121 
122 typedef struct DASHContext {
123  const AVClass *class;
124  char *base_url;
125 
126  int n_videos;
128  int n_audios;
130 
131  /* MediaPresentationDescription Attribute */
136  uint64_t publish_time;
139  uint64_t min_buffer_time;
140 
141  /* Period Attribute */
142  uint64_t period_duration;
143  uint64_t period_start;
144 
145  int is_live;
150 
151  /* Flags for init section*/
154 
155 } DASHContext;
156 
157 static int ishttp(char *url)
158 {
159  const char *proto_name = avio_find_protocol_name(url);
160  return av_strstart(proto_name, "http", NULL);
161 }
162 
163 static int aligned(int val)
164 {
165  return ((val + 0x3F) >> 6) << 6;
166 }
167 
168 static uint64_t get_current_time_in_sec(void)
169 {
170  return av_gettime() / 1000000;
171 }
172 
173 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
174 {
175  struct tm timeinfo;
176  int year = 0;
177  int month = 0;
178  int day = 0;
179  int hour = 0;
180  int minute = 0;
181  int ret = 0;
182  float second = 0.0;
183 
184  /* ISO-8601 date parser */
185  if (!datetime)
186  return 0;
187 
188  ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
189  /* year, month, day, hour, minute, second 6 arguments */
190  if (ret != 6) {
191  av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
192  }
193  timeinfo.tm_year = year - 1900;
194  timeinfo.tm_mon = month - 1;
195  timeinfo.tm_mday = day;
196  timeinfo.tm_hour = hour;
197  timeinfo.tm_min = minute;
198  timeinfo.tm_sec = (int)second;
199 
200  return av_timegm(&timeinfo);
201 }
202 
203 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
204 {
205  /* ISO-8601 duration parser */
206  uint32_t days = 0;
207  uint32_t hours = 0;
208  uint32_t mins = 0;
209  uint32_t secs = 0;
210  int size = 0;
211  float value = 0;
212  char type = '\0';
213  const char *ptr = duration;
214 
215  while (*ptr) {
216  if (*ptr == 'P' || *ptr == 'T') {
217  ptr++;
218  continue;
219  }
220 
221  if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
222  av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
223  return 0; /* parser error */
224  }
225  switch (type) {
226  case 'D':
227  days = (uint32_t)value;
228  break;
229  case 'H':
230  hours = (uint32_t)value;
231  break;
232  case 'M':
233  mins = (uint32_t)value;
234  break;
235  case 'S':
236  secs = (uint32_t)value;
237  break;
238  default:
239  // handle invalid type
240  break;
241  }
242  ptr += size;
243  }
244  return ((days * 24 + hours) * 60 + mins) * 60 + secs;
245 }
246 
247 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
248 {
249  int64_t start_time = 0;
250  int64_t i = 0;
251  int64_t j = 0;
252  int64_t num = 0;
253 
254  if (pls->n_timelines) {
255  for (i = 0; i < pls->n_timelines; i++) {
256  if (pls->timelines[i]->starttime > 0) {
257  start_time = pls->timelines[i]->starttime;
258  }
259  if (num == cur_seq_no)
260  goto finish;
261 
262  start_time += pls->timelines[i]->duration;
263 
264  if (pls->timelines[i]->repeat == -1) {
265  start_time = pls->timelines[i]->duration * cur_seq_no;
266  goto finish;
267  }
268 
269  for (j = 0; j < pls->timelines[i]->repeat; j++) {
270  num++;
271  if (num == cur_seq_no)
272  goto finish;
273  start_time += pls->timelines[i]->duration;
274  }
275  num++;
276  }
277  }
278 finish:
279  return start_time;
280 }
281 
282 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
283 {
284  int64_t i = 0;
285  int64_t j = 0;
286  int64_t num = 0;
287  int64_t start_time = 0;
288 
289  for (i = 0; i < pls->n_timelines; i++) {
290  if (pls->timelines[i]->starttime > 0) {
291  start_time = pls->timelines[i]->starttime;
292  }
293  if (start_time > cur_time)
294  goto finish;
295 
296  start_time += pls->timelines[i]->duration;
297  for (j = 0; j < pls->timelines[i]->repeat; j++) {
298  num++;
299  if (start_time > cur_time)
300  goto finish;
301  start_time += pls->timelines[i]->duration;
302  }
303  num++;
304  }
305 
306  return -1;
307 
308 finish:
309  return num;
310 }
311 
312 static void free_fragment(struct fragment **seg)
313 {
314  if (!(*seg)) {
315  return;
316  }
317  av_freep(&(*seg)->url);
318  av_freep(seg);
319 }
320 
321 static void free_fragment_list(struct representation *pls)
322 {
323  int i;
324 
325  for (i = 0; i < pls->n_fragments; i++) {
326  free_fragment(&pls->fragments[i]);
327  }
328  av_freep(&pls->fragments);
329  pls->n_fragments = 0;
330 }
331 
332 static void free_timelines_list(struct representation *pls)
333 {
334  int i;
335 
336  for (i = 0; i < pls->n_timelines; i++) {
337  av_freep(&pls->timelines[i]);
338  }
339  av_freep(&pls->timelines);
340  pls->n_timelines = 0;
341 }
342 
343 static void free_representation(struct representation *pls)
344 {
345  free_fragment_list(pls);
346  free_timelines_list(pls);
347  free_fragment(&pls->cur_seg);
349  av_freep(&pls->init_sec_buf);
350  av_freep(&pls->pb.buffer);
351  if (pls->input)
352  ff_format_io_close(pls->parent, &pls->input);
353  if (pls->ctx) {
354  pls->ctx->pb = NULL;
355  avformat_close_input(&pls->ctx);
356  }
357 
358  av_freep(&pls->url_template);
359  av_freep(&pls);
360 }
361 
363 {
364  int i;
365  for (i = 0; i < c->n_videos; i++) {
366  struct representation *pls = c->videos[i];
367  free_representation(pls);
368  }
369  av_freep(&c->videos);
370  c->n_videos = 0;
371 }
372 
374 {
375  int i;
376  for (i = 0; i < c->n_audios; i++) {
377  struct representation *pls = c->audios[i];
378  free_representation(pls);
379  }
380  av_freep(&c->audios);
381  c->n_audios = 0;
382 }
383 
384 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
385  AVDictionary *opts, AVDictionary *opts2, int *is_http)
386 {
387  DASHContext *c = s->priv_data;
388  AVDictionary *tmp = NULL;
389  const char *proto_name = NULL;
390  int ret;
391 
392  av_dict_copy(&tmp, opts, 0);
393  av_dict_copy(&tmp, opts2, 0);
394 
395  if (av_strstart(url, "crypto", NULL)) {
396  if (url[6] == '+' || url[6] == ':')
397  proto_name = avio_find_protocol_name(url + 7);
398  }
399 
400  if (!proto_name)
401  proto_name = avio_find_protocol_name(url);
402 
403  if (!proto_name)
404  return AVERROR_INVALIDDATA;
405 
406  // only http(s) & file are allowed
407  if (av_strstart(proto_name, "file", NULL)) {
408  if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
409  av_log(s, AV_LOG_ERROR,
410  "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
411  "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
412  url);
413  return AVERROR_INVALIDDATA;
414  }
415  } else if (av_strstart(proto_name, "http", NULL)) {
416  ;
417  } else
418  return AVERROR_INVALIDDATA;
419 
420  if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
421  ;
422  else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
423  ;
424  else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
425  return AVERROR_INVALIDDATA;
426 
427  av_freep(pb);
428  ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
429  if (ret >= 0) {
430  // update cookies on http response with setcookies.
431  char *new_cookies = NULL;
432 
433  if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
434  av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
435 
436  if (new_cookies) {
437  av_dict_set(&opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
438  }
439 
440  }
441 
442  av_dict_free(&tmp);
443 
444  if (is_http)
445  *is_http = av_strstart(proto_name, "http", NULL);
446 
447  return ret;
448 }
449 
450 static char *get_content_url(xmlNodePtr *baseurl_nodes,
451  int n_baseurl_nodes,
452  int max_url_size,
453  char *rep_id_val,
454  char *rep_bandwidth_val,
455  char *val)
456 {
457  int i;
458  char *text;
459  char *url = NULL;
460  char *tmp_str = av_mallocz(max_url_size);
461  char *tmp_str_2 = av_mallocz(max_url_size);
462 
463  if (!tmp_str || !tmp_str_2) {
464  return NULL;
465  }
466 
467  for (i = 0; i < n_baseurl_nodes; ++i) {
468  if (baseurl_nodes[i] &&
469  baseurl_nodes[i]->children &&
470  baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
471  text = xmlNodeGetContent(baseurl_nodes[i]->children);
472  if (text) {
473  memset(tmp_str, 0, max_url_size);
474  memset(tmp_str_2, 0, max_url_size);
475  ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
476  av_strlcpy(tmp_str, tmp_str_2, max_url_size);
477  xmlFree(text);
478  }
479  }
480  }
481 
482  if (val)
483  av_strlcat(tmp_str, (const char*)val, max_url_size);
484 
485  if (rep_id_val) {
486  url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
487  if (!url) {
488  goto end;
489  }
490  av_strlcpy(tmp_str, url, max_url_size);
491  }
492  if (rep_bandwidth_val && tmp_str[0] != '\0') {
493  // free any previously assigned url before reassigning
494  av_free(url);
495  url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
496  if (!url) {
497  goto end;
498  }
499  }
500 end:
501  av_free(tmp_str);
502  av_free(tmp_str_2);
503  return url;
504 }
505 
506 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
507 {
508  int i;
509  char *val;
510 
511  for (i = 0; i < n_nodes; ++i) {
512  if (nodes[i]) {
513  val = xmlGetProp(nodes[i], attrname);
514  if (val)
515  return val;
516  }
517  }
518 
519  return NULL;
520 }
521 
522 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
523 {
524  xmlNodePtr node = rootnode;
525  if (!node) {
526  return NULL;
527  }
528 
529  node = xmlFirstElementChild(node);
530  while (node) {
531  if (!av_strcasecmp(node->name, nodename)) {
532  return node;
533  }
534  node = xmlNextElementSibling(node);
535  }
536  return NULL;
537 }
538 
539 static enum AVMediaType get_content_type(xmlNodePtr node)
540 {
542  int i = 0;
543  const char *attr;
544  char *val = NULL;
545 
546  if (node) {
547  for (i = 0; i < 2; i++) {
548  attr = i ? "mimeType" : "contentType";
549  val = xmlGetProp(node, attr);
550  if (val) {
551  if (av_stristr((const char *)val, "video")) {
552  type = AVMEDIA_TYPE_VIDEO;
553  } else if (av_stristr((const char *)val, "audio")) {
554  type = AVMEDIA_TYPE_AUDIO;
555  }
556  xmlFree(val);
557  }
558  }
559  }
560  return type;
561 }
562 
563 static struct fragment * get_Fragment(char *range)
564 {
565  struct fragment * seg = av_mallocz(sizeof(struct fragment));
566 
567  if (!seg)
568  return NULL;
569 
570  seg->size = -1;
571  if (range) {
572  char *str_end_offset;
573  char *str_offset = av_strtok(range, "-", &str_end_offset);
574  seg->url_offset = strtoll(str_offset, NULL, 10);
575  seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset;
576  }
577 
578  return seg;
579 }
580 
582  xmlNodePtr fragmenturl_node,
583  xmlNodePtr *baseurl_nodes,
584  char *rep_id_val,
585  char *rep_bandwidth_val)
586 {
587  DASHContext *c = s->priv_data;
588  char *initialization_val = NULL;
589  char *media_val = NULL;
590  char *range_val = NULL;
591  int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
592 
593  if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
594  initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
595  range_val = xmlGetProp(fragmenturl_node, "range");
596  if (initialization_val || range_val) {
597  rep->init_section = get_Fragment(range_val);
598  if (!rep->init_section) {
599  xmlFree(initialization_val);
600  xmlFree(range_val);
601  return AVERROR(ENOMEM);
602  }
603  rep->init_section->url = get_content_url(baseurl_nodes, 4,
604  max_url_size,
605  rep_id_val,
606  rep_bandwidth_val,
607  initialization_val);
608 
609  if (!rep->init_section->url) {
610  av_free(rep->init_section);
611  xmlFree(initialization_val);
612  xmlFree(range_val);
613  return AVERROR(ENOMEM);
614  }
615  xmlFree(initialization_val);
616  xmlFree(range_val);
617  }
618  } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
619  media_val = xmlGetProp(fragmenturl_node, "media");
620  range_val = xmlGetProp(fragmenturl_node, "mediaRange");
621  if (media_val || range_val) {
622  struct fragment *seg = get_Fragment(range_val);
623  if (!seg) {
624  xmlFree(media_val);
625  xmlFree(range_val);
626  return AVERROR(ENOMEM);
627  }
628  seg->url = get_content_url(baseurl_nodes, 4,
629  max_url_size,
630  rep_id_val,
631  rep_bandwidth_val,
632  media_val);
633  if (!seg->url) {
634  av_free(seg);
635  xmlFree(media_val);
636  xmlFree(range_val);
637  return AVERROR(ENOMEM);
638  }
639  dynarray_add(&rep->fragments, &rep->n_fragments, seg);
640  xmlFree(media_val);
641  xmlFree(range_val);
642  }
643  }
644 
645  return 0;
646 }
647 
649  xmlNodePtr fragment_timeline_node)
650 {
651  xmlAttrPtr attr = NULL;
652  char *val = NULL;
653 
654  if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
655  struct timeline *tml = av_mallocz(sizeof(struct timeline));
656  if (!tml) {
657  return AVERROR(ENOMEM);
658  }
659  attr = fragment_timeline_node->properties;
660  while (attr) {
661  val = xmlGetProp(fragment_timeline_node, attr->name);
662 
663  if (!val) {
664  av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
665  continue;
666  }
667 
668  if (!av_strcasecmp(attr->name, (const char *)"t")) {
669  tml->starttime = (int64_t)strtoll(val, NULL, 10);
670  } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
671  tml->repeat =(int64_t) strtoll(val, NULL, 10);
672  } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
673  tml->duration = (int64_t)strtoll(val, NULL, 10);
674  }
675  attr = attr->next;
676  xmlFree(val);
677  }
678  dynarray_add(&rep->timelines, &rep->n_timelines, tml);
679  }
680 
681  return 0;
682 }
683 
684 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) {
685 
686  char *tmp_str = NULL;
687  char *path = NULL;
688  char *mpdName = NULL;
689  xmlNodePtr node = NULL;
690  char *baseurl = NULL;
691  char *root_url = NULL;
692  char *text = NULL;
693  char *tmp = NULL;
694 
695  int isRootHttp = 0;
696  char token ='/';
697  int start = 0;
698  int rootId = 0;
699  int updated = 0;
700  int size = 0;
701  int i;
702  int tmp_max_url_size = strlen(url);
703 
704  for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
705  text = xmlNodeGetContent(baseurl_nodes[i]);
706  if (!text)
707  continue;
708  tmp_max_url_size += strlen(text);
709  if (ishttp(text)) {
710  xmlFree(text);
711  break;
712  }
713  xmlFree(text);
714  }
715 
716  tmp_max_url_size = aligned(tmp_max_url_size);
717  text = av_mallocz(tmp_max_url_size);
718  if (!text) {
719  updated = AVERROR(ENOMEM);
720  goto end;
721  }
722  av_strlcpy(text, url, strlen(url)+1);
723  tmp = text;
724  while (mpdName = av_strtok(tmp, "/", &tmp)) {
725  size = strlen(mpdName);
726  }
727  av_free(text);
728 
729  path = av_mallocz(tmp_max_url_size);
730  tmp_str = av_mallocz(tmp_max_url_size);
731  if (!tmp_str || !path) {
732  updated = AVERROR(ENOMEM);
733  goto end;
734  }
735 
736  av_strlcpy (path, url, strlen(url) - size + 1);
737  for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
738  if (!(node = baseurl_nodes[rootId])) {
739  continue;
740  }
741  text = xmlNodeGetContent(node);
742  if (ishttp(text)) {
743  xmlFree(text);
744  break;
745  }
746  xmlFree(text);
747  }
748 
749  node = baseurl_nodes[rootId];
750  baseurl = xmlNodeGetContent(node);
751  root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
752  if (node) {
753  xmlNodeSetContent(node, root_url);
754  updated = 1;
755  }
756 
757  size = strlen(root_url);
758  isRootHttp = ishttp(root_url);
759 
760  if (root_url[size - 1] != token) {
761  av_strlcat(root_url, "/", size + 2);
762  size += 2;
763  }
764 
765  for (i = 0; i < n_baseurl_nodes; ++i) {
766  if (i == rootId) {
767  continue;
768  }
769  text = xmlNodeGetContent(baseurl_nodes[i]);
770  if (text) {
771  memset(tmp_str, 0, strlen(tmp_str));
772  if (!ishttp(text) && isRootHttp) {
773  av_strlcpy(tmp_str, root_url, size + 1);
774  }
775  start = (text[0] == token);
776  av_strlcat(tmp_str, text + start, tmp_max_url_size);
777  xmlNodeSetContent(baseurl_nodes[i], tmp_str);
778  updated = 1;
779  xmlFree(text);
780  }
781  }
782 
783 end:
784  if (tmp_max_url_size > *max_url_size) {
785  *max_url_size = tmp_max_url_size;
786  }
787  av_free(path);
788  av_free(tmp_str);
789  xmlFree(baseurl);
790  return updated;
791 
792 }
793 
795  xmlNodePtr node,
796  xmlNodePtr adaptionset_node,
797  xmlNodePtr mpd_baseurl_node,
798  xmlNodePtr period_baseurl_node,
799  xmlNodePtr period_segmenttemplate_node,
800  xmlNodePtr period_segmentlist_node,
801  xmlNodePtr fragment_template_node,
802  xmlNodePtr content_component_node,
803  xmlNodePtr adaptionset_baseurl_node,
804  xmlNodePtr adaptionset_segmentlist_node,
805  xmlNodePtr adaptionset_supplementalproperty_node)
806 {
807  int32_t ret = 0;
808  int32_t audio_rep_idx = 0;
809  int32_t video_rep_idx = 0;
810  DASHContext *c = s->priv_data;
811  struct representation *rep = NULL;
812  struct fragment *seg = NULL;
813  xmlNodePtr representation_segmenttemplate_node = NULL;
814  xmlNodePtr representation_baseurl_node = NULL;
815  xmlNodePtr representation_segmentlist_node = NULL;
816  xmlNodePtr segmentlists_tab[2];
817  xmlNodePtr fragment_timeline_node = NULL;
818  xmlNodePtr fragment_templates_tab[5];
819  char *duration_val = NULL;
820  char *presentation_timeoffset_val = NULL;
821  char *startnumber_val = NULL;
822  char *timescale_val = NULL;
823  char *initialization_val = NULL;
824  char *media_val = NULL;
825  char *val = NULL;
826  xmlNodePtr baseurl_nodes[4];
827  xmlNodePtr representation_node = node;
828  char *rep_id_val = xmlGetProp(representation_node, "id");
829  char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
830  char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
832 
833  // try get information from representation
834  if (type == AVMEDIA_TYPE_UNKNOWN)
835  type = get_content_type(representation_node);
836  // try get information from contentComponen
837  if (type == AVMEDIA_TYPE_UNKNOWN)
838  type = get_content_type(content_component_node);
839  // try get information from adaption set
840  if (type == AVMEDIA_TYPE_UNKNOWN)
841  type = get_content_type(adaptionset_node);
842  if (type == AVMEDIA_TYPE_UNKNOWN) {
843  av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
844  } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) {
845  // convert selected representation to our internal struct
846  rep = av_mallocz(sizeof(struct representation));
847  if (!rep) {
848  ret = AVERROR(ENOMEM);
849  goto end;
850  }
851  representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
852  representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
853  representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
854 
855  baseurl_nodes[0] = mpd_baseurl_node;
856  baseurl_nodes[1] = period_baseurl_node;
857  baseurl_nodes[2] = adaptionset_baseurl_node;
858  baseurl_nodes[3] = representation_baseurl_node;
859 
860  ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
862  + (rep_id_val ? strlen(rep_id_val) : 0)
863  + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
864  if (ret == AVERROR(ENOMEM) || ret == 0) {
865  goto end;
866  }
867  if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
868  fragment_timeline_node = NULL;
869  fragment_templates_tab[0] = representation_segmenttemplate_node;
870  fragment_templates_tab[1] = adaptionset_segmentlist_node;
871  fragment_templates_tab[2] = fragment_template_node;
872  fragment_templates_tab[3] = period_segmenttemplate_node;
873  fragment_templates_tab[4] = period_segmentlist_node;
874 
875  presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
876  duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
877  startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
878  timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
879  initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
880  media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
881 
882  if (initialization_val) {
883  rep->init_section = av_mallocz(sizeof(struct fragment));
884  if (!rep->init_section) {
885  av_free(rep);
886  ret = AVERROR(ENOMEM);
887  goto end;
888  }
889  c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
890  rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
891  if (!rep->init_section->url) {
892  av_free(rep->init_section);
893  av_free(rep);
894  ret = AVERROR(ENOMEM);
895  goto end;
896  }
897  rep->init_section->size = -1;
898  xmlFree(initialization_val);
899  }
900 
901  if (media_val) {
902  c->max_url_size = aligned(c->max_url_size + strlen(media_val));
903  rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
904  xmlFree(media_val);
905  }
906 
907  if (presentation_timeoffset_val) {
908  rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
909  av_log(s, AV_LOG_TRACE, "rep->presentation_timeoffset = [%"PRId64"]\n", rep->presentation_timeoffset);
910  xmlFree(presentation_timeoffset_val);
911  }
912  if (duration_val) {
913  rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
914  av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
915  xmlFree(duration_val);
916  }
917  if (timescale_val) {
918  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
919  av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
920  xmlFree(timescale_val);
921  }
922  if (startnumber_val) {
923  rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
924  av_log(s, AV_LOG_TRACE, "rep->first_seq_no = [%"PRId64"]\n", rep->first_seq_no);
925  xmlFree(startnumber_val);
926  }
927  if (adaptionset_supplementalproperty_node) {
928  if (!av_strcasecmp(xmlGetProp(adaptionset_supplementalproperty_node,"schemeIdUri"), "http://dashif.org/guidelines/last-segment-number")) {
929  val = xmlGetProp(adaptionset_supplementalproperty_node,"value");
930  if (!val) {
931  av_log(s, AV_LOG_ERROR, "Missing value attribute in adaptionset_supplementalproperty_node\n");
932  } else {
933  rep->last_seq_no =(int64_t) strtoll(val, NULL, 10) - 1;
934  xmlFree(val);
935  }
936  }
937  }
938 
939  fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
940 
941  if (!fragment_timeline_node)
942  fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
943  if (!fragment_timeline_node)
944  fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
945  if (!fragment_timeline_node)
946  fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
947  if (fragment_timeline_node) {
948  fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
949  while (fragment_timeline_node) {
950  ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
951  if (ret < 0) {
952  return ret;
953  }
954  fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
955  }
956  }
957  } else if (representation_baseurl_node && !representation_segmentlist_node) {
958  seg = av_mallocz(sizeof(struct fragment));
959  if (!seg) {
960  ret = AVERROR(ENOMEM);
961  goto end;
962  }
963  seg->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, NULL);
964  if (!seg->url) {
965  av_free(seg);
966  ret = AVERROR(ENOMEM);
967  goto end;
968  }
969  seg->size = -1;
970  dynarray_add(&rep->fragments, &rep->n_fragments, seg);
971  } else if (representation_segmentlist_node) {
972  // TODO: https://www.brendanlong.com/the-structure-of-an-mpeg-dash-mpd.html
973  // http://www-itec.uni-klu.ac.at/dash/ddash/mpdGenerator.php?fragmentlength=15&type=full
974  xmlNodePtr fragmenturl_node = NULL;
975  segmentlists_tab[0] = representation_segmentlist_node;
976  segmentlists_tab[1] = adaptionset_segmentlist_node;
977 
978  duration_val = get_val_from_nodes_tab(segmentlists_tab, 2, "duration");
979  timescale_val = get_val_from_nodes_tab(segmentlists_tab, 2, "timescale");
980  if (duration_val) {
981  rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
982  av_log(s, AV_LOG_TRACE, "rep->fragment_duration = [%"PRId64"]\n", rep->fragment_duration);
983  xmlFree(duration_val);
984  }
985  if (timescale_val) {
986  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
987  av_log(s, AV_LOG_TRACE, "rep->fragment_timescale = [%"PRId64"]\n", rep->fragment_timescale);
988  xmlFree(timescale_val);
989  }
990  fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
991  while (fragmenturl_node) {
992  ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
993  baseurl_nodes,
994  rep_id_val,
995  rep_bandwidth_val);
996  if (ret < 0) {
997  return ret;
998  }
999  fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
1000  }
1001 
1002  fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
1003 
1004  if (!fragment_timeline_node)
1005  fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
1006  if (!fragment_timeline_node)
1007  fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1008  if (!fragment_timeline_node)
1009  fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1010  if (fragment_timeline_node) {
1011  fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1012  while (fragment_timeline_node) {
1013  ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1014  if (ret < 0) {
1015  return ret;
1016  }
1017  fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1018  }
1019  }
1020  } else {
1021  free_representation(rep);
1022  rep = NULL;
1023  av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1024  }
1025 
1026  if (rep) {
1027  if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1028  rep->fragment_timescale = 1;
1029  rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1030  strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1031  rep->framerate = av_make_q(0, 0);
1032  if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
1033  ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1034  if (ret < 0)
1035  av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1036  }
1037 
1038  if (type == AVMEDIA_TYPE_VIDEO) {
1039  rep->rep_idx = video_rep_idx;
1040  dynarray_add(&c->videos, &c->n_videos, rep);
1041  } else {
1042  rep->rep_idx = audio_rep_idx;
1043  dynarray_add(&c->audios, &c->n_audios, rep);
1044  }
1045  }
1046  }
1047 
1048  video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
1049  audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
1050 
1051 end:
1052  if (rep_id_val)
1053  xmlFree(rep_id_val);
1054  if (rep_bandwidth_val)
1055  xmlFree(rep_bandwidth_val);
1056  if (rep_framerate_val)
1057  xmlFree(rep_framerate_val);
1058 
1059  return ret;
1060 }
1061 
1063  xmlNodePtr adaptionset_node,
1064  xmlNodePtr mpd_baseurl_node,
1065  xmlNodePtr period_baseurl_node,
1066  xmlNodePtr period_segmenttemplate_node,
1067  xmlNodePtr period_segmentlist_node)
1068 {
1069  int ret = 0;
1070  xmlNodePtr fragment_template_node = NULL;
1071  xmlNodePtr content_component_node = NULL;
1072  xmlNodePtr adaptionset_baseurl_node = NULL;
1073  xmlNodePtr adaptionset_segmentlist_node = NULL;
1074  xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1075  xmlNodePtr node = NULL;
1076 
1077  node = xmlFirstElementChild(adaptionset_node);
1078  while (node) {
1079  if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1080  fragment_template_node = node;
1081  } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1082  content_component_node = node;
1083  } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1084  adaptionset_baseurl_node = node;
1085  } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1086  adaptionset_segmentlist_node = node;
1087  } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1088  adaptionset_supplementalproperty_node = node;
1089  } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1090  ret = parse_manifest_representation(s, url, node,
1091  adaptionset_node,
1092  mpd_baseurl_node,
1093  period_baseurl_node,
1094  period_segmenttemplate_node,
1095  period_segmentlist_node,
1096  fragment_template_node,
1097  content_component_node,
1098  adaptionset_baseurl_node,
1099  adaptionset_segmentlist_node,
1100  adaptionset_supplementalproperty_node);
1101  if (ret < 0) {
1102  return ret;
1103  }
1104  }
1105  node = xmlNextElementSibling(node);
1106  }
1107  return 0;
1108 }
1109 
1110 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1111 {
1112  DASHContext *c = s->priv_data;
1113  int ret = 0;
1114  int close_in = 0;
1115  uint8_t *new_url = NULL;
1116  int64_t filesize = 0;
1117  char *buffer = NULL;
1118  AVDictionary *opts = NULL;
1119  xmlDoc *doc = NULL;
1120  xmlNodePtr root_element = NULL;
1121  xmlNodePtr node = NULL;
1122  xmlNodePtr period_node = NULL;
1123  xmlNodePtr tmp_node = NULL;
1124  xmlNodePtr mpd_baseurl_node = NULL;
1125  xmlNodePtr period_baseurl_node = NULL;
1126  xmlNodePtr period_segmenttemplate_node = NULL;
1127  xmlNodePtr period_segmentlist_node = NULL;
1128  xmlNodePtr adaptionset_node = NULL;
1129  xmlAttrPtr attr = NULL;
1130  char *val = NULL;
1131  uint32_t period_duration_sec = 0;
1132  uint32_t period_start_sec = 0;
1133 
1134  if (!in) {
1135  close_in = 1;
1136 
1137  av_dict_copy(&opts, c->avio_opts, 0);
1138  ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1139  av_dict_free(&opts);
1140  if (ret < 0)
1141  return ret;
1142  }
1143 
1144  if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1145  c->base_url = av_strdup(new_url);
1146  } else {
1147  c->base_url = av_strdup(url);
1148  }
1149 
1150  filesize = avio_size(in);
1151  if (filesize <= 0) {
1152  filesize = 8 * 1024;
1153  }
1154 
1155  buffer = av_mallocz(filesize);
1156  if (!buffer) {
1157  av_free(c->base_url);
1158  return AVERROR(ENOMEM);
1159  }
1160 
1161  filesize = avio_read(in, buffer, filesize);
1162  if (filesize <= 0) {
1163  av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
1164  ret = AVERROR_INVALIDDATA;
1165  } else {
1166  LIBXML_TEST_VERSION
1167 
1168  doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
1169  root_element = xmlDocGetRootElement(doc);
1170  node = root_element;
1171 
1172  if (!node) {
1173  ret = AVERROR_INVALIDDATA;
1174  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1175  goto cleanup;
1176  }
1177 
1178  if (node->type != XML_ELEMENT_NODE ||
1179  av_strcasecmp(node->name, (const char *)"MPD")) {
1180  ret = AVERROR_INVALIDDATA;
1181  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1182  goto cleanup;
1183  }
1184 
1185  val = xmlGetProp(node, "type");
1186  if (!val) {
1187  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1188  ret = AVERROR_INVALIDDATA;
1189  goto cleanup;
1190  }
1191  if (!av_strcasecmp(val, (const char *)"dynamic"))
1192  c->is_live = 1;
1193  xmlFree(val);
1194 
1195  attr = node->properties;
1196  while (attr) {
1197  val = xmlGetProp(node, attr->name);
1198 
1199  if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
1200  c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
1201  av_log(s, AV_LOG_TRACE, "c->availability_start_time = [%"PRId64"]\n", c->availability_start_time);
1202  } else if (!av_strcasecmp(attr->name, (const char *)"availabilityEndTime")) {
1203  c->availability_end_time = get_utc_date_time_insec(s, (const char *)val);
1204  av_log(s, AV_LOG_TRACE, "c->availability_end_time = [%"PRId64"]\n", c->availability_end_time);
1205  } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
1206  c->publish_time = get_utc_date_time_insec(s, (const char *)val);
1207  av_log(s, AV_LOG_TRACE, "c->publish_time = [%"PRId64"]\n", c->publish_time);
1208  } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
1209  c->minimum_update_period = get_duration_insec(s, (const char *)val);
1210  av_log(s, AV_LOG_TRACE, "c->minimum_update_period = [%"PRId64"]\n", c->minimum_update_period);
1211  } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
1212  c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
1213  av_log(s, AV_LOG_TRACE, "c->time_shift_buffer_depth = [%"PRId64"]\n", c->time_shift_buffer_depth);
1214  } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
1215  c->min_buffer_time = get_duration_insec(s, (const char *)val);
1216  av_log(s, AV_LOG_TRACE, "c->min_buffer_time = [%"PRId64"]\n", c->min_buffer_time);
1217  } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
1218  c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
1219  av_log(s, AV_LOG_TRACE, "c->suggested_presentation_delay = [%"PRId64"]\n", c->suggested_presentation_delay);
1220  } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
1221  c->media_presentation_duration = get_duration_insec(s, (const char *)val);
1222  av_log(s, AV_LOG_TRACE, "c->media_presentation_duration = [%"PRId64"]\n", c->media_presentation_duration);
1223  }
1224  attr = attr->next;
1225  xmlFree(val);
1226  }
1227 
1228  tmp_node = find_child_node_by_name(node, "BaseURL");
1229  if (tmp_node) {
1230  mpd_baseurl_node = xmlCopyNode(tmp_node,1);
1231  } else {
1232  mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1233  }
1234 
1235  // at now we can handle only one period, with the longest duration
1236  node = xmlFirstElementChild(node);
1237  while (node) {
1238  if (!av_strcasecmp(node->name, (const char *)"Period")) {
1239  period_duration_sec = 0;
1240  period_start_sec = 0;
1241  attr = node->properties;
1242  while (attr) {
1243  val = xmlGetProp(node, attr->name);
1244  if (!av_strcasecmp(attr->name, (const char *)"duration")) {
1245  period_duration_sec = get_duration_insec(s, (const char *)val);
1246  } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
1247  period_start_sec = get_duration_insec(s, (const char *)val);
1248  }
1249  attr = attr->next;
1250  xmlFree(val);
1251  }
1252  if ((period_duration_sec) >= (c->period_duration)) {
1253  period_node = node;
1254  c->period_duration = period_duration_sec;
1255  c->period_start = period_start_sec;
1256  if (c->period_start > 0)
1258  }
1259  }
1260  node = xmlNextElementSibling(node);
1261  }
1262  if (!period_node) {
1263  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1264  ret = AVERROR_INVALIDDATA;
1265  goto cleanup;
1266  }
1267 
1268  adaptionset_node = xmlFirstElementChild(period_node);
1269  while (adaptionset_node) {
1270  if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1271  period_baseurl_node = adaptionset_node;
1272  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
1273  period_segmenttemplate_node = adaptionset_node;
1274  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
1275  period_segmentlist_node = adaptionset_node;
1276  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1277  parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1278  }
1279  adaptionset_node = xmlNextElementSibling(adaptionset_node);
1280  }
1281 cleanup:
1282  /*free the document */
1283  xmlFreeDoc(doc);
1284  xmlCleanupParser();
1285  xmlFreeNode(mpd_baseurl_node);
1286  }
1287 
1288  av_free(new_url);
1289  av_free(buffer);
1290  if (close_in) {
1291  avio_close(in);
1292  }
1293  return ret;
1294 }
1295 
1296 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1297 {
1298  DASHContext *c = s->priv_data;
1299  int64_t num = 0;
1300  int64_t start_time_offset = 0;
1301 
1302  if (c->is_live) {
1303  if (pls->n_fragments) {
1304  av_log(s, AV_LOG_TRACE, "in n_fragments mode\n");
1305  num = pls->first_seq_no;
1306  } else if (pls->n_timelines) {
1307  av_log(s, AV_LOG_TRACE, "in n_timelines mode\n");
1308  start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1309  num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1310  if (num == -1)
1311  num = pls->first_seq_no;
1312  else
1313  num += pls->first_seq_no;
1314  } else if (pls->fragment_duration){
1315  av_log(s, AV_LOG_TRACE, "in fragment_duration mode fragment_timescale = %"PRId64", presentation_timeoffset = %"PRId64"\n", pls->fragment_timescale, pls->presentation_timeoffset);
1316  if (pls->presentation_timeoffset) {
1318  } else if (c->publish_time > 0 && !c->availability_start_time) {
1319  if (c->min_buffer_time) {
1321  } else {
1323  }
1324  } else {
1326  }
1327  }
1328  } else {
1329  num = pls->first_seq_no;
1330  }
1331  return num;
1332 }
1333 
1334 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1335 {
1336  DASHContext *c = s->priv_data;
1337  int64_t num = 0;
1338 
1339  if (c->is_live && pls->fragment_duration) {
1340  av_log(s, AV_LOG_TRACE, "in live mode\n");
1342  } else {
1343  num = pls->first_seq_no;
1344  }
1345  return num;
1346 }
1347 
1348 static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
1349 {
1350  int64_t num = 0;
1351 
1352  if (pls->n_fragments) {
1353  num = pls->first_seq_no + pls->n_fragments - 1;
1354  } else if (pls->n_timelines) {
1355  int i = 0;
1356  num = pls->first_seq_no + pls->n_timelines - 1;
1357  for (i = 0; i < pls->n_timelines; i++) {
1358  if (pls->timelines[i]->repeat == -1) {
1359  int length_of_each_segment = pls->timelines[i]->duration / pls->fragment_timescale;
1360  num = c->period_duration / length_of_each_segment;
1361  } else {
1362  num += pls->timelines[i]->repeat;
1363  }
1364  }
1365  } else if (c->is_live && pls->fragment_duration) {
1367  } else if (pls->fragment_duration) {
1369  }
1370 
1371  return num;
1372 }
1373 
1374 static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1375 {
1376  if (rep_dest && rep_src ) {
1377  free_timelines_list(rep_dest);
1378  rep_dest->timelines = rep_src->timelines;
1379  rep_dest->n_timelines = rep_src->n_timelines;
1380  rep_dest->first_seq_no = rep_src->first_seq_no;
1381  rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1382  rep_src->timelines = NULL;
1383  rep_src->n_timelines = 0;
1384  rep_dest->cur_seq_no = rep_src->cur_seq_no;
1385  }
1386 }
1387 
1388 static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1389 {
1390  if (rep_dest && rep_src ) {
1391  free_fragment_list(rep_dest);
1392  if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1393  rep_dest->cur_seq_no = 0;
1394  else
1395  rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1396  rep_dest->fragments = rep_src->fragments;
1397  rep_dest->n_fragments = rep_src->n_fragments;
1398  rep_dest->parent = rep_src->parent;
1399  rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1400  rep_src->fragments = NULL;
1401  rep_src->n_fragments = 0;
1402  }
1403 }
1404 
1405 
1407 {
1408 
1409  int ret = 0, i;
1410  DASHContext *c = s->priv_data;
1411 
1412  // save current context
1413  int n_videos = c->n_videos;
1414  struct representation **videos = c->videos;
1415  int n_audios = c->n_audios;
1416  struct representation **audios = c->audios;
1417  char *base_url = c->base_url;
1418 
1419  c->base_url = NULL;
1420  c->n_videos = 0;
1421  c->videos = NULL;
1422  c->n_audios = 0;
1423  c->audios = NULL;
1424  ret = parse_manifest(s, s->url, NULL);
1425  if (ret)
1426  goto finish;
1427 
1428  if (c->n_videos != n_videos) {
1429  av_log(c, AV_LOG_ERROR,
1430  "new manifest has mismatched no. of video representations, %d -> %d\n",
1431  n_videos, c->n_videos);
1432  return AVERROR_INVALIDDATA;
1433  }
1434  if (c->n_audios != n_audios) {
1435  av_log(c, AV_LOG_ERROR,
1436  "new manifest has mismatched no. of audio representations, %d -> %d\n",
1437  n_audios, c->n_audios);
1438  return AVERROR_INVALIDDATA;
1439  }
1440 
1441  for (i = 0; i < n_videos; i++) {
1442  struct representation *cur_video = videos[i];
1443  struct representation *ccur_video = c->videos[i];
1444  if (cur_video->timelines) {
1445  // calc current time
1446  int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1447  // update segments
1448  ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1449  if (ccur_video->cur_seq_no >= 0) {
1450  move_timelines(ccur_video, cur_video, c);
1451  }
1452  }
1453  if (cur_video->fragments) {
1454  move_segments(ccur_video, cur_video, c);
1455  }
1456  }
1457  for (i = 0; i < n_audios; i++) {
1458  struct representation *cur_audio = audios[i];
1459  struct representation *ccur_audio = c->audios[i];
1460  if (cur_audio->timelines) {
1461  // calc current time
1462  int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1463  // update segments
1464  ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1465  if (ccur_audio->cur_seq_no >= 0) {
1466  move_timelines(ccur_audio, cur_audio, c);
1467  }
1468  }
1469  if (cur_audio->fragments) {
1470  move_segments(ccur_audio, cur_audio, c);
1471  }
1472  }
1473 
1474 finish:
1475  // restore context
1476  if (c->base_url)
1477  av_free(base_url);
1478  else
1479  c->base_url = base_url;
1480  if (c->audios)
1481  free_audio_list(c);
1482  if (c->videos)
1483  free_video_list(c);
1484  c->n_audios = n_audios;
1485  c->audios = audios;
1486  c->n_videos = n_videos;
1487  c->videos = videos;
1488  return ret;
1489 }
1490 
1491 static struct fragment *get_current_fragment(struct representation *pls)
1492 {
1493  int64_t min_seq_no = 0;
1494  int64_t max_seq_no = 0;
1495  struct fragment *seg = NULL;
1496  struct fragment *seg_ptr = NULL;
1497  DASHContext *c = pls->parent->priv_data;
1498 
1499  while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1500  if (pls->cur_seq_no < pls->n_fragments) {
1501  seg_ptr = pls->fragments[pls->cur_seq_no];
1502  seg = av_mallocz(sizeof(struct fragment));
1503  if (!seg) {
1504  return NULL;
1505  }
1506  seg->url = av_strdup(seg_ptr->url);
1507  if (!seg->url) {
1508  av_free(seg);
1509  return NULL;
1510  }
1511  seg->size = seg_ptr->size;
1512  seg->url_offset = seg_ptr->url_offset;
1513  return seg;
1514  } else if (c->is_live) {
1515  refresh_manifest(pls->parent);
1516  } else {
1517  break;
1518  }
1519  }
1520  if (c->is_live) {
1521  min_seq_no = calc_min_seg_no(pls->parent, pls);
1522  max_seq_no = calc_max_seg_no(pls, c);
1523 
1524  if (pls->timelines || pls->fragments) {
1525  refresh_manifest(pls->parent);
1526  }
1527  if (pls->cur_seq_no <= min_seq_no) {
1528  av_log(pls->parent, AV_LOG_VERBOSE, "old fragment: cur[%"PRId64"] min[%"PRId64"] max[%"PRId64"], playlist %d\n", (int64_t)pls->cur_seq_no, min_seq_no, max_seq_no, (int)pls->rep_idx);
1529  pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1530  } else if (pls->cur_seq_no > max_seq_no) {
1531  av_log(pls->parent, AV_LOG_VERBOSE, "new fragment: min[%"PRId64"] max[%"PRId64"], playlist %d\n", min_seq_no, max_seq_no, (int)pls->rep_idx);
1532  }
1533  seg = av_mallocz(sizeof(struct fragment));
1534  if (!seg) {
1535  return NULL;
1536  }
1537  } else if (pls->cur_seq_no <= pls->last_seq_no) {
1538  seg = av_mallocz(sizeof(struct fragment));
1539  if (!seg) {
1540  return NULL;
1541  }
1542  }
1543  if (seg) {
1544  char *tmpfilename= av_mallocz(c->max_url_size);
1545  if (!tmpfilename) {
1546  return NULL;
1547  }
1549  seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1550  if (!seg->url) {
1551  av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1552  seg->url = av_strdup(pls->url_template);
1553  if (!seg->url) {
1554  av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1555  av_free(tmpfilename);
1556  return NULL;
1557  }
1558  }
1559  av_free(tmpfilename);
1560  seg->size = -1;
1561  }
1562 
1563  return seg;
1564 }
1565 
1566 static int read_from_url(struct representation *pls, struct fragment *seg,
1567  uint8_t *buf, int buf_size)
1568 {
1569  int ret;
1570 
1571  /* limit read if the fragment was only a part of a file */
1572  if (seg->size >= 0)
1573  buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1574 
1575  ret = avio_read(pls->input, buf, buf_size);
1576  if (ret > 0)
1577  pls->cur_seg_offset += ret;
1578 
1579  return ret;
1580 }
1581 
1582 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1583 {
1584  AVDictionary *opts = NULL;
1585  char *url = NULL;
1586  int ret = 0;
1587 
1588  url = av_mallocz(c->max_url_size);
1589  if (!url) {
1590  goto cleanup;
1591  }
1592 
1593  if (seg->size >= 0) {
1594  /* try to restrict the HTTP request to the part we want
1595  * (if this is in fact a HTTP request) */
1596  av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1597  av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1598  }
1599 
1600  ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1601  av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
1602  url, seg->url_offset, pls->rep_idx);
1603  ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
1604  if (ret < 0) {
1605  goto cleanup;
1606  }
1607 
1608 cleanup:
1609  av_free(url);
1610  av_dict_free(&opts);
1611  pls->cur_seg_offset = 0;
1612  pls->cur_seg_size = seg->size;
1613  return ret;
1614 }
1615 
1616 static int update_init_section(struct representation *pls)
1617 {
1618  static const int max_init_section_size = 1024 * 1024;
1619  DASHContext *c = pls->parent->priv_data;
1620  int64_t sec_size;
1621  int64_t urlsize;
1622  int ret;
1623 
1624  if (!pls->init_section || pls->init_sec_buf)
1625  return 0;
1626 
1627  ret = open_input(c, pls, pls->init_section);
1628  if (ret < 0) {
1630  "Failed to open an initialization section in playlist %d\n",
1631  pls->rep_idx);
1632  return ret;
1633  }
1634 
1635  if (pls->init_section->size >= 0)
1636  sec_size = pls->init_section->size;
1637  else if ((urlsize = avio_size(pls->input)) >= 0)
1638  sec_size = urlsize;
1639  else
1640  sec_size = max_init_section_size;
1641 
1642  av_log(pls->parent, AV_LOG_DEBUG,
1643  "Downloading an initialization section of size %"PRId64"\n",
1644  sec_size);
1645 
1646  sec_size = FFMIN(sec_size, max_init_section_size);
1647 
1648  av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1649 
1650  ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1651  pls->init_sec_buf_size);
1652  ff_format_io_close(pls->parent, &pls->input);
1653 
1654  if (ret < 0)
1655  return ret;
1656 
1657  pls->init_sec_data_len = ret;
1658  pls->init_sec_buf_read_offset = 0;
1659 
1660  return 0;
1661 }
1662 
1663 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1664 {
1665  struct representation *v = opaque;
1666  if (v->n_fragments && !v->init_sec_data_len) {
1667  return avio_seek(v->input, offset, whence);
1668  }
1669 
1670  return AVERROR(ENOSYS);
1671 }
1672 
1673 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1674 {
1675  int ret = 0;
1676  struct representation *v = opaque;
1677  DASHContext *c = v->parent->priv_data;
1678 
1679 restart:
1680  if (!v->input) {
1681  free_fragment(&v->cur_seg);
1682  v->cur_seg = get_current_fragment(v);
1683  if (!v->cur_seg) {
1684  ret = AVERROR_EOF;
1685  goto end;
1686  }
1687 
1688  /* load/update Media Initialization Section, if any */
1689  ret = update_init_section(v);
1690  if (ret)
1691  goto end;
1692 
1693  ret = open_input(c, v, v->cur_seg);
1694  if (ret < 0) {
1696  goto end;
1697  ret = AVERROR_EXIT;
1698  }
1699  av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
1700  v->cur_seq_no++;
1701  goto restart;
1702  }
1703  }
1704 
1706  /* Push init section out first before first actual fragment */
1707  int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1708  memcpy(buf, v->init_sec_buf, copy_size);
1709  v->init_sec_buf_read_offset += copy_size;
1710  ret = copy_size;
1711  goto end;
1712  }
1713 
1714  /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1715  if (!v->cur_seg) {
1716  v->cur_seg = get_current_fragment(v);
1717  }
1718  if (!v->cur_seg) {
1719  ret = AVERROR_EOF;
1720  goto end;
1721  }
1722  ret = read_from_url(v, v->cur_seg, buf, buf_size);
1723  if (ret > 0)
1724  goto end;
1725 
1726  if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1727  if (!v->is_restart_needed)
1728  v->cur_seq_no++;
1729  v->is_restart_needed = 1;
1730  }
1731 
1732 end:
1733  return ret;
1734 }
1735 
1737 {
1738  DASHContext *c = s->priv_data;
1739  const char *opts[] = { "headers", "user_agent", "cookies", NULL }, **opt = opts;
1740  uint8_t *buf = NULL;
1741  int ret = 0;
1742 
1743  while (*opt) {
1744  if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1745  if (buf[0] != '\0') {
1746  ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1747  if (ret < 0) {
1748  av_freep(&buf);
1749  return ret;
1750  }
1751  } else {
1752  av_freep(&buf);
1753  }
1754  }
1755  opt++;
1756  }
1757 
1758  return ret;
1759 }
1760 
1761 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1762  int flags, AVDictionary **opts)
1763 {
1764  av_log(s, AV_LOG_ERROR,
1765  "A DASH playlist item '%s' referred to an external file '%s'. "
1766  "Opening this file was forbidden for security reasons\n",
1767  s->url, url);
1768  return AVERROR(EPERM);
1769 }
1770 
1772 {
1773  /* note: the internal buffer could have changed */
1774  av_freep(&pls->pb.buffer);
1775  memset(&pls->pb, 0x00, sizeof(AVIOContext));
1776  pls->ctx->pb = NULL;
1777  avformat_close_input(&pls->ctx);
1778  pls->ctx = NULL;
1779 }
1780 
1782 {
1783  DASHContext *c = s->priv_data;
1784  AVInputFormat *in_fmt = NULL;
1785  AVDictionary *in_fmt_opts = NULL;
1786  uint8_t *avio_ctx_buffer = NULL;
1787  int ret = 0, i;
1788 
1789  if (pls->ctx) {
1791  }
1792 
1794  ret = AVERROR_EXIT;
1795  goto fail;
1796  }
1797 
1798  if (!(pls->ctx = avformat_alloc_context())) {
1799  ret = AVERROR(ENOMEM);
1800  goto fail;
1801  }
1802 
1803  avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1804  if (!avio_ctx_buffer ) {
1805  ret = AVERROR(ENOMEM);
1806  avformat_free_context(pls->ctx);
1807  pls->ctx = NULL;
1808  goto fail;
1809  }
1810  if (c->is_live) {
1811  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1812  } else {
1813  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1814  }
1815  pls->pb.seekable = 0;
1816 
1817  if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1818  goto fail;
1819 
1820  pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1821  pls->ctx->probesize = 1024 * 4;
1823  ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1824  if (ret < 0) {
1825  av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
1826  avformat_free_context(pls->ctx);
1827  pls->ctx = NULL;
1828  goto fail;
1829  }
1830 
1831  pls->ctx->pb = &pls->pb;
1832  pls->ctx->io_open = nested_io_open;
1833 
1834  // provide additional information from mpd if available
1835  ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1836  av_dict_free(&in_fmt_opts);
1837  if (ret < 0)
1838  goto fail;
1839  if (pls->n_fragments) {
1840 #if FF_API_R_FRAME_RATE
1841  if (pls->framerate.den) {
1842  for (i = 0; i < pls->ctx->nb_streams; i++)
1843  pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1844  }
1845 #endif
1846 
1847  ret = avformat_find_stream_info(pls->ctx, NULL);
1848  if (ret < 0)
1849  goto fail;
1850  }
1851 
1852 fail:
1853  return ret;
1854 }
1855 
1857 {
1858  int ret = 0;
1859  int i;
1860 
1861  pls->parent = s;
1862  pls->cur_seq_no = calc_cur_seg_no(s, pls);
1863 
1864  if (!pls->last_seq_no) {
1865  pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1866  }
1867 
1868  ret = reopen_demux_for_component(s, pls);
1869  if (ret < 0) {
1870  goto fail;
1871  }
1872  for (i = 0; i < pls->ctx->nb_streams; i++) {
1873  AVStream *st = avformat_new_stream(s, NULL);
1874  AVStream *ist = pls->ctx->streams[i];
1875  if (!st) {
1876  ret = AVERROR(ENOMEM);
1877  goto fail;
1878  }
1879  st->id = i;
1882  }
1883 
1884  return 0;
1885 fail:
1886  return ret;
1887 }
1888 
1889 static int is_common_init_section_exist(struct representation **pls, int n_pls)
1890 {
1891  struct fragment *first_init_section = pls[0]->init_section;
1892  char *url =NULL;
1893  int64_t url_offset = -1;
1894  int64_t size = -1;
1895  int i = 0;
1896 
1897  if (first_init_section == NULL || n_pls == 0)
1898  return 0;
1899 
1900  url = first_init_section->url;
1901  url_offset = first_init_section->url_offset;
1902  size = pls[0]->init_section->size;
1903  for (i=0;i<n_pls;i++) {
1904  if (av_strcasecmp(pls[i]->init_section->url,url) || pls[i]->init_section->url_offset != url_offset || pls[i]->init_section->size != size) {
1905  return 0;
1906  }
1907  }
1908  return 1;
1909 }
1910 
1911 static void copy_init_section(struct representation *rep_dest, struct representation *rep_src)
1912 {
1913  rep_dest->init_sec_buf = av_mallocz(rep_src->init_sec_buf_size);
1914  memcpy(rep_dest->init_sec_buf, rep_src->init_sec_buf, rep_src->init_sec_data_len);
1915  rep_dest->init_sec_buf_size = rep_src->init_sec_buf_size;
1916  rep_dest->init_sec_data_len = rep_src->init_sec_data_len;
1917  rep_dest->cur_timestamp = rep_src->cur_timestamp;
1918 }
1919 
1920 
1922 {
1923  DASHContext *c = s->priv_data;
1924  int ret = 0;
1925  int stream_index = 0;
1926  int i;
1927 
1929 
1930  if ((ret = save_avio_options(s)) < 0)
1931  goto fail;
1932 
1933  av_dict_set(&c->avio_opts, "seekable", "0", 0);
1934 
1935  if ((ret = parse_manifest(s, s->url, s->pb)) < 0)
1936  goto fail;
1937 
1938  /* If this isn't a live stream, fill the total duration of the
1939  * stream. */
1940  if (!c->is_live) {
1942  }
1943 
1944  if(c->n_videos)
1946 
1947  /* Open the demuxer for video and audio components if available */
1948  for (i = 0; i < c->n_videos; i++) {
1949  struct representation *cur_video = c->videos[i];
1950  if (i > 0 && c->is_init_section_common_video) {
1951  copy_init_section(cur_video,c->videos[0]);
1952  }
1953  ret = open_demux_for_component(s, cur_video);
1954 
1955  if (ret)
1956  goto fail;
1957  cur_video->stream_index = stream_index;
1958  ++stream_index;
1959  }
1960 
1961  if(c->n_audios)
1963 
1964  for (i = 0; i < c->n_audios; i++) {
1965  struct representation *cur_audio = c->audios[i];
1966  if (i > 0 && c->is_init_section_common_audio) {
1967  copy_init_section(cur_audio,c->audios[0]);
1968  }
1969  ret = open_demux_for_component(s, cur_audio);
1970 
1971  if (ret)
1972  goto fail;
1973  cur_audio->stream_index = stream_index;
1974  ++stream_index;
1975  }
1976 
1977  if (!stream_index) {
1978  ret = AVERROR_INVALIDDATA;
1979  goto fail;
1980  }
1981 
1982  /* Create a program */
1983  if (!ret) {
1984  AVProgram *program;
1985  program = av_new_program(s, 0);
1986  if (!program) {
1987  goto fail;
1988  }
1989 
1990  for (i = 0; i < c->n_videos; i++) {
1991  struct representation *pls = c->videos[i];
1992 
1994  pls->assoc_stream = s->streams[pls->stream_index];
1995  if (pls->bandwidth > 0)
1996  av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
1997  if (pls->id[0])
1998  av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
1999  }
2000  for (i = 0; i < c->n_audios; i++) {
2001  struct representation *pls = c->audios[i];
2002 
2004  pls->assoc_stream = s->streams[pls->stream_index];
2005  if (pls->bandwidth > 0)
2006  av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
2007  if (pls->id[0])
2008  av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
2009  }
2010  }
2011 
2012  return 0;
2013 fail:
2014  return ret;
2015 }
2016 
2018 {
2019  int i, j;
2020 
2021  for (i = 0; i < n; i++) {
2022  struct representation *pls = p[i];
2023 
2024  int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
2025  if (needed && !pls->ctx) {
2026  pls->cur_seg_offset = 0;
2027  pls->init_sec_buf_read_offset = 0;
2028  /* Catch up */
2029  for (j = 0; j < n; j++) {
2030  pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
2031  }
2033  av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
2034  } else if (!needed && pls->ctx) {
2036  if (pls->input)
2037  ff_format_io_close(pls->parent, &pls->input);
2038  av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
2039  }
2040  }
2041 }
2042 
2044 {
2045  DASHContext *c = s->priv_data;
2046  int ret = 0, i;
2047  int64_t mints = 0;
2048  struct representation *cur = NULL;
2049 
2052 
2053  for (i = 0; i < c->n_videos; i++) {
2054  struct representation *pls = c->videos[i];
2055  if (!pls->ctx)
2056  continue;
2057  if (!cur || pls->cur_timestamp < mints) {
2058  cur = pls;
2059  mints = pls->cur_timestamp;
2060  }
2061  }
2062  for (i = 0; i < c->n_audios; i++) {
2063  struct representation *pls = c->audios[i];
2064  if (!pls->ctx)
2065  continue;
2066  if (!cur || pls->cur_timestamp < mints) {
2067  cur = pls;
2068  mints = pls->cur_timestamp;
2069  }
2070  }
2071 
2072  if (!cur) {
2073  return AVERROR_INVALIDDATA;
2074  }
2075  while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2076  ret = av_read_frame(cur->ctx, pkt);
2077  if (ret >= 0) {
2078  /* If we got a packet, return it */
2079  cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2080  pkt->stream_index = cur->stream_index;
2081  return 0;
2082  }
2083  if (cur->is_restart_needed) {
2084  cur->cur_seg_offset = 0;
2085  cur->init_sec_buf_read_offset = 0;
2086  if (cur->input)
2087  ff_format_io_close(cur->parent, &cur->input);
2088  ret = reopen_demux_for_component(s, cur);
2089  cur->is_restart_needed = 0;
2090  }
2091  }
2092  return AVERROR_EOF;
2093 }
2094 
2096 {
2097  DASHContext *c = s->priv_data;
2098  free_audio_list(c);
2099  free_video_list(c);
2100 
2101  av_dict_free(&c->avio_opts);
2102  av_freep(&c->base_url);
2103  return 0;
2104 }
2105 
2106 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2107 {
2108  int ret = 0;
2109  int i = 0;
2110  int j = 0;
2111  int64_t duration = 0;
2112 
2113  av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
2114  seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
2115 
2116  // single fragment mode
2117  if (pls->n_fragments == 1) {
2118  pls->cur_timestamp = 0;
2119  pls->cur_seg_offset = 0;
2120  if (dry_run)
2121  return 0;
2122  ff_read_frame_flush(pls->ctx);
2123  return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2124  }
2125 
2126  if (pls->input)
2127  ff_format_io_close(pls->parent, &pls->input);
2128 
2129  // find the nearest fragment
2130  if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2131  int64_t num = pls->first_seq_no;
2132  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2133  "last_seq_no[%"PRId64"], playlist %d.\n",
2134  (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
2135  for (i = 0; i < pls->n_timelines; i++) {
2136  if (pls->timelines[i]->starttime > 0) {
2137  duration = pls->timelines[i]->starttime;
2138  }
2139  duration += pls->timelines[i]->duration;
2140  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2141  goto set_seq_num;
2142  }
2143  for (j = 0; j < pls->timelines[i]->repeat; j++) {
2144  duration += pls->timelines[i]->duration;
2145  num++;
2146  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2147  goto set_seq_num;
2148  }
2149  }
2150  num++;
2151  }
2152 
2153 set_seq_num:
2154  pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2155  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
2156  (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
2157  } else if (pls->fragment_duration > 0) {
2158  pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2159  } else {
2160  av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2161  pls->cur_seq_no = pls->first_seq_no;
2162  }
2163  pls->cur_timestamp = 0;
2164  pls->cur_seg_offset = 0;
2165  pls->init_sec_buf_read_offset = 0;
2166  ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2167 
2168  return ret;
2169 }
2170 
2171 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2172 {
2173  int ret = 0, i;
2174  DASHContext *c = s->priv_data;
2175  int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2176  s->streams[stream_index]->time_base.den,
2177  flags & AVSEEK_FLAG_BACKWARD ?
2179  if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2180  return AVERROR(ENOSYS);
2181 
2182  /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2183  for (i = 0; i < c->n_videos; i++) {
2184  if (!ret)
2185  ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2186  }
2187  for (i = 0; i < c->n_audios; i++) {
2188  if (!ret)
2189  ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2190  }
2191 
2192  return ret;
2193 }
2194 
2195 static int dash_probe(AVProbeData *p)
2196 {
2197  if (!av_stristr(p->buf, "<MPD"))
2198  return 0;
2199 
2200  if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2201  av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2202  av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2203  av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
2204  return AVPROBE_SCORE_MAX;
2205  }
2206  if (av_stristr(p->buf, "dash:profile")) {
2207  return AVPROBE_SCORE_MAX;
2208  }
2209 
2210  return 0;
2211 }
2212 
2213 #define OFFSET(x) offsetof(DASHContext, x)
2214 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2215 static const AVOption dash_options[] = {
2216  {"allowed_extensions", "List of file extensions that dash is allowed to access",
2217  OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2218  {.str = "aac,m4a,m4s,m4v,mov,mp4"},
2219  INT_MIN, INT_MAX, FLAGS},
2220  {NULL}
2221 };
2222 
2223 static const AVClass dash_class = {
2224  .class_name = "dash",
2225  .item_name = av_default_item_name,
2226  .option = dash_options,
2227  .version = LIBAVUTIL_VERSION_INT,
2228 };
2229 
2231  .name = "dash",
2232  .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2233  .priv_class = &dash_class,
2234  .priv_data_size = sizeof(DASHContext),
2241 };
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition: parseutils.c:568
int64_t cur_seg_size
Definition: dashdec.c:109
#define FLAGS
Definition: dashdec.c:2214
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2504
int64_t probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1517
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:1933
AVIOContext * input
Definition: dashdec.c:78
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:80
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
int64_t url_offset
Definition: dashdec.c:34
int n_fragments
Definition: dashdec.c:92
char * allowed_extensions
Definition: dashdec.c:147
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:179
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1629
AVOption.
Definition: opt.h:246
int n_audios
Definition: dashdec.c:128
static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
Definition: dashdec.c:247
int n_timelines
Definition: dashdec.c:95
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVPacket pkt
Definition: dashdec.c:81
static int read_data(void *opaque, uint8_t *buf, int buf_size)
Definition: dashdec.c:1673
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4899
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:164
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int ishttp(char *url)
Definition: dashdec.c:157
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
#define AVIO_FLAG_READ
read-only
Definition: avio.h:654
int64_t size
Definition: dashdec.c:35
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
static struct fragment * get_current_fragment(struct representation *pls)
Definition: dashdec.c:1491
static int read_from_url(struct representation *pls, struct fragment *seg, uint8_t *buf, int buf_size)
Definition: dashdec.c:1566
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
static const AVOption dash_options[]
Definition: dashdec.c:2215
static int64_t seek_data(void *opaque, int64_t offset, int whence)
Definition: dashdec.c:1663
discard all
Definition: avcodec.h:803
static AVPacket pkt
int64_t cur_timestamp
Definition: dashdec.c:118
int n_videos
Definition: dashdec.c:126
uint64_t availability_end_time
Definition: dashdec.c:135
static int parse_manifest_segmenturlnode(AVFormatContext *s, struct representation *rep, xmlNodePtr fragmenturl_node, xmlNodePtr *baseurl_nodes, char *rep_id_val, char *rep_bandwidth_val)
Definition: dashdec.c:581
int is_init_section_common_audio
Definition: dashdec.c:153
uint64_t min_buffer_time
Definition: dashdec.c:139
static void free_fragment(struct fragment **seg)
Definition: dashdec.c:312
Format I/O context.
Definition: avformat.h:1351
#define MAX_URL_SIZE
Definition: internal.h:30
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
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1927
struct fragment * init_section
Definition: dashdec.c:113
uint32_t init_sec_buf_read_offset
Definition: dashdec.c:117
int stream_index
Definition: dashdec.c:84
static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
Definition: dashdec.c:173
static char buffer[20]
Definition: seek.c:32
static int64_t start_time
Definition: ffplay.c:330
uint64_t suggested_presentation_delay
Definition: dashdec.c:133
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
#define av_malloc(s)
uint64_t media_presentation_duration
Definition: dashdec.c:132
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t presentation_timeoffset
Definition: dashdec.c:105
int id
Format-specific stream ID.
Definition: avformat.h:881
static int dash_close(AVFormatContext *s)
Definition: dashdec.c:2095
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5687
uint64_t period_duration
Definition: dashdec.c:142
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4469
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
int64_t duration
Definition: movenc.c:63
int64_t first_seq_no
Definition: dashdec.c:98
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
AVIOContext pb
Definition: dashdec.c:77
static void finish(void)
Definition: movenc.c:345
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:4568
struct timeline ** timelines
Definition: dashdec.c:96
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:38
uint64_t publish_time
Definition: dashdec.c:136
static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
Definition: dashdec.c:2017
uint64_t availability_start_time
Definition: dashdec.c:134
static enum AVMediaType get_content_type(xmlNodePtr node)
Definition: dashdec.c:539
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
struct representation ** audios
Definition: dashdec.c:129
#define INITIAL_BUFFER_SIZE
Definition: dashdec.c:31
static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
Definition: dashdec.c:522
static int aligned(int val)
Definition: dashdec.c:163
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Like av_probe_input_buffer2() but returns 0 on success.
Definition: format.c:312
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2086
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint32_t init_sec_data_len
Definition: dashdec.c:116
static void free_timelines_list(struct representation *pls)
Definition: dashdec.c:332
int64_t starttime
Definition: dashdec.c:57
static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
Definition: dashdec.c:1388
static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
Definition: dashdec.c:1348
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1296
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1189
char * url
input or output URL.
Definition: avformat.h:1447
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int is_common_init_section_exist(struct representation **pls, int n_pls)
Definition: dashdec.c:1889
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: dashdec.c:2171
char * av_strireplace(const char *str, const char *from, const char *to)
Locale-independent strings replace.
Definition: avstring.c:235
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1268
#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:117
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:500
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
uint64_t minimum_update_period
Definition: dashdec.c:137
struct fragment ** fragments
Definition: dashdec.c:93
static void free_representation(struct representation *pls)
Definition: dashdec.c:343
AVIOInterruptCB * interrupt_callback
Definition: dashdec.c:146
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
static void free_audio_list(DASHContext *c)
Definition: dashdec.c:373
AVDictionary * opts
Definition: movenc.c:50
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:198
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
static void free_fragment_list(struct representation *pls)
Definition: dashdec.c:321
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:556
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int32_t
static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
Definition: dashdec.c:1374
static void copy_init_section(struct representation *rep_dest, struct representation *rep_src)
Definition: dashdec.c:1911
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
Definition: dashdec.c:203
#define s(width, name)
Definition: cbs_vp9.c:257
int is_live
Definition: dashdec.c:145
#define OFFSET(x)
Definition: dashdec.c:2213
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary *opts, AVDictionary *opts2, int *is_http)
Definition: dashdec.c:384
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
Definition: dashdec.c:1110
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don&#39;t avio_close() it.
Definition: avformat.h:1490
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static int save_avio_options(AVFormatContext *s)
Definition: dashdec.c:1736
char * url
Definition: dashdec.c:36
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
uint64_t period_start
Definition: dashdec.c:143
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition: avformat.h:1525
static char * get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
Definition: dashdec.c:506
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition: dashdec.c:1761
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:548
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static int dash_probe(AVProbeData *p)
Definition: dashdec.c:2195
Stream structure.
Definition: avformat.h:874
void ff_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: dash.c:96
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVFormatContext * parent
Definition: dashdec.c:79
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:667
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
int64_t last_seq_no
Definition: dashdec.c:99
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
uint32_t init_sec_buf_size
Definition: dashdec.c:115
int64_t cur_seq_no
Definition: dashdec.c:107
int max_url_size
Definition: dashdec.c:149
static int dash_read_header(AVFormatContext *s)
Definition: dashdec.c:1921
void * buf
Definition: avisynth_c.h:690
uint64_t time_shift_buffer_depth
Definition: dashdec.c:138
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:70
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
double value
Definition: eval.c:98
Describe the class of an AVClass context structure.
Definition: log.h:67
static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
Definition: dashdec.c:1582
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dashdec.c:2043
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes)
Definition: dashdec.c:684
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2505
cl_device_type type
AVMediaType
Definition: avutil.h:199
static struct fragment * get_Fragment(char *range)
Definition: dashdec.c:563
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep, xmlNodePtr fragment_timeline_node)
Definition: dashdec.c:648
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1177
char id[20]
Definition: dashdec.c:87
AVDictionary * avio_opts
Definition: dashdec.c:148
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4403
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1777
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
Round toward -infinity.
Definition: mathematics.h:82
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:476
#define flags(name, subs,...)
Definition: cbs_av1.c:610
AVInputFormat ff_dash_demuxer
Definition: dashdec.c:2230
static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
Definition: dashdec.c:2106
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2517
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
static int update_init_section(struct representation *pls)
Definition: dashdec.c:1616
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
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
int
int64_t duration
Definition: dashdec.c:67
int ffio_init_context(AVIOContext *s, 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))
Definition: aviobuf.c:81
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3578
static const AVClass dash_class
Definition: dashdec.c:2223
static double c[64]
int64_t fragment_duration
Definition: dashdec.c:102
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
struct fragment * cur_seg
Definition: dashdec.c:110
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1066
int bandwidth
Definition: dashdec.c:88
int den
Denominator.
Definition: rational.h:60
AVFormatContext * ctx
Definition: dashdec.c:80
int rep_count
Definition: dashdec.c:83
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4441
int64_t fragment_timescale
Definition: dashdec.c:103
static void close_demux_for_component(struct representation *pls)
Definition: dashdec.c:1771
int is_restart_needed
Definition: dashdec.c:119
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:761
#define av_free(p)
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:477
static int parse_manifest_adaptationset(AVFormatContext *s, const char *url, xmlNodePtr adaptionset_node, xmlNodePtr mpd_baseurl_node, xmlNodePtr period_baseurl_node, xmlNodePtr period_segmenttemplate_node, xmlNodePtr period_segmentlist_node)
Definition: dashdec.c:1062
uint8_t * init_sec_buf
Definition: dashdec.c:114
static char * get_content_url(xmlNodePtr *baseurl_nodes, int n_baseurl_nodes, int max_url_size, char *rep_id_val, char *rep_bandwidth_val, char *val)
Definition: dashdec.c:450
AVRational framerate
Definition: dashdec.c:89
void * priv_data
Format private data.
Definition: avformat.h:1379
int64_t start_number
Definition: dashdec.c:100
static uint64_t get_current_time_in_sec(void)
Definition: dashdec.c:168
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:540
static int parse_manifest_representation(AVFormatContext *s, const char *url, xmlNodePtr node, xmlNodePtr adaptionset_node, xmlNodePtr mpd_baseurl_node, xmlNodePtr period_baseurl_node, xmlNodePtr period_segmenttemplate_node, xmlNodePtr period_segmentlist_node, xmlNodePtr fragment_template_node, xmlNodePtr content_component_node, xmlNodePtr adaptionset_baseurl_node, xmlNodePtr adaptionset_segmentlist_node, xmlNodePtr adaptionset_supplementalproperty_node)
Definition: dashdec.c:794
static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1334
int64_t cur_seg_offset
Definition: dashdec.c:108
struct representation ** videos
Definition: dashdec.c:127
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1466
static void free_video_list(DASHContext *c)
Definition: dashdec.c:362
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
char * url_template
Definition: dashdec.c:76
int is_init_section_common_video
Definition: dashdec.c:152
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1781
int64_t repeat
Definition: dashdec.c:63
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:929
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:998
This structure stores compressed data.
Definition: avcodec.h:1422
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
Definition: dashdec.c:282
char * base_url
Definition: dashdec.c:124
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1856
AVStream * assoc_stream
Definition: dashdec.c:90
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:127
static int refresh_manifest(AVFormatContext *s)
Definition: dashdec.c:1406
static uint8_t tmp[11]
Definition: aes_ctr.c:26