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.

114 lines
3.3KB

  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. typedef struct {
  29. unsigned int frame;
  30. int vsub, hsub;
  31. } BBoxContext;
  32. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  33. {
  34. BBoxContext *bbox = ctx->priv;
  35. bbox->frame = 0;
  36. return 0;
  37. }
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. static const enum PixelFormat pix_fmts[] = {
  41. PIX_FMT_YUV420P,
  42. PIX_FMT_YUV444P,
  43. PIX_FMT_YUV440P,
  44. PIX_FMT_YUV422P,
  45. PIX_FMT_YUV411P,
  46. PIX_FMT_NONE,
  47. };
  48. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  49. return 0;
  50. }
  51. static void end_frame(AVFilterLink *inlink)
  52. {
  53. AVFilterContext *ctx = inlink->dst;
  54. BBoxContext *bbox = ctx->priv;
  55. AVFilterBufferRef *picref = inlink->cur_buf;
  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. avfilter_end_frame(inlink->dst->outputs[0]);
  78. }
  79. AVFilter avfilter_vf_bbox = {
  80. .name = "bbox",
  81. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  82. .priv_size = sizeof(BBoxContext),
  83. .query_formats = query_formats,
  84. .init = init,
  85. .inputs = (const AVFilterPad[]) {
  86. { .name = "default",
  87. .type = AVMEDIA_TYPE_VIDEO,
  88. .get_video_buffer = avfilter_null_get_video_buffer,
  89. .start_frame = avfilter_null_start_frame,
  90. .end_frame = end_frame,
  91. .min_perms = AV_PERM_READ, },
  92. { .name = NULL }
  93. },
  94. .outputs = (const AVFilterPad[]) {
  95. { .name = "default",
  96. .type = AVMEDIA_TYPE_VIDEO },
  97. { .name = NULL }
  98. },
  99. };