FFmpeg
ops_backend.h
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 #ifndef SWSCALE_OPS_BACKEND_H
22 #define SWSCALE_OPS_BACKEND_H
23 
24 /**
25  * Helper macros for the C-based backend.
26  *
27  * To use these macros, the following types must be defined:
28  * - PIXEL_TYPE should be one of SWS_PIXEL_*
29  * - pixel_t should be the type of pixels
30  * - block_t should be the type of blocks (groups of pixels)
31  */
32 
33 #include <assert.h>
34 #include <float.h>
35 #include <stdint.h>
36 
37 #include "libavutil/attributes.h"
38 #include "libavutil/mem.h"
39 
40 #include "ops_chain.h"
41 
42 /**
43  * Internal context holding per-iter execution data. The data pointers will be
44  * directly incremented by the corresponding read/write functions.
45  */
46 typedef struct SwsOpIter {
47  uintptr_t in[4];
48  uintptr_t out[4];
49  int x, y;
50 
51  /* Link back to per-slice execution context */
52  const SwsOpExec *exec;
53 } SwsOpIter;
54 
55 #ifdef __clang__
56 # define SWS_FUNC
57 # define SWS_LOOP AV_PRAGMA(clang loop vectorize(assume_safety))
58 #elif defined(__GNUC__)
59 # define SWS_FUNC __attribute__((optimize("tree-vectorize")))
60 # define SWS_LOOP AV_PRAGMA(GCC ivdep)
61 #else
62 # define SWS_FUNC
63 # define SWS_LOOP
64 #endif
65 
66 /* Miscellaneous helpers */
67 #define bitfn2(name, ext) name ## _ ## ext
68 #define bitfn(name, ext) bitfn2(name, ext)
69 
70 #define FN_SUFFIX AV_JOIN(FMT_CHAR, BIT_DEPTH)
71 #define fn(name) bitfn(name, FN_SUFFIX)
72 
73 #define av_q2pixel(q) ((q).den ? (pixel_t) (q).num / (q).den : 0)
74 #define bump_ptr(ptr, bump) ((pixel_t *) ((uintptr_t) (ptr) + (bump)))
75 
76 /* Helper macros to make writing common function signatures less painful */
77 #define DECL_FUNC(NAME, ...) \
78  static av_always_inline void fn(NAME)(SwsOpIter *restrict iter, \
79  const SwsOpImpl *restrict impl, \
80  block_t x, block_t y, \
81  block_t z, block_t w, \
82  __VA_ARGS__)
83 
84 #define DECL_READ(NAME, ...) \
85  DECL_FUNC(NAME, const pixel_t *restrict in0, const pixel_t *restrict in1, \
86  const pixel_t *restrict in2, const pixel_t *restrict in3, \
87  __VA_ARGS__)
88 
89 #define DECL_WRITE(NAME, ...) \
90  DECL_FUNC(NAME, pixel_t *restrict out0, pixel_t *restrict out1, \
91  pixel_t *restrict out2, pixel_t *restrict out3, \
92  __VA_ARGS__)
93 
94 /* Helper macros to call into functions declared with DECL_FUNC_* */
95 #define CALL(FUNC, ...) \
96  fn(FUNC)(iter, impl, x, y, z, w, __VA_ARGS__)
97 
98 #define CALL_READ(FUNC, ...) \
99  CALL(FUNC, (const pixel_t *) iter->in[0], (const pixel_t *) iter->in[1], \
100  (const pixel_t *) iter->in[2], (const pixel_t *) iter->in[3], \
101  __VA_ARGS__)
102 
103 #define CALL_WRITE(FUNC, ...) \
104  CALL(FUNC, (pixel_t *) iter->out[0], (pixel_t *) iter->out[1], \
105  (pixel_t *) iter->out[2], (pixel_t *) iter->out[3], __VA_ARGS__)
106 
107 /* Helper macros to declare continuation functions */
108 #define DECL_IMPL(NAME) \
109  static SWS_FUNC void fn(NAME)(SwsOpIter *restrict iter, \
110  const SwsOpImpl *restrict impl, \
111  block_t x, block_t y, \
112  block_t z, block_t w)
113 
114 /* Helper macro to call into the next continuation with a given type */
115 #define CONTINUE(TYPE, ...) \
116  ((void (*)(SwsOpIter *, const SwsOpImpl *, \
117  TYPE x, TYPE y, TYPE z, TYPE w)) impl->cont) \
118  (iter, &impl[1], __VA_ARGS__)
119 
120 /* Helper macros for common op setup code */
121 #define DECL_SETUP(NAME, PARAMS, OUT) \
122  static int fn(NAME)(const SwsImplParams *PARAMS, SwsImplResult *OUT)
123 
124 #define SETUP_MEMDUP(c, out) ff_setup_memdup(&(c), sizeof(c), out)
125 static inline int ff_setup_memdup(const void *c, size_t size, SwsImplResult *out)
126 {
127  out->priv.ptr = av_memdup(c, size);
128  out->free = ff_op_priv_free;
129  return out->priv.ptr ? 0 : AVERROR(ENOMEM);
130 }
131 
132 /* Helper macro for declaring op table entries */
133 #define DECL_ENTRY(NAME, ...) \
134  static const SwsOpEntry fn(op_##NAME) = { \
135  .func = (SwsFuncPtr) fn(NAME), \
136  .type = PIXEL_TYPE, \
137  __VA_ARGS__ \
138  }
139 
140 /* Helpers to define functions for common subsets of components */
141 #define DECL_PATTERN(NAME) \
142  DECL_FUNC(NAME, const bool X, const bool Y, const bool Z, const bool W)
143 
144 #define WRAP_PATTERN(FUNC, X, Y, Z, W, ...) \
145  DECL_IMPL(FUNC##_##X##Y##Z##W) \
146  { \
147  CALL(FUNC, X, Y, Z, W); \
148  } \
149  \
150  DECL_ENTRY(FUNC##_##X##Y##Z##W, \
151  .unused = { !X, !Y, !Z, !W }, \
152  __VA_ARGS__ \
153  )
154 
155 #define WRAP_COMMON_PATTERNS(FUNC, ...) \
156  WRAP_PATTERN(FUNC, 1, 0, 0, 0, __VA_ARGS__); \
157  WRAP_PATTERN(FUNC, 1, 0, 0, 1, __VA_ARGS__); \
158  WRAP_PATTERN(FUNC, 1, 1, 1, 0, __VA_ARGS__); \
159  WRAP_PATTERN(FUNC, 1, 1, 1, 1, __VA_ARGS__)
160 
161 #define REF_COMMON_PATTERNS(NAME) \
162  &fn(op_##NAME##_1000), \
163  &fn(op_##NAME##_1001), \
164  &fn(op_##NAME##_1110), \
165  &fn(op_##NAME##_1111)
166 
167 #endif
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
out
static FILE * out
Definition: movenc.c:55
SwsOpIter::exec
const SwsOpExec * exec
Definition: ops_backend.h:52
float.h
SwsOpIter
Copyright (C) 2025 Niklas Haas.
Definition: ops_backend.h:46
SwsOpIter::x
int x
Definition: ops_backend.h:49
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
ops_chain.h
SwsOpIter::out
uintptr_t out[4]
Definition: ops_backend.h:48
SwsOpExec
Copyright (C) 2026 Niklas Haas.
Definition: ops_dispatch.h:35
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
SwsOpIter::in
uintptr_t in[4]
Definition: ops_backend.h:47
size
int size
Definition: twinvq_data.h:10344
attributes.h
ff_op_priv_free
static void ff_op_priv_free(SwsOpPriv *priv)
Definition: ops_chain.h:149
mem.h
ff_setup_memdup
static int ff_setup_memdup(const void *c, size_t size, SwsImplResult *out)
Definition: ops_backend.h:125
SwsImplResult
Definition: ops_chain.h:111
SwsOpIter::y
int y
Definition: ops_backend.h:49