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.

416 lines
17KB

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