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"
25 #include "framequeue.h"
28 typedef struct CueContext {
38 static av_cold int init(AVFilterContext *ctx)
40 CueContext *s = ctx->priv;
41 ff_framequeue_init(&s->queue, &ctx->graph->internal->frame_queues);
45 static av_cold void uninit(AVFilterContext *ctx)
47 CueContext *s = ctx->priv;
48 ff_framequeue_free(&s->queue);
51 static int activate(AVFilterContext *ctx)
53 AVFilterLink *inlink = ctx->inputs[0];
54 AVFilterLink *outlink = ctx->outputs[0];
55 CueContext *s = ctx->priv;
57 AVFrame *frame = NULL;
59 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
61 if (s->status < 3 || s->status == 5) {
62 int ret = ff_inlink_consume_frame(inlink, &frame);
66 pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
69 if (!s->status && frame) {
73 if (s->status == 1 && frame) {
74 if (pts - s->first_pts < s->preroll)
75 return ff_filter_frame(outlink, frame);
79 if (s->status == 2 && frame) {
80 int ret = ff_framequeue_add(&s->queue, frame);
82 av_frame_free(&frame);
86 if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
91 while ((diff = (av_gettime() - s->cue)) < 0)
92 av_usleep(av_clip(-diff / 2, 100, 1000000));
96 if (ff_framequeue_queued_frames(&s->queue))
97 return ff_filter_frame(outlink, ff_framequeue_take(&s->queue));
100 if (s->status == 5 && frame)
101 return ff_filter_frame(outlink, frame);
103 FF_FILTER_FORWARD_STATUS(inlink, outlink);
104 FF_FILTER_FORWARD_WANTED(outlink, inlink);
106 return FFERROR_NOT_READY;
109 #define OFFSET(x) offsetof(CueContext, x)
110 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
111 static const AVOption options[] = {
112 { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
113 { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
114 { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
118 #if CONFIG_CUE_FILTER
119 #define cue_options options
120 AVFILTER_DEFINE_CLASS(cue);
122 static const AVFilterPad cue_inputs[] = {
125 .type = AVMEDIA_TYPE_VIDEO,
130 static const AVFilterPad cue_outputs[] = {
133 .type = AVMEDIA_TYPE_VIDEO,
138 AVFilter ff_vf_cue = {
140 .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
141 .priv_size = sizeof(CueContext),
142 .priv_class = &cue_class,
145 .inputs = cue_inputs,
146 .outputs = cue_outputs,
147 .activate = activate,
149 #endif /* CONFIG_CUE_FILTER */
151 #if CONFIG_ACUE_FILTER
152 #define acue_options options
153 AVFILTER_DEFINE_CLASS(acue);
155 static const AVFilterPad acue_inputs[] = {
158 .type = AVMEDIA_TYPE_AUDIO,
163 static const AVFilterPad acue_outputs[] = {
166 .type = AVMEDIA_TYPE_AUDIO,
171 AVFilter ff_af_acue = {
173 .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
174 .priv_size = sizeof(CueContext),
175 .priv_class = &acue_class,
178 .inputs = acue_inputs,
179 .outputs = acue_outputs,
180 .activate = activate,
182 #endif /* CONFIG_ACUE_FILTER */