vf_blackdetect.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 frame_count; ///< frame number
  42. unsigned int nb_black_pixels; ///< number of black pixels counted so far
  43. } BlackDetectContext;
  44. #define OFFSET(x) offsetof(BlackDetectContext, x)
  45. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption blackdetect_options[] = {
  47. { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
  48. { "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 },
  49. { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  50. { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  51. { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  52. { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  53. { NULL },
  54. };
  55. AVFILTER_DEFINE_CLASS(blackdetect);
  56. #define YUVJ_FORMATS \
  57. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P
  58. static enum PixelFormat yuvj_formats[] = {
  59. YUVJ_FORMATS, PIX_FMT_NONE
  60. };
  61. static int query_formats(AVFilterContext *ctx)
  62. {
  63. static const enum PixelFormat pix_fmts[] = {
  64. PIX_FMT_YUV410P, PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NV12,
  65. PIX_FMT_NV21, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
  66. YUVJ_FORMATS,
  67. PIX_FMT_NONE
  68. };
  69. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  70. return 0;
  71. }
  72. static av_cold int init(AVFilterContext *ctx, const char *args)
  73. {
  74. int ret;
  75. BlackDetectContext *blackdetect = ctx->priv;
  76. blackdetect->class = &blackdetect_class;
  77. av_opt_set_defaults(blackdetect);
  78. if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0)
  79. return ret;
  80. return 0;
  81. }
  82. static int config_input(AVFilterLink *inlink)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. BlackDetectContext *blackdetect = ctx->priv;
  86. blackdetect->black_min_duration =
  87. blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
  88. blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  89. // luminance_minimum_value + pixel_black_th * luminance_range_size
  90. blackdetect->pixel_black_th * 255 :
  91. 16 + blackdetect->pixel_black_th * (235 - 16);
  92. av_log(blackdetect, AV_LOG_VERBOSE,
  93. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  94. av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
  95. blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
  96. blackdetect->picture_black_ratio_th);
  97. return 0;
  98. }
  99. static void check_black_end(AVFilterContext *ctx)
  100. {
  101. BlackDetectContext *blackdetect = ctx->priv;
  102. AVFilterLink *inlink = ctx->inputs[0];
  103. if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
  104. av_log(blackdetect, AV_LOG_INFO,
  105. "black_start:%s black_end:%s black_duration:%s\n",
  106. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  107. av_ts2timestr(blackdetect->black_end, &inlink->time_base),
  108. av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
  109. }
  110. }
  111. static int request_frame(AVFilterLink *outlink)
  112. {
  113. AVFilterContext *ctx = outlink->src;
  114. BlackDetectContext *blackdetect = ctx->priv;
  115. AVFilterLink *inlink = ctx->inputs[0];
  116. int ret = ff_request_frame(inlink);
  117. if (ret == AVERROR_EOF && blackdetect->black_started) {
  118. // FIXME: black_end should be set to last_picref_pts + last_picref_duration
  119. blackdetect->black_end = blackdetect->last_picref_pts;
  120. check_black_end(ctx);
  121. }
  122. return ret;
  123. }
  124. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. BlackDetectContext *blackdetect = ctx->priv;
  128. AVFilterBufferRef *picref = inlink->cur_buf;
  129. int x, i;
  130. const uint8_t *p = picref->data[0] + y * picref->linesize[0];
  131. for (i = 0; i < h; i++) {
  132. for (x = 0; x < inlink->w; x++)
  133. blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
  134. p += picref->linesize[0];
  135. }
  136. return ff_draw_slice(ctx->outputs[0], y, h, slice_dir);
  137. }
  138. static int end_frame(AVFilterLink *inlink)
  139. {
  140. AVFilterContext *ctx = inlink->dst;
  141. BlackDetectContext *blackdetect = ctx->priv;
  142. AVFilterBufferRef *picref = inlink->cur_buf;
  143. double picture_black_ratio = 0;
  144. picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
  145. av_log(ctx, AV_LOG_DEBUG,
  146. "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
  147. blackdetect->frame_count, picture_black_ratio,
  148. picref->pos, av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
  149. av_get_picture_type_char(picref->video->pict_type));
  150. if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
  151. if (!blackdetect->black_started) {
  152. /* black starts here */
  153. blackdetect->black_started = 1;
  154. blackdetect->black_start = picref->pts;
  155. }
  156. } else if (blackdetect->black_started) {
  157. /* black ends here */
  158. blackdetect->black_started = 0;
  159. blackdetect->black_end = picref->pts;
  160. check_black_end(ctx);
  161. }
  162. blackdetect->last_picref_pts = picref->pts;
  163. blackdetect->frame_count++;
  164. blackdetect->nb_black_pixels = 0;
  165. return ff_end_frame(inlink->dst->outputs[0]);
  166. }
  167. AVFilter avfilter_vf_blackdetect = {
  168. .name = "blackdetect",
  169. .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
  170. .priv_size = sizeof(BlackDetectContext),
  171. .init = init,
  172. .query_formats = query_formats,
  173. .inputs = (const AVFilterPad[]) {
  174. { .name = "default",
  175. .type = AVMEDIA_TYPE_VIDEO,
  176. .config_props = config_input,
  177. .draw_slice = draw_slice,
  178. .get_video_buffer = ff_null_get_video_buffer,
  179. .start_frame = ff_null_start_frame,
  180. .end_frame = end_frame, },
  181. { .name = NULL }
  182. },
  183. .outputs = (const AVFilterPad[]) {
  184. { .name = "default",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .request_frame = request_frame, },
  187. { .name = NULL }
  188. },
  189. .priv_class = &blackdetect_class,
  190. };