vf_bbox.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 PixelFormat pix_fmts[] = {
  42. PIX_FMT_YUV420P,
  43. PIX_FMT_YUV444P,
  44. PIX_FMT_YUV440P,
  45. PIX_FMT_YUV422P,
  46. PIX_FMT_YUV411P,
  47. PIX_FMT_NONE,
  48. };
  49. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  50. return 0;
  51. }
  52. static int end_frame(AVFilterLink *inlink)
  53. {
  54. AVFilterContext *ctx = inlink->dst;
  55. BBoxContext *bbox = ctx->priv;
  56. AVFilterBufferRef *picref = inlink->cur_buf;
  57. FFBoundingBox box;
  58. int has_bbox, w, h;
  59. has_bbox =
  60. ff_calculate_bounding_box(&box,
  61. picref->data[0], picref->linesize[0],
  62. inlink->w, inlink->h, 16);
  63. w = box.x2 - box.x1 + 1;
  64. h = box.y2 - box.y1 + 1;
  65. av_log(ctx, AV_LOG_INFO,
  66. "n:%d pts:%s pts_time:%s", bbox->frame,
  67. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base));
  68. if (has_bbox) {
  69. av_log(ctx, AV_LOG_INFO,
  70. " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
  71. " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
  72. box.x1, box.x2, box.y1, box.y2, w, h,
  73. w, h, box.x1, box.y1, /* crop params */
  74. box.x1, box.y1, w, h); /* drawbox params */
  75. }
  76. av_log(ctx, AV_LOG_INFO, "\n");
  77. bbox->frame++;
  78. return ff_end_frame(inlink->dst->outputs[0]);
  79. }
  80. AVFilter avfilter_vf_bbox = {
  81. .name = "bbox",
  82. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  83. .priv_size = sizeof(BBoxContext),
  84. .query_formats = query_formats,
  85. .init = init,
  86. .inputs = (const AVFilterPad[]) {
  87. { .name = "default",
  88. .type = AVMEDIA_TYPE_VIDEO,
  89. .get_video_buffer = ff_null_get_video_buffer,
  90. .start_frame = ff_null_start_frame,
  91. .end_frame = end_frame,
  92. .min_perms = AV_PERM_READ, },
  93. { .name = NULL }
  94. },
  95. .outputs = (const AVFilterPad[]) {
  96. { .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO },
  98. { .name = NULL }
  99. },
  100. };