FFmpeg
Loading...
Searching...
No Matches
ops.c
Go to the documentation of this file.
1/**
2 * Copyright (C) 2025-2026 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 <float.h>
22
23#include "libavutil/avassert.h"
24#include "libavutil/mem.h"
25#include "libavutil/x86/cpu.h"
26
27#include "../ops_chain.h"
28#include "../uops.h"
29#include "uops_macros.h"
30
32{
33 const SwsUOp *uop = params->uop;
34
35 /* 3-component packed reads/writes process one extra garbage word */
36 if (uop->mask == SWS_COMP_ELEMS(3)) {
37 switch (uop->uop) {
38 case SWS_UOP_READ_PACKED: out->over_read[0] = sizeof(uint32_t); break;
39 case SWS_UOP_WRITE_PACKED: out->over_write[0] = sizeof(uint32_t); break;
40 }
41 }
42
43 return 0;
44}
45
46/* Private data for the filter kernels; layout hard-coded in ops_float.asm */
47typedef struct FilterPriv {
48 void *weights;
51
52static_assert(offsetof(FilterPriv, filter_size) == sizeof(void *),
53 "FilterPriv layout mismatch");
54
56{
57 const SwsFilterWeights *filter = params->uop->data.kernel;
58
59 /* Pre-convert weights to float */
60 float *weights = ff_refstruct_calloc(filter->num_weights, sizeof(float));
61 if (!weights)
62 return AVERROR(ENOMEM);
63
64 for (int i = 0; i < filter->num_weights; i++)
65 weights[i] = (float) filter->weights[i] / SWS_FILTER_SCALE;
66
67 const FilterPriv priv = {
68 .weights = weights,
69 .filter_size = filter->filter_size,
70 };
71
72 SWS_OP_PRIV_PACK(out, priv);
73 out->ref = priv.weights;
74 return 0;
75}
76
77static int hscale_sizeof_weight(const SwsUOp *uop)
78{
79 switch (uop->type) {
80 case SWS_PIXEL_U8: return sizeof(int16_t);
81 case SWS_PIXEL_U16: return sizeof(int16_t);
82 case SWS_PIXEL_F32: return sizeof(float);
83 default: return 0;
84 }
85}
86
88{
89 const SwsUOp *uop = params->uop;
90 const SwsFilterWeights *filter = uop->data.kernel;
91
92 /**
93 * `vpgatherdd` gathers 32 bits at a time; so if we're filtering a smaller
94 * size, we need to gather 2/4 taps simultaneously and unroll the inner
95 * loop over several packed samples.
96 */
97 const int pixel_size = ff_sws_pixel_type_size(uop->type);
98 const int taps_align = sizeof(int32_t) / pixel_size;
99 const int filter_size = filter->filter_size;
100 const int block_size = params->table->block_size;
101 const size_t aligned_size = FFALIGN(filter_size, taps_align);
102 const size_t line_size = FFALIGN(filter->dst_size, block_size);
103 av_assert1(FFALIGN(line_size, taps_align) == line_size);
104 if (aligned_size > INT_MAX)
105 return AVERROR(EINVAL);
106
107 union {
108 void *ptr;
109 int16_t *i16;
110 float *f32;
111 } weights;
112
113 const int sizeof_weight = hscale_sizeof_weight(uop);
114 weights.ptr = ff_refstruct_calloc(line_size, sizeof_weight * aligned_size);
115 if (!weights.ptr)
116 return AVERROR(ENOMEM);
117
118 /**
119 * Transpose filter weights to group (aligned) taps by block
120 */
121 const int mmsize = block_size * 2;
122 const int gather_size = mmsize / sizeof(int32_t); /* pixels per vpgatherdd */
123 for (size_t x = 0; x < line_size; x += block_size) {
124 const int elems = FFMIN(block_size, filter->dst_size - x);
125 for (int j = 0; j < filter_size; j++) {
126 const int jb = j & ~(taps_align - 1);
127 const int ji = j - jb;
128 const size_t idx_base = x * aligned_size + jb * block_size + ji;
129 for (int i = 0; i < elems; i++) {
130 const int w = filter->weights[(x + i) * filter_size + j];
131 size_t idx = idx_base;
132 if (uop->type == SWS_PIXEL_U8) {
133 /* Interleave the pixels within each lane, i.e.:
134 * [a0 a1 a2 a3 | b0 b1 b2 b3 ] pixels 0-1, taps 0-3 (lane 0)
135 * [e0 e1 e2 e3 | f0 f1 f2 f3 ] pixels 4-5, taps 0-3 (lane 1)
136 * [c0 c1 c2 c3 | d0 d1 d2 d3 ] pixels 2-3, taps 0-3 (lane 0)
137 * [g0 g1 g2 g3 | h0 h1 h2 h3 ] pixels 6-7, taps 0-3 (lane 1)
138 * [i0 i1 i2 i3 | j0 j1 j2 j3 ] pixels 8-9, taps 0-3 (lane 0)
139 * ...
140 * [o0 o1 o2 o3 | p0 p1 p2 p3 ] pixels 14-15, taps 0-3 (lane 1)
141 * (repeat for taps 4-7, etc.)
142 */
143 const int gather_base = i & ~(gather_size - 1);
144 const int gather_pos = i - gather_base;
145 const int lane_idx = gather_pos >> 2;
146 const int pos_in_lane = gather_pos & 3;
147 idx += gather_base * 4 /* which gather (m0 or m1) */
148 + (pos_in_lane >> 1) * (mmsize / 2) /* lo/hi unpack */
149 + lane_idx * 8 /* 8 ints per lane */
150 + (pos_in_lane & 1) * 4; /* 4 taps per pair */
151 } else {
152 idx += i * taps_align;
153 }
154
155 switch (uop->type) {
156 case SWS_PIXEL_U8: weights.i16[idx] = w; break;
157 case SWS_PIXEL_U16: weights.i16[idx] = w; break;
158 case SWS_PIXEL_F32: weights.f32[idx] = w; break;
159 }
160 }
161 }
162 }
163
164 const FilterPriv priv = {
165 .weights = weights.ptr,
166 .filter_size = aligned_size,
167 };
168
169 SWS_OP_PRIV_PACK(out, priv);
170 out->ref = priv.weights;
171
172 for (int i = 0; i < 4; i++) {
173 if (uop->mask & SWS_COMP(i))
174 out->over_read[i] = (aligned_size - filter_size) * pixel_size;
175 }
176 return 0;
177}
178
180{
181 SwsContext *ctx = params->ctx;
182 const SwsUOp *uop = params->uop;
183 if ((ctx->flags & SWS_BITEXACT) && uop->type == SWS_PIXEL_F32)
184 return false; /* different accumulation order due to 4x4 transpose */
185
186 const int cpu_flags = av_get_cpu_flags();
188 return true; /* always prefer over gathers if gathers are slow */
189
190 /**
191 * Otherwise, prefer it above a certain filter size. Empirically, this
192 * kernel seems to be faster whenever the reference/gather kernel crosses
193 * a breakpoint for the number of gathers needed, but this filter doesn't.
194 *
195 * Tested on a Lunar Lake (Intel Core Ultra 7 258V) system.
196 */
197 const SwsFilterWeights *filter = uop->data.kernel;
198 return uop->type == SWS_PIXEL_U8 && filter->filter_size > 12 ||
199 uop->type == SWS_PIXEL_U16 && filter->filter_size > 4 ||
200 uop->type == SWS_PIXEL_F32 && filter->filter_size > 1;
201}
202
204{
205 const SwsUOp *uop = params->uop;
206 const SwsFilterWeights *filter = uop->data.kernel;
207 const int pixel_size = ff_sws_pixel_type_size(uop->type);
208 const int sizeof_weights = hscale_sizeof_weight(uop);
209 const int block_size = params->table->block_size;
210 const int taps_align = 16 / sizeof_weights; /* taps per iteration (XMM) */
211 const int pixels_align = 4; /* pixels per iteration */
212 const int filter_size = filter->filter_size;
213 const size_t aligned_size = FFALIGN(filter_size, taps_align);
214 const int line_size = FFALIGN(filter->dst_size, block_size);
215 av_assert1(FFALIGN(line_size, pixels_align) == line_size);
216
217 union {
218 void *ptr;
219 int16_t *i16;
220 float *f32;
221 } weights;
222
223 weights.ptr = ff_refstruct_calloc(line_size, aligned_size * sizeof_weights);
224 if (!weights.ptr)
225 return AVERROR(ENOMEM);
226
227 /**
228 * Desired memory layout: [w][taps][pixels_align][taps_align]
229 *
230 * Example with taps_align=8, pixels_align=4:
231 * [a0, a1, ... a7] weights for pixel 0, taps 0..7
232 * [b0, b1, ... b7] weights for pixel 1, taps 0..7
233 * [c0, c1, ... c7] weights for pixel 2, taps 0..7
234 * [d0, d1, ... d7] weights for pixel 3, taps 0..7
235 * [a8, a9, ... a15] weights for pixel 0, taps 8..15
236 * ...
237 * repeat for all taps, then move on to pixels 4..7, etc.
238 */
239 for (int x = 0; x < filter->dst_size; x++) {
240 for (int j = 0; j < filter_size; j++) {
241 const int xb = x & ~(pixels_align - 1);
242 const int jb = j & ~(taps_align - 1);
243 const int xi = x - xb, ji = j - jb;
244 const int w = filter->weights[x * filter_size + j];
245 const int idx = xb * aligned_size + jb * pixels_align + xi * taps_align + ji;
246
247 switch (uop->type) {
248 case SWS_PIXEL_U8: weights.i16[idx] = w; break;
249 case SWS_PIXEL_U16: weights.i16[idx] = w; break;
250 case SWS_PIXEL_F32: weights.f32[idx] = w; break;
251 }
252 }
253 }
254
255 const FilterPriv priv = {
256 .weights = weights.ptr,
257 .filter_size = aligned_size * sizeof_weights,
258 };
259
260 SWS_OP_PRIV_PACK(out, priv);
261 out->ref = priv.weights;
262
263 for (int i = 0; i < 4; i++) {
264 if (uop->mask & SWS_COMP(i))
265 out->over_read[i] = (aligned_size - filter_size) * pixel_size;
266 }
267 return 0;
268}
269
271{
272 const SwsPixel scalar = params->uop->data.scalar;
273 switch (params->uop->type) {
274 case SWS_PIXEL_U8: {
275 const uint32_t u32 = scalar.u8; /* for movd */
276 SWS_OP_PRIV_PACK(out, u32);
277 break;
278 }
279 case SWS_PIXEL_U16: SWS_OP_PRIV_PACK(out, scalar.u16); break;
280 case SWS_PIXEL_U32: SWS_OP_PRIV_PACK(out, scalar.u32); break;
281 case SWS_PIXEL_F32: SWS_OP_PRIV_PACK(out, scalar.f32); break;
282 default: return AVERROR(EINVAL);
283 }
284
285 return 0;
286}
287
289{
290 const SwsUOp *uop = params->uop;
291 for (int i = 0; i < 4; i++)
293 return 0;
294}
295
297{
299 return 0;
300}
301
302static void splat_lane(void *dst, SwsPixelType type, SwsPixel px)
303{
304 switch (ff_sws_pixel_type_size(type)) {
305 case 1:
306 memset(dst, px.u8, 16);
307 break;
308 case 2:
309 for (int i = 0; i < 8; i++)
310 ((uint16_t *) dst)[i] = px.u16;
311 break;
312 case 4:
313 for (int i = 0; i < 4; i++)
314 ((uint32_t *) dst)[i] = px.u32;
315 break;
316 }
317}
318
320{
321 const SwsUOp *uop = params->uop;
322 if (uop->type == SWS_PIXEL_F32) {
323 /* Layout hard-coded in ops_float.asm */
324 static_assert(sizeof(uop->data.mat4x5) == 4 * 5 * sizeof(float),
325 "mat4x5 layout mismatch");
327 return 0;
328 }
329
330 uint8_t *mat = av_refstruct_allocz(4 * 5 * 16); /* one lane per component */
331 if (!mat)
332 return AVERROR(ENOMEM);
334
335 for (int i = 0; i < 4; i++) {
336 for (int j = 0; j < 5; j++) {
337 SwsPixel px = uop->data.mat4x5[i][j];
338 SwsPixelType type = uop->type;
339 if (type == SWS_PIXEL_U8) {
340 type = SWS_PIXEL_U16; /* for pmullw */
341 px.u16 = (px.u8 << 8) | px.u8;
342 }
343
344 splat_lane(mat, type, px);
345 mat += 16;
346 }
347 }
348 return 0;
349}
350
351static bool uop_is_type_invariant(const SwsUOpType uop)
352{
353 switch (uop) {
356 case SWS_UOP_CLEAR:
357 return true;
358 default:
359 return false;
360 }
361}
362
363#define REF_ENTRY(EXT, NAME, ...) &uop_##NAME##EXT,
364#define DECL_ENTRY(EXT, CHECK, SETUP, NAME, ...) \
365 void ff_##NAME##EXT(void); \
366 static const SwsUOpEntry uop_##NAME##EXT = { \
367 .func = (SwsFuncPtr) ff_##NAME##EXT, \
368 .check = CHECK, \
369 .setup = SETUP, \
370 __VA_ARGS__, \
371 };
372
373/* Define all UOPs except conversion ops and type-invariant ops */
374#define DECL_OPS_COMMON(EXT, TYPE) \
375SWS_FOR_STRUCT(TYPE, READ_PACKED, DECL_ENTRY, EXT, NULL, setup_rw_packed) \
376SWS_FOR_STRUCT(TYPE, READ_NIBBLE, DECL_ENTRY, EXT, NULL, NULL) \
377SWS_FOR_STRUCT(TYPE, READ_BIT, DECL_ENTRY, EXT, NULL, NULL) \
378SWS_FOR_STRUCT(TYPE, READ_PALETTE, DECL_ENTRY, EXT, NULL, NULL) \
379SWS_FOR_STRUCT(TYPE, WRITE_PACKED, DECL_ENTRY, EXT, NULL, setup_rw_packed) \
380SWS_FOR_STRUCT(TYPE, WRITE_NIBBLE, DECL_ENTRY, EXT, NULL, NULL) \
381SWS_FOR_STRUCT(TYPE, WRITE_BIT, DECL_ENTRY, EXT, NULL, NULL) \
382SWS_FOR_STRUCT(TYPE, SWAP_BYTES, DECL_ENTRY, EXT, NULL, NULL) \
383SWS_FOR_STRUCT(TYPE, EXPAND_BIT, DECL_ENTRY, EXT, NULL, NULL) \
384SWS_FOR_STRUCT(TYPE, PERMUTE, DECL_ENTRY, EXT, NULL, NULL) \
385SWS_FOR_STRUCT(TYPE, COPY, DECL_ENTRY, EXT, NULL, NULL) \
386SWS_FOR_STRUCT(TYPE, SCALE, DECL_ENTRY, EXT, NULL, setup_scale) \
387SWS_FOR_STRUCT(TYPE, ADD, DECL_ENTRY, EXT, NULL, ff_sws_setup_vec4) \
388SWS_FOR_STRUCT(TYPE, MIN, DECL_ENTRY, EXT, NULL, ff_sws_setup_vec4) \
389SWS_FOR_STRUCT(TYPE, MAX, DECL_ENTRY, EXT, NULL, ff_sws_setup_vec4) \
390SWS_FOR_STRUCT(TYPE, UNPACK, DECL_ENTRY, EXT, NULL, NULL) \
391SWS_FOR_STRUCT(TYPE, PACK, DECL_ENTRY, EXT, NULL, NULL) \
392SWS_FOR_STRUCT(TYPE, LSHIFT, DECL_ENTRY, EXT, NULL, NULL) \
393SWS_FOR_STRUCT(TYPE, RSHIFT, DECL_ENTRY, EXT, NULL, NULL) \
394SWS_FOR_STRUCT(TYPE, LINEAR, DECL_ENTRY, EXT, NULL, setup_linear) \
395SWS_FOR_STRUCT(TYPE, LINEAR_FMA, DECL_ENTRY, EXT, NULL, setup_linear) \
396SWS_FOR_STRUCT(TYPE, DITHER, DECL_ENTRY, EXT, NULL, setup_dither) \
397/* end of macro */
398
399#define REF_OPS_COMMON(EXT, TYPE) \
400 SWS_FOR(TYPE, READ_PACKED, REF_ENTRY, EXT) \
401 SWS_FOR(TYPE, READ_NIBBLE, REF_ENTRY, EXT) \
402 SWS_FOR(TYPE, READ_BIT, REF_ENTRY, EXT) \
403 SWS_FOR(TYPE, READ_PALETTE, REF_ENTRY, EXT) \
404 SWS_FOR(TYPE, WRITE_PACKED, REF_ENTRY, EXT) \
405 SWS_FOR(TYPE, WRITE_NIBBLE, REF_ENTRY, EXT) \
406 SWS_FOR(TYPE, WRITE_BIT, REF_ENTRY, EXT) \
407 SWS_FOR(TYPE, SWAP_BYTES, REF_ENTRY, EXT) \
408 SWS_FOR(TYPE, EXPAND_BIT, REF_ENTRY, EXT) \
409 SWS_FOR(TYPE, PERMUTE, REF_ENTRY, EXT) \
410 SWS_FOR(TYPE, COPY, REF_ENTRY, EXT) \
411 SWS_FOR(TYPE, SCALE, REF_ENTRY, EXT) \
412 SWS_FOR(TYPE, ADD, REF_ENTRY, EXT) \
413 SWS_FOR(TYPE, MIN, REF_ENTRY, EXT) \
414 SWS_FOR(TYPE, MAX, REF_ENTRY, EXT) \
415 SWS_FOR(TYPE, UNPACK, REF_ENTRY, EXT) \
416 SWS_FOR(TYPE, PACK, REF_ENTRY, EXT) \
417 SWS_FOR(TYPE, LSHIFT, REF_ENTRY, EXT) \
418 SWS_FOR(TYPE, RSHIFT, REF_ENTRY, EXT) \
419 SWS_FOR(TYPE, LINEAR, REF_ENTRY, EXT) \
420 SWS_FOR(TYPE, LINEAR_FMA, REF_ENTRY, EXT) \
421 SWS_FOR(TYPE, DITHER, REF_ENTRY, EXT) \
422 /* end of macro */
423
424#define DECL_TABLE_U8(EXT, SIZE, FLAG) \
425DECL_OPS_COMMON(EXT, U8) \
426SWS_FOR_STRUCT(U8, READ_PLANAR, DECL_ENTRY, EXT, NULL, NULL) \
427SWS_FOR_STRUCT(U8, WRITE_PLANAR, DECL_ENTRY, EXT, NULL, NULL) \
428SWS_FOR_STRUCT(U8, CLEAR, DECL_ENTRY, EXT, NULL, setup_clear) \
429 \
430static const SwsUOpTable uops_u8##EXT = { \
431 .cpu_flags = AV_CPU_FLAG_##FLAG, \
432 .block_size = SIZE, \
433 .entries = { \
434 REF_OPS_COMMON(EXT, U8) \
435 SWS_FOR(U8, READ_PLANAR, REF_ENTRY, EXT) \
436 SWS_FOR(U8, WRITE_PLANAR, REF_ENTRY, EXT) \
437 SWS_FOR(U8, CLEAR, REF_ENTRY, EXT) \
438 NULL \
439 }, \
440};
441
442#define DECL_TABLE_U16(EXT, SIZE, FLAG) \
443DECL_OPS_COMMON(EXT, U16) \
444SWS_FOR_STRUCT(U8, TO_U16, DECL_ENTRY, EXT, NULL, NULL) \
445SWS_FOR_STRUCT(U16, TO_U8, DECL_ENTRY, EXT, NULL, NULL) \
446SWS_FOR_STRUCT(U8, EXPAND_PAIR, DECL_ENTRY, EXT, NULL, NULL) \
447 \
448static const SwsUOpTable uops_u16##EXT = { \
449 .cpu_flags = AV_CPU_FLAG_##FLAG, \
450 .block_size = SIZE, \
451 .entries = { \
452 REF_OPS_COMMON(EXT, U16) \
453 SWS_FOR(U8, TO_U16, REF_ENTRY, EXT) \
454 SWS_FOR(U16, TO_U8, REF_ENTRY, EXT) \
455 SWS_FOR(U8, EXPAND_PAIR, REF_ENTRY, EXT) \
456 NULL \
457 }, \
458};
459
460#define DECL_TABLE_U32(EXT, SIZE, FLAG) \
461DECL_OPS_COMMON(EXT, U32) \
462SWS_FOR_STRUCT(U8, TO_U32, DECL_ENTRY, EXT, NULL, NULL) \
463SWS_FOR_STRUCT(U32, TO_U8, DECL_ENTRY, EXT, NULL, NULL) \
464SWS_FOR_STRUCT(U16, TO_U32, DECL_ENTRY, EXT, NULL, NULL) \
465SWS_FOR_STRUCT(U32, TO_U16, DECL_ENTRY, EXT, NULL, NULL) \
466SWS_FOR_STRUCT(U8, EXPAND_QUAD, DECL_ENTRY, EXT, NULL, NULL) \
467 \
468static const SwsUOpTable uops_u32##EXT = { \
469 .cpu_flags = AV_CPU_FLAG_##FLAG, \
470 .block_size = SIZE, \
471 .entries = { \
472 REF_OPS_COMMON(EXT, U32) \
473 SWS_FOR(U8, TO_U32, REF_ENTRY, EXT) \
474 SWS_FOR(U32, TO_U8, REF_ENTRY, EXT) \
475 SWS_FOR(U16, TO_U32, REF_ENTRY, EXT) \
476 SWS_FOR(U32, TO_U16, REF_ENTRY, EXT) \
477 SWS_FOR(U8, EXPAND_QUAD, REF_ENTRY, EXT) \
478 NULL \
479 }, \
480};
481
482#define DECL_TABLE_F32(EXT, SIZE, FLAG) \
483DECL_OPS_COMMON(EXT, F32) \
484SWS_FOR_STRUCT(U8, TO_F32, DECL_ENTRY, EXT, NULL, NULL) \
485SWS_FOR_STRUCT(F32, TO_U8, DECL_ENTRY, EXT, NULL, NULL) \
486SWS_FOR_STRUCT(U16, TO_F32, DECL_ENTRY, EXT, NULL, NULL) \
487SWS_FOR_STRUCT(F32, TO_U16, DECL_ENTRY, EXT, NULL, NULL) \
488SWS_FOR_STRUCT(U8, READ_PLANAR_FH, DECL_ENTRY, EXT, NULL, setup_filter_h) \
489SWS_FOR_STRUCT(U16, READ_PLANAR_FH, DECL_ENTRY, EXT, NULL, setup_filter_h) \
490SWS_FOR_STRUCT(F32, READ_PLANAR_FH, DECL_ENTRY, EXT, NULL, setup_filter_h) \
491SWS_FOR_STRUCT(U8, READ_PLANAR_FH, DECL_ENTRY, _4x4##EXT, \
492 check_filter_h_4x4, setup_filter_h_4x4) \
493SWS_FOR_STRUCT(U16, READ_PLANAR_FH, DECL_ENTRY, _4x4##EXT, \
494 check_filter_h_4x4, setup_filter_h_4x4) \
495SWS_FOR_STRUCT(F32, READ_PLANAR_FH, DECL_ENTRY, _4x4##EXT, \
496 check_filter_h_4x4, setup_filter_h_4x4) \
497SWS_FOR_STRUCT(U8, READ_PLANAR_FV, DECL_ENTRY, EXT, NULL, setup_filter_v) \
498SWS_FOR_STRUCT(U16, READ_PLANAR_FV, DECL_ENTRY, EXT, NULL, setup_filter_v) \
499SWS_FOR_STRUCT(F32, READ_PLANAR_FV, DECL_ENTRY, EXT, NULL, setup_filter_v) \
500SWS_FOR_STRUCT(U8, READ_PLANAR_FV_FMA, DECL_ENTRY, EXT, NULL, setup_filter_v) \
501SWS_FOR_STRUCT(U16, READ_PLANAR_FV_FMA, DECL_ENTRY, EXT, NULL, setup_filter_v) \
502SWS_FOR_STRUCT(F32, READ_PLANAR_FV_FMA, DECL_ENTRY, EXT, NULL, setup_filter_v) \
503 \
504static const SwsUOpTable uops_f32##EXT = { \
505 .cpu_flags = AV_CPU_FLAG_##FLAG, \
506 .block_size = SIZE, \
507 .entries = { \
508 REF_OPS_COMMON(EXT, F32) \
509 SWS_FOR(U8, TO_F32, REF_ENTRY, EXT) \
510 SWS_FOR(F32, TO_U8, REF_ENTRY, EXT) \
511 SWS_FOR(U16, TO_F32, REF_ENTRY, EXT) \
512 SWS_FOR(F32, TO_U16, REF_ENTRY, EXT) \
513 SWS_FOR(U8, READ_PLANAR_FH, REF_ENTRY, _4x4##EXT) \
514 SWS_FOR(U16, READ_PLANAR_FH, REF_ENTRY, _4x4##EXT) \
515 SWS_FOR(F32, READ_PLANAR_FH, REF_ENTRY, _4x4##EXT) \
516 SWS_FOR(U8, READ_PLANAR_FH, REF_ENTRY, EXT) \
517 SWS_FOR(U16, READ_PLANAR_FH, REF_ENTRY, EXT) \
518 SWS_FOR(F32, READ_PLANAR_FH, REF_ENTRY, EXT) \
519 SWS_FOR(U8, READ_PLANAR_FV, REF_ENTRY, EXT) \
520 SWS_FOR(U16, READ_PLANAR_FV, REF_ENTRY, EXT) \
521 SWS_FOR(F32, READ_PLANAR_FV, REF_ENTRY, EXT) \
522 SWS_FOR(U8, READ_PLANAR_FV_FMA, REF_ENTRY, EXT) \
523 SWS_FOR(U16, READ_PLANAR_FV_FMA, REF_ENTRY, EXT) \
524 SWS_FOR(F32, READ_PLANAR_FV_FMA, REF_ENTRY, EXT) \
525 NULL \
526 }, \
527};
528
529DECL_TABLE_U8( _m1_sse4, 16, SSE4)
530DECL_TABLE_U8( _m1_avx2, 32, AVX2)
531DECL_TABLE_U8( _m2_sse4, 32, SSE4)
532DECL_TABLE_U8( _m2_avx2, 64, AVX2)
533DECL_TABLE_U16(_m1_avx2, 16, AVX2)
534DECL_TABLE_U16(_m2_avx2, 32, AVX2)
535DECL_TABLE_U32(_m2_avx2, 16, AVX2)
536DECL_TABLE_F32(_m2_avx2, 16, AVX2)
537
538static const SwsUOpTable *const tables[] = {
539 &uops_u8_m1_sse4,
540 &uops_u8_m1_avx2, /* order before _m2_sse4 */
541 &uops_u8_m2_sse4,
542 &uops_u8_m2_avx2,
543 &uops_u16_m1_avx2,
544 &uops_u16_m2_avx2,
545 &uops_u32_m2_avx2,
546 &uops_f32_m2_avx2,
547};
548
549SWS_DECL_FUNC(ff_sws_process1_x86);
550SWS_DECL_FUNC(ff_sws_process2_x86);
551SWS_DECL_FUNC(ff_sws_process3_x86);
552SWS_DECL_FUNC(ff_sws_process4_x86);
553
554/* Declare packed shuffle functions */
555SWS_FOR_STRUCT(U8, RW_SHUFFLE, DECL_ENTRY, _sse4, NULL, NULL)
556SWS_FOR_STRUCT(U8, RW_SHUFFLE, DECL_ENTRY, _avx2, NULL, NULL)
557SWS_FOR_STRUCT(U8, RW_SHUFFLE, DECL_ENTRY, _avx512, NULL, NULL)
558SWS_FOR_STRUCT(U8, RW_SHUFFLE, DECL_ENTRY, _avx512icl, NULL, NULL)
559
560static int get_mmsize(void)
561{
562 const int cpu_flags = av_get_cpu_flags();
564 return 64;
565 else if (EXTERNAL_AVX2(cpu_flags))
566 return 32;
567 else if (EXTERNAL_SSE4(cpu_flags))
568 return 16;
569 else
570 return AVERROR(ENOTSUP);
571}
572
573static int movsize(const int bytes, const int mmsize)
574{
575 return bytes <= 4 ? 4 : /* movd */
576 bytes <= 8 ? 8 : /* movq */
577 bytes <= 16 ? 16 : /* xmm movu */
578 bytes <= 32 ? 32 : /* ymm movu */
579 mmsize; /* zmm movu */
580}
581
582static int translate_shuffle(const SwsUOp *uop, int mmsize, SwsCompiledOp *out)
583{
584 /* We can't shuffle across lanes, so restrict the vector size to XMM
585 * whenever the read/write size would be a subset of the full vector,
586 * unless we have access to AVX-512 ICL vpermb */
587 const SwsShuffleUOp *par = &uop->par.shuffle;
588 const int lane_aligned = par->read_size == par->write_size &&
589 16 % par->read_size == 0;
590 if (!lane_aligned && !EXTERNAL_AVX512ICL(av_get_cpu_flags()))
591 mmsize = 16;
592
593 /* Generate the shuffle mask */
594 const int mask_size = lane_aligned ? 16 : mmsize;
595 int8_t *mask = av_malloc(mask_size);
596 if (!mask)
597 return AVERROR(ENOMEM);
598
599 const int groups = ff_sws_shuffle_mask(uop, mask, mask_size);
600 if (groups < 0) {
601 av_free(mask);
602 return groups;
603 }
604
605 const int read_chunk = groups * par->read_size;
606 const int write_chunk = groups * par->write_size;
607 const int num_lanes = lane_aligned ? mmsize / 16 : 1;
608 const int in_total = num_lanes * read_chunk;
609 const int out_total = num_lanes * write_chunk;
610 *out = (SwsCompiledOp) {
611 .priv = mask,
612 .free = av_free,
613 .slice_align = 1,
614 .block_size = groups * uop->data.shuffle.pixels * num_lanes,
615 .over_read = { movsize(in_total, mmsize) - in_total },
616 .over_write = { movsize(out_total, mmsize) - out_total },
617 };
618
619#define ASSIGN_SHUFFLE_FUNC(CPU, EXT, NAME, ...) \
620do { \
621 const SwsUOpEntry *entry = &uop_##NAME##EXT; \
622 if (!memcmp(&uop->par, &entry->par, sizeof(uop->par))) { \
623 out->func = (SwsOpFunc) entry->func; \
624 out->cpu_flags = AV_CPU_FLAG_##CPU; \
625 } \
626} while (0);
627
628 switch (mmsize) {
629 case 16: SWS_FOR(U8, RW_SHUFFLE, ASSIGN_SHUFFLE_FUNC, SSE4, _sse4); break;
630 case 32: SWS_FOR(U8, RW_SHUFFLE, ASSIGN_SHUFFLE_FUNC, AVX2, _avx2); break;
631 case 64:
632 if (lane_aligned) {
633 SWS_FOR(U8, RW_SHUFFLE, ASSIGN_SHUFFLE_FUNC, AVX512, _avx512);
634 } else { /* vpermb variant */
635 SWS_FOR(U8, RW_SHUFFLE, ASSIGN_SHUFFLE_FUNC, AVX512ICL, _avx512icl);
636 }
637 break;
638 }
639
640 if (!out->func) {
641 av_free(mask);
642 return AVERROR(ENOTSUP);
643 }
644
645 return 0;
646}
647
649{
650 int ret, mmsize = get_mmsize();
651 if (mmsize < 0)
652 return mmsize;
653
654 if (uops->num_ops == 1 && uops->ops[0].uop == SWS_UOP_RW_SHUFFLE) {
655 const SwsUOp *uop = &uops->ops[0];
656 ret = translate_shuffle(uop, mmsize, out);
657 if (ret >= 0) {
659 ff_sws_uop_name(uop, name);
660 av_log(ctx, AV_LOG_VERBOSE, "Using x86 packed shuffle fast path: %s\n", name);
661 }
662 return ret;
663 }
664
666 if (!chain)
667 return AVERROR(ENOMEM);
668
669 *out = (SwsCompiledOp) {
670 /* Use at most two full YMM regs during the widest precision section */
671 .block_size = 2 * FFMIN(mmsize, 32) / uops->pixel_size_max,
672 .slice_align = 1,
673 };
674
675 for (int i = 0; i < uops->num_ops; i++) {
676 SwsUOp *uop = &uops->ops[i];
677 int op_block_size = out->block_size;
678
679 if (uop_is_type_invariant(uop->uop)) {
680 op_block_size *= ff_sws_pixel_type_size(uop->type);
681 uop->type = SWS_PIXEL_U8;
682 }
683
685 op_block_size, chain);
686 if (ret < 0)
687 goto fail;
688 }
689
690 switch (av_popcount(uops->planes_in | uops->planes_out)) {
691 case 1: out->func = ff_sws_process1_x86; break;
692 case 2: out->func = ff_sws_process2_x86; break;
693 case 3: out->func = ff_sws_process3_x86; break;
694 case 4: out->func = ff_sws_process4_x86; break;
695 }
696
699 return 0;
700
701fail:
703 return ret;
704}
705
707{
708 const int cpu_flags = av_get_cpu_flags();
709 const int mmsize = get_mmsize();
710 if (mmsize < 0)
711 return mmsize;
712
720
722 if (!uops)
723 return AVERROR(ENOMEM);
724
725 int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
726 if (ret < 0)
727 goto fail;
728
729 ret = compile_uops_x86(ctx, uops, out);
730
731fail:
733 return ret;
734}
735
737 .name = "x86",
738 .flags = SWS_BACKEND_X86,
739 .compile = compile_ops_x86,
740 .compile_uops = compile_uops_x86,
741 .hw_format = AV_PIX_FMT_NONE,
742};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
SwsAArch64OpImplParams params
Definition ops.c:52
static FILE * out
static AVFormatContext * ctx
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define xi(width, name, var, range_min, range_max, subs,...)
Definition cbs_h264.c:115
#define av_popcount
Definition common.h:154
#define NULL
Definition coverity.c:32
static int read_chunk(AVFormatContext *s)
Definition dhav.c:173
static const OptionGroupDef groups[]
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
@ SWS_BACKEND_X86
Chained x86 SIMD kernels.
Definition swscale.h:118
@ SWS_BITEXACT
Definition swscale.h:178
static const int weights[]
Definition hevc_pel.c:32
enum MPFType type
Definition jpegmpfenc.c:55
static atomic_int cpu_flags
Definition cpu.c:56
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition cpu.c:109
#define AV_CPU_FLAG_SLOW_GATHER
CPU has slow gathers.
Definition cpu.h:63
#define EXTERNAL_AVX512ICL(flags)
Definition cpu.h:79
#define EXTERNAL_FMA3(flags)
Definition cpu.h:69
#define EXTERNAL_SSE4(flags)
Definition cpu.h:63
#define EXTERNAL_AVX512(flags)
Definition cpu.h:78
#define EXTERNAL_AVX2(flags)
Definition cpu.h:73
@ SWS_FILTER_SCALE
14-bit coefficients are picked to fit comfortably within int16_t for efficient SIMD processing (e....
Definition filters.h:40
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
const SwsOpBackend backend_x86
Definition ops.c:736
SwsOpChain * ff_sws_op_chain_alloc(void)
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.c:27
void ff_sws_op_chain_attach(const SwsOpChain *chain, SwsCompiledOp *out)
Attach a SwsOpChain and all metadata to a SwsCompiledOp (as out->priv).
Definition ops_chain.c:67
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:77
static void ff_sws_op_priv_pack_refptr(SwsImplResult *out, const void *data)
Definition ops_chain.h:144
static void ff_sws_op_chain_free(SwsOpChain *chain)
Definition ops_chain.h:85
#define SWS_OP_PRIV_PACK(out, data)
Definition ops_chain.h:136
#define SWS_DECL_FUNC(NAME)
int ff_sws_shuffle_mask(const SwsUOp *uop, int8_t shuffle[], int size)
Compute a shuffle mask for pshufb-style ASM functions, by repeating the shuffle pattern for as many g...
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
const char * name
Definition qsvenc.c:142
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:141
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition refstruct.h:105
#define FF_ARRAY_ELEMS(a)
size_t filter_size
Definition ops.c:49
void * weights
Definition ops.c:48
Main external API structure.
Definition swscale.h:227
Represents a computed filter kernel.
Definition filters.h:91
Compiled "chain" of operations, which can be dispatched efficiently.
Definition ops_chain.h:66
Helper struct for representing a list of operations.
Definition ops.h:318
uint8_t pixels
Definition uops.h:233
uint8_t write_size
Definition uops.h:228
uint8_t read_size
Definition uops.h:227
SwsUOp * ops
Definition uops.h:345
int pixel_size_max
Definition uops.h:351
int num_ops
Definition uops.h:346
SwsCompMask planes_in
Definition uops.h:349
SwsCompMask planes_out
Definition uops.h:350
Copyright (C) 2025 Niklas Haas.
Definition ops_chain.h:151
Definition uops.h:308
SwsCompMask mask
Definition uops.h:312
SwsUOpType uop
Definition uops.h:311
SwsUOpParams par
Definition uops.h:313
SwsFilterWeights * kernel
Definition uops.h:317
union SwsUOp::@242237116251216327057105100216205033300341206345 data
SwsPixelType type
Definition uops.h:310
SwsShuffleMask shuffle
Definition uops.h:322
SwsPixel mat4x5[4][5]
Definition uops.h:321
SwsPixel vec4[4]
Definition uops.h:320
static void * ff_refstruct_calloc(size_t n, size_t size)
#define av_free(p)
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static const uint8_t *const tables[]
float f32
Definition uops.h:85
uint32_t u32
Definition uops.h:84
uint8_t u8
Definition uops.h:82
uint16_t u16
Definition uops.h:83
SwsShuffleUOp shuffle
Definition uops.h:297
int ff_sws_ops_translate(SwsContext *ctx, const SwsOpList *ops, SwsUOpFlags flags, SwsUOpList *uops)
Translate a list of operations down to micro-ops, which can be further optimized and then directly ex...
Definition uops.c:644
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition uops.c:54
SwsUOpList * ff_sws_uop_list_alloc(void)
Definition uops.c:176
void ff_sws_uop_list_free(SwsUOpList **p_ops)
Definition uops.c:162
void ff_sws_uop_list_print(void *log, int lev, const SwsUOpList *uops)
Print the micro-ops of a compiled list at the given log level.
Definition uops.c:205
uint32_t SwsUOpFlags
Definition uops.h:158
SwsPixelType
Definition uops.h:40
@ SWS_PIXEL_F32
Definition uops.h:45
@ SWS_PIXEL_U32
Definition uops.h:44
@ SWS_PIXEL_U16
Definition uops.h:43
@ SWS_PIXEL_U8
Definition uops.h:42
#define SWS_COMP(X)
Definition uops.h:133
#define SWS_COMP_ELEMS(N)
Definition uops.h:136
SwsUOpType
Definition uops.h:169
@ SWS_UOP_READ_PLANAR
Definition uops.h:173
@ SWS_UOP_WRITE_PLANAR
Definition uops.h:182
@ SWS_UOP_WRITE_PACKED
Definition uops.h:183
@ SWS_UOP_READ_PACKED
Definition uops.h:177
@ SWS_UOP_CLEAR
Definition uops.h:215
@ SWS_UOP_RW_SHUFFLE
Definition uops.h:188
@ SWS_UOP_FLAG_FMA
Definition uops.h:161
@ SWS_UOP_FLAG_PSHUFB
Definition uops.h:162
@ SWS_UOP_FLAG_EXPAND_BIT
Definition uops.h:163
@ SWS_UOP_FLAG_ADD
Definition uops.h:165
@ SWS_UOP_FLAG_READ_PALETTE
Definition uops.h:164
@ SWS_UOP_FLAG_PUNNING
Definition uops.h:166
#define SWS_UOP_NAME_MAX
Generate a unique name for a SwsUOp.
Definition uops.h:341
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:51
#define SWS_FOR(TYPE, UOP, MACRO,...)
Definition uops_macros.h:17
#define SWS_FOR_STRUCT(TYPE, UOP, MACRO,...)
Definition uops_macros.h:19
#define DECL_ENTRY(SETUP, NAME,...)
Definition uops_tmpl.h:168
static int setup_clear(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:288
static int movsize(const int bytes, const int mmsize)
Definition ops.c:573
#define ASSIGN_SHUFFLE_FUNC(CPU, EXT, NAME,...)
static int hscale_sizeof_weight(const SwsUOp *uop)
Definition ops.c:77
static int setup_filter_h_4x4(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:203
static int compile_ops_x86(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition ops.c:706
static bool uop_is_type_invariant(const SwsUOpType uop)
Definition ops.c:351
static int compile_uops_x86(SwsContext *ctx, const SwsUOpList *uops, SwsCompiledOp *out)
Definition ops.c:648
static void splat_lane(void *dst, SwsPixelType type, SwsPixel px)
Definition ops.c:302
#define DECL_TABLE_U8(EXT, SIZE, FLAG)
Definition ops.c:424
#define DECL_TABLE_U16(EXT, SIZE, FLAG)
Definition ops.c:442
static int setup_linear(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:319
static int setup_scale(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:270
static int setup_filter_v(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:55
#define DECL_TABLE_U32(EXT, SIZE, FLAG)
Definition ops.c:460
static bool check_filter_h_4x4(const SwsImplParams *params)
Definition ops.c:179
static int setup_dither(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:296
static int translate_shuffle(const SwsUOp *uop, int mmsize, SwsCompiledOp *out)
Definition ops.c:582
static int setup_filter_h(const SwsImplParams *params, SwsImplResult *out)
Definition ops.c:87
#define DECL_TABLE_F32(EXT, SIZE, FLAG)
Definition ops.c:482
static int setup_rw_packed(const SwsImplParams *params, SwsImplResult *out)
Copyright (C) 2025-2026 Niklas Haas.
Definition ops.c:31
static int get_mmsize(void)
Definition ops.c:560