| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2019, 2020, 2022, 2026 Vladimir Panteleev | ||
| 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 | #include <float.h> | ||
| 22 | |||
| 23 | #include "libavutil/opt.h" | ||
| 24 | #include "avfilter.h" | ||
| 25 | |||
| 26 | #include "filters.h" | ||
| 27 | #include "video.h" | ||
| 28 | |||
| 29 | #define MAX_FRAMES 240 | ||
| 30 | #define GRID_SIZE 8 | ||
| 31 | #define NUM_CHANNELS 3 | ||
| 32 | |||
| 33 | typedef struct PhotosensitivityFrame { | ||
| 34 | uint8_t grid[GRID_SIZE][GRID_SIZE][4]; | ||
| 35 | } PhotosensitivityFrame; | ||
| 36 | |||
| 37 | typedef struct PhotosensitivityContext { | ||
| 38 | const AVClass *class; | ||
| 39 | |||
| 40 | int nb_frames; | ||
| 41 | int skip; | ||
| 42 | float threshold_multiplier, blend_factor; | ||
| 43 | int bypass; | ||
| 44 | |||
| 45 | int badness_threshold; | ||
| 46 | |||
| 47 | /* Circular buffer */ | ||
| 48 | int history[MAX_FRAMES]; | ||
| 49 | int history_pos, history_size; | ||
| 50 | |||
| 51 | PhotosensitivityFrame last_frame_e; | ||
| 52 | AVFrame *last_frame_av; | ||
| 53 | } PhotosensitivityContext; | ||
| 54 | |||
| 55 | #define OFFSET(x) offsetof(PhotosensitivityContext, x) | ||
| 56 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 57 | |||
| 58 | static const AVOption photosensitivity_options[] = { | ||
| 59 | { "frames", "set how many frames to use", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS }, | ||
| 60 | { "f", "set how many frames to use", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS }, | ||
| 61 | { "threshold", "set detection threshold factor (lower is stricter)", OFFSET(threshold_multiplier), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.1, FLT_MAX, FLAGS }, | ||
| 62 | { "t", "set detection threshold factor (lower is stricter)", OFFSET(threshold_multiplier), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.1, FLT_MAX, FLAGS }, | ||
| 63 | { "skip", "set pixels to skip when sampling frames", OFFSET(skip), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS }, | ||
| 64 | { "bypass", "leave frames unchanged", OFFSET(bypass), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS }, | ||
| 65 | { "blend", "set blending factor (0 always duplicates frames)", OFFSET(blend_factor), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, FLAGS }, | ||
| 66 | { NULL } | ||
| 67 | }; | ||
| 68 | |||
| 69 | AVFILTER_DEFINE_CLASS(photosensitivity); | ||
| 70 | |||
| 71 | typedef struct ThreadData_convert_frame | ||
| 72 | { | ||
| 73 | AVFrame *in; | ||
| 74 | PhotosensitivityFrame *out; | ||
| 75 | int skip; | ||
| 76 | } ThreadData_convert_frame; | ||
| 77 | |||
| 78 | #define NUM_CELLS (GRID_SIZE * GRID_SIZE) | ||
| 79 | |||
| 80 | 54 | static int convert_frame_partial(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 81 | { | ||
| 82 | int cell, gx, gy, x0, x1, y0, y1, x, y, c, area; | ||
| 83 | int sum[NUM_CHANNELS]; | ||
| 84 | const uint8_t *p; | ||
| 85 | |||
| 86 | 54 | ThreadData_convert_frame *td = arg; | |
| 87 | |||
| 88 | 54 | const int slice_start = ff_slice_pos(NUM_CELLS, jobnr, nb_jobs); | |
| 89 | 54 | const int slice_end = ff_slice_pos(NUM_CELLS, jobnr + 1, nb_jobs); | |
| 90 | |||
| 91 | 54 | int width = td->in->width, height = td->in->height, linesize = td->in->linesize[0], skip = td->skip; | |
| 92 | 54 | const uint8_t *data = td->in->data[0]; | |
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 54 times.
|
438 | for (cell = slice_start; cell < slice_end; cell++) { |
| 95 | 384 | gx = cell % GRID_SIZE; | |
| 96 | 384 | gy = cell / GRID_SIZE; | |
| 97 | |||
| 98 | 384 | x0 = width * gx / GRID_SIZE; | |
| 99 | 384 | x1 = width * (gx+1) / GRID_SIZE; | |
| 100 | 384 | y0 = height * gy / GRID_SIZE; | |
| 101 | 384 | y1 = height * (gy+1) / GRID_SIZE; | |
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 384 times.
|
1536 | for (c = 0; c < NUM_CHANNELS; c++) { |
| 104 | 1152 | sum[c] = 0; | |
| 105 | } | ||
| 106 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 384 times.
|
1152 | for (y = y0; y < y1; y += skip) { |
| 107 | 768 | p = data + y * linesize + x0 * NUM_CHANNELS; | |
| 108 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 768 times.
|
2304 | for (x = x0; x < x1; x += skip) { |
| 109 | //av_log(NULL, AV_LOG_VERBOSE, "%d %d %d : (%d,%d) (%d,%d) -> %d,%d | *%d\n", c, gx, gy, x0, y0, x1, y1, x, y, (int)row); | ||
| 110 | 1536 | sum[0] += p[0]; | |
| 111 | 1536 | sum[1] += p[1]; | |
| 112 | 1536 | sum[2] += p[2]; | |
| 113 | 1536 | p += NUM_CHANNELS * skip; | |
| 114 | // TODO: variable size | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | 384 | area = ((x1 - x0 + skip - 1) / skip) * ((y1 - y0 + skip - 1) / skip); | |
| 119 |
2/2✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 384 times.
|
1536 | for (c = 0; c < NUM_CHANNELS; c++) { |
| 120 |
1/2✓ Branch 0 taken 1152 times.
✗ Branch 1 not taken.
|
1152 | if (area) |
| 121 | 1152 | sum[c] /= area; | |
| 122 | 1152 | td->out->grid[gy][gx][c] = sum[c]; | |
| 123 | } | ||
| 124 | } | ||
| 125 | 54 | return 0; | |
| 126 | } | ||
| 127 | |||
| 128 | 6 | static void convert_frame(AVFilterContext *ctx, AVFrame *in, PhotosensitivityFrame *out, int skip) | |
| 129 | { | ||
| 130 | ThreadData_convert_frame td; | ||
| 131 | 6 | td.in = in; | |
| 132 | 6 | td.out = out; | |
| 133 | 6 | td.skip = skip; | |
| 134 | 6 | ff_filter_execute(ctx, convert_frame_partial, &td, NULL, | |
| 135 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | FFMIN(NUM_CELLS, ff_filter_get_nb_threads(ctx))); |
| 136 | 6 | } | |
| 137 | |||
| 138 | typedef struct ThreadData_blend_frame | ||
| 139 | { | ||
| 140 | AVFrame *target; | ||
| 141 | AVFrame *source; | ||
| 142 | uint16_t s_mul; | ||
| 143 | } ThreadData_blend_frame; | ||
| 144 | |||
| 145 | 9 | static int blend_frame_partial(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 146 | { | ||
| 147 | int x, y; | ||
| 148 | uint8_t *t, *s; | ||
| 149 | |||
| 150 | 9 | ThreadData_blend_frame *td = arg; | |
| 151 | 9 | const uint16_t s_mul = td->s_mul; | |
| 152 | 9 | const uint16_t t_mul = 0x100 - s_mul; | |
| 153 | 9 | const int slice_start = ff_slice_pos(td->target->height, jobnr, nb_jobs); | |
| 154 | 9 | const int slice_end = ff_slice_pos(td->target->height, jobnr + 1, nb_jobs); | |
| 155 | 9 | const int linesize = td->target->linesize[0]; | |
| 156 | |||
| 157 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9 times.
|
25 | for (y = slice_start; y < slice_end; y++) { |
| 158 | 16 | t = td->target->data[0] + y * td->target->linesize[0]; | |
| 159 | 16 | s = td->source->data[0] + y * td->source->linesize[0]; | |
| 160 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 16 times.
|
784 | for (x = 0; x < linesize; x++) { |
| 161 | 768 | *t = (*t * t_mul + *s * s_mul) >> 8; | |
| 162 | 768 | t++; s++; | |
| 163 | } | ||
| 164 | } | ||
| 165 | 9 | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | 1 | static void blend_frame(AVFilterContext *ctx, AVFrame *target, AVFrame *source, float factor) | |
| 169 | { | ||
| 170 | ThreadData_blend_frame td; | ||
| 171 | 1 | td.target = target; | |
| 172 | 1 | td.source = source; | |
| 173 | 1 | td.s_mul = (uint16_t)(factor * 0x100); | |
| 174 | 1 | ff_filter_execute(ctx, blend_frame_partial, &td, NULL, | |
| 175 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | FFMIN(ctx->outputs[0]->h, ff_filter_get_nb_threads(ctx))); |
| 176 | 1 | } | |
| 177 | |||
| 178 | 6 | static int get_badness(PhotosensitivityFrame *a, PhotosensitivityFrame *b) | |
| 179 | { | ||
| 180 | int badness, x, y, c; | ||
| 181 | 6 | badness = 0; | |
| 182 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
|
24 | for (c = 0; c < NUM_CHANNELS; c++) { |
| 183 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 18 times.
|
162 | for (y = 0; y < GRID_SIZE; y++) { |
| 184 |
2/2✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 144 times.
|
1296 | for (x = 0; x < GRID_SIZE; x++) { |
| 185 | 1152 | badness += abs((int)a->grid[y][x][c] - (int)b->grid[y][x][c]); | |
| 186 | //av_log(NULL, AV_LOG_VERBOSE, "%d - %d -> %d \n", a->grid[y][x], b->grid[y][x], badness); | ||
| 187 | //av_log(NULL, AV_LOG_VERBOSE, "%d -> %d \n", abs((int)a->grid[y][x] - (int)b->grid[y][x]), badness); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | 6 | return badness; | |
| 192 | } | ||
| 193 | |||
| 194 | 2 | static int config_input(AVFilterLink *inlink) | |
| 195 | { | ||
| 196 | /* const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); */ | ||
| 197 | 2 | AVFilterContext *ctx = inlink->dst; | |
| 198 | 2 | PhotosensitivityContext *s = ctx->priv; | |
| 199 | |||
| 200 | 2 | s->badness_threshold = (int)(GRID_SIZE * GRID_SIZE * 4 * 256 * s->threshold_multiplier / 128); | |
| 201 | |||
| 202 | 2 | return 0; | |
| 203 | } | ||
| 204 | |||
| 205 | 5 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 206 | { | ||
| 207 | int this_badness, current_badness, fixed_badness, new_badness, badness_threshold, i, res, start; | ||
| 208 | PhotosensitivityFrame ef; | ||
| 209 | AVFrame *src, *out; | ||
| 210 | 5 | int free_in = 0; | |
| 211 | float factor; | ||
| 212 | AVDictionary **metadata; | ||
| 213 | |||
| 214 | 5 | AVFilterContext *ctx = inlink->dst; | |
| 215 | 5 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 216 | 5 | PhotosensitivityContext *s = ctx->priv; | |
| 217 | |||
| 218 | /* weighted moving average */ | ||
| 219 | 5 | current_badness = 0; | |
| 220 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | if (s->history_size) { |
| 221 | 3 | start = s->nb_frames + s->history_pos - s->history_size; | |
| 222 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | for (i = 1; i < s->history_size; i++) |
| 223 | 1 | current_badness += i * s->history[(start + i) % s->nb_frames]; | |
| 224 | 3 | current_badness /= s->history_size; | |
| 225 | 3 | badness_threshold = s->badness_threshold * s->history_size; | |
| 226 | } else { | ||
| 227 | 2 | badness_threshold = INT_MAX; | |
| 228 | } | ||
| 229 | |||
| 230 | 5 | convert_frame(ctx, in, &ef, s->skip); | |
| 231 | 5 | this_badness = get_badness(&ef, &s->last_frame_e); | |
| 232 | 5 | new_badness = current_badness + this_badness; | |
| 233 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | av_log(ctx, AV_LOG_VERBOSE, "badness: %6d -> %6d / %6d (%3d%% - %s)\n", |
| 234 | current_badness, new_badness, badness_threshold, | ||
| 235 | 5 | 100 * new_badness / badness_threshold, new_badness < badness_threshold ? "OK" : "EXCEEDED"); | |
| 236 | |||
| 237 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
5 | if (new_badness < badness_threshold || !s->last_frame_av || s->bypass) { |
| 238 | 3 | factor = 1; /* for metadata */ | |
| 239 | 3 | av_frame_free(&s->last_frame_av); | |
| 240 | 3 | s->last_frame_av = src = in; | |
| 241 | 3 | s->last_frame_e = ef; | |
| 242 | 3 | fixed_badness = new_badness; | |
| 243 | } else { | ||
| 244 | 2 | factor = (float)(badness_threshold - current_badness) / (new_badness - current_badness) * s->blend_factor; | |
| 245 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (factor <= 0) { |
| 246 | /* just duplicate the frame */ | ||
| 247 | 1 | this_badness = 0; /* frame was duplicated, thus, delta is zero */ | |
| 248 | 1 | fixed_badness = current_badness; | |
| 249 | } else { | ||
| 250 | 1 | res = ff_inlink_make_frame_writable(inlink, &s->last_frame_av); | |
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (res) { |
| 252 | ✗ | av_frame_free(&in); | |
| 253 | ✗ | return res; | |
| 254 | } | ||
| 255 | 1 | blend_frame(ctx, s->last_frame_av, in, factor); | |
| 256 | |||
| 257 | 1 | convert_frame(ctx, s->last_frame_av, &ef, s->skip); | |
| 258 | 1 | this_badness = get_badness(&ef, &s->last_frame_e); | |
| 259 | 1 | fixed_badness = current_badness + this_badness; | |
| 260 | 1 | av_log(ctx, AV_LOG_VERBOSE, " fixed: %6d -> %6d / %6d (%3d%%) factor=%5.3f\n", | |
| 261 | current_badness, fixed_badness, badness_threshold, | ||
| 262 | 1 | 100 * new_badness / badness_threshold, factor); | |
| 263 | 1 | s->last_frame_e = ef; | |
| 264 | } | ||
| 265 | 2 | src = s->last_frame_av; | |
| 266 | 2 | free_in = 1; | |
| 267 | } | ||
| 268 | 5 | s->history[s->history_pos] = this_badness; | |
| 269 | 5 | s->history_pos = (s->history_pos + 1) % s->nb_frames; | |
| 270 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if (s->history_size < s->nb_frames) |
| 271 | 4 | s->history_size++; | |
| 272 | |||
| 273 | 5 | out = ff_get_video_buffer(outlink, in->width, in->height); | |
| 274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!out) { |
| 275 | ✗ | if (free_in == 1) | |
| 276 | ✗ | av_frame_free(&in); | |
| 277 | ✗ | return AVERROR(ENOMEM); | |
| 278 | } | ||
| 279 | 5 | av_frame_copy_props(out, in); | |
| 280 | 5 | metadata = &out->metadata; | |
| 281 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (metadata) { |
| 282 | char value[128]; | ||
| 283 | |||
| 284 | 5 | snprintf(value, sizeof(value), "%f", (float)new_badness / badness_threshold); | |
| 285 | 5 | av_dict_set(metadata, "lavfi.photosensitivity.badness", value, 0); | |
| 286 | |||
| 287 | 5 | snprintf(value, sizeof(value), "%f", (float)fixed_badness / badness_threshold); | |
| 288 | 5 | av_dict_set(metadata, "lavfi.photosensitivity.fixed-badness", value, 0); | |
| 289 | |||
| 290 | 5 | snprintf(value, sizeof(value), "%f", (float)this_badness / badness_threshold); | |
| 291 | 5 | av_dict_set(metadata, "lavfi.photosensitivity.frame-badness", value, 0); | |
| 292 | |||
| 293 | 5 | snprintf(value, sizeof(value), "%f", factor); | |
| 294 | 5 | av_dict_set(metadata, "lavfi.photosensitivity.factor", value, 0); | |
| 295 | } | ||
| 296 | 5 | av_frame_copy(out, src); | |
| 297 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if (free_in == 1) |
| 298 | 2 | av_frame_free(&in); | |
| 299 | 5 | return ff_filter_frame(outlink, out); | |
| 300 | } | ||
| 301 | |||
| 302 | 3 | static av_cold void uninit(AVFilterContext *ctx) | |
| 303 | { | ||
| 304 | 3 | PhotosensitivityContext *s = ctx->priv; | |
| 305 | |||
| 306 | 3 | av_frame_free(&s->last_frame_av); | |
| 307 | 3 | } | |
| 308 | |||
| 309 | static const AVFilterPad inputs[] = { | ||
| 310 | { | ||
| 311 | .name = "default", | ||
| 312 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 313 | .filter_frame = filter_frame, | ||
| 314 | .config_props = config_input, | ||
| 315 | }, | ||
| 316 | }; | ||
| 317 | |||
| 318 | const FFFilter ff_vf_photosensitivity = { | ||
| 319 | .p.name = "photosensitivity", | ||
| 320 | .p.description = NULL_IF_CONFIG_SMALL("Filter out photosensitive epilepsy seizure-inducing flashes."), | ||
| 321 | .p.priv_class = &photosensitivity_class, | ||
| 322 | .priv_size = sizeof(PhotosensitivityContext), | ||
| 323 | .uninit = uninit, | ||
| 324 | FILTER_INPUTS(inputs), | ||
| 325 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 326 | FILTER_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24), | ||
| 327 | }; | ||
| 328 |