52static_assert(offsetof(
FilterPriv, filter_size) ==
sizeof(
void *),
53 "FilterPriv layout mismatch");
64 for (
int i = 0;
i <
filter->num_weights;
i++)
69 .filter_size =
filter->filter_size,
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);
104 if (aligned_size > INT_MAX)
121 const int mmsize = block_size * 2;
122 const int gather_size = mmsize /
sizeof(
int32_t);
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;
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
148 + (pos_in_lane >> 1) * (mmsize / 2)
150 + (pos_in_lane & 1) * 4;
152 idx +=
i * taps_align;
166 .filter_size = aligned_size,
172 for (
int i = 0;
i < 4;
i++) {
174 out->over_read[
i] = (aligned_size - filter_size) * pixel_size;
209 const int block_size =
params->table->block_size;
210 const int taps_align = 16 / sizeof_weights;
211 const int pixels_align = 4;
212 const int filter_size =
filter->filter_size;
213 const size_t aligned_size =
FFALIGN(filter_size, taps_align);
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;
257 .filter_size = aligned_size * sizeof_weights,
263 for (
int i = 0;
i < 4;
i++) {
265 out->over_read[
i] = (aligned_size - filter_size) * pixel_size;
273 switch (
params->uop->type) {
275 const uint32_t u32 = scalar.
u8;
282 default:
return AVERROR(EINVAL);
291 for (
int i = 0;
i < 4;
i++)
306 memset(
dst, px.
u8, 16);
309 for (
int i = 0;
i < 8;
i++)
310 ((uint16_t *)
dst)[
i] = px.
u16;
313 for (
int i = 0;
i < 4;
i++)
314 ((uint32_t *)
dst)[
i] = px.
u32;
325 "mat4x5 layout mismatch");
335 for (
int i = 0;
i < 4;
i++) {
336 for (
int j = 0; j < 5; j++) {
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, \
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) \
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) \
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) \
430static const SwsUOpTable uops_u8##EXT = { \
431 .cpu_flags = AV_CPU_FLAG_##FLAG, \
432 .block_size = SIZE, \
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) \
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) \
448static const SwsUOpTable uops_u16##EXT = { \
449 .cpu_flags = AV_CPU_FLAG_##FLAG, \
450 .block_size = SIZE, \
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) \
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) \
468static const SwsUOpTable uops_u32##EXT = { \
469 .cpu_flags = AV_CPU_FLAG_##FLAG, \
470 .block_size = SIZE, \
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) \
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) \
504static const SwsUOpTable uops_f32##EXT = { \
505 .cpu_flags = AV_CPU_FLAG_##FLAG, \
506 .block_size = SIZE, \
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) \
573static int movsize(
const int bytes,
const int mmsize)
575 return bytes <= 4 ? 4 :
594 const int mask_size = lane_aligned ? 16 : mmsize;
607 const int num_lanes = lane_aligned ? mmsize / 16 : 1;
609 const int out_total = num_lanes * write_chunk;
615 .over_read = {
movsize(in_total, mmsize) - in_total },
616 .over_write = {
movsize(out_total, mmsize) - out_total },
619#define ASSIGN_SHUFFLE_FUNC(CPU, EXT, NAME, ...) \
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; \
677 int op_block_size =
out->block_size;
685 op_block_size, chain);
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;
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
SwsAArch64OpImplParams params
static AVFormatContext * ctx
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.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
#define xi(width, name, var, range_min, range_max, subs,...)
static int read_chunk(AVFormatContext *s)
static const OptionGroupDef groups[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_VERBOSE
Detailed information.
@ SWS_BACKEND_X86
Chained x86 SIMD kernels.
static const int weights[]
static atomic_int cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
#define AV_CPU_FLAG_SLOW_GATHER
CPU has slow gathers.
#define EXTERNAL_AVX512ICL(flags)
#define EXTERNAL_FMA3(flags)
#define EXTERNAL_SSE4(flags)
#define EXTERNAL_AVX512(flags)
#define EXTERNAL_AVX2(flags)
@ SWS_FILTER_SCALE
14-bit coefficients are picked to fit comfortably within int16_t for efficient SIMD processing (e....
static const uint16_t mask[17]
Memory handling functions.
const SwsOpBackend backend_x86
SwsOpChain * ff_sws_op_chain_alloc(void)
Copyright (C) 2025 Niklas Haas.
void ff_sws_op_chain_attach(const SwsOpChain *chain, SwsCompiledOp *out)
Attach a SwsOpChain and all metadata to a SwsCompiledOp (as out->priv).
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...
static void ff_sws_op_priv_pack_refptr(SwsImplResult *out, const void *data)
static void ff_sws_op_chain_free(SwsOpChain *chain)
#define SWS_OP_PRIV_PACK(out, data)
#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...
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
#define FF_ARRAY_ELEMS(a)
Main external API structure.
Represents a computed filter kernel.
Compiled "chain" of operations, which can be dispatched efficiently.
Helper struct for representing a list of operations.
Copyright (C) 2025 Niklas Haas.
SwsFilterWeights * kernel
union SwsUOp::@242237116251216327057105100216205033300341206345 data
static void * ff_refstruct_calloc(size_t n, size_t size)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
static const uint8_t *const tables[]
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...
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
SwsUOpList * ff_sws_uop_list_alloc(void)
void ff_sws_uop_list_free(SwsUOpList **p_ops)
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.
#define SWS_COMP_ELEMS(N)
@ SWS_UOP_FLAG_EXPAND_BIT
@ SWS_UOP_FLAG_READ_PALETTE
#define SWS_UOP_NAME_MAX
Generate a unique name for a SwsUOp.
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
#define SWS_FOR(TYPE, UOP, MACRO,...)
#define SWS_FOR_STRUCT(TYPE, UOP, MACRO,...)
#define DECL_ENTRY(SETUP, NAME,...)
static int setup_clear(const SwsImplParams *params, SwsImplResult *out)
static int movsize(const int bytes, const int mmsize)
#define ASSIGN_SHUFFLE_FUNC(CPU, EXT, NAME,...)
static int hscale_sizeof_weight(const SwsUOp *uop)
static int setup_filter_h_4x4(const SwsImplParams *params, SwsImplResult *out)
static int compile_ops_x86(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
static bool uop_is_type_invariant(const SwsUOpType uop)
static int compile_uops_x86(SwsContext *ctx, const SwsUOpList *uops, SwsCompiledOp *out)
static void splat_lane(void *dst, SwsPixelType type, SwsPixel px)
#define DECL_TABLE_U8(EXT, SIZE, FLAG)
#define DECL_TABLE_U16(EXT, SIZE, FLAG)
static int setup_linear(const SwsImplParams *params, SwsImplResult *out)
static int setup_scale(const SwsImplParams *params, SwsImplResult *out)
static int setup_filter_v(const SwsImplParams *params, SwsImplResult *out)
#define DECL_TABLE_U32(EXT, SIZE, FLAG)
static bool check_filter_h_4x4(const SwsImplParams *params)
static int setup_dither(const SwsImplParams *params, SwsImplResult *out)
static int translate_shuffle(const SwsUOp *uop, int mmsize, SwsCompiledOp *out)
static int setup_filter_h(const SwsImplParams *params, SwsImplResult *out)
#define DECL_TABLE_F32(EXT, SIZE, FLAG)
static int setup_rw_packed(const SwsImplParams *params, SwsImplResult *out)
Copyright (C) 2025-2026 Niklas Haas.
static int get_mmsize(void)