vf_dedot.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "avfilter.h"
  23. #include "filters.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. typedef struct DedotContext {
  27. const AVClass *class;
  28. int m;
  29. float lt;
  30. float tl;
  31. float tc;
  32. float ct;
  33. const AVPixFmtDescriptor *desc;
  34. int depth;
  35. int max;
  36. int luma2d;
  37. int lumaT;
  38. int chromaT1;
  39. int chromaT2;
  40. int eof;
  41. int eof_frames;
  42. int nb_planes;
  43. int planewidth[4];
  44. int planeheight[4];
  45. AVFrame *frames[5];
  46. int (*dedotcrawl)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  47. int (*derainbow)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  48. } DedotContext;
  49. static const enum AVPixelFormat pixel_fmts[] = {
  50. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  51. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  52. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  53. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  54. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  55. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  56. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  57. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  58. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  59. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  60. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  61. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  62. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  63. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  64. AV_PIX_FMT_NONE
  65. };
  66. #define DEFINE_DEDOTCRAWL(name, type, div) \
  67. static int dedotcrawl##name(AVFilterContext *ctx, void *arg, \
  68. int jobnr, int nb_jobs) \
  69. { \
  70. DedotContext *s = ctx->priv; \
  71. AVFrame *out = arg; \
  72. int src_linesize = s->frames[2]->linesize[0] / div; \
  73. int dst_linesize = out->linesize[0] / div; \
  74. int p0_linesize = s->frames[0]->linesize[0] / div; \
  75. int p1_linesize = s->frames[1]->linesize[0] / div; \
  76. int p3_linesize = s->frames[3]->linesize[0] / div; \
  77. int p4_linesize = s->frames[4]->linesize[0] / div; \
  78. const int h = s->planeheight[0]; \
  79. int slice_start = (h * jobnr) / nb_jobs; \
  80. int slice_end = (h * (jobnr+1)) / nb_jobs; \
  81. type *p0 = (type *)s->frames[0]->data[0]; \
  82. type *p1 = (type *)s->frames[1]->data[0]; \
  83. type *p3 = (type *)s->frames[3]->data[0]; \
  84. type *p4 = (type *)s->frames[4]->data[0]; \
  85. type *src = (type *)s->frames[2]->data[0]; \
  86. type *dst = (type *)out->data[0]; \
  87. const int luma2d = s->luma2d; \
  88. const int lumaT = s->lumaT; \
  89. \
  90. if (!slice_start) { \
  91. slice_start++; \
  92. } \
  93. p0 += p0_linesize * slice_start; \
  94. p1 += p1_linesize * slice_start; \
  95. p3 += p3_linesize * slice_start; \
  96. p4 += p4_linesize * slice_start; \
  97. src += src_linesize * slice_start; \
  98. dst += dst_linesize * slice_start; \
  99. if (slice_end == h) { \
  100. slice_end--; \
  101. } \
  102. for (int y = slice_start; y < slice_end; y++) { \
  103. for (int x = 1; x < s->planewidth[0] - 1; x++) { \
  104. int above = src[x - src_linesize]; \
  105. int below = src[x + src_linesize]; \
  106. int cur = src[x]; \
  107. int left = src[x - 1]; \
  108. int right = src[x + 1]; \
  109. \
  110. if (FFABS(above + below - 2 * cur) <= luma2d && \
  111. FFABS(left + right - 2 * cur) <= luma2d) \
  112. continue; \
  113. \
  114. if (FFABS(cur - p0[x]) <= lumaT && \
  115. FFABS(cur - p4[x]) <= lumaT && \
  116. FFABS(p1[x] - p3[x]) <= lumaT) { \
  117. int diff1 = FFABS(cur - p1[x]); \
  118. int diff2 = FFABS(cur - p3[x]); \
  119. \
  120. if (diff1 < diff2) \
  121. dst[x] = (src[x] + p1[x] + 1) >> 1; \
  122. else \
  123. dst[x] = (src[x] + p3[x] + 1) >> 1; \
  124. } \
  125. } \
  126. \
  127. dst += dst_linesize; \
  128. src += src_linesize; \
  129. p0 += p0_linesize; \
  130. p1 += p1_linesize; \
  131. p3 += p3_linesize; \
  132. p4 += p4_linesize; \
  133. } \
  134. return 0; \
  135. }
  136. DEFINE_DEDOTCRAWL(8, uint8_t, 1)
  137. DEFINE_DEDOTCRAWL(16, uint16_t, 2)
  138. typedef struct ThreadData {
  139. AVFrame *out;
  140. int plane;
  141. } ThreadData;
  142. #define DEFINE_DERAINBOW(name, type, div) \
  143. static int derainbow##name(AVFilterContext *ctx, void *arg, \
  144. int jobnr, int nb_jobs) \
  145. { \
  146. DedotContext *s = ctx->priv; \
  147. ThreadData *td = arg; \
  148. AVFrame *out = td->out; \
  149. const int plane = td->plane; \
  150. const int h = s->planeheight[plane]; \
  151. int slice_start = (h * jobnr) / nb_jobs; \
  152. int slice_end = (h * (jobnr+1)) / nb_jobs; \
  153. int src_linesize = s->frames[2]->linesize[plane] / div; \
  154. int dst_linesize = out->linesize[plane] / div; \
  155. int p0_linesize = s->frames[0]->linesize[plane] / div; \
  156. int p1_linesize = s->frames[1]->linesize[plane] / div; \
  157. int p3_linesize = s->frames[3]->linesize[plane] / div; \
  158. int p4_linesize = s->frames[4]->linesize[plane] / div; \
  159. type *p0 = (type *)s->frames[0]->data[plane]; \
  160. type *p1 = (type *)s->frames[1]->data[plane]; \
  161. type *p3 = (type *)s->frames[3]->data[plane]; \
  162. type *p4 = (type *)s->frames[4]->data[plane]; \
  163. type *src = (type *)s->frames[2]->data[plane]; \
  164. type *dst = (type *)out->data[plane]; \
  165. const int chromaT1 = s->chromaT1; \
  166. const int chromaT2 = s->chromaT2; \
  167. \
  168. p0 += slice_start * p0_linesize; \
  169. p1 += slice_start * p1_linesize; \
  170. p3 += slice_start * p3_linesize; \
  171. p4 += slice_start * p4_linesize; \
  172. src += slice_start * src_linesize; \
  173. dst += slice_start * dst_linesize; \
  174. for (int y = slice_start; y < slice_end; y++) { \
  175. for (int x = 0; x < s->planewidth[plane]; x++) { \
  176. int cur = src[x]; \
  177. \
  178. if (FFABS(cur - p0[x]) <= chromaT1 && \
  179. FFABS(cur - p4[x]) <= chromaT1 && \
  180. FFABS(p1[x] - p3[x]) <= chromaT1 && \
  181. FFABS(cur - p1[x]) > chromaT2 && \
  182. FFABS(cur - p3[x]) > chromaT2) { \
  183. int diff1 = FFABS(cur - p1[x]); \
  184. int diff2 = FFABS(cur - p3[x]); \
  185. \
  186. if (diff1 < diff2) \
  187. dst[x] = (src[x] + p1[x] + 1) >> 1; \
  188. else \
  189. dst[x] = (src[x] + p3[x] + 1) >> 1; \
  190. } \
  191. } \
  192. \
  193. dst += dst_linesize; \
  194. src += src_linesize; \
  195. p0 += p0_linesize; \
  196. p1 += p1_linesize; \
  197. p3 += p3_linesize; \
  198. p4 += p4_linesize; \
  199. } \
  200. return 0; \
  201. }
  202. DEFINE_DERAINBOW(8, uint8_t, 1)
  203. DEFINE_DERAINBOW(16, uint16_t, 2)
  204. static int config_output(AVFilterLink *outlink)
  205. {
  206. AVFilterContext *ctx = outlink->src;
  207. DedotContext *s = ctx->priv;
  208. AVFilterLink *inlink = ctx->inputs[0];
  209. s->desc = av_pix_fmt_desc_get(outlink->format);
  210. if (!s->desc)
  211. return AVERROR_BUG;
  212. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  213. s->depth = s->desc->comp[0].depth;
  214. s->max = (1 << s->depth) - 1;
  215. s->luma2d = s->lt * s->max;
  216. s->lumaT = s->tl * s->max;
  217. s->chromaT1 = s->tc * s->max;
  218. s->chromaT2 = s->ct * s->max;
  219. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  220. s->planewidth[0] = s->planewidth[3] = inlink->w;
  221. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  222. s->planeheight[0] = s->planeheight[3] = inlink->h;
  223. if (s->depth <= 8) {
  224. s->dedotcrawl = dedotcrawl8;
  225. s->derainbow = derainbow8;
  226. } else {
  227. s->dedotcrawl = dedotcrawl16;
  228. s->derainbow = derainbow16;
  229. }
  230. return 0;
  231. }
  232. static int activate(AVFilterContext *ctx)
  233. {
  234. AVFilterLink *inlink = ctx->inputs[0];
  235. AVFilterLink *outlink = ctx->outputs[0];
  236. DedotContext *s = ctx->priv;
  237. AVFrame *frame = NULL;
  238. int64_t pts;
  239. int status;
  240. int ret = 0;
  241. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  242. if (s->eof == 0) {
  243. ret = ff_inlink_consume_frame(inlink, &frame);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. if (frame || s->eof_frames > 0) {
  248. AVFrame *out = NULL;
  249. if (frame) {
  250. for (int i = 2; i < 5; i++) {
  251. if (!s->frames[i])
  252. s->frames[i] = av_frame_clone(frame);
  253. }
  254. av_frame_free(&frame);
  255. } else if (s->frames[3]) {
  256. s->eof_frames--;
  257. s->frames[4] = av_frame_clone(s->frames[3]);
  258. }
  259. if (s->frames[0] &&
  260. s->frames[1] &&
  261. s->frames[2] &&
  262. s->frames[3] &&
  263. s->frames[4]) {
  264. out = av_frame_clone(s->frames[2]);
  265. if (out && !ctx->is_disabled) {
  266. ret = ff_inlink_make_frame_writable(inlink, &out);
  267. if (ret >= 0) {
  268. if (s->m & 1)
  269. ff_filter_execute(ctx, s->dedotcrawl, out, NULL,
  270. FFMIN(ff_filter_get_nb_threads(ctx),
  271. s->planeheight[0]));
  272. if (s->m & 2) {
  273. ThreadData td;
  274. td.out = out; td.plane = 1;
  275. ff_filter_execute(ctx, s->derainbow, &td, NULL,
  276. FFMIN(ff_filter_get_nb_threads(ctx),
  277. s->planeheight[1]));
  278. td.plane = 2;
  279. ff_filter_execute(ctx, s->derainbow, &td, NULL,
  280. FFMIN(ff_filter_get_nb_threads(ctx),
  281. s->planeheight[2]));
  282. }
  283. } else
  284. av_frame_free(&out);
  285. } else if (!out) {
  286. ret = AVERROR(ENOMEM);
  287. }
  288. }
  289. av_frame_free(&s->frames[0]);
  290. s->frames[0] = s->frames[1];
  291. s->frames[1] = s->frames[2];
  292. s->frames[2] = s->frames[3];
  293. s->frames[3] = s->frames[4];
  294. s->frames[4] = NULL;
  295. if (ret < 0)
  296. return ret;
  297. if (out)
  298. return ff_filter_frame(outlink, out);
  299. }
  300. if (s->eof) {
  301. if (s->eof_frames <= 0) {
  302. ff_outlink_set_status(outlink, AVERROR_EOF, s->frames[2]->pts);
  303. } else {
  304. ff_filter_set_ready(ctx, 10);
  305. }
  306. return 0;
  307. }
  308. if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  309. if (status == AVERROR_EOF) {
  310. s->eof = 1;
  311. s->eof_frames = !!s->frames[0] + !!s->frames[1];
  312. if (s->eof_frames <= 0) {
  313. ff_outlink_set_status(outlink, AVERROR_EOF, pts);
  314. return 0;
  315. }
  316. ff_filter_set_ready(ctx, 10);
  317. return 0;
  318. }
  319. }
  320. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  321. return FFERROR_NOT_READY;
  322. }
  323. static av_cold void uninit(AVFilterContext *ctx)
  324. {
  325. DedotContext *s = ctx->priv;
  326. for (int i = 0; i < 5; i++)
  327. av_frame_free(&s->frames[i]);
  328. }
  329. #define OFFSET(x) offsetof(DedotContext, x)
  330. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  331. static const AVOption dedot_options[] = {
  332. { "m", "set filtering mode", OFFSET( m), AV_OPT_TYPE_FLAGS, {.i64=3}, 0, 3, FLAGS, "m" },
  333. { "dotcrawl", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "m" },
  334. { "rainbows", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "m" },
  335. { "lt", "set spatial luma threshold", OFFSET(lt), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
  336. { "tl", "set tolerance for temporal luma", OFFSET(tl), AV_OPT_TYPE_FLOAT, {.dbl=.079}, 0, 1, FLAGS },
  337. { "tc", "set tolerance for chroma temporal variation", OFFSET(tc), AV_OPT_TYPE_FLOAT, {.dbl=.058}, 0, 1, FLAGS },
  338. { "ct", "set temporal chroma threshold", OFFSET(ct), AV_OPT_TYPE_FLOAT, {.dbl=.019}, 0, 1, FLAGS },
  339. { NULL },
  340. };
  341. static const AVFilterPad outputs[] = {
  342. {
  343. .name = "default",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. .config_props = config_output,
  346. },
  347. };
  348. AVFILTER_DEFINE_CLASS(dedot);
  349. const AVFilter ff_vf_dedot = {
  350. .name = "dedot",
  351. .description = NULL_IF_CONFIG_SMALL("Reduce cross-luminance and cross-color."),
  352. .priv_size = sizeof(DedotContext),
  353. .priv_class = &dedot_class,
  354. .activate = activate,
  355. .uninit = uninit,
  356. FILTER_INPUTS(ff_video_default_filterpad),
  357. FILTER_OUTPUTS(outputs),
  358. FILTER_PIXFMTS_ARRAY(pixel_fmts),
  359. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  360. };