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.

225 lines
7.2KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * video scene change detection filter
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/timestamp.h"
  27. #include "avfilter.h"
  28. #include "filters.h"
  29. #include "scene_sad.h"
  30. typedef struct SCDetContext {
  31. const AVClass *class;
  32. ptrdiff_t width[4];
  33. ptrdiff_t height[4];
  34. int nb_planes;
  35. int bitdepth;
  36. ff_scene_sad_fn sad;
  37. double prev_mafd;
  38. double scene_score;
  39. AVFrame *prev_picref;
  40. double threshold;
  41. int sc_pass;
  42. } SCDetContext;
  43. #define OFFSET(x) offsetof(SCDetContext, x)
  44. #define V AV_OPT_FLAG_VIDEO_PARAM
  45. #define F AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption scdet_options[] = {
  47. { "threshold", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F },
  48. { "t", "set scene change detect threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 10.}, 0, 100., V|F },
  49. { "sc_pass", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.dbl = 0 }, 0, 1, V|F },
  50. { "s", "Set the flag to pass scene change frames", OFFSET(sc_pass), AV_OPT_TYPE_BOOL, {.dbl = 0 }, 0, 1, V|F },
  51. {NULL}
  52. };
  53. AVFILTER_DEFINE_CLASS(scdet);
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. static const enum AVPixelFormat pix_fmts[] = {
  57. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
  58. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8,
  59. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  60. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  61. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  62. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  63. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
  64. AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
  65. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  66. AV_PIX_FMT_NONE
  67. };
  68. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  69. if (!fmts_list)
  70. return AVERROR(ENOMEM);
  71. return ff_set_common_formats(ctx, fmts_list);
  72. }
  73. static int config_input(AVFilterLink *inlink)
  74. {
  75. AVFilterContext *ctx = inlink->dst;
  76. SCDetContext *s = ctx->priv;
  77. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  78. int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  79. (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  80. desc->nb_components >= 3;
  81. s->bitdepth = desc->comp[0].depth;
  82. s->nb_planes = is_yuv ? 1 : av_pix_fmt_count_planes(inlink->format);
  83. for (int plane = 0; plane < 4; plane++) {
  84. ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
  85. s->width[plane] = line_size >> (s->bitdepth > 8);
  86. s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? desc->log2_chroma_h : 0);
  87. }
  88. s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
  89. if (!s->sad)
  90. return AVERROR(EINVAL);
  91. return 0;
  92. }
  93. static av_cold void uninit(AVFilterContext *ctx)
  94. {
  95. SCDetContext *s = ctx->priv;
  96. av_frame_free(&s->prev_picref);
  97. }
  98. static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
  99. {
  100. double ret = 0;
  101. SCDetContext *s = ctx->priv;
  102. AVFrame *prev_picref = s->prev_picref;
  103. if (prev_picref && frame->height == prev_picref->height
  104. && frame->width == prev_picref->width) {
  105. uint64_t sad = 0;
  106. double mafd, diff;
  107. uint64_t count = 0;
  108. for (int plane = 0; plane < s->nb_planes; plane++) {
  109. uint64_t plane_sad;
  110. s->sad(prev_picref->data[plane], prev_picref->linesize[plane],
  111. frame->data[plane], frame->linesize[plane],
  112. s->width[plane], s->height[plane], &plane_sad);
  113. sad += plane_sad;
  114. count += s->width[plane] * s->height[plane];
  115. }
  116. emms_c();
  117. mafd = (double)sad * 100. / count / (1ULL << s->bitdepth);
  118. diff = fabs(mafd - s->prev_mafd);
  119. ret = av_clipf(FFMIN(mafd, diff), 0, 100.);
  120. s->prev_mafd = mafd;
  121. av_frame_free(&prev_picref);
  122. }
  123. s->prev_picref = av_frame_clone(frame);
  124. return ret;
  125. }
  126. static int set_meta(SCDetContext *s, AVFrame *frame, const char *key, const char *value)
  127. {
  128. return av_dict_set(&frame->metadata, key, value, 0);
  129. }
  130. static int activate(AVFilterContext *ctx)
  131. {
  132. int ret;
  133. AVFilterLink *inlink = ctx->inputs[0];
  134. AVFilterLink *outlink = ctx->outputs[0];
  135. SCDetContext *s = ctx->priv;
  136. AVFrame *frame;
  137. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  138. ret = ff_inlink_consume_frame(inlink, &frame);
  139. if (ret < 0)
  140. return ret;
  141. if (frame) {
  142. char buf[64];
  143. s->scene_score = get_scene_score(ctx, frame);
  144. snprintf(buf, sizeof(buf), "%0.3f", s->prev_mafd);
  145. set_meta(s, frame, "lavfi.scd.mafd", buf);
  146. snprintf(buf, sizeof(buf), "%0.3f", s->scene_score);
  147. set_meta(s, frame, "lavfi.scd.score", buf);
  148. if (s->scene_score > s->threshold) {
  149. av_log(s, AV_LOG_INFO, "lavfi.scd.score: %.3f, lavfi.scd.time: %s\n",
  150. s->scene_score, av_ts2timestr(frame->pts, &inlink->time_base));
  151. set_meta(s, frame, "lavfi.scd.time",
  152. av_ts2timestr(frame->pts, &inlink->time_base));
  153. }
  154. if (s->sc_pass) {
  155. if (s->scene_score > s->threshold)
  156. return ff_filter_frame(outlink, frame);
  157. else {
  158. av_frame_free(&frame);
  159. }
  160. } else
  161. return ff_filter_frame(outlink, frame);
  162. }
  163. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  164. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  165. return FFERROR_NOT_READY;
  166. }
  167. static const AVFilterPad scdet_inputs[] = {
  168. {
  169. .name = "default",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .config_props = config_input,
  172. },
  173. { NULL }
  174. };
  175. static const AVFilterPad scdet_outputs[] = {
  176. {
  177. .name = "default",
  178. .type = AVMEDIA_TYPE_VIDEO,
  179. },
  180. { NULL }
  181. };
  182. AVFilter ff_vf_scdet = {
  183. .name = "scdet",
  184. .description = NULL_IF_CONFIG_SMALL("Detect video scene change"),
  185. .priv_size = sizeof(SCDetContext),
  186. .priv_class = &scdet_class,
  187. .uninit = uninit,
  188. .query_formats = query_formats,
  189. .inputs = scdet_inputs,
  190. .outputs = scdet_outputs,
  191. .activate = activate,
  192. };