| 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 | 16000 | static av_cold void uninit(AVFilterContext *ctx) | |
| 56 | { | ||
| 57 | 16000 | FormatContext *s = ctx->priv; | |
| 58 | 16000 | ff_formats_unref(&s->formats); | |
| 59 | 16000 | ff_formats_unref(&s->color_spaces); | |
| 60 | 16000 | ff_formats_unref(&s->color_ranges); | |
| 61 | 16000 | ff_formats_unref(&s->alpha_modes); | |
| 62 | 16000 | ff_formats_unref(&s->chroma_locations); | |
| 63 | 16000 | } | |
| 64 | |||
| 65 | 10 | static av_cold int invert_formats(AVFilterFormats **fmts, | |
| 66 | AVFilterFormats *allfmts) | ||
| 67 | { | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!allfmts) |
| 69 | ✗ | return AVERROR(ENOMEM); | |
| 70 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 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 0 taken 14 times.
✓ Branch 1 taken 2 times.
|
16 | for (int i = 0; i < allfmts->nb_formats; i++) { |
| 77 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 10 times.
|
36 | for (int j = 0; j < (*fmts)->nb_formats; j++) { |
| 78 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 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 1 not taken.
✓ Branch 2 taken 18022 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
18022 | DEFINE_PARSE(pixel_format, AVPixelFormat, av_get_pix_fmt, "pixel format", !av_pix_fmt_desc_get(val)) |
| 111 |
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) |
| 112 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 2255 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2255 | DEFINE_PARSE(color_range, AVColorRange, av_color_range_from_name, "color range", 0) |
| 113 |
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) |
| 114 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 725 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
725 | DEFINE_PARSE(chroma_location, AVChromaLocation, av_chroma_location_from_name, "chroma location", 0) |
| 115 | |||
| 116 | 16000 | static av_cold int init(AVFilterContext *ctx) | |
| 117 | { | ||
| 118 | 16000 | 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 0 taken 2030 times.
✓ Branch 1 taken 15992 times.
✓ Branch 2 taken 2030 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18022 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 18022 times.
✓ Branch 10 taken 18022 times.
✓ Branch 11 taken 16000 times.
|
34022 | PARSE_LIST(pix_fmts, formats, pix_fmt, parse_pixel_format) |
| 137 |
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 16000 times.
|
16020 | PARSE_LIST(csps, color_spaces, csp, parse_color_space) |
| 138 |
7/10✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2218 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2255 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2255 times.
✓ Branch 10 taken 2255 times.
✓ Branch 11 taken 16000 times.
|
18255 | PARSE_LIST(ranges, color_ranges, crg, parse_color_range) |
| 139 |
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 16000 times.
|
16040 | PARSE_LIST(alphamodes, alpha_modes, alm, parse_alpha_mode) |
| 140 |
5/10✗ Branch 0 not taken.
✓ Branch 1 taken 725 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 5 taken 725 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 725 times.
✓ Branch 10 taken 725 times.
✓ Branch 11 taken 16000 times.
|
16725 | PARSE_LIST(chromalocations, chroma_locations, cloc, parse_chroma_location) |
| 141 | |||
| 142 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15998 times.
|
16000 | if (!strcmp(ctx->filter->name, "noformat")) { |
| 143 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 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 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 || |
| 147 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 || |
| 148 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 || |
| 149 |
1/2✗ Branch 2 not taken.
✓ Branch 3 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 0 taken 15992 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 15992 times.
✗ Branch 4 not taken.
|
16000 | if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 || |
| 156 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 15980 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
16000 | s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 || |
| 157 |
3/4✓ Branch 0 taken 2218 times.
✓ Branch 1 taken 13782 times.
✓ Branch 3 taken 2218 times.
✗ Branch 4 not taken.
|
16000 | s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 || |
| 158 |
3/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 15960 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
|
16000 | s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0 || |
| 159 |
3/4✓ Branch 0 taken 725 times.
✓ Branch 1 taken 15275 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 725 times.
|
16000 | s->chroma_locations && (ret = ff_formats_ref(s->chroma_locations, &s->chroma_locations)) < 0) |
| 160 | ✗ | return ret; | |
| 161 | |||
| 162 | 16000 | return 0; | |
| 163 | } | ||
| 164 | |||
| 165 | 10912 | static int query_formats(const AVFilterContext *ctx, | |
| 166 | AVFilterFormatsConfig **cfg_in, | ||
| 167 | AVFilterFormatsConfig **cfg_out) | ||
| 168 | { | ||
| 169 | 10912 | FormatContext *s = ctx->priv; | |
| 170 | int ret; | ||
| 171 | |||
| 172 |
3/4✓ Branch 0 taken 10908 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 10908 times.
✗ Branch 4 not taken.
|
10912 | if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 || |
| 173 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10892 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
10912 | s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 || |
| 174 |
3/4✓ Branch 0 taken 2218 times.
✓ Branch 1 taken 8694 times.
✓ Branch 3 taken 2218 times.
✗ Branch 4 not taken.
|
10912 | s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 || |
| 175 |
3/4✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10873 times.
✓ Branch 3 taken 39 times.
✗ Branch 4 not taken.
|
10912 | s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0 || |
| 176 |
3/4✓ Branch 0 taken 719 times.
✓ Branch 1 taken 10193 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 719 times.
|
10912 | s->chroma_locations && (ret = ff_set_common_chroma_locations2(ctx, cfg_in, cfg_out, s->chroma_locations)) < 0) |
| 177 | ✗ | return ret; | |
| 178 | |||
| 179 | 10912 | 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 | }; | ||
| 221 | #endif /* CONFIG_FORMAT_FILTER */ | ||
| 222 | |||
| 223 | #if CONFIG_NOFORMAT_FILTER | ||
| 224 | const FFFilter ff_vf_noformat = { | ||
| 225 | .p.name = "noformat", | ||
| 226 | .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."), | ||
| 227 | .p.priv_class = &format_class, | ||
| 228 | |||
| 229 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 230 | |||
| 231 | .init = init, | ||
| 232 | .uninit = uninit, | ||
| 233 | |||
| 234 | .priv_size = sizeof(FormatContext), | ||
| 235 | |||
| 236 | FILTER_INPUTS(inputs), | ||
| 237 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 238 | |||
| 239 | FILTER_QUERY_FUNC2(query_formats), | ||
| 240 | }; | ||
| 241 | #endif /* CONFIG_NOFORMAT_FILTER */ | ||
| 242 |