vf_bbox.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/pixdesc.h"
  25. #include "libavutil/timestamp.h"
  26. #include "avfilter.h"
  27. #include "bbox.h"
  28. #include "internal.h"
  29. typedef struct {
  30. unsigned int frame;
  31. int vsub, hsub;
  32. } BBoxContext;
  33. static av_cold int init(AVFilterContext *ctx, const char *args)
  34. {
  35. BBoxContext *bbox = ctx->priv;
  36. bbox->frame = 0;
  37. return 0;
  38. }
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. static const enum AVPixelFormat pix_fmts[] = {
  42. AV_PIX_FMT_YUV420P,
  43. AV_PIX_FMT_YUV444P,
  44. AV_PIX_FMT_YUV440P,
  45. AV_PIX_FMT_YUV422P,
  46. AV_PIX_FMT_YUV411P,
  47. AV_PIX_FMT_NONE,
  48. };
  49. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  50. return 0;
  51. }
  52. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  53. {
  54. AVFilterContext *ctx = inlink->dst;
  55. BBoxContext *bbox = ctx->priv;
  56. FFBoundingBox box;
  57. int has_bbox, w, h;
  58. has_bbox =
  59. ff_calculate_bounding_box(&box,
  60. picref->data[0], picref->linesize[0],
  61. inlink->w, inlink->h, 16);
  62. w = box.x2 - box.x1 + 1;
  63. h = box.y2 - box.y1 + 1;
  64. av_log(ctx, AV_LOG_INFO,
  65. "n:%d pts:%s pts_time:%s", bbox->frame,
  66. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base));
  67. if (has_bbox) {
  68. av_log(ctx, AV_LOG_INFO,
  69. " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
  70. " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
  71. box.x1, box.x2, box.y1, box.y2, w, h,
  72. w, h, box.x1, box.y1, /* crop params */
  73. box.x1, box.y1, w, h); /* drawbox params */
  74. }
  75. av_log(ctx, AV_LOG_INFO, "\n");
  76. bbox->frame++;
  77. return ff_filter_frame(inlink->dst->outputs[0], picref);
  78. }
  79. static const AVFilterPad bbox_inputs[] = {
  80. {
  81. .name = "default",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .get_video_buffer = ff_null_get_video_buffer,
  84. .filter_frame = filter_frame,
  85. .min_perms = AV_PERM_READ,
  86. },
  87. { NULL }
  88. };
  89. static const AVFilterPad bbox_outputs[] = {
  90. {
  91. .name = "default",
  92. .type = AVMEDIA_TYPE_VIDEO,
  93. },
  94. { NULL }
  95. };
  96. AVFilter avfilter_vf_bbox = {
  97. .name = "bbox",
  98. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  99. .priv_size = sizeof(BBoxContext),
  100. .query_formats = query_formats,
  101. .init = init,
  102. .inputs = bbox_inputs,
  103. .outputs = bbox_outputs,
  104. };