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 <string.h>
27 
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/opt.h"
32 
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 typedef struct FormatContext {
39  const AVClass *class;
40  char *pix_fmts;
41 
42  /**
43  * pix_fmts parsed into AVPixelFormats and terminated with
44  * AV_PIX_FMT_NONE
45  */
48 
50 {
51  FormatContext *s = ctx->priv;
52  av_freep(&s->formats);
53 }
54 
56 {
57  FormatContext *s = ctx->priv;
58  char *cur, *sep;
59  int nb_formats = 1;
60  int i;
61  int ret;
62 
63  if (!s->pix_fmts) {
64  av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
65  return AVERROR(EINVAL);
66  }
67 
68  /* count the formats */
69  cur = s->pix_fmts;
70  while ((cur = strchr(cur, '|'))) {
71  nb_formats++;
72  if (*cur)
73  cur++;
74  }
75 
76  s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
77  if (!s->formats)
78  return AVERROR(ENOMEM);
79 
80  /* parse the list of formats */
81  cur = s->pix_fmts;
82  for (i = 0; i < nb_formats; i++) {
83  sep = strchr(cur, '|');
84  if (sep)
85  *sep++ = 0;
86 
87  if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0)
88  return ret;
89 
90  cur = sep;
91  }
92  s->formats[nb_formats] = AV_PIX_FMT_NONE;
93 
94  if (!strcmp(ctx->filter->name, "noformat")) {
95  const AVPixFmtDescriptor *desc = NULL;
96  enum AVPixelFormat *formats_allowed;
97  int nb_formats_lavu = 0, nb_formats_allowed = 0;
98 
99  /* count the formats known to lavu */
100  while ((desc = av_pix_fmt_desc_next(desc)))
101  nb_formats_lavu++;
102 
103  formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
104  if (!formats_allowed)
105  return AVERROR(ENOMEM);
106 
107  /* for each format known to lavu, check if it's in the list of
108  * forbidden formats */
109  while ((desc = av_pix_fmt_desc_next(desc))) {
111 
112  for (i = 0; i < nb_formats; i++) {
113  if (s->formats[i] == pix_fmt)
114  break;
115  }
116  if (i < nb_formats)
117  continue;
118 
119  formats_allowed[nb_formats_allowed++] = pix_fmt;
120  }
121  formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
122  av_freep(&s->formats);
123  s->formats = formats_allowed;
124  }
125 
126  return 0;
127 }
128 
130 {
131  FormatContext *s = ctx->priv;
132 
133  return ff_set_common_formats_from_list(ctx, s->formats);
134 }
135 
136 
137 #define OFFSET(x) offsetof(FormatContext, x)
138 static const AVOption options[] = {
139  { "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 },
140  { NULL }
141 };
142 
144 
145 #if CONFIG_FORMAT_FILTER
146 
147 static const AVFilterPad avfilter_vf_format_inputs[] = {
148  {
149  .name = "default",
150  .type = AVMEDIA_TYPE_VIDEO,
151  .get_buffer.video = ff_null_get_video_buffer,
152  },
153 };
154 
155 static const AVFilterPad avfilter_vf_format_outputs[] = {
156  {
157  .name = "default",
158  .type = AVMEDIA_TYPE_VIDEO
159  },
160 };
161 
162 const AVFilter ff_vf_format = {
163  .name = "format",
164  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
165 
166  .init = init,
167  .uninit = uninit,
168 
169  .priv_size = sizeof(FormatContext),
170  .priv_class = &format_class,
171 
172  FILTER_INPUTS(avfilter_vf_format_inputs),
173  FILTER_OUTPUTS(avfilter_vf_format_outputs),
174 
176 };
177 #endif /* CONFIG_FORMAT_FILTER */
178 
179 #if CONFIG_NOFORMAT_FILTER
180 
181 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
182  {
183  .name = "default",
184  .type = AVMEDIA_TYPE_VIDEO,
185  .get_buffer.video = ff_null_get_video_buffer,
186  },
187 };
188 
189 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
190  {
191  .name = "default",
192  .type = AVMEDIA_TYPE_VIDEO
193  },
194 };
195 
196 const AVFilter ff_vf_noformat = {
197  .name = "noformat",
198  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
199  .priv_class = &format_class,
200 
201  .init = init,
202  .uninit = uninit,
203 
204  .priv_size = sizeof(FormatContext),
205 
206  FILTER_INPUTS(avfilter_vf_noformat_inputs),
207  FILTER_OUTPUTS(avfilter_vf_noformat_outputs),
208 
210 };
211 #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:280
pixdesc.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:49
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
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:2571
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
FormatContext::pix_fmts
char * pix_fmts
Definition: vf_format.c:40
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:293
formats.h
options
static const AVOption options[]
Definition: vf_format.c:138
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
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
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_format.c:129
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:705
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
ctx
AVFormatContext * ctx
Definition: movenc.c:48
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
FormatContext
Definition: vf_format.c:38
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:117
OFFSET
#define OFFSET(x)
Definition: vf_format.c:137
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2583
ff_null_get_video_buffer
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:38
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:271
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:55
internal.h
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
ff_vf_format
const AVFilter ff_vf_format
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
desc
const char * desc
Definition: libsvtav1.c:79
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:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
format
fg outputs[0] format
Definition: ffmpeg_filter.c:175
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:762
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
FormatContext::formats
enum AVPixelFormat * formats
pix_fmts parsed into AVPixelFormats and terminated with AV_PIX_FMT_NONE
Definition: vf_format.c:46