• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
config_input(AVFilterLink * inlink)91 static int config_input(AVFilterLink *inlink)
92 {
93     AVFilterContext *ctx = inlink->dst;
94     FreezeDetectContext *s = ctx->priv;
95     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
96 
97     s->bitdepth = pix_desc->comp[0].depth;
98 
99     for (int plane = 0; plane < 4; plane++) {
100         ptrdiff_t line_size = av_image_get_linesize(inlink->format, inlink->w, plane);
101         s->width[plane] = line_size >> (s->bitdepth > 8);
102         s->height[plane] = inlink->h >> ((plane == 1 || plane == 2) ? pix_desc->log2_chroma_h : 0);
103     }
104 
105     s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
106     if (!s->sad)
107         return AVERROR(EINVAL);
108 
109     return 0;
110 }
111 
uninit(AVFilterContext * ctx)112 static av_cold void uninit(AVFilterContext *ctx)
113 {
114     FreezeDetectContext *s = ctx->priv;
115     av_frame_free(&s->reference_frame);
116 }
117 
is_frozen(FreezeDetectContext * s,AVFrame * reference,AVFrame * frame)118 static int is_frozen(FreezeDetectContext *s, AVFrame *reference, AVFrame *frame)
119 {
120     uint64_t sad = 0;
121     uint64_t count = 0;
122     double mafd;
123     for (int plane = 0; plane < 4; plane++) {
124         if (s->width[plane]) {
125             uint64_t plane_sad;
126             s->sad(frame->data[plane], frame->linesize[plane],
127                    reference->data[plane], reference->linesize[plane],
128                    s->width[plane], s->height[plane], &plane_sad);
129             sad += plane_sad;
130             count += s->width[plane] * s->height[plane];
131         }
132     }
133     emms_c();
134     mafd = (double)sad / count / (1ULL << s->bitdepth);
135     return (mafd <= s->noise);
136 }
137 
set_meta(FreezeDetectContext * s,AVFrame * frame,const char * key,const char * value)138 static int set_meta(FreezeDetectContext *s, AVFrame *frame, const char *key, const char *value)
139 {
140     av_log(s, AV_LOG_INFO, "%s: %s\n", key, value);
141     return av_dict_set(&frame->metadata, key, value, 0);
142 }
143 
activate(AVFilterContext * ctx)144 static int activate(AVFilterContext *ctx)
145 {
146     int ret;
147     AVFilterLink *inlink = ctx->inputs[0];
148     AVFilterLink *outlink = ctx->outputs[0];
149     FreezeDetectContext *s = ctx->priv;
150     AVFrame *frame;
151 
152     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
153 
154     ret = ff_inlink_consume_frame(inlink, &frame);
155     if (ret < 0)
156         return ret;
157 
158     if (frame) {
159         int frozen = 0;
160         s->n++;
161 
162         if (s->reference_frame) {
163             int64_t duration;
164             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                 duration = av_rescale_q(frame->pts - s->reference_frame->pts, inlink->time_base, AV_TIME_BASE_Q);
168 
169             frozen = is_frozen(s, s->reference_frame, frame);
170             if (duration >= s->duration) {
171                 if (!s->frozen)
172                     set_meta(s, frame, "lavfi.freezedetect.freeze_start", av_ts2timestr(s->reference_frame->pts, &inlink->time_base));
173                 if (!frozen) {
174                     set_meta(s, frame, "lavfi.freezedetect.freeze_duration", av_ts2timestr(duration, &AV_TIME_BASE_Q));
175                     set_meta(s, frame, "lavfi.freezedetect.freeze_end", av_ts2timestr(frame->pts, &inlink->time_base));
176                 }
177                 s->frozen = frozen;
178             }
179         }
180 
181         if (!frozen) {
182             av_frame_free(&s->reference_frame);
183             s->reference_frame = av_frame_clone(frame);
184             s->reference_n = s->n;
185             if (!s->reference_frame) {
186                 av_frame_free(&frame);
187                 return AVERROR(ENOMEM);
188             }
189         }
190         return ff_filter_frame(outlink, frame);
191     }
192 
193     FF_FILTER_FORWARD_STATUS(inlink, outlink);
194     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