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