FFmpeg
Loading...
Searching...
No Matches
ops.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Ramiro Polla
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 "../ops_chain.h"
22
23#include "libavutil/avassert.h"
24#include "libavutil/avstring.h"
25#include "libavutil/tree.h"
26
27#include "ops_impl_conv.c"
28
29/**
30 * Check that there is no mismatch for the SwsOpExec/SwsOpImpl offset
31 * values used by ops_static.
32 * NOTE: The check is performed here since this file only ever targets
33 * aarch64, differently from ops_static which may be built on any
34 * host.
35 */
36static_assert(offsetof_exec_in == offsetof(SwsOpExec, in), "SwsOpExec layout mismatch");
37static_assert(offsetof_exec_out == offsetof(SwsOpExec, out), "SwsOpExec layout mismatch");
38static_assert(offsetof_exec_in_bump == offsetof(SwsOpExec, in_bump), "SwsOpExec layout mismatch");
39static_assert(offsetof_exec_out_bump == offsetof(SwsOpExec, out_bump), "SwsOpExec layout mismatch");
40static_assert(offsetof_impl_func == offsetof(SwsOpImpl, func), "SwsOpImpl layout mismatch");
41static_assert(offsetof_impl_priv == offsetof(SwsOpImpl, priv), "SwsOpImpl layout mismatch");
42static_assert(alignof_impl == alignof(SwsOpImpl), "SwsOpImpl layout mismatch");
43
44/*********************************************************************/
45/* Forward-declare exported functions. */
46#define ENTRY(fname, ...) extern void fname(void);
47#include "ops_entries.c"
48#undef ENTRY
49
50static const struct {
51 void (*func)(void);
53} ops_entries[] = {
54#define ENTRY(fname, ...) { .func = fname, .params = __VA_ARGS__ },
55#include "ops_entries.c"
56#undef ENTRY
57};
58
59/* Look up the exported function pointer for the given parameters. */
61{
62 for (int i = 0; i < FF_ARRAY_ELEMS(ops_entries); i++)
63 if (!memcmp(p, &ops_entries[i].params, sizeof(SwsAArch64OpImplParams)))
64 return ops_entries[i].func;
65 return NULL;
66}
67
68/*********************************************************************/
70 const SwsUOp *uop, SwsImplResult *res)
71{
72 /**
73 * Compute number of full vector registers needed to pack all non-zero
74 * coefficients.
75 */
76 const int num_vregs = linear_num_vregs(p);
77 av_assert0(num_vregs <= 4);
78 union {
79 uint8_t u8[4 * 16];
80 uint16_t u16[4 * 8];
81 uint32_t u32[4 * 4];
82 float f32[4 * 4];
83 } coeffs = { 0 };
84
85 /**
86 * Copy non-zero coefficients, packed in sequential order, offset first.
87 * The same order must be followed in asmgen_op_linear().
88 */
89 int i_coeff = 0;
90 LOOP_MASK(p, i) {
91 for (int j = 0; j < 5; j++) {
92 const int jj = (j == 0) ? 4 : (j - 1);
93 if (!(p->par.lin.zero & SWS_MASK(i, jj))) {
94 const SwsPixel px = uop->data.mat4x5[i][jj];
95 switch (p->type) {
96 case SWS_PIXEL_U8: coeffs.u8[i_coeff++] = px.u8; break;
97 case SWS_PIXEL_U16: coeffs.u16[i_coeff++] = px.u16; break;
98 case SWS_PIXEL_U32: coeffs.u32[i_coeff++] = px.u32; break;
99 case SWS_PIXEL_F32: coeffs.f32[i_coeff++] = px.f32; break;
100 }
101 }
102 }
103 }
104
105 ff_sws_op_priv_pack(res, &coeffs, num_vregs * 16);
106 return 0;
107}
108
109/*********************************************************************/
110static int aarch64_setup_dither(const SwsUOp *uop, SwsImplResult *res)
111{
113 return 0;
114}
115
116/*********************************************************************/
117static int aarch64_setup(const SwsUOp *uop, const SwsAArch64OpImplParams *p,
119{
120 switch (uop->uop) {
121 case SWS_UOP_READ_BIT: {
122 /* Negative shift values to perform right shift using ushl. */
123 static const int8_t shift[16] = {
124 -7, -6, -5, -4, -3, -2, -1, 0,
125 -7, -6, -5, -4, -3, -2, -1, 0,
126 };
128 break;
129 }
130 case SWS_UOP_WRITE_BIT: {
131 /* Shift values for ushl. */
132 static const uint8_t shift[16] = {
133 7, 6, 5, 4, 3, 2, 1, 0,
134 7, 6, 5, 4, 3, 2, 1, 0,
135 };
137 break;
138 }
139 case SWS_UOP_CLEAR:
140 case SWS_UOP_ADD:
141 case SWS_UOP_MIN:
142 case SWS_UOP_MAX: {
143 int ret = ff_sws_setup_vec4(&(const SwsImplParams) { .uop = uop }, out);
144 if (ret < 0)
145 return ret;
146 /* Pad to the full vector register loaded by the kernel */
147 static const uint8_t zero[16];
148 ff_sws_op_priv_pack(out, zero, sizeof(zero) - out->priv_size);
149 return 0;
150 }
151 case SWS_UOP_SCALE:
152 return ff_sws_setup_scalar(&(const SwsImplParams) { .uop = uop }, out);
153 case SWS_UOP_LINEAR:
155 return aarch64_setup_linear(p, uop, out);
156 case SWS_UOP_DITHER:
157 return aarch64_setup_dither(uop, out);
158 }
159 return 0;
160}
161
162/*********************************************************************/
165{
166 int ret;
167
168 const int cpu_flags = av_get_cpu_flags();
170 return AVERROR(ENOTSUP);
171
172 /* Use at most two full vregs during the widest precision section */
173 int block_size = (uops->pixel_size_max == 4) ? 8 : 16;
174
176 if (!chain)
177 return AVERROR(ENOMEM);
179
180 *out = (SwsCompiledOp) {
181 .block_size = block_size,
182 .slice_align = 1,
183 };
184
185 /* Look up kernel functions. */
186 for (int i = 0; i < uops->num_ops; i++) {
188 convert_to_aarch64_impl(&uops->ops[i], block_size, &params);
190 if (!func) {
191 ret = AVERROR(ENOTSUP);
192 goto error;
193 }
194 SwsImplResult res = { 0 };
195 ret = aarch64_setup(&uops->ops[i], &params, &res);
196 if (ret < 0)
197 goto error;
199 ret = ff_sws_op_chain_append(chain, func, res.ref, res.priv, res.priv_size);
201 if (ret < 0)
202 goto error;
203 }
204
205 /* Look up process function. */
206 void ff_sws_process_0001_neon(void);
207 void ff_sws_process_0011_neon(void);
208 void ff_sws_process_0111_neon(void);
209 void ff_sws_process_1111_neon(void);
210
211 SwsOpFunc process_func = NULL;
212 switch (av_popcount(uops->planes_in | uops->planes_out)) {
213 case 1: process_func = (SwsOpFunc) ff_sws_process_0001_neon; break;
214 case 2: process_func = (SwsOpFunc) ff_sws_process_0011_neon; break;
215 case 3: process_func = (SwsOpFunc) ff_sws_process_0111_neon; break;
216 case 4: process_func = (SwsOpFunc) ff_sws_process_1111_neon; break;
217 }
218
219 out->func = process_func;
221
222 return 0;
223
224error:
226 return ret;
227}
228
229/*********************************************************************/
232{
234 if (!uops)
235 return AVERROR(ENOMEM);
236
238 if (!(ctx->flags & SWS_BITEXACT))
240 int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
241 if (ret < 0)
242 goto error;
243
245
246error:
248 return ret;
249}
250
251/*********************************************************************/
253 .name = "aarch64",
254 .flags = SWS_BACKEND_AARCH64,
255 .compile = compile_ops_aarch64,
256 .compile_uops = compile_uops_aarch64,
257 .hw_format = AV_PIX_FMT_NONE,
258};
static int aarch64_setup_dither(const SwsUOp *uop, SwsImplResult *res)
Definition ops.c:110
static int compile_ops_aarch64(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition ops.c:230
static int aarch64_setup(const SwsUOp *uop, const SwsAArch64OpImplParams *p, SwsImplResult *out)
Definition ops.c:117
static int aarch64_setup_linear(const SwsAArch64OpImplParams *p, const SwsUOp *uop, SwsImplResult *res)
Definition ops.c:69
static const struct @070174075374214234277303002223103136214276265127 ops_entries[]
SwsAArch64OpImplParams params
Definition ops.c:52
static int compile_uops_aarch64(SwsContext *ctx, const SwsUOpList *uops, SwsCompiledOp *out)
Definition ops.c:163
const SwsOpBackend backend_aarch64
Definition ops.c:252
static SwsFuncPtr aarch64_lookup(const SwsAArch64OpImplParams *p)
Definition ops.c:60
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define av_popcount
Definition common.h:154
#define NULL
Definition coverity.c:32
int ret
Definition ffplay.c:391
#define AVERROR(e)
Definition error.h:45
@ SWS_BACKEND_AARCH64
Chained AArch64 NEON kernels.
Definition swscale.h:119
@ SWS_BITEXACT
Definition swscale.h:178
static int zero(InterplayACMContext *s, unsigned ind, unsigned col)
static int shift(int a, int b)
Definition bonk.c:261
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:67
static atomic_int cpu_flags
Definition cpu.c:56
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition cpu.c:109
#define AV_CPU_FLAG_NEON
Definition cpu.h:74
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
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_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:144
static void ff_sws_op_priv_pack_refptr(SwsImplResult *out, const void *data)
Definition ops_chain.h:144
static void ff_sws_op_chain_free(SwsOpChain *chain)
Definition ops_chain.h:85
void(* SwsFuncPtr)(void)
Per-kernel execution context.
Definition ops_chain.h:50
static void ff_sws_op_priv_pack(SwsImplResult *out, const void *data, size_t size)
Definition ops_chain.h:137
#define SWS_OP_PRIV_PACK(out, data)
Definition ops_chain.h:136
void(* SwsOpFunc)(const SwsOpExec *exec, const void *priv, int bx_start, int y_start, int bx_end, int y_end)
Process a given range of pixel blocks.
static int linear_num_vregs(const SwsAArch64OpImplParams *params)
Definition ops_impl.h:73
#define offsetof_exec_out_bump
Definition ops_impl.h:119
#define offsetof_impl_func
Definition ops_impl.h:120
#define offsetof_exec_in
These values will be used by ops_asmgen to access fields inside of SwsOpExec and SwsOpImpl.
Definition ops_impl.h:116
#define alignof_impl
Definition ops_impl.h:122
#define offsetof_exec_out
Definition ops_impl.h:117
#define LOOP_MASK(p, idx)
Definition ops_impl.h:63
#define offsetof_exec_in_bump
Definition ops_impl.h:118
static int aarch64_priv_size(const SwsAArch64OpImplParams *params)
Number of bytes of private data consumed by each kernel; must match what aarch64_setup() packs.
Definition ops_impl.h:89
#define offsetof_impl_priv
Definition ops_impl.h:121
static void convert_to_aarch64_impl(const SwsUOp *uop, int block_size, SwsAArch64OpImplParams *out)
NOTE: This file is #include'd directly by both the NEON backend and the sws_ops_aarch64 tool.
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:141
#define FF_ARRAY_ELEMS(a)
SwsAArch64OpImplParams describes the parameters for an SwsUOpType operation.
Definition ops_impl.h:47
Main external API structure.
Definition swscale.h:227
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
Compiled "chain" of operations, which can be dispatched efficiently.
Definition ops_chain.h:66
int cpu_flags
Definition ops_chain.h:74
Copyright (C) 2026 Niklas Haas.
Helper struct for representing a list of operations.
Definition ops.h:318
SwsUOp * ops
Definition uops.h:345
int pixel_size_max
Definition uops.h:351
int num_ops
Definition uops.h:346
SwsCompMask planes_in
Definition uops.h:349
SwsCompMask planes_out
Definition uops.h:350
Definition uops.h:308
SwsUOpType uop
Definition uops.h:311
SwsPixel * ptr
Definition uops.h:318
union SwsUOp::@242237116251216327057105100216205033300341206345 data
SwsPixel mat4x5[4][5]
Definition uops.h:321
static void error(const char *err)
A tree container.
float f32
Definition uops.h:85
uint32_t u32
Definition uops.h:84
uint8_t u8
Definition uops.h:82
uint16_t u16
Definition uops.h:83
int ff_sws_ops_translate(SwsContext *ctx, const SwsOpList *ops, SwsUOpFlags flags, SwsUOpList *uops)
Translate a list of operations down to micro-ops, which can be further optimized and then directly ex...
Definition uops.c:644
SwsUOpList * ff_sws_uop_list_alloc(void)
Definition uops.c:176
void ff_sws_uop_list_free(SwsUOpList **p_ops)
Definition uops.c:162
uint32_t SwsUOpFlags
Definition uops.h:158
@ SWS_PIXEL_F32
Definition uops.h:45
@ SWS_PIXEL_U32
Definition uops.h:44
@ SWS_PIXEL_U16
Definition uops.h:43
@ SWS_PIXEL_U8
Definition uops.h:42
#define SWS_MASK(I, J)
Definition uops.h:275
@ SWS_UOP_MAX
Definition uops.h:208
@ SWS_UOP_READ_BIT
Definition uops.h:179
@ SWS_UOP_MIN
Definition uops.h:207
@ SWS_UOP_LINEAR
Definition uops.h:216
@ SWS_UOP_LINEAR_FMA
Definition uops.h:217
@ SWS_UOP_SCALE
Definition uops.h:205
@ SWS_UOP_WRITE_BIT
Definition uops.h:185
@ SWS_UOP_DITHER
Definition uops.h:218
@ SWS_UOP_ADD
Definition uops.h:206
@ SWS_UOP_CLEAR
Definition uops.h:215
@ SWS_UOP_FLAG_FMA
Definition uops.h:161
@ SWS_UOP_FLAG_ADD
Definition uops.h:165
@ SWS_UOP_FLAG_PUNNING
Definition uops.h:166