Skip to content

Commit 5ee15d4

Browse files
buddydvdPhil Elwell
authored and
Phil Elwell
committed
bcm2835-camera: Fix timestamp calculation problem (#2214)
* bcm2835-camera: Fix timestamp calculation problem Use div_s64_rem() to convert usec timestamp to timeval to avoid integer signedness bug. * bcm2835-camera: Store kernel start time in NSEC instead of USEC * bcm2835-camera: Reword debug message for clarity
1 parent 9d88979 commit 5ee15d4

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

drivers/media/platform/bcm2835/bcm2835-camera.c

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -363,37 +363,17 @@ static void buffer_cb(struct vchiq_mmal_instance *instance,
363363
buf->vb.vb2_buf.timestamp);
364364

365365
} else if(pts != 0) {
366-
struct timeval timestamp;
367366
s64 runtime_us = pts -
368367
dev->capture.vc_start_timestamp;
369-
u32 div = 0;
370-
u32 rem = 0;
371-
372-
div =
373-
div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
374-
timestamp.tv_sec =
375-
dev->capture.kernel_start_ts.tv_sec + div;
376-
timestamp.tv_usec =
377-
dev->capture.kernel_start_ts.tv_usec + rem;
378-
379-
if (timestamp.tv_usec >=
380-
USEC_PER_SEC) {
381-
timestamp.tv_sec++;
382-
timestamp.tv_usec -=
383-
USEC_PER_SEC;
384-
}
368+
buf->vb.vb2_buf.timestamp = (runtime_us * NSEC_PER_USEC) +
369+
dev->capture.kernel_start_timestamp;
385370
v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
386-
"Convert start time %d.%06d and %llu "
387-
"with offset %llu to %d.%06d\n",
388-
(int)dev->capture.kernel_start_ts.
389-
tv_sec,
390-
(int)dev->capture.kernel_start_ts.
391-
tv_usec,
392-
dev->capture.vc_start_timestamp, pts,
393-
(int)timestamp.tv_sec,
394-
(int)timestamp.tv_usec);
395-
buf->vb.vb2_buf.timestamp = timestamp.tv_sec * 1000000000ULL +
396-
timestamp.tv_usec * 1000ULL;
371+
"Buffer time set as converted timestamp - %llu "
372+
"= (pts [%lld usec] - vc start time [%llu usec]) "
373+
"+ kernel start time [%llu nsec]\n",
374+
buf->vb.vb2_buf.timestamp,
375+
pts, dev->capture.vc_start_timestamp,
376+
dev->capture.kernel_start_timestamp);
397377
} else {
398378
if (dev->capture.last_timestamp) {
399379
buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
@@ -403,8 +383,7 @@ static void buffer_cb(struct vchiq_mmal_instance *instance,
403383
}
404384
else {
405385
buf->vb.vb2_buf.timestamp =
406-
dev->capture.kernel_start_ts.tv_sec * 1000000000ULL +
407-
dev->capture.kernel_start_ts.tv_usec * 1000ULL;
386+
dev->capture.kernel_start_timestamp;
408387
v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
409388
"Buffer time set as start timestamp - %lld",
410389
buf->vb.vb2_buf.timestamp);
@@ -586,7 +565,7 @@ static int start_streaming(struct vb2_queue *vq, unsigned int count)
586565

587566
dev->capture.last_timestamp = 0;
588567

589-
v4l2_get_timestamp(&dev->capture.kernel_start_ts);
568+
dev->capture.kernel_start_timestamp = ktime_get_ns();
590569

591570
/* enable the camera port */
592571
dev->capture.port->cb_ctx = dev;

drivers/media/platform/bcm2835/bcm2835-camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct bm2835_mmal_dev {
9292
/* VC start timestamp for streaming */
9393
s64 vc_start_timestamp;
9494
/* Kernel start timestamp for streaming */
95-
struct timeval kernel_start_ts;
95+
u64 kernel_start_timestamp;
9696
/* Timestamp of last frame */
9797
u64 last_timestamp;
9898

0 commit comments

Comments
 (0)