History

avfilter/vf_deband: add planes coupling mode

Paul B Mahol committed 9 years ago

Commit fdcb7a81 parent 47cd8ef2 files · +166 −3Browse files

Unified · Split

2 files
  1. doc/filters.texi +6 −1
  2. libavfilter/vf_deband.c +160 −2
doc/filters.texi+6−1
@@ -6203,11 +6203,16 @@ absolute value will be picked. For example direction 0, -PI or -2*PI radians · 6202 unmodified lines
62036203 will pick only pixels on same row and -PI/2 will pick only pixels on same
62046204 column.
62056205
6206−@item blur
6206+@item blur, b
62076207 If enabled, current pixel is compared with average value of all four
62086208 surrounding pixels. The default is enabled. If disabled current pixel is
62096209 compared with all four surrounding pixels. The pixel is considered banded
62106210 if only all four differences with surrounding pixels are less than threshold.
6211+
6212+@item coupling, c
6213+If enabled, current pixel is changed if and only if all pixel components are banded,
6214+e.g. banding detection threshold is triggered for all color components.
6215+The default is disabled.
62116216 @end table
62126217
62136218 @anchor{decimate}
libavfilter/vf_deband.c+160−2
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 typedef struct DebandContext {
3131 const AVClass *class;
3232
33+ int coupling;
3334 float threshold[4];
3435 int range;
3536 int blur;
@@ -38,6 +39,7 @@ typedef struct DebandContext { · 2 unmodified lines
3839 int nb_components;
3940 int planewidth[4];
4041 int planeheight[4];
42+ int shift[2];
4143 int thr[4];
4244
4345 int *x_pos;
@@ -59,6 +61,9 @@ static const AVOption deband_options[] = { · 15 unmodified lines
5961 { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
6062 { "d", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
6163 { "blur", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
64+ { "b", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
65+ { "coupling", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
66+ { "c", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
6267 { NULL }
6368 };
6469
@@ -66,6 +71,8 @@ AVFILTER_DEFINE_CLASS(deband); · 1 unmodified line
6671
6772 static int query_formats(AVFilterContext *ctx)
6873 {
74+ DebandContext *s = ctx->priv;
75+
6976 static const enum AVPixelFormat pix_fmts[] = {
7077 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
7178 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
@@ -86,7 +93,21 @@ static int query_formats(AVFilterContext *ctx) · 14 unmodified lines
8693 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
8794 AV_PIX_FMT_NONE
8895 };
89− AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
96+
97+ static const enum AVPixelFormat cpix_fmts[] = {
98+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
99+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P9,
100+ AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10,
101+ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
102+ AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
103+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
104+ AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
105+ AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
106+ AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
107+ AV_PIX_FMT_NONE
108+ };
109+
110+ AVFilterFormats *fmts_list = ff_make_format_list(s->coupling ? cpix_fmts : pix_fmts);
90111 if (!fmts_list)
91112 return AVERROR(ENOMEM);
92113
@@ -158,6 +179,138 @@ static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) · 65 unmodified lines
158179 return 0;
159180 }
160181
182+static int deband_8_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
183+{
184+ DebandContext *s = ctx->priv;
185+ ThreadData *td = arg;
186+ AVFrame *in = td->in;
187+ AVFrame *out = td->out;
188+ const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
189+ const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
190+ int x, y, p;
191+
192+ for (y = start; y < end; y++) {
193+ const int pos = y * s->planewidth[0];
194+
195+ for (x = 0; x < s->planewidth[p]; x++) {
196+ const int x_pos = s->x_pos[pos + x];
197+ const int y_pos = s->y_pos[pos + x];
198+ int avg[4], cmp[4] = { 0 }, src[4];
199+
200+ for (p = 0; p < s->nb_components; p++) {
201+ const uint8_t *src_ptr = (const uint8_t *)in->data[p];
202+ const int src_linesize = in->linesize[p];
203+ const int thr = s->thr[p];
204+ const int w = s->planewidth[p] - 1;
205+ const int h = s->planeheight[p] - 1;
206+ const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
207+ const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
208+ const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
209+ const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
210+ const int src0 = src_ptr[y * src_linesize + x];
211+
212+ src[p] = src0;
213+ avg[p] = get_avg(ref0, ref1, ref2, ref3);
214+
215+ if (s->blur) {
216+ cmp[p] = FFABS(src0 - avg[p]) < thr;
217+ } else {
218+ cmp[p] = (FFABS(src0 - ref0) < thr) &&
219+ (FFABS(src0 - ref1) < thr) &&
220+ (FFABS(src0 - ref2) < thr) &&
221+ (FFABS(src0 - ref3) < thr);
222+ }
223+ }
224+
225+ for (p = 0; p < s->nb_components; p++)
226+ if (!cmp[p])
227+ break;
228+ if (p == s->nb_components) {
229+ for (p = 0; p < s->nb_components; p++) {
230+ const int dst_linesize = out->linesize[p];
231+
232+ out->data[p][y * dst_linesize + x] = avg[p];
233+ }
234+ } else {
235+ for (p = 0; p < s->nb_components; p++) {
236+ const int dst_linesize = out->linesize[p];
237+
238+ out->data[p][y * dst_linesize + x] = src[p];
239+ }
240+ }
241+ }
242+ }
243+
244+ return 0;
245+}
246+
247+static int deband_16_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
248+{
249+ DebandContext *s = ctx->priv;
250+ ThreadData *td = arg;
251+ AVFrame *in = td->in;
252+ AVFrame *out = td->out;
253+ const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
254+ const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
255+ int x, y, p, z;
256+
257+ for (y = start; y < end; y++) {
258+ const int pos = y * s->planewidth[0];
259+
260+ for (x = 0; x < s->planewidth[p]; x++) {
261+ const int x_pos = s->x_pos[pos + x];
262+ const int y_pos = s->y_pos[pos + x];
263+ int avg[4], cmp[4] = { 0 }, src[4];
264+
265+ for (p = 0; p < s->nb_components; p++) {
266+ const uint16_t *src_ptr = (const uint16_t *)in->data[p];
267+ const int src_linesize = in->linesize[p] / 2;
268+ const int thr = s->thr[p];
269+ const int w = s->planewidth[p] - 1;
270+ const int h = s->planeheight[p] - 1;
271+ const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
272+ const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
273+ const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
274+ const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
275+ const int src0 = src_ptr[y * src_linesize + x];
276+
277+ src[p] = src0;
278+ avg[p] = get_avg(ref0, ref1, ref2, ref3);
279+
280+ if (s->blur) {
281+ cmp[p] = FFABS(src0 - avg[p]) < thr;
282+ } else {
283+ cmp[p] = (FFABS(src0 - ref0) < thr) &&
284+ (FFABS(src0 - ref1) < thr) &&
285+ (FFABS(src0 - ref2) < thr) &&
286+ (FFABS(src0 - ref3) < thr);
287+ }
288+ }
289+
290+ for (z = 0; z < s->nb_components; z++)
291+ if (!cmp[z])
292+ break;
293+ if (z == s->nb_components) {
294+ for (p = 0; p < s->nb_components; p++) {
295+ const int dst_linesize = out->linesize[p] / 2;
296+ uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
297+
298+ dst[0] = avg[p];
299+ }
300+ } else {
301+ for (p = 0; p < s->nb_components; p++) {
302+ const int dst_linesize = out->linesize[p] / 2;
303+ uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
304+
305+ dst[0] = src[p];
306+ }
307+ }
308+ }
309+ }
310+
311+ return 0;
312+}
313+
161314 static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
162315 {
163316 DebandContext *s = ctx->priv;
@@ -222,8 +375,13 @@ static int config_input(AVFilterLink *inlink) · 58 unmodified lines
222375 s->planeheight[0] = s->planeheight[3] = inlink->h;
223376 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
224377 s->planewidth[0] = s->planewidth[3] = inlink->w;
378+ s->shift[0] = desc->log2_chroma_w;
379+ s->shift[1] = desc->log2_chroma_h;
225380
226− s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
381+ if (s->coupling)
382+ s->deband = desc->comp[0].depth > 8 ? deband_16_coupling_c : deband_8_coupling_c;
383+ else
384+ s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
227385
228386 s->thr[0] = ((1 << desc->comp[0].depth) - 1) * s->threshold[0];
229387 s->thr[1] = ((1 << desc->comp[1].depth) - 1) * s->threshold[1];

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t