vf_freezedetect.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 freeze 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 FreezeDetectContext {
  31. const AVClass *class;
  32. ptrdiff_t width[4];
  33. ptrdiff_t height[4];
  34. ff_scene_sad_fn sad;
  35. int bitdepth;
  36. AVFrame *reference_frame;
  37. int64_t n;
  38. int64_t reference_n;
  39. int frozen;
  40. double noise;
  41. int64_t duration; ///< minimum duration of frozen frame until notification
  42. } FreezeDetectContext;
  43. #define OFFSET(x) offsetof(FreezeDetectContext, x)
  44. #define V AV_OPT_FLAG_VIDEO_PARAM
  45. #define F AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption freezedetect_options[] = {
  47. { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, 1.0, V|F },
  48. { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, 1.0, V|F },
  49. { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, INT64_MAX, V|F },
  50. { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, INT64_MAX, V|F },
  51. {NULL}
  52. };
  53. AVFILTER_DEFINE_CLASS(freezedetect);
  54. static int query_formats(AVFilterContext *ctx)
  55. {
  56. static const enum AVPixelFormat pix_fmts[] = {
  57. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_RGB24,
  58. AV_PIX_FMT_BGR24, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  59. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
  60. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  61. AV_PIX_FMT_UYVY422, AV_PIX_FMT_NV12, AV_PIX_FMT_NV21, AV_PIX_FMT_ARGB,
  62. AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY16,
  63. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVA420P,
  64. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  65. AV_PIX_FMT_YA8, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10,
  66. AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
  67. AV_PIX_FMT_YUV422P9, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
  68. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP16, AV_PIX_FMT_YUVA422P,
  69. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9,
  70. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10,
  71. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16,
  72. AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_NV16, AV_PIX_FMT_YVYU422,
  73. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_YUV420P12,
  74. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14,
  75. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP12,
  76. AV_PIX_FMT_GBRP14, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV440P10,
  77. AV_PIX_FMT_YUV440P12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP10,
  78. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY9,
  79. AV_PIX_FMT_GRAY14,
  80. AV_PIX_FMT_NONE
  81. };
  82. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  83. if (!fmts_list)
  84. return AVERROR(ENOMEM);
  85. return ff_set_common_formats(ctx, fmts_list);
  86. }
  87. static int config_input(AVFilterLink *inlink)
  88. {
  89. AVFilterContext *ctx = inlink->dst;
  90. FreezeDetectContext *s = ctx->priv;
  91. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  92. s->bitdepth = pix_desc->comp[0].depth;
  93. for (int plane = 0; plane < 4; plane++) {
  94. ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
  95. s->width[plane] = line_size >> (s->bitdepth > 8);
  96. s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? pix_desc->log2_chroma_h : 0);
  97. }
  98. s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
  99. if (!s->sad)
  100. return AVERROR(EINVAL);
  101. return 0;
  102. }
  103. static av_cold void uninit(AVFilterContext *ctx)
  104. {
  105. FreezeDetectContext *s = ctx->priv;
  106. av_frame_free(&s->reference_frame);
  107. }
  108. static int is_frozen(FreezeDetectContext *s, AVFrame *reference, AVFrame *frame)
  109. {
  110. uint64_t sad = 0;
  111. uint64_t count = 0;
  112. double mafd;
  113. for (int plane = 0; plane < 4; plane++) {
  114. if (s->width[plane]) {
  115. uint64_t plane_sad;
  116. s->sad(frame->data[plane], frame->linesize[plane],
  117. reference->data[plane], reference->linesize[plane],
  118. s->width[plane], s->height[plane], &plane_sad);
  119. sad += plane_sad;
  120. count += s->width[plane] * s->height[plane];
  121. }
  122. }
  123. emms_c();
  124. mafd = (double)sad / count / (1ULL << s->bitdepth);
  125. return (mafd <= s->noise);
  126. }
  127. static int set_meta(FreezeDetectContext *s, AVFrame *frame, const char *key, const char *value)
  128. {
  129. av_log(s, AV_LOG_INFO, "%s: %s\n", key, value);
  130. return av_dict_set(&frame->metadata, key, value, 0);
  131. }
  132. static int activate(AVFilterContext *ctx)
  133. {
  134. int ret;
  135. AVFilterLink *inlink = ctx->inputs[0];
  136. AVFilterLink *outlink = ctx->outputs[0];
  137. FreezeDetectContext *s = ctx->priv;
  138. AVFrame *frame;
  139. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  140. ret = ff_inlink_consume_frame(inlink, &frame);
  141. if (ret < 0)
  142. return ret;
  143. if (frame) {
  144. int frozen = 0;
  145. s->n++;
  146. if (s->reference_frame) {
  147. int64_t duration;
  148. if (s->reference_frame->pts == AV_NOPTS_VALUE || frame->pts == AV_NOPTS_VALUE || frame->pts < s->reference_frame->pts) // Discontinuity?
  149. duration = inlink->frame_rate.num > 0 ? av_rescale_q(s->n - s->reference_n, av_inv_q(inlink->frame_rate), AV_TIME_BASE_Q) : 0;
  150. else
  151. duration = av_rescale_q(frame->pts - s->reference_frame->pts, inlink->time_base, AV_TIME_BASE_Q);
  152. frozen = is_frozen(s, s->reference_frame, frame);
  153. if (duration >= s->duration) {
  154. if (!s->frozen)
  155. set_meta(s, frame, "lavfi.freezedetect.freeze_start", av_ts2timestr(s->reference_frame->pts, &inlink->time_base));
  156. if (!frozen) {
  157. set_meta(s, frame, "lavfi.freezedetect.freeze_duration", av_ts2timestr(duration, &AV_TIME_BASE_Q));
  158. set_meta(s, frame, "lavfi.freezedetect.freeze_end", av_ts2timestr(frame->pts, &inlink->time_base));
  159. }
  160. s->frozen = frozen;
  161. }
  162. }
  163. if (!frozen) {
  164. av_frame_free(&s->reference_frame);
  165. s->reference_frame = av_frame_clone(frame);
  166. s->reference_n = s->n;
  167. if (!s->reference_frame) {
  168. av_frame_free(&frame);
  169. return AVERROR(ENOMEM);
  170. }
  171. }
  172. return ff_filter_frame(outlink, frame);
  173. }
  174. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  175. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  176. return FFERROR_NOT_READY;
  177. }
  178. static const AVFilterPad freezedetect_inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .config_props = config_input,
  183. },
  184. { NULL }
  185. };
  186. static const AVFilterPad freezedetect_outputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. },
  191. { NULL }
  192. };
  193. AVFilter ff_vf_freezedetect = {
  194. .name = "freezedetect",
  195. .description = NULL_IF_CONFIG_SMALL("Detects frozen video input."),
  196. .priv_size = sizeof(FreezeDetectContext),
  197. .priv_class = &freezedetect_class,
  198. .uninit = uninit,
  199. .query_formats = query_formats,
  200. .inputs = freezedetect_inputs,
  201. .outputs = freezedetect_outputs,
  202. .activate = activate,
  203. };