2 * Copyright (c) 2018 Marton Balint
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/opt.h"
22 #include "libavutil/time.h"
27 typedef struct CueContext {
36 static int activate(AVFilterContext *ctx)
38 AVFilterLink *inlink = ctx->inputs[0];
39 AVFilterLink *outlink = ctx->outputs[0];
40 CueContext *s = ctx->priv;
42 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
44 if (ff_inlink_queued_frames(inlink)) {
45 AVFrame *frame = ff_inlink_peek_frame(inlink, 0);
46 int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
53 if (pts - s->first_pts < s->preroll) {
54 int ret = ff_inlink_consume_frame(inlink, &frame);
57 return ff_filter_frame(outlink, frame);
63 frame = ff_inlink_peek_frame(inlink, ff_inlink_queued_frames(inlink) - 1);
64 pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
65 if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
70 while ((diff = (av_gettime() - s->cue)) < 0)
71 av_usleep(av_clip(-diff / 2, 100, 1000000));
75 int ret = ff_inlink_consume_frame(inlink, &frame);
78 return ff_filter_frame(outlink, frame);
82 FF_FILTER_FORWARD_STATUS(inlink, outlink);
83 FF_FILTER_FORWARD_WANTED(outlink, inlink);
85 return FFERROR_NOT_READY;
88 #define OFFSET(x) offsetof(CueContext, x)
89 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
90 static const AVOption options[] = {
91 { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
92 { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
93 { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
98 #define cue_options options
99 AVFILTER_DEFINE_CLASS(cue);
101 static const AVFilterPad cue_inputs[] = {
104 .type = AVMEDIA_TYPE_VIDEO,
109 static const AVFilterPad cue_outputs[] = {
112 .type = AVMEDIA_TYPE_VIDEO,
117 AVFilter ff_vf_cue = {
119 .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
120 .priv_size = sizeof(CueContext),
121 .priv_class = &cue_class,
122 .inputs = cue_inputs,
123 .outputs = cue_outputs,
124 .activate = activate,
126 #endif /* CONFIG_CUE_FILTER */
128 #if CONFIG_ACUE_FILTER
129 #define acue_options options
130 AVFILTER_DEFINE_CLASS(acue);
132 static const AVFilterPad acue_inputs[] = {
135 .type = AVMEDIA_TYPE_AUDIO,
140 static const AVFilterPad acue_outputs[] = {
143 .type = AVMEDIA_TYPE_AUDIO,
148 AVFilter ff_af_acue = {
150 .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
151 .priv_size = sizeof(CueContext),
152 .priv_class = &acue_class,
153 .inputs = acue_inputs,
154 .outputs = acue_outputs,
155 .activate = activate,
157 #endif /* CONFIG_ACUE_FILTER */