FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_photosensitivity.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 0 131 0.0%
Functions: 0 8 0.0%
Branches: 0 48 0.0%

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