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.

369 lines
12KB

  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_YUV440P10, AV_PIX_FMT_YUV444P10,
  50. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV444P12,
  51. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  52. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  53. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  54. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  55. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  56. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  57. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  58. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12 , AV_PIX_FMT_GBRAP16,
  59. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
  60. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  61. AV_PIX_FMT_NONE
  62. };
  63. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  64. }
  65. typedef struct ThreadData {
  66. AVFrame *in;
  67. AVFrame *threshold;
  68. AVFrame *min;
  69. AVFrame *max;
  70. AVFrame *out;
  71. } ThreadData;
  72. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  73. {
  74. ThresholdContext *s = ctx->priv;
  75. ThreadData *td = arg;
  76. AVFrame *min = td->min;
  77. AVFrame *max = td->max;
  78. AVFrame *threshold = td->threshold;
  79. AVFrame *in = td->in;
  80. AVFrame *out = td->out;
  81. for (int p = 0; p < s->nb_planes; p++) {
  82. const int h = s->height[p];
  83. const int slice_start = (h * jobnr) / nb_jobs;
  84. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  85. if (!(s->planes & (1 << p))) {
  86. av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
  87. out->linesize[p],
  88. in->data[p] + slice_start * in->linesize[p],
  89. in->linesize[p],
  90. s->width[p] * s->bpc,
  91. slice_end - slice_start);
  92. continue;
  93. }
  94. s->threshold(in->data[p] + slice_start * in->linesize[p],
  95. threshold->data[p] + slice_start * threshold->linesize[p],
  96. min->data[p] + slice_start * min->linesize[p],
  97. max->data[p] + slice_start * max->linesize[p],
  98. out->data[p] + slice_start * out->linesize[p],
  99. in->linesize[p], threshold->linesize[p],
  100. min->linesize[p], max->linesize[p],
  101. out->linesize[p],
  102. s->width[p], slice_end - slice_start);
  103. }
  104. return 0;
  105. }
  106. static int process_frame(FFFrameSync *fs)
  107. {
  108. AVFilterContext *ctx = fs->parent;
  109. ThresholdContext *s = fs->opaque;
  110. AVFilterLink *outlink = ctx->outputs[0];
  111. AVFrame *out, *in, *threshold, *min, *max;
  112. ThreadData td;
  113. int ret;
  114. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
  115. (ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 ||
  116. (ret = ff_framesync_get_frame(&s->fs, 2, &min, 0)) < 0 ||
  117. (ret = ff_framesync_get_frame(&s->fs, 3, &max, 0)) < 0)
  118. return ret;
  119. if (ctx->is_disabled) {
  120. out = av_frame_clone(in);
  121. if (!out)
  122. return AVERROR(ENOMEM);
  123. } else {
  124. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  125. if (!out)
  126. return AVERROR(ENOMEM);
  127. av_frame_copy_props(out, in);
  128. td.out = out;
  129. td.in = in;
  130. td.threshold = threshold;
  131. td.min = min;
  132. td.max = max;
  133. ctx->internal->execute(ctx, filter_slice, &td, NULL,
  134. FFMIN(s->height[2], ff_filter_get_nb_threads(ctx)));
  135. }
  136. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  137. return ff_filter_frame(outlink, out);
  138. }
  139. static void threshold8(const uint8_t *in, const uint8_t *threshold,
  140. const uint8_t *min, const uint8_t *max,
  141. uint8_t *out,
  142. ptrdiff_t ilinesize, ptrdiff_t tlinesize,
  143. ptrdiff_t flinesize, ptrdiff_t slinesize,
  144. ptrdiff_t olinesize,
  145. int w, int h)
  146. {
  147. int x, y;
  148. for (y = 0; y < h; y++) {
  149. for (x = 0; x < w; x++) {
  150. out[x] = in[x] < threshold[x] ? min[x] : max[x];
  151. }
  152. in += ilinesize;
  153. threshold += tlinesize;
  154. min += flinesize;
  155. max += slinesize;
  156. out += olinesize;
  157. }
  158. }
  159. static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
  160. const uint8_t *ffirst, const uint8_t *ssecond,
  161. uint8_t *oout,
  162. ptrdiff_t ilinesize, ptrdiff_t tlinesize,
  163. ptrdiff_t flinesize, ptrdiff_t slinesize,
  164. ptrdiff_t olinesize,
  165. int w, int h)
  166. {
  167. const uint16_t *in = (const uint16_t *)iin;
  168. const uint16_t *threshold = (const uint16_t *)tthreshold;
  169. const uint16_t *min = (const uint16_t *)ffirst;
  170. const uint16_t *max = (const uint16_t *)ssecond;
  171. uint16_t *out = (uint16_t *)oout;
  172. int x, y;
  173. for (y = 0; y < h; y++) {
  174. for (x = 0; x < w; x++) {
  175. out[x] = in[x] < threshold[x] ? min[x] : max[x];
  176. }
  177. in += ilinesize / 2;
  178. threshold += tlinesize / 2;
  179. min += flinesize / 2;
  180. max += slinesize / 2;
  181. out += olinesize / 2;
  182. }
  183. }
  184. static int config_input(AVFilterLink *inlink)
  185. {
  186. AVFilterContext *ctx = inlink->dst;
  187. ThresholdContext *s = ctx->priv;
  188. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  189. int vsub, hsub;
  190. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  191. hsub = desc->log2_chroma_w;
  192. vsub = desc->log2_chroma_h;
  193. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  194. s->height[0] = s->height[3] = inlink->h;
  195. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  196. s->width[0] = s->width[3] = inlink->w;
  197. s->depth = desc->comp[0].depth;
  198. ff_threshold_init(s);
  199. return 0;
  200. }
  201. void ff_threshold_init(ThresholdContext *s)
  202. {
  203. if (s->depth == 8) {
  204. s->threshold = threshold8;
  205. s->bpc = 1;
  206. } else {
  207. s->threshold = threshold16;
  208. s->bpc = 2;
  209. }
  210. if (ARCH_X86)
  211. ff_threshold_init_x86(s);
  212. }
  213. static int config_output(AVFilterLink *outlink)
  214. {
  215. AVFilterContext *ctx = outlink->src;
  216. ThresholdContext *s = ctx->priv;
  217. AVFilterLink *base = ctx->inputs[0];
  218. AVFilterLink *threshold = ctx->inputs[1];
  219. AVFilterLink *min = ctx->inputs[2];
  220. AVFilterLink *max = ctx->inputs[3];
  221. FFFrameSyncIn *in;
  222. int ret;
  223. if (base->format != threshold->format ||
  224. base->format != min->format ||
  225. base->format != max->format) {
  226. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  227. return AVERROR(EINVAL);
  228. }
  229. if (base->w != threshold->w ||
  230. base->h != threshold->h ||
  231. base->w != min->w ||
  232. base->h != min->h ||
  233. base->w != max->w ||
  234. base->h != max->h) {
  235. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  236. "(size %dx%d) do not match the corresponding "
  237. "second input link %s parameters (%dx%d) "
  238. "and/or third input link %s parameters (%dx%d) "
  239. "and/or fourth input link %s parameters (%dx%d)\n",
  240. ctx->input_pads[0].name, base->w, base->h,
  241. ctx->input_pads[1].name, threshold->w, threshold->h,
  242. ctx->input_pads[2].name, min->w, min->h,
  243. ctx->input_pads[3].name, max->w, max->h);
  244. return AVERROR(EINVAL);
  245. }
  246. outlink->w = base->w;
  247. outlink->h = base->h;
  248. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  249. outlink->frame_rate = base->frame_rate;
  250. if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
  251. return ret;
  252. in = s->fs.in;
  253. in[0].time_base = base->time_base;
  254. in[1].time_base = threshold->time_base;
  255. in[2].time_base = min->time_base;
  256. in[3].time_base = max->time_base;
  257. in[0].sync = 1;
  258. in[0].before = EXT_STOP;
  259. in[0].after = EXT_STOP;
  260. in[1].sync = 1;
  261. in[1].before = EXT_STOP;
  262. in[1].after = EXT_STOP;
  263. in[2].sync = 1;
  264. in[2].before = EXT_STOP;
  265. in[2].after = EXT_STOP;
  266. in[3].sync = 1;
  267. in[3].before = EXT_STOP;
  268. in[3].after = EXT_STOP;
  269. s->fs.opaque = s;
  270. s->fs.on_event = process_frame;
  271. ret = ff_framesync_configure(&s->fs);
  272. outlink->time_base = s->fs.time_base;
  273. return ret;
  274. }
  275. static int activate(AVFilterContext *ctx)
  276. {
  277. ThresholdContext *s = ctx->priv;
  278. return ff_framesync_activate(&s->fs);
  279. }
  280. static av_cold void uninit(AVFilterContext *ctx)
  281. {
  282. ThresholdContext *s = ctx->priv;
  283. ff_framesync_uninit(&s->fs);
  284. }
  285. static const AVFilterPad inputs[] = {
  286. {
  287. .name = "default",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .config_props = config_input,
  290. },
  291. {
  292. .name = "threshold",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. },
  295. {
  296. .name = "min",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. },
  299. {
  300. .name = "max",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. },
  303. { NULL }
  304. };
  305. static const AVFilterPad outputs[] = {
  306. {
  307. .name = "default",
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. .config_props = config_output,
  310. },
  311. { NULL }
  312. };
  313. AVFilter ff_vf_threshold = {
  314. .name = "threshold",
  315. .description = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
  316. .priv_size = sizeof(ThresholdContext),
  317. .priv_class = &threshold_class,
  318. .uninit = uninit,
  319. .query_formats = query_formats,
  320. .activate = activate,
  321. .inputs = inputs,
  322. .outputs = outputs,
  323. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  324. };