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.

848 lines
31KB

  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
  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 filter_prewitt(uint8_t *dst, 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. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  148. const uint8_t *c3 = c[3], *c5 = c[5];
  149. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  150. int x;
  151. for (x = 0; x < width; x++) {
  152. float suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
  153. c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
  154. float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
  155. c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
  156. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  157. }
  158. }
  159. static void filter_roberts(uint8_t *dst, int width,
  160. float scale, float delta, const int *const matrix,
  161. const uint8_t *c[], int peak, int radius,
  162. int dstride, int stride)
  163. {
  164. int x;
  165. for (x = 0; x < width; x++) {
  166. float suma = c[0][x] * 1 + c[1][x] * -1;
  167. float sumb = c[4][x] * 1 + c[3][x] * -1;
  168. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  169. }
  170. }
  171. static void filter_sobel(uint8_t *dst, int width,
  172. float scale, float delta, const int *const matrix,
  173. const uint8_t *c[], int peak, int radius,
  174. int dstride, int stride)
  175. {
  176. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  177. const uint8_t *c3 = c[3], *c5 = c[5];
  178. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  179. int x;
  180. for (x = 0; x < width; x++) {
  181. float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
  182. c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
  183. float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
  184. c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
  185. dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
  186. }
  187. }
  188. static void filter16_3x3(uint8_t *dstp, int width,
  189. float rdiv, float bias, const int *const matrix,
  190. const uint8_t *c[], int peak, int radius,
  191. int dstride, int stride)
  192. {
  193. uint16_t *dst = (uint16_t *)dstp;
  194. int x;
  195. for (x = 0; x < width; x++) {
  196. int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
  197. AV_RN16A(&c[1][2 * x]) * matrix[1] +
  198. AV_RN16A(&c[2][2 * x]) * matrix[2] +
  199. AV_RN16A(&c[3][2 * x]) * matrix[3] +
  200. AV_RN16A(&c[4][2 * x]) * matrix[4] +
  201. AV_RN16A(&c[5][2 * x]) * matrix[5] +
  202. AV_RN16A(&c[6][2 * x]) * matrix[6] +
  203. AV_RN16A(&c[7][2 * x]) * matrix[7] +
  204. AV_RN16A(&c[8][2 * x]) * matrix[8];
  205. sum = (int)(sum * rdiv + bias + 0.5f);
  206. dst[x] = av_clip(sum, 0, peak);
  207. }
  208. }
  209. static void filter16_5x5(uint8_t *dstp, int width,
  210. float rdiv, float bias, const int *const matrix,
  211. const uint8_t *c[], int peak, int radius,
  212. int dstride, int stride)
  213. {
  214. uint16_t *dst = (uint16_t *)dstp;
  215. int x;
  216. for (x = 0; x < width; x++) {
  217. int i, sum = 0;
  218. for (i = 0; i < 25; i++)
  219. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  220. sum = (int)(sum * rdiv + bias + 0.5f);
  221. dst[x] = av_clip(sum, 0, peak);
  222. }
  223. }
  224. static void filter16_7x7(uint8_t *dstp, int width,
  225. float rdiv, float bias, const int *const matrix,
  226. const uint8_t *c[], int peak, int radius,
  227. int dstride, int stride)
  228. {
  229. uint16_t *dst = (uint16_t *)dstp;
  230. int x;
  231. for (x = 0; x < width; x++) {
  232. int i, sum = 0;
  233. for (i = 0; i < 49; i++)
  234. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  235. sum = (int)(sum * rdiv + bias + 0.5f);
  236. dst[x] = av_clip(sum, 0, peak);
  237. }
  238. }
  239. static void filter16_row(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. for (x = 0; x < width; x++) {
  247. int i, sum = 0;
  248. for (i = 0; i < 2 * radius + 1; i++)
  249. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  250. sum = (int)(sum * rdiv + bias + 0.5f);
  251. dst[x] = av_clip(sum, 0, peak);
  252. }
  253. }
  254. static void filter16_column(uint8_t *dstp, int height,
  255. float rdiv, float bias, const int *const matrix,
  256. const uint8_t *c[], int peak, int radius,
  257. int dstride, int stride)
  258. {
  259. uint16_t *dst = (uint16_t *)dstp;
  260. int y;
  261. for (y = 0; y < height; y++) {
  262. int i, sum = 0;
  263. for (i = 0; i < 2 * radius + 1; i++)
  264. sum += AV_RN16A(&c[i][0 + y * stride]) * matrix[i];
  265. sum = (int)(sum * rdiv + bias + 0.5f);
  266. dst[0] = av_clip(sum, 0, peak);
  267. dst += dstride / 2;
  268. }
  269. }
  270. static void filter_7x7(uint8_t *dst, int width,
  271. float rdiv, float bias, const int *const matrix,
  272. const uint8_t *c[], int peak, int radius,
  273. int dstride, int stride)
  274. {
  275. int x;
  276. for (x = 0; x < width; x++) {
  277. int i, sum = 0;
  278. for (i = 0; i < 49; i++)
  279. sum += c[i][x] * matrix[i];
  280. sum = (int)(sum * rdiv + bias + 0.5f);
  281. dst[x] = av_clip_uint8(sum);
  282. }
  283. }
  284. static void filter_5x5(uint8_t *dst, int width,
  285. float rdiv, float bias, const int *const matrix,
  286. const uint8_t *c[], int peak, int radius,
  287. int dstride, int stride)
  288. {
  289. int x;
  290. for (x = 0; x < width; x++) {
  291. int i, sum = 0;
  292. for (i = 0; i < 25; i++)
  293. sum += c[i][x] * matrix[i];
  294. sum = (int)(sum * rdiv + bias + 0.5f);
  295. dst[x] = av_clip_uint8(sum);
  296. }
  297. }
  298. static void filter_3x3(uint8_t *dst, 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. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  304. const uint8_t *c3 = c[3], *c4 = c[4], *c5 = c[5];
  305. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  306. int x;
  307. for (x = 0; x < width; x++) {
  308. int sum = c0[x] * matrix[0] + c1[x] * matrix[1] + c2[x] * matrix[2] +
  309. c3[x] * matrix[3] + c4[x] * matrix[4] + c5[x] * matrix[5] +
  310. c6[x] * matrix[6] + c7[x] * matrix[7] + c8[x] * matrix[8];
  311. sum = (int)(sum * rdiv + bias + 0.5f);
  312. dst[x] = av_clip_uint8(sum);
  313. }
  314. }
  315. static void filter_row(uint8_t *dst, int width,
  316. float rdiv, float bias, const int *const matrix,
  317. const uint8_t *c[], int peak, int radius,
  318. int dstride, int stride)
  319. {
  320. int x;
  321. for (x = 0; x < width; x++) {
  322. int i, sum = 0;
  323. for (i = 0; i < 2 * radius + 1; i++)
  324. sum += c[i][x] * matrix[i];
  325. sum = (int)(sum * rdiv + bias + 0.5f);
  326. dst[x] = av_clip_uint8(sum);
  327. }
  328. }
  329. static void filter_column(uint8_t *dst, int height,
  330. float rdiv, float bias, const int *const matrix,
  331. const uint8_t *c[], int peak, int radius,
  332. int dstride, int stride)
  333. {
  334. int y;
  335. for (y = 0; y < height; y++) {
  336. int i, sum = 0;
  337. for (i = 0; i < 2 * radius + 1; i++)
  338. sum += c[i][0 + y * stride] * matrix[i];
  339. sum = (int)(sum * rdiv + bias + 0.5f);
  340. dst[0] = av_clip_uint8(sum);
  341. dst += dstride;
  342. }
  343. }
  344. static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  345. int x, int w, int y, int h, int bpc)
  346. {
  347. int i;
  348. for (i = 0; i < 9; i++) {
  349. int xoff = FFABS(x + ((i % 3) - 1));
  350. int yoff = FFABS(y + (i / 3) - 1);
  351. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  352. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  353. c[i] = src + xoff * bpc + yoff * stride;
  354. }
  355. }
  356. static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  357. int x, int w, int y, int h, int bpc)
  358. {
  359. int i;
  360. for (i = 0; i < 25; i++) {
  361. int xoff = FFABS(x + ((i % 5) - 2));
  362. int yoff = FFABS(y + (i / 5) - 2);
  363. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  364. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  365. c[i] = src + xoff * bpc + yoff * stride;
  366. }
  367. }
  368. static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  369. int x, int w, int y, int h, int bpc)
  370. {
  371. int i;
  372. for (i = 0; i < 49; i++) {
  373. int xoff = FFABS(x + ((i % 7) - 3));
  374. int yoff = FFABS(y + (i / 7) - 3);
  375. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  376. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  377. c[i] = src + xoff * bpc + yoff * stride;
  378. }
  379. }
  380. static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  381. int x, int w, int y, int h, int bpc)
  382. {
  383. int i;
  384. for (i = 0; i < radius * 2 + 1; i++) {
  385. int xoff = FFABS(x + i - radius);
  386. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  387. c[i] = src + xoff * bpc + y * stride;
  388. }
  389. }
  390. static void setup_column(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  391. int x, int w, int y, int h, int bpc)
  392. {
  393. int i;
  394. for (i = 0; i < radius * 2 + 1; i++) {
  395. int xoff = FFABS(x + i - radius);
  396. xoff = xoff >= h ? 2 * h - 1 - xoff : xoff;
  397. c[i] = src + y * bpc + xoff * stride;
  398. }
  399. }
  400. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  401. {
  402. ConvolutionContext *s = ctx->priv;
  403. ThreadData *td = arg;
  404. AVFrame *in = td->in;
  405. AVFrame *out = td->out;
  406. int plane;
  407. for (plane = 0; plane < s->nb_planes; plane++) {
  408. const int mode = s->mode[plane];
  409. const int bpc = s->bpc;
  410. const int radius = s->size[plane] / 2;
  411. const int height = s->planeheight[plane];
  412. const int width = s->planewidth[plane];
  413. const int stride = in->linesize[plane];
  414. const int dstride = out->linesize[plane];
  415. const int sizeh = mode == MATRIX_COLUMN ? width : height;
  416. const int sizew = mode == MATRIX_COLUMN ? height : width;
  417. const int slice_start = (sizeh * jobnr) / nb_jobs;
  418. const int slice_end = (sizeh * (jobnr+1)) / nb_jobs;
  419. const float rdiv = s->rdiv[plane];
  420. const float bias = s->bias[plane];
  421. const uint8_t *src = in->data[plane];
  422. const int dst_pos = slice_start * (mode == MATRIX_COLUMN ? bpc : dstride);
  423. uint8_t *dst = out->data[plane] + dst_pos;
  424. const int *matrix = s->matrix[plane];
  425. const uint8_t *c[49];
  426. int y, x;
  427. if (s->copy[plane]) {
  428. if (mode == MATRIX_COLUMN)
  429. av_image_copy_plane(dst, dstride, src + slice_start * bpc, stride,
  430. (slice_end - slice_start) * bpc, height);
  431. else
  432. av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
  433. width * bpc, slice_end - slice_start);
  434. continue;
  435. }
  436. for (y = slice_start; y < slice_end; y++) {
  437. const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
  438. const int yoff = mode == MATRIX_COLUMN ? radius * stride : 0;
  439. for (x = 0; x < radius; x++) {
  440. const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
  441. const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
  442. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  443. s->filter[plane](dst + yoff + xoff, 1, rdiv,
  444. bias, matrix, c, s->max, radius,
  445. dstride, stride);
  446. }
  447. s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
  448. s->filter[plane](dst + yoff + xoff, sizew - 2 * radius,
  449. rdiv, bias, matrix, c, s->max, radius,
  450. dstride, stride);
  451. for (x = sizew - radius; x < sizew; x++) {
  452. const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : x * bpc;
  453. const int yoff = mode == MATRIX_COLUMN ? x * stride : 0;
  454. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  455. s->filter[plane](dst + yoff + xoff, 1, rdiv,
  456. bias, matrix, c, s->max, radius,
  457. dstride, stride);
  458. }
  459. if (mode != MATRIX_COLUMN)
  460. dst += dstride;
  461. }
  462. }
  463. return 0;
  464. }
  465. static int config_input(AVFilterLink *inlink)
  466. {
  467. AVFilterContext *ctx = inlink->dst;
  468. ConvolutionContext *s = ctx->priv;
  469. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  470. int p;
  471. s->depth = desc->comp[0].depth;
  472. s->max = (1 << s->depth) - 1;
  473. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  474. s->planewidth[0] = s->planewidth[3] = inlink->w;
  475. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  476. s->planeheight[0] = s->planeheight[3] = inlink->h;
  477. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  478. s->nb_threads = ff_filter_get_nb_threads(ctx);
  479. s->bpc = (s->depth + 7) / 8;
  480. if (!strcmp(ctx->filter->name, "convolution")) {
  481. if (s->depth > 8) {
  482. for (p = 0; p < s->nb_planes; p++) {
  483. if (s->mode[p] == MATRIX_ROW)
  484. s->filter[p] = filter16_row;
  485. else if (s->mode[p] == MATRIX_COLUMN)
  486. s->filter[p] = filter16_column;
  487. else if (s->size[p] == 3)
  488. s->filter[p] = filter16_3x3;
  489. else if (s->size[p] == 5)
  490. s->filter[p] = filter16_5x5;
  491. else if (s->size[p] == 7)
  492. s->filter[p] = filter16_7x7;
  493. }
  494. }
  495. #if CONFIG_CONVOLUTION_FILTER && ARCH_X86_64
  496. ff_convolution_init_x86(s);
  497. #endif
  498. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  499. if (s->depth > 8)
  500. for (p = 0; p < s->nb_planes; p++)
  501. s->filter[p] = filter16_prewitt;
  502. } else if (!strcmp(ctx->filter->name, "roberts")) {
  503. if (s->depth > 8)
  504. for (p = 0; p < s->nb_planes; p++)
  505. s->filter[p] = filter16_roberts;
  506. } else if (!strcmp(ctx->filter->name, "sobel")) {
  507. if (s->depth > 8)
  508. for (p = 0; p < s->nb_planes; p++)
  509. s->filter[p] = filter16_sobel;
  510. }
  511. return 0;
  512. }
  513. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  514. {
  515. AVFilterContext *ctx = inlink->dst;
  516. ConvolutionContext *s = ctx->priv;
  517. AVFilterLink *outlink = ctx->outputs[0];
  518. AVFrame *out;
  519. ThreadData td;
  520. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  521. if (!out) {
  522. av_frame_free(&in);
  523. return AVERROR(ENOMEM);
  524. }
  525. av_frame_copy_props(out, in);
  526. td.in = in;
  527. td.out = out;
  528. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN3(s->planeheight[1], s->planewidth[1], s->nb_threads));
  529. av_frame_free(&in);
  530. return ff_filter_frame(outlink, out);
  531. }
  532. static av_cold int init(AVFilterContext *ctx)
  533. {
  534. ConvolutionContext *s = ctx->priv;
  535. int i;
  536. if (!strcmp(ctx->filter->name, "convolution")) {
  537. for (i = 0; i < 4; i++) {
  538. int *matrix = (int *)s->matrix[i];
  539. char *p, *arg, *saveptr = NULL;
  540. float sum = 0;
  541. p = s->matrix_str[i];
  542. while (s->matrix_length[i] < 49) {
  543. if (!(arg = av_strtok(p, " ", &saveptr)))
  544. break;
  545. p = NULL;
  546. sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
  547. sum += matrix[s->matrix_length[i]];
  548. s->matrix_length[i]++;
  549. }
  550. if (!(s->matrix_length[i] & 1)) {
  551. av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
  552. return AVERROR(EINVAL);
  553. }
  554. if (s->mode[i] == MATRIX_ROW) {
  555. s->filter[i] = filter_row;
  556. s->setup[i] = setup_row;
  557. s->size[i] = s->matrix_length[i];
  558. } else if (s->mode[i] == MATRIX_COLUMN) {
  559. s->filter[i] = filter_column;
  560. s->setup[i] = setup_column;
  561. s->size[i] = s->matrix_length[i];
  562. } else if (s->matrix_length[i] == 9) {
  563. s->size[i] = 3;
  564. if (!memcmp(matrix, same3x3, sizeof(same3x3)))
  565. s->copy[i] = 1;
  566. else
  567. s->filter[i] = filter_3x3;
  568. s->setup[i] = setup_3x3;
  569. } else if (s->matrix_length[i] == 25) {
  570. s->size[i] = 5;
  571. if (!memcmp(matrix, same5x5, sizeof(same5x5)))
  572. s->copy[i] = 1;
  573. else
  574. s->filter[i] = filter_5x5;
  575. s->setup[i] = setup_5x5;
  576. } else if (s->matrix_length[i] == 49) {
  577. s->size[i] = 7;
  578. if (!memcmp(matrix, same7x7, sizeof(same7x7)))
  579. s->copy[i] = 1;
  580. else
  581. s->filter[i] = filter_7x7;
  582. s->setup[i] = setup_7x7;
  583. } else {
  584. return AVERROR(EINVAL);
  585. }
  586. if (sum == 0)
  587. sum = 1;
  588. if (s->rdiv[i] == 0)
  589. s->rdiv[i] = 1. / sum;
  590. if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
  591. s->copy[i] = 0;
  592. }
  593. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  594. for (i = 0; i < 4; i++) {
  595. if ((1 << i) & s->planes)
  596. s->filter[i] = filter_prewitt;
  597. else
  598. s->copy[i] = 1;
  599. s->size[i] = 3;
  600. s->setup[i] = setup_3x3;
  601. s->rdiv[i] = s->scale;
  602. s->bias[i] = s->delta;
  603. }
  604. } else if (!strcmp(ctx->filter->name, "roberts")) {
  605. for (i = 0; i < 4; i++) {
  606. if ((1 << i) & s->planes)
  607. s->filter[i] = filter_roberts;
  608. else
  609. s->copy[i] = 1;
  610. s->size[i] = 3;
  611. s->setup[i] = setup_3x3;
  612. s->rdiv[i] = s->scale;
  613. s->bias[i] = s->delta;
  614. }
  615. } else if (!strcmp(ctx->filter->name, "sobel")) {
  616. for (i = 0; i < 4; i++) {
  617. if ((1 << i) & s->planes)
  618. s->filter[i] = filter_sobel;
  619. else
  620. s->copy[i] = 1;
  621. s->size[i] = 3;
  622. s->setup[i] = setup_3x3;
  623. s->rdiv[i] = s->scale;
  624. s->bias[i] = s->delta;
  625. }
  626. }
  627. return 0;
  628. }
  629. static const AVFilterPad convolution_inputs[] = {
  630. {
  631. .name = "default",
  632. .type = AVMEDIA_TYPE_VIDEO,
  633. .config_props = config_input,
  634. .filter_frame = filter_frame,
  635. },
  636. { NULL }
  637. };
  638. static const AVFilterPad convolution_outputs[] = {
  639. {
  640. .name = "default",
  641. .type = AVMEDIA_TYPE_VIDEO,
  642. },
  643. { NULL }
  644. };
  645. #if CONFIG_CONVOLUTION_FILTER
  646. AVFilter ff_vf_convolution = {
  647. .name = "convolution",
  648. .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
  649. .priv_size = sizeof(ConvolutionContext),
  650. .priv_class = &convolution_class,
  651. .init = init,
  652. .query_formats = query_formats,
  653. .inputs = convolution_inputs,
  654. .outputs = convolution_outputs,
  655. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  656. };
  657. #endif /* CONFIG_CONVOLUTION_FILTER */
  658. #if CONFIG_PREWITT_FILTER
  659. static const AVOption prewitt_options[] = {
  660. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  661. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  662. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  663. { NULL }
  664. };
  665. AVFILTER_DEFINE_CLASS(prewitt);
  666. AVFilter ff_vf_prewitt = {
  667. .name = "prewitt",
  668. .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
  669. .priv_size = sizeof(ConvolutionContext),
  670. .priv_class = &prewitt_class,
  671. .init = init,
  672. .query_formats = query_formats,
  673. .inputs = convolution_inputs,
  674. .outputs = convolution_outputs,
  675. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  676. };
  677. #endif /* CONFIG_PREWITT_FILTER */
  678. #if CONFIG_SOBEL_FILTER
  679. static const AVOption sobel_options[] = {
  680. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  681. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  682. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  683. { NULL }
  684. };
  685. AVFILTER_DEFINE_CLASS(sobel);
  686. AVFilter ff_vf_sobel = {
  687. .name = "sobel",
  688. .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
  689. .priv_size = sizeof(ConvolutionContext),
  690. .priv_class = &sobel_class,
  691. .init = init,
  692. .query_formats = query_formats,
  693. .inputs = convolution_inputs,
  694. .outputs = convolution_outputs,
  695. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  696. };
  697. #endif /* CONFIG_SOBEL_FILTER */
  698. #if CONFIG_ROBERTS_FILTER
  699. static const AVOption roberts_options[] = {
  700. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  701. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  702. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  703. { NULL }
  704. };
  705. AVFILTER_DEFINE_CLASS(roberts);
  706. AVFilter ff_vf_roberts = {
  707. .name = "roberts",
  708. .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
  709. .priv_size = sizeof(ConvolutionContext),
  710. .priv_class = &roberts_class,
  711. .init = init,
  712. .query_formats = query_formats,
  713. .inputs = convolution_inputs,
  714. .outputs = convolution_outputs,
  715. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  716. };
  717. #endif /* CONFIG_ROBERTS_FILTER */