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.

232 lines
8.5KB

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