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 
28 {
29  return av_mallocz(sizeof(SwsOpChain));
30 }
31 
32 void ff_sws_op_chain_free_cb(void *ptr)
33 {
34  if (!ptr)
35  return;
36 
37  SwsOpChain *chain = ptr;
38  for (int i = 0; i < chain->num_impl + 1; i++) {
39  if (chain->free[i])
40  chain->free[i](&chain->impl[i].priv);
41  }
42 
43  av_free(chain);
44 }
45 
47  void (*free)(SwsOpPriv *), const SwsOpPriv *priv)
48 {
49  const int idx = chain->num_impl;
50  if (idx == SWS_MAX_OPS)
51  return AVERROR(EINVAL);
52 
54  chain->impl[idx].cont = func;
55  chain->impl[idx + 1].priv = *priv;
56  chain->free[idx + 1] = free;
57  chain->num_impl++;
58  return 0;
59 }
60 
61 #define q2pixel(type, q) ((q).den ? (type) (q).num / (q).den : 0)
62 
64 {
65  const SwsOp *op = params->op;
66  const AVRational64 factor = op->scale.factor;
67  switch (op->type) {
68  case SWS_PIXEL_U8: out->priv.u8[0] = q2pixel(uint8_t, factor); break;
69  case SWS_PIXEL_U16: out->priv.u16[0] = q2pixel(uint16_t, factor); break;
70  case SWS_PIXEL_U32: out->priv.u32[0] = q2pixel(uint32_t, factor); break;
71  case SWS_PIXEL_F32: out->priv.f32[0] = q2pixel(float, factor); break;
72  default: return AVERROR(EINVAL);
73  }
74 
75  return 0;
76 }
77 
79 {
80  const SwsOp *op = params->op;
81  for (int i = 0; i < 4; i++) {
82  const AVRational64 limit = op->clamp.limit[i];
83  switch (op->type) {
84  case SWS_PIXEL_U8: out->priv.u8[i] = q2pixel(uint8_t, limit); break;
85  case SWS_PIXEL_U16: out->priv.u16[i] = q2pixel(uint16_t, limit); break;
86  case SWS_PIXEL_U32: out->priv.u32[i] = q2pixel(uint32_t, limit); break;
87  case SWS_PIXEL_F32: out->priv.f32[i] = q2pixel(float, limit); break;
88  default: return AVERROR(EINVAL);
89  }
90  }
91 
92  return 0;
93 }
94 
96 {
97  const SwsOp *op = params->op;
98  for (int i = 0; i < 4; i++) {
99  const AVRational64 value = op->clear.value[i];
100  if (!value.den)
101  continue;
102  switch (op->type) {
103  case SWS_PIXEL_U8: out->priv.u8[i] = q2pixel(uint8_t, value); break;
104  case SWS_PIXEL_U16: out->priv.u16[i] = q2pixel(uint16_t, value); break;
105  case SWS_PIXEL_U32: out->priv.u32[i] = q2pixel(uint32_t, value); break;
106  case SWS_PIXEL_F32: out->priv.f32[i] = q2pixel(float, value); break;
107  default: return AVERROR(EINVAL);
108  }
109  }
110 
111  return 0;
112 }
113 
115  int num_tables, const SwsUOp *uop, const int block_size,
116  SwsOpChain *chain)
117 {
118  const unsigned cpu_flags = av_get_cpu_flags();
119  const SwsUOpEntry *match = NULL;
120  int ret;
121 
122  SwsImplParams params = {
123  .ctx = ctx,
124  .uop = uop
125  };
126 
127  for (int n = 0; !match && n < num_tables; n++) {
128  const SwsUOpTable *table = params.table = tables[n];
129  if (table->block_size && table->block_size != block_size ||
130  table->cpu_flags & ~cpu_flags)
131  continue;
132 
133  for (int i = 0; table->entries[i]; i++) {
134  const SwsUOpEntry *entry = table->entries[i];
135  const SwsUOp entry_uop = {
136  .uop = entry->uop,
137  .type = entry->type,
138  .mask = entry->mask,
139  .par = entry->par,
140  };
141 
142  if (ff_sws_uop_cmp(uop, &entry_uop) != 0)
143  continue;
144  if (entry->check && !entry->check(&params))
145  continue;
146 
147  match = entry;
148  break;
149  }
150  }
151 
152  if (!match) {
153  char name[64];
154  ff_sws_uop_name(uop, name);
155  av_log(ctx, AV_LOG_DEBUG, "No implementation found for: %s\n", name);
156  return AVERROR(ENOTSUP);
157  }
158 
159  SwsImplResult res = {0};
160  if (match->setup) {
161  ret = match->setup(&params, &res);
162  if (ret < 0)
163  return ret;
164  }
165 
166  ret = ff_sws_op_chain_append(chain, res.func ? res.func : match->func,
167  res.free, &res.priv);
168  if (ret < 0) {
169  if (res.free)
170  res.free(&res.priv);
171  return ret;
172  }
173 
174  for (int i = 0; i < 4; i++) {
175  chain->over_read[i] = FFMAX(chain->over_read[i], res.over_read[i]);
176  chain->over_write[i] = FFMAX(chain->over_write[i], res.over_write[i]);
177  }
178 
179  chain->cpu_flags |= params.table->cpu_flags;
180  return 0;
181 }
182 
184 {
185  const SwsUOp *uop = params->uop;
186  const SwsPixel scalar = uop->data.scalar;
187  switch (uop->type) {
188  case SWS_PIXEL_U8: out->priv.u8[0] = scalar.u8; break;
189  case SWS_PIXEL_U16: out->priv.u16[0] = scalar.u16; break;
190  case SWS_PIXEL_U32: out->priv.u32[0] = scalar.u32; break;
191  case SWS_PIXEL_F32: out->priv.f32[0] = scalar.f32; break;
192  default: return AVERROR(EINVAL);
193  }
194 
195  return 0;
196 }
197 
199 {
200  const SwsUOp *uop = params->uop;
201  for (int i = 0; i < 4; i++) {
202  const SwsPixel vi = uop->data.vec4[i];
203  switch (uop->type) {
204  case SWS_PIXEL_U8: out->priv.u8[i] = vi.u8; break;
205  case SWS_PIXEL_U16: out->priv.u16[i] = vi.u16; break;
206  case SWS_PIXEL_U32: out->priv.u32[i] = vi.u32; break;
207  case SWS_PIXEL_F32: out->priv.f32[i] = vi.f32; break;
208  default: return AVERROR(EINVAL);
209  }
210  }
211 
212  return 0;
213 }
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:95
SWS_MAX_OPS
#define SWS_MAX_OPS
Definition: ops_chain.h:85
out
static FILE * out
Definition: movenc.c:55
SwsUOp::data
union SwsUOp::@589 data
rational.h
ff_sws_setup_scale
int ff_sws_setup_scale(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:63
SwsOpImpl::cont
SwsFuncPtr cont
Definition: ops_chain.h:72
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
SwsUOpTable
Copyright (C) 2025 Niklas Haas.
Definition: ops_chain.h:154
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
SwsImplParams::table
const SwsUOpTable * table
Definition: ops_chain.h:106
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)
Copyright (C) 2025 Niklas Haas.
Definition: ops_chain.c:27
SwsUOp::uop
SwsUOpType uop
Definition: uops.h:224
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
SwsImplParams::op
const SwsOp * op
Definition: ops_chain.h:109
SwsUOpEntry::func
SwsFuncPtr func
Definition: ops_chain.h:130
SwsUOpEntry::setup
int(* setup)(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.h:131
ff_sws_setup_vec4
int ff_sws_setup_vec4(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:198
SwsImplParams
Definition: ops_chain.h:105
ff_sws_setup_clamp
int ff_sws_setup_clamp(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:78
SwsPixel::u16
uint16_t u16
Definition: uops.h:55
SwsUOp
Definition: uops.h:221
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AVRational64
64-bit Rational number (pair of numerator and denominator).
Definition: rational64.h:52
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
ff_sws_op_chain_free_cb
void ff_sws_op_chain_free_cb(void *ptr)
Definition: ops_chain.c:32
SwsImplParams::ctx
SwsContext * ctx
Definition: ops_chain.h:111
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:232
SwsImplResult::free
void(* free)(SwsOpPriv *priv)
Definition: ops_chain.h:117
SwsOp
Definition: ops.h:210
limit
static double limit(double x)
Definition: vf_pseudocolor.c:142
SwsUOp::type
SwsPixelType type
Definition: uops.h:223
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:233
ff_sws_setup_scalar
int ff_sws_setup_scalar(const SwsImplParams *params, SwsImplResult *out)
Definition: ops_chain.c:183
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
ff_sws_uop_lookup
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:114
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SwsUOpEntry
Definition: ops_chain.h:122
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:46
SwsUOpTable::cpu_flags
unsigned cpu_flags
Definition: ops_chain.h:155
q2pixel
#define q2pixel(type, q)
Definition: ops_chain.c:61
SwsContext
Main external API structure.
Definition: swscale.h:227
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