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