... | ... |
@@ -2129,6 +2129,7 @@ blackframe_filter_deps="gpl" |
2129 | 2129 |
boxblur_filter_deps="gpl" |
2130 | 2130 |
colormatrix_filter_deps="gpl" |
2131 | 2131 |
cropdetect_filter_deps="gpl" |
2132 |
+dctdnoiz_filter_deps="avcodec" |
|
2132 | 2133 |
delogo_filter_deps="gpl" |
2133 | 2134 |
deshake_filter_deps="avcodec" |
2134 | 2135 |
deshake_filter_select="dsputil" |
... | ... |
@@ -2655,6 +2655,59 @@ curves=psfile='MyCurvesPresets/purple.asv':green='0.45/0.53' |
2655 | 2655 |
@end example |
2656 | 2656 |
@end itemize |
2657 | 2657 |
|
2658 |
+@section dctdnoiz |
|
2659 |
+ |
|
2660 |
+Denoise frames using 2D DCT (frequency domain filtering). |
|
2661 |
+ |
|
2662 |
+This filter is not designed for real time and can be extremely slow. |
|
2663 |
+ |
|
2664 |
+The filter accepts the following options: |
|
2665 |
+ |
|
2666 |
+@table @option |
|
2667 |
+@item sigma, s |
|
2668 |
+Set the noise sigma constant. |
|
2669 |
+ |
|
2670 |
+This @var{sigma} defines a hard threshold of @code{3 * sigma}; every DCT |
|
2671 |
+coefficient (absolute value) below this threshold with be dropped. |
|
2672 |
+ |
|
2673 |
+If you need a more advanced filtering, see @option{expr}. |
|
2674 |
+ |
|
2675 |
+Default is @code{0}. |
|
2676 |
+ |
|
2677 |
+@item overlap |
|
2678 |
+Set number overlapping pixels for each block. Each block is of size |
|
2679 |
+@code{16x16}. Since the filter can be slow, you may want to reduce this value, |
|
2680 |
+at the cost of a less effective filter and the risk of various artefacts. |
|
2681 |
+ |
|
2682 |
+If the overlapping value doesn't allow to process the whole input width or |
|
2683 |
+height, a warning will be displayed and according borders won't be denoised. |
|
2684 |
+ |
|
2685 |
+Default value is @code{15}. |
|
2686 |
+ |
|
2687 |
+@item expr, e |
|
2688 |
+Set the coefficient factor expression. |
|
2689 |
+ |
|
2690 |
+For each coefficient of a DCT block, this expression will be evaluated as a |
|
2691 |
+multiplier value for the coefficient. |
|
2692 |
+ |
|
2693 |
+If this is option is set, the @option{sigma} option will be ignored. |
|
2694 |
+ |
|
2695 |
+The absolute value of the coefficient can be accessed through the @var{c} |
|
2696 |
+variable. |
|
2697 |
+@end table |
|
2698 |
+ |
|
2699 |
+@subsection Examples |
|
2700 |
+ |
|
2701 |
+Apply a denoise with a @option{sigma} of @code{4.5}: |
|
2702 |
+@example |
|
2703 |
+dctdnoiz=4.5 |
|
2704 |
+@end example |
|
2705 |
+ |
|
2706 |
+The same operation can be achieved using the expression system: |
|
2707 |
+@example |
|
2708 |
+dctdnoiz=e='gte(c, 4.5*3)' |
|
2709 |
+@end example |
|
2710 |
+ |
|
2658 | 2711 |
@anchor{decimate} |
2659 | 2712 |
@section decimate |
2660 | 2713 |
|
... | ... |
@@ -116,6 +116,7 @@ OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o |
116 | 116 |
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o |
117 | 117 |
OBJS-$(CONFIG_CROPDETECT_FILTER) += vf_cropdetect.o |
118 | 118 |
OBJS-$(CONFIG_CURVES_FILTER) += vf_curves.o |
119 |
+OBJS-$(CONFIG_DCTDNOIZ_FILTER) += vf_dctdnoiz.o |
|
119 | 120 |
OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o |
120 | 121 |
OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o |
121 | 122 |
OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o |
... | ... |
@@ -114,6 +114,7 @@ void avfilter_register_all(void) |
114 | 114 |
REGISTER_FILTER(CROP, crop, vf); |
115 | 115 |
REGISTER_FILTER(CROPDETECT, cropdetect, vf); |
116 | 116 |
REGISTER_FILTER(CURVES, curves, vf); |
117 |
+ REGISTER_FILTER(DCTDNOIZ, dctdnoiz, vf); |
|
117 | 118 |
REGISTER_FILTER(DECIMATE, decimate, vf); |
118 | 119 |
REGISTER_FILTER(DELOGO, delogo, vf); |
119 | 120 |
REGISTER_FILTER(DESHAKE, deshake, vf); |
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 66 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 67 |
|
33 | 33 |
#define LIBAVFILTER_VERSION_MICRO 100 |
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
36 | 36 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,431 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2013 Clément Bœsch |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * A simple, relatively efficient and extremely slow DCT image denoiser. |
|
22 |
+ * @see http://www.ipol.im/pub/art/2011/ys-dct/ |
|
23 |
+ */ |
|
24 |
+ |
|
25 |
+#include "libavcodec/avfft.h" |
|
26 |
+#include "libavutil/eval.h" |
|
27 |
+#include "libavutil/opt.h" |
|
28 |
+#include "drawutils.h" |
|
29 |
+#include "internal.h" |
|
30 |
+ |
|
31 |
+#define NBITS 4 |
|
32 |
+#define BSIZE (1<<(NBITS)) |
|
33 |
+ |
|
34 |
+static const char *const var_names[] = { "c", NULL }; |
|
35 |
+enum { VAR_C, VAR_VARS_NB }; |
|
36 |
+ |
|
37 |
+typedef struct { |
|
38 |
+ const AVClass *class; |
|
39 |
+ |
|
40 |
+ /* coefficient factor expression */ |
|
41 |
+ char *expr_str; |
|
42 |
+ AVExpr *expr; |
|
43 |
+ double var_values[VAR_VARS_NB]; |
|
44 |
+ |
|
45 |
+ int pr_width, pr_height; // width and height to process |
|
46 |
+ float sigma; // used when no expression are st |
|
47 |
+ float th; // threshold (3*sigma) |
|
48 |
+ float color_dct[3][3]; // 3x3 DCT for color decorrelation |
|
49 |
+ float *cbuf[2][3]; // two planar rgb color buffers |
|
50 |
+ float *weights; // dct coeff are cumulated with overlapping; these values are used for averaging |
|
51 |
+ int p_linesize; // line sizes for color and weights |
|
52 |
+ int overlap; // number of block overlapping pixels |
|
53 |
+ int step; // block step increment (BSIZE - overlap) |
|
54 |
+ DCTContext *dct, *idct; // DCT and inverse DCT contexts |
|
55 |
+ float *block, *tmp_block; // two BSIZE x BSIZE block buffers |
|
56 |
+} DCTdnoizContext; |
|
57 |
+ |
|
58 |
+#define OFFSET(x) offsetof(DCTdnoizContext, x) |
|
59 |
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
|
60 |
+static const AVOption dctdnoiz_options[] = { |
|
61 |
+ { "sigma", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS }, |
|
62 |
+ { "s", "set noise sigma constant", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 999, .flags = FLAGS }, |
|
63 |
+ { "overlap", "set number of block overlapping pixels", OFFSET(overlap), AV_OPT_TYPE_INT, {.i64=(1<<NBITS)-1}, 0, (1<<NBITS)-1, .flags = FLAGS }, |
|
64 |
+ { "expr", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, |
|
65 |
+ { "e", "set coefficient factor expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, |
|
66 |
+ { NULL } |
|
67 |
+}; |
|
68 |
+ |
|
69 |
+AVFILTER_DEFINE_CLASS(dctdnoiz); |
|
70 |
+ |
|
71 |
+static float *dct_block(DCTdnoizContext *ctx, const float *src, int src_linesize) |
|
72 |
+{ |
|
73 |
+ int x, y; |
|
74 |
+ float *column; |
|
75 |
+ |
|
76 |
+ for (y = 0; y < BSIZE; y++) { |
|
77 |
+ float *line = ctx->block; |
|
78 |
+ |
|
79 |
+ memcpy(line, src, BSIZE * sizeof(*line)); |
|
80 |
+ src += src_linesize; |
|
81 |
+ av_dct_calc(ctx->dct, line); |
|
82 |
+ |
|
83 |
+ column = ctx->tmp_block + y; |
|
84 |
+ for (x = 0; x < BSIZE; x++) { |
|
85 |
+ *line *= x == 0 ? 1. / sqrt(BSIZE) : sqrt(2. / BSIZE); |
|
86 |
+ *column = *line++; |
|
87 |
+ column += BSIZE; |
|
88 |
+ } |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ column = ctx->tmp_block; |
|
92 |
+ for (x = 0; x < BSIZE; x++) { |
|
93 |
+ av_dct_calc(ctx->dct, column); |
|
94 |
+ for (y = 0; y < BSIZE; y++) |
|
95 |
+ column[y] *= y == 0 ? 1. / sqrt(BSIZE) : sqrt(2. / BSIZE); |
|
96 |
+ column += BSIZE; |
|
97 |
+ } |
|
98 |
+ |
|
99 |
+ for (y = 0; y < BSIZE; y++) |
|
100 |
+ for (x = 0; x < BSIZE; x++) |
|
101 |
+ ctx->block[y*BSIZE + x] = ctx->tmp_block[x*BSIZE + y]; |
|
102 |
+ |
|
103 |
+ return ctx->block; |
|
104 |
+} |
|
105 |
+ |
|
106 |
+static void idct_block(DCTdnoizContext *ctx, float *dst, int dst_linesize) |
|
107 |
+{ |
|
108 |
+ int x, y; |
|
109 |
+ float *block = ctx->block; |
|
110 |
+ float *tmp = ctx->tmp_block; |
|
111 |
+ |
|
112 |
+ for (y = 0; y < BSIZE; y++) { |
|
113 |
+ for (x = 0; x < BSIZE; x++) |
|
114 |
+ block[x] *= x == 0 ? sqrt(BSIZE) : 1./sqrt(2. / BSIZE); |
|
115 |
+ av_dct_calc(ctx->idct, block); |
|
116 |
+ block += BSIZE; |
|
117 |
+ } |
|
118 |
+ |
|
119 |
+ block = ctx->block; |
|
120 |
+ for (y = 0; y < BSIZE; y++) { |
|
121 |
+ for (x = 0; x < BSIZE; x++) { |
|
122 |
+ tmp[x] = block[x*BSIZE + y]; |
|
123 |
+ tmp[x] *= x == 0 ? sqrt(BSIZE) : 1./sqrt(2. / BSIZE); |
|
124 |
+ } |
|
125 |
+ av_dct_calc(ctx->idct, tmp); |
|
126 |
+ for (x = 0; x < BSIZE; x++) |
|
127 |
+ dst[x*dst_linesize + y] += tmp[x]; |
|
128 |
+ } |
|
129 |
+} |
|
130 |
+ |
|
131 |
+static int config_input(AVFilterLink *inlink) |
|
132 |
+{ |
|
133 |
+ AVFilterContext *ctx = inlink->dst; |
|
134 |
+ DCTdnoizContext *s = ctx->priv; |
|
135 |
+ int i, x, y, bx, by, linesize, *iweights; |
|
136 |
+ const float dct_3x3[3][3] = { |
|
137 |
+ { 1./sqrt(3), 1./sqrt(3), 1./sqrt(3) }, |
|
138 |
+ { 1./sqrt(2), 0, -1./sqrt(2) }, |
|
139 |
+ { 1./sqrt(6), -2./sqrt(6), 1./sqrt(6) }, |
|
140 |
+ }; |
|
141 |
+ uint8_t rgba_map[4]; |
|
142 |
+ |
|
143 |
+ ff_fill_rgba_map(rgba_map, inlink->format); |
|
144 |
+ for (y = 0; y < 3; y++) |
|
145 |
+ for (x = 0; x < 3; x++) |
|
146 |
+ s->color_dct[y][x] = dct_3x3[rgba_map[y]][rgba_map[x]]; |
|
147 |
+ |
|
148 |
+ s->pr_width = inlink->w - (inlink->w - BSIZE) % s->step; |
|
149 |
+ s->pr_height = inlink->w - (inlink->h - BSIZE) % s->step; |
|
150 |
+ if (s->pr_width != inlink->w) |
|
151 |
+ av_log(ctx, AV_LOG_WARNING, "The last %d horizontal pixels won't be denoised\n", |
|
152 |
+ inlink->w - s->pr_width); |
|
153 |
+ if (s->pr_height != inlink->h) |
|
154 |
+ av_log(ctx, AV_LOG_WARNING, "The last %d vertical pixels won't be denoised\n", |
|
155 |
+ inlink->h - s->pr_height); |
|
156 |
+ |
|
157 |
+ s->p_linesize = linesize = FFALIGN(s->pr_width, 32); |
|
158 |
+ for (i = 0; i < 2; i++) { |
|
159 |
+ s->cbuf[i][0] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][0])); |
|
160 |
+ s->cbuf[i][1] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][1])); |
|
161 |
+ s->cbuf[i][2] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][2])); |
|
162 |
+ if (!s->cbuf[i][0] || !s->cbuf[i][1] || !s->cbuf[i][2]) |
|
163 |
+ return AVERROR(ENOMEM); |
|
164 |
+ } |
|
165 |
+ |
|
166 |
+ s->weights = av_malloc(s->pr_height * linesize * sizeof(*s->weights)); |
|
167 |
+ if (!s->weights) |
|
168 |
+ return AVERROR(ENOMEM); |
|
169 |
+ iweights = av_calloc(s->pr_height, linesize * sizeof(*iweights)); |
|
170 |
+ if (!iweights) |
|
171 |
+ return AVERROR(ENOMEM); |
|
172 |
+ for (y = 0; y < s->pr_height - BSIZE + 1; y += s->step) |
|
173 |
+ for (x = 0; x < s->pr_width - BSIZE + 1; x += s->step) |
|
174 |
+ for (by = 0; by < BSIZE; by++) |
|
175 |
+ for (bx = 0; bx < BSIZE; bx++) |
|
176 |
+ iweights[(y + by)*linesize + x + bx]++; |
|
177 |
+ for (y = 0; y < s->pr_height; y++) |
|
178 |
+ for (x = 0; x < s->pr_width; x++) |
|
179 |
+ s->weights[y*linesize + x] = 1. / iweights[y*linesize + x]; |
|
180 |
+ av_free(iweights); |
|
181 |
+ |
|
182 |
+ return 0; |
|
183 |
+} |
|
184 |
+ |
|
185 |
+static av_cold int init(AVFilterContext *ctx) |
|
186 |
+{ |
|
187 |
+ DCTdnoizContext *s = ctx->priv; |
|
188 |
+ |
|
189 |
+ if (s->expr_str) { |
|
190 |
+ int ret = av_expr_parse(&s->expr, s->expr_str, var_names, |
|
191 |
+ NULL, NULL, NULL, NULL, 0, ctx); |
|
192 |
+ if (ret < 0) |
|
193 |
+ return ret; |
|
194 |
+ } |
|
195 |
+ |
|
196 |
+ s->th = s->sigma * 3.; |
|
197 |
+ s->step = BSIZE - s->overlap; |
|
198 |
+ s->dct = av_dct_init(NBITS, DCT_II); |
|
199 |
+ s->idct = av_dct_init(NBITS, DCT_III); |
|
200 |
+ s->block = av_malloc(BSIZE * BSIZE * sizeof(*s->block)); |
|
201 |
+ s->tmp_block = av_malloc(BSIZE * BSIZE * sizeof(*s->tmp_block)); |
|
202 |
+ |
|
203 |
+ if (!s->dct || !s->idct || !s->tmp_block || !s->block) |
|
204 |
+ return AVERROR(ENOMEM); |
|
205 |
+ |
|
206 |
+ return 0; |
|
207 |
+} |
|
208 |
+ |
|
209 |
+static int query_formats(AVFilterContext *ctx) |
|
210 |
+{ |
|
211 |
+ static const enum AVPixelFormat pix_fmts[] = { |
|
212 |
+ AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24, |
|
213 |
+ AV_PIX_FMT_NONE |
|
214 |
+ }; |
|
215 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
216 |
+ return 0; |
|
217 |
+} |
|
218 |
+ |
|
219 |
+static void color_decorrelation(float dct3ch[3][3], float **dst, int dst_linesize, |
|
220 |
+ const uint8_t *src, int src_linesize, int w, int h) |
|
221 |
+{ |
|
222 |
+ int x, y; |
|
223 |
+ float *dstp_r = dst[0]; |
|
224 |
+ float *dstp_g = dst[1]; |
|
225 |
+ float *dstp_b = dst[2]; |
|
226 |
+ |
|
227 |
+ for (y = 0; y < h; y++) { |
|
228 |
+ const uint8_t *srcp = src; |
|
229 |
+ |
|
230 |
+ for (x = 0; x < w; x++) { |
|
231 |
+ dstp_r[x] = srcp[0] * dct3ch[0][0] + srcp[1] * dct3ch[0][1] + srcp[2] * dct3ch[0][2]; |
|
232 |
+ dstp_g[x] = srcp[0] * dct3ch[1][0] + srcp[1] * dct3ch[1][1] + srcp[2] * dct3ch[1][2]; |
|
233 |
+ dstp_b[x] = srcp[0] * dct3ch[2][0] + srcp[1] * dct3ch[2][1] + srcp[2] * dct3ch[2][2]; |
|
234 |
+ srcp += 3; |
|
235 |
+ } |
|
236 |
+ src += src_linesize; |
|
237 |
+ dstp_r += dst_linesize; |
|
238 |
+ dstp_g += dst_linesize; |
|
239 |
+ dstp_b += dst_linesize; |
|
240 |
+ } |
|
241 |
+} |
|
242 |
+ |
|
243 |
+static void color_correlation(float dct3ch[3][3], uint8_t *dst, int dst_linesize, |
|
244 |
+ float **src, int src_linesize, int w, int h) |
|
245 |
+{ |
|
246 |
+ int x, y; |
|
247 |
+ const float *src_r = src[0]; |
|
248 |
+ const float *src_g = src[1]; |
|
249 |
+ const float *src_b = src[2]; |
|
250 |
+ |
|
251 |
+ for (y = 0; y < h; y++) { |
|
252 |
+ uint8_t *dstp = dst; |
|
253 |
+ |
|
254 |
+ for (x = 0; x < w; x++) { |
|
255 |
+ dstp[0] = av_clip_uint8(src_r[x] * dct3ch[0][0] + src_g[x] * dct3ch[1][0] + src_b[x] * dct3ch[2][0]); |
|
256 |
+ dstp[1] = av_clip_uint8(src_r[x] * dct3ch[0][1] + src_g[x] * dct3ch[1][1] + src_b[x] * dct3ch[2][1]); |
|
257 |
+ dstp[2] = av_clip_uint8(src_r[x] * dct3ch[0][2] + src_g[x] * dct3ch[1][2] + src_b[x] * dct3ch[2][2]); |
|
258 |
+ dstp += 3; |
|
259 |
+ } |
|
260 |
+ dst += dst_linesize; |
|
261 |
+ src_r += src_linesize; |
|
262 |
+ src_g += src_linesize; |
|
263 |
+ src_b += src_linesize; |
|
264 |
+ } |
|
265 |
+} |
|
266 |
+ |
|
267 |
+static void filter_plane(AVFilterContext *ctx, |
|
268 |
+ float *dst, int dst_linesize, |
|
269 |
+ const float *src, int src_linesize, |
|
270 |
+ int w, int h) |
|
271 |
+{ |
|
272 |
+ int x, y, bx, by; |
|
273 |
+ DCTdnoizContext *s = ctx->priv; |
|
274 |
+ float *dst0 = dst; |
|
275 |
+ const float *weights = s->weights; |
|
276 |
+ |
|
277 |
+ // reset block sums |
|
278 |
+ memset(dst, 0, h * dst_linesize * sizeof(*dst)); |
|
279 |
+ |
|
280 |
+ // block dct sums |
|
281 |
+ for (y = 0; y < h - BSIZE + 1; y += s->step) { |
|
282 |
+ for (x = 0; x < w - BSIZE + 1; x += s->step) { |
|
283 |
+ float *ftb = dct_block(s, src + x, src_linesize); |
|
284 |
+ |
|
285 |
+ if (s->expr) { |
|
286 |
+ for (by = 0; by < BSIZE; by++) { |
|
287 |
+ for (bx = 0; bx < BSIZE; bx++) { |
|
288 |
+ s->var_values[VAR_C] = FFABS(*ftb); |
|
289 |
+ *ftb++ *= av_expr_eval(s->expr, s->var_values, s); |
|
290 |
+ } |
|
291 |
+ } |
|
292 |
+ } else { |
|
293 |
+ for (by = 0; by < BSIZE; by++) { |
|
294 |
+ for (bx = 0; bx < BSIZE; bx++) { |
|
295 |
+ if (FFABS(*ftb) < s->th) |
|
296 |
+ *ftb = 0; |
|
297 |
+ ftb++; |
|
298 |
+ } |
|
299 |
+ } |
|
300 |
+ } |
|
301 |
+ idct_block(s, dst + x, dst_linesize); |
|
302 |
+ } |
|
303 |
+ src += s->step * src_linesize; |
|
304 |
+ dst += s->step * dst_linesize; |
|
305 |
+ } |
|
306 |
+ |
|
307 |
+ // average blocks |
|
308 |
+ dst = dst0; |
|
309 |
+ for (y = 0; y < h; y++) { |
|
310 |
+ for (x = 0; x < w; x++) |
|
311 |
+ dst[x] *= weights[x]; |
|
312 |
+ dst += dst_linesize; |
|
313 |
+ weights += dst_linesize; |
|
314 |
+ } |
|
315 |
+} |
|
316 |
+ |
|
317 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
318 |
+{ |
|
319 |
+ AVFilterContext *ctx = inlink->dst; |
|
320 |
+ DCTdnoizContext *s = ctx->priv; |
|
321 |
+ AVFilterLink *outlink = inlink->dst->outputs[0]; |
|
322 |
+ int direct, plane; |
|
323 |
+ AVFrame *out; |
|
324 |
+ |
|
325 |
+ if (av_frame_is_writable(in)) { |
|
326 |
+ direct = 1; |
|
327 |
+ out = in; |
|
328 |
+ } else { |
|
329 |
+ direct = 0; |
|
330 |
+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
|
331 |
+ if (!out) { |
|
332 |
+ av_frame_free(&in); |
|
333 |
+ return AVERROR(ENOMEM); |
|
334 |
+ } |
|
335 |
+ av_frame_copy_props(out, in); |
|
336 |
+ } |
|
337 |
+ |
|
338 |
+ color_decorrelation(s->color_dct, s->cbuf[0], s->p_linesize, |
|
339 |
+ in->data[0], in->linesize[0], s->pr_width, s->pr_height); |
|
340 |
+ for (plane = 0; plane < 3; plane++) |
|
341 |
+ filter_plane(ctx, s->cbuf[1][plane], s->p_linesize, |
|
342 |
+ s->cbuf[0][plane], s->p_linesize, |
|
343 |
+ s->pr_width, s->pr_height); |
|
344 |
+ color_correlation(s->color_dct, out->data[0], out->linesize[0], |
|
345 |
+ s->cbuf[1], s->p_linesize, s->pr_width, s->pr_height); |
|
346 |
+ |
|
347 |
+ if (!direct) { |
|
348 |
+ int y; |
|
349 |
+ uint8_t *dst = out->data[0]; |
|
350 |
+ const uint8_t *src = in->data[0]; |
|
351 |
+ const int dst_linesize = out->linesize[0]; |
|
352 |
+ const int src_linesize = in->linesize[0]; |
|
353 |
+ const int hpad = (inlink->w - s->pr_width) * 3; |
|
354 |
+ const int vpad = (inlink->h - s->pr_height); |
|
355 |
+ |
|
356 |
+ if (hpad) { |
|
357 |
+ uint8_t *dstp = dst + s->pr_width * 3; |
|
358 |
+ const uint8_t *srcp = src + s->pr_width * 3; |
|
359 |
+ |
|
360 |
+ for (y = 0; y < s->pr_height; y++) { |
|
361 |
+ memcpy(dstp, srcp, hpad); |
|
362 |
+ dstp += dst_linesize; |
|
363 |
+ srcp += src_linesize; |
|
364 |
+ } |
|
365 |
+ } |
|
366 |
+ if (vpad) { |
|
367 |
+ uint8_t *dstp = dst + s->pr_height * dst_linesize; |
|
368 |
+ const uint8_t *srcp = src + s->pr_height * src_linesize; |
|
369 |
+ |
|
370 |
+ for (y = 0; y < vpad; y++) { |
|
371 |
+ memcpy(dstp, srcp, inlink->w * 3); |
|
372 |
+ dstp += dst_linesize; |
|
373 |
+ srcp += src_linesize; |
|
374 |
+ } |
|
375 |
+ } |
|
376 |
+ |
|
377 |
+ av_frame_free(&in); |
|
378 |
+ } |
|
379 |
+ |
|
380 |
+ return ff_filter_frame(outlink, out); |
|
381 |
+} |
|
382 |
+ |
|
383 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
384 |
+{ |
|
385 |
+ int i; |
|
386 |
+ DCTdnoizContext *s = ctx->priv; |
|
387 |
+ |
|
388 |
+ av_dct_end(s->dct); |
|
389 |
+ av_dct_end(s->idct); |
|
390 |
+ av_free(s->block); |
|
391 |
+ av_free(s->tmp_block); |
|
392 |
+ av_free(s->weights); |
|
393 |
+ for (i = 0; i < 2; i++) { |
|
394 |
+ av_free(s->cbuf[i][0]); |
|
395 |
+ av_free(s->cbuf[i][1]); |
|
396 |
+ av_free(s->cbuf[i][2]); |
|
397 |
+ } |
|
398 |
+ av_expr_free(s->expr); |
|
399 |
+} |
|
400 |
+ |
|
401 |
+static const AVFilterPad dctdnoiz_inputs[] = { |
|
402 |
+ { |
|
403 |
+ .name = "default", |
|
404 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
405 |
+ .filter_frame = filter_frame, |
|
406 |
+ .config_props = config_input, |
|
407 |
+ }, |
|
408 |
+ { NULL } |
|
409 |
+}; |
|
410 |
+ |
|
411 |
+static const AVFilterPad dctdnoiz_outputs[] = { |
|
412 |
+ { |
|
413 |
+ .name = "default", |
|
414 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
415 |
+ }, |
|
416 |
+ { NULL } |
|
417 |
+}; |
|
418 |
+ |
|
419 |
+AVFilter avfilter_vf_dctdnoiz = { |
|
420 |
+ .name = "dctdnoiz", |
|
421 |
+ .description = NULL_IF_CONFIG_SMALL("Denoise frames using 2D DCT."), |
|
422 |
+ .priv_size = sizeof(DCTdnoizContext), |
|
423 |
+ .init = init, |
|
424 |
+ .uninit = uninit, |
|
425 |
+ .query_formats = query_formats, |
|
426 |
+ .inputs = dctdnoiz_inputs, |
|
427 |
+ .outputs = dctdnoiz_outputs, |
|
428 |
+ .priv_class = &dctdnoiz_class, |
|
429 |
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, |
|
430 |
+}; |