FFmpeg  4.0.3
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 */
135  uint64_t publish_time;
138  uint64_t min_buffer_time;
139 
140  /* Period Attribute */
141  uint64_t period_duration;
142  uint64_t period_start;
143 
144  int is_live;
146  char *user_agent; ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
147  char *cookies; ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
148  char *headers; ///< holds HTTP headers set as an AVOption to the HTTP protocol context
152 } DASHContext;
153 
154 static int ishttp(char *url)
155 {
156  const char *proto_name = avio_find_protocol_name(url);
157  return av_strstart(proto_name, "http", NULL);
158 }
159 
160 static int aligned(int val)
161 {
162  return ((val + 0x3F) >> 6) << 6;
163 }
164 
165 static uint64_t get_current_time_in_sec(void)
166 {
167  return av_gettime() / 1000000;
168 }
169 
170 static uint64_t get_utc_date_time_insec(AVFormatContext *s, const char *datetime)
171 {
172  struct tm timeinfo;
173  int year = 0;
174  int month = 0;
175  int day = 0;
176  int hour = 0;
177  int minute = 0;
178  int ret = 0;
179  float second = 0.0;
180 
181  /* ISO-8601 date parser */
182  if (!datetime)
183  return 0;
184 
185  ret = sscanf(datetime, "%d-%d-%dT%d:%d:%fZ", &year, &month, &day, &hour, &minute, &second);
186  /* year, month, day, hour, minute, second 6 arguments */
187  if (ret != 6) {
188  av_log(s, AV_LOG_WARNING, "get_utc_date_time_insec get a wrong time format\n");
189  }
190  timeinfo.tm_year = year - 1900;
191  timeinfo.tm_mon = month - 1;
192  timeinfo.tm_mday = day;
193  timeinfo.tm_hour = hour;
194  timeinfo.tm_min = minute;
195  timeinfo.tm_sec = (int)second;
196 
197  return av_timegm(&timeinfo);
198 }
199 
200 static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
201 {
202  /* ISO-8601 duration parser */
203  uint32_t days = 0;
204  uint32_t hours = 0;
205  uint32_t mins = 0;
206  uint32_t secs = 0;
207  int size = 0;
208  float value = 0;
209  char type = '\0';
210  const char *ptr = duration;
211 
212  while (*ptr) {
213  if (*ptr == 'P' || *ptr == 'T') {
214  ptr++;
215  continue;
216  }
217 
218  if (sscanf(ptr, "%f%c%n", &value, &type, &size) != 2) {
219  av_log(s, AV_LOG_WARNING, "get_duration_insec get a wrong time format\n");
220  return 0; /* parser error */
221  }
222  switch (type) {
223  case 'D':
224  days = (uint32_t)value;
225  break;
226  case 'H':
227  hours = (uint32_t)value;
228  break;
229  case 'M':
230  mins = (uint32_t)value;
231  break;
232  case 'S':
233  secs = (uint32_t)value;
234  break;
235  default:
236  // handle invalid type
237  break;
238  }
239  ptr += size;
240  }
241  return ((days * 24 + hours) * 60 + mins) * 60 + secs;
242 }
243 
244 static int64_t get_segment_start_time_based_on_timeline(struct representation *pls, int64_t cur_seq_no)
245 {
246  int64_t start_time = 0;
247  int64_t i = 0;
248  int64_t j = 0;
249  int64_t num = 0;
250 
251  if (pls->n_timelines) {
252  for (i = 0; i < pls->n_timelines; i++) {
253  if (pls->timelines[i]->starttime > 0) {
254  start_time = pls->timelines[i]->starttime;
255  }
256  if (num == cur_seq_no)
257  goto finish;
258 
259  start_time += pls->timelines[i]->duration;
260  for (j = 0; j < pls->timelines[i]->repeat; j++) {
261  num++;
262  if (num == cur_seq_no)
263  goto finish;
264  start_time += pls->timelines[i]->duration;
265  }
266  num++;
267  }
268  }
269 finish:
270  return start_time;
271 }
272 
273 static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
274 {
275  int64_t i = 0;
276  int64_t j = 0;
277  int64_t num = 0;
278  int64_t start_time = 0;
279 
280  for (i = 0; i < pls->n_timelines; i++) {
281  if (pls->timelines[i]->starttime > 0) {
282  start_time = pls->timelines[i]->starttime;
283  }
284  if (start_time > cur_time)
285  goto finish;
286 
287  start_time += pls->timelines[i]->duration;
288  for (j = 0; j < pls->timelines[i]->repeat; j++) {
289  num++;
290  if (start_time > cur_time)
291  goto finish;
292  start_time += pls->timelines[i]->duration;
293  }
294  num++;
295  }
296 
297  return -1;
298 
299 finish:
300  return num;
301 }
302 
303 static void free_fragment(struct fragment **seg)
304 {
305  if (!(*seg)) {
306  return;
307  }
308  av_freep(&(*seg)->url);
309  av_freep(seg);
310 }
311 
312 static void free_fragment_list(struct representation *pls)
313 {
314  int i;
315 
316  for (i = 0; i < pls->n_fragments; i++) {
317  free_fragment(&pls->fragments[i]);
318  }
319  av_freep(&pls->fragments);
320  pls->n_fragments = 0;
321 }
322 
323 static void free_timelines_list(struct representation *pls)
324 {
325  int i;
326 
327  for (i = 0; i < pls->n_timelines; i++) {
328  av_freep(&pls->timelines[i]);
329  }
330  av_freep(&pls->timelines);
331  pls->n_timelines = 0;
332 }
333 
334 static void free_representation(struct representation *pls)
335 {
336  free_fragment_list(pls);
337  free_timelines_list(pls);
338  free_fragment(&pls->cur_seg);
340  av_freep(&pls->init_sec_buf);
341  av_freep(&pls->pb.buffer);
342  if (pls->input)
343  ff_format_io_close(pls->parent, &pls->input);
344  if (pls->ctx) {
345  pls->ctx->pb = NULL;
346  avformat_close_input(&pls->ctx);
347  }
348 
349  av_freep(&pls->url_template);
350  av_freep(&pls);
351 }
352 
354 {
355  int i;
356  for (i = 0; i < c->n_videos; i++) {
357  struct representation *pls = c->videos[i];
358  free_representation(pls);
359  }
360  av_freep(&c->videos);
361  c->n_videos = 0;
362 }
363 
365 {
366  int i;
367  for (i = 0; i < c->n_audios; i++) {
368  struct representation *pls = c->audios[i];
369  free_representation(pls);
370  }
371  av_freep(&c->audios);
372  c->n_audios = 0;
373 }
374 
376 {
377  // broker prior HTTP options that should be consistent across requests
378  av_dict_set(opts, "user-agent", c->user_agent, 0);
379  av_dict_set(opts, "cookies", c->cookies, 0);
380  av_dict_set(opts, "headers", c->headers, 0);
381  if (c->is_live) {
382  av_dict_set(opts, "seekable", "0", 0);
383  }
384 }
385 static void update_options(char **dest, const char *name, void *src)
386 {
387  av_freep(dest);
388  av_opt_get(src, name, AV_OPT_SEARCH_CHILDREN, (uint8_t**)dest);
389  if (*dest)
390  av_freep(dest);
391 }
392 
393 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
394  AVDictionary *opts, AVDictionary *opts2, int *is_http)
395 {
396  DASHContext *c = s->priv_data;
397  AVDictionary *tmp = NULL;
398  const char *proto_name = NULL;
399  int ret;
400 
401  av_dict_copy(&tmp, opts, 0);
402  av_dict_copy(&tmp, opts2, 0);
403 
404  if (av_strstart(url, "crypto", NULL)) {
405  if (url[6] == '+' || url[6] == ':')
406  proto_name = avio_find_protocol_name(url + 7);
407  }
408 
409  if (!proto_name)
410  proto_name = avio_find_protocol_name(url);
411 
412  if (!proto_name)
413  return AVERROR_INVALIDDATA;
414 
415  // only http(s) & file are allowed
416  if (av_strstart(proto_name, "file", NULL)) {
417  if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
418  av_log(s, AV_LOG_ERROR,
419  "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
420  "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
421  url);
422  return AVERROR_INVALIDDATA;
423  }
424  } else if (av_strstart(proto_name, "http", NULL)) {
425  ;
426  } else
427  return AVERROR_INVALIDDATA;
428 
429  if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
430  ;
431  else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
432  ;
433  else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
434  return AVERROR_INVALIDDATA;
435 
436  av_freep(pb);
437  ret = avio_open2(pb, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
438  if (ret >= 0) {
439  // update cookies on http response with setcookies.
440  char *new_cookies = NULL;
441 
442  if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
443  av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
444 
445  if (new_cookies) {
446  av_free(c->cookies);
447  c->cookies = new_cookies;
448  }
449 
450  av_dict_set(&opts, "cookies", c->cookies, 0);
451  }
452 
453  av_dict_free(&tmp);
454 
455  if (is_http)
456  *is_http = av_strstart(proto_name, "http", NULL);
457 
458  return ret;
459 }
460 
461 static char *get_content_url(xmlNodePtr *baseurl_nodes,
462  int n_baseurl_nodes,
463  int max_url_size,
464  char *rep_id_val,
465  char *rep_bandwidth_val,
466  char *val)
467 {
468  int i;
469  char *text;
470  char *url = NULL;
471  char *tmp_str = av_mallocz(max_url_size);
472  char *tmp_str_2 = av_mallocz(max_url_size);
473 
474  if (!tmp_str || !tmp_str_2) {
475  return NULL;
476  }
477 
478  for (i = 0; i < n_baseurl_nodes; ++i) {
479  if (baseurl_nodes[i] &&
480  baseurl_nodes[i]->children &&
481  baseurl_nodes[i]->children->type == XML_TEXT_NODE) {
482  text = xmlNodeGetContent(baseurl_nodes[i]->children);
483  if (text) {
484  memset(tmp_str, 0, max_url_size);
485  memset(tmp_str_2, 0, max_url_size);
486  ff_make_absolute_url(tmp_str_2, max_url_size, tmp_str, text);
487  av_strlcpy(tmp_str, tmp_str_2, max_url_size);
488  xmlFree(text);
489  }
490  }
491  }
492 
493  if (val)
494  av_strlcat(tmp_str, (const char*)val, max_url_size);
495 
496  if (rep_id_val) {
497  url = av_strireplace(tmp_str, "$RepresentationID$", (const char*)rep_id_val);
498  if (!url) {
499  goto end;
500  }
501  av_strlcpy(tmp_str, url, max_url_size);
502  }
503  if (rep_bandwidth_val && tmp_str[0] != '\0') {
504  // free any previously assigned url before reassigning
505  av_free(url);
506  url = av_strireplace(tmp_str, "$Bandwidth$", (const char*)rep_bandwidth_val);
507  if (!url) {
508  goto end;
509  }
510  }
511 end:
512  av_free(tmp_str);
513  av_free(tmp_str_2);
514  return url;
515 }
516 
517 static char *get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
518 {
519  int i;
520  char *val;
521 
522  for (i = 0; i < n_nodes; ++i) {
523  if (nodes[i]) {
524  val = xmlGetProp(nodes[i], attrname);
525  if (val)
526  return val;
527  }
528  }
529 
530  return NULL;
531 }
532 
533 static xmlNodePtr find_child_node_by_name(xmlNodePtr rootnode, const char *nodename)
534 {
535  xmlNodePtr node = rootnode;
536  if (!node) {
537  return NULL;
538  }
539 
540  node = xmlFirstElementChild(node);
541  while (node) {
542  if (!av_strcasecmp(node->name, nodename)) {
543  return node;
544  }
545  node = xmlNextElementSibling(node);
546  }
547  return NULL;
548 }
549 
550 static enum AVMediaType get_content_type(xmlNodePtr node)
551 {
553  int i = 0;
554  const char *attr;
555  char *val = NULL;
556 
557  if (node) {
558  for (i = 0; i < 2; i++) {
559  attr = i ? "mimeType" : "contentType";
560  val = xmlGetProp(node, attr);
561  if (val) {
562  if (av_stristr((const char *)val, "video")) {
563  type = AVMEDIA_TYPE_VIDEO;
564  } else if (av_stristr((const char *)val, "audio")) {
565  type = AVMEDIA_TYPE_AUDIO;
566  }
567  xmlFree(val);
568  }
569  }
570  }
571  return type;
572 }
573 
574 static struct fragment * get_Fragment(char *range)
575 {
576  struct fragment * seg = av_mallocz(sizeof(struct fragment));
577 
578  if (!seg)
579  return NULL;
580 
581  seg->size = -1;
582  if (range) {
583  char *str_end_offset;
584  char *str_offset = av_strtok(range, "-", &str_end_offset);
585  seg->url_offset = strtoll(str_offset, NULL, 10);
586  seg->size = strtoll(str_end_offset, NULL, 10) - seg->url_offset;
587  }
588 
589  return seg;
590 }
591 
593  xmlNodePtr fragmenturl_node,
594  xmlNodePtr *baseurl_nodes,
595  char *rep_id_val,
596  char *rep_bandwidth_val)
597 {
598  DASHContext *c = s->priv_data;
599  char *initialization_val = NULL;
600  char *media_val = NULL;
601  char *range_val = NULL;
602  int max_url_size = c ? c->max_url_size: MAX_URL_SIZE;
603 
604  if (!av_strcasecmp(fragmenturl_node->name, (const char *)"Initialization")) {
605  initialization_val = xmlGetProp(fragmenturl_node, "sourceURL");
606  range_val = xmlGetProp(fragmenturl_node, "range");
607  if (initialization_val || range_val) {
608  rep->init_section = get_Fragment(range_val);
609  if (!rep->init_section) {
610  xmlFree(initialization_val);
611  xmlFree(range_val);
612  return AVERROR(ENOMEM);
613  }
614  rep->init_section->url = get_content_url(baseurl_nodes, 4,
615  max_url_size,
616  rep_id_val,
617  rep_bandwidth_val,
618  initialization_val);
619 
620  if (!rep->init_section->url) {
621  av_free(rep->init_section);
622  xmlFree(initialization_val);
623  xmlFree(range_val);
624  return AVERROR(ENOMEM);
625  }
626  xmlFree(initialization_val);
627  xmlFree(range_val);
628  }
629  } else if (!av_strcasecmp(fragmenturl_node->name, (const char *)"SegmentURL")) {
630  media_val = xmlGetProp(fragmenturl_node, "media");
631  range_val = xmlGetProp(fragmenturl_node, "mediaRange");
632  if (media_val || range_val) {
633  struct fragment *seg = get_Fragment(range_val);
634  if (!seg) {
635  xmlFree(media_val);
636  xmlFree(range_val);
637  return AVERROR(ENOMEM);
638  }
639  seg->url = get_content_url(baseurl_nodes, 4,
640  max_url_size,
641  rep_id_val,
642  rep_bandwidth_val,
643  media_val);
644  if (!seg->url) {
645  av_free(seg);
646  xmlFree(media_val);
647  xmlFree(range_val);
648  return AVERROR(ENOMEM);
649  }
650  dynarray_add(&rep->fragments, &rep->n_fragments, seg);
651  xmlFree(media_val);
652  xmlFree(range_val);
653  }
654  }
655 
656  return 0;
657 }
658 
660  xmlNodePtr fragment_timeline_node)
661 {
662  xmlAttrPtr attr = NULL;
663  char *val = NULL;
664 
665  if (!av_strcasecmp(fragment_timeline_node->name, (const char *)"S")) {
666  struct timeline *tml = av_mallocz(sizeof(struct timeline));
667  if (!tml) {
668  return AVERROR(ENOMEM);
669  }
670  attr = fragment_timeline_node->properties;
671  while (attr) {
672  val = xmlGetProp(fragment_timeline_node, attr->name);
673 
674  if (!val) {
675  av_log(s, AV_LOG_WARNING, "parse_manifest_segmenttimeline attr->name = %s val is NULL\n", attr->name);
676  continue;
677  }
678 
679  if (!av_strcasecmp(attr->name, (const char *)"t")) {
680  tml->starttime = (int64_t)strtoll(val, NULL, 10);
681  } else if (!av_strcasecmp(attr->name, (const char *)"r")) {
682  tml->repeat =(int64_t) strtoll(val, NULL, 10);
683  } else if (!av_strcasecmp(attr->name, (const char *)"d")) {
684  tml->duration = (int64_t)strtoll(val, NULL, 10);
685  }
686  attr = attr->next;
687  xmlFree(val);
688  }
689  dynarray_add(&rep->timelines, &rep->n_timelines, tml);
690  }
691 
692  return 0;
693 }
694 
695 static int resolve_content_path(AVFormatContext *s, const char *url, int *max_url_size, xmlNodePtr *baseurl_nodes, int n_baseurl_nodes) {
696 
697  char *tmp_str = NULL;
698  char *path = NULL;
699  char *mpdName = NULL;
700  xmlNodePtr node = NULL;
701  char *baseurl = NULL;
702  char *root_url = NULL;
703  char *text = NULL;
704 
705  int isRootHttp = 0;
706  char token ='/';
707  int start = 0;
708  int rootId = 0;
709  int updated = 0;
710  int size = 0;
711  int i;
712  int tmp_max_url_size = strlen(url);
713 
714  for (i = n_baseurl_nodes-1; i >= 0 ; i--) {
715  text = xmlNodeGetContent(baseurl_nodes[i]);
716  if (!text)
717  continue;
718  tmp_max_url_size += strlen(text);
719  if (ishttp(text)) {
720  xmlFree(text);
721  break;
722  }
723  xmlFree(text);
724  }
725 
726  tmp_max_url_size = aligned(tmp_max_url_size);
727  text = av_mallocz(tmp_max_url_size);
728  if (!text) {
729  updated = AVERROR(ENOMEM);
730  goto end;
731  }
732  av_strlcpy(text, url, strlen(url)+1);
733  while (mpdName = av_strtok(text, "/", &text)) {
734  size = strlen(mpdName);
735  }
736 
737  path = av_mallocz(tmp_max_url_size);
738  tmp_str = av_mallocz(tmp_max_url_size);
739  if (!tmp_str || !path) {
740  updated = AVERROR(ENOMEM);
741  goto end;
742  }
743 
744  av_strlcpy (path, url, strlen(url) - size + 1);
745  for (rootId = n_baseurl_nodes - 1; rootId > 0; rootId --) {
746  if (!(node = baseurl_nodes[rootId])) {
747  continue;
748  }
749  if (ishttp(xmlNodeGetContent(node))) {
750  break;
751  }
752  }
753 
754  node = baseurl_nodes[rootId];
755  baseurl = xmlNodeGetContent(node);
756  root_url = (av_strcasecmp(baseurl, "")) ? baseurl : path;
757  if (node) {
758  xmlNodeSetContent(node, root_url);
759  updated = 1;
760  }
761 
762  size = strlen(root_url);
763  isRootHttp = ishttp(root_url);
764 
765  if (root_url[size - 1] != token) {
766  av_strlcat(root_url, "/", size + 2);
767  size += 2;
768  }
769 
770  for (i = 0; i < n_baseurl_nodes; ++i) {
771  if (i == rootId) {
772  continue;
773  }
774  text = xmlNodeGetContent(baseurl_nodes[i]);
775  if (text) {
776  memset(tmp_str, 0, strlen(tmp_str));
777  if (!ishttp(text) && isRootHttp) {
778  av_strlcpy(tmp_str, root_url, size + 1);
779  }
780  start = (text[0] == token);
781  av_strlcat(tmp_str, text + start, tmp_max_url_size);
782  xmlNodeSetContent(baseurl_nodes[i], tmp_str);
783  updated = 1;
784  xmlFree(text);
785  }
786  }
787 
788 end:
789  if (tmp_max_url_size > *max_url_size) {
790  *max_url_size = tmp_max_url_size;
791  }
792  av_free(path);
793  av_free(tmp_str);
794  return updated;
795 
796 }
797 
799  xmlNodePtr node,
800  xmlNodePtr adaptionset_node,
801  xmlNodePtr mpd_baseurl_node,
802  xmlNodePtr period_baseurl_node,
803  xmlNodePtr period_segmenttemplate_node,
804  xmlNodePtr period_segmentlist_node,
805  xmlNodePtr fragment_template_node,
806  xmlNodePtr content_component_node,
807  xmlNodePtr adaptionset_baseurl_node,
808  xmlNodePtr adaptionset_segmentlist_node,
809  xmlNodePtr adaptionset_supplementalproperty_node)
810 {
811  int32_t ret = 0;
812  int32_t audio_rep_idx = 0;
813  int32_t video_rep_idx = 0;
814  DASHContext *c = s->priv_data;
815  struct representation *rep = NULL;
816  struct fragment *seg = NULL;
817  xmlNodePtr representation_segmenttemplate_node = NULL;
818  xmlNodePtr representation_baseurl_node = NULL;
819  xmlNodePtr representation_segmentlist_node = NULL;
820  xmlNodePtr segmentlists_tab[2];
821  xmlNodePtr fragment_timeline_node = NULL;
822  xmlNodePtr fragment_templates_tab[5];
823  char *duration_val = NULL;
824  char *presentation_timeoffset_val = NULL;
825  char *startnumber_val = NULL;
826  char *timescale_val = NULL;
827  char *initialization_val = NULL;
828  char *media_val = NULL;
829  char *val = NULL;
830  xmlNodePtr baseurl_nodes[4];
831  xmlNodePtr representation_node = node;
832  char *rep_id_val = xmlGetProp(representation_node, "id");
833  char *rep_bandwidth_val = xmlGetProp(representation_node, "bandwidth");
834  char *rep_framerate_val = xmlGetProp(representation_node, "frameRate");
836 
837  // try get information from representation
838  if (type == AVMEDIA_TYPE_UNKNOWN)
839  type = get_content_type(representation_node);
840  // try get information from contentComponen
841  if (type == AVMEDIA_TYPE_UNKNOWN)
842  type = get_content_type(content_component_node);
843  // try get information from adaption set
844  if (type == AVMEDIA_TYPE_UNKNOWN)
845  type = get_content_type(adaptionset_node);
846  if (type == AVMEDIA_TYPE_UNKNOWN) {
847  av_log(s, AV_LOG_VERBOSE, "Parsing '%s' - skipp not supported representation type\n", url);
848  } else if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) {
849  // convert selected representation to our internal struct
850  rep = av_mallocz(sizeof(struct representation));
851  if (!rep) {
852  ret = AVERROR(ENOMEM);
853  goto end;
854  }
855  representation_segmenttemplate_node = find_child_node_by_name(representation_node, "SegmentTemplate");
856  representation_baseurl_node = find_child_node_by_name(representation_node, "BaseURL");
857  representation_segmentlist_node = find_child_node_by_name(representation_node, "SegmentList");
858 
859  baseurl_nodes[0] = mpd_baseurl_node;
860  baseurl_nodes[1] = period_baseurl_node;
861  baseurl_nodes[2] = adaptionset_baseurl_node;
862  baseurl_nodes[3] = representation_baseurl_node;
863 
864  ret = resolve_content_path(s, url, &c->max_url_size, baseurl_nodes, 4);
866  + (rep_id_val ? strlen(rep_id_val) : 0)
867  + (rep_bandwidth_val ? strlen(rep_bandwidth_val) : 0));
868  if (ret == AVERROR(ENOMEM) || ret == 0) {
869  goto end;
870  }
871  if (representation_segmenttemplate_node || fragment_template_node || period_segmenttemplate_node) {
872  fragment_timeline_node = NULL;
873  fragment_templates_tab[0] = representation_segmenttemplate_node;
874  fragment_templates_tab[1] = adaptionset_segmentlist_node;
875  fragment_templates_tab[2] = fragment_template_node;
876  fragment_templates_tab[3] = period_segmenttemplate_node;
877  fragment_templates_tab[4] = period_segmentlist_node;
878 
879  presentation_timeoffset_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "presentationTimeOffset");
880  duration_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "duration");
881  startnumber_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "startNumber");
882  timescale_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "timescale");
883  initialization_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "initialization");
884  media_val = get_val_from_nodes_tab(fragment_templates_tab, 4, "media");
885 
886  if (initialization_val) {
887  rep->init_section = av_mallocz(sizeof(struct fragment));
888  if (!rep->init_section) {
889  av_free(rep);
890  ret = AVERROR(ENOMEM);
891  goto end;
892  }
893  c->max_url_size = aligned(c->max_url_size + strlen(initialization_val));
894  rep->init_section->url = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, initialization_val);
895  if (!rep->init_section->url) {
896  av_free(rep->init_section);
897  av_free(rep);
898  ret = AVERROR(ENOMEM);
899  goto end;
900  }
901  rep->init_section->size = -1;
902  xmlFree(initialization_val);
903  }
904 
905  if (media_val) {
906  c->max_url_size = aligned(c->max_url_size + strlen(media_val));
907  rep->url_template = get_content_url(baseurl_nodes, 4, c->max_url_size, rep_id_val, rep_bandwidth_val, media_val);
908  xmlFree(media_val);
909  }
910 
911  if (presentation_timeoffset_val) {
912  rep->presentation_timeoffset = (int64_t) strtoll(presentation_timeoffset_val, NULL, 10);
913  xmlFree(presentation_timeoffset_val);
914  }
915  if (duration_val) {
916  rep->fragment_duration = (int64_t) strtoll(duration_val, NULL, 10);
917  xmlFree(duration_val);
918  }
919  if (timescale_val) {
920  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
921  xmlFree(timescale_val);
922  }
923  if (startnumber_val) {
924  rep->first_seq_no = (int64_t) strtoll(startnumber_val, NULL, 10);
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  xmlFree(duration_val);
983  }
984  if (timescale_val) {
985  rep->fragment_timescale = (int64_t) strtoll(timescale_val, NULL, 10);
986  xmlFree(timescale_val);
987  }
988  fragmenturl_node = xmlFirstElementChild(representation_segmentlist_node);
989  while (fragmenturl_node) {
990  ret = parse_manifest_segmenturlnode(s, rep, fragmenturl_node,
991  baseurl_nodes,
992  rep_id_val,
993  rep_bandwidth_val);
994  if (ret < 0) {
995  return ret;
996  }
997  fragmenturl_node = xmlNextElementSibling(fragmenturl_node);
998  }
999 
1000  fragment_timeline_node = find_child_node_by_name(representation_segmenttemplate_node, "SegmentTimeline");
1001 
1002  if (!fragment_timeline_node)
1003  fragment_timeline_node = find_child_node_by_name(fragment_template_node, "SegmentTimeline");
1004  if (!fragment_timeline_node)
1005  fragment_timeline_node = find_child_node_by_name(adaptionset_segmentlist_node, "SegmentTimeline");
1006  if (!fragment_timeline_node)
1007  fragment_timeline_node = find_child_node_by_name(period_segmentlist_node, "SegmentTimeline");
1008  if (fragment_timeline_node) {
1009  fragment_timeline_node = xmlFirstElementChild(fragment_timeline_node);
1010  while (fragment_timeline_node) {
1011  ret = parse_manifest_segmenttimeline(s, rep, fragment_timeline_node);
1012  if (ret < 0) {
1013  return ret;
1014  }
1015  fragment_timeline_node = xmlNextElementSibling(fragment_timeline_node);
1016  }
1017  }
1018  } else {
1019  free_representation(rep);
1020  rep = NULL;
1021  av_log(s, AV_LOG_ERROR, "Unknown format of Representation node id[%s] \n", (const char *)rep_id_val);
1022  }
1023 
1024  if (rep) {
1025  if (rep->fragment_duration > 0 && !rep->fragment_timescale)
1026  rep->fragment_timescale = 1;
1027  rep->bandwidth = rep_bandwidth_val ? atoi(rep_bandwidth_val) : 0;
1028  strncpy(rep->id, rep_id_val ? rep_id_val : "", sizeof(rep->id));
1029  rep->framerate = av_make_q(0, 0);
1030  if (type == AVMEDIA_TYPE_VIDEO && rep_framerate_val) {
1031  ret = av_parse_video_rate(&rep->framerate, rep_framerate_val);
1032  if (ret < 0)
1033  av_log(s, AV_LOG_VERBOSE, "Ignoring invalid frame rate '%s'\n", rep_framerate_val);
1034  }
1035 
1036  if (type == AVMEDIA_TYPE_VIDEO) {
1037  rep->rep_idx = video_rep_idx;
1038  dynarray_add(&c->videos, &c->n_videos, rep);
1039  } else {
1040  rep->rep_idx = audio_rep_idx;
1041  dynarray_add(&c->audios, &c->n_audios, rep);
1042  }
1043  }
1044  }
1045 
1046  video_rep_idx += type == AVMEDIA_TYPE_VIDEO;
1047  audio_rep_idx += type == AVMEDIA_TYPE_AUDIO;
1048 
1049 end:
1050  if (rep_id_val)
1051  xmlFree(rep_id_val);
1052  if (rep_bandwidth_val)
1053  xmlFree(rep_bandwidth_val);
1054  if (rep_framerate_val)
1055  xmlFree(rep_framerate_val);
1056 
1057  return ret;
1058 }
1059 
1061  xmlNodePtr adaptionset_node,
1062  xmlNodePtr mpd_baseurl_node,
1063  xmlNodePtr period_baseurl_node,
1064  xmlNodePtr period_segmenttemplate_node,
1065  xmlNodePtr period_segmentlist_node)
1066 {
1067  int ret = 0;
1068  xmlNodePtr fragment_template_node = NULL;
1069  xmlNodePtr content_component_node = NULL;
1070  xmlNodePtr adaptionset_baseurl_node = NULL;
1071  xmlNodePtr adaptionset_segmentlist_node = NULL;
1072  xmlNodePtr adaptionset_supplementalproperty_node = NULL;
1073  xmlNodePtr node = NULL;
1074 
1075  node = xmlFirstElementChild(adaptionset_node);
1076  while (node) {
1077  if (!av_strcasecmp(node->name, (const char *)"SegmentTemplate")) {
1078  fragment_template_node = node;
1079  } else if (!av_strcasecmp(node->name, (const char *)"ContentComponent")) {
1080  content_component_node = node;
1081  } else if (!av_strcasecmp(node->name, (const char *)"BaseURL")) {
1082  adaptionset_baseurl_node = node;
1083  } else if (!av_strcasecmp(node->name, (const char *)"SegmentList")) {
1084  adaptionset_segmentlist_node = node;
1085  } else if (!av_strcasecmp(node->name, (const char *)"SupplementalProperty")) {
1086  adaptionset_supplementalproperty_node = node;
1087  } else if (!av_strcasecmp(node->name, (const char *)"Representation")) {
1088  ret = parse_manifest_representation(s, url, node,
1089  adaptionset_node,
1090  mpd_baseurl_node,
1091  period_baseurl_node,
1092  period_segmenttemplate_node,
1093  period_segmentlist_node,
1094  fragment_template_node,
1095  content_component_node,
1096  adaptionset_baseurl_node,
1097  adaptionset_segmentlist_node,
1098  adaptionset_supplementalproperty_node);
1099  if (ret < 0) {
1100  return ret;
1101  }
1102  }
1103  node = xmlNextElementSibling(node);
1104  }
1105  return 0;
1106 }
1107 
1108 static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
1109 {
1110  DASHContext *c = s->priv_data;
1111  int ret = 0;
1112  int close_in = 0;
1113  uint8_t *new_url = NULL;
1114  int64_t filesize = 0;
1115  char *buffer = NULL;
1116  AVDictionary *opts = NULL;
1117  xmlDoc *doc = NULL;
1118  xmlNodePtr root_element = NULL;
1119  xmlNodePtr node = NULL;
1120  xmlNodePtr period_node = NULL;
1121  xmlNodePtr mpd_baseurl_node = NULL;
1122  xmlNodePtr period_baseurl_node = NULL;
1123  xmlNodePtr period_segmenttemplate_node = NULL;
1124  xmlNodePtr period_segmentlist_node = NULL;
1125  xmlNodePtr adaptionset_node = NULL;
1126  xmlAttrPtr attr = NULL;
1127  char *val = NULL;
1128  uint32_t period_duration_sec = 0;
1129  uint32_t period_start_sec = 0;
1130 
1131  if (!in) {
1132  close_in = 1;
1133 
1134  set_httpheader_options(c, &opts);
1135  ret = avio_open2(&in, url, AVIO_FLAG_READ, c->interrupt_callback, &opts);
1136  av_dict_free(&opts);
1137  if (ret < 0)
1138  return ret;
1139  }
1140 
1141  if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) {
1142  c->base_url = av_strdup(new_url);
1143  } else {
1144  c->base_url = av_strdup(url);
1145  }
1146 
1147  filesize = avio_size(in);
1148  if (filesize <= 0) {
1149  filesize = 8 * 1024;
1150  }
1151 
1152  buffer = av_mallocz(filesize);
1153  if (!buffer) {
1154  av_free(c->base_url);
1155  return AVERROR(ENOMEM);
1156  }
1157 
1158  filesize = avio_read(in, buffer, filesize);
1159  if (filesize <= 0) {
1160  av_log(s, AV_LOG_ERROR, "Unable to read to offset '%s'\n", url);
1161  ret = AVERROR_INVALIDDATA;
1162  } else {
1163  LIBXML_TEST_VERSION
1164 
1165  doc = xmlReadMemory(buffer, filesize, c->base_url, NULL, 0);
1166  root_element = xmlDocGetRootElement(doc);
1167  node = root_element;
1168 
1169  if (!node) {
1170  ret = AVERROR_INVALIDDATA;
1171  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing root node\n", url);
1172  goto cleanup;
1173  }
1174 
1175  if (node->type != XML_ELEMENT_NODE ||
1176  av_strcasecmp(node->name, (const char *)"MPD")) {
1177  ret = AVERROR_INVALIDDATA;
1178  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - wrong root node name[%s] type[%d]\n", url, node->name, (int)node->type);
1179  goto cleanup;
1180  }
1181 
1182  val = xmlGetProp(node, "type");
1183  if (!val) {
1184  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing type attrib\n", url);
1185  ret = AVERROR_INVALIDDATA;
1186  goto cleanup;
1187  }
1188  if (!av_strcasecmp(val, (const char *)"dynamic"))
1189  c->is_live = 1;
1190  xmlFree(val);
1191 
1192  attr = node->properties;
1193  while (attr) {
1194  val = xmlGetProp(node, attr->name);
1195 
1196  if (!av_strcasecmp(attr->name, (const char *)"availabilityStartTime")) {
1197  c->availability_start_time = get_utc_date_time_insec(s, (const char *)val);
1198  } else if (!av_strcasecmp(attr->name, (const char *)"publishTime")) {
1199  c->publish_time = get_utc_date_time_insec(s, (const char *)val);
1200  } else if (!av_strcasecmp(attr->name, (const char *)"minimumUpdatePeriod")) {
1201  c->minimum_update_period = get_duration_insec(s, (const char *)val);
1202  } else if (!av_strcasecmp(attr->name, (const char *)"timeShiftBufferDepth")) {
1203  c->time_shift_buffer_depth = get_duration_insec(s, (const char *)val);
1204  } else if (!av_strcasecmp(attr->name, (const char *)"minBufferTime")) {
1205  c->min_buffer_time = get_duration_insec(s, (const char *)val);
1206  } else if (!av_strcasecmp(attr->name, (const char *)"suggestedPresentationDelay")) {
1207  c->suggested_presentation_delay = get_duration_insec(s, (const char *)val);
1208  } else if (!av_strcasecmp(attr->name, (const char *)"mediaPresentationDuration")) {
1209  c->media_presentation_duration = get_duration_insec(s, (const char *)val);
1210  }
1211  attr = attr->next;
1212  xmlFree(val);
1213  }
1214 
1215  mpd_baseurl_node = find_child_node_by_name(node, "BaseURL");
1216  if (!mpd_baseurl_node) {
1217  mpd_baseurl_node = xmlNewNode(NULL, "BaseURL");
1218  }
1219 
1220  // at now we can handle only one period, with the longest duration
1221  node = xmlFirstElementChild(node);
1222  while (node) {
1223  if (!av_strcasecmp(node->name, (const char *)"Period")) {
1224  period_duration_sec = 0;
1225  period_start_sec = 0;
1226  attr = node->properties;
1227  while (attr) {
1228  val = xmlGetProp(node, attr->name);
1229  if (!av_strcasecmp(attr->name, (const char *)"duration")) {
1230  period_duration_sec = get_duration_insec(s, (const char *)val);
1231  } else if (!av_strcasecmp(attr->name, (const char *)"start")) {
1232  period_start_sec = get_duration_insec(s, (const char *)val);
1233  }
1234  attr = attr->next;
1235  xmlFree(val);
1236  }
1237  if ((period_duration_sec) >= (c->period_duration)) {
1238  period_node = node;
1239  c->period_duration = period_duration_sec;
1240  c->period_start = period_start_sec;
1241  if (c->period_start > 0)
1243  }
1244  }
1245  node = xmlNextElementSibling(node);
1246  }
1247  if (!period_node) {
1248  av_log(s, AV_LOG_ERROR, "Unable to parse '%s' - missing Period node\n", url);
1249  ret = AVERROR_INVALIDDATA;
1250  goto cleanup;
1251  }
1252 
1253  adaptionset_node = xmlFirstElementChild(period_node);
1254  while (adaptionset_node) {
1255  if (!av_strcasecmp(adaptionset_node->name, (const char *)"BaseURL")) {
1256  period_baseurl_node = adaptionset_node;
1257  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentTemplate")) {
1258  period_segmenttemplate_node = adaptionset_node;
1259  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"SegmentList")) {
1260  period_segmentlist_node = adaptionset_node;
1261  } else if (!av_strcasecmp(adaptionset_node->name, (const char *)"AdaptationSet")) {
1262  parse_manifest_adaptationset(s, url, adaptionset_node, mpd_baseurl_node, period_baseurl_node, period_segmenttemplate_node, period_segmentlist_node);
1263  }
1264  adaptionset_node = xmlNextElementSibling(adaptionset_node);
1265  }
1266 cleanup:
1267  /*free the document */
1268  xmlFreeDoc(doc);
1269  xmlCleanupParser();
1270  }
1271 
1272  av_free(new_url);
1273  av_free(buffer);
1274  if (close_in) {
1275  avio_close(in);
1276  }
1277  return ret;
1278 }
1279 
1280 static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
1281 {
1282  DASHContext *c = s->priv_data;
1283  int64_t num = 0;
1284  int64_t start_time_offset = 0;
1285 
1286  if (c->is_live) {
1287  if (pls->n_fragments) {
1288  num = pls->first_seq_no;
1289  } else if (pls->n_timelines) {
1290  start_time_offset = get_segment_start_time_based_on_timeline(pls, 0xFFFFFFFF) - 60 * pls->fragment_timescale; // 60 seconds before end
1291  num = calc_next_seg_no_from_timelines(pls, start_time_offset);
1292  if (num == -1)
1293  num = pls->first_seq_no;
1294  else
1295  num += pls->first_seq_no;
1296  } else if (pls->fragment_duration){
1297  if (pls->presentation_timeoffset) {
1299  } else if (c->publish_time > 0 && !c->availability_start_time) {
1301  } else {
1303  }
1304  }
1305  } else {
1306  num = pls->first_seq_no;
1307  }
1308  return num;
1309 }
1310 
1311 static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
1312 {
1313  DASHContext *c = s->priv_data;
1314  int64_t num = 0;
1315 
1316  if (c->is_live && pls->fragment_duration) {
1318  } else {
1319  num = pls->first_seq_no;
1320  }
1321  return num;
1322 }
1323 
1324 static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
1325 {
1326  int64_t num = 0;
1327 
1328  if (pls->n_fragments) {
1329  num = pls->first_seq_no + pls->n_fragments - 1;
1330  } else if (pls->n_timelines) {
1331  int i = 0;
1332  num = pls->first_seq_no + pls->n_timelines - 1;
1333  for (i = 0; i < pls->n_timelines; i++) {
1334  num += pls->timelines[i]->repeat;
1335  }
1336  } else if (c->is_live && pls->fragment_duration) {
1338  } else if (pls->fragment_duration) {
1340  }
1341 
1342  return num;
1343 }
1344 
1345 static void move_timelines(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1346 {
1347  if (rep_dest && rep_src ) {
1348  free_timelines_list(rep_dest);
1349  rep_dest->timelines = rep_src->timelines;
1350  rep_dest->n_timelines = rep_src->n_timelines;
1351  rep_dest->first_seq_no = rep_src->first_seq_no;
1352  rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1353  rep_src->timelines = NULL;
1354  rep_src->n_timelines = 0;
1355  rep_dest->cur_seq_no = rep_src->cur_seq_no;
1356  }
1357 }
1358 
1359 static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
1360 {
1361  if (rep_dest && rep_src ) {
1362  free_fragment_list(rep_dest);
1363  if (rep_src->start_number > (rep_dest->start_number + rep_dest->n_fragments))
1364  rep_dest->cur_seq_no = 0;
1365  else
1366  rep_dest->cur_seq_no += rep_src->start_number - rep_dest->start_number;
1367  rep_dest->fragments = rep_src->fragments;
1368  rep_dest->n_fragments = rep_src->n_fragments;
1369  rep_dest->parent = rep_src->parent;
1370  rep_dest->last_seq_no = calc_max_seg_no(rep_dest, c);
1371  rep_src->fragments = NULL;
1372  rep_src->n_fragments = 0;
1373  }
1374 }
1375 
1376 
1378 {
1379 
1380  int ret = 0, i;
1381  DASHContext *c = s->priv_data;
1382 
1383  // save current context
1384  int n_videos = c->n_videos;
1385  struct representation **videos = c->videos;
1386  int n_audios = c->n_audios;
1387  struct representation **audios = c->audios;
1388  char *base_url = c->base_url;
1389 
1390  c->base_url = NULL;
1391  c->n_videos = 0;
1392  c->videos = NULL;
1393  c->n_audios = 0;
1394  c->audios = NULL;
1395  ret = parse_manifest(s, s->filename, NULL);
1396  if (ret)
1397  goto finish;
1398 
1399  if (c->n_videos != n_videos) {
1400  av_log(c, AV_LOG_ERROR,
1401  "new manifest has mismatched no. of video representations, %d -> %d\n",
1402  n_videos, c->n_videos);
1403  return AVERROR_INVALIDDATA;
1404  }
1405  if (c->n_audios != n_audios) {
1406  av_log(c, AV_LOG_ERROR,
1407  "new manifest has mismatched no. of audio representations, %d -> %d\n",
1408  n_audios, c->n_audios);
1409  return AVERROR_INVALIDDATA;
1410  }
1411 
1412  for (i = 0; i < n_videos; i++) {
1413  struct representation *cur_video = videos[i];
1414  struct representation *ccur_video = c->videos[i];
1415  if (cur_video->timelines) {
1416  // calc current time
1417  int64_t currentTime = get_segment_start_time_based_on_timeline(cur_video, cur_video->cur_seq_no) / cur_video->fragment_timescale;
1418  // update segments
1419  ccur_video->cur_seq_no = calc_next_seg_no_from_timelines(ccur_video, currentTime * cur_video->fragment_timescale - 1);
1420  if (ccur_video->cur_seq_no >= 0) {
1421  move_timelines(ccur_video, cur_video, c);
1422  }
1423  }
1424  if (cur_video->fragments) {
1425  move_segments(ccur_video, cur_video, c);
1426  }
1427  }
1428  for (i = 0; i < n_audios; i++) {
1429  struct representation *cur_audio = audios[i];
1430  struct representation *ccur_audio = c->audios[i];
1431  if (cur_audio->timelines) {
1432  // calc current time
1433  int64_t currentTime = get_segment_start_time_based_on_timeline(cur_audio, cur_audio->cur_seq_no) / cur_audio->fragment_timescale;
1434  // update segments
1435  ccur_audio->cur_seq_no = calc_next_seg_no_from_timelines(ccur_audio, currentTime * cur_audio->fragment_timescale - 1);
1436  if (ccur_audio->cur_seq_no >= 0) {
1437  move_timelines(ccur_audio, cur_audio, c);
1438  }
1439  }
1440  if (cur_audio->fragments) {
1441  move_segments(ccur_audio, cur_audio, c);
1442  }
1443  }
1444 
1445 finish:
1446  // restore context
1447  if (c->base_url)
1448  av_free(base_url);
1449  else
1450  c->base_url = base_url;
1451  if (c->audios)
1452  free_audio_list(c);
1453  if (c->videos)
1454  free_video_list(c);
1455  c->n_audios = n_audios;
1456  c->audios = audios;
1457  c->n_videos = n_videos;
1458  c->videos = videos;
1459  return ret;
1460 }
1461 
1462 static struct fragment *get_current_fragment(struct representation *pls)
1463 {
1464  int64_t min_seq_no = 0;
1465  int64_t max_seq_no = 0;
1466  struct fragment *seg = NULL;
1467  struct fragment *seg_ptr = NULL;
1468  DASHContext *c = pls->parent->priv_data;
1469 
1470  while (( !ff_check_interrupt(c->interrupt_callback)&& pls->n_fragments > 0)) {
1471  if (pls->cur_seq_no < pls->n_fragments) {
1472  seg_ptr = pls->fragments[pls->cur_seq_no];
1473  seg = av_mallocz(sizeof(struct fragment));
1474  if (!seg) {
1475  return NULL;
1476  }
1477  seg->url = av_strdup(seg_ptr->url);
1478  if (!seg->url) {
1479  av_free(seg);
1480  return NULL;
1481  }
1482  seg->size = seg_ptr->size;
1483  seg->url_offset = seg_ptr->url_offset;
1484  return seg;
1485  } else if (c->is_live) {
1486  refresh_manifest(pls->parent);
1487  } else {
1488  break;
1489  }
1490  }
1491  if (c->is_live) {
1492  min_seq_no = calc_min_seg_no(pls->parent, pls);
1493  max_seq_no = calc_max_seg_no(pls, c);
1494 
1495  if (pls->timelines || pls->fragments) {
1496  refresh_manifest(pls->parent);
1497  }
1498  if (pls->cur_seq_no <= min_seq_no) {
1499  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);
1500  pls->cur_seq_no = calc_cur_seg_no(pls->parent, pls);
1501  } else if (pls->cur_seq_no > max_seq_no) {
1502  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);
1503  }
1504  seg = av_mallocz(sizeof(struct fragment));
1505  if (!seg) {
1506  return NULL;
1507  }
1508  } else if (pls->cur_seq_no <= pls->last_seq_no) {
1509  seg = av_mallocz(sizeof(struct fragment));
1510  if (!seg) {
1511  return NULL;
1512  }
1513  }
1514  if (seg) {
1515  char *tmpfilename= av_mallocz(c->max_url_size);
1516  if (!tmpfilename) {
1517  return NULL;
1518  }
1520  seg->url = av_strireplace(pls->url_template, pls->url_template, tmpfilename);
1521  if (!seg->url) {
1522  av_log(pls->parent, AV_LOG_WARNING, "Unable to resolve template url '%s', try to use origin template\n", pls->url_template);
1523  seg->url = av_strdup(pls->url_template);
1524  if (!seg->url) {
1525  av_log(pls->parent, AV_LOG_ERROR, "Cannot resolve template url '%s'\n", pls->url_template);
1526  av_free(tmpfilename);
1527  return NULL;
1528  }
1529  }
1530  av_free(tmpfilename);
1531  seg->size = -1;
1532  }
1533 
1534  return seg;
1535 }
1536 
1540 };
1541 
1542 static int read_from_url(struct representation *pls, struct fragment *seg,
1543  uint8_t *buf, int buf_size,
1544  enum ReadFromURLMode mode)
1545 {
1546  int ret;
1547 
1548  /* limit read if the fragment was only a part of a file */
1549  if (seg->size >= 0)
1550  buf_size = FFMIN(buf_size, pls->cur_seg_size - pls->cur_seg_offset);
1551 
1552  if (mode == READ_COMPLETE) {
1553  ret = avio_read(pls->input, buf, buf_size);
1554  if (ret < buf_size) {
1555  av_log(pls->parent, AV_LOG_WARNING, "Could not read complete fragment.\n");
1556  }
1557  } else {
1558  ret = avio_read(pls->input, buf, buf_size);
1559  }
1560  if (ret > 0)
1561  pls->cur_seg_offset += ret;
1562 
1563  return ret;
1564 }
1565 
1566 static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
1567 {
1568  AVDictionary *opts = NULL;
1569  char *url = NULL;
1570  int ret = 0;
1571 
1572  url = av_mallocz(c->max_url_size);
1573  if (!url) {
1574  goto cleanup;
1575  }
1576  set_httpheader_options(c, &opts);
1577  if (seg->size >= 0) {
1578  /* try to restrict the HTTP request to the part we want
1579  * (if this is in fact a HTTP request) */
1580  av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1581  av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1582  }
1583 
1584  ff_make_absolute_url(url, c->max_url_size, c->base_url, seg->url);
1585  av_log(pls->parent, AV_LOG_VERBOSE, "DASH request for url '%s', offset %"PRId64", playlist %d\n",
1586  url, seg->url_offset, pls->rep_idx);
1587  ret = open_url(pls->parent, &pls->input, url, c->avio_opts, opts, NULL);
1588  if (ret < 0) {
1589  goto cleanup;
1590  }
1591 
1592 cleanup:
1593  av_free(url);
1594  av_dict_free(&opts);
1595  pls->cur_seg_offset = 0;
1596  pls->cur_seg_size = seg->size;
1597  return ret;
1598 }
1599 
1600 static int update_init_section(struct representation *pls)
1601 {
1602  static const int max_init_section_size = 1024 * 1024;
1603  DASHContext *c = pls->parent->priv_data;
1604  int64_t sec_size;
1605  int64_t urlsize;
1606  int ret;
1607 
1608  if (!pls->init_section || pls->init_sec_buf)
1609  return 0;
1610 
1611  ret = open_input(c, pls, pls->init_section);
1612  if (ret < 0) {
1614  "Failed to open an initialization section in playlist %d\n",
1615  pls->rep_idx);
1616  return ret;
1617  }
1618 
1619  if (pls->init_section->size >= 0)
1620  sec_size = pls->init_section->size;
1621  else if ((urlsize = avio_size(pls->input)) >= 0)
1622  sec_size = urlsize;
1623  else
1624  sec_size = max_init_section_size;
1625 
1626  av_log(pls->parent, AV_LOG_DEBUG,
1627  "Downloading an initialization section of size %"PRId64"\n",
1628  sec_size);
1629 
1630  sec_size = FFMIN(sec_size, max_init_section_size);
1631 
1632  av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1633 
1634  ret = read_from_url(pls, pls->init_section, pls->init_sec_buf,
1636  ff_format_io_close(pls->parent, &pls->input);
1637 
1638  if (ret < 0)
1639  return ret;
1640 
1641  pls->init_sec_data_len = ret;
1642  pls->init_sec_buf_read_offset = 0;
1643 
1644  return 0;
1645 }
1646 
1647 static int64_t seek_data(void *opaque, int64_t offset, int whence)
1648 {
1649  struct representation *v = opaque;
1650  if (v->n_fragments && !v->init_sec_data_len) {
1651  return avio_seek(v->input, offset, whence);
1652  }
1653 
1654  return AVERROR(ENOSYS);
1655 }
1656 
1657 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1658 {
1659  int ret = 0;
1660  struct representation *v = opaque;
1661  DASHContext *c = v->parent->priv_data;
1662 
1663 restart:
1664  if (!v->input) {
1665  free_fragment(&v->cur_seg);
1666  v->cur_seg = get_current_fragment(v);
1667  if (!v->cur_seg) {
1668  ret = AVERROR_EOF;
1669  goto end;
1670  }
1671 
1672  /* load/update Media Initialization Section, if any */
1673  ret = update_init_section(v);
1674  if (ret)
1675  goto end;
1676 
1677  ret = open_input(c, v, v->cur_seg);
1678  if (ret < 0) {
1680  goto end;
1681  ret = AVERROR_EXIT;
1682  }
1683  av_log(v->parent, AV_LOG_WARNING, "Failed to open fragment of playlist %d\n", v->rep_idx);
1684  v->cur_seq_no++;
1685  goto restart;
1686  }
1687  }
1688 
1690  /* Push init section out first before first actual fragment */
1691  int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1692  memcpy(buf, v->init_sec_buf, copy_size);
1693  v->init_sec_buf_read_offset += copy_size;
1694  ret = copy_size;
1695  goto end;
1696  }
1697 
1698  /* check the v->cur_seg, if it is null, get current and double check if the new v->cur_seg*/
1699  if (!v->cur_seg) {
1700  v->cur_seg = get_current_fragment(v);
1701  }
1702  if (!v->cur_seg) {
1703  ret = AVERROR_EOF;
1704  goto end;
1705  }
1706  ret = read_from_url(v, v->cur_seg, buf, buf_size, READ_NORMAL);
1707  if (ret > 0)
1708  goto end;
1709 
1710  if (c->is_live || v->cur_seq_no < v->last_seq_no) {
1711  if (!v->is_restart_needed)
1712  v->cur_seq_no++;
1713  v->is_restart_needed = 1;
1714  }
1715 
1716 end:
1717  return ret;
1718 }
1719 
1721 {
1722  DASHContext *c = s->priv_data;
1723  const char *opts[] = { "headers", "user_agent", "user-agent", "cookies", NULL }, **opt = opts;
1724  uint8_t *buf = NULL;
1725  int ret = 0;
1726 
1727  while (*opt) {
1728  if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
1729  if (buf[0] != '\0') {
1730  ret = av_dict_set(&c->avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL);
1731  if (ret < 0) {
1732  av_freep(&buf);
1733  return ret;
1734  }
1735  } else {
1736  av_freep(&buf);
1737  }
1738  }
1739  opt++;
1740  }
1741 
1742  return ret;
1743 }
1744 
1745 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1746  int flags, AVDictionary **opts)
1747 {
1748  av_log(s, AV_LOG_ERROR,
1749  "A DASH playlist item '%s' referred to an external file '%s'. "
1750  "Opening this file was forbidden for security reasons\n",
1751  s->filename, url);
1752  return AVERROR(EPERM);
1753 }
1754 
1756 {
1757  /* note: the internal buffer could have changed */
1758  av_freep(&pls->pb.buffer);
1759  memset(&pls->pb, 0x00, sizeof(AVIOContext));
1760  pls->ctx->pb = NULL;
1761  avformat_close_input(&pls->ctx);
1762  pls->ctx = NULL;
1763 }
1764 
1766 {
1767  DASHContext *c = s->priv_data;
1768  AVInputFormat *in_fmt = NULL;
1769  AVDictionary *in_fmt_opts = NULL;
1770  uint8_t *avio_ctx_buffer = NULL;
1771  int ret = 0, i;
1772 
1773  if (pls->ctx) {
1775  }
1776  if (!(pls->ctx = avformat_alloc_context())) {
1777  ret = AVERROR(ENOMEM);
1778  goto fail;
1779  }
1780 
1781  avio_ctx_buffer = av_malloc(INITIAL_BUFFER_SIZE);
1782  if (!avio_ctx_buffer ) {
1783  ret = AVERROR(ENOMEM);
1784  avformat_free_context(pls->ctx);
1785  pls->ctx = NULL;
1786  goto fail;
1787  }
1788  if (c->is_live) {
1789  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL);
1790  } else {
1791  ffio_init_context(&pls->pb, avio_ctx_buffer , INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, seek_data);
1792  }
1793  pls->pb.seekable = 0;
1794 
1795  if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
1796  goto fail;
1797 
1798  pls->ctx->flags = AVFMT_FLAG_CUSTOM_IO;
1799  pls->ctx->probesize = 1024 * 4;
1801  ret = av_probe_input_buffer(&pls->pb, &in_fmt, "", NULL, 0, 0);
1802  if (ret < 0) {
1803  av_log(s, AV_LOG_ERROR, "Error when loading first fragment, playlist %d\n", (int)pls->rep_idx);
1804  avformat_free_context(pls->ctx);
1805  pls->ctx = NULL;
1806  goto fail;
1807  }
1808 
1809  pls->ctx->pb = &pls->pb;
1810  pls->ctx->io_open = nested_io_open;
1811 
1812  // provide additional information from mpd if available
1813  ret = avformat_open_input(&pls->ctx, "", in_fmt, &in_fmt_opts); //pls->init_section->url
1814  av_dict_free(&in_fmt_opts);
1815  if (ret < 0)
1816  goto fail;
1817  if (pls->n_fragments) {
1818 #if FF_API_R_FRAME_RATE
1819  if (pls->framerate.den) {
1820  for (i = 0; i < pls->ctx->nb_streams; i++)
1821  pls->ctx->streams[i]->r_frame_rate = pls->framerate;
1822  }
1823 #endif
1824 
1825  ret = avformat_find_stream_info(pls->ctx, NULL);
1826  if (ret < 0)
1827  goto fail;
1828  }
1829 
1830 fail:
1831  return ret;
1832 }
1833 
1835 {
1836  int ret = 0;
1837  int i;
1838 
1839  pls->parent = s;
1840  pls->cur_seq_no = calc_cur_seg_no(s, pls);
1841 
1842  if (!pls->last_seq_no) {
1843  pls->last_seq_no = calc_max_seg_no(pls, s->priv_data);
1844  }
1845 
1846  ret = reopen_demux_for_component(s, pls);
1847  if (ret < 0) {
1848  goto fail;
1849  }
1850  for (i = 0; i < pls->ctx->nb_streams; i++) {
1851  AVStream *st = avformat_new_stream(s, NULL);
1852  AVStream *ist = pls->ctx->streams[i];
1853  if (!st) {
1854  ret = AVERROR(ENOMEM);
1855  goto fail;
1856  }
1857  st->id = i;
1860  }
1861 
1862  return 0;
1863 fail:
1864  return ret;
1865 }
1866 
1868 {
1869  void *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb;
1870  DASHContext *c = s->priv_data;
1871  int ret = 0;
1872  int stream_index = 0;
1873  int i;
1874 
1875  c->interrupt_callback = &s->interrupt_callback;
1876  // if the URL context is good, read important options we must broker later
1877  if (u) {
1878  update_options(&c->user_agent, "user-agent", u);
1879  update_options(&c->cookies, "cookies", u);
1880  update_options(&c->headers, "headers", u);
1881  }
1882 
1883  if ((ret = parse_manifest(s, s->filename, s->pb)) < 0)
1884  goto fail;
1885 
1886  if ((ret = save_avio_options(s)) < 0)
1887  goto fail;
1888 
1889  /* If this isn't a live stream, fill the total duration of the
1890  * stream. */
1891  if (!c->is_live) {
1892  s->duration = (int64_t) c->media_presentation_duration * AV_TIME_BASE;
1893  }
1894 
1895  /* Open the demuxer for video and audio components if available */
1896  for (i = 0; i < c->n_videos; i++) {
1897  struct representation *cur_video = c->videos[i];
1898  ret = open_demux_for_component(s, cur_video);
1899  if (ret)
1900  goto fail;
1901  cur_video->stream_index = stream_index;
1902  ++stream_index;
1903  }
1904 
1905  for (i = 0; i < c->n_audios; i++) {
1906  struct representation *cur_audio = c->audios[i];
1907  ret = open_demux_for_component(s, cur_audio);
1908  if (ret)
1909  goto fail;
1910  cur_audio->stream_index = stream_index;
1911  ++stream_index;
1912  }
1913 
1914  if (!stream_index) {
1915  ret = AVERROR_INVALIDDATA;
1916  goto fail;
1917  }
1918 
1919  /* Create a program */
1920  if (!ret) {
1921  AVProgram *program;
1922  program = av_new_program(s, 0);
1923  if (!program) {
1924  goto fail;
1925  }
1926 
1927  for (i = 0; i < c->n_videos; i++) {
1928  struct representation *pls = c->videos[i];
1929 
1931  pls->assoc_stream = s->streams[pls->stream_index];
1932  if (pls->bandwidth > 0)
1933  av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
1934  if (pls->id[0])
1935  av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
1936  }
1937  for (i = 0; i < c->n_audios; i++) {
1938  struct representation *pls = c->audios[i];
1939 
1941  pls->assoc_stream = s->streams[pls->stream_index];
1942  if (pls->bandwidth > 0)
1943  av_dict_set_int(&pls->assoc_stream->metadata, "variant_bitrate", pls->bandwidth, 0);
1944  if (pls->id[0])
1945  av_dict_set(&pls->assoc_stream->metadata, "id", pls->id, 0);
1946  }
1947  }
1948 
1949  return 0;
1950 fail:
1951  return ret;
1952 }
1953 
1955 {
1956  int i, j;
1957 
1958  for (i = 0; i < n; i++) {
1959  struct representation *pls = p[i];
1960 
1961  int needed = !pls->assoc_stream || pls->assoc_stream->discard < AVDISCARD_ALL;
1962  if (needed && !pls->ctx) {
1963  pls->cur_seg_offset = 0;
1964  pls->init_sec_buf_read_offset = 0;
1965  /* Catch up */
1966  for (j = 0; j < n; j++) {
1967  pls->cur_seq_no = FFMAX(pls->cur_seq_no, p[j]->cur_seq_no);
1968  }
1970  av_log(s, AV_LOG_INFO, "Now receiving stream_index %d\n", pls->stream_index);
1971  } else if (!needed && pls->ctx) {
1973  if (pls->input)
1974  ff_format_io_close(pls->parent, &pls->input);
1975  av_log(s, AV_LOG_INFO, "No longer receiving stream_index %d\n", pls->stream_index);
1976  }
1977  }
1978 }
1979 
1981 {
1982  DASHContext *c = s->priv_data;
1983  int ret = 0, i;
1984  int64_t mints = 0;
1985  struct representation *cur = NULL;
1986 
1989 
1990  for (i = 0; i < c->n_videos; i++) {
1991  struct representation *pls = c->videos[i];
1992  if (!pls->ctx)
1993  continue;
1994  if (!cur || pls->cur_timestamp < mints) {
1995  cur = pls;
1996  mints = pls->cur_timestamp;
1997  }
1998  }
1999  for (i = 0; i < c->n_audios; i++) {
2000  struct representation *pls = c->audios[i];
2001  if (!pls->ctx)
2002  continue;
2003  if (!cur || pls->cur_timestamp < mints) {
2004  cur = pls;
2005  mints = pls->cur_timestamp;
2006  }
2007  }
2008 
2009  if (!cur) {
2010  return AVERROR_INVALIDDATA;
2011  }
2012  while (!ff_check_interrupt(c->interrupt_callback) && !ret) {
2013  ret = av_read_frame(cur->ctx, pkt);
2014  if (ret >= 0) {
2015  /* If we got a packet, return it */
2016  cur->cur_timestamp = av_rescale(pkt->pts, (int64_t)cur->ctx->streams[0]->time_base.num * 90000, cur->ctx->streams[0]->time_base.den);
2017  pkt->stream_index = cur->stream_index;
2018  return 0;
2019  }
2020  if (cur->is_restart_needed) {
2021  cur->cur_seg_offset = 0;
2022  cur->init_sec_buf_read_offset = 0;
2023  if (cur->input)
2024  ff_format_io_close(cur->parent, &cur->input);
2025  ret = reopen_demux_for_component(s, cur);
2026  cur->is_restart_needed = 0;
2027  }
2028  }
2029  return AVERROR_EOF;
2030 }
2031 
2033 {
2034  DASHContext *c = s->priv_data;
2035  free_audio_list(c);
2036  free_video_list(c);
2037 
2038  av_freep(&c->cookies);
2039  av_freep(&c->user_agent);
2040  av_dict_free(&c->avio_opts);
2041  av_freep(&c->base_url);
2042  return 0;
2043 }
2044 
2045 static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
2046 {
2047  int ret = 0;
2048  int i = 0;
2049  int j = 0;
2050  int64_t duration = 0;
2051 
2052  av_log(pls->parent, AV_LOG_VERBOSE, "DASH seek pos[%"PRId64"ms], playlist %d%s\n",
2053  seek_pos_msec, pls->rep_idx, dry_run ? " (dry)" : "");
2054 
2055  // single fragment mode
2056  if (pls->n_fragments == 1) {
2057  pls->cur_timestamp = 0;
2058  pls->cur_seg_offset = 0;
2059  if (dry_run)
2060  return 0;
2061  ff_read_frame_flush(pls->ctx);
2062  return av_seek_frame(pls->ctx, -1, seek_pos_msec * 1000, flags);
2063  }
2064 
2065  if (pls->input)
2066  ff_format_io_close(pls->parent, &pls->input);
2067 
2068  // find the nearest fragment
2069  if (pls->n_timelines > 0 && pls->fragment_timescale > 0) {
2070  int64_t num = pls->first_seq_no;
2071  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline start n_timelines[%d] "
2072  "last_seq_no[%"PRId64"], playlist %d.\n",
2073  (int)pls->n_timelines, (int64_t)pls->last_seq_no, (int)pls->rep_idx);
2074  for (i = 0; i < pls->n_timelines; i++) {
2075  if (pls->timelines[i]->starttime > 0) {
2076  duration = pls->timelines[i]->starttime;
2077  }
2078  duration += pls->timelines[i]->duration;
2079  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2080  goto set_seq_num;
2081  }
2082  for (j = 0; j < pls->timelines[i]->repeat; j++) {
2083  duration += pls->timelines[i]->duration;
2084  num++;
2085  if (seek_pos_msec < ((duration * 1000) / pls->fragment_timescale)) {
2086  goto set_seq_num;
2087  }
2088  }
2089  num++;
2090  }
2091 
2092 set_seq_num:
2093  pls->cur_seq_no = num > pls->last_seq_no ? pls->last_seq_no : num;
2094  av_log(pls->parent, AV_LOG_VERBOSE, "dash_seek with SegmentTimeline end cur_seq_no[%"PRId64"], playlist %d.\n",
2095  (int64_t)pls->cur_seq_no, (int)pls->rep_idx);
2096  } else if (pls->fragment_duration > 0) {
2097  pls->cur_seq_no = pls->first_seq_no + ((seek_pos_msec * pls->fragment_timescale) / pls->fragment_duration) / 1000;
2098  } else {
2099  av_log(pls->parent, AV_LOG_ERROR, "dash_seek missing timeline or fragment_duration\n");
2100  pls->cur_seq_no = pls->first_seq_no;
2101  }
2102  pls->cur_timestamp = 0;
2103  pls->cur_seg_offset = 0;
2104  pls->init_sec_buf_read_offset = 0;
2105  ret = dry_run ? 0 : reopen_demux_for_component(s, pls);
2106 
2107  return ret;
2108 }
2109 
2110 static int dash_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
2111 {
2112  int ret = 0, i;
2113  DASHContext *c = s->priv_data;
2114  int64_t seek_pos_msec = av_rescale_rnd(timestamp, 1000,
2115  s->streams[stream_index]->time_base.den,
2116  flags & AVSEEK_FLAG_BACKWARD ?
2118  if ((flags & AVSEEK_FLAG_BYTE) || c->is_live)
2119  return AVERROR(ENOSYS);
2120 
2121  /* Seek in discarded streams with dry_run=1 to avoid reopening them */
2122  for (i = 0; i < c->n_videos; i++) {
2123  if (!ret)
2124  ret = dash_seek(s, c->videos[i], seek_pos_msec, flags, !c->videos[i]->ctx);
2125  }
2126  for (i = 0; i < c->n_audios; i++) {
2127  if (!ret)
2128  ret = dash_seek(s, c->audios[i], seek_pos_msec, flags, !c->audios[i]->ctx);
2129  }
2130 
2131  return ret;
2132 }
2133 
2134 static int dash_probe(AVProbeData *p)
2135 {
2136  if (!av_stristr(p->buf, "<MPD"))
2137  return 0;
2138 
2139  if (av_stristr(p->buf, "dash:profile:isoff-on-demand:2011") ||
2140  av_stristr(p->buf, "dash:profile:isoff-live:2011") ||
2141  av_stristr(p->buf, "dash:profile:isoff-live:2012") ||
2142  av_stristr(p->buf, "dash:profile:isoff-main:2011")) {
2143  return AVPROBE_SCORE_MAX;
2144  }
2145  if (av_stristr(p->buf, "dash:profile")) {
2146  return AVPROBE_SCORE_MAX;
2147  }
2148 
2149  return 0;
2150 }
2151 
2152 #define OFFSET(x) offsetof(DASHContext, x)
2153 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2154 static const AVOption dash_options[] = {
2155  {"allowed_extensions", "List of file extensions that dash is allowed to access",
2156  OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2157  {.str = "aac,m4a,m4s,m4v,mov,mp4"},
2158  INT_MIN, INT_MAX, FLAGS},
2159  {NULL}
2160 };
2161 
2162 static const AVClass dash_class = {
2163  .class_name = "dash",
2164  .item_name = av_default_item_name,
2165  .option = dash_options,
2166  .version = LIBAVUTIL_VERSION_INT,
2167 };
2168 
2170  .name = "dash",
2171  .long_name = NULL_IF_CONFIG_SMALL("Dynamic Adaptive Streaming over HTTP"),
2172  .priv_class = &dash_class,
2173  .priv_data_size = sizeof(DASHContext),
2180 };
const char * name
Definition: avisynth_c.h:775
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:2153
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2486
int64_t probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1506
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:1922
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
const char * s
Definition: avisynth_c.h:768
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:149
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:1618
AVOption.
Definition: opt.h:246
ReadFromURLMode
Definition: dashdec.c:1537
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:244
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:1657
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:4823
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:154
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:1462
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:2154
static int64_t seek_data(void *opaque, int64_t offset, int whence)
Definition: dashdec.c:1647
discard all
Definition: avcodec.h:794
static AVPacket pkt
int64_t cur_timestamp
Definition: dashdec.c:118
#define src
Definition: vp8dsp.c:254
int n_videos
Definition: dashdec.c:126
char * headers
holds HTTP headers set as an AVOption to the HTTP protocol context
Definition: dashdec.c:148
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:592
uint64_t min_buffer_time
Definition: dashdec.c:138
static void free_fragment(struct fragment **seg)
Definition: dashdec.c:303
Format I/O context.
Definition: avformat.h:1342
#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:1918
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:170
static char buffer[20]
Definition: seek.c:32
static int64_t start_time
Definition: ffplay.c:327
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.
static void set_httpheader_options(DASHContext *c, AVDictionary **opts)
Definition: dashdec.c:375
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:880
static int dash_close(AVFormatContext *s)
Definition: dashdec.c:2032
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5611
uint64_t period_duration
Definition: dashdec.c:141
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4453
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:344
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
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:1473
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:4552
struct timeline ** timelines
Definition: dashdec.c:96
static int flags
Definition: log.c:55
#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:135
static void recheck_discard_flags(AVFormatContext *s, struct representation **p, int n)
Definition: dashdec.c:1954
uint64_t availability_start_time
Definition: dashdec.c:134
static enum AVMediaType get_content_type(xmlNodePtr node)
Definition: dashdec.c:550
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
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:533
static int aligned(int val)
Definition: dashdec.c:160
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:320
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2003
#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:323
int64_t starttime
Definition: dashdec.c:57
static void move_segments(struct representation *rep_src, struct representation *rep_dest, DASHContext *c)
Definition: dashdec.c:1359
static int64_t calc_max_seg_no(struct representation *pls, DASHContext *c)
Definition: dashdec.c:1324
#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:186
static int64_t calc_cur_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1280
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1190
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
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:2110
char * av_strireplace(const char *str, const char *from, const char *to)
Locale-independent strings replace.
Definition: avstring.c:234
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:1260
#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:116
static int read_from_url(struct representation *pls, struct fragment *seg, uint8_t *buf, int buf_size, enum ReadFromURLMode mode)
Definition: dashdec.c:1542
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:488
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:136
struct fragment ** fragments
Definition: dashdec.c:93
static void free_representation(struct representation *pls)
Definition: dashdec.c:334
AVIOInterruptCB * interrupt_callback
Definition: dashdec.c:145
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1398
static void free_audio_list(DASHContext *c)
Definition: dashdec.c:364
AVDictionary * opts
Definition: movenc.c:50
char * user_agent
holds HTTP user agent set as an AVOption to the HTTP protocol context
Definition: dashdec.c:146
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:312
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:555
#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:1345
static uint32_t get_duration_insec(AVFormatContext *s, const char *duration)
Definition: dashdec.c:200
int is_live
Definition: dashdec.c:144
#define OFFSET(x)
Definition: dashdec.c:2152
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary *opts, AVDictionary *opts2, int *is_http)
Definition: dashdec.c:393
static int parse_manifest(AVFormatContext *s, const char *url, AVIOContext *in)
Definition: dashdec.c:1108
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:937
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don&#39;t avio_close() it.
Definition: avformat.h:1481
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static int save_avio_options(AVFormatContext *s)
Definition: dashdec.c:1720
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:142
if(ret< 0)
Definition: vf_mcdeint.c:279
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:1514
static char * get_val_from_nodes_tab(xmlNodePtr *nodes, const int n_nodes, const char *attrname)
Definition: dashdec.c:517
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition: dashdec.c:1745
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static int dash_probe(AVProbeData *p)
Definition: dashdec.c:2134
Stream structure.
Definition: avformat.h:873
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:664
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
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:151
static int dash_read_header(AVFormatContext *s)
Definition: dashdec.c:1867
void * buf
Definition: avisynth_c.h:690
uint64_t time_shift_buffer_depth
Definition: dashdec.c:137
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 void update_options(char **dest, const char *name, void *src)
Definition: dashdec.c:385
static int open_input(DASHContext *c, struct representation *pls, struct fragment *seg)
Definition: dashdec.c:1566
static int dash_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dashdec.c:1980
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:695
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2487
cl_device_type type
AVMediaType
Definition: avutil.h:199
static struct fragment * get_Fragment(char *range)
Definition: dashdec.c:574
static int parse_manifest_segmenttimeline(AVFormatContext *s, struct representation *rep, xmlNodePtr fragment_timeline_node)
Definition: dashdec.c:659
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:1178
char id[20]
Definition: dashdec.c:87
AVDictionary * avio_opts
Definition: dashdec.c:150
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4387
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:1768
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:473
AVInputFormat ff_dash_demuxer
Definition: dashdec.c:2169
static int dash_seek(AVFormatContext *s, struct representation *pls, int64_t seek_pos_msec, int flags, int dry_run)
Definition: dashdec.c:2045
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2506
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:1600
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
char * cookies
holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol co...
Definition: dashdec.c:147
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:3562
static const AVClass dash_class
Definition: dashdec.c:2162
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:1065
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:4425
int64_t fragment_timescale
Definition: dashdec.c:103
static void close_demux_for_component(struct representation *pls)
Definition: dashdec.c:1755
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:751
#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:1060
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:461
AVRational framerate
Definition: dashdec.c:89
void * priv_data
Format private data.
Definition: avformat.h:1370
int64_t start_number
Definition: dashdec.c:100
static uint64_t get_current_time_in_sec(void)
Definition: dashdec.c:165
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:537
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:798
static int64_t calc_min_seg_no(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1311
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:1457
static void free_video_list(DASHContext *c)
Definition: dashdec.c:353
#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:1020
char * url_template
Definition: dashdec.c:76
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
static int reopen_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1765
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:928
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:997
This structure stores compressed data.
Definition: avcodec.h:1407
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
static int64_t calc_next_seg_no_from_timelines(struct representation *pls, int64_t cur_time)
Definition: dashdec.c:273
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:1423
static int open_demux_for_component(AVFormatContext *s, struct representation *pls)
Definition: dashdec.c:1834
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:1377
static uint8_t tmp[11]
Definition: aes_ctr.c:26