FFmpeg
op_list_gen_template.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdbool.h>
20 
21 #include "libswscale/format.h"
22 #include "libswscale/ops.h"
23 #include "libswscale/swscale.h"
24 
25 #include "libavutil/error.h"
26 #include "libavutil/macros.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/pixfmt.h"
29 
30 #define DUMMY_SIZE 16
31 
32 static int enum_ops_fmt(SwsContext *ctx, void *opaque,
33  enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
34  int (*cb)(SwsContext *ctx, void *opaque, SwsOpList *ops))
35 {
36  int ret = 0;
37  SwsOpList *ops = NULL;
38  SwsFormat src, dst;
39  ff_fmt_from_pixfmt(src_fmt, &src);
40  ff_fmt_from_pixfmt(dst_fmt, &dst);
41  bool incomplete = ff_infer_colors(&src.color, &dst.color);
42  src.width = src.height = DUMMY_SIZE;
43 
44  static const int dst_sizes[][2] = {
46  { DUMMY_SIZE, DUMMY_SIZE * 2 },
47  { DUMMY_SIZE * 2, DUMMY_SIZE },
48  { DUMMY_SIZE * 2, DUMMY_SIZE * 2 },
49  };
50 
51  for (int i = 0; i < FF_ARRAY_ELEMS(dst_sizes); i++) {
52  dst.width = dst_sizes[i][0];
53  dst.height = dst_sizes[i][1];
54 
55  ret = ff_sws_op_list_generate(ctx, &src, &dst, &ops, &incomplete);
56  if (ret == AVERROR(ENOTSUP))
57  return 0; /* silently skip unsupported formats */
58  else if (ret < 0)
59  return ret;
60 
62  if (ret < 0)
63  goto fail;
64 
65  ret = cb(ctx, opaque, ops);
66  if (ret < 0)
67  goto fail;
68 
69  ff_sws_op_list_free(&ops);
70  }
71 
72 fail:
73  ff_sws_op_list_free(&ops);
74  return ret;
75 }
76 
77 /**
78  * Helper function to enumerate over all possible (optimized) operation lists,
79  * under the current set of options in `ctx`, and run the given callback on
80  * each list.
81  *
82  * @param src_fmt If set (not AV_PIX_FMT_NONE), constrain the source format
83  * @param dst_fmt If set (not AV_PIX_FMT_NONE), constrain the destination format
84  * @return 0 on success, the return value if cb() < 0, or a negative error code
85  *
86  * @note `ops` belongs to sws_enum_op_lists(), but may be mutated by `cb`.
87  */
88 static inline
89 int ff_sws_enum_op_lists(SwsContext *ctx, void *opaque,
90  enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
91  int (*cb)(SwsContext *ctx, void *opaque, SwsOpList *ops))
92 {
93  const AVPixFmtDescriptor *src_start = av_pix_fmt_desc_next(NULL);
94  const AVPixFmtDescriptor *dst_start = src_start;
95  if (src_fmt != AV_PIX_FMT_NONE)
96  src_start = av_pix_fmt_desc_get(src_fmt);
97  if (dst_fmt != AV_PIX_FMT_NONE)
98  dst_start = av_pix_fmt_desc_get(dst_fmt);
99 
100  const AVPixFmtDescriptor *src, *dst;
101  for (src = src_start; src; src = av_pix_fmt_desc_next(src)) {
102  const enum AVPixelFormat src_f = av_pix_fmt_desc_get_id(src);
103  for (dst = dst_start; dst; dst = av_pix_fmt_desc_next(dst)) {
104  const enum AVPixelFormat dst_f = av_pix_fmt_desc_get_id(dst);
105  int ret = enum_ops_fmt(ctx, opaque, src_f, dst_f, cb);
106  if (ret < 0)
107  return ret;
108  if (dst_fmt != AV_PIX_FMT_NONE)
109  break;
110  }
111  if (src_fmt != AV_PIX_FMT_NONE)
112  break;
113  }
114 
115  return 0;
116 }
ff_sws_op_list_free
void ff_sws_op_list_free(SwsOpList **p_ops)
Definition: ops.c:649
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
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
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
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
enum_ops_fmt
static int enum_ops_fmt(SwsContext *ctx, void *opaque, enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, int(*cb)(SwsContext *ctx, void *opaque, SwsOpList *ops))
Definition: op_list_gen_template.c:32
format.h
macros.h
ff_sws_op_list_generate
int ff_sws_op_list_generate(SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst, SwsOpList **out_ops, bool *incomplete)
Generate an SwsOpList defining a conversion from src to dst.
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ff_sws_enum_op_lists
static 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: op_list_gen_template.c:89
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
fail
#define fail
Definition: test.h:478
DUMMY_SIZE
#define DUMMY_SIZE
Definition: op_list_gen_template.c:30
NULL
#define NULL
Definition: coverity.c:32
ff_infer_colors
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition: format.c:537
error.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
SwsFormat
Definition: format.h:77
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:3475
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:340
ff_fmt_from_pixfmt
void ff_fmt_from_pixfmt(enum AVPixelFormat pixfmt, SwsFormat *fmt)
Subset of ff_fmt_from_frame() that sets default metadata for the format.
Definition: format.c:480
ret
ret
Definition: filter_design.txt:187
pixfmt.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:265
SwsContext
Main external API structure.
Definition: swscale.h:229
src
#define src
Definition: vp8dsp.c:248
swscale.h