avformat/webvttdec: add support for HLS WebVTT MPEG timestamp maps

WebVTT subtitle files in HLS streams contain a header to synchronize the
subtitles with the MPEG TS timestamps of the HLS stream.

Add an AVOption to prefer MPEG TS style timestamps generated according
to the mapping header, if available.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: add support for alternative renditions

HLS protocol version 4 added alternative renditions to the
specification (e.g. alternative audio tracks).

The EXT-X-MEDIA tags can also contain metadata for "renditions" (i.e.
tracks) of the main Media Playlist.

Add support for those.

Note that the same rendition (AVStream) may be associated with multiple
variants (AVPrograms).

Also note that alternative subtitle tracks require my preceding and
following commits to work properly, and alternative audio tracks require
my preceding commits.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: always return EOF for unneeded playlist streams

Even if we returned AVERROR_EOF previously due to playlist no longer
being needed, we may still be called again, and we do not want to
trigger a segment download in that case.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: add support for byte-ranged segments

Add support for EXT-X-BYTERANGE added in HLS protocol v4.

v2: Better comment explaining ffurl_seek call and fix cur_seg_offset not
being updated.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: add some logging messages

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat: add AVFMT_RAW_ID3 for demuxers requiring unstripped ID3

This is needed by hls_id3_audio since the ID3 tag contains private
timestamp data needed by read_header() and read_probe().

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls_id3_audio: add HLS ID3 timestamped audio demuxer

This is to be mainly used via the hls demuxer.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: pass segment offset info to hls_id3_audio demuxer

HLS provides MPEG TS timestamps via ID3 tags in the beginning of each
segment of elementary audio streams. This is handled by the new
hls_id3_audio demuxer, but it requires us to pass along the segment
switch offsets via AVOptions.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: demux each subtitle segment separately

Subtitle demuxers (e.g. WebVTT used in HLS) read the entire file in
read_header(), so only provide them a single segment at a time, and open
the demuxer again for a following segment.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: request MPEG TS timestamps from WebVTT demuxer

Request the HLS-specific MPEG TS timestamps from the WebVTT demuxer so
that the subtitles will be synchronized properly.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: track seeking on a per-playlist basis

Seeking needs to be tracked on a per-playlist basis, since the resyncing
code in hls_read_packet() has to sync each playlist to the seek
timestamp instead of stopping after the first playlist has reached it.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: improve segment selection when restarting list reception

Improve selection of the segment sequence number when restarting the
reception of a playlist after it was suspended due to being unneeded
(due to discard flags).

The current code assumes that each playlist contains matching data with
the same sequence number, while spec 3.4.3 specifically says that that
is not the case. Often subtitle playlists also have longer target
durations as well, causing the selection to be completely wrong.

Instead prefer using the playlist segment duration information for
non-live playlists, and other means if that is not possible.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: avoid unnecessary segment retrievals

Check if the playlist is still needed just before requesting the next
segment instead of after exhausting the previous segment.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: do not care about stream start timestamps

While selecting a packet to return to caller in read_packet(), the code
corrects the timestamps for starting timestamps.

However, this is wrong, since for live streams the initial timestamps
might differ just because of the time delay between the retrieval of the
various Media Playlists.

Fortunately, spec 6.2.4 mandates that all variant streams must have
matching timestamps, so we do not need to correct for initial
timestamps.

Drop the correction code.

Note that ID3 and WebVTT MPEGTS timestamps were previously ignored, so
this code was previously actually needed.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: do not use sequence numbers for packet ordering

As per spec 3.4.3 ("A client MUST NOT assume that segments with the same
sequence number in different Media Playlists contain matching content.")
we cannot use sequence numbers for packet ordering.

This can be seen e.g. in the subtitle streams of
bipbop_16x9_variant.m3u8 that have considerably longer segments and
therefore different numbering.

Since the code now exclusively syncs using timestamps that may wrap, add
some additional checking for that.

According to the HLS spec all the timestamps should be in 33-bit MPEG
format and synced together.

After this commit (and the preceding commits) HLS WebVTT subtitles
should work properly (ticket #2833).

v2: cleaner wrap detection

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: factor identical playlist allocations out of parse_playlist

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: open playlists immediately when AVDISCARD_ALL is dropped

Do not try to delay opening newly required playlists until a segment
switch. Applications expect that newly selected undiscarded streams are
available immediately, especially with alternative rendition streams
(selectable audio/subtitle tracks).

One might think that delaying variant stream switch until a segment
switch would allow a "seamless" switch without us having to download a
specific segment from two different variant playlists. However, that is
not the case, since the application would have to keep the previous
stream available (undiscarded) until the first packet of the newly
selected stream arrives, but by that time the demuxer would have already
downloaded the next segment of both variants.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: properly take stream_index into account when seeking

Properly take stream_index into account so that a keyframe will be
looked for in the specified stream_index only.

Similarly, only check timestamp validity against the specified
stream_index.

Also remove code for stream_index == -1 case which does not actually
happen as it is handled by generic code.

This is based on an initial patch by James Deng.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: flush the subdemuxer when seeking

Since we are basically seeking the AVIOContext under the subdemuxer, we
need to flush the subdemuxer to avoid old packets from being read from
the packet queue after the seek.

Signed-off-by: Anssi Hannula <anssi.hannula@iki.fi>
avformat/hls: support for seeking on event playlists

This allows clients to seek between first played segment
and the last one in the live stream playlist with