]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_cue.c
Merge commit '76eef04f30a768fa80366d679f3de7e9447b67d5'
[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 "framequeue.h"
26 #include "internal.h"
27
28 typedef struct CueContext {
29     const AVClass *class;
30     int64_t first_pts;
31     int64_t cue;
32     int64_t preroll;
33     int64_t buffer;
34     int status;
35     FFFrameQueue queue;
36 } CueContext;
37
38 static av_cold int init(AVFilterContext *ctx)
39 {
40     CueContext *s = ctx->priv;
41     ff_framequeue_init(&s->queue, &ctx->graph->internal->frame_queues);
42     return 0;
43 }
44
45 static av_cold void uninit(AVFilterContext *ctx)
46 {
47     CueContext *s = ctx->priv;
48     ff_framequeue_free(&s->queue);
49 }
50
51 static int activate(AVFilterContext *ctx)
52 {
53     AVFilterLink *inlink = ctx->inputs[0];
54     AVFilterLink *outlink = ctx->outputs[0];
55     CueContext *s = ctx->priv;
56     int64_t pts;
57     AVFrame *frame = NULL;
58
59     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
60
61     if (s->status < 3 || s->status == 5) {
62         int ret = ff_inlink_consume_frame(inlink, &frame);
63         if (ret < 0)
64             return ret;
65         if (frame)
66             pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
67     }
68
69     if (!s->status && frame) {
70         s->first_pts = pts;
71         s->status++;
72     }
73     if (s->status == 1 && frame) {
74         if (pts - s->first_pts < s->preroll)
75             return ff_filter_frame(outlink, frame);
76         s->first_pts = pts;
77         s->status++;
78     }
79     if (s->status == 2 && frame) {
80         int ret = ff_framequeue_add(&s->queue, frame);
81         if (ret < 0) {
82             av_frame_free(&frame);
83             return ret;
84         }
85         frame = NULL;
86         if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
87             s->status++;
88     }
89     if (s->status == 3) {
90         int64_t diff;
91         while ((diff = (av_gettime() - s->cue)) < 0)
92             av_usleep(av_clip(-diff / 2, 100, 1000000));
93         s->status++;
94     }
95     if (s->status == 4) {
96         if (ff_framequeue_queued_frames(&s->queue))
97             return ff_filter_frame(outlink, ff_framequeue_take(&s->queue));
98         s->status++;
99     }
100     if (s->status == 5 && frame)
101         return ff_filter_frame(outlink, frame);
102
103     FF_FILTER_FORWARD_STATUS(inlink, outlink);
104     FF_FILTER_FORWARD_WANTED(outlink, inlink);
105
106     return FFERROR_NOT_READY;
107 }
108
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 },
115     { NULL }
116 };
117
118 #if CONFIG_CUE_FILTER
119 #define cue_options options
120 AVFILTER_DEFINE_CLASS(cue);
121
122 static const AVFilterPad cue_inputs[] = {
123     {
124         .name = "default",
125         .type = AVMEDIA_TYPE_VIDEO,
126     },
127     { NULL }
128 };
129
130 static const AVFilterPad cue_outputs[] = {
131     {
132         .name = "default",
133         .type = AVMEDIA_TYPE_VIDEO,
134     },
135     { NULL }
136 };
137
138 AVFilter ff_vf_cue = {
139     .name        = "cue",
140     .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
141     .priv_size   = sizeof(CueContext),
142     .priv_class  = &cue_class,
143     .init        = init,
144     .uninit      = uninit,
145     .inputs      = cue_inputs,
146     .outputs     = cue_outputs,
147     .activate    = activate,
148 };
149 #endif /* CONFIG_CUE_FILTER */
150
151 #if CONFIG_ACUE_FILTER
152 #define acue_options options
153 AVFILTER_DEFINE_CLASS(acue);
154
155 static const AVFilterPad acue_inputs[] = {
156     {
157         .name = "default",
158         .type = AVMEDIA_TYPE_AUDIO,
159     },
160     { NULL }
161 };
162
163 static const AVFilterPad acue_outputs[] = {
164     {
165         .name = "default",
166         .type = AVMEDIA_TYPE_AUDIO,
167     },
168     { NULL }
169 };
170
171 AVFilter ff_af_acue = {
172     .name        = "acue",
173     .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
174     .priv_size   = sizeof(CueContext),
175     .priv_class  = &acue_class,
176     .init        = init,
177     .uninit      = uninit,
178     .inputs      = acue_inputs,
179     .outputs     = acue_outputs,
180     .activate    = activate,
181 };
182 #endif /* CONFIG_ACUE_FILTER */