2 * Copyright (c) 2019 Vladimir Panteleev
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
32 #define MAX_FRAMES 240
34 #define NUM_CHANNELS 3
36 typedef struct PhotosensitivityFrame {
37 uint8_t grid[GRID_SIZE][GRID_SIZE][4];
38 } PhotosensitivityFrame;
40 typedef struct PhotosensitivityContext {
45 float threshold_multiplier;
48 int badness_threshold;
51 int history[MAX_FRAMES];
54 PhotosensitivityFrame last_frame_e;
55 AVFrame *last_frame_av;
56 } PhotosensitivityContext;
58 #define OFFSET(x) offsetof(PhotosensitivityContext, x)
59 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
61 static const AVOption photosensitivity_options[] = {
62 { "frames", "set how many frames to use", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS },
63 { "f", "set how many frames to use", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS },
64 { "threshold", "set detection threshold factor (lower is stricter)", OFFSET(threshold_multiplier), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.1, FLT_MAX, FLAGS },
65 { "t", "set detection threshold factor (lower is stricter)", OFFSET(threshold_multiplier), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0.1, FLT_MAX, FLAGS },
66 { "skip", "set pixels to skip when sampling frames", OFFSET(skip), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
67 { "bypass", "leave frames unchanged", OFFSET(bypass), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
71 AVFILTER_DEFINE_CLASS(photosensitivity);
73 static int query_formats(AVFilterContext *ctx)
75 static const enum AVPixelFormat pixel_fmts[] = {
80 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
82 return AVERROR(ENOMEM);
83 return ff_set_common_formats(ctx, formats);
86 typedef struct ThreadData_convert_frame
89 PhotosensitivityFrame *out;
91 } ThreadData_convert_frame;
93 #define NUM_CELLS (GRID_SIZE * GRID_SIZE)
95 static int convert_frame_partial(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
97 int cell, gx, gy, x0, x1, y0, y1, x, y, c, area;
98 int sum[NUM_CHANNELS];
101 ThreadData_convert_frame *td = arg;
103 const int slice_start = (NUM_CELLS * jobnr) / nb_jobs;
104 const int slice_end = (NUM_CELLS * (jobnr+1)) / nb_jobs;
106 int width = td->in->width, height = td->in->height, linesize = td->in->linesize[0], skip = td->skip;
107 const uint8_t *data = td->in->data[0];
109 for (cell = slice_start; cell < slice_end; cell++) {
110 gx = cell % GRID_SIZE;
111 gy = cell / GRID_SIZE;
113 x0 = width * gx / GRID_SIZE;
114 x1 = width * (gx+1) / GRID_SIZE;
115 y0 = height * gy / GRID_SIZE;
116 y1 = height * (gy+1) / GRID_SIZE;
118 for (c = 0; c < NUM_CHANNELS; c++) {
121 for (y = y0; y < y1; y += skip) {
122 p = data + y * linesize + x0 * NUM_CHANNELS;
123 for (x = x0; x < x1; x += skip) {
124 //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);
128 p += NUM_CHANNELS * skip;
129 // TODO: variable size
133 area = ((x1 - x0 + skip - 1) / skip) * ((y1 - y0 + skip - 1) / skip);
134 for (c = 0; c < NUM_CHANNELS; c++) {
137 td->out->grid[gy][gx][c] = sum[c];
143 static void convert_frame(AVFilterContext *ctx, AVFrame *in, PhotosensitivityFrame *out, int skip)
145 ThreadData_convert_frame td;
149 ctx->internal->execute(ctx, convert_frame_partial, &td, NULL, FFMIN(NUM_CELLS, ff_filter_get_nb_threads(ctx)));
152 typedef struct ThreadData_blend_frame
157 } ThreadData_blend_frame;
159 static int blend_frame_partial(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
164 ThreadData_blend_frame *td = arg;
165 const uint16_t s_mul = td->s_mul;
166 const uint16_t t_mul = 0x100 - s_mul;
167 const int slice_start = (td->target->height * jobnr) / nb_jobs;
168 const int slice_end = (td->target->height * (jobnr+1)) / nb_jobs;
169 const int linesize = td->target->linesize[0];
171 for (y = slice_start; y < slice_end; y++) {
172 t = td->target->data[0] + y * td->target->linesize[0];
173 s = td->source->data[0] + y * td->source->linesize[0];
174 for (x = 0; x < linesize; x++) {
175 *t = (*t * t_mul + *s * s_mul) >> 8;
182 static void blend_frame(AVFilterContext *ctx, AVFrame *target, AVFrame *source, float factor)
184 ThreadData_blend_frame td;
187 td.s_mul = (uint16_t)(factor * 0x100);
188 ctx->internal->execute(ctx, blend_frame_partial, &td, NULL, FFMIN(ctx->outputs[0]->h, ff_filter_get_nb_threads(ctx)));
191 static int get_badness(PhotosensitivityFrame *a, PhotosensitivityFrame *b)
193 int badness, x, y, c;
195 for (c = 0; c < NUM_CHANNELS; c++) {
196 for (y = 0; y < GRID_SIZE; y++) {
197 for (x = 0; x < GRID_SIZE; x++) {
198 badness += abs((int)a->grid[y][x][c] - (int)b->grid[y][x][c]);
199 //av_log(NULL, AV_LOG_VERBOSE, "%d - %d -> %d \n", a->grid[y][x], b->grid[y][x], badness);
200 //av_log(NULL, AV_LOG_VERBOSE, "%d -> %d \n", abs((int)a->grid[y][x] - (int)b->grid[y][x]), badness);
207 static int config_input(AVFilterLink *inlink)
209 /* const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); */
210 AVFilterContext *ctx = inlink->dst;
211 PhotosensitivityContext *s = ctx->priv;
213 s->badness_threshold = (int)(GRID_SIZE * GRID_SIZE * 4 * 256 * s->nb_frames * s->threshold_multiplier / 128);
218 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
220 int this_badness, current_badness, fixed_badness, new_badness, i, res;
221 PhotosensitivityFrame ef;
225 AVDictionary **metadata;
227 AVFilterContext *ctx = inlink->dst;
228 AVFilterLink *outlink = ctx->outputs[0];
229 PhotosensitivityContext *s = ctx->priv;
231 /* weighted moving average */
233 for (i = 1; i < s->nb_frames; i++)
234 current_badness += i * s->history[(s->history_pos + i) % s->nb_frames];
235 current_badness /= s->nb_frames;
237 convert_frame(ctx, in, &ef, s->skip);
238 this_badness = get_badness(&ef, &s->last_frame_e);
239 new_badness = current_badness + this_badness;
240 av_log(s, AV_LOG_VERBOSE, "badness: %6d -> %6d / %6d (%3d%% - %s)\n",
241 current_badness, new_badness, s->badness_threshold,
242 100 * new_badness / s->badness_threshold, new_badness < s->badness_threshold ? "OK" : "EXCEEDED");
244 fixed_badness = new_badness;
245 if (new_badness < s->badness_threshold || !s->last_frame_av || s->bypass) {
246 factor = 1; /* for metadata */
247 av_frame_free(&s->last_frame_av);
248 s->last_frame_av = src = in;
249 s->last_frame_e = ef;
250 s->history[s->history_pos] = this_badness;
252 factor = (float)(s->badness_threshold - current_badness) / (new_badness - current_badness);
254 /* just duplicate the frame */
255 s->history[s->history_pos] = 0; /* frame was duplicated, thus, delta is zero */
257 res = av_frame_make_writable(s->last_frame_av);
262 blend_frame(ctx, s->last_frame_av, in, factor);
264 convert_frame(ctx, s->last_frame_av, &ef, s->skip);
265 this_badness = get_badness(&ef, &s->last_frame_e);
266 fixed_badness = current_badness + this_badness;
267 av_log(s, AV_LOG_VERBOSE, " fixed: %6d -> %6d / %6d (%3d%%) factor=%5.3f\n",
268 current_badness, fixed_badness, s->badness_threshold,
269 100 * new_badness / s->badness_threshold, factor);
270 s->last_frame_e = ef;
271 s->history[s->history_pos] = this_badness;
273 src = s->last_frame_av;
276 s->history_pos = (s->history_pos + 1) % s->nb_frames;
278 out = ff_get_video_buffer(outlink, in->width, in->height);
282 return AVERROR(ENOMEM);
284 av_frame_copy_props(out, in);
285 metadata = &out->metadata;
289 snprintf(value, sizeof(value), "%f", (float)new_badness / s->badness_threshold);
290 av_dict_set(metadata, "lavfi.photosensitivity.badness", value, 0);
292 snprintf(value, sizeof(value), "%f", (float)fixed_badness / s->badness_threshold);
293 av_dict_set(metadata, "lavfi.photosensitivity.fixed-badness", value, 0);
295 snprintf(value, sizeof(value), "%f", (float)this_badness / s->badness_threshold);
296 av_dict_set(metadata, "lavfi.photosensitivity.frame-badness", value, 0);
298 snprintf(value, sizeof(value), "%f", factor);
299 av_dict_set(metadata, "lavfi.photosensitivity.factor", value, 0);
301 av_frame_copy(out, src);
304 return ff_filter_frame(outlink, out);
307 static av_cold void uninit(AVFilterContext *ctx)
309 PhotosensitivityContext *s = ctx->priv;
311 av_frame_free(&s->last_frame_av);
314 static const AVFilterPad inputs[] = {
317 .type = AVMEDIA_TYPE_VIDEO,
318 .filter_frame = filter_frame,
319 .config_props = config_input,
324 static const AVFilterPad outputs[] = {
327 .type = AVMEDIA_TYPE_VIDEO,
332 AVFilter ff_vf_photosensitivity = {
333 .name = "photosensitivity",
334 .description = NULL_IF_CONFIG_SMALL("Filter out photosensitive epilepsy seizure-inducing flashes."),
335 .priv_size = sizeof(PhotosensitivityContext),
336 .priv_class = &photosensitivity_class,
338 .query_formats = query_formats,