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.

784 lines
28KB

  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 "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. enum MatrixMode {
  31. MATRIX_SQUARE,
  32. MATRIX_ROW,
  33. MATRIX_NBMODES,
  34. };
  35. typedef struct ConvolutionContext {
  36. const AVClass *class;
  37. char *matrix_str[4];
  38. float rdiv[4];
  39. float bias[4];
  40. int mode[4];
  41. float scale;
  42. float delta;
  43. int planes;
  44. int size[4];
  45. int depth;
  46. int max;
  47. int bpc;
  48. int nb_planes;
  49. int nb_threads;
  50. int planewidth[4];
  51. int planeheight[4];
  52. int matrix[4][49];
  53. int matrix_length[4];
  54. int copy[4];
  55. void (*setup[4])(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  56. int x, int width, int y, int height, int bpc);
  57. void (*filter[4])(uint8_t *dst, const uint8_t *src, int width,
  58. float rdiv, float bias, const int *const matrix,
  59. const uint8_t *c[], int peak, int radius);
  60. } ConvolutionContext;
  61. #define OFFSET(x) offsetof(ConvolutionContext, x)
  62. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  63. static const AVOption convolution_options[] = {
  64. { "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 },
  65. { "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 },
  66. { "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 },
  67. { "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 },
  68. { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  69. { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  70. { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  71. { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  72. { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  73. { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  74. { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  75. { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
  76. { "0mode", "set matrix mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  77. { "1mode", "set matrix mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  78. { "2mode", "set matrix mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  79. { "3mode", "set matrix mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=MATRIX_SQUARE}, 0, MATRIX_NBMODES-1, FLAGS, "mode" },
  80. { "square", "square matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_SQUARE}, 0, 0, FLAGS, "mode" },
  81. { "row", "single row matrix", 0, AV_OPT_TYPE_CONST, {.i64=MATRIX_ROW} , 0, 0, FLAGS, "mode" },
  82. { NULL }
  83. };
  84. AVFILTER_DEFINE_CLASS(convolution);
  85. static const int same3x3[9] = {0, 0, 0,
  86. 0, 1, 0,
  87. 0, 0, 0};
  88. static const int same5x5[25] = {0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0,
  90. 0, 0, 1, 0, 0,
  91. 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0};
  93. static const int same7x7[49] = {0, 0, 0, 0, 0, 0, 0,
  94. 0, 0, 0, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 0, 0,
  96. 0, 0, 0, 1, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0};
  100. static int query_formats(AVFilterContext *ctx)
  101. {
  102. static const enum AVPixelFormat pix_fmts[] = {
  103. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  104. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  105. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  106. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  107. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  108. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  109. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  110. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  111. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  112. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  113. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  114. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  115. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  116. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  117. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  118. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  119. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  120. AV_PIX_FMT_NONE
  121. };
  122. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  123. }
  124. typedef struct ThreadData {
  125. AVFrame *in, *out;
  126. } ThreadData;
  127. static void filter16_prewitt(uint8_t *dstp, const uint8_t *src, int width,
  128. float scale, float delta, const int *const matrix,
  129. const uint8_t *c[], int peak, int radius)
  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]) * -1 + AV_RN16A(&c[2][2 * x]) * -1 +
  135. AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 1 + 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]) * -1 +
  137. AV_RN16A(&c[5][2 * x]) * 1 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
  138. dst[x] = av_clip(sqrt(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  139. }
  140. }
  141. static void filter16_roberts(uint8_t *dstp, const uint8_t *src, int width,
  142. float scale, float delta, const int *const matrix,
  143. const uint8_t *c[], int peak, int radius)
  144. {
  145. uint16_t *dst = (uint16_t *)dstp;
  146. int x;
  147. for (x = 0; x < width; x++) {
  148. int suma = AV_RN16A(&c[0][2 * x]) * 1 + AV_RN16A(&c[1][2 * x]) * -1;
  149. int sumb = AV_RN16A(&c[4][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -1;
  150. dst[x] = av_clip(sqrt(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  151. }
  152. }
  153. static void filter16_sobel(uint8_t *dstp, const uint8_t *src, int width,
  154. float scale, float delta, const int *const matrix,
  155. const uint8_t *c[], int peak, int radius)
  156. {
  157. uint16_t *dst = (uint16_t *)dstp;
  158. int x;
  159. for (x = 0; x < width; x++) {
  160. int suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
  161. AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
  162. int sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
  163. AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
  164. dst[x] = av_clip(sqrt(suma*suma + sumb*sumb) * scale + delta, 0, peak);
  165. }
  166. }
  167. static void filter_prewitt(uint8_t *dst, const uint8_t *src, int width,
  168. float scale, float delta, const int *const matrix,
  169. const uint8_t *c[], int peak, int radius)
  170. {
  171. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  172. const uint8_t *c3 = c[3], *c5 = c[5];
  173. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  174. int x;
  175. for (x = 0; x < width; x++) {
  176. int suma = c0[x] * -1 + c1[x] * -1 + c2[x] * -1 +
  177. c6[x] * 1 + c7[x] * 1 + c8[x] * 1;
  178. int sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -1 +
  179. c5[x] * 1 + c6[x] * -1 + c8[x] * 1;
  180. dst[x] = av_clip_uint8(sqrt(suma*suma + sumb*sumb) * scale + delta);
  181. }
  182. }
  183. static void filter_roberts(uint8_t *dst, const uint8_t *src, int width,
  184. float scale, float delta, const int *const matrix,
  185. const uint8_t *c[], int peak, int radius)
  186. {
  187. int x;
  188. for (x = 0; x < width; x++) {
  189. int suma = c[0][x] * 1 + c[1][x] * -1;
  190. int sumb = c[4][x] * 1 + c[3][x] * -1;
  191. dst[x] = av_clip_uint8(sqrt(suma*suma + sumb*sumb) * scale + delta);
  192. }
  193. }
  194. static void filter_sobel(uint8_t *dst, const uint8_t *src, int width,
  195. float scale, float delta, const int *const matrix,
  196. const uint8_t *c[], int peak, int radius)
  197. {
  198. const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
  199. const uint8_t *c3 = c[3], *c5 = c[5];
  200. const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
  201. int x;
  202. for (x = 0; x < width; x++) {
  203. int suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
  204. c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
  205. int sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
  206. c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
  207. dst[x] = av_clip_uint8(sqrt(suma*suma + sumb*sumb) * scale + delta);
  208. }
  209. }
  210. static void filter16_3x3(uint8_t *dstp, const uint8_t *src, int width,
  211. float rdiv, float bias, const int *const matrix,
  212. const uint8_t *c[], int peak, int radius)
  213. {
  214. uint16_t *dst = (uint16_t *)dstp;
  215. int x;
  216. for (x = 0; x < width; x++) {
  217. int sum = AV_RN16A(&c[0][2 * x]) * matrix[0] +
  218. AV_RN16A(&c[1][2 * x]) * matrix[1] +
  219. AV_RN16A(&c[2][2 * x]) * matrix[2] +
  220. AV_RN16A(&c[3][2 * x]) * matrix[3] +
  221. AV_RN16A(&c[4][2 * x]) * matrix[4] +
  222. AV_RN16A(&c[5][2 * x]) * matrix[5] +
  223. AV_RN16A(&c[6][2 * x]) * matrix[6] +
  224. AV_RN16A(&c[7][2 * x]) * matrix[7] +
  225. AV_RN16A(&c[8][2 * x]) * matrix[8];
  226. sum = (int)(sum * rdiv + bias + 0.5f);
  227. dst[x] = av_clip(sum, 0, peak);
  228. }
  229. }
  230. static void filter16_5x5(uint8_t *dstp, const uint8_t *src, int width,
  231. float rdiv, float bias, const int *const matrix,
  232. const uint8_t *c[], int peak, int radius)
  233. {
  234. uint16_t *dst = (uint16_t *)dstp;
  235. int x;
  236. for (x = 0; x < width; x++) {
  237. int i, sum = 0;
  238. for (i = 0; i < 25; i++)
  239. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  240. sum = (int)(sum * rdiv + bias + 0.5f);
  241. dst[x] = av_clip(sum, 0, peak);
  242. }
  243. }
  244. static void filter16_7x7(uint8_t *dstp, const uint8_t *src, int width,
  245. float rdiv, float bias, const int *const matrix,
  246. const uint8_t *c[], int peak, int radius)
  247. {
  248. uint16_t *dst = (uint16_t *)dstp;
  249. int x;
  250. for (x = 0; x < width; x++) {
  251. int i, sum = 0;
  252. for (i = 0; i < 49; i++)
  253. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  254. sum = (int)(sum * rdiv + bias + 0.5f);
  255. dst[x] = av_clip(sum, 0, peak);
  256. }
  257. }
  258. static void filter16_row(uint8_t *dstp, const uint8_t *src, int width,
  259. float rdiv, float bias, const int *const matrix,
  260. const uint8_t *c[], int peak, int radius)
  261. {
  262. uint16_t *dst = (uint16_t *)dstp;
  263. int x;
  264. for (x = 0; x < width; x++) {
  265. int i, sum = 0;
  266. for (i = 0; i < 2 * radius + 1; i++)
  267. sum += AV_RN16A(&c[i][2 * x]) * matrix[i];
  268. sum = (int)(sum * rdiv + bias + 0.5f);
  269. dst[x] = av_clip(sum, 0, peak);
  270. }
  271. }
  272. static void filter_7x7(uint8_t *dst, const uint8_t *src, int width,
  273. float rdiv, float bias, const int *const matrix,
  274. const uint8_t *c[], int peak, int radius)
  275. {
  276. int x;
  277. for (x = 0; x < width; x++) {
  278. int i, sum = 0;
  279. for (i = 0; i < 49; i++)
  280. sum += c[i][x] * matrix[i];
  281. sum = (int)(sum * rdiv + bias + 0.5f);
  282. dst[x] = av_clip_uint8(sum);
  283. }
  284. }
  285. static void filter_5x5(uint8_t *dst, const uint8_t *src, int width,
  286. float rdiv, float bias, const int *const matrix,
  287. const uint8_t *c[], int peak, int radius)
  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, const uint8_t *src, int width,
  299. float rdiv, float bias, const int *const matrix,
  300. const uint8_t *c[], int peak, int radius)
  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, const uint8_t *src, int width,
  315. float rdiv, float bias, const int *const matrix,
  316. const uint8_t *c[], int peak, int radius)
  317. {
  318. int x;
  319. for (x = 0; x < width; x++) {
  320. int i, sum = 0;
  321. for (i = 0; i < 2 * radius + 1; i++)
  322. sum += c[i][x] * matrix[i];
  323. sum = (int)(sum * rdiv + bias + 0.5f);
  324. dst[x] = av_clip_uint8(sum);
  325. }
  326. }
  327. static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  328. int x, int w, int y, int h, int bpc)
  329. {
  330. int i;
  331. for (i = 0; i < 9; i++) {
  332. int xoff = FFABS(x + ((i % 3) - 1));
  333. int yoff = FFABS(y + (i / 3) - 1);
  334. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  335. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  336. c[i] = src + xoff * bpc + yoff * stride;
  337. }
  338. }
  339. static void setup_5x5(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  340. int x, int w, int y, int h, int bpc)
  341. {
  342. int i;
  343. for (i = 0; i < 25; i++) {
  344. int xoff = FFABS(x + ((i % 5) - 2));
  345. int yoff = FFABS(y + (i / 5) - 2);
  346. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  347. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  348. c[i] = src + xoff * bpc + yoff * stride;
  349. }
  350. }
  351. static void setup_7x7(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  352. int x, int w, int y, int h, int bpc)
  353. {
  354. int i;
  355. for (i = 0; i < 49; i++) {
  356. int xoff = FFABS(x + ((i % 7) - 3));
  357. int yoff = FFABS(y + (i / 7) - 3);
  358. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  359. yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
  360. c[i] = src + xoff * bpc + yoff * stride;
  361. }
  362. }
  363. static void setup_row(int radius, const uint8_t *c[], const uint8_t *src, int stride,
  364. int x, int w, int y, int h, int bpc)
  365. {
  366. int i;
  367. for (i = 0; i < radius * 2 + 1; i++) {
  368. int xoff = FFABS(x + i - radius);
  369. xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
  370. c[i] = src + xoff * bpc + y * stride;
  371. }
  372. }
  373. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  374. {
  375. ConvolutionContext *s = ctx->priv;
  376. ThreadData *td = arg;
  377. AVFrame *in = td->in;
  378. AVFrame *out = td->out;
  379. int plane;
  380. for (plane = 0; plane < s->nb_planes; plane++) {
  381. const int radius = s->size[plane] / 2;
  382. const int height = s->planeheight[plane];
  383. const int width = s->planewidth[plane];
  384. const int stride = in->linesize[plane];
  385. const int dstride = out->linesize[plane];
  386. const int slice_start = (height * jobnr) / nb_jobs;
  387. const int slice_end = (height * (jobnr+1)) / nb_jobs;
  388. const float rdiv = s->rdiv[plane];
  389. const float bias = s->bias[plane];
  390. const uint8_t *src = in->data[plane];
  391. uint8_t *dst = out->data[plane] + slice_start * out->linesize[plane];
  392. const int *matrix = s->matrix[plane];
  393. const int bpc = s->bpc;
  394. const uint8_t *c[49];
  395. int y, x;
  396. if (s->copy[plane]) {
  397. av_image_copy_plane(dst, dstride, src + slice_start * stride, stride,
  398. width * bpc, slice_end - slice_start);
  399. continue;
  400. }
  401. for (y = slice_start; y < slice_end; y++) {
  402. for (x = 0; x < radius; x++) {
  403. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  404. s->filter[plane](dst + x * bpc, src, 1, rdiv,
  405. bias, matrix, c, s->max, radius);
  406. }
  407. s->setup[plane](radius, c, src, stride, radius, width, y, height, bpc);
  408. s->filter[plane](dst + radius * bpc, src, width - 2 * radius,
  409. rdiv, bias, matrix, c, s->max, radius);
  410. for (x = width - radius; x < width; x++) {
  411. s->setup[plane](radius, c, src, stride, x, width, y, height, bpc);
  412. s->filter[plane](dst + x * bpc, src, 1, rdiv,
  413. bias, matrix, c, s->max, radius);
  414. }
  415. dst += dstride;
  416. }
  417. }
  418. return 0;
  419. }
  420. static int config_input(AVFilterLink *inlink)
  421. {
  422. AVFilterContext *ctx = inlink->dst;
  423. ConvolutionContext *s = ctx->priv;
  424. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  425. int p;
  426. s->depth = desc->comp[0].depth;
  427. s->max = (1 << s->depth) - 1;
  428. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  429. s->planewidth[0] = s->planewidth[3] = inlink->w;
  430. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  431. s->planeheight[0] = s->planeheight[3] = inlink->h;
  432. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  433. s->nb_threads = ff_filter_get_nb_threads(ctx);
  434. s->bpc = (s->depth + 7) / 8;
  435. if (!strcmp(ctx->filter->name, "convolution")) {
  436. if (s->depth > 8) {
  437. for (p = 0; p < s->nb_planes; p++) {
  438. if (s->mode[p] == MATRIX_ROW)
  439. s->filter[p] = filter16_row;
  440. else if (s->size[p] == 3)
  441. s->filter[p] = filter16_3x3;
  442. else if (s->size[p] == 5)
  443. s->filter[p] = filter16_5x5;
  444. else if (s->size[p] == 7)
  445. s->filter[p] = filter16_7x7;
  446. }
  447. }
  448. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  449. if (s->depth > 8)
  450. for (p = 0; p < s->nb_planes; p++)
  451. s->filter[p] = filter16_prewitt;
  452. } else if (!strcmp(ctx->filter->name, "roberts")) {
  453. if (s->depth > 8)
  454. for (p = 0; p < s->nb_planes; p++)
  455. s->filter[p] = filter16_roberts;
  456. } else if (!strcmp(ctx->filter->name, "sobel")) {
  457. if (s->depth > 8)
  458. for (p = 0; p < s->nb_planes; p++)
  459. s->filter[p] = filter16_sobel;
  460. }
  461. return 0;
  462. }
  463. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  464. {
  465. AVFilterContext *ctx = inlink->dst;
  466. ConvolutionContext *s = ctx->priv;
  467. AVFilterLink *outlink = ctx->outputs[0];
  468. AVFrame *out;
  469. ThreadData td;
  470. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  471. if (!out) {
  472. av_frame_free(&in);
  473. return AVERROR(ENOMEM);
  474. }
  475. av_frame_copy_props(out, in);
  476. td.in = in;
  477. td.out = out;
  478. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(s->planeheight[1], s->nb_threads));
  479. av_frame_free(&in);
  480. return ff_filter_frame(outlink, out);
  481. }
  482. static av_cold int init(AVFilterContext *ctx)
  483. {
  484. ConvolutionContext *s = ctx->priv;
  485. int i;
  486. if (!strcmp(ctx->filter->name, "convolution")) {
  487. for (i = 0; i < 4; i++) {
  488. int *matrix = (int *)s->matrix[i];
  489. char *p, *arg, *saveptr = NULL;
  490. float sum = 0;
  491. p = s->matrix_str[i];
  492. while (s->matrix_length[i] < 49) {
  493. if (!(arg = av_strtok(p, " ", &saveptr)))
  494. break;
  495. p = NULL;
  496. sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
  497. sum += matrix[s->matrix_length[i]];
  498. s->matrix_length[i]++;
  499. }
  500. if (!(s->matrix_length[i] & 1)) {
  501. av_log(ctx, AV_LOG_ERROR, "number of matrix elements must be odd\n");
  502. return AVERROR(EINVAL);
  503. }
  504. if (s->mode[i] == MATRIX_ROW) {
  505. s->filter[i] = filter_row;
  506. s->setup[i] = setup_row;
  507. s->size[i] = s->matrix_length[i];
  508. } else if (s->matrix_length[i] == 9) {
  509. s->size[i] = 3;
  510. if (!memcmp(matrix, same3x3, sizeof(same3x3)))
  511. s->copy[i] = 1;
  512. else
  513. s->filter[i] = filter_3x3;
  514. s->setup[i] = setup_3x3;
  515. } else if (s->matrix_length[i] == 25) {
  516. s->size[i] = 5;
  517. if (!memcmp(matrix, same5x5, sizeof(same5x5)))
  518. s->copy[i] = 1;
  519. else
  520. s->filter[i] = filter_5x5;
  521. s->setup[i] = setup_5x5;
  522. } else if (s->matrix_length[i] == 49) {
  523. s->size[i] = 7;
  524. if (!memcmp(matrix, same7x7, sizeof(same7x7)))
  525. s->copy[i] = 1;
  526. else
  527. s->filter[i] = filter_7x7;
  528. s->setup[i] = setup_7x7;
  529. } else {
  530. return AVERROR(EINVAL);
  531. }
  532. if (sum == 0)
  533. sum = 1;
  534. if (s->rdiv[i] == 0)
  535. s->rdiv[i] = 1. / sum;
  536. if (s->copy[i] && (s->rdiv[i] != 1. || s->bias[i] != 0.))
  537. s->copy[i] = 0;
  538. }
  539. } else if (!strcmp(ctx->filter->name, "prewitt")) {
  540. for (i = 0; i < 4; i++) {
  541. if ((1 << i) & s->planes)
  542. s->filter[i] = filter_prewitt;
  543. else
  544. s->copy[i] = 1;
  545. s->size[i] = 3;
  546. s->setup[i] = setup_3x3;
  547. s->rdiv[i] = s->scale;
  548. s->bias[i] = s->delta;
  549. }
  550. } else if (!strcmp(ctx->filter->name, "roberts")) {
  551. for (i = 0; i < 4; i++) {
  552. if ((1 << i) & s->planes)
  553. s->filter[i] = filter_roberts;
  554. else
  555. s->copy[i] = 1;
  556. s->size[i] = 3;
  557. s->setup[i] = setup_3x3;
  558. s->rdiv[i] = s->scale;
  559. s->bias[i] = s->delta;
  560. }
  561. } else if (!strcmp(ctx->filter->name, "sobel")) {
  562. for (i = 0; i < 4; i++) {
  563. if ((1 << i) & s->planes)
  564. s->filter[i] = filter_sobel;
  565. else
  566. s->copy[i] = 1;
  567. s->size[i] = 3;
  568. s->setup[i] = setup_3x3;
  569. s->rdiv[i] = s->scale;
  570. s->bias[i] = s->delta;
  571. }
  572. }
  573. return 0;
  574. }
  575. static const AVFilterPad convolution_inputs[] = {
  576. {
  577. .name = "default",
  578. .type = AVMEDIA_TYPE_VIDEO,
  579. .config_props = config_input,
  580. .filter_frame = filter_frame,
  581. },
  582. { NULL }
  583. };
  584. static const AVFilterPad convolution_outputs[] = {
  585. {
  586. .name = "default",
  587. .type = AVMEDIA_TYPE_VIDEO,
  588. },
  589. { NULL }
  590. };
  591. #if CONFIG_CONVOLUTION_FILTER
  592. AVFilter ff_vf_convolution = {
  593. .name = "convolution",
  594. .description = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
  595. .priv_size = sizeof(ConvolutionContext),
  596. .priv_class = &convolution_class,
  597. .init = init,
  598. .query_formats = query_formats,
  599. .inputs = convolution_inputs,
  600. .outputs = convolution_outputs,
  601. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  602. };
  603. #endif /* CONFIG_CONVOLUTION_FILTER */
  604. #if CONFIG_PREWITT_FILTER
  605. static const AVOption prewitt_options[] = {
  606. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  607. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  608. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  609. { NULL }
  610. };
  611. AVFILTER_DEFINE_CLASS(prewitt);
  612. AVFilter ff_vf_prewitt = {
  613. .name = "prewitt",
  614. .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator."),
  615. .priv_size = sizeof(ConvolutionContext),
  616. .priv_class = &prewitt_class,
  617. .init = init,
  618. .query_formats = query_formats,
  619. .inputs = convolution_inputs,
  620. .outputs = convolution_outputs,
  621. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  622. };
  623. #endif /* CONFIG_PREWITT_FILTER */
  624. #if CONFIG_SOBEL_FILTER
  625. static const AVOption sobel_options[] = {
  626. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  627. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  628. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  629. { NULL }
  630. };
  631. AVFILTER_DEFINE_CLASS(sobel);
  632. AVFilter ff_vf_sobel = {
  633. .name = "sobel",
  634. .description = NULL_IF_CONFIG_SMALL("Apply sobel operator."),
  635. .priv_size = sizeof(ConvolutionContext),
  636. .priv_class = &sobel_class,
  637. .init = init,
  638. .query_formats = query_formats,
  639. .inputs = convolution_inputs,
  640. .outputs = convolution_outputs,
  641. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  642. };
  643. #endif /* CONFIG_SOBEL_FILTER */
  644. #if CONFIG_ROBERTS_FILTER
  645. static const AVOption roberts_options[] = {
  646. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  647. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
  648. { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
  649. { NULL }
  650. };
  651. AVFILTER_DEFINE_CLASS(roberts);
  652. AVFilter ff_vf_roberts = {
  653. .name = "roberts",
  654. .description = NULL_IF_CONFIG_SMALL("Apply roberts cross operator."),
  655. .priv_size = sizeof(ConvolutionContext),
  656. .priv_class = &roberts_class,
  657. .init = init,
  658. .query_formats = query_formats,
  659. .inputs = convolution_inputs,
  660. .outputs = convolution_outputs,
  661. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  662. };
  663. #endif /* CONFIG_ROBERTS_FILTER */