Commit 3762e82d91 for ffmpeg

commit 3762e82d91585e1faae828b4bab04e2ae7fc47ea
Author: Zhao Zhili <quinkblack@foxmail.com>
Date:   Tue Nov 17 20:53:20 2026 +0800

    avcodec/zmbv: inflate keyframes straight into the output frame

    Inflate a packed, non-palette keyframe directly into the frame buffer
    to avoid a row-by-row copy from the scratch buffer. Other keyframes
    use the scratch buffer.

    Probe for excess output when the frame buffer fills before zlib
    reports the end of the stream. Reject fatal inflate errors even when
    the probe produces no output.

    ESP32-S3 240 MHz, decoder calls only, per frame:

    keyframe, 320x240 16bpp        16245.0us ->  9502.0us (1.71x)

    x86-64 shows no measurable change.

diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 783553ca6b..4692203aac 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -484,6 +484,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
     int len = buf_size;
     int hi_ver, lo_ver, ret;
     int expected_size;
+    int direct = 0;

     /* parse header */
     if (len < 1)
@@ -599,17 +600,43 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
         c->decomp_len = len;
     } else { // ZLIB-compressed data
         z_stream *const zstream = &c->zstream.zstream;
+        uint8_t *out = c->decomp_buf;
+        int out_size = c->decomp_size;
+
+        /* A packed keyframe without a palette is inflated straight into
+         * the output frame, which saves the copy from decomp_buf.
+         */
+        if ((c->flags & ZMBV_KEYFRAME) && c->fmt != ZMBV_FMT_8BPP) {
+            ret = get_video_buffer(avctx, frame);
+            if (ret < 0)
+                return ret;
+            if (frame->linesize[0] == c->stride) {
+                direct   = 1;
+                out      = frame->data[0];
+                out_size = expected_size;
+            }
+        }

         zstream->total_in  = zstream->total_out = 0;
         zstream->next_in   = buf;
         zstream->avail_in  = len;
-        zstream->next_out  = c->decomp_buf;
-        zstream->avail_out = c->decomp_size;
+        zstream->next_out  = out;
+        zstream->avail_out = out_size;
         zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK && zret != Z_STREAM_END) {
             av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
             return AVERROR_INVALIDDATA;
         }
+        if (direct && !zstream->avail_out && zret != Z_STREAM_END) {
+            /* Any further output means the payload exceeds the frame. */
+            zstream->next_out  = c->decomp_buf;
+            zstream->avail_out = 1;
+            zret = inflate(zstream, Z_SYNC_FLUSH);
+            if (zret != Z_OK && zret != Z_STREAM_END && zret != Z_BUF_ERROR) {
+                av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
+                return AVERROR_INVALIDDATA;
+            }
+        }
         c->decomp_len = zstream->total_out;
     }
     if (expected_size > c->decomp_len ||
@@ -621,13 +648,15 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
     /* Decode directly into the output frame. It is kept as the reference
      * frame for the next P frame, hence the reference flag.
      */
-    if ((ret = get_video_buffer(avctx, frame)) < 0)
+    if (!frame->buf[0] &&
+        (ret = get_video_buffer(avctx, frame)) < 0)
         return ret;

     if (c->flags & ZMBV_KEYFRAME) {
         frame->flags |= AV_FRAME_FLAG_KEY;
         frame->pict_type = AV_PICTURE_TYPE_I;
-        zmbv_decode_intra(c, frame->data[0], frame->linesize[0]);
+        if (!direct)
+            zmbv_decode_intra(c, frame->data[0], frame->linesize[0]);
     } else {
         frame->flags &= ~AV_FRAME_FLAG_KEY;
         frame->pict_type = AV_PICTURE_TYPE_P;