You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1001 lines
36KB

  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. #include "libavutil/avstring.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "convolution.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #define OFFSET(x) offsetof(ConvolutionContext, x)
  32. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  33. static const AVOption convolution_options[] = {
  34. { "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 },
  35. { "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 },
  36. { "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 },
  37. { "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 },
  38. { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  39. { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  40. { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  41. { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  42. { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  43. { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  44. { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  45. { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  46. { "0mode", "set matrix mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  47. { "1mode", "set matrix mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  48. { "2mode", "set matrix mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  49. { "3mode", "set matrix mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  50. { "square", "square matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_SQUARE}, 0, 0, FLAGS, "mode" },
  51. { "row", "single row matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_ROW} , 0, 0, FLAGS, "mode" },
  52. { "column", "single column matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_COLUMN}, 0, 0, FLAGS, "mode" },
  53. { NULL }
  54. };
  55. AVFILTER_DEFINE_CLASS(convolution);
  56. static const int same3x3[9] = {0, 0, 0,
  57. 0, 1, 0,
  58. 0, 0, 0};
  59. static const int same5x5[25] = {0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0,
  61. 0, 0, 1, 0, 0,
  62. 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0, 0};
  64. static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 1, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0,
  70. 0, 0, 0, 0, 0, 0, 0};
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. static const enum AVPixelFormat pix_fmts[] = {
  74. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  75. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  76. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  77. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  78. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  79. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  80. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  81. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  82. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  83. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  84. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  85. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  86. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  87. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  88. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  89. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  90. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  91. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  92. AV_PIX_FMT_NONE
  93. };
  94. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  95. }
  96. typedef struct ThreadData {
  97. AVFrame *in, *out;
  98. } ThreadData;
  99. static void filter16_prewitt(uint8_t *dstp, int width,
  100. float scale, float delta, const int *const matrix,
  101. const uint8_t *c[], int peak, int radius,
  102. int dstride, int stride)
  103. {
  104. uint16_t *dst = (uint16_t *)dstp;
  105. int x;
  106. for (x = 0; x < width; x++) {
  107. float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
  108. AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + AV_RN16A(&c[8][2 * x]) * 1;
  109. float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1 +
  110. AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
  111. dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  112. }
  113. }
  114. static void filter16_roberts(uint8_t *dstp, int width,
  115. float scale, float delta, const int *const matrix,
  116. const uint8_t *c[], int peak, int radius,
  117. int dstride, int stride)
  118. {
  119. uint16_t *dst = (uint16_t *)dstp;
  120. int x;
  121. for (x = 0; x < width; x++) {
  122. float suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
  123. float sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
  124. dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  125. }
  126. }
  127. static void filter16_sobel(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. for (x = 0; x < width; x++) {
  135. float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
  136. AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
  137. float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
  138. AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
  139. dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  140. }
  141. }
  142. static void filter16_kirsch(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)
  146. {
  147. uint16_t *dst = (uint16_t *)dstp;
  148. const uint16_t *c0 = (const uint16_t *)c[0], *c1 = (const uint16_t *)c[1], *c2 = (const uint16_t *)c[2];
  149. const uint16_t *c3 = (const uint16_t *)c[3], *c5 = (const uint16_t *)c[5];
  150. const uint16_t *c6 = (const uint16_t *)c[6], *c7 = (const uint16_t *)c[7], *c8 = (const uint16_t *)c[8];
  151. int x;
  152. for (x = 0; x < width; x++) {
  153. int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
  154. c3[x] * -3 + c5[x] * -3 +
  155. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  156. int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
  157. c3[x] * 5 + c5[x] * -3 +
  158. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  159. int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
  160. c3[x] * 5 + c5[x] * 5 +
  161. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  162. int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  163. c3[x] * 5 + c5[x] * 5 +
  164. c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
  165. int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  166. c3[x] * -3 + c5[x] * 5 +
  167. c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
  168. int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  169. c3[x] * -3 + c5[x] * -3 +
  170. c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
  171. int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
  172. c3[x] * -3 + c5[x] * -3 +
  173. c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
  174. int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
  175. c3[x] * -3 + c5[x] * -3 +
  176. c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
  177. sum0 = FFMAX(sum0, sum1);
  178. sum2 = FFMAX(sum2, sum3);
  179. sum4 = FFMAX(sum4, sum5);
  180. sum6 = FFMAX(sum6, sum7);
  181. sum0 = FFMAX(sum0, sum2);
  182. sum4 = FFMAX(sum4, sum6);
  183. sum0 = FFMAX(sum0, sum4);
  184. dst[x] = av_clip(FFABS(sum0) * scale + delta, 0, peak);
  185. }
  186. }
  187. static void filter_prewitt(uint8_t *dst, int width,
  188. float scale, float delta, const int *const matrix,
  189. const uint8_t *c[], int peak, int radius,
  190. int dstride, int stride)
  191. {
  192. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  193. const uint8_t *c3 = c[3], *c5 = c[5];
  194. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  195. int x;
  196. for (x = 0; x < width; x++) {
  197. float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
  198. c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
  199. float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
  200. c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
  201. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  202. }
  203. }
  204. static void filter_roberts(uint8_t *dst, int width,
  205. float scale, float delta, const int *const matrix,
  206. const uint8_t *c[], int peak, int radius,
  207. int dstride, int stride)
  208. {
  209. int x;
  210. for (x = 0; x < width; x++) {
  211. float suma = c[0][x] * 1 + c[1][x] * -1;
  212. float sumb = c[4][x] * 1 + c[3][x] * -1;
  213. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  214. }
  215. }
  216. static void filter_sobel(uint8_t *dst, int width,
  217. float scale, float delta, const int *const matrix,
  218. const uint8_t *c[], int peak, int radius,
  219. int dstride, int stride)
  220. {
  221. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  222. const uint8_t *c3 = c[3], *c5 = c[5];
  223. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  224. int x;
  225. for (x = 0; x < width; x++) {
  226. float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
  227. c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
  228. float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
  229. c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
  230. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  231. }
  232. }
  233. static void filter_kirsch(uint8_t *dst, int width,
  234. float scale, float delta, const int *const matrix,
  235. const uint8_t *c[], int peak, int radius,
  236. int dstride, int stride)
  237. {
  238. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  239. const uint8_t *c3 = c[3], *c5 = c[5];
  240. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  241. int x;
  242. for (x = 0; x < width; x++) {
  243. int sum0 = c0[x] * 5 + c1[x] * 5 + c2[x] * 5 +
  244. c3[x] * -3 + c5[x] * -3 +
  245. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  246. int sum1 = c0[x] * -3 + c1[x] * 5 + c2[x] * 5 +
  247. c3[x] * 5 + c5[x] * -3 +
  248. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  249. int sum2 = c0[x] * -3 + c1[x] * -3 + c2[x] * 5 +
  250. c3[x] * 5 + c5[x] * 5 +
  251. c6[x] * -3 + c7[x] * -3 + c8[x] * -3;
  252. int sum3 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  253. c3[x] * 5 + c5[x] * 5 +
  254. c6[x] * 5 + c7[x] * -3 + c8[x] * -3;
  255. int sum4 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  256. c3[x] * -3 + c5[x] * 5 +
  257. c6[x] * 5 + c7[x] * 5 + c8[x] * -3;
  258. int sum5 = c0[x] * -3 + c1[x] * -3 + c2[x] * -3 +
  259. c3[x] * -3 + c5[x] * -3 +
  260. c6[x] * 5 + c7[x] * 5 + c8[x] * 5;
  261. int sum6 = c0[x] * 5 + c1[x] * -3 + c2[x] * -3 +
  262. c3[x] * -3 + c5[x] * -3 +
  263. c6[x] * -3 + c7[x] * 5 + c8[x] * 5;
  264. int sum7 = c0[x] * 5 + c1[x] * 5 + c2[x] * -3 +
  265. c3[x] * -3 + c5[x] * -3 +
  266. c6[x] * -3 + c7[x] * -3 + c8[x] * 5;
  267. sum0 = FFMAX(sum0, sum1);
  268. sum2 = FFMAX(sum2, sum3);
  269. sum4 = FFMAX(sum4, sum5);
  270. sum6 = FFMAX(sum6, sum7);
  271. sum0 = FFMAX(sum0, sum2);
  272. sum4 = FFMAX(sum4, sum6);
  273. sum0 = FFMAX(sum0, sum4);
  274. dst[x] = av_clip_uint8(FFABS(sum0) * scale + delta);
  275. }
  276. }
  277. static void filter16_3x3(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. for (x = 0; x < width; x++) {
  285. int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
  286. AV_RN16A(&c[1][2 * x]) * matrix[1] +
  287. AV_RN16A(&c[2][2 * x]) * matrix[2] +
  288. AV_RN16A(&c[3][2 * x]) * matrix[3] +
  289. AV_RN16A(&c[4][2 * x]) * matrix[4] +
  290. AV_RN16A(&c[5][2 * x]) * matrix[5] +
  291. AV_RN16A(&c[6][2 * x]) * matrix[6] +
  292. AV_RN16A(&c[7][2 * x]) * matrix[7] +
  293. AV_RN16A(&c[8][2 * x]) * matrix[8];
  294. sum = (int)(sum * rdiv + bias + 0.5f);
  295. dst[x] = av_clip(sum, 0, peak);
  296. }
  297. }
  298. static void filter16_5x5(uint8_t *dstp, int width,
  299. float rdiv, float bias, const int *const matrix,
  300. const uint8_t *c[], int peak, int radius,
  301. int dstride, int stride)
  302. {
  303. uint16_t *dst = (uint16_t *)dstp;
  304. int x;
  305. for (x = 0; x < width; x++) {
  306. int i, sum = 0;
  307. for (i = 0; i < 25; i++)
  308. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  309. sum = (int)(sum * rdiv + bias + 0.5f);
  310. dst[x] = av_clip(sum, 0, peak);
  311. }
  312. }
  313. static void filter16_7x7(uint8_t *dstp, int width,
  314. float rdiv, float bias, const int *const matrix,
  315. const uint8_t *c[], int peak, int radius,
  316. int dstride, int stride)
  317. {
  318. uint16_t *dst = (uint16_t *)dstp;
  319. int x;
  320. for (x = 0; x < width; x++) {
  321. int i, sum = 0;
  322. for (i = 0; i < 49; i++)
  323. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  324. sum = (int)(sum * rdiv + bias + 0.5f);
  325. dst[x] = av_clip(sum, 0, peak);
  326. }
  327. }
  328. static void filter16_row(uint8_t *dstp, int width,
  329. float rdiv, float bias, const int *const matrix,
  330. const uint8_t *c[], int peak, int radius,
  331. int dstride, int stride)
  332. {
  333. uint16_t *dst = (uint16_t *)dstp;
  334. int x;
  335. for (x = 0; x < width; x++) {
  336. int i, sum = 0;
  337. for (i = 0; i < 2 * radius + 1; i++)
  338. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  339. sum = (int)(sum * rdiv + bias + 0.5f);
  340. dst[x] = av_clip(sum, 0, peak);
  341. }
  342. }
  343. static void filter16_column(uint8_t *dstp, int height,
  344. float rdiv, float bias, const int *const matrix,
  345. const uint8_t *c[], int peak, int radius,
  346. int dstride, int stride)
  347. {
  348. uint16_t *dst = (uint16_t *)dstp;
  349. int y;
  350. for (y = 0; y < height; y++) {
  351. int i, sum = 0;
  352. for (i = 0; i < 2 * radius + 1; i++)
  353. sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
  354. sum = (int)(sum * rdiv + bias + 0.5f);
  355. dst[0] = av_clip(sum, 0, peak);
  356. dst += dstride / 2;
  357. }
  358. }
  359. static void filter_7x7(uint8_t *dst, int width,
  360. float rdiv, float bias, const int *const matrix,
  361. const uint8_t *c[], int peak, int radius,
  362. int dstride, int stride)
  363. {
  364. int x;
  365. for (x = 0; x < width; x++) {
  366. int i, sum = 0;
  367. for (i = 0; i < 49; i++)
  368. sum += c[i][x] * matrix[i];
  369. sum = (int)(sum * rdiv + bias + 0.5f);
  370. dst[x] = av_clip_uint8(sum);
  371. }
  372. }
  373. static void filter_5x5(uint8_t *dst, int width,
  374. float rdiv, float bias, const int *const matrix,
  375. const uint8_t *c[], int peak, int radius,
  376. int dstride, int stride)
  377. {
  378. int x;
  379. for (x = 0; x < width; x++) {
  380. int i, sum = 0;
  381. for (i = 0; i < 25; i++)
  382. sum += c[i][x] * matrix[i];
  383. sum = (int)(sum * rdiv + bias + 0.5f);
  384. dst[x] = av_clip_uint8(sum);
  385. }
  386. }
  387. static void filter_3x3(uint8_t *dst, int width,
  388. float rdiv, float bias, const int *const matrix,
  389. const uint8_t *c[], int peak, int radius,
  390. int dstride, int stride)
  391. {
  392. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  393. const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
  394. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  395. int x;
  396. for (x = 0; x < width; x++) {
  397. int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
  398. c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
  399. c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
  400. sum = (int)(sum * rdiv + bias + 0.5f);
  401. dst[x] = av_clip_uint8(sum);
  402. }
  403. }
  404. static void filter_row(uint8_t *dst, int width,
  405. float rdiv, float bias, const int *const matrix,
  406. const uint8_t *c[], int peak, int radius,
  407. int dstride, int stride)
  408. {
  409. int x;
  410. for (x = 0; x < width; x++) {
  411. int i, sum = 0;
  412. for (i = 0; i < 2 * radius + 1; i++)
  413. sum += c[i][x] * matrix[i];
  414. sum = (int)(sum * rdiv + bias + 0.5f);
  415. dst[x] = av_clip_uint8(sum);
  416. }
  417. }
  418. static void filter_column(uint8_t *dst, int height,
  419. float rdiv, float bias, const int *const matrix,
  420. const uint8_t *c[], int peak, int radius,
  421. int dstride, int stride)
  422. {
  423. int y;
  424. for (y = 0; y < height; y++) {
  425. int i, sum = 0;
  426. for (i = 0; i < 2 * radius + 1; i++)
  427. sum += c[i][0 + y * stride] * matrix[i];
  428. sum = (int)(sum * rdiv + bias + 0.5f);
  429. dst[0] = av_clip_uint8(sum);
  430. dst += dstride;
  431. }
  432. }
  433. static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  434. int x, int w, int y, int h, int bpc)
  435. {
  436. int i;
  437. for (i = 0; i < 9; i++) {
  438. int xoff = FFABS(x + ((i % 3) - 1));
  439. int yoff = FFABS(y + (i / 3) - 1);
  440. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  441. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  442. c[i] = src + xoff * bpc + yoff * stride;
  443. }
  444. }
  445. static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  446. int x, int w, int y, int h, int bpc)
  447. {
  448. int i;
  449. for (i = 0; i < 25; i++) {
  450. int xoff = FFABS(x + ((i % 5) - 2));
  451. int yoff = FFABS(y + (i / 5) - 2);
  452. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  453. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  454. c[i] = src + xoff * bpc + yoff * stride;
  455. }
  456. }
  457. static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  458. int x, int w, int y, int h, int bpc)
  459. {
  460. int i;
  461. for (i = 0; i < 49; i++) {
  462. int xoff = FFABS(x + ((i % 7) - 3));
  463. int yoff = FFABS(y + (i / 7) - 3);
  464. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  465. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  466. c[i] = src + xoff * bpc + yoff * stride;
  467. }
  468. }
  469. static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  470. int x, int w, int y, int h, int bpc)
  471. {
  472. int i;
  473. for (i = 0; i < radius * 2 + 1; i++) {
  474. int xoff = FFABS(x + i - radius);
  475. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  476. c[i] = src + xoff * bpc + y * stride;
  477. }
  478. }
  479. static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  480. int x, int w, int y, int h, int bpc)
  481. {
  482. int i;
  483. for (i = 0; i < radius * 2 + 1; i++) {
  484. int xoff = FFABS(x + i - radius);
  485. xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
  486. c[i] = src + y * bpc + xoff * stride;
  487. }
  488. }
  489. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  490. {
  491. ConvolutionContext *s = ctx->priv;
  492. ThreadData *td = arg;
  493. AVFrame *in = td->in;
  494. AVFrame *out = td->out;
  495. int plane;
  496. for (plane = 0; plane < s->nb_planes; plane++) {
  497. const int mode = s->mode[plane];
  498. const int bpc = s->bpc;
  499. const int radius = s->size[plane] / 2;
  500. const int height = s->planeheight[plane];
  501. const int width = s->planewidth[plane];
  502. const int stride = in->linesize[plane];
  503. const int dstride = out->linesize[plane];
  504. const int sizeh = mode == MATRIX_COLUMN ? width : height;
  505. const int sizew = mode == MATRIX_COLUMN ? height : width;
  506. const int slice_start = (sizeh * jobnr) / nb_jobs;
  507. const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
  508. const float rdiv = s->rdiv[plane];
  509. const float bias = s->bias[plane];
  510. const uint8_t *src = in->data[plane];
  511. const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
  512. uint8_t *dst = out->data[plane] + dst_pos;
  513. const int *matrix = s->matrix[plane];
  514. const uint8_t *c[49];
  515. int y, x;
  516. if (s->copy[plane]) {
  517. if (mode == MATRIX_COLUMN)
  518. av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
  519. (slice_end - slice_start) * bpc, height);
  520. else
  521. av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
  522. width * bpc, slice_end - slice_start);
  523. continue;
  524. }
  525. for (y = slice_start; y < slice_end; y++) {
  526. const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
  527. const int yoff = mode == MATRIX_COLUMN ? radius * dstride : 0;
  528. for (x = 0; x < radius; x++) {
  529. const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
  530. const int yoff = mode == MATRIX_COLUMN ? x * dstride : 0;
  531. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  532. s->filter[plane](dst + yoff + xoff, 1, rdiv,
  533. bias, matrix, c, s->max, radius,
  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 * dstride : 0;
  543. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  544. s->filter[plane](dst + yoff + xoff, 1, rdiv,
  545. bias, matrix, c, s->max, radius,
  546. dstride, stride);
  547. }
  548. if (mode != MATRIX_COLUMN)
  549. dst += dstride;
  550. }
  551. }
  552. return 0;
  553. }
  554. static int config_input(AVFilterLink *inlink)
  555. {
  556. AVFilterContext *ctx = inlink->dst;
  557. ConvolutionContext *s = ctx->priv;
  558. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  559. int p;
  560. s->depth = desc->comp[0].depth;
  561. s->max = (1 << s->depth) - 1;
  562. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  563. s->planewidth[0] = s->planewidth[3] = inlink->w;
  564. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  565. s->planeheight[0] = s->planeheight[3] = inlink->h;
  566. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  567. s->nb_threads = ff_filter_get_nb_threads(ctx);
  568. s->bpc = (s->depth + 7) / 8;
  569. if (!strcmp(ctx->filter->name, "convolution")) {
  570. if (s->depth > 8) {
  571. for (p = 0; p < s->nb_planes; p++) {
  572. if (s->mode[p] == MATRIX_ROW)
  573. s->filter[p] = filter16_row;
  574. else if (s->mode[p] == MATRIX_COLUMN)
  575. s->filter[p] = filter16_column;
  576. else if (s->size[p] == 3)
  577. s->filter[p] = filter16_3x3;
  578. else if (s->size[p] == 5)
  579. s->filter[p] = filter16_5x5;
  580. else if (s->size[p] == 7)
  581. s->filter[p] = filter16_7x7;
  582. }
  583. }
  584. #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
  585. ff_convolution_init_x86(s);
  586. #endif
  587. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  588. if (s->depth > 8)
  589. for (p = 0; p < s->nb_planes; p++)
  590. s->filter[p] = filter16_prewitt;
  591. } else if (!strcmp(ctx->filter->name, "roberts")) {
  592. if (s->depth > 8)
  593. for (p = 0; p < s->nb_planes; p++)
  594. s->filter[p] = filter16_roberts;
  595. } else if (!strcmp(ctx->filter->name, "sobel")) {
  596. if (s->depth > 8)
  597. for (p = 0; p < s->nb_planes; p++)
  598. s->filter[p] = filter16_sobel;
  599. } else if (!strcmp(ctx->filter->name, "kirsch")) {
  600. if (s->depth > 8)
  601. for (p = 0; p < s->nb_planes; p++)
  602. s->filter[p] = filter16_kirsch;
  603. }
  604. return 0;
  605. }
  606. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  607. {
  608. AVFilterContext *ctx = inlink->dst;
  609. ConvolutionContext *s = ctx->priv;
  610. AVFilterLink *outlink = ctx->outputs[0];
  611. AVFrame *out;
  612. ThreadData td;
  613. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  614. if (!out) {
  615. av_frame_free(&in);
  616. return AVERROR(ENOMEM);
  617. }
  618. av_frame_copy_props(out, in);
  619. td.in = in;
  620. td.out = out;
  621. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
  622. av_frame_free(&in);
  623. return ff_filter_frame(outlink, out);
  624. }
  625. static av_cold int init(AVFilterContext *ctx)
  626. {
  627. ConvolutionContext *s = ctx->priv;
  628. int i;
  629. if (!strcmp(ctx->filter->name, "convolution")) {
  630. for (i = 0; i < 4; i++) {
  631. int *matrix = (int *)s->matrix[i];
  632. char *p, *arg, *saveptr = NULL;
  633. float sum = 0;
  634. p = s->matrix_str[i];
  635. if (p) {
  636. s->matrix_length[i] = 0;
  637. while (s->matrix_length[i] < 49) {
  638. if (!(arg = av_strtok(p, " |", &saveptr)))
  639. break;
  640. p = NULL;
  641. sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
  642. sum += matrix[s->matrix_length[i]];
  643. s->matrix_length[i]++;
  644. }
  645. if (!(s->matrix_length[i] & 1)) {
  646. av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
  647. return AVERROR(EINVAL);
  648. }
  649. }
  650. if (s->mode[i] == MATRIX_ROW) {
  651. s->filter[i] = filter_row;
  652. s->setup[i] = setup_row;
  653. s->size[i] = s->matrix_length[i];
  654. } else if (s->mode[i] == MATRIX_COLUMN) {
  655. s->filter[i] = filter_column;
  656. s->setup[i] = setup_column;
  657. s->size[i] = s->matrix_length[i];
  658. } else if (s->matrix_length[i] == 9) {
  659. s->size[i] = 3;
  660. if (!memcmp(matrix, same3x3, sizeof(same3x3))) {
  661. s->copy[i] = 1;
  662. } else {
  663. s->filter[i] = filter_3x3;
  664. s->copy[i] = 0;
  665. }
  666. s->setup[i] = setup_3x3;
  667. } else if (s->matrix_length[i] == 25) {
  668. s->size[i] = 5;
  669. if (!memcmp(matrix, same5x5, sizeof(same5x5))) {
  670. s->copy[i] = 1;
  671. } else {
  672. s->filter[i] = filter_5x5;
  673. s->copy[i] = 0;
  674. }
  675. s->setup[i] = setup_5x5;
  676. } else if (s->matrix_length[i] == 49) {
  677. s->size[i] = 7;
  678. if (!memcmp(matrix, same7x7, sizeof(same7x7))) {
  679. s->copy[i] = 1;
  680. } else {
  681. s->filter[i] = filter_7x7;
  682. s->copy[i] = 0;
  683. }
  684. s->setup[i] = setup_7x7;
  685. } else {
  686. return AVERROR(EINVAL);
  687. }
  688. if (sum == 0)
  689. sum = 1;
  690. if (s->rdiv[i] == 0)
  691. s->rdiv[i] = 1. / sum;
  692. if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
  693. s->copy[i] = 0;
  694. }
  695. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  696. for (i = 0; i < 4; i++) {
  697. if ((1 << i) & s->planes)
  698. s->filter[i] = filter_prewitt;
  699. else
  700. s->copy[i] = 1;
  701. s->size[i] = 3;
  702. s->setup[i] = setup_3x3;
  703. s->rdiv[i] = s->scale;
  704. s->bias[i] = s->delta;
  705. }
  706. } else if (!strcmp(ctx->filter->name, "roberts")) {
  707. for (i = 0; i < 4; i++) {
  708. if ((1 << i) & s->planes)
  709. s->filter[i] = filter_roberts;
  710. else
  711. s->copy[i] = 1;
  712. s->size[i] = 3;
  713. s->setup[i] = setup_3x3;
  714. s->rdiv[i] = s->scale;
  715. s->bias[i] = s->delta;
  716. }
  717. } else if (!strcmp(ctx->filter->name, "sobel")) {
  718. for (i = 0; i < 4; i++) {
  719. if ((1 << i) & s->planes)
  720. s->filter[i] = filter_sobel;
  721. else
  722. s->copy[i] = 1;
  723. s->size[i] = 3;
  724. s->setup[i] = setup_3x3;
  725. s->rdiv[i] = s->scale;
  726. s->bias[i] = s->delta;
  727. }
  728. } else if (!strcmp(ctx->filter->name, "kirsch")) {
  729. for (i = 0; i < 4; i++) {
  730. if ((1 << i) & s->planes)
  731. s->filter[i] = filter_kirsch;
  732. else
  733. s->copy[i] = 1;
  734. s->size[i] = 3;
  735. s->setup[i] = setup_3x3;
  736. s->rdiv[i] = s->scale;
  737. s->bias[i] = s->delta;
  738. }
  739. }
  740. return 0;
  741. }
  742. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  743. char *res, int res_len, int flags)
  744. {
  745. int ret;
  746. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  747. if (ret < 0)
  748. return ret;
  749. return init(ctx);
  750. }
  751. static const AVFilterPad convolution_inputs[] = {
  752. {
  753. .name = "default",
  754. .type = AVMEDIA_TYPE_VIDEO,
  755. .config_props = config_input,
  756. .filter_frame = filter_frame,
  757. },
  758. { NULL }
  759. };
  760. static const AVFilterPad convolution_outputs[] = {
  761. {
  762. .name = "default",
  763. .type = AVMEDIA_TYPE_VIDEO,
  764. },
  765. { NULL }
  766. };
  767. #if CONFIG_CONVOLUTION_FILTER
  768. AVFilter ff_vf_convolution = {
  769. .name = "convolution",
  770. .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
  771. .priv_size = sizeof(ConvolutionContext),
  772. .priv_class = &convolution_class,
  773. .init = init,
  774. .query_formats = query_formats,
  775. .inputs = convolution_inputs,
  776. .outputs = convolution_outputs,
  777. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  778. .process_command = process_command,
  779. };
  780. #endif /* CONFIG_CONVOLUTION_FILTER */
  781. #if CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER
  782. static const AVOption prewitt_roberts_sobel_options[] = {
  783. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  784. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  785. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  786. { NULL }
  787. };
  788. #if CONFIG_PREWITT_FILTER
  789. #define prewitt_options prewitt_roberts_sobel_options
  790. AVFILTER_DEFINE_CLASS(prewitt);
  791. AVFilter ff_vf_prewitt = {
  792. .name = "prewitt",
  793. .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
  794. .priv_size = sizeof(ConvolutionContext),
  795. .priv_class = &prewitt_class,
  796. .init = init,
  797. .query_formats = query_formats,
  798. .inputs = convolution_inputs,
  799. .outputs = convolution_outputs,
  800. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  801. .process_command = process_command,
  802. };
  803. #endif /* CONFIG_PREWITT_FILTER */
  804. #if CONFIG_SOBEL_FILTER
  805. #define sobel_options prewitt_roberts_sobel_options
  806. AVFILTER_DEFINE_CLASS(sobel);
  807. AVFilter ff_vf_sobel = {
  808. .name = "sobel",
  809. .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
  810. .priv_size = sizeof(ConvolutionContext),
  811. .priv_class = &sobel_class,
  812. .init = init,
  813. .query_formats = query_formats,
  814. .inputs = convolution_inputs,
  815. .outputs = convolution_outputs,
  816. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  817. .process_command = process_command,
  818. };
  819. #endif /* CONFIG_SOBEL_FILTER */
  820. #if CONFIG_ROBERTS_FILTER
  821. #define roberts_options prewitt_roberts_sobel_options
  822. AVFILTER_DEFINE_CLASS(roberts);
  823. AVFilter ff_vf_roberts = {
  824. .name = "roberts",
  825. .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
  826. .priv_size = sizeof(ConvolutionContext),
  827. .priv_class = &roberts_class,
  828. .init = init,
  829. .query_formats = query_formats,
  830. .inputs = convolution_inputs,
  831. .outputs = convolution_outputs,
  832. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  833. .process_command = process_command,
  834. };
  835. #endif /* CONFIG_ROBERTS_FILTER */
  836. #if CONFIG_KIRSCH_FILTER
  837. #define kirsch_options prewitt_roberts_sobel_options
  838. AVFILTER_DEFINE_CLASS(kirsch);
  839. AVFilter ff_vf_kirsch = {
  840. .name = "kirsch",
  841. .description = NULL_IF_CONFIG_SMALL("Apply kirsch operator."),
  842. .priv_size = sizeof(ConvolutionContext),
  843. .priv_class = &kirsch_class,
  844. .init = init,
  845. .query_formats = query_formats,
  846. .inputs = convolution_inputs,
  847. .outputs = convolution_outputs,
  848. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  849. .process_command = process_command,
  850. };
  851. #endif /* CONFIG_KIRSCH_FILTER */
  852. #endif /* CONFIG_PREWITT_FILTER || CONFIG_ROBERTS_FILTER || CONFIG_SOBEL_FILTER */