FFmpeg
f_latency.c
Go to the documentation of this file.
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 "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "filters.h"
24 #include "formats.h"
25 #include "internal.h"
26 
27 typedef struct LatencyContext {
28  int64_t min_latency;
29  int64_t max_latency;
30  int64_t sum;
32 
34 {
35  LatencyContext *s = ctx->priv;
36 
37  s->min_latency = INT64_MAX;
38  s->max_latency = INT64_MIN;
39 
40  return 0;
41 }
42 
44 {
45  LatencyContext *s = ctx->priv;
46  AVFilterLink *inlink = ctx->inputs[0];
47  AVFilterLink *outlink = ctx->outputs[0];
48 
50 
51  if (!ctx->is_disabled && ctx->inputs[0]->src &&
52  ctx->inputs[0]->src->nb_inputs > 0) {
53  AVFilterLink *prevlink = ctx->inputs[0]->src->inputs[0];
54  int64_t delta = 0;
55 
56  switch (prevlink->type) {
57  case AVMEDIA_TYPE_AUDIO:
58  delta = prevlink->sample_count_in - inlink->sample_count_out;
59  break;
60  case AVMEDIA_TYPE_VIDEO:
61  delta = prevlink->frame_count_in - inlink->frame_count_out;
62  break;
63  }
64 
65  if (delta > 0) {
66  s->min_latency = FFMIN(s->min_latency, delta);
67  s->max_latency = FFMAX(s->max_latency, delta);
68  }
69  }
70 
72  AVFrame *frame = NULL;
73  int ret;
74 
76  if (ret < 0)
77  return ret;
78  if (ret > 0)
79  return ff_filter_frame(outlink, frame);
80  }
81 
84 
85  return FFERROR_NOT_READY;
86 }
87 
89 {
90  LatencyContext *s = ctx->priv;
91 
92  if (s->min_latency != INT64_MAX)
93  av_log(ctx, AV_LOG_INFO, "Min latency: %"PRId64"\n", s->min_latency);
94  if (s->max_latency != INT64_MIN)
95  av_log(ctx, AV_LOG_INFO, "Max latency: %"PRId64"\n", s->max_latency);
96 }
97 
98 #if CONFIG_LATENCY_FILTER
99 
100 static const AVFilterPad latency_inputs[] = {
101  {
102  .name = "default",
103  .type = AVMEDIA_TYPE_VIDEO,
104  },
105 };
106 
107 static const AVFilterPad latency_outputs[] = {
108  {
109  .name = "default",
110  .type = AVMEDIA_TYPE_VIDEO,
111  },
112 };
113 
114 const AVFilter ff_vf_latency = {
115  .name = "latency",
116  .description = NULL_IF_CONFIG_SMALL("Report video filtering latency."),
117  .priv_size = sizeof(LatencyContext),
118  .init = init,
119  .uninit = uninit,
120  .activate = activate,
123  FILTER_INPUTS(latency_inputs),
124  FILTER_OUTPUTS(latency_outputs),
125 };
126 
127 #endif // CONFIG_LATENCY_FILTER
128 
129 #if CONFIG_ALATENCY_FILTER
130 
131 static const AVFilterPad alatency_inputs[] = {
132  {
133  .name = "default",
134  .type = AVMEDIA_TYPE_AUDIO,
135  },
136 };
137 
138 static const AVFilterPad alatency_outputs[] = {
139  {
140  .name = "default",
141  .type = AVMEDIA_TYPE_AUDIO,
142  },
143 };
144 
145 const AVFilter ff_af_alatency = {
146  .name = "alatency",
147  .description = NULL_IF_CONFIG_SMALL("Report audio filtering latency."),
148  .priv_size = sizeof(LatencyContext),
149  .init = init,
150  .uninit = uninit,
151  .activate = activate,
153  FILTER_INPUTS(alatency_inputs),
154  FILTER_OUTPUTS(alatency_outputs),
155 };
156 #endif // CONFIG_ALATENCY_FILTER
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
LatencyContext::sum
int64_t sum
Definition: f_latency.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1417
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
av_cold
#define av_cold
Definition: attributes.h:90
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_latency.c:88
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
LatencyContext::min_latency
int64_t min_latency
Definition: f_latency.c:28
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
NULL
#define NULL
Definition: coverity.c:32
LatencyContext::max_latency
int64_t max_latency
Definition: f_latency.c:29
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1386
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
activate
static int activate(AVFilterContext *ctx)
Definition: f_latency.c:43
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
LatencyContext
Definition: f_latency.c:27
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_vf_latency
const AVFilter ff_vf_latency
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:137
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:154
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_latency.c:33
ff_af_alatency
const AVFilter ff_af_alatency