FFmpeg
f_cue.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Marton Balint
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 License
8  * 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 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 "libavutil/time.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct CueContext {
32  const AVClass *class;
33  int64_t first_pts;
34  int64_t cue;
35  int64_t preroll;
36  int64_t buffer;
37  int status;
38 } CueContext;
39 
41 {
42  AVFilterLink *inlink = ctx->inputs[0];
43  AVFilterLink *outlink = ctx->outputs[0];
44  CueContext *s = ctx->priv;
45 
47 
50  int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
51 
52  if (!s->status) {
53  s->first_pts = pts;
54  s->status++;
55  }
56  if (s->status == 1) {
57  if (pts - s->first_pts < s->preroll) {
59  if (ret < 0)
60  return ret;
61  return ff_filter_frame(outlink, frame);
62  }
63  s->first_pts = pts;
64  s->status++;
65  }
66  if (s->status == 2) {
68  pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
69  if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
70  s->status++;
71  }
72  if (s->status == 3) {
73  int64_t diff;
74  while ((diff = (av_gettime() - s->cue)) < 0)
75  av_usleep(av_clip(-diff / 2, 100, 1000000));
76  s->status++;
77  }
78  if (s->status == 4) {
80  if (ret < 0)
81  return ret;
82  return ff_filter_frame(outlink, frame);
83  }
84  }
85 
88 
89  return FFERROR_NOT_READY;
90 }
91 
92 #define OFFSET(x) offsetof(CueContext, x)
93 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
94 static const AVOption options[] = {
95  { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
96  { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
97  { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
98  { NULL }
99 };
100 
101 AVFILTER_DEFINE_CLASS_EXT(cue_acue, "(a)cue", options);
102 
103 #if CONFIG_CUE_FILTER
104 const AVFilter ff_vf_cue = {
105  .name = "cue",
106  .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
107  .priv_class = &cue_acue_class,
108  .priv_size = sizeof(CueContext),
111  .activate = activate,
112 };
113 #endif /* CONFIG_CUE_FILTER */
114 
115 #if CONFIG_ACUE_FILTER
116 const AVFilter ff_af_acue = {
117  .name = "acue",
118  .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
119  .priv_class = &cue_acue_class,
120  .priv_size = sizeof(CueContext),
124  .activate = activate,
125 };
126 #endif /* CONFIG_ACUE_FILTER */
av_clip
#define av_clip
Definition: common.h:96
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:978
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
CueContext::cue
int64_t cue
Definition: f_cue.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
FLAGS
#define FLAGS
Definition: f_cue.c:93
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVOption
AVOption.
Definition: opt.h:251
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
CueContext::buffer
int64_t buffer
Definition: f_cue.c:36
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
activate
static int activate(AVFilterContext *ctx)
Definition: f_cue.c:40
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:1383
CueContext::first_pts
int64_t first_pts
Definition: f_cue.c:33
CueContext
Definition: f_cue.c:31
CueContext::preroll
int64_t preroll
Definition: f_cue.c:35
options
static const AVOption options[]
Definition: f_cue.c:94
pts
static int64_t pts
Definition: transcode_aac.c:643
ff_af_acue
const AVFilter ff_af_acue
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:36
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_inlink_peek_frame
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
Definition: avfilter.c:1422
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(cue_acue, "(a)cue", options)
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:32
time.h
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1352
CueContext::status
int status
Definition: f_cue.c:37
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:106
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
internal.h
OFFSET
#define OFFSET(x)
Definition: f_cue.c:92
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
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:397
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
audio.h
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
ff_vf_cue
const AVFilter ff_vf_cue