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.

121 lines
3.5KB

  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  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. * bounding box detection filter
  23. */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/timestamp.h"
  27. #include "avfilter.h"
  28. #include "bbox.h"
  29. #include "internal.h"
  30. typedef struct {
  31. const AVClass *class;
  32. int min_val;
  33. } BBoxContext;
  34. #define OFFSET(x) offsetof(BBoxContext, x)
  35. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  36. static const AVOption bbox_options[] = {
  37. { "min_val", "set minimum luminance value for bounding box", OFFSET(min_val), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 254, FLAGS },
  38. { NULL }
  39. };
  40. AVFILTER_DEFINE_CLASS(bbox);
  41. static int query_formats(AVFilterContext *ctx)
  42. {
  43. static const enum AVPixelFormat pix_fmts[] = {
  44. AV_PIX_FMT_YUV420P,
  45. AV_PIX_FMT_YUV444P,
  46. AV_PIX_FMT_YUV440P,
  47. AV_PIX_FMT_YUV422P,
  48. AV_PIX_FMT_YUV411P,
  49. AV_PIX_FMT_NONE,
  50. };
  51. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  52. return 0;
  53. }
  54. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  55. {
  56. AVFilterContext *ctx = inlink->dst;
  57. BBoxContext *bbox = ctx->priv;
  58. FFBoundingBox box;
  59. int has_bbox, w, h;
  60. has_bbox =
  61. ff_calculate_bounding_box(&box,
  62. frame->data[0], frame->linesize[0],
  63. inlink->w, inlink->h, bbox->min_val);
  64. w = box.x2 - box.x1 + 1;
  65. h = box.y2 - box.y1 + 1;
  66. av_log(ctx, AV_LOG_INFO,
  67. "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count,
  68. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
  69. if (has_bbox) {
  70. av_log(ctx, AV_LOG_INFO,
  71. " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
  72. " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
  73. box.x1, box.x2, box.y1, box.y2, w, h,
  74. w, h, box.x1, box.y1, /* crop params */
  75. box.x1, box.y1, w, h); /* drawbox params */
  76. }
  77. av_log(ctx, AV_LOG_INFO, "\n");
  78. return ff_filter_frame(inlink->dst->outputs[0], frame);
  79. }
  80. static const AVFilterPad bbox_inputs[] = {
  81. {
  82. .name = "default",
  83. .type = AVMEDIA_TYPE_VIDEO,
  84. .filter_frame = filter_frame,
  85. },
  86. { NULL }
  87. };
  88. static const AVFilterPad bbox_outputs[] = {
  89. {
  90. .name = "default",
  91. .type = AVMEDIA_TYPE_VIDEO,
  92. },
  93. { NULL }
  94. };
  95. AVFilter avfilter_vf_bbox = {
  96. .name = "bbox",
  97. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  98. .priv_size = sizeof(BBoxContext),
  99. .priv_class = &bbox_class,
  100. .query_formats = query_formats,
  101. .inputs = bbox_inputs,
  102. .outputs = bbox_outputs,
  103. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  104. };