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.

847 lines
30KB

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