FFmpeg
Loading...
Searching...
No Matches
ops_chain.h
Go to the documentation of this file.
1/**
2 * Copyright (C) 2025 Niklas Haas
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#ifndef SWSCALE_OPS_CHAIN_H
22#define SWSCALE_OPS_CHAIN_H
23
24#include "libavutil/avassert.h"
25#include "libavutil/avconfig.h"
26#include "libavutil/cpu.h"
27#include "libavutil/mem.h"
28
29#include "ops_internal.h"
30#include "swscale_internal.h"
31
32/**
33 * Helpers for SIMD implementations based on chained kernels, using a
34 * continuation passing style to link them together.
35 *
36 * The basic idea here is to "link" together a series of different operation
37 * kernels by constructing a list of kernel addresses into an SwsOpChain. Each
38 * kernel will load the address of the next kernel (the "continuation") from
39 * this struct, and jump directly into it; using an internal function signature
40 * that is an implementation detail of the specific backend.
41 */
42
43typedef struct SwsUOpTable SwsUOpTable;
44
45/**
46 * Per-kernel execution context.
47 *
48 * Note: This struct is hard-coded in assembly, so do not change the layout.
49 */
50typedef void (*SwsFuncPtr)(void);
51typedef struct SwsOpImpl {
52 SwsFuncPtr func; /* Function pointer for this operation. */
53 char priv[]; /* Private data for this operation. */
54} SwsOpImpl;
55
56#if ARCH_X86_64 || ARCH_AARCH64
57static_assert(offsetof(SwsOpImpl, priv) == 8, "SwsOpImpl layout mismatch");
58static_assert(alignof(SwsOpImpl) == sizeof(void *), "SwsOpImpl alignment mismatch");
59#endif
60
61/**
62 * Compiled "chain" of operations, which can be dispatched efficiently.
63 * Effectively just a list of function pointers, alongside a small amount of
64 * private data for each operation.
65 */
66typedef struct SwsOpChain {
67 /* Generic data buffer. This contains a packed representation of
68 * continuation addresses and kernel private data. This is placed at
69 * the front of the struct so SIMD kernels can access it directly. */
70 DECLARE_ALIGNED_8(uint8_t, impl)[1024];
72
73 /* Metadata for the chain */
74 int cpu_flags; /* set of all used CPU flags */
75 int over_read[4]; /* chain over-reads input by this many bytes */
76 int over_write[4]; /* chain over-writes output by this many bytes */
77
78 /* Extra references owned by each kernel */
79 const void *ref[16];
82
84void ff_sws_op_chain_free_cb(void *chain);
85static inline void ff_sws_op_chain_free(SwsOpChain *chain)
86{
88}
89
90/* Returns 0 on success, or a negative error code. */
91int ff_sws_op_chain_append(SwsOpChain *chain, SwsFuncPtr func, const void *ref,
92 const void *data, size_t data_size);
93
94/**
95 * Attach a SwsOpChain and all metadata to a SwsCompiledOp (as out->priv).
96 *
97 * Note: This does not initialize *out. The caller is responsible for
98 * assigning out->func.
99 */
101
107
108typedef struct SwsImplResult {
109 SwsFuncPtr func; /* overrides `SwsUOpEntry.func` if non-NULL */
110 int over_read[4]; /* implementation over-reads input by this many bytes */
111 int over_write[4]; /* implementation over-writes output by this many bytes */
112 const void *ref; /* external data backing `priv`, ownership passes to caller */
113
114 /* Private data for this kernel */
115 size_t priv_size;
116 uint8_t priv[128];
118
119typedef struct SwsUOpEntry {
120 /* Kernel metadata; reduced size subset of SwsUOp (sans data) */
125
126 /* Kernel implementation */
128 int (*setup)(const SwsImplParams *params, SwsImplResult *out); /* optional */
129 bool (*check)(const SwsImplParams *params); /* optional, return true if supported */
131
132/* Setup helpers for SwsUOp data */
135
136#define SWS_OP_PRIV_PACK(out, data) ff_sws_op_priv_pack(out, &data, sizeof(data))
137static inline void ff_sws_op_priv_pack(SwsImplResult *out, const void *data, size_t size)
138{
139 av_assert0(out->priv_size + size <= sizeof(out->priv));
140 memcpy(&out->priv[out->priv_size], data, size);
141 out->priv_size += size;
142}
143
144static inline void ff_sws_op_priv_pack_refptr(SwsImplResult *out, const void *data)
145{
146 ff_sws_op_priv_pack(out, &data, sizeof(data));
147 av_assert0(!out->ref);
148 out->ref = data;
149}
150
152 unsigned cpu_flags; /* required CPU flags for this table */
153 int block_size; /* fixed block size of this table */
154 const SwsUOpEntry *entries[]; /* terminated by NULL */
155};
156
157/**
158 * "Compile" a single uop by looking it up in a list of fixed size uop tables,
159 * in decreasing order of preference.
160 *
161 * Returns 0 or a negative error code.
162 */
164 int num_tables, const SwsUOp *uop, const int block_size,
165 SwsOpChain *chain);
166
167#endif
SwsAArch64OpImplParams params
Definition ops.c:52
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:67
Memory handling functions.
const char data[16]
Definition mxf.c:155
static void ff_sws_op_priv_pack_refptr(SwsImplResult *out, const void *data)
Definition ops_chain.h:144
int ff_sws_op_chain_append(SwsOpChain *chain, SwsFuncPtr func, const void *ref, const void *data, size_t data_size)
Definition ops_chain.c:44
static void ff_sws_op_chain_free(SwsOpChain *chain)
Definition ops_chain.h:85
void ff_sws_op_chain_free_cb(void *chain)
Definition ops_chain.c:32
void(* SwsFuncPtr)(void)
Per-kernel execution context.
Definition ops_chain.h:50
int ff_sws_setup_vec4(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:159
SwsOpChain * ff_sws_op_chain_alloc(void)
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.c:27
void ff_sws_op_chain_attach(const SwsOpChain *chain, SwsCompiledOp *out)
Attach a SwsOpChain and all metadata to a SwsCompiledOp (as out->priv).
Definition ops_chain.c:67
int ff_sws_uop_lookup(SwsContext *ctx, const SwsUOpTable *const tables[], int num_tables, const SwsUOp *uop, const int block_size, SwsOpChain *chain)
"Compile" a single uop by looking it up in a list of fixed size uop tables, in decreasing order of pr...
Definition ops_chain.c:77
static void ff_sws_op_priv_pack(SwsImplResult *out, const void *data, size_t size)
Definition ops_chain.h:137
int ff_sws_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:144
Main external API structure.
Definition swscale.h:227
const SwsUOpTable * table
Definition ops_chain.h:103
SwsContext * ctx
Definition ops_chain.h:105
const SwsUOp * uop
Definition ops_chain.h:104
int over_write[4]
Definition ops_chain.h:111
SwsFuncPtr func
Definition ops_chain.h:109
const void * ref
Definition ops_chain.h:112
uint8_t priv[128]
Definition ops_chain.h:116
size_t priv_size
Definition ops_chain.h:115
int over_read[4]
Definition ops_chain.h:110
Compiled "chain" of operations, which can be dispatched efficiently.
Definition ops_chain.h:66
int over_read[4]
Definition ops_chain.h:75
int impl_size
Definition ops_chain.h:71
const void * ref[16]
Definition ops_chain.h:79
int over_write[4]
Definition ops_chain.h:76
DECLARE_ALIGNED_8(uint8_t, impl)[1024]
int cpu_flags
Definition ops_chain.h:74
int num_ref
Definition ops_chain.h:80
char priv[]
Definition ops_chain.h:53
SwsFuncPtr func
Definition ops_chain.h:52
SwsFuncPtr func
Definition ops_chain.h:127
int(* setup)(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.h:128
SwsUOpParams par
Definition ops_chain.h:124
SwsCompMask mask
Definition ops_chain.h:123
SwsPixelType type
Definition ops_chain.h:122
bool(* check)(const SwsImplParams *params)
Definition ops_chain.h:129
SwsUOpType uop
Definition ops_chain.h:121
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.h:151
const SwsUOpEntry * entries[]
Definition ops_chain.h:154
unsigned cpu_flags
Definition ops_chain.h:152
Definition uops.h:308
static int ref[MAX_W *MAX_W]
static const uint8_t *const tables[]
int size
SwsPixelType
Definition uops.h:40
SwsUOpType
Definition uops.h:169
uint8_t SwsCompMask
Bit-mask of components.
Definition uops.h:129