• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018 Paul B Mahol
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 "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 
25 #include "avfilter.h"
26 #include "filters.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct DedotContext {
32     const AVClass *class;
33     int m;
34     float lt;
35     float tl;
36     float tc;
37     float ct;
38 
39     const AVPixFmtDescriptor *desc;
40     int depth;
41     int max;
42     int luma2d;
43     int lumaT;
44     int chromaT1;
45     int chromaT2;
46 
47     int eof;
48     int eof_frames;
49     int nb_planes;
50     int planewidth[4];
51     int planeheight[4];
52 
53     AVFrame *frames[5];
54 
55     int (*dedotcrawl)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
56     int (*derainbow)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
57 } DedotContext;
58 
59 static const enum AVPixelFormat pixel_fmts[] = {
60     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
61     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
62     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
63     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
64     AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
65     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
66     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
67     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
68     AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
69     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
70     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
71     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
72     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
73     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
74     AV_PIX_FMT_NONE
75 };
76 
77 #define DEFINE_DEDOTCRAWL(name, type, div)                       \
78 static int dedotcrawl##name(AVFilterContext *ctx, void *arg,     \
79                             int jobnr, int nb_jobs)              \
80 {                                                                \
81     DedotContext *s = ctx->priv;                                 \
82     AVFrame *out = arg;                                          \
83     int src_linesize = s->frames[2]->linesize[0] / div;          \
84     int dst_linesize = out->linesize[0] / div;                   \
85     int p0_linesize = s->frames[0]->linesize[0] / div;           \
86     int p1_linesize = s->frames[1]->linesize[0] / div;           \
87     int p3_linesize = s->frames[3]->linesize[0] / div;           \
88     int p4_linesize = s->frames[4]->linesize[0] / div;           \
89     const int h = s->planeheight[0];                             \
90     int slice_start = (h * jobnr) / nb_jobs;                     \
91     int slice_end = (h * (jobnr+1)) / nb_jobs;                   \
92     type *p0 = (type *)s->frames[0]->data[0];                    \
93     type *p1 = (type *)s->frames[1]->data[0];                    \
94     type *p3 = (type *)s->frames[3]->data[0];                    \
95     type *p4 = (type *)s->frames[4]->data[0];                    \
96     type *src = (type *)s->frames[2]->data[0];                   \
97     type *dst = (type *)out->data[0];                            \
98     const int luma2d = s->luma2d;                                \
99     const int lumaT = s->lumaT;                                  \
100                                                                  \
101     if (!slice_start) {                                          \
102         slice_start++;                                           \
103     }                                                            \
104     p0 += p0_linesize * slice_start;                             \
105     p1 += p1_linesize * slice_start;                             \
106     p3 += p3_linesize * slice_start;                             \
107     p4 += p4_linesize * slice_start;                             \
108     src += src_linesize * slice_start;                           \
109     dst += dst_linesize * slice_start;                           \
110     if (slice_end == h) {                                        \
111         slice_end--;                                             \
112     }                                                            \
113     for (int y = slice_start; y < slice_end; y++) {              \
114         for (int x = 1; x < s->planewidth[0] - 1; x++) {         \
115             int above = src[x - src_linesize];                   \
116             int bellow = src[x + src_linesize];                  \
117             int cur = src[x];                                    \
118             int left = src[x - 1];                               \
119             int right = src[x + 1];                              \
120                                                                  \
121             if (FFABS(above + bellow - 2 * cur) <= luma2d &&     \
122                 FFABS(left + right - 2 * cur) <= luma2d)         \
123                 continue;                                        \
124                                                                  \
125             if (FFABS(cur - p0[x]) <= lumaT &&                   \
126                 FFABS(cur - p4[x]) <= lumaT &&                   \
127                 FFABS(p1[x] - p3[x]) <= lumaT) {                 \
128                 int diff1 = FFABS(cur - p1[x]);                  \
129                 int diff2 = FFABS(cur - p3[x]);                  \
130                                                                  \
131                 if (diff1 < diff2)                               \
132                     dst[x] = (src[x] + p1[x] + 1) >> 1;          \
133                 else                                             \
134                     dst[x] = (src[x] + p3[x] + 1) >> 1;          \
135             }                                                    \
136         }                                                        \
137                                                                  \
138         dst += dst_linesize;                                     \
139         src += src_linesize;                                     \
140         p0 += p0_linesize;                                       \
141         p1 += p1_linesize;                                       \
142         p3 += p3_linesize;                                       \
143         p4 += p4_linesize;                                       \
144     }                                                            \
145     return 0;                                                    \
146 }
147 
148 DEFINE_DEDOTCRAWL(8, uint8_t, 1)
149 DEFINE_DEDOTCRAWL(16, uint16_t, 2)
150 
151 typedef struct ThreadData {
152     AVFrame *out;
153     int plane;
154 } ThreadData;
155 
156 #define DEFINE_DERAINBOW(name, type, div)                    \
157 static int derainbow##name(AVFilterContext *ctx, void *arg,  \
158                            int jobnr, int nb_jobs)           \
159 {                                                            \
160     DedotContext *s = ctx->priv;                             \
161     ThreadData *td = arg;                                    \
162     AVFrame *out = td->out;                                  \
163     const int plane = td->plane;                             \
164     const int h = s->planeheight[plane];                     \
165     int slice_start = (h * jobnr) / nb_jobs;                 \
166     int slice_end = (h * (jobnr+1)) / nb_jobs;               \
167     int src_linesize = s->frames[2]->linesize[plane] / div;  \
168     int dst_linesize = out->linesize[plane] / div;           \
169     int p0_linesize = s->frames[0]->linesize[plane] / div;   \
170     int p1_linesize = s->frames[1]->linesize[plane] / div;   \
171     int p3_linesize = s->frames[3]->linesize[plane] / div;   \
172     int p4_linesize = s->frames[4]->linesize[plane] / div;   \
173     type *p0 = (type *)s->frames[0]->data[plane];            \
174     type *p1 = (type *)s->frames[1]->data[plane];            \
175     type *p3 = (type *)s->frames[3]->data[plane];            \
176     type *p4 = (type *)s->frames[4]->data[plane];            \
177     type *src = (type *)s->frames[2]->data[plane];           \
178     type *dst = (type *)out->data[plane];                    \
179     const int chromaT1 = s->chromaT1;                        \
180     const int chromaT2 = s->chromaT2;                        \
181                                                              \
182     p0 += slice_start * p0_linesize;                         \
183     p1 += slice_start * p1_linesize;                         \
184     p3 += slice_start * p3_linesize;                         \
185     p4 += slice_start * p4_linesize;                         \
186     src += slice_start * src_linesize;                       \
187     dst += slice_start * dst_linesize;                       \
188     for (int y = slice_start; y < slice_end; y++) {          \
189         for (int x = 0; x < s->planewidth[plane]; x++) {     \
190             int cur = src[x];                                \
191                                                              \
192             if (FFABS(cur - p0[x]) <= chromaT1 &&            \
193                 FFABS(cur - p4[x]) <= chromaT1 &&            \
194                 FFABS(p1[x] - p3[x]) <= chromaT1 &&          \
195                 FFABS(cur - p1[x]) > chromaT2 &&             \
196                 FFABS(cur - p3[x]) > chromaT2) {             \
197                 int diff1 = FFABS(cur - p1[x]);              \
198                 int diff2 = FFABS(cur - p3[x]);              \
199                                                              \
200                 if (diff1 < diff2)                           \
201                     dst[x] = (src[x] + p1[x] + 1) >> 1;      \
202                 else                                         \
203                     dst[x] = (src[x] + p3[x] + 1) >> 1;      \
204             }                                                \
205         }                                                    \
206                                                              \
207         dst += dst_linesize;                                 \
208         src += src_linesize;                                 \
209         p0 += p0_linesize;                                   \
210         p1 += p1_linesize;                                   \
211         p3 += p3_linesize;                                   \
212         p4 += p4_linesize;                                   \
213     }                                                        \
214     return 0;                                                \
215 }
216 
217 DEFINE_DERAINBOW(8, uint8_t, 1)
218 DEFINE_DERAINBOW(16, uint16_t, 2)
219 
config_output(AVFilterLink * outlink)220 static int config_output(AVFilterLink *outlink)
221 {
222     AVFilterContext *ctx = outlink->src;
223     DedotContext *s = ctx->priv;
224     AVFilterLink *inlink = ctx->inputs[0];
225 
226     s->desc = av_pix_fmt_desc_get(outlink->format);
227     if (!s->desc)
228         return AVERROR_BUG;
229     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
230     s->depth = s->desc->comp[0].depth;
231     s->max = (1 << s->depth) - 1;
232     s->luma2d = s->lt * s->max;
233     s->lumaT = s->tl * s->max;
234     s->chromaT1 = s->tc * s->max;
235     s->chromaT2 = s->ct * s->max;
236 
237     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
238     s->planewidth[0] = s->planewidth[3] = inlink->w;
239 
240     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
241     s->planeheight[0] = s->planeheight[3] = inlink->h;
242 
243     if (s->depth <= 8) {
244         s->dedotcrawl = dedotcrawl8;
245         s->derainbow = derainbow8;
246     } else {
247         s->dedotcrawl = dedotcrawl16;
248         s->derainbow = derainbow16;
249     }
250 
251     return 0;
252 }
253 
activate(AVFilterContext * ctx)254 static int activate(AVFilterContext *ctx)
255 {
256     AVFilterLink *inlink = ctx->inputs[0];
257     AVFilterLink *outlink = ctx->outputs[0];
258     DedotContext *s = ctx->priv;
259     AVFrame *frame = NULL;
260     int64_t pts;
261     int status;
262     int ret = 0;
263 
264     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
265 
266     if (s->eof == 0) {
267         ret = ff_inlink_consume_frame(inlink, &frame);
268         if (ret < 0)
269             return ret;
270     }
271     if (frame || s->eof_frames > 0) {
272         AVFrame *out = NULL;
273 
274         if (frame) {
275             for (int i = 2; i < 5; i++) {
276                 if (!s->frames[i])
277                     s->frames[i] = av_frame_clone(frame);
278             }
279             av_frame_free(&frame);
280         } else if (s->frames[3]) {
281             s->eof_frames--;
282             s->frames[4] = av_frame_clone(s->frames[3]);
283         }
284 
285         if (s->frames[0] &&
286             s->frames[1] &&
287             s->frames[2] &&
288             s->frames[3] &&
289             s->frames[4]) {
290             out = av_frame_clone(s->frames[2]);
291             if (out && !ctx->is_disabled) {
292                 ret = av_frame_make_writable(out);
293                 if (ret >= 0) {
294                     if (s->m & 1)
295                         ff_filter_execute(ctx, s->dedotcrawl, out, NULL,
296                                           FFMIN(ff_filter_get_nb_threads(ctx),
297                                                 s->planeheight[0]));
298                     if (s->m & 2) {
299                         ThreadData td;
300                         td.out = out; td.plane = 1;
301                         ff_filter_execute(ctx, s->derainbow, &td, NULL,
302                                           FFMIN(ff_filter_get_nb_threads(ctx),
303                                                 s->planeheight[1]));
304                         td.plane = 2;
305                         ff_filter_execute(ctx, s->derainbow, &td, NULL,
306                                           FFMIN(ff_filter_get_nb_threads(ctx),
307                                                 s->planeheight[2]));
308                     }
309                 } else
310                     av_frame_free(&out);
311             } else if (!out) {
312                 ret = AVERROR(ENOMEM);
313             }
314         }
315 
316         av_frame_free(&s->frames[0]);
317         s->frames[0] = s->frames[1];
318         s->frames[1] = s->frames[2];
319         s->frames[2] = s->frames[3];
320         s->frames[3] = s->frames[4];
321         s->frames[4] = NULL;
322 
323         if (ret < 0)
324             return ret;
325         if (out)
326             return ff_filter_frame(outlink, out);
327     }
328 
329     if (s->eof) {
330         if (s->eof_frames <= 0) {
331             ff_outlink_set_status(outlink, AVERROR_EOF, s->frames[2]->pts);
332         } else {
333             ff_filter_set_ready(ctx, 10);
334         }
335         return 0;
336     }
337 
338     if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
339         if (status == AVERROR_EOF) {
340             s->eof = 1;
341             s->eof_frames = !!s->frames[0] + !!s->frames[1];
342             if (s->eof_frames <= 0) {
343                 ff_outlink_set_status(outlink, AVERROR_EOF, pts);
344                 return 0;
345             }
346             ff_filter_set_ready(ctx, 10);
347             return 0;
348         }
349     }
350 
351     FF_FILTER_FORWARD_WANTED(outlink, inlink);
352 
353     return FFERROR_NOT_READY;
354 }
355 
uninit(AVFilterContext * ctx)356 static av_cold void uninit(AVFilterContext *ctx)
357 {
358     DedotContext *s = ctx->priv;
359 
360     for (int i = 0; i < 5; i++)
361         av_frame_free(&s->frames[i]);
362 }
363 
364 #define OFFSET(x) offsetof(DedotContext, x)
365 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
366 
367 static const AVOption dedot_options[] = {
368     { "m",   "set filtering mode",                          OFFSET( m), AV_OPT_TYPE_FLAGS, {.i64=3},    0, 3, FLAGS, "m" },
369     { "dotcrawl",                                           0,       0, AV_OPT_TYPE_CONST, {.i64=1},    0, 0, FLAGS, "m" },
370     { "rainbows",                                           0,       0, AV_OPT_TYPE_CONST, {.i64=2},    0, 0, FLAGS, "m" },
371     { "lt",  "set spatial luma threshold",                  OFFSET(lt), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
372     { "tl",  "set tolerance for temporal luma",             OFFSET(tl), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
373     { "tc",  "set tolerance for chroma temporal variation", OFFSET(tc), AV_OPT_TYPE_FLOAT, {.dbl=.058}, 0, 1, FLAGS },
374     { "ct",  "set temporal chroma threshold",               OFFSET(ct), AV_OPT_TYPE_FLOAT, {.dbl=.019}, 0, 1, FLAGS },
375     { NULL },
376 };
377 
378 static const AVFilterPad inputs[] = {
379     {
380         .name           = "default",
381         .type           = AVMEDIA_TYPE_VIDEO,
382     },
383 };
384 
385 static const AVFilterPad outputs[] = {
386     {
387         .name          = "default",
388         .type          = AVMEDIA_TYPE_VIDEO,
389         .config_props  = config_output,
390     },
391 };
392 
393 AVFILTER_DEFINE_CLASS(dedot);
394 
395 const AVFilter ff_vf_dedot = {
396     .name          = "dedot",
397     .description   = NULL_IF_CONFIG_SMALL("Reduce cross-luminance and cross-color."),
398     .priv_size     = sizeof(DedotContext),
399     .priv_class    = &dedot_class,
400     .activate      = activate,
401     .uninit        = uninit,
402     FILTER_INPUTS(inputs),
403     FILTER_OUTPUTS(outputs),
404     FILTER_PIXFMTS_ARRAY(pixel_fmts),
405     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
406 };
407