FFmpeg
ops_internal.h
Go to the documentation of this file.
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_OPS_INTERNAL_H
22 #define SWSCALE_OPS_INTERNAL_H
23 
24 #include "libavutil/mem_internal.h"
25 
26 #include "ops.h"
27 
28 #define Q(N) ((AVRational) { N, 1 })
29 
31 {
32  const int src = ff_sws_pixel_type_size(from);
33  const int dst = ff_sws_pixel_type_size(to);
34  int scale = 0;
35  for (int i = 0; i < dst / src; i++)
36  scale = scale << src * 8 | 1;
37  return Q(scale);
38 }
39 
40 static inline void ff_sws_pack_op_decode(const SwsOp *op, uint64_t mask[4], int shift[4])
41 {
42  const int size = ff_sws_pixel_type_size(op->type) * 8;
43  for (int i = 0; i < 4; i++) {
44  const int bits = op->pack.pattern[i];
45  mask[i] = (UINT64_C(1) << bits) - 1;
46  shift[i] = (i ? shift[i - 1] : size) - bits;
47  }
48 }
49 
50 /**
51  * Global execution context for all compiled functions.
52  *
53  * Note: This struct is hard-coded in assembly, so do not change the layout
54  * without updating the corresponding assembly definitions.
55  */
56 typedef struct SwsOpExec {
57  /* The data pointers point to the first pixel to process */
58  const uint8_t *in[4];
59  uint8_t *out[4];
60 
61  /* Separation between lines in bytes */
62  ptrdiff_t in_stride[4];
63  ptrdiff_t out_stride[4];
64 
65  /* Pointer bump, difference between stride and processed line size */
66  ptrdiff_t in_bump[4];
67  ptrdiff_t out_bump[4];
68 
69  /* Extra metadata, may or may not be useful */
70  int32_t width, height; /* Overall image dimensions */
71  int32_t slice_y, slice_h; /* Start and height of current slice */
72  int32_t block_size_in; /* Size of a block of pixels in bytes */
74 } SwsOpExec;
75 
76 static_assert(sizeof(SwsOpExec) == 24 * sizeof(void *) + 6 * sizeof(int32_t),
77  "SwsOpExec layout mismatch");
78 
79 /**
80  * Process a given range of pixel blocks.
81  *
82  * Note: `bx_start` and `bx_end` are in units of `SwsCompiledOp.block_size`.
83  */
84 typedef void (*SwsOpFunc)(const SwsOpExec *exec, const void *priv,
85  int bx_start, int y_start, int bx_end, int y_end);
86 
87 #define SWS_DECL_FUNC(NAME) \
88  void NAME(const SwsOpExec *, const void *, int, int, int, int)
89 
90 typedef struct SwsCompiledOp {
92 
93  int block_size; /* number of pixels processed per iteration */
94  int over_read; /* implementation over-reads input by this many bytes */
95  int over_write; /* implementation over-writes output by this many bytes */
96  int cpu_flags; /* active set of CPU flags (informative) */
97 
98  /* Arbitrary private data */
99  void *priv;
100  void (*free)(void *priv);
101 } SwsCompiledOp;
102 
103 typedef struct SwsOpBackend {
104  const char *name; /* Descriptive name for this backend */
105 
106  /**
107  * Compile an operation list to an implementation chain. May modify `ops`
108  * freely; the original list will be freed automatically by the caller.
109  *
110  * Returns 0 or a negative error code.
111  */
113 } SwsOpBackend;
114 
115 /* List of all backends, terminated by NULL */
116 extern const SwsOpBackend *const ff_sws_op_backends[];
117 
118 /**
119  * Attempt to compile a list of operations using a specific backend.
120  *
121  * Returns 0 on success, or a negative error code on failure.
122  */
124  const SwsOpList *ops, SwsCompiledOp *out);
125 
126 /**
127  * Compile a list of operations using the best available backend.
128  *
129  * Returns 0 on success, or a negative error code on failure.
130  */
132 
133 /**
134  * "Solve" an op list into a fixed shuffle mask, with an optional ability to
135  * also directly clear the output value (for e.g. rgb24 -> rgb0). This can
136  * accept any operation chain that only consists of the following operations:
137  *
138  * - SWS_OP_READ (non-planar, non-fractional)
139  * - SWS_OP_SWIZZLE
140  * - SWS_OP_SWAP_BYTES
141  * - SWS_OP_CLEAR to zero (when clear_val is specified)
142  * - SWS_OP_CONVERT (integer expand)
143  * - SWS_OP_WRITE (non-planar, non-fractional)
144  *
145  * Basically, any operation that purely consists of moving around and reordering
146  * bytes within a single plane, can be turned into a shuffle mask.
147  *
148  * @param ops The operation list to decompose.
149  * @param shuffle The output shuffle mask.
150  * @param size The size (in bytes) of the output shuffle mask.
151  * @param clear_val If nonzero, this index will be used to clear the output.
152  * @param read_bytes Returns the number of bytes read per shuffle iteration.
153  * @param write_bytes Returns the number of bytes written per shuffle iteration.
154  *
155  * @return The number of pixels processed per iteration, or a negative error
156  code; in particular AVERROR(ENOTSUP) for unsupported operations.
157  */
158 int ff_sws_solve_shuffle(const SwsOpList *ops, uint8_t shuffle[], int size,
159  uint8_t clear_val, int *read_bytes, int *write_bytes);
160 
161 #endif /* SWSCALE_OPS_INTERNAL_H */
SwsCompiledOp::func
SwsOpFunc func
Definition: ops_internal.h:91
mem_internal.h
out
FILE * out
Definition: movenc.c:55
SwsOpExec::in_bump
ptrdiff_t in_bump[4]
Definition: ops_internal.h:66
SwsOpExec::out_stride
ptrdiff_t out_stride[4]
Definition: ops_internal.h:63
SwsOpExec::in
const uint8_t * in[4]
Definition: ops_internal.h:58
mask
int mask
Definition: mediacodecdec_common.c:154
ops.h
SwsOpExec::block_size_in
int32_t block_size_in
Definition: ops_internal.h:72
read_bytes
static void read_bytes(const uint8_t *src, float *dst, int src_stride, int dst_stride, int width, int height, float scale)
Definition: vf_nnedi.c:442
Q
#define Q(N)
Copyright (C) 2025 Niklas Haas.
Definition: ops_internal.h:28
SwsCompiledOp::over_read
int over_read
Definition: ops_internal.h:94
SwsCompiledOp::cpu_flags
int cpu_flags
Definition: ops_internal.h:96
SwsOpExec::in_stride
ptrdiff_t in_stride[4]
Definition: ops_internal.h:62
SwsOpBackend::name
const char * name
Definition: ops_internal.h:104
ff_sws_pixel_type_size
int ff_sws_pixel_type_size(SwsPixelType type)
Definition: ops.c:64
SwsPixelType
SwsPixelType
Copyright (C) 2025 Niklas Haas.
Definition: ops.h:30
ff_sws_op_backends
const SwsOpBackend *const ff_sws_op_backends[]
Definition: ops.c:34
SwsOpBackend::compile
int(* compile)(SwsContext *ctx, SwsOpList *ops, SwsCompiledOp *out)
Compile an operation list to an implementation chain.
Definition: ops_internal.h:112
ff_sws_ops_compile_backend
int ff_sws_ops_compile_backend(SwsContext *ctx, const SwsOpBackend *backend, const SwsOpList *ops, SwsCompiledOp *out)
Attempt to compile a list of operations using a specific backend.
SwsCompiledOp::over_write
int over_write
Definition: ops_internal.h:95
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
bits
uint8_t bits
Definition: vp3data.h:128
from
const char * from
Definition: jacosubdec.c:66
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_sws_pixel_expand
static AVRational ff_sws_pixel_expand(SwsPixelType from, SwsPixelType to)
Definition: ops_internal.h:30
SwsOpBackend
Definition: ops_internal.h:103
SwsOpExec::height
int32_t height
Definition: ops_internal.h:70
SwsOpExec
Global execution context for all compiled functions.
Definition: ops_internal.h:56
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
SwsOpExec::slice_h
int32_t slice_h
Definition: ops_internal.h:71
to
const char * to
Definition: webvttdec.c:35
SwsOpExec::block_size_out
int32_t block_size_out
Definition: ops_internal.h:73
shift
static int shift(int a, int b)
Definition: bonk.c:261
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
size
int size
Definition: twinvq_data.h:10344
SwsOpExec::out
uint8_t * out[4]
Definition: ops_internal.h:59
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
SwsOpFunc
void(* SwsOpFunc)(const SwsOpExec *exec, const void *priv, int bx_start, int y_start, int bx_end, int y_end)
Process a given range of pixel blocks.
Definition: ops_internal.h:84
ff_sws_ops_compile
int ff_sws_ops_compile(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Compile a list of operations using the best available backend.
SwsOp
Definition: ops.h:179
SwsOpExec::width
int32_t width
Definition: ops_internal.h:70
write_bytes
static void write_bytes(const float *src, uint8_t *dst, int src_stride, int dst_stride, int width, int height, int depth, float scale)
Definition: vf_nnedi.c:484
SwsCompiledOp::priv
void * priv
Definition: ops_internal.h:99
SwsCompiledOp::block_size
int block_size
Definition: ops_internal.h:93
SwsCompiledOp
Definition: ops_internal.h:90
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 out...
Definition: ops_optimizer.c:774
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:273
ff_sws_pack_op_decode
static void ff_sws_pack_op_decode(const SwsOp *op, uint64_t mask[4], int shift[4])
Definition: ops_internal.h:40
int32_t
int32_t
Definition: audioconvert.c:56
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:209
SwsOpExec::slice_y
int32_t slice_y
Definition: ops_internal.h:71
SwsContext
Main external API structure.
Definition: swscale.h:189
shuffle
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len)
Definition: des.c:179
src
#define src
Definition: vp8dsp.c:248
SwsOpExec::out_bump
ptrdiff_t out_bump[4]
Definition: ops_internal.h:67
SwsCompiledOp::free
void(* free)(void *priv)
Definition: ops_internal.h:100