Commit 96e9f3ae7d for ffmpeg
commit 96e9f3ae7df7b5dd36eadcfb5f49a8a10918c941
Author: Zhao Zhili <quinkblack@foxmail.com>
Date: Tue Nov 17 17:56:56 2026 +0800
avcodec/zmbv: skip inter frames that only copy their reference
An inter frame with an all-zero motion vector table can share the
reference pixels. Screen capture content produces these frames when
the screen is idle. Fill the output frame's properties from the
current packet and codec context.
Scan the vectors only when there is no XOR block data. A rejected
palette delta can leave the decoder palette newer than the reference
palette, so decode until the updated palette reaches the reference.
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.
diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 4692203aac..4a33d3508d 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -64,6 +64,7 @@ typedef struct ZmbvContext {
unsigned int decomp_size;
uint8_t* decomp_buf;
uint8_t pal[768];
+ int palette_dirty;
AVFrame *prev_frame;
int width, height;
int fmt;
@@ -141,6 +142,7 @@ static int zmbv_decode_xor_8(ZmbvContext *c, uint8_t *output,
if (c->flags & ZMBV_DELTAPAL) {
for (i = 0; i < 768; i++)
c->pal[i] ^= *src++;
+ c->palette_dirty = 1;
}
mvec = (int8_t*)src;
@@ -645,6 +647,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.
+ * Zero vectors let us reuse the reference if its palette is current.
+ * A rejected delta can leave a palette change pending.
+ */
+ if (!(c->flags & (ZMBV_KEYFRAME | ZMBV_DELTAPAL)) &&
+ !c->palette_dirty && 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 = ref_video_buffers(frame, c->prev_frame);
+ if (ret < 0)
+ return ret;
+ ret = ff_decode_frame_props(avctx, frame);
+ if (ret < 0)
+ return ret;
+ ret = ff_attach_decode_data(avctx, frame);
+ if (ret < 0)
+ return ret;
+ 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.
*/
@@ -678,6 +708,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
ret = ref_video_buffers(c->prev_frame, frame);
if (ret < 0)
return ret;
+ c->palette_dirty = 0;
*got_frame = 1;