]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
Merge commit '3dc06b6972cf389269e9c36ff0a4373f80f7149b'
[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 "avfilter.h"
32 #include "internal.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 typedef struct {
38     /**
39      * List of flags telling if a given image format has been listed
40      * as argument to the filter.
41      */
42     int listed_pix_fmt_flags[AV_PIX_FMT_NB];
43 } FormatContext;
44
45 #define AV_PIX_FMT_NAME_MAXSIZE 32
46
47 static av_cold int init(AVFilterContext *ctx, const char *args)
48 {
49     FormatContext *format = ctx->priv;
50     const char *cur, *sep;
51     char             pix_fmt_name[AV_PIX_FMT_NAME_MAXSIZE];
52     int              pix_fmt_name_len, ret;
53     enum AVPixelFormat pix_fmt;
54
55     /* parse the list of formats */
56     for (cur = args; cur; cur = sep ? sep+1 : NULL) {
57         if (!(sep = strchr(cur, ':')))
58             pix_fmt_name_len = strlen(cur);
59         else
60             pix_fmt_name_len = sep - cur;
61         if (pix_fmt_name_len >= AV_PIX_FMT_NAME_MAXSIZE) {
62             av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
63             return -1;
64         }
65
66         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
67         pix_fmt_name[pix_fmt_name_len] = 0;
68
69         if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
70             return ret;
71
72         format->listed_pix_fmt_flags[pix_fmt] = 1;
73     }
74
75     return 0;
76 }
77
78 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
79 {
80     AVFilterFormats *formats;
81     enum AVPixelFormat pix_fmt;
82
83     formats = av_mallocz(sizeof(AVFilterFormats));
84     formats->formats = av_malloc(sizeof(enum AVPixelFormat) * AV_PIX_FMT_NB);
85
86     for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
87         if (format->listed_pix_fmt_flags[pix_fmt] == flag)
88             formats->formats[formats->format_count++] = pix_fmt;
89
90     return formats;
91 }
92
93 #if CONFIG_FORMAT_FILTER
94 static int query_formats_format(AVFilterContext *ctx)
95 {
96     ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
97     return 0;
98 }
99
100 static const AVFilterPad avfilter_vf_format_inputs[] = {
101     {
102         .name             = "default",
103         .type             = AVMEDIA_TYPE_VIDEO,
104         .get_video_buffer = ff_null_get_video_buffer,
105         .start_frame      = ff_null_start_frame,
106         .draw_slice       = ff_null_draw_slice,
107         .end_frame        = ff_null_end_frame,
108     },
109     { NULL }
110 };
111
112 static const AVFilterPad avfilter_vf_format_outputs[] = {
113     {
114         .name = "default",
115         .type = AVMEDIA_TYPE_VIDEO
116     },
117     { NULL }
118 };
119
120 AVFilter avfilter_vf_format = {
121     .name      = "format",
122     .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
123
124     .init      = init,
125
126     .query_formats = query_formats_format,
127
128     .priv_size = sizeof(FormatContext),
129
130     .inputs    = avfilter_vf_format_inputs,
131     .outputs   = avfilter_vf_format_outputs,
132 };
133 #endif /* CONFIG_FORMAT_FILTER */
134
135 #if CONFIG_NOFORMAT_FILTER
136 static int query_formats_noformat(AVFilterContext *ctx)
137 {
138     ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
139     return 0;
140 }
141
142 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
143     {
144         .name             = "default",
145         .type             = AVMEDIA_TYPE_VIDEO,
146         .get_video_buffer = ff_null_get_video_buffer,
147         .start_frame      = ff_null_start_frame,
148         .draw_slice       = ff_null_draw_slice,
149         .end_frame        = ff_null_end_frame,
150     },
151     { NULL }
152 };
153
154 static const AVFilterPad avfilter_vf_noformat_outputs[] = {
155     {
156         .name = "default",
157         .type = AVMEDIA_TYPE_VIDEO
158     },
159     { NULL }
160 };
161
162 AVFilter avfilter_vf_noformat = {
163     .name      = "noformat",
164     .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
165
166     .init      = init,
167
168     .query_formats = query_formats_noformat,
169
170     .priv_size = sizeof(FormatContext),
171
172     .inputs    = avfilter_vf_noformat_inputs,
173     .outputs   = avfilter_vf_noformat_outputs,
174 };
175 #endif /* CONFIG_NOFORMAT_FILTER */