PR #25913 opened by mindfreeze
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25913
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25913.patch
The bitstream buffer was fixed at 128 MiB, and liboapv splits it between
the tiles by area. A frame above 128 MiB, or a tile above its share,
fails with OAPV_ERR_OUT_OF_BS_BUF ("oapve_encode() failed"). This is hit
at 16K more often: a 15360x8640 4:4:4 12-bit frame with complex content.
Size the buffer as oapv_app_enc does: twice the raw size of the frame,
padded to whole MBs, at two bytes per sample, plus per-MB tile overhead
and headroom for the headers. Every tile then gets at least twice its raw
size. Up to 3840x2160 this is at most the previous 128 MiB (64.5 MiB for
4:2:2, 127.8 MiB for 4:4:4:4); 15360x8640 4:4:4:4 needs 2045 MiB, which
still fits in the int liboapv takes. Above that, the buffer is capped at
INT_MAX with a warning, so larger frames that compress well still encode.
## Change
The buffer is sized the way `oapv_app_enc` does it
([`enc_max_bs_buf_size()`](https://github.com/AcademySoftwareFoundation/openapv/blob/main/app/oapv_app_enc.c#L42),
[OpenAPV PR
#251](https://github.com/AcademySoftwareFoundation/openapv/pull/251)): twice
the raw
frame, padded to whole MBs, at two bytes per sample, plus 40 bytes per MB and
16 KiB of headroom. Each tile
then gets at least twice its raw size, so liboapv's area-based split can't
overflow either.
| Frame | Buffer |
|---|---|
| 3840×2160 4:2:2 | 64.5 MiB |
| 3840×2160 4:4:4:4 | 127.8 MiB |
| 7680×4320 4:2:2 | 258 MiB |
| 15360×8640 4:4:4 | 1538 MiB |
| 15360×8640 4:4:4:4 | 2045 MiB |
Up to 4K this is no more than the old 128 MiB. Above the `int` that liboapv
takes, the buffer is capped at
`INT_MAX` with a warning, so larger frames that compress well still encode, as
before.
## How to reproduce
```
ffmpeg -f rawvideo -pix_fmt yuv444p -s 7680x4320 -i /dev/urandom -frames:v 1 \
-vf pad=15360:8640:0:0:gray -pix_fmt yuv444p12le -c:v liboapv -preset
fastest -qp 50 \
-oapv-params tile-w=7680:tile-h=4320 out.apv
```
Before: `oapve_encode() failed`. The frame is 75 MiB as a single tile, but with
2×2 tiles the noisy tile
needs about 67 MiB and gets 32 MiB. After: it encodes.
## Testing
- The command above encodes: as 2×2 tiles, and as one tile (75 MiB).
- A 140 MiB frame (noise in the top half of 15360×8640 4:4:4 12-bit, QP 50)
fails before the patch. After it,
the frame encodes, and FFmpeg's decode is byte-identical to OpenAPV's
`oapv_app_dec`. That decoder was built
with a larger input buffer for this check, because its own `MAX_BS_BUF` is
128 MiB.
- A full 15360×8640 4:4:4 12-bit noise frame at QP 0 (943 MiB) encodes.
Claude found a few other limits, btw,
1.
https://code.ffmpeg.org/FFmpeg/FFmpeg/src/commit/66f94beb0ab7bbc7b5bc3904bb318c062609bd18/libavformat/apvdec.c#L210
2.
https://code.ffmpeg.org/FFmpeg/FFmpeg/src/commit/66f94beb0ab7bbc7b5bc3904bb318c062609bd18/libavcodec/cbs_apv.c#L230
From 97f4b8f21cc748d402cdb2ff85d8d4b89d59d038 Mon Sep 17 00:00:00 2001
From: Vibhoothi <[email protected]>
Date: Tue, 10 Nov 2026 16:18:43 +0000
Subject: [PATCH] avcodec/liboapvenc: size the bitstream buffer from the frame
size
The bitstream buffer was fixed at 128 MiB, and liboapv splits it between
the tiles by area. A frame above 128 MiB, or a tile above its share,
fails with OAPV_ERR_OUT_OF_BS_BUF ("oapve_encode() failed"). This is hit
at 16K more often: a 15360x8640 4:4:4 12-bit frame with complex content.
Size the buffer as oapv_app_enc does: twice the raw size of the frame,
padded to whole MBs, at two bytes per sample, plus per-MB tile overhead
and headroom for the headers. Every tile then gets at least twice its raw
size. Up to 3840x2160 this is at most the previous 128 MiB (64.5 MiB for
4:2:2, 127.8 MiB for 4:4:4:4); 15360x8640 4:4:4:4 needs 2045 MiB, which
still fits in the int liboapv takes. Above that the buffer is capped at
INT_MAX with a warning, so larger frames that compress well still encode.
Signed-off-by: Vibhoothi <[email protected]>
---
libavcodec/liboapvenc.c | 48 ++++++++++++++++++++++++++++++++++++-----
1 file changed, 43 insertions(+), 5 deletions(-)
diff --git a/libavcodec/liboapvenc.c b/libavcodec/liboapvenc.c
index 0f59d65e146..8a31c52c8a2 100644
--- a/libavcodec/liboapvenc.c
+++ b/libavcodec/liboapvenc.c
@@ -42,7 +42,6 @@
#include "encode.h"
#include "profiles.h"
-#define MAX_BS_BUF (128 * 1024 * 1024)
#define MAX_NUM_FRMS (1) // supports only 1-frame in an access unit
#define FRM_IDX (0) // supports only 1-frame in an access unit
#define MAX_NUM_CC (OAPV_MAX_CC) // Max number of color components (upto
4:4:4:4)
@@ -64,6 +63,7 @@ typedef struct ApvEncContext {
oapvm_t mid;
oapve_cdesc_t cdsc; // coding parameters i.e profile, width & height
of input frame, num of therads, frame rate ...
oapv_bitb_t bitb; // bitstream buffer (output)
+ int bs_buf_size; // size of the bitstream buffer in bytes
oapve_stat_t stat; // encoding status (output)
oapv_frms_t ifrms; // frames for input
@@ -388,7 +388,7 @@ static int get_conf(AVCodecContext *avctx, oapve_cdesc_t
*cdsc)
cdsc->param[FRM_IDX].color_description_present_flag = 1;
}
- cdsc->max_bs_buf_size = MAX_BS_BUF; /* maximum bitstream buffer size */
+ cdsc->max_bs_buf_size = apv->bs_buf_size; /* maximum bitstream buffer size
*/
cdsc->max_num_frms = MAX_NUM_FRMS;
const AVDictionaryEntry *en = NULL;
@@ -466,6 +466,42 @@ static int handle_side_data(AVCodecContext *avctx,
ApvEncContext *apv)
return 0;
}
+/**
+ * Compute the size of a bitstream buffer that any frame fits in.
+ *
+ * This is the bound oapv_app_enc uses: twice the raw size of the frame,
+ * padded to whole MBs, at two bytes per sample, plus per-MB tile overhead
+ * and headroom for the access unit, frame headers and metadata. liboapv
+ * splits the buffer between the tiles by area, so each tile also gets
+ * twice its raw size.
+ *
+ * @return the buffer size in bytes, capped to the int that liboapv takes
+ */
+static int get_bs_buf_size(AVCodecContext *avctx)
+{
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+ int64_t mbs = (int64_t)((avctx->width + 15) >> 4) *
+ ((avctx->height + 15) >> 4);
+ int64_t samples_per_mb = 0, size;
+
+ for (int i = 0; i < desc->nb_components; i++) {
+ int chroma = i == 1 || i == 2;
+ samples_per_mb += (16 >> (chroma ? desc->log2_chroma_w : 0)) *
+ (16 >> (chroma ? desc->log2_chroma_h : 0));
+ }
+
+ size = mbs * samples_per_mb * 2 * 2 + mbs * 40 + 16 * 1024;
+ if (size > INT_MAX) {
+ av_log(avctx, AV_LOG_WARNING,
+ "A %dx%d frame can need a bitstream buffer of %"PRId64" bytes, "
+ "more than liboapv supports; frames that compress poorly may "
+ "fail to encode\n", avctx->width, avctx->height, size);
+ size = INT_MAX;
+ }
+
+ return size;
+}
+
/**
* @brief Initialize APV codec
* Create an encoder instance and allocate all the needed resources
@@ -487,13 +523,15 @@ static av_cold int liboapve_init(AVCodecContext *avctx)
apv->payloads = NULL;
/* allocate bitstream buffer */
- bs_buf = (unsigned char *)av_malloc(MAX_BS_BUF);
+ apv->bs_buf_size = get_bs_buf_size(avctx);
+
+ bs_buf = (unsigned char *)av_malloc(apv->bs_buf_size);
if (bs_buf == NULL) {
- av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer,
size=%d\n", MAX_BS_BUF);
+ av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer,
size=%d\n", apv->bs_buf_size);
return AVERROR(ENOMEM);
}
apv->bitb.addr = bs_buf;
- apv->bitb.bsize = MAX_BS_BUF;
+ apv->bitb.bsize = apv->bs_buf_size;
/* read configurations and set values for created descriptor (APV_CDSC) */
ret = get_conf(avctx, cdsc);
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]