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.

272 lines
9.8KB

  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/pixdesc.h"
  28. #include "libavutil/timestamp.h"
  29. #include "avfilter.h"
  30. #include "internal.h"
  31. typedef struct BlackDetectContext {
  32. const AVClass *class;
  33. double black_min_duration_time; ///< minimum duration of detected black, in seconds
  34. int64_t black_min_duration; ///< minimum duration of detected black, expressed in timebase units
  35. int64_t black_start; ///< pts start time of the first black picture
  36. int64_t black_end; ///< pts end time of the last black picture
  37. int64_t last_picref_pts; ///< pts of the last input picture
  38. int black_started;
  39. double picture_black_ratio_th;
  40. double pixel_black_th;
  41. unsigned int pixel_black_th_i;
  42. unsigned int nb_black_pixels; ///< number of black pixels counted so far
  43. AVRational time_base;
  44. int depth;
  45. int nb_threads;
  46. unsigned int *counter;
  47. } BlackDetectContext;
  48. #define OFFSET(x) offsetof(BlackDetectContext, x)
  49. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  50. static const AVOption blackdetect_options[] = {
  51. { "d", "set minimum detected black duration in seconds", OFFSET(black_min_duration_time), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, DBL_MAX, FLAGS },
  52. { "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 },
  53. { "picture_black_ratio_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  54. { "pic_th", "set the picture black ratio threshold", OFFSET(picture_black_ratio_th), AV_OPT_TYPE_DOUBLE, {.dbl=.98}, 0, 1, FLAGS },
  55. { "pixel_black_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  56. { "pix_th", "set the pixel black threshold", OFFSET(pixel_black_th), AV_OPT_TYPE_DOUBLE, {.dbl=.10}, 0, 1, FLAGS },
  57. { NULL }
  58. };
  59. AVFILTER_DEFINE_CLASS(blackdetect);
  60. #define YUVJ_FORMATS \
  61. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P
  62. static const enum AVPixelFormat yuvj_formats[] = {
  63. YUVJ_FORMATS, AV_PIX_FMT_NONE
  64. };
  65. static int query_formats(AVFilterContext *ctx)
  66. {
  67. static const enum AVPixelFormat pix_fmts[] = {
  68. AV_PIX_FMT_GRAY8,
  69. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  70. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  71. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  72. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  73. YUVJ_FORMATS,
  74. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  75. AV_PIX_FMT_GRAY16,
  76. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  77. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  78. AV_PIX_FMT_YUV440P10,
  79. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  80. AV_PIX_FMT_YUV440P12,
  81. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  82. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  83. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  84. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
  85. AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
  86. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
  87. AV_PIX_FMT_NONE
  88. };
  89. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  90. if (!fmts_list)
  91. return AVERROR(ENOMEM);
  92. return ff_set_common_formats(ctx, fmts_list);
  93. }
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. BlackDetectContext *s = ctx->priv;
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  99. const int depth = desc->comp[0].depth;
  100. const int max = (1 << depth) - 1;
  101. const int factor = (1 << (depth - 8));
  102. s->depth = depth;
  103. s->nb_threads = ff_filter_get_nb_threads(ctx);
  104. s->time_base = inlink->time_base;
  105. s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
  106. s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
  107. if (!s->counter)
  108. return AVERROR(ENOMEM);
  109. s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
  110. // luminance_minimum_value + pixel_black_th * luminance_range_size
  111. s->pixel_black_th * max :
  112. 16 * factor + s->pixel_black_th * (235 - 16) * factor;
  113. av_log(s, AV_LOG_VERBOSE,
  114. "black_min_duration:%s pixel_black_th:%f pixel_black_th_i:%d picture_black_ratio_th:%f\n",
  115. av_ts2timestr(s->black_min_duration, &s->time_base),
  116. s->pixel_black_th, s->pixel_black_th_i,
  117. s->picture_black_ratio_th);
  118. return 0;
  119. }
  120. static void check_black_end(AVFilterContext *ctx)
  121. {
  122. BlackDetectContext *s = ctx->priv;
  123. if ((s->black_end - s->black_start) >= s->black_min_duration) {
  124. av_log(s, AV_LOG_INFO,
  125. "black_start:%s black_end:%s black_duration:%s\n",
  126. av_ts2timestr(s->black_start, &s->time_base),
  127. av_ts2timestr(s->black_end, &s->time_base),
  128. av_ts2timestr(s->black_end - s->black_start, &s->time_base));
  129. }
  130. }
  131. static int black_counter(AVFilterContext *ctx, void *arg,
  132. int jobnr, int nb_jobs)
  133. {
  134. BlackDetectContext *s = ctx->priv;
  135. const unsigned int threshold = s->pixel_black_th_i;
  136. unsigned int *counterp = &s->counter[jobnr];
  137. AVFrame *in = arg;
  138. const int linesize = in->linesize[0];
  139. const int w = in->width;
  140. const int h = in->height;
  141. const int start = (h * jobnr) / nb_jobs;
  142. const int end = (h * (jobnr+1)) / nb_jobs;
  143. const int size = end - start;
  144. unsigned int counter = 0;
  145. if (s->depth == 8) {
  146. const uint8_t *p = in->data[0] + start * linesize;
  147. for (int i = 0; i < size; i++) {
  148. for (int x = 0; x < w; x++)
  149. counter += p[x] <= threshold;
  150. p += linesize;
  151. }
  152. } else {
  153. const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
  154. for (int i = 0; i < size; i++) {
  155. for (int x = 0; x < w; x++)
  156. counter += p[x] <= threshold;
  157. p += linesize / 2;
  158. }
  159. }
  160. *counterp = counter;
  161. return 0;
  162. }
  163. static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
  164. {
  165. AVFilterContext *ctx = inlink->dst;
  166. BlackDetectContext *s = ctx->priv;
  167. double picture_black_ratio = 0;
  168. ctx->internal->execute(ctx, black_counter, picref, NULL,
  169. FFMIN(inlink->h, s->nb_threads));
  170. for (int i = 0; i < s->nb_threads; i++)
  171. s->nb_black_pixels += s->counter[i];
  172. picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
  173. av_log(ctx, AV_LOG_DEBUG,
  174. "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
  175. inlink->frame_count_out, picture_black_ratio,
  176. av_ts2str(picref->pts), av_ts2timestr(picref->pts, &s->time_base),
  177. av_get_picture_type_char(picref->pict_type));
  178. if (picture_black_ratio >= s->picture_black_ratio_th) {
  179. if (!s->black_started) {
  180. /* black starts here */
  181. s->black_started = 1;
  182. s->black_start = picref->pts;
  183. av_dict_set(&picref->metadata, "lavfi.black_start",
  184. av_ts2timestr(s->black_start, &s->time_base), 0);
  185. }
  186. } else if (s->black_started) {
  187. /* black ends here */
  188. s->black_started = 0;
  189. s->black_end = picref->pts;
  190. check_black_end(ctx);
  191. av_dict_set(&picref->metadata, "lavfi.black_end",
  192. av_ts2timestr(s->black_end, &s->time_base), 0);
  193. }
  194. s->last_picref_pts = picref->pts;
  195. s->nb_black_pixels = 0;
  196. return ff_filter_frame(inlink->dst->outputs[0], picref);
  197. }
  198. static av_cold void uninit(AVFilterContext *ctx)
  199. {
  200. BlackDetectContext *s = ctx->priv;
  201. av_freep(&s->counter);
  202. if (s->black_started) {
  203. // FIXME: black_end should be set to last_picref_pts + last_picref_duration
  204. s->black_end = s->last_picref_pts;
  205. check_black_end(ctx);
  206. }
  207. }
  208. static const AVFilterPad blackdetect_inputs[] = {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .config_props = config_input,
  213. .filter_frame = filter_frame,
  214. },
  215. { NULL }
  216. };
  217. static const AVFilterPad blackdetect_outputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. },
  222. { NULL }
  223. };
  224. AVFilter ff_vf_blackdetect = {
  225. .name = "blackdetect",
  226. .description = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
  227. .priv_size = sizeof(BlackDetectContext),
  228. .query_formats = query_formats,
  229. .inputs = blackdetect_inputs,
  230. .outputs = blackdetect_outputs,
  231. .uninit = uninit,
  232. .priv_class = &blackdetect_class,
  233. .flags = AVFILTER_FLAG_SLICE_THREADS,
  234. };