FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_deband.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 0 222 0.0%
Functions: 0 11 0.0%
Branches: 0 118 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Niklas Haas
3 * Copyright (c) 2015 Paul B Mahol
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct DebandContext {
33 const AVClass *class;
34
35 int coupling;
36 float threshold[4];
37 int range;
38 int blur;
39 float direction;
40
41 int nb_components;
42 int planewidth[4];
43 int planeheight[4];
44 int shift[2];
45 int thr[4];
46
47 int *x_pos;
48 int *y_pos;
49
50 int (*deband)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
51 } DebandContext;
52
53 #define OFFSET(x) offsetof(DebandContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
55
56 static const AVOption deband_options[] = {
57 { "1thr", "set 1st plane threshold", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
58 { "2thr", "set 2nd plane threshold", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
59 { "3thr", "set 3rd plane threshold", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
60 { "4thr", "set 4th plane threshold", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
61 { "range", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
62 { "r", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
63 { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
64 { "d", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
65 { "blur", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
66 { "b", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
67 { "coupling", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
68 { "c", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
69 { NULL }
70 };
71
72 AVFILTER_DEFINE_CLASS(deband);
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76 DebandContext *s = ctx->priv;
77
78 static const enum AVPixelFormat pix_fmts[] = {
79 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
80 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
81 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
82 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
83 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
84 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ440P,
85 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
86 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
87 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
88 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
89 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
90 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
91 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
92 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
93 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
94 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
95 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
96 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
97 AV_PIX_FMT_NONE
98 };
99
100 static const enum AVPixelFormat cpix_fmts[] = {
101 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
102 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P9,
103 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10,
104 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
105 AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
106 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
107 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
108 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
109 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
110 AV_PIX_FMT_NONE
111 };
112
113 return ff_set_common_formats_from_list(ctx, s->coupling ? cpix_fmts : pix_fmts);
114 }
115
116 static float frand(int x, int y)
117 {
118 const float r = sinf(x * 12.9898f + y * 78.233f) * 43758.545f;
119
120 return r - floorf(r);
121 }
122
123 static int inline get_avg(int ref0, int ref1, int ref2, int ref3)
124 {
125 return (ref0 + ref1 + ref2 + ref3) / 4;
126 }
127
128 typedef struct ThreadData {
129 AVFrame *in, *out;
130 } ThreadData;
131
132 static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
133 {
134 DebandContext *s = ctx->priv;
135 ThreadData *td = arg;
136 AVFrame *in = td->in;
137 AVFrame *out = td->out;
138 int x, y, p;
139
140 for (p = 0; p < s->nb_components; p++) {
141 const uint8_t *src_ptr = (const uint8_t *)in->data[p];
142 uint8_t *dst_ptr = (uint8_t *)out->data[p];
143 const int dst_linesize = out->linesize[p];
144 const int src_linesize = in->linesize[p];
145 const int thr = s->thr[p];
146 const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
147 const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
148 const int w = s->planewidth[p] - 1;
149 const int h = s->planeheight[p] - 1;
150
151 for (y = start; y < end; y++) {
152 const int pos = y * s->planewidth[0];
153
154 for (x = 0; x < s->planewidth[p]; x++) {
155 const int x_pos = s->x_pos[pos + x];
156 const int y_pos = s->y_pos[pos + x];
157 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
158 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
159 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
160 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
161 const int src0 = src_ptr[y * src_linesize + x];
162
163 if (s->blur) {
164 const int avg = get_avg(ref0, ref1, ref2, ref3);
165 const int diff = FFABS(src0 - avg);
166
167 dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
168 } else {
169 dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
170 (FFABS(src0 - ref1) < thr) &&
171 (FFABS(src0 - ref2) < thr) &&
172 (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
173 }
174 }
175 }
176 }
177
178 return 0;
179 }
180
181 static int deband_8_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
182 {
183 DebandContext *s = ctx->priv;
184 ThreadData *td = arg;
185 AVFrame *in = td->in;
186 AVFrame *out = td->out;
187 const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
188 const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
189 int x, y, p;
190
191 for (y = start; y < end; y++) {
192 const int pos = y * s->planewidth[0];
193
194 for (x = 0; x < s->planewidth[0]; x++) {
195 const int x_pos = s->x_pos[pos + x];
196 const int y_pos = s->y_pos[pos + x];
197 int avg[4], cmp[4] = { 0 }, src[4];
198
199 for (p = 0; p < s->nb_components; p++) {
200 const uint8_t *src_ptr = (const uint8_t *)in->data[p];
201 const int src_linesize = in->linesize[p];
202 const int thr = s->thr[p];
203 const int w = s->planewidth[p] - 1;
204 const int h = s->planeheight[p] - 1;
205 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
206 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
207 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
208 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
209 const int src0 = src_ptr[y * src_linesize + x];
210
211 src[p] = src0;
212 avg[p] = get_avg(ref0, ref1, ref2, ref3);
213
214 if (s->blur) {
215 cmp[p] = FFABS(src0 - avg[p]) < thr;
216 } else {
217 cmp[p] = (FFABS(src0 - ref0) < thr) &&
218 (FFABS(src0 - ref1) < thr) &&
219 (FFABS(src0 - ref2) < thr) &&
220 (FFABS(src0 - ref3) < thr);
221 }
222 }
223
224 for (p = 0; p < s->nb_components; p++)
225 if (!cmp[p])
226 break;
227 if (p == s->nb_components) {
228 for (p = 0; p < s->nb_components; p++) {
229 const int dst_linesize = out->linesize[p];
230
231 out->data[p][y * dst_linesize + x] = avg[p];
232 }
233 } else {
234 for (p = 0; p < s->nb_components; p++) {
235 const int dst_linesize = out->linesize[p];
236
237 out->data[p][y * dst_linesize + x] = src[p];
238 }
239 }
240 }
241 }
242
243 return 0;
244 }
245
246 static int deband_16_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
247 {
248 DebandContext *s = ctx->priv;
249 ThreadData *td = arg;
250 AVFrame *in = td->in;
251 AVFrame *out = td->out;
252 const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
253 const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
254 int x, y, p, z;
255
256 for (y = start; y < end; y++) {
257 const int pos = y * s->planewidth[0];
258
259 for (x = 0; x < s->planewidth[0]; x++) {
260 const int x_pos = s->x_pos[pos + x];
261 const int y_pos = s->y_pos[pos + x];
262 int avg[4], cmp[4] = { 0 }, src[4];
263
264 for (p = 0; p < s->nb_components; p++) {
265 const uint16_t *src_ptr = (const uint16_t *)in->data[p];
266 const int src_linesize = in->linesize[p] / 2;
267 const int thr = s->thr[p];
268 const int w = s->planewidth[p] - 1;
269 const int h = s->planeheight[p] - 1;
270 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
271 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
272 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
273 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
274 const int src0 = src_ptr[y * src_linesize + x];
275
276 src[p] = src0;
277 avg[p] = get_avg(ref0, ref1, ref2, ref3);
278
279 if (s->blur) {
280 cmp[p] = FFABS(src0 - avg[p]) < thr;
281 } else {
282 cmp[p] = (FFABS(src0 - ref0) < thr) &&
283 (FFABS(src0 - ref1) < thr) &&
284 (FFABS(src0 - ref2) < thr) &&
285 (FFABS(src0 - ref3) < thr);
286 }
287 }
288
289 for (z = 0; z < s->nb_components; z++)
290 if (!cmp[z])
291 break;
292 if (z == s->nb_components) {
293 for (p = 0; p < s->nb_components; p++) {
294 const int dst_linesize = out->linesize[p] / 2;
295 uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
296
297 dst[0] = avg[p];
298 }
299 } else {
300 for (p = 0; p < s->nb_components; p++) {
301 const int dst_linesize = out->linesize[p] / 2;
302 uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
303
304 dst[0] = src[p];
305 }
306 }
307 }
308 }
309
310 return 0;
311 }
312
313 static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
314 {
315 DebandContext *s = ctx->priv;
316 ThreadData *td = arg;
317 AVFrame *in = td->in;
318 AVFrame *out = td->out;
319 int x, y, p;
320
321 for (p = 0; p < s->nb_components; p++) {
322 const uint16_t *src_ptr = (const uint16_t *)in->data[p];
323 uint16_t *dst_ptr = (uint16_t *)out->data[p];
324 const int dst_linesize = out->linesize[p] / 2;
325 const int src_linesize = in->linesize[p] / 2;
326 const int thr = s->thr[p];
327 const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
328 const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
329 const int w = s->planewidth[p] - 1;
330 const int h = s->planeheight[p] - 1;
331
332 for (y = start; y < end; y++) {
333 const int pos = y * s->planewidth[0];
334
335 for (x = 0; x < s->planewidth[p]; x++) {
336 const int x_pos = s->x_pos[pos + x];
337 const int y_pos = s->y_pos[pos + x];
338 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
339 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
340 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
341 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
342 const int src0 = src_ptr[y * src_linesize + x];
343
344 if (s->blur) {
345 const int avg = get_avg(ref0, ref1, ref2, ref3);
346 const int diff = FFABS(src0 - avg);
347
348 dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
349 } else {
350 dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
351 (FFABS(src0 - ref1) < thr) &&
352 (FFABS(src0 - ref2) < thr) &&
353 (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
354 }
355 }
356 }
357 }
358
359 return 0;
360 }
361
362 static int config_input(AVFilterLink *inlink)
363 {
364 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
365 AVFilterContext *ctx = inlink->dst;
366 DebandContext *s = ctx->priv;
367 const float direction = s->direction;
368 const int range = s->range;
369 int x, y;
370
371 s->nb_components = desc->nb_components;
372
373 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
374 s->planeheight[0] = s->planeheight[3] = inlink->h;
375 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
376 s->planewidth[0] = s->planewidth[3] = inlink->w;
377 s->shift[0] = desc->log2_chroma_w;
378 s->shift[1] = desc->log2_chroma_h;
379
380 if (s->coupling)
381 s->deband = desc->comp[0].depth > 8 ? deband_16_coupling_c : deband_8_coupling_c;
382 else
383 s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
384
385 s->thr[0] = ((1 << desc->comp[0].depth) - 1) * s->threshold[0];
386 s->thr[1] = ((1 << desc->comp[1].depth) - 1) * s->threshold[1];
387 s->thr[2] = ((1 << desc->comp[2].depth) - 1) * s->threshold[2];
388 s->thr[3] = ((1 << desc->comp[3].depth) - 1) * s->threshold[3];
389
390 if (!s->x_pos)
391 s->x_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->x_pos));
392 if (!s->y_pos)
393 s->y_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->y_pos));
394 if (!s->x_pos || !s->y_pos)
395 return AVERROR(ENOMEM);
396
397 for (y = 0; y < s->planeheight[0]; y++) {
398 for (x = 0; x < s->planewidth[0]; x++) {
399 const float r = frand(x, y);
400 const float dir = direction < 0 ? -direction : r * direction;
401 const int dist = range < 0 ? -range : r * range;
402
403 s->x_pos[y * s->planewidth[0] + x] = cosf(dir) * dist;
404 s->y_pos[y * s->planewidth[0] + x] = sinf(dir) * dist;
405 }
406 }
407
408 return 0;
409 }
410
411 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
412 {
413 AVFilterContext *ctx = inlink->dst;
414 AVFilterLink *outlink = ctx->outputs[0];
415 DebandContext *s = ctx->priv;
416 AVFrame *out;
417 ThreadData td;
418
419 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
420 if (!out) {
421 av_frame_free(&in);
422 return AVERROR(ENOMEM);
423 }
424 av_frame_copy_props(out, in);
425
426 td.in = in; td.out = out;
427 ff_filter_execute(ctx, s->deband, &td, NULL,
428 FFMIN3(s->planeheight[1], s->planeheight[2],
429 ff_filter_get_nb_threads(ctx)));
430
431 av_frame_free(&in);
432 return ff_filter_frame(outlink, out);
433 }
434
435 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
436 char *res, int res_len, int flags)
437 {
438 int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
439
440 if (ret < 0)
441 return ret;
442
443 return config_input(ctx->inputs[0]);
444 }
445
446 static av_cold void uninit(AVFilterContext *ctx)
447 {
448 DebandContext *s = ctx->priv;
449
450 av_freep(&s->x_pos);
451 av_freep(&s->y_pos);
452 }
453
454 static const AVFilterPad avfilter_vf_deband_inputs[] = {
455 {
456 .name = "default",
457 .type = AVMEDIA_TYPE_VIDEO,
458 .config_props = config_input,
459 .filter_frame = filter_frame,
460 },
461 };
462
463 const AVFilter ff_vf_deband = {
464 .name = "deband",
465 .description = NULL_IF_CONFIG_SMALL("Debands video."),
466 .priv_size = sizeof(DebandContext),
467 .priv_class = &deband_class,
468 .uninit = uninit,
469 FILTER_INPUTS(avfilter_vf_deband_inputs),
470 FILTER_OUTPUTS(ff_video_default_filterpad),
471 FILTER_QUERY_FUNC(query_formats),
472 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
473 .process_command = process_command,
474 };
475