2 * Copyright (c) 2020 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/avstring.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
31 typedef struct ChromaNRContext {
55 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
58 static int query_formats(AVFilterContext *ctx)
60 static const enum AVPixelFormat pix_fmts[] = {
61 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV444P,
62 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
63 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
64 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
65 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV444P10,
66 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV420P12,
67 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
68 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
69 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
70 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
71 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
72 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
76 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
78 return AVERROR(ENOMEM);
79 return ff_set_common_formats(ctx, fmts_list);
82 #define FILTER_FUNC(name, type) \
83 static int filter_slice##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
85 ChromaNRContext *s = ctx->priv; \
87 AVFrame *out = s->out; \
88 const int in_ylinesize = in->linesize[0]; \
89 const int in_ulinesize = in->linesize[1]; \
90 const int in_vlinesize = in->linesize[2]; \
91 const int out_ulinesize = out->linesize[1]; \
92 const int out_vlinesize = out->linesize[2]; \
93 const int chroma_w = s->chroma_w; \
94 const int chroma_h = s->chroma_h; \
95 const int stepw = s->stepw; \
96 const int steph = s->steph; \
97 const int sizew = s->sizew; \
98 const int sizeh = s->sizeh; \
99 const int thres = s->thres; \
100 const int thres_y = s->thres_y; \
101 const int thres_u = s->thres_u; \
102 const int thres_v = s->thres_v; \
103 const int h = s->planeheight[1]; \
104 const int w = s->planewidth[1]; \
105 const int slice_start = (h * jobnr) / nb_jobs; \
106 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
107 type *out_uptr = (type *)(out->data[1] + slice_start * out_ulinesize); \
108 type *out_vptr = (type *)(out->data[2] + slice_start * out_vlinesize); \
111 const int h = s->planeheight[0]; \
112 const int slice_start = (h * jobnr) / nb_jobs; \
113 const int slice_end = (h * (jobnr+1)) / nb_jobs; \
115 av_image_copy_plane(out->data[0] + slice_start * out->linesize[0], \
117 in->data[0] + slice_start * in->linesize[0], \
119 s->linesize[0], slice_end - slice_start); \
121 if (s->nb_planes == 4) { \
122 av_image_copy_plane(out->data[3] + slice_start * out->linesize[3], \
124 in->data[3] + slice_start * in->linesize[3], \
126 s->linesize[3], slice_end - slice_start); \
130 for (int y = slice_start; y < slice_end; y++) { \
131 const type *in_yptr = (const type *)(in->data[0] + y * chroma_h * in_ylinesize); \
132 const type *in_uptr = (const type *)(in->data[1] + y * in_ulinesize); \
133 const type *in_vptr = (const type *)(in->data[2] + y * in_vlinesize); \
135 for (int x = 0; x < w; x++) { \
136 const int cy = in_yptr[x * chroma_w]; \
137 const int cu = in_uptr[x]; \
138 const int cv = in_vptr[x]; \
143 for (int yy = FFMAX(0, y - sizeh); yy < FFMIN(y + sizeh, h); yy += steph) { \
144 const type *in_yptr = (const type *)(in->data[0] + yy * chroma_h * in_ylinesize); \
145 const type *in_uptr = (const type *)(in->data[1] + yy * in_ulinesize); \
146 const type *in_vptr = (const type *)(in->data[2] + yy * in_vlinesize); \
148 for (int xx = FFMAX(0, x - sizew); xx < FFMIN(x + sizew, w); xx += stepw) { \
149 const int Y = in_yptr[xx * chroma_w]; \
150 const int U = in_uptr[xx]; \
151 const int V = in_vptr[xx]; \
153 if (FFABS(cu - U) + FFABS(cv - V) + FFABS(cy - Y) < thres && \
154 FFABS(cu - U) < thres_u && FFABS(cv - V) < thres_v && \
155 FFABS(cy - Y) < thres_y && \
156 xx != x && yy != y) { \
164 out_uptr[x] = su / cn; \
165 out_vptr[x] = sv / cn; \
168 out_uptr += out_ulinesize / sizeof(type); \
169 out_vptr += out_vlinesize / sizeof(type); \
175 FILTER_FUNC(8, uint8_t)
176 FILTER_FUNC(16, uint16_t)
178 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
180 AVFilterContext *ctx = inlink->dst;
181 AVFilterLink *outlink = ctx->outputs[0];
182 ChromaNRContext *s = ctx->priv;
185 s->thres = s->threshold * (1 << (s->depth - 8));
186 s->thres_y = s->threshold_y * (1 << (s->depth - 8));
187 s->thres_u = s->threshold_u * (1 << (s->depth - 8));
188 s->thres_v = s->threshold_v * (1 << (s->depth - 8));
190 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
193 return AVERROR(ENOMEM);
196 av_frame_copy_props(out, in);
198 ctx->internal->execute(ctx, s->filter_slice, in, NULL,
199 FFMIN3(s->planeheight[1],
201 ff_filter_get_nb_threads(ctx)));
204 return ff_filter_frame(outlink, out);
207 static int config_input(AVFilterLink *inlink)
209 AVFilterContext *ctx = inlink->dst;
210 ChromaNRContext *s = ctx->priv;
211 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
214 s->nb_planes = desc->nb_components;
215 s->depth = desc->comp[0].depth;
216 s->filter_slice = s->depth <= 8 ? filter_slice8 : filter_slice16;
217 s->chroma_w = 1 << desc->log2_chroma_w;
218 s->chroma_h = 1 << desc->log2_chroma_h;
219 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
220 s->planeheight[0] = s->planeheight[3] = inlink->h;
221 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
222 s->planewidth[0] = s->planewidth[3] = inlink->w;
224 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
230 #define OFFSET(x) offsetof(ChromaNRContext, x)
231 #define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
233 static const AVOption chromanr_options[] = {
234 { "thres", "set y+u+v threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=30}, 1, 200, VF },
235 { "sizew", "set horizontal size", OFFSET(sizew), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
236 { "sizeh", "set vertical size", OFFSET(sizeh), AV_OPT_TYPE_INT, {.i64=5}, 1, 100, VF },
237 { "stepw", "set horizontal step", OFFSET(stepw), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
238 { "steph", "set vertical step", OFFSET(steph), AV_OPT_TYPE_INT, {.i64=1}, 1, 50, VF },
239 { "threy", "set y threshold", OFFSET(threshold_y), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
240 { "threu", "set u threshold", OFFSET(threshold_u), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
241 { "threv", "set v threshold", OFFSET(threshold_v), AV_OPT_TYPE_FLOAT, {.dbl=200},1, 200, VF },
245 static const AVFilterPad inputs[] = {
248 .type = AVMEDIA_TYPE_VIDEO,
249 .filter_frame = filter_frame,
250 .config_props = config_input,
255 static const AVFilterPad outputs[] = {
258 .type = AVMEDIA_TYPE_VIDEO,
263 AVFILTER_DEFINE_CLASS(chromanr);
265 const AVFilter ff_vf_chromanr = {
267 .description = NULL_IF_CONFIG_SMALL("Reduce chrominance noise."),
268 .priv_size = sizeof(ChromaNRContext),
269 .priv_class = &chromanr_class,
270 .query_formats = query_formats,
273 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
274 .process_command = ff_filter_process_command,