FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_format.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 41 72 56.9%
Functions: 4 5 80.0%
Branches: 39 78 50.0%

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
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 13741 static av_cold void uninit(AVFilterContext *ctx)
52 {
53 13741 FormatContext *s = ctx->priv;
54 13741 ff_formats_unref(&s->formats);
55 13741 ff_formats_unref(&s->color_spaces);
56 13741 ff_formats_unref(&s->color_ranges);
57 13741 }
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 15551 static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
89 {
90 char *tail;
91 15551 int pix_fmt = av_get_pix_fmt(arg);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15551 times.
15551 if (pix_fmt == AV_PIX_FMT_NONE) {
93 pix_fmt = strtol(arg, &tail, 0);
94 if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
95 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
96 return AVERROR(EINVAL);
97 }
98 }
99 15551 *ret = pix_fmt;
100 15551 return 0;
101 }
102
103 13741 static av_cold int init(AVFilterContext *ctx)
104 {
105 13741 FormatContext *s = ctx->priv;
106 enum AVPixelFormat pix_fmt;
107 int ret;
108
109
2/2
✓ Branch 0 taken 15551 times.
✓ Branch 1 taken 13741 times.
29292 for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
110 15551 sep = strchr(cur, '|');
111
3/4
✓ Branch 0 taken 1811 times.
✓ Branch 1 taken 13740 times.
✓ Branch 2 taken 1811 times.
✗ Branch 3 not taken.
15551 if (sep && *sep)
112 1811 *sep++ = 0;
113
2/4
✓ Branch 1 taken 15551 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 15551 times.
31102 if ((ret = parse_pixel_format(&pix_fmt, cur, ctx)) < 0 ||
114 15551 (ret = ff_add_format(&s->formats, pix_fmt)) < 0)
115 return ret;
116 }
117
118
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 13741 times.
13761 for (char *sep, *cur = s->csps; cur; cur = sep) {
119 20 sep = strchr(cur, '|');
120
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 if (sep && *sep)
121 *sep++ = 0;
122
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
40 if ((ret = av_color_space_from_name(cur)) < 0 ||
123 20 (ret = ff_add_format(&s->color_spaces, ret)) < 0)
124 return ret;
125 }
126
127
2/2
✓ Branch 0 taken 1870 times.
✓ Branch 1 taken 13741 times.
15611 for (char *sep, *cur = s->ranges; cur; cur = sep) {
128 1870 sep = strchr(cur, '|');
129
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1833 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
1870 if (sep && *sep)
130 37 *sep++ = 0;
131
2/4
✓ Branch 1 taken 1870 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1870 times.
3740 if ((ret = av_color_range_from_name(cur)) < 0 ||
132 1870 (ret = ff_add_format(&s->color_ranges, ret)) < 0)
133 return ret;
134 }
135
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13741 times.
13741 if (!strcmp(ctx->filter->name, "noformat")) {
137 if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
138 (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
139 (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0)
140 return ret;
141 }
142
143 /* hold on to a ref for the lifetime of the filter */
144
3/4
✓ Branch 0 taken 13740 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 13740 times.
✗ Branch 4 not taken.
13741 if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
145
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 13721 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
13741 s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
146
3/4
✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 11908 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1833 times.
13741 s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0)
147 return ret;
148
149 13741 return 0;
150 }
151
152 9504 static int query_formats(const AVFilterContext *ctx,
153 AVFilterFormatsConfig **cfg_in,
154 AVFilterFormatsConfig **cfg_out)
155 {
156 9504 FormatContext *s = ctx->priv;
157 int ret;
158
159
3/4
✓ Branch 0 taken 9503 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 9503 times.
✗ Branch 4 not taken.
9504 if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 ||
160
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9484 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
9504 s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 ||
161
3/4
✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 7671 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1833 times.
9504 s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0)
162 return ret;
163
164 9504 return 0;
165 }
166
167
168 #define OFFSET(x) offsetof(FormatContext, x)
169 static const AVOption options[] = {
170 { "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 },
171 { "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 },
172 { "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 },
173 { NULL }
174 };
175
176 AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options);
177
178 static const AVFilterPad inputs[] = {
179 {
180 .name = "default",
181 .type = AVMEDIA_TYPE_VIDEO,
182 .get_buffer.video = ff_null_get_video_buffer,
183 },
184 };
185
186 #if CONFIG_FORMAT_FILTER
187 const AVFilter ff_vf_format = {
188 .name = "format",
189 .description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
190
191 .init = init,
192 .uninit = uninit,
193
194 .priv_size = sizeof(FormatContext),
195 .priv_class = &format_class,
196
197 .flags = AVFILTER_FLAG_METADATA_ONLY,
198
199 FILTER_INPUTS(inputs),
200 FILTER_OUTPUTS(ff_video_default_filterpad),
201
202 FILTER_QUERY_FUNC2(query_formats),
203 };
204 #endif /* CONFIG_FORMAT_FILTER */
205
206 #if CONFIG_NOFORMAT_FILTER
207 const AVFilter ff_vf_noformat = {
208 .name = "noformat",
209 .description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
210 .priv_class = &format_class,
211
212 .init = init,
213 .uninit = uninit,
214
215 .priv_size = sizeof(FormatContext),
216
217 .flags = AVFILTER_FLAG_METADATA_ONLY,
218
219 FILTER_INPUTS(inputs),
220 FILTER_OUTPUTS(ff_video_default_filterpad),
221
222 FILTER_QUERY_FUNC2(query_formats),
223 };
224 #endif /* CONFIG_NOFORMAT_FILTER */
225