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  FsyncContext *s = ctx->priv;
228  int ret;
229 
230  // read first line to get output timebase
231  ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den);
232  if (ret != 4) {
233  av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i of 4).\n", ret);
235  return AVERROR_INVALIDDATA;
236  }
237 
238  outlink->frame_rate = av_make_q(1, 0); // unknown or dynamic
239  outlink->time_base = av_make_q(s->tb_num, s->tb_den);
240 
241  return 0;
242 }
243 
245 {
246  FsyncContext *s = ctx->priv;
247  int ret;
248 
249  av_log(ctx, AV_LOG_DEBUG, "filename: %s\n", s->filename);
250 
251  s->buf = av_malloc(BUF_SIZE + 1);
252  if (!s->buf)
253  return AVERROR(ENOMEM);
254 
255  ret = avio_open(&s->avio_ctx, s->filename, AVIO_FLAG_READ);
256  if (ret < 0)
257  return ret;
258 
259  s->cur = s->buf;
260  s->end = s->buf + BUF_SIZE;
261  s->buf[BUF_SIZE] = '\0';
262 
263  ret = buf_fill(s);
264  if (ret < 0)
265  return ret;
266 
267 
268  return 0;
269 }
270 
272 {
273  FsyncContext *s = ctx->priv;
274 
275  avio_closep(&s->avio_ctx);
276  av_freep(&s->buf);
277  av_frame_free(&s->last_frame);
278 }
279 
280 AVFILTER_DEFINE_CLASS(fsync);
281 
282 static const AVFilterPad fsync_outputs[] = {
283  {
284  .name = "default",
285  .type = AVMEDIA_TYPE_VIDEO,
286  .config_props = fsync_config_props,
287  },
288 };
289 
291  .name = "fsync",
292  .description = NULL_IF_CONFIG_SMALL("Synchronize video frames from external source."),
293  .init = fsync_init,
294  .uninit = fsync_uninit,
295  .priv_size = sizeof(FsyncContext),
296  .priv_class = &fsync_class,
297  .activate = activate,
298  .formats_state = FF_FILTER_FORMATS_PASSTHROUGH,
302 };
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:1015
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
activate
static int activate(AVFilterContext *ctx)
Definition: vf_fsync.c:138
AVOption
AVOption.
Definition: opt.h:346
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:170
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:199
ff_inoutlink_check_flow
int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
Check for flow control between input and output.
Definition: avfilter.c:1599
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:1442
ff_vf_fsync
const AVFilter ff_vf_fsync
Definition: vf_fsync.c:290
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:189
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:201
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:593
FsyncContext::buf
char * buf
Definition: vf_fsync.c:43
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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
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:298
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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: internal.h:151
fsync_outputs
static const AVFilterPad fsync_outputs[]
Definition: vf_fsync.c:282
FsyncContext::end
char * end
Definition: vf_fsync.c:45
AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:275
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
fsync_init
static av_cold int fsync_init(AVFilterContext *ctx)
Definition: vf_fsync.c:244
AVFilter
Filter definition.
Definition: avfilter.h:166
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:133
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:271
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
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)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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
Definition: opt.h:239
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:235
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346