PR #22238 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22238
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22238.patch

Spotted while wading through the mess that is all of our many divergent plane 
allocation implementations.


>From 0f5e09a5ea99793f6e271565546f80c543627da6 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Fri, 20 Feb 2026 20:43:42 +0100
Subject: [PATCH 1/2] avutil/imgutils: eliminate dead code

This condition is impossible to satisfy:

  if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] &&
      pointers[1] - pointers[0] > linesizes[0] * h)

Specifically, pointers[1] - pointers[0] is equal by definition of
av_image_fill_pointers() to:

  sizes[0] = (size_t)h * linesizes[0]; // via av_image_fill_plane_sizes()
  data[1] = data[0] + sizes[0];

So it's impossible for there to be extra padding between the planes unless
something drastic changes, which is unlikely at this stage.
---
 libavutil/imgutils.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
index 8ff8a6be41..ae671e005f 100644
--- a/libavutil/imgutils.c
+++ b/libavutil/imgutils.c
@@ -261,13 +261,6 @@ int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
         }
     }
 
-    if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] &&
-        pointers[1] - pointers[0] > linesizes[0] * h) {
-        /* zero-initialize the padding before the palette */
-        memset(pointers[0] + linesizes[0] * h, 0,
-               pointers[1] - pointers[0] - linesizes[0] * h);
-    }
-
     return ret;
 }
 
-- 
2.52.0


>From a452b87121e5e81510683859e855708fa697431b Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 21 Feb 2026 12:50:45 +0100
Subject: [PATCH 2/2] avutil/imgutils: always correctly align palette formats

There is literally no reason to error out here when we can just trivially
increase the alignment requirement. As far as I can tell, there is no
wording or documentation for av_image_alloc() that says "align=0" should
guarantee tightly packed data.
---
 libavutil/imgutils.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
index ae671e005f..9ccd62bfa6 100644
--- a/libavutil/imgutils.c
+++ b/libavutil/imgutils.c
@@ -227,6 +227,9 @@ int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
     if (!desc)
         return AVERROR(EINVAL);
 
+    if (desc->flags & AV_PIX_FMT_FLAG_PAL)
+        align = FFMAX(align, sizeof(uint32_t));
+
     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
         return ret;
     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? 
FFALIGN(w, 8) : w)) < 0)
@@ -252,14 +255,8 @@ int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
         av_free(buf);
         return ret;
     }
-    if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
+    if (desc->flags & AV_PIX_FMT_FLAG_PAL)
         avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
-        if (align < 4) {
-            av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a 
minimum alignment of 4\n");
-            av_free(buf);
-            return AVERROR(EINVAL);
-        }
-    }
 
     return ret;
 }
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to