• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "avfilter.h"
26 #include "filters.h"
27 #include "internal.h"
28 
29 typedef struct CueContext {
30     const AVClass *class;
31     int64_t first_pts;
32     int64_t cue;
33     int64_t preroll;
34     int64_t buffer;
35     int status;
36 } CueContext;
37 
activate(AVFilterContext * ctx)38 static int activate(AVFilterContext *ctx)
39 {
40     AVFilterLink *inlink = ctx->inputs[0];
41     AVFilterLink *outlink = ctx->outputs[0];
42     CueContext *s = ctx->priv;
43 
44     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
45 
46     if (ff_inlink_queued_frames(inlink)) {
47         AVFrame *frame = ff_inlink_peek_frame(inlink, 0);
48         int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
49 
50         if (!s->status) {
51             s->first_pts = pts;
52             s->status++;
53         }
54         if (s->status == 1) {
55             if (pts - s->first_pts < s->preroll) {
56                 int ret = ff_inlink_consume_frame(inlink, &frame);
57                 if (ret < 0)
58                     return ret;
59                 return ff_filter_frame(outlink, frame);
60             }
61             s->first_pts = pts;
62             s->status++;
63         }
64         if (s->status == 2) {
65             frame = ff_inlink_peek_frame(inlink, ff_inlink_queued_frames(inlink) - 1);
66             pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
67             if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
68                 s->status++;
69         }
70         if (s->status == 3) {
71             int64_t diff;
72             while ((diff = (av_gettime() - s->cue)) < 0)
73                 av_usleep(av_clip(-diff / 2, 100, 1000000));
74             s->status++;
75         }
76         if (s->status == 4) {
77             int ret = ff_inlink_consume_frame(inlink, &frame);
78             if (ret < 0)
79                 return ret;
80             return ff_filter_frame(outlink, frame);
81         }
82     }
83 
84     FF_FILTER_FORWARD_STATUS(inlink, outlink);
85     FF_FILTER_FORWARD_WANTED(outlink, inlink);
86 
87     return FFERROR_NOT_READY;
88 }
89 
90 #define OFFSET(x) offsetof(CueContext, x)
91 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
92 static const AVOption options[] = {
93     { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
94     { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
95     { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
96     { NULL }
97 };
98 
99 AVFILTER_DEFINE_CLASS_EXT(cue_acue, "(a)cue", options);
100 
101 #if CONFIG_CUE_FILTER
102 static const AVFilterPad cue_inputs[] = {
103     {
104         .name = "default",
105         .type = AVMEDIA_TYPE_VIDEO,
106     },
107 };
108 
109 static const AVFilterPad cue_outputs[] = {
110     {
111         .name = "default",
112         .type = AVMEDIA_TYPE_VIDEO,
113     },
114 };
115 
116 const AVFilter ff_vf_cue = {
117     .name        = "cue",
118     .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
119     .priv_class  = &cue_acue_class,
120     .priv_size   = sizeof(CueContext),
121     FILTER_INPUTS(cue_inputs),
122     FILTER_OUTPUTS(cue_outputs),
123     .activate    = activate,
124 };
125 #endif /* CONFIG_CUE_FILTER */
126 
127 #if CONFIG_ACUE_FILTER
128 static const AVFilterPad acue_inputs[] = {
129     {
130         .name = "default",
131         .type = AVMEDIA_TYPE_AUDIO,
132     },
133 };
134 
135 static const AVFilterPad acue_outputs[] = {
136     {
137         .name = "default",
138         .type = AVMEDIA_TYPE_AUDIO,
139     },
140 };
141 
142 const AVFilter ff_af_acue = {
143     .name        = "acue",
144     .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
145     .priv_class  = &cue_acue_class,
146     .priv_size   = sizeof(CueContext),
147     .flags       = AVFILTER_FLAG_METADATA_ONLY,
148     FILTER_INPUTS(acue_inputs),
149     FILTER_OUTPUTS(acue_outputs),
150     .activate    = activate,
151 };
152 #endif /* CONFIG_ACUE_FILTER */
153