Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Jack Lau via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Jack Lau <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avformat/hlsenc: fix IVs only be auto generated once (PR #21088)
Date: Wed, 03 Dec 2025 12:20:46 -0000
Message-ID: <176476444657.39.16497287036433057657@2cb04c0e5124> (raw)

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 <jacklau1222gm@gmail.com>


>From 2ee8b1596f626d608f6fac50f18af0c5f967010e Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222gm@gmail.com>
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 <jacklau1222gm@gmail.com>
---
 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 -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2025-12-03 12:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=176476444657.39.16497287036433057657@2cb04c0e5124 \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git