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 /**
20 * @file
21 * video freeze detection filter
22 */
23
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"
29
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "scene_sad.h"
33
34 typedef struct FreezeDetectContext {
35 const AVClass *class;
36
37 ptrdiff_t width[4];
38 ptrdiff_t height[4];
39 ff_scene_sad_fn sad;
40 int bitdepth;
41 AVFrame *reference_frame;
42 int64_t n;
43 int64_t reference_n;
44 int frozen;
45
46 double noise;
47 int64_t duration; ///< minimum duration of frozen frame until notification
48 } FreezeDetectContext;
49
50 #define OFFSET(x) offsetof(FreezeDetectContext, x)
51 #define V AV_OPT_FLAG_VIDEO_PARAM
52 #define F AV_OPT_FLAG_FILTERING_PARAM
53
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 },
59
60 {NULL}
61 };
62
63 AVFILTER_DEFINE_CLASS(freezedetect);
64
query_formats(AVFilterContext * ctx)65 static int query_formats(AVFilterContext *ctx)
66 {
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,
90 AV_PIX_FMT_GRAY14,
91 AV_PIX_FMT_NONE
92 };
93
94 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
95 if (!fmts_list)
96 return AVERROR(ENOMEM);
97 return ff_set_common_formats(ctx, fmts_list);
98 }
99
config_input(AVFilterLink * inlink)100 static int config_input(AVFilterLink *inlink)
101 {
102 AVFilterContext *ctx = inlink->dst;
103 FreezeDetectContext *s = ctx->priv;
104 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
105
106 s->bitdepth = pix_desc->comp[0].depth;
107
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);
112 }
113
114 s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
115 if (!s->sad)
116 return AVERROR(EINVAL);
117
118 return 0;
119 }
120
uninit(AVFilterContext * ctx)121 static av_cold void uninit(AVFilterContext *ctx)
122 {
123 FreezeDetectContext *s = ctx->priv;
124 av_frame_free(&s->reference_frame);
125 }
126
is_frozen(FreezeDetectContext * s,AVFrame * reference,AVFrame * frame)127 static int is_frozen(FreezeDetectContext *s, AVFrame *reference, AVFrame *frame)
128 {
129 uint64_t sad = 0;
130 uint64_t count = 0;
131 double mafd;
132 for (int plane = 0; plane < 4; plane++) {
133 if (s->width[plane]) {
134 uint64_t plane_sad;
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);
138 sad += plane_sad;
139 count += s->width[plane] * s->height[plane];
140 }
141 }
142 emms_c();
143 mafd = (double)sad / count / (1ULL << s->bitdepth);
144 return (mafd <= s->noise);
145 }
146
set_meta(FreezeDetectContext * s,AVFrame * frame,const char * key,const char * value)147 static int set_meta(FreezeDetectContext *s, AVFrame *frame, const char *key, const char *value)
148 {
149 av_log(s, AV_LOG_INFO, "%s: %s\n", key, value);
150 return av_dict_set(&frame->metadata, key, value, 0);
151 }
152
activate(AVFilterContext * ctx)153 static int activate(AVFilterContext *ctx)
154 {
155 int ret;
156 AVFilterLink *inlink = ctx->inputs[0];
157 AVFilterLink *outlink = ctx->outputs[0];
158 FreezeDetectContext *s = ctx->priv;
159 AVFrame *frame;
160
161 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
162
163 ret = ff_inlink_consume_frame(inlink, &frame);
164 if (ret < 0)
165 return ret;
166
167 if (frame) {
168 int frozen = 0;
169 s->n++;
170
171 if (s->reference_frame) {
172 int64_t duration;
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;
175 else
176 duration = av_rescale_q(frame->pts - s->reference_frame->pts, inlink->time_base, AV_TIME_BASE_Q);
177
178 frozen = is_frozen(s, s->reference_frame, frame);
179 if (duration >= s->duration) {
180 if (!s->frozen)
181 set_meta(s, frame, "lavfi.freezedetect.freeze_start", av_ts2timestr(s->reference_frame->pts, &inlink->time_base));
182 if (!frozen) {
183 set_meta(s, frame, "lavfi.freezedetect.freeze_duration", av_ts2timestr(duration, &AV_TIME_BASE_Q));
184 set_meta(s, frame, "lavfi.freezedetect.freeze_end", av_ts2timestr(frame->pts, &inlink->time_base));
185 }
186 s->frozen = frozen;
187 }
188 }
189
190 if (!frozen) {
191 av_frame_free(&s->reference_frame);
192 s->reference_frame = av_frame_clone(frame);
193 s->reference_n = s->n;
194 if (!s->reference_frame) {
195 av_frame_free(&frame);
196 return AVERROR(ENOMEM);
197 }
198 }
199 return ff_filter_frame(outlink, frame);
200 }
201
202 FF_FILTER_FORWARD_STATUS(inlink, outlink);
203 FF_FILTER_FORWARD_WANTED(outlink, inlink);
204
205 return FFERROR_NOT_READY;
206 }
207
208 static const AVFilterPad freezedetect_inputs[] = {
209 {
210 .name = "default",
211 .type = AVMEDIA_TYPE_VIDEO,
212 .config_props = config_input,
213 },
214 { NULL }
215 };
216
217 static const AVFilterPad freezedetect_outputs[] = {
218 {
219 .name = "default",
220 .type = AVMEDIA_TYPE_VIDEO,
221 },
222 { NULL }
223 };
224
225 AVFilter ff_vf_freezedetect = {
226 .name = "freezedetect",
227 .description = NULL_IF_CONFIG_SMALL("Detects frozen video input."),
228 .priv_size = sizeof(FreezeDetectContext),
229 .priv_class = &freezedetect_class,
230 .uninit = uninit,
231 .query_formats = query_formats,
232 .inputs = freezedetect_inputs,
233 .outputs = freezedetect_outputs,
234 .activate = activate,
235 };
236