From c3f0e6aa4472f0754a7f392759b9f93cb3c7651b Mon Sep 17 00:00:00 2001
From: Jaime Passos <lazymyuutsu@gmail.com>
Date: Tue, 17 Dec 2019 15:37:43 -0300
Subject: [PATCH] Avoid recreating the color LUT mid-recording-frame

---
 src/m_anigif.c | 21 +++++++++++++--------
 src/v_video.c  |  6 +++---
 src/v_video.h  |  2 +-
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/m_anigif.c b/src/m_anigif.c
index 91f70dcfe0..f761db143b 100644
--- a/src/m_anigif.c
+++ b/src/m_anigif.c
@@ -33,6 +33,7 @@ consvar_t cv_gif_downscale =  {"gif_downscale", "On", CV_SAVE, CV_OnOff, NULL, 0
 #ifdef HAVE_ANIGIF
 static boolean gif_optimize = false; // So nobody can do something dumb
 static boolean gif_downscale = false; // like changing cvars mid output
+static RGBA_t *gif_palette = NULL;
 
 static FILE *gif_out = NULL;
 static INT32 gif_frames = 0;
@@ -432,13 +433,7 @@ static void GIF_headwrite(void)
 
 	// write color table
 	{
-		RGBA_t *pal = ((cv_screenshot_colorprofile.value
-#ifdef HWRENDER
-		&& (rendermode == render_soft)
-#endif
-		) ? pLocalPalette
-		: pMasterPalette);
-
+		RGBA_t *pal = gif_palette;
 		for (i = 0; i < 256; i++)
 		{
 			WRITEUINT8(p, pal[i].s.red);
@@ -473,7 +468,7 @@ static void hwrconvert(void)
 	INT32 x, y;
 	size_t i = 0;
 
-	InitColorLUT();
+	InitColorLUT(gif_palette);
 
 	for (y = 0; y < vid.height; y++)
 	{
@@ -643,6 +638,16 @@ INT32 GIF_open(const char *filename)
 
 	gif_optimize = (!!cv_gif_optimize.value);
 	gif_downscale = (!!cv_gif_downscale.value);
+
+	// GIF color table
+	// In hardware mode, uses the master palette
+	gif_palette = ((cv_screenshot_colorprofile.value
+#ifdef HWRENDER
+	&& (rendermode == render_soft)
+#endif
+	) ? pLocalPalette
+	: pMasterPalette);
+
 	GIF_headwrite();
 	gif_frames = 0;
 	return 1;
diff --git a/src/v_video.c b/src/v_video.c
index 1e901f2b3f..c91e5f213e 100644
--- a/src/v_video.c
+++ b/src/v_video.c
@@ -3251,19 +3251,19 @@ Unoptimized version
 
 UINT8 colorlookup[CLUTSIZE][CLUTSIZE][CLUTSIZE];
 
-void InitColorLUT(void)
+void InitColorLUT(RGBA_t *palette)
 {
 	UINT8 r, g, b;
 	static boolean clutinit = false;
 	static RGBA_t *lastpalette = NULL;
-	if ((!clutinit) || (lastpalette != pLocalPalette))
+	if ((!clutinit) || (lastpalette != palette))
 	{
 		for (r = 0; r < CLUTSIZE; r++)
 			for (g = 0; g < CLUTSIZE; g++)
 				for (b = 0; b < CLUTSIZE; b++)
 					colorlookup[r][g][b] = NearestColor(r << SHIFTCOLORBITS, g << SHIFTCOLORBITS, b << SHIFTCOLORBITS);
 		clutinit = true;
-		lastpalette = pLocalPalette;
+		lastpalette = palette;
 	}
 }
 
diff --git a/src/v_video.h b/src/v_video.h
index 0cd78d2e59..36d1914af6 100644
--- a/src/v_video.h
+++ b/src/v_video.h
@@ -47,7 +47,7 @@ void V_Init(void);
 
 extern UINT8 colorlookup[CLUTSIZE][CLUTSIZE][CLUTSIZE];
 
-void InitColorLUT(void);
+void InitColorLUT(RGBA_t *palette);
 
 // Set the current RGB palette lookup to use for palettized graphics
 void V_SetPalette(INT32 palettenum);
-- 
GitLab