PR #21088 opened by Jack Lau (JackLau) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21088 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21088.patch
Fix #20970 In previous code, it generates IV for first segment by its sequence number when the IV don't be provided. But it's wrong that use the first generated IV for every segments. Refer to RFC 8216 section 5.2 and 4.3.2.4, - EXT-X-KEY tag with a KEYFORMAT of "identity" that does not have an IV attribute indicates that the Media Sequence Number is to be used as the IV. - KEYFORMAT attribute is OPTIONAL; its absence indicates an implicit value of "identity". - The EXT-X-KEY tag applies to every Media Segment between it and the next EXT-X-KEY tag in the Playlist file with the same KEYFORMAT attribute (or the end of the Playlist file). This patch adds auto_iv variable to record whether we should auto generate IV for every segments. If the auto_iv is enabled, this patch does these changes: 1. IVs will be auto generated for every segments 2. Omit IV info in EXT-X-KEY tag 3. Only show the EXT-X-KEY tag once for these segments Example: command: ffmpeg -i input.mp4 -f hls -hls_key_info_file enc.keyinfo -hls_segment_filename chunk_%06d.ts -y playlist.m3u8 m3u8: #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:6 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-KEY:METHOD=AES-128,URI="http://localhost:8000/enc.key" #EXTINF:4.266667, chunk_000000.ts #EXTINF:5.733333, chunk_000001.ts #EXT-X-ENDLIST Signed-off-by: Jack Lau <[email protected]> >From 2ee8b1596f626d608f6fac50f18af0c5f967010e Mon Sep 17 00:00:00 2001 From: Jack Lau <[email protected]> Date: Wed, 3 Dec 2025 17:22:27 +0800 Subject: [PATCH] avformat/hlsenc: fix IVs only be auto generated once Fix #20970 In previous code, it generates IV for first segment by its sequence number when the IV don't be provided. But it's wrong that use the first generated IV for every segments. Refer to RFC 8216 section 5.2 and 4.3.2.4, - EXT-X-KEY tag with a KEYFORMAT of "identity" that does not have an IV attribute indicates that the Media Sequence Number is to be used as the IV. - KEYFORMAT attribute is OPTIONAL; its absence indicates an implicit value of "identity". - The EXT-X-KEY tag applies to every Media Segment between it and the next EXT-X-KEY tag in the Playlist file with the same KEYFORMAT attribute (or the end of the Playlist file). This patch adds auto_iv variable to record whether we should auto generate IV for every segments. If the auto_iv is enabled, this patch does these changes: 1. IVs will be auto generated for every segments 2. Omit IV info in EXT-X-KEY tag 3. Only show the EXT-X-KEY tag once for these segments Example: command: ffmpeg -i input.mp4 -f hls -hls_key_info_file enc.keyinfo -hls_segment_filename chunk_%06d.ts -y playlist.m3u8 m3u8: #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:6 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-KEY:METHOD=AES-128,URI="http://localhost:8000/enc.key" #EXTINF:4.266667, chunk_000000.ts #EXTINF:5.733333, chunk_000001.ts #EXT-X-ENDLIST Signed-off-by: Jack Lau <[email protected]> --- libavformat/hlsenc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 7105404d1e..7a9161808d 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -229,6 +229,7 @@ typedef struct HLSContext { char *iv; char *key_basename; int encrypt_started; + int auto_iv; char *key_info_file; char key_file[LINE_BUFFER_SIZE + 1]; @@ -664,6 +665,7 @@ static int do_encrypt(AVFormatContext *s, VariantStream *vs) uint8_t iv[16] = { 0 }; char buf[33]; + hls->auto_iv = !hls->iv ? 1 : 0; if (!hls->iv) { AV_WB64(iv + 8, vs->sequence); } else { @@ -734,6 +736,8 @@ static int hls_encryption_start(AVFormatContext *s, VariantStream *vs) ff_get_line(pb, vs->iv_string, sizeof(vs->iv_string)); vs->iv_string[strcspn(vs->iv_string, "\r\n")] = '\0'; + hls->auto_iv = !*vs->iv_string ? 1 : 0; + ff_format_io_close(s, &pb); if (!*vs->key_uri) { @@ -1585,9 +1589,9 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs) } for (en = vs->segments; en; en = en->next) { if ((hls->encrypt || hls->key_info_file) && (!key_uri || strcmp(en->key_uri, key_uri) || - av_strcasecmp(en->iv_string, iv_string))) { + (av_strcasecmp(en->iv_string, iv_string) && !hls->auto_iv))) { avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"", en->key_uri); - if (*en->iv_string) + if (*en->iv_string && !hls->auto_iv) avio_printf(byterange_mode ? hls->m3u8_out : vs->out, ",IV=0x%s", en->iv_string); avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "\n"); key_uri = en->key_uri; @@ -1779,7 +1783,7 @@ static int hls_start(AVFormatContext *s, VariantStream *vs) vs->encrypt_started = 1; } err = av_strlcpy(iv_string, vs->iv_string, sizeof(iv_string)); - if (!err) { + if (!err || c->auto_iv) { snprintf(iv_string, sizeof(iv_string), "%032"PRIx64, vs->sequence); memset(vs->iv_string, 0, sizeof(vs->iv_string)); memcpy(vs->iv_string, iv_string, sizeof(iv_string)); -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
