Libav
vf_format.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
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 
48 
49 static av_cold void uninit(AVFilterContext *ctx)
50 {
51  FormatContext *s = ctx->priv;
52  av_freep(&s->formats);
53 }
54 
55 static av_cold int init(AVFilterContext *ctx)
56 {
57  FormatContext *s = ctx->priv;
58  char *cur, *sep;
59  int nb_formats = 1;
60  int i;
61 
62  if (!s->pix_fmts) {
63  av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
64  return AVERROR(EINVAL);
65  }
66 
67  /* count the formats */
68  cur = s->pix_fmts;
69  while ((cur = strchr(cur, '|'))) {
70  nb_formats++;
71  if (*cur)
72  cur++;
73  }
74 
75  s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
76  if (!s->formats)
77  return AVERROR(ENOMEM);
78 
79  /* parse the list of formats */
80  cur = s->pix_fmts;
81  for (i = 0; i < nb_formats; i++) {
82  sep = strchr(cur, '|');
83  if (sep)
84  *sep++ = 0;
85 
86  s->formats[i] = av_get_pix_fmt(cur);
87  if (s->formats[i] == AV_PIX_FMT_NONE) {
88  av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
89  return AVERROR(EINVAL);
90  }
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;
135 
136  if (!formats)
137  return AVERROR(ENOMEM);
138 
139  ff_set_common_formats(ctx, formats);
140  return 0;
141 }
142 
143 
144 #define OFFSET(x) offsetof(FormatContext, x)
145 static const AVOption options[] = {
146  { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
147  { NULL },
148 };
149 
150 #if CONFIG_FORMAT_FILTER
151 static const AVClass format_class = {
152  .class_name = "format",
153  .item_name = av_default_item_name,
154  .option = options,
155  .version = LIBAVUTIL_VERSION_INT,
156 };
157 
158 static const AVFilterPad avfilter_vf_format_inputs[] = {
159  {
160  .name = "default",
161  .type = AVMEDIA_TYPE_VIDEO,
162  .get_video_buffer = ff_null_get_video_buffer,
163  },
164  { NULL }
165 };
166 
167 static const AVFilterPad avfilter_vf_format_outputs[] = {
168  {
169  .name = "default",
170  .type = AVMEDIA_TYPE_VIDEO
171  },
172  { NULL }
173 };
174 
175 AVFilter ff_vf_format = {
176  .name = "format",
177  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
178 
179  .init = init,
180  .uninit = uninit,
181 
182  .query_formats = query_formats,
183 
184  .priv_size = sizeof(FormatContext),
185  .priv_class = &format_class,
186 
187  .inputs = avfilter_vf_format_inputs,
188  .outputs = avfilter_vf_format_outputs,
189 };
190 #endif /* CONFIG_FORMAT_FILTER */
191 
192 #if CONFIG_NOFORMAT_FILTER
193 static const AVClass noformat_class = {
194  .class_name = "noformat",
195  .item_name = av_default_item_name,
196  .option = options,
197  .version = LIBAVUTIL_VERSION_INT,
198 };
199 
200 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
201  {
202  .name = "default",
203  .type = AVMEDIA_TYPE_VIDEO,
204  .get_video_buffer = ff_null_get_video_buffer,
205  },
206  { NULL }
207 };
208 
209 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
210  {
211  .name = "default",
212  .type = AVMEDIA_TYPE_VIDEO
213  },
214  { NULL }
215 };
216 
217 AVFilter ff_vf_noformat = {
218  .name = "noformat",
219  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
220 
221  .init = init,
222  .uninit = uninit,
223 
224  .query_formats = query_formats,
225 
226  .priv_size = sizeof(FormatContext),
227  .priv_class = &noformat_class,
228 
229  .inputs = avfilter_vf_noformat_inputs,
230  .outputs = avfilter_vf_noformat_outputs,
231 };
232 #endif /* CONFIG_NOFORMAT_FILTER */
AVOption.
Definition: opt.h:234
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
memory handling functions
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
#define OFFSET(x)
Definition: vf_format.c:144
static enum AVSampleFormat formats[]
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define av_cold
Definition: attributes.h:66
AVOptions.
void 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:379
A filter pad used for either input or output.
Definition: internal.h:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static int query_formats(AVFilterContext *ctx)
Definition: vf_format.c:131
#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:150
void * priv
private data for use by the filter
Definition: avfilter.h:584
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
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:1615
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
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const AVOption options[]
Definition: vf_format.c:145
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:270
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
const char * name
Filter name.
Definition: avfilter.h:425
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:92
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1552
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:566
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:1606