]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_format.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "internal.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, ret;
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
63         if ((ret = ff_parse_pixel_format(&pix_fmt, pix_fmt_name, ctx)) < 0)
64             return ret;
65
66         format->listed_pix_fmt_flags[pix_fmt] = 1;
67     }
68
69     return 0;
70 }
71
72 static AVFilterFormats *make_format_list(FormatContext *format, int flag)
73 {
74     AVFilterFormats *formats;
75     enum PixelFormat pix_fmt;
76
77     formats = av_mallocz(sizeof(AVFilterFormats));
78     formats->formats = av_malloc(sizeof(enum PixelFormat) * PIX_FMT_NB);
79
80     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
81         if (format->listed_pix_fmt_flags[pix_fmt] == flag)
82             formats->formats[formats->format_count++] = pix_fmt;
83
84     return formats;
85 }
86
87 #if CONFIG_FORMAT_FILTER
88 static int query_formats_format(AVFilterContext *ctx)
89 {
90     avfilter_set_common_pixel_formats(ctx, make_format_list(ctx->priv, 1));
91     return 0;
92 }
93
94 AVFilter avfilter_vf_format = {
95     .name      = "format",
96     .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
97
98     .init      = init,
99
100     .query_formats = query_formats_format,
101
102     .priv_size = sizeof(FormatContext),
103
104     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
105                                     .type            = AVMEDIA_TYPE_VIDEO,
106                                     .get_video_buffer= ff_null_get_video_buffer,
107                                     .start_frame     = ff_null_start_frame,
108                                     .draw_slice      = ff_null_draw_slice,
109                                     .end_frame       = ff_null_end_frame, },
110                                   { .name = NULL}},
111     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
112                                     .type            = AVMEDIA_TYPE_VIDEO },
113                                   { .name = NULL}},
114 };
115 #endif /* CONFIG_FORMAT_FILTER */
116
117 #if CONFIG_NOFORMAT_FILTER
118 static int query_formats_noformat(AVFilterContext *ctx)
119 {
120     avfilter_set_common_pixel_formats(ctx, make_format_list(ctx->priv, 0));
121     return 0;
122 }
123
124 AVFilter avfilter_vf_noformat = {
125     .name      = "noformat",
126     .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
127
128     .init      = init,
129
130     .query_formats = query_formats_noformat,
131
132     .priv_size = sizeof(FormatContext),
133
134     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
135                                     .type            = AVMEDIA_TYPE_VIDEO,
136                                     .get_video_buffer= ff_null_get_video_buffer,
137                                     .start_frame     = ff_null_start_frame,
138                                     .draw_slice      = ff_null_draw_slice,
139                                     .end_frame       = ff_null_end_frame, },
140                                   { .name = NULL}},
141     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
142                                     .type            = AVMEDIA_TYPE_VIDEO },
143                                   { .name = NULL}},
144 };
145 #endif /* CONFIG_NOFORMAT_FILTER */