FFmpeg
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 {
28  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(sw_format);
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  SPEC_LIST_ADD(sl, 5, 32, 1 << bits);
45  SPEC_LIST_ADD(sl, 6, 32, f->colorspace);
46  SPEC_LIST_ADD(sl, 7, 32, f->transparency);
47  SPEC_LIST_ADD(sl, 8, 32, ff_vk_mt_is_np_rgb(sw_format) &&
48  (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
49  SPEC_LIST_ADD(sl, 9, 32, f->plane_count);
50  SPEC_LIST_ADD(sl, 10, 32, color_planes);
51  SPEC_LIST_ADD(sl, 11, 32, av_pix_fmt_count_planes(sw_format));
52  SPEC_LIST_ADD(sl, 12, 32, bits + is_rgb);
53 
54  SPEC_LIST_ADD(sl, 13, 32, f->chroma_h_shift);
55  SPEC_LIST_ADD(sl, 14, 32, f->chroma_v_shift);
56 }
57 
58 static void set_crc_tab(uint32_t *buf)
59 {
60  for (uint32_t i = 0; i < 256; i++) {
61  uint32_t c = i << 24;
62  for (int j = 0; j < 8; j++)
63  c = (c << 1) ^ (0x04C11DB7 & (((int32_t) c) >> 31));
64  buf[i] = av_bswap32(c);
65  }
66 }
67 
68 static void set_rc_state_tab(FFV1Context *f, uint8_t *buf)
69 {
70  for (int i = 1; i < 256; i++) {
71  buf[256 + i] = f->state_transition[i];
72  buf[256 - i] = 256 - (int)f->state_transition[i];
73  }
74 }
75 
77 {
78  int err;
79 
80  uint8_t *buf_mapped;
81  size_t buf_len = 256*sizeof(uint32_t) + /* CRC */
82  512*sizeof(uint8_t) + /* Rangecoder */
85  MAX_QUANT_TABLE_SIZE*sizeof(int16_t);
86 
87  RET(ff_vk_create_buf(s, vkb,
88  buf_len,
89  NULL, NULL,
90  VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
91  VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
92  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
93  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
94  RET(ff_vk_map_buffer(s, vkb, (void *)&buf_mapped, 0));
95 
96  set_crc_tab((uint32_t *)buf_mapped);
97 
98  set_rc_state_tab(f, buf_mapped + 256*sizeof(uint32_t));
99 
100  memcpy(buf_mapped + 256*sizeof(uint32_t) + 512*sizeof(uint8_t),
101  f->quant_tables, sizeof(f->quant_tables));
102 
103  RET(ff_vk_unmap_buffer(s, vkb, 1));
104 
105 fail:
106  return err;
107 }
ff_vk_create_buf
int ff_vk_create_buf(FFVulkanContext *s, FFVkBuffer *buf, size_t size, void *pNext, void *alloc_pNext, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags)
Definition: vulkan.c:1020
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:140
set_crc_tab
static void set_crc_tab(uint32_t *buf)
Definition: ffv1_vulkan.c:58
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
RET
#define RET(x)
Definition: vulkan.h:68
ff_vk_map_buffer
static int ff_vk_map_buffer(FFVulkanContext *s, FFVkBuffer *buf, uint8_t **mem, int invalidate)
Definition: vulkan.h:603
MAX_QUANT_TABLE_SIZE
#define MAX_QUANT_TABLE_SIZE
Definition: ffv1.h:48
SPEC_LIST_ADD
#define SPEC_LIST_ADD(name, idx, val_bits, val)
Definition: vulkan.h:86
crc.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3496
fail
#define fail()
Definition: checkasm.h:218
set_rc_state_tab
static void set_rc_state_tab(FFV1Context *f, uint8_t *buf)
Definition: ffv1_vulkan.c:68
s
#define s(width, name)
Definition: cbs_vp9.c:198
bits
uint8_t bits
Definition: vp3data.h:128
ff_ffv1_vk_init_consts
int ff_ffv1_vk_init_consts(FFVulkanContext *s, FFVkBuffer *vkb, FFV1Context *f)
Definition: ffv1_vulkan.c:76
NULL
#define NULL
Definition: coverity.c:32
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
ffv1_vulkan.h
FFVulkanContext
Definition: vulkan.h:312
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
f
f
Definition: af_crystalizer.c:122
av_bswap32
#define av_bswap32
Definition: bswap.h:47
ff_vk_mt_is_np_rgb
int ff_vk_mt_is_np_rgb(enum AVPixelFormat pix_fmt)
Returns 1 if pixfmt is a usable RGB format.
Definition: vulkan.c:1539
ff_ffv1_vk_set_common_sl
void ff_ffv1_vk_set_common_sl(AVCodecContext *avctx, FFV1Context *f, VkSpecializationInfo *sl, enum AVPixelFormat sw_format)
Definition: ffv1_vulkan.c:24
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_vk_unmap_buffer
static int ff_vk_unmap_buffer(FFVulkanContext *s, FFVkBuffer *buf, int flush)
Definition: vulkan.h:610
MAX_CONTEXT_INPUTS
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:50
AVCodecContext
main external API structure.
Definition: avcodec.h:439
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:132
desc
const char * desc
Definition: libsvtav1.c:78
MAX_QUANT_TABLES
#define MAX_QUANT_TABLES
Definition: ffv1.h:47
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FFV1Context
Definition: ffv1.h:122
FFVkBuffer
Definition: vulkan.h:125
int32_t
int32_t
Definition: audioconvert.c:56