1 From aa6e148bb75f76291e9625316f1f7c73458daf47 Mon Sep 17 00:00:00 2001
2 From: David H <davidjhu@gmail.com>
3 Date: Wed, 4 Oct 2017 04:43:07 -0700
4 Subject: [PATCH 200/454] bcm2835-camera: Fix timestamp calculation problem
7 * bcm2835-camera: Fix timestamp calculation problem
9 Use div_s64_rem() to convert usec timestamp to timeval
10 to avoid integer signedness bug.
12 * bcm2835-camera: Store kernel start time in NSEC instead of USEC
14 * bcm2835-camera: Reword debug message for clarity
16 .../bcm2835-camera/bcm2835-camera.c | 41 +++++--------------
17 .../bcm2835-camera/bcm2835-camera.h | 2 +-
18 2 files changed, 11 insertions(+), 32 deletions(-)
20 --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
21 +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
22 @@ -348,37 +348,17 @@ static void buffer_cb(struct vchiq_mmal_
23 buf->vb.vb2_buf.timestamp);
26 - struct timeval timestamp;
27 s64 runtime_us = pts -
28 dev->capture.vc_start_timestamp;
33 - div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
35 - dev->capture.kernel_start_ts.tv_sec + div;
37 - dev->capture.kernel_start_ts.tv_usec + rem;
39 - if (timestamp.tv_usec >=
42 - timestamp.tv_usec -=
45 + buf->vb.vb2_buf.timestamp = (runtime_us * NSEC_PER_USEC) +
46 + dev->capture.kernel_start_timestamp;
47 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
48 - "Convert start time %d.%06d and %llu "
49 - "with offset %llu to %d.%06d\n",
50 - (int)dev->capture.kernel_start_ts.
52 - (int)dev->capture.kernel_start_ts.
54 - dev->capture.vc_start_timestamp, pts,
55 - (int)timestamp.tv_sec,
56 - (int)timestamp.tv_usec);
57 - buf->vb.vb2_buf.timestamp = timestamp.tv_sec * 1000000000ULL +
58 - timestamp.tv_usec * 1000ULL;
59 + "Buffer time set as converted timestamp - %llu "
60 + "= (pts [%lld usec] - vc start time [%llu usec]) "
61 + "+ kernel start time [%llu nsec]\n",
62 + buf->vb.vb2_buf.timestamp,
63 + pts, dev->capture.vc_start_timestamp,
64 + dev->capture.kernel_start_timestamp);
66 if (dev->capture.last_timestamp) {
67 buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
68 @@ -388,8 +368,7 @@ static void buffer_cb(struct vchiq_mmal_
71 buf->vb.vb2_buf.timestamp =
72 - dev->capture.kernel_start_ts.tv_sec * 1000000000ULL +
73 - dev->capture.kernel_start_ts.tv_usec * 1000ULL;
74 + dev->capture.kernel_start_timestamp;
75 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
76 "Buffer time set as start timestamp - %lld",
77 buf->vb.vb2_buf.timestamp);
78 @@ -574,7 +553,7 @@ static int start_streaming(struct vb2_qu
80 dev->capture.last_timestamp = 0;
82 - v4l2_get_timestamp(&dev->capture.kernel_start_ts);
83 + dev->capture.kernel_start_timestamp = ktime_get_ns();
85 /* enable the camera port */
86 dev->capture.port->cb_ctx = dev;
87 --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.h
88 +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.h
89 @@ -92,7 +92,7 @@ struct bm2835_mmal_dev {
90 /* VC start timestamp for streaming */
91 s64 vc_start_timestamp;
92 /* Kernel start timestamp for streaming */
93 - struct timeval kernel_start_ts;
94 + u64 kernel_start_timestamp;
95 /* Timestamp of last frame */