FFmpeg
Loading...
Searching...
No Matches
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/mem.h"
22#include "libavutil/pixdesc.h"
23#include "libswscale/ops.h"
27#include "libswscale/format.h"
28
29#ifdef _WIN32
30#include <io.h>
31#include <fcntl.h>
32#endif
33
34static int pass_idx;
35
37{
39 if (!uops)
40 return AVERROR(ENOMEM);
41
45 int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
46 if (ret == AVERROR(ENOTSUP))
47 goto fail;
48
49 if (pass_idx > 0)
50 av_log(NULL, AV_LOG_INFO, " Sub-pass #%d:\n", pass_idx);
51
53 if (ret < 0) {
54 av_log(NULL, AV_LOG_ERROR, "Error translating ops: %s\n", av_err2str(ret));
55 goto fail;
56 }
57
58 av_log(NULL, AV_LOG_INFO, " translated micro-ops:\n");
59 for (int i = 0; i < uops->num_ops; i++) {
61 ff_sws_uop_name(&uops->ops[i], name);
62 av_log(NULL, AV_LOG_INFO, " %s\n", name);
63 }
64
65 *out = (SwsCompiledOp) {0}; /* dummy value, will be immediately freed */
66 pass_idx++;
67 ret = 0;
68
69fail:
71 return ret;
72}
73
74/* Dummy backend that just prints all seen op lists */
76 .name = "print_ops",
77 .compile = print_ops,
78};
79
80static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
81{
82 av_log(NULL, AV_LOG_INFO, "%s %dx%d -> %s %dx%d:\n",
84 ops->src.width, ops->src.height,
86 ops->dst.width, ops->dst.height);
87
88 if (ff_sws_op_list_is_noop(ops)) {
89 av_log(NULL, AV_LOG_INFO, " (no-op)\n");
90 return 0;
91 }
92
93 /* ff_sws_compile_pass() takes over ownership of `ops` */
95 if (!copy)
96 return AVERROR(ENOMEM);
97
98 pass_idx = 0;
101}
102static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
103{
104 if (level != AV_LOG_INFO) {
105 av_log_default_callback(avcl, level, fmt, vl);
106 } else if (av_log_get_level() >= AV_LOG_INFO) {
107 vfprintf(stdout, fmt, vl);
108 }
109}
110
111int main(int argc, char **argv)
112{
113 enum AVPixelFormat src_fmt = AV_PIX_FMT_NONE;
114 enum AVPixelFormat dst_fmt = AV_PIX_FMT_NONE;
116 SwsGraph *graph = NULL;
117 int ret = 1;
118
119#ifdef _WIN32
120 _setmode(_fileno(stdout), _O_BINARY);
121#endif
122
123 for (int i = 1; i < argc; i++) {
124 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
125 fprintf(stderr,
126 "sws_ops [options...]\n"
127 " -help\n"
128 " This text\n"
129 " -dst <pixfmt>\n"
130 " Only test the specified destination pixel format\n"
131 " -src <pixfmt>\n"
132 " Only test the specified source pixel format\n"
133 " -v <level>\n"
134 " Enable log verbosity at given level\n"
135 );
136 return 0;
137 }
138 if (!strcmp(argv[i], "-src")) {
139 if (i + 1 >= argc)
140 goto bad_option;
141 src_fmt = av_get_pix_fmt(argv[i + 1]);
142 if (src_fmt == AV_PIX_FMT_NONE) {
143 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
144 return AVERROR(EINVAL);
145 }
146 i++;
147 } else if (!strcmp(argv[i], "-dst")) {
148 if (i + 1 >= argc)
149 goto bad_option;
150 dst_fmt = av_get_pix_fmt(argv[i + 1]);
151 if (dst_fmt == AV_PIX_FMT_NONE) {
152 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
153 return AVERROR(EINVAL);
154 }
155 i++;
156 } else if (!strcmp(argv[i], "-v")) {
157 if (i + 1 >= argc)
158 goto bad_option;
159 av_log_set_level(atoi(argv[i + 1]));
160 i++;
161 } else {
162bad_option:
163 fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
164 return AVERROR(EINVAL);
165 }
166 }
167
168 /* Allocate dummy graph and context for ff_sws_compile_pass() */
169 graph = ff_sws_graph_alloc();
170 if (!graph)
171 goto fail;
172 graph->ctx = ctx = sws_alloc_context();
173 if (!ctx)
174 goto fail;
175 ctx->scaler = SWS_SCALE_BILINEAR; /* reduce filter generation overhead */
176
178
179 ret = ff_sws_enum_op_lists(ctx, graph, NULL, src_fmt, dst_fmt, print_passes);
180 if (ret < 0)
181 goto fail;
182
183 ret = 0;
184fail:
186 ff_sws_graph_free(&graph);
187 return ret;
188}
static FILE * out
static AVFormatContext * ctx
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
int main
Definition dovi_rpuenc.c:38
SwsGraph * ff_sws_graph_alloc(void)
Allocate an empty SwsGraph.
Definition graph.c:872
void ff_sws_graph_free(SwsGraph **pgraph)
Uninitialize any state associate with this filter graph and free it.
Definition graph.c:948
#define fail
Definition test.h:479
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition log.c:491
void av_log_set_level(int level)
Set the log level.
Definition log.c:476
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
Definition log.c:379
int av_log_get_level(void)
Get the current log level.
Definition log.c:471
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition utils.c:1051
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition utils.c:2374
@ SWS_SCALE_BILINEAR
bilinear filtering
Definition swscale.h:98
Memory handling functions.
static int ff_sws_enum_op_lists(SwsContext *ctx, void *opaque, const SwsLut3D *lut3d, 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 ...
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:756
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:981
SwsOpList * ff_sws_op_list_duplicate(const SwsOpList *ops)
Returns a duplicate of ops, or NULL on OOM.
Definition ops.c:668
int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend, SwsOpList **pops, int flags, SwsPass *input, SwsPass **output)
Resolves an operation list to a graph pass.
@ SWS_OP_FLAG_DRY_RUN
@ SWS_OP_FLAG_SPLIT_MEMCPY
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:3404
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3416
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
const char * name
Definition qsvenc.c:142
Main external API structure.
Definition swscale.h:227
enum AVPixelFormat format
Definition format.h:81
int width
Definition format.h:78
int height
Definition format.h:78
Filter graph, which represents a 'baked' pixel format conversion.
Definition graph.h:132
SwsContext * ctx
Definition graph.h:133
Helper struct for representing a list of operations.
Definition ops.h:297
SwsFormat dst
Definition ops.h:302
SwsFormat src
Definition ops.h:302
SwsUOp * ops
Definition uops.h:329
int num_ops
Definition uops.h:330
uint8_t level
Definition svq3.c:208
static int pass_idx
Definition sws_ops.c:34
static const SwsOpBackend backend_print
Definition sws_ops.c:75
static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
Definition sws_ops.c:80
static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
Definition sws_ops.c:102
static int print_ops(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition sws_ops.c:36
#define av_log(a,...)
int ff_sws_ops_translate(SwsContext *ctx, const SwsOpList *ops, SwsUOpFlags flags, SwsUOpList *uops)
Translate a list of operations down to micro-ops, which can be further optimized and then directly ex...
Definition uops.c:618
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition uops.c:52
SwsUOpList * ff_sws_uop_list_alloc(void)
Definition uops.c:174
void ff_sws_uop_list_free(SwsUOpList **p_ops)
Definition uops.c:160
uint32_t SwsUOpFlags
Definition uops.h:147
@ SWS_UOP_FLAG_EXPAND_BIT
Definition uops.h:152
@ SWS_UOP_FLAG_ADD
Definition uops.h:154
@ SWS_UOP_FLAG_READ_PALETTE
Definition uops.h:153
#define SWS_UOP_NAME_MAX
Generate a unique name for a SwsUOp.
Definition uops.h:325
static void copy(const float *p1, float *p2, const int length)