Commit 53d06fe633 for ffmpeg

commit 53d06fe6338787dd8e22f3e624232ac1c71c4fda
Author: TADANO Tokumei <aimingoff@pc.nifty.jp>
Date:   Sun Sep 13 20:19:58 2026 +0900

    avfilter/v360_vulkan: referctoring with few fixes

    Referctoring for later enhancements.
    There are 2 fixes with the referctoring.
    * Move PushData in context.
      Parameters are not recalculated in every v360_vulkan_filter_frame().
      Dynamically changed parameters are reflected by process_command().
    * Avoid infinity on calculate_iflat_range() and calculate_flat_range().
    * Remove unused variables in context.

    Signed-off-by: TADANO Tokumei <aimingoff@pc.nifty.jp>

diff --git a/libavfilter/vf_v360_vulkan.c b/libavfilter/vf_v360_vulkan.c
index a88fc6f0a6..8a86a880fd 100644
--- a/libavfilter/vf_v360_vulkan.c
+++ b/libavfilter/vf_v360_vulkan.c
@@ -26,6 +26,14 @@
 extern const unsigned char ff_v360_comp_spv_data[];
 extern const unsigned int ff_v360_comp_spv_len;

+/* Push constants */
+struct PushData {
+    float rot_mat[4][4];
+    int in_img_size[4][2];
+    float iflat_range[2];
+    float flat_range[2];
+};
+
 typedef struct V360ulkanContext {
     FFVulkanContext vkctx;

@@ -34,10 +42,9 @@ typedef struct V360ulkanContext {
     AVVulkanDeviceQueueFamily *qf;
     FFVulkanShader shd;
     VkSampler sampler;
+    struct PushData pd;

     /* Options */
-    int   planewidth[4], planeheight[4];
-    int   inplanewidth[4], inplaneheight[4];
     int   in, out;
     int   width, height;
     float h_fov, v_fov;
@@ -47,14 +54,6 @@ typedef struct V360ulkanContext {
     int   rotation_order[3];
 } V360VulkanContext;

-/* Push constants */
-struct PushData {
-    float rot_mat[4][4];
-    int in_img_size[4][2];
-    float iflat_range[2];
-    float flat_range[2];
-};
-
 static int get_rorder(char c)
 {
     switch (c) {
@@ -72,93 +71,6 @@ static int get_rorder(char c)
     }
 }

-static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
-{
-    int err;
-    V360VulkanContext *s = ctx->priv;
-    FFVulkanContext *vkctx = &s->vkctx;
-    const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
-
-    for (int order = 0; order < NB_RORDERS; order++) {
-        const char c = s->rorder[order];
-        int rorder;
-
-        if (c == '\0') {
-            av_log(ctx, AV_LOG_WARNING,
-                   "Incomplete rorder option. "
-                   "Direction for all 3 rotation orders should be specified. "
-                   "Switching to default rorder.\n");
-            s->rotation_order[0] = YAW;
-            s->rotation_order[1] = PITCH;
-            s->rotation_order[2] = ROLL;
-            break;
-        }
-
-        rorder = get_rorder(c);
-        if (rorder == -1) {
-            av_log(ctx, AV_LOG_WARNING,
-                   "Incorrect rotation order symbol '%c' in rorder option. "
-                   "Switching to default rorder.\n", c);
-            s->rotation_order[0] = YAW;
-            s->rotation_order[1] = PITCH;
-            s->rotation_order[2] = ROLL;
-            break;        }
-
-        s->rotation_order[order] = rorder;
-    }
-
-    RET(ff_vk_init_sampler(vkctx, &s->sampler, 0, VK_FILTER_LINEAR));
-
-    s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
-    if (!s->qf) {
-        av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
-        err = AVERROR(ENOTSUP);
-        goto fail;
-    }
-
-    RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, 2, 0, 0, 0, NULL));
-
-    SPEC_LIST_CREATE(sl, 4, 2*sizeof(int) + 2*sizeof(float))
-    SPEC_LIST_ADD(sl, 0, 32, s->out);
-    SPEC_LIST_ADD(sl, 1, 32, s->in);
-
-    const float m_pi = M_PI, m_pi2 = M_PI_2;
-    SPEC_LIST_ADD(sl, 2, 32, av_float2int(m_pi));
-    SPEC_LIST_ADD(sl, 3, 32, av_float2int(m_pi2));
-
-    ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT,
-                      sl, (uint32_t []) { 16, 16, 1 }, 0);
-
-    ff_vk_shader_add_push_const(&s->shd, 0, sizeof(struct PushData),
-                                VK_SHADER_STAGE_COMPUTE_BIT);
-
-    const FFVulkanDescriptorSetBinding desc_set[] = {
-        { /* input_img */
-            .type     = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
-            .stages   = VK_SHADER_STAGE_COMPUTE_BIT,
-            .elems    = planes,
-            .samplers = DUP_SAMPLER(s->sampler),
-        },
-        { /* output_img */
-            .type   = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
-            .stages = VK_SHADER_STAGE_COMPUTE_BIT,
-            .elems  = planes,
-        },
-    };
-    ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc_set, 2, 0);
-
-    RET(ff_vk_shader_link(vkctx, &s->shd,
-                          ff_v360_comp_spv_data,
-                          ff_v360_comp_spv_len, "main"));
-
-    RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
-
-    s->initialized = 1;
-
-fail:
-    return err;
-}
-
 static void multiply_matrix(float c[4][4], const float a[4][4], const float b[4][4])
 {
     for (int i = 0; i < 3; i++) {
@@ -176,8 +88,8 @@ static inline void calculate_iflat_range(int in, float ih_fov, float iv_fov,
 {
     switch (in) {
     case FLAT:
-        iflat_range[0] = tanf(0.5f * ih_fov * M_PI / 180.f);
-        iflat_range[1] = tanf(0.5f * iv_fov * M_PI / 180.f);
+        iflat_range[0] = tanf(0.5f * FFMIN(ih_fov, 179.f) * M_PI / 180.f);
+        iflat_range[1] = tanf(0.5f * FFMIN(iv_fov, 179.f) * M_PI / 180.f);
         break;
     case STEREOGRAPHIC:
         iflat_range[0] = tanf(FFMIN(ih_fov, 359.f) * M_PI / 720.f);
@@ -198,8 +110,8 @@ static inline void calculate_flat_range(int out, float h_fov, float v_fov,
 {
     switch (out) {
     case FLAT:
-        flat_range[0] = tanf(0.5f * h_fov * M_PI / 180.f);
-        flat_range[1] = tanf(0.5f * v_fov * M_PI / 180.f);
+        flat_range[0] = tanf(0.5f * FFMIN(h_fov, 179.f) * M_PI / 180.f);
+        flat_range[1] = tanf(0.5f * FFMIN(v_fov, 179.f) * M_PI / 180.f);
         break;
     case STEREOGRAPHIC:
         flat_range[0] = tanf(FFMIN(h_fov, 359.f) * M_PI / 720.f);
@@ -249,6 +161,113 @@ static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
     multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
 }

+static void config_params(AVFilterContext *ctx)
+{
+    V360VulkanContext *s = ctx->priv;
+
+    for (int order = 0; order < NB_RORDERS; order++) {
+        const char c = s->rorder[order];
+        int rorder;
+
+        if (c == '\0') {
+            av_log(ctx, AV_LOG_WARNING,
+                   "Incomplete rorder option. "
+                   "Direction for all 3 rotation orders should be specified. "
+                   "Switching to default rorder.\n");
+            s->rotation_order[0] = YAW;
+            s->rotation_order[1] = PITCH;
+            s->rotation_order[2] = ROLL;
+            break;
+        }
+
+        rorder = get_rorder(c);
+        if (rorder == -1) {
+            av_log(ctx, AV_LOG_WARNING,
+                   "Incorrect rotation order symbol '%c' in rorder option. "
+                   "Switching to default rorder.\n", c);
+            s->rotation_order[0] = YAW;
+            s->rotation_order[1] = PITCH;
+            s->rotation_order[2] = ROLL;
+            break;        }
+
+        s->rotation_order[order] = rorder;
+    }
+
+    calculate_iflat_range(s->in, s->ih_fov, s->iv_fov, s->pd.iflat_range);
+    calculate_flat_range(s->out, s->h_fov, s->v_fov, s->pd.flat_range);
+    calculate_rotation_matrix(s->yaw, s->pitch, s->roll,
+                              s->pd.rot_mat, s->rotation_order);
+
+    return;
+}
+
+static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
+{
+    int err;
+    V360VulkanContext *s = ctx->priv;
+    FFVulkanContext *vkctx = &s->vkctx;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->vkctx.output_format);
+    const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
+
+    s->pd.in_img_size[0][0] = s->pd.in_img_size[3][0] = in->width;
+    s->pd.in_img_size[0][1] = s->pd.in_img_size[3][1] = in->height;
+    s->pd.in_img_size[1][0] = s->pd.in_img_size[2][0] =
+        FF_CEIL_RSHIFT(in->width, desc->log2_chroma_w);
+    s->pd.in_img_size[1][1] = s->pd.in_img_size[2][1] =
+        FF_CEIL_RSHIFT(in->height, desc->log2_chroma_h);
+
+    RET(ff_vk_init_sampler(vkctx, &s->sampler, 0, VK_FILTER_LINEAR));
+
+    s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
+    if (!s->qf) {
+        av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
+        err = AVERROR(ENOTSUP);
+        goto fail;
+    }
+
+    RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, 2, 0, 0, 0, NULL));
+
+    SPEC_LIST_CREATE(sl, 4, 2*sizeof(int) + 2*sizeof(float))
+    SPEC_LIST_ADD(sl, 0, 32, s->out);
+    SPEC_LIST_ADD(sl, 1, 32, s->in);
+
+    const float m_pi = M_PI, m_pi2 = M_PI_2;
+    SPEC_LIST_ADD(sl, 2, 32, av_float2int(m_pi));
+    SPEC_LIST_ADD(sl, 3, 32, av_float2int(m_pi2));
+
+    ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT,
+                      sl, (uint32_t []) { 16, 16, 1 }, 0);
+
+    ff_vk_shader_add_push_const(&s->shd, 0, sizeof(struct PushData),
+                                VK_SHADER_STAGE_COMPUTE_BIT);
+
+    const FFVulkanDescriptorSetBinding desc_set[] = {
+        { /* input_img */
+            .type     = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
+            .stages   = VK_SHADER_STAGE_COMPUTE_BIT,
+            .elems    = planes,
+            .samplers = DUP_SAMPLER(s->sampler),
+        },
+        { /* output_img */
+            .type   = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
+            .stages = VK_SHADER_STAGE_COMPUTE_BIT,
+            .elems  = planes,
+        },
+    };
+    ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc_set, 2, 0);
+
+    RET(ff_vk_shader_link(vkctx, &s->shd,
+                          ff_v360_comp_spv_data,
+                          ff_v360_comp_spv_len, "main"));
+
+    RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
+
+    s->initialized = 1;
+
+fail:
+    return err;
+}
+
 static int v360_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
 {
     int err;
@@ -266,25 +285,9 @@ static int v360_vulkan_filter_frame(AVFilterLink *link, AVFrame *in)
     if (!s->initialized)
         RET(init_filter(ctx, in));

-    /* Push constants */
-    struct PushData pd = { 0 };
-
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->vkctx.input_format);
-    pd.in_img_size[0][0] = pd.in_img_size[3][0] = in->width;
-    pd.in_img_size[0][1] = pd.in_img_size[3][1] = in->height;
-    pd.in_img_size[1][0] = pd.in_img_size[2][0] =
-        FF_CEIL_RSHIFT(in->width, desc->log2_chroma_w);
-    pd.in_img_size[1][1] = pd.in_img_size[2][1] =
-        FF_CEIL_RSHIFT(in->height, desc->log2_chroma_h);
-
-    calculate_iflat_range(s->in, s->ih_fov, s->iv_fov, pd.iflat_range);
-    calculate_flat_range(s->out, s->h_fov, s->v_fov, pd.flat_range);
-    calculate_rotation_matrix(s->yaw, s->pitch, s->roll,
-                              pd.rot_mat, s->rotation_order);
-
     RET(ff_vk_filter_process_simple(&s->vkctx, &s->e, &s->shd,
                                     out, in, s->sampler, 1,
-                                    &pd, sizeof(pd)));
+                                    &s->pd, sizeof(s->pd)));

     err = av_frame_copy_props(out, in);
     if (err < 0)
@@ -300,6 +303,32 @@ fail:
     return err;
 }

+
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
+                           char *res, int res_len, int flags)
+{
+    int err;
+
+    RET(ff_filter_process_command(ctx, cmd, args, res, res_len, flags));
+    config_params(ctx);
+
+fail:
+    return err;
+}
+
+static av_cold int v360_vulkan_config_output(AVFilterLink *outlink)
+{
+    int err;
+    AVFilterContext *ctx = outlink->src;
+
+    config_params(ctx);
+
+    RET(ff_vk_filter_config_output(outlink));
+
+fail:
+    return err;
+}
+
 static void v360_vulkan_uninit(AVFilterContext *avctx)
 {
     V360VulkanContext *s = avctx->priv;
@@ -343,7 +372,7 @@ static const AVOption v360_vulkan_options[] = {
     {       "yaw", "yaw rotation",                       OFFSET(yaw), AV_OPT_TYPE_FLOAT,  {.dbl = 0.0f},     -180.f,               180.f, DYNAMIC, "yaw" },
     {     "pitch", "pitch rotation",                   OFFSET(pitch), AV_OPT_TYPE_FLOAT,  {.dbl = 0.0f},     -180.f,               180.f, DYNAMIC, "pitch" },
     {      "roll", "roll rotation",                     OFFSET(roll), AV_OPT_TYPE_FLOAT,  {.dbl = 0.0f},     -180.f,               180.f, DYNAMIC, "roll" },
-    {    "rorder", "rotation order",                  OFFSET(rorder), AV_OPT_TYPE_STRING, {.str = "ypr"},         0,                   0,   FLAGS, "rorder" },
+    {    "rorder", "rotation order",                  OFFSET(rorder), AV_OPT_TYPE_STRING, {.str = "ypr"},         0,                   0, DYNAMIC, "rorder" },
     {     "h_fov", "set output horizontal FOV angle",  OFFSET(h_fov), AV_OPT_TYPE_FLOAT,  {.dbl = 90.0f},  0.00001f,              360.0f, DYNAMIC, "h_fov" },
     {     "v_fov", "set output vertical FOV angle",    OFFSET(v_fov), AV_OPT_TYPE_FLOAT,  {.dbl = 45.0f},  0.00001f,              360.0f, DYNAMIC, "v_fov" },
     {    "ih_fov", "set input horizontal FOV angle",  OFFSET(ih_fov), AV_OPT_TYPE_FLOAT,  {.dbl = 90.0f},  0.00001f,              360.0f, DYNAMIC, "ih_fov" },
@@ -367,7 +396,7 @@ static const AVFilterPad v360_vulkan_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
-        .config_props = &ff_vk_filter_config_output,
+        .config_props = &v360_vulkan_config_output,
     },
 };

@@ -383,4 +412,5 @@ const FFFilter ff_vf_v360_vulkan = {
     FILTER_OUTPUTS(v360_vulkan_outputs),
     FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VULKAN),
     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+    .process_command = &process_command,
 };