/src/ffmpeg/libswscale/uops.h
Line | Count | Source |
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_UOPS_H |
22 | | #define SWSCALE_UOPS_H |
23 | | |
24 | | #include <assert.h> |
25 | | #include <stdbool.h> |
26 | | #include <stdint.h> |
27 | | |
28 | | /*************************************************************************** |
29 | | * Note: This header must be usable at build time, to generate asm sources * |
30 | | ***************************************************************************/ |
31 | | |
32 | | #include "libavutil/attributes.h" |
33 | | |
34 | | typedef struct SwsContext SwsContext; |
35 | | typedef struct SwsFilterWeights SwsFilterWeights; |
36 | | typedef struct SwsOpList SwsOpList; |
37 | | |
38 | | typedef enum SwsPixelType { |
39 | | SWS_PIXEL_NONE = 0, |
40 | | SWS_PIXEL_U8, |
41 | | SWS_PIXEL_U16, |
42 | | SWS_PIXEL_U32, |
43 | | SWS_PIXEL_F32, |
44 | | SWS_PIXEL_TYPE_NB |
45 | | } SwsPixelType; |
46 | | |
47 | | const char *ff_sws_pixel_type_name(SwsPixelType type); |
48 | | int ff_sws_pixel_type_size(SwsPixelType type) av_const; |
49 | | bool ff_sws_pixel_type_is_int(SwsPixelType type) av_const; |
50 | | |
51 | | typedef union SwsPixel { |
52 | | char data[4]; |
53 | | |
54 | | uint8_t u8; |
55 | | uint16_t u16; |
56 | | uint32_t u32; |
57 | | float f32; |
58 | | } SwsPixel; |
59 | | |
60 | | /* Ensures (SwsPixel) {0} is properly initialized to all zeros */ |
61 | | static_assert(sizeof(SwsPixel) == sizeof(char[4]), "SwsPixel size mismatch"); |
62 | | |
63 | | /** |
64 | | * Bit-mask of components. Exact meaning depends on the usage context. |
65 | | */ |
66 | | typedef uint8_t SwsCompMask; |
67 | | enum { |
68 | | SWS_COMP_NONE = 0, |
69 | | SWS_COMP_ALL = 0xF, |
70 | 0 | #define SWS_COMP(X) (1 << (X)) |
71 | 0 | #define SWS_COMP_TEST(mask, X) (!!((mask) & SWS_COMP(X))) |
72 | | #define SWS_COMP_INV(mask) ((mask) ^ SWS_COMP_ALL) |
73 | 0 | #define SWS_COMP_ELEMS(N) ((1 << (N)) - 1) |
74 | | #define SWS_COMP_COUNT(mask) (av_popcount((mask) & SWS_COMP_ALL)) |
75 | | #define SWS_COMP_MASK(X, Y, Z, W) \ |
76 | 0 | (((X) ? SWS_COMP(0) : 0) | \ |
77 | 0 | ((Y) ? SWS_COMP(1) : 0) | \ |
78 | 0 | ((Z) ? SWS_COMP(2) : 0) | \ |
79 | 0 | ((W) ? SWS_COMP(3) : 0)) |
80 | | }; |
81 | | |
82 | | |
83 | 0 | #define ff_sws_comp_mask_str(mask) ff_sws_comp_mask_print(mask, (char[5]){0}) |
84 | | static inline char *ff_sws_comp_mask_print(SwsCompMask mask, char buf[5]) |
85 | 0 | { |
86 | 0 | char *ptr = buf; |
87 | 0 | for (int c = 0; c < 4; c++) { |
88 | 0 | if (SWS_COMP_TEST(mask, c)) |
89 | 0 | *ptr++ = "xyzw"[c]; |
90 | 0 | } |
91 | 0 | *ptr = '\0'; |
92 | 0 | return buf; |
93 | 0 | } Unexecuted instantiation: format.c:ff_sws_comp_mask_print Unexecuted instantiation: graph.c:ff_sws_comp_mask_print Unexecuted instantiation: ops.c:ff_sws_comp_mask_print Unexecuted instantiation: ops_dispatch.c:ff_sws_comp_mask_print Unexecuted instantiation: ops_memcpy.c:ff_sws_comp_mask_print Unexecuted instantiation: ops_optimizer.c:ff_sws_comp_mask_print Unexecuted instantiation: uops_backend.c:ff_sws_comp_mask_print Unexecuted instantiation: ops_chain.c:ff_sws_comp_mask_print Unexecuted instantiation: uops.c:ff_sws_comp_mask_print |
94 | | |
95 | | typedef uint32_t SwsUOpFlags; |
96 | | typedef enum SwsUOpFlagBits { |
97 | | SWS_UOP_FLAG_NONE = 0, |
98 | | SWS_UOP_FLAG_FMA = (1 << 0), /* platform supports FMA ops */ |
99 | | } SwsUOpFlagBits; |
100 | | |
101 | | typedef enum SwsUOpType { |
102 | | SWS_UOP_INVALID = 0, |
103 | | |
104 | | /* Read/write uops; mask = components to read/write */ |
105 | | SWS_UOP_READ_PLANAR, /* simple planar byte-aligned read */ |
106 | | SWS_UOP_READ_PLANAR_FH, /* planar read with horizontal filter */ |
107 | | SWS_UOP_READ_PLANAR_FV, /* planar read with vertical filter */ |
108 | | SWS_UOP_READ_PLANAR_FV_FMA, |
109 | | SWS_UOP_READ_PACKED, /* simple packed byte-aligned read */ |
110 | | SWS_UOP_READ_NIBBLE, /* fractional read (4 bits) from single plane */ |
111 | | SWS_UOP_READ_BIT, /* fractional read (1 bit) from single plane */ |
112 | | SWS_UOP_READ_PALETTE, /* indexed read from palette in plane 1 */ |
113 | | |
114 | | SWS_UOP_WRITE_PLANAR, /* simple planar byte-aligned write */ |
115 | | SWS_UOP_WRITE_PACKED, /* simple packed byte-aligned write */ |
116 | | SWS_UOP_WRITE_NIBBLE, /* fractional write (4 bits) to single plane */ |
117 | | SWS_UOP_WRITE_BIT, /* fractional write (1 bit) to single plane */ |
118 | | |
119 | | /* Data rearrangement uops; mask = needed or trivial components */ |
120 | | SWS_UOP_PERMUTE, /* permute pointers (no duplicates) */ |
121 | | SWS_UOP_COPY, /* permute data (may contain duplicates) */ |
122 | | |
123 | | /* Data conversion / manipulation uops; mask = affected components */ |
124 | | SWS_UOP_SWAP_BYTES, /* swap byte order in components */ |
125 | | SWS_UOP_EXPAND_BIT, /* expand low-order bit to all bits in type */ |
126 | | SWS_UOP_EXPAND_PAIR, /* expand bytes in pairs (16 bit) */ |
127 | | SWS_UOP_EXPAND_QUAD, /* expand bytes in quads (32 bit) */ |
128 | | SWS_UOP_TO_U8, /* cast pixel values to SWS_PIXEL_U8 */ |
129 | | SWS_UOP_TO_U16, /* cast pixel values to SWS_PIXEL_U16 */ |
130 | | SWS_UOP_TO_U32, /* cast pixel values to SWS_PIXEL_U32 */ |
131 | | SWS_UOP_TO_F32, /* cast pixel values to SWS_PIXEL_F32 */ |
132 | | |
133 | | /* Arithmetic uops */ |
134 | | SWS_UOP_SCALE, /* multiply masked components by scalar */ |
135 | | SWS_UOP_ADD, /* add vec4 to masked components */ |
136 | | SWS_UOP_MIN, /* min(x, vec4) on masked components */ |
137 | | SWS_UOP_MAX, /* max(x, vec4) on masked components */ |
138 | | |
139 | | /* Identical to corresponding SwsOpType */ |
140 | | SWS_UOP_UNPACK, /* mask = nonzero components in pack pattern */ |
141 | | SWS_UOP_PACK, /* mask = nonzero components in pack pattern */ |
142 | | SWS_UOP_LSHIFT, /* mask = components to shift */ |
143 | | SWS_UOP_RSHIFT, /* mask = components to shift */ |
144 | | SWS_UOP_CLEAR, /* mask = components to clear */ |
145 | | SWS_UOP_LINEAR, /* mask = non-trivial output rows */ |
146 | | SWS_UOP_LINEAR_FMA, /* with SWS_UOP_FLAG_FMA */ |
147 | | SWS_UOP_DITHER, /* mask = components to dither */ |
148 | | |
149 | | /* Platform-specific uops would go here */ |
150 | | SWS_UOP_TYPE_NB, |
151 | | } SwsUOpType; |
152 | | |
153 | | typedef struct SwsFilterUOp { |
154 | | SwsPixelType type; /* pixel type to store result as */ |
155 | | } SwsFilterUOp; |
156 | | |
157 | | typedef struct SwsShiftUOp { |
158 | | uint8_t amount; |
159 | | } SwsShiftUOp; |
160 | | |
161 | | typedef struct SwsMoveUOp { |
162 | | /* The worst case number of moves (for two independent cycles) */ |
163 | | #define SWS_UOP_MOVE_MAX 6 |
164 | | int num_moves; |
165 | | |
166 | | /* This may involve a temporary register (index -1) */ |
167 | | int8_t dst[SWS_UOP_MOVE_MAX]; /* destination register index */ |
168 | | int8_t src[SWS_UOP_MOVE_MAX]; /* source register index */ |
169 | | } SwsMoveUOp; |
170 | | |
171 | | typedef struct SwsPackUOp { |
172 | | uint8_t pattern[4]; /* bit depth pattern, from MSB to LSB */ |
173 | | } SwsPackUOp; |
174 | | |
175 | | typedef struct SwsClearUOp { |
176 | | SwsCompMask one; /* mask of coefficients equal to all 1s */ |
177 | | SwsCompMask zero; /* mask of coefficients equal to all 0s */ |
178 | | } SwsClearUOp; |
179 | | |
180 | | typedef struct SwsLinearUOp { |
181 | | uint32_t one; /* mask of coefficients equal to one */ |
182 | | uint32_t zero; /* mask of coefficients equal to zero */ |
183 | | |
184 | | /* for SWS_UOP_LINEAR_FMA only */ |
185 | | uint32_t exact; /* mask of coefficients whose product is exact */ |
186 | | } SwsLinearUOp; |
187 | | |
188 | 0 | #define SWS_MASK(I, J) (1 << (5 * (I) + (J))) |
189 | | #define SWS_MASK_OFF(I) SWS_MASK(I, 4) |
190 | 0 | #define SWS_MASK_ROW(I) (0x1F << (5 * (I))) |
191 | 0 | #define SWS_MASK_COL(J) (0x8421 << J) |
192 | 0 | #define SWS_MASK_DIAG4 (0x41041) |
193 | | |
194 | | typedef struct SwsDitherUOp { |
195 | | uint8_t y_offset[4]; |
196 | | uint8_t size_log2; |
197 | | } SwsDitherUOp; |
198 | | |
199 | | /** |
200 | | * Computes (1 << size_log2) + MAX(y_offset). The dither matrix attached to |
201 | | * the SwsUOp is always pre-padded to this number of lines. |
202 | | */ |
203 | | int ff_sws_dither_height(const SwsDitherUOp *dither); |
204 | | |
205 | | typedef union SwsUOpParams { |
206 | | SwsFilterUOp filter; /* for SWS_UOP_READ_*_FV/FH */ |
207 | | SwsShiftUOp shift; |
208 | | SwsMoveUOp move; /* for SWS_UOP_PERMUTE and SWS_UOP_COPY */ |
209 | | SwsPackUOp pack; |
210 | | SwsClearUOp clear; |
211 | | SwsLinearUOp lin; |
212 | | SwsDitherUOp dither; |
213 | | } SwsUOpParams; |
214 | | |
215 | | typedef struct SwsUOp { |
216 | | /* These fields uniquely identify the uop implementation */ |
217 | | SwsPixelType type; |
218 | | SwsUOpType uop; |
219 | | SwsCompMask mask; |
220 | | SwsUOpParams par; |
221 | | |
222 | | /* Constant data for this uop; not part of the unique identifier */ |
223 | | union { |
224 | | SwsFilterWeights *kernel; /* refstruct */ |
225 | | SwsPixel *ptr; /* refstruct */ |
226 | | SwsPixel scalar; |
227 | | SwsPixel vec4[4]; |
228 | | SwsPixel mat4[4][5]; /* row major */ |
229 | | void *opaque; /* reserved for internal use */ |
230 | | } data; |
231 | | } SwsUOp; |
232 | | |
233 | | /** |
234 | | * Compare two SwsUOps for equality (excluding constant data). |
235 | | */ |
236 | | int ff_sws_uop_cmp(const SwsUOp *a, const SwsUOp *b); |
237 | | |
238 | | static inline int ff_sws_uop_cmp_v(const void *a, const void *b) |
239 | 0 | { |
240 | 0 | return ff_sws_uop_cmp(a, b); |
241 | 0 | } Unexecuted instantiation: format.c:ff_sws_uop_cmp_v Unexecuted instantiation: graph.c:ff_sws_uop_cmp_v Unexecuted instantiation: ops.c:ff_sws_uop_cmp_v Unexecuted instantiation: ops_dispatch.c:ff_sws_uop_cmp_v Unexecuted instantiation: ops_memcpy.c:ff_sws_uop_cmp_v Unexecuted instantiation: ops_optimizer.c:ff_sws_uop_cmp_v Unexecuted instantiation: uops_backend.c:ff_sws_uop_cmp_v Unexecuted instantiation: ops_chain.c:ff_sws_uop_cmp_v Unexecuted instantiation: uops.c:ff_sws_uop_cmp_v |
242 | | |
243 | | /** |
244 | | * Generate a unique name for a SwsUOp. |
245 | | */ |
246 | 0 | #define SWS_UOP_NAME_MAX 64 |
247 | | void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX]); |
248 | | |
249 | | typedef struct SwsUOpList { |
250 | | SwsUOp *ops; |
251 | | int num_ops; |
252 | | |
253 | | /* Additional metadata for implementations */ |
254 | | SwsCompMask planes_in; /* mask of planes read from */ |
255 | | SwsCompMask planes_out; /* mask of planes written to */ |
256 | | int pixel_size_max; /* size of largest pixel type seen in any uop */ |
257 | | } SwsUOpList; |
258 | | |
259 | | SwsUOpList *ff_sws_uop_list_alloc(void); |
260 | | void ff_sws_uop_list_free(SwsUOpList **ops); |
261 | | |
262 | | /* Takes over ownership of `uop` and sets it to {0}, even on failure. */ |
263 | | int ff_sws_uop_list_append(SwsUOpList *uops, SwsUOp *uop); |
264 | | |
265 | | /** |
266 | | * Translate a list of operations down to micro-ops, which can be further |
267 | | * optimized and then directly executed by backends. |
268 | | * |
269 | | * Return 0 or a negative error code. |
270 | | */ |
271 | | int ff_sws_ops_translate(SwsContext *ctx, const SwsOpList *ops, |
272 | | SwsUOpFlags flags, SwsUOpList *uops); |
273 | | |
274 | | #endif |