2 * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
28 #include "convolution.h"
33 #define OFFSET(x) offsetof(ConvolutionContext, x)
34 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
36 static const AVOption convolution_options[] = {
37 { "0m", "set matrix for 1st plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
38 { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
39 { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
40 { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
41 { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
42 { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
43 { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
44 { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
45 { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
46 { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
47 { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
48 { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
49 { "0mode", "set matrix mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
50 { "1mode", "set matrix mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
51 { "2mode", "set matrix mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
52 { "3mode", "set matrix mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
53 { "square", "square matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_SQUARE}, 0, 0, FLAGS, "mode" },
54 { "row", "single row matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_ROW} , 0, 0, FLAGS, "mode" },
55 { "column", "single column matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_COLUMN}, 0, 0, FLAGS, "mode" },
59 AVFILTER_DEFINE_CLASS(convolution);
61 static const int same3x3[9] = {0, 0, 0,
65 static const int same5x5[25] = {0, 0, 0, 0, 0,
71 static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
79 static int query_formats(AVFilterContext *ctx)
81 static const enum AVPixelFormat pix_fmts[] = {
82 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
83 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
84 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
85 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
86 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
87 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
88 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
89 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
90 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
91 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
92 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
93 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
94 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
95 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
96 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
97 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
98 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
99 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
103 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
106 typedef struct ThreadData {
110 static void filter16_prewitt(uint8_t *dstp, int width,
111 float scale, float delta, const int *const matrix,
112 const uint8_t *c[], int peak, int radius,
113 int dstride, int stride)
115 uint16_t *dst = (uint16_t *)dstp;
118 for (x = 0; x < width; x++) {
119 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
120 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + AV_RN16A(&c[8][2 * x]) * 1;
121 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1 +
122 AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
124 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
128 static void filter16_roberts(uint8_t *dstp, int width,
129 float scale, float delta, const int *const matrix,
130 const uint8_t *c[], int peak, int radius,
131 int dstride, int stride)
133 uint16_t *dst = (uint16_t *)dstp;
136 for (x = 0; x < width; x++) {
137 float suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
138 float sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
140 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
144 static void filter16_sobel(uint8_t *dstp, int width,
145 float scale, float delta, const int *const matrix,
146 const uint8_t *c[], int peak, int radius,
147 int dstride, int stride)
149 uint16_t *dst = (uint16_t *)dstp;
152 for (x = 0; x < width; x++) {
153 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
154 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
155 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
156 AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
158 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
162 static void filter16_kirsch(uint8_t *dstp, int width,
163 float scale, float delta, const int *const matrix,
164 const uint8_t *c[], int peak, int radius,
165 int dstride, int stride)
167 uint16_t *dst = (uint16_t *)dstp;
168 const uint16_t *c0 = (const uint16_t *)c[0], *c1 = (const uint16_t *)c[1], *c2 = (const uint16_t *)c[2];
169 const uint16_t *c3 = (const uint16_t *)c[3], *c5 = (const uint16_t *)c[5];
170 const uint16_t *c6 = (const uint16_t *)c[6], *c7 = (const uint16_t *)c[7], *c8 = (const uint16_t *)c[8];
173 for (x = 0; x < width; x++) {
174 int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
175 c3[x] * -3 + c5[x] * -3 +
176 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
177 int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
178 c3[x] * 5 + c5[x] * -3 +
179 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
180 int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
181 c3[x] * 5 + c5[x] * 5 +
182 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
183 int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
184 c3[x] * 5 + c5[x] * 5 +
185 c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
186 int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
187 c3[x] * -3 + c5[x] * 5 +
188 c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
189 int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
190 c3[x] * -3 + c5[x] * -3 +
191 c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
192 int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
193 c3[x] * -3 + c5[x] * -3 +
194 c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
195 int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
196 c3[x] * -3 + c5[x] * -3 +
197 c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
199 sum0 = FFMAX(sum0, sum1);
200 sum2 = FFMAX(sum2, sum3);
201 sum4 = FFMAX(sum4, sum5);
202 sum6 = FFMAX(sum6, sum7);
203 sum0 = FFMAX(sum0, sum2);
204 sum4 = FFMAX(sum4, sum6);
205 sum0 = FFMAX(sum0, sum4);
207 dst[x] = av_clip(FFABS(sum0) * scale + delta, 0, peak);
211 static void filter_prewitt(uint8_t *dst, int width,
212 float scale, float delta, const int *const matrix,
213 const uint8_t *c[], int peak, int radius,
214 int dstride, int stride)
216 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
217 const uint8_t *c3 = c[3], *c5 = c[5];
218 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
221 for (x = 0; x < width; x++) {
222 float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
223 c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
224 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
225 c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
227 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
231 static void filter_roberts(uint8_t *dst, int width,
232 float scale, float delta, const int *const matrix,
233 const uint8_t *c[], int peak, int radius,
234 int dstride, int stride)
238 for (x = 0; x < width; x++) {
239 float suma = c[0][x] * 1 + c[1][x] * -1;
240 float sumb = c[4][x] * 1 + c[3][x] * -1;
242 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
246 static void filter_sobel(uint8_t *dst, int width,
247 float scale, float delta, const int *const matrix,
248 const uint8_t *c[], int peak, int radius,
249 int dstride, int stride)
251 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
252 const uint8_t *c3 = c[3], *c5 = c[5];
253 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
256 for (x = 0; x < width; x++) {
257 float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
258 c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
259 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
260 c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
262 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
266 static void filter_kirsch(uint8_t *dst, int width,
267 float scale, float delta, const int *const matrix,
268 const uint8_t *c[], int peak, int radius,
269 int dstride, int stride)
271 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
272 const uint8_t *c3 = c[3], *c5 = c[5];
273 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
276 for (x = 0; x < width; x++) {
277 int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
278 c3[x] * -3 + c5[x] * -3 +
279 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
280 int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
281 c3[x] * 5 + c5[x] * -3 +
282 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
283 int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
284 c3[x] * 5 + c5[x] * 5 +
285 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
286 int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
287 c3[x] * 5 + c5[x] * 5 +
288 c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
289 int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
290 c3[x] * -3 + c5[x] * 5 +
291 c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
292 int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
293 c3[x] * -3 + c5[x] * -3 +
294 c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
295 int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
296 c3[x] * -3 + c5[x] * -3 +
297 c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
298 int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
299 c3[x] * -3 + c5[x] * -3 +
300 c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
302 sum0 = FFMAX(sum0, sum1);
303 sum2 = FFMAX(sum2, sum3);
304 sum4 = FFMAX(sum4, sum5);
305 sum6 = FFMAX(sum6, sum7);
306 sum0 = FFMAX(sum0, sum2);
307 sum4 = FFMAX(sum4, sum6);
308 sum0 = FFMAX(sum0, sum4);
310 dst[x] = av_clip_uint8(FFABS(sum0) * scale + delta);
314 static void filter16_3x3(uint8_t *dstp, int width,
315 float rdiv, float bias, const int *const matrix,
316 const uint8_t *c[], int peak, int radius,
317 int dstride, int stride)
319 uint16_t *dst = (uint16_t *)dstp;
322 for (x = 0; x < width; x++) {
323 int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
324 AV_RN16A(&c[1][2 * x]) * matrix[1] +
325 AV_RN16A(&c[2][2 * x]) * matrix[2] +
326 AV_RN16A(&c[3][2 * x]) * matrix[3] +
327 AV_RN16A(&c[4][2 * x]) * matrix[4] +
328 AV_RN16A(&c[5][2 * x]) * matrix[5] +
329 AV_RN16A(&c[6][2 * x]) * matrix[6] +
330 AV_RN16A(&c[7][2 * x]) * matrix[7] +
331 AV_RN16A(&c[8][2 * x]) * matrix[8];
332 sum = (int)(sum * rdiv + bias + 0.5f);
333 dst[x] = av_clip(sum, 0, peak);
337 static void filter16_5x5(uint8_t *dstp, int width,
338 float rdiv, float bias, const int *const matrix,
339 const uint8_t *c[], int peak, int radius,
340 int dstride, int stride)
342 uint16_t *dst = (uint16_t *)dstp;
345 for (x = 0; x < width; x++) {
348 for (i = 0; i < 25; i++)
349 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
351 sum = (int)(sum * rdiv + bias + 0.5f);
352 dst[x] = av_clip(sum, 0, peak);
356 static void filter16_7x7(uint8_t *dstp, int width,
357 float rdiv, float bias, const int *const matrix,
358 const uint8_t *c[], int peak, int radius,
359 int dstride, int stride)
361 uint16_t *dst = (uint16_t *)dstp;
364 for (x = 0; x < width; x++) {
367 for (i = 0; i < 49; i++)
368 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
370 sum = (int)(sum * rdiv + bias + 0.5f);
371 dst[x] = av_clip(sum, 0, peak);
375 static void filter16_row(uint8_t *dstp, int width,
376 float rdiv, float bias, const int *const matrix,
377 const uint8_t *c[], int peak, int radius,
378 int dstride, int stride)
380 uint16_t *dst = (uint16_t *)dstp;
383 for (x = 0; x < width; x++) {
386 for (i = 0; i < 2 * radius + 1; i++)
387 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
389 sum = (int)(sum * rdiv + bias + 0.5f);
390 dst[x] = av_clip(sum, 0, peak);
394 static void filter16_column(uint8_t *dstp, int height,
395 float rdiv, float bias, const int *const matrix,
396 const uint8_t *c[], int peak, int radius,
397 int dstride, int stride)
399 uint16_t *dst = (uint16_t *)dstp;
402 for (y = 0; y < height; y++) {
405 for (i = 0; i < 2 * radius + 1; i++)
406 sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
408 sum = (int)(sum * rdiv + bias + 0.5f);
409 dst[0] = av_clip(sum, 0, peak);
414 static void filter_7x7(uint8_t *dst, int width,
415 float rdiv, float bias, const int *const matrix,
416 const uint8_t *c[], int peak, int radius,
417 int dstride, int stride)
421 for (x = 0; x < width; x++) {
424 for (i = 0; i < 49; i++)
425 sum += c[i][x] * matrix[i];
427 sum = (int)(sum * rdiv + bias + 0.5f);
428 dst[x] = av_clip_uint8(sum);
432 static void filter_5x5(uint8_t *dst, int width,
433 float rdiv, float bias, const int *const matrix,
434 const uint8_t *c[], int peak, int radius,
435 int dstride, int stride)
439 for (x = 0; x < width; x++) {
442 for (i = 0; i < 25; i++)
443 sum += c[i][x] * matrix[i];
445 sum = (int)(sum * rdiv + bias + 0.5f);
446 dst[x] = av_clip_uint8(sum);
450 static void filter_3x3(uint8_t *dst, int width,
451 float rdiv, float bias, const int *const matrix,
452 const uint8_t *c[], int peak, int radius,
453 int dstride, int stride)
455 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
456 const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
457 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
460 for (x = 0; x < width; x++) {
461 int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
462 c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
463 c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
464 sum = (int)(sum * rdiv + bias + 0.5f);
465 dst[x] = av_clip_uint8(sum);
469 static void filter_row(uint8_t *dst, int width,
470 float rdiv, float bias, const int *const matrix,
471 const uint8_t *c[], int peak, int radius,
472 int dstride, int stride)
476 for (x = 0; x < width; x++) {
479 for (i = 0; i < 2 * radius + 1; i++)
480 sum += c[i][x] * matrix[i];
482 sum = (int)(sum * rdiv + bias + 0.5f);
483 dst[x] = av_clip_uint8(sum);
487 static void filter_column(uint8_t *dst, int height,
488 float rdiv, float bias, const int *const matrix,
489 const uint8_t *c[], int peak, int radius,
490 int dstride, int stride)
494 for (y = 0; y < height; y++) {
497 for (i = 0; i < 2 * radius + 1; i++)
498 sum += c[i][0 + y * stride] * matrix[i];
500 sum = (int)(sum * rdiv + bias + 0.5f);
501 dst[0] = av_clip_uint8(sum);
506 static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
507 int x, int w, int y, int h, int bpc)
511 for (i = 0; i < 9; i++) {
512 int xoff = FFABS(x + ((i % 3) - 1));
513 int yoff = FFABS(y + (i / 3) - 1);
515 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
516 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
518 c[i] = src + xoff * bpc + yoff * stride;
522 static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
523 int x, int w, int y, int h, int bpc)
527 for (i = 0; i < 25; i++) {
528 int xoff = FFABS(x + ((i % 5) - 2));
529 int yoff = FFABS(y + (i / 5) - 2);
531 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
532 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
534 c[i] = src + xoff * bpc + yoff * stride;
538 static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
539 int x, int w, int y, int h, int bpc)
543 for (i = 0; i < 49; i++) {
544 int xoff = FFABS(x + ((i % 7) - 3));
545 int yoff = FFABS(y + (i / 7) - 3);
547 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
548 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
550 c[i] = src + xoff * bpc + yoff * stride;
554 static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
555 int x, int w, int y, int h, int bpc)
559 for (i = 0; i < radius * 2 + 1; i++) {
560 int xoff = FFABS(x + i - radius);
562 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
564 c[i] = src + xoff * bpc + y * stride;
568 static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
569 int x, int w, int y, int h, int bpc)
573 for (i = 0; i < radius * 2 + 1; i++) {
574 int xoff = FFABS(x + i - radius);
576 xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
578 c[i] = src + y * bpc + xoff * stride;
582 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
584 ConvolutionContext *s = ctx->priv;
585 ThreadData *td = arg;
586 AVFrame *in = td->in;
587 AVFrame *out = td->out;
590 for (plane = 0; plane < s->nb_planes; plane++) {
591 const int mode = s->mode[plane];
592 const int bpc = s->bpc;
593 const int radius = s->size[plane] / 2;
594 const int height = s->planeheight[plane];
595 const int width = s->planewidth[plane];
596 const int stride = in->linesize[plane];
597 const int dstride = out->linesize[plane];
598 const int sizeh = mode == MATRIX_COLUMN ? width : height;
599 const int sizew = mode == MATRIX_COLUMN ? height : width;
600 const int slice_start = (sizeh * jobnr) / nb_jobs;
601 const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
602 const float rdiv = s->rdiv[plane];
603 const float bias = s->bias[plane];
604 const uint8_t *src = in->data[plane];
605 const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
606 uint8_t *dst = out->data[plane] + dst_pos;
607 const int *matrix = s->matrix[plane];
608 const uint8_t *c[49];
611 if (s->copy[plane]) {
612 if (mode == MATRIX_COLUMN)
613 av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
614 (slice_end - slice_start) * bpc, height);
616 av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
617 width * bpc, slice_end - slice_start);
621 for (y = slice_start; y < slice_end; y++) {
622 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
623 const int yoff = mode == MATRIX_COLUMN ? radius * dstride : 0;
625 for (x = 0; x < radius; x++) {
626 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
627 const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
629 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
630 s->filter[plane](dst + yoff + xoff, 1, rdiv,
631 bias, matrix, c, s->max, radius,
634 s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
635 s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
636 rdiv, bias, matrix, c, s->max, radius,
638 for (x = sizew - radius; x < sizew; x++) {
639 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
640 const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
642 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
643 s->filter[plane](dst + yoff + xoff, 1, rdiv,
644 bias, matrix, c, s->max, radius,
647 if (mode != MATRIX_COLUMN)
655 static int config_input(AVFilterLink *inlink)
657 AVFilterContext *ctx = inlink->dst;
658 ConvolutionContext *s = ctx->priv;
659 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
662 s->depth = desc->comp[0].depth;
663 s->max = (1 << s->depth) - 1;
665 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
666 s->planewidth[0] = s->planewidth[3] = inlink->w;
667 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
668 s->planeheight[0] = s->planeheight[3] = inlink->h;
670 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
671 s->nb_threads = ff_filter_get_nb_threads(ctx);
672 s->bpc = (s->depth + 7) / 8;
674 if (!strcmp(ctx->filter->name, "convolution")) {
676 for (p = 0; p < s->nb_planes; p++) {
677 if (s->mode[p] == MATRIX_ROW)
678 s->filter[p] = filter16_row;
679 else if (s->mode[p] == MATRIX_COLUMN)
680 s->filter[p] = filter16_column;
681 else if (s->size[p] == 3)
682 s->filter[p] = filter16_3x3;
683 else if (s->size[p] == 5)
684 s->filter[p] = filter16_5x5;
685 else if (s->size[p] == 7)
686 s->filter[p] = filter16_7x7;
689 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
690 ff_convolution_init_x86(s);
692 } else if (!strcmp(ctx->filter->name, "prewitt")) {
694 for (p = 0; p < s->nb_planes; p++)
695 s->filter[p] = filter16_prewitt;
696 } else if (!strcmp(ctx->filter->name, "roberts")) {
698 for (p = 0; p < s->nb_planes; p++)
699 s->filter[p] = filter16_roberts;
700 } else if (!strcmp(ctx->filter->name, "sobel")) {
702 for (p = 0; p < s->nb_planes; p++)
703 s->filter[p] = filter16_sobel;
704 } else if (!strcmp(ctx->filter->name, "kirsch")) {
706 for (p = 0; p < s->nb_planes; p++)
707 s->filter[p] = filter16_kirsch;
713 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
715 AVFilterContext *ctx = inlink->dst;
716 ConvolutionContext *s = ctx->priv;
717 AVFilterLink *outlink = ctx->outputs[0];
721 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
724 return AVERROR(ENOMEM);
726 av_frame_copy_props(out, in);
730 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
733 return ff_filter_frame(outlink, out);
736 static av_cold int init(AVFilterContext *ctx)
738 ConvolutionContext *s = ctx->priv;
741 if (!strcmp(ctx->filter->name, "convolution")) {
742 for (i = 0; i < 4; i++) {
743 int *matrix = (int *)s->matrix[i];
744 char *p, *arg, *saveptr = NULL;
747 p = s->matrix_str[i];
749 s->matrix_length[i] = 0;
751 while (s->matrix_length[i] < 49) {
752 if (!(arg = av_strtok(p, " |", &saveptr)))
756 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
757 sum += matrix[s->matrix_length[i]];
758 s->matrix_length[i]++;
761 if (!(s->matrix_length[i] & 1)) {
762 av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
763 return AVERROR(EINVAL);
767 if (s->mode[i] == MATRIX_ROW) {
768 s->filter[i] = filter_row;
769 s->setup[i] = setup_row;
770 s->size[i] = s->matrix_length[i];
771 } else if (s->mode[i] == MATRIX_COLUMN) {
772 s->filter[i] = filter_column;
773 s->setup[i] = setup_column;
774 s->size[i] = s->matrix_length[i];
775 } else if (s->matrix_length[i] == 9) {
778 if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
781 s->filter[i] = filter_3x3;
784 s->setup[i] = setup_3x3;
785 } else if (s->matrix_length[i] == 25) {
787 if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
790 s->filter[i] = filter_5x5;
793 s->setup[i] = setup_5x5;
794 } else if (s->matrix_length[i] == 49) {
796 if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
799 s->filter[i] = filter_7x7;
802 s->setup[i] = setup_7x7;
804 return AVERROR(EINVAL);
810 s->rdiv[i] = 1. / sum;
812 if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
815 } else if (!strcmp(ctx->filter->name, "prewitt")) {
816 for (i = 0; i < 4; i++) {
817 if ((1 << i) & s->planes)
818 s->filter[i] = filter_prewitt;
822 s->setup[i] = setup_3x3;
823 s->rdiv[i] = s->scale;
824 s->bias[i] = s->delta;
826 } else if (!strcmp(ctx->filter->name, "roberts")) {
827 for (i = 0; i < 4; i++) {
828 if ((1 << i) & s->planes)
829 s->filter[i] = filter_roberts;
833 s->setup[i] = setup_3x3;
834 s->rdiv[i] = s->scale;
835 s->bias[i] = s->delta;
837 } else if (!strcmp(ctx->filter->name, "sobel")) {
838 for (i = 0; i < 4; i++) {
839 if ((1 << i) & s->planes)
840 s->filter[i] = filter_sobel;
844 s->setup[i] = setup_3x3;
845 s->rdiv[i] = s->scale;
846 s->bias[i] = s->delta;
848 } else if (!strcmp(ctx->filter->name, "kirsch")) {
849 for (i = 0; i < 4; i++) {
850 if ((1 << i) & s->planes)
851 s->filter[i] = filter_kirsch;
855 s->setup[i] = setup_3x3;
856 s->rdiv[i] = s->scale;
857 s->bias[i] = s->delta;
864 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
865 char *res, int res_len, int flags)
869 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
876 static const AVFilterPad convolution_inputs[] = {
879 .type = AVMEDIA_TYPE_VIDEO,
880 .config_props = config_input,
881 .filter_frame = filter_frame,
886 static const AVFilterPad convolution_outputs[] = {
889 .type = AVMEDIA_TYPE_VIDEO,
894 #if CONFIG_CONVOLUTION_FILTER
896 AVFilter ff_vf_convolution = {
897 .name = "convolution",
898 .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
899 .priv_size = sizeof(ConvolutionContext),
900 .priv_class = &convolution_class,
902 .query_formats = query_formats,
903 .inputs = convolution_inputs,
904 .outputs = convolution_outputs,
905 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
906 .process_command = process_command,
909 #endif /* CONFIG_CONVOLUTION_FILTER */
911 #if CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER
913 static const AVOption prewitt_roberts_sobel_options[] = {
914 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
915 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
916 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
920 #if CONFIG_PREWITT_FILTER
922 #define prewitt_options prewitt_roberts_sobel_options
923 AVFILTER_DEFINE_CLASS(prewitt);
925 AVFilter ff_vf_prewitt = {
927 .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
928 .priv_size = sizeof(ConvolutionContext),
929 .priv_class = &prewitt_class,
931 .query_formats = query_formats,
932 .inputs = convolution_inputs,
933 .outputs = convolution_outputs,
934 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
935 .process_command = process_command,
938 #endif /* CONFIG_PREWITT_FILTER */
940 #if CONFIG_SOBEL_FILTER
942 #define sobel_options prewitt_roberts_sobel_options
943 AVFILTER_DEFINE_CLASS(sobel);
945 AVFilter ff_vf_sobel = {
947 .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
948 .priv_size = sizeof(ConvolutionContext),
949 .priv_class = &sobel_class,
951 .query_formats = query_formats,
952 .inputs = convolution_inputs,
953 .outputs = convolution_outputs,
954 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
955 .process_command = process_command,
958 #endif /* CONFIG_SOBEL_FILTER */
960 #if CONFIG_ROBERTS_FILTER
962 #define roberts_options prewitt_roberts_sobel_options
963 AVFILTER_DEFINE_CLASS(roberts);
965 AVFilter ff_vf_roberts = {
967 .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
968 .priv_size = sizeof(ConvolutionContext),
969 .priv_class = &roberts_class,
971 .query_formats = query_formats,
972 .inputs = convolution_inputs,
973 .outputs = convolution_outputs,
974 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
975 .process_command = process_command,
978 #endif /* CONFIG_ROBERTS_FILTER */
980 #if CONFIG_KIRSCH_FILTER
982 #define kirsch_options prewitt_roberts_sobel_options
983 AVFILTER_DEFINE_CLASS(kirsch);
985 AVFilter ff_vf_kirsch = {
987 .description = NULL_IF_CONFIG_SMALL("Apply kirsch operator."),
988 .priv_size = sizeof(ConvolutionContext),
989 .priv_class = &kirsch_class,
991 .query_formats = query_formats,
992 .inputs = convolution_inputs,
993 .outputs = convolution_outputs,
994 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
995 .process_command = process_command,
998 #endif /* CONFIG_KIRSCH_FILTER */
1000 #endif /* CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER */