Skip to content

Commit

Permalink
fix(videoframe, corevideosource): Added buffer alignment between lines
Browse files Browse the repository at this point in the history
Fixed warning from qTox#3527.
  • Loading branch information
Diadlo committed Jul 21, 2016
1 parent f970bb5 commit 57344b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/video/corevideosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ void CoreVideoSource::pushFrame(const vpx_image_t* vpxframe)
AVFrame* avframe = av_frame_alloc();
if (!avframe)
return;

avframe->width = width;
avframe->height = height;
avframe->format = AV_PIX_FMT_YUV420P;

int imgBufferSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1);
int imgBufferSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 16);
uint8_t* buf = (uint8_t*)av_malloc(imgBufferSize);
if (!buf)
{
Expand All @@ -62,7 +63,7 @@ void CoreVideoSource::pushFrame(const vpx_image_t* vpxframe)

uint8_t** data = avframe->data;
int* linesize = avframe->linesize;
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_YUV420P, width, height, 1);
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_YUV420P, width, height, 16);

for (int i = 0; i < 3; i++)
{
Expand Down
29 changes: 17 additions & 12 deletions src/video/videoframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ QImage VideoFrame::toQImage(QSize size)

QMutexLocker locker(&biglock);

return QImage(*frameRGB24->data, frameRGB24->width, frameRGB24->height, *frameRGB24->linesize, QImage::Format_RGB888);
return QImage(*frameRGB24->data,
frameRGB24->width, frameRGB24->height,
*frameRGB24->linesize, QImage::Format_RGB888);
}

vpx_image *VideoFrame::toVpxImage()
{
vpx_image* img = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, width, height, 0);
vpx_image* img = vpx_img_alloc(nullptr, VPX_IMG_FMT_I420, width, height, 16);

if (!convertToYUV420())
return img;
Expand All @@ -114,6 +116,8 @@ vpx_image *VideoFrame::toVpxImage()

bool VideoFrame::convertToRGB24(QSize size)
{
size.setHeight(size.height() / 16 * 16);
size.setWidth(size.width() / 16 * 16);
QMutexLocker locker(&biglock);

AVFrame* sourceFrame;
Expand All @@ -130,7 +134,6 @@ bool VideoFrame::convertToRGB24(QSize size)
qWarning() << "None of the frames are valid! Did someone release us?";
return false;
}
//std::cout << "converting to RGB24" << std::endl;

if (size.isEmpty())
{
Expand All @@ -148,35 +151,38 @@ bool VideoFrame::convertToRGB24(QSize size)
av_frame_free(&frameRGB24);
}

frameRGB24=av_frame_alloc();
frameRGB24 = av_frame_alloc();
if (!frameRGB24)
{
qCritical() << "av_frame_alloc failed";
return false;
}

int imgBufferSize = av_image_get_buffer_size(AV_PIX_FMT_RGB24, size.width(), size.height(), 1);
// 24 bit at all, 8 bit per color
int imgBufferSize = size.width() * size.height() * (24/8);
uint8_t* buf = (uint8_t*)av_malloc(imgBufferSize);
if (!buf)
{
qCritical() << "av_malloc failed";
av_frame_free(&frameRGB24);
return false;
}
frameRGB24->opaque = buf;


frameRGB24->opaque = buf;
uint8_t** data = frameRGB24->data;
int* linesize = frameRGB24->linesize;
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_RGB24, size.width(), size.height(), 1);
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_RGB24, size.width(), size.height(), 16);
frameRGB24->width = size.width();
frameRGB24->height = size.height();

// Bilinear is better for shrinking, bicubic better for upscaling
int resizeAlgo = size.width()<=width ? SWS_BILINEAR : SWS_BICUBIC;
int resizeAlgo = size.width() <= width ? SWS_BILINEAR : SWS_BICUBIC;

SwsContext *swsCtx = sws_getContext(width, height, (AVPixelFormat)pixFmt,
size.width(), size.height(), AV_PIX_FMT_RGB24,
resizeAlgo, nullptr, nullptr, nullptr);

sws_scale(swsCtx, (uint8_t const * const *)sourceFrame->data,
sourceFrame->linesize, 0, height,
frameRGB24->data, frameRGB24->linesize);
Expand Down Expand Up @@ -206,16 +212,15 @@ bool VideoFrame::convertToYUV420()
qCritical() << "None of the frames are valid! Did someone release us?";
return false;
}
//std::cout << "converting to YUV420" << std::endl;

frameYUV420=av_frame_alloc();
frameYUV420 = av_frame_alloc();
if (!frameYUV420)
{
qCritical() << "av_frame_alloc failed";
return false;
}

int imgBufferSize = av_image_get_buffer_size(AV_PIX_FMT_RGB24, width, height, 1);
int imgBufferSize = av_image_get_buffer_size(AV_PIX_FMT_RGB24, width, height, 16);
uint8_t* buf = (uint8_t*)av_malloc(imgBufferSize);
if (!buf)
{
Expand All @@ -227,7 +232,7 @@ bool VideoFrame::convertToYUV420()

uint8_t** data = frameYUV420->data;
int* linesize = frameYUV420->linesize;
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_YUV420P, width, height, 1);
av_image_fill_arrays(data, linesize, buf, AV_PIX_FMT_YUV420P, width, height, 16);

SwsContext *swsCtx = sws_getContext(width, height, (AVPixelFormat)pixFmt,
width, height, AV_PIX_FMT_YUV420P,
Expand Down
9 changes: 7 additions & 2 deletions src/widget/form/settings/avform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ void AVForm::onVideoModesIndexChanged(int index)
auto onGrabbed = [screenshotGrabber, devName, this] (QRect region)
{
VideoMode mode(region);
mode.width = mode.width / 2 * 2;
mode.height = mode.height / 2 * 2;
mode.width = mode.width / 16 * 16;
mode.height = mode.height / 16 * 16;

Settings::getInstance().setScreenRegion(mode.toRect());
Settings::getInstance().setScreenGrabbed(true);
Expand Down Expand Up @@ -370,6 +370,7 @@ void AVForm::updateVideoModes(int curIndex)
if (isScreen)
{
QRect rect = Settings::getInstance().getScreenRegion();
qDebug() << "Load grabbed screen region: " << rect;
VideoMode mode(rect);

Settings::getInstance().setScreenGrabbed(true);
Expand Down Expand Up @@ -418,6 +419,7 @@ void AVForm::onVideoDevChanged(int index)
void AVForm::getVideoDevices()
{
QString settingsInDev = Settings::getInstance().getVideoDev();
qDebug() << settingsInDev;
int videoDevIndex = 0;
videoDeviceList = CameraDevice::getDeviceList();
//prevent currentIndexChanged to be fired while adding items
Expand All @@ -427,7 +429,10 @@ void AVForm::getVideoDevices()
{
bodyUI->videoDevCombobox->addItem(device.second);
if (device.first == settingsInDev)
{
qDebug() << "Device loaded";
videoDevIndex = bodyUI->videoDevCombobox->count()-1;
}
}
bodyUI->videoDevCombobox->setCurrentIndex(videoDevIndex);
bodyUI->videoDevCombobox->blockSignals(false);
Expand Down

0 comments on commit 57344b7

Please sign in to comment.