FFmpeg  4.4.4
hls.c
Go to the documentation of this file.
1 /*
2  * Apple HTTP Live Streaming demuxer
3  * Copyright (c) 2010 Martin Storsjo
4  * Copyright (c) 2013 Anssi Hannula
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 
23 /**
24  * @file
25  * Apple HTTP Live Streaming demuxer
26  * https://www.rfc-editor.org/rfc/rfc8216.txt
27  */
28 
29 #include "libavformat/http.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/dict.h"
36 #include "libavutil/time.h"
37 #include "avformat.h"
38 #include "internal.h"
39 #include "avio_internal.h"
40 #include "id3v2.h"
41 
42 #define INITIAL_BUFFER_SIZE 32768
43 
44 #define MAX_FIELD_LEN 64
45 #define MAX_CHARACTERISTICS_LEN 512
46 
47 #define MPEG_TIME_BASE 90000
48 #define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
49 
50 /*
51  * An apple http stream consists of a playlist with media segment files,
52  * played sequentially. There may be several playlists with the same
53  * video content, in different bandwidth variants, that are played in
54  * parallel (preferably only one bandwidth variant at a time). In this case,
55  * the user supplied the url to a main playlist that only lists the variant
56  * playlists.
57  *
58  * If the main playlist doesn't point at any variants, we still create
59  * one anonymous toplevel variant for this, to maintain the structure.
60  */
61 
62 enum KeyType {
66 };
67 
68 struct segment {
69  int64_t duration;
70  int64_t url_offset;
71  int64_t size;
72  char *url;
73  char *key;
74  enum KeyType key_type;
75  uint8_t iv[16];
76  /* associated Media Initialization Section, treated as a segment */
78 };
79 
80 struct rendition;
81 
86 };
87 
88 /*
89  * Each playlist has its own demuxer. If it currently is active,
90  * it has an open AVIOContext too, and potentially an AVPacket
91  * containing the next packet from this stream.
92  */
93 struct playlist {
102  int index;
106 
107  /* main demuxer streams associated with this playlist
108  * indexed by the subdemuxer stream indexes */
111 
112  int finished;
113  enum PlaylistType type;
115  int64_t start_seq_no;
117  struct segment **segments;
118  int needed;
119  int broken;
120  int64_t cur_seq_no;
121  int64_t last_seq_no;
123  int64_t cur_seg_offset;
124  int64_t last_load_time;
125 
126  /* Currently active Media Initialization Section */
129  unsigned int init_sec_buf_size;
130  unsigned int init_sec_data_len;
132 
135 
136  /* ID3 timestamp handling (elementary audio streams have ID3 timestamps
137  * (and possibly other ID3 tags) in the beginning of each segment) */
138  int is_id3_timestamped; /* -1: not yet known */
139  int64_t id3_mpegts_timestamp; /* in mpegts tb */
140  int64_t id3_offset; /* in stream original tb */
141  uint8_t* id3_buf; /* temp buffer for id3 parsing */
142  unsigned int id3_buf_size;
143  AVDictionary *id3_initial; /* data from first id3 tag */
144  int id3_found; /* ID3 tag found at some point */
145  int id3_changed; /* ID3 tag data has changed at some point */
146  ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */
147 
148  int64_t seek_timestamp;
150  int seek_stream_index; /* into subdemuxer stream array */
151 
152  /* Renditions associated with this playlist, if any.
153  * Alternative rendition playlists have a single rendition associated
154  * with them, and variant main Media Playlists may have
155  * multiple (playlist-less) renditions associated with them. */
158 
159  /* Media Initialization Sections (EXT-X-MAP) associated with this
160  * playlist, if any. */
163 };
164 
165 /*
166  * Renditions are e.g. alternative subtitle or audio streams.
167  * The rendition may either be an external playlist or it may be
168  * contained in the main Media Playlist of the variant (in which case
169  * playlist is NULL).
170  */
171 struct rendition {
172  enum AVMediaType type;
178 };
179 
180 struct variant {
182 
183  /* every variant contains at least the main Media Playlist in index 0 */
185  struct playlist **playlists;
186 
190 };
191 
192 typedef struct HLSContext {
193  AVClass *class;
196  struct variant **variants;
198  struct playlist **playlists;
201 
202  int64_t cur_seq_no;
207  int64_t cur_timestamp;
216 } HLSContext;
217 
218 static void free_segment_dynarray(struct segment **segments, int n_segments)
219 {
220  int i;
221  for (i = 0; i < n_segments; i++) {
222  av_freep(&segments[i]->key);
223  av_freep(&segments[i]->url);
224  av_freep(&segments[i]);
225  }
226 }
227 
228 static void free_segment_list(struct playlist *pls)
229 {
231  av_freep(&pls->segments);
232  pls->n_segments = 0;
233 }
234 
235 static void free_init_section_list(struct playlist *pls)
236 {
237  int i;
238  for (i = 0; i < pls->n_init_sections; i++) {
239  av_freep(&pls->init_sections[i]->key);
240  av_freep(&pls->init_sections[i]->url);
241  av_freep(&pls->init_sections[i]);
242  }
243  av_freep(&pls->init_sections);
244  pls->n_init_sections = 0;
245 }
246 
248 {
249  int i;
250  for (i = 0; i < c->n_playlists; i++) {
251  struct playlist *pls = c->playlists[i];
252  free_segment_list(pls);
254  av_freep(&pls->main_streams);
255  av_freep(&pls->renditions);
256  av_freep(&pls->id3_buf);
257  av_dict_free(&pls->id3_initial);
259  av_freep(&pls->init_sec_buf);
260  av_packet_free(&pls->pkt);
261  av_freep(&pls->pb.buffer);
262  ff_format_io_close(c->ctx, &pls->input);
263  pls->input_read_done = 0;
264  ff_format_io_close(c->ctx, &pls->input_next);
265  pls->input_next_requested = 0;
266  if (pls->ctx) {
267  pls->ctx->pb = NULL;
268  avformat_close_input(&pls->ctx);
269  }
270  av_free(pls);
271  }
272  av_freep(&c->playlists);
273  c->n_playlists = 0;
274 }
275 
277 {
278  int i;
279  for (i = 0; i < c->n_variants; i++) {
280  struct variant *var = c->variants[i];
281  av_freep(&var->playlists);
282  av_free(var);
283  }
284  av_freep(&c->variants);
285  c->n_variants = 0;
286 }
287 
289 {
290  int i;
291  for (i = 0; i < c->n_renditions; i++)
292  av_freep(&c->renditions[i]);
293  av_freep(&c->renditions);
294  c->n_renditions = 0;
295 }
296 
297 static struct playlist *new_playlist(HLSContext *c, const char *url,
298  const char *base)
299 {
300  struct playlist *pls = av_mallocz(sizeof(struct playlist));
301  if (!pls)
302  return NULL;
303  pls->pkt = av_packet_alloc();
304  if (!pls->pkt) {
305  av_free(pls);
306  return NULL;
307  }
308  ff_make_absolute_url(pls->url, sizeof(pls->url), base, url);
309  if (!pls->url[0]) {
310  av_packet_free(&pls->pkt);
311  av_free(pls);
312  return NULL;
313  }
315 
316  pls->is_id3_timestamped = -1;
318 
319  dynarray_add(&c->playlists, &c->n_playlists, pls);
320  return pls;
321 }
322 
323 struct variant_info {
324  char bandwidth[20];
325  /* variant group ids: */
329 };
330 
331 static struct variant *new_variant(HLSContext *c, struct variant_info *info,
332  const char *url, const char *base)
333 {
334  struct variant *var;
335  struct playlist *pls;
336 
337  pls = new_playlist(c, url, base);
338  if (!pls)
339  return NULL;
340 
341  var = av_mallocz(sizeof(struct variant));
342  if (!var)
343  return NULL;
344 
345  if (info) {
346  var->bandwidth = atoi(info->bandwidth);
347  strcpy(var->audio_group, info->audio);
348  strcpy(var->video_group, info->video);
349  strcpy(var->subtitles_group, info->subtitles);
350  }
351 
352  dynarray_add(&c->variants, &c->n_variants, var);
353  dynarray_add(&var->playlists, &var->n_playlists, pls);
354  return var;
355 }
356 
357 static void handle_variant_args(struct variant_info *info, const char *key,
358  int key_len, char **dest, int *dest_len)
359 {
360  if (!strncmp(key, "BANDWIDTH=", key_len)) {
361  *dest = info->bandwidth;
362  *dest_len = sizeof(info->bandwidth);
363  } else if (!strncmp(key, "AUDIO=", key_len)) {
364  *dest = info->audio;
365  *dest_len = sizeof(info->audio);
366  } else if (!strncmp(key, "VIDEO=", key_len)) {
367  *dest = info->video;
368  *dest_len = sizeof(info->video);
369  } else if (!strncmp(key, "SUBTITLES=", key_len)) {
370  *dest = info->subtitles;
371  *dest_len = sizeof(info->subtitles);
372  }
373 }
374 
375 struct key_info {
377  char method[11];
378  char iv[35];
379 };
380 
381 static void handle_key_args(struct key_info *info, const char *key,
382  int key_len, char **dest, int *dest_len)
383 {
384  if (!strncmp(key, "METHOD=", key_len)) {
385  *dest = info->method;
386  *dest_len = sizeof(info->method);
387  } else if (!strncmp(key, "URI=", key_len)) {
388  *dest = info->uri;
389  *dest_len = sizeof(info->uri);
390  } else if (!strncmp(key, "IV=", key_len)) {
391  *dest = info->iv;
392  *dest_len = sizeof(info->iv);
393  }
394 }
395 
398  char byterange[32];
399 };
400 
401 static struct segment *new_init_section(struct playlist *pls,
402  struct init_section_info *info,
403  const char *url_base)
404 {
405  struct segment *sec;
406  char tmp_str[MAX_URL_SIZE], *ptr = tmp_str;
407 
408  if (!info->uri[0])
409  return NULL;
410 
411  sec = av_mallocz(sizeof(*sec));
412  if (!sec)
413  return NULL;
414 
415  if (!av_strncasecmp(info->uri, "data:", 5)) {
416  ptr = info->uri;
417  } else {
418  ff_make_absolute_url(tmp_str, sizeof(tmp_str), url_base, info->uri);
419  if (!tmp_str[0]) {
420  av_free(sec);
421  return NULL;
422  }
423  }
424  sec->url = av_strdup(ptr);
425  if (!sec->url) {
426  av_free(sec);
427  return NULL;
428  }
429 
430  if (info->byterange[0]) {
431  sec->size = strtoll(info->byterange, NULL, 10);
432  ptr = strchr(info->byterange, '@');
433  if (ptr)
434  sec->url_offset = strtoll(ptr+1, NULL, 10);
435  } else {
436  /* the entire file is the init section */
437  sec->size = -1;
438  }
439 
440  dynarray_add(&pls->init_sections, &pls->n_init_sections, sec);
441 
442  return sec;
443 }
444 
445 static void handle_init_section_args(struct init_section_info *info, const char *key,
446  int key_len, char **dest, int *dest_len)
447 {
448  if (!strncmp(key, "URI=", key_len)) {
449  *dest = info->uri;
450  *dest_len = sizeof(info->uri);
451  } else if (!strncmp(key, "BYTERANGE=", key_len)) {
452  *dest = info->byterange;
453  *dest_len = sizeof(info->byterange);
454  }
455 }
456 
458  char type[16];
464  char defaultr[4];
465  char forced[4];
467 };
468 
469 static struct rendition *new_rendition(HLSContext *c, struct rendition_info *info,
470  const char *url_base)
471 {
472  struct rendition *rend;
474  char *characteristic;
475  char *chr_ptr;
476  char *saveptr;
477 
478  if (!strcmp(info->type, "AUDIO"))
480  else if (!strcmp(info->type, "VIDEO"))
482  else if (!strcmp(info->type, "SUBTITLES"))
484  else if (!strcmp(info->type, "CLOSED-CAPTIONS"))
485  /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in
486  * AVC SEI RBSP anyway */
487  return NULL;
488 
489  if (type == AVMEDIA_TYPE_UNKNOWN) {
490  av_log(c->ctx, AV_LOG_WARNING, "Can't support the type: %s\n", info->type);
491  return NULL;
492  }
493 
494  /* URI is mandatory for subtitles as per spec */
495  if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0]) {
496  av_log(c->ctx, AV_LOG_ERROR, "The URI tag is REQUIRED for subtitle.\n");
497  return NULL;
498  }
499 
500  /* TODO: handle subtitles (each segment has to parsed separately) */
501  if (c->ctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL)
502  if (type == AVMEDIA_TYPE_SUBTITLE) {
503  av_log(c->ctx, AV_LOG_WARNING, "Can't support the subtitle(uri: %s)\n", info->uri);
504  return NULL;
505  }
506 
507  rend = av_mallocz(sizeof(struct rendition));
508  if (!rend)
509  return NULL;
510 
511  dynarray_add(&c->renditions, &c->n_renditions, rend);
512 
513  rend->type = type;
514  strcpy(rend->group_id, info->group_id);
515  strcpy(rend->language, info->language);
516  strcpy(rend->name, info->name);
517 
518  /* add the playlist if this is an external rendition */
519  if (info->uri[0]) {
520  rend->playlist = new_playlist(c, info->uri, url_base);
521  if (rend->playlist)
523  &rend->playlist->n_renditions, rend);
524  }
525 
526  if (info->assoc_language[0]) {
527  int langlen = strlen(rend->language);
528  if (langlen < sizeof(rend->language) - 3) {
529  rend->language[langlen] = ',';
530  strncpy(rend->language + langlen + 1, info->assoc_language,
531  sizeof(rend->language) - langlen - 2);
532  }
533  }
534 
535  if (!strcmp(info->defaultr, "YES"))
537  if (!strcmp(info->forced, "YES"))
539 
540  chr_ptr = info->characteristics;
541  while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) {
542  if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound"))
544  else if (!strcmp(characteristic, "public.accessibility.describes-video"))
546 
547  chr_ptr = NULL;
548  }
549 
550  return rend;
551 }
552 
553 static void handle_rendition_args(struct rendition_info *info, const char *key,
554  int key_len, char **dest, int *dest_len)
555 {
556  if (!strncmp(key, "TYPE=", key_len)) {
557  *dest = info->type;
558  *dest_len = sizeof(info->type);
559  } else if (!strncmp(key, "URI=", key_len)) {
560  *dest = info->uri;
561  *dest_len = sizeof(info->uri);
562  } else if (!strncmp(key, "GROUP-ID=", key_len)) {
563  *dest = info->group_id;
564  *dest_len = sizeof(info->group_id);
565  } else if (!strncmp(key, "LANGUAGE=", key_len)) {
566  *dest = info->language;
567  *dest_len = sizeof(info->language);
568  } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) {
569  *dest = info->assoc_language;
570  *dest_len = sizeof(info->assoc_language);
571  } else if (!strncmp(key, "NAME=", key_len)) {
572  *dest = info->name;
573  *dest_len = sizeof(info->name);
574  } else if (!strncmp(key, "DEFAULT=", key_len)) {
575  *dest = info->defaultr;
576  *dest_len = sizeof(info->defaultr);
577  } else if (!strncmp(key, "FORCED=", key_len)) {
578  *dest = info->forced;
579  *dest_len = sizeof(info->forced);
580  } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) {
581  *dest = info->characteristics;
582  *dest_len = sizeof(info->characteristics);
583  }
584  /*
585  * ignored:
586  * - AUTOSELECT: client may autoselect based on e.g. system language
587  * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4")
588  */
589 }
590 
591 /* used by parse_playlist to allocate a new variant+playlist when the
592  * playlist is detected to be a Media Playlist (not Master Playlist)
593  * and we have no parent Master Playlist (parsing of which would have
594  * allocated the variant and playlist already)
595  * *pls == NULL => Master Playlist or parentless Media Playlist
596  * *pls != NULL => parented Media Playlist, playlist+variant allocated */
597 static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
598 {
599  if (*pls)
600  return 0;
601  if (!new_variant(c, NULL, url, NULL))
602  return AVERROR(ENOMEM);
603  *pls = c->playlists[c->n_playlists - 1];
604  return 0;
605 }
606 
608  const char *url, AVDictionary **options)
609 {
610 #if !CONFIG_HTTP_PROTOCOL
612 #else
613  int ret;
614  URLContext *uc = ffio_geturlcontext(*pb);
615  av_assert0(uc);
616  (*pb)->eof_reached = 0;
617  ret = ff_http_do_new_request2(uc, url, options);
618  if (ret < 0) {
619  ff_format_io_close(s, pb);
620  }
621  return ret;
622 #endif
623 }
624 
625 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
626  AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
627 {
628  HLSContext *c = s->priv_data;
629  AVDictionary *tmp = NULL;
630  const char *proto_name = NULL;
631  int ret;
632  int is_http = 0;
633 
634  if (av_strstart(url, "crypto", NULL)) {
635  if (url[6] == '+' || url[6] == ':')
636  proto_name = avio_find_protocol_name(url + 7);
637  } else if (av_strstart(url, "data", NULL)) {
638  if (url[4] == '+' || url[4] == ':')
639  proto_name = avio_find_protocol_name(url + 5);
640  }
641 
642  if (!proto_name)
643  proto_name = avio_find_protocol_name(url);
644 
645  if (!proto_name)
646  return AVERROR_INVALIDDATA;
647 
648  // only http(s) & file are allowed
649  if (av_strstart(proto_name, "file", NULL)) {
650  if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
652  "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
653  "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
654  url);
655  return AVERROR_INVALIDDATA;
656  }
657  } else if (av_strstart(proto_name, "http", NULL)) {
658  is_http = 1;
659  } else if (av_strstart(proto_name, "data", NULL)) {
660  ;
661  } else
662  return AVERROR_INVALIDDATA;
663 
664  if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
665  ;
666  else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
667  ;
668  else if (av_strstart(url, "data", NULL) && !strncmp(proto_name, url + 5, strlen(proto_name)) && url[5 + strlen(proto_name)] == ':')
669  ;
670  else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
671  return AVERROR_INVALIDDATA;
672 
673  av_dict_copy(&tmp, *opts, 0);
674  av_dict_copy(&tmp, opts2, 0);
675 
676  if (is_http && c->http_persistent && *pb) {
677  ret = open_url_keepalive(c->ctx, pb, url, &tmp);
678  if (ret == AVERROR_EXIT) {
679  av_dict_free(&tmp);
680  return ret;
681  } else if (ret < 0) {
682  if (ret != AVERROR_EOF)
684  "keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n",
685  url, av_err2str(ret));
686  av_dict_copy(&tmp, *opts, 0);
687  av_dict_copy(&tmp, opts2, 0);
688  ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
689  }
690  } else {
691  ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
692  }
693  if (ret >= 0) {
694  // update cookies on http response with setcookies.
695  char *new_cookies = NULL;
696 
697  if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
698  av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
699 
700  if (new_cookies)
701  av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
702  }
703 
704  av_dict_free(&tmp);
705 
706  if (is_http_out)
707  *is_http_out = is_http;
708 
709  return ret;
710 }
711 
712 static int parse_playlist(HLSContext *c, const char *url,
713  struct playlist *pls, AVIOContext *in)
714 {
715  int ret = 0, is_segment = 0, is_variant = 0;
716  int64_t duration = 0;
717  enum KeyType key_type = KEY_NONE;
718  uint8_t iv[16] = "";
719  int has_iv = 0;
720  char key[MAX_URL_SIZE] = "";
721  char line[MAX_URL_SIZE];
722  const char *ptr;
723  int close_in = 0;
724  int64_t seg_offset = 0;
725  int64_t seg_size = -1;
726  uint8_t *new_url = NULL;
727  struct variant_info variant_info;
728  char tmp_str[MAX_URL_SIZE];
729  struct segment *cur_init_section = NULL;
730  int is_http = av_strstart(url, "http", NULL);
731  struct segment **prev_segments = NULL;
732  int prev_n_segments = 0;
733  int64_t prev_start_seq_no = -1;
734 
735  if (is_http && !in && c->http_persistent && c->playlist_pb) {
736  in = c->playlist_pb;
737  ret = open_url_keepalive(c->ctx, &c->playlist_pb, url, NULL);
738  if (ret == AVERROR_EXIT) {
739  return ret;
740  } else if (ret < 0) {
741  if (ret != AVERROR_EOF)
742  av_log(c->ctx, AV_LOG_WARNING,
743  "keepalive request failed for '%s' with error: '%s' when parsing playlist\n",
744  url, av_err2str(ret));
745  in = NULL;
746  }
747  }
748 
749  if (!in) {
751  av_dict_copy(&opts, c->avio_opts, 0);
752 
753  if (c->http_persistent)
754  av_dict_set(&opts, "multiple_requests", "1", 0);
755 
756  ret = c->ctx->io_open(c->ctx, &in, url, AVIO_FLAG_READ, &opts);
757  av_dict_free(&opts);
758  if (ret < 0)
759  return ret;
760 
761  if (is_http && c->http_persistent)
762  c->playlist_pb = in;
763  else
764  close_in = 1;
765  }
766 
767  if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
768  url = new_url;
769 
770  ff_get_chomp_line(in, line, sizeof(line));
771  if (strcmp(line, "#EXTM3U")) {
772  ret = AVERROR_INVALIDDATA;
773  goto fail;
774  }
775 
776  if (pls) {
777  prev_start_seq_no = pls->start_seq_no;
778  prev_segments = pls->segments;
779  prev_n_segments = pls->n_segments;
780  pls->segments = NULL;
781  pls->n_segments = 0;
782 
783  pls->finished = 0;
784  pls->type = PLS_TYPE_UNSPECIFIED;
785  }
786  while (!avio_feof(in)) {
787  ff_get_chomp_line(in, line, sizeof(line));
788  if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
789  is_variant = 1;
790  memset(&variant_info, 0, sizeof(variant_info));
792  &variant_info);
793  } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
794  struct key_info info = {{0}};
796  &info);
797  key_type = KEY_NONE;
798  has_iv = 0;
799  if (!strcmp(info.method, "AES-128"))
800  key_type = KEY_AES_128;
801  if (!strcmp(info.method, "SAMPLE-AES"))
802  key_type = KEY_SAMPLE_AES;
803  if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {
804  ff_hex_to_data(iv, info.iv + 2);
805  has_iv = 1;
806  }
807  av_strlcpy(key, info.uri, sizeof(key));
808  } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
809  struct rendition_info info = {{0}};
811  &info);
812  new_rendition(c, &info, url);
813  } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
814  int64_t t;
815  ret = ensure_playlist(c, &pls, url);
816  if (ret < 0)
817  goto fail;
818  t = strtoll(ptr, NULL, 10);
819  if (t < 0 || t >= INT64_MAX / AV_TIME_BASE) {
820  ret = AVERROR_INVALIDDATA;
821  goto fail;
822  }
823  pls->target_duration = t * AV_TIME_BASE;
824  } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
825  uint64_t seq_no;
826  ret = ensure_playlist(c, &pls, url);
827  if (ret < 0)
828  goto fail;
829  seq_no = strtoull(ptr, NULL, 10);
830  if (seq_no > INT64_MAX/2) {
831  av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than "
832  "INT64_MAX/2, mask out the highest bit\n");
833  seq_no &= INT64_MAX/2;
834  }
835  pls->start_seq_no = seq_no;
836  } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
837  ret = ensure_playlist(c, &pls, url);
838  if (ret < 0)
839  goto fail;
840  if (!strcmp(ptr, "EVENT"))
841  pls->type = PLS_TYPE_EVENT;
842  else if (!strcmp(ptr, "VOD"))
843  pls->type = PLS_TYPE_VOD;
844  } else if (av_strstart(line, "#EXT-X-MAP:", &ptr)) {
845  struct init_section_info info = {{0}};
846  ret = ensure_playlist(c, &pls, url);
847  if (ret < 0)
848  goto fail;
850  &info);
851  cur_init_section = new_init_section(pls, &info, url);
852  cur_init_section->key_type = key_type;
853  if (has_iv) {
854  memcpy(cur_init_section->iv, iv, sizeof(iv));
855  } else {
856  int64_t seq = pls->start_seq_no + pls->n_segments;
857  memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv));
858  AV_WB64(cur_init_section->iv + 8, seq);
859  }
860 
861  if (key_type != KEY_NONE) {
862  ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
863  if (!tmp_str[0]) {
864  av_free(cur_init_section);
865  ret = AVERROR_INVALIDDATA;
866  goto fail;
867  }
868  cur_init_section->key = av_strdup(tmp_str);
869  if (!cur_init_section->key) {
870  av_free(cur_init_section);
871  ret = AVERROR(ENOMEM);
872  goto fail;
873  }
874  } else {
875  cur_init_section->key = NULL;
876  }
877 
878  } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
879  if (pls)
880  pls->finished = 1;
881  } else if (av_strstart(line, "#EXTINF:", &ptr)) {
882  is_segment = 1;
883  duration = atof(ptr) * AV_TIME_BASE;
884  } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
885  seg_size = strtoll(ptr, NULL, 10);
886  ptr = strchr(ptr, '@');
887  if (ptr)
888  seg_offset = strtoll(ptr+1, NULL, 10);
889  } else if (av_strstart(line, "#", NULL)) {
890  av_log(c->ctx, AV_LOG_INFO, "Skip ('%s')\n", line);
891  continue;
892  } else if (line[0]) {
893  if (is_variant) {
894  if (!new_variant(c, &variant_info, line, url)) {
895  ret = AVERROR(ENOMEM);
896  goto fail;
897  }
898  is_variant = 0;
899  }
900  if (is_segment) {
901  struct segment *seg;
902  ret = ensure_playlist(c, &pls, url);
903  if (ret < 0)
904  goto fail;
905  seg = av_malloc(sizeof(struct segment));
906  if (!seg) {
907  ret = AVERROR(ENOMEM);
908  goto fail;
909  }
910  if (has_iv) {
911  memcpy(seg->iv, iv, sizeof(iv));
912  } else {
913  uint64_t seq = pls->start_seq_no + (uint64_t)pls->n_segments;
914  memset(seg->iv, 0, sizeof(seg->iv));
915  AV_WB64(seg->iv + 8, seq);
916  }
917 
918  if (key_type != KEY_NONE) {
919  ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
920  if (!tmp_str[0]) {
921  ret = AVERROR_INVALIDDATA;
922  av_free(seg);
923  goto fail;
924  }
925  seg->key = av_strdup(tmp_str);
926  if (!seg->key) {
927  av_free(seg);
928  ret = AVERROR(ENOMEM);
929  goto fail;
930  }
931  } else {
932  seg->key = NULL;
933  }
934 
935  ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line);
936  if (!tmp_str[0]) {
937  ret = AVERROR_INVALIDDATA;
938  if (seg->key)
939  av_free(seg->key);
940  av_free(seg);
941  goto fail;
942  }
943  seg->url = av_strdup(tmp_str);
944  if (!seg->url) {
945  av_free(seg->key);
946  av_free(seg);
947  ret = AVERROR(ENOMEM);
948  goto fail;
949  }
950 
951  if (duration < 0.001 * AV_TIME_BASE) {
952  av_log(c->ctx, AV_LOG_WARNING, "Cannot get correct #EXTINF value of segment %s,"
953  " set to default value to 1ms.\n", seg->url);
954  duration = 0.001 * AV_TIME_BASE;
955  }
956  seg->duration = duration;
957  seg->key_type = key_type;
958  dynarray_add(&pls->segments, &pls->n_segments, seg);
959  is_segment = 0;
960 
961  seg->size = seg_size;
962  if (seg_size >= 0) {
963  seg->url_offset = seg_offset;
964  seg_offset += seg_size;
965  seg_size = -1;
966  } else {
967  seg->url_offset = 0;
968  seg_offset = 0;
969  }
970 
971  seg->init_section = cur_init_section;
972  }
973  }
974  }
975  if (prev_segments) {
976  if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE) {
977  int64_t prev_timestamp = c->first_timestamp;
978  int i;
979  int64_t diff = pls->start_seq_no - prev_start_seq_no;
980  for (i = 0; i < prev_n_segments && i < diff; i++) {
981  c->first_timestamp += prev_segments[i]->duration;
982  }
983  av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")"
984  " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n",
985  prev_start_seq_no, pls->start_seq_no,
986  prev_timestamp, c->first_timestamp);
987  } else if (pls->start_seq_no < prev_start_seq_no) {
988  av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n",
989  prev_start_seq_no, pls->start_seq_no);
990  }
991  free_segment_dynarray(prev_segments, prev_n_segments);
992  av_freep(&prev_segments);
993  }
994  if (pls)
996 
997 fail:
998  av_free(new_url);
999  if (close_in)
1000  ff_format_io_close(c->ctx, &in);
1001  c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE;
1002  if (!c->n_variants || !c->variants[0]->n_playlists ||
1003  !(c->variants[0]->playlists[0]->finished ||
1004  c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT))
1005  c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE;
1006  return ret;
1007 }
1008 
1009 static struct segment *current_segment(struct playlist *pls)
1010 {
1011  return pls->segments[pls->cur_seq_no - pls->start_seq_no];
1012 }
1013 
1014 static struct segment *next_segment(struct playlist *pls)
1015 {
1016  int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1017  if (n >= pls->n_segments)
1018  return NULL;
1019  return pls->segments[n];
1020 }
1021 
1022 static int read_from_url(struct playlist *pls, struct segment *seg,
1023  uint8_t *buf, int buf_size)
1024 {
1025  int ret;
1026 
1027  /* limit read if the segment was only a part of a file */
1028  if (seg->size >= 0)
1029  buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
1030 
1031  ret = avio_read(pls->input, buf, buf_size);
1032  if (ret > 0)
1033  pls->cur_seg_offset += ret;
1034 
1035  return ret;
1036 }
1037 
1038 /* Parse the raw ID3 data and pass contents to caller */
1040  AVDictionary **metadata, int64_t *dts,
1041  ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
1042 {
1043  static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp";
1044  ID3v2ExtraMeta *meta;
1045 
1046  ff_id3v2_read_dict(pb, metadata, ID3v2_DEFAULT_MAGIC, extra_meta);
1047  for (meta = *extra_meta; meta; meta = meta->next) {
1048  if (!strcmp(meta->tag, "PRIV")) {
1049  ID3v2ExtraMetaPRIV *priv = &meta->data.priv;
1050  if (priv->datasize == 8 && !strcmp(priv->owner, id3_priv_owner_ts)) {
1051  /* 33-bit MPEG timestamp */
1052  int64_t ts = AV_RB64(priv->data);
1053  av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts);
1054  if ((ts & ~((1ULL << 33) - 1)) == 0)
1055  *dts = ts;
1056  else
1057  av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts);
1058  }
1059  } else if (!strcmp(meta->tag, "APIC") && apic)
1060  *apic = &meta->data.apic;
1061  }
1062 }
1063 
1064 /* Check if the ID3 metadata contents have changed */
1065 static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata,
1066  ID3v2ExtraMetaAPIC *apic)
1067 {
1068  AVDictionaryEntry *entry = NULL;
1069  AVDictionaryEntry *oldentry;
1070  /* check that no keys have changed values */
1071  while ((entry = av_dict_get(metadata, "", entry, AV_DICT_IGNORE_SUFFIX))) {
1072  oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE);
1073  if (!oldentry || strcmp(oldentry->value, entry->value) != 0)
1074  return 1;
1075  }
1076 
1077  /* check if apic appeared */
1078  if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
1079  return 1;
1080 
1081  if (apic) {
1082  int size = pls->ctx->streams[1]->attached_pic.size;
1083  if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE)
1084  return 1;
1085 
1086  if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
1087  return 1;
1088  }
1089 
1090  return 0;
1091 }
1092 
1093 /* Parse ID3 data and handle the found data */
1094 static void handle_id3(AVIOContext *pb, struct playlist *pls)
1095 {
1096  AVDictionary *metadata = NULL;
1097  ID3v2ExtraMetaAPIC *apic = NULL;
1098  ID3v2ExtraMeta *extra_meta = NULL;
1099  int64_t timestamp = AV_NOPTS_VALUE;
1100 
1101  parse_id3(pls->ctx, pb, &metadata, &timestamp, &apic, &extra_meta);
1102 
1103  if (timestamp != AV_NOPTS_VALUE) {
1104  pls->id3_mpegts_timestamp = timestamp;
1105  pls->id3_offset = 0;
1106  }
1107 
1108  if (!pls->id3_found) {
1109  /* initial ID3 tags */
1111  pls->id3_found = 1;
1112 
1113  /* get picture attachment and set text metadata */
1114  if (pls->ctx->nb_streams)
1115  ff_id3v2_parse_apic(pls->ctx, extra_meta);
1116  else
1117  /* demuxer not yet opened, defer picture attachment */
1118  pls->id3_deferred_extra = extra_meta;
1119 
1120  ff_id3v2_parse_priv_dict(&metadata, extra_meta);
1121  av_dict_copy(&pls->ctx->metadata, metadata, 0);
1122  pls->id3_initial = metadata;
1123 
1124  } else {
1125  if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) {
1126  avpriv_report_missing_feature(pls->parent, "Changing ID3 metadata in HLS audio elementary stream");
1127  pls->id3_changed = 1;
1128  }
1129  av_dict_free(&metadata);
1130  }
1131 
1132  if (!pls->id3_deferred_extra)
1133  ff_id3v2_free_extra_meta(&extra_meta);
1134 }
1135 
1136 static void intercept_id3(struct playlist *pls, uint8_t *buf,
1137  int buf_size, int *len)
1138 {
1139  /* intercept id3 tags, we do not want to pass them to the raw
1140  * demuxer on all segment switches */
1141  int bytes;
1142  int id3_buf_pos = 0;
1143  int fill_buf = 0;
1144  struct segment *seg = current_segment(pls);
1145 
1146  /* gather all the id3 tags */
1147  while (1) {
1148  /* see if we can retrieve enough data for ID3 header */
1149  if (*len < ID3v2_HEADER_SIZE && buf_size >= ID3v2_HEADER_SIZE) {
1150  bytes = read_from_url(pls, seg, buf + *len, ID3v2_HEADER_SIZE - *len);
1151  if (bytes > 0) {
1152 
1153  if (bytes == ID3v2_HEADER_SIZE - *len)
1154  /* no EOF yet, so fill the caller buffer again after
1155  * we have stripped the ID3 tags */
1156  fill_buf = 1;
1157 
1158  *len += bytes;
1159 
1160  } else if (*len <= 0) {
1161  /* error/EOF */
1162  *len = bytes;
1163  fill_buf = 0;
1164  }
1165  }
1166 
1167  if (*len < ID3v2_HEADER_SIZE)
1168  break;
1169 
1170  if (ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) {
1171  int64_t maxsize = seg->size >= 0 ? seg->size : 1024*1024;
1172  int taglen = ff_id3v2_tag_len(buf);
1173  int tag_got_bytes = FFMIN(taglen, *len);
1174  int remaining = taglen - tag_got_bytes;
1175 
1176  if (taglen > maxsize) {
1177  av_log(pls->parent, AV_LOG_ERROR, "Too large HLS ID3 tag (%d > %"PRId64" bytes)\n",
1178  taglen, maxsize);
1179  break;
1180  }
1181 
1182  /*
1183  * Copy the id3 tag to our temporary id3 buffer.
1184  * We could read a small id3 tag directly without memcpy, but
1185  * we would still need to copy the large tags, and handling
1186  * both of those cases together with the possibility for multiple
1187  * tags would make the handling a bit complex.
1188  */
1189  pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen);
1190  if (!pls->id3_buf)
1191  break;
1192  memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes);
1193  id3_buf_pos += tag_got_bytes;
1194 
1195  /* strip the intercepted bytes */
1196  *len -= tag_got_bytes;
1197  memmove(buf, buf + tag_got_bytes, *len);
1198  av_log(pls->parent, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes);
1199 
1200  if (remaining > 0) {
1201  /* read the rest of the tag in */
1202  if (read_from_url(pls, seg, pls->id3_buf + id3_buf_pos, remaining) != remaining)
1203  break;
1204  id3_buf_pos += remaining;
1205  av_log(pls->parent, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining);
1206  }
1207 
1208  } else {
1209  /* no more ID3 tags */
1210  break;
1211  }
1212  }
1213 
1214  /* re-fill buffer for the caller unless EOF */
1215  if (*len >= 0 && (fill_buf || *len == 0)) {
1216  bytes = read_from_url(pls, seg, buf + *len, buf_size - *len);
1217 
1218  /* ignore error if we already had some data */
1219  if (bytes >= 0)
1220  *len += bytes;
1221  else if (*len == 0)
1222  *len = bytes;
1223  }
1224 
1225  if (pls->id3_buf) {
1226  /* Now parse all the ID3 tags */
1227  AVIOContext id3ioctx;
1228  ffio_init_context(&id3ioctx, pls->id3_buf, id3_buf_pos, 0, NULL, NULL, NULL, NULL);
1229  handle_id3(&id3ioctx, pls);
1230  }
1231 
1232  if (pls->is_id3_timestamped == -1)
1234 }
1235 
1236 static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in)
1237 {
1238  AVDictionary *opts = NULL;
1239  int ret;
1240  int is_http = 0;
1241 
1242  if (c->http_persistent)
1243  av_dict_set(&opts, "multiple_requests", "1", 0);
1244 
1245  if (seg->size >= 0) {
1246  /* try to restrict the HTTP request to the part we want
1247  * (if this is in fact a HTTP request) */
1248  av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1249  av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0);
1250  }
1251 
1252  av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
1253  seg->url, seg->url_offset, pls->index);
1254 
1255  if (seg->key_type == KEY_NONE) {
1256  ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http);
1257  } else if (seg->key_type == KEY_AES_128) {
1258  char iv[33], key[33], url[MAX_URL_SIZE];
1259  if (strcmp(seg->key, pls->key_url)) {
1260  AVIOContext *pb = NULL;
1261  if (open_url(pls->parent, &pb, seg->key, &c->avio_opts, opts, NULL) == 0) {
1262  ret = avio_read(pb, pls->key, sizeof(pls->key));
1263  if (ret != sizeof(pls->key)) {
1264  av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s\n",
1265  seg->key);
1266  }
1267  ff_format_io_close(pls->parent, &pb);
1268  } else {
1269  av_log(pls->parent, AV_LOG_ERROR, "Unable to open key file %s\n",
1270  seg->key);
1271  }
1272  av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url));
1273  }
1274  ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
1275  ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
1276  iv[32] = key[32] = '\0';
1277  if (strstr(seg->url, "://"))
1278  snprintf(url, sizeof(url), "crypto+%s", seg->url);
1279  else
1280  snprintf(url, sizeof(url), "crypto:%s", seg->url);
1281 
1282  av_dict_set(&opts, "key", key, 0);
1283  av_dict_set(&opts, "iv", iv, 0);
1284 
1285  ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http);
1286  if (ret < 0) {
1287  goto cleanup;
1288  }
1289  ret = 0;
1290  } else if (seg->key_type == KEY_SAMPLE_AES) {
1291  av_log(pls->parent, AV_LOG_ERROR,
1292  "SAMPLE-AES encryption is not supported yet\n");
1293  ret = AVERROR_PATCHWELCOME;
1294  }
1295  else
1296  ret = AVERROR(ENOSYS);
1297 
1298  /* Seek to the requested position. If this was a HTTP request, the offset
1299  * should already be where want it to, but this allows e.g. local testing
1300  * without a HTTP server.
1301  *
1302  * This is not done for HTTP at all as avio_seek() does internal bookkeeping
1303  * of file offset which is out-of-sync with the actual offset when "offset"
1304  * AVOption is used with http protocol, causing the seek to not be a no-op
1305  * as would be expected. Wrong offset received from the server will not be
1306  * noticed without the call, though.
1307  */
1308  if (ret == 0 && !is_http && seg->url_offset) {
1309  int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET);
1310  if (seekret < 0) {
1311  av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
1312  ret = seekret;
1313  ff_format_io_close(pls->parent, in);
1314  }
1315  }
1316 
1317 cleanup:
1318  av_dict_free(&opts);
1319  pls->cur_seg_offset = 0;
1320  return ret;
1321 }
1322 
1323 static int update_init_section(struct playlist *pls, struct segment *seg)
1324 {
1325  static const int max_init_section_size = 1024*1024;
1326  HLSContext *c = pls->parent->priv_data;
1327  int64_t sec_size;
1328  int64_t urlsize;
1329  int ret;
1330 
1331  if (seg->init_section == pls->cur_init_section)
1332  return 0;
1333 
1334  pls->cur_init_section = NULL;
1335 
1336  if (!seg->init_section)
1337  return 0;
1338 
1339  ret = open_input(c, pls, seg->init_section, &pls->input);
1340  if (ret < 0) {
1342  "Failed to open an initialization section in playlist %d\n",
1343  pls->index);
1344  return ret;
1345  }
1346 
1347  if (seg->init_section->size >= 0)
1348  sec_size = seg->init_section->size;
1349  else if ((urlsize = avio_size(pls->input)) >= 0)
1350  sec_size = urlsize;
1351  else
1352  sec_size = max_init_section_size;
1353 
1354  av_log(pls->parent, AV_LOG_DEBUG,
1355  "Downloading an initialization section of size %"PRId64"\n",
1356  sec_size);
1357 
1358  sec_size = FFMIN(sec_size, max_init_section_size);
1359 
1360  av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1361 
1362  ret = read_from_url(pls, seg->init_section, pls->init_sec_buf,
1363  pls->init_sec_buf_size);
1364  ff_format_io_close(pls->parent, &pls->input);
1365 
1366  if (ret < 0)
1367  return ret;
1368 
1369  pls->cur_init_section = seg->init_section;
1370  pls->init_sec_data_len = ret;
1371  pls->init_sec_buf_read_offset = 0;
1372 
1373  /* spec says audio elementary streams do not have media initialization
1374  * sections, so there should be no ID3 timestamps */
1375  pls->is_id3_timestamped = 0;
1376 
1377  return 0;
1378 }
1379 
1380 static int64_t default_reload_interval(struct playlist *pls)
1381 {
1382  return pls->n_segments > 0 ?
1383  pls->segments[pls->n_segments - 1]->duration :
1384  pls->target_duration;
1385 }
1386 
1387 static int playlist_needed(struct playlist *pls)
1388 {
1389  AVFormatContext *s = pls->parent;
1390  int i, j;
1391  int stream_needed = 0;
1392  int first_st;
1393 
1394  /* If there is no context or streams yet, the playlist is needed */
1395  if (!pls->ctx || !pls->n_main_streams)
1396  return 1;
1397 
1398  /* check if any of the streams in the playlist are needed */
1399  for (i = 0; i < pls->n_main_streams; i++) {
1400  if (pls->main_streams[i]->discard < AVDISCARD_ALL) {
1401  stream_needed = 1;
1402  break;
1403  }
1404  }
1405 
1406  /* If all streams in the playlist were discarded, the playlist is not
1407  * needed (regardless of whether whole programs are discarded or not). */
1408  if (!stream_needed)
1409  return 0;
1410 
1411  /* Otherwise, check if all the programs (variants) this playlist is in are
1412  * discarded. Since all streams in the playlist are part of the same programs
1413  * we can just check the programs of the first stream. */
1414 
1415  first_st = pls->main_streams[0]->index;
1416 
1417  for (i = 0; i < s->nb_programs; i++) {
1418  AVProgram *program = s->programs[i];
1419  if (program->discard < AVDISCARD_ALL) {
1420  for (j = 0; j < program->nb_stream_indexes; j++) {
1421  if (program->stream_index[j] == first_st) {
1422  /* playlist is in an undiscarded program */
1423  return 1;
1424  }
1425  }
1426  }
1427  }
1428 
1429  /* some streams were not discarded but all the programs were */
1430  return 0;
1431 }
1432 
1433 static int read_data(void *opaque, uint8_t *buf, int buf_size)
1434 {
1435  struct playlist *v = opaque;
1436  HLSContext *c = v->parent->priv_data;
1437  int ret;
1438  int just_opened = 0;
1439  int reload_count = 0;
1440  struct segment *seg;
1441 
1442 restart:
1443  if (!v->needed)
1444  return AVERROR_EOF;
1445 
1446  if (!v->input || (c->http_persistent && v->input_read_done)) {
1447  int64_t reload_interval;
1448 
1449  /* Check that the playlist is still needed before opening a new
1450  * segment. */
1451  v->needed = playlist_needed(v);
1452 
1453  if (!v->needed) {
1454  av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d ('%s')\n",
1455  v->index, v->url);
1456  return AVERROR_EOF;
1457  }
1458 
1459  /* If this is a live stream and the reload interval has elapsed since
1460  * the last playlist reload, reload the playlists now. */
1461  reload_interval = default_reload_interval(v);
1462 
1463 reload:
1464  reload_count++;
1465  if (reload_count > c->max_reload)
1466  return AVERROR_EOF;
1467  if (!v->finished &&
1468  av_gettime_relative() - v->last_load_time >= reload_interval) {
1469  if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) {
1470  if (ret != AVERROR_EXIT)
1471  av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n",
1472  v->index);
1473  return ret;
1474  }
1475  /* If we need to reload the playlist again below (if
1476  * there's still no more segments), switch to a reload
1477  * interval of half the target duration. */
1478  reload_interval = v->target_duration / 2;
1479  }
1480  if (v->cur_seq_no < v->start_seq_no) {
1482  "skipping %"PRId64" segments ahead, expired from playlists\n",
1483  v->start_seq_no - v->cur_seq_no);
1484  v->cur_seq_no = v->start_seq_no;
1485  }
1486  if (v->cur_seq_no > v->last_seq_no) {
1487  v->last_seq_no = v->cur_seq_no;
1488  v->m3u8_hold_counters = 0;
1489  } else if (v->last_seq_no == v->cur_seq_no) {
1490  v->m3u8_hold_counters++;
1491  if (v->m3u8_hold_counters >= c->m3u8_hold_counters) {
1492  return AVERROR_EOF;
1493  }
1494  } else {
1495  av_log(v->parent, AV_LOG_WARNING, "maybe the m3u8 list sequence have been wraped.\n");
1496  }
1497  if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
1498  if (v->finished)
1499  return AVERROR_EOF;
1500  while (av_gettime_relative() - v->last_load_time < reload_interval) {
1501  if (ff_check_interrupt(c->interrupt_callback))
1502  return AVERROR_EXIT;
1503  av_usleep(100*1000);
1504  }
1505  /* Enough time has elapsed since the last reload */
1506  goto reload;
1507  }
1508 
1509  v->input_read_done = 0;
1510  seg = current_segment(v);
1511 
1512  /* load/update Media Initialization Section, if any */
1513  ret = update_init_section(v, seg);
1514  if (ret)
1515  return ret;
1516 
1517  if (c->http_multiple == 1 && v->input_next_requested) {
1518  FFSWAP(AVIOContext *, v->input, v->input_next);
1519  v->cur_seg_offset = 0;
1520  v->input_next_requested = 0;
1521  ret = 0;
1522  } else {
1523  ret = open_input(c, v, seg, &v->input);
1524  }
1525  if (ret < 0) {
1526  if (ff_check_interrupt(c->interrupt_callback))
1527  return AVERROR_EXIT;
1528  av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1529  v->cur_seq_no,
1530  v->index);
1531  v->cur_seq_no += 1;
1532  goto reload;
1533  }
1534  just_opened = 1;
1535  }
1536 
1537  if (c->http_multiple == -1) {
1538  uint8_t *http_version_opt = NULL;
1539  int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt);
1540  if (r >= 0) {
1541  c->http_multiple = (!strncmp((const char *)http_version_opt, "1.1", 3) || !strncmp((const char *)http_version_opt, "2.0", 3));
1542  av_freep(&http_version_opt);
1543  }
1544  }
1545 
1546  seg = next_segment(v);
1547  if (c->http_multiple == 1 && !v->input_next_requested &&
1548  seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) {
1549  ret = open_input(c, v, seg, &v->input_next);
1550  if (ret < 0) {
1551  if (ff_check_interrupt(c->interrupt_callback))
1552  return AVERROR_EXIT;
1553  av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1554  v->cur_seq_no + 1,
1555  v->index);
1556  } else {
1557  v->input_next_requested = 1;
1558  }
1559  }
1560 
1562  /* Push init section out first before first actual segment */
1563  int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1564  memcpy(buf, v->init_sec_buf, copy_size);
1565  v->init_sec_buf_read_offset += copy_size;
1566  return copy_size;
1567  }
1568 
1569  seg = current_segment(v);
1570  ret = read_from_url(v, seg, buf, buf_size);
1571  if (ret > 0) {
1572  if (just_opened && v->is_id3_timestamped != 0) {
1573  /* Intercept ID3 tags here, elementary audio streams are required
1574  * to convey timestamps using them in the beginning of each segment. */
1575  intercept_id3(v, buf, buf_size, &ret);
1576  }
1577 
1578  return ret;
1579  }
1580  if (c->http_persistent &&
1581  seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) {
1582  v->input_read_done = 1;
1583  } else {
1584  ff_format_io_close(v->parent, &v->input);
1585  }
1586  v->cur_seq_no++;
1587 
1588  c->cur_seq_no = v->cur_seq_no;
1589 
1590  goto restart;
1591 }
1592 
1593 static void add_renditions_to_variant(HLSContext *c, struct variant *var,
1594  enum AVMediaType type, const char *group_id)
1595 {
1596  int i;
1597 
1598  for (i = 0; i < c->n_renditions; i++) {
1599  struct rendition *rend = c->renditions[i];
1600 
1601  if (rend->type == type && !strcmp(rend->group_id, group_id)) {
1602 
1603  if (rend->playlist)
1604  /* rendition is an external playlist
1605  * => add the playlist to the variant */
1606  dynarray_add(&var->playlists, &var->n_playlists, rend->playlist);
1607  else
1608  /* rendition is part of the variant main Media Playlist
1609  * => add the rendition to the main Media Playlist */
1610  dynarray_add(&var->playlists[0]->renditions,
1611  &var->playlists[0]->n_renditions,
1612  rend);
1613  }
1614  }
1615 }
1616 
1618  enum AVMediaType type)
1619 {
1620  int rend_idx = 0;
1621  int i;
1622 
1623  for (i = 0; i < pls->n_main_streams; i++) {
1624  AVStream *st = pls->main_streams[i];
1625 
1626  if (st->codecpar->codec_type != type)
1627  continue;
1628 
1629  for (; rend_idx < pls->n_renditions; rend_idx++) {
1630  struct rendition *rend = pls->renditions[rend_idx];
1631 
1632  if (rend->type != type)
1633  continue;
1634 
1635  if (rend->language[0])
1636  av_dict_set(&st->metadata, "language", rend->language, 0);
1637  if (rend->name[0])
1638  av_dict_set(&st->metadata, "comment", rend->name, 0);
1639 
1640  st->disposition |= rend->disposition;
1641  }
1642  if (rend_idx >=pls->n_renditions)
1643  break;
1644  }
1645 }
1646 
1647 /* if timestamp was in valid range: returns 1 and sets seq_no
1648  * if not: returns 0 and sets seq_no to closest segment */
1650  int64_t timestamp, int64_t *seq_no)
1651 {
1652  int i;
1653  int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
1654  0 : c->first_timestamp;
1655 
1656  if (timestamp < pos) {
1657  *seq_no = pls->start_seq_no;
1658  return 0;
1659  }
1660 
1661  for (i = 0; i < pls->n_segments; i++) {
1662  int64_t diff = pos + pls->segments[i]->duration - timestamp;
1663  if (diff > 0) {
1664  *seq_no = pls->start_seq_no + i;
1665  return 1;
1666  }
1667  pos += pls->segments[i]->duration;
1668  }
1669 
1670  *seq_no = pls->start_seq_no + pls->n_segments - 1;
1671 
1672  return 0;
1673 }
1674 
1675 static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
1676 {
1677  int64_t seq_no;
1678 
1679  if (!pls->finished && !c->first_packet &&
1681  /* reload the playlist since it was suspended */
1682  parse_playlist(c, pls->url, pls, NULL);
1683 
1684  /* If playback is already in progress (we are just selecting a new
1685  * playlist) and this is a complete file, find the matching segment
1686  * by counting durations. */
1687  if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
1688  find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no);
1689  return seq_no;
1690  }
1691 
1692  if (!pls->finished) {
1693  if (!c->first_packet && /* we are doing a segment selection during playback */
1694  c->cur_seq_no >= pls->start_seq_no &&
1695  c->cur_seq_no < pls->start_seq_no + pls->n_segments)
1696  /* While spec 3.4.3 says that we cannot assume anything about the
1697  * content at the same sequence number on different playlists,
1698  * in practice this seems to work and doing it otherwise would
1699  * require us to download a segment to inspect its timestamps. */
1700  return c->cur_seq_no;
1701 
1702  /* If this is a live stream, start live_start_index segments from the
1703  * start or end */
1704  if (c->live_start_index < 0)
1705  return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0);
1706  else
1707  return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1);
1708  }
1709 
1710  /* Otherwise just start on the first segment. */
1711  return pls->start_seq_no;
1712 }
1713 
1715 {
1716  HLSContext *c = s->priv_data;
1717  static const char * const opts[] = {
1718  "headers", "http_proxy", "user_agent", "cookies", "referer", "rw_timeout", "icy", NULL };
1719  const char * const * opt = opts;
1720  uint8_t *buf;
1721  int ret = 0;
1722 
1723  while (*opt) {
1724  if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN | AV_OPT_ALLOW_NULL, &buf) >= 0) {
1725  ret = av_dict_set(&c->avio_opts, *opt, buf,
1727  if (ret < 0)
1728  return ret;
1729  }
1730  opt++;
1731  }
1732 
1733  return ret;
1734 }
1735 
1736 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1737  int flags, AVDictionary **opts)
1738 {
1740  "A HLS playlist item '%s' referred to an external file '%s'. "
1741  "Opening this file was forbidden for security reasons\n",
1742  s->url, url);
1743  return AVERROR(EPERM);
1744 }
1745 
1746 static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
1747 {
1748  HLSContext *c = s->priv_data;
1749  int i, j;
1750  int bandwidth = -1;
1751 
1752  for (i = 0; i < c->n_variants; i++) {
1753  struct variant *v = c->variants[i];
1754 
1755  for (j = 0; j < v->n_playlists; j++) {
1756  if (v->playlists[j] != pls)
1757  continue;
1758 
1759  av_program_add_stream_index(s, i, stream->index);
1760 
1761  if (bandwidth < 0)
1762  bandwidth = v->bandwidth;
1763  else if (bandwidth != v->bandwidth)
1764  bandwidth = -1; /* stream in multiple variants with different bandwidths */
1765  }
1766  }
1767 
1768  if (bandwidth >= 0)
1769  av_dict_set_int(&stream->metadata, "variant_bitrate", bandwidth, 0);
1770 }
1771 
1773 {
1774  int err;
1775 
1776  err = avcodec_parameters_copy(st->codecpar, ist->codecpar);
1777  if (err < 0)
1778  return err;
1779 
1780  if (pls->is_id3_timestamped) /* custom timestamps via id3 */
1781  avpriv_set_pts_info(st, 33, 1, MPEG_TIME_BASE);
1782  else
1784 
1785  // copy disposition
1786  st->disposition = ist->disposition;
1787 
1788  // copy side data
1789  for (int i = 0; i < ist->nb_side_data; i++) {
1790  const AVPacketSideData *sd_src = &ist->side_data[i];
1791  uint8_t *dst_data;
1792 
1793  dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
1794  if (!dst_data)
1795  return AVERROR(ENOMEM);
1796  memcpy(dst_data, sd_src->data, sd_src->size);
1797  }
1798 
1799  st->internal->need_context_update = 1;
1800 
1801  return 0;
1802 }
1803 
1804 /* add new subdemuxer streams to our context, if any */
1806 {
1807  int err;
1808 
1809  while (pls->n_main_streams < pls->ctx->nb_streams) {
1810  int ist_idx = pls->n_main_streams;
1812  AVStream *ist = pls->ctx->streams[ist_idx];
1813 
1814  if (!st)
1815  return AVERROR(ENOMEM);
1816 
1817  st->id = pls->index;
1818  dynarray_add(&pls->main_streams, &pls->n_main_streams, st);
1819 
1820  add_stream_to_programs(s, pls, st);
1821 
1822  err = set_stream_info_from_input_stream(st, pls, ist);
1823  if (err < 0)
1824  return err;
1825  }
1826 
1827  return 0;
1828 }
1829 
1831 {
1832  HLSContext *c = s->priv_data;
1833  int flag_needed = 0;
1834  int i;
1835 
1836  for (i = 0; i < c->n_playlists; i++) {
1837  struct playlist *pls = c->playlists[i];
1838 
1839  if (pls->has_noheader_flag) {
1840  flag_needed = 1;
1841  break;
1842  }
1843  }
1844 
1845  if (flag_needed)
1846  s->ctx_flags |= AVFMTCTX_NOHEADER;
1847  else
1848  s->ctx_flags &= ~AVFMTCTX_NOHEADER;
1849 }
1850 
1852 {
1853  HLSContext *c = s->priv_data;
1854 
1858 
1859  av_dict_free(&c->avio_opts);
1860  ff_format_io_close(c->ctx, &c->playlist_pb);
1861 
1862  return 0;
1863 }
1864 
1866 {
1867  HLSContext *c = s->priv_data;
1868  int ret = 0, i;
1869  int64_t highest_cur_seq_no = 0;
1870 
1871  c->ctx = s;
1872  c->interrupt_callback = &s->interrupt_callback;
1873 
1874  c->first_packet = 1;
1875  c->first_timestamp = AV_NOPTS_VALUE;
1876  c->cur_timestamp = AV_NOPTS_VALUE;
1877 
1878  if ((ret = save_avio_options(s)) < 0)
1879  goto fail;
1880 
1881  /* XXX: Some HLS servers don't like being sent the range header,
1882  in this case, need to setting http_seekable = 0 to disable
1883  the range header */
1884  av_dict_set_int(&c->avio_opts, "seekable", c->http_seekable, 0);
1885 
1886  if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
1887  goto fail;
1888 
1889  if (c->n_variants == 0) {
1890  av_log(s, AV_LOG_WARNING, "Empty playlist\n");
1891  ret = AVERROR_EOF;
1892  goto fail;
1893  }
1894  /* If the playlist only contained playlists (Master Playlist),
1895  * parse each individual playlist. */
1896  if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
1897  for (i = 0; i < c->n_playlists; i++) {
1898  struct playlist *pls = c->playlists[i];
1899  pls->m3u8_hold_counters = 0;
1900  if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0) {
1901  av_log(s, AV_LOG_WARNING, "parse_playlist error %s [%s]\n", av_err2str(ret), pls->url);
1902  pls->broken = 1;
1903  if (c->n_playlists > 1)
1904  continue;
1905  goto fail;
1906  }
1907  }
1908  }
1909 
1910  for (i = 0; i < c->n_variants; i++) {
1911  if (c->variants[i]->playlists[0]->n_segments == 0) {
1912  av_log(s, AV_LOG_WARNING, "Empty segment [%s]\n", c->variants[i]->playlists[0]->url);
1913  c->variants[i]->playlists[0]->broken = 1;
1914  }
1915  }
1916 
1917  /* If this isn't a live stream, calculate the total duration of the
1918  * stream. */
1919  if (c->variants[0]->playlists[0]->finished) {
1920  int64_t duration = 0;
1921  for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
1922  duration += c->variants[0]->playlists[0]->segments[i]->duration;
1923  s->duration = duration;
1924  }
1925 
1926  /* Associate renditions with variants */
1927  for (i = 0; i < c->n_variants; i++) {
1928  struct variant *var = c->variants[i];
1929 
1930  if (var->audio_group[0])
1932  if (var->video_group[0])
1934  if (var->subtitles_group[0])
1936  }
1937 
1938  /* Create a program for each variant */
1939  for (i = 0; i < c->n_variants; i++) {
1940  struct variant *v = c->variants[i];
1941  AVProgram *program;
1942 
1943  program = av_new_program(s, i);
1944  if (!program)
1945  goto fail;
1946  av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
1947  }
1948 
1949  /* Select the starting segments */
1950  for (i = 0; i < c->n_playlists; i++) {
1951  struct playlist *pls = c->playlists[i];
1952 
1953  if (pls->n_segments == 0)
1954  continue;
1955 
1956  pls->cur_seq_no = select_cur_seq_no(c, pls);
1957  highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no);
1958  }
1959 
1960  /* Open the demuxer for each playlist */
1961  for (i = 0; i < c->n_playlists; i++) {
1962  struct playlist *pls = c->playlists[i];
1963  char *url;
1964  ff_const59 AVInputFormat *in_fmt = NULL;
1965 
1966  if (!(pls->ctx = avformat_alloc_context())) {
1967  ret = AVERROR(ENOMEM);
1968  goto fail;
1969  }
1970 
1971  if (pls->n_segments == 0)
1972  continue;
1973 
1974  pls->index = i;
1975  pls->needed = 1;
1976  pls->parent = s;
1977 
1978  /*
1979  * If this is a live stream and this playlist looks like it is one segment
1980  * behind, try to sync it up so that every substream starts at the same
1981  * time position (so e.g. avformat_find_stream_info() will see packets from
1982  * all active streams within the first few seconds). This is not very generic,
1983  * though, as the sequence numbers are technically independent.
1984  */
1985  if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 &&
1986  highest_cur_seq_no < pls->start_seq_no + pls->n_segments) {
1987  pls->cur_seq_no = highest_cur_seq_no;
1988  }
1989 
1991  if (!pls->read_buffer){
1992  ret = AVERROR(ENOMEM);
1993  avformat_free_context(pls->ctx);
1994  pls->ctx = NULL;
1995  goto fail;
1996  }
1997  ffio_init_context(&pls->pb, pls->read_buffer, INITIAL_BUFFER_SIZE, 0, pls,
1998  read_data, NULL, NULL);
1999  pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
2000  pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
2001  pls->ctx->interrupt_callback = s->interrupt_callback;
2002  url = av_strdup(pls->segments[0]->url);
2003  ret = av_probe_input_buffer(&pls->pb, &in_fmt, url, NULL, 0, 0);
2004  if (ret < 0) {
2005  /* Free the ctx - it isn't initialized properly at this point,
2006  * so avformat_close_input shouldn't be called. If
2007  * avformat_open_input fails below, it frees and zeros the
2008  * context, so it doesn't need any special treatment like this. */
2009  av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url);
2010  avformat_free_context(pls->ctx);
2011  pls->ctx = NULL;
2012  av_free(url);
2013  goto fail;
2014  }
2015  av_free(url);
2016  pls->ctx->pb = &pls->pb;
2017  pls->ctx->io_open = nested_io_open;
2018  pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
2019 
2020  if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
2021  goto fail;
2022 
2023  ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, NULL);
2024  if (ret < 0)
2025  goto fail;
2026 
2027  if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) {
2032  }
2033 
2034  if (pls->is_id3_timestamped == -1)
2035  av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n");
2036 
2037  /*
2038  * For ID3 timestamped raw audio streams we need to detect the packet
2039  * durations to calculate timestamps in fill_timing_for_id3_timestamped_stream(),
2040  * but for other streams we can rely on our user calling avformat_find_stream_info()
2041  * on us if they want to.
2042  */
2043  if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) {
2044  ret = avformat_find_stream_info(pls->ctx, NULL);
2045  if (ret < 0)
2046  goto fail;
2047  }
2048 
2049  pls->has_noheader_flag = !!(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER);
2050 
2051  /* Create new AVStreams for each stream in this playlist */
2052  ret = update_streams_from_subdemuxer(s, pls);
2053  if (ret < 0)
2054  goto fail;
2055 
2056  /*
2057  * Copy any metadata from playlist to main streams, but do not set
2058  * event flags.
2059  */
2060  if (pls->n_main_streams)
2061  av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0);
2062 
2066  }
2067 
2069 
2070  return 0;
2071 fail:
2072  hls_close(s);
2073  return ret;
2074 }
2075 
2076 static int recheck_discard_flags(AVFormatContext *s, int first)
2077 {
2078  HLSContext *c = s->priv_data;
2079  int i, changed = 0;
2080  int cur_needed;
2081 
2082  /* Check if any new streams are needed */
2083  for (i = 0; i < c->n_playlists; i++) {
2084  struct playlist *pls = c->playlists[i];
2085 
2086  cur_needed = playlist_needed(c->playlists[i]);
2087 
2088  if (pls->broken) {
2089  continue;
2090  }
2091  if (cur_needed && !pls->needed) {
2092  pls->needed = 1;
2093  changed = 1;
2094  pls->cur_seq_no = select_cur_seq_no(c, pls);
2095  pls->pb.eof_reached = 0;
2096  if (c->cur_timestamp != AV_NOPTS_VALUE) {
2097  /* catch up */
2098  pls->seek_timestamp = c->cur_timestamp;
2099  pls->seek_flags = AVSEEK_FLAG_ANY;
2100  pls->seek_stream_index = -1;
2101  }
2102  av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no);
2103  } else if (first && !cur_needed && pls->needed) {
2104  ff_format_io_close(pls->parent, &pls->input);
2105  pls->input_read_done = 0;
2106  ff_format_io_close(pls->parent, &pls->input_next);
2107  pls->input_next_requested = 0;
2108  pls->needed = 0;
2109  changed = 1;
2110  av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
2111  }
2112  }
2113  return changed;
2114 }
2115 
2117 {
2118  if (pls->id3_offset >= 0) {
2119  pls->pkt->dts = pls->id3_mpegts_timestamp +
2120  av_rescale_q(pls->id3_offset,
2121  pls->ctx->streams[pls->pkt->stream_index]->time_base,
2123  if (pls->pkt->duration)
2124  pls->id3_offset += pls->pkt->duration;
2125  else
2126  pls->id3_offset = -1;
2127  } else {
2128  /* there have been packets with unknown duration
2129  * since the last id3 tag, should not normally happen */
2130  pls->pkt->dts = AV_NOPTS_VALUE;
2131  }
2132 
2133  if (pls->pkt->duration)
2134  pls->pkt->duration = av_rescale_q(pls->pkt->duration,
2135  pls->ctx->streams[pls->pkt->stream_index]->time_base,
2137 
2138  pls->pkt->pts = AV_NOPTS_VALUE;
2139 }
2140 
2141 static AVRational get_timebase(struct playlist *pls)
2142 {
2143  if (pls->is_id3_timestamped)
2144  return MPEG_TIME_BASE_Q;
2145 
2146  return pls->ctx->streams[pls->pkt->stream_index]->time_base;
2147 }
2148 
2149 static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a,
2150  int64_t ts_b, struct playlist *pls_b)
2151 {
2152  int64_t scaled_ts_a = av_rescale_q(ts_a, get_timebase(pls_a), MPEG_TIME_BASE_Q);
2153  int64_t scaled_ts_b = av_rescale_q(ts_b, get_timebase(pls_b), MPEG_TIME_BASE_Q);
2154 
2155  return av_compare_mod(scaled_ts_a, scaled_ts_b, 1LL << 33);
2156 }
2157 
2159 {
2160  HLSContext *c = s->priv_data;
2161  int ret, i, minplaylist = -1;
2162 
2163  recheck_discard_flags(s, c->first_packet);
2164  c->first_packet = 0;
2165 
2166  for (i = 0; i < c->n_playlists; i++) {
2167  struct playlist *pls = c->playlists[i];
2168  /* Make sure we've got one buffered packet from each open playlist
2169  * stream */
2170  if (pls->needed && !pls->pkt->data) {
2171  while (1) {
2172  int64_t ts_diff;
2173  AVRational tb;
2174  ret = av_read_frame(pls->ctx, pls->pkt);
2175  if (ret < 0) {
2176  if (!avio_feof(&pls->pb) && ret != AVERROR_EOF)
2177  return ret;
2178  break;
2179  } else {
2180  /* stream_index check prevents matching picture attachments etc. */
2181  if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) {
2182  /* audio elementary streams are id3 timestamped */
2184  }
2185 
2186  if (c->first_timestamp == AV_NOPTS_VALUE &&
2187  pls->pkt->dts != AV_NOPTS_VALUE)
2188  c->first_timestamp = av_rescale_q(pls->pkt->dts,
2190  }
2191 
2192  if (pls->seek_timestamp == AV_NOPTS_VALUE)
2193  break;
2194 
2195  if (pls->seek_stream_index < 0 ||
2196  pls->seek_stream_index == pls->pkt->stream_index) {
2197 
2198  if (pls->pkt->dts == AV_NOPTS_VALUE) {
2200  break;
2201  }
2202 
2203  tb = get_timebase(pls);
2204  ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE,
2205  tb.den, AV_ROUND_DOWN) -
2206  pls->seek_timestamp;
2207  if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
2208  pls->pkt->flags & AV_PKT_FLAG_KEY)) {
2210  break;
2211  }
2212  }
2213  av_packet_unref(pls->pkt);
2214  }
2215  }
2216  /* Check if this stream has the packet with the lowest dts */
2217  if (pls->pkt->data) {
2218  struct playlist *minpls = minplaylist < 0 ?
2219  NULL : c->playlists[minplaylist];
2220  if (minplaylist < 0) {
2221  minplaylist = i;
2222  } else {
2223  int64_t dts = pls->pkt->dts;
2224  int64_t mindts = minpls->pkt->dts;
2225 
2226  if (dts == AV_NOPTS_VALUE ||
2227  (mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
2228  minplaylist = i;
2229  }
2230  }
2231  }
2232 
2233  /* If we got a packet, return it */
2234  if (minplaylist >= 0) {
2235  struct playlist *pls = c->playlists[minplaylist];
2236  AVStream *ist;
2237  AVStream *st;
2238 
2239  ret = update_streams_from_subdemuxer(s, pls);
2240  if (ret < 0) {
2241  av_packet_unref(pls->pkt);
2242  return ret;
2243  }
2244 
2245  // If sub-demuxer reports updated metadata, copy it to the first stream
2246  // and set its AVSTREAM_EVENT_FLAG_METADATA_UPDATED flag.
2248  if (pls->n_main_streams) {
2249  st = pls->main_streams[0];
2250  av_dict_copy(&st->metadata, pls->ctx->metadata, 0);
2252  }
2254  }
2255 
2256  /* check if noheader flag has been cleared by the subdemuxer */
2257  if (pls->has_noheader_flag && !(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER)) {
2258  pls->has_noheader_flag = 0;
2260  }
2261 
2262  if (pls->pkt->stream_index >= pls->n_main_streams) {
2263  av_log(s, AV_LOG_ERROR, "stream index inconsistency: index %d, %d main streams, %d subdemuxer streams\n",
2264  pls->pkt->stream_index, pls->n_main_streams, pls->ctx->nb_streams);
2265  av_packet_unref(pls->pkt);
2266  return AVERROR_BUG;
2267  }
2268 
2269  ist = pls->ctx->streams[pls->pkt->stream_index];
2270  st = pls->main_streams[pls->pkt->stream_index];
2271 
2272  av_packet_move_ref(pkt, pls->pkt);
2273  pkt->stream_index = st->index;
2274 
2275  if (pkt->dts != AV_NOPTS_VALUE)
2276  c->cur_timestamp = av_rescale_q(pkt->dts,
2277  ist->time_base,
2278  AV_TIME_BASE_Q);
2279 
2280  /* There may be more situations where this would be useful, but this at least
2281  * handles newly probed codecs properly (i.e. request_probe by mpegts). */
2282  if (ist->codecpar->codec_id != st->codecpar->codec_id) {
2283  ret = set_stream_info_from_input_stream(st, pls, ist);
2284  if (ret < 0) {
2285  return ret;
2286  }
2287  }
2288 
2289  return 0;
2290  }
2291  return AVERROR_EOF;
2292 }
2293 
2294 static int hls_read_seek(AVFormatContext *s, int stream_index,
2295  int64_t timestamp, int flags)
2296 {
2297  HLSContext *c = s->priv_data;
2298  struct playlist *seek_pls = NULL;
2299  int i, j;
2300  int stream_subdemuxer_index;
2301  int64_t first_timestamp, seek_timestamp, duration;
2302  int64_t seq_no;
2303 
2304  if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
2305  return AVERROR(ENOSYS);
2306 
2307  first_timestamp = c->first_timestamp == AV_NOPTS_VALUE ?
2308  0 : c->first_timestamp;
2309 
2311  s->streams[stream_index]->time_base.den,
2314 
2315  duration = s->duration == AV_NOPTS_VALUE ?
2316  0 : s->duration;
2317 
2318  if (0 < duration && duration < seek_timestamp - first_timestamp)
2319  return AVERROR(EIO);
2320 
2321  /* find the playlist with the specified stream */
2322  for (i = 0; i < c->n_playlists; i++) {
2323  struct playlist *pls = c->playlists[i];
2324  for (j = 0; j < pls->n_main_streams; j++) {
2325  if (pls->main_streams[j] == s->streams[stream_index]) {
2326  seek_pls = pls;
2327  stream_subdemuxer_index = j;
2328  break;
2329  }
2330  }
2331  }
2332  /* check if the timestamp is valid for the playlist with the
2333  * specified stream index */
2334  if (!seek_pls || !find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no))
2335  return AVERROR(EIO);
2336 
2337  /* set segment now so we do not need to search again below */
2338  seek_pls->cur_seq_no = seq_no;
2339  seek_pls->seek_stream_index = stream_subdemuxer_index;
2340 
2341  for (i = 0; i < c->n_playlists; i++) {
2342  /* Reset reading */
2343  struct playlist *pls = c->playlists[i];
2344  ff_format_io_close(pls->parent, &pls->input);
2345  pls->input_read_done = 0;
2346  ff_format_io_close(pls->parent, &pls->input_next);
2347  pls->input_next_requested = 0;
2348  av_packet_unref(pls->pkt);
2349  pls->pb.eof_reached = 0;
2350  /* Clear any buffered data */
2351  pls->pb.buf_end = pls->pb.buf_ptr = pls->pb.buffer;
2352  /* Reset the pos, to let the mpegts demuxer know we've seeked. */
2353  pls->pb.pos = 0;
2354  /* Flush the packet queue of the subdemuxer. */
2355  ff_read_frame_flush(pls->ctx);
2356 
2358  pls->seek_flags = flags;
2359 
2360  if (pls != seek_pls) {
2361  /* set closest segment seq_no for playlists not handled above */
2363  /* seek the playlist to the given position without taking
2364  * keyframes into account since this playlist does not have the
2365  * specified stream where we should look for the keyframes */
2366  pls->seek_stream_index = -1;
2367  pls->seek_flags |= AVSEEK_FLAG_ANY;
2368  }
2369  }
2370 
2371  c->cur_timestamp = seek_timestamp;
2372 
2373  return 0;
2374 }
2375 
2376 static int hls_probe(const AVProbeData *p)
2377 {
2378  /* Require #EXTM3U at the start, and either one of the ones below
2379  * somewhere for a proper match. */
2380  if (strncmp(p->buf, "#EXTM3U", 7))
2381  return 0;
2382 
2383  if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
2384  strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
2385  strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:"))
2386  return AVPROBE_SCORE_MAX;
2387  return 0;
2388 }
2389 
2390 #define OFFSET(x) offsetof(HLSContext, x)
2391 #define FLAGS AV_OPT_FLAG_DECODING_PARAM
2392 static const AVOption hls_options[] = {
2393  {"live_start_index", "segment index to start live streams at (negative values are from the end)",
2394  OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
2395  {"allowed_extensions", "List of file extensions that hls is allowed to access",
2396  OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
2397  {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,wav"},
2398  INT_MIN, INT_MAX, FLAGS},
2399  {"max_reload", "Maximum number of times a insufficient list is attempted to be reloaded",
2400  OFFSET(max_reload), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS},
2401  {"m3u8_hold_counters", "The maximum number of times to load m3u8 when it refreshes without new segments",
2402  OFFSET(m3u8_hold_counters), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS},
2403  {"http_persistent", "Use persistent HTTP connections",
2404  OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
2405  {"http_multiple", "Use multiple HTTP connections for fetching segments",
2406  OFFSET(http_multiple), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, FLAGS},
2407  {"http_seekable", "Use HTTP partial requests, 0 = disable, 1 = enable, -1 = auto",
2408  OFFSET(http_seekable), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, FLAGS},
2409  {NULL}
2410 };
2411 
2412 static const AVClass hls_class = {
2413  .class_name = "hls demuxer",
2414  .item_name = av_default_item_name,
2415  .option = hls_options,
2416  .version = LIBAVUTIL_VERSION_INT,
2417 };
2418 
2420  .name = "hls",
2421  .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
2422  .priv_class = &hls_class,
2423  .priv_data_size = sizeof(HLSContext),
2425  .read_probe = hls_probe,
2428  .read_close = hls_close,
2430 };
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)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:1606
Main libavformat public API header.
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1562
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:831
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1177
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:464
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2416
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:469
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2417
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning.
Definition: avformat.h:533
#define AVFMTCTX_UNSEEKABLE
signal that the stream is definitely not seekable, and attempts to call the seek function will fail.
Definition: avformat.h:1179
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition: avformat.h:830
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:832
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:999
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1371
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:818
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:661
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:470
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition: aviobuf.c:979
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:88
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
Same as ff_get_line but strip the white-space characters in the text tail.
Definition: aviobuf.c:806
#define AV_RB64
Definition: intreadwrite.h:164
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:72
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
Public dictionary API.
const OptionDef options[]
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:127
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:560
#define AV_OPT_ALLOW_NULL
In av_opt_get, return NULL if the option has a pointer type and is set to NULL, rather than returning...
Definition: opt.h:574
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:690
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
uint8_t * av_stream_new_side_data(AVStream *stream, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:4607
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4436
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
int av_probe_input_buffer(AVIOContext *pb, ff_const59 AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Like av_probe_input_buffer2() but returns 0 on success.
Definition: format.c:312
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1741
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3602
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4481
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:512
int avformat_queue_attached_pictures(AVFormatContext *s)
Definition: utils.c:454
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
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:74
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
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:69
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
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
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition: error.h:63
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare the remainders of two integer operands divided by a common divisor.
Definition: mathematics.c:160
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:82
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
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:502
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:478
AVMediaType
Definition: avutil.h:199
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
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:186
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
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
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:225
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:779
static void update_noheader_flag(AVFormatContext *s)
Definition: hls.c:1830
#define MAX_CHARACTERISTICS_LEN
Definition: hls.c:45
static void handle_variant_args(struct variant_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hls.c:357
#define MAX_FIELD_LEN
Definition: hls.c:44
static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in)
Definition: hls.c:1236
static int update_init_section(struct playlist *pls, struct segment *seg)
Definition: hls.c:1323
static struct segment * next_segment(struct playlist *pls)
Definition: hls.c:1014
static struct rendition * new_rendition(HLSContext *c, struct rendition_info *info, const char *url_base)
Definition: hls.c:469
static int read_from_url(struct playlist *pls, struct segment *seg, uint8_t *buf, int buf_size)
Definition: hls.c:1022
#define INITIAL_BUFFER_SIZE
Definition: hls.c:42
static void handle_init_section_args(struct init_section_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hls.c:445
static int hls_read_header(AVFormatContext *s)
Definition: hls.c:1865
static struct segment * current_segment(struct playlist *pls)
Definition: hls.c:1009
static int read_data(void *opaque, uint8_t *buf, int buf_size)
Definition: hls.c:1433
static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls, AVStream *ist)
Definition: hls.c:1772
static void free_segment_list(struct playlist *pls)
Definition: hls.c:228
#define MPEG_TIME_BASE
Definition: hls.c:47
static void handle_rendition_args(struct rendition_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hls.c:553
static void free_init_section_list(struct playlist *pls)
Definition: hls.c:235
static void add_renditions_to_variant(HLSContext *c, struct variant *var, enum AVMediaType type, const char *group_id)
Definition: hls.c:1593
static int hls_probe(const AVProbeData *p)
Definition: hls.c:2376
static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
Definition: hls.c:597
PlaylistType
Definition: hls.c:82
@ PLS_TYPE_UNSPECIFIED
Definition: hls.c:83
@ PLS_TYPE_VOD
Definition: hls.c:85
@ PLS_TYPE_EVENT
Definition: hls.c:84
static int recheck_discard_flags(AVFormatContext *s, int first)
Definition: hls.c:2076
static AVRational get_timebase(struct playlist *pls)
Definition: hls.c:2141
static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
Definition: hls.c:2116
static void parse_id3(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, int64_t *dts, ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
Definition: hls.c:1039
static void handle_key_args(struct key_info *info, const char *key, int key_len, char **dest, int *dest_len)
Definition: hls.c:381
static int64_t default_reload_interval(struct playlist *pls)
Definition: hls.c:1380
static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t timestamp, int64_t *seq_no)
Definition: hls.c:1649
static int open_url_keepalive(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **options)
Definition: hls.c:607
static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls, enum AVMediaType type)
Definition: hls.c:1617
static int update_streams_from_subdemuxer(AVFormatContext *s, struct playlist *pls)
Definition: hls.c:1805
#define FLAGS
Definition: hls.c:2391
static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
Definition: hls.c:1746
static int hls_close(AVFormatContext *s)
Definition: hls.c:1851
static struct playlist * new_playlist(HLSContext *c, const char *url, const char *base)
Definition: hls.c:297
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition: hls.c:1736
static void handle_id3(AVIOContext *pb, struct playlist *pls)
Definition: hls.c:1094
static void free_segment_dynarray(struct segment **segments, int n_segments)
Definition: hls.c:218
static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a, int64_t ts_b, struct playlist *pls_b)
Definition: hls.c:2149
static struct segment * new_init_section(struct playlist *pls, struct init_section_info *info, const char *url_base)
Definition: hls.c:401
static int parse_playlist(HLSContext *c, const char *url, struct playlist *pls, AVIOContext *in)
Definition: hls.c:712
#define MPEG_TIME_BASE_Q
Definition: hls.c:48
KeyType
Definition: hls.c:62
@ KEY_SAMPLE_AES
Definition: hls.c:65
@ KEY_NONE
Definition: hls.c:63
@ KEY_AES_128
Definition: hls.c:64
static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: hls.c:2158
static int save_avio_options(AVFormatContext *s)
Definition: hls.c:1714
static const AVOption hls_options[]
Definition: hls.c:2392
static void free_rendition_list(HLSContext *c)
Definition: hls.c:288
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
Definition: hls.c:625
#define OFFSET(x)
Definition: hls.c:2390
static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, ID3v2ExtraMetaAPIC *apic)
Definition: hls.c:1065
static void free_playlist_list(HLSContext *c)
Definition: hls.c:247
static const AVClass hls_class
Definition: hls.c:2412
static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
Definition: hls.c:1675
static struct variant * new_variant(HLSContext *c, struct variant_info *info, const char *url, const char *base)
Definition: hls.c:331
static void intercept_id3(struct playlist *pls, uint8_t *buf, int buf_size, int *len)
Definition: hls.c:1136
static int playlist_needed(struct playlist *pls)
Definition: hls.c:1387
static void free_variant_list(HLSContext *c)
Definition: hls.c:276
AVInputFormat ff_hls_demuxer
Definition: hls.c:2419
static int hls_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: hls.c:2294
int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
Send a new HTTP request, reusing the old connection.
Definition: http.c:396
cl_device_type type
const char * key
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:156
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:143
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1126
int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
Parse PRIV tags into a dictionary.
Definition: id3v2.c:1233
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition: id3v2.c:1142
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition: id3v2.c:1273
void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition: id3v2.c:1114
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
int i
Definition: input.c:407
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
void(* ff_parse_key_val_cb)(void *context, const char *key, int key_len, char **dest, int *dest_len)
Callback function type for ff_parse_key_value.
Definition: internal.h:511
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:160
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:4917
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:355
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Definition: utils.c:4896
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:4945
#define MAX_URL_SIZE
Definition: internal.h:30
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1892
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5692
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf, void *context)
Parse a string with comma-separated key=value pairs.
Definition: utils.c:4975
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
AVOptions.
#define tb
Definition: regdef.h:68
#define snprintf
Definition: snprintf.h:34
unsigned int pos
Definition: spdifenc.c:412
int size
Size of data in bytes.
Definition: buffer.h:97
uint8_t * data
The data buffer.
Definition: buffer.h:92
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
char * key
Definition: dict.h:82
char * value
Definition: dict.h:83
Format I/O context.
Definition: avformat.h:1232
int event_flags
Flags indicating events happening on the file, a combination of AVFMT_EVENT_FLAG_*.
Definition: avformat.h:1555
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1474
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1363
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:1408
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1281
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1512
void * priv_data
Format private data.
Definition: avformat.h:1260
int64_t probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1400
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
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:1828
Bytestream IO Context.
Definition: avio.h:161
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition: avio.h:229
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
int64_t pos
position in the file of the current buffer
Definition: avio.h:238
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
Callback for checking whether to abort blocking functions.
Definition: avio.h:58
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVOption.
Definition: opt.h:248
uint8_t * data
Definition: packet.h:307
enum AVPacketSideDataType type
Definition: packet.h:313
size_t size
Definition: packet.h:311
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1150
unsigned int nb_stream_indexes
Definition: avformat.h:1155
unsigned int * stream_index
Definition: avformat.h:1154
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:1153
AVDictionary * metadata
Definition: avformat.h:1156
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition: internal.h:200
Stream structure.
Definition: avformat.h:873
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:975
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:928
AVDictionary * metadata
Definition: avformat.h:937
int id
Format-specific stream ID.
Definition: avformat.h:880
int index
stream index in AVFormatContext
Definition: avformat.h:874
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1055
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:955
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:992
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:979
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1113
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
int n_renditions
Definition: hls.c:199
int http_multiple
Definition: hls.c:213
struct rendition ** renditions
Definition: hls.c:200
int m3u8_hold_counters
Definition: hls.c:203
AVIOInterruptCB * interrupt_callback
Definition: hls.c:208
AVDictionary * avio_opts
Definition: hls.c:209
int n_variants
Definition: hls.c:195
int http_persistent
Definition: hls.c:212
AVFormatContext * ctx
Definition: hls.c:194
int64_t cur_seq_no
Definition: hls.c:202
int live_start_index
Definition: hls.c:204
AVIOContext * playlist_pb
Definition: hls.c:215
int first_packet
Definition: hls.c:205
int max_reload
Definition: hls.c:211
int n_playlists
Definition: hls.c:197
int64_t cur_timestamp
Definition: hls.c:207
struct variant ** variants
Definition: hls.c:196
int64_t first_timestamp
Definition: hls.c:206
struct playlist ** playlists
Definition: hls.c:198
char * allowed_extensions
Definition: hls.c:210
int http_seekable
Definition: hls.c:214
AVBufferRef * buf
Definition: id3v2.h:66
uint8_t * data
Definition: id3v2.h:74
uint32_t datasize
Definition: id3v2.h:75
uint8_t * owner
Definition: id3v2.h:73
ID3v2ExtraMetaPRIV priv
Definition: id3v2.h:91
ID3v2ExtraMetaAPIC apic
Definition: id3v2.h:88
union ID3v2ExtraMeta::@258 data
const char * tag
Definition: id3v2.h:85
struct ID3v2ExtraMeta * next
Definition: id3v2.h:86
Definition: url.h:38
char uri[MAX_URL_SIZE]
Definition: hls.c:397
char byterange[32]
Definition: hls.c:398
Definition: hls.c:375
char iv[35]
Definition: hls.c:378
char uri[MAX_URL_SIZE]
Definition: hls.c:376
char method[11]
Definition: hls.c:377
Definition: graph2dot.c:48
Definition: hls.c:93
int finished
Definition: hls.c:112
int id3_changed
Definition: hls.c:145
int broken
Definition: hls.c:119
int64_t cur_seg_offset
Definition: hls.c:123
AVIOContext * input
Definition: hls.c:97
uint8_t * read_buffer
Definition: hls.c:96
int64_t cur_seq_no
Definition: hls.c:120
int n_renditions
Definition: hls.c:156
int64_t id3_mpegts_timestamp
Definition: hls.c:139
struct rendition ** renditions
Definition: hls.c:157
unsigned int init_sec_data_len
Definition: hls.c:130
int64_t id3_offset
Definition: hls.c:140
int64_t target_duration
Definition: hls.c:114
int64_t last_load_time
Definition: hls.c:124
int64_t last_seq_no
Definition: hls.c:121
int seek_stream_index
Definition: hls.c:150
int64_t start_seq_no
Definition: hls.c:115
unsigned int id3_buf_size
Definition: hls.c:142
AVStream ** main_streams
Definition: hls.c:109
AVIOContext * input_next
Definition: hls.c:99
unsigned int init_sec_buf_read_offset
Definition: hls.c:131
AVIOContext pb
Definition: hls.c:95
int64_t seek_timestamp
Definition: hls.c:148
AVPacket * pkt
Definition: hls.c:104
int input_read_done
Definition: hls.c:98
AVFormatContext * ctx
Definition: hls.c:103
uint8_t * init_sec_buf
Definition: hls.c:128
struct segment * cur_init_section
Definition: hls.c:127
uint8_t * id3_buf
Definition: hls.c:141
uint8_t key[16]
Definition: hls.c:134
int id3_found
Definition: hls.c:144
int index
Definition: hls.c:102
int m3u8_hold_counters
Definition: hls.c:122
int needed
Definition: hls.c:118
int has_noheader_flag
Definition: hls.c:105
char key_url[MAX_URL_SIZE]
Definition: hls.c:133
int n_init_sections
Definition: hls.c:161
struct segment ** segments
Definition: hls.c:117
int n_segments
Definition: hls.c:116
struct segment ** init_sections
Definition: hls.c:162
char url[MAX_URL_SIZE]
Definition: hls.c:94
AVFormatContext * parent
Definition: hls.c:101
int seek_flags
Definition: hls.c:149
int is_id3_timestamped
Definition: hls.c:138
unsigned int init_sec_buf_size
Definition: hls.c:129
int n_main_streams
Definition: hls.c:110
int input_next_requested
Definition: hls.c:100
ID3v2ExtraMeta * id3_deferred_extra
Definition: hls.c:146
AVDictionary * id3_initial
Definition: hls.c:143
enum PlaylistType type
Definition: hls.c:113
char type[16]
Definition: hls.c:458
char defaultr[4]
Definition: hls.c:464
char name[MAX_FIELD_LEN]
Definition: hls.c:463
char language[MAX_FIELD_LEN]
Definition: hls.c:461
char forced[4]
Definition: hls.c:465
char uri[MAX_URL_SIZE]
Definition: hls.c:459
char group_id[MAX_FIELD_LEN]
Definition: hls.c:460
char characteristics[MAX_CHARACTERISTICS_LEN]
Definition: hls.c:466
char assoc_language[MAX_FIELD_LEN]
Definition: hls.c:462
Definition: hls.c:171
char name[MAX_FIELD_LEN]
Definition: hls.c:176
enum AVMediaType type
Definition: hls.c:172
char group_id[MAX_FIELD_LEN]
Definition: hls.c:174
char language[MAX_FIELD_LEN]
Definition: hls.c:175
int disposition
Definition: hls.c:177
struct playlist * playlist
Definition: hls.c:173
Definition: hls.c:68
struct segment * init_section
Definition: hls.c:77
int64_t size
Definition: hls.c:71
uint8_t iv[16]
Definition: hls.c:75
char * url
Definition: hls.c:72
int64_t duration
Definition: hls.c:69
int64_t url_offset
Definition: hls.c:70
char * key
Definition: hls.c:73
enum KeyType key_type
Definition: hls.c:74
char bandwidth[20]
Definition: hls.c:324
char video[MAX_FIELD_LEN]
Definition: hls.c:327
char subtitles[MAX_FIELD_LEN]
Definition: hls.c:328
char audio[MAX_FIELD_LEN]
Definition: hls.c:326
Definition: hls.c:180
int bandwidth
Definition: hls.c:181
char audio_group[MAX_FIELD_LEN]
Definition: hls.c:187
int n_playlists
Definition: hls.c:184
char url[MAX_URL_SIZE]
Definition: hlsproto.c:55
struct playlist ** playlists
Definition: hls.c:185
char video_group[MAX_FIELD_LEN]
Definition: hls.c:188
char subtitles_group[MAX_FIELD_LEN]
Definition: hls.c:189
#define av_free(p)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
int64_t duration
Definition: movenc.c:64
AVPacket * pkt
Definition: movenc.c:59
AVDictionary * opts
Definition: movenc.c:50
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
int size
int 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:319
const char * r
Definition: vf_curves.c:116
static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int len
uint8_t base
Definition: vp3data.h:141
static double c[64]