]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
Merge commit '345a96c327e8f2a8077189af9f5e8d2b3f4ad5fe'
[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 "formats.h"
35 #include "internal.h"
36 #include "video.h"
37
38 typedef struct {
39     const AVClass *class;
40     char *pix_fmts;
41     /**
42      * List of flags telling if a given image format has been listed
43      * as argument to the filter.
44      */
45     int listed_pix_fmt_flags[AV_PIX_FMT_NB];
46 } FormatContext;
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, ret;
56     enum AVPixelFormat pix_fmt;
57
58     if (!s->pix_fmts)
59         return AVERROR(EINVAL);
60
61     /* parse the list of formats */
62     for (cur = s->pix_fmts; cur; cur = sep ? sep + 1 : NULL) {
63         if (!(sep = strchr(cur, '|')))
64             pix_fmt_name_len = strlen(cur);
65         else
66             pix_fmt_name_len = sep - cur;
67         if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
68             av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
69             return -1;
70         }
71
72         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
73         pix_fmt_name[pix_fmt_name_len] = 0;
74
75         if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
76             return ret;
77
78         s->listed_pix_fmt_flags[pix_fmt] = 1;
79     }
80
81     return 0;
82 }
83
84 static AVFilterFormats *make_format_list(FormatContext *s, int flag)
85 {
86     AVFilterFormats *formats = NULL;
87     enum AVPixelFormat pix_fmt;
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 {
110     ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
111     return 0;
112 }
113
114 #define format_options options
115 AVFILTER_DEFINE_CLASS(format);
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 ff_vf_format = {
135     .name          = "format",
136     .description   = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
137     .init          = init,
138     .query_formats = query_formats_format,
139     .priv_size     = sizeof(FormatContext),
140     .priv_class    = &format_class,
141     .inputs        = avfilter_vf_format_inputs,
142     .outputs       = avfilter_vf_format_outputs,
143 };
144 #endif /* CONFIG_FORMAT_FILTER */
145
146 #if CONFIG_NOFORMAT_FILTER
147 static int query_formats_noformat(AVFilterContext *ctx)
148 {
149     ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
150     return 0;
151 }
152
153 #define noformat_options options
154 AVFILTER_DEFINE_CLASS(noformat);
155
156 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
157     {
158         .name             = "default",
159         .type             = AVMEDIA_TYPE_VIDEO,
160         .get_video_buffer = ff_null_get_video_buffer,
161     },
162     { NULL }
163 };
164
165 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
166     {
167         .name = "default",
168         .type = AVMEDIA_TYPE_VIDEO
169     },
170     { NULL }
171 };
172
173 AVFilter ff_vf_noformat = {
174     .name          = "noformat",
175     .description   = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
176     .init          = init,
177     .query_formats = query_formats_noformat,
178     .priv_size     = sizeof(FormatContext),
179     .priv_class    = &noformat_class,
180     .inputs        = avfilter_vf_noformat_inputs,
181     .outputs       = avfilter_vf_noformat_outputs,
182 };
183 #endif /* CONFIG_NOFORMAT_FILTER */