From cff3d3c4f1a6ece10486fb80ad2d35fe2ef311a6 Mon Sep 17 00:00:00 2001 From: Malin E Date: Wed, 12 Oct 2022 13:44:56 +0200 Subject: [PATCH] WIP find corresponding frameindex in video depending on in game time --- .../src/tileprovider/ffmpegtileprovider.cpp | 27 ++++++++++--------- .../src/tileprovider/ffmpegtileprovider.h | 6 +++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.cpp b/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.cpp index 81cd7bcb9c..cbade189e5 100644 --- a/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.cpp +++ b/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include namespace { @@ -193,16 +194,18 @@ void FfmpegTileProvider::update() { return; } // Check if it is time for a new frame - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - std::chrono::system_clock::duration diff = now - _lastFrameTime; - const double j2000Time = Time::now().j2000Seconds(); + const double now = global::timeManager->time().j2000Seconds(); + double videoTime = now - _startJ200Time; + int currentFrameIndex = std::floor(videoTime / _nSecPerFrame); - const bool hasNewFrame = (j2000Time > Time::convertTime(_startTime)) && - diff > _frameTime; + const bool hasNewFrame = + now > Time::convertTime(_startTime) && _prevFrameIndex != currentFrameIndex; if(!hasNewFrame) { return; } + + //LINFO(fmt::format("Frame index {} matches video duration {}", currentFrameIndex, videoTime)); _tileIsReady = false; // Read frame @@ -250,13 +253,12 @@ void FfmpegTileProvider::update() { } // Update times - if (_frameTime.count() <= 0) { + if (_nSecPerFrame < 0) { // Calculate frame time - double sPerFrame = av_q2d(_codecContext->time_base) * _codecContext->ticks_per_frame; - int msPerFrame = static_cast(sPerFrame * 1000); - _frameTime = std::chrono::milliseconds(msPerFrame); + _nSecPerFrame = av_q2d(_codecContext->time_base) * _codecContext->ticks_per_frame; } _lastFrameTime = now; + _prevFrameIndex = currentFrameIndex; // TODO: Need to check the format of the video and decide what formats we want to // support and how they relate to the GL formats @@ -328,6 +330,7 @@ float FfmpegTileProvider::noDataValueAsFloat() { } void FfmpegTileProvider::internalInitialize() { + _startJ200Time = Time::convertTime(_startTime); std::string path = absPath(_videoFile).string(); // Open video @@ -408,12 +411,10 @@ void FfmpegTileProvider::internalInitialize() { 1 ); - // Update times - _lastFrameTime = std::chrono::system_clock::now(); - _isInitialized = true; - // Create PBO for async texture upload glGenBuffers(1, &_pbo); + + _isInitialized = true; } FfmpegTileProvider::~FfmpegTileProvider() { diff --git a/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.h b/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.h index 87e8a6eadc..6626574f42 100644 --- a/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.h +++ b/modules/globebrowsing/src/tileprovider/ffmpegtileprovider.h @@ -65,9 +65,11 @@ public: private: std::filesystem::path _videoFile; - std::chrono::milliseconds _frameTime = std::chrono::milliseconds(0); - std::chrono::system_clock::time_point _lastFrameTime; + double _nSecPerFrame = -1.0; + double _lastFrameTime = std::numeric_limits::min(); std::string _startTime; + double _startJ200Time = std::numeric_limits::min(); + int _prevFrameIndex = -1; bool _tileIsReady = false; bool _isInitialized = false;