FFmpeg
Loading...
Searching...
No Matches
ops_optimizer.c File Reference
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/bswap.h"
#include "libavutil/rational.h"
#include "ops.h"
#include "ops_internal.h"

Go to the source code of this file.

Macros

#define RET(x)
 Copyright (C) 2025 Niklas Haas.
 

Functions

static bool op_commute_clear (SwsOp *op, SwsOp *next)
 Try to commute a clear op with the next operation.
 
static bool op_commute_swizzle (SwsOp *op, SwsOp *next)
 Try to commute a swizzle op with the next operation.
 
static bool op_commute_filter (SwsOp *op, SwsOp *prev)
 Try to commute a filter op with the previous operation.
 
static int exact_log2 (const int x)
 
static int exact_log2_q64 (const AVRational64 x)
 
static bool extract_scalar (const SwsLinearOp *c, const SwsComps *comps, const SwsComps *prev, SwsScaleOp *out_scale)
 If a linear operation can be reduced to a scalar multiplication, returns the corresponding scaling factor, or 0 otherwise.
 
static bool extract_constant_rows (SwsLinearOp *c, const SwsComps *prev, SwsClearOp *out_clear)
 
static bool extract_swizzle (SwsLinearOp *op, const SwsComps *prev, SwsSwizzleOp *out_swiz)
 
static int op_result_is_exact (const SwsOp *op)
 
int ff_sws_op_list_optimize (SwsOpList *ops)
 Fuse compatible and eliminate redundant operations, as well as replacing some operations with more efficient alternatives.
 
static int select_planes (SwsOpList *ops, SwsCompMask planes)
 
int ff_sws_op_list_split_planes (SwsOpList *ops1, SwsOpList **out_ops2, SwsCompMask planes)
 Reduce an op list into a reduced subset that operates only on a given subset of planes.
 
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 groups as will fit.
 
static bool pixel_is_repeating (SwsPixelType type, SwsPixel val)
 
static int solve_shuffle (const SwsUOpList *const uops, SwsUOp *out)
 
int ff_sws_uop_list_optimize (SwsContext *ctx, SwsUOpFlags flags, SwsUOpList *uops)
 Called internally by ff_sws_ops_translate().
 
static enum AVPixelFormat get_planar_fmt (SwsPixelType type, int nb_planes)
 Determine a suitable intermediate buffer format for a given combination of pixel types and number of planes.
 
static void get_input_size (const SwsOpList *ops, SwsFormat *fmt)
 
int ff_sws_op_list_split_at (SwsOpList *ops1, SwsOpList **out_ops2, int index)
 Split an op list into two at the given index.
 

Macro Definition Documentation

◆ RET

#define RET ( x)
Value:
do { \
if ((ret = (x)) < 0) \
return ret; \
} while (0)

Copyright (C) 2025 Niklas Haas.

This file is part of FFmpeg.

FFmpeg is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

FFmpeg is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with FFmpeg; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Definition at line 29 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

Function Documentation

◆ op_commute_clear()

static bool op_commute_clear ( SwsOp * op,
SwsOp * next )
static

Try to commute a clear op with the next operation.

Makes any adjustments to the operations as needed, but does not perform the actual commutation.

Returns whether successful.

Definition at line 41 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ op_commute_swizzle()

static bool op_commute_swizzle ( SwsOp * op,
SwsOp * next )
static

Try to commute a swizzle op with the next operation.

Makes any adjustments to the operations as needed, but does not perform the actual commutation.

Returns whether successful.

We can commute per-channel ops only if the per-channel constants are the same for all duplicated channels; e.g.: SWIZZLE {0, 0, 0, 3} NEXT {x, x, x, w} -> NEXT {x, _, _, w} SWIZZLE {0, 0, 0, 3}

Definition at line 96 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ op_commute_filter()

static bool op_commute_filter ( SwsOp * op,
SwsOp * prev )
static

Try to commute a filter op with the previous operation.

Makes any adjustments to the operations as needed, but does not perform the actual commutation.

Returns whether successful.

Definition at line 178 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ exact_log2()

static int exact_log2 ( const int x)
static

Definition at line 214 of file ops_optimizer.c.

Referenced by exact_log2_q64().

◆ exact_log2_q64()

static int exact_log2_q64 ( const AVRational64 x)
static

Definition at line 223 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_scalar()

static bool extract_scalar ( const SwsLinearOp * c,
const SwsComps * comps,
const SwsComps * prev,
SwsScaleOp * out_scale )
static

If a linear operation can be reduced to a scalar multiplication, returns the corresponding scaling factor, or 0 otherwise.

Definition at line 237 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_constant_rows()

static bool extract_constant_rows ( SwsLinearOp * c,
const SwsComps * prev,
SwsClearOp * out_clear )
static

Definition at line 263 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_swizzle()

static bool extract_swizzle ( SwsLinearOp * op,
const SwsComps * prev,
SwsSwizzleOp * out_swiz )
static

Definition at line 292 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ op_result_is_exact()

static int op_result_is_exact ( const SwsOp * op)
static

Definition at line 332 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ ff_sws_op_list_optimize()

int ff_sws_op_list_optimize ( SwsOpList * ops)

Fuse compatible and eliminate redundant operations, as well as replacing some operations with more efficient alternatives.

Definition at line 342 of file ops_optimizer.c.

Referenced by enum_ops_fmt(), ff_sws_compile_pass(), ff_sws_op_list_split_at(), and select_planes().

◆ select_planes()

static int select_planes ( SwsOpList * ops,
SwsCompMask planes )
static

Definition at line 807 of file ops_optimizer.c.

Referenced by ff_sws_op_list_split_planes().

◆ ff_sws_op_list_split_planes()

int ff_sws_op_list_split_planes ( SwsOpList * ops1,
SwsOpList ** ops2,
SwsCompMask planes )

Reduce an op list into a reduced subset that operates only on a given subset of planes.

No effect if the output is not planar, or if the plane mask is empty or equal to all planes.

Parameters
ops1Updated in-place to contain only the selected planes.
ops2The removed remainder is returned here, or NULL if no-op.
planesA mask of the plane indices to keep.

Returns 0 or a negative error code.

Definition at line 836 of file ops_optimizer.c.

Referenced by compile_subpass().

◆ ff_sws_shuffle_mask()

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 groups as will fit.

Parameters
uopAn operation of type SWS_UOP_RW_SHUFFLE.
shuffleThe output shuffle index mask (or -1 to clear bytes).
sizeThe maximum size (in bytes) of the output shuffle mask.
Returns
the number of groups on success, or a negative error code.
Note
The shuffle mask is already pre-expanded to fill up to 16 bytes, so this is only needed for larger shuffle instructions (e.g. vpermb).

Definition at line 869 of file ops_optimizer.c.

Referenced by solve_shuffle(), and translate_shuffle().

◆ pixel_is_repeating()

static bool pixel_is_repeating ( SwsPixelType type,
SwsPixel val )
static

Definition at line 893 of file ops_optimizer.c.

Referenced by solve_shuffle().

◆ solve_shuffle()

static int solve_shuffle ( const SwsUOpList *const uops,
SwsUOp * out )
static

Definition at line 906 of file ops_optimizer.c.

Referenced by ff_sws_uop_list_optimize().

◆ ff_sws_uop_list_optimize()

int ff_sws_uop_list_optimize ( SwsContext * ctx,
SwsUOpFlags flags,
SwsUOpList * uops )

Called internally by ff_sws_ops_translate().

Definition at line 1028 of file ops_optimizer.c.

Referenced by ff_sws_ops_translate().

◆ get_planar_fmt()

static enum AVPixelFormat get_planar_fmt ( SwsPixelType type,
int nb_planes )
static

Determine a suitable intermediate buffer format for a given combination of pixel types and number of planes.

The exact interpretation of these formats does not matter at all; since they will only ever be used as temporary intermediate buffers. We still need to pick some format as a consequence of ff_sws_graph_add_pass() taking an AVPixelFormat for the output buffer.

Definition at line 1067 of file ops_optimizer.c.

Referenced by ff_sws_op_list_split_at().

◆ get_input_size()

static void get_input_size ( const SwsOpList * ops,
SwsFormat * fmt )
static

Definition at line 1100 of file ops_optimizer.c.

Referenced by ff_sws_op_list_split_at().

◆ ff_sws_op_list_split_at()

int ff_sws_op_list_split_at ( SwsOpList * ops1,
SwsOpList ** ops2,
int index )

Split an op list into two at the given index.

The split will be mediated by a set of planar read/write operations, plus a swizzle (if necessary) to re-order only used components. If a split is performed, both output lists will be optimized before returning.

Parameters
ops1The first part of the split op list. Will be modified in-place.
ops2The second part of the split op list will be returned here, or NULL if no split was necessary.
indexThe index of the operation to split before. The operation itself will be absent from ops1 and instead moved to the start of ops2.

Returnse 0 or a negative error code.

Not all components may be needed; but we need the ones that are used to be contiguous for the write/read operations. So, first compress them into a linearly ascending list of components

Definition at line 1113 of file ops_optimizer.c.

Referenced by compile_subpass().