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