Map cv::Mat to QAbstractPlanarVideoBuffer subclass
Unsolved
General and Desktop
-
Im trying to map a cv::Mat to QAbstractPlanarVideoBuffer and then to QVideoFrame.
A conversion from cv::Mat to QImage and then to QVideoFrame is to slow for my use case.
The following code produces mostly green frames. I have no experience with video encoding so I don't know what the problem could be. Any help would be appreciated. Thanks in advance.CVBuffer.h:
#ifndef CVBUFFER_H #define CVBUFFER_H #include <QAbstractPlanarVideoBuffer> #include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/calib3d.hpp" class CVBuffer : public QAbstractPlanarVideoBuffer { public: CVBuffer(cv::Mat *frame); ~CVBuffer(); int map(MapMode mode, int *numBytes, int bytesPerLine[4], uchar *data[4]); MapMode mapMode() const; void unmap(); private: cv::Mat frame; }; #endif // CVBUFFER_H
CVBuffer.cpp
#include "cvbuffer.h" CVFBuffer::FBuffer(cv::Mat* frame) : QAbstractPlanarVideoBuffer(NoHandle) , frame(frame->clone()) { } CVBuffer::~CVBuffer() { } int FBuffer::map(MapMode mode, int *numBytes, int bytesPerLine[4], uchar *data[4]) { if (mode == ReadOnly) { *numBytes = frame.total() * frame.elemSize(); bytesPerLine[0] = frame.step; data[0] = frame.data; return 3; } return 0; } FBuffer::MapMode FBuffer::mapMode() const { return ReadOnly; } void FBuffer::unmap() { }
-
Why to you return 3? I only see that you have one plane.
Also you should free/unref your frame in the dtor -
I have no experience in this but it may help to look at how Qt use gstreamer's video frame to construct a planar video buffer:
https://code.woboq.org/qt5/qtmultimedia/src/gsttools/qgstvideobuffer.cpp.html