FFmpeg
sws_ops.c
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 #include "libavutil/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/tree.h"
26 #include "libswscale/ops.h"
27 #include "libswscale/format.h"
28 
29 #ifdef _WIN32
30 #include <io.h>
31 #include <fcntl.h>
32 #endif
33 
34 static int print_ops(SwsContext *const ctx, void *opaque, SwsOpList *ops)
35 {
36  av_log(opaque, AV_LOG_INFO, "%s -> %s:\n",
39 
40  if (ff_sws_op_list_is_noop(ops))
41  av_log(opaque, AV_LOG_INFO, " (no-op)\n");
42  else
44 
45  return 0;
46 }
47 
48 static int cmp_str(const void *a, const void *b)
49 {
50  return strcmp(a, b);
51 }
52 
53 static const bool unused[4] = { false, false, false, false };
54 static int register_op(SwsContext *ctx, void *opaque, SwsOp *op)
55 {
56  struct AVTreeNode **root = opaque;
57  AVBPrint bp;
58  char *desc;
59 
60  /* Strip irrelevant constant data from some operations */
61  switch (op->op) {
62  case SWS_OP_LINEAR:
63  for (int i = 0; i < 4; i++) {
64  for (int j = 0; j < 5; j++)
65  op->lin.m[i][j] = (AVRational) { 0, 1 };
66  }
67  break;
68  case SWS_OP_SCALE:
69  case SWS_OP_MIN:
70  case SWS_OP_MAX:
71  case SWS_OP_CLEAR:
72  for (int i = 0; i < 4; i++)
73  op->c.q4[i] = (AVRational) { 0, !!op->c.q4[i].den };
74  break;
75  case SWS_OP_DITHER:
76  /* Strip arbitrary offset */
77  for (int i = 0; i < 4; i++)
78  op->dither.y_offset[i] = op->dither.y_offset[i] >= 0 ? 0 : -1;
79  break;
80  }
81 
83  ff_sws_op_desc(&bp, op, unused);
84  int ret = av_bprint_finalize(&bp, &desc);
85  if (ret < 0)
86  return ret;
87 
88  struct AVTreeNode *node = av_tree_node_alloc();
89  if (!node) {
90  av_free(desc);
91  return AVERROR(ENOMEM);
92  }
93 
94  av_tree_insert(root, desc, cmp_str, &node);
95  if (node) {
96  av_free(node);
97  av_free(desc);
98  }
99  return ret;
100 }
101 
102 static int print_and_free_summary(void *opaque, void *key)
103 {
104  char *desc = key;
105  av_log(opaque, AV_LOG_INFO, "%s\n", desc);
106  av_free(desc);
107  return 0;
108 }
109 
110 static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
111 {
112  if (level != AV_LOG_INFO) {
113  av_log_default_callback(avcl, level, fmt, vl);
114  } else if (av_log_get_level() >= AV_LOG_INFO) {
115  vfprintf(stdout, fmt, vl);
116  }
117 }
118 
119 int main(int argc, char **argv)
120 {
121  enum AVPixelFormat src_fmt = AV_PIX_FMT_NONE;
122  enum AVPixelFormat dst_fmt = AV_PIX_FMT_NONE;
123  bool summarize = false;
124  int ret = 1;
125 
126 #ifdef _WIN32
127  _setmode(_fileno(stdout), _O_BINARY);
128 #endif
129 
130  for (int i = 1; i < argc; i++) {
131  if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
132  fprintf(stderr,
133  "sws_ops [options...]\n"
134  " -help\n"
135  " This text\n"
136  " -dst <pixfmt>\n"
137  " Only test the specified destination pixel format\n"
138  " -src <pixfmt>\n"
139  " Only test the specified source pixel format\n"
140  " -v <level>\n"
141  " Enable log verbosity at given level\n"
142  " -summarize\n"
143  " Summarize operation types, instead of printing op lists\n"
144  );
145  return 0;
146  }
147  if (!strcmp(argv[i], "-src")) {
148  if (i + 1 >= argc)
149  goto bad_option;
150  src_fmt = av_get_pix_fmt(argv[i + 1]);
151  if (src_fmt == AV_PIX_FMT_NONE) {
152  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
153  goto error;
154  }
155  i++;
156  } else if (!strcmp(argv[i], "-dst")) {
157  if (i + 1 >= argc)
158  goto bad_option;
159  dst_fmt = av_get_pix_fmt(argv[i + 1]);
160  if (dst_fmt == AV_PIX_FMT_NONE) {
161  fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
162  goto error;
163  }
164  i++;
165  } else if (!strcmp(argv[i], "-v")) {
166  if (i + 1 >= argc)
167  goto bad_option;
168  av_log_set_level(atoi(argv[i + 1]));
169  i++;
170  } else if (!strcmp(argv[i], "-summarize")) {
171  summarize = true;
172  } else {
173 bad_option:
174  fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
175  goto error;
176  }
177  }
178 
180  if (!ctx)
181  goto fail;
182 
184 
185  if (summarize) {
186  struct AVTreeNode *root = NULL;
187  ret = ff_sws_enum_ops(ctx, &root, src_fmt, dst_fmt, register_op);
188  if (ret < 0)
189  goto fail;
191  av_tree_destroy(root);
192  } else {
193  ret = ff_sws_enum_op_lists(ctx, NULL, src_fmt, dst_fmt, print_ops);
194  if (ret < 0)
195  goto fail;
196  }
197 
198  ret = 0;
199 fail:
201  return ret;
202 
203 error:
204  return AVERROR(EINVAL);
205 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
ff_sws_enum_ops
int ff_sws_enum_ops(SwsContext *ctx, void *opaque, enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, int(*cb)(SwsContext *ctx, void *opaque, SwsOp *op))
Helper function to enumerate over all possible operations, under the current set of options in ctx,...
Definition: ops.c:978
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
level
uint8_t level
Definition: svq3.c:208
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
print_ops
static int print_ops(SwsContext *const ctx, void *opaque, SwsOpList *ops)
Definition: sws_ops.c:34
ff_sws_op_desc
void ff_sws_op_desc(AVBPrint *bp, const SwsOp *op, const bool unused[4])
Describe an operation in human-readable form.
Definition: ops.c:758
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
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
SWS_OP_CLEAR
@ SWS_OP_CLEAR
Definition: ops.h:61
main
int main(int argc, char **argv)
Definition: sws_ops.c:119
pixdesc.h
ops.h
SWS_OP_DITHER
@ SWS_OP_DITHER
Definition: ops.h:69
b
#define b
Definition: input.c:42
av_tree_node_alloc
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition: tree.c:34
format.h
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
ff_sws_op_list_print
void ff_sws_op_list_print(void *log, int lev, int lev_extra, const SwsOpList *ops)
Print out the contents of an operation list.
Definition: ops.c:834
fail
#define fail()
Definition: checkasm.h:222
AV_BPRINT_SIZE_AUTOMATIC
#define AV_BPRINT_SIZE_AUTOMATIC
print_and_free_summary
static int print_and_free_summary(void *opaque, void *key)
Definition: sws_ops.c:102
SWS_OP_SCALE
@ SWS_OP_SCALE
Definition: ops.h:65
avassert.h
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
SWS_OP_MIN
@ SWS_OP_MIN
Definition: ops.h:63
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
SWS_OP_LINEAR
@ SWS_OP_LINEAR
Definition: ops.h:68
key
const char * key
Definition: hwcontext_opencl.c:189
av_log_get_level
int av_log_get_level(void)
Get the current log level.
Definition: log.c:472
ff_sws_op_list_is_noop
bool ff_sws_op_list_is_noop(const SwsOpList *ops)
Returns whether an op list represents a true no-op operation, i.e.
Definition: ops.c:620
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVTreeNode
Definition: tree.c:26
av_tree_destroy
void av_tree_destroy(AVTreeNode *t)
Definition: tree.c:146
av_log_set_callback
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:492
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
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
SwsOpList::src
SwsFormat src
Definition: ops.h:236
tree.h
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
log_stdout
static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
Definition: sws_ops.c:110
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
av_log_set_level
void av_log_set_level(int level)
Set the log level.
Definition: log.c:477
SwsFormat::format
enum AVPixelFormat format
Definition: format.h:80
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:933
SwsOp
Definition: ops.h:190
ret
ret
Definition: filter_design.txt:187
SwsOpList::dst
SwsFormat dst
Definition: ops.h:236
SWS_OP_MAX
@ SWS_OP_MAX
Definition: ops.h:64
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:3388
cmp_str
static int cmp_str(const void *a, const void *b)
Definition: sws_ops.c:48
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
register_op
static int register_op(SwsContext *ctx, void *opaque, SwsOp *op)
Definition: sws_ops.c:54
desc
const char * desc
Definition: libsvtav1.c:82
av_log_default_callback
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
Definition: log.c:380
mem.h
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
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:231
unused
static const bool unused[4]
Definition: sws_ops.c:53
SwsContext
Main external API structure.
Definition: swscale.h:206
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3376