vf_bbox.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #define SET_META(key, value) \
  55. snprintf(buf, sizeof(buf), "%d", value); \
  56. av_dict_set(metadata, key, buf, 0);
  57. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  58. {
  59. AVFilterContext *ctx = inlink->dst;
  60. BBoxContext *bbox = ctx->priv;
  61. FFBoundingBox box;
  62. int has_bbox, w, h;
  63. char buf[32];
  64. has_bbox =
  65. ff_calculate_bounding_box(&box,
  66. frame->data[0], frame->linesize[0],
  67. inlink->w, inlink->h, bbox->min_val);
  68. w = box.x2 - box.x1 + 1;
  69. h = box.y2 - box.y1 + 1;
  70. av_log(ctx, AV_LOG_INFO,
  71. "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count,
  72. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
  73. if (has_bbox) {
  74. AVDictionary **metadata = avpriv_frame_get_metadatap(frame);
  75. SET_META("lavfi.bbox.x1", box.x1)
  76. SET_META("lavfi.bbox.x2", box.x2)
  77. SET_META("lavfi.bbox.y1", box.y1)
  78. SET_META("lavfi.bbox.y2", box.y2)
  79. SET_META("lavfi.bbox.w", w)
  80. SET_META("lavfi.bbox.h", h)
  81. av_log(ctx, AV_LOG_INFO,
  82. " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
  83. " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
  84. box.x1, box.x2, box.y1, box.y2, w, h,
  85. w, h, box.x1, box.y1, /* crop params */
  86. box.x1, box.y1, w, h); /* drawbox params */
  87. }
  88. av_log(ctx, AV_LOG_INFO, "\n");
  89. return ff_filter_frame(inlink->dst->outputs[0], frame);
  90. }
  91. static const AVFilterPad bbox_inputs[] = {
  92. {
  93. .name = "default",
  94. .type = AVMEDIA_TYPE_VIDEO,
  95. .filter_frame = filter_frame,
  96. },
  97. { NULL }
  98. };
  99. static const AVFilterPad bbox_outputs[] = {
  100. {
  101. .name = "default",
  102. .type = AVMEDIA_TYPE_VIDEO,
  103. },
  104. { NULL }
  105. };
  106. AVFilter ff_vf_bbox = {
  107. .name = "bbox",
  108. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  109. .priv_size = sizeof(BBoxContext),
  110. .priv_class = &bbox_class,
  111. .query_formats = query_formats,
  112. .inputs = bbox_inputs,
  113. .outputs = bbox_outputs,
  114. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  115. };