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 "formats.h"
37 #include "internal.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 
89 {
90  FormatContext *s = ctx->priv;
92  int ret;
93 
94  for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
95  sep = strchr(cur, '|');
96  if (sep && *sep)
97  *sep++ = 0;
98  if ((ret = ff_parse_pixel_format(&pix_fmt, cur, ctx)) < 0 ||
99  (ret = ff_add_format(&s->formats, pix_fmt)) < 0)
100  return ret;
101  }
102 
103  for (char *sep, *cur = s->csps; cur; cur = sep) {
104  sep = strchr(cur, '|');
105  if (sep && *sep)
106  *sep++ = 0;
107  if ((ret = av_color_space_from_name(cur)) < 0 ||
108  (ret = ff_add_format(&s->color_spaces, ret)) < 0)
109  return ret;
110  }
111 
112  for (char *sep, *cur = s->ranges; cur; cur = sep) {
113  sep = strchr(cur, '|');
114  if (sep && *sep)
115  *sep++ = 0;
116  if ((ret = av_color_range_from_name(cur)) < 0 ||
117  (ret = ff_add_format(&s->color_ranges, ret)) < 0)
118  return ret;
119  }
120 
121  if (!strcmp(ctx->filter->name, "noformat")) {
122  if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
123  (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
124  (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0)
125  return ret;
126  }
127 
128  /* hold on to a ref for the lifetime of the filter */
129  if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
130  s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
131  s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0)
132  return ret;
133 
134  return 0;
135 }
136 
138 {
139  FormatContext *s = ctx->priv;
140  int ret;
141 
142  if (s->formats && (ret = ff_set_common_formats(ctx, s->formats)) < 0 ||
143  s->color_spaces && (ret = ff_set_common_color_spaces(ctx, s->color_spaces)) < 0 ||
144  s->color_ranges && (ret = ff_set_common_color_ranges(ctx, s->color_ranges)) < 0)
145  return ret;
146 
147  return 0;
148 }
149 
150 
151 #define OFFSET(x) offsetof(FormatContext, x)
152 static const AVOption options[] = {
153  { "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 },
154  { "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 },
155  { "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 },
156  { NULL }
157 };
158 
160 
161 static const AVFilterPad inputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO,
165  .get_buffer.video = ff_null_get_video_buffer,
166  },
167 };
168 
169 #if CONFIG_FORMAT_FILTER
170 const AVFilter ff_vf_format = {
171  .name = "format",
172  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
173 
174  .init = init,
175  .uninit = uninit,
176 
177  .priv_size = sizeof(FormatContext),
178  .priv_class = &format_class,
179 
181 
184 
186 };
187 #endif /* CONFIG_FORMAT_FILTER */
188 
189 #if CONFIG_NOFORMAT_FILTER
190 const AVFilter ff_vf_noformat = {
191  .name = "noformat",
192  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
193  .priv_class = &format_class,
194 
195  .init = init,
196  .uninit = uninit,
197 
198  .priv_size = sizeof(FormatContext),
199 
201 
204 
206 };
207 #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
FormatContext::color_ranges
AVFilterFormats * color_ranges
parsed from ranges
Definition: vf_format.c:48
inputs
static const AVFilterPad inputs[]
Definition: vf_format.c:161
av_color_space_from_name
int av_color_space_from_name(const char *name)
Definition: pixdesc.c:3350
pixdesc.h
av_color_range_from_name
int av_color_range_from_name(const char *name)
Definition: pixdesc.c:3290
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:51
AVOption
AVOption.
Definition: opt.h:294
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
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
options
static const AVOption options[]
Definition: vf_format.c:152
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: internal.h:33
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:867
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
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_format.c:137
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
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
FormatContext
Definition: vf_format.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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
ff_all_color_spaces
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition: formats.c:630
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:280
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:106
OFFSET
#define OFFSET(x)
Definition: vf_format.c:151
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
internal.h
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
ff_set_common_color_ranges
int ff_set_common_color_ranges(AVFilterContext *ctx, AVFilterFormats *color_ranges)
Definition: formats.c:844
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:88
internal.h
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:257
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
ff_set_common_color_spaces
int ff_set_common_color_spaces(AVFilterContext *ctx, AVFilterFormats *color_spaces)
Definition: formats.c:826
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:133
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:409
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
ff_parse_pixel_format
int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Parse a pixel format.
Definition: formats.c:939
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229