| 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 | |||
| 47 | AVFilterFormats *formats; ///< parsed from `pix_fmts` | ||
| 48 | AVFilterFormats *color_spaces; ///< parsed from `csps` | ||
| 49 | AVFilterFormats *color_ranges; ///< parsed from `ranges` | ||
| 50 | AVFilterFormats *alpha_modes; ///< parsed from `alphamodes` | ||
| 51 | } FormatContext; | ||
| 52 | |||
| 53 | 15900 | static av_cold void uninit(AVFilterContext *ctx) | |
| 54 | { | ||
| 55 | 15900 | FormatContext *s = ctx->priv; | |
| 56 | 15900 | ff_formats_unref(&s->formats); | |
| 57 | 15900 | ff_formats_unref(&s->color_spaces); | |
| 58 | 15900 | ff_formats_unref(&s->color_ranges); | |
| 59 | 15900 | ff_formats_unref(&s->alpha_modes); | |
| 60 | 15900 | } | |
| 61 | |||
| 62 | ✗ | static av_cold int invert_formats(AVFilterFormats **fmts, | |
| 63 | AVFilterFormats *allfmts) | ||
| 64 | { | ||
| 65 | ✗ | if (!allfmts) | |
| 66 | ✗ | return AVERROR(ENOMEM); | |
| 67 | ✗ | if (!*fmts) { | |
| 68 | /* empty fmt list means no restriction, regardless of filter type */ | ||
| 69 | ✗ | ff_formats_unref(&allfmts); | |
| 70 | ✗ | return 0; | |
| 71 | } | ||
| 72 | |||
| 73 | ✗ | for (int i = 0; i < allfmts->nb_formats; i++) { | |
| 74 | ✗ | for (int j = 0; j < (*fmts)->nb_formats; j++) { | |
| 75 | ✗ | if (allfmts->formats[i] == (*fmts)->formats[j]) { | |
| 76 | /* format is forbidden, remove it from allfmts list */ | ||
| 77 | ✗ | memmove(&allfmts->formats[i], &allfmts->formats[i+1], | |
| 78 | ✗ | (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats)); | |
| 79 | ✗ | allfmts->nb_formats--; | |
| 80 | ✗ | i--; /* repeat loop with same idx */ | |
| 81 | ✗ | break; | |
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | ✗ | ff_formats_unref(fmts); | |
| 87 | ✗ | *fmts = allfmts; | |
| 88 | ✗ | return 0; | |
| 89 | } | ||
| 90 | |||
| 91 | #define DEFINE_PARSE(name, Type, get, descr, extra_test) \ | ||
| 92 | static int parse_##name(enum Type *ret, const char *arg, void *log_ctx) \ | ||
| 93 | { \ | ||
| 94 | char *tail; \ | ||
| 95 | int val = get(arg); \ | ||
| 96 | if (val < 0) { \ | ||
| 97 | val = strtol(arg, &tail, 0); \ | ||
| 98 | if (*tail || extra_test) { \ | ||
| 99 | av_log(log_ctx, AV_LOG_ERROR, "Invalid " descr " '%s'\n", arg); \ | ||
| 100 | return AVERROR(EINVAL); \ | ||
| 101 | } \ | ||
| 102 | } \ | ||
| 103 | *ret = val; \ | ||
| 104 | return 0; \ | ||
| 105 | } | ||
| 106 | |||
| 107 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 17913 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
17913 | DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val)) |
| 108 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
20 | DEFINE_PARSE(color_space, AVColorSpace, av_color_space_from_name, "color space", 0) |
| 109 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 2242 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2242 | DEFINE_PARSE(color_range, AVColorRange, av_color_range_from_name, "color range", 0) |
| 110 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
40 | DEFINE_PARSE(alpha_mode, AVAlphaMode, av_alpha_mode_from_name, "alpha mode", 0) |
| 111 | |||
| 112 | 15900 | static av_cold int init(AVFilterContext *ctx) | |
| 113 | { | ||
| 114 | 15900 | FormatContext *s = ctx->priv; | |
| 115 | enum AVPixelFormat pix_fmt; | ||
| 116 | enum AVColorSpace csp; | ||
| 117 | enum AVColorRange crg; | ||
| 118 | enum AVAlphaMode alm; | ||
| 119 | int ret; | ||
| 120 | |||
| 121 | #define PARSE_LIST(strfield, fmtfield, tvar, parse) \ | ||
| 122 | for (char *sep, *cur = s->strfield; cur; cur = sep) { \ | ||
| 123 | sep = strchr(cur, '|'); \ | ||
| 124 | if (sep && *sep) \ | ||
| 125 | *sep++ = 0; \ | ||
| 126 | if ((ret = parse(&tvar, cur, ctx)) < 0 || \ | ||
| 127 | (ret = ff_add_format(&s->fmtfield, tvar)) < 0) \ | ||
| 128 | return ret; \ | ||
| 129 | } | ||
| 130 | |||
| 131 |
7/10✓ Branch 0 taken 2013 times.
✓ Branch 1 taken 15900 times.
✓ Branch 2 taken 2013 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 17913 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 17913 times.
✓ Branch 10 taken 17913 times.
✓ Branch 11 taken 15900 times.
|
33813 | PARSE_LIST(pix_fmts, formats, pix_fmt, parse_pixel_format) |
| 132 |
5/10✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 20 times.
✓ Branch 11 taken 15900 times.
|
15920 | PARSE_LIST(csps, color_spaces, csp, parse_color_space) |
| 133 |
7/10✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2205 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2242 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2242 times.
✓ Branch 10 taken 2242 times.
✓ Branch 11 taken 15900 times.
|
18142 | PARSE_LIST(ranges, color_ranges, crg, parse_color_range) |
| 134 |
5/10✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 5 taken 40 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 40 times.
✓ Branch 10 taken 40 times.
✓ Branch 11 taken 15900 times.
|
15940 | PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode) |
| 135 | |||
| 136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15900 times.
|
15900 | 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 | ✗ | (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0) | |
| 141 | ✗ | return ret; | |
| 142 | } | ||
| 143 | |||
| 144 | /* hold on to a ref for the lifetime of the filter */ | ||
| 145 |
2/4✓ Branch 0 taken 15900 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15900 times.
✗ Branch 4 not taken.
|
15900 | if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 || |
| 146 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 15880 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
15900 | s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 || |
| 147 |
3/4✓ Branch 0 taken 2205 times.
✓ Branch 1 taken 13695 times.
✓ Branch 3 taken 2205 times.
✗ Branch 4 not taken.
|
15900 | s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 || |
| 148 |
3/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 15860 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
|
15900 | s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0) |
| 149 | ✗ | return ret; | |
| 150 | |||
| 151 | 15900 | return 0; | |
| 152 | } | ||
| 153 | |||
| 154 | 10852 | static int query_formats(const AVFilterContext *ctx, | |
| 155 | AVFilterFormatsConfig **cfg_in, | ||
| 156 | AVFilterFormatsConfig **cfg_out) | ||
| 157 | { | ||
| 158 | 10852 | FormatContext *s = ctx->priv; | |
| 159 | int ret; | ||
| 160 | |||
| 161 |
2/4✓ Branch 0 taken 10852 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 10852 times.
✗ Branch 4 not taken.
|
10852 | if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 || |
| 162 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10832 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
10852 | s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 || |
| 163 |
3/4✓ Branch 0 taken 2205 times.
✓ Branch 1 taken 8647 times.
✓ Branch 3 taken 2205 times.
✗ Branch 4 not taken.
|
10852 | s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 || |
| 164 |
3/4✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10813 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39 times.
|
10852 | s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0) |
| 165 | ✗ | return ret; | |
| 166 | |||
| 167 | 10852 | return 0; | |
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | #define OFFSET(x) offsetof(FormatContext, x) | ||
| 172 | static const AVOption options[] = { | ||
| 173 | { "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 }, | ||
| 174 | { "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 }, | ||
| 175 | { "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 }, | ||
| 176 | { "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 }, | ||
| 177 | { NULL } | ||
| 178 | }; | ||
| 179 | |||
| 180 | AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options); | ||
| 181 | |||
| 182 | static const AVFilterPad inputs[] = { | ||
| 183 | { | ||
| 184 | .name = "default", | ||
| 185 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 186 | .get_buffer.video = ff_null_get_video_buffer, | ||
| 187 | }, | ||
| 188 | }; | ||
| 189 | |||
| 190 | #if CONFIG_FORMAT_FILTER | ||
| 191 | const FFFilter ff_vf_format = { | ||
| 192 | .p.name = "format", | ||
| 193 | .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."), | ||
| 194 | .p.priv_class = &format_class, | ||
| 195 | |||
| 196 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 197 | |||
| 198 | .init = init, | ||
| 199 | .uninit = uninit, | ||
| 200 | |||
| 201 | .priv_size = sizeof(FormatContext), | ||
| 202 | |||
| 203 | FILTER_INPUTS(inputs), | ||
| 204 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 205 | |||
| 206 | FILTER_QUERY_FUNC2(query_formats), | ||
| 207 | }; | ||
| 208 | #endif /* CONFIG_FORMAT_FILTER */ | ||
| 209 | |||
| 210 | #if CONFIG_NOFORMAT_FILTER | ||
| 211 | const FFFilter ff_vf_noformat = { | ||
| 212 | .p.name = "noformat", | ||
| 213 | .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."), | ||
| 214 | .p.priv_class = &format_class, | ||
| 215 | |||
| 216 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 217 | |||
| 218 | .init = init, | ||
| 219 | .uninit = uninit, | ||
| 220 | |||
| 221 | .priv_size = sizeof(FormatContext), | ||
| 222 | |||
| 223 | FILTER_INPUTS(inputs), | ||
| 224 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 225 | |||
| 226 | FILTER_QUERY_FUNC2(query_formats), | ||
| 227 | }; | ||
| 228 | #endif /* CONFIG_NOFORMAT_FILTER */ | ||
| 229 |