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 filter_prewitt(uint8_t *dst, 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 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
168 const uint8_t *c3 = c[3], *c5 = c[5];
169 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
172 for (x = 0; x < width; x++) {
173 float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
174 c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
175 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
176 c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
178 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
182 static void filter_roberts(uint8_t *dst, int width,
183 float scale, float delta, const int *const matrix,
184 const uint8_t *c[], int peak, int radius,
185 int dstride, int stride)
189 for (x = 0; x < width; x++) {
190 float suma = c[0][x] * 1 + c[1][x] * -1;
191 float sumb = c[4][x] * 1 + c[3][x] * -1;
193 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
197 static void filter_sobel(uint8_t *dst, int width,
198 float scale, float delta, const int *const matrix,
199 const uint8_t *c[], int peak, int radius,
200 int dstride, int stride)
202 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
203 const uint8_t *c3 = c[3], *c5 = c[5];
204 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
207 for (x = 0; x < width; x++) {
208 float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
209 c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
210 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
211 c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
213 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
217 static void filter16_3x3(uint8_t *dstp, int width,
218 float rdiv, float bias, const int *const matrix,
219 const uint8_t *c[], int peak, int radius,
220 int dstride, int stride)
222 uint16_t *dst = (uint16_t *)dstp;
225 for (x = 0; x < width; x++) {
226 int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
227 AV_RN16A(&c[1][2 * x]) * matrix[1] +
228 AV_RN16A(&c[2][2 * x]) * matrix[2] +
229 AV_RN16A(&c[3][2 * x]) * matrix[3] +
230 AV_RN16A(&c[4][2 * x]) * matrix[4] +
231 AV_RN16A(&c[5][2 * x]) * matrix[5] +
232 AV_RN16A(&c[6][2 * x]) * matrix[6] +
233 AV_RN16A(&c[7][2 * x]) * matrix[7] +
234 AV_RN16A(&c[8][2 * x]) * matrix[8];
235 sum = (int)(sum * rdiv + bias + 0.5f);
236 dst[x] = av_clip(sum, 0, peak);
240 static void filter16_5x5(uint8_t *dstp, int width,
241 float rdiv, float bias, const int *const matrix,
242 const uint8_t *c[], int peak, int radius,
243 int dstride, int stride)
245 uint16_t *dst = (uint16_t *)dstp;
248 for (x = 0; x < width; x++) {
251 for (i = 0; i < 25; i++)
252 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
254 sum = (int)(sum * rdiv + bias + 0.5f);
255 dst[x] = av_clip(sum, 0, peak);
259 static void filter16_7x7(uint8_t *dstp, int width,
260 float rdiv, float bias, const int *const matrix,
261 const uint8_t *c[], int peak, int radius,
262 int dstride, int stride)
264 uint16_t *dst = (uint16_t *)dstp;
267 for (x = 0; x < width; x++) {
270 for (i = 0; i < 49; i++)
271 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
273 sum = (int)(sum * rdiv + bias + 0.5f);
274 dst[x] = av_clip(sum, 0, peak);
278 static void filter16_row(uint8_t *dstp, int width,
279 float rdiv, float bias, const int *const matrix,
280 const uint8_t *c[], int peak, int radius,
281 int dstride, int stride)
283 uint16_t *dst = (uint16_t *)dstp;
286 for (x = 0; x < width; x++) {
289 for (i = 0; i < 2 * radius + 1; i++)
290 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
292 sum = (int)(sum * rdiv + bias + 0.5f);
293 dst[x] = av_clip(sum, 0, peak);
297 static void filter16_column(uint8_t *dstp, int height,
298 float rdiv, float bias, const int *const matrix,
299 const uint8_t *c[], int peak, int radius,
300 int dstride, int stride)
302 uint16_t *dst = (uint16_t *)dstp;
305 for (y = 0; y < height; y++) {
308 for (i = 0; i < 2 * radius + 1; i++)
309 sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
311 sum = (int)(sum * rdiv + bias + 0.5f);
312 dst[0] = av_clip(sum, 0, peak);
317 static void filter_7x7(uint8_t *dst, int width,
318 float rdiv, float bias, const int *const matrix,
319 const uint8_t *c[], int peak, int radius,
320 int dstride, int stride)
324 for (x = 0; x < width; x++) {
327 for (i = 0; i < 49; i++)
328 sum += c[i][x] * matrix[i];
330 sum = (int)(sum * rdiv + bias + 0.5f);
331 dst[x] = av_clip_uint8(sum);
335 static void filter_5x5(uint8_t *dst, int width,
336 float rdiv, float bias, const int *const matrix,
337 const uint8_t *c[], int peak, int radius,
338 int dstride, int stride)
342 for (x = 0; x < width; x++) {
345 for (i = 0; i < 25; i++)
346 sum += c[i][x] * matrix[i];
348 sum = (int)(sum * rdiv + bias + 0.5f);
349 dst[x] = av_clip_uint8(sum);
353 static void filter_3x3(uint8_t *dst, int width,
354 float rdiv, float bias, const int *const matrix,
355 const uint8_t *c[], int peak, int radius,
356 int dstride, int stride)
358 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
359 const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
360 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
363 for (x = 0; x < width; x++) {
364 int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
365 c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
366 c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
367 sum = (int)(sum * rdiv + bias + 0.5f);
368 dst[x] = av_clip_uint8(sum);
372 static void filter_row(uint8_t *dst, int width,
373 float rdiv, float bias, const int *const matrix,
374 const uint8_t *c[], int peak, int radius,
375 int dstride, int stride)
379 for (x = 0; x < width; x++) {
382 for (i = 0; i < 2 * radius + 1; i++)
383 sum += c[i][x] * matrix[i];
385 sum = (int)(sum * rdiv + bias + 0.5f);
386 dst[x] = av_clip_uint8(sum);
390 static void filter_column(uint8_t *dst, int height,
391 float rdiv, float bias, const int *const matrix,
392 const uint8_t *c[], int peak, int radius,
393 int dstride, int stride)
397 for (y = 0; y < height; y++) {
400 for (i = 0; i < 2 * radius + 1; i++)
401 sum += c[i][0 + y * stride] * matrix[i];
403 sum = (int)(sum * rdiv + bias + 0.5f);
404 dst[0] = av_clip_uint8(sum);
409 static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
410 int x, int w, int y, int h, int bpc)
414 for (i = 0; i < 9; i++) {
415 int xoff = FFABS(x + ((i % 3) - 1));
416 int yoff = FFABS(y + (i / 3) - 1);
418 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
419 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
421 c[i] = src + xoff * bpc + yoff * stride;
425 static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
426 int x, int w, int y, int h, int bpc)
430 for (i = 0; i < 25; i++) {
431 int xoff = FFABS(x + ((i % 5) - 2));
432 int yoff = FFABS(y + (i / 5) - 2);
434 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
435 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
437 c[i] = src + xoff * bpc + yoff * stride;
441 static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
442 int x, int w, int y, int h, int bpc)
446 for (i = 0; i < 49; i++) {
447 int xoff = FFABS(x + ((i % 7) - 3));
448 int yoff = FFABS(y + (i / 7) - 3);
450 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
451 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
453 c[i] = src + xoff * bpc + yoff * stride;
457 static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
458 int x, int w, int y, int h, int bpc)
462 for (i = 0; i < radius * 2 + 1; i++) {
463 int xoff = FFABS(x + i - radius);
465 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
467 c[i] = src + xoff * bpc + y * stride;
471 static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
472 int x, int w, int y, int h, int bpc)
476 for (i = 0; i < radius * 2 + 1; i++) {
477 int xoff = FFABS(x + i - radius);
479 xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
481 c[i] = src + y * bpc + xoff * stride;
485 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
487 ConvolutionContext *s = ctx->priv;
488 ThreadData *td = arg;
489 AVFrame *in = td->in;
490 AVFrame *out = td->out;
493 for (plane = 0; plane < s->nb_planes; plane++) {
494 const int mode = s->mode[plane];
495 const int bpc = s->bpc;
496 const int radius = s->size[plane] / 2;
497 const int height = s->planeheight[plane];
498 const int width = s->planewidth[plane];
499 const int stride = in->linesize[plane];
500 const int dstride = out->linesize[plane];
501 const int sizeh = mode == MATRIX_COLUMN ? width : height;
502 const int sizew = mode == MATRIX_COLUMN ? height : width;
503 const int slice_start = (sizeh * jobnr) / nb_jobs;
504 const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
505 const float rdiv = s->rdiv[plane];
506 const float bias = s->bias[plane];
507 const uint8_t *src = in->data[plane];
508 const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
509 uint8_t *dst = out->data[plane] + dst_pos;
510 const int *matrix = s->matrix[plane];
511 const uint8_t *c[49];
514 if (s->copy[plane]) {
515 if (mode == MATRIX_COLUMN)
516 av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
517 (slice_end - slice_start) * bpc, height);
519 av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
520 width * bpc, slice_end - slice_start);
524 for (y = slice_start; y < slice_end; y++) {
525 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
526 const int yoff = mode == MATRIX_COLUMN ? radius * stride : 0;
528 for (x = 0; x < radius; x++) {
529 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
530 const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
532 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
533 s->filter[plane](dst + yoff + xoff, 1, rdiv,
534 bias, matrix, c, s->max, radius,
537 s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
538 s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
539 rdiv, bias, matrix, c, s->max, radius,
541 for (x = sizew - radius; x < sizew; x++) {
542 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
543 const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
545 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
546 s->filter[plane](dst + yoff + xoff, 1, rdiv,
547 bias, matrix, c, s->max, radius,
550 if (mode != MATRIX_COLUMN)
558 static int config_input(AVFilterLink *inlink)
560 AVFilterContext *ctx = inlink->dst;
561 ConvolutionContext *s = ctx->priv;
562 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
565 s->depth = desc->comp[0].depth;
566 s->max = (1 << s->depth) - 1;
568 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
569 s->planewidth[0] = s->planewidth[3] = inlink->w;
570 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
571 s->planeheight[0] = s->planeheight[3] = inlink->h;
573 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
574 s->nb_threads = ff_filter_get_nb_threads(ctx);
575 s->bpc = (s->depth + 7) / 8;
577 if (!strcmp(ctx->filter->name, "convolution")) {
579 for (p = 0; p < s->nb_planes; p++) {
580 if (s->mode[p] == MATRIX_ROW)
581 s->filter[p] = filter16_row;
582 else if (s->mode[p] == MATRIX_COLUMN)
583 s->filter[p] = filter16_column;
584 else if (s->size[p] == 3)
585 s->filter[p] = filter16_3x3;
586 else if (s->size[p] == 5)
587 s->filter[p] = filter16_5x5;
588 else if (s->size[p] == 7)
589 s->filter[p] = filter16_7x7;
592 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
593 ff_convolution_init_x86(s);
595 } else if (!strcmp(ctx->filter->name, "prewitt")) {
597 for (p = 0; p < s->nb_planes; p++)
598 s->filter[p] = filter16_prewitt;
599 } else if (!strcmp(ctx->filter->name, "roberts")) {
601 for (p = 0; p < s->nb_planes; p++)
602 s->filter[p] = filter16_roberts;
603 } else if (!strcmp(ctx->filter->name, "sobel")) {
605 for (p = 0; p < s->nb_planes; p++)
606 s->filter[p] = filter16_sobel;
612 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
614 AVFilterContext *ctx = inlink->dst;
615 ConvolutionContext *s = ctx->priv;
616 AVFilterLink *outlink = ctx->outputs[0];
620 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
623 return AVERROR(ENOMEM);
625 av_frame_copy_props(out, in);
629 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
632 return ff_filter_frame(outlink, out);
635 static av_cold int init(AVFilterContext *ctx)
637 ConvolutionContext *s = ctx->priv;
640 if (!strcmp(ctx->filter->name, "convolution")) {
641 for (i = 0; i < 4; i++) {
642 int *matrix = (int *)s->matrix[i];
643 char *p, *arg, *saveptr = NULL;
646 p = s->matrix_str[i];
648 s->matrix_length[i] = 0;
650 while (s->matrix_length[i] < 49) {
651 if (!(arg = av_strtok(p, " |", &saveptr)))
655 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
656 sum += matrix[s->matrix_length[i]];
657 s->matrix_length[i]++;
660 if (!(s->matrix_length[i] & 1)) {
661 av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
662 return AVERROR(EINVAL);
666 if (s->mode[i] == MATRIX_ROW) {
667 s->filter[i] = filter_row;
668 s->setup[i] = setup_row;
669 s->size[i] = s->matrix_length[i];
670 } else if (s->mode[i] == MATRIX_COLUMN) {
671 s->filter[i] = filter_column;
672 s->setup[i] = setup_column;
673 s->size[i] = s->matrix_length[i];
674 } else if (s->matrix_length[i] == 9) {
677 if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
680 s->filter[i] = filter_3x3;
683 s->setup[i] = setup_3x3;
684 } else if (s->matrix_length[i] == 25) {
686 if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
689 s->filter[i] = filter_5x5;
692 s->setup[i] = setup_5x5;
693 } else if (s->matrix_length[i] == 49) {
695 if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
698 s->filter[i] = filter_7x7;
701 s->setup[i] = setup_7x7;
703 return AVERROR(EINVAL);
709 s->rdiv[i] = 1. / sum;
711 if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
714 } else if (!strcmp(ctx->filter->name, "prewitt")) {
715 for (i = 0; i < 4; i++) {
716 if ((1 << i) & s->planes)
717 s->filter[i] = filter_prewitt;
721 s->setup[i] = setup_3x3;
722 s->rdiv[i] = s->scale;
723 s->bias[i] = s->delta;
725 } else if (!strcmp(ctx->filter->name, "roberts")) {
726 for (i = 0; i < 4; i++) {
727 if ((1 << i) & s->planes)
728 s->filter[i] = filter_roberts;
732 s->setup[i] = setup_3x3;
733 s->rdiv[i] = s->scale;
734 s->bias[i] = s->delta;
736 } else if (!strcmp(ctx->filter->name, "sobel")) {
737 for (i = 0; i < 4; i++) {
738 if ((1 << i) & s->planes)
739 s->filter[i] = filter_sobel;
743 s->setup[i] = setup_3x3;
744 s->rdiv[i] = s->scale;
745 s->bias[i] = s->delta;
752 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
753 char *res, int res_len, int flags)
757 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
764 static const AVFilterPad convolution_inputs[] = {
767 .type = AVMEDIA_TYPE_VIDEO,
768 .config_props = config_input,
769 .filter_frame = filter_frame,
774 static const AVFilterPad convolution_outputs[] = {
777 .type = AVMEDIA_TYPE_VIDEO,
782 #if CONFIG_CONVOLUTION_FILTER
784 AVFilter ff_vf_convolution = {
785 .name = "convolution",
786 .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
787 .priv_size = sizeof(ConvolutionContext),
788 .priv_class = &convolution_class,
790 .query_formats = query_formats,
791 .inputs = convolution_inputs,
792 .outputs = convolution_outputs,
793 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
794 .process_command = process_command,
797 #endif /* CONFIG_CONVOLUTION_FILTER */
799 #if CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER
801 static const AVOption prewitt_roberts_sobel_options[] = {
802 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
803 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
804 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
808 #if CONFIG_PREWITT_FILTER
810 #define prewitt_options prewitt_roberts_sobel_options
811 AVFILTER_DEFINE_CLASS(prewitt);
813 AVFilter ff_vf_prewitt = {
815 .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
816 .priv_size = sizeof(ConvolutionContext),
817 .priv_class = &prewitt_class,
819 .query_formats = query_formats,
820 .inputs = convolution_inputs,
821 .outputs = convolution_outputs,
822 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
823 .process_command = process_command,
826 #endif /* CONFIG_PREWITT_FILTER */
828 #if CONFIG_SOBEL_FILTER
830 #define sobel_options prewitt_roberts_sobel_options
831 AVFILTER_DEFINE_CLASS(sobel);
833 AVFilter ff_vf_sobel = {
835 .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
836 .priv_size = sizeof(ConvolutionContext),
837 .priv_class = &sobel_class,
839 .query_formats = query_formats,
840 .inputs = convolution_inputs,
841 .outputs = convolution_outputs,
842 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
843 .process_command = process_command,
846 #endif /* CONFIG_SOBEL_FILTER */
848 #if CONFIG_ROBERTS_FILTER
850 #define roberts_options prewitt_roberts_sobel_options
851 AVFILTER_DEFINE_CLASS(roberts);
853 AVFilter ff_vf_roberts = {
855 .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
856 .priv_size = sizeof(ConvolutionContext),
857 .priv_class = &roberts_class,
859 .query_formats = query_formats,
860 .inputs = convolution_inputs,
861 .outputs = convolution_outputs,
862 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
863 .process_command = process_command,
866 #endif /* CONFIG_ROBERTS_FILTER */
867 #endif /* CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER */