FFmpeg
Loading...
Searching...
No Matches
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 * Copyright (c) 2021 Nachiket Tarate
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/**
25 * @file
26 * Apple HTTP Live Streaming demuxer
27 * https://www.rfc-editor.org/rfc/rfc8216.txt
28 */
29
30#include "config_components.h"
31
32#include "libavformat/http.h"
33#include "libavutil/aes.h"
34#include "libavutil/avstring.h"
35#include "libavutil/avassert.h"
38#include "libavutil/mem.h"
39#include "libavutil/opt.h"
40#include "libavutil/dict.h"
41#include "libavutil/time.h"
42#include "libavutil/tree.h"
43#include "avformat.h"
44#include "demux.h"
45#include "internal.h"
46#include "avio_internal.h"
47#include "id3v2.h"
48#include "url.h"
49
51
52#define INITIAL_BUFFER_SIZE 32768
53
54#define MAX_FIELD_LEN 64
55#define MAX_CHARACTERISTICS_LEN 512
56
57#define MPEG_TIME_BASE 90000
58#define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
59
60#define PACKED_AUDIO_FORMATS "aac,ac3,eac3,mp3"
61
62/*
63 * An apple http stream consists of a playlist with media segment files,
64 * played sequentially. There may be several playlists with the same
65 * video content, in different bandwidth variants, that are played in
66 * parallel (preferably only one bandwidth variant at a time). In this case,
67 * the user supplied the url to a main playlist that only lists the variant
68 * playlists.
69 *
70 * If the main playlist doesn't point at any variants, we still create
71 * one anonymous toplevel variant for this, to maintain the structure.
72 */
73
79
80struct segment {
84 char *url;
85 char *key;
87 uint8_t iv[16];
88 /* associated Media Initialization Section, treated as a segment */
90};
91
92struct rendition;
93struct rendition_group;
94struct variant;
95
101
102/*
103 * Each playlist has its own demuxer. If it currently is active,
104 * it has an open AVIOContext too, and potentially an AVPacket
105 * containing the next packet from this stream.
106 */
107struct playlist {
112 int64_t input_end; /* end of the byte range requested on input, or -1 */
117 int index;
121
122 /* main demuxer streams associated with this playlist
123 * indexed by the subdemuxer stream indexes */
126
143
144 /* Currently active Media Initialization Section */
146 uint8_t *init_sec_buf;
147 unsigned int init_sec_buf_size;
148 unsigned int init_sec_data_len;
150
152 uint8_t key[16];
153
154 /* ID3 timestamp handling (elementary audio streams have ID3 timestamps
155 * (and possibly other ID3 tags) in the beginning of each segment) */
156 int is_id3_timestamped; /* -1: not yet known */
157 int64_t id3_mpegts_timestamp; /* in mpegts tb */
158 int64_t id3_offset; /* in stream original tb */
159 uint8_t* id3_buf; /* temp buffer for id3 parsing */
160 unsigned int id3_buf_size;
161 AVDictionary *id3_initial; /* data from first id3 tag */
162 int id3_found; /* ID3 tag found at some point */
163 int id3_changed; /* ID3 tag data has changed at some point */
164 ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */
165
167
168 /* Constant offset of this playlist's DTS baseline relative to
169 * c->first_timestamp. */
173 int seek_stream_index; /* into subdemuxer stream array */
174
175 /* Renditions associated with this playlist, if any.
176 * Alternative rendition playlists have a single rendition associated
177 * with them, and variant main Media Playlists may have
178 * multiple (playlist-less) renditions associated with them. */
181
182 /* The last variant this playlist was added to and the last rendition
183 * group whose renditions were added to it. */
186
187 /* Media Initialization Sections (EXT-X-MAP) associated with this
188 * playlist, if any. */
191 int is_subtitle; /* Indicates if it's a subtitle playlist */
192
193 /* Some HLS streams repeat the current timed-ID3 metadata at every segment
194 * boundary so late-joining clients get a fresh copy. Track the last seen
195 * value to avoid raising AVSTREAM_EVENT_FLAG_METADATA_UPDATED spuriously
196 * on those duplicates. timed_id3_stream_index is the subdemuxer stream
197 * index of the first timed-ID3 stream seen; -1 if none. */
200};
201
202/*
203 * Renditions are e.g. alternative subtitle or audio streams.
204 * The rendition may either be an external playlist or it may be
205 * contained in the main Media Playlist of the variant (in which case
206 * playlist is NULL).
207 */
215
216struct variant {
218
219 /* every variant contains at least the main Media Playlist in index 0 */
222
226};
227
228/*
229 * The renditions with the same TYPE and GROUP-ID, which variants reference
230 * by the group id.
231 */
235
236 /* playlists of the external renditions */
239
240 /* renditions that are part of the variant main Media Playlists */
243
244 /* variants that reference the group */
247};
248
287
289{
290 int i;
291 for (i = 0; i < n_segments; i++) {
295 }
296}
297
298static void free_segment_list(struct playlist *pls)
299{
301 av_freep(&pls->segments);
302 pls->n_segments = 0;
303}
304
305static void free_init_section_list(struct playlist *pls)
306{
307 int i;
308 for (i = 0; i < pls->n_init_sections; i++) {
309 av_freep(&pls->init_sections[i]->key);
310 av_freep(&pls->init_sections[i]->url);
311 av_freep(&pls->init_sections[i]);
312 }
313 av_freep(&pls->init_sections);
314 pls->n_init_sections = 0;
315}
316
318{
319 int i;
320 for (i = 0; i < c->n_playlists; i++) {
321 struct playlist *pls = c->playlists[i];
324 av_freep(&pls->main_streams);
325 av_freep(&pls->renditions);
326 av_freep(&pls->id3_buf);
330 av_freep(&pls->init_sec_buf);
331 av_packet_free(&pls->pkt);
332 av_freep(&pls->pb.pub.buffer);
333 ff_format_io_close(c->ctx, &pls->input);
334 pls->input_read_done = 0;
335 ff_format_io_close(c->ctx, &pls->input_next);
336 pls->input_next_requested = 0;
337 if (pls->ctx) {
338 pls->ctx->pb = NULL;
340 }
341 av_free(pls);
342 }
343 av_freep(&c->playlists);
344 c->n_playlists = 0;
345 av_tree_destroy(c->playlists_by_url);
346 c->playlists_by_url = NULL;
347}
348
350{
351 int i;
352 for (i = 0; i < c->n_variants; i++) {
353 struct variant *var = c->variants[i];
354 av_freep(&var->playlists);
355 av_free(var);
356 }
357 av_freep(&c->variants);
358 c->n_variants = 0;
359}
360
362{
363 int i;
364 for (i = 0; i < c->n_renditions; i++)
365 av_freep(&c->renditions[i]);
366 av_freep(&c->renditions);
367 c->n_renditions = 0;
368 for (i = 0; i < c->n_rendition_groups; i++) {
369 av_freep(&c->rendition_groups[i]->playlists);
370 av_freep(&c->rendition_groups[i]->renditions);
371 av_freep(&c->rendition_groups[i]->variants);
372 av_freep(&c->rendition_groups[i]);
373 }
374 av_freep(&c->rendition_groups);
375 c->n_rendition_groups = 0;
376 av_tree_destroy(c->rendition_groups_by_id);
377 c->rendition_groups_by_id = NULL;
378}
379
380static int cmp_url_playlist(const void *key, const void *b)
381{
382 const struct playlist *pls = b;
383 return strcmp(key, pls->url);
384}
385
386static int cmp_playlist_url(const void *a, const void *b)
387{
388 const struct playlist *pa = a, *pb = b;
389 return strcmp(pa->url, pb->url);
390}
391
392static struct playlist *new_playlist(HLSContext *c, const char *url,
393 const char *base)
394{
395 struct playlist *pls;
396 struct AVTreeNode *node;
397 char abs_url[MAX_URL_SIZE];
398
399 ff_make_absolute_url(abs_url, sizeof(abs_url), base, url);
400 if (!abs_url[0])
401 return NULL;
402
403 /* the same URL in several variants or renditions shares one playlist */
404 pls = av_tree_find(c->playlists_by_url, abs_url, cmp_url_playlist, NULL);
405 if (pls)
406 return pls;
407
408 pls = av_mallocz(sizeof(struct playlist));
409 node = av_tree_node_alloc();
410 if (!pls || !node)
411 goto fail;
412
413 pls->pkt = av_packet_alloc();
414 if (!pls->pkt)
415 goto fail;
416
417 av_strlcpy(pls->url, abs_url, sizeof(pls->url));
420 pls->first_read_seq_no = -1;
421
422 pls->is_id3_timestamped = -1;
424 pls->timed_id3_stream_index = -1;
425
426 if (av_dynarray_add_nofree(&c->playlists, &c->n_playlists, pls) < 0)
427 goto fail;
428 av_tree_insert(&c->playlists_by_url, pls, cmp_playlist_url, &node);
429 return pls;
430
431fail:
432 if (pls)
433 av_packet_free(&pls->pkt);
434 av_free(pls);
435 av_free(node);
436 return NULL;
437}
438
440 char bandwidth[20];
441 /* variant group ids: */
445};
446
447static struct variant *new_variant(HLSContext *c, struct variant_info *info,
448 const char *url, const char *base)
449{
450 struct variant *var;
451 struct playlist *pls;
452
453 pls = new_playlist(c, url, base);
454 if (!pls)
455 return NULL;
456
457 var = av_mallocz(sizeof(struct variant));
458 if (!var)
459 return NULL;
460
461 if (info) {
462 var->bandwidth = atoi(info->bandwidth);
463 strcpy(var->audio_group, info->audio);
464 strcpy(var->video_group, info->video);
465 strcpy(var->subtitles_group, info->subtitles);
466 }
467
468 dynarray_add(&c->variants, &c->n_variants, var);
469 dynarray_add(&var->playlists, &var->n_playlists, pls);
470 return var;
471}
472
473static void handle_variant_args(void *context, const char *key,
474 int key_len, char **dest, int *dest_len)
475{
476 struct variant_info *info = context;
477 if (!strncmp(key, "BANDWIDTH=", key_len)) {
478 *dest = info->bandwidth;
479 *dest_len = sizeof(info->bandwidth);
480 } else if (!strncmp(key, "AUDIO=", key_len)) {
481 *dest = info->audio;
482 *dest_len = sizeof(info->audio);
483 } else if (!strncmp(key, "VIDEO=", key_len)) {
484 *dest = info->video;
485 *dest_len = sizeof(info->video);
486 } else if (!strncmp(key, "SUBTITLES=", key_len)) {
487 *dest = info->subtitles;
488 *dest_len = sizeof(info->subtitles);
489 }
490}
491
492struct key_info {
494 char method[11];
495 char iv[35];
496};
497
498static void handle_key_args(void *context, const char *key,
499 int key_len, char **dest, int *dest_len)
500{
501 struct key_info *info = context;
502 if (!strncmp(key, "METHOD=", key_len)) {
503 *dest = info->method;
504 *dest_len = sizeof(info->method);
505 } else if (!strncmp(key, "URI=", key_len)) {
506 *dest = info->uri;
507 *dest_len = sizeof(info->uri);
508 } else if (!strncmp(key, "IV=", key_len)) {
509 *dest = info->iv;
510 *dest_len = sizeof(info->iv);
511 }
512}
513
516 char byterange[32];
517};
518
519static struct segment *new_init_section(struct playlist *pls,
520 struct init_section_info *info,
521 const char *url_base)
522{
523 struct segment *sec;
524 char tmp_str[MAX_URL_SIZE], *ptr = tmp_str;
525
526 if (!info->uri[0])
527 return NULL;
528
529 sec = av_mallocz(sizeof(*sec));
530 if (!sec)
531 return NULL;
532
533 if (!av_strncasecmp(info->uri, "data:", 5)) {
534 ptr = info->uri;
535 } else {
536 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url_base, info->uri);
537 if (!tmp_str[0]) {
538 av_free(sec);
539 return NULL;
540 }
541 }
542 sec->url = av_strdup(ptr);
543 if (!sec->url) {
544 av_free(sec);
545 return NULL;
546 }
547
548 if (info->byterange[0]) {
549 sec->size = strtoll(info->byterange, NULL, 10);
550 ptr = strchr(info->byterange, '@');
551 if (ptr)
552 sec->url_offset = strtoll(ptr+1, NULL, 10);
553 if (sec->size < 0 || sec->url_offset < 0 || sec->size > INT64_MAX - sec->url_offset) {
554 av_freep(&sec->url);
555 av_free(sec);
556 return NULL;
557 }
558 } else {
559 /* the entire file is the init section */
560 sec->size = -1;
561 }
562
563 dynarray_add(&pls->init_sections, &pls->n_init_sections, sec);
564
565 return sec;
566}
567
568static void handle_init_section_args(void *context, const char *key,
569 int key_len, char **dest, int *dest_len)
570{
571 struct init_section_info *info = context;
572 if (!strncmp(key, "URI=", key_len)) {
573 *dest = info->uri;
574 *dest_len = sizeof(info->uri);
575 } else if (!strncmp(key, "BYTERANGE=", key_len)) {
576 *dest = info->byterange;
577 *dest_len = sizeof(info->byterange);
578 }
579}
580
592
593static int cmp_rendition_group(const void *a, const void *b)
594{
595 const struct rendition_group *ga = a, *gb = b;
596
597 if (ga->type != gb->type)
598 return FFDIFFSIGN(ga->type, gb->type);
599 return strcmp(ga->id, gb->id);
600}
601
603 enum AVMediaType type,
604 const char *id)
605{
606 struct rendition_group key = { .type = type };
607
608 av_strlcpy(key.id, id, sizeof(key.id));
609 return av_tree_find(c->rendition_groups_by_id, &key, cmp_rendition_group, NULL);
610}
611
613 enum AVMediaType type,
614 const char *id)
615{
616 struct rendition_group *group = find_rendition_group(c, type, id);
617 struct AVTreeNode *node;
618
619 if (group)
620 return group;
621
622 group = av_mallocz(sizeof(*group));
623 node = av_tree_node_alloc();
624 if (!group || !node ||
625 av_dynarray_add_nofree(&c->rendition_groups, &c->n_rendition_groups, group) < 0) {
626 av_free(group);
627 av_free(node);
628 return NULL;
629 }
630 group->type = type;
631 av_strlcpy(group->id, id, sizeof(group->id));
632 av_tree_insert(&c->rendition_groups_by_id, group, cmp_rendition_group, &node);
633 return group;
634}
635
636static int new_rendition(HLSContext *c, struct rendition_info *info,
637 const char *url_base)
638{
639 struct rendition_group *group;
640 struct rendition *rend;
642 char *characteristic;
643 char *chr_ptr;
644 char *saveptr;
645 int ret;
646
647 if (!strcmp(info->type, "AUDIO"))
649 else if (!strcmp(info->type, "VIDEO"))
651 else if (!strcmp(info->type, "SUBTITLES"))
653 else if (!strcmp(info->type, "CLOSED-CAPTIONS"))
654 /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in
655 * AVC SEI RBSP anyway */
656 return 0;
657
658 if (type == AVMEDIA_TYPE_UNKNOWN) {
659 av_log(c->ctx, AV_LOG_WARNING, "Can't support the type: %s\n", info->type);
660 return 0;
661 }
662
663 /* URI is mandatory for subtitles as per spec */
664 if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0]) {
665 av_log(c->ctx, AV_LOG_ERROR, "The URI tag is REQUIRED for subtitle.\n");
666 return 0;
667 }
668
669 group = new_rendition_group(c, type, info->group_id);
670 if (!group)
671 return AVERROR(ENOMEM);
672
673 rend = av_mallocz(sizeof(struct rendition));
674 if (!rend)
675 return AVERROR(ENOMEM);
676
677 ret = av_dynarray_add_nofree(&c->renditions, &c->n_renditions, rend);
678 if (ret < 0) {
679 av_free(rend);
680 return ret;
681 }
682
683 rend->type = type;
684 strcpy(rend->language, info->language);
685 strcpy(rend->name, info->name);
686
687 /* add the playlist if this is an external rendition */
688 if (info->uri[0]) {
689 rend->playlist = new_playlist(c, info->uri, url_base);
690 if (rend->playlist) {
692 rend->playlist->is_subtitle = 1;
693 rend->playlist->is_id3_timestamped = 0;
694 }
696 &rend->playlist->n_renditions, rend);
697 if (ret < 0)
698 return ret;
699 }
700 }
701 if (rend->playlist)
702 ret = av_dynarray_add_nofree(&group->playlists, &group->n_playlists, rend->playlist);
703 else
704 ret = av_dynarray_add_nofree(&group->renditions, &group->n_renditions, rend);
705 if (ret < 0)
706 return ret;
707
708 if (info->assoc_language[0]) {
709 size_t langlen = strlen(rend->language);
710 if (langlen < sizeof(rend->language) - 3) {
711 size_t assoc_len;
712 rend->language[langlen] = ',';
713 assoc_len = av_strlcpy(rend->language + langlen + 1,
714 info->assoc_language,
715 sizeof(rend->language) - langlen - 1);
716 if (langlen + assoc_len + 2 > sizeof(rend->language)) // truncation occurred
717 av_log(c->ctx, AV_LOG_WARNING, "Truncated rendition language: %s\n",
718 info->assoc_language);
719 }
720 }
721
722 if (!strcmp(info->defaultr, "YES"))
724 if (!strcmp(info->forced, "YES"))
726
727 chr_ptr = info->characteristics;
728 while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) {
729 if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound"))
731 else if (!strcmp(characteristic, "public.accessibility.describes-video"))
733
734 chr_ptr = NULL;
735 }
736
737 return 0;
738}
739
740static void handle_rendition_args(void *vinfo, const char *key,
741 int key_len, char **dest, int *dest_len)
742{
743 struct rendition_info *info = vinfo;
744
745 if (!strncmp(key, "TYPE=", key_len)) {
746 *dest = info->type;
747 *dest_len = sizeof(info->type);
748 } else if (!strncmp(key, "URI=", key_len)) {
749 *dest = info->uri;
750 *dest_len = sizeof(info->uri);
751 } else if (!strncmp(key, "GROUP-ID=", key_len)) {
752 *dest = info->group_id;
753 *dest_len = sizeof(info->group_id);
754 } else if (!strncmp(key, "LANGUAGE=", key_len)) {
755 *dest = info->language;
756 *dest_len = sizeof(info->language);
757 } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) {
758 *dest = info->assoc_language;
759 *dest_len = sizeof(info->assoc_language);
760 } else if (!strncmp(key, "NAME=", key_len)) {
761 *dest = info->name;
762 *dest_len = sizeof(info->name);
763 } else if (!strncmp(key, "DEFAULT=", key_len)) {
764 *dest = info->defaultr;
765 *dest_len = sizeof(info->defaultr);
766 } else if (!strncmp(key, "FORCED=", key_len)) {
767 *dest = info->forced;
768 *dest_len = sizeof(info->forced);
769 } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) {
770 *dest = info->characteristics;
771 *dest_len = sizeof(info->characteristics);
772 }
773 /*
774 * ignored:
775 * - AUTOSELECT: client may autoselect based on e.g. system language
776 * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4")
777 */
778}
779
780/* used by parse_playlist to allocate a new variant+playlist when the
781 * playlist is detected to be a Media Playlist (not Master Playlist)
782 * and we have no parent Master Playlist (parsing of which would have
783 * allocated the variant and playlist already)
784 * *pls == NULL => Master Playlist or parentless Media Playlist
785 * *pls != NULL => parented Media Playlist, playlist+variant allocated */
786static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
787{
788 if (*pls)
789 return 0;
790 struct variant *var = new_variant(c, NULL, url, NULL);
791 if (!var)
792 return AVERROR(ENOMEM);
793 *pls = var->playlists[0];
794 return 0;
795}
796
798{
799#if !CONFIG_HTTP_PROTOCOL
800 return 0;
801#else
803 return uc && !strncmp(uc->prot->name, "http", 4);
804#endif
805}
806
808 const char *url, AVDictionary **options)
809{
810#if !CONFIG_HTTP_PROTOCOL
812#else
813 int ret;
815 av_assert0(uc);
816 (*pb)->eof_reached = 0;
818 if (ret < 0) {
820 }
821 return ret;
822#endif
823}
824
825static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
826 AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
827{
828 HLSContext *c = s->priv_data;
830 const char *proto_name = NULL;
831 int ret;
832 int is_http = 0;
833
834 if (av_strstart(url, "crypto", NULL)) {
835 if (url[6] == '+' || url[6] == ':')
836 proto_name = avio_find_protocol_name(url + 7);
837 } else if (av_strstart(url, "data", NULL)) {
838 if (url[4] == '+' || url[4] == ':')
839 proto_name = avio_find_protocol_name(url + 5);
840 }
841
842 if (!proto_name)
843 proto_name = avio_find_protocol_name(url);
844
845 if (!proto_name)
846 return AVERROR_INVALIDDATA;
847
848 // only http(s) & file are allowed
849 if (av_strstart(proto_name, "file", NULL)) {
850 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
852 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
853 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
854 url);
855 return AVERROR_INVALIDDATA;
856 }
857 } else if (av_strstart(proto_name, "http", NULL)) {
858 is_http = 1;
859 } else if (av_strstart(proto_name, "data", NULL)) {
860 ;
861 } else
862 return AVERROR_INVALIDDATA;
863
864 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
865 ;
866 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
867 ;
868 else if (av_strstart(url, "data", NULL) && !strncmp(proto_name, url + 5, strlen(proto_name)) && url[5 + strlen(proto_name)] == ':')
869 ;
870 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
871 return AVERROR_INVALIDDATA;
872
873 av_dict_copy(&tmp, *opts, 0);
874 av_dict_copy(&tmp, opts2, 0);
875
876 if (is_http && c->http_persistent && is_native_http(*pb)) {
877 ret = open_url_keepalive(c->ctx, pb, url, &tmp);
878 if (ret == AVERROR_EXIT) {
880 return ret;
881 } else if (ret < 0) {
882 if (ret != AVERROR_EOF)
884 "keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n",
885 url, av_err2str(ret));
886 av_dict_copy(&tmp, *opts, 0);
887 av_dict_copy(&tmp, opts2, 0);
888 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
889 }
890 } else {
891 av_assert0(!*pb);
892 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
893 }
894 if (ret >= 0) {
895 // update cookies on http response with setcookies.
896 char *new_cookies = NULL;
897
898 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
899 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
900
901 if (new_cookies)
902 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
903 }
904
906
907 if (is_http_out)
908 *is_http_out = is_http;
909
910 return ret;
911}
912
913static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
914{
915 HLSContext *c = s->priv_data;
916 int matchA = 3;
917 int matchF = 0;
918
919 if (!c->extension_picky)
920 return 0;
921
922 if (strcmp(c->allowed_segment_extensions, "ALL"))
923 matchA = av_match_ext (seg->url, c->allowed_segment_extensions)
924 + 2*(ff_match_url_ext(seg->url, c->allowed_segment_extensions) > 0);
925
926 if (!matchA) {
927 av_log(s, AV_LOG_ERROR, "URL %s is not in allowed_segment_extensions, consider updating hls.c and submitting a patch to ffmpeg-devel, if this should be added\n", seg->url);
928 return AVERROR_INVALIDDATA;
929 }
930
931 if (in_fmt) {
932 if (in_fmt->extensions) {
933 matchF = av_match_ext( seg->url, in_fmt->extensions)
934 + 2*(ff_match_url_ext(seg->url, in_fmt->extensions) > 0);
935 // Youtube uses aac files with .ts extension
936 if(av_match_name("mp4", in_fmt->name) || av_match_name("aac", in_fmt->name)) {
937 matchF |= av_match_ext( seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa")
938 + 2*(ff_match_url_ext(seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa") > 0);
939 }
940 } else if (!strcmp(in_fmt->name, "mpegts")) {
941 const char *str = "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts"
942 ",html" // https://flash1.bogulus.cfd/
943 ;
944 matchF = av_match_ext( seg->url, str)
945 + 2*(ff_match_url_ext(seg->url, str) > 0);
946 } else if (!strcmp(in_fmt->name, "webvtt")) {
947 matchF = av_match_ext( seg->url, "vtt,webvtt")
948 + 2*(ff_match_url_ext(seg->url, "vtt,webvtt") > 0);
949 }
950
951 if (!(matchA & matchF)) {
952 av_log(s, AV_LOG_ERROR, "detected format %s extension %s mismatches allowed extensions in url %s\n", in_fmt->name, in_fmt->extensions ? in_fmt->extensions : "none", seg->url);
953 return AVERROR_INVALIDDATA;
954 }
955 }
956
957 return 0;
958}
959
960static int parse_playlist(HLSContext *c, const char *url,
961 struct playlist *pls, AVIOContext *in)
962{
963 int ret = 0, is_segment = 0, is_variant = 0;
964 int64_t duration = 0;
965 enum KeyType key_type = KEY_NONE;
966 uint8_t iv[16] = "";
967 int has_iv = 0;
968 char key[MAX_URL_SIZE] = "";
969 char line[MAX_URL_SIZE];
970 const char *ptr;
971 int close_in = 0;
972 int64_t seg_offset = 0;
973 int64_t seg_size = -1;
974 uint8_t *new_url = NULL;
976 char tmp_str[MAX_URL_SIZE];
977 struct segment *cur_init_section = NULL;
978 int is_http = av_strstart(url, "http", NULL);
979 struct segment **prev_segments = NULL;
980 int prev_n_segments = 0;
981 int64_t prev_start_seq_no = -1;
982 int64_t load_start = av_gettime_relative();
983 int64_t start_time_offset = 0;
984 int time_offset_flag = 0;
985 int finished = 0;
986
987 if (is_http && !in && c->http_persistent && c->playlist_pb) {
988 in = c->playlist_pb;
989 ret = open_url_keepalive(c->ctx, &c->playlist_pb, url, NULL);
990 if (ret == AVERROR_EXIT) {
991 return ret;
992 } else if (ret < 0) {
993 if (ret != AVERROR_EOF)
994 av_log(c->ctx, AV_LOG_WARNING,
995 "keepalive request failed for '%s' with error: '%s' when parsing playlist\n",
996 url, av_err2str(ret));
997 in = NULL;
998 }
999 }
1000
1001 if (!in) {
1003 av_dict_copy(&opts, c->avio_opts, 0);
1004
1005 if (c->http_persistent)
1006 av_dict_set(&opts, "multiple_requests", "1", 0);
1007
1008 ret = open_url(c->ctx, &in, url, &opts, NULL, NULL);
1010 if (ret < 0)
1011 return ret;
1012
1013 if (is_http && c->http_persistent && is_native_http(in))
1014 c->playlist_pb = in;
1015 else
1016 close_in = 1;
1017 }
1018
1019 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
1020 url = new_url;
1021
1022 ff_get_chomp_line(in, line, sizeof(line));
1023 if (strcmp(line, "#EXTM3U")) {
1025 goto fail;
1026 }
1027
1028 if (pls) {
1029 prev_start_seq_no = pls->start_seq_no;
1030 prev_segments = pls->segments;
1031 prev_n_segments = pls->n_segments;
1032 pls->segments = NULL;
1033 pls->n_segments = 0;
1034
1035 pls->finished = 0;
1037 }
1038 while (!avio_feof(in)) {
1039 ff_get_chomp_line(in, line, sizeof(line));
1040 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
1041 if (pls) {
1042 av_log(c->ctx, AV_LOG_ERROR,
1043 "Master Playlist tag found in a Media Playlist\n");
1045 goto fail;
1046 }
1047 is_variant = 1;
1048 memset(&variant_info, 0, sizeof(variant_info));
1050 } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
1051 struct key_info info = {{0}};
1053 key_type = KEY_NONE;
1054 has_iv = 0;
1055 if (!strcmp(info.method, "AES-128"))
1056 key_type = KEY_AES_128;
1057 if (!strcmp(info.method, "SAMPLE-AES"))
1058 key_type = KEY_SAMPLE_AES;
1059 if (!av_strncasecmp(info.iv, "0x", 2)) {
1060 ff_hex_to_data(iv, info.iv + 2);
1061 has_iv = 1;
1062 }
1063 av_strlcpy(key, info.uri, sizeof(key));
1064 } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
1065 struct rendition_info info = {{0}};
1066 if (pls) {
1067 av_log(c->ctx, AV_LOG_ERROR,
1068 "Master Playlist tag found in a Media Playlist\n");
1070 goto fail;
1071 }
1073 ret = new_rendition(c, &info, url);
1074 if (ret < 0)
1075 goto fail;
1076 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
1077 int64_t t;
1078 ret = ensure_playlist(c, &pls, url);
1079 if (ret < 0)
1080 goto fail;
1081 t = strtoll(ptr, NULL, 10);
1082 if (t < 0 || t >= INT64_MAX / AV_TIME_BASE) {
1084 goto fail;
1085 }
1086 pls->target_duration = t * AV_TIME_BASE;
1087 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
1088 uint64_t seq_no;
1089 ret = ensure_playlist(c, &pls, url);
1090 if (ret < 0)
1091 goto fail;
1092 seq_no = strtoull(ptr, NULL, 10);
1093 if (seq_no > INT64_MAX/2) {
1094 av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than "
1095 "INT64_MAX/2, mask out the highest bit\n");
1096 seq_no &= INT64_MAX/2;
1097 }
1098 pls->start_seq_no = seq_no;
1099 } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
1100 ret = ensure_playlist(c, &pls, url);
1101 if (ret < 0)
1102 goto fail;
1103 if (!strcmp(ptr, "EVENT"))
1104 pls->type = PLS_TYPE_EVENT;
1105 else if (!strcmp(ptr, "VOD"))
1106 pls->type = PLS_TYPE_VOD;
1107 } else if (av_strstart(line, "#EXT-X-MAP:", &ptr)) {
1108 struct init_section_info info = {{0}};
1109 ret = ensure_playlist(c, &pls, url);
1110 if (ret < 0)
1111 goto fail;
1113 cur_init_section = new_init_section(pls, &info, url);
1114 if (!cur_init_section) {
1115 ret = AVERROR(ENOMEM);
1116 goto fail;
1117 }
1118 cur_init_section->key_type = key_type;
1119 if (has_iv) {
1120 memcpy(cur_init_section->iv, iv, sizeof(iv));
1121 } else {
1122 int64_t seq = pls->start_seq_no + pls->n_segments;
1123 memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv));
1124 AV_WB64(cur_init_section->iv + 8, seq);
1125 }
1126
1127 if (key_type != KEY_NONE) {
1128 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
1129 if (!tmp_str[0]) {
1130 av_free(cur_init_section);
1132 goto fail;
1133 }
1134 cur_init_section->key = av_strdup(tmp_str);
1135 if (!cur_init_section->key) {
1136 av_free(cur_init_section);
1137 ret = AVERROR(ENOMEM);
1138 goto fail;
1139 }
1140 } else {
1141 cur_init_section->key = NULL;
1142 }
1143
1144 } else if (av_strstart(line, "#EXT-X-START:", &ptr)) {
1145 const char *time_offset_value = NULL;
1146 if (av_strstart(ptr, "TIME-OFFSET=", &time_offset_value)) {
1147 double offset = strtod(time_offset_value, NULL) * AV_TIME_BASE;
1148 if (offset >= -0x1p63 && offset < 0x1p63) {
1149 start_time_offset = offset;
1150 time_offset_flag = 1;
1151 } else {
1152 av_log(c->ctx, AV_LOG_WARNING, "TIME-OFFSET value is"
1153 "invalid, it will be ignored");
1154 }
1155 } else {
1156 av_log(c->ctx, AV_LOG_WARNING, "#EXT-X-START value is"
1157 "invalid, it will be ignored");
1158 continue;
1159 }
1160 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
1161 finished = 1;
1162 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
1163 double d = atof(ptr) * AV_TIME_BASE;
1164 if (d < 0 || d > INT64_MAX || isnan(d)) {
1165 av_log(c->ctx, AV_LOG_WARNING, "EXTINF %f unsupported\n", d / AV_TIME_BASE);
1166 d = 0;
1167 }
1168 duration = d;
1169 is_segment = 1;
1170 } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
1171 seg_size = strtoll(ptr, NULL, 10);
1172 ptr = strchr(ptr, '@');
1173 if (ptr)
1174 seg_offset = strtoll(ptr+1, NULL, 10);
1175 if (seg_size < 0 || seg_offset > INT64_MAX - seg_size) {
1177 goto fail;
1178 }
1179 } else if (av_strstart(line, "#", NULL)) {
1180 av_log(c->ctx, AV_LOG_VERBOSE, "Skip ('%s')\n", line);
1181 continue;
1182 } else if (line[0]) {
1183 if (is_variant) {
1184 if (!new_variant(c, &variant_info, line, url)) {
1185 ret = AVERROR(ENOMEM);
1186 goto fail;
1187 }
1188 is_variant = 0;
1189 }
1190 if (is_segment) {
1191 struct segment *seg;
1192 ret = ensure_playlist(c, &pls, url);
1193 if (ret < 0)
1194 goto fail;
1195 seg = av_malloc(sizeof(struct segment));
1196 if (!seg) {
1197 ret = AVERROR(ENOMEM);
1198 goto fail;
1199 }
1200 if (has_iv) {
1201 memcpy(seg->iv, iv, sizeof(iv));
1202 } else {
1203 uint64_t seq = pls->start_seq_no + (uint64_t)pls->n_segments;
1204 memset(seg->iv, 0, sizeof(seg->iv));
1205 AV_WB64(seg->iv + 8, seq);
1206 }
1207
1208 if (key_type != KEY_NONE) {
1209 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
1210 if (!tmp_str[0]) {
1212 av_free(seg);
1213 goto fail;
1214 }
1215 seg->key = av_strdup(tmp_str);
1216 if (!seg->key) {
1217 av_free(seg);
1218 ret = AVERROR(ENOMEM);
1219 goto fail;
1220 }
1221 } else {
1222 seg->key = NULL;
1223 }
1224
1225 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line);
1226 if (!tmp_str[0]) {
1228 if (seg->key)
1229 av_free(seg->key);
1230 av_free(seg);
1231 goto fail;
1232 }
1233 seg->url = av_strdup(tmp_str);
1234 if (!seg->url) {
1235 av_free(seg->key);
1236 av_free(seg);
1237 ret = AVERROR(ENOMEM);
1238 goto fail;
1239 }
1240
1241 ret = test_segment(c->ctx, pls->ctx ? pls->ctx->iformat : NULL, pls, seg);
1242 if (ret < 0) {
1243 av_free(seg->url);
1244 av_free(seg->key);
1245 av_free(seg);
1246 goto fail;
1247 }
1248
1249 if (duration < 0.001 * AV_TIME_BASE) {
1250 av_log(c->ctx, AV_LOG_WARNING, "Cannot get correct #EXTINF value of segment %s,"
1251 " set to default value to 1ms.\n", seg->url);
1252 duration = 0.001 * AV_TIME_BASE;
1253 }
1254 seg->duration = duration;
1255 seg->key_type = key_type;
1256 dynarray_add(&pls->segments, &pls->n_segments, seg);
1257 is_segment = 0;
1258
1259 seg->size = seg_size;
1260 if (seg_size >= 0) {
1261 seg->url_offset = seg_offset;
1262 seg_offset += seg_size;
1263 seg_size = -1;
1264 } else {
1265 seg->url_offset = 0;
1266 seg_offset = 0;
1267 }
1268
1269 seg->init_section = cur_init_section;
1270 }
1271 }
1272 }
1273 if (prev_segments) {
1274 if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE &&
1275 c->first_timestamp_pls == pls) {
1276 int64_t prev_timestamp = c->first_timestamp;
1277 int i;
1278 int64_t diff = pls->start_seq_no - prev_start_seq_no;
1279 for (i = 0; i < prev_n_segments && i < diff; i++) {
1280 c->first_timestamp += prev_segments[i]->duration;
1281 }
1282 av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")"
1283 " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n",
1284 prev_start_seq_no, pls->start_seq_no,
1285 prev_timestamp, c->first_timestamp);
1286 } else if (pls->start_seq_no < prev_start_seq_no) {
1287 av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n",
1288 prev_start_seq_no, pls->start_seq_no);
1289 }
1290 }
1291
1292 /* Media Playlist tags may precede the first tag that allocates the
1293 * playlist. */
1294 if (pls && finished)
1295 pls->finished = 1;
1296
1297 /* EXT-X-START is valid in both playlist types. A value from the Master
1298 * Playlist applies to all its Media Playlists, which then ignore their
1299 * own (RFC 8216 4.3.5). */
1300 if (time_offset_flag && !c->master_time_offset_flag) {
1301 if (pls) {
1302 pls->start_time_offset = start_time_offset;
1303 pls->time_offset_flag = 1;
1304 } else {
1305 c->master_time_offset_flag = 1;
1306 for (int i = 0; i < c->n_playlists; i++) {
1307 c->playlists[i]->start_time_offset = start_time_offset;
1308 c->playlists[i]->time_offset_flag = 1;
1309 }
1310 }
1311 }
1312
1313fail:
1314 free_segment_dynarray(prev_segments, prev_n_segments);
1315 av_freep(&prev_segments);
1316 if (pls)
1317 pls->last_load_time = load_start;
1318 av_free(new_url);
1319 if (close_in)
1320 ff_format_io_close(c->ctx, &in);
1321 c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE;
1322 if (!c->n_variants || !c->variants[0]->n_playlists ||
1323 !c->variants[0]->playlists[0]->n_segments)
1324 c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE;
1325
1326 if (c->n_variants && c->variants[0]->n_playlists &&
1327 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT &&
1328 !c->variants[0]->playlists[0]->finished) {
1329 struct playlist *p = c->variants[0]->playlists[0];
1330 int64_t duration = 0;
1331 for (int i = 0; i < p->n_segments; i++)
1332 duration += p->segments[i]->duration;
1333 c->ctx->duration = duration;
1334 }
1335
1336 return ret;
1337}
1338
1339static struct segment *current_segment(struct playlist *pls)
1340{
1341 int64_t n = pls->cur_seq_no - pls->start_seq_no;
1342 if (n >= pls->n_segments)
1343 return NULL;
1344 return pls->segments[n];
1345}
1346
1347static struct segment *next_segment(struct playlist *pls)
1348{
1349 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1350 if (n >= pls->n_segments)
1351 return NULL;
1352 return pls->segments[n];
1353}
1354
1355/* True if 'next' is an unencrypted byte range that directly follows 'cur' in
1356 * the same resource and ends within the range requested up to 'end', so that
1357 * it is read on from the input of 'cur' (i.e. consecutive EXT-X-BYTERANGE
1358 * segments). */
1359static int segment_continues(const struct segment *cur,
1360 const struct segment *next, int64_t end)
1361{
1362 return cur && next &&
1363 cur->size >= 0 && next->size >= 0 &&
1364 next->url_offset == cur->url_offset + cur->size &&
1365 next->url_offset + next->size <= end &&
1366 cur->key_type == KEY_NONE && next->key_type == KEY_NONE &&
1367 next->init_section == cur->init_section &&
1368 !strcmp(next->url, cur->url);
1369}
1370
1371static int64_t range_end(struct playlist *pls, const struct segment *seg)
1372{
1373 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1374
1375 if (seg == next_segment(pls))
1376 n++;
1377 else if (seg != current_segment(pls))
1378 return seg->url_offset + seg->size;
1379 for (; n < pls->n_segments &&
1380 segment_continues(seg, pls->segments[n], INT64_MAX); n++)
1381 seg = pls->segments[n];
1382 return seg->url_offset + seg->size;
1383}
1384
1385static int read_from_url(struct playlist *pls, struct segment *seg,
1386 uint8_t *buf, int buf_size)
1387{
1388 int ret;
1389
1390 /* limit read if the segment was only a part of a file */
1391 if (seg->size >= 0)
1392 buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
1393
1394 ret = avio_read(pls->input, buf, buf_size);
1395 if (ret > 0)
1396 pls->cur_seg_offset += ret;
1397
1398 return ret;
1399}
1400
1401/* Parse the raw ID3 data and pass contents to caller */
1403 AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info,
1404 ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
1405{
1406 static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp";
1407 static const char id3_priv_owner_audio_setup[] = "com.apple.streaming.audioDescription";
1408 ID3v2ExtraMeta *meta;
1409
1411 for (meta = *extra_meta; meta; meta = meta->next) {
1412 if (!strcmp(meta->tag, "PRIV")) {
1413 ID3v2ExtraMetaPRIV *priv = &meta->data.priv;
1414 if (priv->datasize == 8 && !av_strncasecmp(priv->owner, id3_priv_owner_ts, 44)) {
1415 /* 33-bit MPEG timestamp */
1416 int64_t ts = AV_RB64(priv->data);
1417 av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts);
1418 if ((ts & ~((1ULL << 33) - 1)) == 0)
1419 *dts = ts;
1420 else
1421 av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts);
1422 } else if (priv->datasize >= 8 && !av_strncasecmp(priv->owner, id3_priv_owner_audio_setup, 36)) {
1423 ff_hls_senc_read_audio_setup_info(audio_setup_info, priv->data, priv->datasize);
1424 }
1425 } else if (!strcmp(meta->tag, "APIC") && apic)
1426 *apic = &meta->data.apic;
1427 }
1428}
1429
1430/* Check if the ID3 metadata contents have changed */
1432 ID3v2ExtraMetaAPIC *apic)
1433{
1434 const AVDictionaryEntry *entry = NULL;
1435 const AVDictionaryEntry *oldentry;
1436 /* check that no keys have changed values */
1437 while ((entry = av_dict_iterate(metadata, entry))) {
1438 oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE);
1439 if (!oldentry || strcmp(oldentry->value, entry->value) != 0)
1440 return 1;
1441 }
1442
1443 /* check if apic appeared */
1444 if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
1445 return 1;
1446
1447 if (apic) {
1448 int size = pls->ctx->streams[1]->attached_pic.size;
1449 if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE)
1450 return 1;
1451
1452 if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
1453 return 1;
1454 }
1455
1456 return 0;
1457}
1458
1459/* Parse ID3 data and handle the found data */
1460static void handle_id3(AVIOContext *pb, struct playlist *pls)
1461{
1463 ID3v2ExtraMetaAPIC *apic = NULL;
1464 ID3v2ExtraMeta *extra_meta = NULL;
1465 int64_t timestamp = AV_NOPTS_VALUE;
1466
1467 parse_id3(pls->ctx, pb, &metadata, &timestamp, &pls->audio_setup_info, &apic, &extra_meta);
1468
1469 if (timestamp != AV_NOPTS_VALUE) {
1470 pls->id3_mpegts_timestamp = timestamp;
1471 pls->id3_offset = 0;
1472 }
1473
1474 if (!pls->id3_found) {
1475 /* initial ID3 tags */
1477 pls->id3_found = 1;
1478
1479 /* get picture attachment and set text metadata */
1480 if (pls->ctx->nb_streams) {
1482 ff_id3v2_parse_apic(pls->ctx, extra_meta);
1483 } else
1484 /* demuxer not yet opened, defer picture attachment */
1485 pls->id3_deferred_extra = extra_meta;
1486
1487 ff_id3v2_parse_priv_dict(&metadata, extra_meta);
1488 av_dict_copy(&pls->ctx->metadata, metadata, 0);
1489 pls->id3_initial = metadata;
1490
1491 } else {
1492 if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) {
1493 avpriv_report_missing_feature(pls->parent, "Changing ID3 metadata in HLS audio elementary stream");
1494 pls->id3_changed = 1;
1495 }
1497 }
1498
1499 if (pls->id3_deferred_extra != extra_meta)
1500 ff_id3v2_free_extra_meta(&extra_meta);
1501}
1502
1503static void intercept_id3(struct playlist *pls, uint8_t *buf,
1504 int buf_size, int *len)
1505{
1506 /* intercept id3 tags, we do not want to pass them to the raw
1507 * demuxer on all segment switches */
1508 int bytes;
1509 int id3_buf_pos = 0;
1510 int fill_buf = 0;
1511 struct segment *seg = current_segment(pls);
1512
1513 /* gather all the id3 tags */
1514 while (1) {
1515 /* see if we can retrieve enough data for ID3 header */
1517 bytes = read_from_url(pls, seg, buf + *len, ID3v2_HEADER_SIZE - *len);
1518 if (bytes > 0) {
1519
1520 if (bytes == ID3v2_HEADER_SIZE - *len)
1521 /* no EOF yet, so fill the caller buffer again after
1522 * we have stripped the ID3 tags */
1523 fill_buf = 1;
1524
1525 *len += bytes;
1526
1527 } else if (*len <= 0) {
1528 /* error/EOF */
1529 *len = bytes;
1530 fill_buf = 0;
1531 }
1532 }
1533
1534 if (*len < ID3v2_HEADER_SIZE)
1535 break;
1536
1538 int64_t maxsize = seg->size >= 0 ? seg->size : 1024*1024;
1539 int taglen = ff_id3v2_tag_len(buf);
1540 int tag_got_bytes = FFMIN(taglen, *len);
1541 int remaining = taglen - tag_got_bytes;
1542
1543 if (taglen > maxsize) {
1544 av_log(pls->parent, AV_LOG_ERROR, "Too large HLS ID3 tag (%d > %"PRId64" bytes)\n",
1545 taglen, maxsize);
1546 break;
1547 }
1548
1549 /*
1550 * Copy the id3 tag to our temporary id3 buffer.
1551 * We could read a small id3 tag directly without memcpy, but
1552 * we would still need to copy the large tags, and handling
1553 * both of those cases together with the possibility for multiple
1554 * tags would make the handling a bit complex.
1555 */
1556 pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen);
1557 if (!pls->id3_buf)
1558 break;
1559 memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes);
1560 id3_buf_pos += tag_got_bytes;
1561
1562 /* strip the intercepted bytes */
1563 *len -= tag_got_bytes;
1564 memmove(buf, buf + tag_got_bytes, *len);
1565 av_log(pls->parent, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes);
1566
1567 if (remaining > 0) {
1568 /* read the rest of the tag in */
1569 if (read_from_url(pls, seg, pls->id3_buf + id3_buf_pos, remaining) != remaining)
1570 break;
1571 id3_buf_pos += remaining;
1572 av_log(pls->parent, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining);
1573 }
1574
1575 } else {
1576 /* no more ID3 tags */
1577 break;
1578 }
1579 }
1580
1581 /* re-fill buffer for the caller unless EOF */
1582 if (*len >= 0 && (fill_buf || *len == 0)) {
1583 bytes = read_from_url(pls, seg, buf + *len, buf_size - *len);
1584
1585 /* ignore error if we already had some data */
1586 if (bytes >= 0)
1587 *len += bytes;
1588 else if (*len == 0)
1589 *len = bytes;
1590 }
1591
1592 if (pls->id3_buf) {
1593 /* Now parse all the ID3 tags */
1594 FFIOContext id3ioctx;
1595 ffio_init_read_context(&id3ioctx, pls->id3_buf, id3_buf_pos);
1596 handle_id3(&id3ioctx.pub, pls);
1597 }
1598
1599 if (pls->is_id3_timestamped == -1)
1601}
1602
1603static int read_key(HLSContext *c, struct playlist *pls, struct segment *seg)
1604{
1605 AVIOContext *pb = NULL;
1606
1607 int ret = open_url(pls->parent, &pb, seg->key, &c->avio_opts, NULL, NULL);
1608 if (ret < 0) {
1609 av_log(pls->parent, AV_LOG_ERROR, "Unable to open key file %s, %s\n",
1610 seg->key, av_err2str(ret));
1611 return ret;
1612 }
1613
1614 ret = avio_read(pb, pls->key, sizeof(pls->key));
1615 ff_format_io_close(pls->parent, &pb);
1616 if (ret != sizeof(pls->key)) {
1617 if (ret < 0) {
1618 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, %s\n",
1619 seg->key, av_err2str(ret));
1620 } else {
1621 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, read bytes %d != %zu\n",
1622 seg->key, ret, sizeof(pls->key));
1624 }
1625
1626 return ret;
1627 }
1628
1629 av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url));
1630
1631 return 0;
1632}
1633
1634static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg,
1635 AVIOContext **in, int64_t *end)
1636{
1638 int ret;
1639 int is_http = 0;
1640
1641 if (c->http_persistent)
1642 av_dict_set(&opts, "multiple_requests", "1", 0);
1643
1644 *end = -1;
1645 if (seg->size >= 0) {
1646 /* Restrict the request to the wanted byte range, extended so that one
1647 * request serves the whole run of segments that continue it. */
1648 *end = range_end(pls, seg);
1649 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1650 av_dict_set_int(&opts, "end_offset", *end, 0);
1651 }
1652
1653 av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
1654 seg->url, seg->url_offset, pls->index);
1655
1656 if (seg->key_type == KEY_AES_128 || seg->key_type == KEY_SAMPLE_AES) {
1657 if (strcmp(seg->key, pls->key_url)) {
1658 ret = read_key(c, pls, seg);
1659 if (ret < 0)
1660 goto cleanup;
1661 }
1662 }
1663
1664 if (seg->key_type == KEY_AES_128) {
1665 char iv[33], key[33], url[MAX_URL_SIZE];
1666 ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
1667 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
1668 if (strstr(seg->url, "://"))
1669 snprintf(url, sizeof(url), "crypto+%s", seg->url);
1670 else
1671 snprintf(url, sizeof(url), "crypto:%s", seg->url);
1672
1673 av_dict_set(&opts, "key", key, 0);
1674 av_dict_set(&opts, "iv", iv, 0);
1675
1676 ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http);
1677 if (ret < 0) {
1678 goto cleanup;
1679 }
1680 ret = 0;
1681 } else {
1682 ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http);
1683 }
1684
1685 /* Seek to the requested position. If this was a HTTP request, the offset
1686 * should already be where want it to, but this allows e.g. local testing
1687 * without a HTTP server.
1688 *
1689 * This is not done for HTTP at all as avio_seek() does internal bookkeeping
1690 * of file offset which is out-of-sync with the actual offset when "offset"
1691 * AVOption is used with http protocol, causing the seek to not be a no-op
1692 * as would be expected. Wrong offset received from the server will not be
1693 * noticed without the call, though.
1694 */
1695 if (ret == 0 && !is_http && seg->url_offset) {
1696 int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET);
1697 if (seekret < 0) {
1698 av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
1699 ret = seekret;
1700 ff_format_io_close(pls->parent, in);
1701 }
1702 }
1703
1704cleanup:
1706 pls->cur_seg_offset = 0;
1707 return ret;
1708}
1709
1710static int update_init_section(struct playlist *pls, struct segment *seg)
1711{
1712 static const int max_init_section_size = 1024*1024;
1713 HLSContext *c = pls->parent->priv_data;
1714 int64_t sec_size;
1715 int64_t urlsize;
1716 int ret;
1717
1718 if (seg->init_section == pls->cur_init_section)
1719 return 0;
1720
1721 pls->cur_init_section = NULL;
1722
1723 if (!seg->init_section)
1724 return 0;
1725
1726 ret = open_input(c, pls, seg->init_section, &pls->input, &pls->input_end);
1727 if (ret < 0) {
1729 "Failed to open an initialization section in playlist %d\n",
1730 pls->index);
1731 return ret;
1732 }
1733
1734 if (seg->init_section->size >= 0)
1735 sec_size = seg->init_section->size;
1736 else if ((urlsize = avio_size(pls->input)) >= 0)
1737 sec_size = urlsize;
1738 else
1739 sec_size = max_init_section_size;
1740
1742 "Downloading an initialization section of size %"PRId64"\n",
1743 sec_size);
1744
1745 sec_size = FFMIN(sec_size, max_init_section_size);
1746
1747 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1748
1749 ret = read_from_url(pls, seg->init_section, pls->init_sec_buf,
1750 pls->init_sec_buf_size);
1751 ff_format_io_close(pls->parent, &pls->input);
1752
1753 if (ret < 0)
1754 return ret;
1755
1756 pls->cur_init_section = seg->init_section;
1757 pls->init_sec_data_len = ret;
1758 pls->init_sec_buf_read_offset = 0;
1759
1760 /* spec says audio elementary streams do not have media initialization
1761 * sections, so there should be no ID3 timestamps */
1762 pls->is_id3_timestamped = 0;
1763
1764 return 0;
1765}
1766
1768{
1769 return pls->n_segments > 0 ?
1770 pls->segments[pls->n_segments - 1]->duration :
1771 pls->target_duration;
1772}
1773
1774static int playlist_needed(struct playlist *pls)
1775{
1776 AVFormatContext *s = pls->parent;
1777 int i, j;
1778 int stream_needed = 0;
1779 int first_st;
1780
1781 /* If there is no context or streams yet, the playlist is needed */
1782 if ((!pls->ctx || !pls->n_main_streams) && !pls->is_subtitle)
1783 return 1;
1784
1785 /* check if any of the streams in the playlist are needed */
1786 for (i = 0; i < pls->n_main_streams; i++) {
1787 if (pls->main_streams[i]->discard < AVDISCARD_ALL) {
1788 stream_needed = 1;
1789 break;
1790 }
1791 }
1792
1793 /* If all streams in the playlist were discarded, the playlist is not
1794 * needed (regardless of whether whole programs are discarded or not). */
1795 if (!stream_needed)
1796 return 0;
1797
1798 /* Otherwise, check if all the programs (variants) this playlist is in are
1799 * discarded. Since all streams in the playlist are part of the same programs
1800 * we can just check the programs of the first stream. */
1801
1802 first_st = pls->main_streams[0]->index;
1803
1804 for (i = 0; i < s->nb_programs; i++) {
1805 AVProgram *program = s->programs[i];
1806 if (program->discard < AVDISCARD_ALL) {
1807 for (j = 0; j < program->nb_stream_indexes; j++) {
1808 if (program->stream_index[j] == first_st) {
1809 /* playlist is in an undiscarded program */
1810 return 1;
1811 }
1812 }
1813 }
1814 }
1815
1816 /* some streams were not discarded but all the programs were */
1817 return 0;
1818}
1819
1820static int reload_playlist(struct playlist *v, HLSContext *c)
1821{
1822 int ret = 0;
1823 int reload_count = 0;
1824
1825 v->needed = playlist_needed(v);
1826
1827 if (!v->needed)
1828 return AVERROR_EOF;
1829
1830 if (!v->input || (c->http_persistent && v->input_read_done)) {
1831 int64_t reload_interval;
1832
1833 /* Check that the playlist is still needed before opening a new
1834 * segment. */
1835 v->needed = playlist_needed(v);
1836
1837 if (!v->needed) {
1838 av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d ('%s')\n",
1839 v->index, v->url);
1840 return AVERROR_EOF;
1841 }
1842
1843 /* If this is a live stream and the reload interval has elapsed since
1844 * the last playlist reload, reload the playlists now. */
1845 reload_interval = default_reload_interval(v);
1846
1847reload:
1848 reload_count++;
1849 if (reload_count > c->max_reload)
1850 return AVERROR_EOF;
1851 if (!v->finished &&
1852 av_gettime_relative() - v->last_load_time >= reload_interval) {
1853 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) {
1854 if (ret != AVERROR_EXIT)
1855 av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n",
1856 v->index);
1857 return ret;
1858 }
1859 /* If we need to reload the playlist again below (if
1860 * there's still no more segments), switch to a reload
1861 * interval of half the target duration. */
1862 reload_interval = v->target_duration / 2;
1863 }
1864 if (v->cur_seq_no < v->start_seq_no) {
1866 "skipping %"PRId64" segments ahead, expired from playlists\n",
1867 v->start_seq_no - v->cur_seq_no);
1868 v->cur_seq_no = v->start_seq_no;
1869 }
1870 if (v->cur_seq_no > v->last_seq_no) {
1871 v->last_seq_no = v->cur_seq_no;
1872 v->m3u8_hold_counters = 0;
1873 } else if (v->last_seq_no == v->cur_seq_no) {
1874 v->m3u8_hold_counters++;
1875 if (v->m3u8_hold_counters >= c->m3u8_hold_counters) {
1876 return AVERROR_EOF;
1877 }
1878 } else {
1879 av_log(v->parent, AV_LOG_WARNING, "The m3u8 list sequence may have been wrapped.\n");
1880 }
1881 if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
1882 if (v->finished || v->is_subtitle)
1883 return AVERROR_EOF;
1884 while (av_gettime_relative() - v->last_load_time < reload_interval) {
1885 if (ff_check_interrupt(c->interrupt_callback))
1886 return AVERROR_EXIT;
1887 av_usleep(100*1000);
1888 }
1889 /* Enough time has elapsed since the last reload */
1890 goto reload;
1891 }
1892
1893 }
1894 return ret;
1895}
1896
1897static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
1898{
1899 struct playlist *v = opaque;
1901 int ret;
1902 int just_opened = 0;
1903 int segment_retries = 0;
1904 struct segment *seg;
1905
1906 if (c->http_persistent && v->input_read_done) {
1907 av_assert0(v->input);
1908 ret = reload_playlist(v, c);
1909 if (ret < 0)
1910 return ret;
1911 }
1912
1913 v->input_read_done = 0;
1914
1915restart:
1916 ret = reload_playlist(v, c);
1917 if (ret < 0)
1918 return ret;
1919
1920 seg = current_segment(v);
1921
1922 if (!v->input || v->input_read_done) {
1923 /* load/update Media Initialization Section, if any */
1924 ret = update_init_section(v, seg);
1925 if (ret)
1926 return ret;
1927
1928 if (c->http_multiple == 1 && v->input_next_requested) {
1931 v->cur_seg_offset = 0;
1932 v->input_next_requested = 0;
1933 ret = 0;
1934 } else {
1935 ret = open_input(c, v, seg, &v->input, &v->input_end);
1936 }
1937 if (ret < 0) {
1938 if (ff_check_interrupt(c->interrupt_callback))
1939 return AVERROR_EXIT;
1940 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1941 v->cur_seq_no,
1942 v->index);
1943 if (segment_retries >= c->seg_max_retry) {
1944 av_log(v->parent, AV_LOG_WARNING, "Segment %"PRId64" of playlist %d failed too many times, skipping\n",
1945 v->cur_seq_no,
1946 v->index);
1947 v->cur_seq_no++;
1948 segment_retries = 0;
1949 } else {
1950 segment_retries++;
1951 }
1952 goto restart;
1953 }
1954 segment_retries = 0;
1955 just_opened = 1;
1956 v->input_read_done = 0;
1957 if (v->first_read_seq_no < 0)
1959 }
1960
1961 if (c->http_multiple == -1) {
1962 uint8_t *http_version_opt = NULL;
1963 int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt);
1964 if (r >= 0) {
1965 c->http_multiple = (!strncmp((const char *)http_version_opt, "1.1", 3) || !strncmp((const char *)http_version_opt, "2.0", 3));
1966 av_freep(&http_version_opt);
1967 }
1968 }
1969
1970 seg = next_segment(v);
1971 if (c->http_multiple == 1 && !v->input_next_requested &&
1972 seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
1974 ret = open_input(c, v, seg, &v->input_next, &v->input_next_end);
1975 if (ret < 0) {
1976 if (ff_check_interrupt(c->interrupt_callback))
1977 return AVERROR_EXIT;
1978 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1979 v->cur_seq_no + 1,
1980 v->index);
1981 } else {
1982 v->input_next_requested = 1;
1983 }
1984 }
1985
1987 /* Push init section out first before first actual segment */
1988 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1989 memcpy(buf, v->init_sec_buf + v->init_sec_buf_read_offset, copy_size);
1990 v->init_sec_buf_read_offset += copy_size;
1991 return copy_size;
1992 }
1993
1994 seg = current_segment(v);
1995 ret = read_from_url(v, seg, buf, buf_size);
1996 if (ret > 0) {
1997 if (just_opened && v->is_id3_timestamped != 0) {
1998 /* Intercept ID3 tags here, elementary audio streams are required
1999 * to convey timestamps using them in the beginning of each segment. */
2000 intercept_id3(v, buf, buf_size, &ret);
2001 }
2002
2003 return ret;
2004 }
2005 if (ret == 0 && segment_continues(seg, next_segment(v), v->input_end)) {
2006 /* The next segment follows within the requested range and is read
2007 * on from the same input. */
2008 v->cur_seq_no++;
2009 c->cur_seq_no = v->cur_seq_no;
2010 v->cur_seg_offset = 0;
2011 just_opened = 1;
2012 goto restart;
2013 }
2014 /* Keep the connection for the next request only if its reply was read
2015 * to the end. */
2016 if (c->http_persistent && is_native_http(v->input) &&
2017 seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
2018 (seg->size >= 0 ? ret == 0 && seg->url_offset + seg->size == v->input_end
2019 : ret == AVERROR_EOF)) {
2020 v->input_read_done = 1;
2021 } else {
2023 }
2024 v->cur_seq_no++;
2025
2026 c->cur_seq_no = v->cur_seq_no;
2027
2028 goto restart;
2029}
2030
2031static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
2032{
2033 struct playlist *v = opaque;
2035 int ret;
2036 struct segment *seg;
2037
2038 if (!v->needed || v->cur_seq_no - v->start_seq_no >= v->n_segments) {
2039 return AVERROR_EOF;
2040 } else {
2041 seg = current_segment(v);
2042 }
2043
2044 if (!v->input) {
2045 ret = open_input(c, v, seg, &v->input, &v->input_end);
2046 if (ret < 0) {
2047 if (ff_check_interrupt(c->interrupt_callback))
2048 return AVERROR_EXIT;
2049 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment of playlist %d\n",
2050 v->index);
2051 return ret;
2052 }
2053 if (v->first_read_seq_no < 0)
2055 }
2056
2057 return read_from_url(v, seg, buf, buf_size);
2058}
2059
2060static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
2061 int flags, AVDictionary **opts)
2062{
2064 "A HLS playlist item '%s' referred to an external file '%s'. "
2065 "Opening this file was forbidden for security reasons\n",
2066 s->url, url);
2067 return AVERROR(EPERM);
2068}
2069
2070static int init_subtitle_context(struct playlist *pls)
2071{
2072 HLSContext *c = pls->parent->priv_data;
2073 const AVInputFormat *in_fmt;
2075 uint8_t *buf;
2076 int ret;
2077
2078 if (!(pls->ctx = avformat_alloc_context()))
2079 return AVERROR(ENOMEM);
2080
2082 if (!buf) {
2084 pls->ctx = NULL;
2085 return AVERROR(ENOMEM);
2086 }
2087
2088 av_freep(&pls->pb.pub.buffer);
2089 ffio_init_context(&pls->pb, buf, INITIAL_BUFFER_SIZE, 0, pls,
2091 pls->pb.pub.seekable = 0;
2092 pls->ctx->pb = &pls->pb.pub;
2093 pls->ctx->io_open = nested_io_open;
2094
2095 ret = ff_copy_whiteblacklists(pls->ctx, pls->parent);
2096 if (ret < 0) {
2098 pls->ctx = NULL;
2099 return ret;
2100 }
2101
2102 in_fmt = av_find_input_format("webvtt");
2103 av_dict_copy(&opts, c->seg_format_opts, 0);
2104 ret = avformat_open_input(&pls->ctx, current_segment(pls)->url, in_fmt, &opts);
2106
2107 return ret;
2108}
2109
2111{
2113 int ret;
2114
2115restart:
2116 ret = reload_playlist(v, c);
2117 if (ret < 0)
2118 return ret;
2119
2120 if (v->input && !v->ctx)
2122
2123 if (!v->input && !v->ctx) {
2125 if (ret < 0)
2126 return ret;
2127 }
2128
2129 ret = av_read_frame(v->ctx, v->pkt);
2130 if (!ret) {
2131 return ret;
2132 }
2134 v->cur_seq_no++;
2135 c->cur_seq_no = v->cur_seq_no;
2136
2138
2139 goto restart;
2140}
2141
2143 enum AVMediaType type, const char *group_id)
2144{
2145 struct rendition_group *group = find_rendition_group(c, type, group_id);
2146 int ret;
2147
2148 if (!group)
2149 return 0;
2150
2151 ret = av_dynarray_add_nofree(&group->variants, &group->n_variants, var);
2152 if (ret < 0)
2153 return ret;
2154
2155 /* add the playlists of the external renditions to the variant */
2156 for (int i = 0; i < group->n_playlists; i++) {
2157 struct playlist *pls = group->playlists[i];
2158
2159 if (pls->last_variant != var) {
2160 ret = av_dynarray_add_nofree(&var->playlists, &var->n_playlists, pls);
2161 if (ret < 0)
2162 return ret;
2163 pls->last_variant = var;
2164 }
2165 }
2166 return 0;
2167}
2168
2169/* The renditions without a playlist are part of the main Media Playlist of
2170 * every variant that references their group. */
2172{
2173 for (int i = 0; i < c->n_rendition_groups; i++) {
2174 struct rendition_group *group = c->rendition_groups[i];
2175
2176 if (group->type != type || !group->n_renditions)
2177 continue;
2178
2179 for (int j = 0; j < group->n_variants; j++) {
2180 struct playlist *pls = group->variants[j]->playlists[0];
2181
2182 if (pls->last_group == group)
2183 continue;
2184
2185 for (int k = 0; k < group->n_renditions; k++) {
2187 group->renditions[k]);
2188 if (ret < 0)
2189 return ret;
2190 }
2191 pls->last_group = group;
2192 }
2193 }
2194 return 0;
2195}
2196
2198 enum AVMediaType type)
2199{
2200 int rend_idx = 0;
2201 int i;
2202
2203 for (i = 0; i < pls->n_main_streams; i++) {
2204 AVStream *st = pls->main_streams[i];
2205
2206 if (st->codecpar->codec_type != type)
2207 continue;
2208
2209 for (; rend_idx < pls->n_renditions; rend_idx++) {
2210 struct rendition *rend = pls->renditions[rend_idx];
2211
2212 if (rend->type != type)
2213 continue;
2214
2215 if (rend->language[0])
2216 av_dict_set(&st->metadata, "language", rend->language, 0);
2217 if (rend->name[0])
2218 av_dict_set(&st->metadata, "comment", rend->name, 0);
2219
2220 st->disposition |= rend->disposition;
2221 }
2222 if (rend_idx >=pls->n_renditions)
2223 break;
2224 }
2225}
2226
2227/* if timestamp was in valid range: returns 1 and sets seq_no
2228 * if not: returns 0 and sets seq_no to closest segment */
2230 int64_t timestamp, int64_t *seq_no,
2231 int64_t *seg_start_ts)
2232{
2233 int i;
2234 int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
2235 0 : c->first_timestamp;
2236
2237 if (timestamp < pos) {
2238 /* Seeking before the start of the playlist, clamp to the first segment. */
2239 *seq_no = pls->start_seq_no;
2240 if (seg_start_ts)
2241 *seg_start_ts = pos;
2242 return 1;
2243 }
2244
2245 for (i = 0; i < pls->n_segments; i++) {
2246 int64_t diff = pos + pls->segments[i]->duration - timestamp;
2247 if (diff > 0) {
2248 *seq_no = pls->start_seq_no + i;
2249 if (seg_start_ts) {
2250 *seg_start_ts = pos;
2251 }
2252 return 1;
2253 }
2254 pos += pls->segments[i]->duration;
2255 }
2256
2257 *seq_no = pls->start_seq_no + pls->n_segments - 1;
2258
2259 if (!pls->finished && pls->n_segments > 0) {
2260 if (seg_start_ts)
2261 *seg_start_ts = pos - pls->segments[pls->n_segments - 1]->duration;
2262 return 1;
2263 }
2264
2265 return 0;
2266}
2267
2269{
2270 int64_t seq_no;
2271
2272 if (!pls->finished && !c->first_packet &&
2274 /* reload the playlist since it was suspended */
2275 parse_playlist(c, pls->url, pls, NULL);
2276
2277 /* If playback is already in progress (we are just selecting a new
2278 * playlist) and this is a complete file, find the matching segment
2279 * by counting durations. */
2280 if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
2281 find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL);
2282 return seq_no;
2283 }
2284
2285 if (!pls->finished) {
2286 if (!c->first_packet && /* we are doing a segment selection during playback */
2287 c->cur_seq_no >= pls->start_seq_no &&
2288 c->cur_seq_no < pls->start_seq_no + pls->n_segments)
2289 /* While spec 3.4.3 says that we cannot assume anything about the
2290 * content at the same sequence number on different playlists,
2291 * in practice this seems to work and doing it otherwise would
2292 * require us to download a segment to inspect its timestamps. */
2293 return c->cur_seq_no;
2294
2295 /* If this is a live stream, start live_start_index segments from the
2296 * start or end */
2297 if (c->live_start_index < 0)
2298 seq_no = pls->start_seq_no + FFMAX(pls->n_segments +
2299 c->live_start_index, 0);
2300 else
2301 seq_no = pls->start_seq_no + FFMIN(c->live_start_index,
2302 pls->n_segments - 1);
2303
2304 /* If #EXT-X-START in playlist, need to recalculate */
2305 if (pls->time_offset_flag && c->prefer_x_start) {
2306 int64_t start_timestamp;
2307 int64_t playlist_duration = 0;
2308 int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 :
2309 c->cur_timestamp;
2310
2311 for (int i = 0; i < pls->n_segments; i++)
2312 playlist_duration += pls->segments[i]->duration;
2313
2314 /* If the absolute value of TIME-OFFSET exceeds
2315 * the duration of the playlist, it indicates either the end of the
2316 * playlist (if positive) or the beginning of the playlist (if
2317 * negative). */
2318 if (pls->start_time_offset >=0 &&
2319 pls->start_time_offset > playlist_duration)
2320 start_timestamp = cur_timestamp + playlist_duration;
2321 else if (pls->start_time_offset >= 0 &&
2322 pls->start_time_offset <= playlist_duration)
2323 start_timestamp = cur_timestamp + pls->start_time_offset;
2324 else if (pls->start_time_offset < 0 &&
2325 pls->start_time_offset < -playlist_duration)
2326 start_timestamp = cur_timestamp;
2327 else if (pls->start_time_offset < 0 &&
2328 pls->start_time_offset > -playlist_duration)
2329 start_timestamp = cur_timestamp + playlist_duration +
2330 pls->start_time_offset;
2331 else
2332 start_timestamp = cur_timestamp;
2333
2334 find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL);
2335 }
2336 return seq_no;
2337 }
2338
2339 /* Otherwise just start on the first segment. */
2340 return pls->start_seq_no;
2341}
2342
2343static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
2344{
2345 HLSContext *c = s->priv_data;
2346 int i, j;
2347 int bandwidth = -1;
2348
2349 for (i = 0; i < c->n_variants; i++) {
2350 struct variant *v = c->variants[i];
2351
2352 for (j = 0; j < v->n_playlists; j++) {
2353 if (v->playlists[j] != pls)
2354 continue;
2355
2357
2358 if (bandwidth == -1)
2359 bandwidth = v->bandwidth;
2360 else if (bandwidth != v->bandwidth)
2361 bandwidth = -2; /* stream in multiple variants with different bandwidths */
2362 }
2363 }
2364
2365 if (bandwidth >= 0)
2366 av_dict_set_int(&stream->metadata, "variant_bitrate", bandwidth, 0);
2367}
2368
2370{
2371 int err;
2372
2373 err = avcodec_parameters_copy(st->codecpar, ist->codecpar);
2374 if (err < 0)
2375 return err;
2376
2377 if (pls->is_id3_timestamped) /* custom timestamps via id3 */
2379 else
2381
2382 // add the dispositions of the subdemuxer stream to the rendition ones
2383 st->disposition |= ist->disposition;
2384
2385 av_dict_copy(&st->metadata, ist->metadata, 0);
2386
2388
2389 return 0;
2390}
2391
2392/* add new subdemuxer streams to our context, if any */
2394{
2395 HLSContext *c = s->priv_data;
2396 int err;
2397
2398 while (pls->n_main_streams < pls->ctx->nb_streams) {
2399 int ist_idx = pls->n_main_streams;
2401 AVStream *ist = pls->ctx->streams[ist_idx];
2402
2403 if (!st)
2404 return AVERROR(ENOMEM);
2405
2406 st->id = pls->index;
2407 dynarray_add(&pls->main_streams, &pls->n_main_streams, st);
2408
2409 add_stream_to_programs(s, pls, st);
2410
2411 err = set_stream_info_from_input_stream(st, pls, ist);
2412 if (err < 0)
2413 return err;
2414
2415 /* Match the start_time of streams created before playback began. */
2416 if (c->first_timestamp != AV_NOPTS_VALUE)
2417 st->start_time = av_rescale_q(c->first_timestamp,
2419
2421 if (pls->timed_id3_stream_index < 0)
2422 pls->timed_id3_stream_index = ist_idx;
2423 else
2425 "Playlist has multiple timed ID3 streams; "
2426 "only stream %d will be used for metadata propagation\n",
2428 }
2429 }
2430
2431 return 0;
2432}
2433
2435{
2436 HLSContext *c = s->priv_data;
2437 int flag_needed = 0;
2438 int i;
2439
2440 for (i = 0; i < c->n_playlists; i++) {
2441 struct playlist *pls = c->playlists[i];
2442
2443 if (pls->has_noheader_flag) {
2444 flag_needed = 1;
2445 break;
2446 }
2447 }
2448
2449 if (flag_needed)
2450 s->ctx_flags |= AVFMTCTX_NOHEADER;
2451 else
2452 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
2453}
2454
2456{
2457 HLSContext *c = s->priv_data;
2458
2462
2463 if (c->crypto_ctx.aes_ctx)
2464 av_free(c->crypto_ctx.aes_ctx);
2465
2466 av_dict_free(&c->avio_opts);
2467 ff_format_io_close(c->ctx, &c->playlist_pb);
2468
2469 return 0;
2470}
2471
2473{
2474 HLSContext *c = s->priv_data;
2475 int ret = 0, i;
2476 int64_t highest_cur_seq_no = 0;
2477
2478 c->ctx = s;
2479 c->interrupt_callback = &s->interrupt_callback;
2480
2481 c->first_packet = 1;
2482 c->first_timestamp = AV_NOPTS_VALUE;
2483 c->first_timestamp_pls = NULL;
2484 c->first_timestamp_locked = 0;
2485 c->cur_timestamp = AV_NOPTS_VALUE;
2486
2487 if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0)
2488 return ret;
2489
2490 /* http_persistent and http_multiple auto-detection both rely on the
2491 * AVIOContext being backed by the builtin URLContext. Neither works
2492 * when io_open is overridden with a custom callback. */
2493 if (!ffio_geturlcontext(s->pb)) {
2494 if (c->http_persistent) {
2495 av_log(s, AV_LOG_WARNING, "Disabling http_persistent due to custom io_open.\n");
2496 c->http_persistent = 0;
2497 }
2498 /* Only auto-detection is disabled, enabling http_multiple can still work
2499 * with custom io_open. */
2500 if (c->http_multiple == -1) {
2501 av_log(s, AV_LOG_WARNING, "Disabling http_multiple due to custom io_open.\n");
2502 c->http_multiple = 0;
2503 }
2504 }
2505
2506 /* XXX: Some HLS servers don't like being sent the range header,
2507 in this case, we need to set http_seekable = 0 to disable
2508 the range header */
2509 av_dict_set_int(&c->avio_opts, "seekable", c->http_seekable, 0);
2510
2511 if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
2512 return ret;
2513
2514 if (c->n_variants == 0) {
2515 av_log(s, AV_LOG_WARNING, "Empty playlist\n");
2516 return AVERROR_EOF;
2517 }
2518 /* If the playlist only contained playlists (Master Playlist),
2519 * parse each individual playlist. */
2520 if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
2521 for (i = 0; i < c->n_playlists; i++) {
2522 struct playlist *pls = c->playlists[i];
2523 pls->m3u8_hold_counters = 0;
2524 if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0) {
2525 av_log(s, AV_LOG_WARNING, "parse_playlist error %s [%s]\n", av_err2str(ret), pls->url);
2526 pls->broken = 1;
2527 if (c->n_playlists > 1)
2528 continue;
2529 return ret;
2530 }
2531 }
2532 }
2533
2534 for (i = 0; i < c->n_variants; i++) {
2535 if (c->variants[i]->playlists[0]->n_segments == 0) {
2536 av_log(s, AV_LOG_WARNING, "Empty segment [%s]\n", c->variants[i]->playlists[0]->url);
2537 c->variants[i]->playlists[0]->broken = 1;
2538 }
2539 }
2540
2541 /* Calculate the total duration of the stream if all segments are
2542 * available (finished or EVENT playlists). */
2543 if (c->variants[0]->playlists[0]->finished ||
2544 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT) {
2545 int64_t duration = 0;
2546 for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
2547 duration += c->variants[0]->playlists[0]->segments[i]->duration;
2548 s->duration = duration;
2549 }
2550
2551 /* Associate renditions with variants */
2552 for (i = 0; i < c->n_variants; i++) {
2553 struct variant *var = c->variants[i];
2554
2555 if (var->audio_group[0] &&
2557 return ret;
2558 if (var->video_group[0] &&
2560 return ret;
2561 if (var->subtitles_group[0] &&
2563 return ret;
2564 }
2567 return ret;
2568
2569 /* Create a program for each variant */
2570 for (i = 0; i < c->n_variants; i++) {
2571 struct variant *v = c->variants[i];
2572 AVProgram *program;
2573
2574 program = av_new_program(s, i);
2575 if (!program)
2576 return AVERROR(ENOMEM);
2577 av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
2578 }
2579
2580 /* Select the starting segments */
2581 for (i = 0; i < c->n_playlists; i++) {
2582 struct playlist *pls = c->playlists[i];
2583
2584 if (pls->n_segments == 0)
2585 continue;
2586
2587 pls->cur_seq_no = select_cur_seq_no(c, pls);
2588 highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no);
2589 }
2590
2591 av_dict_set(&c->seg_format_opts, "prefer_hls_mpegts_pts", "1", 0);
2592
2593 /* Open the demuxer for each playlist */
2594 for (i = 0; i < c->n_playlists; i++) {
2595 struct playlist *pls = c->playlists[i];
2596 const AVInputFormat *in_fmt = NULL;
2597 char *url;
2599 struct segment *seg = NULL;
2600 uint8_t *buf;
2601 int buf_size;
2602
2603 if (!(pls->ctx = avformat_alloc_context()))
2604 return AVERROR(ENOMEM);
2605
2606 if (pls->n_segments == 0)
2607 continue;
2608
2609 pls->index = i;
2610 pls->needed = 1;
2611 pls->parent = s;
2612
2613 /*
2614 * If this is a live stream and this playlist looks like it is one segment
2615 * behind, try to sync it up so that every substream starts at the same
2616 * time position (so e.g. avformat_find_stream_info() will see packets from
2617 * all active streams within the first few seconds). This is not very generic,
2618 * though, as the sequence numbers are technically independent.
2619 */
2620 if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 &&
2621 highest_cur_seq_no < pls->start_seq_no + pls->n_segments) {
2622 pls->cur_seq_no = highest_cur_seq_no;
2623 }
2624
2625 if (pls->is_subtitle) {
2626 buf_size = strlen("WEBVTT\n");
2627 buf = av_memdup("WEBVTT\n", buf_size);
2628 } else {
2629 buf_size = INITIAL_BUFFER_SIZE;
2630 buf = av_malloc(buf_size);
2631 }
2632 if (!buf) {
2634 pls->ctx = NULL;
2635 return AVERROR(ENOMEM);
2636 }
2637
2638 ffio_init_context(&pls->pb, buf, buf_size, 0, pls,
2640
2641 /*
2642 * If encryption scheme is SAMPLE-AES, try to read ID3 tags of
2643 * external audio track that contains audio setup information
2644 */
2645 seg = current_segment(pls);
2646 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->n_renditions > 0 &&
2647 pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO) {
2648 uint8_t buf[HLS_MAX_ID3_TAGS_DATA_LEN];
2649 if ((ret = avio_read(&pls->pb.pub, buf, HLS_MAX_ID3_TAGS_DATA_LEN)) < 0) {
2650 /* Fail if error was not end of file */
2651 if (ret != AVERROR_EOF) {
2653 pls->ctx = NULL;
2654 return ret;
2655 }
2656 }
2657 ret = 0;
2658 /* Reset reading */
2659 ff_format_io_close(pls->parent, &pls->input);
2660 pls->input = NULL;
2661 pls->input_read_done = 0;
2663 pls->input_next = NULL;
2664 pls->input_next_requested = 0;
2665 pls->cur_seg_offset = 0;
2666 pls->cur_init_section = NULL;
2667 /* Reset EOF flag */
2668 pls->pb.pub.eof_reached = 0;
2669 /* Clear any buffered data */
2670 pls->pb.pub.buf_end = pls->pb.pub.buf_ptr = pls->pb.pub.buffer;
2671 /* Reset the position */
2672 pls->pb.pub.pos = 0;
2673 }
2674
2675 /*
2676 * If encryption scheme is SAMPLE-AES and audio setup information is present in external audio track,
2677 * use that information to find the media format, otherwise probe input data
2678 */
2679 seg = current_segment(pls);
2680 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->is_id3_timestamped &&
2685 // Keep this list in sync with ff_hls_senc_read_audio_setup_info()
2687 pls->audio_setup_info.codec_id == AV_CODEC_ID_AC3 ? "ac3" : "eac3");
2688 } else {
2689 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
2690 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
2691 pls->ctx->interrupt_callback = s->interrupt_callback;
2692 url = av_strdup(pls->segments[0]->url);
2693 ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, url, NULL, 0, 0);
2694
2695 for (int n = 0; n < pls->n_segments; n++)
2696 if (ret >= 0)
2697 ret = test_segment(s, in_fmt, pls, pls->segments[n]);
2698
2699 if (ret < 0) {
2700 /* Free the ctx - it isn't initialized properly at this point,
2701 * so avformat_close_input shouldn't be called. If
2702 * avformat_open_input fails below, it frees and zeros the
2703 * context, so it doesn't need any special treatment like this. */
2704 av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url);
2706 pls->ctx = NULL;
2707 av_free(url);
2708 return ret;
2709 }
2710 av_free(url);
2711 }
2712
2713 seg = current_segment(pls);
2714 if (seg && seg->key_type == KEY_SAMPLE_AES) {
2715 if (strstr(in_fmt->name, "mov")) {
2716 char key[33];
2717 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
2718 av_dict_set(&options, "decryption_key", key, 0);
2719 } else if (!c->crypto_ctx.aes_ctx) {
2720 c->crypto_ctx.aes_ctx = av_aes_alloc();
2721 if (!c->crypto_ctx.aes_ctx) {
2723 pls->ctx = NULL;
2724 return AVERROR(ENOMEM);
2725 }
2726 }
2727 }
2728
2729 pls->ctx->pb = &pls->pb.pub;
2730 pls->ctx->io_open = nested_io_open;
2731 pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
2732
2733 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
2734 return ret;
2735
2736 av_dict_copy(&options, c->seg_format_opts, 0);
2737
2738 ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &options);
2740 if (ret < 0)
2741 return ret;
2742
2743 if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) {
2749 }
2750
2751 if (pls->is_id3_timestamped == -1)
2752 av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n");
2753
2754 /*
2755 * For ID3 timestamped raw audio streams we need to detect the packet
2756 * durations to calculate timestamps in fill_timing_for_id3_timestamped_stream(),
2757 * but for other streams we can rely on our user calling avformat_find_stream_info()
2758 * on us if they want to.
2759 */
2760 if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) {
2761 seg = current_segment(pls);
2762 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->audio_setup_info.setup_data_length > 0 &&
2763 pls->ctx->nb_streams == 1)
2765 else
2767
2768 if (ret < 0)
2769 return ret;
2770 }
2771
2773
2774 /* Create new AVStreams for each stream in this playlist */
2776 if (ret < 0)
2777 return ret;
2778
2779 /*
2780 * Copy any metadata from playlist to main streams, but do not set
2781 * event flags.
2782 */
2783 if (pls->n_main_streams)
2784 av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0);
2785
2786 if (pls->is_subtitle) {
2788 pls->ctx = NULL;
2789 pls->needed = 0;
2791 }
2792
2796 }
2797
2799
2800 return 0;
2801}
2802
2804{
2805 HLSContext *c = s->priv_data;
2806 int i, changed = 0;
2807 int cur_needed;
2808
2809 /* Check if any new streams are needed */
2810 for (i = 0; i < c->n_playlists; i++) {
2811 struct playlist *pls = c->playlists[i];
2812
2813 cur_needed = playlist_needed(c->playlists[i]);
2814
2815 if (pls->broken) {
2816 continue;
2817 }
2818 if (cur_needed && !pls->needed) {
2819 pls->needed = 1;
2820 changed = 1;
2821 pls->cur_seq_no = select_cur_seq_no(c, pls);
2822 pls->first_read_seq_no = -1;
2823 pls->pb.pub.eof_reached = 0;
2824 if (c->cur_timestamp != AV_NOPTS_VALUE) {
2825 /* catch up */
2826 pls->seek_timestamp = av_sat_add64(c->cur_timestamp,
2827 pls->ts_offset != AV_NOPTS_VALUE ? pls->ts_offset : 0);
2829 pls->seek_stream_index = -1;
2830 }
2831 av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no);
2832 } else if (first && !cur_needed && pls->needed) {
2833 ff_format_io_close(pls->parent, &pls->input);
2834 pls->input_read_done = 0;
2836 pls->input_next_requested = 0;
2837 if (pls->is_subtitle)
2839 pls->needed = 0;
2840 changed = 1;
2841 av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
2842 }
2843 }
2844 return changed;
2845}
2846
2848{
2849 if (pls->id3_offset >= 0) {
2852 pls->ctx->streams[pls->pkt->stream_index]->time_base,
2854 if (pls->pkt->duration)
2855 pls->id3_offset = av_sat_add64(pls->id3_offset, pls->pkt->duration);
2856 else
2857 pls->id3_offset = -1;
2858 } else {
2859 /* there have been packets with unknown duration
2860 * since the last id3 tag, should not normally happen */
2861 pls->pkt->dts = AV_NOPTS_VALUE;
2862 }
2863
2864 if (pls->pkt->duration)
2865 pls->pkt->duration = av_rescale_q(pls->pkt->duration,
2866 pls->ctx->streams[pls->pkt->stream_index]->time_base,
2868
2869 pls->pkt->pts = AV_NOPTS_VALUE;
2870}
2871
2873{
2874 if (pls->is_id3_timestamped)
2875 return MPEG_TIME_BASE_Q;
2876
2877 return pls->ctx->streams[pls->pkt->stream_index]->time_base;
2878}
2879
2880static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a,
2881 int64_t ts_b, struct playlist *pls_b)
2882{
2883 int64_t scaled_ts_a = av_rescale_q(ts_a, get_timebase(pls_a), MPEG_TIME_BASE_Q);
2884 int64_t scaled_ts_b = av_rescale_q(ts_b, get_timebase(pls_b), MPEG_TIME_BASE_Q);
2885
2886 return av_compare_mod(scaled_ts_a, scaled_ts_b, 1LL << 33);
2887}
2888
2889/**
2890 * Check whether any entry in @p update differs from the corresponding entry
2891 * in @p current.
2892 *
2893 * HLS segments can repeat timed ID3 metadata in every segment so that
2894 * listeners joining mid-stream immediately receive the current track
2895 * information. Only signal a metadata change when values actually differ
2896 * to avoid spurious updates on every segment boundary.
2897 */
2899 const AVDictionary *update)
2900{
2902 return 1;
2903 const AVDictionaryEntry *e = NULL;
2904 while ((e = av_dict_iterate(update, e))) {
2905 const AVDictionaryEntry *cur = av_dict_get(current, e->key, NULL, 0);
2906 if (!cur || strcmp(cur->value, e->value))
2907 return 1;
2908 }
2909 return 0;
2910}
2911
2913{
2914 HLSContext *c = s->priv_data;
2915 int ret, i, minplaylist = -1;
2916
2917 recheck_discard_flags(s, c->first_packet);
2918 c->first_packet = 0;
2919
2920 for (i = 0; i < c->n_playlists; i++) {
2921 struct playlist *pls = c->playlists[i];
2922 /* Make sure we've got one buffered packet from each open playlist
2923 * stream */
2924 if (pls->needed && !pls->pkt->data) {
2925 while (1) {
2926 int64_t ts_diff;
2927 AVRational tb;
2928 struct segment *seg = NULL;
2929 if (pls->is_subtitle)
2930 ret = read_subtitle_packet(pls, pls->pkt);
2931 else
2932 ret = av_read_frame(pls->ctx, pls->pkt);
2933 if (ret < 0) {
2934 if (!avio_feof(&pls->pb.pub) && ret != AVERROR_EOF)
2935 return ret;
2936 break;
2937 }
2938
2939 /* stream_index check prevents matching picture attachments etc. */
2940 if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) {
2941 /* audio elementary streams are id3 timestamped */
2943 }
2944
2945 if (pls->pkt->dts != AV_NOPTS_VALUE &&
2946 (pls->ts_offset == AV_NOPTS_VALUE ||
2947 (!c->first_timestamp_locked &&
2948 c->first_timestamp_pls == pls &&
2949 pls->cur_seq_no <= pls->first_read_seq_no + 1))) {
2950 int adjusted = 0;
2951 /* Packet timestamp rebased onto the start of the segment list. */
2952 int64_t seg_idx = pls->first_read_seq_no - pls->start_seq_no;
2953 int64_t ts = av_rescale_q(pls->pkt->pts != AV_NOPTS_VALUE ?
2954 pls->pkt->pts : pls->pkt->dts,
2956
2957 for (int64_t k = 0; k < seg_idx && k < pls->n_segments; k++)
2958 ts = av_sat_sub64(ts, pls->segments[k]->duration);
2959
2960 if (c->first_timestamp == AV_NOPTS_VALUE) {
2961 c->first_timestamp = ts;
2962 c->first_timestamp_pls = pls;
2963 adjusted = 1;
2964 } else if (c->first_timestamp_pls == pls) {
2965 /* first_timestamp came from the first packet in mux
2966 * order, but another stream in the same segment may
2967 * start earlier. Lower the estimate while packets of
2968 * that segment are still arriving. */
2969 int64_t delta = av_sat_sub64(c->first_timestamp, ts);
2970
2971 if (delta > 0 && delta <= pls->target_duration) {
2972 c->first_timestamp = ts;
2973 for (int k = 0; k < c->n_playlists; k++)
2974 if (c->playlists[k]->ts_offset != AV_NOPTS_VALUE)
2975 c->playlists[k]->ts_offset = av_sat_add64(c->playlists[k]->ts_offset, delta);
2976 adjusted = 1;
2977 }
2978 }
2979
2980 if (pls->ts_offset == AV_NOPTS_VALUE)
2981 pls->ts_offset = av_sat_sub64(ts, c->first_timestamp);
2982
2983 if (adjusted) {
2984 for (unsigned k = 0; k < s->nb_streams; k++)
2985 s->streams[k]->start_time =
2986 av_rescale_q(c->first_timestamp, AV_TIME_BASE_Q,
2987 s->streams[k]->time_base);
2989 "First timestamp %"PRId64" from playlist %d\n",
2990 c->first_timestamp, pls->index);
2991 }
2992 }
2993
2994 seg = current_segment(pls);
2995 if (seg && seg->key_type == KEY_SAMPLE_AES && !strstr(pls->ctx->iformat->name, "mov")) {
2997 memcpy(c->crypto_ctx.iv, seg->iv, sizeof(seg->iv));
2998 memcpy(c->crypto_ctx.key, pls->key, sizeof(pls->key));
2999 ff_hls_senc_decrypt_frame(codec_id, &c->crypto_ctx, pls->pkt);
3000 }
3001
3002 if (pls->seek_timestamp == AV_NOPTS_VALUE)
3003 break;
3004
3005 if (pls->seek_stream_index < 0 ||
3006 pls->seek_stream_index == pls->pkt->stream_index) {
3007
3008 if (pls->pkt->dts == AV_NOPTS_VALUE) {
3010 break;
3011 }
3012
3013 tb = get_timebase(pls);
3014 ts_diff = av_rescale_q_rnd(pls->pkt->dts, tb,
3016 pls->seek_timestamp;
3017 if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
3018 pls->pkt->flags & AV_PKT_FLAG_KEY)) {
3020 break;
3021 }
3022 }
3023 av_packet_unref(pls->pkt);
3024 }
3025 }
3026 /* Check if this stream has the packet with the lowest dts */
3027 if (pls->pkt->data) {
3028 struct playlist *minpls = minplaylist < 0 ?
3029 NULL : c->playlists[minplaylist];
3030 if (minplaylist < 0) {
3031 minplaylist = i;
3032 } else {
3033 int64_t dts = pls->pkt->dts;
3034 int64_t mindts = minpls->pkt->dts;
3035
3036 if (dts == AV_NOPTS_VALUE ||
3037 (mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
3038 minplaylist = i;
3039 }
3040 }
3041 }
3042
3043 /* If we got a packet, return it */
3044 if (minplaylist >= 0) {
3045 struct playlist *pls = c->playlists[minplaylist];
3046 AVStream *ist;
3047 AVStream *st;
3048
3050 if (ret < 0) {
3051 av_packet_unref(pls->pkt);
3052 return ret;
3053 }
3054
3055 // If sub-demuxer reports updated metadata, copy it to the first stream
3056 // and set its AVSTREAM_EVENT_FLAG_METADATA_UPDATED flag.
3058 if (pls->n_main_streams) {
3059 st = pls->main_streams[0];
3060 av_dict_copy(&st->metadata, pls->ctx->metadata, 0);
3062 }
3064 }
3065
3066 /* check if noheader flag has been cleared by the subdemuxer */
3067 if (pls->has_noheader_flag && !(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER)) {
3068 pls->has_noheader_flag = 0;
3070 }
3071
3072 if (pls->pkt->stream_index >= pls->n_main_streams) {
3073 av_log(s, AV_LOG_ERROR, "stream index inconsistency: index %d, %d main streams, %d subdemuxer streams\n",
3074 pls->pkt->stream_index, pls->n_main_streams, pls->ctx->nb_streams);
3075 av_packet_unref(pls->pkt);
3076 return AVERROR_BUG;
3077 }
3078
3079 ist = pls->ctx->streams[pls->pkt->stream_index];
3080 st = pls->main_streams[pls->pkt->stream_index];
3081
3082 // Propagate timed-ID3 metadata changes to the main stream, suppressing
3083 // duplicate packets that repeat unchanged metadata at segment boundaries.
3085 pls->pkt->stream_index == pls->timed_id3_stream_index &&
3089 int ret = av_dict_copy(&st->metadata, ist->metadata, 0);
3090 if (ret < 0)
3091 return ret;
3093 ret = av_dict_copy(&pls->timed_id3_metadata, ist->metadata, 0);
3094 if (ret < 0)
3095 return ret;
3097 }
3098 }
3099
3100 av_packet_move_ref(pkt, pls->pkt);
3101 pkt->stream_index = st->index;
3102
3103 if (pkt->dts != AV_NOPTS_VALUE)
3104 c->cur_timestamp = av_rescale_q(pkt->dts,
3105 ist->time_base,
3107
3108 /* There may be more situations where this would be useful, but this at least
3109 * handles newly probed codecs properly (i.e. request_probe by mpegts). */
3110 if (ist->codecpar->codec_id != st->codecpar->codec_id) {
3111 ret = set_stream_info_from_input_stream(st, pls, ist);
3112 if (ret < 0) {
3113 return ret;
3114 }
3115 }
3116
3117 return 0;
3118 }
3119 return AVERROR_EOF;
3120}
3121
3122static int hls_read_seek(AVFormatContext *s, int stream_index,
3123 int64_t timestamp, int flags)
3124{
3125 HLSContext *c = s->priv_data;
3126 struct playlist *seek_pls = NULL;
3127 int i, j;
3128 int stream_subdemuxer_index;
3129 int64_t first_timestamp, seek_timestamp, duration;
3130 int64_t seq_no, seg_start_ts;
3131 int snapped_to_segment = 0;
3132
3133 if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
3134 return AVERROR(ENOSYS);
3135
3136 seek_timestamp = av_rescale_q_rnd(timestamp,
3137 s->streams[stream_index]->time_base,
3139
3140 /* find the playlist with the specified stream */
3141 for (i = 0; i < c->n_playlists; i++) {
3142 struct playlist *pls = c->playlists[i];
3143 for (j = 0; j < pls->n_main_streams; j++) {
3144 if (pls->main_streams[j] == s->streams[stream_index]) {
3145 seek_pls = pls;
3146 stream_subdemuxer_index = j;
3147 break;
3148 }
3149 }
3150 }
3151 if (!seek_pls)
3152 return AVERROR(EIO);
3153
3154 /* Live and EVENT playlists may have gained segments since the last load,
3155 * and sliding-window playlists may also have expired some. Refresh before
3156 * resolving a seek that may fall outside the loaded window, rate limited
3157 * to the minimum reload interval (RFC 8216 6.3.4). */
3158 if (!seek_pls->finished) {
3159 int64_t elapsed = av_gettime_relative() - seek_pls->last_load_time;
3160 int64_t start = c->first_timestamp == AV_NOPTS_VALUE ?
3161 0 : c->first_timestamp;
3162 int64_t end = start;
3163 int need_refresh;
3164 for (i = 0; i < seek_pls->n_segments; i++)
3165 end += seek_pls->segments[i]->duration;
3166 need_refresh = seek_timestamp >= end;
3167 if (seek_pls->type == PLS_TYPE_UNSPECIFIED)
3168 need_refresh |= seek_timestamp < start + elapsed +
3169 seek_pls->target_duration;
3170 if (need_refresh && elapsed >= default_reload_interval(seek_pls))
3171 parse_playlist(c, seek_pls->url, seek_pls, NULL);
3172 }
3173
3174 first_timestamp = c->first_timestamp == AV_NOPTS_VALUE ?
3175 0 : c->first_timestamp;
3176
3177 duration = s->duration == AV_NOPTS_VALUE ?
3178 0 : s->duration;
3179
3180 /* Only finished playlists cannot grow past the known duration. */
3181 if (seek_pls->finished &&
3182 0 < duration && duration < seek_timestamp - first_timestamp)
3183 return AVERROR(EIO);
3184
3185 /* check if the timestamp is valid for the playlist with the
3186 * specified stream index */
3187 if (!find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts))
3188 return AVERROR(EIO);
3189
3190 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
3192 /* Seeking to start of segment ensures we seek to a keyframe located
3193 * before the given timestamp. */
3194 seek_timestamp = seg_start_ts;
3195 snapped_to_segment = 1;
3196 }
3197
3198 av_log(s, AV_LOG_DEBUG, "Seek to %"PRId64" mapped to segment %"PRId64
3199 " of playlist %d (start %"PRId64")\n",
3200 seek_timestamp, seq_no, seek_pls->index, seg_start_ts);
3201
3202 /* Keep timeline start fixed from now on. */
3203 c->first_timestamp_locked = 1;
3204
3205 /* set segment now so we do not need to search again below */
3206 seek_pls->cur_seq_no = seq_no;
3207 seek_pls->seek_stream_index = stream_subdemuxer_index;
3208
3209 /* Reset PTS wrap detection so backward seeks don't get misinterpreted
3210 * as a forward PTS wrap. */
3211 for (i = 0; i < (int)s->nb_streams; i++)
3213
3214 for (i = 0; i < c->n_playlists; i++) {
3215 /* Reset reading */
3216 struct playlist *pls = c->playlists[i];
3217 AVIOContext *const pb = &pls->pb.pub;
3218 ff_format_io_close(pls->parent, &pls->input);
3219 pls->input_read_done = 0;
3220 pls->first_read_seq_no = -1;
3222 pls->input_next_requested = 0;
3223 av_packet_unref(pls->pkt);
3224 pb->eof_reached = 0;
3225 /* Clear any buffered data */
3226 pb->buf_end = pb->buf_ptr = pb->buffer;
3227 /* Reset the pos, to let the mpegts/mov demuxer know we've seeked. */
3228 pb->pos = 0;
3229 /* Flush the packet queue of the subdemuxer. */
3230 if (pls->ctx) {
3232 for (j = 0; j < (int)pls->ctx->nb_streams; j++)
3234 }
3235 if (pls->is_subtitle)
3237
3238 /* Reset the init segment so it's re-fetched and served appropriately */
3239 pls->cur_init_section = NULL;
3240
3241 /* The discard in hls_read_packet compares this playlist's packet DTS
3242 * against seek_timestamp, but seek_timestamp is on c->first_timestamp's
3243 * baseline (whichever stream produced the first packet). Streams can
3244 * have different DTS baselines, so translate the threshold onto this
3245 * playlist's own baseline using its captured offset. */
3246 if (pls->ts_offset != AV_NOPTS_VALUE)
3248 else
3250 pls->seek_flags = flags;
3251
3252 if (pls == seek_pls && snapped_to_segment) {
3253 /* The first keyframe of the target segment may have a slightly
3254 * lower DTS than the EXTINF-derived seg_start_ts. Relax the cutoff
3255 * to not discard the keyframe and land one segment late. */
3256 int64_t idx = seq_no - pls->start_seq_no;
3257 if (idx >= 0 && idx < pls->n_segments)
3259 }
3260
3261 if (pls != seek_pls) {
3262 /* set closest segment seq_no for playlists not handled above */
3264 /* seek the playlist to the given position without taking
3265 * keyframes into account since this playlist does not have the
3266 * specified stream where we should look for the keyframes */
3267 pls->seek_stream_index = -1;
3269 }
3270
3271 pls->last_seq_no = pls->cur_seq_no;
3272 }
3273
3274 c->cur_timestamp = seek_timestamp;
3275
3276 return 0;
3277}
3278
3279static int hls_probe(const AVProbeData *p)
3280{
3281 /* Require #EXTM3U at the start, and either one of the ones below
3282 * somewhere for a proper match. */
3283 if (strncmp(p->buf, "#EXTM3U", 7))
3284 return 0;
3285
3286 if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
3287 strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
3288 strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:")) {
3289
3290 int mime_ok = p->mime_type && !(
3291 av_strcasecmp(p->mime_type, "application/vnd.apple.mpegurl") &&
3292 av_strcasecmp(p->mime_type, "audio/mpegurl")
3293 );
3294
3295 int mime_x = p->mime_type && !(
3296 av_strcasecmp(p->mime_type, "audio/x-mpegurl") &&
3297 av_strcasecmp(p->mime_type, "application/x-mpegurl")
3298 );
3299
3300 if (!mime_ok &&
3301 !mime_x &&
3302 !av_match_ext (p->filename, "m3u8,m3u") &&
3303 ff_match_url_ext(p->filename, "m3u8,m3u") <= 0) {
3304 av_log(NULL, AV_LOG_ERROR, "Not detecting m3u8/hls with non standard extension and non standard mime type\n");
3305 return 0;
3306 }
3307 if (mime_x)
3308 av_log(NULL, AV_LOG_WARNING, "mime type is not rfc8216 compliant\n");
3309
3310 return AVPROBE_SCORE_MAX;
3311 }
3312 return 0;
3313}
3314
3315#define OFFSET(x) offsetof(HLSContext, x)
3316#define FLAGS AV_OPT_FLAG_DECODING_PARAM
3317static const AVOption hls_options[] = {
3318 {"live_start_index", "segment index to start live streams at (negative values are from the end)",
3319 OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
3320 {"prefer_x_start", "prefer to use #EXT-X-START if it's in playlist instead of live_start_index",
3321 OFFSET(prefer_x_start), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS},
3322 {"allowed_extensions", "List of file extensions that hls is allowed to access",
3323 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
3324 {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,vtt,wav,webvtt"
3325 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3326 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3327 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3328 },
3329 INT_MIN, INT_MAX, FLAGS},
3330 {"allowed_segment_extensions", "List of file extensions that hls is allowed to access",
3331 OFFSET(allowed_segment_extensions), AV_OPT_TYPE_STRING,
3332 {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,vtt,wav,webvtt"
3333 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3334 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3335 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3336 ",html" // https://flash1.bogulus.cfd/
3337 },
3338 INT_MIN, INT_MAX, FLAGS},
3339 {"extension_picky", "Be picky with all extensions matching",
3340 OFFSET(extension_picky), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
3341 {"max_reload", "Maximum number of times a insufficient list is attempted to be reloaded",
3342 OFFSET(max_reload), AV_OPT_TYPE_INT, {.i64 = 100}, 0, INT_MAX, FLAGS},
3343 {"m3u8_hold_counters", "The maximum number of times to load m3u8 when it refreshes without new segments",
3344 OFFSET(m3u8_hold_counters), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS},
3345 {"http_persistent", "Use persistent HTTP connections",
3346 OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
3347 {"http_multiple", "Use multiple HTTP connections for fetching segments",
3348 OFFSET(http_multiple), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, FLAGS},
3349 {"http_seekable", "Use HTTP partial requests, 0 = disable, 1 = enable, -1 = auto",
3350 OFFSET(http_seekable), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, FLAGS},
3351 {"seg_format_options", "Set options for segment demuxer",
3352 OFFSET(seg_format_opts), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, FLAGS},
3353 {"seg_max_retry", "Maximum number of times to reload a segment on error.",
3354 OFFSET(seg_max_retry), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
3355 {NULL}
3356};
3357
3358static const AVClass hls_class = {
3359 .class_name = "hls demuxer",
3360 .item_name = av_default_item_name,
3361 .option = hls_options,
3362 .version = LIBAVUTIL_VERSION_INT,
3363};
3364
3366 .p.name = "hls",
3367 .p.long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
3368 .p.priv_class = &hls_class,
3370 .priv_data_size = sizeof(HLSContext),
3371 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
3377};
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
const FFInputFormat ff_hls_demuxer
Definition hls.c:3365
#define entry
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition avformat.c:992
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:910
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition avformat.c:367
Main libavformat public API header.
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1796
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1352
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition avformat.h:502
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:508
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2729
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition avformat.h:507
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition avformat.h:498
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition avformat.h:2730
#define AVFMTCTX_UNSEEKABLE
signal that the stream is definitely not seekable, and attempts to call the seek function will fail.
Definition avformat.h:1354
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition avformat.h:674
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:886
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition avformat.h:1561
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2728
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition avformat.h:641
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition avio.c:113
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition avio.c:934
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition avio.c:732
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_FLAG_READ
read-only
Definition avio.h:630
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:620
void ffio_init_context(FFIOContext *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, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition aviobuf.c:50
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition aviobuf.c:99
int ffio_copy_url_options(AVIOContext *pb, AVDictionary **avio_opts)
Read url related dictionary options from the AVIOContext and write to the given dictionary.
Definition aviobuf.c:962
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
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:765
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define FLAGS
Definition cmdutils.c:596
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define av_sat_sub64
Definition common.h:142
#define av_sat_add64
Definition common.h:139
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define INITIAL_BUFFER_SIZE
Definition dashdec.c:36
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition dashdec.c:1949
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:801
static AVPacket * pkt
Public dictionary API.
static struct @111144215057303131116103221376075045141373005341 current
static int read_key(void)
Definition ffmpeg.c:264
const char * key
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static av_cold void cleanup(FlashSV2Context *s)
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:611
@ AV_CODEC_ID_EAC3
Definition codec_id.h:498
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AAC
Definition codec_id.h:460
@ AV_CODEC_ID_AC3
Definition codec_id.h:461
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:246
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition avformat.c:299
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:165
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition format.c:146
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1645
int av_probe_input_buffer(AVIOContext *pb, const 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:348
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2673
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
int avformat_queue_attached_pictures(AVFormatContext *s)
Definition demux_utils.c:87
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:41
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition aes.c:37
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
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:61
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
#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:79
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:87
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:38
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition dict.h:74
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:180
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition error.h:65
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
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.
@ AV_ROUND_DOWN
Round toward -infinity.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
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:601
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:661
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
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:179
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition avstring.c:344
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:36
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:85
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition avstring.c:218
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(const void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition tree.c:59
void av_tree_destroy(AVTreeNode *t)
Definition tree.c:146
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition tree.c:34
#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:1295
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
int a
static void update_noheader_flag(AVFormatContext *s)
Definition hls.c:2434
#define MAX_CHARACTERISTICS_LEN
Definition hls.c:55
#define MAX_FIELD_LEN
Definition hls.c:54
static int is_native_http(AVIOContext *pb)
Definition hls.c:797
static int update_init_section(struct playlist *pls, struct segment *seg)
Definition hls.c:1710
static struct rendition_group * find_rendition_group(HLSContext *c, enum AVMediaType type, const char *id)
Definition hls.c:602
static int cmp_rendition_group(const void *a, const void *b)
Definition hls.c:593
static int read_from_url(struct playlist *pls, struct segment *seg, uint8_t *buf, int buf_size)
Definition hls.c:1385
static int add_renditions_to_variant(HLSContext *c, struct variant *var, enum AVMediaType type, const char *group_id)
Definition hls.c:2142
static struct segment * new_init_section(struct playlist *pls, struct init_section_info *info, const char *url_base)
Definition hls.c:519
static int cmp_url_playlist(const void *key, const void *b)
Definition hls.c:380
static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t timestamp, int64_t *seq_no, int64_t *seg_start_ts)
Definition hls.c:2229
static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:1897
static int hls_read_header(AVFormatContext *s)
Definition hls.c:2472
static struct playlist * new_playlist(HLSContext *c, const char *url, const char *base)
Definition hls.c:392
static int read_subtitle_packet(struct playlist *v, AVPacket *pkt)
Definition hls.c:2110
static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls, AVStream *ist)
Definition hls.c:2369
static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
Definition hls.c:913
static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in, int64_t *end)
Definition hls.c:1634
static void free_segment_list(struct playlist *pls)
Definition hls.c:298
static void handle_key_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:498
#define MPEG_TIME_BASE
Definition hls.c:57
static void free_init_section_list(struct playlist *pls)
Definition hls.c:305
static int hls_probe(const AVProbeData *p)
Definition hls.c:3279
static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
Definition hls.c:786
static int segment_continues(const struct segment *cur, const struct segment *next, int64_t end)
Definition hls.c:1359
PlaylistType
Definition hls.c:96
@ PLS_TYPE_UNSPECIFIED
Definition hls.c:97
@ PLS_TYPE_VOD
Definition hls.c:99
@ PLS_TYPE_EVENT
Definition hls.c:98
static int recheck_discard_flags(AVFormatContext *s, int first)
Definition hls.c:2803
static AVRational get_timebase(struct playlist *pls)
Definition hls.c:2872
static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
Definition hls.c:2847
static int64_t default_reload_interval(struct playlist *pls)
Definition hls.c:1767
static int open_url_keepalive(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **options)
Definition hls.c:807
static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls, enum AVMediaType type)
Definition hls.c:2197
static int update_streams_from_subdemuxer(AVFormatContext *s, struct playlist *pls)
Definition hls.c:2393
static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
Definition hls.c:2343
static int hls_close(AVFormatContext *s)
Definition hls.c:2455
static int metadata_has_changed(const AVDictionary *current, const AVDictionary *update)
Check whether any entry in update differs from the corresponding entry in current.
Definition hls.c:2898
static struct segment * next_segment(struct playlist *pls)
Definition hls.c:1347
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition hls.c:2060
static int reload_playlist(struct playlist *v, HLSContext *c)
Definition hls.c:1820
static void handle_id3(AVIOContext *pb, struct playlist *pls)
Definition hls.c:1460
#define PACKED_AUDIO_FORMATS
Definition hls.c:60
static int add_renditions_to_playlists(HLSContext *c, enum AVMediaType type)
Definition hls.c:2171
static void handle_rendition_args(void *vinfo, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:740
static int new_rendition(HLSContext *c, struct rendition_info *info, const char *url_base)
Definition hls.c:636
static void free_segment_dynarray(struct segment **segments, int n_segments)
Definition hls.c:288
static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:2031
static int init_subtitle_context(struct playlist *pls)
Definition hls.c:2070
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:2880
static int parse_playlist(HLSContext *c, const char *url, struct playlist *pls, AVIOContext *in)
Definition hls.c:960
#define MPEG_TIME_BASE_Q
Definition hls.c:58
KeyType
Definition hls.c:74
@ KEY_SAMPLE_AES
Definition hls.c:77
@ KEY_NONE
Definition hls.c:75
@ KEY_AES_128
Definition hls.c:76
static void handle_variant_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:473
static struct segment * current_segment(struct playlist *pls)
Definition hls.c:1339
static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition hls.c:2912
static const AVOption hls_options[]
Definition hls.c:3317
static void free_rendition_list(HLSContext *c)
Definition hls.c:361
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
Definition hls.c:825
#define OFFSET(x)
Definition hls.c:3315
static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, ID3v2ExtraMetaAPIC *apic)
Definition hls.c:1431
static void free_playlist_list(HLSContext *c)
Definition hls.c:317
static const AVClass hls_class
Definition hls.c:3358
static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
Definition hls.c:2268
static struct variant * new_variant(HLSContext *c, struct variant_info *info, const char *url, const char *base)
Definition hls.c:447
static void intercept_id3(struct playlist *pls, uint8_t *buf, int buf_size, int *len)
Definition hls.c:1503
static struct rendition_group * new_rendition_group(HLSContext *c, enum AVMediaType type, const char *id)
Definition hls.c:612
static int playlist_needed(struct playlist *pls)
Definition hls.c:1774
static void free_variant_list(HLSContext *c)
Definition hls.c:349
static int64_t range_end(struct playlist *pls, const struct segment *seg)
Definition hls.c:1371
static void parse_id3(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info, ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
Definition hls.c:1402
static void handle_init_section_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:568
static int hls_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition hls.c:3122
static int cmp_playlist_url(const void *a, const void *b)
Definition hls.c:386
int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info)
void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size)
Apple HTTP Live Streaming Sample Encryption https://developer.apple.com/library/ios/documentation/Aud...
#define HLS_MAX_ID3_TAGS_DATA_LEN
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:590
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition id3v2.c:165
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition id3v2.c:152
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition id3v2.c:1196
int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
Parse PRIV tags into a dictionary.
Definition id3v2.c:1268
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:1212
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition id3v2.c:1308
void ff_id3v2_read_dict(AVFormatContext *s, 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:1184
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define ID3v2_HEADER_SIZE
Definition id3v2.h:30
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_WB64(p, v)
#define AV_RB64(p)
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
int ff_match_url_ext(const char *url, const char *extensions)
Return a positive value if the given url has one of the given extensions, negative AVERROR on error,...
Definition format.c:54
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition utils.c:495
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:384
#define dynarray_add(tab, nb_ptr, elem)
Definition internal.h:395
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:473
#define MAX_URL_SIZE
Definition internal.h:30
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:523
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
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:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
unsigned int pos
Definition spdifenc.c:431
double strtod(const char *, char **)
uint8_t * data
The data buffer.
Definition buffer.h:90
size_t size
Size of data in bytes.
Definition buffer.h:94
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
char * key
Definition dict.h:91
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1401
int event_flags
Flags indicating events happening on the file, a combination of AVFMT_EVENT_FLAG_*.
Definition avformat.h:1789
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1457
AVIOContext * pb
I/O context.
Definition avformat.h:1443
AVDictionary * metadata
Metadata that applies to the whole file.
Definition avformat.h:1649
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1553
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:1609
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1450
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1687
const struct AVInputFormat * iformat
The input container format.
Definition avformat.h:1413
void * priv_data
Format private data.
Definition avformat.h:1429
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition avformat.h:1601
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1469
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:2022
Bytestream IO Context.
Definition avio.h:160
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:228
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
unsigned char * buf_ptr
Current position in the buffer.
Definition avio.h:227
int64_t pos
position in the file of the current buffer
Definition avio.h:237
unsigned char * buffer
Start of the buffer.
Definition avio.h:225
int eof_reached
true if was unable to read due to error or eof
Definition avio.h:238
Callback for checking whether to abort blocking functions.
Definition avio.h:59
const char * name
A comma separated list of short names for the format.
Definition avformat.h:572
const char * extensions
If extensions are defined, then no probe is done.
Definition avformat.h:594
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int stream_index
Definition packet.h:612
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
This structure contains the data a format has to probe a file.
Definition avformat.h:473
New fields can be added to the end with minor version bumps.
Definition avformat.h:1325
unsigned int nb_stream_indexes
Definition avformat.h:1330
unsigned int * stream_index
Definition avformat.h:1329
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition avformat.h:1328
AVDictionary * metadata
Definition avformat.h:1331
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
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:839
AVDictionary * metadata
Definition avformat.h:848
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int pts_wrap_bits
Number of bits in timestamps.
Definition avformat.h:911
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition avformat.h:866
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition avformat.h:879
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
AVIOContext pub
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition internal.h:281
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition internal.h:185
int prefer_x_start
Definition hls.c:266
int first_timestamp_locked
Definition hls.c:271
int n_renditions
Definition hls.c:257
int http_multiple
Definition hls.c:281
AVDictionary * seg_format_opts
Definition hls.c:275
struct rendition ** renditions
Definition hls.c:258
int m3u8_hold_counters
Definition hls.c:264
AVIOInterruptCB * interrupt_callback
Definition hls.c:273
int seg_max_retry
Definition hls.c:283
int master_time_offset_flag
EXT-X-START was set by the Master Playlist.
Definition hls.c:267
AVDictionary * avio_opts
Definition hls.c:274
char * allowed_segment_extensions
Definition hls.c:277
int n_variants
Definition hls.c:252
int http_persistent
Definition hls.c:280
struct AVTreeNode * rendition_groups_by_id
Definition hls.c:261
struct variant ** variants
Definition hls.c:253
int extension_picky
Definition hls.c:278
AVFormatContext * ctx
Definition hls.c:251
int64_t cur_seq_no
Definition hls.c:263
int live_start_index
Definition hls.c:265
AVIOContext * playlist_pb
Definition hls.c:284
int first_packet
Definition hls.c:268
int max_reload
Definition hls.c:279
int n_playlists
Definition hls.c:254
struct playlist * first_timestamp_pls
Definition hls.c:270
int64_t cur_timestamp
Definition hls.c:272
HLSCryptoContext crypto_ctx
Definition hls.c:285
int n_rendition_groups
Definition hls.c:259
struct AVTreeNode * playlists_by_url
Definition hls.c:256
int64_t first_timestamp
Definition hls.c:269
struct playlist ** playlists
Definition hls.c:255
char * allowed_extensions
Definition hls.c:276
int http_seekable
Definition hls.c:282
struct rendition_group ** rendition_groups
Definition hls.c:260
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::@257041253361347165140143067330023232307201221326 data
const char * tag
Definition id3v2.h:85
struct ID3v2ExtraMeta * next
Definition id3v2.h:86
const struct URLProtocol * prot
Definition url.h:37
const char * name
Definition url.h:55
char uri[MAX_URL_SIZE]
Definition hls.c:515
char byterange[32]
Definition hls.c:516
char iv[35]
Definition hls.c:495
char uri[MAX_URL_SIZE]
Definition hls.c:493
char method[11]
Definition hls.c:494
int finished
Definition hls.c:127
int id3_changed
Definition hls.c:163
int broken
Definition hls.c:136
int64_t cur_seg_offset
Definition hls.c:141
AVIOContext * input
Definition hls.c:110
int64_t cur_seq_no
Definition hls.c:137
int n_renditions
Definition hls.c:179
int64_t first_read_seq_no
Definition hls.c:139
int64_t id3_mpegts_timestamp
Definition hls.c:157
FFIOContext pb
Definition hls.c:109
struct rendition ** renditions
Definition hls.c:180
unsigned int init_sec_data_len
Definition hls.c:148
int64_t id3_offset
Definition hls.c:158
int64_t target_duration
Definition hls.c:129
int64_t last_load_time
Definition hls.c:142
int64_t last_seq_no
Definition hls.c:138
int seek_stream_index
Definition hls.c:173
int64_t input_next_end
Definition hls.c:115
HLSAudioSetupInfo audio_setup_info
Definition hls.c:166
int64_t start_seq_no
Definition hls.c:130
int is_subtitle
Definition hls.c:191
unsigned int id3_buf_size
Definition hls.c:160
AVStream ** main_streams
Definition hls.c:124
AVIOContext * input_next
Definition hls.c:113
unsigned int init_sec_buf_read_offset
Definition hls.c:149
struct variant * last_variant
Definition hls.c:184
int64_t ts_offset
Definition hls.c:170
int64_t seek_timestamp
Definition hls.c:171
int64_t input_end
Definition hls.c:112
AVPacket * pkt
Definition hls.c:119
int input_read_done
Definition hls.c:111
AVFormatContext * ctx
Definition hls.c:118
uint8_t * init_sec_buf
Definition hls.c:146
struct segment * cur_init_section
Definition hls.c:145
uint8_t * id3_buf
Definition hls.c:159
uint8_t key[16]
Definition hls.c:152
int id3_found
Definition hls.c:162
int index
Definition hls.c:117
int m3u8_hold_counters
Definition hls.c:140
int needed
Definition hls.c:135
int has_noheader_flag
Definition hls.c:120
char key_url[MAX_URL_SIZE]
Definition hls.c:151
int timed_id3_stream_index
Definition hls.c:199
struct rendition_group * last_group
Definition hls.c:185
int n_init_sections
Definition hls.c:189
int64_t start_time_offset
Definition hls.c:132
AVDictionary * timed_id3_metadata
Definition hls.c:198
struct segment ** segments
Definition hls.c:134
int n_segments
Definition hls.c:133
struct segment ** init_sections
Definition hls.c:190
char url[MAX_URL_SIZE]
Definition hls.c:108
AVFormatContext * parent
Definition hls.c:116
int time_offset_flag
Definition hls.c:131
int seek_flags
Definition hls.c:172
int is_id3_timestamped
Definition hls.c:156
unsigned int init_sec_buf_size
Definition hls.c:147
int n_main_streams
Definition hls.c:125
int input_next_requested
Definition hls.c:114
ID3v2ExtraMeta * id3_deferred_extra
Definition hls.c:164
AVDictionary * id3_initial
Definition hls.c:161
enum PlaylistType type
Definition hls.c:128
struct rendition ** renditions
Definition hls.c:242
int n_playlists
Definition hls.c:237
int n_variants
Definition hls.c:245
struct variant ** variants
Definition hls.c:246
char id[MAX_FIELD_LEN]
Definition hls.c:234
int n_renditions
Definition hls.c:241
struct playlist ** playlists
Definition hls.c:238
enum AVMediaType type
Definition hls.c:233
char type[16]
Definition hls.c:582
char defaultr[4]
Definition hls.c:588
char name[MAX_FIELD_LEN]
Definition hls.c:587
char language[MAX_FIELD_LEN]
Definition hls.c:585
char forced[4]
Definition hls.c:589
char uri[MAX_URL_SIZE]
Definition hls.c:583
char group_id[MAX_FIELD_LEN]
Definition hls.c:584
char characteristics[MAX_CHARACTERISTICS_LEN]
Definition hls.c:590
char assoc_language[MAX_FIELD_LEN]
Definition hls.c:586
char name[MAX_FIELD_LEN]
Definition hls.c:212
enum AVMediaType type
Definition hls.c:209
char language[MAX_FIELD_LEN]
Definition hls.c:211
int disposition
Definition hls.c:213
struct playlist * playlist
Definition hls.c:210
Definition hls.c:80
struct segment * init_section
Definition hls.c:89
int64_t size
Definition hls.c:83
uint8_t iv[16]
Definition hls.c:87
char * url
Definition hls.c:84
int64_t duration
Definition hls.c:81
int64_t url_offset
Definition hls.c:82
char * key
Definition hls.c:85
enum KeyType key_type
Definition hls.c:86
char bandwidth[20]
Definition hls.c:440
char video[MAX_FIELD_LEN]
Definition hls.c:443
char subtitles[MAX_FIELD_LEN]
Definition hls.c:444
char audio[MAX_FIELD_LEN]
Definition hls.c:442
Definition hls.c:216
int bandwidth
Definition hls.c:217
char audio_group[MAX_FIELD_LEN]
Definition hls.c:223
int n_playlists
Definition hls.c:220
struct playlist ** playlists
Definition hls.c:221
char video_group[MAX_FIELD_LEN]
Definition hls.c:224
char subtitles_group[MAX_FIELD_LEN]
Definition hls.c:225
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition tree.c:39
A tree container.
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:321
unbuffered private I/O API
enum AVCodecID codec_id
static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
int len
uint8_t base
Definition vp3data.h:128
static double c[64]