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/pixdesc.h"
22 #include "libswscale/ops.h"
23 #include "libswscale/format.h"
24 
25 #ifdef _WIN32
26 #include <io.h>
27 #include <fcntl.h>
28 #endif
29 
30 static int run_test(SwsContext *const ctx, AVFrame *frame,
31  const AVPixFmtDescriptor *const src_desc,
32  const AVPixFmtDescriptor *const dst_desc)
33 {
34  /* Reuse ff_fmt_from_frame() to ensure correctly sanitized metadata */
35  frame->format = av_pix_fmt_desc_get_id(src_desc);
37  frame->format = av_pix_fmt_desc_get_id(dst_desc);
39  bool incomplete = ff_infer_colors(&src.color, &dst.color);
40 
42  if (!ops)
43  return AVERROR(ENOMEM);
44  ops->src = src;
45  ops->dst = dst;
46 
47  if (ff_sws_decode_pixfmt(ops, src.format) < 0)
48  goto fail;
49  if (ff_sws_decode_colors(ctx, SWS_PIXEL_F32, ops, src, &incomplete) < 0)
50  goto fail;
51  if (ff_sws_encode_colors(ctx, SWS_PIXEL_F32, ops, dst, &incomplete) < 0)
52  goto fail;
53  if (ff_sws_encode_pixfmt(ops, dst.format) < 0)
54  goto fail;
55 
56  av_log(NULL, AV_LOG_INFO, "%s -> %s:\n",
58 
61 
62 fail:
63  /* silently skip unsupported formats */
64  ff_sws_op_list_free(&ops);
65  return 0;
66 }
67 
68 static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
69 {
70  if (level != AV_LOG_INFO) {
71  av_log_default_callback(avcl, level, fmt, vl);
72  } else {
73  vfprintf(stdout, fmt, vl);
74  }
75 }
76 
77 int main(int argc, char **argv)
78 {
79  int ret = 1;
80 
81 #ifdef _WIN32
82  _setmode(_fileno(stdout), _O_BINARY);
83 #endif
84 
87  if (!ctx || !frame)
88  goto fail;
89  frame->width = frame->height = 16;
90 
92  for (const AVPixFmtDescriptor *src = NULL; (src = av_pix_fmt_desc_next(src));) {
93  for (const AVPixFmtDescriptor *dst = NULL; (dst = av_pix_fmt_desc_next(dst));) {
94  int err = run_test(ctx, frame, src, dst);
95  if (err < 0)
96  goto fail;
97  }
98  }
99 
100  ret = 0;
101 fail:
104  return ret;
105 }
ff_sws_op_list_free
void ff_sws_op_list_free(SwsOpList **p_ops)
Definition: ops.c:230
level
uint8_t level
Definition: svq3.c:208
ff_sws_op_list_alloc
SwsOpList * ff_sws_op_list_alloc(void)
Definition: ops.c:219
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
ff_fmt_from_frame
SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
This function also sanitizes and strips the input data, removing irrelevant fields for certain format...
Definition: format.c:307
main
int main(int argc, char **argv)
Definition: sws_ops.c:77
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
pixdesc.h
ops.h
av_pix_fmt_desc_next
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:3463
format.h
SWS_PIXEL_F32
@ SWS_PIXEL_F32
Definition: ops.h:35
fail
#define fail()
Definition: checkasm.h:214
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
ff_sws_encode_colors
int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat fmt, bool *incomplete)
ctx
AVFormatContext * ctx
Definition: movenc.c:49
NULL
#define NULL
Definition: coverity.c:32
ff_infer_colors
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition: format.c:508
av_log_set_callback
void av_log_set_callback(void(*callback)(void *, int, const char *, va_list))
Set the logging callback.
Definition: log.c:490
sws_alloc_context
SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext and set its fields to default values.
Definition: utils.c:1019
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
SwsOpList::src
SwsFormat src
Definition: ops.h:213
SwsFormat
Definition: format.h:77
run_test
static int run_test(SwsContext *const ctx, AVFrame *frame, const AVPixFmtDescriptor *const src_desc, const AVPixFmtDescriptor *const dst_desc)
Definition: sws_ops.c:30
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:3475
log_stdout
static void log_stdout(void *avcl, int level, const char *fmt, va_list vl)
Definition: sws_ops.c:68
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
ff_sws_op_list_optimize
int ff_sws_op_list_optimize(SwsOpList *ops)
Fuse compatible and eliminate redundant operations, as well as replacing some operations with more ef...
Definition: ops_optimizer.c:412
ff_sws_decode_pixfmt
int ff_sws_decode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
Append a set of operations for decoding/encoding raw pixels.
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
SwsOpList::dst
SwsFormat dst
Definition: ops.h:213
ff_sws_decode_colors
int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat fmt, bool *incomplete)
Append a set of operations for transforming decoded pixel values to/from normalized RGB in the specif...
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:378
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
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:2327
ff_sws_op_list_print
void ff_sws_op_list_print(void *log, int lev, const SwsOpList *ops)
Print out the contents of an operation list.
Definition: ops.c:396
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:208
SwsContext
Main external API structure.
Definition: swscale.h:189
src
#define src
Definition: vp8dsp.c:248
ff_sws_encode_pixfmt
int ff_sws_encode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
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