Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcm2835-camera: Fix timestamp calculation problem #2214

Merged
merged 3 commits into from
Oct 4, 2017

Conversation

buddydvd
Copy link

Use div_s64_rem() to convert usec timestamp to timeval to avoid integer signedness bug.

While capturing uncompressed frames with ffmpeg, I notice the bcm2835-v4l2 module generates very large negative timestamp values (e.g., -140448221.376697). These large negative values trigger a bunch of issues (e.g., DTS/PTS "invalid dropping" warnings) during capture.

ffmpeg -hide_banner -f v4l2 -input_format yuv420p -framerate 30 -i /dev/video0 -show_streams -f null -

By turning on module's debug flag, I see this in system log:

Convert start time 14389.960211 and 14393894979 with offset 14393759849 to -140448221.376697

Reading the bcm2835-camera.c carefully, I noticed the use of div_u64_rem(...) causes an integer signedness bug. For the conversion, it should be 14389.825081 Instead of -140448221.376697.

Use div_s64_rem() to convert usec timestamp to timeval
to avoid integer signedness bug.
@6by9
Copy link
Contributor

6by9 commented Oct 2, 2017

Looks highly plausible.
The values in struct timeval appear to resolve to long, so certainly the signedness of div and rem was wrong, and therefore needs div_s64_rem.

Actually that code ought to be reworked as the actual vb2_buffer timestamp field is now a u64 taking nsecs, hence line 395 is converting the struct timeval straight back to a u64. That changed betwen 4.4 and 4.9.

I'm a little bemused as to how come you're getting a frame timestamped 135ms before we made the request. You're using yuv420 planar, but what resolution are you asking for? It's possibly another issue that needs investigating.

@buddydvd
Copy link
Author

buddydvd commented Oct 2, 2017

(Sorry for the typo above, should be using ffprobe instead ffmpeg.)

I found the bug while trying to figure out why this was happening. I noticed that start_pts turn positive every time I change the frame rate.

# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459272023127
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459270296572
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459268840187
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=3379884531
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459227361431
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459225620679
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=3395484761
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459213252133
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459211759103
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=3408621387
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459200382364
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 25 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459198083999
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=3424381115
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459184723692
# ffprobe -hide_banner -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -show_streams 2>&1 | grep start_pts
start_pts=-140459183221752

I reformatted the output in table form for clarity. Do you see the pattern?

fps start_pts
30 -140459272023127
30 -140459270296572
30 -140459268840187
25 3379884531
25 -140459227361431
25 -140459225620679
30 3395484761
30 -140459213252133
30 -140459211759103
25 3408621387
25 -140459200382364
25 -140459198083999
30 3424381115
30 -140459184723692
30 -140459183221752

This is what I see when I execute ffmpeg without the fix.

# ffmpeg -f v4l2 -input_format yuv420p -video_size 1280x720 -framerate 30 -i /dev/video0 -f null -
ffmpeg version N-87326-g3ffd3b7f5f Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 6.3.0 (Raspbian 6.3.0-18+rpi1) 20170516
  configuration: --enable-omx --enable-omx-rpi
  libavutil      55. 75.100 / 55. 75.100
  libavcodec     57.106.101 / 57.106.101
  libavformat    57. 82.100 / 57. 82.100
  libavdevice    57.  8.101 / 57.  8.101
  libavfilter     6.105.100 /  6.105.100
  libswscale      4.  7.103 /  4.  7.103
  libswresample   2.  8.100 /  2.  8.100
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: -140457942.655219, bitrate: 331776 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1280x720, 331776 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> wrapped_avframe (native))
Press [q] to stop, [?] for help
Output #0, null, to 'pipe:':
  Metadata:
    encoder         : Lavf57.82.100
    Stream #0:0: Video: wrapped_avframe, yuv420p, 1280x720, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc
    Metadata:
      encoder         : Lavc57.106.101 wrapped_avframe
DTS 140462610581692, next:33333 st:0 invalid dropping
PTS 140462610581692, next:33333 invalid dropping st:0
DTS 140462610615019, next:66666 st:0 invalid dropping
PTS 140462610615019, next:66666 invalid dropping st:0
DTS 140462610648347, next:99999 st:0 invalid dropping
PTS 140462610648347, next:99999 invalid dropping st:0
DTS 140462610681675, next:133332 st:0 invalid dropping
PTS 140462610681675, next:133332 invalid dropping st:0
DTS 140462610715001, next:166665 st:0 invalid dropping
PTS 140462610715001, next:166665 invalid dropping st:0
DTS 140462610748328, next:199998 st:0 invalid dropping
PTS 140462610748328, next:199998 invalid dropping st:0
DTS 140462610781656, next:233331 st:0 invalid dropping
PTS 140462610781656, next:233331 invalid dropping st:0
DTS 140462610814982, next:266664 st:0 invalid dropping

@6by9
Copy link
Contributor

6by9 commented Oct 3, 2017

A big thank you for the second update - my comment was more for my own benefit than a request to change the PR, but the update is very welcome.

Looks fine to me.
We don't have a realistic wrapping issue with s64/u64 - 2^63nsecs I reckon comes out as 106751 years!

@buddydvd
Copy link
Author

buddydvd commented Oct 3, 2017

Sure, no problem. Let me know if you want me to make any other changes.

@6by9
Copy link
Contributor

6by9 commented Oct 3, 2017

I'm good.
Ping @pelwell and @popcornmix. Any comments from you, or are you happy to merge?

@popcornmix popcornmix merged commit cb0f972 into raspberrypi:rpi-4.9.y Oct 4, 2017
@popcornmix
Copy link
Collaborator

Thanks!

popcornmix added a commit to raspberrypi/firmware that referenced this pull request Oct 6, 2017
kernel: brcmfmac: Avoid possible out-of-bounds read
kernel: brcmfmac: Delete redundant length check
kernel: brcmfmac: Add check for short event packets

kernel: bcm2835-camera: Fix timestamp calculation problem
See: raspberrypi/linux#2214

kernel: amba_pl011: Add cts-event-workaround DT property
See: raspberrypi/linux#1280

kernel: amba_pl011: Insert mb() for correct FIFO handling

kernel: brcmfmac: add CLM download support
kernel: brcmfmac: request_firmware_direct is quieter
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this pull request Oct 6, 2017
kernel: brcmfmac: Avoid possible out-of-bounds read
kernel: brcmfmac: Delete redundant length check
kernel: brcmfmac: Add check for short event packets

kernel: bcm2835-camera: Fix timestamp calculation problem
See: raspberrypi/linux#2214

kernel: amba_pl011: Add cts-event-workaround DT property
See: raspberrypi/linux#1280

kernel: amba_pl011: Insert mb() for correct FIFO handling

kernel: brcmfmac: add CLM download support
kernel: brcmfmac: request_firmware_direct is quieter

firmware: arm_loader: Add mbox call to query RSTS register
See: arm_loader: Add mbox call to query RSTS register

firmware: IL Camera: Add manual analog and digital gain controls
firmware: video_decode: Allow stride to be >= min required
firmware: IL ISP: Add white balance and digital gain to ISP component
firmware: variants: drop [read|write]_[media|still] and image_[read|write]
popcornmix pushed a commit that referenced this pull request Oct 29, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 18, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
pelwell pushed a commit to pelwell/linux that referenced this pull request Nov 19, 2017
* 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
ryncsn pushed a commit to ryncsn/linux-rasp that referenced this pull request Nov 21, 2017
* 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
6by9 pushed a commit to 6by9/linux that referenced this pull request Feb 7, 2018
* 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
popcornmix pushed a commit that referenced this pull request Feb 9, 2018
* 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
popcornmix pushed a commit that referenced this pull request Feb 9, 2018
* 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
TiejunChina pushed a commit to TiejunChina/linux that referenced this pull request Feb 14, 2018
* 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
pelwell pushed a commit that referenced this pull request Mar 21, 2018
* 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
popcornmix pushed a commit that referenced this pull request Mar 22, 2018
* 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
popcornmix pushed a commit that referenced this pull request Mar 26, 2018
* 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
popcornmix pushed a commit that referenced this pull request Mar 29, 2018
* 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
popcornmix pushed a commit that referenced this pull request Apr 3, 2018
* 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
popcornmix pushed a commit that referenced this pull request Apr 9, 2018
* 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
popcornmix pushed a commit that referenced this pull request Apr 16, 2018
* 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
popcornmix pushed a commit that referenced this pull request May 5, 2018
* 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
pelwell pushed a commit that referenced this pull request May 20, 2018
* 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
6by9 added a commit to 6by9/linux that referenced this pull request Jul 23, 2018
…i#2214)"

This reverts commit 90ac037.
This has been fixed in an alternate format upstream, so adopt that.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
6by9 added a commit to 6by9/linux that referenced this pull request Jul 24, 2018
…i#2214)"

This reverts commit 90ac037.
This has been fixed in an alternate format upstream, so adopt that.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
pelwell pushed a commit that referenced this pull request Aug 17, 2018
This reverts commit 90ac037.
This has been fixed in an alternate format upstream, so adopt that.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants