| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2025 Niklas Haas | ||
| 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 | * Video color space detector, tries to auto-detect YUV range and alpha mode. | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <stdbool.h> | ||
| 27 | #include <stdatomic.h> | ||
| 28 | |||
| 29 | #include "config.h" | ||
| 30 | |||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | #include "libavutil/pixdesc.h" | ||
| 34 | |||
| 35 | #include "avfilter.h" | ||
| 36 | #include "filters.h" | ||
| 37 | #include "formats.h" | ||
| 38 | #include "video.h" | ||
| 39 | |||
| 40 | #include "vf_colordetectdsp.h" | ||
| 41 | |||
| 42 | enum ColorDetectMode { | ||
| 43 | COLOR_DETECT_COLOR_RANGE = 1 << 0, | ||
| 44 | COLOR_DETECT_ALPHA_MODE = 1 << 1, | ||
| 45 | }; | ||
| 46 | |||
| 47 | typedef struct ColorDetectContext { | ||
| 48 | const AVClass *class; | ||
| 49 | FFColorDetectDSPContext dsp; | ||
| 50 | unsigned mode; | ||
| 51 | |||
| 52 | const AVPixFmtDescriptor *desc; | ||
| 53 | int nb_threads; | ||
| 54 | int depth; | ||
| 55 | int idx_a; | ||
| 56 | int mpeg_min; | ||
| 57 | int mpeg_max; | ||
| 58 | |||
| 59 | atomic_int detected_range; // enum AVColorRange | ||
| 60 | atomic_int detected_alpha; // enum FFAlphaDetect | ||
| 61 | } ColorDetectContext; | ||
| 62 | |||
| 63 | #define OFFSET(x) offsetof(ColorDetectContext, x) | ||
| 64 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 65 | |||
| 66 | static const AVOption colordetect_options[] = { | ||
| 67 | { "mode", "Image properties to detect", OFFSET(mode), AV_OPT_TYPE_FLAGS, {.i64 = -1}, 0, UINT_MAX, FLAGS, .unit = "mode" }, | ||
| 68 | { "color_range", "Detect (YUV) color range", 0, AV_OPT_TYPE_CONST, {.i64 = COLOR_DETECT_COLOR_RANGE}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 69 | { "alpha_mode", "Detect alpha mode", 0, AV_OPT_TYPE_CONST, {.i64 = COLOR_DETECT_ALPHA_MODE }, 0, 0, FLAGS, .unit = "mode" }, | ||
| 70 | { "all", "Detect all supported properties", 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, FLAGS, .unit = "mode" }, | ||
| 71 | { NULL } | ||
| 72 | }; | ||
| 73 | |||
| 74 | AVFILTER_DEFINE_CLASS(colordetect); | ||
| 75 | |||
| 76 | ✗ | static int query_format(const AVFilterContext *ctx, | |
| 77 | AVFilterFormatsConfig **cfg_in, | ||
| 78 | AVFilterFormatsConfig **cfg_out) | ||
| 79 | { | ||
| 80 | ✗ | int want_flags = AV_PIX_FMT_FLAG_PLANAR; | |
| 81 | ✗ | int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL | | |
| 82 | AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_FLOAT | | ||
| 83 | AV_PIX_FMT_FLAG_BAYER | AV_PIX_FMT_FLAG_XYZ; | ||
| 84 | |||
| 85 | if (HAVE_BIGENDIAN) { | ||
| 86 | want_flags |= AV_PIX_FMT_FLAG_BE; | ||
| 87 | } else { | ||
| 88 | ✗ | reject_flags |= AV_PIX_FMT_FLAG_BE; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | AVFilterFormats *formats = ff_formats_pixdesc_filter(want_flags, reject_flags); | |
| 92 | ✗ | return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats); | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | static int config_input(AVFilterLink *inlink) | |
| 96 | { | ||
| 97 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 98 | ✗ | ColorDetectContext *s = ctx->priv; | |
| 99 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 100 | ✗ | const int depth = desc->comp[0].depth; | |
| 101 | ✗ | const int mpeg_min = 16 << (depth - 8); | |
| 102 | ✗ | const int mpeg_max = 235 << (depth - 8); | |
| 103 | ✗ | if (depth > 16) /* not currently possible; prevent future bugs */ | |
| 104 | ✗ | return AVERROR(ENOTSUP); | |
| 105 | |||
| 106 | ✗ | s->desc = desc; | |
| 107 | ✗ | s->depth = depth; | |
| 108 | ✗ | s->mpeg_min = mpeg_min; | |
| 109 | ✗ | s->mpeg_max = mpeg_max; | |
| 110 | ✗ | s->nb_threads = ff_filter_get_nb_threads(ctx); | |
| 111 | |||
| 112 | ✗ | if (desc->flags & AV_PIX_FMT_FLAG_RGB) { | |
| 113 | ✗ | atomic_init(&s->detected_range, AVCOL_RANGE_JPEG); | |
| 114 | } else { | ||
| 115 | ✗ | atomic_init(&s->detected_range, AVCOL_RANGE_UNSPECIFIED); | |
| 116 | } | ||
| 117 | |||
| 118 | ✗ | if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) { | |
| 119 | ✗ | s->idx_a = desc->comp[desc->nb_components - 1].plane; | |
| 120 | ✗ | atomic_init(&s->detected_alpha, FF_ALPHA_UNDETERMINED); | |
| 121 | } else { | ||
| 122 | ✗ | atomic_init(&s->detected_alpha, FF_ALPHA_NONE); | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | ff_color_detect_dsp_init(&s->dsp, depth, inlink->color_range); | |
| 126 | ✗ | return 0; | |
| 127 | } | ||
| 128 | |||
| 129 | ✗ | static int detect_range(AVFilterContext *ctx, void *arg, | |
| 130 | int jobnr, int nb_jobs) | ||
| 131 | { | ||
| 132 | ✗ | ColorDetectContext *s = ctx->priv; | |
| 133 | ✗ | const AVFrame *in = arg; | |
| 134 | ✗ | const ptrdiff_t stride = in->linesize[0]; | |
| 135 | ✗ | const int y_start = (in->height * jobnr) / nb_jobs; | |
| 136 | ✗ | const int y_end = (in->height * (jobnr + 1)) / nb_jobs; | |
| 137 | ✗ | const int h_slice = y_end - y_start; | |
| 138 | |||
| 139 | ✗ | if (s->dsp.detect_range(in->data[0] + y_start * stride, stride, | |
| 140 | ✗ | in->width, h_slice, s->mpeg_min, s->mpeg_max)) | |
| 141 | ✗ | atomic_store_explicit(&s->detected_range, AVCOL_RANGE_JPEG, | |
| 142 | memory_order_relaxed); | ||
| 143 | |||
| 144 | ✗ | return 0; | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | static int detect_alpha(AVFilterContext *ctx, void *arg, | |
| 148 | int jobnr, int nb_jobs) | ||
| 149 | { | ||
| 150 | ✗ | ColorDetectContext *s = ctx->priv; | |
| 151 | ✗ | const AVFrame *in = arg; | |
| 152 | ✗ | const int w = in->width; | |
| 153 | ✗ | const int h = in->height; | |
| 154 | ✗ | const int y_start = (h * jobnr) / nb_jobs; | |
| 155 | ✗ | const int y_end = (h * (jobnr + 1)) / nb_jobs; | |
| 156 | ✗ | const int h_slice = y_end - y_start; | |
| 157 | |||
| 158 | ✗ | const int nb_planes = (s->desc->flags & AV_PIX_FMT_FLAG_RGB) ? 3 : 1; | |
| 159 | ✗ | const ptrdiff_t alpha_stride = in->linesize[s->idx_a]; | |
| 160 | ✗ | const uint8_t *alpha = in->data[s->idx_a] + y_start * alpha_stride; | |
| 161 | |||
| 162 | /** | ||
| 163 | * To check if a value is out of range, we need to compare the color value | ||
| 164 | * against the maximum possible color for a given alpha value. | ||
| 165 | * x > ((mpeg_max - mpeg_min) / pixel_max) * a + mpeg_min | ||
| 166 | * | ||
| 167 | * This simplifies to: | ||
| 168 | * (x - mpeg_min) * pixel_max > (mpeg_max - mpeg_min) * a | ||
| 169 | * = alpha_max * x - offset > mpeg_range * a in the below formula. | ||
| 170 | * | ||
| 171 | * We subtract an additional offset of (1 << (depth - 1)) to account for | ||
| 172 | * rounding errors in the value of `x`. | ||
| 173 | */ | ||
| 174 | ✗ | const int alpha_max = (1 << s->depth) - 1; | |
| 175 | ✗ | const int mpeg_range = s->mpeg_max - s->mpeg_min; | |
| 176 | ✗ | const int offset = alpha_max * s->mpeg_min + (1 << (s->depth - 1)); | |
| 177 | |||
| 178 | ✗ | int ret = 0; | |
| 179 | ✗ | for (int i = 0; i < nb_planes; i++) { | |
| 180 | ✗ | const ptrdiff_t stride = in->linesize[i]; | |
| 181 | ✗ | ret = s->dsp.detect_alpha(in->data[i] + y_start * stride, stride, | |
| 182 | alpha, alpha_stride, w, h_slice, alpha_max, | ||
| 183 | mpeg_range, offset); | ||
| 184 | ✗ | ret |= atomic_fetch_or_explicit(&s->detected_alpha, ret, memory_order_relaxed); | |
| 185 | ✗ | if (ret == FF_ALPHA_STRAIGHT) | |
| 186 | ✗ | break; | |
| 187 | } | ||
| 188 | |||
| 189 | ✗ | return 0; | |
| 190 | } | ||
| 191 | |||
| 192 | ✗ | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 193 | { | ||
| 194 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 195 | ✗ | ColorDetectContext *s = ctx->priv; | |
| 196 | ✗ | const int nb_threads = FFMIN(inlink->h, s->nb_threads); | |
| 197 | |||
| 198 | ✗ | enum AVColorRange detected_range = atomic_load_explicit(&s->detected_range, memory_order_relaxed); | |
| 199 | ✗ | if (s->mode & COLOR_DETECT_COLOR_RANGE && detected_range == AVCOL_RANGE_UNSPECIFIED) | |
| 200 | ✗ | ff_filter_execute(ctx, detect_range, in, NULL, nb_threads); | |
| 201 | |||
| 202 | ✗ | enum FFAlphaDetect detected_alpha = atomic_load_explicit(&s->detected_alpha, memory_order_relaxed); | |
| 203 | ✗ | if (s->mode & COLOR_DETECT_ALPHA_MODE && detected_alpha != FF_ALPHA_NONE && | |
| 204 | detected_alpha != FF_ALPHA_STRAIGHT) | ||
| 205 | ✗ | ff_filter_execute(ctx, detect_alpha, in, NULL, nb_threads); | |
| 206 | |||
| 207 | ✗ | return ff_filter_frame(inlink->dst->outputs[0], in); | |
| 208 | } | ||
| 209 | |||
| 210 | ✗ | static av_cold void report_detected_props(AVFilterContext *ctx) | |
| 211 | { | ||
| 212 | ✗ | ColorDetectContext *s = ctx->priv; | |
| 213 | ✗ | if (!s->mode) | |
| 214 | ✗ | return; | |
| 215 | |||
| 216 | ✗ | av_log(ctx, AV_LOG_INFO, "Detected color properties:\n"); | |
| 217 | ✗ | if (s->mode & COLOR_DETECT_COLOR_RANGE) { | |
| 218 | ✗ | enum AVColorRange detected_range = atomic_load_explicit(&s->detected_range, | |
| 219 | memory_order_relaxed); | ||
| 220 | ✗ | av_log(ctx, AV_LOG_INFO, " Color range: %s\n", | |
| 221 | detected_range == AVCOL_RANGE_JPEG ? "JPEG / full range" | ||
| 222 | : "undetermined"); | ||
| 223 | } | ||
| 224 | |||
| 225 | ✗ | if (s->mode & COLOR_DETECT_ALPHA_MODE) { | |
| 226 | ✗ | enum FFAlphaDetect detected_alpha = atomic_load_explicit(&s->detected_alpha, | |
| 227 | memory_order_relaxed); | ||
| 228 | ✗ | av_log(ctx, AV_LOG_INFO, " Alpha mode: %s\n", | |
| 229 | detected_alpha == FF_ALPHA_NONE ? "none" : | ||
| 230 | ✗ | detected_alpha == FF_ALPHA_STRAIGHT ? "straight" : | |
| 231 | detected_alpha == FF_ALPHA_TRANSPARENT ? "undetermined" | ||
| 232 | ✗ | : "opaque"); | |
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | ✗ | static int activate(AVFilterContext *ctx) | |
| 237 | { | ||
| 238 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 239 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 240 | AVFrame *frame; | ||
| 241 | int64_t pts; | ||
| 242 | int ret; | ||
| 243 | |||
| 244 | ✗ | ret = ff_outlink_get_status(outlink); | |
| 245 | ✗ | if (ret) { | |
| 246 | ✗ | ff_inlink_set_status(inlink, ret); | |
| 247 | ✗ | report_detected_props(ctx); | |
| 248 | ✗ | return 0; | |
| 249 | } | ||
| 250 | |||
| 251 | ✗ | ret = ff_inlink_consume_frame(inlink, &frame); | |
| 252 | ✗ | if (ret < 0) { | |
| 253 | ✗ | return ret; | |
| 254 | ✗ | } else if (ret) { | |
| 255 | ✗ | return filter_frame(inlink, frame); | |
| 256 | } | ||
| 257 | |||
| 258 | ✗ | if (ff_inlink_acknowledge_status(inlink, &ret, &pts)) { | |
| 259 | ✗ | ff_outlink_set_status(outlink, ret, pts); | |
| 260 | ✗ | report_detected_props(ctx); | |
| 261 | ✗ | return 0; | |
| 262 | } | ||
| 263 | |||
| 264 | ✗ | FF_FILTER_FORWARD_WANTED(outlink, inlink); | |
| 265 | ✗ | return FFERROR_NOT_READY; | |
| 266 | } | ||
| 267 | |||
| 268 | static const AVFilterPad colordetect_inputs[] = { | ||
| 269 | { | ||
| 270 | .name = "default", | ||
| 271 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 272 | .config_props = config_input, | ||
| 273 | }, | ||
| 274 | }; | ||
| 275 | |||
| 276 | const FFFilter ff_vf_colordetect = { | ||
| 277 | .p.name = "colordetect", | ||
| 278 | .p.description = NULL_IF_CONFIG_SMALL("Detect video color properties."), | ||
| 279 | .p.priv_class = &colordetect_class, | ||
| 280 | .p.flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_METADATA_ONLY, | ||
| 281 | .priv_size = sizeof(ColorDetectContext), | ||
| 282 | FILTER_INPUTS(colordetect_inputs), | ||
| 283 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 284 | FILTER_QUERY_FUNC2(query_format), | ||
| 285 | .activate = activate, | ||
| 286 | }; | ||
| 287 |