FFmpeg  4.0
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;
133 
134  if (!formats)
135  return AVERROR(ENOMEM);
136 
137  return ff_set_common_formats(ctx, formats);
138 }
139 
140 
141 #define OFFSET(x) offsetof(FormatContext, x)
142 static const AVOption options[] = {
143  { "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 },
144  { NULL }
145 };
146 
147 #if CONFIG_FORMAT_FILTER
148 
149 #define format_options options
151 
152 static const AVFilterPad avfilter_vf_format_inputs[] = {
153  {
154  .name = "default",
155  .type = AVMEDIA_TYPE_VIDEO,
156  .get_video_buffer = ff_null_get_video_buffer,
157  },
158  { NULL }
159 };
160 
161 static const AVFilterPad avfilter_vf_format_outputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO
165  },
166  { NULL }
167 };
168 
170  .name = "format",
171  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
172 
173  .init = init,
174  .uninit = uninit,
175 
176  .query_formats = query_formats,
177 
178  .priv_size = sizeof(FormatContext),
179  .priv_class = &format_class,
180 
181  .inputs = avfilter_vf_format_inputs,
182  .outputs = avfilter_vf_format_outputs,
183 };
184 #endif /* CONFIG_FORMAT_FILTER */
185 
186 #if CONFIG_NOFORMAT_FILTER
187 
188 #define noformat_options options
189 AVFILTER_DEFINE_CLASS(noformat);
190 
191 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
192  {
193  .name = "default",
194  .type = AVMEDIA_TYPE_VIDEO,
195  .get_video_buffer = ff_null_get_video_buffer,
196  },
197  { NULL }
198 };
199 
200 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
201  {
202  .name = "default",
203  .type = AVMEDIA_TYPE_VIDEO
204  },
205  { NULL }
206 };
207 
209  .name = "noformat",
210  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
211 
212  .init = init,
213  .uninit = uninit,
214 
215  .query_formats = query_formats,
216 
217  .priv_size = sizeof(FormatContext),
218  .priv_class = &noformat_class,
219 
220  .inputs = avfilter_vf_noformat_inputs,
221  .outputs = avfilter_vf_noformat_outputs,
222 };
223 #endif /* CONFIG_NOFORMAT_FILTER */
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static enum AVPixelFormat pix_fmt
static const char * format[]
Definition: af_aiir.c:311
AVOption.
Definition: opt.h:246
int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Parse a pixel format.
Definition: formats.c:609
Main libavfilter public API header.
Memory handling functions.
const char * desc
Definition: nvenc.c:65
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:39
#define OFFSET(x)
Definition: vf_format.c:141
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
#define av_cold
Definition: attributes.h:82
AVOptions.
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
Definition: opt.h:291
static int query_formats(AVFilterContext *ctx)
Definition: vf_format.c:129
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
char * pix_fmts
Definition: vf_format.c:40
common internal API header
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2382
AVFilter ff_vf_noformat
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:55
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_format.c:49
enum AVPixelFormat * formats
pix_fmts parsed into AVPixelFormats and terminated with AV_PIX_FMT_NONE
Definition: vf_format.c:46
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static const AVOption options[]
Definition: vf_format.c:142
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:279
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilter ff_vf_format
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
#define av_freep(p)
#define av_malloc_array(a, b)
formats
Definition: signature.h:48
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:2370