[PATCH 1/2] timefilter: allow to evaluate at other times.
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- libavdevice/timefilter.c | 5 +++++ libavdevice/timefilter.h | 9 +++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/libavdevice/timefilter.c b/libavdevice/timefilter.c index 8451466..a423fc0 100644 --- a/libavdevice/timefilter.c +++ b/libavdevice/timefilter.c @@ -83,6 +83,11 @@ double ff_timefilter_update(TimeFilter *self, double system_time, double period) return self->cycle_time; } +double ff_timefilter_eval(TimeFilter *self, double delta) +{ + return self->cycle_time + self->clock_period * delta; +} + #ifdef TEST #include "libavutil/lfg.h" #define LFG_MAX ((1LL << 32) - 1) diff --git a/libavdevice/timefilter.h b/libavdevice/timefilter.h index 33862f6..6662959 100644 --- a/libavdevice/timefilter.h +++ b/libavdevice/timefilter.h @@ -82,6 +82,15 @@ TimeFilter * ff_timefilter_new(double clock_period, double feedback2_factor, dou double ff_timefilter_update(TimeFilter *self, double system_time, double period); /** + * Evaluate the filter at a specified time + * + * @param delta difference between the requested time and the current time + * (last call to ff_timefilter_update). + * @return the filtered time + */ +double ff_timefilter_eval(TimeFilter *self, double delta); + +/** * Reset the filter * * This function should mainly be called in case of XRUN. -- 1.7.9.1
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-) diff --git a/configure b/configure index 9ab2302..61ae6e5 100755 --- a/configure +++ b/configure @@ -1162,6 +1162,7 @@ HAVE_LIST=" attribute_may_alias attribute_packed cbrtf + clock_gettime closesocket cmov dcbzl @@ -3034,6 +3035,7 @@ fi # Solaris has nanosleep in -lrt, OpenSolaris no longer needs that check_func nanosleep || { check_func nanosleep -lrt && add_extralibs -lrt; } +check_func clock_gettime || { check_func clock_gettime -lrt && add_extralibs -lrt; } check_func fcntl check_func fork check_func getaddrinfo $network_extralibs diff --git a/doc/indevs.texi b/doc/indevs.texi index 3b2c862..a04cc3e 100644 --- a/doc/indevs.texi +++ b/doc/indevs.texi @@ -524,6 +524,13 @@ try to auto-detect the size to use. Only for the video4linux2 device, if the frame rate is set to 0/0 the input device will use the frame rate value already set in the driver. +The time base for the timestamps is 1 microsecond. Depending on the kernel +version and configuration, the timestamps may be derived from the real time +clock (origin at the Unix Epoch) or the monotonic clock (origin usually at +boot time, unaffected by NTP or manual changes to the clock). The +@option{-timestamps abs} or @option{-ts abs} option can be used to force +conversion into the real time clock. + Video4Linux support is deprecated since Linux 2.6.30, and will be dropped in later versions. diff --git a/libavdevice/Makefile b/libavdevice/Makefile index d7806ea..7f0c1d3 100644 --- a/libavdevice/Makefile +++ b/libavdevice/Makefile @@ -28,7 +28,7 @@ OBJS-$(CONFIG_PULSE_INDEV) += pulse.o OBJS-$(CONFIG_SDL_OUTDEV) += sdl.o OBJS-$(CONFIG_SNDIO_INDEV) += sndio_common.o sndio_dec.o OBJS-$(CONFIG_SNDIO_OUTDEV) += sndio_common.o sndio_enc.o -OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o +OBJS-$(CONFIG_V4L2_INDEV) += v4l2.o timefilter.o OBJS-$(CONFIG_V4L_INDEV) += v4l.o OBJS-$(CONFIG_VFWCAP_INDEV) += vfwcap.o OBJS-$(CONFIG_X11_GRAB_DEVICE_INDEV) += x11grab.o diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 642df1d..0c3419c 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -51,6 +51,7 @@ #include "libavutil/log.h" #include "libavutil/opt.h" #include "avdevice.h" +#include "timefilter.h" #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "libavutil/avstring.h" @@ -73,6 +74,9 @@ static const int desired_video_buffers = 256; #define V4L_RAWFORMATS 1 #define V4L_COMPFORMATS 2 +#define V4L_TS_DEFAULT 0 +#define V4L_TS_ABS 1 + struct video_data { AVClass *class; int fd; @@ -81,6 +85,9 @@ struct video_data { int frame_size; int interlaced; int top_field_first; + int ts_mode; + TimeFilter *timefilter; + int64_t last_time_m; int buffers; void **buf_start; @@ -455,6 +462,64 @@ static void mmap_release_buffer(AVPacket *pkt) pkt->size = 0; } +#if HAVE_CLOCK_GETTIME +static int64_t av_gettime_monotonic(void) +{ + struct timespec tv; + + clock_gettime(CLOCK_MONOTONIC, &tv); + return (int64_t)tv.tv_sec * 1000000 + tv.tv_nsec / 1000; +} +#endif + +static int detect_timestamp(AVFormatContext *ctx, int64_t ts) +{ + struct video_data *s = ctx->priv_data; + int64_t now; + + now = av_gettime(); + if (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE) { + av_log(ctx, AV_LOG_INFO, "Detected absolute timestamps\n"); + s->ts_mode = V4L_TS_DEFAULT; + return 0; + } +#if HAVE_CLOCK_GETTIME + now = av_gettime_monotonic(); + if (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE) { + int64_t period = av_rescale_q(1, ctx->streams[0]->codec->time_base, + AV_TIME_BASE_Q); + av_log(ctx, AV_LOG_INFO, "Detected monotonic timestamps, converting\n"); + /* microseconds instead of seconds, MHz instead of Hz */ + s->timefilter = ff_timefilter_new(1, period, 1.0E-6); + s->ts_mode = V4L_TS_DEFAULT; + return 0; + } +#endif + av_log(ctx, AV_LOG_ERROR, "Unknown timestamps\n"); + return AVERROR(EIO); +} + +static int convert_timestamp(AVFormatContext *ctx, int64_t *ts) +{ + struct video_data *s = ctx->priv_data; + + if (s->ts_mode == V4L_TS_ABS) { + int r = detect_timestamp(ctx, *ts); + if (r < 0) + return r; + } +#if HAVE_CLOCK_GETTIME + if (s->timefilter) { + int64_t nowa = av_gettime(); + int64_t nowm = av_gettime_monotonic(); + ff_timefilter_update(s->timefilter, nowa, nowm - s->last_time_m); + s->last_time_m = nowm; + *ts = ff_timefilter_eval(s->timefilter, *ts - nowm); + } +#endif + return 0; +} + static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) { struct video_data *s = ctx->priv_data; @@ -490,6 +555,9 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) pkt->data= s->buf_start[buf.index]; pkt->size = buf.bytesused; pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec; + res = convert_timestamp(ctx, &pkt->pts); + if (res < 0) + return res; pkt->destruct = mmap_release_buffer; buf_descriptor = av_malloc(sizeof(struct buff_data)); if (buf_descriptor == NULL) { @@ -847,6 +915,10 @@ static const AVOption options[] = { { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" }, { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" }, { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" }, + { "timestamps", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, DEC, "timestamps" }, + { "default", "Use timestamps from the kernel", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.dbl = V4L_TS_DEFAULT }, 0, 1, DEC, "timestamps" }, + { "abs", "Use absolute timestamps (wall clock)", OFFSET(ts_mode), AV_OPT_TYPE_CONST, {.dbl = V4L_TS_ABS }, 0, 1, DEC, "timestamps" }, + { "ts", "Kind of timestamps for grabbed frames", OFFSET(ts_mode), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, 1, DEC, "timestamps" }, { NULL }, }; -- 1.7.9.1
L'octidi 18 ventôse, an CCXX, Nicolas George a écrit :
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-)
Ping? Regards, -- Nicolas George
On Wed, Mar 21, 2012 at 07:52:24PM +0100, Nicolas George wrote:
L'octidi 18 ventôse, an CCXX, Nicolas George a écrit :
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-)
Ping?
luca, do you have time to review the patch ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is what and why we do it that matters, not just one of them.
On 21/03/12 21:32, Michael Niedermayer wrote:
On Wed, Mar 21, 2012 at 07:52:24PM +0100, Nicolas George wrote:
L'octidi 18 ventôse, an CCXX, Nicolas George a écrit :
Signed-off-by: Nicolas George<nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-)
Ping?
luca, do you have time to review the patch ? Sorry, I think I did not receive this patch... I'll try to have a look today or tomorrow
Luca
On 21/03/12 21:32, Michael Niedermayer wrote:
On Wed, Mar 21, 2012 at 07:52:24PM +0100, Nicolas George wrote:
L'octidi 18 ventôse, an CCXX, Nicolas George a écrit :
Signed-off-by: Nicolas George<nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-)
Ping?
luca, do you have time to review the patch ? Sorry, I've been preempted by other things... I'll look at the patch on Monday.
Luca
Hi all, On 03/21/2012 09:32 PM, Michael Niedermayer wrote:
On Wed, Mar 21, 2012 at 07:52:24PM +0100, Nicolas George wrote:
L'octidi 18 ventôse, an CCXX, Nicolas George a écrit :
Signed-off-by: Nicolas George<nicolas.george@normalesup.org> --- configure | 2 + doc/indevs.texi | 7 +++++ libavdevice/Makefile | 2 +- libavdevice/v4l2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletions(-)
Ping?
luca, do you have time to review the patch ? I finally found the time to look at this patch... I found it on the mailing list archives, here: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-March/121685.html (please correct me if I looked at the wrong version of the patch).
I think the concept (allowing to convert from monotonic timestamps to gettimeofday() times) is very good; I just have some questions on the implementation: It seems to me that detect_timestamp() is invoked only when ts_mode == V4L_TS_ABS, (use absolute timestamps) and it will either set ts_mode = V4L_TS_DEFAULT or fail... What is the exact meaning of V4L_TS_ABS? Based on the source code, I'd call it V4L_TS_AUTODETECT (isn't this what it is really doing?). And I'd also offer an option to always perform the conversion, without any kind of autodetection. Regarding the timestamps filtering code, I think it is ok if tested. Also, it seems to me that the patch is adding two options (-ts and -timestamps) to do the same thing... Why? Finally, maybe (but I am not sure about this) av_gettime_monotonic() can be moved to libavformat/utils.c, where av_gettime() already is. Luca
Le septidi 7 germinal, an CCXX, Luca Abeni a écrit :
I finally found the time to look at this patch... I found it on the mailing list archives, here: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-March/121685.html (please correct me if I looked at the wrong version of the patch).
Thanks. That is the right (and only, at this time) version.
I think the concept (allowing to convert from monotonic timestamps to gettimeofday() times) is very good; I just have some questions on the implementation: It seems to me that detect_timestamp() is invoked only when ts_mode == V4L_TS_ABS, (use absolute timestamps) and it will either set ts_mode = V4L_TS_DEFAULT or fail... What is the exact meaning of V4L_TS_ABS? Based on the source code, I'd call it V4L_TS_AUTODETECT (isn't this what it is really doing?).
There are two kinds of timestamps to consider: the ones we get from the kernel and the ones we return to the user. The ones we get from the kernel can be either wall-clock (old kernels or recent kernels with a special option), monotonic-clock (recent kernels by default) or something-else (bogus drivers, ?). The ones we return to the user can be either same-as-kernel (the current code), wall-clock (what this patch intends to add), or anything else in a future patch. If I put that in a small ASCII-art tableau: ,-------- to user: --------. from kernel: same-as-kernel wall-clock | wall no conversion no conversion | monotonic no conversion CONVERSION \ strange no conversion impossible V4L_TS_* are the values for the to-user timestamp. As you can notice, for V4L_TS_RAW, no conversion is done, so no detection is necessary. Also, I am somewhat abusing the option field: if the init code detects that the kernel is returning wall-clock timestamps, then V4L_TS_RAW and V4L_TS_ABS are synonyms. If the init code detects that the kernel is returning monotonic-clock timestamps, the option field is set to V4L_TS_RAW as a means to say "detection is already done", and the timefilter field is what really matter. What I could do without making the code more complex is define V4L_TS_AUTODETECTION_DONE as an alias to V4L_TS_RAW and add what I just explained as a comment.
And I'd also offer an option to always perform the conversion, without any kind of autodetection.
How do you mean exactly?
Regarding the timestamps filtering code, I think it is ok if tested.
For me it works, famous last words.
Also, it seems to me that the patch is adding two options (-ts and -timestamps) to do the same thing... Why?
-timestamps is long, -ts is not very explicit; there are already several places where synonyms for options are offered, especially in lavfi.
Finally, maybe (but I am not sure about this) av_gettime_monotonic() can be moved to libavformat/utils.c, where av_gettime() already is.
I named the function so that it could be easily moved later, but there are difficult considerations about that: some crappy OSes do not offer the clock_gettime function or miss the CLOCK_MONOTONIC clock source. What should av_gettime_monotonic() return on those OSes? Regards, -- Nicolas George
Hi, On Mon, 2012-03-26 at 15:14 +0200, Nicolas George wrote: [...]
I think the concept (allowing to convert from monotonic timestamps to gettimeofday() times) is very good; I just have some questions on the implementation: It seems to me that detect_timestamp() is invoked only when ts_mode == V4L_TS_ABS, (use absolute timestamps) and it will either set ts_mode = V4L_TS_DEFAULT or fail... What is the exact meaning of V4L_TS_ABS? Based on the source code, I'd call it V4L_TS_AUTODETECT (isn't this what it is really doing?).
There are two kinds of timestamps to consider: the ones we get from the kernel and the ones we return to the user.
The ones we get from the kernel can be either wall-clock (old kernels or recent kernels with a special option), monotonic-clock (recent kernels by default) or something-else (bogus drivers, ?). Yes, this part I knew :)
V4L_TS_* are the values for the to-user timestamp. Ok, this is part of my misunderstanding. I assumed that "user" timestamps are wall-clock (what libavdevice always used), and that V4L_TS_* values simply controlled the "convert from monotonic to wall-clock" or "do not convert" behaviour (small difference respect to
As you can notice, for V4L_TS_RAW, no conversion is done, so no detection is necessary. Ok, I see I am not the one being confused :) It seems to me that there is no V4L_TS_RAW value (I think you mean V4L_TS_DEFAULT, right? BTW. I
[...] the real meaning of this parameter). think _RAW is more understandable) One of the things that initially confused me is that convert_timestamp() is called also in the V4L_TS_DEFAULT case... This is of course ok, it just confused me when reading the patch (I think it would have been simpler to call convert_timestamp() only if ts_mode != V4L_TS_DEFAULT/RAW)
Also, I am somewhat abusing the option field: if the init code detects that the kernel is returning wall-clock timestamps, then V4L_TS_RAW and V4L_TS_ABS are synonyms. Yes, this originally confused me too... Maybe a comment in the code would help.
[...]
And I'd also offer an option to always perform the conversion, without any kind of autodetection.
How do you mean exactly? Just provide a V4L_TS_* value which always asks for a conversion (without detecting if the kernel timestamps are monotonic or something else... When I specify this value, I say "I know kernel timestamps are monotonic, please convert them to wall-clock").
Also, it seems to me that the patch is adding two options (-ts and -timestamps) to do the same thing... Why?
-timestamps is long, -ts is not very explicit; there are already several places where synonyms for options are offered, especially in lavfi. Ok, then.
Finally, maybe (but I am not sure about this) av_gettime_monotonic() can be moved to libavformat/utils.c, where av_gettime() already is.
I named the function so that it could be easily moved later, but there are difficult considerations about that: some crappy OSes do not offer the clock_gettime function or miss the CLOCK_MONOTONIC clock source. What should av_gettime_monotonic() return on those OSes? Well, it could just fail, or return the wall clock... Anyway, this can be done later.
I think the patch looks ok, and IMHO it can be applied (if you can address my small doubts highlighted in these emails, that would be good. But these doubts are not stoppers). Thanks, Luca
L'octidi 8 germinal, an CCXX, Luca Abeni a écrit :
Ok, this is part of my misunderstanding. I assumed that "user" timestamps are wall-clock (what libavdevice always used), and that
"User" timestamps should be wall-clock always, but if the kernel does not provide them, the conversion is somewhat fragile, and since it is not always necessary, making it an option seems better.
Ok, I see I am not the one being confused :) It seems to me that there is no V4L_TS_RAW value (I think you mean V4L_TS_DEFAULT, right?
Right.
BTW. I think _RAW is more understandable)
For people who know the inners of the device, indeed. But for a simple user, I prefer "default".
One of the things that initially confused me is that convert_timestamp() is called also in the V4L_TS_DEFAULT case... This is of course ok, it just confused me when reading the patch (I think it would have been simpler to call convert_timestamp() only if ts_mode != V4L_TS_DEFAULT/RAW)
I wanted to keep changes grouped.
Yes, this originally confused me too... Maybe a comment in the code would help.
Added.
Just provide a V4L_TS_* value which always asks for a conversion (without detecting if the kernel timestamps are monotonic or something else... When I specify this value, I say "I know kernel timestamps are monotonic, please convert them to wall-clock").
I added "mono2abs". Note that specifying it when timestamps are not from the monotonic clock result in ffmpeg freezing (duplicating frames endlessly, actually); but of course, that is to be expected.
Well, it could just fail,
Not very practical from the application point of view.
or return the wall clock...
Could be a concern if the code that uses it relies on actually monotonic values.
Anyway, this can be done later.
Agree.
I think the patch looks ok, and IMHO it can be applied (if you can address my small doubts highlighted in these emails, that would be good. But these doubts are not stoppers).
I send an updated patch immediately. Thanks for your review. Regards, -- Nicolas George
On Wed, Mar 07, 2012 at 04:06:39PM +0100, Nicolas George wrote:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- libavdevice/timefilter.c | 5 +++++ libavdevice/timefilter.h | 9 +++++++++ 2 files changed, 14 insertions(+), 0 deletions(-)
interresting idea patch LGTM [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is dangerous to be right in matters on which the established authorities are wrong. -- Voltaire
participants (3)
-
Luca Abeni -
Michael Niedermayer -
Nicolas George