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 "config_components.h"
22 
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct LatencyContext {
34 
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 
46 {
47  LatencyContext *s = ctx->priv;
48  AVFilterLink *inlink = ctx->inputs[0];
49  AVFilterLink *outlink = ctx->outputs[0];
50 
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 
74  AVFrame *frame = NULL;
75  int ret;
76 
78  if (ret < 0)
79  return ret;
80  if (ret > 0)
81  return ff_filter_frame(outlink, frame);
82  }
83 
86 
87  return FFERROR_NOT_READY;
88 }
89 
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 const AVFilter ff_vf_latency = {
103  .name = "latency",
104  .description = NULL_IF_CONFIG_SMALL("Report video filtering latency."),
105  .priv_size = sizeof(LatencyContext),
106  .init = init,
107  .uninit = uninit,
108  .activate = activate,
113 };
114 
115 #endif // CONFIG_LATENCY_FILTER
116 
117 #if CONFIG_ALATENCY_FILTER
118 
119 const AVFilter ff_af_alatency = {
120  .name = "alatency",
121  .description = NULL_IF_CONFIG_SMALL("Report audio filtering latency."),
122  .priv_size = sizeof(LatencyContext),
123  .init = init,
124  .uninit = uninit,
125  .activate = activate,
129 };
130 #endif // CONFIG_ALATENCY_FILTER
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
int64_t
long long int64_t
Definition: coverity.c:34
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:374
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
LatencyContext::sum
int64_t sum
Definition: f_latency.c:32
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
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
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:1442
av_cold
#define av_cold
Definition: attributes.h:90
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_latency.c:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
LatencyContext::min_latency
int64_t min_latency
Definition: f_latency.c:30
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
NULL
#define NULL
Definition: coverity.c:32
LatencyContext::max_latency
int64_t max_latency
Definition: f_latency.c:31
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:33
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1405
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:94
activate
static int activate(AVFilterContext *ctx)
Definition: f_latency.c:45
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:29
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilter
Filter definition.
Definition: avfilter.h:166
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:133
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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:155
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_latency.c:35
ff_af_alatency
const AVFilter ff_af_alatency