From 940840c9047c11a4df2c2934295e7b8a8ab87933 Mon Sep 17 00:00:00 2001 From: Brenno Date: Thu, 12 Nov 2020 21:33:53 -0300 Subject: [PATCH] Solved FPS bug. --- src/KeyFrameBBox.cpp | 31 ++++++++++++++++--------------- src/effects/Tracker.cpp | 16 ++++++++++++---- src/effects/Tracker.h | 1 + 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/KeyFrameBBox.cpp b/src/KeyFrameBBox.cpp index 98307a89..ddb5c3fc 100644 --- a/src/KeyFrameBBox.cpp +++ b/src/KeyFrameBBox.cpp @@ -279,19 +279,19 @@ void KeyFrameBBox::RemoveScale(int64_t frame_number) { BBox KeyFrameBBox::GetValue(int64_t frame_number){ double time = this->FrameNToTime(frame_number, this->TimeScale); - auto it = BoxVec.lower_bound(time); + if (it == BoxVec.end()){ BBox resp; return resp; } - if (it->first == time){ + if ((it->first == time) || (it == BoxVec.begin())){ BBox res = it->second; - res.cx += this->delta_x.GetValue(time); - res.cy += this->delta_y.GetValue(time); - res.height += this->scale_y.GetValue(time); - res.width += this->scale_x.GetValue(time); + res.cx += this->delta_x.GetValue(it->first); + res.cy += this->delta_y.GetValue(it->first); + res.height += this->scale_y.GetValue(it->first); + res.width += this->scale_x.GetValue(it->first); return res; } @@ -324,7 +324,7 @@ BBox KeyFrameBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, Point p2_right(t2, right.cy, openshot::InterpolationType::LINEAR); Point p2 = InterpolateBetween(p2_left, p2_right, target, 0.01); - + Point p3_left(t1, left.height, openshot::InterpolationType::LINEAR); Point p3_right(t2, right.height, openshot::InterpolationType::LINEAR); @@ -334,8 +334,8 @@ BBox KeyFrameBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, Point p4_right(t2, right.width, openshot::InterpolationType::LINEAR); Point p4 = InterpolateBetween(p4_left, p4_right, target, 0.01); - - BBox ans(p1.co.Y, p2.co.Y, p3.co.Y, p4.co.Y); + + BBox ans(p1.co.Y, p2.co.Y, p4.co.Y, p3.co.Y); return ans; } @@ -351,7 +351,7 @@ Fraction KeyFrameBBox::GetBaseFPS(){ } double KeyFrameBBox::FrameNToTime(int64_t frame_number, double time_scale){ - double time = ((double) frame_number) * this->BaseFps.Reciprocal().ToDouble() * time_scale; + double time = ((double) frame_number) * this->BaseFps.Reciprocal().ToDouble() * (1.0 / time_scale); return time; } @@ -381,13 +381,13 @@ Json::Value KeyFrameBBox::JsonValue() { root["boxes"] = Json::Value(Json::arrayValue); // loop through points - /*for (auto const& x : BoxVec){ + for (auto const& x : BoxVec){ Json::Value elem; elem["key"] = x.first; elem["val"] = x.second.JsonValue(); root["boxes"].append(elem); } - */ + root["delta_x"] = delta_x.JsonValue(); root["delta_y"] = delta_y.JsonValue(); @@ -443,9 +443,10 @@ void KeyFrameBBox::SetJsonValue(const Json::Value root) { BaseFps.den = (int) root["BaseFPS"]["den"].asInt(); } if (!root["TimeScale"].isNull()) { - this->TimeScale = (double) root["TimeScale"].asDouble(); + double scale = (double) root["TimeScale"].asDouble(); + this->ScalePoints(scale); } - /* + if (!root["boxes"].isNull()){ // loop through points for (const auto existing_point : root["boxes"]) { @@ -456,6 +457,6 @@ void KeyFrameBBox::SetJsonValue(const Json::Value root) { BoxVec.insert({existing_point["key"].asDouble(), box}); } } - */ + return; } \ No newline at end of file diff --git a/src/effects/Tracker.cpp b/src/effects/Tracker.cpp index 423328b3..42ed3ea2 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -69,6 +69,7 @@ void Tracker::init_effect_details() info.has_audio = false; info.has_video = true; + this->TimeScale = 1.0; } // This method is required for all derived classes of EffectBase, and returns a @@ -144,8 +145,10 @@ bool Tracker::LoadTrackedData(std::string inputFilePath){ // Assign data to tracker map //trackedDataById[id] = EffectFrameData(id, rotation, x1, y1, x2, y2); - trackedData.AddBox(id, x1, y1, (x2-x1), (y2-y1)); - trackedData.AddRotation(id, rotation); + if ((x1 >= 0.0) && (y1 >= 0.0) && (x2 >= 0.0) && (y2 >= 0.0)){ + trackedData.AddBox(id, x1, y1, (x2-x1), (y2-y1)); + trackedData.AddRotation(id, rotation); + } } // Show the time stamp from the last update in tracker data file @@ -180,7 +183,7 @@ Json::Value Tracker::JsonValue() const { root["protobuf_data_path"] = protobuf_data_path; root["BaseFPS"]["num"] = BaseFPS.num; root["BaseFPS"]["den"] = BaseFPS.den; - + root["TimeScale"] = this->TimeScale; // return JsonValue return root; } @@ -215,8 +218,13 @@ void Tracker::SetJsonValue(const Json::Value root) { BaseFPS.den = (int) root["BaseFPS"]["den"].asInt(); } + if (!root["TimeScale"].isNull()){ + TimeScale = (double) root["TimeScale"].asDouble(); + } + trackedData.SetBaseFPS(this->BaseFPS); - + trackedData.ScalePoints(TimeScale); + // Set data from Json (if key is found) if (!root["protobuf_data_path"].isNull()){ protobuf_data_path = (root["protobuf_data_path"].asString()); diff --git a/src/effects/Tracker.h b/src/effects/Tracker.h index 3350428a..391e8934 100644 --- a/src/effects/Tracker.h +++ b/src/effects/Tracker.h @@ -92,6 +92,7 @@ namespace openshot void init_effect_details(); std::string protobuf_data_path; Fraction BaseFPS; + double TimeScale; public: std::map trackedDataById; // Save object tracking box data