]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
lavfi: make AVFilterPad opaque after two major bumps.
[ffmpeg] / libavfilter / vf_format.c
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
21 /**
22  * @file
23  * format and noformat video filters
24  */
25
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct {
33     /**
34      * List of flags telling if a given image format has been listed
35      * as argument to the filter.
36      */
37     int listed_pix_fmt_flags[PIX_FMT_NB];
38 } FormatContext;
39
40 #define PIX_FMT_NAME_MAXSIZE 32
41
42 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
43 {
44     FormatContext *format = ctx->priv;
45     const char *cur, *sep;
46     char             pix_fmt_name[PIX_FMT_NAME_MAXSIZE];
47     int              pix_fmt_name_len;
48     enum PixelFormat pix_fmt;
49
50     /* parse the list of formats */
51     for (cur = args; cur; cur = sep ? sep+1 : NULL) {
52         if (!(sep = strchr(cur, ':')))
53             pix_fmt_name_len = strlen(cur);
54         else
55             pix_fmt_name_len = sep - cur;
56         if (pix_fmt_name_len >= PIX_FMT_NAME_MAXSIZE) {
57             av_log(ctx, AV_LOG_ERROR, "Format name too long\n");
58             return -1;
59         }
60
61         memcpy(pix_fmt_name, cur, pix_fmt_name_len);
62         pix_fmt_name[pix_fmt_name_len] = 0;
63         pix_fmt = av_get_pix_fmt(pix_fmt_name);
64
65         if (pix_fmt == PIX_FMT_NONE) {
66             av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: %s\n", pix_fmt_name);
67             return -1;
68         }
69
70         format->listed_pix_fmt_flags[pix_fmt] = 1;
71     }
72
73     return 0;
74 }
75
76 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
77 {
78     AVFilterFormats *formats;
79     enum PixelFormat pix_fmt;
80
81     formats = av_mallocz(sizeof(AVFilterFormats));
82     formats->formats = av_malloc(sizeof(enum PixelFormat) * PIX_FMT_NB);
83
84     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
85         if (format->listed_pix_fmt_flags[pix_fmt] == flag)
86             formats->formats[formats->format_count++] = pix_fmt;
87
88     return formats;
89 }
90
91 #if CONFIG_FORMAT_FILTER
92 static int query_formats_format(AVFilterContext *ctx)
93 {
94     ff_set_common_formats(ctx, make_format_list(ctx->priv, 1));
95     return 0;
96 }
97
98 AVFilter avfilter_vf_format = {
99     .name      = "format",
100     .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
101
102     .init      = init,
103
104     .query_formats = query_formats_format,
105
106     .priv_size = sizeof(FormatContext),
107
108     .inputs    = (AVFilterPad[]) {{ .name            = "default",
109                                     .type            = AVMEDIA_TYPE_VIDEO,
110                                     .get_video_buffer= ff_null_get_video_buffer,
111                                     .start_frame     = ff_null_start_frame,
112                                     .draw_slice      = ff_null_draw_slice,
113                                     .end_frame       = ff_null_end_frame, },
114                                   { .name = NULL}},
115     .outputs   = (AVFilterPad[]) {{ .name            = "default",
116                                     .type            = AVMEDIA_TYPE_VIDEO },
117                                   { .name = NULL}},
118 };
119 #endif /* CONFIG_FORMAT_FILTER */
120
121 #if CONFIG_NOFORMAT_FILTER
122 static int query_formats_noformat(AVFilterContext *ctx)
123 {
124     ff_set_common_formats(ctx, make_format_list(ctx->priv, 0));
125     return 0;
126 }
127
128 AVFilter avfilter_vf_noformat = {
129     .name      = "noformat",
130     .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
131
132     .init      = init,
133
134     .query_formats = query_formats_noformat,
135
136     .priv_size = sizeof(FormatContext),
137
138     .inputs    = (AVFilterPad[]) {{ .name            = "default",
139                                     .type            = AVMEDIA_TYPE_VIDEO,
140                                     .get_video_buffer= ff_null_get_video_buffer,
141                                     .start_frame     = ff_null_start_frame,
142                                     .draw_slice      = ff_null_draw_slice,
143                                     .end_frame       = ff_null_end_frame, },
144                                   { .name = NULL}},
145     .outputs   = (AVFilterPad[]) {{ .name            = "default",
146                                     .type            = AVMEDIA_TYPE_VIDEO },
147                                   { .name = NULL}},
148 };
149 #endif /* CONFIG_NOFORMAT_FILTER */