h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_format.c
CommitLineData
fcbed3c7 1/*
3fa77bde 2 * Copyright (c) 2007 Bobby Bingham
fcbed3c7 3 *
2912e87a 4 * This file is part of Libav.
fcbed3c7 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
fcbed3c7
SS
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
fcbed3c7
SS
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
fcbed3c7
SS
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
ba87f080 22 * @file
dc264e77 23 * format and noformat video filters
fcbed3c7
SS
24 */
25
1d9c2dc8
MS
26#include <string.h>
27
28#include "libavutil/internal.h"
29#include "libavutil/mem.h"
718c7b18 30#include "libavutil/pixdesc.h"
e67a87ea
AK
31#include "libavutil/opt.h"
32
fcbed3c7 33#include "avfilter.h"
b74a1da4 34#include "formats.h"
9d0bfc50 35#include "internal.h"
c04c533f 36#include "video.h"
fcbed3c7 37
58400ac1 38typedef struct FormatContext {
e67a87ea
AK
39 const AVClass *class;
40 char *pix_fmts;
45fc73ed 41
fcbed3c7 42 /**
45fc73ed
AK
43 * pix_fmts parsed into AVPixelFormats and terminated with
44 * AV_PIX_FMT_NONE
fcbed3c7 45 */
45fc73ed 46 enum AVPixelFormat *formats;
fcbed3c7
SS
47} FormatContext;
48
45fc73ed
AK
49static av_cold void uninit(AVFilterContext *ctx)
50{
51 FormatContext *s = ctx->priv;
52 av_freep(&s->formats);
53}
fcbed3c7 54
c43a7eca 55static av_cold int init(AVFilterContext *ctx)
fcbed3c7 56{
cbec213a 57 FormatContext *s = ctx->priv;
45fc73ed
AK
58 char *cur, *sep;
59 int nb_formats = 1;
60 int i;
61
d1afd3e1
VG
62 if (!s->pix_fmts) {
63 av_log(ctx, AV_LOG_ERROR, "Empty output format string.\n");
64 return AVERROR(EINVAL);
65 }
66
45fc73ed
AK
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);
fcbed3c7
SS
78
79 /* parse the list of formats */
45fc73ed
AK
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);
fcbed3c7
SS
90 }
91
45fc73ed
AK
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;;
fcbed3c7 100
45fc73ed
AK
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))) {
112 enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc);
fcbed3c7 113
45fc73ed
AK
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;
fcbed3c7
SS
126 }
127
128 return 0;
129}
130
45fc73ed 131static int query_formats(AVFilterContext *ctx)
fcbed3c7 132{
45fc73ed
AK
133 FormatContext *s = ctx->priv;
134 AVFilterFormats *formats = ff_make_format_list(s->formats);
135
136 if (!formats)
137 return AVERROR(ENOMEM);
fcbed3c7 138
45fc73ed
AK
139 ff_set_common_formats(ctx, formats);
140 return 0;
fcbed3c7
SS
141}
142
45fc73ed 143
e67a87ea
AK
144#define OFFSET(x) offsetof(FormatContext, x)
145static 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
c0bc2fed 150#if CONFIG_FORMAT_FILTER
e67a87ea
AK
151static 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
568c70e7
MR
158static const AVFilterPad avfilter_vf_format_inputs[] = {
159 {
160 .name = "default",
161 .type = AVMEDIA_TYPE_VIDEO,
162 .get_video_buffer = ff_null_get_video_buffer,
568c70e7
MR
163 },
164 { NULL }
165};
166
167static const AVFilterPad avfilter_vf_format_outputs[] = {
168 {
169 .name = "default",
170 .type = AVMEDIA_TYPE_VIDEO
171 },
172 { NULL }
173};
174
cd43ca04 175AVFilter ff_vf_format = {
fcbed3c7 176 .name = "format",
a6881c6a 177 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
fcbed3c7
SS
178
179 .init = init,
45fc73ed 180 .uninit = uninit,
fcbed3c7 181
45fc73ed 182 .query_formats = query_formats,
fcbed3c7
SS
183
184 .priv_size = sizeof(FormatContext),
e67a87ea 185 .priv_class = &format_class,
fcbed3c7 186
568c70e7
MR
187 .inputs = avfilter_vf_format_inputs,
188 .outputs = avfilter_vf_format_outputs,
fcbed3c7 189};
c0bc2fed 190#endif /* CONFIG_FORMAT_FILTER */
fcbed3c7 191
c0bc2fed 192#if CONFIG_NOFORMAT_FILTER
e67a87ea
AK
193static 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
568c70e7
MR
200static const AVFilterPad avfilter_vf_noformat_inputs[] = {
201 {
202 .name = "default",
203 .type = AVMEDIA_TYPE_VIDEO,
204 .get_video_buffer = ff_null_get_video_buffer,
568c70e7
MR
205 },
206 { NULL }
207};
208
209static const AVFilterPad avfilter_vf_noformat_outputs[] = {
210 {
211 .name = "default",
212 .type = AVMEDIA_TYPE_VIDEO
213 },
214 { NULL }
215};
216
cd43ca04 217AVFilter ff_vf_noformat = {
fcbed3c7 218 .name = "noformat",
a6881c6a 219 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
fcbed3c7
SS
220
221 .init = init,
45fc73ed 222 .uninit = uninit,
fcbed3c7 223
45fc73ed 224 .query_formats = query_formats,
fcbed3c7
SS
225
226 .priv_size = sizeof(FormatContext),
e67a87ea 227 .priv_class = &noformat_class,
fcbed3c7 228
568c70e7
MR
229 .inputs = avfilter_vf_noformat_inputs,
230 .outputs = avfilter_vf_noformat_outputs,
fcbed3c7 231};
c0bc2fed 232#endif /* CONFIG_NOFORMAT_FILTER */