vf_blackdetect.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. int black_started;
  37. double picture_black_ratio_th;
  38. double pixel_black_th;
  39. unsigned int pixel_black_th_i;
  40. unsigned int frame_count; ///< frame number
  41. unsigned int nb_black_pixels; ///< number of black pixels counted so far
  42. } BlackDetectContext;
  43. #define OFFSET(x) offsetof(BlackDetectContext, x)
  44. static const AVOption blackdetect_options[] = {
  45. { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
  46. { "black_min_duration", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX},
  47. { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
  48. { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1},
  49. { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
  50. { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1},
  51. { NULL },
  52. };
  53. static const char *blackdetect_get_name(void *ctx)
  54. {
  55. return "blackdetect";
  56. }
  57. static const AVClass blackdetect_class = {
  58. .class_name = "BlackDetectContext",
  59. .item_name = blackdetect_get_name,
  60. .option = blackdetect_options,
  61. };
  62. #define YUVJ_FORMATS \
  63. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
  64. static enum PixelFormat yuvj_formats[] = {
  65. YUVJ_FORMATS, PIX_FMT_NONE
  66. };
  67. static int query_formats(AVFilterContext *ctx)
  68. {
  69. static const enum PixelFormat pix_fmts[] = {
  70. PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12,
  71. PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
  72. YUVJ_FORMATS,
  73. PIX_FMT_NONE
  74. };
  75. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  76. return 0;
  77. }
  78. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  79. {
  80. int ret;
  81. BlackDetectContext *blackdetect = ctx->priv;
  82. blackdetect->class = &blackdetect_class;
  83. av_opt_set_defaults(blackdetect);
  84. if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0) {
  85. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. static int config_input(AVFilterLink *inlink)
  91. {
  92. AVFilterContext *ctx = inlink->dst;
  93. BlackDetectContext *blackdetect = ctx->priv;
  94. blackdetect->black_min_duration =
  95. blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
  96. blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  97. // luminance_minimum_value + pixel_black_th * luminance_range_size
  98. blackdetect->pixel_black_th * 255 :
  99. 16 + blackdetect->pixel_black_th * (235 - 16);
  100. av_log(blackdetect, AV_LOG_INFO,
  101. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  102. av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
  103. blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
  104. blackdetect->picture_black_ratio_th);
  105. return 0;
  106. }
  107. static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  108. {
  109. AVFilterContext *ctx = inlink->dst;
  110. BlackDetectContext *blackdetect = ctx->priv;
  111. AVFilterBufferRef *picref = inlink->cur_buf;
  112. int x, i;
  113. const uint8_t *p = picref->data[0] + y * picref->linesize[0];
  114. for (i = 0; i < h; i++) {
  115. for (x = 0; x < inlink->w; x++)
  116. blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
  117. p += picref->linesize[0];
  118. }
  119. avfilter_draw_slice(ctx->outputs[0], y, h, slice_dir);
  120. }
  121. static void end_frame(AVFilterLink *inlink)
  122. {
  123. AVFilterContext *ctx = inlink->dst;
  124. BlackDetectContext *blackdetect = ctx->priv;
  125. AVFilterBufferRef *picref = inlink->cur_buf;
  126. double picture_black_ratio = 0;
  127. picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
  128. av_log(ctx, AV_LOG_DEBUG,
  129. "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
  130. blackdetect->frame_count, picture_black_ratio,
  131. picref->pos, av_ts2str(picref->pts),
  132. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  133. av_get_picture_type_char(picref->video->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. if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
  145. av_log(blackdetect, AV_LOG_INFO,
  146. "black_start:%s black_end:%s black_duration:%s\n",
  147. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  148. av_ts2timestr(blackdetect->black_end, &inlink->time_base),
  149. av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
  150. }
  151. }
  152. blackdetect->frame_count++;
  153. blackdetect->nb_black_pixels = 0;
  154. avfilter_unref_buffer(picref);
  155. avfilter_end_frame(inlink->dst->outputs[0]);
  156. }
  157. AVFilter avfilter_vf_blackdetect = {
  158. .name = "blackdetect",
  159. .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
  160. .priv_size = sizeof(BlackDetectContext),
  161. .init = init,
  162. .query_formats = query_formats,
  163. .inputs = (const AVFilterPad[]) {
  164. { .name = "default",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .config_props = config_input,
  167. .draw_slice = draw_slice,
  168. .get_video_buffer = avfilter_null_get_video_buffer,
  169. .start_frame = ff_null_start_frame_keep_ref,
  170. .end_frame = end_frame, },
  171. { .name = NULL }
  172. },
  173. .outputs = (const AVFilterPad[]) {
  174. { .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO },
  176. { .name = NULL }
  177. },
  178. };