VideoCapture segfault fix for #22126#22137
Conversation
Please rebase to have 1 commit only. |
|
I used this code for testing: The video is the one attached to the issue (#22126). Before the patch (original 4.x) when I run the application it ends with: After the patch: (no segfault). I also save all frames as jpeg and checked that they are ok: not distorted or cropped. @alalek: Can you please provide me a link to a video that don't work so I can test it ? NOTE: I rebased the code to have only one commit. |
|
|
|
I saved the frames a JPEG before and after the patch:
Sure the frame size changes (and it does with the original code too) but this is how the video was created. |
alalek
left a comment
There was a problem hiding this comment.
Looks like it works. Thank you!
As a bug fix this patch should go into 3.4 branch first.
We will merge changes from 3.4 into 4.x regularly (weekly/bi-weekly).
Please:
- change "base" branch of this PR: 4.x => 3.4 (use "Edit" button near PR title)
- rebase your commits from 4.x onto 3.4 branch. For example:
git rebase -i --onto upstream/3.4 upstream/4.x
(check list of your commits, save and quit (Esc + "wq" + Enter)
whereupstreamis configured by following this GitHub guide and fetched (git fetch upstream). - push rebased commits into source branch of your fork (with
--forceoption)
Note: no needs to re-open PR, apply changes "inplace".
| img_convert_ctx, | ||
| sw_picture->data, | ||
| sw_picture->linesize, | ||
| 0, context->coded_height, |
There was a problem hiding this comment.
Lets use sw_picture->height here.
According to ffmpeg sample code: https://github.com/FFmpeg/FFmpeg/blob/n4.4.2/fftools/ffplay.c#L925-L926
BTW, from the same samples code there is no usage of coded_width/coded_height values with modern FFmpeg.
Perhaps we need to update sws_getCachedContext() call too (probably later, and align with "n0,8" ffmpeg version).
There was a problem hiding this comment.
Actually I tried 3.4 as it is and it works like a charm. No segfault.
Even the issue #22145 is not present.
Maybe there are some specific 4.x changes that brakes it.
There was a problem hiding this comment.
Code is different, but there is the same problem on 3.4: https://github.com/opencv/opencv/blob/3.4.18/modules/videoio/src/cap_ffmpeg_impl.hpp#L1409-L1411
There was a problem hiding this comment.
Actually 3.4 always use "video_st->codec" but 4.x uses "context" and sometimes it mixes both like:
if( img_convert_ctx == NULL ||
frame.width != video_st->CV_FFMPEG_CODEC_FIELD->width ||
frame.height != video_st->CV_FFMPEG_CODEC_FIELD->height ||
frame.data == NULL )
{
// Some sws_scale optimizations have some assumptions about alignment of data/step/width/height
// Also we use coded_width/height to workaround problem with legacy ffmpeg versions (like n0.8)
int buffer_width = context->coded_width, buffer_height = context->coded_height;
May be the issue is there, but for sure it's related to 4.x (3.4 is OK).
There was a problem hiding this comment.
My bad. I don't know why but from time to time it pass without crashing.
But 3.4 can crash too.
I'll update the PR.
There was a problem hiding this comment.
PR ready for 3.4 (builds and run correctly)
| picture->data, | ||
| picture->linesize, | ||
| 0, video_st->codec->coded_height, | ||
| 0, video_st->codec->height, |
There was a problem hiding this comment.
Again, please use picture->height here.
According to ffmpeg sample code: https://github.com/FFmpeg/FFmpeg/blob/n4.4.2/fftools/ffplay.c#L925-L926
There was a problem hiding this comment.
You are right, we use picture->data and picture->linesize so it make sense.
Done (build and run OK)
Can you explain the reason for this problem? What caused cv2.VideoCapture segfaults? |






resolves #22126
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.