Line | Branch | Exec | Source |
---|---|---|---|
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 "config_components.h" | ||
27 | |||
28 | #include <string.h> | ||
29 | |||
30 | #include "libavutil/internal.h" | ||
31 | #include "libavutil/mem.h" | ||
32 | #include "libavutil/pixdesc.h" | ||
33 | #include "libavutil/opt.h" | ||
34 | |||
35 | #include "avfilter.h" | ||
36 | #include "formats.h" | ||
37 | #include "internal.h" | ||
38 | #include "video.h" | ||
39 | |||
40 | typedef struct FormatContext { | ||
41 | const AVClass *class; | ||
42 | char *pix_fmts; | ||
43 | char *csps; | ||
44 | char *ranges; | ||
45 | |||
46 | AVFilterFormats *formats; ///< parsed from `pix_fmts` | ||
47 | AVFilterFormats *color_spaces; ///< parsed from `csps` | ||
48 | AVFilterFormats *color_ranges; ///< parsed from `ranges` | ||
49 | } FormatContext; | ||
50 | |||
51 | 8937 | static av_cold void uninit(AVFilterContext *ctx) | |
52 | { | ||
53 | 8937 | FormatContext *s = ctx->priv; | |
54 | 8937 | ff_formats_unref(&s->formats); | |
55 | 8937 | ff_formats_unref(&s->color_spaces); | |
56 | 8937 | ff_formats_unref(&s->color_ranges); | |
57 | 8937 | } | |
58 | |||
59 | ✗ | static av_cold int invert_formats(AVFilterFormats **fmts, | |
60 | AVFilterFormats *allfmts) | ||
61 | { | ||
62 | ✗ | if (!allfmts) | |
63 | ✗ | return AVERROR(ENOMEM); | |
64 | ✗ | if (!*fmts) { | |
65 | /* empty fmt list means no restriction, regardless of filter type */ | ||
66 | ✗ | ff_formats_unref(&allfmts); | |
67 | ✗ | return 0; | |
68 | } | ||
69 | |||
70 | ✗ | for (int i = 0; i < allfmts->nb_formats; i++) { | |
71 | ✗ | for (int j = 0; j < (*fmts)->nb_formats; j++) { | |
72 | ✗ | if (allfmts->formats[i] == (*fmts)->formats[j]) { | |
73 | /* format is forbidden, remove it from allfmts list */ | ||
74 | ✗ | memmove(&allfmts->formats[i], &allfmts->formats[i+1], | |
75 | ✗ | (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats)); | |
76 | ✗ | allfmts->nb_formats--; | |
77 | ✗ | i--; /* repeat loop with same idx */ | |
78 | ✗ | break; | |
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | ✗ | ff_formats_unref(fmts); | |
84 | ✗ | *fmts = allfmts; | |
85 | ✗ | return 0; | |
86 | } | ||
87 | |||
88 | 8937 | static av_cold int init(AVFilterContext *ctx) | |
89 | { | ||
90 | 8937 | FormatContext *s = ctx->priv; | |
91 | enum AVPixelFormat pix_fmt; | ||
92 | int ret; | ||
93 | |||
94 |
2/2✓ Branch 0 taken 10518 times.
✓ Branch 1 taken 8937 times.
|
19455 | for (char *sep, *cur = s->pix_fmts; cur; cur = sep) { |
95 | 10518 | sep = strchr(cur, '|'); | |
96 |
3/4✓ Branch 0 taken 1581 times.
✓ Branch 1 taken 8937 times.
✓ Branch 2 taken 1581 times.
✗ Branch 3 not taken.
|
10518 | if (sep && *sep) |
97 | 1581 | *sep++ = 0; | |
98 |
2/4✓ Branch 1 taken 10518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10518 times.
|
21036 | if ((ret = ff_parse_pixel_format(&pix_fmt, cur, ctx)) < 0 || |
99 | 10518 | (ret = ff_add_format(&s->formats, pix_fmt)) < 0) | |
100 | ✗ | return ret; | |
101 | } | ||
102 | |||
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8937 times.
|
8937 | for (char *sep, *cur = s->csps; cur; cur = sep) { |
104 | ✗ | sep = strchr(cur, '|'); | |
105 | ✗ | if (sep && *sep) | |
106 | ✗ | *sep++ = 0; | |
107 | ✗ | if ((ret = av_color_space_from_name(cur)) < 0 || | |
108 | ✗ | (ret = ff_add_format(&s->color_spaces, ret)) < 0) | |
109 | ✗ | return ret; | |
110 | } | ||
111 | |||
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8937 times.
|
8937 | for (char *sep, *cur = s->ranges; cur; cur = sep) { |
113 | ✗ | sep = strchr(cur, '|'); | |
114 | ✗ | if (sep && *sep) | |
115 | ✗ | *sep++ = 0; | |
116 | ✗ | if ((ret = av_color_range_from_name(cur)) < 0 || | |
117 | ✗ | (ret = ff_add_format(&s->color_ranges, ret)) < 0) | |
118 | ✗ | return ret; | |
119 | } | ||
120 | |||
121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8937 times.
|
8937 | if (!strcmp(ctx->filter->name, "noformat")) { |
122 | ✗ | if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 || | |
123 | ✗ | (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 || | |
124 | ✗ | (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0) | |
125 | ✗ | return ret; | |
126 | } | ||
127 | |||
128 | /* hold on to a ref for the lifetime of the filter */ | ||
129 |
2/4✓ Branch 0 taken 8937 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8937 times.
✗ Branch 4 not taken.
|
8937 | if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 || |
130 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8937 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
8937 | s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 || |
131 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8937 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
8937 | s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0) |
132 | ✗ | return ret; | |
133 | |||
134 | 8937 | return 0; | |
135 | } | ||
136 | |||
137 | 6536 | static int query_formats(AVFilterContext *ctx) | |
138 | { | ||
139 | 6536 | FormatContext *s = ctx->priv; | |
140 | int ret; | ||
141 | |||
142 |
2/4✓ Branch 0 taken 6536 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6536 times.
✗ Branch 4 not taken.
|
6536 | if (s->formats && (ret = ff_set_common_formats(ctx, s->formats)) < 0 || |
143 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6536 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6536 | s->color_spaces && (ret = ff_set_common_color_spaces(ctx, s->color_spaces)) < 0 || |
144 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6536 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6536 | s->color_ranges && (ret = ff_set_common_color_ranges(ctx, s->color_ranges)) < 0) |
145 | ✗ | return ret; | |
146 | |||
147 | 6536 | return 0; | |
148 | } | ||
149 | |||
150 | |||
151 | #define OFFSET(x) offsetof(FormatContext, x) | ||
152 | static const AVOption options[] = { | ||
153 | { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM }, | ||
154 | { "color_spaces", "A '|'-separated list of color spaces", OFFSET(csps), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM }, | ||
155 | { "color_ranges", "A '|'-separated list of color ranges", OFFSET(ranges), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM }, | ||
156 | { NULL } | ||
157 | }; | ||
158 | |||
159 | AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options); | ||
160 | |||
161 | static const AVFilterPad inputs[] = { | ||
162 | { | ||
163 | .name = "default", | ||
164 | .type = AVMEDIA_TYPE_VIDEO, | ||
165 | .get_buffer.video = ff_null_get_video_buffer, | ||
166 | }, | ||
167 | }; | ||
168 | |||
169 | #if CONFIG_FORMAT_FILTER | ||
170 | const AVFilter ff_vf_format = { | ||
171 | .name = "format", | ||
172 | .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."), | ||
173 | |||
174 | .init = init, | ||
175 | .uninit = uninit, | ||
176 | |||
177 | .priv_size = sizeof(FormatContext), | ||
178 | .priv_class = &format_class, | ||
179 | |||
180 | .flags = AVFILTER_FLAG_METADATA_ONLY, | ||
181 | |||
182 | FILTER_INPUTS(inputs), | ||
183 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
184 | |||
185 | FILTER_QUERY_FUNC(query_formats), | ||
186 | }; | ||
187 | #endif /* CONFIG_FORMAT_FILTER */ | ||
188 | |||
189 | #if CONFIG_NOFORMAT_FILTER | ||
190 | const AVFilter ff_vf_noformat = { | ||
191 | .name = "noformat", | ||
192 | .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."), | ||
193 | .priv_class = &format_class, | ||
194 | |||
195 | .init = init, | ||
196 | .uninit = uninit, | ||
197 | |||
198 | .priv_size = sizeof(FormatContext), | ||
199 | |||
200 | .flags = AVFILTER_FLAG_METADATA_ONLY, | ||
201 | |||
202 | FILTER_INPUTS(inputs), | ||
203 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
204 | |||
205 | FILTER_QUERY_FUNC(query_formats), | ||
206 | }; | ||
207 | #endif /* CONFIG_NOFORMAT_FILTER */ | ||
208 |