2 * This file is part of FFmpeg.
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.
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.
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
21 * video freeze detection filter
24 #include "libavutil/avassert.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/timestamp.h"
32 #include "scene_sad.h"
34 typedef struct FreezeDetectContext {
41 AVFrame *reference_frame;
47 int64_t duration; ///< minimum duration of frozen frame until notification
48 } FreezeDetectContext;
50 #define OFFSET(x) offsetof(FreezeDetectContext, x)
51 #define V AV_OPT_FLAG_VIDEO_PARAM
52 #define F AV_OPT_FLAG_FILTERING_PARAM
54 static const AVOption freezedetect_options[] = {
55 { "n", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, 1.0, V|F },
56 { "noise", "set noise tolerance", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0, 1.0, V|F },
57 { "d", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, INT64_MAX, V|F },
58 { "duration", "set minimum duration in seconds", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=2000000}, 0, INT64_MAX, V|F },
63 AVFILTER_DEFINE_CLASS(freezedetect);
65 static int query_formats(AVFilterContext *ctx)
67 static const enum AVPixelFormat pix_fmts[] = {
68 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_RGB24,
69 AV_PIX_FMT_BGR24, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
70 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
71 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
72 AV_PIX_FMT_UYVY422, AV_PIX_FMT_NV12, AV_PIX_FMT_NV21, AV_PIX_FMT_ARGB,
73 AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY16,
74 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVA420P,
75 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
76 AV_PIX_FMT_YA8, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10,
77 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
78 AV_PIX_FMT_YUV422P9, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
79 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP16, AV_PIX_FMT_YUVA422P,
80 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9,
81 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10,
82 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16,
83 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_NV16, AV_PIX_FMT_YVYU422,
84 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_YUV420P12,
85 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14,
86 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_GBRP12,
87 AV_PIX_FMT_GBRP14, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV440P10,
88 AV_PIX_FMT_YUV440P12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP10,
89 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY9,
94 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
96 return AVERROR(ENOMEM);
97 return ff_set_common_formats(ctx, fmts_list);
100 static int config_input(AVFilterLink *inlink)
102 AVFilterContext *ctx = inlink->dst;
103 FreezeDetectContext *s = ctx->priv;
104 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
106 s->bitdepth = pix_desc->comp[0].depth;
108 for (int plane = 0; plane < 4; plane++) {
109 ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
110 s->width[plane] = line_size >> (s->bitdepth > 8);
111 s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? pix_desc->log2_chroma_h : 0);
114 s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
116 return AVERROR(EINVAL);
121 static av_cold void uninit(AVFilterContext *ctx)
123 FreezeDetectContext *s = ctx->priv;
124 av_frame_free(&s->reference_frame);
127 static int is_frozen(FreezeDetectContext *s, AVFrame *reference, AVFrame *frame)
132 for (int plane = 0; plane < 4; plane++) {
133 if (s->width[plane]) {
135 s->sad(frame->data[plane], frame->linesize[plane],
136 reference->data[plane], reference->linesize[plane],
137 s->width[plane], s->height[plane], &plane_sad);
139 count += s->width[plane] * s->height[plane];
143 mafd = (double)sad / count / (1ULL << s->bitdepth);
144 return (mafd <= s->noise);
147 static int set_meta(FreezeDetectContext *s, AVFrame *frame, const char *key, const char *value)
149 av_log(s, AV_LOG_INFO, "%s: %s\n", key, value);
150 return av_dict_set(&frame->metadata, key, value, 0);
153 static int activate(AVFilterContext *ctx)
156 AVFilterLink *inlink = ctx->inputs[0];
157 AVFilterLink *outlink = ctx->outputs[0];
158 FreezeDetectContext *s = ctx->priv;
161 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
163 ret = ff_inlink_consume_frame(inlink, &frame);
171 if (s->reference_frame) {
173 if (s->reference_frame->pts == AV_NOPTS_VALUE || frame->pts == AV_NOPTS_VALUE || frame->pts < s->reference_frame->pts) // Discontinuity?
174 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;
176 duration = av_rescale_q(frame->pts - s->reference_frame->pts, inlink->time_base, AV_TIME_BASE_Q);
178 frozen = is_frozen(s, s->reference_frame, frame);
179 if (duration >= s->duration) {
182 set_meta(s, frame, "lavfi.freezedetect.freeze_start", av_ts2timestr(s->reference_frame->pts, &inlink->time_base));
184 set_meta(s, frame, "lavfi.freezedetect.freeze_duration", av_ts2timestr(duration, &AV_TIME_BASE_Q));
185 set_meta(s, frame, "lavfi.freezedetect.freeze_end", av_ts2timestr(frame->pts, &inlink->time_base));
192 av_frame_free(&s->reference_frame);
193 s->reference_frame = av_frame_clone(frame);
194 s->reference_n = s->n;
195 if (!s->reference_frame) {
196 av_frame_free(&frame);
197 return AVERROR(ENOMEM);
200 return ff_filter_frame(outlink, frame);
203 FF_FILTER_FORWARD_STATUS(inlink, outlink);
204 FF_FILTER_FORWARD_WANTED(outlink, inlink);
206 return FFERROR_NOT_READY;
209 static const AVFilterPad freezedetect_inputs[] = {
212 .type = AVMEDIA_TYPE_VIDEO,
213 .config_props = config_input,
218 static const AVFilterPad freezedetect_outputs[] = {
221 .type = AVMEDIA_TYPE_VIDEO,
226 AVFilter ff_vf_freezedetect = {
227 .name = "freezedetect",
228 .description = NULL_IF_CONFIG_SMALL("Detects frozen video input."),
229 .priv_size = sizeof(FreezeDetectContext),
230 .priv_class = &freezedetect_class,
232 .query_formats = query_formats,
233 .inputs = freezedetect_inputs,
234 .outputs = freezedetect_outputs,
235 .activate = activate,