-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
Zerocopy gstreamer videocapture #18377
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
base: 4.x
Are you sure you want to change the base?
Zerocopy gstreamer videocapture #18377
Conversation
@michaelgruner Thanks for the contribution! |
@michaelgruner , does it work when GStreamer backend is built as a plug-in? |
I'm not sure. I'll give it a try. |
Just tested. It works as with built-in.
|
@asmorkalov, shouldn't we just merge it? |
@vpisarev Consider reviewing this first. Current As you can see several methods are just not implemented - do we really need them in this basic interface? |
@vpisarev . I retriggered Ci build to get status and looking the code right now. Will come back with review results today. |
Please note, that NONE of triggered builds from default "pre-commit" scope performs building/testing of GStreamer code. |
@alalek is this something I need to do on my side as external contributor? |
@michaelgruner Thanks for your contribution. Alexander works on release right now. We have a bunch of changes related to multimedia and plugins support and plan to integrate your work after release with plugins API rework. |
try { | ||
dst.getMatRef() = GStreamerMat(sample.get()); | ||
} catch (cv::Exception &e) { | ||
CV_WARN(e.err); | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part introduces regression.
OutputArray
is a wrapper over multiple container types, including UMat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I get some clarification on this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
retrieveFrame
method has OuputArray
as parameter. OutputArray is proxy class that wraps cv::Mat, cv::UMat, std::vector and some other containers to provide flexibility to OpenCV users. In general case dst
can be UMat and the proposed solution fails. OutputArray
has kind()
method that reports original container type. You need to check it before assignment. Other cases can be handled with copy as it was done before.
{ | ||
flags += CV_MAT_TYPE(_type); | ||
rows = _rows; | ||
cols = _cols; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fillMat
naming is not accurate and confusing.
There are setSize()
/ finalizeHdr()
/ updateContinuityFlag()
calls in the core module.
The best option is trying to reuse these calls (to keep library consistent by avoiding duplication of code).
Or at least, rename to fillMatHeader()
.
if (!sample) | ||
return false; | ||
if (false == gst_buffer_map (gstdata->buffer, &(gstdata->info), GST_MAP_READ)) { | ||
CV_Error(Error::Code::StsUnsupportedFormat, ("Unable to map GstBuffer")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memory leak is caused here (new gstdata
)
gstdata->sample.release(); | ||
|
||
delete gstdata; | ||
delete u; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it is better to swap order of delete operators to avoid having of dangling pointers.
* \brief CvCapture_GStreamer::retrieveFrame | ||
* \return IplImage pointer. [Transfer Full] | ||
* Retrieve the previously grabbed buffer, and wrap it in an IPLImage structure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that it is copy-paste, but I believe we should not copy outdated comments anyway.
UMatData* u = new UMatData(this); | ||
u->data = u->origdata = gstdata->info.data; | ||
u->size = gstdata->info.size; | ||
u->userdata = gstdata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vpisarev Is it valid usage of userdata
field?
{ | ||
CV_Error(Error::Code::StsBadFunc, ("GStreamer allocator may not alloc raw data")); | ||
return nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CV_Error()
throws exception (marked with "noreturn" attribute).
return nullptr;
is unreachable code.
pipeline.release(); | ||
} | ||
CV_Error(Error::Code::StsBadFunc, ("GStreamer allocator may not alloc raw data")); | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return is unreachable too.
@michaelgruner Could you rebase you PR to current master and fix remarks? |
Sure thing. Allow me a few days and I’ll ping you back.
… On 27 Jan 2021, at 04:43, Alexander Smorkalov ***@***.***> wrote:
@michaelgruner Could you rebase you PR to current master and fix remarks?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
@michaelgruner Friendly reminder. |
ff38ea7
to
2efaffd
Compare
Hey all, I'm sorry I got lost for a while there. I'm back with some time to get this through. I'm rebasing the changes to the latest master and figuring out conflicts. |
Pulling #22018 first as a dependency. |
… mat lifespan Instead of copying the data from the GstSample into a Mat, the allocator keeps a mapped reference of the GstBuffer pulled from the pipeline. By implementing a custom GStreamerAllocator we instruct OpenCV on how to properly unmap and unref the GstBuffer, even if the data was copied to a regular Mat.
2efaffd
to
25df008
Compare
25df008
to
7a3fbbb
Compare
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Description
Removes an exisiting memory copy when creating a Mat from a Gstreamer buffer, when using the VideoCapture class. To do so, the pointer to the Gstreamer buffer data is shared in the Mat. To handle the pointer's lifespan, a Gstreamer custom allocator is proposed so that when the Mat's data is to be destroyed, the data is properly returned and freed by Gstreamer.
Gstreamer is the preferred multimedia framework for embedded devices. Considering this, performance is typically a priority. Currently, capturing with OpenCV is slower than capturing with plain Gstreamer. I'm keeping track of the improvements here.
Pending Issues