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.

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