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
42 int ret = ff_sws_ops_translate(ctx, ops, 0, uops);
43 if (ret == AVERROR(ENOTSUP))
44 goto fail;
45
46 if (pass_idx > 0)
47 av_log(NULL, AV_LOG_INFO, " Sub-pass #%d:\n", pass_idx);
48
50 if (ret < 0) {
51 av_log(NULL, AV_LOG_ERROR, "Error translating ops: %s\n", av_err2str(ret));
52 goto fail;
53 }
54
55 av_log(NULL, AV_LOG_INFO, " translated micro-ops:\n");
56 for (int i = 0; i < uops->num_ops; i++) {
58 ff_sws_uop_name(&uops->ops[i], name);
59 av_log(NULL, AV_LOG_INFO, " %s\n", name);
60 }
61
62 *out = (SwsCompiledOp) {0}; /* dummy value, will be immediately freed */
63 pass_idx++;
64 ret = 0;
65
66fail:
68 return ret;
69}
70
71/* Dummy backend that just prints all seen op lists */
73 .name = "print_ops",
74 .compile = print_ops,
75};
76
77static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
78{
79 av_log(NULL, AV_LOG_INFO, "%s %dx%d -> %s %dx%d:\n",
81 ops->src.width, ops->src.height,
83 ops->dst.width, ops->dst.height);
84
85 if (ff_sws_op_list_is_noop(ops)) {
86 av_log(NULL, AV_LOG_INFO, " (no-op)\n");
87 return 0;
88 }
89
90 /* ff_sws_compile_pass() takes over ownership of `ops` */
92 if (!copy)
93 return AVERROR(ENOMEM);
94
95 pass_idx = 0;
98}
99static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
100{
101 if (level != AV_LOG_INFO) {
102 av_log_default_callback(avcl, level, fmt, vl);
103 } else if (av_log_get_level() >= AV_LOG_INFO) {
104 vfprintf(stdout, fmt, vl);
105 }
106}
107
108int main(int argc, char **argv)
109{
110 enum AVPixelFormat src_fmt = AV_PIX_FMT_NONE;
111 enum AVPixelFormat dst_fmt = AV_PIX_FMT_NONE;
113 SwsGraph *graph = NULL;
114 int ret = 1;
115
116#ifdef _WIN32
117 _setmode(_fileno(stdout), _O_BINARY);
118#endif
119
120 for (int i = 1; i < argc; i++) {
121 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
122 fprintf(stderr,
123 "sws_ops [options...]\n"
124 " -help\n"
125 " This text\n"
126 " -dst <pixfmt>\n"
127 " Only test the specified destination pixel format\n"
128 " -src <pixfmt>\n"
129 " Only test the specified source pixel format\n"
130 " -v <level>\n"
131 " Enable log verbosity at given level\n"
132 );
133 return 0;
134 }
135 if (!strcmp(argv[i], "-src")) {
136 if (i + 1 >= argc)
137 goto bad_option;
138 src_fmt = av_get_pix_fmt(argv[i + 1]);
139 if (src_fmt == AV_PIX_FMT_NONE) {
140 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
141 return AVERROR(EINVAL);
142 }
143 i++;
144 } else if (!strcmp(argv[i], "-dst")) {
145 if (i + 1 >= argc)
146 goto bad_option;
147 dst_fmt = av_get_pix_fmt(argv[i + 1]);
148 if (dst_fmt == AV_PIX_FMT_NONE) {
149 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
150 return AVERROR(EINVAL);
151 }
152 i++;
153 } else if (!strcmp(argv[i], "-v")) {
154 if (i + 1 >= argc)
155 goto bad_option;
156 av_log_set_level(atoi(argv[i + 1]));
157 i++;
158 } else {
159bad_option:
160 fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
161 return AVERROR(EINVAL);
162 }
163 }
164
165 /* Allocate dummy graph and context for ff_sws_compile_pass() */
166 graph = ff_sws_graph_alloc();
167 if (!graph)
168 goto fail;
169 graph->ctx = ctx = sws_alloc_context();
170 if (!ctx)
171 goto fail;
172 ctx->scaler = SWS_SCALE_BILINEAR; /* reduce filter generation overhead */
173
175
176 ret = ff_sws_enum_op_lists(ctx, graph, NULL, src_fmt, dst_fmt, print_passes);
177 if (ret < 0)
178 goto fail;
179
180 ret = 0;
181fail:
183 ff_sws_graph_free(&graph);
184 return ret;
185}
#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:866
void ff_sws_graph_free(SwsGraph **pgraph)
Uninitialize any state associate with this filter graph and free it.
Definition graph.c:942
#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:1032
void sws_free_context(SwsContext **ctx)
Free the context and everything associated with it, and write NULL to the provided pointer.
Definition utils.c:2321
@ 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:761
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:987
SwsOpList * ff_sws_op_list_duplicate(const SwsOpList *ops)
Returns a duplicate of ops, or NULL on OOM.
Definition ops.c:673
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:3380
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3392
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:293
SwsFormat dst
Definition ops.h:298
SwsFormat src
Definition ops.h:298
SwsUOp * ops
Definition uops.h:301
int num_ops
Definition uops.h:302
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:72
static int print_passes(SwsContext *ctx, void *graph, SwsOpList *ops)
Definition sws_ops.c:77
static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
Definition sws_ops.c:99
static int print_ops(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Definition sws_ops.c:36
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
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:677
void ff_sws_uop_name(const SwsUOp *op, char buf[SWS_UOP_NAME_MAX])
Definition uops.c:81
SwsUOpList * ff_sws_uop_list_alloc(void)
Definition uops.c:203
void ff_sws_uop_list_free(SwsUOpList **p_ops)
Definition uops.c:189
#define SWS_UOP_NAME_MAX
Generate a unique name for a SwsUOp.
Definition uops.h:297
static void copy(const float *p1, float *p2, const int length)