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 !(sw_format == AV_PIX_FMT_YA8);
32
33 SPEC_LIST_ADD(sl, 2, 32, f->version);
34 SPEC_LIST_ADD(sl, 3, 32, f->quant_table_count);
35
36 for (int i = 0; i < f->quant_table_count; i++) {
37 if (f->quant_tables[i][3][127] || f->quant_tables[i][4][127]) {
38 SPEC_LIST_ADD(sl, 4, 32, 1);
39 break;
40 }
41 }
42
43 int bits = desc->comp[0].depth;
44 /* Bayer pixfmts report misleading per-component depth in comp[0].depth
45 * (it counts the fraction of bits each component contributes per output
46 * pixel, not the per-sample bit width). Use bits_per_raw_sample. The
47 * encoder fills f->bits_per_raw_sample directly; the decoder only
48 * fills f->avctx->bits_per_raw_sample. Prefer the FFV1Context field
49 * with the avctx field as a fallback so this works from both sides. */
50 if (f->bayer)
51 bits = f->bits_per_raw_sample ? f->bits_per_raw_sample
52 : f->avctx->bits_per_raw_sample;
53 SPEC_LIST_ADD(sl, 5, 32, (uint32_t)(1ULL << bits));
54 SPEC_LIST_ADD(sl, 6, 32, f->colorspace);
55 SPEC_LIST_ADD(sl, 7, 32, f->transparency);
56 SPEC_LIST_ADD(sl, 8, 32, ff_vk_mt_is_np_rgb(sw_format) &&
57 (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
58 SPEC_LIST_ADD(sl, 9, 32, f->plane_count);
59 SPEC_LIST_ADD(sl, 10, 32, color_planes);
60 SPEC_LIST_ADD(sl, 11, 32, av_pix_fmt_count_planes(sw_format));
61 SPEC_LIST_ADD(sl, 12, 32, bits + is_rgb);
62
63 SPEC_LIST_ADD(sl, 13, 32, f->chroma_h_shift);
64 SPEC_LIST_ADD(sl, 14, 32, f->chroma_v_shift);
65}
66
67static void set_crc_tab(uint32_t *buf)
68{
69 for (uint32_t i = 0; i < 256; i++) {
70 uint32_t c = i << 24;
71 for (int j = 0; j < 8; j++)
72 c = (c << 1) ^ (0x04C11DB7 & (((int32_t) c) >> 31));
73 buf[i] = av_bswap32(c);
74 }
75}
76
77static void set_rc_state_tab(FFV1Context *f, uint8_t *buf)
78{
79 for (int i = 1; i < 256; i++) {
80 buf[256 + i] = f->state_transition[i];
81 buf[256 - i] = 256 - (int)f->state_transition[i];
82 }
83}
84
85uint32_t ff_ffv1_vk_rc_lanes(const FFVulkanContext *s, uint32_t *required)
86{
87 const VkPhysicalDeviceSubgroupSizeControlProperties *p = &s->subgroup_props;
88 uint32_t min = p->minSubgroupSize ? p->minSubgroupSize : s->props_11.subgroupSize;
89 uint32_t max = p->maxSubgroupSize ? p->maxSubgroupSize : s->props_11.subgroupSize;
90
91 /* A workgroup of the required size is a single, full subgroup */
92 if ((p->requiredSubgroupSizeStages & VK_SHADER_STAGE_COMPUTE_BIT) &&
93 min <= CONTEXT_SIZE) {
94 *required = FFMIN(max, CONTEXT_SIZE);
95 return *required;
96 }
97
98 /* Otherwise, a workgroup no larger than the smallest subgroup is part of
99 * a single one */
100 *required = 0;
101 return FFMIN(min, CONTEXT_SIZE);
102}
103
105{
106 int ok = 1;
107
108 for (int i = 0; i < MAX_QUANT_TABLES; i++)
109 for (int k = 0; k < 32; k++)
110 qb->thresh[i][k][0] = qb->thresh[i][k][1] = 128;
111 memset(qb->scale_off, 0, sizeof(qb->scale_off));
112
113 for (int i = 0; i < f->quant_table_count; i++) {
114 for (int j = 0; j < 2; j++) {
115 const int16_t *qt = f->quant_tables[i][3*j];
116 int n = 0, scale = 0;
117
118 for (int d = -127; d < 128; d++) {
119 int step = qt[d & 255] - qt[(d - 1) & 255];
120 if (!step)
121 continue;
122 if (!scale)
123 scale = step;
124 if (step != scale || n == 32 || (!j && step != 1)) {
125 ok = 0;
126 break;
127 }
128 qb->thresh[i][n++][j] = d;
129 }
130
131 if (j)
132 qb->scale_off[i][0] = scale;
133 qb->scale_off[i][1] += qt[128];
134 }
135 }
136
137 return ok;
138}
139
141{
142 int err;
143
144 uint8_t *buf_mapped;
146 size_t buf_len = 256*sizeof(uint32_t) + /* CRC */
147 512*sizeof(uint8_t) + /* Rangecoder */
151 sizeof(FFv1QuantBallot);
152
154 buf_len,
155 NULL, NULL,
156 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
157 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
158 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
159 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
160 RET(ff_vk_map_buffer(s, vkb, (void *)&buf_mapped, 0));
161
162 set_crc_tab((uint32_t *)buf_mapped);
163
164 set_rc_state_tab(f, buf_mapped + 256*sizeof(uint32_t));
165
166 quant_tables = (void *)(buf_mapped + 256*sizeof(uint32_t) + 512*sizeof(uint8_t));
167 for (int i = 0; i < MAX_QUANT_TABLES; i++)
168 for (int j = 0; j < MAX_CONTEXT_INPUTS; j++)
169 for (int k = 0; k < MAX_QUANT_TABLE_SIZE; k++)
170 quant_tables[i][j][k] = f->quant_tables[i][j][k];
171
173
174 RET(ff_vk_unmap_buffer(s, vkb, 1));
175
176fail:
177 return err;
178}
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:85
static void set_crc_tab(uint32_t *buf)
Definition ffv1_vulkan.c:67
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:77
#define fail
Definition test.h:479
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, void *pNext, void *alloc_pNext, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Definition vulkan.c:1204
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if pixfmt is a usable RGB format.
Definition vulkan.c:1692
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:653
static int ff_vk_map_buffer(FFVulkanContext *s, FFVkBuffer *buf, uint8_t **mem, int invalidate)
Definition vulkan.h:646