From c64d56a2f53455f803456811873ff08fce98e122 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Tue, 20 Jul 2021 20:07:36 +0800 Subject: [PATCH] avformat/hlsenc: minus subtitle streams count when subtitle stream between video and audio streams because subtitles streams will be written to webvtt m3u8 list so the stream index should minus subtitles streams count when subtitle between audio and video streams. testcase: before patch: ffmpeg -i input -map 0:a:0 -map 0:s:0 -map 0:v:0 -f hls aaaa.m3u8 will EXC_BAD_ACCESS after patch: ffmpeg -i input -map 0:a:0 -map 0:s:0 -map 0:v:0 -f hls aaaa.m3u8 will ok Signed-off-by: Steven Liu --- libavformat/hlsenc.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 8cd938eb97..7c37bc50b9 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -2293,6 +2293,7 @@ static int hls_write_header(AVFormatContext *s) VariantStream *vs = NULL; for (i = 0; i < hls->nb_varstreams; i++) { + int subtitle_streams = 0; vs = &hls->var_streams[i]; ret = avformat_write_header(vs->avf, NULL); @@ -2313,10 +2314,11 @@ static int hls_write_header(AVFormatContext *s) } if (outer_st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) - inner_st = vs->avf->streams[j]; - else if (vs->vtt_avf) + inner_st = vs->avf->streams[j - subtitle_streams]; + else if (vs->vtt_avf) { inner_st = vs->vtt_avf->streams[0]; - else { + subtitle_streams++; + } else { /* We have a subtitle stream, when the user does not want one */ inner_st = NULL; continue; @@ -2403,6 +2405,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) int is_ref_pkt = 1; int ret = 0, can_split = 1, i, j; int stream_index = 0; + int subtitle_streams = 0; int range_length = 0; const char *proto = NULL; int use_temp_file = 0; @@ -2412,13 +2415,16 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) for (i = 0; i < hls->nb_varstreams; i++) { vs = &hls->var_streams[i]; for (j = 0; j < vs->nb_streams; j++) { + if (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { + subtitle_streams++; + } if (vs->streams[j] == st) { if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { oc = vs->vtt_avf; stream_index = 0; } else { oc = vs->avf; - stream_index = j; + stream_index = j - subtitle_streams; } break; } @@ -2647,7 +2653,6 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) if (ret < 0) { return ret; } - } vs->packets_written++;