FFmpeg
Loading...
Searching...
No Matches
ops_chain.c
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#include "libavutil/avassert.h"
22#include "libavutil/mem.h"
23#include "libavutil/refstruct.h"
24
25#include "ops_chain.h"
26
28{
29 return av_mallocz(sizeof(SwsOpChain));
30}
31
33{
34 if (!ptr)
35 return;
36
37 SwsOpChain *chain = ptr;
38 for (int i = 0; i < chain->num_ref; i++)
39 av_refstruct_unref(&chain->ref[i]);
40
41 av_free(chain);
42}
43
45 const void *data, size_t data_size)
46{
47 /* Ensures each SwsOpImpl is stored aligned, so that we can deref it */
48 const size_t op_size = FFALIGN(offsetof(SwsOpImpl, priv) + data_size,
49 alignof(SwsOpImpl));
50
51 if (chain->impl_size + op_size > sizeof(chain->impl))
52 return AVERROR(EINVAL);
53 if (ref && chain->num_ref >= FF_ARRAY_ELEMS(chain->ref))
54 return AVERROR(EINVAL);
55
56 SwsOpImpl *impl = (SwsOpImpl *) &chain->impl[chain->impl_size];
57 impl->func = func;
58 if (data_size)
59 memcpy(&impl->priv, data, data_size);
60 chain->impl_size += op_size;
61 if (ref)
62 chain->ref[chain->num_ref++] = av_refstruct_ref_c(ref);
63
64 return 0;
65}
66
68{
69 av_assert0(!out->priv);
70 out->priv = (void *) chain;
72 out->cpu_flags = chain->cpu_flags;
73 memcpy(out->over_read, chain->over_read, sizeof(out->over_read));
74 memcpy(out->over_write, chain->over_write, sizeof(out->over_write));
75}
76
78 int num_tables, const SwsUOp *uop, const int block_size,
79 SwsOpChain *chain)
80{
81 const unsigned cpu_flags = av_get_cpu_flags();
82 const SwsUOpEntry *match = NULL;
83 int ret;
84
86 .ctx = ctx,
87 .uop = uop
88 };
89
90 for (int n = 0; !match && n < num_tables; n++) {
91 const SwsUOpTable *table = params.table = tables[n];
92 if (table->block_size && table->block_size != block_size ||
93 table->cpu_flags & ~cpu_flags)
94 continue;
95
96 for (int i = 0; table->entries[i]; i++) {
97 const SwsUOpEntry *entry = table->entries[i];
98 const SwsUOp entry_uop = {
99 .uop = entry->uop,
100 .type = entry->type,
101 .mask = entry->mask,
102 .par = entry->par,
103 };
104
105 if (ff_sws_uop_cmp(uop, &entry_uop) != 0)
106 continue;
107 if (entry->check && !entry->check(&params))
108 continue;
109
110 match = entry;
111 break;
112 }
113 }
114
115 if (!match) {
116 char name[64];
117 ff_sws_uop_name(uop, name);
118 av_log(ctx, AV_LOG_DEBUG, "No implementation found for: %s\n", name);
119 return AVERROR(ENOTSUP);
120 }
121
122 SwsImplResult res = {0};
123 if (match->setup) {
124 ret = match->setup(&params, &res);
125 if (ret < 0)
126 return ret;
127 }
128
129 ret = ff_sws_op_chain_append(chain, res.func ? res.func : match->func,
130 res.ref, res.priv, res.priv_size);
132 if (ret < 0)
133 return ret;
134
135 for (int i = 0; i < 4; i++) {
136 chain->over_read[i] = FFMAX(chain->over_read[i], res.over_read[i]);
137 chain->over_write[i] = FFMAX(chain->over_write[i], res.over_write[i]);
138 }
139
140 chain->cpu_flags |= params.table->cpu_flags;
141 return 0;
142}
143
145{
146 const SwsUOp *uop = params->uop;
147 const SwsPixel scalar = uop->data.scalar;
148 switch (uop->type) {
149 case SWS_PIXEL_U8: SWS_OP_PRIV_PACK(out, scalar.u8); break;
150 case SWS_PIXEL_U16: SWS_OP_PRIV_PACK(out, scalar.u16); break;
151 case SWS_PIXEL_U32: SWS_OP_PRIV_PACK(out, scalar.u32); break;
152 case SWS_PIXEL_F32: SWS_OP_PRIV_PACK(out, scalar.f32); break;
153 default: return AVERROR(EINVAL);
154 }
155
156 return 0;
157}
158
160{
161 const SwsUOp *uop = params->uop;
162 for (int i = 0; i < 4; i++) {
163 const SwsPixel vi = uop->data.vec4[i];
164 switch (uop->type) {
165 case SWS_PIXEL_U8: SWS_OP_PRIV_PACK(out, vi.u8); break;
166 case SWS_PIXEL_U16: SWS_OP_PRIV_PACK(out, vi.u16); break;
167 case SWS_PIXEL_U32: SWS_OP_PRIV_PACK(out, vi.u32); break;
168 case SWS_PIXEL_F32: SWS_OP_PRIV_PACK(out, vi.f32); break;
169 default: return AVERROR(EINVAL);
170 }
171 }
172
173 return 0;
174}
SwsAArch64OpImplParams params
Definition ops.c:52
#define entry
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
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
int ret
Definition ffplay.c:391
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
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 FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
const char data[16]
Definition mxf.c:149
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_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
void ff_sws_op_chain_free_cb(void *ptr)
Definition ops_chain.c:32
int ff_sws_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:144
void(* SwsFuncPtr)(void)
Per-kernel execution context.
Definition ops_chain.h:50
#define SWS_OP_PRIV_PACK(out, data)
Definition ops_chain.h:136
static const uint16_t table[]
Definition prosumer.c:203
const char * name
Definition qsvenc.c:142
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
const void * av_refstruct_ref_c(const void *obj)
Analog of av_refstruct_ref(), but for constant objects.
Definition refstruct.c:150
#define FF_ARRAY_ELEMS(a)
Main external API structure.
Definition swscale.h:227
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
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
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.h:151
Definition uops.h:308
SwsPixel scalar
Definition uops.h:319
union SwsUOp::@242237116251216327057105100216205033300341206345 data
SwsPixelType type
Definition uops.h:310
SwsPixel vec4[4]
Definition uops.h:320
#define av_free(p)
#define av_mallocz(s)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
static const uint8_t *const tables[]
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
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition uops.c:54
int ff_sws_uop_cmp(const SwsUOp *a, const SwsUOp *b)
Copyright (C) 2026 Niklas Haas.
Definition uops.c:35
@ 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