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.

329 lines
10KB

  1. /*
  2. * Copyright (c) 2016 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. /**
  21. * @file
  22. * threshold video filter
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "framesync.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #include "threshold.h"
  33. #define OFFSET(x) offsetof(ThresholdContext, x)
  34. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  35. static const AVOption threshold_options[] = {
  36. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
  37. { NULL }
  38. };
  39. AVFILTER_DEFINE_CLASS(threshold);
  40. static int query_formats(AVFilterContext *ctx)
  41. {
  42. static const enum AVPixelFormat pix_fmts[] = {
  43. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  44. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  45. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  46. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  47. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  48. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  49. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  50. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  51. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  52. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  53. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  54. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  55. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  56. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12 , AV_PIX_FMT_GBRAP16,
  57. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
  58. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  59. AV_PIX_FMT_NONE
  60. };
  61. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  62. }
  63. static int process_frame(FFFrameSync *fs)
  64. {
  65. AVFilterContext *ctx = fs->parent;
  66. ThresholdContext *s = fs->opaque;
  67. AVFilterLink *outlink = ctx->outputs[0];
  68. AVFrame *out, *in, *threshold, *min, *max;
  69. int ret;
  70. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  71. (ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 ||
  72. (ret = ff_framesync_get_frame(&s->fs, 2, &min, 0)) < 0 ||
  73. (ret = ff_framesync_get_frame(&s->fs, 3, &max, 0)) < 0)
  74. return ret;
  75. if (ctx->is_disabled) {
  76. out = av_frame_clone(in);
  77. if (!out)
  78. return AVERROR(ENOMEM);
  79. } else {
  80. int p;
  81. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  82. if (!out)
  83. return AVERROR(ENOMEM);
  84. av_frame_copy_props(out, in);
  85. for (p = 0; p < s->nb_planes; p++) {
  86. if (!(s->planes & (1 << p))) {
  87. av_image_copy_plane(out->data[p], out->linesize[p],
  88. in->data[p], in->linesize[p],
  89. s->width[p] * s->bpc,
  90. s->height[p]);
  91. continue;
  92. }
  93. s->threshold(in->data[p], threshold->data[p],
  94. min->data[p], max->data[p],
  95. out->data[p],
  96. in->linesize[p], threshold->linesize[p],
  97. min->linesize[p], max->linesize[p],
  98. out->linesize[p],
  99. s->width[p], s->height[p]);
  100. }
  101. }
  102. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  103. return ff_filter_frame(outlink, out);
  104. }
  105. static void threshold8(const uint8_t *in, const uint8_t *threshold,
  106. const uint8_t *min, const uint8_t *max,
  107. uint8_t *out,
  108. ptrdiff_t ilinesize, ptrdiff_t tlinesize,
  109. ptrdiff_t flinesize, ptrdiff_t slinesize,
  110. ptrdiff_t olinesize,
  111. int w, int h)
  112. {
  113. int x, y;
  114. for (y = 0; y < h; y++) {
  115. for (x = 0; x < w; x++) {
  116. out[x] = in[x] < threshold[x] ? min[x] : max[x];
  117. }
  118. in += ilinesize;
  119. threshold += tlinesize;
  120. min += flinesize;
  121. max += slinesize;
  122. out += olinesize;
  123. }
  124. }
  125. static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
  126. const uint8_t *ffirst, const uint8_t *ssecond,
  127. uint8_t *oout,
  128. ptrdiff_t ilinesize, ptrdiff_t tlinesize,
  129. ptrdiff_t flinesize, ptrdiff_t slinesize,
  130. ptrdiff_t olinesize,
  131. int w, int h)
  132. {
  133. const uint16_t *in = (const uint16_t *)iin;
  134. const uint16_t *threshold = (const uint16_t *)tthreshold;
  135. const uint16_t *min = (const uint16_t *)ffirst;
  136. const uint16_t *max = (const uint16_t *)ssecond;
  137. uint16_t *out = (uint16_t *)oout;
  138. int x, y;
  139. for (y = 0; y < h; y++) {
  140. for (x = 0; x < w; x++) {
  141. out[x] = in[x] < threshold[x] ? min[x] : max[x];
  142. }
  143. in += ilinesize / 2;
  144. threshold += tlinesize / 2;
  145. min += flinesize / 2;
  146. max += slinesize / 2;
  147. out += olinesize / 2;
  148. }
  149. }
  150. static int config_input(AVFilterLink *inlink)
  151. {
  152. AVFilterContext *ctx = inlink->dst;
  153. ThresholdContext *s = ctx->priv;
  154. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  155. int vsub, hsub;
  156. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  157. hsub = desc->log2_chroma_w;
  158. vsub = desc->log2_chroma_h;
  159. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  160. s->height[0] = s->height[3] = inlink->h;
  161. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  162. s->width[0] = s->width[3] = inlink->w;
  163. s->depth = desc->comp[0].depth;
  164. ff_threshold_init(s);
  165. return 0;
  166. }
  167. void ff_threshold_init(ThresholdContext *s)
  168. {
  169. if (s->depth == 8) {
  170. s->threshold = threshold8;
  171. s->bpc = 1;
  172. } else {
  173. s->threshold = threshold16;
  174. s->bpc = 2;
  175. }
  176. if (ARCH_X86)
  177. ff_threshold_init_x86(s);
  178. }
  179. static int config_output(AVFilterLink *outlink)
  180. {
  181. AVFilterContext *ctx = outlink->src;
  182. ThresholdContext *s = ctx->priv;
  183. AVFilterLink *base = ctx->inputs[0];
  184. AVFilterLink *threshold = ctx->inputs[1];
  185. AVFilterLink *min = ctx->inputs[2];
  186. AVFilterLink *max = ctx->inputs[3];
  187. FFFrameSyncIn *in;
  188. int ret;
  189. if (base->format != threshold->format ||
  190. base->format != min->format ||
  191. base->format != max->format) {
  192. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  193. return AVERROR(EINVAL);
  194. }
  195. if (base->w != threshold->w ||
  196. base->h != threshold->h ||
  197. base->w != min->w ||
  198. base->h != min->h ||
  199. base->w != max->w ||
  200. base->h != max->h) {
  201. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  202. "(size %dx%d) do not match the corresponding "
  203. "second input link %s parameters (%dx%d) "
  204. "and/or third input link %s parameters (%dx%d) "
  205. "and/or fourth input link %s parameters (%dx%d)\n",
  206. ctx->input_pads[0].name, base->w, base->h,
  207. ctx->input_pads[1].name, threshold->w, threshold->h,
  208. ctx->input_pads[2].name, min->w, min->h,
  209. ctx->input_pads[3].name, max->w, max->h);
  210. return AVERROR(EINVAL);
  211. }
  212. outlink->w = base->w;
  213. outlink->h = base->h;
  214. outlink->time_base = base->time_base;
  215. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  216. outlink->frame_rate = base->frame_rate;
  217. if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
  218. return ret;
  219. in = s->fs.in;
  220. in[0].time_base = base->time_base;
  221. in[1].time_base = threshold->time_base;
  222. in[2].time_base = min->time_base;
  223. in[3].time_base = max->time_base;
  224. in[0].sync = 1;
  225. in[0].before = EXT_STOP;
  226. in[0].after = EXT_STOP;
  227. in[1].sync = 1;
  228. in[1].before = EXT_STOP;
  229. in[1].after = EXT_STOP;
  230. in[2].sync = 1;
  231. in[2].before = EXT_STOP;
  232. in[2].after = EXT_STOP;
  233. in[3].sync = 1;
  234. in[3].before = EXT_STOP;
  235. in[3].after = EXT_STOP;
  236. s->fs.opaque = s;
  237. s->fs.on_event = process_frame;
  238. return ff_framesync_configure(&s->fs);
  239. }
  240. static int activate(AVFilterContext *ctx)
  241. {
  242. ThresholdContext *s = ctx->priv;
  243. return ff_framesync_activate(&s->fs);
  244. }
  245. static av_cold void uninit(AVFilterContext *ctx)
  246. {
  247. ThresholdContext *s = ctx->priv;
  248. ff_framesync_uninit(&s->fs);
  249. }
  250. static const AVFilterPad inputs[] = {
  251. {
  252. .name = "default",
  253. .type = AVMEDIA_TYPE_VIDEO,
  254. .config_props = config_input,
  255. },
  256. {
  257. .name = "threshold",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. },
  260. {
  261. .name = "min",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. },
  264. {
  265. .name = "max",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. },
  268. { NULL }
  269. };
  270. static const AVFilterPad outputs[] = {
  271. {
  272. .name = "default",
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .config_props = config_output,
  275. },
  276. { NULL }
  277. };
  278. AVFilter ff_vf_threshold = {
  279. .name = "threshold",
  280. .description = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
  281. .priv_size = sizeof(ThresholdContext),
  282. .priv_class = &threshold_class,
  283. .uninit = uninit,
  284. .query_formats = query_formats,
  285. .activate = activate,
  286. .inputs = inputs,
  287. .outputs = outputs,
  288. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  289. };