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.

205 lines
7.6KB

  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. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  58. static enum AVPixelFormat yuvj_formats[] = {
  59. YUVJ_FORMATS, AV_PIX_FMT_NONE
  60. };
  61. static int query_formats(AVFilterContext *ctx)
  62. {
  63. static const enum AVPixelFormat pix_fmts[] = {
  64. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NV12,
  65. AV_PIX_FMT_NV21, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
  66. YUVJ_FORMATS,
  67. AV_PIX_FMT_NONE
  68. };
  69. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  70. return 0;
  71. }
  72. static int config_input(AVFilterLink *inlink)
  73. {
  74. AVFilterContext *ctx = inlink->dst;
  75. BlackDetectContext *blackdetect = ctx->priv;
  76. blackdetect->black_min_duration =
  77. blackdetect->black_min_duration_time / av_q2d(inlink->time_base);
  78. blackdetect->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  79. // luminance_minimum_value + pixel_black_th * luminance_range_size
  80. blackdetect->pixel_black_th * 255 :
  81. 16 + blackdetect->pixel_black_th * (235 - 16);
  82. av_log(blackdetect, AV_LOG_VERBOSE,
  83. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  84. av_ts2timestr(blackdetect->black_min_duration, &inlink->time_base),
  85. blackdetect->pixel_black_th, blackdetect->pixel_black_th_i,
  86. blackdetect->picture_black_ratio_th);
  87. return 0;
  88. }
  89. static void check_black_end(AVFilterContext *ctx)
  90. {
  91. BlackDetectContext *blackdetect = ctx->priv;
  92. AVFilterLink *inlink = ctx->inputs[0];
  93. if ((blackdetect->black_end - blackdetect->black_start) >= blackdetect->black_min_duration) {
  94. av_log(blackdetect, AV_LOG_INFO,
  95. "black_start:%s black_end:%s black_duration:%s\n",
  96. av_ts2timestr(blackdetect->black_start, &inlink->time_base),
  97. av_ts2timestr(blackdetect->black_end, &inlink->time_base),
  98. av_ts2timestr(blackdetect->black_end - blackdetect->black_start, &inlink->time_base));
  99. }
  100. }
  101. static int request_frame(AVFilterLink *outlink)
  102. {
  103. AVFilterContext *ctx = outlink->src;
  104. BlackDetectContext *blackdetect = ctx->priv;
  105. AVFilterLink *inlink = ctx->inputs[0];
  106. int ret = ff_request_frame(inlink);
  107. if (ret == AVERROR_EOF && blackdetect->black_started) {
  108. // FIXME: black_end should be set to last_picref_pts + last_picref_duration
  109. blackdetect->black_end = blackdetect->last_picref_pts;
  110. check_black_end(ctx);
  111. }
  112. return ret;
  113. }
  114. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  115. {
  116. AVFilterContext *ctx = inlink->dst;
  117. BlackDetectContext *blackdetect = ctx->priv;
  118. double picture_black_ratio = 0;
  119. const uint8_t *p = picref->data[0];
  120. int x, i;
  121. for (i = 0; i < inlink->h; i++) {
  122. for (x = 0; x < inlink->w; x++)
  123. blackdetect->nb_black_pixels += p[x] <= blackdetect->pixel_black_th_i;
  124. p += picref->linesize[0];
  125. }
  126. picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
  127. av_log(ctx, AV_LOG_DEBUG,
  128. "frame:%u picture_black_ratio:%f pts:%s t:%s type:%c\n",
  129. blackdetect->frame_count, picture_black_ratio,
  130. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
  131. av_get_picture_type_char(picref->pict_type));
  132. if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
  133. if (!blackdetect->black_started) {
  134. /* black starts here */
  135. blackdetect->black_started = 1;
  136. blackdetect->black_start = picref->pts;
  137. }
  138. } else if (blackdetect->black_started) {
  139. /* black ends here */
  140. blackdetect->black_started = 0;
  141. blackdetect->black_end = picref->pts;
  142. check_black_end(ctx);
  143. }
  144. blackdetect->last_picref_pts = picref->pts;
  145. blackdetect->frame_count++;
  146. blackdetect->nb_black_pixels = 0;
  147. return ff_filter_frame(inlink->dst->outputs[0], picref);
  148. }
  149. static const AVFilterPad blackdetect_inputs[] = {
  150. {
  151. .name = "default",
  152. .type = AVMEDIA_TYPE_VIDEO,
  153. .config_props = config_input,
  154. .get_video_buffer = ff_null_get_video_buffer,
  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 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. .query_formats = query_formats,
  172. .inputs = blackdetect_inputs,
  173. .outputs = blackdetect_outputs,
  174. .priv_class = &blackdetect_class,
  175. };