FFmpeg
Loading...
Searching...
No Matches
ffv1_vulkan.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "ffv1_vulkan.h"
22#include "libavutil/crc.h"
23
25 VkSpecializationInfo *sl,
26 enum AVPixelFormat sw_format)
27{
29 int color_planes = av_pix_fmt_desc_get(sw_format)->nb_components;
30 int is_rgb = f->colorspace != 0 && sw_format != AV_PIX_FMT_YA8;
31
32 SPEC_LIST_ADD(sl, 2, 32, f->version);
33 SPEC_LIST_ADD(sl, 3, 32, f->quant_table_count);
34
35 for (int i = 0; i < f->quant_table_count; i++) {
36 if (f->quant_tables[i][3][127] || f->quant_tables[i][4][127]) {
37 SPEC_LIST_ADD(sl, 4, 32, 1);
38 break;
39 }
40 }
41
42 int bits = desc->comp[0].depth;
43 /* Bayer pixfmts report misleading per-component depth in comp[0].depth
44 * (it counts the fraction of bits each component contributes per output
45 * pixel, not the per-sample bit width). Use bits_per_raw_sample. The
46 * encoder fills f->bits_per_raw_sample directly; the decoder only
47 * fills f->avctx->bits_per_raw_sample. Prefer the FFV1Context field
48 * with the avctx field as a fallback so this works from both sides. */
49 if (f->bayer)
50 bits = f->bits_per_raw_sample ? f->bits_per_raw_sample
51 : f->avctx->bits_per_raw_sample;
52 SPEC_LIST_ADD(sl, 5, 32, (uint32_t)(1ULL << bits));
53 SPEC_LIST_ADD(sl, 6, 32, f->colorspace);
54 SPEC_LIST_ADD(sl, 7, 32, f->transparency);
55 SPEC_LIST_ADD(sl, 8, 32, ff_vk_mt_is_np_rgb(sw_format) &&
56 (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
57 SPEC_LIST_ADD(sl, 9, 32, f->plane_count);
58 SPEC_LIST_ADD(sl, 10, 32, color_planes);
59 SPEC_LIST_ADD(sl, 11, 32, av_pix_fmt_count_planes(sw_format));
60 SPEC_LIST_ADD(sl, 12, 32, bits + is_rgb);
61
62 SPEC_LIST_ADD(sl, 13, 32, f->chroma_h_shift);
63 SPEC_LIST_ADD(sl, 14, 32, f->chroma_v_shift);
64}
65
66static void set_crc_tab(uint32_t *buf)
67{
68 for (uint32_t i = 0; i < 256; i++) {
69 uint32_t c = i << 24;
70 for (int j = 0; j < 8; j++)
71 c = (c << 1) ^ (0x04C11DB7 & (((int32_t) c) >> 31));
72 buf[i] = av_bswap32(c);
73 }
74}
75
76static void set_rc_state_tab(FFV1Context *f, uint8_t *buf)
77{
78 for (int i = 1; i < 256; i++) {
79 buf[256 + i] = f->state_transition[i];
80 buf[256 - i] = 256 - (int)f->state_transition[i];
81 }
82}
83
84uint32_t ff_ffv1_vk_rc_lanes(const FFVulkanContext *s, uint32_t *required)
85{
86 const VkPhysicalDeviceSubgroupSizeControlProperties *p = &s->subgroup_props;
87 uint32_t min = p->minSubgroupSize ? p->minSubgroupSize : s->props_11.subgroupSize;
88 uint32_t max = p->maxSubgroupSize ? p->maxSubgroupSize : s->props_11.subgroupSize;
89
90 /* A workgroup of the required size is a single, full subgroup */
91 if ((p->requiredSubgroupSizeStages & VK_SHADER_STAGE_COMPUTE_BIT) &&
92 min <= CONTEXT_SIZE) {
93 *required = FFMIN(max, CONTEXT_SIZE);
94 return *required;
95 }
96
97 /* Otherwise, a workgroup no larger than the smallest subgroup is part of
98 * a single one */
99 *required = 0;
100 return FFMIN(min, CONTEXT_SIZE);
101}
102
104{
105 int ok = 1;
106
107 for (int i = 0; i < MAX_QUANT_TABLES; i++)
108 for (int k = 0; k < 32; k++)
109 qb->thresh[i][k][0] = qb->thresh[i][k][1] = 128;
110 memset(qb->scale_off, 0, sizeof(qb->scale_off));
111
112 for (int i = 0; i < f->quant_table_count; i++) {
113 for (int j = 0; j < 2; j++) {
114 const int16_t *qt = f->quant_tables[i][3*j];
115 int n = 0, scale = 0;
116
117 for (int d = -127; d < 128; d++) {
118 int step = qt[d & 255] - qt[(d - 1) & 255];
119 if (!step)
120 continue;
121 if (!scale)
122 scale = step;
123 if (step != scale || n == 32 || (!j && step != 1)) {
124 ok = 0;
125 break;
126 }
127 qb->thresh[i][n++][j] = d;
128 }
129
130 if (j)
131 qb->scale_off[i][0] = scale;
132 qb->scale_off[i][1] += qt[128];
133 }
134 }
135
136 return ok;
137}
138
140{
141 int err;
142
143 uint8_t *buf_mapped;
145 size_t buf_len = 256*sizeof(uint32_t) + /* CRC */
146 512*sizeof(uint8_t) + /* Rangecoder */
150 sizeof(FFv1QuantBallot);
151
153 buf_len,
154 NULL, NULL,
155 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
156 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
157 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
158 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
159 RET(ff_vk_map_buffer(s, vkb, (void *)&buf_mapped, 0));
160
161 set_crc_tab((uint32_t *)buf_mapped);
162
163 set_rc_state_tab(f, buf_mapped + 256*sizeof(uint32_t));
164
165 quant_tables = (void *)(buf_mapped + 256*sizeof(uint32_t) + 512*sizeof(uint8_t));
166 for (int i = 0; i < MAX_QUANT_TABLES; i++)
167 for (int j = 0; j < MAX_CONTEXT_INPUTS; j++)
168 for (int k = 0; k < MAX_QUANT_TABLE_SIZE; k++)
169 quant_tables[i][j][k] = f->quant_tables[i][j][k];
170
172
173 RET(ff_vk_unmap_buffer(s, vkb, 1));
174
175fail:
176 return err;
177}
int32_t
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
Public header for CRC hash function implementation.
#define min(a, b)
#define max(a, b)
static const uint8_t bits[8]
Definition fastaudio.c:100
#define CONTEXT_SIZE
Definition ffv1.h:45
#define MAX_QUANT_TABLE_SIZE
Definition ffv1.h:48
#define MAX_QUANT_TABLES
Definition ffv1.h:47
#define MAX_CONTEXT_INPUTS
Definition ffv1.h:50
int ff_ffv1_vk_quant_ballot(const FFV1Context *f, FFv1QuantBallot *qb)
Fill in the ballot quantizers of all quant tables.
int ff_ffv1_vk_init_consts(FFVulkanContext *s, FFVkBuffer *vkb, FFV1Context *f)
uint32_t ff_ffv1_vk_rc_lanes(const FFVulkanContext *s, uint32_t *required)
Number of invocations that range code a slice together, as a single subgroup.
Definition ffv1_vulkan.c:84
static void set_crc_tab(uint32_t *buf)
Definition ffv1_vulkan.c:66
void ff_ffv1_vk_set_common_sl(AVCodecContext *avctx, FFV1Context *f, VkSpecializationInfo *sl, enum AVPixelFormat sw_format)
Definition ffv1_vulkan.c:24
static void set_rc_state_tab(FFV1Context *f, uint8_t *buf)
Definition ffv1_vulkan.c:76
#define fail
Definition test.h:479
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, void *pNext, void *alloc_pNext, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Definition vulkan.c:1208
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if pixfmt is a usable RGB format.
Definition vulkan.c:1696
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3524
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition pixfmt.h:140
#define av_bswap32
Definition bswap.h:47
main external API structure.
Definition avcodec.h:443
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
int32_t scale_off[MAX_QUANT_TABLES][2]
Definition ffv1_vulkan.h:39
int32_t thresh[MAX_QUANT_TABLES][32][2]
Definition ffv1_vulkan.h:38
static const uint8_t quant_tables[]
static double c[64]
#define RET(x)
Definition vulkan.h:37
#define SPEC_LIST_ADD(name, idx, val_bits, val)
Definition vulkan.h:55
static int ff_vk_unmap_buffer(FFVulkanContext *s, FFVkBuffer *buf, int flush)
Definition vulkan.h:668
static int ff_vk_map_buffer(FFVulkanContext *s, FFVkBuffer *buf, uint8_t **mem, int invalidate)
Definition vulkan.h:661