Commit 0f32473571 for ffmpeg

commit 0f324735715d4f75407b320fad6a3a884bfda39a
Author: Michael Niedermayer <michael@niedermayer.cc>
Date:   Wed Nov 25 09:37:55 2026 +0000

    avcodec/libjxlenc: pass the actual cropped frame buffer size

    A cropped frame can start inside its backing allocation while retaining
    the original stride. Passing stride times height makes libjxl copy beyond
    the allocation when the final row does not contain a full stride. Pass
    the span through the active pixels of the final row for both encoders.

    Reuse the checked row offset to locate the first row in memory order
    for negative strides, rather than multiplying the stride and height in
    signed int arithmetic.

    Fixes: #26292
    Reported-by: T-Dynamos
    Assisted-by: Fairy

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index e5b09c0815..b0f6f1c3de 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -32,6 +32,7 @@
 #include "libavutil/display.h"
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
@@ -546,16 +547,17 @@ end:
  * this is evaluated once per output file. If this is an animated JPEG XL encode, it
  * is called once per frame.
  *
- * This returns a buffer to the data that should be passed to libjxl (via the
- * argument **data). If the linesize is nonnegative, this will be frame->data[0],
- * although if the linesize is negative, it will be the start of the buffer
- * instead. *data is just a pointer to a location in frame->data so it should not be
- * freed directly.
+ * On success, *data points to the first row in memory order and *size is the
+ * span through the active pixels of the last row. The pointer aliases the
+ * frame buffer and must not be freed.
  */
-static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame, const uint8_t **data)
+static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame,
+                                   const uint8_t **data, size_t *size)
 {
     LibJxlEncodeContext *ctx = avctx->priv_data;
     JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
+    size_t offset;
+    int row_size, ret;

     /* these shouldn't fail, libjxl bug notwithstanding */
     if (JxlEncoderFrameSettingsSetOption(ctx->options, JXL_ENC_FRAME_SETTING_EFFORT, ctx->effort)
@@ -580,14 +582,29 @@ static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame,
         return AVERROR_EXTERNAL;
     }

+    row_size = av_image_get_linesize(frame->format, frame->width, 0);
+    if (row_size < 0)
+        return row_size;
+    if (frame->height <= 0 || frame->linesize[0] == INT_MIN)
+        return AVERROR(EINVAL);
+
+    jxl_fmt->align = FFABS(frame->linesize[0]);
+    if (jxl_fmt->align < (size_t)row_size)
+        return AVERROR(EINVAL);
+
+    /* libjxl needs only the active pixels of the final row. A cropped frame
+     * may not have a full stride of allocated memory after that row. */
+    ret = av_size_mult(jxl_fmt->align, frame->height - 1, &offset);
+    if (ret < 0)
+        return ret;
+    if (offset > SIZE_MAX - row_size)
+        return AVERROR(EINVAL);
+
     jxl_fmt->endianness = JXL_NATIVE_ENDIAN;
-    if (frame->linesize[0] >= 0) {
-        jxl_fmt->align = frame->linesize[0];
-        *data = frame->data[0];
-    } else {
-        jxl_fmt->align = -frame->linesize[0];
-        *data = frame->data[0] + frame->linesize[0] * (frame->height - 1);
-    }
+    *size = offset + row_size;
+    *data = frame->data[0];
+    if (frame->linesize[0] < 0)
+        *data -= offset;

     return 0;
 }
@@ -646,7 +663,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra

     LibJxlEncodeContext *ctx = avctx->priv_data;
     int ret;
-    size_t bytes_written = 0;
+    size_t bytes_written = 0, input_size;
     const uint8_t *data;

     ret = libjxl_init_jxl_encoder(avctx);
@@ -659,11 +676,11 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     if (ret < 0)
         return ret;

-    ret = libjxl_preprocess_frame(avctx, frame, &data);
+    ret = libjxl_preprocess_frame(avctx, frame, &data, &input_size);
     if (ret < 0)
         return ret;

-    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, ctx->jxl_fmt.align * frame->height)
+    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, input_size)
             != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
         return AVERROR_EXTERNAL;
@@ -703,7 +720,7 @@ static int libjxl_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt)
     LibJxlEncodeContext *ctx = avctx->priv_data;
     int ret = 0;
     JxlFrameHeader frame_header;
-    size_t bytes_written = 0;
+    size_t bytes_written = 0, input_size;
     const uint8_t *data;

     if (!ctx->prev) {
@@ -743,11 +760,11 @@ static int libjxl_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt)
         return AVERROR_EXTERNAL;
     }

-    ret = libjxl_preprocess_frame(avctx, ctx->prev, &data);
+    ret = libjxl_preprocess_frame(avctx, ctx->prev, &data, &input_size);
     if (ret < 0)
         return ret;

-    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, ctx->jxl_fmt.align * ctx->prev->height)
+    if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, input_size)
             != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
         return AVERROR_EXTERNAL;