2 * Copyright (c) 2019 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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 * Redistribution and use in source and binary forms, with or without modification,
20 * are permitted provided that the following conditions are met:
23 #include "libavutil/avassert.h"
32 #define pixel uint16_t
36 #define htype uint16_t
41 #define SHIFT ((DEPTH + 1) / 2)
42 #define BINS (1 << SHIFT)
43 #define MASK (BINS - 1)
44 #define fn3(a,b) a##_##b
45 #define fn2(a,b) fn3(a,b)
46 #define fn(a) fn2(a, DEPTH)
48 #define PICK_COARSE_BIN(x, y) (BINS * (x) + ((y) >> SHIFT))
49 #define PICK_FINE_BIN(x, y, z) (BINS * ((x) * ((y) >> SHIFT) + (z)) + ((y) & MASK))
51 static void fn(filter_plane)(AVFilterContext *ctx, const uint8_t *ssrc, int src_linesize,
52 uint8_t *ddst, int dst_linesize, int width, int height,
53 int slice_h_start, int slice_h_end, int jobnr)
55 MedianContext *s = ctx->priv;
56 htype *ccoarse = s->coarse[jobnr];
57 htype *cfine = s->fine[jobnr];
58 const int radius = s->radius;
59 const int radiusV = s->radiusV;
61 const pixel *src = (const pixel *)ssrc;
62 pixel *dst = (pixel *)ddst;
66 src_linesize /= sizeof(pixel);
67 dst_linesize /= sizeof(pixel);
69 memset(cfine, 0, s->fine_size * sizeof(*cfine));
70 memset(ccoarse, 0, s->coarse_size * sizeof(*ccoarse));
72 srcp = src + FFMAX(0, slice_h_start - radiusV) * src_linesize;
74 for (int i = 0; i < width; i++) {
75 cfine[PICK_FINE_BIN(width, srcp[i], i)] += radiusV + 1;
76 ccoarse[PICK_COARSE_BIN(i, srcp[i])] += radiusV + 1;
80 srcp = src + FFMAX(0, slice_h_start - radiusV - (jobnr != 0)) * src_linesize;
81 for (int i = 0; i < radiusV + (jobnr != 0) * (1 + radiusV); i++) {
82 for (int j = 0; j < width; j++) {
83 cfine[PICK_FINE_BIN(width, srcp[j], j)]++;
84 ccoarse[PICK_COARSE_BIN(j, srcp[j])]++;
91 for (int i = slice_h_start; i < slice_h_end; i++) {
92 htype coarse[BINS] = { 0 };
93 htype fine[BINS][BINS] = { { 0 } };
94 htype luc[BINS] = { 0 };
96 p = srcp + src_linesize * FFMAX(0, i - radiusV - 1);
97 for (int j = 0; j < width; j++) {
98 cfine[PICK_FINE_BIN(width, p[j], j)]--;
99 ccoarse[PICK_COARSE_BIN(j, p[j])]--;
102 p = srcp + src_linesize * FFMIN(height - 1, i + radiusV);
103 for (int j = 0; j < width; j++) {
104 cfine[PICK_FINE_BIN(width, p[j], j)]++;
105 ccoarse[PICK_COARSE_BIN(j, p[j])]++;
108 s->hmuladd(coarse, &ccoarse[0], radius, BINS);
109 for (int j = 0; j < radius; j++)
110 s->hadd(coarse, &ccoarse[BINS * j], BINS);
111 for (int k = 0; k < BINS; k++)
112 s->hmuladd(&fine[k][0], &cfine[BINS * width * k], 2 * radius + 1, BINS);
114 for (int j = 0; j < width; j++) {
118 s->hadd(coarse, &ccoarse[BINS * FFMIN(j + radius, width - 1)], BINS);
120 for (k = 0; k < BINS; k++) {
127 av_assert0(k < BINS);
129 if (luc[k] <= j - radius) {
130 memset(&fine[k], 0, BINS * sizeof(htype));
131 for (luc[k] = j - radius; luc[k] < FFMIN(j + radius + 1, width); luc[k]++)
132 s->hadd(fine[k], &cfine[BINS * (width * k + luc[k])], BINS);
133 if (luc[k] < j + radius + 1) {
134 s->hmuladd(&fine[k][0], &cfine[BINS * (width * k + width - 1)], j + radius + 1 - width, BINS);
135 luc[k] = j + radius + 1;
138 for (; luc[k] < j + radius + 1; luc[k]++) {
139 s->hsub(fine[k], &cfine[BINS * (width * k + FFMAX(luc[k] - 2 * radius - 1, 0))], BINS);
140 s->hadd(fine[k], &cfine[BINS * (width * k + FFMIN(luc[k], width - 1))], BINS);
144 s->hsub(coarse, &ccoarse[BINS * FFMAX(j - radius, 0)], BINS);
147 for (b = 0; b < BINS; b++) {
150 dst[j] = BINS * k + b;
154 av_assert0(b < BINS);