FFmpeg
Loading...
Searching...
No Matches
f_cue.c
Go to the documentation of this file.
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 "audio.h"
26#include "avfilter.h"
27#include "filters.h"
28#include "video.h"
29
38
40{
41 AVFilterLink *inlink = ctx->inputs[0];
42 AVFilterLink *outlink = ctx->outputs[0];
43 CueContext *s = ctx->priv;
44
45 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
46
47 if (ff_inlink_queued_frames(inlink)) {
50
51 if (!s->status) {
52 s->first_pts = pts;
53 s->status++;
54 }
55 if (s->status == 1) {
56 if (pts - s->first_pts < s->preroll) {
57 int ret = ff_inlink_consume_frame(inlink, &frame);
58 if (ret < 0)
59 return ret;
60 return ff_filter_frame(outlink, frame);
61 }
62 s->first_pts = pts;
63 s->status++;
64 }
65 if (s->status == 2) {
68 if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
69 s->status++;
70 }
71 if (s->status == 3) {
73 while ((diff = (av_gettime() - s->cue)) < 0)
74 av_usleep(av_clip(-diff / 2, 100, 1000000));
75 s->status++;
76 }
77 if (s->status == 4) {
78 int ret = ff_inlink_consume_frame(inlink, &frame);
79 if (ret < 0)
80 return ret;
81 return ff_filter_frame(outlink, frame);
82 }
83 }
84
85 FF_FILTER_FORWARD_STATUS(inlink, outlink);
86 FF_FILTER_FORWARD_WANTED(outlink, inlink);
87
88 return FFERROR_NOT_READY;
89}
90
91#define OFFSET(x) offsetof(CueContext, x)
92#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
93static const AVOption options[] = {
94 { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
95 { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
96 { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
97 { NULL }
98};
99
101
102#if CONFIG_CUE_FILTER
103const FFFilter ff_vf_cue = {
104 .p.name = "cue",
105 .p.description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
106 .p.priv_class = &cue_acue_class,
107 .priv_size = sizeof(CueContext),
110 .activate = activate,
111};
112#endif /* CONFIG_CUE_FILTER */
113
114#if CONFIG_ACUE_FILTER
115const FFFilter ff_af_acue = {
116 .p.name = "acue",
117 .p.description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
118 .p.priv_class = &cue_acue_class,
120 .priv_size = sizeof(CueContext),
123 .activate = activate,
124};
125#endif /* CONFIG_ACUE_FILTER */
const FFFilter ff_af_acue
const FFFilter ff_vf_cue
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition avfilter.c:1483
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
Definition avfilter.c:1561
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int activate(AVFilterContext *ctx)
Definition f_cue.c:39
#define OFFSET(x)
Definition f_cue.c:91
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:639
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
int64_t first_pts
Definition f_cue.c:32
int64_t buffer
Definition f_cue.c:35
int status
Definition f_cue.c:36
int64_t preroll
Definition f_cue.c:34
int64_t cue
Definition f_cue.c:33
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
static int64_t pts
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37