• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "config_components.h"
22 
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "internal.h"
28 
29 typedef struct LatencyContext {
30     int64_t min_latency;
31     int64_t max_latency;
32     int64_t sum;
33 } LatencyContext;
34 
init(AVFilterContext * ctx)35 static av_cold int init(AVFilterContext *ctx)
36 {
37     LatencyContext *s = ctx->priv;
38 
39     s->min_latency = INT64_MAX;
40     s->max_latency = INT64_MIN;
41 
42     return 0;
43 }
44 
activate(AVFilterContext * ctx)45 static int activate(AVFilterContext *ctx)
46 {
47     LatencyContext *s = ctx->priv;
48     AVFilterLink *inlink = ctx->inputs[0];
49     AVFilterLink *outlink = ctx->outputs[0];
50 
51     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
52 
53     if (!ctx->is_disabled && ctx->inputs[0]->src &&
54         ctx->inputs[0]->src->nb_inputs > 0) {
55         AVFilterLink *prevlink = ctx->inputs[0]->src->inputs[0];
56         int64_t delta = 0;
57 
58         switch (prevlink->type) {
59         case AVMEDIA_TYPE_AUDIO:
60             delta = prevlink->sample_count_in - inlink->sample_count_out;
61             break;
62         case AVMEDIA_TYPE_VIDEO:
63             delta = prevlink->frame_count_in - inlink->frame_count_out;
64             break;
65         }
66 
67         if (delta > 0) {
68             s->min_latency = FFMIN(s->min_latency, delta);
69             s->max_latency = FFMAX(s->max_latency, delta);
70         }
71     }
72 
73     if (ff_inlink_queued_frames(inlink)) {
74         AVFrame *frame = NULL;
75         int ret;
76 
77         ret = ff_inlink_consume_frame(inlink, &frame);
78         if (ret < 0)
79             return ret;
80         if (ret > 0)
81             return ff_filter_frame(outlink, frame);
82     }
83 
84     FF_FILTER_FORWARD_STATUS(inlink, outlink);
85     FF_FILTER_FORWARD_WANTED(outlink, inlink);
86 
87     return FFERROR_NOT_READY;
88 }
89 
uninit(AVFilterContext * ctx)90 static av_cold void uninit(AVFilterContext *ctx)
91 {
92     LatencyContext *s = ctx->priv;
93 
94     if (s->min_latency != INT64_MAX)
95         av_log(ctx, AV_LOG_INFO, "Min latency: %"PRId64"\n", s->min_latency);
96     if (s->max_latency != INT64_MIN)
97         av_log(ctx, AV_LOG_INFO, "Max latency: %"PRId64"\n", s->max_latency);
98 }
99 
100 #if CONFIG_LATENCY_FILTER
101 
102 static const AVFilterPad latency_inputs[] = {
103     {
104         .name = "default",
105         .type = AVMEDIA_TYPE_VIDEO,
106     },
107 };
108 
109 static const AVFilterPad latency_outputs[] = {
110     {
111         .name = "default",
112         .type = AVMEDIA_TYPE_VIDEO,
113     },
114 };
115 
116 const AVFilter ff_vf_latency = {
117     .name          = "latency",
118     .description   = NULL_IF_CONFIG_SMALL("Report video filtering latency."),
119     .priv_size     = sizeof(LatencyContext),
120     .init          = init,
121     .uninit        = uninit,
122     .activate      = activate,
123     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
124                      AVFILTER_FLAG_METADATA_ONLY,
125     FILTER_INPUTS(latency_inputs),
126     FILTER_OUTPUTS(latency_outputs),
127 };
128 
129 #endif // CONFIG_LATENCY_FILTER
130 
131 #if CONFIG_ALATENCY_FILTER
132 
133 static const AVFilterPad alatency_inputs[] = {
134     {
135         .name = "default",
136         .type = AVMEDIA_TYPE_AUDIO,
137     },
138 };
139 
140 static const AVFilterPad alatency_outputs[] = {
141     {
142         .name = "default",
143         .type = AVMEDIA_TYPE_AUDIO,
144     },
145 };
146 
147 const AVFilter ff_af_alatency = {
148     .name          = "alatency",
149     .description   = NULL_IF_CONFIG_SMALL("Report audio filtering latency."),
150     .priv_size     = sizeof(LatencyContext),
151     .init          = init,
152     .uninit        = uninit,
153     .activate      = activate,
154     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
155     FILTER_INPUTS(alatency_inputs),
156     FILTER_OUTPUTS(alatency_outputs),
157 };
158 #endif // CONFIG_ALATENCY_FILTER
159