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>
35 #include <stdint.h>
36
39
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 */
51
52 #ifdef __clang__
53 # define SWS_FUNC
54 # define SWS_LOOP AV_PRAGMA(clang loop vectorize(assume_safety))
55 #elif defined(__GNUC__)
56 # define SWS_FUNC __attribute__((optimize("tree-vectorize")))
57 # define SWS_LOOP AV_PRAGMA(GCC ivdep)
58 #else
59 # define SWS_FUNC
60 # define SWS_LOOP
61 #endif
62
63 /* Miscellaneous helpers */
64 #define bitfn2(name, ext) name ## _ ## ext
65 #define bitfn(name, ext) bitfn2(name, ext)
66
67 #define FN_SUFFIX AV_JOIN(FMT_CHAR, BIT_DEPTH)
68 #define fn(name) bitfn(name, FN_SUFFIX)
69
70 #define av_q2pixel(q) ((q).den ? (pixel_t) (q).num / (q).den : 0)
71
72 /* Helper macros to make writing common function signatures less painful */
73 #define DECL_FUNC(NAME, ...) \
74 static av_always_inline void fn(NAME)(SwsOpIter *restrict iter, \
75 const SwsOpImpl *restrict impl, \
76 block_t x, block_t y, \
77 block_t z, block_t w, \
78 __VA_ARGS__)
79
80 #define DECL_READ(NAME, ...) \
81 static av_always_inline void fn(NAME)(SwsOpIter *restrict iter, \
82 const SwsOpImpl *restrict impl, \
83 const pixel_t *restrict in0, \
84 const pixel_t *restrict in1, \
85 const pixel_t *restrict in2, \
86 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 fn(FUNC)(iter, impl, (const pixel_t *) iter->in[0], \
100 (const pixel_t *) iter->in[1], \
101 (const pixel_t *) iter->in[2], \
102 (const pixel_t *) iter->in[3], __VA_ARGS__)
103
104 #define CALL_WRITE(FUNC, ...) \
105 CALL(FUNC, (pixel_t *) iter->out[0], (pixel_t *) iter->out[1], \
106 (pixel_t *) iter->out[2], (pixel_t *) iter->out[3], __VA_ARGS__)
107
108 /* Helper macros to declare continuation functions */
109 #define DECL_IMPL(NAME) \
110 static SWS_FUNC void fn(NAME)(SwsOpIter *restrict iter, \
111 const SwsOpImpl *restrict impl, \
112 block_t x, block_t y, \
113 block_t z, block_t w)
114
115 #define DECL_IMPL_READ(NAME) \
116 static SWS_FUNC void fn(NAME)(SwsOpIter *restrict iter, \
117 const SwsOpImpl *restrict impl)
118
119 /* Helper macro to call into the next continuation with a given type */
120 #define CONTINUE(TYPE, ...) \
121 ((void (*)(SwsOpIter *, const SwsOpImpl *, \
122 TYPE x, TYPE y, TYPE z, TYPE w)) impl->cont) \
123 (iter, &impl[1], __VA_ARGS__)
124
125 /* Helper macros for common op setup code */
126 #define DECL_SETUP(NAME) \
127 static int fn(NAME)(const SwsOp *op, SwsOpPriv *out)
128
129 #define SETUP_MEMDUP(c) ff_setup_memdup(&(c), sizeof(c), out)
131 {
134 }
135
136 /* Helper macro for declaring op table entries */
137 #define DECL_ENTRY(NAME, ...) \
138 static const SwsOpEntry fn(op_##NAME) = { \
139 .func = (SwsFuncPtr) fn(NAME), \
140 .type = PIXEL_TYPE, \
141 __VA_ARGS__ \
142 }
143
144 /* Helpers to define functions for common subsets of components */
145 #define DECL_PATTERN(NAME) \
146 DECL_FUNC(NAME, const bool X, const bool Y, const bool Z, const bool W)
147
148 #define WRAP_PATTERN(FUNC, X, Y, Z, W, ...) \
149 DECL_IMPL(FUNC##_##X##Y##Z##W) \
150 { \
151 CALL(FUNC, X, Y, Z, W); \
152 } \
153 \
154 DECL_ENTRY(FUNC##_##X##Y##Z##W, \
155 .unused = { !X, !Y, !Z, !W }, \
156 __VA_ARGS__ \
157 )
158
159 #define WRAP_COMMON_PATTERNS(FUNC, ...) \
160 WRAP_PATTERN(FUNC, 1, 0, 0, 0, __VA_ARGS__); \
161 WRAP_PATTERN(FUNC, 1, 0, 0, 1, __VA_ARGS__); \
162 WRAP_PATTERN(FUNC, 1, 1, 1, 0, __VA_ARGS__); \
163 WRAP_PATTERN(FUNC, 1, 1, 1, 1, __VA_ARGS__)
164
165 #define REF_COMMON_PATTERNS(NAME) \
166 &fn(op_##NAME##_1000), \
167 &fn(op_##NAME##_1001), \
168 &fn(op_##NAME##_1110), \
169 &fn(op_##NAME##_1111)
170
171 #endif