PR #26139 opened by Zhao Zhili (quink)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26139
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26139.patch

# Summary of changes

- decode directly into the output frame
- skip inter frames that only copy their reference
- inflate keyframes straight into the output frame

Test decoding time takes before and after these 3 commits on ESP32-S3:

| | average (ms) | max (ms) | min (ms) | fps |
| --- | --- | --- | --- | --- |
| Before | 28.602 / 28.604 / 28.642 | 14.15 | 42.85 | 35.0 |
| After | 17.458 / 17.457 / 17.457 | 0.36 | 35.58 | 57.3 |

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From dd05fa756d9d1e3c7f19ae66ff67bd8589765597 Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Sat, 14 Nov 2026 13:32:26 +0800
Subject: [PATCH 1/3] avcodec/zmbv: decode directly into the output frame

The decoder reconstructed each picture in a private buffer and then
copied the whole frame to the output AVFrame. That copy is redundant:
it adds a full-frame pass of memory traffic per frame and keeps two
extra full-frame buffers alive. It hurts most on memory-bandwidth
limited targets such as ESP32-S3 (frame buffers in PSRAM), but removing
it benefits every platform.

x86-64 i7-12700F, ffmpeg -threads 1, whole pipeline:

zmbv_32bit.avi     0.435s -> 0.360s (1.21x)
wc2_001-partial    0.168s -> 0.145s (1.16x)
zmbv_16bit.avi     0.331s -> 0.322s (1.03x)

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

zmbv_32bit.avi     27042.7us -> 15776.5us (1.71x)
wc2_001-partial     7192.2us ->  4212.5us (1.71x)
zmbv_16bit.avi     13840.2us ->  8075.0us (1.71x)
zmbv_15bit.avi     13821.3us ->  8085.9us (1.71x)
---
 libavcodec/zmbv.c | 196 +++++++++++++++++++++++-----------------------
 1 file changed, 97 insertions(+), 99 deletions(-)

diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 3e2e7fa98a9..ab3f6682b04 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -28,7 +28,6 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "avcodec.h"
@@ -60,11 +59,10 @@ typedef struct ZmbvContext {
     AVCodecContext *avctx;
 
     int bpp;
-    int alloc_bpp;
     unsigned int decomp_size;
     uint8_t* decomp_buf;
     uint8_t pal[768];
-    uint8_t *prev, *cur;
+    AVFrame *prev_frame;
     int width, height;
     int fmt;
     int comp;
@@ -74,17 +72,20 @@ typedef struct ZmbvContext {
     int decomp_len;
     int got_keyframe;
     FFZStream zstream;
-    int (*decode_xor)(struct ZmbvContext *c);
+    int (*decode_xor)(struct ZmbvContext *c, uint8_t *output,
+                      ptrdiff_t out_linesize, const uint8_t *prev,
+                      ptrdiff_t prev_linesize);
 } ZmbvContext;
 
 /**
  * Decode XOR'ed frame - 8bpp version
  */
 
-static int zmbv_decode_xor_8(ZmbvContext *c)
+static int zmbv_decode_xor_8(ZmbvContext *c, uint8_t *output,
+                             ptrdiff_t out_linesize, const uint8_t *prev,
+                             ptrdiff_t prev_linesize)
 {
     uint8_t *src = c->decomp_buf;
-    uint8_t *output, *prev;
     int8_t *mvec;
     int x, y;
     int d, dx, dy, bw2, bh2;
@@ -92,9 +93,6 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
     int i, j;
     int mx, my;
 
-    output = c->cur;
-    prev = c->prev;
-
     if (c->flags & ZMBV_DELTAPAL) {
         for (i = 0; i < 768; i++)
             c->pal[i] ^= *src++;
@@ -107,7 +105,8 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
     for (y = 0; y < c->height; y += c->bh) {
         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
         for (x = 0; x < c->width; x += c->bw) {
-            uint8_t *out, *tprev;
+            uint8_t *out;
+            const uint8_t *tprev;
 
             d = mvec[block] & 1;
             dx = mvec[block] >> 1;
@@ -118,7 +117,7 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
 
             /* copy block - motion vectors out of bounds are used to zero 
blocks */
             out = output + x;
-            tprev = prev + x + dx + dy * c->width;
+            tprev = prev + x + dx + dy * prev_linesize;
             mx = x + dx;
             my = y + dy;
             for (j = 0; j < bh2; j++) {
@@ -134,8 +133,8 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
                             out[i] = tprev[i];
                     }
                 }
-                out += c->width;
-                tprev += c->width;
+                out += out_linesize;
+                tprev += prev_linesize;
             }
 
             if (d) { /* apply XOR'ed difference */
@@ -145,12 +144,12 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
                 for (j = 0; j < bh2; j++) {
                     for (i = 0; i < bw2; i++)
                         out[i] ^= *src++;
-                    out += c->width;
+                    out += out_linesize;
                 }
             }
         }
-        output += c->width * c->bh;
-        prev += c->width * c->bh;
+        output += out_linesize * c->bh;
+        prev += prev_linesize * c->bh;
     }
     if (src - c->decomp_buf != c->decomp_len)
         av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
@@ -162,10 +161,15 @@ static int zmbv_decode_xor_8(ZmbvContext *c)
  * Decode XOR'ed frame - 15bpp and 16bpp version
  */
 
-static int zmbv_decode_xor_16(ZmbvContext *c)
+static int zmbv_decode_xor_16(ZmbvContext *c, uint8_t *output,
+                              ptrdiff_t out_linesize, const uint8_t *prev,
+                              ptrdiff_t prev_linesize)
 {
     uint8_t *src = c->decomp_buf;
-    uint16_t *output, *prev;
+    uint16_t *dst = (uint16_t *)output;
+    const uint16_t *ref = (const uint16_t *)prev;
+    ptrdiff_t out_step = out_linesize / 2;
+    ptrdiff_t prev_step = prev_linesize / 2;
     int8_t *mvec;
     int x, y;
     int d, dx, dy, bw2, bh2;
@@ -173,9 +177,6 @@ static int zmbv_decode_xor_16(ZmbvContext *c)
     int i, j;
     int mx, my;
 
-    output = (uint16_t*)c->cur;
-    prev = (uint16_t*)c->prev;
-
     mvec = (int8_t*)src;
     src += ((c->bx * c->by * 2 + 3) & ~3);
 
@@ -183,7 +184,8 @@ static int zmbv_decode_xor_16(ZmbvContext *c)
     for (y = 0; y < c->height; y += c->bh) {
         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
         for (x = 0; x < c->width; x += c->bw) {
-            uint16_t *out, *tprev;
+            uint16_t *out;
+            const uint16_t *tprev;
 
             d = mvec[block] & 1;
             dx = mvec[block] >> 1;
@@ -193,8 +195,8 @@ static int zmbv_decode_xor_16(ZmbvContext *c)
             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
 
             /* copy block - motion vectors out of bounds are used to zero 
blocks */
-            out = output + x;
-            tprev = prev + x + dx + dy * c->width;
+            out = dst + x;
+            tprev = ref + x + dx + dy * prev_step;
             mx = x + dx;
             my = y + dy;
             for (j = 0; j < bh2; j++) {
@@ -210,25 +212,25 @@ static int zmbv_decode_xor_16(ZmbvContext *c)
                             out[i] = tprev[i];
                     }
                 }
-                out += c->width;
-                tprev += c->width;
+                out += out_step;
+                tprev += prev_step;
             }
 
             if (d) { /* apply XOR'ed difference */
                 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2 * 2)
                     return AVERROR_INVALIDDATA;
-                out = output + x;
+                out = dst + x;
                 for (j = 0; j < bh2; j++){
                     for (i = 0; i < bw2; i++) {
                         out[i] ^= *((uint16_t*)src);
                         src += 2;
                     }
-                    out += c->width;
+                    out += out_step;
                 }
             }
         }
-        output += c->width * c->bh;
-        prev += c->width * c->bh;
+        dst += out_step * c->bh;
+        ref += prev_step * c->bh;
     }
     if (src - c->decomp_buf != c->decomp_len)
         av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
@@ -241,22 +243,20 @@ static int zmbv_decode_xor_16(ZmbvContext *c)
  * Decode XOR'ed frame - 24bpp version
  */
 
-static int zmbv_decode_xor_24(ZmbvContext *c)
+static int zmbv_decode_xor_24(ZmbvContext *c, uint8_t *output,
+                              ptrdiff_t out_linesize, const uint8_t *prev,
+                              ptrdiff_t prev_linesize)
 {
     uint8_t *src = c->decomp_buf;
-    uint8_t *output, *prev;
+    ptrdiff_t stride = out_linesize;
+    ptrdiff_t pstride = prev_linesize;
     int8_t *mvec;
     int x, y;
     int d, dx, dy, bw2, bh2;
     int block;
     int i, j;
     int mx, my;
-    int stride;
 
-    output = c->cur;
-    prev = c->prev;
-
-    stride = c->width * 3;
     mvec = (int8_t*)src;
     src += ((c->bx * c->by * 2 + 3) & ~3);
 
@@ -264,7 +264,8 @@ static int zmbv_decode_xor_24(ZmbvContext *c)
     for (y = 0; y < c->height; y += c->bh) {
         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
         for (x = 0; x < c->width; x += c->bw) {
-            uint8_t *out, *tprev;
+            uint8_t *out;
+            const uint8_t *tprev;
 
             d = mvec[block] & 1;
             dx = mvec[block] >> 1;
@@ -275,7 +276,7 @@ static int zmbv_decode_xor_24(ZmbvContext *c)
 
             /* copy block - motion vectors out of bounds are used to zero 
blocks */
             out = output + x * 3;
-            tprev = prev + (x + dx) * 3 + dy * stride;
+            tprev = prev + (x + dx) * 3 + dy * pstride;
             mx = x + dx;
             my = y + dy;
             for (j = 0; j < bh2; j++) {
@@ -297,7 +298,7 @@ static int zmbv_decode_xor_24(ZmbvContext *c)
                     }
                 }
                 out += stride;
-                tprev += stride;
+                tprev += pstride;
             }
 
             if (d) { /* apply XOR'ed difference */
@@ -315,7 +316,7 @@ static int zmbv_decode_xor_24(ZmbvContext *c)
             }
         }
         output += stride * c->bh;
-        prev += stride * c->bh;
+        prev += pstride * c->bh;
     }
     if (src - c->decomp_buf != c->decomp_len)
         av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
@@ -328,10 +329,15 @@ static int zmbv_decode_xor_24(ZmbvContext *c)
  * Decode XOR'ed frame - 32bpp version
  */
 
-static int zmbv_decode_xor_32(ZmbvContext *c)
+static int zmbv_decode_xor_32(ZmbvContext *c, uint8_t *output,
+                              ptrdiff_t out_linesize, const uint8_t *prev,
+                              ptrdiff_t prev_linesize)
 {
     uint8_t *src = c->decomp_buf;
-    uint32_t *output, *prev;
+    uint32_t *dst = (uint32_t *)output;
+    const uint32_t *ref = (const uint32_t *)prev;
+    ptrdiff_t out_step = out_linesize / 4;
+    ptrdiff_t prev_step = prev_linesize / 4;
     int8_t *mvec;
     int x, y;
     int d, dx, dy, bw2, bh2;
@@ -339,9 +345,6 @@ static int zmbv_decode_xor_32(ZmbvContext *c)
     int i, j;
     int mx, my;
 
-    output = (uint32_t*)c->cur;
-    prev = (uint32_t*)c->prev;
-
     mvec = (int8_t*)src;
     src += ((c->bx * c->by * 2 + 3) & ~3);
 
@@ -349,7 +352,8 @@ static int zmbv_decode_xor_32(ZmbvContext *c)
     for (y = 0; y < c->height; y += c->bh) {
         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
         for (x = 0; x < c->width; x += c->bw) {
-            uint32_t *out, *tprev;
+            uint32_t *out;
+            const uint32_t *tprev;
 
             d = mvec[block] & 1;
             dx = mvec[block] >> 1;
@@ -359,8 +363,8 @@ static int zmbv_decode_xor_32(ZmbvContext *c)
             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
 
             /* copy block - motion vectors out of bounds are used to zero 
blocks */
-            out = output + x;
-            tprev = prev + x + dx + dy * c->width;
+            out = dst + x;
+            tprev = ref + x + dx + dy * prev_step;
             mx = x + dx;
             my = y + dy;
             for (j = 0; j < bh2; j++) {
@@ -376,25 +380,25 @@ static int zmbv_decode_xor_32(ZmbvContext *c)
                             out[i] = tprev[i];
                     }
                 }
-                out += c->width;
-                tprev += c->width;
+                out += out_step;
+                tprev += prev_step;
             }
 
             if (d) { /* apply XOR'ed difference */
                 if (c->decomp_len - (src - c->decomp_buf) < bw2 * bh2 * 4)
                     return AVERROR_INVALIDDATA;
-                out = output + x;
+                out = dst + x;
                 for (j = 0; j < bh2; j++){
                     for (i = 0; i < bw2; i++) {
                         out[i] ^= *((uint32_t *) src);
                         src += 4;
                     }
-                    out += c->width;
+                    out += out_step;
                 }
             }
         }
-        output += c->width * c->bh;
-        prev   += c->width * c->bh;
+        dst += out_step * c->bh;
+        ref += prev_step * c->bh;
     }
     if (src - c->decomp_buf != c->decomp_len)
         av_log(c->avctx, AV_LOG_ERROR, "Used %td of %i bytes\n",
@@ -405,9 +409,11 @@ static int zmbv_decode_xor_32(ZmbvContext *c)
 /**
  * Decode intraframe
  */
-static int zmbv_decode_intra(ZmbvContext *c)
+static int zmbv_decode_intra(ZmbvContext *c, uint8_t *dst,
+                             ptrdiff_t dst_linesize)
 {
     uint8_t *src = c->decomp_buf;
+    int y;
 
     /* make the palette available on the way out */
     if (c->fmt == ZMBV_FMT_8BPP) {
@@ -415,7 +421,11 @@ static int zmbv_decode_intra(ZmbvContext *c)
         src += 768;
     }
 
-    memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
+    for (y = 0; y < c->height; y++) {
+        memcpy(dst, src, c->stride);
+        dst += dst_linesize;
+        src += c->stride;
+    }
     return 0;
 }
 
@@ -509,19 +519,12 @@ static int decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
             return AVERROR_UNKNOWN;
         }
 
-        if (c->alloc_bpp < c->bpp) {
-            c->cur  = av_realloc_f(c->cur, avctx->width * avctx->height,  
(c->bpp / 8));
-            c->prev = av_realloc_f(c->prev, avctx->width * avctx->height,  
(c->bpp / 8));
-            c->alloc_bpp = c->bpp;
-        }
         c->bx = (c->width + c->bw - 1) / c->bw;
         c->by = (c->height+ c->bh - 1) / c->bh;
-        if (!c->cur || !c->prev) {
-            c->alloc_bpp = 0;
-            return AVERROR(ENOMEM);
-        }
-        memset(c->cur, 0, avctx->width * avctx->height * (c->bpp / 8));
-        memset(c->prev, 0, avctx->width * avctx->height * (c->bpp / 8));
+        /* A keyframe may change the pixel format; drop the reference frame
+         * so a P frame never references a frame in a different format.
+         */
+        av_frame_unref(c->prev_frame);
         c->got_keyframe = 1;
     }
     if (c->flags & ZMBV_KEYFRAME) {
@@ -537,6 +540,10 @@ static int decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
         av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
         return AVERROR_INVALIDDATA;
     }
+    if (!(c->flags & ZMBV_KEYFRAME) && !c->prev_frame->data[0]) {
+        av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
+        return AVERROR_INVALIDDATA;
+    }
 
     if (c->comp == 0) { // uncompressed data
         if (c->decomp_size < len) {
@@ -565,50 +572,39 @@ static int decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
         av_log(avctx, AV_LOG_ERROR, "decompressed size %d is incorrect, 
expected %d\n", c->decomp_len, expected_size);
         return AVERROR_INVALIDDATA;
     }
-    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+
+    /* Decode directly into the output frame. It is kept as the reference
+     * frame for the next P frame, hence the reference flag.
+     */
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 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);
+        zmbv_decode_intra(c, frame->data[0], frame->linesize[0]);
     } else {
         frame->flags &= ~AV_FRAME_FLAG_KEY;
         frame->pict_type = AV_PICTURE_TYPE_P;
         if (c->decomp_len < 2LL * ((c->width + c->bw - 1) / c->bw) * 
((c->height + c->bh - 1) / c->bh))
             return AVERROR_INVALIDDATA;
         if (c->decomp_len) {
-            if ((ret = c->decode_xor(c)) < 0)
+            if ((ret = c->decode_xor(c, frame->data[0], frame->linesize[0],
+                                     c->prev_frame->data[0],
+                                     c->prev_frame->linesize[0])) < 0)
                 return ret;
         }
     }
 
-    /* update frames */
-    {
-        uint8_t *out, *src;
-        int j;
-
-        out = frame->data[0];
-        src = c->cur;
-        switch (c->fmt) {
-        case ZMBV_FMT_8BPP:
-            for (j = 0; j < 256; j++)
-                AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | 
AV_RB24(&c->pal[j * 3]));
-            av_fallthrough;
-        case ZMBV_FMT_15BPP:
-        case ZMBV_FMT_16BPP:
-#ifdef ZMBV_ENABLE_24BPP
-        case ZMBV_FMT_24BPP:
-#endif
-        case ZMBV_FMT_32BPP:
-            av_image_copy_plane(out, frame->linesize[0], src, c->stride,
-                                c->stride, c->height);
-            break;
-        default:
-            av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
-        }
-        FFSWAP(uint8_t *, c->cur, c->prev);
+    if (c->fmt == ZMBV_FMT_8BPP) {
+        for (int j = 0; j < 256; j++)
+            AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 
3]));
     }
+
+    ret = av_frame_replace(c->prev_frame, frame);
+    if (ret < 0)
+        return ret;
+
     *got_frame = 1;
 
     /* always report that the buffer was completely consumed */
@@ -641,6 +637,10 @@ static av_cold int decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
+    c->prev_frame = av_frame_alloc();
+    if (!c->prev_frame)
+        return AVERROR(ENOMEM);
+
     return ff_inflate_init(&c->zstream, avctx);
 }
 
@@ -649,9 +649,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
     ZmbvContext * const c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
-
-    av_freep(&c->cur);
-    av_freep(&c->prev);
+    av_frame_free(&c->prev_frame);
     ff_inflate_end(&c->zstream);
 
     return 0;
-- 
2.54.0


>From 9448e8162d6b49551eaba0ba2566d0b112f9b79a Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Tue, 17 Nov 2026 17:56:56 +0800
Subject: [PATCH 2/3] avcodec/zmbv: skip inter frames that only copy their
 reference

An inter frame with an all-zero motion vector table and no palette
delta equals its reference frame, so return the reference instead of
running the motion compensation pass. Screen capture content produces
such frames whenever the screen is idle.

Block differences are appended after the table, so only a payload that
is exactly the table can be all zero. Only those payloads are scanned,
with an early exit on the first non-zero byte. The decoded output is
unchanged.

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

wc2_001-partial.avi    4253.5us ->  1869.0us (2.28x)
zmbv_15bit.avi         7771.4us ->  3882.6us (2.00x)
zmbv_16bit.avi         7696.0us ->  3436.1us (2.24x)
zmbv_32bit.avi        14545.8us ->  9356.1us (1.56x)

x86-64 Core Ultra 7 265, decoder calls only, per frame:

wc2_001-partial.avi     10.96us ->   6.36us (1.72x)
zmbv_15bit.avi          17.84us ->  14.15us (1.26x)
zmbv_16bit.avi          16.19us ->  12.17us (1.33x)
zmbv_32bit.avi          24.44us ->  21.65us (1.13x)

Fully animated content is unaffected: a 320x240 16bpp clip takes
21494.3 -> 21505.2 us per frame on ESP32-S3 and 230.7 -> 231.3 us on
x86-64.
---
 libavcodec/zmbv.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index ab3f6682b04..2d7471671f1 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -573,6 +573,34 @@ static int decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
         return AVERROR_INVALIDDATA;
     }
 
+    /* A payload that is only the motion vector table has no block data.
+     * If the vectors are all zero and there is no palette delta, the frame
+     * equals its reference, so output the reference instead of decoding.
+     */
+    if (!(c->flags & (ZMBV_KEYFRAME | ZMBV_DELTAPAL)) &&
+        c->decomp_len == expected_size) {
+        const uint8_t *mvec = c->decomp_buf;
+        int i, n = c->bx * c->by * 2;
+
+        for (i = 0; i < n; i++)
+            if (mvec[i])
+                break;
+        if (i == n) {
+            ret = av_frame_ref(frame, c->prev_frame);
+            if (ret < 0)
+                return ret;
+            av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
+            av_dict_free(&frame->metadata);
+            ret = ff_decode_frame_props(avctx, frame);
+            if (ret < 0)
+                return ret;
+            frame->flags &= ~(AV_FRAME_FLAG_KEY | AV_FRAME_FLAG_DISCARD);
+            frame->pict_type = AV_PICTURE_TYPE_P;
+            *got_frame = 1;
+            return buf_size;
+        }
+    }
+
     /* Decode directly into the output frame. It is kept as the reference
      * frame for the next P frame, hence the reference flag.
      */
-- 
2.54.0


>From 965727bd0fbc2b9c4e74ead11a6e88dedfdb6712 Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Tue, 17 Nov 2026 20:53:20 +0800
Subject: [PATCH 3/3] avcodec/zmbv: inflate keyframes straight into the output
 frame

Inflate a packed, non-palette keyframe directly into the frame buffer
instead of the scratch buffer followed by a row-by-row copy. Other
keyframes keep using the scratch buffer. The decoded output is
unchanged.

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

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

x86-64 shows no measurable change.
---
 libavcodec/zmbv.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 2d7471671f1..aae876afc55 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -439,6 +439,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)
@@ -554,17 +555,39 @@ 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 = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
+            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) {
+            /* Any further output means the payload exceeds the frame. */
+            zstream->next_out  = c->decomp_buf;
+            zstream->avail_out = 1;
+            inflate(zstream, Z_SYNC_FLUSH);
+        }
         c->decomp_len = zstream->total_out;
     }
     if (expected_size > c->decomp_len ||
@@ -604,13 +627,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 = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+    if (!frame->buf[0] &&
+        (ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 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;
-- 
2.54.0

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

Reply via email to