FFmpeg
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/rational.h"
24 
25 #include "ops_chain.h"
26 
27 #define Q(N) ((AVRational) { N, 1 })
28 
30 {
31  return av_mallocz(sizeof(SwsOpChain));
32 }
33 
34 void ff_sws_op_chain_free_cb(void *ptr)
35 {
36  if (!ptr)
37  return;
38 
39  SwsOpChain *chain = ptr;
40  for (int i = 0; i < chain->num_impl + 1; i++) {
41  if (chain->free[i])
42  chain->free[i](&chain->impl[i].priv);
43  }
44 
45  av_free(chain);
46 }
47 
49  void (*free)(SwsOpPriv *), const SwsOpPriv *priv)
50 {
51  const int idx = chain->num_impl;
52  if (idx == SWS_MAX_OPS)
53  return AVERROR(EINVAL);
54 
56  chain->impl[idx].cont = func;
57  chain->impl[idx + 1].priv = *priv;
58  chain->free[idx + 1] = free;
59  chain->num_impl++;
60  return 0;
61 }
62 
63 #define q2pixel(type, q) ((q).den ? (type) (q).num / (q).den : 0)
64 
66 {
67  const SwsOp *op = params->op;
68  const AVRational factor = op->scale.factor;
69  switch (op->type) {
70  case SWS_PIXEL_U8: out->priv.u8[0] = q2pixel(uint8_t, factor); break;
71  case SWS_PIXEL_U16: out->priv.u16[0] = q2pixel(uint16_t, factor); break;
72  case SWS_PIXEL_U32: out->priv.u32[0] = q2pixel(uint32_t, factor); break;
73  case SWS_PIXEL_F32: out->priv.f32[0] = q2pixel(float, factor); break;
74  default: return AVERROR(EINVAL);
75  }
76 
77  return 0;
78 }
79 
81 {
82  const SwsOp *op = params->op;
83  for (int i = 0; i < 4; i++) {
84  const AVRational limit = op->clamp.limit[i];
85  switch (op->type) {
86  case SWS_PIXEL_U8: out->priv.u8[i] = q2pixel(uint8_t, limit); break;
87  case SWS_PIXEL_U16: out->priv.u16[i] = q2pixel(uint16_t, limit); break;
88  case SWS_PIXEL_U32: out->priv.u32[i] = q2pixel(uint32_t, limit); break;
89  case SWS_PIXEL_F32: out->priv.f32[i] = q2pixel(float, limit); break;
90  default: return AVERROR(EINVAL);
91  }
92  }
93 
94  return 0;
95 }
96 
98 {
99  const SwsOp *op = params->op;
100  for (int i = 0; i < 4; i++) {
101  const AVRational value = op->clear.value[i];
102  if (!value.den)
103  continue;
104  switch (op->type) {
105  case SWS_PIXEL_U8: out->priv.u8[i] = q2pixel(uint8_t, value); break;
106  case SWS_PIXEL_U16: out->priv.u16[i] = q2pixel(uint16_t, value); break;
107  case SWS_PIXEL_U32: out->priv.u32[i] = q2pixel(uint32_t, value); break;
108  case SWS_PIXEL_F32: out->priv.f32[i] = q2pixel(float, value); break;
109  default: return AVERROR(EINVAL);
110  }
111  }
112 
113  return 0;
114 }
115 
117  int num_tables, const SwsUOp *uop, const int block_size,
118  SwsOpChain *chain)
119 {
120  const unsigned cpu_flags = av_get_cpu_flags();
121  const SwsOpEntry *match = NULL;
122  int ret;
123 
124  SwsImplParams params = {
125  .ctx = ctx,
126  .uop = uop
127  };
128 
129  for (int n = 0; !match && n < num_tables; n++) {
130  const SwsOpTable *table = params.table = tables[n];
131  if (table->block_size && table->block_size != block_size ||
132  table->cpu_flags & ~cpu_flags)
133  continue;
134 
135  for (int i = 0; table->entries[i]; i++) {
136  const SwsOpEntry *entry = table->entries[i];
137  const SwsUOp entry_uop = {
138  .uop = entry->uop,
139  .type = entry->type,
140  .mask = entry->mask,
141  .par = entry->par,
142  };
143 
144  if (ff_sws_uop_cmp(uop, &entry_uop) != 0)
145  continue;
146  if (entry->check && !entry->check(&params))
147  continue;
148 
149  match = entry;
150  break;
151  }
152  }
153 
154  if (!match) {
155  char name[64];
156  ff_sws_uop_name(uop, name);
157  av_log(ctx, AV_LOG_DEBUG, "No implementation found for: %s\n", name);
158  return AVERROR(ENOTSUP);
159  }
160 
161  SwsImplResult res = {0};
162  if (match->setup) {
163  ret = match->setup(&params, &res);
164  if (ret < 0)
165  return ret;
166  }
167 
168  ret = ff_sws_op_chain_append(chain, res.func ? res.func : match->func,
169  res.free, &res.priv);
170  if (ret < 0) {
171  if (res.free)
172  res.free(&res.priv);
173  return ret;
174  }
175 
176  for (int i = 0; i < 4; i++) {
177  chain->over_read[i] = FFMAX(chain->over_read[i], res.over_read[i]);
178  chain->over_write[i] = FFMAX(chain->over_write[i], res.over_write[i]);
179  }
180 
181  chain->cpu_flags |= params.table->cpu_flags;
182  return 0;
183 }
184 
186 {
187  const SwsUOp *uop = params->uop;
188  const SwsPixel scalar = uop->data.scalar;
189  switch (uop->type) {
190  case SWS_PIXEL_U8: out->priv.u8[0] = scalar.u8; break;
191  case SWS_PIXEL_U16: out->priv.u16[0] = scalar.u16; break;
192  case SWS_PIXEL_U32: out->priv.u32[0] = scalar.u32; break;
193  case SWS_PIXEL_F32: out->priv.f32[0] = scalar.f32; break;
194  default: return AVERROR(EINVAL);
195  }
196 
197  return 0;
198 }
199 
201 {
202  const SwsUOp *uop = params->uop;
203  for (int i = 0; i < 4; i++) {
204  const SwsPixel vi = uop->data.vec4[i];
205  switch (uop->type) {
206  case SWS_PIXEL_U8: out->priv.u8[i] = vi.u8; break;
207  case SWS_PIXEL_U16: out->priv.u16[i] = vi.u16; break;
208  case SWS_PIXEL_U32: out->priv.u32[i] = vi.u32; break;
209  case SWS_PIXEL_F32: out->priv.f32[i] = vi.f32; break;
210  default: return AVERROR(EINVAL);
211  }
212  }
213 
214  return 0;
215 }
SwsOpTable
Copyright (C) 2025 Niklas Haas.
Definition: ops_chain.h:154
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:66
factor
static const int factor[16]
Definition: vf_pp7.c:98
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
entry
#define entry
Definition: aom_film_grain_template.c:66
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
SwsImplResult::func
SwsFuncPtr func
Definition: ops_chain.h:115
SwsOpChain::over_read
int over_read[4]
Definition: ops_chain.h:90
ff_sws_setup_clear
int ff_sws_setup_clear(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:97
SWS_MAX_OPS
#define SWS_MAX_OPS
Definition: ops_chain.h:85
out
static FILE * out
Definition: movenc.c:55
rational.h
ff_sws_setup_scale
int ff_sws_setup_scale(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:65
SwsOpImpl::cont
SwsFuncPtr cont
Definition: ops_chain.h:72
ff_sws_uop_lookup
int ff_sws_uop_lookup(SwsContext *ctx, const SwsOpTable *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:116
table
static const uint16_t table[]
Definition: prosumer.c:203
ff_sws_uop_cmp
int ff_sws_uop_cmp(const SwsUOp *a, const SwsUOp *b)
Copyright (C) 2026 Niklas Haas.
Definition: uops.c:32
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
SwsOpEntry::setup
int(* setup)(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.h:131
SwsOpChain::cpu_flags
int cpu_flags
Definition: ops_chain.h:89
SwsFuncPtr
void(* SwsFuncPtr)(void)
Per-kernel execution context.
Definition: ops_chain.h:70
tables
Writing a table generator This documentation is preliminary Parts of the API are not good and should be changed Basic concepts A table generator consists of two *_tablegen c and *_tablegen h The h file will provide the variable declarations and initialization code for the tables
Definition: tablegen.txt:10
ops_chain.h
SwsOpChain::free
void(* free[SWS_MAX_OPS+1])(SwsOpPriv *)
Definition: ops_chain.h:87
avassert.h
ff_sws_op_chain_alloc
SwsOpChain * ff_sws_op_chain_alloc(void)
Definition: ops_chain.c:29
SwsUOp::uop
SwsUOpType uop
Definition: uops.h:204
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
SwsPixel::f32
float f32
Definition: uops.h:57
SwsOpChain::impl
SwsOpImpl impl[SWS_MAX_OPS+1]
Definition: ops_chain.h:86
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
SwsPixel::u8
uint8_t u8
Definition: uops.h:54
SwsOpChain
Compiled "chain" of operations, which can be dispatched efficiently.
Definition: ops_chain.h:84
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
SwsImplParams::op
const SwsOp * op
Definition: ops_chain.h:109
ff_sws_setup_vec4
int ff_sws_setup_vec4(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:200
SwsImplParams
Definition: ops_chain.h:105
SwsUOp::data
union SwsUOp::@586 data
ff_sws_setup_clamp
int ff_sws_setup_clamp(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:80
SwsPixel::u16
uint16_t u16
Definition: uops.h:55
SwsUOp
Definition: uops.h:201
SwsOpEntry::func
SwsFuncPtr func
Definition: ops_chain.h:130
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
SWS_PIXEL_U32
@ SWS_PIXEL_U32
Definition: uops.h:42
cpu_flags
CheckasmCpu cpu_flags
Definition: checkasm.c:84
SwsOpChain::num_impl
int num_impl
Definition: ops_chain.h:88
SwsPixel
Definition: uops.h:51
SwsOpEntry
Definition: ops_chain.h:122
ff_sws_op_chain_free_cb
void ff_sws_op_chain_free_cb(void *ptr)
Definition: ops_chain.c:34
SwsImplParams::ctx
SwsContext * ctx
Definition: ops_chain.h:111
SwsOpTable::cpu_flags
unsigned cpu_flags
Definition: ops_chain.h:155
SWS_PIXEL_U8
@ SWS_PIXEL_U8
Definition: uops.h:40
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
SwsOpChain::over_write
int over_write[4]
Definition: ops_chain.h:91
SwsUOp::scalar
SwsPixel scalar
Definition: uops.h:212
SwsImplResult::free
void(* free)(SwsOpPriv *priv)
Definition: ops_chain.h:117
SwsOp
Definition: ops.h:226
limit
static double limit(double x)
Definition: vf_pseudocolor.c:142
SwsUOp::type
SwsPixelType type
Definition: uops.h:203
ret
ret
Definition: filter_design.txt:187
SwsOpImpl::priv
SwsOpPriv priv
Definition: ops_chain.h:73
SwsImplResult::over_read
int over_read[4]
Definition: ops_chain.h:118
SwsImplResult::priv
SwsOpPriv priv
Definition: ops_chain.h:116
ff_sws_uop_name
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition: uops.c:129
SwsImplResult::over_write
int over_write[4]
Definition: ops_chain.h:119
SwsUOp::vec4
SwsPixel vec4[4]
Definition: uops.h:213
ff_sws_setup_scalar
int ff_sws_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:185
mem.h
SWS_PIXEL_F32
@ SWS_PIXEL_F32
Definition: uops.h:43
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
SwsImplParams::uop
const SwsUOp * uop
Definition: ops_chain.h:108
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_sws_op_chain_append
int ff_sws_op_chain_append(SwsOpChain *chain, SwsFuncPtr func, void(*free)(SwsOpPriv *), const SwsOpPriv *priv)
Definition: ops_chain.c:48
q2pixel
#define q2pixel(type, q)
Definition: ops_chain.c:63
SwsContext
Main external API structure.
Definition: swscale.h:229
SWS_PIXEL_U16
@ SWS_PIXEL_U16
Definition: uops.h:41
SwsPixel::u32
uint32_t u32
Definition: uops.h:56
SwsOpPriv
Private data for each kernel.
Definition: ops_chain.h:45
SwsImplResult
Definition: ops_chain.h:114
SwsImplParams::table
const SwsOpTable * table
Definition: ops_chain.h:106