FFmpeg
sws_ops_aarch64.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 Ramiro Polla
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 <stdio.h>
22 
23 #include "libavutil/mem.h"
24 #include "libavutil/tree.h"
25 #include "libswscale/ops.h"
26 #include "libswscale/ops_chain.h"
27 
30 
31 #ifdef _WIN32
32 #include <io.h>
33 #include <fcntl.h>
34 #endif
35 
36 /*********************************************************************/
37 static int aarch64_op_impl_cmp(const void *a, const void *b)
38 {
39  const SwsAArch64OpImplParams *pa = (const SwsAArch64OpImplParams *) a;
40  const SwsAArch64OpImplParams *pb = (const SwsAArch64OpImplParams *) b;
41 
42  const ParamField **fields = op_fields[pa->op];
43  for (int i = 0; fields[i]; i++) {
44  const ParamField *field = fields[i];
45  int diff = field->cmp_val((void *) (((uintptr_t) pa) + field->offset),
46  (void *) (((uintptr_t) pb) + field->offset));
47  if (diff)
48  return diff;
49  }
50  return 0;
51 }
52 
53 /*********************************************************************/
54 /* Insert the SwsAArch64OpImplParams structure into the AVTreeNode. */
55 static int aarch64_collect_op(const SwsAArch64OpImplParams *params, struct AVTreeNode **root)
56 {
57  int ret = 0;
58 
59  struct AVTreeNode *node = av_tree_node_alloc();
60  SwsAArch64OpImplParams *copy = av_memdup(params, sizeof(*params));
61  if (!node || !copy) {
62  ret = AVERROR(ENOMEM);
63  goto error;
64  }
66  if (!node)
67  copy = NULL;
68 
69 error:
70  av_free(node);
71  av_free(copy);
72  return ret;
73 }
74 
75 /* Collect the parameters for the process/process_return functions. */
76 static int aarch64_collect_process(const SwsOpList *ops, struct AVTreeNode **root)
77 {
78  const SwsOp *read = ff_sws_op_list_input(ops);
79  const SwsOp *write = ff_sws_op_list_output(ops);
80  const int read_planes = read ? (read->rw.packed ? 1 : read->rw.elems) : 0;
81  const int write_planes = write->rw.packed ? 1 : write->rw.elems;
82  int ret;
83 
85  for (int i = 0; i < FFMAX(read_planes, write_planes); i++)
86  MASK_SET(mask, i, 1);
87  SwsAArch64OpImplParams params = {
89  .mask = mask,
90  };
91 
92  ret = aarch64_collect_op(&params, root);
93  if (ret < 0)
94  return ret;
95 
97  ret = aarch64_collect_op(&params, root);
98  if (ret < 0)
99  return ret;
100 
101  return 0;
102 }
103 
104 static int register_op(SwsContext *ctx, void *opaque, SwsOpList *ops)
105 {
106  struct AVTreeNode **root = (struct AVTreeNode **) opaque;
107  int ret;
108 
109  /* Make on-stack copy of `ops` to iterate over */
110  SwsOpList rest = *ops;
111  /* Use at most two full vregs during the widest precision section */
112  int block_size = (ff_sws_op_list_max_size(ops) == 4) ? 8 : 16;
113 
114  ret = aarch64_collect_process(&rest, root);
115  if (ret < 0)
116  return ret;
117 
118  for (int i = 0; i < rest.num_ops; i++) {
119  SwsAArch64OpImplParams params = { 0 };
120  ret = convert_to_aarch64_impl(ctx, &rest, i, block_size, &params);
121  if (ret < 0)
122  goto end;
123  ret = aarch64_collect_op(&params, root);
124  if (ret < 0)
125  goto end;
126  if (params.op == AARCH64_SWS_OP_LINEAR) {
127  /**
128  * Generate both sets of linear op functions that do use
129  * and do not use fmla (selected by SWS_BITEXACT).
130  */
131  params.linear.fmla = !params.linear.fmla;
132  ret = aarch64_collect_op(&params, root);
133  if (ret < 0)
134  goto end;
135  }
136  }
137 
138  ret = 0;
139 
140 end:
141  return ret;
142 }
143 
144 /*********************************************************************/
145 static void serialize_op(char *buf, size_t size, const SwsAArch64OpImplParams *params)
146 {
147  buf_appendf(&buf, &size, "{");
148  const ParamField **fields = op_fields[params->op];
149  for (int i = 0; fields[i]; i++) {
150  const ParamField *field = fields[i];
151  void *p = (void *) (((uintptr_t) params) + field->offset);
152  if (i)
153  buf_appendf(&buf, &size, ",");
154  buf_appendf(&buf, &size, " .%s = ", field->name);
155  field->print_val(&buf, &size, p);
156  }
157  buf_appendf(&buf, &size, " }");
158  av_assert0(size && "string buffer exhausted");
159 }
160 
161 /* Serialize SwsAArch64OpImplParams for one function. */
162 static int print_op(void *opaque, void *elem)
163 {
165  FILE *fp = (FILE *) opaque;
166 
167  char buf[256];
168  serialize_op(buf, sizeof(buf), params);
169  fprintf(fp, "%s,\n", buf);
170 
171  av_free(params);
172 
173  return 0;
174 }
175 
176 /*********************************************************************/
177 int main(int argc, char *argv[])
178 {
179  struct AVTreeNode *root = NULL;
180  int ret = 1;
181 
182 #ifdef _WIN32
183  _setmode(_fileno(stdout), _O_BINARY);
184 #endif
185 
187  if (!ctx)
188  goto fail;
189 
191  register_op);
192 
193  /**
194  * Generate a C file with all the unique function parameter entries
195  * collected by aarch64_enum_ops().
196  */
197  printf("/*\n");
198  printf(" * This file is automatically generated. Do not edit manually.\n");
199  printf(" * To regenerate, run: make sws_ops_entries_aarch64\n");
200  printf(" */\n");
201  printf("\n");
202  av_tree_enumerate(root, stdout, NULL, print_op);
203 
204 fail:
205  av_tree_destroy(root);
207  return ret;
208 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
printf
__device__ int printf(const char *,...)
ParamField
The following structure is used to describe one field from SwsAArch64OpImplParams.
Definition: ops_impl.c:190
ff_sws_op_list_input
const SwsOp * ff_sws_op_list_input(const SwsOpList *ops)
Returns the input operation for a given op list, or NULL if there is none (e.g.
Definition: ops.c:634
aarch64_collect_process
static int aarch64_collect_process(const SwsOpList *ops, struct AVTreeNode **root)
Definition: sws_ops_aarch64.c:76
av_tree_insert
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(const void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition: tree.c:59
ff_sws_op_list_max_size
int ff_sws_op_list_max_size(const SwsOpList *ops)
Returns the size of the largest pixel type used in ops.
Definition: ops.c:711
mask
int mask
Definition: mediacodecdec_common.c:154
SwsOp::rw
SwsReadWriteOp rw
Definition: ops.h:223
ops.h
AVTreeNode::elem
void * elem
Definition: tree.c:28
b
#define b
Definition: input.c:42
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_tree_node_alloc
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition: tree.c:34
ops_impl.c
av_tree_enumerate
void av_tree_enumerate(AVTreeNode *t, void *opaque, int(*cmp)(void *opaque, void *elem), int(*enu)(void *opaque, void *elem))
Apply enu(opaque, &elem) to all the elements in the tree in a given range.
Definition: tree.c:155
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
SwsAArch64LinearOp::fmla
uint8_t fmla
Definition: ops_impl.h:81
fail
#define fail()
Definition: checkasm.h:223
SwsOpList::num_ops
int num_ops
Definition: ops.h:265
ops_chain.h
SwsAArch64OpMask
uint16_t SwsAArch64OpMask
Definition: ops_impl.h:68
AARCH64_SWS_OP_LINEAR
@ AARCH64_SWS_OP_LINEAR
Definition: ops_impl.h:62
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
ff_sws_op_list_output
const SwsOp * ff_sws_op_list_output(const SwsOpList *ops)
Returns the output operation for a given op list, or NULL if there is none.
Definition: ops.c:643
AARCH64_SWS_OP_PROCESS
@ AARCH64_SWS_OP_PROCESS
Definition: ops_impl.h:40
op_fields
static const ParamField * op_fields[AARCH64_SWS_OP_TYPE_NB][MAX_LEVELS]
Definition: ops_impl.c:327
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
NULL
#define NULL
Definition: coverity.c:32
aarch64_op_impl_cmp
static int aarch64_op_impl_cmp(const void *a, const void *b)
Definition: sws_ops_aarch64.c:37
AVTreeNode
Definition: tree.c:26
print_op
static int print_op(void *opaque, void *elem)
Definition: sws_ops_aarch64.c:162
av_tree_destroy
void av_tree_destroy(AVTreeNode *t)
Definition: tree.c:146
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:186
sws_alloc_context
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition: utils.c:1031
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
size
int size
Definition: twinvq_data.h:10344
SwsAArch64OpImplParams::op
SwsAArch64OpType op
Definition: ops_impl.h:95
tree.h
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
SwsAArch64OpImplParams::linear
SwsAArch64LinearOp linear
Definition: ops_impl.h:104
ops_impl_conv.c
ff_sws_enum_op_lists
int ff_sws_enum_op_lists(SwsContext *ctx, void *opaque, enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, int(*cb)(SwsContext *ctx, void *opaque, SwsOpList *ops))
Helper function to enumerate over all possible (optimized) operation lists, under the current set of ...
Definition: ops.c:1015
SwsOp
Definition: ops.h:218
ret
ret
Definition: filter_design.txt:187
MASK_SET
#define MASK_SET(mask, idx, val)
Definition: ops_impl.h:112
convert_to_aarch64_impl
static int convert_to_aarch64_impl(SwsContext *ctx, const SwsOpList *ops, int n, int block_size, SwsAArch64OpImplParams *out)
Convert SwsOp to a SwsAArch64OpImplParams.
Definition: ops_impl_conv.c:59
SwsReadWriteOp::packed
bool packed
Definition: ops.h:110
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
main
int main(int argc, char *argv[])
Definition: sws_ops_aarch64.c:177
aarch64_collect_op
static int aarch64_collect_op(const SwsAArch64OpImplParams *params, struct AVTreeNode **root)
Definition: sws_ops_aarch64.c:55
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
register_op
static int register_op(SwsContext *ctx, void *opaque, SwsOpList *ops)
Definition: sws_ops_aarch64.c:104
SwsReadWriteOp::elems
uint8_t elems
Examples: rgba = 4x u8 packed yuv444p = 3x u8 rgb565 = 1x u16 <- use SWS_OP_UNPACK to unpack monow = ...
Definition: ops.h:108
SwsAArch64OpImplParams
SwsAArch64OpImplParams describes the parameters for an SwsAArch64OpType operation.
Definition: ops_impl.h:94
mem.h
AARCH64_SWS_OP_PROCESS_RETURN
@ AARCH64_SWS_OP_PROCESS_RETURN
Definition: ops_impl.h:41
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
sws_free_context
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition: utils.c:2368
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:263
serialize_op
static void serialize_op(char *buf, size_t size, const SwsAArch64OpImplParams *params)
Definition: sws_ops_aarch64.c:145
SwsContext
Main external API structure.
Definition: swscale.h:206
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:239