Create new utility function for scaling with aspect ratio and use it for DXVA2

This commit is contained in:
Cameron Gutman 2018-08-04 22:22:15 -07:00
parent b076744f00
commit 5cbb38091b
4 changed files with 49 additions and 18 deletions

View file

@ -81,7 +81,8 @@ SOURCES += \
streaming/session.cpp \
streaming/audio.cpp \
gui/computermodel.cpp \
gui/appmodel.cpp
gui/appmodel.cpp \
streaming/streamutils.cpp
HEADERS += \
utils.h \
@ -95,7 +96,8 @@ HEADERS += \
streaming/session.hpp \
gui/computermodel.h \
gui/appmodel.h \
streaming/video/decoder.h
streaming/video/decoder.h \
streaming/streamutils.h
# Platform-specific renderers and decoders
ffmpeg {

View file

@ -0,0 +1,20 @@
#include "streamutils.h"
void StreamUtils::scaleSourceToDestinationSurface(SDL_Rect* src, SDL_Rect* dst)
{
int dstH = dst->w * src->h / src->w;
int dstW = dst->h * src->w / src->h;
if (dstH > dst->h) {
dst->y = 0;
dst->x = (dst->w - dstW) / 2;
dst->w = dstW;
SDL_assert(dst->w * src->h / src->w <= dst->h);
}
else {
dst->x = 0;
dst->y = (dst->h - dstH) / 2;
dst->h = dstH;
SDL_assert(dst->h * src->w / src->h <= dst->w);
}
}

View file

@ -0,0 +1,10 @@
#pragma once
#include <SDL.h>
class StreamUtils
{
public:
static
void scaleSourceToDestinationSurface(SDL_Rect* src, SDL_Rect* dst);
};

View file

@ -1,6 +1,7 @@
#include <initguid.h>
#include "dxva2.h"
#include "../ffmpeg.h"
#include <streaming/streamutils.h>
#include <Limelight.h>
@ -593,22 +594,20 @@ void DXVA2Renderer::renderFrame(AVFrame* frame)
sample.PlanarAlpha = DXVA2_Fixed32OpaqueAlpha();
// Center in frame and preserve aspect ratio
double srcAspectRatio = (double)m_Desc.SampleWidth / (double)m_Desc.SampleHeight;
double dstAspectRatio = (double)m_DisplayWidth / (double)m_DisplayHeight;
if (dstAspectRatio < srcAspectRatio) {
// Greater height per width
int drawHeight = (int)(m_DisplayWidth / srcAspectRatio);
sample.DstRect.top = (m_DisplayHeight - drawHeight) / 2;
sample.DstRect.bottom = sample.DstRect.bottom + drawHeight;
sample.DstRect.right = m_DisplayWidth;
}
else {
// Greater width per height
int drawWidth = (int)(m_DisplayHeight * srcAspectRatio);
sample.DstRect.bottom = m_DisplayHeight;
sample.DstRect.left = (m_DisplayWidth - drawWidth) / 2;
sample.DstRect.right = sample.DstRect.left + drawWidth;
}
SDL_Rect src, dst;
src.x = src.y = 0;
src.w = m_Desc.SampleWidth;
src.h = m_Desc.SampleHeight;
dst.x = dst.y = 0;
dst.w = m_DisplayWidth;
dst.h = m_DisplayHeight;
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
sample.DstRect.left = dst.x;
sample.DstRect.right = dst.x + dst.w;
sample.DstRect.top = dst.y;
sample.DstRect.bottom = dst.y + dst.h;
DXVA2_VideoProcessBltParams bltParams = {};