../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
58 / 63
92.1%
Branches:
82 / 138
59.4%
Functions:
9 / 9
100.0%

libavfilter/vf_format.c
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 "filters.h"
37 #include "formats.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 char *alphamodes;
46 char *chromalocations;
47
48 AVFilterFormats *formats; ///< parsed from `pix_fmts`
49 AVFilterFormats *color_spaces; ///< parsed from `csps`
50 AVFilterFormats *color_ranges; ///< parsed from `ranges`
51 AVFilterFormats *alpha_modes; ///< parsed from `alphamodes`
52 AVFilterFormats *chroma_locations; ///< parsed from `chromalocations`
53 } FormatContext;
54
55 16598 static av_cold void uninit(AVFilterContext *ctx)
56 {
57 16598 FormatContext *s = ctx->priv;
58 16598 ff_formats_unref(&s->formats);
59 16598 ff_formats_unref(&s->color_spaces);
60 16598 ff_formats_unref(&s->color_ranges);
61 16598 ff_formats_unref(&s->alpha_modes);
62 16598 ff_formats_unref(&s->chroma_locations);
63 16598 }
64
65 10 static av_cold int invert_formats(AVFilterFormats **fmts,
66 AVFilterFormats *allfmts)
67 {
68
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 10 times.
10 if (!allfmts)
69 ✗ return AVERROR(ENOMEM);
70
2/2
✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 7 taken 2 times.
10 if (!*fmts) {
71 /* empty fmt list means no restriction, regardless of filter type */
72 8 ff_formats_unref(&allfmts);
73 8 return 0;
74 }
75
76
2/2
✓ Branch 14 → 8 taken 14 times.
✓ Branch 14 → 15 taken 2 times.
16 for (int i = 0; i < allfmts->nb_formats; i++) {
77
2/2
✓ Branch 12 → 9 taken 26 times.
✓ Branch 12 → 13 taken 10 times.
36 for (int j = 0; j < (*fmts)->nb_formats; j++) {
78
2/2
✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 11 taken 22 times.
26 if (allfmts->formats[i] == (*fmts)->formats[j]) {
79 /* format is forbidden, remove it from allfmts list */
80 4 memmove(&allfmts->formats[i], &allfmts->formats[i+1],
81 4 (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats));
82 4 allfmts->nb_formats--;
83 4 i--; /* repeat loop with same idx */
84 4 break;
85 }
86 }
87 }
88
89 2 ff_formats_unref(fmts);
90 2 *fmts = allfmts;
91 2 return 0;
92 }
93
94 #define DEFINE_PARSE(name, Type, get, descr, extra_test) \
95 static int parse_##name(enum Type *ret, const char *arg, void *log_ctx) \
96 { \
97 char *tail; \
98 int val = get(arg); \
99 if (val < 0) { \
100 val = strtol(arg, &tail, 0); \
101 if (*tail || extra_test) { \
102 av_log(log_ctx, AV_LOG_ERROR, "Invalid " descr " '%s'\n", arg); \
103 return AVERROR(EINVAL); \
104 } \
105 } \
106 *ret = val; \
107 return 0; \
108 }
109
110
1/6
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 10 taken 18816 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
18816 DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val))
111
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 8 taken 20 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
20 DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color space", 0)
112
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 8 taken 2533 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
2533 DEFINE_PARSE(color_range, AVColorRange, av_color_range_from_name, "color range", 0)
113
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 8 taken 40 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
40 DEFINE_PARSE(alpha_mode, AVAlphaMode, av_alpha_mode_from_name, "alpha mode", 0)
114
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 8 taken 736 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
736 DEFINE_PARSE(chroma_location, AVChromaLocation, av_chroma_location_from_name, "chroma location", 0)
115
116 16598 static av_cold int init(AVFilterContext *ctx)
117 {
118 16598 FormatContext *s = ctx->priv;
119 enum AVPixelFormat pix_fmt;
120 enum AVColorSpace csp;
121 enum AVColorRange crg;
122 enum AVAlphaMode alm;
123 enum AVChromaLocation cloc;
124 int ret;
125
126 #define PARSE_LIST(strfield, fmtfield, tvar, parse) \
127 for (char *sep, *cur = s->strfield; cur; cur = sep) { \
128 sep = strchr(cur, '|'); \
129 if (sep && *sep) \
130 *sep++ = 0; \
131 if ((ret = parse(&tvar, cur, ctx)) < 0 || \
132 (ret = ff_add_format(&s->fmtfield, tvar)) < 0) \
133 return ret; \
134 }
135
136
7/10
✓ Branch 3 → 4 taken 2226 times.
✓ Branch 3 → 6 taken 16590 times.
✓ Branch 4 → 5 taken 2226 times.
✗ Branch 4 → 6 not taken.
✓ Branch 7 → 8 taken 18816 times.
✗ Branch 7 → 10 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 18816 times.
✓ Branch 12 → 3 taken 18816 times.
✓ Branch 12 → 13 taken 16598 times.
35414 PARSE_LIST(pix_fmts, formats, pix_fmt, parse_pixel_format)
137
5/10
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 20 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✓ Branch 18 → 19 taken 20 times.
✗ Branch 18 → 21 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 20 times.
✓ Branch 23 → 14 taken 20 times.
✓ Branch 23 → 24 taken 16598 times.
16618 PARSE_LIST(csps, color_spaces, csp, parse_color_space)
138
7/10
✓ Branch 25 → 26 taken 37 times.
✓ Branch 25 → 28 taken 2496 times.
✓ Branch 26 → 27 taken 37 times.
✗ Branch 26 → 28 not taken.
✓ Branch 29 → 30 taken 2533 times.
✗ Branch 29 → 32 not taken.
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 2533 times.
✓ Branch 34 → 25 taken 2533 times.
✓ Branch 34 → 35 taken 16598 times.
19131 PARSE_LIST(ranges, color_ranges, crg, parse_color_range)
139
5/10
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 39 taken 40 times.
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
✓ Branch 40 → 41 taken 40 times.
✗ Branch 40 → 43 not taken.
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 40 times.
✓ Branch 45 → 36 taken 40 times.
✓ Branch 45 → 46 taken 16598 times.
16638 PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode)
140
5/10
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 736 times.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✓ Branch 51 → 52 taken 736 times.
✗ Branch 51 → 54 not taken.
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 736 times.
✓ Branch 56 → 47 taken 736 times.
✓ Branch 56 → 57 taken 16598 times.
17334 PARSE_LIST(chromalocations, chroma_locations, cloc, parse_chroma_location)
141
142
2/2
✓ Branch 57 → 58 taken 2 times.
✓ Branch 57 → 78 taken 16596 times.
16598 if (!strcmp(ctx->filter->name, "noformat")) {
143
2/4
✓ Branch 58 → 59 taken 2 times.
✗ Branch 58 → 62 not taken.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 62 taken 2 times.
4 if (s->chroma_locations &&
144 2 (ret = ff_add_format(&s->chroma_locations, AVCHROMA_LOC_UNSPECIFIED)) < 0)
145 ✗ return ret;
146
2/4
✓ Branch 64 → 65 taken 2 times.
✗ Branch 64 → 77 not taken.
✓ Branch 67 → 68 taken 2 times.
✗ Branch 67 → 77 not taken.
4 if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
147
1/2
✓ Branch 70 → 71 taken 2 times.
✗ Branch 70 → 77 not taken.
4 (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
148
1/2
✓ Branch 73 → 74 taken 2 times.
✗ Branch 73 → 77 not taken.
4 (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 ||
149
1/2
✗ Branch 76 → 77 not taken.
✓ Branch 76 → 78 taken 2 times.
4 (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0 ||
150 2 (ret = invert_formats(&s->chroma_locations, ff_all_chroma_locations())) < 0)
151 ✗ return ret;
152 }
153
154 /* hold on to a ref for the lifetime of the filter */
155
3/4
✓ Branch 78 → 79 taken 16590 times.
✓ Branch 78 → 81 taken 8 times.
✓ Branch 80 → 81 taken 16590 times.
✗ Branch 80 → 93 not taken.
16598 if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
156
3/4
✓ Branch 81 → 82 taken 20 times.
✓ Branch 81 → 84 taken 16578 times.
✓ Branch 83 → 84 taken 20 times.
✗ Branch 83 → 93 not taken.
16598 s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
157
3/4
✓ Branch 84 → 85 taken 2496 times.
✓ Branch 84 → 87 taken 14102 times.
✓ Branch 86 → 87 taken 2496 times.
✗ Branch 86 → 93 not taken.
16598 s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 ||
158
3/4
✓ Branch 87 → 88 taken 40 times.
✓ Branch 87 → 90 taken 16558 times.
✓ Branch 89 → 90 taken 40 times.
✗ Branch 89 → 93 not taken.
16598 s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0 ||
159
3/4
✓ Branch 90 → 91 taken 736 times.
✓ Branch 90 → 94 taken 15862 times.
✗ Branch 92 → 93 not taken.
✓ Branch 92 → 94 taken 736 times.
16598 s->chroma_locations && (ret = ff_formats_ref(s->chroma_locations, &s->chroma_locations)) < 0)
160 ✗ return ret;
161
162 16598 return 0;
163 }
164
165 11399 static int query_formats(const AVFilterContext *ctx,
166 AVFilterFormatsConfig **cfg_in,
167 AVFilterFormatsConfig **cfg_out)
168 {
169 11399 FormatContext *s = ctx->priv;
170 int ret;
171
172
3/4
✓ Branch 2 → 3 taken 11395 times.
✓ Branch 2 → 5 taken 4 times.
✓ Branch 4 → 5 taken 11395 times.
✗ Branch 4 → 17 not taken.
11399 if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
173
3/4
✓ Branch 5 → 6 taken 20 times.
✓ Branch 5 → 8 taken 11379 times.
✓ Branch 7 → 8 taken 20 times.
✗ Branch 7 → 17 not taken.
11399 s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
174
3/4
✓ Branch 8 → 9 taken 2496 times.
✓ Branch 8 → 11 taken 8903 times.
✓ Branch 10 → 11 taken 2496 times.
✗ Branch 10 → 17 not taken.
11399 s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 ||
175
3/4
✓ Branch 11 → 12 taken 39 times.
✓ Branch 11 → 14 taken 11360 times.
✓ Branch 13 → 14 taken 39 times.
✗ Branch 13 → 17 not taken.
11399 s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0 ||
176
3/4
✓ Branch 14 → 15 taken 730 times.
✓ Branch 14 → 18 taken 10669 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 730 times.
11399 s->chroma_locations && (ret = ff_set_common_chroma_locations2(ctx, cfg_in, cfg_out, s->chroma_locations)) < 0)
177 ✗ return ret;
178
179 11399 return 0;
180 }
181
182
183 #define OFFSET(x) offsetof(FormatContext, x)
184 static const AVOption options[] = {
185 { "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 },
186 { "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 },
187 { "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 },
188 { "alpha_modes", "A '|'-separated list of alpha modes", OFFSET(alphamodes), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
189 { "chroma_locations", "A '|'-separated list of chroma sample locations", OFFSET(chromalocations), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
190 { NULL }
191 };
192
193 AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options);
194
195 static const AVFilterPad inputs[] = {
196 {
197 .name = "default",
198 .type = AVMEDIA_TYPE_VIDEO,
199 .get_buffer.video = ff_null_get_video_buffer,
200 },
201 };
202
203 #if CONFIG_FORMAT_FILTER
204 const FFFilter ff_vf_format = {
205 .p.name = "format",
206 .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
207 .p.priv_class = &format_class,
208
209 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
210
211 .init = init,
212 .uninit = uninit,
213
214 .priv_size = sizeof(FormatContext),
215
216 FILTER_INPUTS(inputs),
217 FILTER_OUTPUTS(ff_video_default_filterpad),
218
219 FILTER_QUERY_FUNC2(query_formats),
220 .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS,
221 };
222 #endif /* CONFIG_FORMAT_FILTER */
223
224 #if CONFIG_NOFORMAT_FILTER
225 const FFFilter ff_vf_noformat = {
226 .p.name = "noformat",
227 .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
228 .p.priv_class = &format_class,
229
230 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
231
232 .init = init,
233 .uninit = uninit,
234
235 .priv_size = sizeof(FormatContext),
236
237 FILTER_INPUTS(inputs),
238 FILTER_OUTPUTS(ff_video_default_filterpad),
239
240 FILTER_QUERY_FUNC2(query_formats),
241 .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS,
242 };
243 #endif /* CONFIG_NOFORMAT_FILTER */
244