Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2015 Paul B Mahol |
3 |
|
|
* |
4 |
|
|
* This file is part of FFmpeg. |
5 |
|
|
* |
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. |
10 |
|
|
* |
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. |
15 |
|
|
* |
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 |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* @file |
23 |
|
|
* Adaptive Temporal Averaging Denoiser, |
24 |
|
|
* based on paper "Video Denoising Based on Adaptive Temporal Averaging" by |
25 |
|
|
* David Bartovčak and Miroslav Vrankić |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include "libavutil/imgutils.h" |
29 |
|
|
#include "libavutil/opt.h" |
30 |
|
|
#include "libavutil/pixdesc.h" |
31 |
|
|
#include "avfilter.h" |
32 |
|
|
|
33 |
|
|
#define FF_BUFQUEUE_SIZE 129 |
34 |
|
|
#include "bufferqueue.h" |
35 |
|
|
|
36 |
|
|
#include "atadenoise.h" |
37 |
|
|
#include "filters.h" |
38 |
|
|
#include "video.h" |
39 |
|
|
|
40 |
|
|
#define SIZE FF_BUFQUEUE_SIZE |
41 |
|
|
|
42 |
|
|
typedef struct ATADenoiseContext { |
43 |
|
|
const AVClass *class; |
44 |
|
|
|
45 |
|
|
float fthra[4], fthrb[4]; |
46 |
|
|
float sigma[4]; |
47 |
|
|
int thra[4], thrb[4]; |
48 |
|
|
int algorithm; |
49 |
|
|
|
50 |
|
|
int planes; |
51 |
|
|
int nb_planes; |
52 |
|
|
int planewidth[4]; |
53 |
|
|
int planeheight[4]; |
54 |
|
|
int linesizes[4]; |
55 |
|
|
|
56 |
|
|
struct FFBufQueue q; |
57 |
|
|
void *data[4][SIZE]; |
58 |
|
|
int linesize[4][SIZE]; |
59 |
|
|
float weights[4][SIZE]; |
60 |
|
|
int size, mid, radius; |
61 |
|
|
int available; |
62 |
|
|
|
63 |
|
|
int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
64 |
|
|
|
65 |
|
|
ATADenoiseDSPContext dsp; |
66 |
|
|
} ATADenoiseContext; |
67 |
|
|
|
68 |
|
|
#define OFFSET(x) offsetof(ATADenoiseContext, x) |
69 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
70 |
|
|
#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
71 |
|
|
|
72 |
|
|
static const AVOption atadenoise_options[] = { |
73 |
|
|
{ "0a", "set threshold A for 1st plane", OFFSET(fthra[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
74 |
|
|
{ "0b", "set threshold B for 1st plane", OFFSET(fthrb[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
75 |
|
|
{ "1a", "set threshold A for 2nd plane", OFFSET(fthra[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
76 |
|
|
{ "1b", "set threshold B for 2nd plane", OFFSET(fthrb[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
77 |
|
|
{ "2a", "set threshold A for 3rd plane", OFFSET(fthra[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0, 0.3, FLAGS }, |
78 |
|
|
{ "2b", "set threshold B for 3rd plane", OFFSET(fthrb[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 5.0, FLAGS }, |
79 |
|
|
{ "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=9}, 5, SIZE, VF }, |
80 |
|
|
{ "p", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 0, 15, FLAGS }, |
81 |
|
|
{ "a", "set variant of algorithm", OFFSET(algorithm),AV_OPT_TYPE_INT, {.i64=PARALLEL}, 0, NB_ATAA-1, FLAGS, .unit = "a" }, |
82 |
|
|
{ "p", "parallel", 0, AV_OPT_TYPE_CONST, {.i64=PARALLEL}, 0, 0, FLAGS, .unit = "a" }, |
83 |
|
|
{ "s", "serial", 0, AV_OPT_TYPE_CONST, {.i64=SERIAL}, 0, 0, FLAGS, .unit = "a" }, |
84 |
|
|
{ "0s", "set sigma for 1st plane", OFFSET(sigma[0]), AV_OPT_TYPE_FLOAT, {.dbl=INT16_MAX}, 0, INT16_MAX, FLAGS }, |
85 |
|
|
{ "1s", "set sigma for 2nd plane", OFFSET(sigma[1]), AV_OPT_TYPE_FLOAT, {.dbl=INT16_MAX}, 0, INT16_MAX, FLAGS }, |
86 |
|
|
{ "2s", "set sigma for 3rd plane", OFFSET(sigma[2]), AV_OPT_TYPE_FLOAT, {.dbl=INT16_MAX}, 0, INT16_MAX, FLAGS }, |
87 |
|
|
{ NULL } |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
|
AVFILTER_DEFINE_CLASS(atadenoise); |
91 |
|
|
|
92 |
|
|
static const enum AVPixelFormat pixel_fmts[] = { |
93 |
|
|
AV_PIX_FMT_GRAY8, |
94 |
|
|
AV_PIX_FMT_GRAY9, |
95 |
|
|
AV_PIX_FMT_GRAY10, |
96 |
|
|
AV_PIX_FMT_GRAY12, |
97 |
|
|
AV_PIX_FMT_GRAY14, |
98 |
|
|
AV_PIX_FMT_GRAY16, |
99 |
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
100 |
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
101 |
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
102 |
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
103 |
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
104 |
|
|
AV_PIX_FMT_YUVJ411P, |
105 |
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
106 |
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
107 |
|
|
AV_PIX_FMT_YUV440P10, |
108 |
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
109 |
|
|
AV_PIX_FMT_YUV440P12, |
110 |
|
|
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
111 |
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
112 |
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
113 |
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
114 |
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
115 |
|
|
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
116 |
|
|
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, |
117 |
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
118 |
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
119 |
|
|
AV_PIX_FMT_NONE |
120 |
|
|
}; |
121 |
|
|
|
122 |
|
✗ |
static av_cold int init(AVFilterContext *ctx) |
123 |
|
|
{ |
124 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
125 |
|
|
|
126 |
|
✗ |
if (!(s->size & 1)) { |
127 |
|
✗ |
av_log(ctx, AV_LOG_WARNING, "size %d is invalid. Must be an odd value, setting it to %d.\n", s->size, s->size|1); |
128 |
|
✗ |
s->size |= 1; |
129 |
|
|
} |
130 |
|
✗ |
s->radius = s->size / 2; |
131 |
|
✗ |
s->mid = s->radius; |
132 |
|
|
|
133 |
|
✗ |
return 0; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
typedef struct ThreadData { |
137 |
|
|
AVFrame *in, *out; |
138 |
|
|
} ThreadData; |
139 |
|
|
|
140 |
|
|
#define WFILTER_ROW(type, name) \ |
141 |
|
|
static void fweight_row##name(const uint8_t *ssrc, uint8_t *ddst, \ |
142 |
|
|
const uint8_t *ssrcf[SIZE], \ |
143 |
|
|
int w, int mid, int size, \ |
144 |
|
|
int thra, int thrb, const float *weights) \ |
145 |
|
|
{ \ |
146 |
|
|
const type *src = (const type *)ssrc; \ |
147 |
|
|
const type **srcf = (const type **)ssrcf; \ |
148 |
|
|
type *dst = (type *)ddst; \ |
149 |
|
|
\ |
150 |
|
|
for (int x = 0; x < w; x++) { \ |
151 |
|
|
const int srcx = src[x]; \ |
152 |
|
|
unsigned lsumdiff = 0, rsumdiff = 0; \ |
153 |
|
|
unsigned ldiff, rdiff; \ |
154 |
|
|
float sum = srcx; \ |
155 |
|
|
float wsum = 1.f; \ |
156 |
|
|
int srcjx, srcix; \ |
157 |
|
|
\ |
158 |
|
|
for (int j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) { \ |
159 |
|
|
srcjx = srcf[j][x]; \ |
160 |
|
|
\ |
161 |
|
|
ldiff = FFABS(srcx - srcjx); \ |
162 |
|
|
lsumdiff += ldiff; \ |
163 |
|
|
if (ldiff > thra || \ |
164 |
|
|
lsumdiff > thrb) \ |
165 |
|
|
break; \ |
166 |
|
|
sum += srcjx * weights[j]; \ |
167 |
|
|
wsum += weights[j]; \ |
168 |
|
|
\ |
169 |
|
|
srcix = srcf[i][x]; \ |
170 |
|
|
\ |
171 |
|
|
rdiff = FFABS(srcx - srcix); \ |
172 |
|
|
rsumdiff += rdiff; \ |
173 |
|
|
if (rdiff > thra || \ |
174 |
|
|
rsumdiff > thrb) \ |
175 |
|
|
break; \ |
176 |
|
|
sum += srcix * weights[i]; \ |
177 |
|
|
wsum += weights[i]; \ |
178 |
|
|
} \ |
179 |
|
|
\ |
180 |
|
|
dst[x] = lrintf(sum / wsum); \ |
181 |
|
|
} \ |
182 |
|
|
} |
183 |
|
|
|
184 |
|
✗ |
WFILTER_ROW(uint8_t, 8) |
185 |
|
✗ |
WFILTER_ROW(uint16_t, 16) |
186 |
|
|
|
187 |
|
|
#define WFILTER_ROW_SERIAL(type, name) \ |
188 |
|
|
static void fweight_row##name##_serial(const uint8_t *ssrc, uint8_t *ddst, \ |
189 |
|
|
const uint8_t *ssrcf[SIZE], \ |
190 |
|
|
int w, int mid, int size, \ |
191 |
|
|
int thra, int thrb, \ |
192 |
|
|
const float *weights) \ |
193 |
|
|
{ \ |
194 |
|
|
const type *src = (const type *)ssrc; \ |
195 |
|
|
const type **srcf = (const type **)ssrcf; \ |
196 |
|
|
type *dst = (type *)ddst; \ |
197 |
|
|
\ |
198 |
|
|
for (int x = 0; x < w; x++) { \ |
199 |
|
|
const int srcx = src[x]; \ |
200 |
|
|
unsigned lsumdiff = 0, rsumdiff = 0; \ |
201 |
|
|
unsigned ldiff, rdiff; \ |
202 |
|
|
float sum = srcx; \ |
203 |
|
|
float wsum = 1.f; \ |
204 |
|
|
int srcjx, srcix; \ |
205 |
|
|
\ |
206 |
|
|
for (int j = mid - 1; j >= 0; j--) { \ |
207 |
|
|
srcjx = srcf[j][x]; \ |
208 |
|
|
\ |
209 |
|
|
ldiff = FFABS(srcx - srcjx); \ |
210 |
|
|
lsumdiff += ldiff; \ |
211 |
|
|
if (ldiff > thra || \ |
212 |
|
|
lsumdiff > thrb) \ |
213 |
|
|
break; \ |
214 |
|
|
sum += srcjx * weights[j]; \ |
215 |
|
|
wsum += weights[j]; \ |
216 |
|
|
} \ |
217 |
|
|
\ |
218 |
|
|
for (int i = mid + 1; i < size; i++) { \ |
219 |
|
|
srcix = srcf[i][x]; \ |
220 |
|
|
\ |
221 |
|
|
rdiff = FFABS(srcx - srcix); \ |
222 |
|
|
rsumdiff += rdiff; \ |
223 |
|
|
if (rdiff > thra || \ |
224 |
|
|
rsumdiff > thrb) \ |
225 |
|
|
break; \ |
226 |
|
|
sum += srcix * weights[i]; \ |
227 |
|
|
wsum += weights[i]; \ |
228 |
|
|
} \ |
229 |
|
|
\ |
230 |
|
|
dst[x] = lrintf(sum / wsum); \ |
231 |
|
|
} \ |
232 |
|
|
} |
233 |
|
|
|
234 |
|
✗ |
WFILTER_ROW_SERIAL(uint8_t, 8) |
235 |
|
✗ |
WFILTER_ROW_SERIAL(uint16_t, 16) |
236 |
|
|
|
237 |
|
|
#define FILTER_ROW(type, name) \ |
238 |
|
|
static void filter_row##name(const uint8_t *ssrc, uint8_t *ddst, \ |
239 |
|
|
const uint8_t *ssrcf[SIZE], \ |
240 |
|
|
int w, int mid, int size, \ |
241 |
|
|
int thra, int thrb, const float *weights) \ |
242 |
|
|
{ \ |
243 |
|
|
const type *src = (const type *)ssrc; \ |
244 |
|
|
const type **srcf = (const type **)ssrcf; \ |
245 |
|
|
type *dst = (type *)ddst; \ |
246 |
|
|
\ |
247 |
|
|
for (int x = 0; x < w; x++) { \ |
248 |
|
|
const int srcx = src[x]; \ |
249 |
|
|
unsigned lsumdiff = 0, rsumdiff = 0; \ |
250 |
|
|
unsigned ldiff, rdiff; \ |
251 |
|
|
unsigned sum = srcx; \ |
252 |
|
|
int l = 0, r = 0; \ |
253 |
|
|
int srcjx, srcix; \ |
254 |
|
|
\ |
255 |
|
|
for (int j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) { \ |
256 |
|
|
srcjx = srcf[j][x]; \ |
257 |
|
|
\ |
258 |
|
|
ldiff = FFABS(srcx - srcjx); \ |
259 |
|
|
lsumdiff += ldiff; \ |
260 |
|
|
if (ldiff > thra || \ |
261 |
|
|
lsumdiff > thrb) \ |
262 |
|
|
break; \ |
263 |
|
|
l++; \ |
264 |
|
|
sum += srcjx; \ |
265 |
|
|
\ |
266 |
|
|
srcix = srcf[i][x]; \ |
267 |
|
|
\ |
268 |
|
|
rdiff = FFABS(srcx - srcix); \ |
269 |
|
|
rsumdiff += rdiff; \ |
270 |
|
|
if (rdiff > thra || \ |
271 |
|
|
rsumdiff > thrb) \ |
272 |
|
|
break; \ |
273 |
|
|
r++; \ |
274 |
|
|
sum += srcix; \ |
275 |
|
|
} \ |
276 |
|
|
\ |
277 |
|
|
dst[x] = (sum + ((r + l + 1) >> 1)) / (r + l + 1); \ |
278 |
|
|
} \ |
279 |
|
|
} |
280 |
|
|
|
281 |
|
✗ |
FILTER_ROW(uint8_t, 8) |
282 |
|
✗ |
FILTER_ROW(uint16_t, 16) |
283 |
|
|
|
284 |
|
|
#define FILTER_ROW_SERIAL(type, name) \ |
285 |
|
|
static void filter_row##name##_serial(const uint8_t *ssrc, uint8_t *ddst, \ |
286 |
|
|
const uint8_t *ssrcf[SIZE], \ |
287 |
|
|
int w, int mid, int size, \ |
288 |
|
|
int thra, int thrb, \ |
289 |
|
|
const float *weights) \ |
290 |
|
|
{ \ |
291 |
|
|
const type *src = (const type *)ssrc; \ |
292 |
|
|
const type **srcf = (const type **)ssrcf; \ |
293 |
|
|
type *dst = (type *)ddst; \ |
294 |
|
|
\ |
295 |
|
|
for (int x = 0; x < w; x++) { \ |
296 |
|
|
const int srcx = src[x]; \ |
297 |
|
|
unsigned lsumdiff = 0, rsumdiff = 0; \ |
298 |
|
|
unsigned ldiff, rdiff; \ |
299 |
|
|
unsigned sum = srcx; \ |
300 |
|
|
int l = 0, r = 0; \ |
301 |
|
|
int srcjx, srcix; \ |
302 |
|
|
\ |
303 |
|
|
for (int j = mid - 1; j >= 0; j--) { \ |
304 |
|
|
srcjx = srcf[j][x]; \ |
305 |
|
|
\ |
306 |
|
|
ldiff = FFABS(srcx - srcjx); \ |
307 |
|
|
lsumdiff += ldiff; \ |
308 |
|
|
if (ldiff > thra || \ |
309 |
|
|
lsumdiff > thrb) \ |
310 |
|
|
break; \ |
311 |
|
|
l++; \ |
312 |
|
|
sum += srcjx; \ |
313 |
|
|
} \ |
314 |
|
|
\ |
315 |
|
|
for (int i = mid + 1; i < size; i++) { \ |
316 |
|
|
srcix = srcf[i][x]; \ |
317 |
|
|
\ |
318 |
|
|
rdiff = FFABS(srcx - srcix); \ |
319 |
|
|
rsumdiff += rdiff; \ |
320 |
|
|
if (rdiff > thra || \ |
321 |
|
|
rsumdiff > thrb) \ |
322 |
|
|
break; \ |
323 |
|
|
r++; \ |
324 |
|
|
sum += srcix; \ |
325 |
|
|
} \ |
326 |
|
|
\ |
327 |
|
|
dst[x] = (sum + ((r + l + 1) >> 1)) / (r + l + 1); \ |
328 |
|
|
} \ |
329 |
|
|
} |
330 |
|
|
|
331 |
|
✗ |
FILTER_ROW_SERIAL(uint8_t, 8) |
332 |
|
✗ |
FILTER_ROW_SERIAL(uint16_t, 16) |
333 |
|
|
|
334 |
|
✗ |
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
335 |
|
|
{ |
336 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
337 |
|
✗ |
ThreadData *td = arg; |
338 |
|
✗ |
AVFrame *in = td->in; |
339 |
|
✗ |
AVFrame *out = td->out; |
340 |
|
✗ |
const int size = s->size; |
341 |
|
✗ |
const int mid = s->mid; |
342 |
|
|
int p, y, i; |
343 |
|
|
|
344 |
|
✗ |
for (p = 0; p < s->nb_planes; p++) { |
345 |
|
✗ |
const float *weights = s->weights[p]; |
346 |
|
✗ |
const int h = s->planeheight[p]; |
347 |
|
✗ |
const int w = s->planewidth[p]; |
348 |
|
✗ |
const int slice_start = (h * jobnr) / nb_jobs; |
349 |
|
✗ |
const int slice_end = (h * (jobnr+1)) / nb_jobs; |
350 |
|
✗ |
const uint8_t *src = in->data[p] + slice_start * in->linesize[p]; |
351 |
|
✗ |
uint8_t *dst = out->data[p] + slice_start * out->linesize[p]; |
352 |
|
✗ |
const int thra = s->thra[p]; |
353 |
|
✗ |
const int thrb = s->thrb[p]; |
354 |
|
✗ |
const uint8_t **data = (const uint8_t **)s->data[p]; |
355 |
|
✗ |
const int *linesize = (const int *)s->linesize[p]; |
356 |
|
|
const uint8_t *srcf[SIZE]; |
357 |
|
|
|
358 |
|
✗ |
if (!((1 << p) & s->planes)) { |
359 |
|
✗ |
av_image_copy_plane(dst, out->linesize[p], src, in->linesize[p], |
360 |
|
|
s->linesizes[p], slice_end - slice_start); |
361 |
|
✗ |
continue; |
362 |
|
|
} |
363 |
|
|
|
364 |
|
✗ |
for (i = 0; i < size; i++) |
365 |
|
✗ |
srcf[i] = data[i] + slice_start * linesize[i]; |
366 |
|
|
|
367 |
|
✗ |
for (y = slice_start; y < slice_end; y++) { |
368 |
|
✗ |
s->dsp.filter_row[p](src, dst, srcf, w, mid, size, thra, thrb, weights); |
369 |
|
|
|
370 |
|
✗ |
dst += out->linesize[p]; |
371 |
|
✗ |
src += in->linesize[p]; |
372 |
|
|
|
373 |
|
✗ |
for (i = 0; i < size; i++) |
374 |
|
✗ |
srcf[i] += linesize[i]; |
375 |
|
|
} |
376 |
|
|
} |
377 |
|
|
|
378 |
|
✗ |
return 0; |
379 |
|
|
} |
380 |
|
|
|
381 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
382 |
|
|
{ |
383 |
|
✗ |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
384 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
385 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
386 |
|
|
int depth, ret; |
387 |
|
|
|
388 |
|
✗ |
s->nb_planes = desc->nb_components; |
389 |
|
|
|
390 |
|
✗ |
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
391 |
|
✗ |
s->planeheight[0] = s->planeheight[3] = inlink->h; |
392 |
|
✗ |
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
393 |
|
✗ |
s->planewidth[0] = s->planewidth[3] = inlink->w; |
394 |
|
|
|
395 |
|
✗ |
depth = desc->comp[0].depth; |
396 |
|
✗ |
s->filter_slice = filter_slice; |
397 |
|
|
|
398 |
|
✗ |
if ((ret = av_image_fill_linesizes(s->linesizes, inlink->format, inlink->w)) < 0) |
399 |
|
✗ |
return ret; |
400 |
|
|
|
401 |
|
✗ |
for (int p = 0; p < s->nb_planes; p++) { |
402 |
|
✗ |
if (depth == 8 && s->sigma[p] == INT16_MAX) |
403 |
|
✗ |
s->dsp.filter_row[p] = s->algorithm == PARALLEL ? filter_row8 : filter_row8_serial; |
404 |
|
✗ |
else if (s->sigma[p] == INT16_MAX) |
405 |
|
✗ |
s->dsp.filter_row[p] = s->algorithm == PARALLEL ? filter_row16 : filter_row16_serial; |
406 |
|
✗ |
else if (depth == 8 && s->sigma[p] < INT16_MAX) |
407 |
|
✗ |
s->dsp.filter_row[p] = s->algorithm == PARALLEL ? fweight_row8 : fweight_row8_serial; |
408 |
|
✗ |
else if (s->sigma[p] < INT16_MAX) |
409 |
|
✗ |
s->dsp.filter_row[p] = s->algorithm == PARALLEL ? fweight_row16 : fweight_row16_serial; |
410 |
|
|
} |
411 |
|
|
|
412 |
|
✗ |
s->thra[0] = s->fthra[0] * (1 << depth) - 1; |
413 |
|
✗ |
s->thra[1] = s->fthra[1] * (1 << depth) - 1; |
414 |
|
✗ |
s->thra[2] = s->fthra[2] * (1 << depth) - 1; |
415 |
|
✗ |
s->thrb[0] = s->fthrb[0] * (1 << depth) - 1; |
416 |
|
✗ |
s->thrb[1] = s->fthrb[1] * (1 << depth) - 1; |
417 |
|
✗ |
s->thrb[2] = s->fthrb[2] * (1 << depth) - 1; |
418 |
|
|
|
419 |
|
✗ |
for (int p = 0; p < s->nb_planes; p++) { |
420 |
|
✗ |
float sigma = s->radius * s->sigma[p]; |
421 |
|
|
|
422 |
|
✗ |
s->weights[p][s->radius] = 1.f; |
423 |
|
✗ |
for (int n = 1; n <= s->radius; n++) { |
424 |
|
✗ |
s->weights[p][s->radius + n] = |
425 |
|
✗ |
s->weights[p][s->radius - n] = expf(-0.5 * (n + 1) * (n + 1) / (sigma * sigma)); |
426 |
|
|
} |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
#if ARCH_X86 |
430 |
|
✗ |
ff_atadenoise_init_x86(&s->dsp, depth, s->algorithm, s->sigma); |
431 |
|
|
#endif |
432 |
|
|
|
433 |
|
✗ |
return 0; |
434 |
|
|
} |
435 |
|
|
|
436 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
437 |
|
|
{ |
438 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
439 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
440 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
441 |
|
|
AVFrame *out, *in; |
442 |
|
|
int i; |
443 |
|
|
|
444 |
|
✗ |
if (s->q.available != s->size) { |
445 |
|
✗ |
if (s->q.available < s->mid) { |
446 |
|
✗ |
for (i = 0; i < s->mid; i++) { |
447 |
|
✗ |
out = av_frame_clone(buf); |
448 |
|
✗ |
if (!out) { |
449 |
|
✗ |
av_frame_free(&buf); |
450 |
|
✗ |
return AVERROR(ENOMEM); |
451 |
|
|
} |
452 |
|
✗ |
ff_bufqueue_add(ctx, &s->q, out); |
453 |
|
|
} |
454 |
|
|
} |
455 |
|
✗ |
if (s->q.available < s->size) { |
456 |
|
✗ |
ff_bufqueue_add(ctx, &s->q, buf); |
457 |
|
✗ |
s->available++; |
458 |
|
|
} |
459 |
|
✗ |
return 0; |
460 |
|
|
} |
461 |
|
|
|
462 |
|
✗ |
in = ff_bufqueue_peek(&s->q, s->mid); |
463 |
|
|
|
464 |
|
✗ |
if (!ctx->is_disabled) { |
465 |
|
|
ThreadData td; |
466 |
|
|
|
467 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
468 |
|
✗ |
if (!out) { |
469 |
|
✗ |
av_frame_free(&buf); |
470 |
|
✗ |
return AVERROR(ENOMEM); |
471 |
|
|
} |
472 |
|
|
|
473 |
|
✗ |
for (i = 0; i < s->size; i++) { |
474 |
|
✗ |
AVFrame *frame = ff_bufqueue_peek(&s->q, i); |
475 |
|
|
|
476 |
|
✗ |
s->data[0][i] = frame->data[0]; |
477 |
|
✗ |
s->data[1][i] = frame->data[1]; |
478 |
|
✗ |
s->data[2][i] = frame->data[2]; |
479 |
|
✗ |
s->linesize[0][i] = frame->linesize[0]; |
480 |
|
✗ |
s->linesize[1][i] = frame->linesize[1]; |
481 |
|
✗ |
s->linesize[2][i] = frame->linesize[2]; |
482 |
|
|
} |
483 |
|
|
|
484 |
|
✗ |
td.in = in; td.out = out; |
485 |
|
✗ |
ff_filter_execute(ctx, s->filter_slice, &td, NULL, |
486 |
|
✗ |
FFMIN3(s->planeheight[1], |
487 |
|
|
s->planeheight[2], |
488 |
|
|
ff_filter_get_nb_threads(ctx))); |
489 |
|
✗ |
av_frame_copy_props(out, in); |
490 |
|
|
} else { |
491 |
|
✗ |
out = av_frame_clone(in); |
492 |
|
✗ |
if (!out) { |
493 |
|
✗ |
av_frame_free(&buf); |
494 |
|
✗ |
return AVERROR(ENOMEM); |
495 |
|
|
} |
496 |
|
|
} |
497 |
|
|
|
498 |
|
✗ |
in = ff_bufqueue_get(&s->q); |
499 |
|
✗ |
av_frame_free(&in); |
500 |
|
✗ |
ff_bufqueue_add(ctx, &s->q, buf); |
501 |
|
|
|
502 |
|
✗ |
return ff_filter_frame(outlink, out); |
503 |
|
|
} |
504 |
|
|
|
505 |
|
✗ |
static int request_frame(AVFilterLink *outlink) |
506 |
|
|
{ |
507 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
508 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
509 |
|
✗ |
int ret = 0; |
510 |
|
|
|
511 |
|
✗ |
ret = ff_request_frame(ctx->inputs[0]); |
512 |
|
|
|
513 |
|
✗ |
if (ret == AVERROR_EOF && !ctx->is_disabled && s->available) { |
514 |
|
✗ |
AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->available)); |
515 |
|
✗ |
if (!buf) |
516 |
|
✗ |
return AVERROR(ENOMEM); |
517 |
|
|
|
518 |
|
✗ |
ret = filter_frame(ctx->inputs[0], buf); |
519 |
|
✗ |
s->available--; |
520 |
|
|
} |
521 |
|
|
|
522 |
|
✗ |
return ret; |
523 |
|
|
} |
524 |
|
|
|
525 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
526 |
|
|
{ |
527 |
|
✗ |
ATADenoiseContext *s = ctx->priv; |
528 |
|
|
|
529 |
|
✗ |
ff_bufqueue_discard_all(&s->q); |
530 |
|
✗ |
} |
531 |
|
|
|
532 |
|
✗ |
static int process_command(AVFilterContext *ctx, |
533 |
|
|
const char *cmd, |
534 |
|
|
const char *arg, |
535 |
|
|
char *res, |
536 |
|
|
int res_len, |
537 |
|
|
int flags) |
538 |
|
|
{ |
539 |
|
✗ |
int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags); |
540 |
|
|
|
541 |
|
✗ |
if (ret < 0) |
542 |
|
✗ |
return ret; |
543 |
|
|
|
544 |
|
✗ |
return config_input(ctx->inputs[0]); |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
static const AVFilterPad inputs[] = { |
548 |
|
|
{ |
549 |
|
|
.name = "default", |
550 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
551 |
|
|
.filter_frame = filter_frame, |
552 |
|
|
.config_props = config_input, |
553 |
|
|
}, |
554 |
|
|
}; |
555 |
|
|
|
556 |
|
|
static const AVFilterPad outputs[] = { |
557 |
|
|
{ |
558 |
|
|
.name = "default", |
559 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
560 |
|
|
.request_frame = request_frame, |
561 |
|
|
}, |
562 |
|
|
}; |
563 |
|
|
|
564 |
|
|
const AVFilter ff_vf_atadenoise = { |
565 |
|
|
.name = "atadenoise", |
566 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply an Adaptive Temporal Averaging Denoiser."), |
567 |
|
|
.priv_size = sizeof(ATADenoiseContext), |
568 |
|
|
.priv_class = &atadenoise_class, |
569 |
|
|
.init = init, |
570 |
|
|
.uninit = uninit, |
571 |
|
|
FILTER_INPUTS(inputs), |
572 |
|
|
FILTER_OUTPUTS(outputs), |
573 |
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts), |
574 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, |
575 |
|
|
.process_command = process_command, |
576 |
|
|
}; |
577 |
|
|
|