From db55edb196134ab39f14c76edfd35225055f1227 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 30 Mar 2024 15:18:44 +0100 Subject: [PATCH] Fix YUV conversion for full color range Take the color range (full vs limited) into account to render the picture. Note that with the current version of SDL, it has no impact with the SDL opengl render driver. Fixes #4756 Refs Refs libusb/#9311 Suggested-by: Simon Chan <1330321+yume-chan@users.noreply.github.com> --- app/src/display.c | 18 ++++++++++++++++++ app/src/display.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/app/src/display.c b/app/src/display.c index c8df615d..25c23265 100644 --- a/app/src/display.c +++ b/app/src/display.c @@ -1,6 +1,7 @@ #include "display.h" #include +#include #include "util/log.h" @@ -65,6 +66,7 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) { display->texture = NULL; display->pending.flags = 0; display->pending.frame = NULL; + display->has_frame = false; return true; } @@ -196,9 +198,25 @@ sc_display_set_texture_size(struct sc_display *display, struct sc_size size) { return SC_DISPLAY_RESULT_OK; } +static SDL_YUV_CONVERSION_MODE +sc_display_to_sdl_color_range(enum AVColorRange color_range) { + return color_range == AVCOL_RANGE_JPEG ? SDL_YUV_CONVERSION_JPEG + : SDL_YUV_CONVERSION_AUTOMATIC; +} + static bool sc_display_update_texture_internal(struct sc_display *display, const AVFrame *frame) { + if (!display->has_frame) { + // First frame + display->has_frame = true; + + // Configure YUV color range conversion + SDL_YUV_CONVERSION_MODE sdl_color_range = + sc_display_to_sdl_color_range(frame->color_range); + SDL_SetYUVConversionMode(sdl_color_range); + } + int ret = SDL_UpdateYUVTexture(display->texture, NULL, frame->data[0], frame->linesize[0], frame->data[1], frame->linesize[1], diff --git a/app/src/display.h b/app/src/display.h index 643ce73c..590715ee 100644 --- a/app/src/display.h +++ b/app/src/display.h @@ -33,6 +33,8 @@ struct sc_display { struct sc_size size; AVFrame *frame; } pending; + + bool has_frame; }; enum sc_display_result {