-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
ffmpeg freezes when enconding with h264_omx at 1080p or 720p #1087
Comments
Have you tried removing option until the problem goes away to see if you can narrow down exactly where the problem is? We don't have any cameras like that in the office, and all this code is someone elses, so unlikely we would be able to help much here. Might be worth posting on the forums as it will get more viewers who might be able to help. |
I've noticed what I believe to be a similar issue with ffmpeg h264_omx lockup. I haven't had a chance to debug it further, but a workaround I've used is revert to an older firmware version |
I'd debug it if you or someone else guide me through. It could also be related to this issue: #851 |
If this is helpful, here is a 30s sample MPEG2-TS file that almost always triggers the hang during encoding (https://www.dropbox.com/s/w7n9nn4eeeyq7qc/sample.ts?dl=1). The ffmpeg command I use is Trying the steps listed here might be useful in analyzing the frozen state. Might also be able to use gdb to see where ffmpeg is stuck. EDIT: I gave it a go.
This may well be an ffmpeg issue, but reverting to an older rpi firmware version seems to get around it. |
Posting the date of working and non-working firmware may be useful. |
The rpi-update hash that first exhibits the ffmpeg h264_omx freeze/hang is |
I'm suspecting that ec9d84e just tweaked the timing of video_encode slightly. It switched to using the ISP (hardware) to format convert the image at the front of video_encode (previously it was on the general purpose VPU). Running
in the logging. So the codec has produced a 98kB frame whilst the output buffer is only 64kB in size. It therefore fragments it over two buffers with As a test I tweaked the codec to say it wanted 256kB buffers, and I then see no stall.
on frame 54 or 55 (that's probably just a UI glitch in ffmpeg - the same stream in should always give exactly the same stream out for the same parameters. It's always a 49207 byte frame being copied). Checking on 64kB buffers again, it's not always exactly the same point. Frame 118 is the 100810byte frame I saw first, but I've now seen frame 134/135 where we have returned a buffer of 56408. Either way, video_encode is waiting on being given a buffer to copy output data into, and the output handling didn't change with that commit. I suspect there is a race condition where buffers are getting lost, and once they are all gone then you have a stall. I couldn't say at this point whether it was in FFmpeg or the firmware/ILCS. |
VPU debugger confirms that video_encode is just waiting for a buffer. There's only one buffer allocated for each of the input and output ports. Minor observation. That commit does relax the INPUT port requirements of video_encode so that nSliceHeight does NOT have to be a multiple of 16 if it can process the frame with the ISP. You can set nSliceHeight to be the same as video_decode, which would avoid the requirement for the copy. |
Sorry for my stupidity, but how can I do that? Google wasn't very helpful. |
"You" as in the FFmpeg application.
Change nSliveHeight to
It was more an observation than saying that was the problem. |
Oh. Before building, I suppose? I'll try that. |
OK, I totally misread where FFmpeg was stuck. It's waiting for an INPUT buffer to be available, not for an output buffer. The codec is blocked waiting for space to CABAC encode a frame, as the output FIFO would appear to be full.
Timestamps are usecs, and the frames are at 33ms increments, so the input is 1.1seconds ahead. Our FIFO is 2MB in size. 15Mbit/s = 1.875MB/s, so that would imply that we are almost bang on with rate control if we're stalling due to the FIFO being full with a 1.1sec difference (should be 1.066s). During startup we do get |
Something appears messed up in FFmpeg and it ISN'T handling
Only that 3rd buffer is complete, but they've all been considered as full frames. Zero copy appears to be a red herring (although I haven't checked if it used that path previously). Something is moving the chroma planes from the contiguous layout that the decoder will have generated, and therefore it isn't correctly laid out to be accepted by the encoder. A shame, but there you go. |
Comparing the VPU utilisation, with the old code it is plainly converting from the internal format in video_decode, and then converting back to the internal format as part of video_encode, taking approx 40ms from the steps for successive frames. Each conversion is around 10ms. With the new code, the second conversion isn't on the VPU, and we're at 33ms between conversions. (Performance gain!). It's obviously tickled the timing though. Several solutions:
|
@andreluisos Sorry, I've been debugging @evanglasgow 's issue as he provided an easily reproducable test case and found the firmware change where his test case failed. Looking at your original post I suspect that your issue is something different. You're running at a couple of frames a second, so the input side of the encoder will be far slower than the output. You're also at a much lower bitrate so very unlikely to fill the codec's output FIFO. It would be worth doing an Running the video side of your command with my USB webcam (Logitech C270) and everything looks fine. You could try starting your command
Should any of those size parameters hit 64kB then you may be seeing the same issue, but otherwise I doubt it. |
@JamesH65 Yes, Mr. Hughes, I have. I'm using
|
@6by9 Thanks for the analysis and deep-dive into ffmpeg! Darn race conditions... I sort of suspected that's where this would end up and it (rightfully) seemed a bit daunting. I really appreciate the expertise. I'll try your quick patch and have it churn through some longer clips just to verify all is well there. I'm on ffmpeg git master and it doesn't look like the diff quite matches what's there, but I think I got the key bit - just that it won't be a perfect test. @andreluisos Sorry for hijacking the issue! |
I'm experiencing ffmpeg freezing problem as well, which did not happen on older version of rpi firmware. It encodes for about a minute, and freezes. I'm running motion software version 4.1.1, ffmpeg 3.4.4, and motion encodes 1600x1200 resolution video via ffmpeg API. I applied the ffmpeg OMX_BUFFERFLAG_ENDOFFRAME patch that @6by9 suggested in the above post, and seems to have fixed the problem. I'll do more testing to confirm that it has really been fixed. |
@andreluisos Sorry, if there isn't a reproducable test case, then there is very little we can do to help. @evanglasgow That sounds positive. The diff was copied from git diff rather than made from a patch, and I had some extra logging lines in there, so that probably explains why it doesn't quite line up. Sorry about that. @jasaw It sounds highly plausible that you're seeing the same problem. TBH I'm amazed that this broken behaviour in FFmpeg hasn't tripped things up before. I'll try to make a clean patch that can be applied to the Raspbian build of ffmpeg fairly quickly, and will then look at getting it upstreamed into mainline FFmpeg. |
Oh! I didn't see that. Been a tough week and I didn't have much time. I don't know if it's still important, but here's the result with the command It hangs in there (little bit more than a minute of recording, I think) with the latest rpi firmware. Also, the comments became too technical for me to understand much. @evanglasgow Thank you for joining in. |
Accidently closed the issue. ehehheheh Don't hate, me guys. :( |
From your screenshot I see
in that log. If you're asking for 1400kbit/s and 2fps, then each frame should be ~87kB. I would therefore expect almost all buffers to be split and therefore trigger the FFmpeg bug, moreso for I-frames as they are generally allocated more bits in the stream. |
Did that stall? Your new command hasn't specified the resolution (V4L2 will generally remember the last request mode), but has increased the frame rate to 5fps and dropped the bitrate. The upshot is that the buffer sizes will have all come down to an average of 25kB per frame, and I'd expect the system to keep going significantly longer. |
It did stall. I did set the resolution through v4l2. I was testing the pixelformats (YUVY and MJPEG available). The manufacturer says the webcam sends the video already encoded with h264. Using copy (as output codec), however, gave me huge video sizes (probably because of the high bitrate it's set by default, which I don't think I can change). I know it's not about the topic, which comes down to h264_omx, but just wanted to let you guys know that I don't think this webcam encodes anything on its own. |
Issue opened on FFmpeg's issue tracker https://trac.ffmpeg.org/ticket/7687 Seeing as jasaw's issue on there hasn't been touched for 17 months I'm not expecting any real input, so I may just throw a patch at ffmpeg-devel. |
v4l2-ctl -V will confirm the resolution and format that was last requested from the webcam. AFAICT you need to either build ffmpeg for yourself, or be patient for a patched version to be released. |
That's no problem! Thank you so much for the help. Meanwhile, I'll be using a previous RPI firmware. About |
FFmpeg patch sorted and a patched build should hopefully be in the Raspbian repos within the next hour. Version should be |
I think it's up. I'll be testing. |
Whatever you did here, it fixed a freezing bug I've been experiencing for some time. Thank you! |
Either way, if this is now resolved then it should be closed. |
Issue fixed! Thank you all involved in detecting and fixing this. |
Bug still present!
Also, I try next command but it freeze too (~1h of encode): This is command for encode and generate image from video feed every second.
|
The original bug is still present in upstream FFmpeg as they haven't responded to their bug tracker - https://trac.ffmpeg.org/ticket/7687. It should be fixed in the Raspbian repos as we have an additional patch. If you think you have a different lockup, then please create a new issue with as much detail as possible. I'd also suggest you describe what you're trying to achieve
raspivid will produce H264 encoded video, and option |
My configuration:
First I try to build latest version of ffmpeg from git but it freeze as this thread say then I try to install latest version of ffmpeg from raspbian repository via "sudo apt-get install ffmpeg" and it installs ffmpeg version 3.2.14-1~deb9u1+rpt1 and it still freeze. I know that I can get h264 from raspivid, but run command with raw video in yuv420p format from raspivid stdout is likely for quick reproduce of the bug because encoder was freeze on 2 seconds. In my case I need two output files from pi camera, one is video in mp4 container and second output must be series of jpeg images for every seconds of video for analysis in other software, like motion detection. So, my real command is below:
But it still freeze at random run time, for example: 1 minute, 24 minutes or 1 h, etc. I try different run commands for ffmpeg but result it stay the same => freeze and in same cases it may cause OS to freeze too. |
I took my car to the garage as the engine warning light was on (symptom). They replaced the spark plugs (diagnosis).
I've done it for you #1185 |
I don't know what happens, it simply freezes after 1-6mins of real time encoding, deppending on the resolution.
ffmpeg -f v4l2 -re -video_size 1024x576 -r 2 -vsync 1 -i /dev/video0 -f alsa -async 1 -i hw:1,0 -c:v h264_omx -b:v 1000k -c:a aac -b:a 170k -f segment -strftime 1 -segment_time 300 -reset_timestamps 1 -segment_format mkv rec_%Y-%m-%d_%H-%M-%S.mkv
It doesn't freeze if I use 1024x576 or lower resolution, apparently. Also, it only happens with h264_omx.
I'm using an USB camera (Logitech C922).
The text was updated successfully, but these errors were encountered: