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/opt.h"
31 #include "libavformat/avio.h"
32 #include "video.h"
33 #include "filters.h"
34 
35 #define BUF_SIZE 256
36 
37 typedef struct FsyncContext {
38  const AVClass *class;
39  AVIOContext *avio_ctx; // reading the map file
40  AVFrame *last_frame; // buffering the last frame for duplicating eventually
41  char *filename; // user-specified map file
42  char *buf; // line buffer for the map file
43  char *cur; // current position in the line buffer
44  char *end; // end pointer of the line buffer
45  int64_t ptsi; // input pts to map to [0-N] output pts
46  int64_t pts; // output pts
47  int tb_num; // output timebase num
48  int tb_den; // output timebase den
49 } FsyncContext;
50 
51 #define OFFSET(x) offsetof(FsyncContext, x)
52 
53 static const AVOption fsync_options[] = {
54  { "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 },
55  { "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 },
56  { NULL }
57 };
58 
59 /**
60  * Fills the buffer from cur to end, add \0 at EOF
61  */
63 {
64  int ret;
65  int num = ctx->end - ctx->cur;
66 
67  ret = avio_read(ctx->avio_ctx, ctx->cur, num);
68  if (ret < 0)
69  return ret;
70  if (ret < num) {
71  *(ctx->cur + ret) = '\0';
72  }
73 
74  return ret;
75 }
76 
77 /**
78  * Copies cur to end to the beginning and fills the rest
79  */
81 {
82  int i, ret;
83  int num = ctx->end - ctx->cur;
84 
85  for (i = 0; i < num; i++) {
86  ctx->buf[i] = *ctx->cur++;
87  }
88 
89  ctx->cur = ctx->buf + i;
90  ret = buf_fill(ctx);
91  if (ret < 0)
92  return ret;
93  ctx->cur = ctx->buf;
94 
95  return ret;
96 }
97 
98 /**
99  * Skip from cur over eol
100  */
102 {
103  char *i;
104  for (i = ctx->cur; i < ctx->end; i++) {
105  if (*i != '\n')// && *i != '\r')
106  break;
107  }
108  ctx->cur = i;
109 }
110 
111 /**
112  * Get number of bytes from cur until eol
113  *
114  * @return >= 0 in case of success,
115  * -1 in case there is no line ending before end of buffer
116  */
118 {
119  int ret = 0;
120  char *i;
121  for (i = ctx->cur; i < ctx->end; i++, ret++) {
122  if (*i == '\0' || *i == '\n')
123  return ret;
124  }
125 
126  return -1;
127 }
128 
129 /**
130  * Get number of bytes from cur to '\0'
131  */
133 {
134  return av_strnlen(ctx->cur, ctx->end - ctx->cur);
135 }
136 
138 {
139  FsyncContext *s = ctx->priv;
140  AVFilterLink *inlink = ctx->inputs[0];
141  AVFilterLink *outlink = ctx->outputs[0];
142 
143  int ret, line_count;
144  AVFrame *frame;
145 
147 
148  buf_skip_eol(s);
149  line_count = buf_get_line_count(s);
150  if (line_count < 0) {
151  line_count = buf_reload(s);
152  if (line_count < 0)
153  return line_count;
154  line_count = buf_get_line_count(s);
155  if (line_count < 0)
156  return line_count;
157  }
158 
159  if (avio_feof(s->avio_ctx) && buf_get_zero(s) < 3) {
160  av_log(ctx, AV_LOG_DEBUG, "End of file. To zero = %i\n", buf_get_zero(s));
161  goto end;
162  }
163 
164  if (s->last_frame) {
165  ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den);
166  if (ret != 4) {
167  av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i / 4).\n", ret);
169  return AVERROR_INVALIDDATA;
170  }
171 
172  av_log(ctx, AV_LOG_DEBUG, "frame %"PRId64" ", s->last_frame->pts);
173 
174  if (s->last_frame->pts >= s->ptsi) {
175  av_log(ctx, AV_LOG_DEBUG, ">= %"PRId64": DUP LAST with pts = %"PRId64"\n", s->ptsi, s->pts);
176 
177  // clone frame
178  frame = av_frame_clone(s->last_frame);
179  if (!frame) {
180  ff_outlink_set_status(outlink, AVERROR(ENOMEM), AV_NOPTS_VALUE);
181  return AVERROR(ENOMEM);
182  }
183 
184  // set output pts and timebase
185  frame->pts = s->pts;
186  frame->time_base = av_make_q((int)s->tb_num, (int)s->tb_den);
187 
188  // advance cur to eol, skip over eol in the next call
189  s->cur += line_count;
190 
191  // call again
192  if (ff_inoutlink_check_flow(inlink, outlink))
193  ff_filter_set_ready(ctx, 100);
194 
195  // filter frame
196  return ff_filter_frame(outlink, frame);
197  } else if (s->last_frame->pts < s->ptsi) {
198  av_log(ctx, AV_LOG_DEBUG, "< %"PRId64": DROP\n", s->ptsi);
199  av_frame_free(&s->last_frame);
200 
201  // call again
202  if (ff_inoutlink_check_flow(inlink, outlink))
203  ff_filter_set_ready(ctx, 100);
204 
205  return 0;
206  }
207  }
208 
209 end:
210  if (s->last_frame)
211  av_frame_free(&s->last_frame);
212 
213  ret = ff_inlink_consume_frame(inlink, &s->last_frame);
214  if (ret < 0)
215  return ret;
216 
219 
220  return FFERROR_NOT_READY;
221 }
222 
223 static int fsync_config_props(AVFilterLink* outlink)
224 {
225  AVFilterContext *ctx = outlink->src;
226  FsyncContext *s = ctx->priv;
227  int ret;
228 
229  // read first line to get output timebase
230  ret = av_sscanf(s->cur, "%"PRId64" %"PRId64" %d/%d", &s->ptsi, &s->pts, &s->tb_num, &s->tb_den);
231  if (ret != 4) {
232  av_log(ctx, AV_LOG_ERROR, "Unexpected format found (%i of 4).\n", ret);
234  return AVERROR_INVALIDDATA;
235  }
236 
237  outlink->frame_rate = av_make_q(1, 0); // unknown or dynamic
238  outlink->time_base = av_make_q(s->tb_num, s->tb_den);
239 
240  return 0;
241 }
242 
244 {
245  FsyncContext *s = ctx->priv;
246  int ret;
247 
248  av_log(ctx, AV_LOG_DEBUG, "filename: %s\n", s->filename);
249 
250  s->buf = av_malloc(BUF_SIZE + 1);
251  if (!s->buf)
252  return AVERROR(ENOMEM);
253 
254  ret = avio_open(&s->avio_ctx, s->filename, AVIO_FLAG_READ);
255  if (ret < 0)
256  return ret;
257 
258  s->cur = s->buf;
259  s->end = s->buf + BUF_SIZE;
260  s->buf[BUF_SIZE] = '\0';
261 
262  ret = buf_fill(s);
263  if (ret < 0)
264  return ret;
265 
266 
267  return 0;
268 }
269 
271 {
272  FsyncContext *s = ctx->priv;
273 
274  avio_closep(&s->avio_ctx);
275  av_freep(&s->buf);
276  av_frame_free(&s->last_frame);
277 }
278 
279 AVFILTER_DEFINE_CLASS(fsync);
280 
281 static const AVFilterPad fsync_outputs[] = {
282  {
283  .name = "default",
284  .type = AVMEDIA_TYPE_VIDEO,
285  .config_props = fsync_config_props,
286  },
287 };
288 
290  .name = "fsync",
291  .description = NULL_IF_CONFIG_SMALL("Synchronize video frames from external source."),
292  .init = fsync_init,
293  .uninit = fsync_uninit,
294  .priv_size = sizeof(FsyncContext),
295  .priv_class = &fsync_class,
296  .activate = activate,
297  .formats_state = FF_FILTER_FORMATS_PASSTHROUGH,
301 };
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:1018
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:117
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:456
activate
static int activate(AVFilterContext *ctx)
Definition: vf_fsync.c:137
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:496
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:62
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:1602
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:1445
ff_vf_fsync
const AVFilter ff_vf_fsync
Definition: vf_fsync.c:289
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:46
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:37
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:39
ctx
AVFormatContext * ctx
Definition: movenc.c:48
buf_reload
static int buf_reload(FsyncContext *ctx)
Copies cur to end to the beginning and fills the rest.
Definition: vf_fsync.c:80
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:563
FsyncContext::buf
char * buf
Definition: vf_fsync.c:42
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:53
FsyncContext::filename
char * filename
Definition: vf_fsync.c:41
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:106
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
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:471
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
FsyncContext::ptsi
int64_t ptsi
Definition: vf_fsync.c:45
FsyncContext::last_frame
AVFrame * last_frame
Definition: vf_fsync.c:40
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
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:281
FsyncContext::end
char * end
Definition: vf_fsync.c:44
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:243
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
OFFSET
#define OFFSET(x)
Definition: vf_fsync.c:51
fsync_config_props
static int fsync_config_props(AVFilterLink *outlink)
Definition: vf_fsync.c:223
buf_get_zero
static int buf_get_zero(FsyncContext *ctx)
Get number of bytes from cur to '\0'.
Definition: vf_fsync.c:132
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:611
fsync_uninit
static av_cold void fsync_uninit(AVFilterContext *ctx)
Definition: vf_fsync.c:270
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:35
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:648
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:47
FsyncContext::cur
char * cur
Definition: vf_fsync.c:43
FsyncContext::tb_den
int tb_den
Definition: vf_fsync.c:48
buf_skip_eol
static void buf_skip_eol(FsyncContext *ctx)
Skip from cur over eol.
Definition: vf_fsync.c:101
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:234
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345