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.

300 lines
9.9KB

  1. /*
  2. * Copyright (c) 2020 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/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "framesync.h"
  28. typedef struct MaskedThresholdContext {
  29. const AVClass *class;
  30. int threshold;
  31. int planes;
  32. int linesize[4];
  33. int planewidth[4], planeheight[4];
  34. int nb_planes;
  35. int depth;
  36. FFFrameSync fs;
  37. void (*maskedthreshold)(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w);
  38. } MaskedThresholdContext;
  39. #define OFFSET(x) offsetof(MaskedThresholdContext, x)
  40. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  41. typedef struct ThreadData {
  42. AVFrame *src, *ref, *dst;
  43. } ThreadData;
  44. static const AVOption maskedthreshold_options[] = {
  45. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=1}, 0, UINT16_MAX, FLAGS },
  46. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  47. { NULL }
  48. };
  49. static int query_formats(AVFilterContext *ctx)
  50. {
  51. static const enum AVPixelFormat pix_fmts[] = {
  52. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  53. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  54. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  55. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  56. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  57. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  58. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  59. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  60. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  61. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  62. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  63. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  64. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  65. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  66. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  67. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  68. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  69. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  70. AV_PIX_FMT_NONE
  71. };
  72. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  73. }
  74. static void threshold8(const uint8_t *src, const uint8_t *ref, uint8_t *dst, int threshold, int w)
  75. {
  76. for (int x = 0; x < w; x++)
  77. dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
  78. }
  79. static void threshold16(const uint8_t *ssrc, const uint8_t *rref, uint8_t *ddst, int threshold, int w)
  80. {
  81. const uint16_t *src = (const uint16_t *)ssrc;
  82. const uint16_t *ref = (const uint16_t *)rref;
  83. uint16_t *dst = (uint16_t *)ddst;
  84. for (int x = 0; x < w; x++)
  85. dst[x] = FFABS(src[x] - ref[x]) <= threshold ? src[x] : ref[x];
  86. }
  87. static int config_input(AVFilterLink *inlink)
  88. {
  89. AVFilterContext *ctx = inlink->dst;
  90. MaskedThresholdContext *s = ctx->priv;
  91. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  92. int vsub, hsub, ret;
  93. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  94. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  95. return ret;
  96. hsub = desc->log2_chroma_w;
  97. vsub = desc->log2_chroma_h;
  98. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  99. s->planeheight[0] = s->planeheight[3] = inlink->h;
  100. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  101. s->planewidth[0] = s->planewidth[3] = inlink->w;
  102. s->depth = desc->comp[0].depth;
  103. if (desc->comp[0].depth == 8)
  104. s->maskedthreshold = threshold8;
  105. else
  106. s->maskedthreshold = threshold16;
  107. return 0;
  108. }
  109. static int threshold_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  110. {
  111. MaskedThresholdContext *s = ctx->priv;
  112. const int threshold = s->threshold;
  113. ThreadData *td = arg;
  114. for (int p = 0; p < s->nb_planes; p++) {
  115. const ptrdiff_t src_linesize = td->src->linesize[p];
  116. const ptrdiff_t ref_linesize = td->ref->linesize[p];
  117. const ptrdiff_t dst_linesize = td->dst->linesize[p];
  118. const int w = s->planewidth[p];
  119. const int h = s->planeheight[p];
  120. const int slice_start = (h * jobnr) / nb_jobs;
  121. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  122. const uint8_t *src = td->src->data[p] + slice_start * src_linesize;
  123. const uint8_t *ref = td->ref->data[p] + slice_start * ref_linesize;
  124. uint8_t *dst = td->dst->data[p] + slice_start * dst_linesize;
  125. if (!((1 << p) & s->planes)) {
  126. av_image_copy_plane(dst, dst_linesize, ref, ref_linesize,
  127. s->linesize[p], slice_end - slice_start);
  128. continue;
  129. }
  130. for (int y = slice_start; y < slice_end; y++) {
  131. s->maskedthreshold(src, ref, dst, threshold, w);
  132. dst += dst_linesize;
  133. src += src_linesize;
  134. ref += ref_linesize;
  135. }
  136. }
  137. return 0;
  138. }
  139. static int process_frame(FFFrameSync *fs)
  140. {
  141. AVFilterContext *ctx = fs->parent;
  142. MaskedThresholdContext *s = fs->opaque;
  143. AVFilterLink *outlink = ctx->outputs[0];
  144. AVFrame *out, *src, *ref;
  145. int ret;
  146. if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 ||
  147. (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0)
  148. return ret;
  149. if (ctx->is_disabled) {
  150. out = av_frame_clone(src);
  151. if (!out)
  152. return AVERROR(ENOMEM);
  153. } else {
  154. ThreadData td;
  155. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  156. if (!out)
  157. return AVERROR(ENOMEM);
  158. av_frame_copy_props(out, src);
  159. td.src = src;
  160. td.ref = ref;
  161. td.dst = out;
  162. ctx->internal->execute(ctx, threshold_slice, &td, NULL, FFMIN(s->planeheight[2],
  163. ff_filter_get_nb_threads(ctx)));
  164. }
  165. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  166. return ff_filter_frame(outlink, out);
  167. }
  168. static int config_output(AVFilterLink *outlink)
  169. {
  170. AVFilterContext *ctx = outlink->src;
  171. MaskedThresholdContext *s = ctx->priv;
  172. AVFilterLink *source = ctx->inputs[0];
  173. AVFilterLink *ref = ctx->inputs[1];
  174. FFFrameSyncIn *in;
  175. int ret;
  176. if (source->format != ref->format) {
  177. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  178. return AVERROR(EINVAL);
  179. }
  180. if (source->w != ref->w || source->h != ref->h) {
  181. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  182. "(size %dx%d) do not match the corresponding "
  183. "second input link %s parameters (%dx%d)\n",
  184. ctx->input_pads[0].name, source->w, source->h,
  185. ctx->input_pads[1].name, ref->w, ref->h);
  186. return AVERROR(EINVAL);
  187. }
  188. outlink->w = source->w;
  189. outlink->h = source->h;
  190. outlink->sample_aspect_ratio = source->sample_aspect_ratio;
  191. outlink->frame_rate = source->frame_rate;
  192. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  193. return ret;
  194. in = s->fs.in;
  195. in[0].time_base = source->time_base;
  196. in[1].time_base = ref->time_base;
  197. in[0].sync = 1;
  198. in[0].before = EXT_STOP;
  199. in[0].after = EXT_INFINITY;
  200. in[1].sync = 1;
  201. in[1].before = EXT_STOP;
  202. in[1].after = EXT_INFINITY;
  203. s->fs.opaque = s;
  204. s->fs.on_event = process_frame;
  205. ret = ff_framesync_configure(&s->fs);
  206. outlink->time_base = s->fs.time_base;
  207. return ret;
  208. }
  209. static int activate(AVFilterContext *ctx)
  210. {
  211. MaskedThresholdContext *s = ctx->priv;
  212. return ff_framesync_activate(&s->fs);
  213. }
  214. static av_cold void uninit(AVFilterContext *ctx)
  215. {
  216. MaskedThresholdContext *s = ctx->priv;
  217. ff_framesync_uninit(&s->fs);
  218. }
  219. static const AVFilterPad maskedthreshold_inputs[] = {
  220. {
  221. .name = "source",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .config_props = config_input,
  224. },
  225. {
  226. .name = "reference",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. },
  229. { NULL }
  230. };
  231. static const AVFilterPad maskedthreshold_outputs[] = {
  232. {
  233. .name = "default",
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .config_props = config_output,
  236. },
  237. { NULL }
  238. };
  239. AVFILTER_DEFINE_CLASS(maskedthreshold);
  240. AVFilter ff_vf_maskedthreshold = {
  241. .name = "maskedthreshold",
  242. .description = NULL_IF_CONFIG_SMALL("Pick pixels comparing absolute difference of two streams with threshold."),
  243. .priv_class = &maskedthreshold_class,
  244. .priv_size = sizeof(MaskedThresholdContext),
  245. .uninit = uninit,
  246. .activate = activate,
  247. .query_formats = query_formats,
  248. .inputs = maskedthreshold_inputs,
  249. .outputs = maskedthreshold_outputs,
  250. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  251. .process_command = ff_filter_process_command,
  252. };