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
46 int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
47 if (ret == AVERROR(ENOTSUP))
48 goto fail;
49
50 if (pass_idx > 0)
51 av_log(NULL, AV_LOG_INFO, " Sub-pass #%d:\n", pass_idx);
52
54 if (ret < 0) {
55 av_log(NULL, AV_LOG_ERROR, "Error translating ops: %s\n", av_err2str(ret));
56 goto fail;
57 }
58
59 av_log(NULL, AV_LOG_INFO, " translated micro-ops:\n");
60 for (int i = 0; i < uops->num_ops; i++) {
62 ff_sws_uop_name(&uops->ops[i], name);
63 av_log(NULL, AV_LOG_INFO, " %s\n", name);
64 }
65
66 *out = (SwsCompiledOp) {0}; /* dummy value, will be immediately freed */
67 pass_idx++;
68 ret = 0;
69
70fail:
72 return ret;
73}
74
75/* Dummy backend that just prints all seen op lists */
77 .name = "print_ops",
78 .compile = print_ops,
79};
80
81static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
82{
83 av_log(NULL, AV_LOG_INFO, "%s %dx%d -> %s %dx%d:\n",
85 ops->src.width, ops->src.height,
87 ops->dst.width, ops->dst.height);
88
89 if (ff_sws_op_list_is_noop(ops)) {
90 av_log(NULL, AV_LOG_INFO, " (no-op)\n");
91 return 0;
92 }
93
94 /* ff_sws_compile_pass() takes over ownership of `ops` */
96 if (!copy)
97 return AVERROR(ENOMEM);
98
99 pass_idx = 0;
102}
103static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
104{
105 if (level != AV_LOG_INFO) {
106 av_log_default_callback(avcl, level, fmt, vl);
107 } else if (av_log_get_level() >= AV_LOG_INFO) {
108 vfprintf(stdout, fmt, vl);
109 }
110}
111
112int main(int argc, char **argv)
113{
114 enum AVPixelFormat src_fmt = AV_PIX_FMT_NONE;
115 enum AVPixelFormat dst_fmt = AV_PIX_FMT_NONE;
117 SwsGraph *graph = NULL;
118 int ret = 1;
119
120#ifdef _WIN32
121 _setmode(_fileno(stdout), _O_BINARY);
122#endif
123
124 for (int i = 1; i < argc; i++) {
125 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
126 fprintf(stderr,
127 "sws_ops [options...]\n"
128 " -help\n"
129 " This text\n"
130 " -dst <pixfmt>\n"
131 " Only test the specified destination pixel format\n"
132 " -src <pixfmt>\n"
133 " Only test the specified source pixel format\n"
134 " -v <level>\n"
135 " Enable log verbosity at given level\n"
136 );
137 return 0;
138 }
139 if (!strcmp(argv[i], "-src")) {
140 if (i + 1 >= argc)
141 goto bad_option;
142 src_fmt = av_get_pix_fmt(argv[i + 1]);
143 if (src_fmt == AV_PIX_FMT_NONE) {
144 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
145 return AVERROR(EINVAL);
146 }
147 i++;
148 } else if (!strcmp(argv[i], "-dst")) {
149 if (i + 1 >= argc)
150 goto bad_option;
151 dst_fmt = av_get_pix_fmt(argv[i + 1]);
152 if (dst_fmt == AV_PIX_FMT_NONE) {
153 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
154 return AVERROR(EINVAL);
155 }
156 i++;
157 } else if (!strcmp(argv[i], "-v")) {
158 if (i + 1 >= argc)
159 goto bad_option;
160 av_log_set_level(atoi(argv[i + 1]));
161 i++;
162 } else {
163bad_option:
164 fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
165 return AVERROR(EINVAL);
166 }
167 }
168
169 /* Allocate dummy graph and context for ff_sws_compile_pass() */
170 graph = ff_sws_graph_alloc();
171 if (!graph)
172 goto fail;
173 graph->ctx = ctx = sws_alloc_context();
174 if (!ctx)
175 goto fail;
176 ctx->scaler = SWS_SCALE_BILINEAR; /* reduce filter generation overhead */
177
179
180 ret = ff_sws_enum_op_lists(ctx, graph, NULL, src_fmt, dst_fmt, print_passes);
181 if (ret < 0)
182 goto fail;
183
184 ret = 0;
185fail:
187 ff_sws_graph_free(&graph);
188 return ret;
189}
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
int ret
Definition ffplay.c:391
SwsGraph * ff_sws_graph_alloc(void)
Allocate an empty SwsGraph.
Definition graph.c:881
void ff_sws_graph_free(SwsGraph **pgraph)
Uninitialize any state associate with this filter graph and free it.
Definition graph.c:957
#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:497
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
void av_log_default_callback(void *ptr, int level, const char *fmt, va_list vl)
Default logging callback.
Definition log.c:385
int av_log_get_level(void)
Get the current log level.
Definition log.c:477
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition utils.c:1082
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition utils.c:2409
@ 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:831
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:1041
SwsOpList * ff_sws_op_list_duplicate(const SwsOpList *ops)
Returns a duplicate of ops, or NULL on OOM.
Definition ops.c:757
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:85
int width
Definition format.h:82
int height
Definition format.h:82
Filter graph, which represents a 'baked' pixel format conversion.
Definition graph.h:133
SwsContext * ctx
Definition graph.h:134
Helper struct for representing a list of operations.
Definition ops.h:318
SwsFormat dst
Definition ops.h:323
SwsFormat src
Definition ops.h:323
SwsUOp * ops
Definition uops.h:345
int num_ops
Definition uops.h:346
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:76
static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
Definition sws_ops.c:81
static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
Definition sws_ops.c:103
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:644
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition uops.c:54
SwsUOpList * ff_sws_uop_list_alloc(void)
Definition uops.c:176
void ff_sws_uop_list_free(SwsUOpList **p_ops)
Definition uops.c:162
uint32_t SwsUOpFlags
Definition uops.h:158
@ SWS_UOP_FLAG_EXPAND_BIT
Definition uops.h:163
@ SWS_UOP_FLAG_ADD
Definition uops.h:165
@ SWS_UOP_FLAG_READ_PALETTE
Definition uops.h:164
@ SWS_UOP_FLAG_PUNNING
Definition uops.h:166
#define SWS_UOP_NAME_MAX
Generate a unique name for a SwsUOp.
Definition uops.h:341
static void copy(const float *p1, float *p2, const int length)