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

refactoring

This commit is contained in:
krepa098 2014-10-23 22:31:39 +02:00
parent 20b7fb0c67
commit f05e2ba9c9
8 changed files with 153 additions and 96 deletions

View File

@ -152,7 +152,8 @@ HEADERS += src/widget/form/addfriendform.h \
src/misc/db/encrypteddb.h \
src/widget/form/inputpassworddialog.h \
src/widget/form/setpassworddialog.h \
src/widget/form/tabcompleter.h
src/widget/form/tabcompleter.h \
src/video/videoframe.h
SOURCES += \
src/widget/form/addfriendform.cpp \
@ -209,4 +210,5 @@ SOURCES += \
src/widget/form/inputpassworddialog.cpp \
src/widget/form/setpassworddialog.cpp \
src/video/netvideosource.cpp \
src/widget/form/tabcompleter.cpp
src/widget/form/tabcompleter.cpp \
src/video/videoframe.cpp

View File

@ -241,7 +241,7 @@ void Core::sendCallVideo(int callId)
if (!calls[callId].active || !calls[callId].videoEnabled)
return;
vpx_image frame = camera->getLastVPXImage();
vpx_image frame = camera->getLastFrame().createVpxImage();
if (frame.w && frame.h)
{
int result;

View File

@ -20,6 +20,13 @@
#include <QThread>
#include <QMutexLocker>
Camera* Camera::getInstance()
{
static Camera instance;
return &instance;
}
Camera::Camera()
: refcount(0)
, workerThread(nullptr)
@ -72,50 +79,6 @@ void Camera::probeResolutions()
worker->probeResolutions();
}
vpx_image Camera::getLastVPXImage()
{
QMutexLocker lock(&mutex);
vpx_image img;
if (currFrame.isNull())
{
img.w = 0;
img.h = 0;
return img;
}
const int w = currFrame.resolution.width();
const int h = currFrame.resolution.height();
// I420 "It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes."
// http://fourcc.org/yuv.php#IYUV
vpx_img_alloc(&img, VPX_IMG_FMT_VPXI420, w, h, 1);
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
uint8_t b = currFrame.frameData.data()[(x + y * w) * 3 + 0];
uint8_t g = currFrame.frameData.data()[(x + y * w) * 3 + 1];
uint8_t r = currFrame.frameData.data()[(x + y * w) * 3 + 2];
img.planes[VPX_PLANE_Y][x + y * img.stride[VPX_PLANE_Y]] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
if (!(x % 2) && !(y % 2))
{
const int i = x / 2;
const int j = y / 2;
img.planes[VPX_PLANE_U][i + j * img.stride[VPX_PLANE_U]] = ((112 * r + -94 * g + -18 * b) >> 8) + 128;
img.planes[VPX_PLANE_V][i + j * img.stride[VPX_PLANE_V]] = ((-38 * r + -74 * g + 112 * b) >> 8) + 128;
}
}
}
return img;
}
void Camera::setResolution(QSize res)
{
worker->setProp(CV_CAP_PROP_FRAME_WIDTH, res.width());
@ -146,9 +109,8 @@ void Camera::onNewFrameAvailable(const VideoFrame frame)
mutex.unlock();
}
Camera* Camera::getInstance()
VideoFrame Camera::getLastFrame()
{
static Camera instance;
return &instance;
QMutexLocker lock(&mutex);
return currFrame;
}

View File

@ -48,7 +48,7 @@ public:
~Camera();
static Camera* getInstance(); ///< Returns the global widget's Camera instance
vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !)
VideoFrame getLastFrame();
void setResolution(QSize res);
QSize getCurrentResolution();

59
src/video/videoframe.cpp Normal file
View File

@ -0,0 +1,59 @@
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#include "videoframe.h"
vpx_image_t VideoFrame::createVpxImage()
{
vpx_image img;
if (isValid())
{
img.w = 0;
img.h = 0;
return img;
}
const int w = resolution.width();
const int h = resolution.height();
// I420 "It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes."
// http://fourcc.org/yuv.php#IYUV
vpx_img_alloc(&img, VPX_IMG_FMT_VPXI420, w, h, 1);
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
uint8_t b = frameData.data()[(x + y * w) * 3 + 0];
uint8_t g = frameData.data()[(x + y * w) * 3 + 1];
uint8_t r = frameData.data()[(x + y * w) * 3 + 2];
img.planes[VPX_PLANE_Y][x + y * img.stride[VPX_PLANE_Y]] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
if (!(x % 2) && !(y % 2))
{
const int i = x / 2;
const int j = y / 2;
img.planes[VPX_PLANE_U][i + j * img.stride[VPX_PLANE_U]] = ((112 * r + -94 * g + -18 * b) >> 8) + 128;
img.planes[VPX_PLANE_V][i + j * img.stride[VPX_PLANE_V]] = ((-38 * r + -74 * g + 112 * b) >> 8) + 128;
}
}
}
return img;
}

58
src/video/videoframe.h Normal file
View File

@ -0,0 +1,58 @@
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#ifndef VIDEOFRAME_H
#define VIDEOFRAME_H
#include <QMetaType>
#include <QByteArray>
#include <QSize>
#include "vpx/vpx_image.h"
struct VideoFrame
{
enum ColorFormat
{
NONE,
BGR,
YUV,
};
QByteArray frameData;
QSize resolution;
ColorFormat format;
VideoFrame() : format(NONE) {}
VideoFrame(QByteArray d, QSize r, ColorFormat f) : frameData(d), resolution(r), format(f) {}
void invalidate()
{
frameData = QByteArray();
resolution = QSize(-1,-1);
}
bool isValid()
{
return !frameData.isEmpty() && resolution.isValid();
}
vpx_image_t createVpxImage();
};
Q_DECLARE_METATYPE(VideoFrame)
#endif // VIDEOFRAME_H

View File

@ -1,3 +1,19 @@
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#ifndef VIDEOSOURCE_H
#define VIDEOSOURCE_H
@ -5,44 +21,7 @@
#include <QSize>
#include <QRgb>
struct VideoFrame
{
enum ColorFormat
{
NONE,
BGR,
YUV,
};
QByteArray frameData;
QSize resolution;
ColorFormat format;
VideoFrame() : format(NONE) {}
VideoFrame(QByteArray d, QSize r, ColorFormat f) : frameData(d), resolution(r), format(f) {}
void setNull()
{
frameData = QByteArray();
}
bool isNull()
{
return frameData.isEmpty();
}
// assumes format is BGR
QRgb getPixel(int x, int y)
{
char b = frameData.data()[resolution.width() * 3 * y + x * 3 + 0];
char g = frameData.data()[resolution.width() * 3 * y + x * 3 + 1];
char r = frameData.data()[resolution.width() * 3 * y + x * 3 + 2];
return qRgb(r, g, b);
}
};
Q_DECLARE_METATYPE(VideoFrame)
#include "videoframe.h"
class VideoSource : public QObject
{

View File

@ -126,9 +126,10 @@ void VideoSurface::paintGL()
{
mutex.lock();
VideoFrame currFrame = frame;
frame.invalidate();
mutex.unlock();
if (res != currFrame.resolution && currFrame.resolution.isValid())
if (currFrame.isValid() && res != currFrame.resolution)
{
res = currFrame.resolution;
@ -143,7 +144,7 @@ void VideoSurface::paintGL()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
if (!currFrame.isNull())
if (currFrame.isValid())
{
pboIndex = (pboIndex + 1) % 2;
int nextPboIndex = (pboIndex + 1) % 2;
@ -177,10 +178,6 @@ void VideoSurface::paintGL()
memcpy(ptr, currFrame.frameData.data(), currFrame.frameData.size());
pbo[nextPboIndex]->unmap();
pbo[nextPboIndex]->release();
mutex.lock();
frame.setNull();
mutex.unlock();
}
// render pbo