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 
44  /**
45  * pix_fmts parsed into AVPixelFormats and terminated with
46  * AV_PIX_FMT_NONE
47  */
50 
52 {
53  FormatContext *s = ctx->priv;
54  av_freep(&s->formats);
55 }
56 
58 {
59  FormatContext *s = ctx->priv;
60  char *cur, *sep;
61  int nb_formats = 1;
62  int i;
63  int ret;
64 
65  if (!s->pix_fmts) {
66  av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
67  return AVERROR(EINVAL);
68  }
69 
70  /* count the formats */
71  cur = s->pix_fmts;
72  while ((cur = strchr(cur, '|'))) {
73  nb_formats++;
74  if (*cur)
75  cur++;
76  }
77 
78  s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
79  if (!s->formats)
80  return AVERROR(ENOMEM);
81 
82  /* parse the list of formats */
83  cur = s->pix_fmts;
84  for (i = 0; i < nb_formats; i++) {
85  sep = strchr(cur, '|');
86  if (sep)
87  *sep++ = 0;
88 
89  if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
90  return ret;
91 
92  cur = sep;
93  }
94  s->formats[nb_formats] = AV_PIX_FMT_NONE;
95 
96  if (!strcmp(ctx->filter->name, "noformat")) {
97  const AVPixFmtDescriptor *desc = NULL;
98  enum AVPixelFormat *formats_allowed;
99  int nb_formats_lavu = 0, nb_formats_allowed = 0;
100 
101  /* count the formats known to lavu */
102  while ((desc = av_pix_fmt_desc_next(desc)))
103  nb_formats_lavu++;
104 
105  formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
106  if (!formats_allowed)
107  return AVERROR(ENOMEM);
108 
109  /* for each format known to lavu, check if it's in the list of
110  * forbidden formats */
111  while ((desc = av_pix_fmt_desc_next(desc))) {
113 
114  for (i = 0; i < nb_formats; i++) {
115  if (s->formats[i] == pix_fmt)
116  break;
117  }
118  if (i < nb_formats)
119  continue;
120 
121  formats_allowed[nb_formats_allowed++] = pix_fmt;
122  }
123  formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
124  av_freep(&s->formats);
125  s->formats = formats_allowed;
126  }
127 
128  return 0;
129 }
130 
132 {
133  FormatContext *s = ctx->priv;
134 
135  return ff_set_common_formats_from_list(ctx, s->formats);
136 }
137 
138 
139 #define OFFSET(x) offsetof(FormatContext, x)
140 static const AVOption options[] = {
141  { "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 },
142  { NULL }
143 };
144 
146 
147 #if CONFIG_FORMAT_FILTER
148 
149 static const AVFilterPad avfilter_vf_format_inputs[] = {
150  {
151  .name = "default",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .get_buffer.video = ff_null_get_video_buffer,
154  },
155 };
156 
157 const AVFilter ff_vf_format = {
158  .name = "format",
159  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
160 
161  .init = init,
162  .uninit = uninit,
163 
164  .priv_size = sizeof(FormatContext),
165  .priv_class = &format_class,
166 
168 
169  FILTER_INPUTS(avfilter_vf_format_inputs),
171 
173 };
174 #endif /* CONFIG_FORMAT_FILTER */
175 
176 #if CONFIG_NOFORMAT_FILTER
177 
178 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
179  {
180  .name = "default",
181  .type = AVMEDIA_TYPE_VIDEO,
182  .get_buffer.video = ff_null_get_video_buffer,
183  },
184 };
185 
186 const AVFilter ff_vf_noformat = {
187  .name = "noformat",
188  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
189  .priv_class = &format_class,
190 
191  .init = init,
192  .uninit = uninit,
193 
194  .priv_size = sizeof(FormatContext),
195 
197 
198  FILTER_INPUTS(avfilter_vf_noformat_inputs),
200 
202 };
203 #endif /* CONFIG_NOFORMAT_FILTER */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:284
pixdesc.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:51
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
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:2971
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
FormatContext::pix_fmts
char * pix_fmts
Definition: vf_format.c:42
video.h
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:297
formats.h
options
static const AVOption options[]
Definition: vf_format.c:140
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:36
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_format.c:131
s
#define s(width, name)
Definition: cbs_vp9.c:198
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_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:776
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:192
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
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:139
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2983
ff_null_get_video_buffer
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:43
internal.h
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options)
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:57
internal.h
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
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
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:833
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
FormatContext::formats
enum AVPixelFormat * formats
pix_fmts parsed into AVPixelFormats and terminated with AV_PIX_FMT_NONE
Definition: vf_format.c:48