Skip to content

Commit 0f49dca

Browse files
committed
Use GL_NEAREST when possible without degrading quality
1 parent 30274f3 commit 0f49dca

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

app/streaming/video/ffmpeg-renderers/eglvid.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,6 @@ bool EGLRenderer::setupVideoRenderingState() {
627627
glGenTextures(EGL_MAX_PLANES, m_Textures);
628628
for (size_t i = 0; i < EGL_MAX_PLANES; ++i) {
629629
glBindTexture(GL_TEXTURE_EXTERNAL_OES, m_Textures[i]);
630-
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
631-
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
632630
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
633631
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
634632
}
@@ -682,8 +680,8 @@ bool EGLRenderer::setupOverlayRenderingState() {
682680
for (size_t i = 0; i < Overlay::OverlayMax; ++i) {
683681
// Set up the overlay texture
684682
glBindTexture(GL_TEXTURE_2D, m_OverlayTextures[i]);
685-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
686-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
683+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
684+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
687685
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
688686
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
689687

@@ -786,13 +784,33 @@ void EGLRenderer::renderFrame(AVFrame* frame)
786784
}
787785
}
788786

787+
int drawableWidth, drawableHeight;
788+
SDL_GL_GetDrawableSize(m_Window, &drawableWidth, &drawableHeight);
789+
SDL_Rect src, dst;
790+
src.x = src.y = dst.x = dst.y = 0;
791+
src.w = frame->width;
792+
src.h = frame->height;
793+
dst.w = drawableWidth;
794+
dst.h = drawableHeight;
795+
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
796+
789797
ssize_t plane_count = m_Backend->exportEGLImages(frame, m_EGLDisplay, imgs);
790798
if (plane_count < 0)
791799
return;
792800
for (ssize_t i = 0; i < plane_count; ++i) {
793801
glActiveTexture(GL_TEXTURE0 + i);
794802
glBindTexture(GL_TEXTURE_EXTERNAL_OES, m_Textures[i]);
795803
m_glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, imgs[i]);
804+
805+
// Use GL_NEAREST to reduce sampling if the video region is a multiple of the frame size
806+
if (dst.w % frame->width == 0 && dst.h % frame->height == 0) {
807+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
808+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
809+
}
810+
else {
811+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
812+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
813+
}
796814
}
797815

798816
// We already called glClear() after last frame's SDL_GL_SwapWindow()
@@ -801,17 +819,7 @@ void EGLRenderer::renderFrame(AVFrame* frame)
801819
glClear(GL_COLOR_BUFFER_BIT);
802820
}
803821

804-
int drawableWidth, drawableHeight;
805-
SDL_GL_GetDrawableSize(m_Window, &drawableWidth, &drawableHeight);
806-
807822
// Set the viewport to the size of the aspect-ratio-scaled video
808-
SDL_Rect src, dst;
809-
src.x = src.y = dst.x = dst.y = 0;
810-
src.w = frame->width;
811-
src.h = frame->height;
812-
dst.w = drawableWidth;
813-
dst.h = drawableHeight;
814-
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
815823
glViewport(dst.x, dst.y, dst.w, dst.h);
816824

817825
glUseProgram(m_ShaderProgram);

app/streaming/video/ffmpeg-renderers/sdlvid.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ void SdlRenderer::renderFrame(AVFrame* frame)
417417
goto Exit;
418418
}
419419

420+
// Never alpha blend this texture when rendering
421+
SDL_SetTextureBlendMode(m_Texture, SDL_BLENDMODE_NONE);
422+
420423
#ifdef HAVE_CUDA
421424
if (frame->format == AV_PIX_FMT_CUDA) {
422425
SDL_assert(m_CudaGLHelper == nullptr);
@@ -567,6 +570,11 @@ void SdlRenderer::renderFrame(AVFrame* frame)
567570
// Ensure the viewport is set to the desired video region
568571
SDL_RenderSetViewport(m_Renderer, &dst);
569572

573+
// Use nearest pixel sampling if the video region size is a multiple of the frame size
574+
SDL_SetTextureScaleMode(m_Texture,
575+
dst.w % frame->width == 0 && dst.h % frame->height == 0 ?
576+
SDL_ScaleModeNearest : SDL_ScaleModeLinear);
577+
570578
// Draw the video content itself
571579
SDL_RenderCopy(m_Renderer, m_Texture, nullptr, nullptr);
572580

0 commit comments

Comments
 (0)