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  /* count the formats */
63  cur = s->pix_fmts;
64  while ((cur = strchr(cur, '|'))) {
65  nb_formats++;
66  if (*cur)
67  cur++;
68  }
69 
70  s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats));
71  if (!s->formats)
72  return AVERROR(ENOMEM);
73 
74  /* parse the list of formats */
75  cur = s->pix_fmts;
76  for (i = 0; i < nb_formats; i++) {
77  sep = strchr(cur, '|');
78  if (sep)
79  *sep++ = 0;
80 
81  s->formats[i] = av_get_pix_fmt(cur);
82  if (s->formats[i] == AV_PIX_FMT_NONE) {
83  av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", cur);
84  return AVERROR(EINVAL);
85  }
86 
87  cur = sep;
88  }
89  s->formats[nb_formats] = AV_PIX_FMT_NONE;
90 
91  if (!strcmp(ctx->filter->name, "noformat")) {
92  const AVPixFmtDescriptor *desc = NULL;
93  enum AVPixelFormat *formats_allowed;
94  int nb_formats_lavu = 0, nb_formats_allowed = 0;;
95 
96  /* count the formats known to lavu */
97  while ((desc = av_pix_fmt_desc_next(desc)))
98  nb_formats_lavu++;
99 
100  formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed));
101  if (!formats_allowed)
102  return AVERROR(ENOMEM);
103 
104  /* for each format known to lavu, check if it's in the list of
105  * forbidden formats */
106  while ((desc = av_pix_fmt_desc_next(desc))) {
108 
109  for (i = 0; i < nb_formats; i++) {
110  if (s->formats[i] == pix_fmt)
111  break;
112  }
113  if (i < nb_formats)
114  continue;
115 
116  formats_allowed[nb_formats_allowed++] = pix_fmt;
117  }
118  formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE;
119  av_freep(&s->formats);
120  s->formats = formats_allowed;
121  }
122 
123  return 0;
124 }
125 
127 {
128  FormatContext *s = ctx->priv;
130 
131  if (!formats)
132  return AVERROR(ENOMEM);
133 
134  ff_set_common_formats(ctx, formats);
135  return 0;
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 },
142  { NULL },
143 };
144 
145 #if CONFIG_FORMAT_FILTER
146 static const AVClass format_class = {
147  .class_name = "format",
148  .item_name = av_default_item_name,
149  .option = options,
150  .version = LIBAVUTIL_VERSION_INT,
151 };
152 
153 static const AVFilterPad avfilter_vf_format_inputs[] = {
154  {
155  .name = "default",
156  .type = AVMEDIA_TYPE_VIDEO,
157  .get_video_buffer = ff_null_get_video_buffer,
158  },
159  { NULL }
160 };
161 
162 static const AVFilterPad avfilter_vf_format_outputs[] = {
163  {
164  .name = "default",
165  .type = AVMEDIA_TYPE_VIDEO
166  },
167  { NULL }
168 };
169 
170 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  .query_formats = query_formats,
178 
179  .priv_size = sizeof(FormatContext),
180  .priv_class = &format_class,
181 
182  .inputs = avfilter_vf_format_inputs,
183  .outputs = avfilter_vf_format_outputs,
184 };
185 #endif /* CONFIG_FORMAT_FILTER */
186 
187 #if CONFIG_NOFORMAT_FILTER
188 static const AVClass noformat_class = {
189  .class_name = "noformat",
190  .item_name = av_default_item_name,
191  .option = options,
192  .version = LIBAVUTIL_VERSION_INT,
193 };
194 
195 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
196  {
197  .name = "default",
198  .type = AVMEDIA_TYPE_VIDEO,
199  .get_video_buffer = ff_null_get_video_buffer,
200  },
201  { NULL }
202 };
203 
204 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
205  {
206  .name = "default",
207  .type = AVMEDIA_TYPE_VIDEO
208  },
209  { NULL }
210 };
211 
212 AVFilter ff_vf_noformat = {
213  .name = "noformat",
214  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
215 
216  .init = init,
217  .uninit = uninit,
218 
219  .query_formats = query_formats,
220 
221  .priv_size = sizeof(FormatContext),
222  .priv_class = &noformat_class,
223 
224  .inputs = avfilter_vf_noformat_inputs,
225  .outputs = avfilter_vf_noformat_outputs,
226 };
227 #endif /* CONFIG_NOFORMAT_FILTER */
AVOption.
Definition: opt.h:234
const char * name
Filter name.
Definition: avfilter.h:425
void * priv
private data for use by the filter
Definition: avfilter.h:584
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:1615
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
#define OFFSET(x)
Definition: vf_format.c:139
static enum AVSampleFormat formats[]
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1552
void av_freep(void *ptr)
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 * 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_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:270
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
#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:126
#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
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
char * pix_fmts
Definition: vf_format.c:40
common internal API header
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
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:566
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
Main libavfilter public API header.
static const AVOption options[]
Definition: vf_format.c:140
NULL
Definition: eval.c:55
#define av_cold
Definition: attributes.h:66
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
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 AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:1606
static void * av_malloc_array(size_t nmemb, size_t size)
Definition: mem.h:92
struct AVFilterPad AVFilterPad
Definition: avfilter.h:67
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
av_default_item_name
Return the context name.
Definition: dnxhdenc.c:52
internal API functions