FFmpeg
vf_format.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
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 /**
22  * @file
23  * format and noformat video filters
24  */
25 
26 #include "config_components.h"
27 
28 #include <string.h>
29 
30 #include "libavutil/internal.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/opt.h"
34 
35 #include "avfilter.h"
36 #include "filters.h"
37 #include "formats.h"
38 #include "video.h"
39 
40 typedef struct FormatContext {
41  const AVClass *class;
42  char *pix_fmts;
43  char *csps;
44  char *ranges;
45 
46  AVFilterFormats *formats; ///< parsed from `pix_fmts`
47  AVFilterFormats *color_spaces; ///< parsed from `csps`
48  AVFilterFormats *color_ranges; ///< parsed from `ranges`
50 
52 {
53  FormatContext *s = ctx->priv;
54  ff_formats_unref(&s->formats);
55  ff_formats_unref(&s->color_spaces);
56  ff_formats_unref(&s->color_ranges);
57 }
58 
60  AVFilterFormats *allfmts)
61 {
62  if (!allfmts)
63  return AVERROR(ENOMEM);
64  if (!*fmts) {
65  /* empty fmt list means no restriction, regardless of filter type */
66  ff_formats_unref(&allfmts);
67  return 0;
68  }
69 
70  for (int i = 0; i < allfmts->nb_formats; i++) {
71  for (int j = 0; j < (*fmts)->nb_formats; j++) {
72  if (allfmts->formats[i] == (*fmts)->formats[j]) {
73  /* format is forbidden, remove it from allfmts list */
74  memmove(&allfmts->formats[i], &allfmts->formats[i+1],
75  (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats));
76  allfmts->nb_formats--;
77  i--; /* repeat loop with same idx */
78  break;
79  }
80  }
81  }
82 
83  ff_formats_unref(fmts);
84  *fmts = allfmts;
85  return 0;
86 }
87 
88 static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
89 {
90  char *tail;
91  int pix_fmt = av_get_pix_fmt(arg);
92  if (pix_fmt == AV_PIX_FMT_NONE) {
93  pix_fmt = strtol(arg, &tail, 0);
94  if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
95  av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
96  return AVERROR(EINVAL);
97  }
98  }
99  *ret = pix_fmt;
100  return 0;
101 }
102 
104 {
105  FormatContext *s = ctx->priv;
106  enum AVPixelFormat pix_fmt;
107  int ret;
108 
109  for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
110  sep = strchr(cur, '|');
111  if (sep && *sep)
112  *sep++ = 0;
113  if ((ret = parse_pixel_format(&pix_fmt, cur, ctx)) < 0 ||
114  (ret = ff_add_format(&s->formats, pix_fmt)) < 0)
115  return ret;
116  }
117 
118  for (char *sep, *cur = s->csps; cur; cur = sep) {
119  sep = strchr(cur, '|');
120  if (sep && *sep)
121  *sep++ = 0;
122  if ((ret = av_color_space_from_name(cur)) < 0 ||
123  (ret = ff_add_format(&s->color_spaces, ret)) < 0)
124  return ret;
125  }
126 
127  for (char *sep, *cur = s->ranges; cur; cur = sep) {
128  sep = strchr(cur, '|');
129  if (sep && *sep)
130  *sep++ = 0;
131  if ((ret = av_color_range_from_name(cur)) < 0 ||
132  (ret = ff_add_format(&s->color_ranges, ret)) < 0)
133  return ret;
134  }
135 
136  if (!strcmp(ctx->filter->name, "noformat")) {
137  if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
138  (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
139  (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0)
140  return ret;
141  }
142 
143  /* hold on to a ref for the lifetime of the filter */
144  if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
145  s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
146  s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0)
147  return ret;
148 
149  return 0;
150 }
151 
153  AVFilterFormatsConfig **cfg_in,
154  AVFilterFormatsConfig **cfg_out)
155 {
156  FormatContext *s = ctx->priv;
157  int ret;
158 
159  if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
160  s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
161  s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0)
162  return ret;
163 
164  return 0;
165 }
166 
167 
168 #define OFFSET(x) offsetof(FormatContext, x)
169 static const AVOption options[] = {
170  { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
171  { "color_spaces", "A '|'-separated list of color spaces", OFFSET(csps), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
172  { "color_ranges", "A '|'-separated list of color ranges", OFFSET(ranges), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
173  { NULL }
174 };
175 
177 
178 static const AVFilterPad inputs[] = {
179  {
180  .name = "default",
181  .type = AVMEDIA_TYPE_VIDEO,
182  .get_buffer.video = ff_null_get_video_buffer,
183  },
184 };
185 
186 #if CONFIG_FORMAT_FILTER
187 const AVFilter ff_vf_format = {
188  .name = "format",
189  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
190 
191  .init = init,
192  .uninit = uninit,
193 
194  .priv_size = sizeof(FormatContext),
195  .priv_class = &format_class,
196 
198 
201 
203 };
204 #endif /* CONFIG_FORMAT_FILTER */
205 
206 #if CONFIG_NOFORMAT_FILTER
207 const AVFilter ff_vf_noformat = {
208  .name = "noformat",
209  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
210  .priv_class = &format_class,
211 
212  .init = init,
213  .uninit = uninit,
214 
215  .priv_size = sizeof(FormatContext),
216 
218 
221 
223 };
224 #endif /* CONFIG_NOFORMAT_FILTER */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FormatContext::ranges
char * ranges
Definition: vf_format.c:44
ff_vf_noformat
const AVFilter ff_vf_noformat
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
opt.h
ff_set_common_color_ranges2
int ff_set_common_color_ranges2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_ranges)
Definition: formats.c:983
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1007
FormatContext::color_ranges
AVFilterFormats * color_ranges
parsed from ranges
Definition: vf_format.c:48
inputs
static const AVFilterPad inputs[]
Definition: vf_format.c:178
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
av_color_space_from_name
int av_color_space_from_name(const char *name)
Definition: pixdesc.c:3552
pixdesc.h
av_color_range_from_name
int av_color_range_from_name(const char *name)
Definition: pixdesc.c:3492
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:51
AVOption
AVOption.
Definition: opt.h:429
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
FormatContext::pix_fmts
char * pix_fmts
Definition: vf_format.c:42
video.h
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:535
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
ff_set_common_color_spaces2
int ff_set_common_color_spaces2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *color_spaces)
Definition: formats.c:959
s
#define s(width, name)
Definition: cbs_vp9.c:198
FormatContext::formats
AVFilterFormats * formats
parsed from pix_fmts
Definition: vf_format.c:46
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:678
filters.h
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
arg
const char * arg
Definition: jacosubdec.c:67
FormatContext
Definition: vf_format.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:504
AVFilterFormats::nb_formats
unsigned nb_formats
number of formats
Definition: formats.h:65
options
Definition: swscale.c:42
ff_all_color_spaces
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition: formats.c:630
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
AV_OPT_FLAG_FILTERING_PARAM
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition: opt.h:381
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
OFFSET
#define OFFSET(x)
Definition: vf_format.c:168
FormatContext::color_spaces
AVFilterFormats * color_spaces
parsed from csps
Definition: vf_format.c:47
invert_formats
static av_cold int invert_formats(AVFilterFormats **fmts, AVFilterFormats *allfmts)
Definition: vf_format.c:59
ff_all_color_ranges
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition: formats.c:646
ff_null_get_video_buffer
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:44
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options)
ff_formats_unref
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to,...
Definition: formats.c:717
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:103
internal.h
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:358
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_format.c:152
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:3102
parse_pixel_format
static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Definition: vf_format.c:88
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:168
ff_vf_format
const AVFilter ff_vf_format
FormatContext::csps
char * csps
Definition: vf_format.c:43
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276