FFmpeg
vf_convolution.c
Go to the documentation of this file.
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 "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 #define OFFSET(x) offsetof(ConvolutionContext, x)
37 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
38 
39 static const AVOption convolution_options[] = {
40  { "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 },
41  { "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 },
42  { "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 },
43  { "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 },
44  { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
45  { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
46  { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
47  { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
48  { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
49  { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
50  { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
51  { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
52  { "0mode", "set matrix mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
53  { "1mode", "set matrix mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
54  { "2mode", "set matrix mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
55  { "3mode", "set matrix mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
56  { "square", "square matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_SQUARE}, 0, 0, FLAGS, "mode" },
57  { "row", "single row matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_ROW} , 0, 0, FLAGS, "mode" },
58  { "column", "single column matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_COLUMN}, 0, 0, FLAGS, "mode" },
59  { NULL }
60 };
61 
62 AVFILTER_DEFINE_CLASS(convolution);
63 
64 static const int same3x3[9] = {0, 0, 0,
65  0, 1, 0,
66  0, 0, 0};
67 
68 static const int same5x5[25] = {0, 0, 0, 0, 0,
69  0, 0, 0, 0, 0,
70  0, 0, 1, 0, 0,
71  0, 0, 0, 0, 0,
72  0, 0, 0, 0, 0};
73 
74 static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
75  0, 0, 0, 0, 0, 0, 0,
76  0, 0, 0, 0, 0, 0, 0,
77  0, 0, 0, 1, 0, 0, 0,
78  0, 0, 0, 0, 0, 0, 0,
79  0, 0, 0, 0, 0, 0, 0,
80  0, 0, 0, 0, 0, 0, 0};
81 
82 static const enum AVPixelFormat pix_fmts[] = {
102 };
103 
104 typedef struct ThreadData {
105  AVFrame *in, *out;
106 } ThreadData;
107 
108 static void filter16_prewitt(uint8_t *dstp, int width,
109  float scale, float delta, const int *const matrix,
110  const uint8_t *c[], int peak, int radius,
111  int dstride, int stride, int size)
112 {
113  uint16_t *dst = (uint16_t *)dstp;
114  int x;
115 
116  for (x = 0; x < width; x++) {
117  float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
118  AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + AV_RN16A(&c[8][2 * x]) * 1;
119  float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1 +
120  AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
121 
122  dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
123  }
124 }
125 
126 static void filter16_roberts(uint8_t *dstp, int width,
127  float scale, float delta, const int *const matrix,
128  const uint8_t *c[], int peak, int radius,
129  int dstride, int stride, int size)
130 {
131  uint16_t *dst = (uint16_t *)dstp;
132  int x;
133 
134  for (x = 0; x < width; x++) {
135  float suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
136  float sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
137 
138  dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
139  }
140 }
141 
142 static void filter16_sobel(uint8_t *dstp, int width,
143  float scale, float delta, const int *const matrix,
144  const uint8_t *c[], int peak, int radius,
145  int dstride, int stride, int size)
146 {
147  uint16_t *dst = (uint16_t *)dstp;
148  int x;
149 
150  for (x = 0; x < width; x++) {
151  float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
152  AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
153  float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
154  AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
155 
156  dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
157  }
158 }
159 
160 static void filter16_scharr(uint8_t *dstp, int width,
161  float scale, float delta, const int *const matrix,
162  const uint8_t *c[], int peak, int radius,
163  int dstride, int stride, int size)
164 {
165  uint16_t *dst = (uint16_t *)dstp;
166  int x;
167 
168  for (x = 0; x < width; x++) {
169  float suma = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[1][2 * x]) * -162 + AV_RN16A(&c[2][2 * x]) * -47 +
170  AV_RN16A(&c[6][2 * x]) * 47 + AV_RN16A(&c[7][2 * x]) * 162 + AV_RN16A(&c[8][2 * x]) * 47;
171  float sumb = AV_RN16A(&c[0][2 * x]) * -47 + AV_RN16A(&c[2][2 * x]) * 47 + AV_RN16A(&c[3][2 * x]) * -162 +
172  AV_RN16A(&c[5][2 * x]) * 162 + AV_RN16A(&c[6][2 * x]) * -47 + AV_RN16A(&c[8][2 * x]) * 47;
173 
174  suma /= 256.f;
175  sumb /= 256.f;
176  dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
177  }
178 }
179 
180 static void filter16_kirsch(uint8_t *dstp, int width,
181  float scale, float delta, const int *const matrix,
182  const uint8_t *c[], int peak, int radius,
183  int dstride, int stride, int size)
184 {
185  uint16_t *dst = (uint16_t *)dstp;
186  const uint16_t *c0 = (const uint16_t *)c[0], *c1 = (const uint16_t *)c[1], *c2 = (const uint16_t *)c[2];
187  const uint16_t *c3 = (const uint16_t *)c[3], *c5 = (const uint16_t *)c[5];
188  const uint16_t *c6 = (const uint16_t *)c[6], *c7 = (const uint16_t *)c[7], *c8 = (const uint16_t *)c[8];
189  int x;
190 
191  for (x = 0; x < width; x++) {
192  int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
193  c3[x] * -3 + c5[x] * -3 +
194  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
195  int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
196  c3[x] * 5 + c5[x] * -3 +
197  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
198  int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
199  c3[x] * 5 + c5[x] * 5 +
200  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
201  int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
202  c3[x] * 5 + c5[x] * 5 +
203  c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
204  int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
205  c3[x] * -3 + c5[x] * 5 +
206  c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
207  int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
208  c3[x] * -3 + c5[x] * -3 +
209  c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
210  int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
211  c3[x] * -3 + c5[x] * -3 +
212  c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
213  int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
214  c3[x] * -3 + c5[x] * -3 +
215  c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
216 
217  sum0 = FFMAX(sum0, sum1);
218  sum2 = FFMAX(sum2, sum3);
219  sum4 = FFMAX(sum4, sum5);
220  sum6 = FFMAX(sum6, sum7);
221  sum0 = FFMAX(sum0, sum2);
222  sum4 = FFMAX(sum4, sum6);
223  sum0 = FFMAX(sum0, sum4);
224 
225  dst[x] = av_clip(FFABS(sum0) * scale + delta, 0, peak);
226  }
227 }
228 
229 static void filter_prewitt(uint8_t *dst, int width,
230  float scale, float delta, const int *const matrix,
231  const uint8_t *c[], int peak, int radius,
232  int dstride, int stride, int size)
233 {
234  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
235  const uint8_t *c3 = c[3], *c5 = c[5];
236  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
237  int x;
238 
239  for (x = 0; x < width; x++) {
240  float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
241  c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
242  float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
243  c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
244 
245  dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
246  }
247 }
248 
249 static void filter_roberts(uint8_t *dst, int width,
250  float scale, float delta, const int *const matrix,
251  const uint8_t *c[], int peak, int radius,
252  int dstride, int stride, int size)
253 {
254  int x;
255 
256  for (x = 0; x < width; x++) {
257  float suma = c[0][x] * 1 + c[1][x] * -1;
258  float sumb = c[4][x] * 1 + c[3][x] * -1;
259 
260  dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
261  }
262 }
263 
264 static void filter_sobel(uint8_t *dst, int width,
265  float scale, float delta, const int *const matrix,
266  const uint8_t *c[], int peak, int radius,
267  int dstride, int stride, int size)
268 {
269  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
270  const uint8_t *c3 = c[3], *c5 = c[5];
271  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
272  int x;
273 
274  for (x = 0; x < width; x++) {
275  float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
276  c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
277  float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
278  c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
279 
280  dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
281  }
282 }
283 
284 static void filter_scharr(uint8_t *dst, int width,
285  float scale, float delta, const int *const matrix,
286  const uint8_t *c[], int peak, int radius,
287  int dstride, int stride, int size)
288 {
289  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
290  const uint8_t *c3 = c[3], *c5 = c[5];
291  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
292  int x;
293 
294  for (x = 0; x < width; x++) {
295  float suma = c0[x] * -47 + c1[x] * -162 + c2[x] * -47 +
296  c6[x] * 47 + c7[x] * 162 + c8[x] * 47;
297  float sumb = c0[x] * -47 + c2[x] * 47 + c3[x] * -162 +
298  c5[x] * 162 + c6[x] * -47 + c8[x] * 47;
299 
300  suma /= 256.f;
301  sumb /= 256.f;
302  dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
303  }
304 }
305 
306 static void filter_kirsch(uint8_t *dst, int width,
307  float scale, float delta, const int *const matrix,
308  const uint8_t *c[], int peak, int radius,
309  int dstride, int stride, int size)
310 {
311  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
312  const uint8_t *c3 = c[3], *c5 = c[5];
313  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
314  int x;
315 
316  for (x = 0; x < width; x++) {
317  int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
318  c3[x] * -3 + c5[x] * -3 +
319  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
320  int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
321  c3[x] * 5 + c5[x] * -3 +
322  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
323  int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
324  c3[x] * 5 + c5[x] * 5 +
325  c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
326  int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
327  c3[x] * 5 + c5[x] * 5 +
328  c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
329  int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
330  c3[x] * -3 + c5[x] * 5 +
331  c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
332  int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
333  c3[x] * -3 + c5[x] * -3 +
334  c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
335  int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
336  c3[x] * -3 + c5[x] * -3 +
337  c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
338  int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
339  c3[x] * -3 + c5[x] * -3 +
340  c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
341 
342  sum0 = FFMAX(sum0, sum1);
343  sum2 = FFMAX(sum2, sum3);
344  sum4 = FFMAX(sum4, sum5);
345  sum6 = FFMAX(sum6, sum7);
346  sum0 = FFMAX(sum0, sum2);
347  sum4 = FFMAX(sum4, sum6);
348  sum0 = FFMAX(sum0, sum4);
349 
350  dst[x] = av_clip_uint8(FFABS(sum0) * scale + delta);
351  }
352 }
353 
354 static void filter16_3x3(uint8_t *dstp, int width,
355  float rdiv, float bias, const int *const matrix,
356  const uint8_t *c[], int peak, int radius,
357  int dstride, int stride, int size)
358 {
359  uint16_t *dst = (uint16_t *)dstp;
360  int x;
361 
362  for (x = 0; x < width; x++) {
363  int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
364  AV_RN16A(&c[1][2 * x]) * matrix[1] +
365  AV_RN16A(&c[2][2 * x]) * matrix[2] +
366  AV_RN16A(&c[3][2 * x]) * matrix[3] +
367  AV_RN16A(&c[4][2 * x]) * matrix[4] +
368  AV_RN16A(&c[5][2 * x]) * matrix[5] +
369  AV_RN16A(&c[6][2 * x]) * matrix[6] +
370  AV_RN16A(&c[7][2 * x]) * matrix[7] +
371  AV_RN16A(&c[8][2 * x]) * matrix[8];
372  sum = (int)(sum * rdiv + bias + 0.5f);
373  dst[x] = av_clip(sum, 0, peak);
374  }
375 }
376 
377 static void filter16_5x5(uint8_t *dstp, int width,
378  float rdiv, float bias, const int *const matrix,
379  const uint8_t *c[], int peak, int radius,
380  int dstride, int stride, int size)
381 {
382  uint16_t *dst = (uint16_t *)dstp;
383  int x;
384 
385  for (x = 0; x < width; x++) {
386  int i, sum = 0;
387 
388  for (i = 0; i < 25; i++)
389  sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
390 
391  sum = (int)(sum * rdiv + bias + 0.5f);
392  dst[x] = av_clip(sum, 0, peak);
393  }
394 }
395 
396 static void filter16_7x7(uint8_t *dstp, int width,
397  float rdiv, float bias, const int *const matrix,
398  const uint8_t *c[], int peak, int radius,
399  int dstride, int stride, int size)
400 {
401  uint16_t *dst = (uint16_t *)dstp;
402  int x;
403 
404  for (x = 0; x < width; x++) {
405  int i, sum = 0;
406 
407  for (i = 0; i < 49; i++)
408  sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
409 
410  sum = (int)(sum * rdiv + bias + 0.5f);
411  dst[x] = av_clip(sum, 0, peak);
412  }
413 }
414 
415 static void filter16_row(uint8_t *dstp, int width,
416  float rdiv, float bias, const int *const matrix,
417  const uint8_t *c[], int peak, int radius,
418  int dstride, int stride, int size)
419 {
420  uint16_t *dst = (uint16_t *)dstp;
421  int x;
422 
423  for (x = 0; x < width; x++) {
424  int i, sum = 0;
425 
426  for (i = 0; i < 2 * radius + 1; i++)
427  sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
428 
429  sum = (int)(sum * rdiv + bias + 0.5f);
430  dst[x] = av_clip(sum, 0, peak);
431  }
432 }
433 
434 static void filter16_column(uint8_t *dstp, int height,
435  float rdiv, float bias, const int *const matrix,
436  const uint8_t *c[], int peak, int radius,
437  int dstride, int stride, int size)
438 {
439  DECLARE_ALIGNED(64, int, sum)[16];
440  uint16_t *dst = (uint16_t *)dstp;
441  const int width = FFMIN(16, size);
442 
443  for (int y = 0; y < height; y++) {
444 
445  memset(sum, 0, sizeof(sum));
446  for (int i = 0; i < 2 * radius + 1; i++) {
447  for (int off16 = 0; off16 < width; off16++)
448  sum[off16] += AV_RN16A(&c[i][0 + y * stride + off16 * 2]) * matrix[i];
449  }
450 
451  for (int off16 = 0; off16 < width; off16++) {
452  sum[off16] = (int)(sum[off16] * rdiv + bias + 0.5f);
453  dst[off16] = av_clip(sum[off16], 0, peak);
454  }
455  dst += dstride / 2;
456  }
457 }
458 
459 static void filter_7x7(uint8_t *dst, int width,
460  float rdiv, float bias, const int *const matrix,
461  const uint8_t *c[], int peak, int radius,
462  int dstride, int stride, int size)
463 {
464  int x;
465 
466  for (x = 0; x < width; x++) {
467  int i, sum = 0;
468 
469  for (i = 0; i < 49; i++)
470  sum += c[i][x] * matrix[i];
471 
472  sum = (int)(sum * rdiv + bias + 0.5f);
473  dst[x] = av_clip_uint8(sum);
474  }
475 }
476 
477 static void filter_5x5(uint8_t *dst, int width,
478  float rdiv, float bias, const int *const matrix,
479  const uint8_t *c[], int peak, int radius,
480  int dstride, int stride, int size)
481 {
482  int x;
483 
484  for (x = 0; x < width; x++) {
485  int i, sum = 0;
486 
487  for (i = 0; i < 25; i++)
488  sum += c[i][x] * matrix[i];
489 
490  sum = (int)(sum * rdiv + bias + 0.5f);
491  dst[x] = av_clip_uint8(sum);
492  }
493 }
494 
495 static void filter_3x3(uint8_t *dst, int width,
496  float rdiv, float bias, const int *const matrix,
497  const uint8_t *c[], int peak, int radius,
498  int dstride, int stride, int size)
499 {
500  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
501  const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
502  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
503  int x;
504 
505  for (x = 0; x < width; x++) {
506  int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
507  c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
508  c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
509  sum = (int)(sum * rdiv + bias + 0.5f);
510  dst[x] = av_clip_uint8(sum);
511  }
512 }
513 
514 static void filter_row(uint8_t *dst, int width,
515  float rdiv, float bias, const int *const matrix,
516  const uint8_t *c[], int peak, int radius,
517  int dstride, int stride, int size)
518 {
519  int x;
520 
521  for (x = 0; x < width; x++) {
522  int i, sum = 0;
523 
524  for (i = 0; i < 2 * radius + 1; i++)
525  sum += c[i][x] * matrix[i];
526 
527  sum = (int)(sum * rdiv + bias + 0.5f);
528  dst[x] = av_clip_uint8(sum);
529  }
530 }
531 
532 static void filter_column(uint8_t *dst, int height,
533  float rdiv, float bias, const int *const matrix,
534  const uint8_t *c[], int peak, int radius,
535  int dstride, int stride, int size)
536 {
537  DECLARE_ALIGNED(64, int, sum)[16];
538 
539  for (int y = 0; y < height; y++) {
540  memset(sum, 0, sizeof(sum));
541 
542  for (int i = 0; i < 2 * radius + 1; i++) {
543  for (int off16 = 0; off16 < 16; off16++)
544  sum[off16] += c[i][0 + y * stride + off16] * matrix[i];
545  }
546 
547  for (int off16 = 0; off16 < 16; off16++) {
548  sum[off16] = (int)(sum[off16] * rdiv + bias + 0.5f);
549  dst[off16] = av_clip_uint8(sum[off16]);
550  }
551  dst += dstride;
552  }
553 }
554 
555 static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
556  int x, int w, int y, int h, int bpc)
557 {
558  int i;
559 
560  for (i = 0; i < 9; i++) {
561  int xoff = FFABS(x + ((i % 3) - 1));
562  int yoff = FFABS(y + (i / 3) - 1);
563 
564  xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
565  yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
566 
567  c[i] = src + xoff * bpc + yoff * stride;
568  }
569 }
570 
571 static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
572  int x, int w, int y, int h, int bpc)
573 {
574  int i;
575 
576  for (i = 0; i < 25; i++) {
577  int xoff = FFABS(x + ((i % 5) - 2));
578  int yoff = FFABS(y + (i / 5) - 2);
579 
580  xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
581  yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
582 
583  c[i] = src + xoff * bpc + yoff * stride;
584  }
585 }
586 
587 static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
588  int x, int w, int y, int h, int bpc)
589 {
590  int i;
591 
592  for (i = 0; i < 49; i++) {
593  int xoff = FFABS(x + ((i % 7) - 3));
594  int yoff = FFABS(y + (i / 7) - 3);
595 
596  xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
597  yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
598 
599  c[i] = src + xoff * bpc + yoff * stride;
600  }
601 }
602 
603 static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
604  int x, int w, int y, int h, int bpc)
605 {
606  int i;
607 
608  for (i = 0; i < radius * 2 + 1; i++) {
609  int xoff = FFABS(x + i - radius);
610 
611  xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
612 
613  c[i] = src + xoff * bpc + y * stride;
614  }
615 }
616 
617 static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
618  int x, int w, int y, int h, int bpc)
619 {
620  int i;
621 
622  for (i = 0; i < radius * 2 + 1; i++) {
623  int xoff = FFABS(x + i - radius);
624 
625  xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
626 
627  c[i] = src + y * bpc + xoff * stride;
628  }
629 }
630 
631 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
632 {
633  ConvolutionContext *s = ctx->priv;
634  ThreadData *td = arg;
635  AVFrame *in = td->in;
636  AVFrame *out = td->out;
637  int plane;
638 
639  for (plane = 0; plane < s->nb_planes; plane++) {
640  const int mode = s->mode[plane];
641  const int bpc = s->bpc;
642  const int radius = s->size[plane] / 2;
643  const int height = s->planeheight[plane];
644  const int width = s->planewidth[plane];
645  const int stride = in->linesize[plane];
646  const int dstride = out->linesize[plane];
647  const int sizeh = mode == MATRIX_COLUMN ? width : height;
648  const int sizew = mode == MATRIX_COLUMN ? height : width;
649  const int slice_start = (sizeh * jobnr) / nb_jobs;
650  const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
651  const float rdiv = s->rdiv[plane];
652  const float bias = s->bias[plane];
653  const uint8_t *src = in->data[plane];
654  const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
655  uint8_t *dst = out->data[plane] + dst_pos;
656  const int *matrix = s->matrix[plane];
657  const int step = mode == MATRIX_COLUMN ? 16 : 1;
658  const uint8_t *c[49];
659  int y, x;
660 
661  if (s->copy[plane]) {
662  if (mode == MATRIX_COLUMN)
663  av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
664  (slice_end - slice_start) * bpc, height);
665  else
666  av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
667  width * bpc, slice_end - slice_start);
668  continue;
669  }
670  for (y = slice_start; y < slice_end; y += step) {
671  const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
672  const int yoff = mode == MATRIX_COLUMN ? radius * dstride : 0;
673 
674  for (x = 0; x < radius; x++) {
675  const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
676  const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
677 
678  s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
679  s->filter[plane](dst + yoff + xoff, 1, rdiv,
680  bias, matrix, c, s->max, radius,
681  dstride, stride, slice_end - step);
682  }
683  s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
684  s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
685  rdiv, bias, matrix, c, s->max, radius,
686  dstride, stride, slice_end - step);
687  for (x = sizew - radius; x < sizew; x++) {
688  const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
689  const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
690 
691  s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
692  s->filter[plane](dst + yoff + xoff, 1, rdiv,
693  bias, matrix, c, s->max, radius,
694  dstride, stride, slice_end - step);
695  }
696  if (mode != MATRIX_COLUMN)
697  dst += dstride;
698  }
699  }
700 
701  return 0;
702 }
703 
705 {
706  ConvolutionContext *s = ctx->priv;
707  AVFilterLink *inlink = ctx->inputs[0];
709  int p, i;
710 
711  if (!strcmp(ctx->filter->name, "convolution")) {
712  for (i = 0; i < 4; i++) {
713  int *matrix = (int *)s->matrix[i];
714  char *orig, *p, *arg, *saveptr = NULL;
715  float sum = 1.f;
716 
717  p = orig = av_strdup(s->matrix_str[i]);
718  if (p) {
719  s->matrix_length[i] = 0;
720  s->rdiv[i] = 0.f;
721  sum = 0.f;
722 
723  while (s->matrix_length[i] < 49) {
724  if (!(arg = av_strtok(p, " |", &saveptr)))
725  break;
726 
727  p = NULL;
728  sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
729  sum += matrix[s->matrix_length[i]];
730  s->matrix_length[i]++;
731  }
732 
733  av_freep(&orig);
734  if (!(s->matrix_length[i] & 1)) {
735  av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
736  return AVERROR(EINVAL);
737  }
738  }
739 
740  if (s->mode[i] == MATRIX_ROW) {
741  s->filter[i] = filter_row;
742  s->setup[i] = setup_row;
743  s->size[i] = s->matrix_length[i];
744  } else if (s->mode[i] == MATRIX_COLUMN) {
745  s->filter[i] = filter_column;
746  s->setup[i] = setup_column;
747  s->size[i] = s->matrix_length[i];
748  } else if (s->matrix_length[i] == 9) {
749  s->size[i] = 3;
750 
751  if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
752  s->copy[i] = 1;
753  } else {
754  s->filter[i] = filter_3x3;
755  s->copy[i] = 0;
756  }
757  s->setup[i] = setup_3x3;
758  } else if (s->matrix_length[i] == 25) {
759  s->size[i] = 5;
760  if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
761  s->copy[i] = 1;
762  } else {
763  s->filter[i] = filter_5x5;
764  s->copy[i] = 0;
765  }
766  s->setup[i] = setup_5x5;
767  } else if (s->matrix_length[i] == 49) {
768  s->size[i] = 7;
769  if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
770  s->copy[i] = 1;
771  } else {
772  s->filter[i] = filter_7x7;
773  s->copy[i] = 0;
774  }
775  s->setup[i] = setup_7x7;
776  } else {
777  return AVERROR(EINVAL);
778  }
779 
780  if (sum == 0)
781  sum = 1;
782  if (s->rdiv[i] == 0)
783  s->rdiv[i] = 1. / sum;
784 
785  if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
786  s->copy[i] = 0;
787  }
788  } else if (!strcmp(ctx->filter->name, "prewitt")) {
789  for (i = 0; i < 4; i++) {
790  s->filter[i] = filter_prewitt;
791  s->copy[i] = !((1 << i) & s->planes);
792  s->size[i] = 3;
793  s->setup[i] = setup_3x3;
794  s->rdiv[i] = s->scale;
795  s->bias[i] = s->delta;
796  }
797  } else if (!strcmp(ctx->filter->name, "roberts")) {
798  for (i = 0; i < 4; i++) {
799  s->filter[i] = filter_roberts;
800  s->copy[i] = !((1 << i) & s->planes);
801  s->size[i] = 3;
802  s->setup[i] = setup_3x3;
803  s->rdiv[i] = s->scale;
804  s->bias[i] = s->delta;
805  }
806  } else if (!strcmp(ctx->filter->name, "sobel")) {
807  for (i = 0; i < 4; i++) {
808  s->filter[i] = filter_sobel;
809  s->copy[i] = !((1 << i) & s->planes);
810  s->size[i] = 3;
811  s->setup[i] = setup_3x3;
812  s->rdiv[i] = s->scale;
813  s->bias[i] = s->delta;
814  }
815  } else if (!strcmp(ctx->filter->name, "kirsch")) {
816  for (i = 0; i < 4; i++) {
817  s->filter[i] = filter_kirsch;
818  s->copy[i] = !((1 << i) & s->planes);
819  s->size[i] = 3;
820  s->setup[i] = setup_3x3;
821  s->rdiv[i] = s->scale;
822  s->bias[i] = s->delta;
823  }
824  } else if (!strcmp(ctx->filter->name, "scharr")) {
825  for (i = 0; i < 4; i++) {
826  s->filter[i] = filter_scharr;
827  s->copy[i] = !((1 << i) & s->planes);
828  s->size[i] = 3;
829  s->setup[i] = setup_3x3;
830  s->rdiv[i] = s->scale;
831  s->bias[i] = s->delta;
832  }
833  }
834 
835  s->depth = desc->comp[0].depth;
836  s->max = (1 << s->depth) - 1;
837 
838  s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
839  s->planewidth[0] = s->planewidth[3] = inlink->w;
840  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
841  s->planeheight[0] = s->planeheight[3] = inlink->h;
842 
843  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
844  s->nb_threads = ff_filter_get_nb_threads(ctx);
845  s->bpc = (s->depth + 7) / 8;
846 
847  if (!strcmp(ctx->filter->name, "convolution")) {
848  if (s->depth > 8) {
849  for (p = 0; p < s->nb_planes; p++) {
850  if (s->mode[p] == MATRIX_ROW)
851  s->filter[p] = filter16_row;
852  else if (s->mode[p] == MATRIX_COLUMN)
853  s->filter[p] = filter16_column;
854  else if (s->size[p] == 3)
855  s->filter[p] = filter16_3x3;
856  else if (s->size[p] == 5)
857  s->filter[p] = filter16_5x5;
858  else if (s->size[p] == 7)
859  s->filter[p] = filter16_7x7;
860  }
861  }
862 #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
864 #endif
865  } else if (!strcmp(ctx->filter->name, "prewitt")) {
866  if (s->depth > 8)
867  for (p = 0; p < s->nb_planes; p++)
868  s->filter[p] = filter16_prewitt;
869  } else if (!strcmp(ctx->filter->name, "roberts")) {
870  if (s->depth > 8)
871  for (p = 0; p < s->nb_planes; p++)
872  s->filter[p] = filter16_roberts;
873  } else if (!strcmp(ctx->filter->name, "sobel")) {
874  if (s->depth > 8)
875  for (p = 0; p < s->nb_planes; p++)
876  s->filter[p] = filter16_sobel;
877  } else if (!strcmp(ctx->filter->name, "kirsch")) {
878  if (s->depth > 8)
879  for (p = 0; p < s->nb_planes; p++)
880  s->filter[p] = filter16_kirsch;
881  } else if (!strcmp(ctx->filter->name, "scharr")) {
882  if (s->depth > 8)
883  for (p = 0; p < s->nb_planes; p++)
884  s->filter[p] = filter16_scharr;
885  }
886 
887  return 0;
888 }
889 
891 {
892  AVFilterContext *ctx = inlink->dst;
893  return param_init(ctx);
894 }
895 
897 {
898  AVFilterContext *ctx = inlink->dst;
899  ConvolutionContext *s = ctx->priv;
900  AVFilterLink *outlink = ctx->outputs[0];
901  AVFrame *out;
902  ThreadData td;
903 
904  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
905  if (!out) {
906  av_frame_free(&in);
907  return AVERROR(ENOMEM);
908  }
910 
911  td.in = in;
912  td.out = out;
914  FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
915 
916  av_frame_free(&in);
917  return ff_filter_frame(outlink, out);
918 }
919 
920 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
921  char *res, int res_len, int flags)
922 {
923  int ret;
924 
925  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
926  if (ret < 0)
927  return ret;
928 
929  return param_init(ctx);
930 }
931 
932 static const AVFilterPad convolution_inputs[] = {
933  {
934  .name = "default",
935  .type = AVMEDIA_TYPE_VIDEO,
936  .config_props = config_input,
937  .filter_frame = filter_frame,
938  },
939 };
940 
942  {
943  .name = "default",
944  .type = AVMEDIA_TYPE_VIDEO,
945  },
946 };
947 
948 #if CONFIG_CONVOLUTION_FILTER
949 
950 const AVFilter ff_vf_convolution = {
951  .name = "convolution",
952  .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
953  .priv_size = sizeof(ConvolutionContext),
954  .priv_class = &convolution_class,
959  .process_command = process_command,
960 };
961 
962 #endif /* CONFIG_CONVOLUTION_FILTER */
963 
964 static const AVOption common_options[] = {
965  { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
966  { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
967  { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
968  { NULL }
969 };
970 
971 AVFILTER_DEFINE_CLASS_EXT(common, "kirsch/prewitt/roberts/scharr/sobel",
973 
974 #if CONFIG_PREWITT_FILTER
975 
976 const AVFilter ff_vf_prewitt = {
977  .name = "prewitt",
978  .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
979  .priv_size = sizeof(ConvolutionContext),
980  .priv_class = &common_class,
985  .process_command = process_command,
986 };
987 
988 #endif /* CONFIG_PREWITT_FILTER */
989 
990 #if CONFIG_SOBEL_FILTER
991 
992 const AVFilter ff_vf_sobel = {
993  .name = "sobel",
994  .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
995  .priv_size = sizeof(ConvolutionContext),
996  .priv_class = &common_class,
1001  .process_command = process_command,
1002 };
1003 
1004 #endif /* CONFIG_SOBEL_FILTER */
1005 
1006 #if CONFIG_ROBERTS_FILTER
1007 
1008 const AVFilter ff_vf_roberts = {
1009  .name = "roberts",
1010  .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
1011  .priv_size = sizeof(ConvolutionContext),
1012  .priv_class = &common_class,
1017  .process_command = process_command,
1018 };
1019 
1020 #endif /* CONFIG_ROBERTS_FILTER */
1021 
1022 #if CONFIG_KIRSCH_FILTER
1023 
1024 const AVFilter ff_vf_kirsch = {
1025  .name = "kirsch",
1026  .description = NULL_IF_CONFIG_SMALL("Apply kirsch operator."),
1027  .priv_size = sizeof(ConvolutionContext),
1028  .priv_class = &common_class,
1033  .process_command = process_command,
1034 };
1035 
1036 #endif /* CONFIG_KIRSCH_FILTER */
1037 
1038 #if CONFIG_SCHARR_FILTER
1039 
1040 const AVFilter ff_vf_scharr = {
1041  .name = "scharr",
1042  .description = NULL_IF_CONFIG_SMALL("Apply scharr operator."),
1043  .priv_size = sizeof(ConvolutionContext),
1044  .priv_class = &common_class,
1049  .process_command = process_command,
1050 };
1051 
1052 #endif /* CONFIG_SCHARR_FILTER */
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:451
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:430
td
#define td
Definition: regdef.h:70
common_options
static const AVOption common_options[]
Definition: vf_convolution.c:964
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
filter16_row
static void filter16_row(uint8_t *dstp, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:415
setup_5x5
static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: vf_convolution.c:571
av_clip
#define av_clip
Definition: common.h:95
same5x5
static const int same5x5[25]
Definition: vf_convolution.c:68
filter16_7x7
static void filter16_7x7(uint8_t *dstp, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:396
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
planes
static const struct @329 planes[]
opt.h
mem_internal.h
filter_3x3
static void filter_3x3(uint8_t *dst, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:495
filter16_roberts
static void filter16_roberts(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:126
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2675
ff_vf_scharr
const AVFilter ff_vf_scharr
matrix
Definition: vc1dsp.c:42
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:170
convolution_inputs
static const AVFilterPad convolution_inputs[]
Definition: vf_convolution.c:932
ff_vf_roberts
const AVFilter ff_vf_roberts
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
OFFSET
#define OFFSET(x)
Definition: vf_convolution.c:36
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:116
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:443
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:450
w
uint8_t w
Definition: llviddspenc.c:38
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:445
ff_vf_kirsch
const AVFilter ff_vf_kirsch
AVOption
AVOption.
Definition: opt.h:251
filter16_prewitt
static void filter16_prewitt(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:108
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:408
ff_vf_prewitt
const AVFilter ff_vf_prewitt
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
c1
static const uint64_t c1
Definition: murmur3.c:51
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
filter_column
static void filter_column(uint8_t *dst, int height, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:532
convolution.h
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:446
filter_sobel
static void filter_sobel(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:264
setup_row
static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: vf_convolution.c:603
AV_PIX_FMT_GRAY9
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:388
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2715
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:442
AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:426
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
filter_scharr
static void filter_scharr(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:284
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:424
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:452
setup_column
static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: vf_convolution.c:617
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:406
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_convolution.c:896
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:392
filter16_3x3
static void filter16_3x3(uint8_t *dstp, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:354
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:411
AV_PIX_FMT_YUVJ411P
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:248
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_convolution.c:920
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:420
MATRIX_NBMODES
@ MATRIX_NBMODES
Definition: convolution.h:29
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:428
filter16_column
static void filter16_column(uint8_t *dstp, int height, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:434
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
MATRIX_SQUARE
@ MATRIX_SQUARE
Definition: convolution.h:26
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:429
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:421
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:50
filter_prewitt
static void filter_prewitt(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:229
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2013
av_strtok
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:189
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(convolution)
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:449
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:405
filter16_5x5
static void filter16_5x5(uint8_t *dstp, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:377
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:419
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:391
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
arg
const char * arg
Definition: jacosubdec.c:67
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:389
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:427
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:603
MATRIX_ROW
@ MATRIX_ROW
Definition: convolution.h:27
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
sqrtf
static __device__ float sqrtf(float a)
Definition: cuda_runtime.h:184
ConvolutionContext
Definition: convolution.h:32
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:409
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_PIX_FMT_GBRP9
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:423
param_init
static int param_init(AVFilterContext *ctx)
Definition: vf_convolution.c:704
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_vf_convolution
const AVFilter ff_vf_convolution
filter_5x5
static void filter_5x5(uint8_t *dst, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:477
f
f
Definition: af_crystalizer.c:122
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
filter_roberts
static void filter_roberts(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:249
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_convolution.c:82
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:413
size
int size
Definition: twinvq_data.h:10344
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:415
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:863
height
#define height
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:447
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:152
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:116
filter_slice
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_convolution.c:631
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
FFMIN3
#define FFMIN3(a, b, c)
Definition: macros.h:50
setup_7x7
static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: vf_convolution.c:587
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:425
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:783
delta
float delta
Definition: vorbis_enc_data.h:430
ThreadData
Used for passing data between threads.
Definition: dsddec.c:68
filter_7x7
static void filter_7x7(uint8_t *dst, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:459
setup_3x3
static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: vf_convolution.c:555
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_PIX_FMT_YUVJ440P
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:407
convolution_options
static const AVOption convolution_options[]
Definition: vf_convolution.c:39
stride
#define stride
Definition: h264pred_template.c:537
AVFilter
Filter definition.
Definition: avfilter.h:171
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:444
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:412
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:417
convolution_outputs
static const AVFilterPad convolution_outputs[]
Definition: vf_convolution.c:941
c2
static const uint64_t c2
Definition: murmur3.c:52
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:448
filter_kirsch
static void filter_kirsch(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:306
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AV_RN16A
#define AV_RN16A(p)
Definition: intreadwrite.h:522
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(common, "kirsch/prewitt/roberts/scharr/sobel", common_options)
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
MATRIX_COLUMN
@ MATRIX_COLUMN
Definition: convolution.h:28
ff_vf_sobel
const AVFilter ff_vf_sobel
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:127
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:280
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
filter_row
static void filter_row(uint8_t *dst, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:514
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
ff_convolution_init_x86
void ff_convolution_init_x86(ConvolutionContext *s)
Definition: vf_convolution_init.c:32
filter16_kirsch
static void filter16_kirsch(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:180
same3x3
static const int same3x3[9]
Definition: vf_convolution.c:64
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_convolution.c:890
filter16_sobel
static void filter16_sobel(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:142
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
same7x7
static const int same7x7[49]
Definition: vf_convolution.c:74
AV_PIX_FMT_YUV440P12
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:414
h
h
Definition: vp9dsp_template.c:2038
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:418
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:390
FLAGS
#define FLAGS
Definition: vf_convolution.c:37
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:142
int
int
Definition: ffmpeg_filter.c:155
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
filter16_scharr
static void filter16_scharr(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: vf_convolution.c:160
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:416