SDL: Make sure fast path RGB <-> YUV conversions are using the same color primaries

From ee87132385014449c4cd33236c661d57539071c1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 5 Mar 2024 17:43:08 -0800
Subject: [PATCH] Make sure fast path RGB <-> YUV conversions are using the
 same color primaries

---
 src/video/SDL_yuv.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/video/SDL_yuv.c b/src/video/SDL_yuv.c
index 127bc016612a..93a97b5ed86f 100644
--- a/src/video/SDL_yuv.c
+++ b/src/video/SDL_yuv.c
@@ -593,26 +593,29 @@ int SDL_ConvertPixels_YUV_to_RGB(int width, int height,
     const Uint8 *v = NULL;
     Uint32 y_stride = 0;
     Uint32 uv_stride = 0;
-    YCbCrType yuv_type = YCBCR_601_LIMITED;
 
     if (GetYUVPlanes(width, height, src_format, src, src_pitch, &y, &u, &v, &y_stride, &uv_stride) < 0) {
         return -1;
     }
 
-    if (GetYUVConversionType(src_colorspace, &yuv_type) < 0) {
-        return -1;
-    }
+    if (SDL_COLORSPACEPRIMARIES(src_colorspace) == SDL_COLORSPACEPRIMARIES(dst_colorspace)) {
+        YCbCrType yuv_type = YCBCR_601_LIMITED;
 
-    if (yuv_rgb_sse(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
-        return 0;
-    }
+        if (GetYUVConversionType(src_colorspace, &yuv_type) < 0) {
+            return -1;
+        }
 
-    if (yuv_rgb_lsx(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
-        return 0;
-    }
+        if (yuv_rgb_sse(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
+            return 0;
+        }
 
-    if (yuv_rgb_std(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
-        return 0;
+        if (yuv_rgb_lsx(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
+            return 0;
+        }
+
+        if (yuv_rgb_std(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8 *)dst, dst_pitch, yuv_type)) {
+            return 0;
+        }
     }
 
     /* No fast path for the RGB format, instead convert using an intermediate buffer */
@@ -1146,12 +1149,14 @@ int SDL_ConvertPixels_RGB_to_YUV(int width, int height,
 #endif
 
     /* ARGB8888 to FOURCC */
-    if (src_format == SDL_PIXELFORMAT_ARGB8888) {
+    if (src_format == SDL_PIXELFORMAT_ARGB8888 &&
+        SDL_COLORSPACEPRIMARIES(src_colorspace) == SDL_COLORSPACEPRIMARIES(dst_colorspace)) {
         return SDL_ConvertPixels_ARGB8888_to_YUV(width, height, src, src_pitch, dst_format, dst, dst_pitch, yuv_type);
     }
 
     if (dst_format == SDL_PIXELFORMAT_P010) {
-        if (src_format == SDL_PIXELFORMAT_XBGR2101010) {
+        if (src_format == SDL_PIXELFORMAT_XBGR2101010 &&
+            SDL_COLORSPACEPRIMARIES(src_colorspace) == SDL_COLORSPACEPRIMARIES(dst_colorspace)) {
             return SDL_ConvertPixels_XBGR2101010_to_P010(width, height, src, src_pitch, dst_format, dst, dst_pitch, yuv_type);
         }