1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Remove all Qt video code

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-08-29 14:42:38 +02:00
parent 938650fa40
commit ccdb3b57fd
2 changed files with 9 additions and 146 deletions

View File

@ -22,54 +22,9 @@
using namespace cv; using namespace cv;
static inline void fromYCbCrToRGB(
uint8_t Y, uint8_t Cb, uint8_t Cr,
uint8_t& R, uint8_t& G, uint8_t& B)
{
int r = Y + ((1436 * (Cr - 128)) >> 10),
g = Y - ((354 * (Cb - 128) + 732 * (Cr - 128)) >> 10),
b = Y + ((1814 * (Cb - 128)) >> 10);
if(r < 0) {
r = 0;
} else if(r > 255) {
r = 255;
}
if(g < 0) {
g = 0;
} else if(g > 255) {
g = 255;
}
if(b < 0) {
b = 0;
} else if(b > 255) {
b = 255;
}
R = static_cast<uint8_t>(r);
G = static_cast<uint8_t>(g);
B = static_cast<uint8_t>(b);
}
Camera::Camera() Camera::Camera()
: refcount{0}, camera{new QCamera} : refcount{0}
{ {
camera->setCaptureMode(QCamera::CaptureVideo);
camera->setViewfinder(this);
#if 0 // Crashes on Windows
QMediaService *m = camera->service();
QVideoEncoderSettingsControl *enc = m->requestControl<QVideoEncoderSettingsControl*>();
QVideoEncoderSettings sets = enc->videoSettings();
sets.setResolution(640, 480);
enc->setVideoSettings(sets);
#endif
connect(camera, SIGNAL(error(QCamera::Error)), this, SLOT(onCameraError(QCamera::Error)));
supportedFormats << QVideoFrame::Format_YUV420P << QVideoFrame::Format_YV12 << QVideoFrame::Format_RGB32;
} }
void Camera::suscribe() void Camera::suscribe()
@ -77,7 +32,7 @@ void Camera::suscribe()
if (refcount <= 0) if (refcount <= 0)
{ {
refcount = 1; refcount = 1;
cap.open(0); cam.open(0);
} }
else else
refcount++; refcount++;
@ -89,7 +44,7 @@ void Camera::unsuscribe()
if (refcount <= 0) if (refcount <= 0)
{ {
cap.release(); cam.release();
refcount = 0; refcount = 0;
} }
} }
@ -97,78 +52,10 @@ void Camera::unsuscribe()
Mat Camera::getLastFrame() Mat Camera::getLastFrame()
{ {
Mat frame; Mat frame;
cap >> frame; cam >> frame;
return frame; return frame;
} }
bool Camera::start(const QVideoSurfaceFormat &format)
{
if(supportedFormats.contains(format.pixelFormat()))
{
frameFormat = format.pixelFormat();
QAbstractVideoSurface::start(format);
return true;
}
else
{
QMessageBox::warning(0, "Camera error", "The camera only supports rare video formats, can't use it");
return false;
}
}
bool Camera::present(const QVideoFrame &frame)
{
QVideoFrame frameMap(frame); // Basically a const_cast because shallow copies
if (!frameMap.map(QAbstractVideoBuffer::ReadOnly))
{
qWarning() << "Camera::present: Unable to map frame";
return false;
}
int w = frameMap.width(), h = frameMap.height();
int bpl = frameMap.bytesPerLine(), size = frameMap.mappedBytes();
QVideoFrame frameCopy(size, QSize(w, h), bpl, frameMap.pixelFormat());
frameCopy.map(QAbstractVideoBuffer::WriteOnly);
memcpy(frameCopy.bits(), frameMap.bits(), size);
frameCopy.unmap();
lastFrame = frameCopy;
frameMap.unmap();
return true;
}
QList<QVideoFrame::PixelFormat> Camera::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
if (handleType == QAbstractVideoBuffer::NoHandle)
return supportedFormats;
else
return QList<QVideoFrame::PixelFormat>();
}
void Camera::onCameraError(QCamera::Error value)
{
QMessageBox::warning(0,"Camera error",QString("Error %1 : %2")
.arg(value).arg(camera->errorString()));
}
bool Camera::isFormatSupported(const QVideoSurfaceFormat& format) const
{
if (format.pixelFormat() == 0)
{
//QMessageBox::warning(0, "Camera eror","The camera's video format is not supported !");
return QAbstractVideoSurface::isFormatSupported(format);
}
else if(supportedFormats.contains(format.pixelFormat()))
{
return true;
}
else
{
QMessageBox::warning(0, tr("Camera eror"),
tr("Camera format %1 not supported, can't use the camera")
.arg(format.pixelFormat()));
return false;
}
}
QImage Camera::getLastImage() QImage Camera::getLastImage()
{ {
Mat3b src = getLastFrame(); Mat3b src = getLastFrame();
@ -190,9 +77,7 @@ vpx_image Camera::getLastVPXImage()
int w = frame.size().width, h = frame.size().height; int w = frame.size().width, h = frame.size().height;
vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1); // I420 == YUV420P, same as YV12 with U and V switched vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1); // I420 == YUV420P, same as YV12 with U and V switched
//qWarning() << "Camera::getLastVPXImage: Using experimental RGB32 conversion code" << w << ","<<h;
size_t i=0, j=0; size_t i=0, j=0;
for( int line = 0; line < h; ++line ) for( int line = 0; line < h; ++line )
{ {
const cv::Vec3b *srcrow = frame[line]; const cv::Vec3b *srcrow = frame[line];
@ -230,6 +115,5 @@ vpx_image Camera::getLastVPXImage()
} }
} }
} }
return img; return img;
} }

View File

@ -17,25 +17,18 @@
#ifndef CAMERA_H #ifndef CAMERA_H
#define CAMERA_H #define CAMERA_H
#include <QCamera> #include <QImage>
#include <QVideoFrame>
#include <QAbstractVideoSurface>
#include "vpx/vpx_image.h" #include "vpx/vpx_image.h"
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
/** /**
* This class is a wrapper to share a camera's captured video frames * This class is a wrapper to share a camera's captured video frames
* In Qt cameras normally only send their frames to a single output at a time * It allows objects to suscribe and unsuscribe to the stream, starting
* So you can't, for example, send the frames over the network * the camera only when needed, and giving access to the last frames
* and output them to a widget on the screen at the same time
*
* Instead this class allows objects to surscribe and unsuscribe, starting
* the camera only when needed, and giving access to the last frame
**/ **/
class Camera : private QAbstractVideoSurface class Camera
{ {
Q_OBJECT
public: public:
Camera(); Camera();
void suscribe(); ///< Call this once before trying to get frames void suscribe(); ///< Call this once before trying to get frames
@ -43,24 +36,10 @@ public:
cv::Mat getLastFrame(); ///< Get the last captured frame cv::Mat getLastFrame(); ///< Get the last captured frame
QImage getLastImage(); ///< Convert the last frame to a QImage (can be expensive !) QImage getLastImage(); ///< Convert the last frame to a QImage (can be expensive !)
vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !) vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !)
bool isFormatSupported(const QVideoSurfaceFormat & format) const;
private slots:
void onCameraError(QCamera::Error value);
private:
bool start(const QVideoSurfaceFormat &format);
bool present(const QVideoFrame &frame);
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const;
private: private:
int refcount; ///< Number of users suscribed to the camera int refcount; ///< Number of users suscribed to the camera
QCamera *camera; cv::VideoCapture cam; ///< OpenCV camera capture opbject
QVideoFrame lastFrame;
int frameFormat;
QList<QVideoFrame::PixelFormat> supportedFormats;
cv::VideoCapture cap;
}; };
#endif // CAMERA_H #endif // CAMERA_H