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
24#include "ops_chain.h"
25
27{
28 return av_mallocz(sizeof(SwsOpChain));
29}
30
32{
33 if (!ptr)
34 return;
35
36 SwsOpChain *chain = ptr;
37 for (int i = 0; i < chain->num_impl + 1; i++) {
38 if (chain->free[i])
39 chain->free[i](&chain->impl[i].priv);
40 }
41
42 av_free(chain);
43}
44
46 void (*free)(SwsOpPriv *), const SwsOpPriv *priv)
47{
48 const int idx = chain->num_impl;
49 if (idx == SWS_MAX_OPS)
50 return AVERROR(EINVAL);
51
53 chain->impl[idx].cont = func;
54 chain->impl[idx + 1].priv = *priv;
55 chain->free[idx + 1] = free;
56 chain->num_impl++;
57 return 0;
58}
59
61 int num_tables, const SwsUOp *uop, const int block_size,
62 SwsOpChain *chain)
63{
64 const unsigned cpu_flags = av_get_cpu_flags();
65 const SwsUOpEntry *match = NULL;
66 int ret;
67
69 .ctx = ctx,
70 .uop = uop
71 };
72
73 for (int n = 0; !match && n < num_tables; n++) {
74 const SwsUOpTable *table = params.table = tables[n];
75 if (table->block_size && table->block_size != block_size ||
76 table->cpu_flags & ~cpu_flags)
77 continue;
78
79 for (int i = 0; table->entries[i]; i++) {
80 const SwsUOpEntry *entry = table->entries[i];
81 const SwsUOp entry_uop = {
82 .uop = entry->uop,
83 .type = entry->type,
84 .mask = entry->mask,
85 .par = entry->par,
86 };
87
88 if (ff_sws_uop_cmp(uop, &entry_uop) != 0)
89 continue;
90 if (entry->check && !entry->check(&params))
91 continue;
92
93 match = entry;
94 break;
95 }
96 }
97
98 if (!match) {
99 char name[64];
100 ff_sws_uop_name(uop, name);
101 av_log(ctx, AV_LOG_DEBUG, "No implementation found for: %s\n", name);
102 return AVERROR(ENOTSUP);
103 }
104
105 SwsImplResult res = {0};
106 if (match->setup) {
107 ret = match->setup(&params, &res);
108 if (ret < 0)
109 return ret;
110 }
111
112 ret = ff_sws_op_chain_append(chain, res.func ? res.func : match->func,
113 res.free, &res.priv);
114 if (ret < 0) {
115 if (res.free)
116 res.free(&res.priv);
117 return ret;
118 }
119
120 for (int i = 0; i < 4; i++) {
121 chain->over_read[i] = FFMAX(chain->over_read[i], res.over_read[i]);
122 chain->over_write[i] = FFMAX(chain->over_write[i], res.over_write[i]);
123 }
124
125 chain->cpu_flags |= params.table->cpu_flags;
126 return 0;
127}
128
130{
131 const SwsUOp *uop = params->uop;
132 const SwsPixel scalar = uop->data.scalar;
133 switch (uop->type) {
134 case SWS_PIXEL_U8: out->priv.u8[0] = scalar.u8; break;
135 case SWS_PIXEL_U16: out->priv.u16[0] = scalar.u16; break;
136 case SWS_PIXEL_U32: out->priv.u32[0] = scalar.u32; break;
137 case SWS_PIXEL_F32: out->priv.f32[0] = scalar.f32; break;
138 default: return AVERROR(EINVAL);
139 }
140
141 return 0;
142}
143
145{
146 const SwsUOp *uop = params->uop;
147 for (int i = 0; i < 4; i++) {
148 const SwsPixel vi = uop->data.vec4[i];
149 switch (uop->type) {
150 case SWS_PIXEL_U8: out->priv.u8[i] = vi.u8; break;
151 case SWS_PIXEL_U16: out->priv.u16[i] = vi.u16; break;
152 case SWS_PIXEL_U32: out->priv.u32[i] = vi.u32; break;
153 case SWS_PIXEL_F32: out->priv.f32[i] = vi.f32; break;
154 default: return AVERROR(EINVAL);
155 }
156 }
157
158 return 0;
159}
SwsAArch64OpImplParams params
Definition ops.c:51
#define entry
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
#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:66
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
Memory handling functions.
int ff_sws_op_chain_append(SwsOpChain *chain, SwsFuncPtr func, void(*free)(SwsOpPriv *), const SwsOpPriv *priv)
Definition ops_chain.c:45
int ff_sws_setup_vec4(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:144
SwsOpChain * ff_sws_op_chain_alloc(void)
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.c:26
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:60
void ff_sws_op_chain_free_cb(void *ptr)
Definition ops_chain.c:31
int ff_sws_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition ops_chain.c:129
#define SWS_MAX_OPS
Definition ops_chain.h:85
void(* SwsFuncPtr)(void)
Per-kernel execution context.
Definition ops_chain.h:70
static const uint16_t table[]
Definition prosumer.c:203
const char * name
Definition qsvenc.c:142
Main external API structure.
Definition swscale.h:227
int over_write[4]
Definition ops_chain.h:116
SwsFuncPtr func
Definition ops_chain.h:112
SwsOpPriv priv
Definition ops_chain.h:113
int over_read[4]
Definition ops_chain.h:115
void(* free)(SwsOpPriv *priv)
Definition ops_chain.h:114
Compiled "chain" of operations, which can be dispatched efficiently.
Definition ops_chain.h:84
int over_read[4]
Definition ops_chain.h:90
SwsOpImpl impl[SWS_MAX_OPS+1]
Definition ops_chain.h:86
int over_write[4]
Definition ops_chain.h:91
int cpu_flags
Definition ops_chain.h:89
int num_impl
Definition ops_chain.h:88
void(* free[SWS_MAX_OPS+1])(SwsOpPriv *)
Definition ops_chain.h:87
SwsFuncPtr cont
Definition ops_chain.h:72
SwsOpPriv priv
Definition ops_chain.h:73
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:146
Definition uops.h:292
SwsPixel scalar
Definition uops.h:303
union SwsUOp::@242237116251216327057105100216205033300341206345 data
SwsPixelType type
Definition uops.h:294
SwsPixel vec4[4]
Definition uops.h:304
#define av_free(p)
#define av_mallocz(s)
#define av_log(a,...)
static const uint8_t *const tables[]
Private data for each kernel.
Definition ops_chain.h:45
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:52
int ff_sws_uop_cmp(const SwsUOp *a, const SwsUOp *b)
Copyright (C) 2026 Niklas Haas.
Definition uops.c:31
@ 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