2 * Copyright (c) 2015 Niklas Haas
3 * Copyright (c) 2015 Paul B Mahol
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
30 typedef struct DebandContext {
46 int (*deband)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49 #define OFFSET(x) offsetof(DebandContext, x)
50 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 static const AVOption deband_options[] = {
53 { "1thr", "set 1st plane threshold", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
54 { "2thr", "set 2nd plane threshold", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
55 { "3thr", "set 3rd plane threshold", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
56 { "4thr", "set 4th plane threshold", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
57 { "range", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
58 { "r", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
59 { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
60 { "d", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
61 { "blur", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
65 AVFILTER_DEFINE_CLASS(deband);
67 static int query_formats(AVFilterContext *ctx)
69 static const enum AVPixelFormat pix_fmts[] = {
70 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
71 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
72 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
73 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
74 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ440P,
75 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
76 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
77 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
78 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
79 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
80 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
81 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
82 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
83 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
84 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
85 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
86 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
89 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
91 return AVERROR(ENOMEM);
93 return ff_set_common_formats(ctx, fmts_list);
96 static float frand(int x, int y)
98 const float r = sinf(x * 12.9898 + y * 78.233) * 43758.545;
100 return r - floorf(r);
103 static int inline get_avg(int ref0, int ref1, int ref2, int ref3)
105 return (ref0 + ref1 + ref2 + ref3) / 4;
108 typedef struct ThreadData {
112 static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
114 DebandContext *s = ctx->priv;
115 ThreadData *td = arg;
116 AVFrame *in = td->in;
117 AVFrame *out = td->out;
120 for (p = 0; p < s->nb_components; p++) {
121 const uint8_t *src_ptr = (const uint8_t *)in->data[p];
122 uint8_t *dst_ptr = (uint8_t *)out->data[p];
123 const int dst_linesize = out->linesize[p];
124 const int src_linesize = in->linesize[p];
125 const int thr = s->thr[p];
126 const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
127 const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
128 const int w = s->planewidth[p] - 1;
129 const int h = s->planeheight[p] - 1;
131 for (y = start; y < end; y++) {
132 const int pos = y * s->planeheight[0];
134 for (x = 0; x < s->planewidth[p]; x++) {
135 const int x_pos = s->x_pos[pos + x];
136 const int y_pos = s->y_pos[pos + x];
137 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
138 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
139 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
140 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
141 const int src0 = src_ptr[y * src_linesize + x];
144 const int avg = get_avg(ref0, ref1, ref2, ref3);
145 const int diff = FFABS(src0 - avg);
147 dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
149 dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
150 (FFABS(src0 - ref1) < thr) &&
151 (FFABS(src0 - ref2) < thr) &&
152 (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
161 static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
163 DebandContext *s = ctx->priv;
164 ThreadData *td = arg;
165 AVFrame *in = td->in;
166 AVFrame *out = td->out;
169 for (p = 0; p < s->nb_components; p++) {
170 const uint16_t *src_ptr = (const uint16_t *)in->data[p];
171 uint16_t *dst_ptr = (uint16_t *)out->data[p];
172 const int dst_linesize = out->linesize[p] / 2;
173 const int src_linesize = in->linesize[p] / 2;
174 const int thr = s->thr[p];
175 const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
176 const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
177 const int w = s->planewidth[p] - 1;
178 const int h = s->planeheight[p] - 1;
180 for (y = start; y < end; y++) {
181 const int pos = y * s->planeheight[0];
183 for (x = 0; x < s->planewidth[p]; x++) {
184 const int x_pos = s->x_pos[pos + x];
185 const int y_pos = s->y_pos[pos + x];
186 const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
187 const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
188 const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
189 const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
190 const int src0 = src_ptr[y * src_linesize + x];
193 const int avg = get_avg(ref0, ref1, ref2, ref3);
194 const int diff = FFABS(src0 - avg);
196 dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
198 dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
199 (FFABS(src0 - ref1) < thr) &&
200 (FFABS(src0 - ref2) < thr) &&
201 (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
210 static int config_input(AVFilterLink *inlink)
212 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
213 AVFilterContext *ctx = inlink->dst;
214 DebandContext *s = ctx->priv;
215 const float direction = s->direction;
216 const int range = s->range;
219 s->nb_components = desc->nb_components;
221 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
222 s->planeheight[0] = s->planeheight[3] = inlink->h;
223 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
224 s->planewidth[0] = s->planewidth[3] = inlink->w;
226 s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
228 s->thr[0] = ((1 << desc->comp[0].depth) - 1) * s->threshold[0];
229 s->thr[1] = ((1 << desc->comp[1].depth) - 1) * s->threshold[1];
230 s->thr[2] = ((1 << desc->comp[2].depth) - 1) * s->threshold[2];
231 s->thr[3] = ((1 << desc->comp[3].depth) - 1) * s->threshold[3];
233 s->x_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->x_pos));
234 s->y_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->y_pos));
235 if (!s->x_pos || !s->y_pos)
236 return AVERROR(ENOMEM);
238 for (y = 0; y < s->planeheight[0]; y++) {
239 for (x = 0; x < s->planewidth[0]; x++) {
240 const float r = frand(x, y);
241 const float dir = direction < 0 ? -direction : r * direction;
242 const int dist = range < 0 ? -range : r * range;
244 s->x_pos[y * s->planeheight[0] + x] = cosf(dir) * dist;
245 s->y_pos[y * s->planeheight[0] + x] = sinf(dir) * dist;
252 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
254 AVFilterContext *ctx = inlink->dst;
255 AVFilterLink *outlink = ctx->outputs[0];
256 DebandContext *s = ctx->priv;
260 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
263 return AVERROR(ENOMEM);
265 av_frame_copy_props(out, in);
267 td.in = in; td.out = out;
268 ctx->internal->execute(ctx, s->deband, &td, NULL, FFMIN3(s->planeheight[1],
270 ctx->graph->nb_threads));
273 return ff_filter_frame(outlink, out);
276 static av_cold void uninit(AVFilterContext *ctx)
278 DebandContext *s = ctx->priv;
284 static const AVFilterPad avfilter_vf_deband_inputs[] = {
287 .type = AVMEDIA_TYPE_VIDEO,
288 .config_props = config_input,
289 .filter_frame = filter_frame,
294 static const AVFilterPad avfilter_vf_deband_outputs[] = {
297 .type = AVMEDIA_TYPE_VIDEO,
302 AVFilter ff_vf_deband = {
304 .description = NULL_IF_CONFIG_SMALL("Debands video."),
305 .priv_size = sizeof(DebandContext),
306 .priv_class = &deband_class,
308 .query_formats = query_formats,
309 .inputs = avfilter_vf_deband_inputs,
310 .outputs = avfilter_vf_deband_outputs,
311 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,