| @@ -38,11 +38,6 @@ · 37 unmodified lines |
| 38 | #include "vf_nlmeans.h" | 38 | #include "vf_nlmeans.h" |
| 39 | #include "video.h" | 39 | #include "video.h" |
| 40 | | 40 | |
| 41 | struct weighted_avg { | | |
| 42 | float total_weight; | | |
| 43 | float sum; | | |
| 44 | }; | | |
| 45 | | | |
| 46 | typedef struct NLMeansContext { | 41 | typedef struct NLMeansContext { |
| 47 | const AVClass *class; | 42 | const AVClass *class; |
| 48 | int nb_planes; | 43 | int nb_planes; |
| @@ -329,6 +324,58 @@ struct thread_data { · 280 unmodified lines |
| 329 | int p; | 324 | int p; |
| 330 | }; | 325 | }; |
| 331 | | 326 | |
| | 327 | static void compute_weights_line_c(const uint32_t *const iia, |
| | 328 | const uint32_t *const iib, |
| | 329 | const uint32_t *const iid, |
| | 330 | const uint32_t *const iie, |
| | 331 | const uint8_t *const src, |
| | 332 | struct weighted_avg *wa, |
| | 333 | const float *const weight_lut, |
| | 334 | int max_meaningful_diff, |
| | 335 | int startx, int endx) |
| | 336 | { |
| | 337 | for (int x = startx; x < endx; x++) { |
| | 338 | /* |
| | 339 | * M is a discrete map where every entry contains the sum of all the entries |
| | 340 | * in the rectangle from the top-left origin of M to its coordinate. In the |
| | 341 | * following schema, "i" contains the sum of the whole map: |
| | 342 | * |
| | 343 | * M = +----------+-----------------+----+ |
| | 344 | * | | | | |
| | 345 | * | | | | |
| | 346 | * | a| b| c| |
| | 347 | * +----------+-----------------+----+ |
| | 348 | * | | | | |
| | 349 | * | | | | |
| | 350 | * | | X | | |
| | 351 | * | | | | |
| | 352 | * | d| e| f| |
| | 353 | * +----------+-----------------+----+ |
| | 354 | * | | | | |
| | 355 | * | g| h| i| |
| | 356 | * +----------+-----------------+----+ |
| | 357 | * |
| | 358 | * The sum of the X box can be calculated with: |
| | 359 | * X = e-d-b+a |
| | 360 | * |
| | 361 | * See https://en.wikipedia.org/wiki/Summed_area_table |
| | 362 | * |
| | 363 | * The compute*_ssd functions compute the integral image M where every entry |
| | 364 | * contains the sum of the squared difference of every corresponding pixels of |
| | 365 | * two input planes of the same size as M. |
| | 366 | */ |
| | 367 | const uint32_t a = iia[x]; |
| | 368 | const uint32_t b = iib[x]; |
| | 369 | const uint32_t d = iid[x]; |
| | 370 | const uint32_t e = iie[x]; |
| | 371 | const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff); |
| | 372 | const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) |
| | 373 | |
| | 374 | wa[x].total_weight += weight; |
| | 375 | wa[x].sum += weight * src[x]; |
| | 376 | } |
| | 377 | } |
| | 378 | |
| 332 | static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | 379 | static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 333 | { | 380 | { |
| 334 | NLMeansContext *s = ctx->priv; | 381 | NLMeansContext *s = ctx->priv; |
| @@ -346,50 +393,19 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs · 11 unmodified lines |
| 346 | const int dist_d = dist_b * s->ii_lz_32; | 393 | const int dist_d = dist_b * s->ii_lz_32; |
| 347 | const int dist_e = dist_d + dist_b; | 394 | const int dist_e = dist_d + dist_b; |
| 348 | const float *const weight_lut = s->weight_lut; | 395 | const float *const weight_lut = s->weight_lut; |
| | 396 | NLMeansDSPContext *dsp = &s->dsp; |
| 349 | | 397 | |
| 350 | for (int y = starty; y < endy; y++) { | 398 | for (int y = starty; y < endy; y++) { |
| 351 | const uint8_t *src = td->src + y*src_linesize; | 399 | const uint8_t *const src = td->src + y*src_linesize; |
| 352 | struct weighted_avg *wa = s->wa + y*s->wa_linesize; | 400 | struct weighted_avg *wa = s->wa + y*s->wa_linesize; |
| 353 | for (int x = td->startx; x < td->endx; x++) { | 401 | const uint32_t *const iia = ii; |
| 354 | /* | 402 | const uint32_t *const iib = ii + dist_b; |
| 355 | * M is a discrete map where every entry contains the sum of all the entries | 403 | const uint32_t *const iid = ii + dist_d; |
| 356 | * in the rectangle from the top-left origin of M to its coordinate. In the | 404 | const uint32_t *const iie = ii + dist_e; |
| 357 | * following schema, "i" contains the sum of the whole map: | 405 | |
| 358 | * | 406 | dsp->compute_weights_line(iia, iib, iid, iie, src, wa, |
| 359 | * M = +----------+-----------------+----+ | 407 | weight_lut, max_meaningful_diff, |
| 360 | * | | | | | 408 | td->startx, td->endx); |
| 361 | * | | | | | | |
| 362 | * | a| b| c| | | |
| 363 | * +----------+-----------------+----+ | | |
| 364 | * | | | | | | |
| 365 | * | | | | | | |
| 366 | * | | X | | | | |
| 367 | * | | | | | | |
| 368 | * | d| e| f| | | |
| 369 | * +----------+-----------------+----+ | | |
| 370 | * | | | | | | |
| 371 | * | g| h| i| | | |
| 372 | * +----------+-----------------+----+ | | |
| 373 | * | | |
| 374 | * The sum of the X box can be calculated with: | | |
| 375 | * X = e-d-b+a | | |
| 376 | * | | |
| 377 | * See https://en.wikipedia.org/wiki/Summed_area_table | | |
| 378 | * | | |
| 379 | * The compute*_ssd functions compute the integral image M where every entry | | |
| 380 | * contains the sum of the squared difference of every corresponding pixels of | | |
| 381 | * two input planes of the same size as M. | | |
| 382 | */ | | |
| 383 | const uint32_t a = ii[x]; | | |
| 384 | const uint32_t b = ii[x + dist_b]; | | |
| 385 | const uint32_t d = ii[x + dist_d]; | | |
| 386 | const uint32_t e = ii[x + dist_e]; | | |
| 387 | const uint32_t patch_diff_sq = FFMIN(e - d - b + a, max_meaningful_diff); | | |
| 388 | const float weight = weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale) | | |
| 389 | | | |
| 390 | wa[x].total_weight += weight; | | |
| 391 | wa[x].sum += weight * src[x]; | | |
| 392 | } | | |
| 393 | ii += s->ii_lz_32; | 409 | ii += s->ii_lz_32; |
| 394 | } | 410 | } |
| 395 | return 0; | 411 | return 0; |
| @@ -493,6 +509,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 97 unmodified lines |
| 493 | void ff_nlmeans_init(NLMeansDSPContext *dsp) | 509 | void ff_nlmeans_init(NLMeansDSPContext *dsp) |
| 494 | { | 510 | { |
| 495 | dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c; | 511 | dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c; |
| | 512 | dsp->compute_weights_line = compute_weights_line_c; |
| 496 | | 513 | |
| 497 | if (ARCH_AARCH64) | 514 | if (ARCH_AARCH64) |
| 498 | ff_nlmeans_init_aarch64(dsp); | 515 | ff_nlmeans_init_aarch64(dsp); |