FFmpeg
Macros | Functions
ops_optimizer.c File Reference
#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. More...
 

Functions

static bool op_commute_clear (SwsOp *op, SwsOp *next)
 Try to commute a clear op with the next operation. More...
 
static bool op_commute_swizzle (SwsOp *op, SwsOp *next)
 Try to commute a swizzle op with the next operation. More...
 
static bool op_commute_filter (SwsOp *op, SwsOp *prev)
 Try to commute a filter op with the previous operation. More...
 
static int exact_log2 (const int x)
 
static int exact_log2_q (const AVRational x)
 
static bool extract_scalar (const SwsLinearOp *c, SwsComps comps, SwsComps prev, SwsConst *out_scale)
 If a linear operation can be reduced to a scalar multiplication, returns the corresponding scaling factor, or 0 otherwise. More...
 
static bool extract_constant_rows (SwsLinearOp *c, SwsComps prev, SwsConst *out_clear)
 
static bool extract_swizzle (SwsLinearOp *op, SwsComps prev, SwsSwizzleOp *out_swiz)
 
int ff_sws_op_list_optimize (SwsOpList *ops)
 Fuse compatible and eliminate redundant operations, as well as replacing some operations with more efficient alternatives. More...
 
int ff_sws_solve_shuffle (const SwsOpList *const ops, uint8_t shuffle[], int size, uint8_t clear_val, int *read_bytes, int *write_bytes)
 "Solve" an op list into a fixed shuffle mask, with an optional ability to also directly clear the output value (for e.g. More...
 
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. More...
 
static void get_input_size (const SwsOpList *ops, SwsFormat *fmt)
 
int ff_sws_op_list_subpass (SwsOpList *ops1, SwsOpList **out_rest)
 Eliminate SWS_OP_FILTER_* operations by merging them with prior SWS_OP_READ operations. More...
 

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 28 of file ops_optimizer.c.

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 40 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 99 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 217 of file ops_optimizer.c.

Referenced by exact_log2_q().

◆ exact_log2_q()

static int exact_log2_q ( const AVRational  x)
static

Definition at line 226 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_scalar()

static bool extract_scalar ( const SwsLinearOp c,
SwsComps  comps,
SwsComps  prev,
SwsConst 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 240 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_constant_rows()

static bool extract_constant_rows ( SwsLinearOp c,
SwsComps  prev,
SwsConst out_clear 
)
static

Definition at line 265 of file ops_optimizer.c.

Referenced by ff_sws_op_list_optimize().

◆ extract_swizzle()

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

Definition at line 293 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 333 of file ops_optimizer.c.

Referenced by enum_ops_fmt(), ff_sws_compile_pass(), and ff_sws_op_list_subpass().

◆ ff_sws_solve_shuffle()

int ff_sws_solve_shuffle ( const SwsOpList ops,
uint8_t  shuffle[],
int  size,
uint8_t  clear_val,
int *  read_bytes,
int *  write_bytes 
)

"Solve" an op list into a fixed shuffle mask, with an optional ability to also directly clear the output value (for e.g.

rgb24 -> rgb0). This can accept any operation chain that only consists of the following operations:

  • SWS_OP_READ (non-planar, non-fractional)
  • SWS_OP_SWIZZLE
  • SWS_OP_SWAP_BYTES
  • SWS_OP_CLEAR to zero (when clear_val is specified)
  • SWS_OP_CONVERT (integer expand)
  • SWS_OP_WRITE (non-planar, non-fractional)

Basically, any operation that purely consists of moving around and reordering bytes within a single plane, can be turned into a shuffle mask.

Parameters
opsThe operation list to decompose.
shuffleThe output shuffle mask.
sizeThe size (in bytes) of the output shuffle mask.
clear_valIf nonzero, this index will be used to clear the output.
read_bytesReturns the number of bytes read per shuffle iteration.
write_bytesReturns the number of bytes written per shuffle iteration.
Returns
The number of pixels processed per iteration, or a negative error code; in particular AVERROR(ENOTSUP) for unsupported operations.

Definition at line 777 of file ops_optimizer.c.

Referenced by solve_shuffle().

◆ 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 881 of file ops_optimizer.c.

Referenced by ff_sws_op_list_subpass().

◆ get_input_size()

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

Definition at line 914 of file ops_optimizer.c.

Referenced by ff_sws_op_list_subpass().

◆ ff_sws_op_list_subpass()

int ff_sws_op_list_subpass ( SwsOpList ops,
SwsOpList **  out_rest 
)

Eliminate SWS_OP_FILTER_* operations by merging them with prior SWS_OP_READ operations.

This may require splitting the op list into multiple subpasses, along filter boundaries. After this function, ops will no longer contain bare filtering operations. The remainder, if any, is output to out_rest.

Returns 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 927 of file ops_optimizer.c.

Referenced by ff_sws_compile_pass().

ret
ret
Definition: filter_design.txt:187