diff --git a/components/codecs/include/ftl/codecs/bitrates.hpp b/components/codecs/include/ftl/codecs/bitrates.hpp index f7a1da5c0635f6423d2cf7abd94c22f20bc0209b..fd50b9c86fb8b4828ee70e1c0f0f766d9112b121 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 035e850d1ff20f5e113db2f7004b85c8bbea3040..45a5057687f8add5cbfdaf02718e880a3361bd40 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 0a56be09be349c57584e0fee83196e902074fe12..6e5a3273da6c4ef7f07197c1e3cbf3f4624642a2 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 ¶ms = src->parameters(); + s->hq_bitrate = ftl::codecs::findPreset(params.width, params.height); + } + //LOG(INFO) << "RAW CALLBACK"; _transmitPacket(s, spkt, pkt, Quality::Any); });