vf_blackdetect.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * Video black detector, loosely based on blackframe with extended
  23. * syntax and features
  24. */
  25. #include <float.h>
  26. #include "libavutil/opt.h"
  27. #include "libavutil/timestamp.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. typedef struct {
  31. const AVClass *class;
  32. double black_min_duration_time; ///< minimum duration of detected black, in seconds
  33. int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
  34. int64_t black_start; ///< pts start time of the first black picture
  35. int64_t black_end; ///< pts end time of the last black picture
  36. int64_t last_picref_pts; ///< pts of the last input picture
  37. int black_started;
  38. double picture_black_ratio_th;
  39. double pixel_black_th;
  40. unsigned int pixel_black_th_i;
  41. unsigned int nb_black_pixels; ///< number of black pixels counted so far
  42. } BlackDetectContext;
  43. #define OFFSET(x) offsetof(BlackDetectContext, x)
  44. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. static const AVOption blackdetect_options[] = {
  46. { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
  47. { "black_min_duration", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
  48. { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  49. { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  50. { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  51. { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  52. { NULL }
  53. };
  54. AVFILTER_DEFINE_CLASS(blackdetect);
  55. #define YUVJ_FORMATS \
  56. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  57. static enum AVPixelFormat yuvj_formats[] = {
  58. YUVJ_FORMATS, AV_PIX_FMT_NONE
  59. };
  60. static int query_formats(AVFilterContext *ctx)
  61. {
  62. static const enum AVPixelFormat pix_fmts[] = {
  63. AV_PIX_FMT_GRAY8,
  64. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  65. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  66. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  67. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  68. YUVJ_FORMATS,
  69. AV_PIX_FMT_NONE
  70. };
  71. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  72. return 0;
  73. }
  74. static int config_input(AVFilterLink *inlink)
  75. {
  76. AVFilterContext *ctx = inlink->dst;
  77. BlackDetectContext *blackdetect = ctx->priv;
  78. blackdetect->black_min_duration =
  79. blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
  80. blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  81. // luminance_minimum_value + pixel_black_th * luminance_range_size
  82. blackdetect->pixel_black_th * 255 :
  83. 16 + blackdetect->pixel_black_th * (235 - 16);
  84. av_log(blackdetect, AV_LOG_VERBOSE,
  85. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  86. av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
  87. blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
  88. blackdetect->picture_black_ratio_th);
  89. return 0;
  90. }
  91. static void check_black_end(AVFilterContext *ctx)
  92. {
  93. BlackDetectContext *blackdetect = ctx->priv;
  94. AVFilterLink *inlink = ctx->inputs[0];
  95. if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
  96. av_log(blackdetect, AV_LOG_INFO,
  97. "black_start:%s black_end:%s black_duration:%s\n",
  98. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  99. av_ts2timestr(blackdetect->black_end, &inlink->time_base),
  100. av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
  101. }
  102. }
  103. static int request_frame(AVFilterLink *outlink)
  104. {
  105. AVFilterContext *ctx = outlink->src;
  106. BlackDetectContext *blackdetect = ctx->priv;
  107. AVFilterLink *inlink = ctx->inputs[0];
  108. int ret = ff_request_frame(inlink);
  109. if (ret == AVERROR_EOF && blackdetect->black_started) {
  110. // FIXME: black_end should be set to last_picref_pts + last_picref_duration
  111. blackdetect->black_end = blackdetect->last_picref_pts;
  112. check_black_end(ctx);
  113. }
  114. return ret;
  115. }
  116. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  117. {
  118. AVFilterContext *ctx = inlink->dst;
  119. BlackDetectContext *blackdetect = ctx->priv;
  120. double picture_black_ratio = 0;
  121. const uint8_t *p = picref->data[0];
  122. int x, i;
  123. for (i = 0; i < inlink->h; i++) {
  124. for (x = 0; x < inlink->w; x++)
  125. blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
  126. p += picref->linesize[0];
  127. }
  128. picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
  129. av_log(ctx, AV_LOG_DEBUG,
  130. "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
  131. inlink->frame_count, picture_black_ratio,
  132. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
  133. av_get_picture_type_char(picref->pict_type));
  134. if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
  135. if (!blackdetect->black_started) {
  136. /* black starts here */
  137. blackdetect->black_started = 1;
  138. blackdetect->black_start = picref->pts;
  139. }
  140. } else if (blackdetect->black_started) {
  141. /* black ends here */
  142. blackdetect->black_started = 0;
  143. blackdetect->black_end = picref->pts;
  144. check_black_end(ctx);
  145. }
  146. blackdetect->last_picref_pts = picref->pts;
  147. blackdetect->nb_black_pixels = 0;
  148. return ff_filter_frame(inlink->dst->outputs[0], picref);
  149. }
  150. static const AVFilterPad blackdetect_inputs[] = {
  151. {
  152. .name = "default",
  153. .type = AVMEDIA_TYPE_VIDEO,
  154. .config_props = config_input,
  155. .filter_frame = filter_frame,
  156. },
  157. { NULL }
  158. };
  159. static const AVFilterPad blackdetect_outputs[] = {
  160. {
  161. .name = "default",
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .request_frame = request_frame,
  164. },
  165. { NULL }
  166. };
  167. AVFilter ff_vf_blackdetect = {
  168. .name = "blackdetect",
  169. .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
  170. .priv_size = sizeof(BlackDetectContext),
  171. .query_formats = query_formats,
  172. .inputs = blackdetect_inputs,
  173. .outputs = blackdetect_outputs,
  174. .priv_class = &blackdetect_class,
  175. };