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 {
39  const AVClass *class;
40  char *pix_fmts;
45  int listed_pix_fmt_flags[AV_PIX_FMT_NB];
47 
48 #define AV_PIX_FMT_NAME_MAXSIZE 32
49 
50 static av_cold int init(AVFilterContext *ctx)
51 {
52  FormatContext *s = ctx->priv;
53  const char *cur, *sep;
54  char pix_fmt_name[AV_PIX_FMT_NAME_MAXSIZE];
55  int pix_fmt_name_len;
57 
58  /* parse the list of formats */
59  for (cur = s->pix_fmts; cur; cur = sep ? sep + 1 : NULL) {
60  if (!(sep = strchr(cur, '|')))
61  pix_fmt_name_len = strlen(cur);
62  else
63  pix_fmt_name_len = sep - cur;
64  if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
65  av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
66  return -1;
67  }
68 
69  memcpy(pix_fmt_name, cur, pix_fmt_name_len);
70  pix_fmt_name[pix_fmt_name_len] = 0;
71  pix_fmt = av_get_pix_fmt(pix_fmt_name);
72 
73  if (pix_fmt == AV_PIX_FMT_NONE) {
74  av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
75  return -1;
76  }
77 
79  }
80 
81  return 0;
82 }
83 
85 {
88 
89  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
90  if (s->listed_pix_fmt_flags[pix_fmt] == flag) {
91  int ret = ff_add_format(&formats, pix_fmt);
92  if (ret < 0) {
93  ff_formats_unref(&formats);
94  return NULL;
95  }
96  }
97 
98  return formats;
99 }
100 
101 #define OFFSET(x) offsetof(FormatContext, x)
102 static const AVOption options[] = {
103  { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
104  { NULL },
105 };
106 
107 #if CONFIG_FORMAT_FILTER
108 static int query_formats_format(AVFilterContext *ctx)
109 {
111  return 0;
112 }
113 
114 static const AVClass format_class = {
115  .class_name = "format",
116  .item_name = av_default_item_name,
117  .option = options,
118  .version = LIBAVUTIL_VERSION_INT,
119 };
120 
121 static const AVFilterPad avfilter_vf_format_inputs[] = {
122  {
123  .name = "default",
124  .type = AVMEDIA_TYPE_VIDEO,
125  .get_video_buffer = ff_null_get_video_buffer,
126  },
127  { NULL }
128 };
129 
130 static const AVFilterPad avfilter_vf_format_outputs[] = {
131  {
132  .name = "default",
133  .type = AVMEDIA_TYPE_VIDEO
134  },
135  { NULL }
136 };
137 
138 AVFilter ff_vf_format = {
139  .name = "format",
140  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
141 
142  .init = init,
143 
144  .query_formats = query_formats_format,
145 
146  .priv_size = sizeof(FormatContext),
147  .priv_class = &format_class,
148 
149  .inputs = avfilter_vf_format_inputs,
150  .outputs = avfilter_vf_format_outputs,
151 };
152 #endif /* CONFIG_FORMAT_FILTER */
153 
154 #if CONFIG_NOFORMAT_FILTER
155 static int query_formats_noformat(AVFilterContext *ctx)
156 {
158  return 0;
159 }
160 
161 static const AVClass noformat_class = {
162  .class_name = "noformat",
163  .item_name = av_default_item_name,
164  .option = options,
165  .version = LIBAVUTIL_VERSION_INT,
166 };
167 
168 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
169  {
170  .name = "default",
171  .type = AVMEDIA_TYPE_VIDEO,
172  .get_video_buffer = ff_null_get_video_buffer,
173  },
174  { NULL }
175 };
176 
177 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
178  {
179  .name = "default",
180  .type = AVMEDIA_TYPE_VIDEO
181  },
182  { NULL }
183 };
184 
185 AVFilter ff_vf_noformat = {
186  .name = "noformat",
187  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
188 
189  .init = init,
190 
191  .query_formats = query_formats_noformat,
192 
193  .priv_size = sizeof(FormatContext),
194  .priv_class = &noformat_class,
195 
196  .inputs = avfilter_vf_noformat_inputs,
197  .outputs = avfilter_vf_noformat_outputs,
198 };
199 #endif /* CONFIG_NOFORMAT_FILTER */
int listed_pix_fmt_flags[AV_PIX_FMT_NB]
List of flags telling if a given image format has been listed as argument to the filter.
Definition: vf_format.c:45
AVOption.
Definition: opt.h:233
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
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:101
static enum AVSampleFormat formats[]
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:375
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
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
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:148
char * pix_fmts
Definition: vf_format.c:40
common internal API header
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:50
enum AVPixelFormat pix_fmt
Definition: movenc.c:821
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const AVOption options[]
Definition: vf_format.c:102
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:45
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:267
#define AV_PIX_FMT_NAME_MAXSIZE
Definition: vf_format.c:48
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:111
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:298
const char * name
Filter name.
Definition: avfilter.h:425
int ff_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:199
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
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:193
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1461
static AVFilterFormats * make_format_list(FormatContext *s, int flag)
Definition: vf_format.c:84
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63