From ec051a81632d4c1e5ca4ff6bcadd04a75ce163c9 Mon Sep 17 00:00:00 2001
From: Andrew Randrianasulu <randrianasulu@gmail.com>
Date: Mon, 12 Jul 2021 23:14:53 +0300
Subject: [PATCH 3/3] Attempt at differenciating 601 pal and ntsc

---
 cinelerra-5.1/cinelerra/appearanceprefs.C     |  3 ++-
 cinelerra-5.1/cinelerra/appearanceprefs.h     |  2 +-
 cinelerra-5.1/cinelerra/ffmpeg.C              | 15 ++++++++++-----
 cinelerra-5.1/cinelerra/preferences.C         |  2 +-
 cinelerra-5.1/guicast/bccolors.C              |  3 ++-
 cinelerra-5.1/guicast/bccolors.h              |  7 +++++--
 cinelerra-5.1/guicast/bccolors.inc            |  3 ++-
 cinelerra-5.1/plugins/colorspace/colorspace.C | 12 +++++++-----
 8 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/cinelerra-5.1/cinelerra/appearanceprefs.C b/cinelerra-5.1/cinelerra/appearanceprefs.C
index 356fe206..76f87ff0 100644
--- a/cinelerra-5.1/cinelerra/appearanceprefs.C
+++ b/cinelerra-5.1/cinelerra/appearanceprefs.C
@@ -887,7 +887,8 @@ int HighlightInverseColor::handle_event()
 
 
 const char *YuvColorSpace::color_space[] = {
-	N_("BT601"), // COLOR_SPACE_BT601
+	N_("BT601_PAL"), // COLOR_SPACE_BT601_PAL
+	N_("BT601_NTSC"), // COLOR SPACE BT601_NTSC
 	N_("BT709"), // COLOR_SPACE_BT709
 	N_("BT2020"), // COLOR_SPACE_BT2020
 };
diff --git a/cinelerra-5.1/cinelerra/appearanceprefs.h b/cinelerra-5.1/cinelerra/appearanceprefs.h
index fc40b04b..ba6763e9 100644
--- a/cinelerra-5.1/cinelerra/appearanceprefs.h
+++ b/cinelerra-5.1/cinelerra/appearanceprefs.h
@@ -377,7 +377,7 @@ public:
 class YuvColorSpace : public BC_PopupMenu
 {
 public:
-	static const char *color_space[3];
+	static const char *color_space[4];
 	YuvColorSpace(int x, int y, PreferencesWindow *pwindow);
 	~YuvColorSpace();
 
diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C
index b863b8b3..952c3ca4 100644
--- a/cinelerra-5.1/cinelerra/ffmpeg.C
+++ b/cinelerra-5.1/cinelerra/ffmpeg.C
@@ -1593,7 +1593,8 @@ int FFVideoConvert::convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *
 	}
 	int color_space = SWS_CS_ITU601;
 	switch( preferences->yuv_color_space ) {
-	case BC_COLORS_BT601:  color_space = SWS_CS_ITU601;  break;
+	case BC_COLORS_BT601_PAL:  color_space = SWS_CS_ITU601;  break;
+	case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break;
 	case BC_COLORS_BT709:  color_space = SWS_CS_ITU709;  break;
 	case BC_COLORS_BT2020: color_space = SWS_CS_BT2020;  break;
 	}
@@ -1722,7 +1723,8 @@ int FFVideoConvert::convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *
 	}
 	int color_space = SWS_CS_ITU601;
 	switch( preferences->yuv_color_space ) {
-	case BC_COLORS_BT601:  color_space = SWS_CS_ITU601;  break;
+	case BC_COLORS_BT601_PAL:  color_space = SWS_CS_ITU601;  break;
+	case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break;
 	case BC_COLORS_BT709:  color_space = SWS_CS_ITU709;  break;
 	case BC_COLORS_BT2020: color_space = SWS_CS_BT2020;  break;
 	}
@@ -2642,8 +2644,10 @@ int FFMPEG::open_decoder()
 			}
 			switch( avpar->color_space ) {
 			case AVCOL_SPC_BT470BG:
+				vid->color_space = BC_COLORS_BT601_PAL;
+				break;
 			case AVCOL_SPC_SMPTE170M:
-				vid->color_space = BC_COLORS_BT601;
+				vid->color_space = BC_COLORS_BT601_NTSC;
 				break;
 			case AVCOL_SPC_BT709:
 				vid->color_space = BC_COLORS_BT709;
@@ -2653,7 +2657,7 @@ int FFMPEG::open_decoder()
 				vid->color_space = BC_COLORS_BT2020;
 				break;
 			default:
-				vid->color_space = !file_base ? BC_COLORS_BT601 :
+				vid->color_space = !file_base ? BC_COLORS_BT601_NTSC :
 					file_base->file->preferences->yuv_color_space;
 				break;
 			}
@@ -2904,7 +2908,8 @@ int FFMPEG::open_encoder(const char *type, const char *spec)
 			if( (vid->color_space = asset->ff_color_space) < 0 )
 				vid->color_space = file_base->file->preferences->yuv_color_space;
 			switch( vid->color_space ) {
-			case BC_COLORS_BT601:  ctx->colorspace = AVCOL_SPC_SMPTE170M;  break;
+			case BC_COLORS_BT601_NTSC:  ctx->colorspace = AVCOL_SPC_SMPTE170M;  break;
+			case BC_COLORS_BT601_PAL: ctx->colorspace = AVCOL_SPC_BT470BG; break;
 			case BC_COLORS_BT709:  ctx->colorspace = AVCOL_SPC_BT709;      break;
 			case BC_COLORS_BT2020: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break;
 			}
diff --git a/cinelerra-5.1/cinelerra/preferences.C b/cinelerra-5.1/cinelerra/preferences.C
index c067e659..4bc65a59 100644
--- a/cinelerra-5.1/cinelerra/preferences.C
+++ b/cinelerra-5.1/cinelerra/preferences.C
@@ -104,7 +104,7 @@ Preferences::Preferences()
 	forward_render_displacement = 0;
 	dvd_yuv420p_interlace = 0;
 	highlight_inverse = 0xffffff;
-	yuv_color_space = BC_COLORS_BT601;
+	yuv_color_space = BC_COLORS_BT601_NTSC;
 	yuv_color_range = BC_COLORS_JPEG;
 	autocolor_assets = 0;
 	ctrl_toggle = 1;
diff --git a/cinelerra-5.1/guicast/bccolors.C b/cinelerra-5.1/guicast/bccolors.C
index e587321d..94dfd20b 100644
--- a/cinelerra-5.1/guicast/bccolors.C
+++ b/cinelerra-5.1/guicast/bccolors.C
@@ -143,7 +143,8 @@ void YUV::yuv_set_colors(int color_space, int color_range)
 	int mpeg;
 	switch( color_space ) {
 	default:
-	case BC_COLORS_BT601:  kr = BT601_Kr;   kb = BT601_Kb;   break;
+	case BC_COLORS_BT601_NTSC:  kr = BT601_NTSC_Kr;   kb = BT601_NTSC_Kb;   break;
+	case BC_COLORS_BT601_PAL: kr = BT601_PAL_Kr; kb = BT601_PAL_Kb; break;
 	case BC_COLORS_BT709:  kr = BT709_Kr;   kb = BT709_Kb;   break;
 	case BC_COLORS_BT2020: kr = BT2020_Kr;  kb = BT2020_Kb;  break;
 	}
diff --git a/cinelerra-5.1/guicast/bccolors.h b/cinelerra-5.1/guicast/bccolors.h
index 94be40bc..820b977d 100644
--- a/cinelerra-5.1/guicast/bccolors.h
+++ b/cinelerra-5.1/guicast/bccolors.h
@@ -104,8 +104,11 @@ inverse:
 // white vector normalized, so:
 //  Kg = 1 - Kr - Kb
 
-#define BT601_Kr 0.299
-#define BT601_Kb 0.114
+#define BT601_NTSC_Kr 0.299
+#define BT601_NTSC_Kb 0.114
+
+#define BT601_PAL_Kr 0.299
+#define BT601_PAL_Kb 0.114
 
 #define BT709_Kr 0.2126
 #define BT709_Kb 0.0722
diff --git a/cinelerra-5.1/guicast/bccolors.inc b/cinelerra-5.1/guicast/bccolors.inc
index 010faca5..58afbaee 100644
--- a/cinelerra-5.1/guicast/bccolors.inc
+++ b/cinelerra-5.1/guicast/bccolors.inc
@@ -25,7 +25,8 @@
 
 class YUV;
 
-#define BC_COLORS_BT601 0
+#define BC_COLORS_BT601_NTSC 0
+#define BC_COLORS_BT601_PAL 3
 #define BC_COLORS_BT709 1
 #define BC_COLORS_BT2020 2
 
diff --git a/cinelerra-5.1/plugins/colorspace/colorspace.C b/cinelerra-5.1/plugins/colorspace/colorspace.C
index 01814ec8..52126aa6 100644
--- a/cinelerra-5.1/plugins/colorspace/colorspace.C
+++ b/cinelerra-5.1/plugins/colorspace/colorspace.C
@@ -35,7 +35,7 @@ REGISTER_PLUGIN(ColorSpaceMain)
 ColorSpaceConfig::ColorSpaceConfig()
 {
 	inverse = 0;
-	inp_colorspace = BC_COLORS_BT601;
+	inp_colorspace = BC_COLORS_BT601_NTSC;
 	inp_colorrange = BC_COLORS_JPEG;
 	out_colorspace = BC_COLORS_BT709;
 	out_colorrange = BC_COLORS_JPEG;
@@ -307,18 +307,20 @@ void XTable::init(int len, int inv,
 	this->inp_space = inp_space;  this->out_space = out_space;
 	this->inp_range = inp_range;  this->out_range = out_range;
 
-	double iKr = BT601_Kr, iKb = BT601_Kb;
-	double oKr = BT601_Kr, oKb = BT601_Kb;
+	double iKr = BT601_NTSC_Kr, iKb = BT601_NTSC_Kb;
+	double oKr = BT601_NTSC_Kr, oKb = BT601_NTSC_Kb;
 	int impg = 0, ompg = 0;
 	switch( inp_space ) {
 	default:
-	case BC_COLORS_BT601:  iKr = BT601_Kr;   iKb = BT601_Kb;   break;
+	case BC_COLORS_BT601_NTSC:  iKr = BT601_NTSC_Kr;   iKb = BT601_NTSC_Kb;   break;
+	case BC_COLORS_BT601_PAL:  iKr = BT601_PAL_Kr;   iKb = BT601_PAL_Kb;   break;
 	case BC_COLORS_BT709:  iKr = BT709_Kr;   iKb = BT709_Kb;   break;
 	case BC_COLORS_BT2020: iKr = BT2020_Kr;  iKb = BT2020_Kb;  break;
 	}
 	switch( out_space ) {
 	default:
-	case BC_COLORS_BT601:  oKr = BT601_Kr;   oKb = BT601_Kb;   break;
+	case BC_COLORS_BT601_NTSC:  oKr = BT601_NTSC_Kr;   oKb = BT601_NTSC_Kb;   break;
+	case BC_COLORS_BT601_PAL:  oKr = BT601_PAL_Kr;   oKb = BT601_PAL_Kb;   break;
 	case BC_COLORS_BT709:  oKr = BT709_Kr;   oKb = BT709_Kb;   break;
 	case BC_COLORS_BT2020: oKr = BT2020_Kr;  oKb = BT2020_Kb;  break;
 	}
-- 
2.32.0

