]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
Merge commit '62549f9655c48f0ec061087fa33a96040ce01145'
[ffmpeg] / libavfilter / vf_format.c
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];
47 } FormatContext;
48
49 #define AV_PIX_FMT_NAME_MAXSIZE 32
50
51 static av_cold int init(AVFilterContext *ctx, const char *args)
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;
57     enum AVPixelFormat pix_fmt;
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
82 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
83 {
84     AVFilterFormats *formats;
85     enum AVPixelFormat pix_fmt;
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 {
106     ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
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 {
153     ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
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 */