FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
framesync.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Nicolas George
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/avassert.h"
22 #include "avfilter.h"
23 #include "bufferqueue.h"
24 #include "framesync.h"
25 #include "internal.h"
26 
27 #define OFFSET(member) offsetof(FFFrameSync, member)
28 
29 static const char *framesync_name(void *ptr)
30 {
31  return "framesync";
32 }
33 
34 static const AVClass framesync_class = {
36  .class_name = "framesync",
37  .item_name = framesync_name,
38  .category = AV_CLASS_CATEGORY_FILTER,
39  .option = NULL,
40  .parent_log_context_offset = OFFSET(parent),
41 };
42 
43 enum {
47 };
48 
49 int ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in)
50 {
51  fs->class = &framesync_class;
52  fs->parent = parent;
53  fs->nb_in = nb_in;
54 
55  fs->in = av_calloc(nb_in, sizeof(*fs->in));
56  if (!fs->in)
57  return AVERROR(ENOMEM);
58  return 0;
59 }
60 
62 {
63  unsigned i, level = 0;
64 
65  for (i = 0; i < fs->nb_in; i++)
66  if (fs->in[i].state != STATE_EOF)
67  level = FFMAX(level, fs->in[i].sync);
68  av_assert0(level <= fs->sync_level);
69  if (level < fs->sync_level)
70  av_log(fs, AV_LOG_VERBOSE, "Sync level %u\n", level);
71  if (level)
72  fs->sync_level = level;
73  else
74  fs->eof = 1;
75 }
76 
78 {
79  unsigned i;
80  int64_t gcd, lcm;
81 
82  if (!fs->time_base.num) {
83  for (i = 0; i < fs->nb_in; i++) {
84  if (fs->in[i].sync) {
85  if (fs->time_base.num) {
86  gcd = av_gcd(fs->time_base.den, fs->in[i].time_base.den);
87  lcm = (fs->time_base.den / gcd) * fs->in[i].time_base.den;
88  if (lcm < AV_TIME_BASE / 2) {
89  fs->time_base.den = lcm;
90  fs->time_base.num = av_gcd(fs->time_base.num,
91  fs->in[i].time_base.num);
92  } else {
93  fs->time_base.num = 1;
95  break;
96  }
97  } else {
98  fs->time_base = fs->in[i].time_base;
99  }
100  }
101  }
102  if (!fs->time_base.num) {
103  av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
104  return AVERROR(EINVAL);
105  }
106  av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
107  fs->time_base.num, fs->time_base.den);
108  }
109 
110  for (i = 0; i < fs->nb_in; i++)
111  fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
112  fs->sync_level = UINT_MAX;
114 
115  return 0;
116 }
117 
119 {
120  int latest;
121  unsigned i;
122  int64_t pts;
123 
124  if (fs->eof)
125  return;
126  while (!fs->frame_ready) {
127  latest = -1;
128  for (i = 0; i < fs->nb_in; i++) {
129  if (!fs->in[i].have_next) {
130  if (latest < 0 || fs->in[i].pts < fs->in[latest].pts)
131  latest = i;
132  }
133  }
134  if (latest >= 0) {
135  fs->in_request = latest;
136  break;
137  }
138 
139  pts = fs->in[0].pts_next;
140  for (i = 1; i < fs->nb_in; i++)
141  if (fs->in[i].pts_next < pts)
142  pts = fs->in[i].pts_next;
143  if (pts == INT64_MAX) {
144  fs->eof = 1;
145  break;
146  }
147  for (i = 0; i < fs->nb_in; i++) {
148  if (fs->in[i].pts_next == pts ||
149  (fs->in[i].before == EXT_INFINITY &&
150  fs->in[i].state == STATE_BOF)) {
151  av_frame_free(&fs->in[i].frame);
152  fs->in[i].frame = fs->in[i].frame_next;
153  fs->in[i].pts = fs->in[i].pts_next;
154  fs->in[i].frame_next = NULL;
155  fs->in[i].pts_next = AV_NOPTS_VALUE;
156  fs->in[i].have_next = 0;
157  fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
158  if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
159  fs->frame_ready = 1;
160  if (fs->in[i].state == STATE_EOF &&
161  fs->in[i].after == EXT_STOP)
162  fs->eof = 1;
163  }
164  }
165  if (fs->eof)
166  fs->frame_ready = 0;
167  if (fs->frame_ready)
168  for (i = 0; i < fs->nb_in; i++)
169  if ((fs->in[i].state == STATE_BOF &&
170  fs->in[i].before == EXT_STOP))
171  fs->frame_ready = 0;
172  fs->pts = pts;
173  }
174 }
175 
176 static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
177  int64_t pts)
178 {
179  /* Possible enhancement: use the link's frame rate */
180  return pts + 1;
181 }
182 
183 static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
184 {
185  int64_t pts;
186 
187  av_assert0(!fs->in[in].have_next);
188  if (frame) {
189  pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
190  frame->pts = pts;
191  } else {
192  pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
193  ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
194  fs->in[in].sync = 0;
196  }
197  fs->in[in].frame_next = frame;
198  fs->in[in].pts_next = pts;
199  fs->in[in].have_next = 1;
200 }
201 
203 {
204  av_assert1(in < fs->nb_in);
205  if (!fs->in[in].have_next)
206  framesync_inject_frame(fs, in, frame);
207  else
208  ff_bufqueue_add(fs, &fs->in[in].queue, frame);
209  return 0;
210 }
211 
213 {
214  unsigned i;
215 
216  av_assert0(!fs->frame_ready);
217  for (i = 0; i < fs->nb_in; i++)
218  if (!fs->in[i].have_next && fs->in[i].queue.available)
220  fs->frame_ready = 0;
221  framesync_advance(fs);
222 }
223 
225 {
226  fs->frame_ready = 0;
227 }
228 
229 int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
230  unsigned get)
231 {
232  AVFrame *frame;
233  unsigned need_copy = 0, i;
234  int64_t pts_next;
235  int ret;
236 
237  if (!fs->in[in].frame) {
238  *rframe = NULL;
239  return 0;
240  }
241  frame = fs->in[in].frame;
242  if (get) {
243  /* Find out if we need to copy the frame: is there another sync
244  stream, and do we know if its current frame will outlast this one? */
245  pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
246  for (i = 0; i < fs->nb_in && !need_copy; i++)
247  if (i != in && fs->in[i].sync &&
248  (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
249  need_copy = 1;
250  if (need_copy) {
251  if (!(frame = av_frame_clone(frame)))
252  return AVERROR(ENOMEM);
253  if ((ret = av_frame_make_writable(frame)) < 0) {
254  av_frame_free(&frame);
255  return ret;
256  }
257  } else {
258  fs->in[in].frame = NULL;
259  }
260  fs->frame_ready = 0;
261  }
262  *rframe = frame;
263  return 0;
264 }
265 
267 {
268  unsigned i;
269 
270  for (i = 0; i < fs->nb_in; i++) {
271  av_frame_free(&fs->in[i].frame);
272  av_frame_free(&fs->in[i].frame_next);
274  }
275 
276  av_freep(&fs->in);
277 }
278 
280 {
281  int ret, count = 0;
282 
283  av_assert0(fs->on_event);
284  while (1) {
285  ff_framesync_next(fs);
286  if (fs->eof || !fs->frame_ready)
287  break;
288  if ((ret = fs->on_event(fs)) < 0)
289  return ret;
290  ff_framesync_drop(fs);
291  count++;
292  if (!all)
293  break;
294  }
295  if (!count && fs->eof)
296  return AVERROR_EOF;
297  return count;
298 }
299 
301  AVFrame *in)
302 {
303  int ret;
304 
305  if ((ret = ff_framesync_process_frame(fs, 1)) < 0)
306  return ret;
307  if ((ret = ff_framesync_add_frame(fs, FF_INLINK_IDX(inlink), in)) < 0)
308  return ret;
309  if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
310  return ret;
311  return 0;
312 }
313 
315 {
316  AVFilterContext *ctx = outlink->src;
317  int input, ret;
318 
319  if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
320  return ret;
321  if (ret > 0)
322  return 0;
323  if (fs->eof)
324  return AVERROR_EOF;
325  input = fs->in_request;
326  ret = ff_request_frame(ctx->inputs[input]);
327  if (ret == AVERROR_EOF) {
328  if ((ret = ff_framesync_add_frame(fs, input, NULL)) < 0)
329  return ret;
330  if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
331  return ret;
332  ret = 0;
333  }
334  return ret;
335 }
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
static void framesync_sync_level_update(FFFrameSync *fs)
Definition: framesync.c:61
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
Main libavfilter public API header.
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:175
int num
numerator
Definition: rational.h:44
static void framesync_advance(FFFrameSync *fs)
Definition: framesync.c:118
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:77
static const char * framesync_name(void *ptr)
Definition: framesync.c:29
int64_t pts
Timestamp of the current event.
Definition: framesync.h:170
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:93
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t have_next
Boolean flagging the next frame, for internal use.
Definition: framesync.h:128
void * parent
Definition: framesync.h:155
void ff_framesync_next(FFFrameSync *fs)
Prepare the next frame event.
Definition: framesync.c:212
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
unsigned sync_level
Synchronization level: only inputs with the same sync level are sync sources.
Definition: framesync.h:191
FFFrameSyncIn * in
Pointer to array of inputs.
Definition: framesync.h:206
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:98
int ff_framesync_process_frame(FFFrameSync *fs, unsigned all)
Process one or several frame using the on_event callback.
Definition: framesync.c:279
#define av_log(a,...)
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
int ff_framesync_filter_frame(FFFrameSync *fs, AVFilterLink *inlink, AVFrame *in)
Accept a frame on a filter input.
Definition: framesync.c:300
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:266
AVFrame * frame_next
Next frame, for internal use.
Definition: framesync.h:113
Frame sync structure.
Definition: framesync.h:153
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define OFFSET(member)
Definition: framesync.c:27
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of a and b.
Definition: mathematics.c:37
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:103
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
uint8_t eof
Flag indicating that output has reached EOF.
Definition: framesync.h:201
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
unsigned in_request
Index of the input that requires a request.
Definition: framesync.h:185
int ff_framesync_request_frame(FFFrameSync *fs, AVFilterLink *outlink)
Request a frame on the filter output.
Definition: framesync.c:314
int ff_framesync_add_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
Add a frame to an input.
Definition: framesync.c:202
AVFormatContext * ctx
Definition: movenc.c:48
AVRational time_base
Time base for the output events.
Definition: framesync.h:165
struct FFBufQueue queue
Queue of incoming AVFrame, and NULL to mark EOF.
Definition: framesync.h:88
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in, int64_t pts)
Definition: framesync.c:176
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:471
static const AVClass framesync_class
Definition: framesync.c:34
Extend the frame to infinity.
Definition: framesync.h:77
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:49
uint8_t state
State: before first, in stream or after EOF, for internal use.
Definition: framesync.h:133
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events...
Definition: framesync.h:146
Describe the class of an AVClass context structure.
Definition: log.h:67
unsigned nb_in
Number of input streams.
Definition: framesync.h:160
AVFrame * frame
Current frame, may be NULL before the first one or after EOF.
Definition: framesync.h:108
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static int64_t pts
Global timestamp for the audio frames.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:537
uint8_t frame_ready
Flag indicating that a frame event is ready.
Definition: framesync.h:196
uint8_t level
Definition: svq3.c:193
int version
LIBAVUTIL_VERSION with which this structure was created.
Definition: log.h:93
#define FF_INLINK_IDX(link)
Find the index of a link.
Definition: internal.h:353
if(ret< 0)
Definition: vf_mcdeint.c:282
int64_t pts
PTS of the current frame.
Definition: framesync.h:118
int den
denominator
Definition: rational.h:45
Completely stop all streams with this one.
Definition: framesync.h:67
void ff_framesync_drop(FFFrameSync *fs)
Drop the current frame event.
Definition: framesync.c:224
An instance of a filter.
Definition: avfilter.h:305
#define av_freep(p)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
Definition: framesync.c:183
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:369
internal API functions
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:229
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
const AVClass * class
Definition: framesync.h:154
int64_t pts_next
PTS of the next frame, for internal use.
Definition: framesync.h:123