FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "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 
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  int ret = ff_inlink_consume_frame(inlink, &frame);
55  if (ret < 0)
56  return ret;
57  return ff_filter_frame(outlink, frame);
58  }
59  s->first_pts = pts;
60  s->status++;
61  }
62  if (s->status == 2) {
63  frame = ff_inlink_peek_frame(inlink, ff_inlink_queued_frames(inlink) - 1);
64  pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
65  if (!(pts - s->first_pts < s->buffer && (av_gettime() - s->cue) < 0))
66  s->status++;
67  }
68  if (s->status == 3) {
69  int64_t diff;
70  while ((diff = (av_gettime() - s->cue)) < 0)
71  av_usleep(av_clip(-diff / 2, 100, 1000000));
72  s->status++;
73  }
74  if (s->status == 4) {
75  int ret = ff_inlink_consume_frame(inlink, &frame);
76  if (ret < 0)
77  return ret;
78  return ff_filter_frame(outlink, frame);
79  }
80  }
81 
82  FF_FILTER_FORWARD_STATUS(inlink, outlink);
83  FF_FILTER_FORWARD_WANTED(outlink, inlink);
84 
85  return FFERROR_NOT_READY;
86 }
87 
88 #define OFFSET(x) offsetof(CueContext, x)
89 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
90 static const AVOption options[] = {
91  { "cue", "cue unix timestamp in microseconds", OFFSET(cue), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
92  { "preroll", "preroll duration in seconds", OFFSET(preroll), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
93  { "buffer", "buffer duration in seconds", OFFSET(buffer), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT64_MAX, FLAGS },
94  { NULL }
95 };
96 
97 #if CONFIG_CUE_FILTER
98 #define cue_options options
100 
101 static const AVFilterPad cue_inputs[] = {
102  {
103  .name = "default",
104  .type = AVMEDIA_TYPE_VIDEO,
105  },
106  { NULL }
107 };
108 
109 static const AVFilterPad cue_outputs[] = {
110  {
111  .name = "default",
112  .type = AVMEDIA_TYPE_VIDEO,
113  },
114  { NULL }
115 };
116 
118  .name = "cue",
119  .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
120  .priv_size = sizeof(CueContext),
121  .priv_class = &cue_class,
122  .inputs = cue_inputs,
123  .outputs = cue_outputs,
124  .activate = activate,
125 };
126 #endif /* CONFIG_CUE_FILTER */
127 
128 #if CONFIG_ACUE_FILTER
129 #define acue_options options
131 
132 static const AVFilterPad acue_inputs[] = {
133  {
134  .name = "default",
135  .type = AVMEDIA_TYPE_AUDIO,
136  },
137  { NULL }
138 };
139 
140 static const AVFilterPad acue_outputs[] = {
141  {
142  .name = "default",
143  .type = AVMEDIA_TYPE_AUDIO,
144  },
145  { NULL }
146 };
147 
149  .name = "acue",
150  .description = NULL_IF_CONFIG_SMALL("Delay filtering to match a cue."),
151  .priv_size = sizeof(CueContext),
152  .priv_class = &acue_class,
153  .inputs = acue_inputs,
154  .outputs = acue_outputs,
155  .activate = activate,
156 };
157 #endif /* CONFIG_ACUE_FILTER */
int64_t cue
Definition: f_cue.c:30
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:1481
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: f_cue.c:88
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
AVFilter ff_af_acue
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
AVFilter ff_vf_cue
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
A filter pad used for either input or output.
Definition: internal.h:54
#define FLAGS
Definition: f_cue.c:89
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
int64_t first_pts
Definition: f_cue.c:29
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:254
static const AVOption options[]
Definition: f_cue.c:90
static int activate(AVFilterContext *ctx)
Definition: f_cue.c:36
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
Definition: avfilter.c:1520
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int64_t buffer
Definition: f_cue.c:32
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int64_t preroll
Definition: f_cue.c:31
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1451
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
int status
Definition: f_cue.c:33
static int64_t pts
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
An instance of a filter.
Definition: avfilter.h:338
internal API functions
GLuint buffer
Definition: opengl_enc.c:102