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
86{
87 int err;
88
89 uint8_t *buf_mapped;
90 size_t buf_len = 256*sizeof(uint32_t) + /* CRC */
91 512*sizeof(uint8_t) + /* Rangecoder */
94 MAX_QUANT_TABLE_SIZE*sizeof(int16_t);
95
97 buf_len,
98 NULL, NULL,
99 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
100 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
101 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
102 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
103 RET(ff_vk_map_buffer(s, vkb, (void *)&buf_mapped, 0));
104
105 set_crc_tab((uint32_t *)buf_mapped);
106
107 set_rc_state_tab(f, buf_mapped + 256*sizeof(uint32_t));
108
109 memcpy(buf_mapped + 256*sizeof(uint32_t) + 512*sizeof(uint8_t),
110 f->quant_tables, sizeof(f->quant_tables));
111
112 RET(ff_vk_unmap_buffer(s, vkb, 1));
113
114fail:
115 return err;
116}
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.
static const uint8_t bits[8]
Definition fastaudio.c:100
#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_init_consts(FFVulkanContext *s, FFVkBuffer *vkb, FFV1Context *f)
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
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, void *pNext, void *alloc_pNext, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Definition vulkan.c:1016
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if pixfmt is a usable RGB format.
Definition vulkan.c:1503
const char * desc
Definition libsvtav1.c:83
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#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
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:621
static int ff_vk_map_buffer(FFVulkanContext *s, FFVkBuffer *buf, uint8_t **mem, int invalidate)
Definition vulkan.h:614