diff mbox series

[FFmpeg-devel] avcodec/ffv1enc: Tighter maxsize

Message ID 20241026224617.136191-1-michael@niedermayer.cc
State New
Headers show
Series [FFmpeg-devel] avcodec/ffv1enc: Tighter maxsize | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Michael Niedermayer Oct. 26, 2024, 10:46 p.m. UTC
This allows using smaller buffers

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1enc.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

Comments

Michael Niedermayer Nov. 3, 2024, 6:29 p.m. UTC | #1
On Sun, Oct 27, 2024 at 12:46:17AM +0200, Michael Niedermayer wrote:
> This allows using smaller buffers
> 
> Sponsored-by: Sovereign Tech Fund
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/ffv1enc.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)

will apply

[...]
diff mbox series

Patch

diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index a32059886c0..7a6c718b41b 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1143,8 +1143,7 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     uint8_t keystate    = 128;
     uint8_t *buf_p;
     int i, ret;
-    int64_t maxsize =   FF_INPUT_BUFFER_MIN_SIZE
-                      + avctx->width*avctx->height*37LL*4;
+    int64_t maxsize;
 
     if(!pict) {
         if (avctx->flags & AV_CODEC_FLAG_PASS1) {
@@ -1192,8 +1191,18 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         return 0;
     }
 
-    if (f->version > 3)
-        maxsize = FF_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
+    maxsize = avctx->width*avctx->height * (1 + f->transparency);
+    if (f->chroma_planes)
+        maxsize += AV_CEIL_RSHIFT(avctx->width, f->chroma_h_shift) * AV_CEIL_RSHIFT(f->height, f->chroma_v_shift) * 2;
+    maxsize += f->slice_count * 800; //for slice header
+    if (f->version > 3) {
+        maxsize *= f->bits_per_raw_sample + 1;
+    } else {
+        maxsize += f->slice_count * 2 * (avctx->width + avctx->height); //for bug with slices that code some pixels more than once
+        maxsize *= 8*(2*f->bits_per_raw_sample + 5);
+    }
+    maxsize >>= 3;
+    maxsize += FF_INPUT_BUFFER_MIN_SIZE;
 
     if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
         av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");