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