Repository navigation
Description
When configured to capture H.264 video from the Raspberry Camera Module v2.1 through the video4linux2 driver, ffmpeg complains that "Timestamps are unset", causing sync issues when combined with audio coming from an other device. As you can see from the ffmpeg output, "This is deprecated and will stop working in the future. Fix your code to set the timestamps properly"
Timestamps are correct when capturing MJPEG instead of H.264. I first tried with the default kernel (4.4.38-v7+) and after upgrading to bleeding edge (4.4.47-v7+) the problem persists. Here is some timestamp debug data I got from ffmpeg -debug_ts -vcodec h264 -i /dev/video0 -c:v copy -t 0.1 timestamps.mkv:
ffmpeg version N-83414-ge477f09 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 4.9.2 (Raspbian 4.9.2-10)
configuration: --enable-gpl --enable-nonfree --enable-mmal --enable-omx-rpi --bindir=/usr/local/bin
libavutil 55. 46.100 / 55. 46.100
libavcodec 57. 75.100 / 57. 75.100
libavformat 57. 66.101 / 57. 66.101
libavdevice 57. 2.100 / 57. 2.100
libavfilter 6. 72.100 / 6. 72.100
libswscale 4. 3.101 / 4. 3.101
libswresample 2. 4.100 / 2. 4.100
libpostproc 54. 2.100 / 54. 2.100
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 5211.202638, bitrate: N/A
Stream #0:0: Video: h264 (High), yuv420p(progressive), 1024x768, 30 fps, 30 tbr, 1000k tbn, 2000k tbc
Output #0, matroska, to 'timestamps.mkv':
Metadata:
encoder : Lavf57.66.101
Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p(progressive), 1024x768, q=2-31, 30 fps, 30 tbr, 1k tbn, 1000k tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
demuxer -> ist_index:0 type:video next_dts:NOPTS next_dts_time:NOPTS next_pts:NOPTS next_pts_time:NOPTS pkt_pts:NOPTS pkt_pts_time:NOPTS pkt_dts:NOPTS pkt_dts_time:NOPTS off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:NOPTS pkt_pts_time:NOPTS pkt_dts:NOPTS pkt_dts_time:NOPTS off:-5211202638 off_time:-5211.2
muxer <- type:video pkt_pts:NOPTS pkt_pts_time:NOPTS pkt_dts:0 pkt_dts_time:0 size:12488
[matroska @ 0x2158b00] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
demuxer -> ist_index:0 type:video next_dts:66666 next_dts_time:0.066666 next_pts:66666 next_pts_time:0.066666 pkt_pts:5211202638 pkt_pts_time:5211.2 pkt_dts:5211202638 pkt_dts_time:5211.2 off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:0 pkt_pts_time:0 pkt_dts:0 pkt_dts_time:0 off:-5211202638 off_time:-5211.2
muxer <- type:video pkt_pts:0 pkt_pts_time:0 pkt_dts:0 pkt_dts_time:0 size:2351
demuxer -> ist_index:0 type:video next_dts:66666 next_dts_time:0.066666 next_pts:66666 next_pts_time:0.066666 pkt_pts:5211235965 pkt_pts_time:5211.24 pkt_dts:5211235965 pkt_dts_time:5211.24 off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:33327 pkt_pts_time:0.033327 pkt_dts:33327 pkt_dts_time:0.033327 off:-5211202638 off_time:-5211.2
muxer <- type:video pkt_pts:33 pkt_pts_time:0.033 pkt_dts:33 pkt_dts_time:0.033 size:2199
demuxer -> ist_index:0 type:video next_dts:99993 next_dts_time:0.099993 next_pts:99993 next_pts_time:0.099993 pkt_pts:5211269292 pkt_pts_time:5211.27 pkt_dts:5211269292 pkt_dts_time:5211.27 off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:66654 pkt_pts_time:0.066654 pkt_dts:66654 pkt_dts_time:0.066654 off:-5211202638 off_time:-5211.2
muxer <- type:video pkt_pts:67 pkt_pts_time:0.067 pkt_dts:67 pkt_dts_time:0.067 size:5292
demuxer -> ist_index:0 type:video next_dts:133320 next_dts_time:0.13332 next_pts:133320 next_pts_time:0.13332 pkt_pts:5211302619 pkt_pts_time:5211.3 pkt_dts:5211302619 pkt_dts_time:5211.3 off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:99981 pkt_pts_time:0.099981 pkt_dts:99981 pkt_dts_time:0.099981 off:-5211202638 off_time:-5211.2
muxer <- type:video pkt_pts:100 pkt_pts_time:0.1 pkt_dts:100 pkt_dts_time:0.1 size:7874
demuxer -> ist_index:0 type:video next_dts:166647 next_dts_time:0.166647 next_pts:166647 next_pts_time:0.166647 pkt_pts:5211335946 pkt_pts_time:5211.34 pkt_dts:5211335946 pkt_dts_time:5211.34 off:-5211202638 off_time:-5211.2
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:133308 pkt_pts_time:0.133308 pkt_dts:133308 pkt_dts_time:0.133308 off:-5211202638 off_time:-5211.2
frame= 5 fps=0.0 q=-1.0 Lsize= 30kB time=00:00:00.10 bitrate=2451.6kbits/s speed=53.6x
video:29kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.476493%
In MJPEG I get correct timestamps (and they seem to sync quite well when I add USB audio). From ffmpeg -debug_ts -vcodec mjpeg -i /dev/video0 -c:v copy -t 0.1 timestamps.mkv:
[...]
demuxer -> ist_index:0 type:video next_dts:NOPTS next_dts_time:NOPTS next_pts:NOPTS next_pts_time:NOPTS pkt_pts:6161714914 pkt_pts_time:6161.71 pkt_dts:6161714914 pkt_dts_time:6161.71 off:-6161714914 off_time:-6161.71
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:0 pkt_pts_time:0 pkt_dts:0 pkt_dts_time:0 off:-6161714914 off_time:-6161.71
muxer <- type:video pkt_pts:0 pkt_pts_time:0 pkt_dts:0 pkt_dts_time:0 size:53961
demuxer -> ist_index:0 type:video next_dts:33333 next_dts_time:0.033333 next_pts:33333 next_pts_time:0.033333 pkt_pts:6161748242 pkt_pts_time:6161.75 pkt_dts:6161748242 pkt_dts_time:6161.75 off:-6161714914 off_time:-6161.71
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:33328 pkt_pts_time:0.033328 pkt_dts:33328 pkt_dts_time:0.033328 off:-6161714914 off_time:-6161.71
muxer <- type:video pkt_pts:33 pkt_pts_time:0.033 pkt_dts:33 pkt_dts_time:0.033 size:44384
demuxer -> ist_index:0 type:video next_dts:66661 next_dts_time:0.066661 next_pts:66661 next_pts_time:0.066661 pkt_pts:6161781569 pkt_pts_time:6161.78 pkt_dts:6161781569 pkt_dts_time:6161.78 off:-6161714914 off_time:-6161.71
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:66655 pkt_pts_time:0.066655 pkt_dts:66655 pkt_dts_time:0.066655 off:-6161714914 off_time:-6161.71
muxer <- type:video pkt_pts:67 pkt_pts_time:0.067 pkt_dts:67 pkt_dts_time:0.067 size:44177
demuxer -> ist_index:0 type:video next_dts:99988 next_dts_time:0.099988 next_pts:99988 next_pts_time:0.099988 pkt_pts:6161814896 pkt_pts_time:6161.81 pkt_dts:6161814896 pkt_dts_time:6161.81 off:-6161714914 off_time:-6161.71
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:99982 pkt_pts_time:0.099982 pkt_dts:99982 pkt_dts_time:0.099982 off:-6161714914 off_time:-6161.71
muxer <- type:video pkt_pts:100 pkt_pts_time:0.1 pkt_dts:100 pkt_dts_time:0.1 size:44061
demuxer -> ist_index:0 type:video next_dts:133315 next_dts_time:0.133315 next_pts:133315 next_pts_time:0.133315 pkt_pts:6161848224 pkt_pts_time:6161.85 pkt_dts:6161848224 pkt_dts_time:6161.85 off:-6161714914 off_time:-6161.71
demuxer+ffmpeg -> ist_index:0 type:video pkt_pts:133310 pkt_pts_time:0.13331 pkt_dts:133310 pkt_dts_time:0.13331 off:-6161714914 off_time:-6161.71
frame= 4 fps=0.0 q=-1.0 Lsize= 183kB time=00:00:00.10 bitrate=14843.9kbits/s speed=0.738x
video:182kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.440019%
Default kernel:
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux
pi@raspberrypi:~ $ modinfo bcm2835-v4l2
filename: /lib/modules/4.4.38-v7+/kernel/drivers/media/platform/bcm2835/bcm2835-v4l2.ko
version: 0.0.2
license: GPL
author: Vincent Sanders
description: Broadcom 2835 MMAL video capture
srcversion: A7D6DBEB2D67BE83A6FC776
depends: videobuf2-v4l2,videodev,videobuf2-vmalloc,videobuf2-core,v4l2-common
intree: Y
vermagic: 4.4.38-v7+ SMP mod_unload modversions ARMv7
parm: debug:int
parm: bcm2835_v4l2_debug:Debug level 0-2
parm: video_nr:videoX start numbers, -1 is autodetect (array of int)
parm: max_video_width:Threshold for video mode (int)
parm: max_video_height:Threshold for video mode (int)
parm: gst_v4l2src_is_broken:If non-zero, enable workaround for Gstreamer (int)
After rpi-update
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.4.47-v7+ #961 SMP Sun Feb 5 20:17:00 GMT 2017 armv7l GNU/Linux
pi@raspberrypi:~ $ modinfo bcm2835-v4l2
filename: /lib/modules/4.4.47-v7+/kernel/drivers/media/platform/bcm2835/bcm2835-v4l2.ko
version: 0.0.2
license: GPL
author: Vincent Sanders
description: Broadcom 2835 MMAL video capture
srcversion: A7D6DBEB2D67BE83A6FC776
depends: videobuf2-v4l2,videodev,videobuf2-vmalloc,videobuf2-core,v4l2-common
intree: Y
vermagic: 4.4.47-v7+ SMP mod_unload modversions ARMv7
parm: debug:int
parm: bcm2835_v4l2_debug:Debug level 0-2
parm: video_nr:videoX start numbers, -1 is autodetect (array of int)
parm: max_video_width:Threshold for video mode (int)
parm: max_video_height:Threshold for video mode (int)
parm: gst_v4l2src_is_broken:If non-zero, enable workaround for Gstreamer (int)
Activity
6by9 commentedon Feb 10, 2017
I've just tried this using v4l2-ctl and setting the driver
debugparameter to 2 to enable more debug logging. I'm seeing PTS values coming back for all video buffers, although admittedly the header bytes have a 0 timestamp from the GPU (shouldn't matter as the kernel driver then grabs the current kernel time):One thought is that the header bytes are likely to arrive at the kernel later than the timestamp on the first frame, so we may well have a backwards jump there. Header bytes in the above will be at 109.773225, whilst the first encoded frame is at 109.558918. Is that what is upsetting ffmpeg? The driver does have the kernel start time stored, so could insert that very easily instead.
MJPEG has no header bytes so that would fit with it having no issues.
(Having been looking at the video encoder over the last couple of days, it seems that if using inline headers then those all come out with a 0 timestamp too. I don't know what the best thing to do in that situation is. Possibly store the last frame timestamp and reuse it)
duvrai commentedon Feb 10, 2017
I don't know what upsets ffmpeg. I just did
modprobe bcm2835-v4l2 debug=2to compareh264 bcm2835-v4l2 debug:
ffmpeg says for the first frame:
pkt_pts:NOPTSandpkt_dts:NOPTSbutoff:-15723895046seems to be the second frame from bcm2835-v4l2 debug above.bcm2835-v4l2 debug with mjpeg:
now ffmpeg gets actual values
pkt_pts:15468476757andpkt_dts:15468476757corresponding to the first frame in bcm2835-v4l2 debug output:duvrai commentedon Feb 10, 2017
In short: timestamps are missing/corrupted for the first frame, subsequent frames are ok.
6by9 commentedon Feb 10, 2017
It looks like there is an issue on both sides.
I've fixed the V4L2 driver to return the start time for the header bytes, and ffmpeg 3.1.1 that I'm running still complains. What version are you running as you appear to have an odd hash as your version number (N-83414-ge477f09). Self built?
vs extended kernel log
All those kernel log timestamps look valid to me. The interesting bit is that the kernel reports delivering buffers of size 27, 19924, 2382, 3834. ffmpeg is reporting frames of 19951, 2382, 3834. It has combined the header bytes into the first frame, and I suspect it has dropped the timestamp in the process.
http://lists.ffmpeg.org/pipermail/ffmpeg-devel-irc/2016-March/003424.html from 13:38:02 CET onwards appears to indicate that there is an issue in ffmpeg if handling raw H264 streams.
I'll look into making sure that the V4L2 timestamps are correct (mostly done already), but I'm not going to go debugging FFmpeg in any detail.
duvrai commentedon Feb 10, 2017
Thanks @6by9 for all the work and the quick response! I'll try your modifications when they are ready and file a bug with ffmpeg if needed.
I built my ffmpeg from the master git branch because I couldn't find a binary distribution. (Compiling times on the pi 3 are now bearable with
make -j4)I also tried libav's avconv, but couldn't get it to work at all with v4l2.
6by9 commentedon Feb 10, 2017
I'll dig out my diffs for V4L2 and push them to my fork of the kernel branch if you fancy rebuilding them. I have been working against the 4.9 tree instead of 4.4, but that shouldn't make a big difference.
As it happens I pulled down and kicked off a build of ffmpeg before I left the office. Hopefully it'll build successfully and be waiting for me on Monday.
6by9 commentedon Feb 13, 2017
Sorry, I'm going to have to say this appears to be FFMPEG mishandling header bytes from V4L2 devices.
With my rebuilt version from top of tree + some extra logging, the V4L2 of FFMPEG is seeing
(I've just added the code to correctly signal key frames through V4L2 in case FFMPEG was needing to see those, and that is why the second buffer has flags 0x2009)
Those buffers coming from V4L2 are correct and have valid timestamps.
Looks to be a tad strange - that duration looks very like AV_NOPTS_VALUE (0x8000000000000000) converted to seconds, but the start time is also the timestamp of the first P-frame, ignoring the header bytes and I-frame.
Then again something has taken the header bytes and I-frame and combined them into a single buffer with pts=AV_NOPTS_VALUE (which is also what triggers the warning), but there's too much code for me to trawl through to try and find where. Hopefully the FFMPEG developers would be able to identify that pretty quickly.
I'll push my driver updates (all kernel side), but then I'm going to have to park this waiting on more information.
There are only a couple of V4L2 cameras that I know of that can produce H264. The Pi is one, and the Logitech C920/930 being the other, so this is quite probably a fairly unusual case within FFMPEG.
6by9 commentedon Feb 13, 2017
Updates pushed to https://github.com/6by9/linux rpi-4.9.y branch.
The timestamp code changed between 4.4 and 4.9, so backporting is a bit of a faff.
BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp
duvrai commentedon Feb 13, 2017
Ok I'll try 4.9
I noticed that the timestamps generated by the bcm2835 driver are boot-time based, while the other devices all use UNIX time. Is that also a bug?
6by9 commentedon Feb 13, 2017
Nope. All our buffers have the V4L2_BUF_FLAG_TIMESTAMP MONOTONIC flag set, which means they are with respect to clock_gettime(CLOCK_MONOTONIC).
man clock_gettimeThe unspecified starting point happens to be boot time.
BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp
BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp
BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp
BCM2835-V4L2: Ensure H264 header bytes get a sensible timestamp
270 remaining items
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…
staging: bcm2835-camera: Ensure H264 header bytes get a sensible time…