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 "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 typedef struct {
41  int listed_pix_fmt_flags[AV_PIX_FMT_NB];
43 
44 #define AV_PIX_FMT_NAME_MAXSIZE 32
45 
46 static av_cold int init(AVFilterContext *ctx, const char *args)
47 {
48  FormatContext *format = ctx->priv;
49  const char *cur, *sep;
50  char pix_fmt_name[AV_PIX_FMT_NAME_MAXSIZE];
51  int pix_fmt_name_len;
53 
54  /* parse the list of formats */
55  for (cur = args; cur; cur = sep ? sep+1 : NULL) {
56  if (!(sep = strchr(cur, ':')))
57  pix_fmt_name_len = strlen(cur);
58  else
59  pix_fmt_name_len = sep - cur;
60  if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
61  av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
62  return -1;
63  }
64 
65  memcpy(pix_fmt_name, cur, pix_fmt_name_len);
66  pix_fmt_name[pix_fmt_name_len] = 0;
67  pix_fmt = av_get_pix_fmt(pix_fmt_name);
68 
69  if (pix_fmt == AV_PIX_FMT_NONE) {
70  av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
71  return -1;
72  }
73 
74  format->listed_pix_fmt_flags[pix_fmt] = 1;
75  }
76 
77  return 0;
78 }
79 
81 {
84 
85  formats = av_mallocz(sizeof(AVFilterFormats));
86  formats->formats = av_malloc(sizeof(enum AVPixelFormat) * AV_PIX_FMT_NB);
87 
88  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
89  if (format->listed_pix_fmt_flags[pix_fmt] == flag)
90  formats->formats[formats->format_count++] = pix_fmt;
91 
92  return formats;
93 }
94 
95 #if CONFIG_FORMAT_FILTER
96 static int query_formats_format(AVFilterContext *ctx)
97 {
99  return 0;
100 }
101 
102 static const AVFilterPad avfilter_vf_format_inputs[] = {
103  {
104  .name = "default",
105  .type = AVMEDIA_TYPE_VIDEO,
106  .get_video_buffer = ff_null_get_video_buffer,
107  },
108  { NULL }
109 };
110 
111 static const AVFilterPad avfilter_vf_format_outputs[] = {
112  {
113  .name = "default",
114  .type = AVMEDIA_TYPE_VIDEO
115  },
116  { NULL }
117 };
118 
119 AVFilter avfilter_vf_format = {
120  .name = "format",
121  .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
122 
123  .init = init,
124 
125  .query_formats = query_formats_format,
126 
127  .priv_size = sizeof(FormatContext),
128 
129  .inputs = avfilter_vf_format_inputs,
130  .outputs = avfilter_vf_format_outputs,
131 };
132 #endif /* CONFIG_FORMAT_FILTER */
133 
134 #if CONFIG_NOFORMAT_FILTER
135 static int query_formats_noformat(AVFilterContext *ctx)
136 {
138  return 0;
139 }
140 
141 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_VIDEO,
145  .get_video_buffer = ff_null_get_video_buffer,
146  },
147  { NULL }
148 };
149 
150 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_VIDEO
154  },
155  { NULL }
156 };
157 
158 AVFilter avfilter_vf_noformat = {
159  .name = "noformat",
160  .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
161 
162  .init = init,
163 
164  .query_formats = query_formats_noformat,
165 
166  .priv_size = sizeof(FormatContext),
167 
168  .inputs = avfilter_vf_noformat_inputs,
169  .outputs = avfilter_vf_noformat_outputs,
170 };
171 #endif /* CONFIG_NOFORMAT_FILTER */
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:61
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:41
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
memory handling functions
static enum AVSampleFormat formats[]
const char * name
Pad name.
Definition: internal.h:39
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:33
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
common internal API header
enum AVPixelFormat pix_fmt
Definition: movenc.c:801
NULL
Definition: eval.c:52
#define AV_PIX_FMT_NAME_MAXSIZE
Definition: vf_format.c:44
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
const char * name
filter name
Definition: avfilter.h:372
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_format.c:46
An instance of a filter.
Definition: avfilter.h:418
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:181
static AVFilterFormats * make_format_list(FormatContext *format, int flag)
Definition: vf_format.c:80
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1389
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
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:158
unsigned format_count
number of formats
Definition: formats.h:65
int * formats
list of media formats
Definition: formats.h:66