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.

167 lines
5.4KB

  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 BBoxContext {
  31. const AVClass *class;
  32. int min_val;
  33. int depth;
  34. } BBoxContext;
  35. #define OFFSET(x) offsetof(BBoxContext, x)
  36. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  37. static const AVOption bbox_options[] = {
  38. { "min_val", "set minimum luminance value for bounding box", OFFSET(min_val), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, UINT16_MAX, FLAGS },
  39. { NULL }
  40. };
  41. AVFILTER_DEFINE_CLASS(bbox);
  42. static int query_formats(AVFilterContext *ctx)
  43. {
  44. static const enum AVPixelFormat pix_fmts[] = {
  45. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  46. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  47. AV_PIX_FMT_GRAY16,
  48. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  49. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  50. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  51. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  52. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  53. AV_PIX_FMT_YUVJ411P,
  54. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  55. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  56. AV_PIX_FMT_YUV440P10,
  57. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  58. AV_PIX_FMT_YUV440P12,
  59. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  60. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  61. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  62. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  63. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  64. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  65. AV_PIX_FMT_NONE,
  66. };
  67. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  68. if (!fmts_list)
  69. return AVERROR(ENOMEM);
  70. return ff_set_common_formats(ctx, fmts_list);
  71. }
  72. #define SET_META(key, value) \
  73. av_dict_set_int(metadata, key, value, 0);
  74. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  75. {
  76. AVFilterContext *ctx = inlink->dst;
  77. BBoxContext *bbox = ctx->priv;
  78. FFBoundingBox box;
  79. int has_bbox, w, h;
  80. has_bbox =
  81. ff_calculate_bounding_box(&box,
  82. frame->data[0], frame->linesize[0],
  83. inlink->w, inlink->h, bbox->min_val, bbox->depth);
  84. w = box.x2 - box.x1 + 1;
  85. h = box.y2 - box.y1 + 1;
  86. av_log(ctx, AV_LOG_INFO,
  87. "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count_out,
  88. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
  89. if (has_bbox) {
  90. AVDictionary **metadata = &frame->metadata;
  91. SET_META("lavfi.bbox.x1", box.x1)
  92. SET_META("lavfi.bbox.x2", box.x2)
  93. SET_META("lavfi.bbox.y1", box.y1)
  94. SET_META("lavfi.bbox.y2", box.y2)
  95. SET_META("lavfi.bbox.w", w)
  96. SET_META("lavfi.bbox.h", h)
  97. av_log(ctx, AV_LOG_INFO,
  98. " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
  99. " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
  100. box.x1, box.x2, box.y1, box.y2, w, h,
  101. w, h, box.x1, box.y1, /* crop params */
  102. box.x1, box.y1, w, h); /* drawbox params */
  103. }
  104. av_log(ctx, AV_LOG_INFO, "\n");
  105. return ff_filter_frame(inlink->dst->outputs[0], frame);
  106. }
  107. static int config_output(AVFilterLink *outlink)
  108. {
  109. AVFilterContext *ctx = outlink->src;
  110. BBoxContext *s = ctx->priv;
  111. const AVPixFmtDescriptor *desc;
  112. desc = av_pix_fmt_desc_get(outlink->format);
  113. if (!desc)
  114. return AVERROR_BUG;
  115. s->depth = desc->comp[0].depth;
  116. return 0;
  117. }
  118. static const AVFilterPad bbox_inputs[] = {
  119. {
  120. .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. .filter_frame = filter_frame,
  123. },
  124. { NULL }
  125. };
  126. static const AVFilterPad bbox_outputs[] = {
  127. {
  128. .name = "default",
  129. .type = AVMEDIA_TYPE_VIDEO,
  130. .config_props = config_output,
  131. },
  132. { NULL }
  133. };
  134. AVFilter ff_vf_bbox = {
  135. .name = "bbox",
  136. .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
  137. .priv_size = sizeof(BBoxContext),
  138. .priv_class = &bbox_class,
  139. .query_formats = query_formats,
  140. .inputs = bbox_inputs,
  141. .outputs = bbox_outputs,
  142. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  143. .process_command = ff_filter_process_command,
  144. };