PR #26129 opened by t-boiko
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26129
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26129.patch

The sequence header, the frame headers and the render size declared the
frame size as the input size aligned up to encodeInputPictureGranularity,
while every encode is submitted with codedExtent set to the input size.

encodeInputPictureGranularity only describes which input texels an
implementation may read; it does not change the coded frame size. When the
two differ, the headers do not match the coded tile data and decoders fail
in the last superblock row or column. An implementation that reports a
granularity of 64x64 (the AV1 superblock size, which the specification
allows) makes every size that is not a multiple of 64 undecodable:
1280x720 is declared as 1280x768, and libdav1d and libaom reject it.

Declare the frame size the implementation codes, as the resolution override
rules of the Vulkan specification define it: the input size, unless the
input size aligned to 8 is not a multiple of codedPictureAlignment, in which
case that size aligned to codedPictureAlignment. Signal the input size as
the render size when the two differ, and choose the level from the coded
frame size.


>From f7d32688d222a5b37fcea931a2b6c3f51f7e3bda Mon Sep 17 00:00:00 2001
From: Tymur Boiko <[email protected]>
Date: Tue, 17 Nov 2026 13:41:33 +0100
Subject: [PATCH] avcodec/vulkan_encode_av1: declare the coded frame size, not
 the granularity-aligned size

The sequence header, the frame headers and the render size declared the
frame size as the input size aligned up to encodeInputPictureGranularity,
while every encode is submitted with codedExtent set to the input size.

encodeInputPictureGranularity only describes which input texels an
implementation may read; it does not change the coded frame size. When the
two differ, the headers do not match the coded tile data and decoders fail
in the last superblock row or column. An implementation that reports a
granularity of 64x64 (the AV1 superblock size, which the specification
allows) makes every size that is not a multiple of 64 undecodable:
1280x720 is declared as 1280x768, and libdav1d and libaom reject it.

Declare the frame size the implementation codes, as the resolution override
rules of the Vulkan specification define it: the input size, unless the
input size aligned to 8 is not a multiple of codedPictureAlignment, in which
case that size aligned to codedPictureAlignment. Signal the input size as
the render size when the two differ, and choose the level from the coded
frame size.

Signed-off-by: Tymur Boiko <[email protected]>
---
 libavcodec/vulkan_encode_av1.c | 71 ++++++++++++++++++++++++++--------
 1 file changed, 54 insertions(+), 17 deletions(-)

diff --git a/libavcodec/vulkan_encode_av1.c b/libavcodec/vulkan_encode_av1.c
index e5d88803a49..9a7c2d24b5c 100644
--- a/libavcodec/vulkan_encode_av1.c
+++ b/libavcodec/vulkan_encode_av1.c
@@ -98,6 +98,10 @@ typedef struct VulkanEncodeAV1Context {
     int seq_tier;
     int seq_level_idx;
 
+    /* Size of the coded frame, see coded_frame_size() */
+    int frame_width;
+    int frame_height;
+
     int q_idx_idr;
     int q_idx_p;
     int q_idx_b;
@@ -105,6 +109,28 @@ typedef struct VulkanEncodeAV1Context {
     uint8_t *padding_payload;
 } VulkanEncodeAV1Context;
 
+/**
+ * Size of the frame the implementation codes for a dimension of the encode
+ * input picture, following the resolution override rules of the Vulkan
+ * specification (encode-av1-resolution-override): the size is coded as it is,
+ * unless its 8-aligned value is not a multiple of codedPictureAlignment, in
+ * which case it is coded at that value aligned up to codedPictureAlignment.
+ *
+ * encodeInputPictureGranularity only describes which input texels the
+ * implementation reads, and does not affect the coded frame size.
+ */
+static int coded_frame_size(int size, uint32_t alignment)
+{
+    int size8 = FFALIGN(size, 8);
+    int aligned;
+
+    if (alignment <= 8)
+        return size;
+
+    aligned = FFALIGN(size8, alignment);
+    return aligned == size8 ? size : aligned;
+}
+
 static int init_pic_rc(AVCodecContext *avctx, FFHWBaseEncodePicture *pic,
                        VkVideoEncodeRateControlInfoKHR *rc_info,
                        VkVideoEncodeRateControlLayerInfoKHR *rc_layer)
@@ -179,7 +205,6 @@ static int init_pic_params(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
 {
     VulkanEncodeAV1Context *enc = avctx->priv_data;
     FFVulkanEncodeContext *ctx = &enc->common;
-    FFHWBaseEncodeContext *base_ctx = &ctx->base;
 
     VulkanEncodeAV1Picture *ap = pic->codec_priv;
     FFHWBaseEncodePicture *ref;
@@ -293,7 +318,8 @@ static int init_pic_params(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
                                     (pic->display_order <= pic->encode_order),
             .disable_cdf_update = 0,
             .use_superres = 0,
-            .render_and_frame_size_different = 0,
+            .render_and_frame_size_different = enc->frame_width  != 
avctx->width ||
+                                               enc->frame_height != 
avctx->height,
             .allow_screen_content_tools = 0,
             .is_filter_switchable = 0,
             .force_integer_mv = 0,
@@ -328,8 +354,8 @@ static int init_pic_params(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
         .primary_ref_frame = 0, // set later
         .refresh_frame_flags = 0x0, // set later
         .coded_denom = 0,
-        .render_width_minus_1 = base_ctx->surface_width - 1,
-        .render_height_minus_1 = base_ctx->surface_height - 1,
+        .render_width_minus_1 = avctx->width - 1,
+        .render_height_minus_1 = avctx->height - 1,
         .interpolation_filter = 0,
         .TxMode = STD_VIDEO_AV1_TX_MODE_SELECT,
         .delta_q_res = 0,
@@ -563,10 +589,12 @@ static int init_pic_params(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
 
         fh->frame_type = AV1_FRAME_INTER;
         fh->refresh_frame_flags = 1 << ap->slot;
-        fh->frame_width_minus_1   = base_ctx->surface_width - 1;
-        fh->frame_height_minus_1  = base_ctx->surface_height - 1;
-        fh->render_width_minus_1  = fh->frame_width_minus_1;
-        fh->render_height_minus_1 = fh->frame_height_minus_1;
+        fh->frame_width_minus_1   = enc->frame_width - 1;
+        fh->frame_height_minus_1  = enc->frame_height - 1;
+        fh->render_width_minus_1  = avctx->width - 1;
+        fh->render_height_minus_1 = avctx->height - 1;
+        fh->render_and_frame_size_different = enc->frame_width  != 
avctx->width ||
+                                              enc->frame_height != 
avctx->height;
 
         memcpy(fh->loop_filter_ref_deltas, default_loop_filter_ref_deltas,
                AV1_TOTAL_REFS_PER_FRAME * sizeof(int8_t));
@@ -582,10 +610,12 @@ static int init_pic_params(AVCodecContext *avctx, 
FFHWBaseEncodePicture *pic,
         fh->show_existing_frame   = 1;
         fh->frame_to_show_map_idx = ap->slot != 0;
         fh->frame_type            = AV1_FRAME_INTER;
-        fh->frame_width_minus_1   = base_ctx->surface_width - 1;
-        fh->frame_height_minus_1  = base_ctx->surface_height - 1;
-        fh->render_width_minus_1  = fh->frame_width_minus_1;
-        fh->render_height_minus_1 = fh->frame_height_minus_1;
+        fh->frame_width_minus_1   = enc->frame_width - 1;
+        fh->frame_height_minus_1  = enc->frame_height - 1;
+        fh->render_width_minus_1  = avctx->width - 1;
+        fh->render_height_minus_1 = avctx->height - 1;
+        fh->render_and_frame_size_different = enc->frame_width  != 
avctx->width ||
+                                              enc->frame_height != 
avctx->height;
     }
 
     return 0;
@@ -740,7 +770,7 @@ static av_cold int init_sequence_headers(AVCodecContext 
*avctx)
             framerate = av_q2d(avctx->framerate);
 
         level = ff_av1_guess_level(avctx->bit_rate, enc->seq_tier,
-                                   base_ctx->surface_width, 
base_ctx->surface_height,
+                                   enc->frame_width, enc->frame_height,
                                    enc->tile_rows * enc->tile_cols,
                                    enc->tile_cols, framerate);
         if (level) {
@@ -762,10 +792,10 @@ static av_cold int init_sequence_headers(AVCodecContext 
*avctx)
         .seq_force_integer_mv = seq->seq_force_screen_content_tools ?
                                 AV1_SELECT_SCREEN_CONTENT_TOOLS :
                                 AV1_SELECT_INTEGER_MV,
-        .frame_width_bits_minus_1 = av_log2(base_ctx->surface_width),
-        .frame_height_bits_minus_1 = av_log2(base_ctx->surface_height),
-        .max_frame_width_minus_1 = base_ctx->surface_width - 1,
-        .max_frame_height_minus_1 = base_ctx->surface_height - 1,
+        .frame_width_bits_minus_1 = av_log2(enc->frame_width),
+        .frame_height_bits_minus_1 = av_log2(enc->frame_height),
+        .max_frame_width_minus_1 = enc->frame_width - 1,
+        .max_frame_height_minus_1 = enc->frame_height - 1,
         .enable_order_hint = 1,
         .order_hint_bits_minus_1 = av_clip_intp2(av_log2(ctx->base.gop_size), 
3),
         .use_128x128_superblock = !!(enc->caps.superblockSizes & 
VK_VIDEO_ENCODE_AV1_SUPERBLOCK_SIZE_128_BIT_KHR),
@@ -1305,6 +1335,13 @@ static av_cold int vulkan_encode_av1_init(AVCodecContext 
*avctx)
            enc->caps.maxLevel);
     av_log(avctx, AV_LOG_VERBOSE, "        codedPictureAlignment: %ix%i\n",
            enc->caps.codedPictureAlignment.width, 
enc->caps.codedPictureAlignment.height);
+    enc->frame_width  = coded_frame_size(avctx->width,
+                                         
enc->caps.codedPictureAlignment.width);
+    enc->frame_height = coded_frame_size(avctx->height,
+                                         
enc->caps.codedPictureAlignment.height);
+    av_log(avctx, AV_LOG_VERBOSE, "    Coded frame size: %ix%i\n",
+           enc->frame_width, enc->frame_height);
+
     av_log(avctx, AV_LOG_VERBOSE, "        maxTiles: %ix%i\n",
            enc->caps.maxTiles.width, enc->caps.maxTiles.height);
     av_log(avctx, AV_LOG_VERBOSE, "        Tile size: %ix%i to %ix%i\n",
-- 
2.54.0

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

Reply via email to