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 "internal.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 typedef struct {
40  const AVClass *class;
41  char *pix_fmts;
42  /**
43  * List of flags telling if a given image format has been listed
44  * as argument to the filter.
45  */
46  int listed_pix_fmt_flags[AV_PIX_FMT_NB];
48 
49 #define AV_PIX_FMT_NAME_MAXSIZE 32
50 
51 static av_cold int init(AVFilterContext *ctx)
52 {
53  FormatContext *format = ctx->priv;
54  const char *cur, *sep;
55  char pix_fmt_name[AV_PIX_FMT_NAME_MAXSIZE];
56  int pix_fmt_name_len, ret;
58 
59  /* parse the list of formats */
60  for (cur = format->pix_fmts; cur; cur = sep ? sep + 1 : NULL) {
61  if (!(sep = strchr(cur, '|')))
62  pix_fmt_name_len = strlen(cur);
63  else
64  pix_fmt_name_len = sep - cur;
65  if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
66  av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
67  return -1;
68  }
69 
70  memcpy(pix_fmt_name, cur, pix_fmt_name_len);
71  pix_fmt_name[pix_fmt_name_len] = 0;
72 
73  if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
74  return ret;
75 
76  format->listed_pix_fmt_flags[pix_fmt] = 1;
77  }
78 
79  return 0;
80 }
81 
83 {
86 
87  formats = av_mallocz(sizeof(AVFilterFormats));
88  formats->formats = av_malloc(sizeof(enum AVPixelFormat) * AV_PIX_FMT_NB);
89 
90  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
91  if (format->listed_pix_fmt_flags[pix_fmt] == flag)
92  formats->formats[formats->format_count++] = pix_fmt;
93 
94  return formats;
95 }
96 
97 #define OFFSET(x) offsetof(FormatContext, x)
98 static const AVOption options[] = {
99  { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
100  { NULL },
101 };
102 
103 #if CONFIG_FORMAT_FILTER
104 static int query_formats_format(AVFilterContext *ctx)
105 {
107  return 0;
108 }
109 
110 static const AVClass format_class = {
111  .class_name = "format",
112  .item_name = av_default_item_name,
113  .option = options,
114  .version = LIBAVUTIL_VERSION_INT,
115 };
116 
117 static const AVFilterPad avfilter_vf_format_inputs[] = {
118  {
119  .name = "default",
120  .type = AVMEDIA_TYPE_VIDEO,
121  .get_video_buffer = ff_null_get_video_buffer,
122  },
123  { NULL }
124 };
125 
126 static const AVFilterPad avfilter_vf_format_outputs[] = {
127  {
128  .name = "default",
129  .type = AVMEDIA_TYPE_VIDEO
130  },
131  { NULL }
132 };
133 
134 AVFilter avfilter_vf_format = {
135  .name = "format",
136  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
137 
138  .init = init,
139 
140  .query_formats = query_formats_format,
141 
142  .priv_size = sizeof(FormatContext),
143  .priv_class = &format_class,
144 
145  .inputs = avfilter_vf_format_inputs,
146  .outputs = avfilter_vf_format_outputs,
147 };
148 #endif /* CONFIG_FORMAT_FILTER */
149 
150 #if CONFIG_NOFORMAT_FILTER
151 static int query_formats_noformat(AVFilterContext *ctx)
152 {
154  return 0;
155 }
156 
157 static const AVClass noformat_class = {
158  .class_name = "noformat",
159  .item_name = av_default_item_name,
160  .option = options,
161  .version = LIBAVUTIL_VERSION_INT,
162 };
163 
164 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
165  {
166  .name = "default",
167  .type = AVMEDIA_TYPE_VIDEO,
168  .get_video_buffer = ff_null_get_video_buffer,
169  },
170  { NULL }
171 };
172 
173 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
174  {
175  .name = "default",
176  .type = AVMEDIA_TYPE_VIDEO
177  },
178  { NULL }
179 };
180 
181 AVFilter avfilter_vf_noformat = {
182  .name = "noformat",
183  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
184 
185  .init = init,
186 
187  .query_formats = query_formats_noformat,
188 
189  .priv_size = sizeof(FormatContext),
190  .priv_class = &noformat_class,
191 
192  .inputs = avfilter_vf_noformat_inputs,
193  .outputs = avfilter_vf_noformat_outputs,
194 };
195 #endif /* CONFIG_NOFORMAT_FILTER */
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
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:46
AVOption.
Definition: opt.h:251
av_default_item_name
int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Parse a pixel format.
Definition: formats.c:579
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
memory handling functions
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
#define OFFSET(x)
Definition: vf_format.c:97
const char * name
Pad name.
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:55
#define av_cold
Definition: attributes.h:78
AVOptions.
enum AVPixelFormat pix_fmt
Definition: v4l.c:63
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:545
A filter pad used for either input or output.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
char * pix_fmts
Definition: vf_format.c:41
common internal API header
static av_cold int init(AVFilterContext *ctx)
Definition: vf_format.c:51
ret
Definition: avfilter.c:821
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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 format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static const AVOption options[]
Definition: vf_format.c:98
NULL
Definition: eval.c:55
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:285
#define AV_PIX_FMT_NAME_MAXSIZE
Definition: vf_format.c:49
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common formats
Definition: swscale.txt:33
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:237
static AVFilterFormats * make_format_list(FormatContext *format, int flag)
Definition: vf_format.c:82
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
unsigned format_count
number of formats
Definition: formats.h:65
int * formats
list of media formats
Definition: formats.h:66