FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_freezedetect.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 60 66 90.9%
Branches: 31 44 70.5%

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