From c258908656720c46061fbd3185c34ee890a7174d Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Fri, 25 Oct 2019 13:17:45 +0300
Subject: [PATCH] Implements #210 Choose definition from source resolution

---
 .../codecs/include/ftl/codecs/bitrates.hpp    |  8 ++++-
 components/codecs/src/bitrates.cpp            | 33 +++++++++++++++++--
 components/rgbd-sources/src/streamer.cpp      |  6 ++++
 3 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/components/codecs/include/ftl/codecs/bitrates.hpp b/components/codecs/include/ftl/codecs/bitrates.hpp
index f7a1da5c0..fd50b9c86 100644
--- a/components/codecs/include/ftl/codecs/bitrates.hpp
+++ b/components/codecs/include/ftl/codecs/bitrates.hpp
@@ -40,9 +40,11 @@ enum struct definition_t : uint8_t {
 	LD360 = 6,
 	Any = 7,
 
-	HTC_VIVE = 8
+	HTC_VIVE = 8,
 
 	// TODO: Add audio definitions
+
+	Invalid
 };
 
 /**
@@ -87,6 +89,8 @@ static const preset_t kPresetLQThreshold = 4;
 
 static const preset_t kPresetHTCVive = -1;
 
+static const preset_t kPresetMinimum = -1;
+
 /**
  * Represents the details of each preset codec configuration.
  */
@@ -125,6 +129,8 @@ preset_t findPreset(definition_t, definition_t);
  */
 preset_t findPreset(definition_t);
 
+preset_t findPreset(size_t width, size_t height);
+
 }
 }
 
diff --git a/components/codecs/src/bitrates.cpp b/components/codecs/src/bitrates.cpp
index 035e850d1..45a505768 100644
--- a/components/codecs/src/bitrates.cpp
+++ b/components/codecs/src/bitrates.cpp
@@ -41,7 +41,8 @@ static const Resolution resolutions[] = {
 	854, 480,		// SD480
 	640, 360,		// LD360
 	0, 0,			// ANY
-	1852, 2056		// HTC_VIVE
+	1852, 2056,		// HTC_VIVE
+	0, 0
 };
 
 int ftl::codecs::getWidth(definition_t d) {
@@ -57,4 +58,32 @@ const CodecPreset &ftl::codecs::getPreset(preset_t p) {
     if (p > kPresetWorst) return presets[kPresetWorst];
     if (p < kPresetBest) return presets[kPresetBest];
     return presets[p];
-};
+}
+
+preset_t ftl::codecs::findPreset(size_t width, size_t height) {
+	int min_error = std::numeric_limits<int>::max();
+
+	// Find best definition
+	int best_def = (int)definition_t::Invalid;
+
+	for (int i=0; i<(int)definition_t::Invalid; ++i) {
+		int dw = resolutions[i].width - width;
+		int dh = resolutions[i].height - height;
+		int error = dw*dw + dh*dh;
+		if (error < min_error) {
+			min_error = error;
+			best_def = i;
+		}
+	}
+
+	// Find preset that matches this best definition
+	for (preset_t i=kPresetMinimum; i<=kPresetWorst; ++i) {
+		const auto &preset = getPreset(i);
+
+		if ((int)preset.colour_res == best_def && (int)preset.depth_res == best_def) {
+			return i;
+		}
+	}
+
+	return kPresetWorst;
+}
diff --git a/components/rgbd-sources/src/streamer.cpp b/components/rgbd-sources/src/streamer.cpp
index 0a56be09b..6e5a3273d 100644
--- a/components/rgbd-sources/src/streamer.cpp
+++ b/components/rgbd-sources/src/streamer.cpp
@@ -189,6 +189,12 @@ void Streamer::add(Source *src) {
 		group_.addSource(src);
 
 		src->addRawCallback([this,s](Source *src, const ftl::codecs::StreamPacket &spkt, const ftl::codecs::Packet &pkt) {
+			if (spkt.channel == Channel::Calibration) {
+				// Calibration changed, so lets re-check the bitrate presets
+				const auto &params = src->parameters();
+				s->hq_bitrate = ftl::codecs::findPreset(params.width, params.height);
+			}
+
 			//LOG(INFO) << "RAW CALLBACK";
 			_transmitPacket(s, spkt, pkt, Quality::Any);
 		});
-- 
GitLab