diff options
author | Mauro Carvalho Chehab <mchehab+huawei@kernel.org> | 2021-11-11 07:43:09 +0000 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab+huawei@kernel.org> | 2021-11-11 07:57:17 +0000 |
commit | 79b4354f1c470527ace542fa0e0e9f551aed6c62 (patch) | |
tree | 814b2c3ac832ec91fea45e4a3f4718076cf80f0c | |
parent | adcb349b856a7e726e0023b900e0a2870bf1cb03 (diff) | |
download | v4l-utils-79b4354f1c470527ace542fa0e0e9f551aed6c62.tar.gz |
v4l2grab: estimate the frame rate
Add a framerate estimation after the end of the capture loop.
the start time is calculated just after streamon for mmap/userptr,
in order to avoid counting the time to allocate/mmap buffers and
to prepare the device for start streaming.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
-rw-r--r-- | contrib/test/v4l2grab.c | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/contrib/test/v4l2grab.c b/contrib/test/v4l2grab.c index eb8ba6c5..a19f7196 100644 --- a/contrib/test/v4l2grab.c +++ b/contrib/test/v4l2grab.c @@ -575,12 +575,15 @@ static int read_capture_loop(int fd, struct buffer *buffers, } static int read_capture(int fd, int n_frames, unsigned char *out_buf, char *out_dir, - struct v4l2_format *fmt) + struct v4l2_format *fmt, struct timeval *start) { struct buffer *buffers; + struct timezone tz = { 0 }; buffers = calloc(1, sizeof(*buffers)); + gettimeofday(start, &tz); + return read_capture_loop(fd, buffers, fmt, n_frames, out_buf, out_dir); } @@ -655,12 +658,13 @@ static int userptr_capture_loop(int fd, struct buffer *buffers, } static int userptr_capture(int fd, int n_frames, unsigned char *out_buf, char *out_dir, - struct v4l2_format *fmt) + struct v4l2_format *fmt, struct timeval *start) { struct v4l2_buffer buf; struct v4l2_requestbuffers req; unsigned int ret, i, n_buffers; struct buffer *buffers; + struct timezone tz = { 0 }; CLEAR(req); req.count = 2; @@ -700,6 +704,8 @@ static int userptr_capture(int fd, int n_frames, unsigned char *out_buf, char *o xioctl(fd, VIDIOC_STREAMON, &req.type); + gettimeofday(start, &tz); + ret = userptr_capture_loop(fd, buffers, req.count, fmt, n_frames, out_buf, out_dir); @@ -931,13 +937,14 @@ static int mmap_capture_loop(int fd, struct buffer *buffers, static int mmap_capture(int fd, int n_frames, unsigned char *out_buf, char *out_dir, int threads, int sleep_ms, - struct v4l2_format *fmt) + struct v4l2_format *fmt, struct timeval *start) { struct v4l2_buffer buf; struct v4l2_requestbuffers req; enum v4l2_buf_type type; unsigned int i, n_buffers; struct buffer *buffers; + struct timezone tz = { 0 }; CLEAR(req); req.count = 2; @@ -986,6 +993,9 @@ static int mmap_capture(int fd, int n_frames, unsigned char *out_buf, type = V4L2_BUF_TYPE_VIDEO_CAPTURE; xioctl(fd, VIDIOC_STREAMON, &type); + + gettimeofday(start, &tz); + if (threads) mmap_capture_threads(fd, buffers, 2, fmt, n_frames, out_buf, out_dir, sleep_ms); @@ -1115,12 +1125,29 @@ static struct argp argp = { .doc = doc, }; +static int elapsed_time(struct timeval *x, struct timeval *y) +{ + long int res; + int nsec; + + nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + + res = (x->tv_sec - y->tv_sec) * 1000000; + res += x->tv_usec - y->tv_usec; + + return res; +} int main(int argc, char **argv) { struct v4l2_format fmt; int ret = EXIT_FAILURE, fd = -1; unsigned char *out_buf = NULL; + struct timezone tz = { 0 }; + struct timeval start, end; + long int elapsed; argp_parse(&argp, argc, argv, 0, 0, 0); @@ -1184,17 +1211,28 @@ int main(int argc, char **argv) /* Calls the desired capture method */ switch (method) { case IO_METHOD_READ: - ret = read_capture(fd, n_frames, out_buf, out_dir, &fmt); + ret = read_capture(fd, n_frames, out_buf, out_dir, + &fmt, &start); break; case IO_METHOD_MMAP: ret = mmap_capture(fd, n_frames, out_buf, out_dir, - threads, sleep_ms, &fmt); + threads, sleep_ms, &fmt, &start); break; case IO_METHOD_USERPTR: - ret = userptr_capture(fd, n_frames, out_buf, out_dir, &fmt); + ret = userptr_capture(fd, n_frames, out_buf, out_dir, &fmt, + &start); break; } + if (!ret) { + gettimeofday(&end, &tz); + + elapsed = elapsed_time(&end, &start); + + printf("Received %d frames in %.2f seconds: %.2f fps\n", + n_frames, elapsed / 1000000., 1000000. * n_frames/ elapsed); + } + /* Closes the file descriptor */ if (libv4l) v4l2_close(fd); |