FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_convolution.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 0 469 0.0%
Functions: 0 27 0.0%
Branches: 0 222 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
3 * Copyright (c) 2015 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config_components.h"
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem_internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "convolution.h"
32 #include "internal.h"
33 #include "video.h"
34
35 #define OFFSET(x) offsetof(ConvolutionContext, x)
36 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
37
38 static const AVOption convolution_options[] = {
39 { "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 },
40 { "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 },
41 { "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 },
42 { "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 },
43 { "0rdiv", "set rdiv for 1st plane", OFFSET(user_rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
44 { "1rdiv", "set rdiv for 2nd plane", OFFSET(user_rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
45 { "2rdiv", "set rdiv for 3rd plane", OFFSET(user_rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
46 { "3rdiv", "set rdiv for 4th plane", OFFSET(user_rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
47 { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
48 { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
49 { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
50 { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
51 { "0mode", "set matrix mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, .unit = "mode" },
52 { "1mode", "set matrix mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, .unit = "mode" },
53 { "2mode", "set matrix mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, .unit = "mode" },
54 { "3mode", "set matrix mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, .unit = "mode" },
55 { "square", "square matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_SQUARE}, 0, 0, FLAGS, .unit = "mode" },
56 { "row", "single row matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_ROW} , 0, 0, FLAGS, .unit = "mode" },
57 { "column", "single column matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_COLUMN}, 0, 0, FLAGS, .unit = "mode" },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(convolution);
62
63 static const int same3x3[9] = {0, 0, 0,
64 0, 1, 0,
65 0, 0, 0};
66
67 static const int same5x5[25] = {0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0,
69 0, 0, 1, 0, 0,
70 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0};
72
73 static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 1, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0};
80
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,
100 AV_PIX_FMT_NONE
101 };
102
103 typedef struct ThreadData {
104 AVFrame *in, *out;
105 } ThreadData;
106
107 static void filter16_prewitt(uint8_t *dstp, int width,
108 float scale, float delta, const int *const matrix,
109 const uint8_t *c[], int peak, int radius,
110 int dstride, int stride, int size)
111 {
112 uint16_t *dst = (uint16_t *)dstp;
113 int x;
114
115 for (x = 0; x < width; x++) {
116 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
117 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + AV_RN16A(&c[8][2 * x]) * 1;
118 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1 +
119 AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
120
121 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
122 }
123 }
124
125 static void filter16_roberts(uint8_t *dstp, int width,
126 float scale, float delta, const int *const matrix,
127 const uint8_t *c[], int peak, int radius,
128 int dstride, int stride, int size)
129 {
130 uint16_t *dst = (uint16_t *)dstp;
131 int x;
132
133 for (x = 0; x < width; x++) {
134 float suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
135 float sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
136
137 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
138 }
139 }
140
141 static void filter16_scharr(uint8_t *dstp, int width,
142 float scale, float delta, const int *const matrix,
143 const uint8_t *c[], int peak, int radius,
144 int dstride, int stride, int size)
145 {
146 uint16_t *dst = (uint16_t *)dstp;
147 int x;
148
149 for (x = 0; x < width; x++) {
150 float suma = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[1][2 * x]) * -162 + AV_RN16A(&c[2][2 * x]) * -47 +
151 AV_RN16A(&c[6][2 * x]) * 47 + AV_RN16A(&c[7][2 * x]) * 162 + AV_RN16A(&c[8][2 * x]) * 47;
152 float sumb = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[2][2 * x]) * 47 + AV_RN16A(&c[3][2 * x]) * -162 +
153 AV_RN16A(&c[5][2 * x]) * 162 + AV_RN16A(&c[6][2 * x]) * -47 + AV_RN16A(&c[8][2 * x]) * 47;
154
155 suma /= 256.f;
156 sumb /= 256.f;
157 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
158 }
159 }
160
161 static void filter16_kirsch(uint8_t *dstp, 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, int size)
165 {
166 uint16_t *dst = (uint16_t *)dstp;
167 const uint16_t *c0 = (const uint16_t *)c[0], *c1 = (const uint16_t *)c[1], *c2 = (const uint16_t *)c[2];
168 const uint16_t *c3 = (const uint16_t *)c[3], *c5 = (const uint16_t *)c[5];
169 const uint16_t *c6 = (const uint16_t *)c[6], *c7 = (const uint16_t *)c[7], *c8 = (const uint16_t *)c[8];
170 int x;
171
172 for (x = 0; x < width; x++) {
173 int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
174 c3[x] * -3 + c5[x] * -3 +
175 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
176 int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
177 c3[x] * 5 + c5[x] * -3 +
178 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
179 int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
180 c3[x] * 5 + c5[x] * 5 +
181 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
182 int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
183 c3[x] * 5 + c5[x] * 5 +
184 c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
185 int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
186 c3[x] * -3 + c5[x] * 5 +
187 c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
188 int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
189 c3[x] * -3 + c5[x] * -3 +
190 c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
191 int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
192 c3[x] * -3 + c5[x] * -3 +
193 c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
194 int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
195 c3[x] * -3 + c5[x] * -3 +
196 c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
197
198 sum0 = FFMAX(sum0, sum1);
199 sum2 = FFMAX(sum2, sum3);
200 sum4 = FFMAX(sum4, sum5);
201 sum6 = FFMAX(sum6, sum7);
202 sum0 = FFMAX(sum0, sum2);
203 sum4 = FFMAX(sum4, sum6);
204 sum0 = FFMAX(sum0, sum4);
205
206 dst[x] = av_clip(FFABS(sum0) * scale + delta, 0, peak);
207 }
208 }
209
210 static void filter_prewitt(uint8_t *dst, int width,
211 float scale, float delta, const int *const matrix,
212 const uint8_t *c[], int peak, int radius,
213 int dstride, int stride, int size)
214 {
215 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
216 const uint8_t *c3 = c[3], *c5 = c[5];
217 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
218 int x;
219
220 for (x = 0; x < width; x++) {
221 float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
222 c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
223 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
224 c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
225
226 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
227 }
228 }
229
230 static void filter_roberts(uint8_t *dst, int width,
231 float scale, float delta, const int *const matrix,
232 const uint8_t *c[], int peak, int radius,
233 int dstride, int stride, int size)
234 {
235 int x;
236
237 for (x = 0; x < width; x++) {
238 float suma = c[0][x] * 1 + c[1][x] * -1;
239 float sumb = c[4][x] * 1 + c[3][x] * -1;
240
241 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
242 }
243 }
244
245 static void filter_scharr(uint8_t *dst, int width,
246 float scale, float delta, const int *const matrix,
247 const uint8_t *c[], int peak, int radius,
248 int dstride, int stride, int size)
249 {
250 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
251 const uint8_t *c3 = c[3], *c5 = c[5];
252 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
253 int x;
254
255 for (x = 0; x < width; x++) {
256 float suma = c0[x] * -47 + c1[x] * -162 + c2[x] * -47 +
257 c6[x] * 47 + c7[x] * 162 + c8[x] * 47;
258 float sumb = c0[x] * -47 + c2[x] * 47 + c3[x] * -162 +
259 c5[x] * 162 + c6[x] * -47 + c8[x] * 47;
260
261 suma /= 256.f;
262 sumb /= 256.f;
263 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
264 }
265 }
266
267 static void filter_kirsch(uint8_t *dst, int width,
268 float scale, float delta, const int *const matrix,
269 const uint8_t *c[], int peak, int radius,
270 int dstride, int stride, int size)
271 {
272 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
273 const uint8_t *c3 = c[3], *c5 = c[5];
274 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
275 int x;
276
277 for (x = 0; x < width; x++) {
278 int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
279 c3[x] * -3 + c5[x] * -3 +
280 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
281 int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
282 c3[x] * 5 + c5[x] * -3 +
283 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
284 int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
285 c3[x] * 5 + c5[x] * 5 +
286 c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
287 int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
288 c3[x] * 5 + c5[x] * 5 +
289 c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
290 int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
291 c3[x] * -3 + c5[x] * 5 +
292 c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
293 int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
294 c3[x] * -3 + c5[x] * -3 +
295 c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
296 int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
297 c3[x] * -3 + c5[x] * -3 +
298 c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
299 int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
300 c3[x] * -3 + c5[x] * -3 +
301 c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
302
303 sum0 = FFMAX(sum0, sum1);
304 sum2 = FFMAX(sum2, sum3);
305 sum4 = FFMAX(sum4, sum5);
306 sum6 = FFMAX(sum6, sum7);
307 sum0 = FFMAX(sum0, sum2);
308 sum4 = FFMAX(sum4, sum6);
309 sum0 = FFMAX(sum0, sum4);
310
311 dst[x] = av_clip_uint8(FFABS(sum0) * scale + delta);
312 }
313 }
314
315 static void filter16_3x3(uint8_t *dstp, int width,
316 float rdiv, float bias, const int *const matrix,
317 const uint8_t *c[], int peak, int radius,
318 int dstride, int stride, int size)
319 {
320 uint16_t *dst = (uint16_t *)dstp;
321 int x;
322
323 for (x = 0; x < width; x++) {
324 int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
325 AV_RN16A(&c[1][2 * x]) * matrix[1] +
326 AV_RN16A(&c[2][2 * x]) * matrix[2] +
327 AV_RN16A(&c[3][2 * x]) * matrix[3] +
328 AV_RN16A(&c[4][2 * x]) * matrix[4] +
329 AV_RN16A(&c[5][2 * x]) * matrix[5] +
330 AV_RN16A(&c[6][2 * x]) * matrix[6] +
331 AV_RN16A(&c[7][2 * x]) * matrix[7] +
332 AV_RN16A(&c[8][2 * x]) * matrix[8];
333 sum = (int)(sum * rdiv + bias + 0.5f);
334 dst[x] = av_clip(sum, 0, peak);
335 }
336 }
337
338 static void filter16_5x5(uint8_t *dstp, int width,
339 float rdiv, float bias, const int *const matrix,
340 const uint8_t *c[], int peak, int radius,
341 int dstride, int stride, int size)
342 {
343 uint16_t *dst = (uint16_t *)dstp;
344 int x;
345
346 for (x = 0; x < width; x++) {
347 int i, sum = 0;
348
349 for (i = 0; i < 25; i++)
350 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
351
352 sum = (int)(sum * rdiv + bias + 0.5f);
353 dst[x] = av_clip(sum, 0, peak);
354 }
355 }
356
357 static void filter16_7x7(uint8_t *dstp, int width,
358 float rdiv, float bias, const int *const matrix,
359 const uint8_t *c[], int peak, int radius,
360 int dstride, int stride, int size)
361 {
362 uint16_t *dst = (uint16_t *)dstp;
363 int x;
364
365 for (x = 0; x < width; x++) {
366 int i, sum = 0;
367
368 for (i = 0; i < 49; i++)
369 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
370
371 sum = (int)(sum * rdiv + bias + 0.5f);
372 dst[x] = av_clip(sum, 0, peak);
373 }
374 }
375
376 static void filter16_row(uint8_t *dstp, int width,
377 float rdiv, float bias, const int *const matrix,
378 const uint8_t *c[], int peak, int radius,
379 int dstride, int stride, int size)
380 {
381 uint16_t *dst = (uint16_t *)dstp;
382 int x;
383
384 for (x = 0; x < width; x++) {
385 int i, sum = 0;
386
387 for (i = 0; i < 2 * radius + 1; i++)
388 sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
389
390 sum = (int)(sum * rdiv + bias + 0.5f);
391 dst[x] = av_clip(sum, 0, peak);
392 }
393 }
394
395 static void filter16_column(uint8_t *dstp, int height,
396 float rdiv, float bias, const int *const matrix,
397 const uint8_t *c[], int peak, int radius,
398 int dstride, int stride, int size)
399 {
400 DECLARE_ALIGNED(64, int, sum)[16];
401 uint16_t *dst = (uint16_t *)dstp;
402 const int width = FFMIN(16, size);
403
404 for (int y = 0; y < height; y++) {
405
406 memset(sum, 0, sizeof(sum));
407 for (int i = 0; i < 2 * radius + 1; i++) {
408 for (int off16 = 0; off16 < width; off16++)
409 sum[off16] += AV_RN16A(&c[i][0 + y * stride + off16 * 2]) * matrix[i];
410 }
411
412 for (int off16 = 0; off16 < width; off16++) {
413 sum[off16] = (int)(sum[off16] * rdiv + bias + 0.5f);
414 dst[off16] = av_clip(sum[off16], 0, peak);
415 }
416 dst += dstride / 2;
417 }
418 }
419
420 static void filter_7x7(uint8_t *dst, int width,
421 float rdiv, float bias, const int *const matrix,
422 const uint8_t *c[], int peak, int radius,
423 int dstride, int stride, int size)
424 {
425 int x;
426
427 for (x = 0; x < width; x++) {
428 int i, sum = 0;
429
430 for (i = 0; i < 49; i++)
431 sum += c[i][x] * matrix[i];
432
433 sum = (int)(sum * rdiv + bias + 0.5f);
434 dst[x] = av_clip_uint8(sum);
435 }
436 }
437
438 static void filter_5x5(uint8_t *dst, int width,
439 float rdiv, float bias, const int *const matrix,
440 const uint8_t *c[], int peak, int radius,
441 int dstride, int stride, int size)
442 {
443 int x;
444
445 for (x = 0; x < width; x++) {
446 int i, sum = 0;
447
448 for (i = 0; i < 25; i++)
449 sum += c[i][x] * matrix[i];
450
451 sum = (int)(sum * rdiv + bias + 0.5f);
452 dst[x] = av_clip_uint8(sum);
453 }
454 }
455
456 static void filter_3x3(uint8_t *dst, int width,
457 float rdiv, float bias, const int *const matrix,
458 const uint8_t *c[], int peak, int radius,
459 int dstride, int stride, int size)
460 {
461 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
462 const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
463 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
464 int x;
465
466 for (x = 0; x < width; x++) {
467 int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
468 c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
469 c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
470 sum = (int)(sum * rdiv + bias + 0.5f);
471 dst[x] = av_clip_uint8(sum);
472 }
473 }
474
475 static void filter_row(uint8_t *dst, int width,
476 float rdiv, float bias, const int *const matrix,
477 const uint8_t *c[], int peak, int radius,
478 int dstride, int stride, int size)
479 {
480 int x;
481
482 for (x = 0; x < width; x++) {
483 int i, sum = 0;
484
485 for (i = 0; i < 2 * radius + 1; i++)
486 sum += c[i][x] * matrix[i];
487
488 sum = (int)(sum * rdiv + bias + 0.5f);
489 dst[x] = av_clip_uint8(sum);
490 }
491 }
492
493 static void filter_column(uint8_t *dst, int height,
494 float rdiv, float bias, const int *const matrix,
495 const uint8_t *c[], int peak, int radius,
496 int dstride, int stride, int size)
497 {
498 DECLARE_ALIGNED(64, int, sum)[16];
499
500 for (int y = 0; y < height; y++) {
501 memset(sum, 0, sizeof(sum));
502
503 for (int i = 0; i < 2 * radius + 1; i++) {
504 for (int off16 = 0; off16 < 16; off16++)
505 sum[off16] += c[i][0 + y * stride + off16] * matrix[i];
506 }
507
508 for (int off16 = 0; off16 < 16; off16++) {
509 sum[off16] = (int)(sum[off16] * rdiv + bias + 0.5f);
510 dst[off16] = av_clip_uint8(sum[off16]);
511 }
512 dst += dstride;
513 }
514 }
515
516 static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
517 int x, int w, int y, int h, int bpc)
518 {
519 int i;
520
521 for (i = 0; i < 25; i++) {
522 int xoff = FFABS(x + ((i % 5) - 2));
523 int yoff = FFABS(y + (i / 5) - 2);
524
525 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
526 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
527
528 c[i] = src + xoff * bpc + yoff * stride;
529 }
530 }
531
532 static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
533 int x, int w, int y, int h, int bpc)
534 {
535 int i;
536
537 for (i = 0; i < 49; i++) {
538 int xoff = FFABS(x + ((i % 7) - 3));
539 int yoff = FFABS(y + (i / 7) - 3);
540
541 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
542 yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
543
544 c[i] = src + xoff * bpc + yoff * stride;
545 }
546 }
547
548 static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
549 int x, int w, int y, int h, int bpc)
550 {
551 int i;
552
553 for (i = 0; i < radius * 2 + 1; i++) {
554 int xoff = FFABS(x + i - radius);
555
556 xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
557
558 c[i] = src + xoff * bpc + y * stride;
559 }
560 }
561
562 static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
563 int x, int w, int y, int h, int bpc)
564 {
565 int i;
566
567 for (i = 0; i < radius * 2 + 1; i++) {
568 int xoff = FFABS(x + i - radius);
569
570 xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
571
572 c[i] = src + y * bpc + xoff * stride;
573 }
574 }
575
576 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
577 {
578 ConvolutionContext *s = ctx->priv;
579 ThreadData *td = arg;
580 AVFrame *in = td->in;
581 AVFrame *out = td->out;
582 int plane;
583
584 for (plane = 0; plane < s->nb_planes; plane++) {
585 const int mode = s->mode[plane];
586 const int bpc = s->bpc;
587 const int radius = s->size[plane] / 2;
588 const int height = s->planeheight[plane];
589 const int width = s->planewidth[plane];
590 const int stride = in->linesize[plane];
591 const int dstride = out->linesize[plane];
592 const int sizeh = mode == MATRIX_COLUMN ? width : height;
593 const int sizew = mode == MATRIX_COLUMN ? height : width;
594 const int slice_start = (sizeh * jobnr) / nb_jobs;
595 const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
596 const float rdiv = s->rdiv[plane];
597 const float bias = s->bias[plane];
598 const uint8_t *src = in->data[plane];
599 const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
600 uint8_t *dst = out->data[plane] + dst_pos;
601 const int *matrix = s->matrix[plane];
602 const int step = mode == MATRIX_COLUMN ? 16 : 1;
603 const uint8_t *c[49];
604 int y, x;
605
606 if (s->copy[plane]) {
607 if (mode == MATRIX_COLUMN)
608 av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
609 (slice_end - slice_start) * bpc, height);
610 else
611 av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
612 width * bpc, slice_end - slice_start);
613 continue;
614 }
615 for (y = slice_start; y < slice_end; y += step) {
616 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
617 const int yoff = mode == MATRIX_COLUMN ? radius * dstride : 0;
618
619 for (x = 0; x < radius; x++) {
620 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
621 const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
622
623 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
624 s->filter[plane](dst + yoff + xoff, 1, rdiv,
625 bias, matrix, c, s->max, radius,
626 dstride, stride, slice_end - step);
627 }
628 s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
629 s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
630 rdiv, bias, matrix, c, s->max, radius,
631 dstride, stride, slice_end - step);
632 for (x = sizew - radius; x < sizew; x++) {
633 const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
634 const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
635
636 s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
637 s->filter[plane](dst + yoff + xoff, 1, rdiv,
638 bias, matrix, c, s->max, radius,
639 dstride, stride, slice_end - step);
640 }
641 if (mode != MATRIX_COLUMN)
642 dst += dstride;
643 }
644 }
645
646 return 0;
647 }
648
649 static int param_init(AVFilterContext *ctx)
650 {
651 ConvolutionContext *s = ctx->priv;
652 AVFilterLink *inlink = ctx->inputs[0];
653 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
654 int p, i;
655
656 s->depth = desc->comp[0].depth;
657 s->max = (1 << s->depth) - 1;
658
659 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
660 s->planewidth[0] = s->planewidth[3] = inlink->w;
661 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
662 s->planeheight[0] = s->planeheight[3] = inlink->h;
663
664 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
665 s->nb_threads = ff_filter_get_nb_threads(ctx);
666 s->bpc = (s->depth + 7) / 8;
667
668 if (!strcmp(ctx->filter->name, "convolution")) {
669 for (i = 0; i < 4; i++) {
670 int *matrix = (int *)s->matrix[i];
671 char *orig, *p, *arg, *saveptr = NULL;
672 float sum = 1.f;
673
674 p = orig = av_strdup(s->matrix_str[i]);
675 if (p) {
676 s->matrix_length[i] = 0;
677 s->rdiv[i] = s->user_rdiv[i];
678 sum = 0.f;
679
680 while (s->matrix_length[i] < 49) {
681 if (!(arg = av_strtok(p, " |", &saveptr)))
682 break;
683
684 p = NULL;
685 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
686 sum += matrix[s->matrix_length[i]];
687 s->matrix_length[i]++;
688 }
689
690 av_freep(&orig);
691 if (!(s->matrix_length[i] & 1)) {
692 av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
693 return AVERROR(EINVAL);
694 }
695 }
696
697 if (s->mode[i] == MATRIX_ROW) {
698 s->filter[i] = filter_row;
699 s->setup[i] = setup_row;
700 s->size[i] = s->matrix_length[i];
701 } else if (s->mode[i] == MATRIX_COLUMN) {
702 s->filter[i] = filter_column;
703 s->setup[i] = setup_column;
704 s->size[i] = s->matrix_length[i];
705 } else if (s->matrix_length[i] == 9) {
706 s->size[i] = 3;
707
708 if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
709 s->copy[i] = 1;
710 } else {
711 s->filter[i] = filter_3x3;
712 s->copy[i] = 0;
713 }
714 s->setup[i] = setup_3x3;
715 } else if (s->matrix_length[i] == 25) {
716 s->size[i] = 5;
717 if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
718 s->copy[i] = 1;
719 } else {
720 s->filter[i] = filter_5x5;
721 s->copy[i] = 0;
722 }
723 s->setup[i] = setup_5x5;
724 } else if (s->matrix_length[i] == 49) {
725 s->size[i] = 7;
726 if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
727 s->copy[i] = 1;
728 } else {
729 s->filter[i] = filter_7x7;
730 s->copy[i] = 0;
731 }
732 s->setup[i] = setup_7x7;
733 } else {
734 return AVERROR(EINVAL);
735 }
736
737 if (sum == 0)
738 sum = 1;
739 if (s->rdiv[i] == 0)
740 s->rdiv[i] = 1. / sum;
741
742 if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
743 s->copy[i] = 0;
744 }
745 } else if (!strcmp(ctx->filter->name, "prewitt")) {
746 for (i = 0; i < 4; i++) {
747 s->filter[i] = filter_prewitt;
748 s->copy[i] = !((1 << i) & s->planes);
749 s->size[i] = 3;
750 s->setup[i] = setup_3x3;
751 s->rdiv[i] = s->scale;
752 s->bias[i] = s->delta;
753 }
754 } else if (!strcmp(ctx->filter->name, "roberts")) {
755 for (i = 0; i < 4; i++) {
756 s->filter[i] = filter_roberts;
757 s->copy[i] = !((1 << i) & s->planes);
758 s->size[i] = 3;
759 s->setup[i] = setup_3x3;
760 s->rdiv[i] = s->scale;
761 s->bias[i] = s->delta;
762 }
763 } else if (!strcmp(ctx->filter->name, "sobel")) {
764 ff_sobel_init(s, s->depth, s->nb_planes);
765 } else if (!strcmp(ctx->filter->name, "kirsch")) {
766 for (i = 0; i < 4; i++) {
767 s->filter[i] = filter_kirsch;
768 s->copy[i] = !((1 << i) & s->planes);
769 s->size[i] = 3;
770 s->setup[i] = setup_3x3;
771 s->rdiv[i] = s->scale;
772 s->bias[i] = s->delta;
773 }
774 } else if (!strcmp(ctx->filter->name, "scharr")) {
775 for (i = 0; i < 4; i++) {
776 s->filter[i] = filter_scharr;
777 s->copy[i] = !((1 << i) & s->planes);
778 s->size[i] = 3;
779 s->setup[i] = setup_3x3;
780 s->rdiv[i] = s->scale;
781 s->bias[i] = s->delta;
782 }
783 }
784
785 if (!strcmp(ctx->filter->name, "convolution")) {
786 if (s->depth > 8) {
787 for (p = 0; p < s->nb_planes; p++) {
788 if (s->mode[p] == MATRIX_ROW)
789 s->filter[p] = filter16_row;
790 else if (s->mode[p] == MATRIX_COLUMN)
791 s->filter[p] = filter16_column;
792 else if (s->size[p] == 3)
793 s->filter[p] = filter16_3x3;
794 else if (s->size[p] == 5)
795 s->filter[p] = filter16_5x5;
796 else if (s->size[p] == 7)
797 s->filter[p] = filter16_7x7;
798 }
799 }
800 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
801 ff_convolution_init_x86(s);
802 #endif
803 } else if (!strcmp(ctx->filter->name, "prewitt")) {
804 if (s->depth > 8)
805 for (p = 0; p < s->nb_planes; p++)
806 s->filter[p] = filter16_prewitt;
807 } else if (!strcmp(ctx->filter->name, "roberts")) {
808 if (s->depth > 8)
809 for (p = 0; p < s->nb_planes; p++)
810 s->filter[p] = filter16_roberts;
811 } else if (!strcmp(ctx->filter->name, "kirsch")) {
812 if (s->depth > 8)
813 for (p = 0; p < s->nb_planes; p++)
814 s->filter[p] = filter16_kirsch;
815 } else if (!strcmp(ctx->filter->name, "scharr")) {
816 if (s->depth > 8)
817 for (p = 0; p < s->nb_planes; p++)
818 s->filter[p] = filter16_scharr;
819 }
820
821 return 0;
822 }
823
824 static int config_input(AVFilterLink *inlink)
825 {
826 AVFilterContext *ctx = inlink->dst;
827 return param_init(ctx);
828 }
829
830 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
831 {
832 AVFilterContext *ctx = inlink->dst;
833 ConvolutionContext *s = ctx->priv;
834 AVFilterLink *outlink = ctx->outputs[0];
835 AVFrame *out;
836 ThreadData td;
837
838 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
839 if (!out) {
840 av_frame_free(&in);
841 return AVERROR(ENOMEM);
842 }
843 av_frame_copy_props(out, in);
844
845 td.in = in;
846 td.out = out;
847 ff_filter_execute(ctx, filter_slice, &td, NULL,
848 FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
849
850 av_frame_free(&in);
851 return ff_filter_frame(outlink, out);
852 }
853
854 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
855 char *res, int res_len, int flags)
856 {
857 int ret;
858
859 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
860 if (ret < 0)
861 return ret;
862
863 return param_init(ctx);
864 }
865
866 static const AVFilterPad convolution_inputs[] = {
867 {
868 .name = "default",
869 .type = AVMEDIA_TYPE_VIDEO,
870 .config_props = config_input,
871 .filter_frame = filter_frame,
872 },
873 };
874
875 #if CONFIG_CONVOLUTION_FILTER
876
877 const AVFilter ff_vf_convolution = {
878 .name = "convolution",
879 .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
880 .priv_size = sizeof(ConvolutionContext),
881 .priv_class = &convolution_class,
882 FILTER_INPUTS(convolution_inputs),
883 FILTER_OUTPUTS(ff_video_default_filterpad),
884 FILTER_PIXFMTS_ARRAY(pix_fmts),
885 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
886 .process_command = process_command,
887 };
888
889 #endif /* CONFIG_CONVOLUTION_FILTER */
890
891 static const AVOption common_options[] = {
892 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
893 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
894 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
895 { NULL }
896 };
897
898 AVFILTER_DEFINE_CLASS_EXT(common, "kirsch/prewitt/roberts/scharr/sobel",
899 common_options);
900
901 #if CONFIG_PREWITT_FILTER
902
903 const AVFilter ff_vf_prewitt = {
904 .name = "prewitt",
905 .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
906 .priv_size = sizeof(ConvolutionContext),
907 .priv_class = &common_class,
908 FILTER_INPUTS(convolution_inputs),
909 FILTER_OUTPUTS(ff_video_default_filterpad),
910 FILTER_PIXFMTS_ARRAY(pix_fmts),
911 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
912 .process_command = process_command,
913 };
914
915 #endif /* CONFIG_PREWITT_FILTER */
916
917 #if CONFIG_SOBEL_FILTER
918
919 const AVFilter ff_vf_sobel = {
920 .name = "sobel",
921 .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
922 .priv_size = sizeof(ConvolutionContext),
923 .priv_class = &common_class,
924 FILTER_INPUTS(convolution_inputs),
925 FILTER_OUTPUTS(ff_video_default_filterpad),
926 FILTER_PIXFMTS_ARRAY(pix_fmts),
927 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
928 .process_command = process_command,
929 };
930
931 #endif /* CONFIG_SOBEL_FILTER */
932
933 #if CONFIG_ROBERTS_FILTER
934
935 const AVFilter ff_vf_roberts = {
936 .name = "roberts",
937 .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
938 .priv_size = sizeof(ConvolutionContext),
939 .priv_class = &common_class,
940 FILTER_INPUTS(convolution_inputs),
941 FILTER_OUTPUTS(ff_video_default_filterpad),
942 FILTER_PIXFMTS_ARRAY(pix_fmts),
943 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
944 .process_command = process_command,
945 };
946
947 #endif /* CONFIG_ROBERTS_FILTER */
948
949 #if CONFIG_KIRSCH_FILTER
950
951 const AVFilter ff_vf_kirsch = {
952 .name = "kirsch",
953 .description = NULL_IF_CONFIG_SMALL("Apply kirsch operator."),
954 .priv_size = sizeof(ConvolutionContext),
955 .priv_class = &common_class,
956 FILTER_INPUTS(convolution_inputs),
957 FILTER_OUTPUTS(ff_video_default_filterpad),
958 FILTER_PIXFMTS_ARRAY(pix_fmts),
959 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
960 .process_command = process_command,
961 };
962
963 #endif /* CONFIG_KIRSCH_FILTER */
964
965 #if CONFIG_SCHARR_FILTER
966
967 const AVFilter ff_vf_scharr = {
968 .name = "scharr",
969 .description = NULL_IF_CONFIG_SMALL("Apply scharr operator."),
970 .priv_size = sizeof(ConvolutionContext),
971 .priv_class = &common_class,
972 FILTER_INPUTS(convolution_inputs),
973 FILTER_OUTPUTS(ff_video_default_filterpad),
974 FILTER_PIXFMTS_ARRAY(pix_fmts),
975 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
976 .process_command = process_command,
977 };
978
979 #endif /* CONFIG_SCHARR_FILTER */
980