]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_convolution.c
avfilter/vf_convolution: Fix build failures
[ffmpeg] / libavfilter / vf_convolution.c
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 "libavutil/avstring.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "convolution.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32
33 #define OFFSET(x) offsetof(ConvolutionContext, x)
34 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
35
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" },
56     { NULL }
57 };
58
59 AVFILTER_DEFINE_CLASS(convolution);
60
61 static const int same3x3[9] = {0, 0, 0,
62                                0, 1, 0,
63                                0, 0, 0};
64
65 static const int same5x5[25] = {0, 0, 0, 0, 0,
66                                 0, 0, 0, 0, 0,
67                                 0, 0, 1, 0, 0,
68                                 0, 0, 0, 0, 0,
69                                 0, 0, 0, 0, 0};
70
71 static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
72                                 0, 0, 0, 0, 0, 0, 0,
73                                 0, 0, 0, 0, 0, 0, 0,
74                                 0, 0, 0, 1, 0, 0, 0,
75                                 0, 0, 0, 0, 0, 0, 0,
76                                 0, 0, 0, 0, 0, 0, 0,
77                                 0, 0, 0, 0, 0, 0, 0};
78
79 static int query_formats(AVFilterContext *ctx)
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_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,
99         AV_PIX_FMT_NONE
100     };
101
102     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
103 }
104
105 typedef struct ThreadData {
106     AVFrame *in, *out;
107 } ThreadData;
108
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)
113 {
114     uint16_t *dst = (uint16_t *)dstp;
115     int x;
116
117     for (x = 0; x < width; x++) {
118         int 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         int 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;
122
123         dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
124     }
125 }
126
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)
131 {
132     uint16_t *dst = (uint16_t *)dstp;
133     int x;
134
135     for (x = 0; x < width; x++) {
136         int suma = AV_RN16A(&c[0][2 * x]) *  1 + AV_RN16A(&c[1][2 * x]) * -1;
137         int sumb = AV_RN16A(&c[4][2 * x]) *  1 + AV_RN16A(&c[3][2 * x]) * -1;
138
139         dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
140     }
141 }
142
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)
147 {
148     uint16_t *dst = (uint16_t *)dstp;
149     int x;
150
151     for (x = 0; x < width; x++) {
152         int 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         int 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;
156
157         dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
158     }
159 }
160
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)
165 {
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];
169     int x;
170
171     for (x = 0; x < width; x++) {
172         int suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
173                    c6[x] *  1 + c7[x] *  1 + c8[x] *  1;
174         int sumb = c0[x] * -1 + c2[x] *  1 + c3[x] * -1 +
175                    c5[x] *  1 + c6[x] * -1 + c8[x] *  1;
176
177         dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
178     }
179 }
180
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)
185 {
186     int x;
187
188     for (x = 0; x < width; x++) {
189         int suma = c[0][x] *  1 + c[1][x] * -1;
190         int sumb = c[4][x] *  1 + c[3][x] * -1;
191
192         dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
193     }
194 }
195
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)
200 {
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];
204     int x;
205
206     for (x = 0; x < width; x++) {
207         int suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
208                    c6[x] *  1 + c7[x] *  2 + c8[x] *  1;
209         int sumb = c0[x] * -1 + c2[x] *  1 + c3[x] * -2 +
210                    c5[x] *  2 + c6[x] * -1 + c8[x] *  1;
211
212         dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
213     }
214 }
215
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)
220 {
221     uint16_t *dst = (uint16_t *)dstp;
222     int x;
223
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);
236     }
237 }
238
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)
243 {
244     uint16_t *dst = (uint16_t *)dstp;
245     int x;
246
247     for (x = 0; x < width; x++) {
248         int i, sum = 0;
249
250         for (i = 0; i < 25; i++)
251             sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
252
253         sum = (int)(sum * rdiv + bias + 0.5f);
254         dst[x] = av_clip(sum, 0, peak);
255     }
256 }
257
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)
262 {
263     uint16_t *dst = (uint16_t *)dstp;
264     int x;
265
266     for (x = 0; x < width; x++) {
267         int i, sum = 0;
268
269         for (i = 0; i < 49; i++)
270             sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
271
272         sum = (int)(sum * rdiv + bias + 0.5f);
273         dst[x] = av_clip(sum, 0, peak);
274     }
275 }
276
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)
281 {
282     uint16_t *dst = (uint16_t *)dstp;
283     int x;
284
285     for (x = 0; x < width; x++) {
286         int i, sum = 0;
287
288         for (i = 0; i < 2 * radius + 1; i++)
289             sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
290
291         sum = (int)(sum * rdiv + bias + 0.5f);
292         dst[x] = av_clip(sum, 0, peak);
293     }
294 }
295
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)
300 {
301     uint16_t *dst = (uint16_t *)dstp;
302     int y;
303
304     for (y = 0; y < height; y++) {
305         int i, sum = 0;
306
307         for (i = 0; i < 2 * radius + 1; i++)
308             sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
309
310         sum = (int)(sum * rdiv + bias + 0.5f);
311         dst[0] = av_clip(sum, 0, peak);
312         dst += dstride / 2;
313     }
314 }
315
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)
320 {
321     int x;
322
323     for (x = 0; x < width; x++) {
324         int i, sum = 0;
325
326         for (i = 0; i < 49; i++)
327             sum += c[i][x] * matrix[i];
328
329         sum = (int)(sum * rdiv + bias + 0.5f);
330         dst[x] = av_clip_uint8(sum);
331     }
332 }
333
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)
338 {
339     int x;
340
341     for (x = 0; x < width; x++) {
342         int i, sum = 0;
343
344         for (i = 0; i < 25; i++)
345             sum += c[i][x] * matrix[i];
346
347         sum = (int)(sum * rdiv + bias + 0.5f);
348         dst[x] = av_clip_uint8(sum);
349     }
350 }
351
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)
356 {
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];
360     int x;
361
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);
368     }
369 }
370
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)
375 {
376     int x;
377
378     for (x = 0; x < width; x++) {
379         int i, sum = 0;
380
381         for (i = 0; i < 2 * radius + 1; i++)
382             sum += c[i][x] * matrix[i];
383
384         sum = (int)(sum * rdiv + bias + 0.5f);
385         dst[x] = av_clip_uint8(sum);
386     }
387 }
388
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)
393 {
394     int y;
395
396     for (y = 0; y < height; y++) {
397         int i, sum = 0;
398
399         for (i = 0; i < 2 * radius + 1; i++)
400             sum += c[i][0 + y * stride] * matrix[i];
401
402         sum = (int)(sum * rdiv + bias + 0.5f);
403         dst[0] = av_clip_uint8(sum);
404         dst += dstride;
405     }
406 }
407
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)
410 {
411     int i;
412
413     for (i = 0; i < 9; i++) {
414         int xoff = FFABS(x + ((i % 3) - 1));
415         int yoff = FFABS(y + (i / 3) - 1);
416
417         xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
418         yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
419
420         c[i] = src + xoff * bpc + yoff * stride;
421     }
422 }
423
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)
426 {
427     int i;
428
429     for (i = 0; i < 25; i++) {
430         int xoff = FFABS(x + ((i % 5) - 2));
431         int yoff = FFABS(y + (i / 5) - 2);
432
433         xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
434         yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
435
436         c[i] = src + xoff * bpc + yoff * stride;
437     }
438 }
439
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)
442 {
443     int i;
444
445     for (i = 0; i < 49; i++) {
446         int xoff = FFABS(x + ((i % 7) - 3));
447         int yoff = FFABS(y + (i / 7) - 3);
448
449         xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
450         yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
451
452         c[i] = src + xoff * bpc + yoff * stride;
453     }
454 }
455
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)
458 {
459     int i;
460
461     for (i = 0; i < radius * 2 + 1; i++) {
462         int xoff = FFABS(x + i - radius);
463
464         xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
465
466         c[i] = src + xoff * bpc + y * stride;
467     }
468 }
469
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)
472 {
473     int i;
474
475     for (i = 0; i < radius * 2 + 1; i++) {
476         int xoff = FFABS(x + i - radius);
477
478         xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
479
480         c[i] = src + y * bpc + xoff * stride;
481     }
482 }
483
484 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
485 {
486     ConvolutionContext *s = ctx->priv;
487     ThreadData *td = arg;
488     AVFrame *in = td->in;
489     AVFrame *out = td->out;
490     int plane;
491
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];
511         int y, x;
512
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);
517             else
518                 av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
519                                     width * bpc, slice_end - slice_start);
520             continue;
521         }
522
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;
526
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;
530
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,
534                                  dstride, stride);
535             }
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,
539                              dstride, stride);
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;
543
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,
547                                  dstride, stride);
548             }
549             if (mode != MATRIX_COLUMN)
550                 dst += dstride;
551         }
552     }
553
554     return 0;
555 }
556
557 static int config_input(AVFilterLink *inlink)
558 {
559     AVFilterContext *ctx = inlink->dst;
560     ConvolutionContext *s = ctx->priv;
561     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
562     int p;
563
564     s->depth = desc->comp[0].depth;
565     s->max = (1 << s->depth) - 1;
566
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;
571
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;
575
576     if (!strcmp(ctx->filter->name, "convolution")) {
577         if (s->depth > 8) {
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;
589             }
590         }
591 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
592         ff_convolution_init_x86(s);
593 #endif
594     } else if (!strcmp(ctx->filter->name, "prewitt")) {
595         if (s->depth > 8)
596             for (p = 0; p < s->nb_planes; p++)
597                 s->filter[p] = filter16_prewitt;
598     } else if (!strcmp(ctx->filter->name, "roberts")) {
599         if (s->depth > 8)
600             for (p = 0; p < s->nb_planes; p++)
601                 s->filter[p] = filter16_roberts;
602     } else if (!strcmp(ctx->filter->name, "sobel")) {
603         if (s->depth > 8)
604             for (p = 0; p < s->nb_planes; p++)
605                 s->filter[p] = filter16_sobel;
606     }
607
608     return 0;
609 }
610
611 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
612 {
613     AVFilterContext *ctx = inlink->dst;
614     ConvolutionContext *s = ctx->priv;
615     AVFilterLink *outlink = ctx->outputs[0];
616     AVFrame *out;
617     ThreadData td;
618
619     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
620     if (!out) {
621         av_frame_free(&in);
622         return AVERROR(ENOMEM);
623     }
624     av_frame_copy_props(out, in);
625
626     td.in = in;
627     td.out = out;
628     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
629
630     av_frame_free(&in);
631     return ff_filter_frame(outlink, out);
632 }
633
634 static av_cold int init(AVFilterContext *ctx)
635 {
636     ConvolutionContext *s = ctx->priv;
637     int i;
638
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;
643             float sum = 0;
644
645             p = s->matrix_str[i];
646             while (s->matrix_length[i] < 49) {
647                 if (!(arg = av_strtok(p, " ", &saveptr)))
648                     break;
649
650                 p = NULL;
651                 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
652                 sum += matrix[s->matrix_length[i]];
653                 s->matrix_length[i]++;
654             }
655
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);
659             }
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) {
669                 s->size[i] = 3;
670                 if (!memcmp(matrix, same3x3, sizeof(same3x3)))
671                     s->copy[i] = 1;
672                 else
673                     s->filter[i] = filter_3x3;
674                 s->setup[i] = setup_3x3;
675             } else if (s->matrix_length[i] == 25) {
676                 s->size[i] = 5;
677                 if (!memcmp(matrix, same5x5, sizeof(same5x5)))
678                     s->copy[i] = 1;
679                 else
680                     s->filter[i] = filter_5x5;
681                 s->setup[i] = setup_5x5;
682             } else if (s->matrix_length[i] == 49) {
683                 s->size[i] = 7;
684                 if (!memcmp(matrix, same7x7, sizeof(same7x7)))
685                     s->copy[i] = 1;
686                 else
687                     s->filter[i] = filter_7x7;
688                 s->setup[i] = setup_7x7;
689             } else {
690                 return AVERROR(EINVAL);
691             }
692
693             if (sum == 0)
694                 sum = 1;
695             if (s->rdiv[i] == 0)
696                 s->rdiv[i] = 1. / sum;
697
698             if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
699                 s->copy[i] = 0;
700         }
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;
705             else
706                 s->copy[i] = 1;
707             s->size[i] = 3;
708             s->setup[i] = setup_3x3;
709             s->rdiv[i] = s->scale;
710             s->bias[i] = s->delta;
711         }
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;
716             else
717                 s->copy[i] = 1;
718             s->size[i] = 3;
719             s->setup[i] = setup_3x3;
720             s->rdiv[i] = s->scale;
721             s->bias[i] = s->delta;
722         }
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;
727             else
728                 s->copy[i] = 1;
729             s->size[i] = 3;
730             s->setup[i] = setup_3x3;
731             s->rdiv[i] = s->scale;
732             s->bias[i] = s->delta;
733         }
734     }
735
736     return 0;
737 }
738
739 static const AVFilterPad convolution_inputs[] = {
740     {
741         .name         = "default",
742         .type         = AVMEDIA_TYPE_VIDEO,
743         .config_props = config_input,
744         .filter_frame = filter_frame,
745     },
746     { NULL }
747 };
748
749 static const AVFilterPad convolution_outputs[] = {
750     {
751         .name = "default",
752         .type = AVMEDIA_TYPE_VIDEO,
753     },
754     { NULL }
755 };
756
757 #if CONFIG_CONVOLUTION_FILTER
758
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,
764     .init          = init,
765     .query_formats = query_formats,
766     .inputs        = convolution_inputs,
767     .outputs       = convolution_outputs,
768     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
769 };
770
771 #endif /* CONFIG_CONVOLUTION_FILTER */
772
773 #if CONFIG_PREWITT_FILTER
774
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},
779     { NULL }
780 };
781
782 AVFILTER_DEFINE_CLASS(prewitt);
783
784 AVFilter ff_vf_prewitt = {
785     .name          = "prewitt",
786     .description   = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
787     .priv_size     = sizeof(ConvolutionContext),
788     .priv_class    = &prewitt_class,
789     .init          = init,
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 };
795
796 #endif /* CONFIG_PREWITT_FILTER */
797
798 #if CONFIG_SOBEL_FILTER
799
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},
804     { NULL }
805 };
806
807 AVFILTER_DEFINE_CLASS(sobel);
808
809 AVFilter ff_vf_sobel = {
810     .name          = "sobel",
811     .description   = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
812     .priv_size     = sizeof(ConvolutionContext),
813     .priv_class    = &sobel_class,
814     .init          = init,
815     .query_formats = query_formats,
816     .inputs        = convolution_inputs,
817     .outputs       = convolution_outputs,
818     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
819 };
820
821 #endif /* CONFIG_SOBEL_FILTER */
822
823 #if CONFIG_ROBERTS_FILTER
824
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},
829     { NULL }
830 };
831
832 AVFILTER_DEFINE_CLASS(roberts);
833
834 AVFilter ff_vf_roberts = {
835     .name          = "roberts",
836     .description   = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
837     .priv_size     = sizeof(ConvolutionContext),
838     .priv_class    = &roberts_class,
839     .init          = init,
840     .query_formats = query_formats,
841     .inputs        = convolution_inputs,
842     .outputs       = convolution_outputs,
843     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
844 };
845
846 #endif /* CONFIG_ROBERTS_FILTER */