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.

206 lines
7.7KB

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