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
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_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
95 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
96 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
97 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
98 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
102 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
105 typedef struct ThreadData {
109 static void filter16_prewitt(uint8_t *dstp, int width,
110 float scale, float delta, const int *const matrix,
111 const uint8_t *c[], int peak, int radius,
112 int dstride, int stride)
114 uint16_t *dst = (uint16_t *)dstp;
117 for (x = 0; x < width; x++) {
118 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
119 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + AV_RN16A(&c[8][2 * x]) * 1;
120 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1 +
121 AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
123 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
127 static void filter16_roberts(uint8_t *dstp, int width,
128 float scale, float delta, const int *const matrix,
129 const uint8_t *c[], int peak, int radius,
130 int dstride, int stride)
132 uint16_t *dst = (uint16_t *)dstp;
135 for (x = 0; x < width; x++) {
136 float suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
137 float sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
139 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
143 static void filter16_sobel(uint8_t *dstp, int width,
144 float scale, float delta, const int *const matrix,
145 const uint8_t *c[], int peak, int radius,
146 int dstride, int stride)
148 uint16_t *dst = (uint16_t *)dstp;
151 for (x = 0; x < width; x++) {
152 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
153 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
154 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
155 AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
157 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
161 static void filter_prewitt(uint8_t *dst, int width,
162 float scale, float delta, const int *const matrix,
163 const uint8_t *c[], int peak, int radius,
164 int dstride, int stride)
166 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
167 const uint8_t *c3 = c[3], *c5 = c[5];
168 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
171 for (x = 0; x < width; x++) {
172 float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
173 c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
174 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
175 c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
177 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
181 static void filter_roberts(uint8_t *dst, int width,
182 float scale, float delta, const int *const matrix,
183 const uint8_t *c[], int peak, int radius,
184 int dstride, int stride)
188 for (x = 0; x < width; x++) {
189 float suma = c[0][x] * 1 + c[1][x] * -1;
190 float sumb = c[4][x] * 1 + c[3][x] * -1;
192 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
196 static void filter_sobel(uint8_t *dst, int width,
197 float scale, float delta, const int *const matrix,
198 const uint8_t *c[], int peak, int radius,
199 int dstride, int stride)
201 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
202 const uint8_t *c3 = c[3], *c5 = c[5];
203 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
206 for (x = 0; x < width; x++) {
207 float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
208 c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
209 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
210 c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
212 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
216 static void filter16_3x3(uint8_t *dstp, int width,
217 float rdiv, float bias, const int *const matrix,
218 const uint8_t *c[], int peak, int radius,
219 int dstride, int stride)
221 uint16_t *dst = (uint16_t *)dstp;
224 for (x = 0; x < width; x++) {
225 int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
226 AV_RN16A(&c[1][2 * x]) * matrix[1] +
227 AV_RN16A(&c[2][2 * x]) * matrix[2] +
228 AV_RN16A(&c[3][2 * x]) * matrix[3] +
229 AV_RN16A(&c[4][2 * x]) * matrix[4] +
230 AV_RN16A(&c[5][2 * x]) * matrix[5] +
231 AV_RN16A(&c[6][2 * x]) * matrix[6] +
232 AV_RN16A(&c[7][2 * x]) * matrix[7] +
233 AV_RN16A(&c[8][2 * x]) * matrix[8];
234 sum = (int)(sum * rdiv + bias + 0.5f);
235 dst[x] = av_clip(sum, 0, peak);
239 static void filter16_5x5(uint8_t *dstp, int width,
240 float rdiv, float bias, const int *const matrix,
241 const uint8_t *c[], int peak, int radius,
242 int dstride, int stride)
244 uint16_t *dst = (uint16_t *)dstp;
247 for (x = 0; x < width; x++) {
250 for (i = 0; i < 25; i++)
251 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
253 sum = (int)(sum * rdiv + bias + 0.5f);
254 dst[x] = av_clip(sum, 0, peak);
258 static void filter16_7x7(uint8_t *dstp, int width,
259 float rdiv, float bias, const int *const matrix,
260 const uint8_t *c[], int peak, int radius,
261 int dstride, int stride)
263 uint16_t *dst = (uint16_t *)dstp;
266 for (x = 0; x < width; x++) {
269 for (i = 0; i < 49; i++)
270 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
272 sum = (int)(sum * rdiv + bias + 0.5f);
273 dst[x] = av_clip(sum, 0, peak);
277 static void filter16_row(uint8_t *dstp, int width,
278 float rdiv, float bias, const int *const matrix,
279 const uint8_t *c[], int peak, int radius,
280 int dstride, int stride)
282 uint16_t *dst = (uint16_t *)dstp;
285 for (x = 0; x < width; x++) {
288 for (i = 0; i < 2 * radius + 1; i++)
289 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
291 sum = (int)(sum * rdiv + bias + 0.5f);
292 dst[x] = av_clip(sum, 0, peak);
296 static void filter16_column(uint8_t *dstp, int height,
297 float rdiv, float bias, const int *const matrix,
298 const uint8_t *c[], int peak, int radius,
299 int dstride, int stride)
301 uint16_t *dst = (uint16_t *)dstp;
304 for (y = 0; y < height; y++) {
307 for (i = 0; i < 2 * radius + 1; i++)
308 sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
310 sum = (int)(sum * rdiv + bias + 0.5f);
311 dst[0] = av_clip(sum, 0, peak);
316 static void filter_7x7(uint8_t *dst, int width,
317 float rdiv, float bias, const int *const matrix,
318 const uint8_t *c[], int peak, int radius,
319 int dstride, int stride)
323 for (x = 0; x < width; x++) {
326 for (i = 0; i < 49; i++)
327 sum += c[i][x] * matrix[i];
329 sum = (int)(sum * rdiv + bias + 0.5f);
330 dst[x] = av_clip_uint8(sum);
334 static void filter_5x5(uint8_t *dst, int width,
335 float rdiv, float bias, const int *const matrix,
336 const uint8_t *c[], int peak, int radius,
337 int dstride, int stride)
341 for (x = 0; x < width; x++) {
344 for (i = 0; i < 25; i++)
345 sum += c[i][x] * matrix[i];
347 sum = (int)(sum * rdiv + bias + 0.5f);
348 dst[x] = av_clip_uint8(sum);
352 static void filter_3x3(uint8_t *dst, int width,
353 float rdiv, float bias, const int *const matrix,
354 const uint8_t *c[], int peak, int radius,
355 int dstride, int stride)
357 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
358 const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
359 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
362 for (x = 0; x < width; x++) {
363 int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
364 c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
365 c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
366 sum = (int)(sum * rdiv + bias + 0.5f);
367 dst[x] = av_clip_uint8(sum);
371 static void filter_row(uint8_t *dst, int width,
372 float rdiv, float bias, const int *const matrix,
373 const uint8_t *c[], int peak, int radius,
374 int dstride, int stride)
378 for (x = 0; x < width; x++) {
381 for (i = 0; i < 2 * radius + 1; i++)
382 sum += c[i][x] * matrix[i];
384 sum = (int)(sum * rdiv + bias + 0.5f);
385 dst[x] = av_clip_uint8(sum);
389 static void filter_column(uint8_t *dst, int height,
390 float rdiv, float bias, const int *const matrix,
391 const uint8_t *c[], int peak, int radius,
392 int dstride, int stride)
396 for (y = 0; y < height; y++) {
399 for (i = 0; i < 2 * radius + 1; i++)
400 sum += c[i][0 + y * stride] * matrix[i];
402 sum = (int)(sum * rdiv + bias + 0.5f);
403 dst[0] = av_clip_uint8(sum);
408 static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
409 int x, int w, int y, int h, int bpc)
413 for (i = 0; i < 9; i++) {
414 int xoff = FFABS(x + ((i % 3) - 1));
415 int yoff = FFABS(y + (i / 3) - 1);
417 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
418 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
420 c[i] = src + xoff * bpc + yoff * stride;
424 static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
425 int x, int w, int y, int h, int bpc)
429 for (i = 0; i < 25; i++) {
430 int xoff = FFABS(x + ((i % 5) - 2));
431 int yoff = FFABS(y + (i / 5) - 2);
433 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
434 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
436 c[i] = src + xoff * bpc + yoff * stride;
440 static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
441 int x, int w, int y, int h, int bpc)
445 for (i = 0; i < 49; i++) {
446 int xoff = FFABS(x + ((i % 7) - 3));
447 int yoff = FFABS(y + (i / 7) - 3);
449 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
450 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
452 c[i] = src + xoff * bpc + yoff * stride;
456 static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
457 int x, int w, int y, int h, int bpc)
461 for (i = 0; i < radius * 2 + 1; i++) {
462 int xoff = FFABS(x + i - radius);
464 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
466 c[i] = src + xoff * bpc + y * stride;
470 static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
471 int x, int w, int y, int h, int bpc)
475 for (i = 0; i < radius * 2 + 1; i++) {
476 int xoff = FFABS(x + i - radius);
478 xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
480 c[i] = src + y * bpc + xoff * stride;
484 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
486 ConvolutionContext *s = ctx->priv;
487 ThreadData *td = arg;
488 AVFrame *in = td->in;
489 AVFrame *out = td->out;
492 for (plane = 0; plane < s->nb_planes; plane++) {
493 const int mode = s->mode[plane];
494 const int bpc = s->bpc;
495 const int radius = s->size[plane] / 2;
496 const int height = s->planeheight[plane];
497 const int width = s->planewidth[plane];
498 const int stride = in->linesize[plane];
499 const int dstride = out->linesize[plane];
500 const int sizeh = mode == MATRIX_COLUMN ? width : height;
501 const int sizew = mode == MATRIX_COLUMN ? height : width;
502 const int slice_start = (sizeh * jobnr) / nb_jobs;
503 const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
504 const float rdiv = s->rdiv[plane];
505 const float bias = s->bias[plane];
506 const uint8_t *src = in->data[plane];
507 const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
508 uint8_t *dst = out->data[plane] + dst_pos;
509 const int *matrix = s->matrix[plane];
510 const uint8_t *c[49];
513 if (s->copy[plane]) {
514 if (mode == MATRIX_COLUMN)
515 av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
516 (slice_end - slice_start) * bpc, height);
518 av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
519 width * bpc, slice_end - slice_start);
523 for (y = slice_start; y < slice_end; y++) {
524 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
525 const int yoff = mode == MATRIX_COLUMN ? radius * stride : 0;
527 for (x = 0; x < radius; x++) {
528 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
529 const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
531 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
532 s->filter[plane](dst + yoff + xoff, 1, rdiv,
533 bias, matrix, c, s->max, radius,
536 s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
537 s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
538 rdiv, bias, matrix, c, s->max, radius,
540 for (x = sizew - radius; x < sizew; x++) {
541 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
542 const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
544 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
545 s->filter[plane](dst + yoff + xoff, 1, rdiv,
546 bias, matrix, c, s->max, radius,
549 if (mode != MATRIX_COLUMN)
557 static int config_input(AVFilterLink *inlink)
559 AVFilterContext *ctx = inlink->dst;
560 ConvolutionContext *s = ctx->priv;
561 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
564 s->depth = desc->comp[0].depth;
565 s->max = (1 << s->depth) - 1;
567 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
568 s->planewidth[0] = s->planewidth[3] = inlink->w;
569 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
570 s->planeheight[0] = s->planeheight[3] = inlink->h;
572 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
573 s->nb_threads = ff_filter_get_nb_threads(ctx);
574 s->bpc = (s->depth + 7) / 8;
576 if (!strcmp(ctx->filter->name, "convolution")) {
578 for (p = 0; p < s->nb_planes; p++) {
579 if (s->mode[p] == MATRIX_ROW)
580 s->filter[p] = filter16_row;
581 else if (s->mode[p] == MATRIX_COLUMN)
582 s->filter[p] = filter16_column;
583 else if (s->size[p] == 3)
584 s->filter[p] = filter16_3x3;
585 else if (s->size[p] == 5)
586 s->filter[p] = filter16_5x5;
587 else if (s->size[p] == 7)
588 s->filter[p] = filter16_7x7;
591 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
592 ff_convolution_init_x86(s);
594 } else if (!strcmp(ctx->filter->name, "prewitt")) {
596 for (p = 0; p < s->nb_planes; p++)
597 s->filter[p] = filter16_prewitt;
598 } else if (!strcmp(ctx->filter->name, "roberts")) {
600 for (p = 0; p < s->nb_planes; p++)
601 s->filter[p] = filter16_roberts;
602 } else if (!strcmp(ctx->filter->name, "sobel")) {
604 for (p = 0; p < s->nb_planes; p++)
605 s->filter[p] = filter16_sobel;
611 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
613 AVFilterContext *ctx = inlink->dst;
614 ConvolutionContext *s = ctx->priv;
615 AVFilterLink *outlink = ctx->outputs[0];
619 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
622 return AVERROR(ENOMEM);
624 av_frame_copy_props(out, in);
628 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
631 return ff_filter_frame(outlink, out);
634 static av_cold int init(AVFilterContext *ctx)
636 ConvolutionContext *s = ctx->priv;
639 if (!strcmp(ctx->filter->name, "convolution")) {
640 for (i = 0; i < 4; i++) {
641 int *matrix = (int *)s->matrix[i];
642 char *p, *arg, *saveptr = NULL;
645 p = s->matrix_str[i];
646 while (s->matrix_length[i] < 49) {
647 if (!(arg = av_strtok(p, " ", &saveptr)))
651 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
652 sum += matrix[s->matrix_length[i]];
653 s->matrix_length[i]++;
656 if (!(s->matrix_length[i] & 1)) {
657 av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
658 return AVERROR(EINVAL);
660 if (s->mode[i] == MATRIX_ROW) {
661 s->filter[i] = filter_row;
662 s->setup[i] = setup_row;
663 s->size[i] = s->matrix_length[i];
664 } else if (s->mode[i] == MATRIX_COLUMN) {
665 s->filter[i] = filter_column;
666 s->setup[i] = setup_column;
667 s->size[i] = s->matrix_length[i];
668 } else if (s->matrix_length[i] == 9) {
670 if (!memcmp(matrix, same3x3, sizeof(same3x3)))
673 s->filter[i] = filter_3x3;
674 s->setup[i] = setup_3x3;
675 } else if (s->matrix_length[i] == 25) {
677 if (!memcmp(matrix, same5x5, sizeof(same5x5)))
680 s->filter[i] = filter_5x5;
681 s->setup[i] = setup_5x5;
682 } else if (s->matrix_length[i] == 49) {
684 if (!memcmp(matrix, same7x7, sizeof(same7x7)))
687 s->filter[i] = filter_7x7;
688 s->setup[i] = setup_7x7;
690 return AVERROR(EINVAL);
696 s->rdiv[i] = 1. / sum;
698 if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
701 } else if (!strcmp(ctx->filter->name, "prewitt")) {
702 for (i = 0; i < 4; i++) {
703 if ((1 << i) & s->planes)
704 s->filter[i] = filter_prewitt;
708 s->setup[i] = setup_3x3;
709 s->rdiv[i] = s->scale;
710 s->bias[i] = s->delta;
712 } else if (!strcmp(ctx->filter->name, "roberts")) {
713 for (i = 0; i < 4; i++) {
714 if ((1 << i) & s->planes)
715 s->filter[i] = filter_roberts;
719 s->setup[i] = setup_3x3;
720 s->rdiv[i] = s->scale;
721 s->bias[i] = s->delta;
723 } else if (!strcmp(ctx->filter->name, "sobel")) {
724 for (i = 0; i < 4; i++) {
725 if ((1 << i) & s->planes)
726 s->filter[i] = filter_sobel;
730 s->setup[i] = setup_3x3;
731 s->rdiv[i] = s->scale;
732 s->bias[i] = s->delta;
739 static const AVFilterPad convolution_inputs[] = {
742 .type = AVMEDIA_TYPE_VIDEO,
743 .config_props = config_input,
744 .filter_frame = filter_frame,
749 static const AVFilterPad convolution_outputs[] = {
752 .type = AVMEDIA_TYPE_VIDEO,
757 #if CONFIG_CONVOLUTION_FILTER
759 AVFilter ff_vf_convolution = {
760 .name = "convolution",
761 .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
762 .priv_size = sizeof(ConvolutionContext),
763 .priv_class = &convolution_class,
765 .query_formats = query_formats,
766 .inputs = convolution_inputs,
767 .outputs = convolution_outputs,
768 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
771 #endif /* CONFIG_CONVOLUTION_FILTER */
773 #if CONFIG_PREWITT_FILTER
775 static const AVOption prewitt_options[] = {
776 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
777 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
778 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
782 AVFILTER_DEFINE_CLASS(prewitt);
784 AVFilter ff_vf_prewitt = {
786 .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
787 .priv_size = sizeof(ConvolutionContext),
788 .priv_class = &prewitt_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,
796 #endif /* CONFIG_PREWITT_FILTER */
798 #if CONFIG_SOBEL_FILTER
800 static const AVOption sobel_options[] = {
801 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
802 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
803 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
807 AVFILTER_DEFINE_CLASS(sobel);
809 AVFilter ff_vf_sobel = {
811 .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
812 .priv_size = sizeof(ConvolutionContext),
813 .priv_class = &sobel_class,
815 .query_formats = query_formats,
816 .inputs = convolution_inputs,
817 .outputs = convolution_outputs,
818 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
821 #endif /* CONFIG_SOBEL_FILTER */
823 #if CONFIG_ROBERTS_FILTER
825 static const AVOption roberts_options[] = {
826 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
827 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
828 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
832 AVFILTER_DEFINE_CLASS(roberts);
834 AVFilter ff_vf_roberts = {
836 .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
837 .priv_size = sizeof(ConvolutionContext),
838 .priv_class = &roberts_class,
840 .query_formats = query_formats,
841 .inputs = convolution_inputs,
842 .outputs = convolution_outputs,
843 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
846 #endif /* CONFIG_ROBERTS_FILTER */