From: mkver via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: mkver <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PR] Reduce size of snow allocations (PR #21793)
Date: Thu, 19 Feb 2026 11:36:03 -0000
Message-ID: <177150096419.25.13741924957209540492@29965ddac10e> (raw)
PR #21793 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21793
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21793.patch
>From 9a1d99a7506414045c6e2b0f5433caa33370bb21 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 9 Feb 2026 14:33:01 +0100
Subject: [PATCH 1/2] avcodec/snow: Only allocate emu_edge_buffer for encoder
Also allocate it during init and move it to the encoder's context.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/snow.c | 6 +-----
libavcodec/snow.h | 1 -
libavcodec/snowenc.c | 9 ++++++++-
3 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index 006d84d8ce..094b893c96 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -540,10 +540,7 @@ int ff_snow_common_init_after_header(AVCodecContext *avctx) {
int plane_index, level, orientation;
if(!s->scratchbuf) {
- int emu_buf_size;
- emu_buf_size = FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
- if (!FF_ALLOCZ_TYPED_ARRAY(s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * 7 * MB_SIZE) ||
- !FF_ALLOCZ_TYPED_ARRAY(s->emu_edge_buffer, emu_buf_size))
+ if (!FF_ALLOCZ_TYPED_ARRAY(s->scratchbuf, FFMAX(s->mconly_picture->linesize[0], 2*avctx->width+256) * 7 * MB_SIZE))
return AVERROR(ENOMEM);
}
@@ -642,7 +639,6 @@ av_cold void ff_snow_common_end(SnowContext *s)
av_freep(&s->block);
av_freep(&s->scratchbuf);
- av_freep(&s->emu_edge_buffer);
for(i=0; i<MAX_REF_FRAMES; i++){
av_frame_free(&s->last_picture[i]);
diff --git a/libavcodec/snow.h b/libavcodec/snow.h
index 83dc6c1256..82417c3324 100644
--- a/libavcodec/snow.h
+++ b/libavcodec/snow.h
@@ -168,7 +168,6 @@ typedef struct SnowContext{
slice_buffer sb;
uint8_t *scratchbuf;
- uint8_t *emu_edge_buffer;
AVMotionVector *avmv;
unsigned avmv_size;
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index 20a41f11a4..f34130f184 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -68,6 +68,8 @@ typedef struct SnowEncContext {
uint64_t encoding_error[SNOW_MAX_PLANES];
+ uint8_t *emu_edge_buffer;
+
IDWTELEM obmc_scratchpad[MB_SIZE * MB_SIZE * 12 * 2];
} SnowEncContext;
@@ -286,6 +288,10 @@ static av_cold int encode_init(AVCodecContext *avctx)
if ((ret = get_encode_buffer(s, s->input_picture)) < 0)
return ret;
+ enc->emu_edge_buffer = av_calloc(avctx->width + 128, 2 * (2 * MB_SIZE + HTAPS_MAX - 1));
+ if (!enc->emu_edge_buffer)
+ return AVERROR(ENOMEM);
+
if (enc->motion_est == FF_ME_ITER) {
int size= s->b_width * s->b_height << 2*s->block_max_depth;
for(i=0; i<s->max_ref_frames; i++){
@@ -770,7 +776,7 @@ static int get_block_rd(SnowEncContext *enc, int mb_x, int mb_y,
const uint8_t *src = s->input_picture->data[plane_index];
IDWTELEM *pred = enc->obmc_scratchpad + plane_index * block_size * block_size * 4;
uint8_t *cur = s->scratchbuf;
- uint8_t *tmp = s->emu_edge_buffer;
+ uint8_t *tmp = enc->emu_edge_buffer;
const int b_stride = s->b_width << s->block_max_depth;
const int b_height = s->b_height<< s->block_max_depth;
const int w= p->width;
@@ -2088,6 +2094,7 @@ static av_cold int encode_end(AVCodecContext *avctx)
enc->m.s.me.temp = NULL;
av_freep(&enc->m.s.me.scratchpad);
+ av_freep(&enc->emu_edge_buffer);
av_freep(&avctx->stats_out);
--
2.52.0
>From 8de00b910c237943c7956f2851ce2ccf3014091d Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 9 Feb 2026 17:57:30 +0100
Subject: [PATCH 2/2] avcodec/snow: Reduce sizeof(SnowContext)
Each SubBand currently contains an array of 519 uint8_t[32],
yet most of these are unused: For both the decoder and the
encoder, at most 34 contexts are actually used: The only
variable index is context+2, where context is the result
of av_log2() and therefore in the 0..31 range.
There are also several accesses using compile-time indices,
the highest of which is 30. FATE passes with 31 contexts
and maybe these are enough, but I don't know.
Reducing the number to 34 reduces sizeof(SnowContext)
from 2141664B to 155104B here (on x64).
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/snow.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/snow.h b/libavcodec/snow.h
index 82417c3324..f8f45b8763 100644
--- a/libavcodec/snow.h
+++ b/libavcodec/snow.h
@@ -92,7 +92,7 @@ typedef struct SubBand{
int stride_line; ///< Stride measured in lines, not pixels.
x_and_coeff * x_coeff;
struct SubBand *parent;
- uint8_t state[/*7*2*/ 7 + 512][32];
+ uint8_t state[34][32];
}SubBand;
typedef struct Plane{
--
2.52.0
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
reply other threads:[~2026-02-19 11:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=177150096419.25.13741924957209540492@29965ddac10e \
--to=ffmpeg-devel@ffmpeg.org \
--cc=code@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git