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.

710 lines
25KB

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