2 * Copyright (c) 2018 Paul B Mahol
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
31 typedef struct DedotContext {
39 const AVPixFmtDescriptor *desc;
55 int (*dedotcrawl)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
56 int (*derainbow)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
59 static int query_formats(AVFilterContext *ctx)
61 static const enum AVPixelFormat pixel_fmts[] = {
62 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
63 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
64 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
65 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
66 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
67 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
68 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
69 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
70 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
71 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
72 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
73 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
74 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
77 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
79 return AVERROR(ENOMEM);
80 return ff_set_common_formats(ctx, formats);
83 #define DEFINE_DEDOTCRAWL(name, type, div) \
84 static int dedotcrawl##name(AVFilterContext *ctx, void *arg, \
85 int jobnr, int nb_jobs) \
87 DedotContext *s = ctx->priv; \
89 int src_linesize = s->frames[2]->linesize[0] / div; \
90 int dst_linesize = out->linesize[0] / div; \
91 int p0_linesize = s->frames[0]->linesize[0] / div; \
92 int p1_linesize = s->frames[1]->linesize[0] / div; \
93 int p3_linesize = s->frames[3]->linesize[0] / div; \
94 int p4_linesize = s->frames[4]->linesize[0] / div; \
95 const int h = s->planeheight[0]; \
96 int slice_start = (h * jobnr) / nb_jobs; \
97 int slice_end = (h * (jobnr+1)) / nb_jobs; \
98 type *p0 = (type *)s->frames[0]->data[0]; \
99 type *p1 = (type *)s->frames[1]->data[0]; \
100 type *p3 = (type *)s->frames[3]->data[0]; \
101 type *p4 = (type *)s->frames[4]->data[0]; \
102 type *src = (type *)s->frames[2]->data[0]; \
103 type *dst = (type *)out->data[0]; \
104 const int luma2d = s->luma2d; \
105 const int lumaT = s->lumaT; \
107 if (!slice_start) { \
110 p0 += p0_linesize * slice_start; \
111 p1 += p1_linesize * slice_start; \
112 p3 += p3_linesize * slice_start; \
113 p4 += p4_linesize * slice_start; \
114 src += src_linesize * slice_start; \
115 dst += dst_linesize * slice_start; \
116 if (slice_end == h) { \
119 for (int y = slice_start; y < slice_end; y++) { \
120 for (int x = 1; x < s->planewidth[0] - 1; x++) { \
121 int above = src[x - src_linesize]; \
122 int bellow = src[x + src_linesize]; \
124 int left = src[x - 1]; \
125 int right = src[x + 1]; \
127 if (FFABS(above + bellow - 2 * cur) <= luma2d && \
128 FFABS(left + right - 2 * cur) <= luma2d) \
131 if (FFABS(cur - p0[x]) <= lumaT && \
132 FFABS(cur - p4[x]) <= lumaT && \
133 FFABS(p1[x] - p3[x]) <= lumaT) { \
134 int diff1 = FFABS(cur - p1[x]); \
135 int diff2 = FFABS(cur - p3[x]); \
138 dst[x] = (src[x] + p1[x] + 1) >> 1; \
140 dst[x] = (src[x] + p3[x] + 1) >> 1; \
144 dst += dst_linesize; \
145 src += src_linesize; \
154 DEFINE_DEDOTCRAWL(8, uint8_t, 1)
155 DEFINE_DEDOTCRAWL(16, uint16_t, 2)
157 typedef struct ThreadData {
162 #define DEFINE_DERAINBOW(name, type, div) \
163 static int derainbow##name(AVFilterContext *ctx, void *arg, \
164 int jobnr, int nb_jobs) \
166 DedotContext *s = ctx->priv; \
167 ThreadData *td = arg; \
168 AVFrame *out = td->out; \
169 const int plane = td->plane; \
170 const int h = s->planeheight[plane]; \
171 int slice_start = (h * jobnr) / nb_jobs; \
172 int slice_end = (h * (jobnr+1)) / nb_jobs; \
173 int src_linesize = s->frames[2]->linesize[plane] / div; \
174 int dst_linesize = out->linesize[plane] / div; \
175 int p0_linesize = s->frames[0]->linesize[plane] / div; \
176 int p1_linesize = s->frames[1]->linesize[plane] / div; \
177 int p3_linesize = s->frames[3]->linesize[plane] / div; \
178 int p4_linesize = s->frames[4]->linesize[plane] / div; \
179 type *p0 = (type *)s->frames[0]->data[plane]; \
180 type *p1 = (type *)s->frames[1]->data[plane]; \
181 type *p3 = (type *)s->frames[3]->data[plane]; \
182 type *p4 = (type *)s->frames[4]->data[plane]; \
183 type *src = (type *)s->frames[2]->data[plane]; \
184 type *dst = (type *)out->data[plane]; \
185 const int chromaT1 = s->chromaT1; \
186 const int chromaT2 = s->chromaT2; \
188 p0 += slice_start * p0_linesize; \
189 p1 += slice_start * p1_linesize; \
190 p3 += slice_start * p3_linesize; \
191 p4 += slice_start * p4_linesize; \
192 src += slice_start * src_linesize; \
193 dst += slice_start * dst_linesize; \
194 for (int y = slice_start; y < slice_end; y++) { \
195 for (int x = 0; x < s->planewidth[plane]; x++) { \
198 if (FFABS(cur - p0[x]) <= chromaT1 && \
199 FFABS(cur - p4[x]) <= chromaT1 && \
200 FFABS(p1[x] - p3[x]) <= chromaT1 && \
201 FFABS(cur - p1[x]) > chromaT2 && \
202 FFABS(cur - p3[x]) > chromaT2) { \
203 int diff1 = FFABS(cur - p1[x]); \
204 int diff2 = FFABS(cur - p3[x]); \
207 dst[x] = (src[x] + p1[x] + 1) >> 1; \
209 dst[x] = (src[x] + p3[x] + 1) >> 1; \
213 dst += dst_linesize; \
214 src += src_linesize; \
223 DEFINE_DERAINBOW(8, uint8_t, 1)
224 DEFINE_DERAINBOW(16, uint16_t, 2)
226 static int config_output(AVFilterLink *outlink)
228 AVFilterContext *ctx = outlink->src;
229 DedotContext *s = ctx->priv;
230 AVFilterLink *inlink = ctx->inputs[0];
232 s->desc = av_pix_fmt_desc_get(outlink->format);
235 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
236 s->depth = s->desc->comp[0].depth;
237 s->max = (1 << s->depth) - 1;
238 s->luma2d = s->lt * s->max;
239 s->lumaT = s->tl * s->max;
240 s->chromaT1 = s->tc * s->max;
241 s->chromaT2 = s->ct * s->max;
243 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
244 s->planewidth[0] = s->planewidth[3] = inlink->w;
246 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
247 s->planeheight[0] = s->planeheight[3] = inlink->h;
250 s->dedotcrawl = dedotcrawl8;
251 s->derainbow = derainbow8;
253 s->dedotcrawl = dedotcrawl16;
254 s->derainbow = derainbow16;
260 static int activate(AVFilterContext *ctx)
262 AVFilterLink *inlink = ctx->inputs[0];
263 AVFilterLink *outlink = ctx->outputs[0];
264 DedotContext *s = ctx->priv;
265 AVFrame *frame = NULL;
270 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
273 ret = ff_inlink_consume_frame(inlink, &frame);
277 if (frame || s->eof_frames > 0) {
281 for (int i = 2; i < 5; i++) {
283 s->frames[i] = av_frame_clone(frame);
285 av_frame_free(&frame);
288 s->frames[4] = av_frame_clone(s->frames[3]);
296 out = av_frame_clone(s->frames[2]);
297 if (out && !ctx->is_disabled) {
298 ret = av_frame_make_writable(out);
301 ctx->internal->execute(ctx, s->dedotcrawl, out, NULL,
302 FFMIN(s->planeheight[0],
303 ff_filter_get_nb_threads(ctx)));
306 td.out = out; td.plane = 1;
307 ctx->internal->execute(ctx, s->derainbow, &td, NULL,
308 FFMIN(s->planeheight[1],
309 ff_filter_get_nb_threads(ctx)));
311 ctx->internal->execute(ctx, s->derainbow, &td, NULL,
312 FFMIN(s->planeheight[2],
313 ff_filter_get_nb_threads(ctx)));
317 ret = AVERROR(ENOMEM);
321 av_frame_free(&s->frames[0]);
322 s->frames[0] = s->frames[1];
323 s->frames[1] = s->frames[2];
324 s->frames[2] = s->frames[3];
325 s->frames[3] = s->frames[4];
331 return ff_filter_frame(outlink, out);
335 if (s->eof_frames <= 0) {
336 ff_outlink_set_status(outlink, AVERROR_EOF, s->frames[2]->pts);
338 ff_filter_set_ready(ctx, 10);
343 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
344 if (status == AVERROR_EOF) {
347 ff_filter_set_ready(ctx, 10);
352 FF_FILTER_FORWARD_WANTED(outlink, inlink);
354 return FFERROR_NOT_READY;
357 static av_cold void uninit(AVFilterContext *ctx)
359 DedotContext *s = ctx->priv;
361 for (int i = 0; i < 5; i++)
362 av_frame_free(&s->frames[i]);
365 #define OFFSET(x) offsetof(DedotContext, x)
366 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
368 static const AVOption dedot_options[] = {
369 { "m", "set filtering mode", OFFSET( m), AV_OPT_TYPE_FLAGS, {.i64=3}, 0, 3, FLAGS, "m" },
370 { "dotcrawl", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "m" },
371 { "rainbows", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "m" },
372 { "lt", "set spatial luma threshold", OFFSET(lt), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
373 { "tl", "set tolerance for temporal luma", OFFSET(tl), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
374 { "tc", "set tolerance for chroma temporal variation", OFFSET(tc), AV_OPT_TYPE_FLOAT, {.dbl=.058}, 0, 1, FLAGS },
375 { "ct", "set temporal chroma threshold", OFFSET(ct), AV_OPT_TYPE_FLOAT, {.dbl=.019}, 0, 1, FLAGS },
379 static const AVFilterPad inputs[] = {
382 .type = AVMEDIA_TYPE_VIDEO,
387 static const AVFilterPad outputs[] = {
390 .type = AVMEDIA_TYPE_VIDEO,
391 .config_props = config_output,
396 AVFILTER_DEFINE_CLASS(dedot);
398 AVFilter ff_vf_dedot = {
400 .description = NULL_IF_CONFIG_SMALL("Reduce cross-luminance and cross-color."),
401 .priv_size = sizeof(DedotContext),
402 .priv_class = &dedot_class,
403 .query_formats = query_formats,
404 .activate = activate,
408 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,