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.

350 lines
11KB

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