| 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 | 15861 | static av_cold void uninit(AVFilterContext *ctx) | |
| 54 | { | ||
| 55 | 15861 | FormatContext *s = ctx->priv; | |
| 56 | 15861 | ff_formats_unref(&s->formats); | |
| 57 | 15861 | ff_formats_unref(&s->color_spaces); | |
| 58 | 15861 | ff_formats_unref(&s->color_ranges); | |
| 59 | 15861 | ff_formats_unref(&s->alpha_modes); | |
| 60 | 15861 | } | |
| 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 | 17829 | static int parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx) | |
| 92 | { | ||
| 93 | char *tail; | ||
| 94 | 17829 | int pix_fmt = av_get_pix_fmt(arg); | |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17829 times.
|
17829 | if (pix_fmt == AV_PIX_FMT_NONE) { |
| 96 | ✗ | pix_fmt = strtol(arg, &tail, 0); | |
| 97 | ✗ | if (*tail || !av_pix_fmt_desc_get(pix_fmt)) { | |
| 98 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg); | |
| 99 | ✗ | return AVERROR(EINVAL); | |
| 100 | } | ||
| 101 | } | ||
| 102 | 17829 | *ret = pix_fmt; | |
| 103 | 17829 | return 0; | |
| 104 | } | ||
| 105 | |||
| 106 | 15861 | static av_cold int init(AVFilterContext *ctx) | |
| 107 | { | ||
| 108 | 15861 | FormatContext *s = ctx->priv; | |
| 109 | enum AVPixelFormat pix_fmt; | ||
| 110 | int ret; | ||
| 111 | |||
| 112 |
2/2✓ Branch 0 taken 17829 times.
✓ Branch 1 taken 15861 times.
|
33690 | for (char *sep, *cur = s->pix_fmts; cur; cur = sep) { |
| 113 | 17829 | sep = strchr(cur, '|'); | |
| 114 |
3/4✓ Branch 0 taken 1968 times.
✓ Branch 1 taken 15861 times.
✓ Branch 2 taken 1968 times.
✗ Branch 3 not taken.
|
17829 | if (sep && *sep) |
| 115 | 1968 | *sep++ = 0; | |
| 116 |
2/4✓ Branch 1 taken 17829 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 17829 times.
|
35658 | if ((ret = parse_pixel_format(&pix_fmt, cur, ctx)) < 0 || |
| 117 | 17829 | (ret = ff_add_format(&s->formats, pix_fmt)) < 0) | |
| 118 | ✗ | return ret; | |
| 119 | } | ||
| 120 | |||
| 121 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 15861 times.
|
15881 | for (char *sep, *cur = s->csps; cur; cur = sep) { |
| 122 | 20 | sep = strchr(cur, '|'); | |
| 123 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
20 | if (sep && *sep) |
| 124 | ✗ | *sep++ = 0; | |
| 125 |
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 || |
| 126 | 20 | (ret = ff_add_format(&s->color_spaces, ret)) < 0) | |
| 127 | ✗ | return ret; | |
| 128 | } | ||
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 2226 times.
✓ Branch 1 taken 15861 times.
|
18087 | for (char *sep, *cur = s->ranges; cur; cur = sep) { |
| 131 | 2226 | sep = strchr(cur, '|'); | |
| 132 |
3/4✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2189 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
|
2226 | if (sep && *sep) |
| 133 | 37 | *sep++ = 0; | |
| 134 |
2/4✓ Branch 1 taken 2226 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2226 times.
|
4452 | if ((ret = av_color_range_from_name(cur)) < 0 || |
| 135 | 2226 | (ret = ff_add_format(&s->color_ranges, ret)) < 0) | |
| 136 | ✗ | return ret; | |
| 137 | } | ||
| 138 | |||
| 139 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 15861 times.
|
15900 | for (char *sep, *cur = s->alphamodes; cur; cur = sep) { |
| 140 | 39 | sep = strchr(cur, '|'); | |
| 141 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
39 | if (sep && *sep) |
| 142 | ✗ | *sep++ = 0; | |
| 143 |
2/4✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 39 times.
|
78 | if ((ret = av_alpha_mode_from_name(cur)) < 0 || |
| 144 | 39 | (ret = ff_add_format(&s->alpha_modes, ret)) < 0) | |
| 145 | ✗ | return ret; | |
| 146 | } | ||
| 147 | |||
| 148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15861 times.
|
15861 | if (!strcmp(ctx->filter->name, "noformat")) { |
| 149 | ✗ | if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 || | |
| 150 | ✗ | (ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 || | |
| 151 | ✗ | (ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0 || | |
| 152 | ✗ | (ret = invert_formats(&s->alpha_modes, ff_all_alpha_modes())) < 0) | |
| 153 | ✗ | return ret; | |
| 154 | } | ||
| 155 | |||
| 156 | /* hold on to a ref for the lifetime of the filter */ | ||
| 157 |
2/4✓ Branch 0 taken 15861 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15861 times.
✗ Branch 4 not taken.
|
15861 | if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 || |
| 158 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 15841 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
15861 | s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 || |
| 159 |
3/4✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 13672 times.
✓ Branch 3 taken 2189 times.
✗ Branch 4 not taken.
|
15861 | s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0 || |
| 160 |
3/4✓ Branch 0 taken 39 times.
✓ Branch 1 taken 15822 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39 times.
|
15861 | s->alpha_modes && (ret = ff_formats_ref(s->alpha_modes, &s->alpha_modes)) < 0) |
| 161 | ✗ | return ret; | |
| 162 | |||
| 163 | 15861 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | 10821 | static int query_formats(const AVFilterContext *ctx, | |
| 167 | AVFilterFormatsConfig **cfg_in, | ||
| 168 | AVFilterFormatsConfig **cfg_out) | ||
| 169 | { | ||
| 170 | 10821 | FormatContext *s = ctx->priv; | |
| 171 | int ret; | ||
| 172 | |||
| 173 |
2/4✓ Branch 0 taken 10821 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 10821 times.
✗ Branch 4 not taken.
|
10821 | if (s->formats && (ret = ff_set_common_formats2 (ctx, cfg_in, cfg_out, s->formats)) < 0 || |
| 174 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10801 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
10821 | s->color_spaces && (ret = ff_set_common_color_spaces2(ctx, cfg_in, cfg_out, s->color_spaces)) < 0 || |
| 175 |
3/4✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 8632 times.
✓ Branch 3 taken 2189 times.
✗ Branch 4 not taken.
|
10821 | s->color_ranges && (ret = ff_set_common_color_ranges2(ctx, cfg_in, cfg_out, s->color_ranges)) < 0 || |
| 176 |
3/4✓ Branch 0 taken 38 times.
✓ Branch 1 taken 10783 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
|
10821 | s->alpha_modes && (ret = ff_set_common_alpha_modes2(ctx, cfg_in, cfg_out, s->alpha_modes)) < 0) |
| 177 | ✗ | return ret; | |
| 178 | |||
| 179 | 10821 | 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 | { NULL } | ||
| 190 | }; | ||
| 191 | |||
| 192 | AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options); | ||
| 193 | |||
| 194 | static const AVFilterPad inputs[] = { | ||
| 195 | { | ||
| 196 | .name = "default", | ||
| 197 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 198 | .get_buffer.video = ff_null_get_video_buffer, | ||
| 199 | }, | ||
| 200 | }; | ||
| 201 | |||
| 202 | #if CONFIG_FORMAT_FILTER | ||
| 203 | const FFFilter ff_vf_format = { | ||
| 204 | .p.name = "format", | ||
| 205 | .p.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."), | ||
| 206 | .p.priv_class = &format_class, | ||
| 207 | |||
| 208 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 209 | |||
| 210 | .init = init, | ||
| 211 | .uninit = uninit, | ||
| 212 | |||
| 213 | .priv_size = sizeof(FormatContext), | ||
| 214 | |||
| 215 | FILTER_INPUTS(inputs), | ||
| 216 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 217 | |||
| 218 | FILTER_QUERY_FUNC2(query_formats), | ||
| 219 | }; | ||
| 220 | #endif /* CONFIG_FORMAT_FILTER */ | ||
| 221 | |||
| 222 | #if CONFIG_NOFORMAT_FILTER | ||
| 223 | const FFFilter ff_vf_noformat = { | ||
| 224 | .p.name = "noformat", | ||
| 225 | .p.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."), | ||
| 226 | .p.priv_class = &format_class, | ||
| 227 | |||
| 228 | .p.flags = AVFILTER_FLAG_METADATA_ONLY, | ||
| 229 | |||
| 230 | .init = init, | ||
| 231 | .uninit = uninit, | ||
| 232 | |||
| 233 | .priv_size = sizeof(FormatContext), | ||
| 234 | |||
| 235 | FILTER_INPUTS(inputs), | ||
| 236 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 237 | |||
| 238 | FILTER_QUERY_FUNC2(query_formats), | ||
| 239 | }; | ||
| 240 | #endif /* CONFIG_NOFORMAT_FILTER */ | ||
| 241 |