FFmpeg
vf_fsync.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Thilo Borgmann <thilo.borgmann _at_ mail.de>
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
8  * License 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Filter for syncing video frames from external source
24  *
25  * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
26  */
27 
28 #include "libavutil/avstring.h"
29 #include "libavutil/error.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavformat/avio.h"
33 #include "video.h"
34 #include "filters.h"
35 
36 #define BUF_SIZE 256
37 
38 typedef struct FsyncContext {
39  const AVClass *class;
40  AVIOContext *avio_ctx; // reading the map file
41  AVFrame *last_frame; // buffering the last frame for duplicating eventually
42  char *filename; // user-specified map file
43  char *buf; // line buffer for the map file
44  char *cur; // current position in the line buffer
45  char *end; // end pointer of the line buffer
46  int64_t ptsi; // input pts to map to [0-N] output pts
47  int64_t pts; // output pts
48  int tb_num; // output timebase num
49  int tb_den; // output timebase den
50 } FsyncContext;
51 
52 #define OFFSET(x) offsetof(FsyncContext, x)
53 
54 static const AVOption fsync_options[] = {
55  { "file", "set the file name to use for frame sync", OFFSET(filename), AV_OPT_TYPE_STRING, { .str = "" }, .flags= AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
56  { "f", "set the file name to use for frame sync", OFFSET(filename), AV_OPT_TYPE_STRING, { .str = "" }, .flags= AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
57  { NULL }
58 };
59 
60 /**
61  * Fills the buffer from cur to end, add \0 at EOF
62  */
64 {
65  int ret;
66  int num = ctx->end - ctx->cur;
67 
68  ret = avio_read(ctx->avio_ctx, ctx->cur, num);
69  if (ret < 0)
70  return ret;
71  if (ret < num) {
72  *(ctx->cur + ret) = '\0';
73  }
74 
75  return ret;
76 }
77 
78 /**
79  * Copies cur to end to the beginning and fills the rest
80  */
82 {
83  int i, ret;
84  int num = ctx->end - ctx->cur;
85 
86  for (i = 0; i < num; i++) {
87  ctx->buf[i] = *ctx->cur++;
88  }
89 
90  ctx->cur = ctx->buf + i;
91  ret = buf_fill(ctx);
92  if (ret < 0)
93  return ret;
94  ctx->cur = ctx->buf;
95 
96  return ret;
97 }
98 
99 /**
100  * Skip from cur over eol
101  */
103 {
104  char *i;
105  for (i = ctx->cur; i < ctx->end; i++) {
106  if (*i != '\n')// && *i != '\r')
107  break;
108  }
109  ctx->cur = i;
110 }
111 
112 /**
113  * Get number of bytes from cur until eol
114  *
115  * @return >= 0 in case of success,
116  * -1 in case there is no line ending before end of buffer
117  */
119 {
120  int ret = 0;
121  char *i;
122  for (i = ctx->cur; i < ctx->end; i++, ret++) {
123  if (*i == '\0' || *i == '\n')
124  return ret;
125  }
126 
127  return -1;
128 }
129 
130 /**
131  * Get number of bytes from cur to '\0'
132  */
134 {
135  return av_strnlen(ctx->cur, ctx->end - ctx->cur);
136 }
137 
139 {
140  FsyncContext *s = ctx->priv;
141  AVFilterLink *inlink = ctx->inputs[0];
142  AVFilterLink *outlink = ctx->outputs[0];
143 
144  int ret, line_count;
145  AVFrame *frame;
146 
148 
149  buf_skip_eol(s);
150  line_count = buf_get_line_count(s);
151  if (line_count < 0) {
152  line_count = buf_reload(s);
153  if (line_count < 0)
154  return line_count;
155  line_count = buf_get_line_count(s);
156  if (line_count < 0)
157  return line_count;
158  }
159 
160  if (avio_feof(s->avio_ctx) && buf_get_zero(s) < 3) {
161  av_log(ctx, AV_LOG_DEBUG, "End of file. To zero = %i\n", buf_get_zero(s));
162  goto end;
163  }
164 
165  if (s->last_frame) {
166  ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den);
167  if (ret != 4) {
168  av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i / 4).\n", ret);
170  return AVERROR_INVALIDDATA;
171  }
172 
173  av_log(ctx, AV_LOG_DEBUG, "frame %"PRId64" ", s->last_frame->pts);
174 
175  if (s->last_frame->pts >= s->ptsi) {
176  av_log(ctx, AV_LOG_DEBUG, ">= %"PRId64": DUP LAST with pts = %"PRId64"\n", s->ptsi, s->pts);
177 
178  // clone frame
179  frame = av_frame_clone(s->last_frame);
180  if (!frame) {
181  ff_outlink_set_status(outlink, AVERROR(ENOMEM), AV_NOPTS_VALUE);
182  return AVERROR(ENOMEM);
183  }
184 
185  // set output pts and timebase
186  frame->pts = s->pts;
187  frame->time_base = av_make_q((int)s->tb_num, (int)s->tb_den);
188 
189  // advance cur to eol, skip over eol in the next call
190  s->cur += line_count;
191 
192  // call again
193  if (ff_inoutlink_check_flow(inlink, outlink))
194  ff_filter_set_ready(ctx, 100);
195 
196  // filter frame
197  return ff_filter_frame(outlink, frame);
198  } else if (s->last_frame->pts < s->ptsi) {
199  av_log(ctx, AV_LOG_DEBUG, "< %"PRId64": DROP\n", s->ptsi);
200  av_frame_free(&s->last_frame);
201 
202  // call again
203  if (ff_inoutlink_check_flow(inlink, outlink))
204  ff_filter_set_ready(ctx, 100);
205 
206  return 0;
207  }
208  }
209 
210 end:
211  if (s->last_frame)
212  av_frame_free(&s->last_frame);
213 
214  ret = ff_inlink_consume_frame(inlink, &s->last_frame);
215  if (ret < 0)
216  return ret;
217 
220 
221  return FFERROR_NOT_READY;
222 }
223 
224 static int fsync_config_props(AVFilterLink* outlink)
225 {
226  AVFilterContext *ctx = outlink->src;
227  FilterLink *l = ff_filter_link(outlink);
228  FsyncContext *s = ctx->priv;
229  int ret;
230 
231  // read first line to get output timebase
232  ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den);
233  if (ret != 4) {
234  av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i of 4).\n", ret);
236  return AVERROR_INVALIDDATA;
237  }
238 
239  l->frame_rate = av_make_q(1, 0); // unknown or dynamic
240  outlink->time_base = av_make_q(s->tb_num, s->tb_den);
241 
242  return 0;
243 }
244 
246 {
247  FsyncContext *s = ctx->priv;
248  int ret;
249 
250  av_log(ctx, AV_LOG_DEBUG, "filename: %s\n", s->filename);
251 
252  s->buf = av_malloc(BUF_SIZE + 1);
253  if (!s->buf)
254  return AVERROR(ENOMEM);
255 
256  ret = avio_open(&s->avio_ctx, s->filename, AVIO_FLAG_READ);
257  if (ret < 0)
258  return ret;
259 
260  s->cur = s->buf;
261  s->end = s->buf + BUF_SIZE;
262  s->buf[BUF_SIZE] = '\0';
263 
264  ret = buf_fill(s);
265  if (ret < 0)
266  return ret;
267 
268 
269  return 0;
270 }
271 
273 {
274  FsyncContext *s = ctx->priv;
275 
276  avio_closep(&s->avio_ctx);
277  av_freep(&s->buf);
278  av_frame_free(&s->last_frame);
279 }
280 
281 AVFILTER_DEFINE_CLASS(fsync);
282 
283 static const AVFilterPad fsync_outputs[] = {
284  {
285  .name = "default",
286  .type = AVMEDIA_TYPE_VIDEO,
287  .config_props = fsync_config_props,
288  },
289 };
290 
292  .name = "fsync",
293  .description = NULL_IF_CONFIG_SMALL("Synchronize video frames from external source."),
294  .init = fsync_init,
295  .uninit = fsync_uninit,
296  .priv_size = sizeof(FsyncContext),
297  .priv_class = &fsync_class,
298  .activate = activate,
299  .formats_state = FF_FILTER_FORMATS_PASSTHROUGH,
303 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
buf_get_line_count
static int buf_get_line_count(FsyncContext *ctx)
Get number of bytes from cur until eol.
Definition: vf_fsync.c:118
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
activate
static int activate(AVFilterContext *ctx)
Definition: vf_fsync.c:138
AVOption
AVOption.
Definition: opt.h:429
avio_open
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: avio.c:497
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
buf_fill
static int buf_fill(FsyncContext *ctx)
Fills the buffer from cur to end, add \0 at EOF.
Definition: vf_fsync.c:63
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(fsync)
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:434
ff_inoutlink_check_flow
int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
Check for flow control between input and output.
Definition: avfilter.c:1624
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_inlink_consume_frame
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:1490
ff_vf_fsync
const AVFilter ff_vf_fsync
Definition: vf_fsync.c:291
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
FsyncContext::pts
int64_t pts
Definition: vf_fsync.c:47
ff_video_default_filterpad
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
FsyncContext
Definition: vf_fsync.c:38
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
FsyncContext::avio_ctx
AVIOContext * avio_ctx
Definition: vf_fsync.c:40
ctx
AVFormatContext * ctx
Definition: movenc.c:49
buf_reload
static int buf_reload(FsyncContext *ctx)
Copies cur to end to the beginning and fills the rest.
Definition: vf_fsync.c:81
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:609
FsyncContext::buf
char * buf
Definition: vf_fsync.c:43
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:961
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
av_strnlen
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
Definition: avstring.h:141
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
error.h
fsync_options
static const AVOption fsync_options[]
Definition: vf_fsync.c:54
FsyncContext::filename
char * filename
Definition: vf_fsync.c:42
AV_OPT_FLAG_FILTERING_PARAM
#define AV_OPT_FLAG_FILTERING_PARAM
A generic parameter which can be set by the user for filtering.
Definition: opt.h:381
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
avio.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
FsyncContext::ptsi
int64_t ptsi
Definition: vf_fsync.c:46
FsyncContext::last_frame
AVFrame * last_frame
Definition: vf_fsync.c:41
FF_FILTER_FORMATS_PASSTHROUGH
@ FF_FILTER_FORMATS_PASSTHROUGH
The default value meaning that this filter supports all formats and (for audio) sample rates and chan...
Definition: filters.h:227
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
fsync_outputs
static const AVFilterPad fsync_outputs[]
Definition: vf_fsync.c:283
FsyncContext::end
char * end
Definition: vf_fsync.c:45
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:358
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
fsync_init
static av_cold int fsync_init(AVFilterContext *ctx)
Definition: vf_fsync.c:245
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
OFFSET
#define OFFSET(x)
Definition: vf_fsync.c:52
fsync_config_props
static int fsync_config_props(AVFilterLink *outlink)
Definition: vf_fsync.c:224
buf_get_zero
static int buf_get_zero(FsyncContext *ctx)
Get number of bytes from cur to '\0'.
Definition: vf_fsync.c:133
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:168
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
fsync_uninit
static av_cold void fsync_uninit(AVFilterContext *ctx)
Definition: vf_fsync.c:272
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
BUF_SIZE
#define BUF_SIZE
Definition: vf_fsync.c:36
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
avio_closep
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: avio.c:649
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avstring.h
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
FsyncContext::tb_num
int tb_num
Definition: vf_fsync.c:48
FsyncContext::cur
char * cur
Definition: vf_fsync.c:44
FsyncContext::tb_den
int tb_den
Definition: vf_fsync.c:49
buf_skip_eol
static void buf_skip_eol(FsyncContext *ctx)
Skip from cur over eol.
Definition: vf_fsync.c:102
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:239
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346