yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2010 Nolan Lum <nol888@gmail.com>
|
yading@10
|
3 * Copyright (c) 2009 Loren Merritt <lorenm@u.washignton.edu>
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * gradfun debanding filter, ported from MPlayer
|
yading@10
|
25 * libmpcodecs/vf_gradfun.c
|
yading@10
|
26 *
|
yading@10
|
27 * Apply a boxblur debanding algorithm (based on the gradfun2db
|
yading@10
|
28 * Avisynth filter by prunedtree).
|
yading@10
|
29 * Foreach pixel, if it's within threshold of the blurred value, make it closer.
|
yading@10
|
30 * So now we have a smoothed and higher bitdepth version of all the shallow
|
yading@10
|
31 * gradients, while leaving detailed areas untouched.
|
yading@10
|
32 * Dither it back to 8bit.
|
yading@10
|
33 */
|
yading@10
|
34
|
yading@10
|
35 #include "libavutil/imgutils.h"
|
yading@10
|
36 #include "libavutil/common.h"
|
yading@10
|
37 #include "libavutil/cpu.h"
|
yading@10
|
38 #include "libavutil/opt.h"
|
yading@10
|
39 #include "libavutil/pixdesc.h"
|
yading@10
|
40 #include "libavutil/opt.h"
|
yading@10
|
41 #include "avfilter.h"
|
yading@10
|
42 #include "formats.h"
|
yading@10
|
43 #include "gradfun.h"
|
yading@10
|
44 #include "internal.h"
|
yading@10
|
45 #include "video.h"
|
yading@10
|
46
|
yading@10
|
47 DECLARE_ALIGNED(16, static const uint16_t, dither)[8][8] = {
|
yading@10
|
48 {0x00,0x60,0x18,0x78,0x06,0x66,0x1E,0x7E},
|
yading@10
|
49 {0x40,0x20,0x58,0x38,0x46,0x26,0x5E,0x3E},
|
yading@10
|
50 {0x10,0x70,0x08,0x68,0x16,0x76,0x0E,0x6E},
|
yading@10
|
51 {0x50,0x30,0x48,0x28,0x56,0x36,0x4E,0x2E},
|
yading@10
|
52 {0x04,0x64,0x1C,0x7C,0x02,0x62,0x1A,0x7A},
|
yading@10
|
53 {0x44,0x24,0x5C,0x3C,0x42,0x22,0x5A,0x3A},
|
yading@10
|
54 {0x14,0x74,0x0C,0x6C,0x12,0x72,0x0A,0x6A},
|
yading@10
|
55 {0x54,0x34,0x4C,0x2C,0x52,0x32,0x4A,0x2A},
|
yading@10
|
56 };
|
yading@10
|
57
|
yading@10
|
58 void ff_gradfun_filter_line_c(uint8_t *dst, const uint8_t *src, const uint16_t *dc, int width, int thresh, const uint16_t *dithers)
|
yading@10
|
59 {
|
yading@10
|
60 int x;
|
yading@10
|
61 for (x = 0; x < width; dc += x & 1, x++) {
|
yading@10
|
62 int pix = src[x] << 7;
|
yading@10
|
63 int delta = dc[0] - pix;
|
yading@10
|
64 int m = abs(delta) * thresh >> 16;
|
yading@10
|
65 m = FFMAX(0, 127 - m);
|
yading@10
|
66 m = m * m * delta >> 14;
|
yading@10
|
67 pix += m + dithers[x & 7];
|
yading@10
|
68 dst[x] = av_clip_uint8(pix >> 7);
|
yading@10
|
69 }
|
yading@10
|
70 }
|
yading@10
|
71
|
yading@10
|
72 void ff_gradfun_blur_line_c(uint16_t *dc, uint16_t *buf, const uint16_t *buf1, const uint8_t *src, int src_linesize, int width)
|
yading@10
|
73 {
|
yading@10
|
74 int x, v, old;
|
yading@10
|
75 for (x = 0; x < width; x++) {
|
yading@10
|
76 v = buf1[x] + src[2 * x] + src[2 * x + 1] + src[2 * x + src_linesize] + src[2 * x + 1 + src_linesize];
|
yading@10
|
77 old = buf[x];
|
yading@10
|
78 buf[x] = v;
|
yading@10
|
79 dc[x] = v - old;
|
yading@10
|
80 }
|
yading@10
|
81 }
|
yading@10
|
82
|
yading@10
|
83 static void filter(GradFunContext *ctx, uint8_t *dst, const uint8_t *src, int width, int height, int dst_linesize, int src_linesize, int r)
|
yading@10
|
84 {
|
yading@10
|
85 int bstride = FFALIGN(width, 16) / 2;
|
yading@10
|
86 int y;
|
yading@10
|
87 uint32_t dc_factor = (1 << 21) / (r * r);
|
yading@10
|
88 uint16_t *dc = ctx->buf + 16;
|
yading@10
|
89 uint16_t *buf = ctx->buf + bstride + 32;
|
yading@10
|
90 int thresh = ctx->thresh;
|
yading@10
|
91
|
yading@10
|
92 memset(dc, 0, (bstride + 16) * sizeof(*buf));
|
yading@10
|
93 for (y = 0; y < r; y++)
|
yading@10
|
94 ctx->blur_line(dc, buf + y * bstride, buf + (y - 1) * bstride, src + 2 * y * src_linesize, src_linesize, width / 2);
|
yading@10
|
95 for (;;) {
|
yading@10
|
96 if (y < height - r) {
|
yading@10
|
97 int mod = ((y + r) / 2) % r;
|
yading@10
|
98 uint16_t *buf0 = buf + mod * bstride;
|
yading@10
|
99 uint16_t *buf1 = buf + (mod ? mod - 1 : r - 1) * bstride;
|
yading@10
|
100 int x, v;
|
yading@10
|
101 ctx->blur_line(dc, buf0, buf1, src + (y + r) * src_linesize, src_linesize, width / 2);
|
yading@10
|
102 for (x = v = 0; x < r; x++)
|
yading@10
|
103 v += dc[x];
|
yading@10
|
104 for (; x < width / 2; x++) {
|
yading@10
|
105 v += dc[x] - dc[x-r];
|
yading@10
|
106 dc[x-r] = v * dc_factor >> 16;
|
yading@10
|
107 }
|
yading@10
|
108 for (; x < (width + r + 1) / 2; x++)
|
yading@10
|
109 dc[x-r] = v * dc_factor >> 16;
|
yading@10
|
110 for (x = -r / 2; x < 0; x++)
|
yading@10
|
111 dc[x] = dc[0];
|
yading@10
|
112 }
|
yading@10
|
113 if (y == r) {
|
yading@10
|
114 for (y = 0; y < r; y++)
|
yading@10
|
115 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
|
yading@10
|
116 }
|
yading@10
|
117 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
|
yading@10
|
118 if (++y >= height) break;
|
yading@10
|
119 ctx->filter_line(dst + y * dst_linesize, src + y * src_linesize, dc - r / 2, width, thresh, dither[y & 7]);
|
yading@10
|
120 if (++y >= height) break;
|
yading@10
|
121 }
|
yading@10
|
122 }
|
yading@10
|
123
|
yading@10
|
124 static av_cold int init(AVFilterContext *ctx)
|
yading@10
|
125 {
|
yading@10
|
126 GradFunContext *gf = ctx->priv;
|
yading@10
|
127
|
yading@10
|
128 gf->thresh = (1 << 15) / gf->strength;
|
yading@10
|
129 gf->radius = av_clip((gf->radius + 1) & ~1, 4, 32);
|
yading@10
|
130
|
yading@10
|
131 gf->blur_line = ff_gradfun_blur_line_c;
|
yading@10
|
132 gf->filter_line = ff_gradfun_filter_line_c;
|
yading@10
|
133
|
yading@10
|
134 if (ARCH_X86)
|
yading@10
|
135 ff_gradfun_init_x86(gf);
|
yading@10
|
136
|
yading@10
|
137 av_log(ctx, AV_LOG_VERBOSE, "threshold:%.2f radius:%d\n", gf->strength, gf->radius);
|
yading@10
|
138
|
yading@10
|
139 return 0;
|
yading@10
|
140 }
|
yading@10
|
141
|
yading@10
|
142 static av_cold void uninit(AVFilterContext *ctx)
|
yading@10
|
143 {
|
yading@10
|
144 GradFunContext *gf = ctx->priv;
|
yading@10
|
145 av_freep(&gf->buf);
|
yading@10
|
146 }
|
yading@10
|
147
|
yading@10
|
148 static int query_formats(AVFilterContext *ctx)
|
yading@10
|
149 {
|
yading@10
|
150 static const enum AVPixelFormat pix_fmts[] = {
|
yading@10
|
151 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P,
|
yading@10
|
152 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NV12,
|
yading@10
|
153 AV_PIX_FMT_NV21, AV_PIX_FMT_YUV444P,
|
yading@10
|
154 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
|
yading@10
|
155 AV_PIX_FMT_YUV440P,
|
yading@10
|
156 AV_PIX_FMT_NONE
|
yading@10
|
157 };
|
yading@10
|
158
|
yading@10
|
159 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
yading@10
|
160
|
yading@10
|
161 return 0;
|
yading@10
|
162 }
|
yading@10
|
163
|
yading@10
|
164 static int config_input(AVFilterLink *inlink)
|
yading@10
|
165 {
|
yading@10
|
166 GradFunContext *gf = inlink->dst->priv;
|
yading@10
|
167 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
yading@10
|
168 int hsub = desc->log2_chroma_w;
|
yading@10
|
169 int vsub = desc->log2_chroma_h;
|
yading@10
|
170
|
yading@10
|
171 gf->buf = av_mallocz((FFALIGN(inlink->w, 16) * (gf->radius + 1) / 2 + 32) * sizeof(uint16_t));
|
yading@10
|
172 if (!gf->buf)
|
yading@10
|
173 return AVERROR(ENOMEM);
|
yading@10
|
174
|
yading@10
|
175 gf->chroma_w = -((-inlink->w) >> hsub);
|
yading@10
|
176 gf->chroma_h = -((-inlink->h) >> vsub);
|
yading@10
|
177 gf->chroma_r = av_clip(((((gf->radius >> hsub) + (gf->radius >> vsub)) / 2 ) + 1) & ~1, 4, 32);
|
yading@10
|
178
|
yading@10
|
179 return 0;
|
yading@10
|
180 }
|
yading@10
|
181
|
yading@10
|
182 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
yading@10
|
183 {
|
yading@10
|
184 GradFunContext *gf = inlink->dst->priv;
|
yading@10
|
185 AVFilterLink *outlink = inlink->dst->outputs[0];
|
yading@10
|
186 AVFrame *out;
|
yading@10
|
187 int p, direct;
|
yading@10
|
188
|
yading@10
|
189 if (av_frame_is_writable(in)) {
|
yading@10
|
190 direct = 1;
|
yading@10
|
191 out = in;
|
yading@10
|
192 } else {
|
yading@10
|
193 direct = 0;
|
yading@10
|
194 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
yading@10
|
195 if (!out) {
|
yading@10
|
196 av_frame_free(&in);
|
yading@10
|
197 return AVERROR(ENOMEM);
|
yading@10
|
198 }
|
yading@10
|
199 av_frame_copy_props(out, in);
|
yading@10
|
200 }
|
yading@10
|
201
|
yading@10
|
202 for (p = 0; p < 4 && in->data[p]; p++) {
|
yading@10
|
203 int w = inlink->w;
|
yading@10
|
204 int h = inlink->h;
|
yading@10
|
205 int r = gf->radius;
|
yading@10
|
206 if (p) {
|
yading@10
|
207 w = gf->chroma_w;
|
yading@10
|
208 h = gf->chroma_h;
|
yading@10
|
209 r = gf->chroma_r;
|
yading@10
|
210 }
|
yading@10
|
211
|
yading@10
|
212 if (FFMIN(w, h) > 2 * r)
|
yading@10
|
213 filter(gf, out->data[p], in->data[p], w, h, out->linesize[p], in->linesize[p], r);
|
yading@10
|
214 else if (out->data[p] != in->data[p])
|
yading@10
|
215 av_image_copy_plane(out->data[p], out->linesize[p], in->data[p], in->linesize[p], w, h);
|
yading@10
|
216 }
|
yading@10
|
217
|
yading@10
|
218 if (!direct)
|
yading@10
|
219 av_frame_free(&in);
|
yading@10
|
220
|
yading@10
|
221 return ff_filter_frame(outlink, out);
|
yading@10
|
222 }
|
yading@10
|
223
|
yading@10
|
224 #define OFFSET(x) offsetof(GradFunContext, x)
|
yading@10
|
225 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
yading@10
|
226
|
yading@10
|
227 static const AVOption gradfun_options[] = {
|
yading@10
|
228 { "strength", "The maximum amount by which the filter will change any one pixel.", OFFSET(strength), AV_OPT_TYPE_FLOAT, { .dbl = 1.2 }, 0.51, 64, FLAGS },
|
yading@10
|
229 { "radius", "The neighborhood to fit the gradient to.", OFFSET(radius), AV_OPT_TYPE_INT, { .i64 = 16 }, 4, 32, FLAGS },
|
yading@10
|
230 { NULL },
|
yading@10
|
231 };
|
yading@10
|
232
|
yading@10
|
233 AVFILTER_DEFINE_CLASS(gradfun);
|
yading@10
|
234
|
yading@10
|
235 static const AVFilterPad avfilter_vf_gradfun_inputs[] = {
|
yading@10
|
236 {
|
yading@10
|
237 .name = "default",
|
yading@10
|
238 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
239 .config_props = config_input,
|
yading@10
|
240 .filter_frame = filter_frame,
|
yading@10
|
241 },
|
yading@10
|
242 { NULL }
|
yading@10
|
243 };
|
yading@10
|
244
|
yading@10
|
245 static const AVFilterPad avfilter_vf_gradfun_outputs[] = {
|
yading@10
|
246 {
|
yading@10
|
247 .name = "default",
|
yading@10
|
248 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
249 },
|
yading@10
|
250 { NULL }
|
yading@10
|
251 };
|
yading@10
|
252
|
yading@10
|
253 AVFilter avfilter_vf_gradfun = {
|
yading@10
|
254 .name = "gradfun",
|
yading@10
|
255 .description = NULL_IF_CONFIG_SMALL("Debands video quickly using gradients."),
|
yading@10
|
256 .priv_size = sizeof(GradFunContext),
|
yading@10
|
257 .priv_class = &gradfun_class,
|
yading@10
|
258 .init = init,
|
yading@10
|
259 .uninit = uninit,
|
yading@10
|
260 .query_formats = query_formats,
|
yading@10
|
261 .inputs = avfilter_vf_gradfun_inputs,
|
yading@10
|
262 .outputs = avfilter_vf_gradfun_outputs,
|
yading@10
|
263 };
|