1 /*
2 * Copyright (c) 2016 Paul B Mahol
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
31
34
38
45 uint16_t
lut[256 * 256 * 256];
47
50
51 #define OFFSET(x) offsetof(AverageBlurContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
53
59 };
60
62
70
71 #define LUT_DIV(sum, area) (lut[(sum)])
72 #define SLOW_DIV(sum, area) ((sum) / (area))
73
74 #define FILTER(name, type, btype, lutunused, areaunused, lutdiv) \
75 static int filter_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
76 { \
77 AverageBlurContext *s = ctx->priv; \
78 ThreadData *td = arg; \
79 areaunused const int area = s->area; \
80 lutunused const uint16_t *lut = s->lut; \
81 const int size_w = s->radius; \
82 const int size_h = s->radiusV; \
83 btype *col_sum = (btype *)s->buffer + size_w; \
84 const int dlinesize = td->dlinesize / sizeof(type); \
85 const int linesize = td->linesize / sizeof(type); \
86 const int height = td->height; \
87 const int width = td->width; \
88 const type *src = td->ptr; \
89 type *dst = td->dptr; \
90 btype sum = 0; \
91 \
92 for (int x = -size_w; x < 0; x++) { \
93 sum = src[0] * size_h; \
94 for (int y = 0; y <= size_h; y++) \
95 sum += src[y * linesize]; \
96 av_assert2(sum >= 0); \
97 col_sum[x] = sum; \
98 } \
99 \
100 for (int x = 0; x < width; x++) { \
101 sum = src[x] * size_h; \
102 for (int y = 0; y <= size_h; y++) \
103 sum += src[x + y * linesize]; \
104 av_assert2(sum >= 0); \
105 col_sum[x] = sum; \
106 } \
107 \
108 for (int x = width; x < width + size_w; x++) { \
109 sum = src[width - 1] * size_h; \
110 for (int y = 0; y <= size_h; y++) \
111 sum += src[width - 1 + y * linesize]; \
112 av_assert2(sum >= 0); \
113 col_sum[x] = sum; \
114 } \
115 \
116 sum = 0; \
117 for (int x = -size_w; x <= size_w; x++) \
118 sum += col_sum[x]; \
119 av_assert2(sum >= 0); \
120 dst[0] = lutdiv(sum, area); \
121 \
122 for (int x = 1; x < width; x++) { \
123 sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
124 av_assert2(sum >= 0); \
125 dst[x] = lutdiv(sum, area); \
126 } \
127 \
128 src = td->ptr; \
129 src += linesize; \
130 dst += dlinesize; \
131 \
132 for (int y = 1; y < height; y++) { \
133 const int syp = FFMIN(size_h, height - y - 1) * linesize; \
134 const int syn = FFMIN(y, size_h + 1) * linesize; \
135 \
136 sum = 0; \
137 \
138 for (int x = -size_w; x < 0; x++) \
139 col_sum[x] += src[0 + syp] - src[0 - syn]; \
140 \
141 for (int x = 0; x < width; x++) \
142 col_sum[x] += src[x + syp] - src[x - syn]; \
143 \
144 for (int x = width; x < width + size_w; x++) \
145 col_sum[x] += src[width - 1 + syp] - src[width - 1 - syn]; \
146 \
147 for (int x = -size_w; x <= size_w; x++) \
148 sum += col_sum[x]; \
149 av_assert2(sum >= 0); \
150 dst[0] = lutdiv(sum, area); \
151 \
152 for (int x = 1; x < width; x++) { \
153 sum = sum - col_sum[x - size_w - 1] + col_sum[x + size_w]; \
154 av_assert2(sum >= 0); \
155 dst[x] = lutdiv(sum, area); \
156 } \
157 \
158 src += linesize; \
159 dst += dlinesize; \
160 } \
161 \
162 return 0; \
163 }
164
167
170
172 {
174 const int area = (2 *
s->radiusV + 1) * (2 *
s->radius + 1);
175
178 return;
179
180 for (
int i = 0, j = 0, k = 0;
i <
max * area;
i++, j++) {
181 if (j == area) {
182 k++;
183 j = 0;
184 }
185
187 }
188 }
189
191 {
193
195 }
196
198 {
202
204
205 s->depth =
desc->comp[0].depth;
206 s->max = 1 <<
s->depth;
208 s->planewidth[0] =
s->planewidth[3] =
inlink->w;
210 s->planeheight[0] =
s->planeheight[3] =
inlink->h;
211
213
217
219 s->radiusV =
s->radius;
220
221 s->filter[0] =
s->depth <= 8 ? filter_lut8 : filter_lut16;
222 s->filter[1] =
s->depth <= 8 ? filter_slow8 : filter_slow16;
223
224 s->radius =
FFMIN(
s->planewidth[1] / 2,
s->radius);
225 s->radiusV =
FFMIN(
s->planeheight[1] / 2,
s->radiusV);
226
228
229 return 0;
230 }
231
233 {
235 const int width =
s->planewidth[plane];
236 const int height =
s->planeheight[plane];
239
246 s->filter[slow](
ctx, &td, 0, 0);
247 }
248
269 };
270
272 {
277 int plane;
278
283 }
285
286 for (plane = 0; plane <
s->nb_planes; plane++) {
287 const int height =
s->planeheight[plane];
288 const int width =
s->planewidth[plane];
289
290 if (!(
s->planes & (1 << plane))) {
291 if (
out->data[plane] != in->
data[plane])
295 continue;
296 }
297
299 }
300
303 }
304
306 char *res,
int res_len,
int flags)
307 {
309 const int area =
s->area;
311
315
317 s->radiusV =
s->radius;
318
319 s->radius =
FFMIN(
s->planewidth[1] / 2,
s->radius);
320 s->radiusV =
FFMIN(
s->planeheight[1] / 2,
s->radiusV);
321
322 if (area != (2 *
s->radiusV + 1) * (2 *
s->radius + 1))
324
325 return 0;
326 }
327
329 {
334 },
335 };
336
340 .p.priv_class = &avgblur_class,
348 };