vf_removegrain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright (c) 2012 Laurent de Soras
  3. * Copyright (c) 2013 Fredrik Mellbin
  4. * Copyright (c) 2015 Paul B Mahol
  5. * Copyright (c) 2015 James Darnley
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/qsort.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "removegrain.h"
  31. #include "video.h"
  32. #define OFFSET(x) offsetof(RemoveGrainContext, x)
  33. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  34. static const AVOption removegrain_options[] = {
  35. { "m0", "set mode for 1st plane", OFFSET(mode[0]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
  36. { "m1", "set mode for 2nd plane", OFFSET(mode[1]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
  37. { "m2", "set mode for 3rd plane", OFFSET(mode[2]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
  38. { "m3", "set mode for 4th plane", OFFSET(mode[3]), AV_OPT_TYPE_INT, {.i64=0}, 0, 24, FLAGS },
  39. {NULL}
  40. };
  41. AVFILTER_DEFINE_CLASS(removegrain);
  42. static int query_formats(AVFilterContext *ctx)
  43. {
  44. static const enum AVPixelFormat pix_fmts[] = {
  45. AV_PIX_FMT_GRAY8,
  46. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  47. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  48. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  49. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  50. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  51. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  52. AV_PIX_FMT_NONE
  53. };
  54. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  55. if (!fmts_list)
  56. return AVERROR(ENOMEM);
  57. return ff_set_common_formats(ctx, fmts_list);
  58. }
  59. #define REMOVE_GRAIN_SORT_AXIS \
  60. const int ma1 = FFMAX(a1, a8); \
  61. const int mi1 = FFMIN(a1, a8); \
  62. const int ma2 = FFMAX(a2, a7); \
  63. const int mi2 = FFMIN(a2, a7); \
  64. const int ma3 = FFMAX(a3, a6); \
  65. const int mi3 = FFMIN(a3, a6); \
  66. const int ma4 = FFMAX(a4, a5); \
  67. const int mi4 = FFMIN(a4, a5);
  68. static int mode01(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  69. {
  70. const int mi = FFMIN(FFMIN(FFMIN(a1, a2), FFMIN(a3, a4)), FFMIN(FFMIN(a5, a6), FFMIN(a7, a8)));
  71. const int ma = FFMAX(FFMAX(FFMAX(a1, a2), FFMAX(a3, a4)), FFMAX(FFMAX(a5, a6), FFMAX(a7, a8)));
  72. return av_clip(c, mi, ma);
  73. }
  74. static int cmp_int(const void *p1, const void *p2)
  75. {
  76. int left = *(const int *)p1;
  77. int right = *(const int *)p2;
  78. return FFDIFFSIGN(left, right);
  79. }
  80. static int mode02(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  81. {
  82. int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
  83. AV_QSORT(a, 8, int, cmp_int);
  84. return av_clip(c, a[2 - 1 ], a[7 - 1]);
  85. }
  86. static int mode03(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  87. {
  88. int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
  89. AV_QSORT(a, 8, int, cmp_int);
  90. return av_clip(c, a[3 - 1 ], a[6 - 1]);
  91. }
  92. static int mode04(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  93. {
  94. int a[8] = { a1, a2, a3, a4, a5, a6, a7, a8 };
  95. AV_QSORT(a, 8, int, cmp_int);
  96. return av_clip(c, a[4 - 1 ], a[5 - 1]);
  97. }
  98. static int mode05(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  99. {
  100. REMOVE_GRAIN_SORT_AXIS
  101. const int c1 = FFABS(c - av_clip(c, mi1, ma1));
  102. const int c2 = FFABS(c - av_clip(c, mi2, ma2));
  103. const int c3 = FFABS(c - av_clip(c, mi3, ma3));
  104. const int c4 = FFABS(c - av_clip(c, mi4, ma4));
  105. const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
  106. /* When adding SIMD notice the return order here: 4, 2, 3, 1. */
  107. if (mindiff == c4) {
  108. return av_clip(c, mi4, ma4);
  109. } else if (mindiff == c2) {
  110. return av_clip(c, mi2, ma2);
  111. } else if (mindiff == c3) {
  112. return av_clip(c, mi3, ma3);
  113. }
  114. return av_clip(c, mi1, ma1);
  115. }
  116. static int mode06(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  117. {
  118. REMOVE_GRAIN_SORT_AXIS
  119. const int d1 = ma1 - mi1;
  120. const int d2 = ma2 - mi2;
  121. const int d3 = ma3 - mi3;
  122. const int d4 = ma4 - mi4;
  123. const int cli1 = av_clip(c, mi1, ma1);
  124. const int cli2 = av_clip(c, mi2, ma2);
  125. const int cli3 = av_clip(c, mi3, ma3);
  126. const int cli4 = av_clip(c, mi4, ma4);
  127. const int c1 = av_clip_uint16((FFABS(c - cli1) << 1) + d1);
  128. const int c2 = av_clip_uint16((FFABS(c - cli2) << 1) + d2);
  129. const int c3 = av_clip_uint16((FFABS(c - cli3) << 1) + d3);
  130. const int c4 = av_clip_uint16((FFABS(c - cli4) << 1) + d4);
  131. const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
  132. if (mindiff == c4) {
  133. return cli4;
  134. } else if (mindiff == c2) {
  135. return cli2;
  136. } else if (mindiff == c3) {
  137. return cli3;
  138. }
  139. return cli1;
  140. }
  141. static int mode07(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  142. {
  143. REMOVE_GRAIN_SORT_AXIS
  144. const int d1 = ma1 - mi1;
  145. const int d2 = ma2 - mi2;
  146. const int d3 = ma3 - mi3;
  147. const int d4 = ma4 - mi4;
  148. const int cli1 = av_clip(c, mi1, ma1);
  149. const int cli2 = av_clip(c, mi2, ma2);
  150. const int cli3 = av_clip(c, mi3, ma3);
  151. const int cli4 = av_clip(c, mi4, ma4);
  152. const int c1 = FFABS(c - cli1) + d1;
  153. const int c2 = FFABS(c - cli2) + d2;
  154. const int c3 = FFABS(c - cli3) + d3;
  155. const int c4 = FFABS(c - cli4) + d4;
  156. const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
  157. if (mindiff == c4) {
  158. return cli4;
  159. } else if (mindiff == c2) {
  160. return cli2;
  161. } else if (mindiff == c3) {
  162. return cli3;
  163. }
  164. return cli1;
  165. }
  166. static int mode08(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  167. {
  168. REMOVE_GRAIN_SORT_AXIS
  169. const int d1 = ma1 - mi1;
  170. const int d2 = ma2 - mi2;
  171. const int d3 = ma3 - mi3;
  172. const int d4 = ma4 - mi4;
  173. const int cli1 = av_clip(c, mi1, ma1);
  174. const int cli2 = av_clip(c, mi2, ma2);
  175. const int cli3 = av_clip(c, mi3, ma3);
  176. const int cli4 = av_clip(c, mi4, ma4);
  177. const int c1 = av_clip_uint16(FFABS(c - cli1) + (d1 << 1));
  178. const int c2 = av_clip_uint16(FFABS(c - cli2) + (d2 << 1));
  179. const int c3 = av_clip_uint16(FFABS(c - cli3) + (d3 << 1));
  180. const int c4 = av_clip_uint16(FFABS(c - cli4) + (d4 << 1));
  181. const int mindiff = FFMIN(FFMIN(c1, c2), FFMIN(c3, c4));
  182. if (mindiff == c4) {
  183. return cli4;
  184. } else if (mindiff == c2) {
  185. return cli2;
  186. } else if (mindiff == c3) {
  187. return cli3;
  188. }
  189. return cli1;
  190. }
  191. static int mode09(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  192. {
  193. REMOVE_GRAIN_SORT_AXIS
  194. const int d1 = ma1 - mi1;
  195. const int d2 = ma2 - mi2;
  196. const int d3 = ma3 - mi3;
  197. const int d4 = ma4 - mi4;
  198. const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4));
  199. if (mindiff == d4) {
  200. return av_clip(c, mi4, ma4);
  201. } else if (mindiff == d2) {
  202. return av_clip(c, mi2, ma2);
  203. } else if (mindiff == d3) {
  204. return av_clip(c, mi3, ma3);
  205. }
  206. return av_clip(c, mi1, ma1);
  207. }
  208. static int mode10(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  209. {
  210. const int d1 = FFABS(c - a1);
  211. const int d2 = FFABS(c - a2);
  212. const int d3 = FFABS(c - a3);
  213. const int d4 = FFABS(c - a4);
  214. const int d5 = FFABS(c - a5);
  215. const int d6 = FFABS(c - a6);
  216. const int d7 = FFABS(c - a7);
  217. const int d8 = FFABS(c - a8);
  218. const int mindiff = FFMIN(FFMIN(FFMIN(d1, d2), FFMIN(d3, d4)),
  219. FFMIN(FFMIN(d5, d6), FFMIN(d7, d8)));
  220. if (mindiff == d7) return a7;
  221. if (mindiff == d8) return a8;
  222. if (mindiff == d6) return a6;
  223. if (mindiff == d2) return a2;
  224. if (mindiff == d3) return a3;
  225. if (mindiff == d1) return a1;
  226. if (mindiff == d5) return a5;
  227. return a4;
  228. }
  229. static int mode1112(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  230. {
  231. const int sum = 4 * c + 2 * (a2 + a4 + a5 + a7) + a1 + a3 + a6 + a8;
  232. const int val = (sum + 8) >> 4;
  233. return val;
  234. }
  235. static int mode1314(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  236. {
  237. const int d1 = FFABS(a1 - a8);
  238. const int d2 = FFABS(a2 - a7);
  239. const int d3 = FFABS(a3 - a6);
  240. const int mindiff = FFMIN(FFMIN(d1, d2), d3);
  241. if (mindiff == d2) {
  242. return (a2 + a7 + 1) >> 1;
  243. }
  244. if (mindiff == d3) {
  245. return (a3 + a6 + 1) >> 1;
  246. }
  247. return (a1 + a8 + 1) >> 1;
  248. }
  249. static int mode1516(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  250. {
  251. const int d1 = FFABS(a1 - a8);
  252. const int d2 = FFABS(a2 - a7);
  253. const int d3 = FFABS(a3 - a6);
  254. const int mindiff = FFMIN(FFMIN(d1, d2), d3);
  255. const int average = (2 * (a2 + a7) + a1 + a3 + a6 + a8 + 4) >> 3;
  256. if (mindiff == d2) {
  257. return av_clip(average, FFMIN(a2, a7), FFMAX(a2, a7));
  258. }
  259. if (mindiff == d3) {
  260. return av_clip(average, FFMIN(a3, a6), FFMAX(a3, a6));
  261. }
  262. return av_clip(average, FFMIN(a1, a8), FFMAX(a1, a8));
  263. }
  264. static int mode17(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  265. {
  266. REMOVE_GRAIN_SORT_AXIS
  267. const int l = FFMAX(FFMAX(mi1, mi2), FFMAX(mi3, mi4));
  268. const int u = FFMIN(FFMIN(ma1, ma2), FFMIN(ma3, ma4));
  269. return av_clip(c, FFMIN(l, u), FFMAX(l, u));
  270. }
  271. static int mode18(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  272. {
  273. const int d1 = FFMAX(FFABS(c - a1), FFABS(c - a8));
  274. const int d2 = FFMAX(FFABS(c - a2), FFABS(c - a7));
  275. const int d3 = FFMAX(FFABS(c - a3), FFABS(c - a6));
  276. const int d4 = FFMAX(FFABS(c - a4), FFABS(c - a5));
  277. const int mindiff = FFMIN(FFMIN(d1, d2), FFMIN(d3, d4));
  278. if (mindiff == d4) {
  279. return av_clip(c, FFMIN(a4, a5), FFMAX(a4, a5));
  280. }
  281. if (mindiff == d2) {
  282. return av_clip(c, FFMIN(a2, a7), FFMAX(a2, a7));
  283. }
  284. if (mindiff == d3) {
  285. return av_clip(c, FFMIN(a3, a6), FFMAX(a3, a6));
  286. }
  287. return av_clip(c, FFMIN(a1, a8), FFMAX(a1, a8));
  288. }
  289. static int mode19(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  290. {
  291. const int sum = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
  292. const int val = (sum + 4) >> 3;
  293. return val;
  294. }
  295. static int mode20(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  296. {
  297. const int sum = a1 + a2 + a3 + a4 + c + a5 + a6 + a7 + a8;
  298. const int val = (sum + 4) / 9;
  299. return val;
  300. }
  301. static int mode21(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  302. {
  303. const int l1l = (a1 + a8) >> 1;
  304. const int l2l = (a2 + a7) >> 1;
  305. const int l3l = (a3 + a6) >> 1;
  306. const int l4l = (a4 + a5) >> 1;
  307. const int l1h = (a1 + a8 + 1) >> 1;
  308. const int l2h = (a2 + a7 + 1) >> 1;
  309. const int l3h = (a3 + a6 + 1) >> 1;
  310. const int l4h = (a4 + a5 + 1) >> 1;
  311. const int mi = FFMIN(FFMIN(l1l, l2l), FFMIN(l3l, l4l));
  312. const int ma = FFMAX(FFMAX(l1h, l2h), FFMAX(l3h, l4h));
  313. return av_clip(c, mi, ma);
  314. }
  315. static int mode22(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  316. {
  317. const int l1 = (a1 + a8 + 1) >> 1;
  318. const int l2 = (a2 + a7 + 1) >> 1;
  319. const int l3 = (a3 + a6 + 1) >> 1;
  320. const int l4 = (a4 + a5 + 1) >> 1;
  321. const int mi = FFMIN(FFMIN(l1, l2), FFMIN(l3, l4));
  322. const int ma = FFMAX(FFMAX(l1, l2), FFMAX(l3, l4));
  323. return av_clip(c, mi, ma);
  324. }
  325. static int mode23(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  326. {
  327. REMOVE_GRAIN_SORT_AXIS
  328. const int linediff1 = ma1 - mi1;
  329. const int linediff2 = ma2 - mi2;
  330. const int linediff3 = ma3 - mi3;
  331. const int linediff4 = ma4 - mi4;
  332. const int u1 = FFMIN(c - ma1, linediff1);
  333. const int u2 = FFMIN(c - ma2, linediff2);
  334. const int u3 = FFMIN(c - ma3, linediff3);
  335. const int u4 = FFMIN(c - ma4, linediff4);
  336. const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0);
  337. const int d1 = FFMIN(mi1 - c, linediff1);
  338. const int d2 = FFMIN(mi2 - c, linediff2);
  339. const int d3 = FFMIN(mi3 - c, linediff3);
  340. const int d4 = FFMIN(mi4 - c, linediff4);
  341. const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0);
  342. return c - u + d; // This probably will never overflow.
  343. }
  344. static int mode24(int c, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  345. {
  346. REMOVE_GRAIN_SORT_AXIS
  347. const int linediff1 = ma1 - mi1;
  348. const int linediff2 = ma2 - mi2;
  349. const int linediff3 = ma3 - mi3;
  350. const int linediff4 = ma4 - mi4;
  351. const int tu1 = c - ma1;
  352. const int tu2 = c - ma2;
  353. const int tu3 = c - ma3;
  354. const int tu4 = c - ma4;
  355. const int u1 = FFMIN(tu1, linediff1 - tu1);
  356. const int u2 = FFMIN(tu2, linediff2 - tu2);
  357. const int u3 = FFMIN(tu3, linediff3 - tu3);
  358. const int u4 = FFMIN(tu4, linediff4 - tu4);
  359. const int u = FFMAX(FFMAX(FFMAX(u1, u2), FFMAX(u3, u4)), 0);
  360. const int td1 = mi1 - c;
  361. const int td2 = mi2 - c;
  362. const int td3 = mi3 - c;
  363. const int td4 = mi4 - c;
  364. const int d1 = FFMIN(td1, linediff1 - td1);
  365. const int d2 = FFMIN(td2, linediff2 - td2);
  366. const int d3 = FFMIN(td3, linediff3 - td3);
  367. const int d4 = FFMIN(td4, linediff4 - td4);
  368. const int d = FFMAX(FFMAX(FFMAX(d1, d2), FFMAX(d3, d4)), 0);
  369. return c - u + d; // This probably will never overflow.
  370. }
  371. static int config_input(AVFilterLink *inlink)
  372. {
  373. RemoveGrainContext *s = inlink->dst->priv;
  374. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  375. int i;
  376. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  377. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  378. s->planeheight[0] = s->planeheight[3] = inlink->h;
  379. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  380. s->planewidth[0] = s->planewidth[3] = inlink->w;
  381. for (i = 0; i < s->nb_planes; i++) {
  382. switch (s->mode[i]) {
  383. case 1: s->rg[i] = mode01; break;
  384. case 2: s->rg[i] = mode02; break;
  385. case 3: s->rg[i] = mode03; break;
  386. case 4: s->rg[i] = mode04; break;
  387. case 5: s->rg[i] = mode05; break;
  388. case 6: s->rg[i] = mode06; break;
  389. case 7: s->rg[i] = mode07; break;
  390. case 8: s->rg[i] = mode08; break;
  391. case 9: s->rg[i] = mode09; break;
  392. case 10: s->rg[i] = mode10; break;
  393. case 11: s->rg[i] = mode1112; break;
  394. case 12: s->rg[i] = mode1112; break;
  395. case 13: s->skip_odd = 1;
  396. s->rg[i] = mode1314; break;
  397. case 14: s->skip_even = 1;
  398. s->rg[i] = mode1314; break;
  399. case 15: s->skip_odd = 1;
  400. s->rg[i] = mode1516; break;
  401. case 16: s->skip_even = 1;
  402. s->rg[i] = mode1516; break;
  403. case 17: s->rg[i] = mode17; break;
  404. case 18: s->rg[i] = mode18; break;
  405. case 19: s->rg[i] = mode19; break;
  406. case 20: s->rg[i] = mode20; break;
  407. case 21: s->rg[i] = mode21; break;
  408. case 22: s->rg[i] = mode22; break;
  409. case 23: s->rg[i] = mode23; break;
  410. case 24: s->rg[i] = mode24; break;
  411. }
  412. }
  413. if (ARCH_X86)
  414. ff_removegrain_init_x86(s);
  415. return 0;
  416. }
  417. typedef struct ThreadData {
  418. AVFrame *in, *out;
  419. int plane;
  420. } ThreadData;
  421. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  422. {
  423. RemoveGrainContext *s = ctx->priv;
  424. ThreadData *td = arg;
  425. AVFrame *in = td->in;
  426. AVFrame *out = td->out;
  427. const int i = td->plane;
  428. const int height = s->planeheight[i];
  429. const int om = in->linesize[i] - 1;
  430. const int o0 = in->linesize[i] ;
  431. const int op = in->linesize[i] + 1;
  432. int start = (height * jobnr ) / nb_jobs;
  433. int end = (height * (jobnr+1)) / nb_jobs;
  434. int x, y;
  435. start = FFMAX(1, start);
  436. end = FFMIN(height-1, end);
  437. for (y = start; y < end; y++) {
  438. uint8_t *dst = out->data[i];
  439. uint8_t *src = in->data[i];
  440. src = in->data[i] + y * in->linesize[i];
  441. dst = out->data[i] + y * out->linesize[i];
  442. if (s->skip_even && !(y & 1)) {
  443. memcpy(dst, src, s->planewidth[i]);
  444. continue;
  445. }
  446. if (s->skip_odd && y & 1) {
  447. memcpy(dst, src, s->planewidth[i]);
  448. continue;
  449. }
  450. *dst++ = *src++;
  451. if (s->fl[i]) {
  452. int w_asm = (s->planewidth[i] - 2) & ~15;
  453. s->fl[i](dst, src, in->linesize[i], w_asm);
  454. x = 1 + w_asm;
  455. dst += w_asm;
  456. src += w_asm;
  457. } else
  458. x = 1;
  459. for (; x < s->planewidth[i] - 1; x++) {
  460. const int a1 = src[-op];
  461. const int a2 = src[-o0];
  462. const int a3 = src[-om];
  463. const int a4 = src[-1 ];
  464. const int c = src[ 0 ];
  465. const int a5 = src[ 1 ];
  466. const int a6 = src[ om];
  467. const int a7 = src[ o0];
  468. const int a8 = src[ op];
  469. const int res = s->rg[i](c, a1, a2, a3, a4, a5, a6, a7, a8);
  470. *dst = res;
  471. dst++, src++;
  472. }
  473. dst[0] = src[0];
  474. }
  475. return 0;
  476. }
  477. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  478. {
  479. AVFilterContext *ctx = inlink->dst;
  480. AVFilterLink *outlink = ctx->outputs[0];
  481. RemoveGrainContext *s = ctx->priv;
  482. ThreadData td;
  483. AVFrame *out;
  484. int i;
  485. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  486. if (!out) {
  487. av_frame_free(&in);
  488. return AVERROR(ENOMEM);
  489. }
  490. av_frame_copy_props(out, in);
  491. for (i = 0; i < s->nb_planes; i++) {
  492. uint8_t *dst = out->data[i];
  493. uint8_t *src = in->data[i];
  494. if (s->mode[i] == 0) {
  495. av_image_copy_plane(dst, out->linesize[i],
  496. src, in->linesize[i],
  497. s->planewidth[i], s->planeheight[i]);
  498. continue;
  499. }
  500. memcpy(dst, src, s->planewidth[i]);
  501. td.in = in; td.out = out; td.plane = i;
  502. ctx->internal->execute(ctx, filter_slice, &td, NULL,
  503. FFMIN(s->planeheight[i], ctx->graph->nb_threads));
  504. src = in->data[i] + (s->planeheight[i] - 1) * in->linesize[i];
  505. dst = out->data[i] + (s->planeheight[i] - 1) * out->linesize[i];
  506. memcpy(dst, src, s->planewidth[i]);
  507. }
  508. av_frame_free(&in);
  509. return ff_filter_frame(outlink, out);
  510. }
  511. static const AVFilterPad removegrain_inputs[] = {
  512. {
  513. .name = "default",
  514. .type = AVMEDIA_TYPE_VIDEO,
  515. .filter_frame = filter_frame,
  516. .config_props = config_input,
  517. },
  518. { NULL }
  519. };
  520. static const AVFilterPad removegrain_outputs[] = {
  521. {
  522. .name = "default",
  523. .type = AVMEDIA_TYPE_VIDEO,
  524. },
  525. { NULL }
  526. };
  527. AVFilter ff_vf_removegrain = {
  528. .name = "removegrain",
  529. .description = NULL_IF_CONFIG_SMALL("Remove grain."),
  530. .priv_size = sizeof(RemoveGrainContext),
  531. .query_formats = query_formats,
  532. .inputs = removegrain_inputs,
  533. .outputs = removegrain_outputs,
  534. .priv_class = &removegrain_class,
  535. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  536. };