vf_blackdetect.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 const 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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  72. if (!fmts_list)
  73. return AVERROR(ENOMEM);
  74. return ff_set_common_formats(ctx, fmts_list);
  75. }
  76. static int config_input(AVFilterLink *inlink)
  77. {
  78. AVFilterContext *ctx = inlink->dst;
  79. BlackDetectContext *blackdetect = ctx->priv;
  80. blackdetect->black_min_duration =
  81. blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
  82. blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  83. // luminance_minimum_value + pixel_black_th * luminance_range_size
  84. blackdetect->pixel_black_th * 255 :
  85. 16 + blackdetect->pixel_black_th * (235 - 16);
  86. av_log(blackdetect, AV_LOG_VERBOSE,
  87. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  88. av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
  89. blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
  90. blackdetect->picture_black_ratio_th);
  91. return 0;
  92. }
  93. static void check_black_end(AVFilterContext *ctx)
  94. {
  95. BlackDetectContext *blackdetect = ctx->priv;
  96. AVFilterLink *inlink = ctx->inputs[0];
  97. if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
  98. av_log(blackdetect, AV_LOG_INFO,
  99. "black_start:%s black_end:%s black_duration:%s\n",
  100. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  101. av_ts2timestr(blackdetect->black_end, &inlink->time_base),
  102. av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
  103. }
  104. }
  105. static int request_frame(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. BlackDetectContext *blackdetect = ctx->priv;
  109. AVFilterLink *inlink = ctx->inputs[0];
  110. int ret = ff_request_frame(inlink);
  111. if (ret == AVERROR_EOF && blackdetect->black_started) {
  112. // FIXME: black_end should be set to last_picref_pts + last_picref_duration
  113. blackdetect->black_end = blackdetect->last_picref_pts;
  114. check_black_end(ctx);
  115. }
  116. return ret;
  117. }
  118. // TODO: document metadata
  119. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. BlackDetectContext *blackdetect = ctx->priv;
  123. double picture_black_ratio = 0;
  124. const uint8_t *p = picref->data[0];
  125. int x, i;
  126. for (i = 0; i < inlink->h; i++) {
  127. for (x = 0; x < inlink->w; x++)
  128. blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
  129. p += picref->linesize[0];
  130. }
  131. picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
  132. av_log(ctx, AV_LOG_DEBUG,
  133. "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
  134. inlink->frame_count, picture_black_ratio,
  135. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
  136. av_get_picture_type_char(picref->pict_type));
  137. if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
  138. if (!blackdetect->black_started) {
  139. /* black starts here */
  140. blackdetect->black_started = 1;
  141. blackdetect->black_start = picref->pts;
  142. av_dict_set(avpriv_frame_get_metadatap(picref), "lavfi.black_start",
  143. av_ts2timestr(blackdetect->black_start, &inlink->time_base), 0);
  144. }
  145. } else if (blackdetect->black_started) {
  146. /* black ends here */
  147. blackdetect->black_started = 0;
  148. blackdetect->black_end = picref->pts;
  149. check_black_end(ctx);
  150. av_dict_set(avpriv_frame_get_metadatap(picref), "lavfi.black_end",
  151. av_ts2timestr(blackdetect->black_end, &inlink->time_base), 0);
  152. }
  153. blackdetect->last_picref_pts = picref->pts;
  154. blackdetect->nb_black_pixels = 0;
  155. return ff_filter_frame(inlink->dst->outputs[0], picref);
  156. }
  157. static const AVFilterPad blackdetect_inputs[] = {
  158. {
  159. .name = "default",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. .config_props = config_input,
  162. .filter_frame = filter_frame,
  163. },
  164. { NULL }
  165. };
  166. static const AVFilterPad blackdetect_outputs[] = {
  167. {
  168. .name = "default",
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .request_frame = request_frame,
  171. },
  172. { NULL }
  173. };
  174. AVFilter ff_vf_blackdetect = {
  175. .name = "blackdetect",
  176. .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
  177. .priv_size = sizeof(BlackDetectContext),
  178. .query_formats = query_formats,
  179. .inputs = blackdetect_inputs,
  180. .outputs = blackdetect_outputs,
  181. .priv_class = &blackdetect_class,
  182. };