Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When flattening or trimming tracks, optionally honor linear timewarps. #1466

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/opentimelineio/stackAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ _flatten_next_item(
std::vector<Track*> const& tracks,
int track_index,
optional<TimeRange> trim_range,
ErrorStatus* error_status)
ErrorStatus* error_status,
TrimPolicy trim_policy)
{
if (track_index < 0)
{
Expand All @@ -34,7 +35,7 @@ _flatten_next_item(
SerializableObject::Retainer<Track> track_retainer;
if (trim_range)
{
track = track_trimmed_to_range(track, *trim_range, error_status);
track = track_trimmed_to_range(track, *trim_range, error_status, trim_policy);
if (track == nullptr || is_error(error_status))
{
return;
Expand Down Expand Up @@ -105,7 +106,8 @@ _flatten_next_item(
tracks,
track_index - 1,
trim,
error_status);
error_status,
trim_policy);
if (track == nullptr || is_error(error_status))
{
return;
Expand All @@ -124,7 +126,9 @@ _flatten_next_item(
}

Track*
flatten_stack(Stack* in_stack, ErrorStatus* error_status)
flatten_stack(Stack* in_stack,
ErrorStatus* error_status,
TrimPolicy trim_policy)
{
std::vector<Track*> tracks;
tracks.reserve(in_stack->children().size());
Expand Down Expand Up @@ -161,12 +165,15 @@ flatten_stack(Stack* in_stack, ErrorStatus* error_status)
tracks,
-1,
nullopt,
error_status);
error_status,
trim_policy);
return flat_track;
}

Track*
flatten_stack(std::vector<Track*> const& tracks, ErrorStatus* error_status)
flatten_stack(std::vector<Track*> const& tracks,
ErrorStatus* error_status,
TrimPolicy trim_policy)
{
Track* flat_track = new Track;
flat_track->set_name("Flattened");
Expand All @@ -178,7 +185,8 @@ flatten_stack(std::vector<Track*> const& tracks, ErrorStatus* error_status)
tracks,
-1,
nullopt,
error_status);
error_status,
trim_policy);
return flat_track;
}
}} // namespace opentimelineio::OPENTIMELINEIO_VERSION
8 changes: 6 additions & 2 deletions src/opentimelineio/stackAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {

Track* flatten_stack(Stack* in_stack, ErrorStatus* error_status = nullptr);
Track* flatten_stack(
Stack* in_stack,
ErrorStatus* error_status = nullptr,
TrimPolicy trim_policy = IgnoreTimeEffects);
Track* flatten_stack(
std::vector<Track*> const& tracks,
ErrorStatus* error_status = nullptr);
ErrorStatus* error_status = nullptr,
TrimPolicy trim_policy = IgnoreTimeEffects);

}} // namespace opentimelineio::OPENTIMELINEIO_VERSION
7 changes: 7 additions & 0 deletions src/opentimelineio/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {

enum TrimPolicy
{
IgnoreTimeEffects = 0,
HonorTimeEffectsExactly,
HonorTimeEffectsWithSnapping
};

class Clip;

class Track : public Composition
Expand Down
90 changes: 81 additions & 9 deletions src/opentimelineio/trackAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,61 @@

#include "opentimelineio/trackAlgorithm.h"
#include "opentimelineio/transition.h"
#include "opentimelineio/linearTimeWarp.h"

namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {

static double _compute_time_scalar(Item* item)
{
double time_scalar = 1.0;

for (const auto& effect : item->effects())
{
// Note: FreezeFrame is handled here because it is a subclass of
// LinearTimeWarp. However, generic TimeEffect is not, since non-linear
// warps cannot be decomposed into a simple scalar value. Non-linear
// warps are simply ignored here and left for future development.
if (const auto linear_time_warp =
dynamic_cast<LinearTimeWarp*>(effect.value)) {
time_scalar *= linear_time_warp->time_scalar();
}
}

return time_scalar;
}

static void _snap_timewarps(Item* item)
{
int item_frames = item->duration().to_frames();
auto range = item->trimmed_range();
// snap the item's start time to whole frames
item->set_source_range(
TimeRange(RationalTime(range.start_time().to_frames(),
range.start_time().rate()),
range.duration()));

for (const auto& effect : item->effects())
{
if (const auto linear_time_warp =
dynamic_cast<LinearTimeWarp*>(effect.value)) {
// TODO: What happens with FreezeFrame here?
// snap the time_scalar to be a rational number
double time_scalar = linear_time_warp->time_scalar();
int media_frames = rint(item_frames * time_scalar);
time_scalar = (double)media_frames / item_frames;
linear_time_warp->set_time_scalar(time_scalar);
}
}
}

// TODO: SnappedLinearTimeWarp???

Track*
track_trimmed_to_range(
Track* in_track,
TimeRange trim_range,
ErrorStatus* error_status)
ErrorStatus* error_status,
TrimPolicy trimPolicy)
{
Track* new_track = dynamic_cast<Track*>(in_track->clone(error_status));
if (is_error(error_status) || !new_track)
Expand Down Expand Up @@ -83,26 +130,51 @@ track_trimmed_to_range(
return nullptr;
}

float time_scalar = 1.0;
switch (trimPolicy) {
case HonorTimeEffectsExactly:
case HonorTimeEffectsWithSnapping:
time_scalar = _compute_time_scalar(child_item);
break;
case IgnoreTimeEffects:
default:
break;
}

// TODO: What if there are non-linear time warp here?

// Does the trim start after this child?
// If so, we need to remove the beginning of the child.
if (trim_range.start_time() > child_range.start_time())
{
auto trim_amount =
trim_range.start_time() - child_range.start_time();
child_source_range = TimeRange(
child_source_range.start_time() + trim_amount,
child_source_range.duration() - trim_amount);
trim_range.start_time() - child_range.start_time();
auto scaled_trim_amount =
RationalTime(trim_amount.value() * time_scalar,
trim_amount.rate());
child_source_range =
TimeRange(child_source_range.start_time() +
scaled_trim_amount,
child_source_range.duration() - trim_amount);
}

// Does the trim end before this child ends?
// If so, we need to remove the end of the child.
auto trim_end = trim_range.end_time_exclusive();
auto child_end = child_range.end_time_exclusive();
if (trim_end < child_end)
{
auto trim_amount = child_end - trim_end;
child_source_range = TimeRange(
child_source_range.start_time(),
child_source_range.duration() - trim_amount);
auto trim_amount = child_end - trim_end;
child_source_range =
TimeRange(
child_source_range.start_time(),
child_source_range.duration() - trim_amount);
}

child_item->set_source_range(child_source_range);
if (trimPolicy == HonorTimeEffectsWithSnapping) {
_snap_timewarps(child_item);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/opentimelineio/trackAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {
Track* track_trimmed_to_range(
Track* in_track,
TimeRange trim_range,
ErrorStatus* error_status = nullptr);
ErrorStatus* error_status = nullptr,
TrimPolicy trimPolicy = IgnoreTimeEffects);

}} // namespace opentimelineio::OPENTIMELINEIO_VERSION
136 changes: 136 additions & 0 deletions tests/test_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <opentimelineio/clip.h>
#include <opentimelineio/stack.h>
#include <opentimelineio/track.h>
#include <opentimelineio/trackAlgorithm.h>
#include <opentimelineio/linearTimeWarp.h>

#include <iostream>

Expand Down Expand Up @@ -107,7 +109,141 @@ main(int argc, char** argv)
assertEqual(result[0].value, cl0.value);
assertEqual(result[1].value, cl1.value);
});
tests.add_test(
"test_track_trimmed_to_range", [] {
using namespace otio;
SerializableObject::Retainer<Clip> cl0 =
new Clip();
cl0->set_source_range(TimeRange(RationalTime(0,24),
RationalTime(10,24)));
SerializableObject::Retainer<Clip> cl1 =
new Clip();
cl1->set_source_range(TimeRange(RationalTime(20,24),
RationalTime(10,24)));
SerializableObject::Retainer<Track> tr =
new Track();
tr->append_child(cl0);
tr->append_child(cl1);

opentimelineio::v1_0::ErrorStatus err;
auto trimmed_track =
track_trimmed_to_range(tr,
TimeRange(RationalTime(5,24),
RationalTime(7,24)),
&err);
assertFalse(is_error(err));

assertEqual(trimmed_track->children().size(), 2);
const auto cl0_trimmed = dynamic_cast<Clip*>(trimmed_track->children().at(0).value);
const auto cl1_trimmed = dynamic_cast<Clip*>(trimmed_track->children().at(1).value);
const auto cl0_trimmed_range = cl0_trimmed->trimmed_range();
const auto cl1_trimmed_range = cl1_trimmed->trimmed_range();
assertEqual(cl0_trimmed_range,
TimeRange(RationalTime(5,24),
RationalTime(5,24)));
assertEqual(cl1_trimmed_range,
TimeRange(RationalTime(20,24),
RationalTime(2,24)));
});
tests.add_test(
"test_track_trimmed_to_range_with_timewarp", [] {
using namespace otio;
SerializableObject::Retainer<Clip> cl0 =
new Clip();
cl0->set_source_range(TimeRange(RationalTime(0,24),
RationalTime(10,24)));
SerializableObject::Retainer<LinearTimeWarp> tw0 =
new LinearTimeWarp("tw0", "", 0.51);
cl0->effects().push_back(dynamic_retainer_cast<Effect>(tw0));
SerializableObject::Retainer<Clip> cl1 =
new Clip();
cl1->set_source_range(TimeRange(RationalTime(20,24),
RationalTime(10,24)));
SerializableObject::Retainer<LinearTimeWarp> tw1 =
new LinearTimeWarp("tw1", "", 0.51);
cl1->effects().push_back(dynamic_retainer_cast<Effect>(tw1));
SerializableObject::Retainer<Track> tr =
new Track();
tr->append_child(cl0);
tr->append_child(cl1);

opentimelineio::v1_0::ErrorStatus err;

// Try IgnoreTimeEffects
auto trimmed_track =
track_trimmed_to_range(tr,
TimeRange(RationalTime(5,24),
RationalTime(7,24)),
&err,
IgnoreTimeEffects);
assertFalse(is_error(err));

assertEqual(trimmed_track->children().size(), 2);
auto cl0_trimmed = dynamic_cast<Clip*>(trimmed_track->children().at(0).value);
auto cl1_trimmed = dynamic_cast<Clip*>(trimmed_track->children().at(1).value);
auto cl0_trimmed_range = cl0_trimmed->trimmed_range();
auto cl1_trimmed_range = cl1_trimmed->trimmed_range();
assertEqual(cl0_trimmed_range,
TimeRange(RationalTime(5,24),
RationalTime(5,24)));
auto tw = dynamic_cast<LinearTimeWarp*>(cl0_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.51);
assertEqual(cl1_trimmed_range,
TimeRange(RationalTime(20,24),
RationalTime(2,24)));
tw = dynamic_cast<LinearTimeWarp*>(cl1_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.51);

// Try HonorTimeEffectsExactly
auto trimmed_track2 =
track_trimmed_to_range(tr,
TimeRange(RationalTime(5,24),
RationalTime(7,24)),
&err,
HonorTimeEffectsExactly);
assertFalse(is_error(err));

assertEqual(trimmed_track2->children().size(), 2);
cl0_trimmed = dynamic_cast<Clip*>(trimmed_track2->children().at(0).value);
cl1_trimmed = dynamic_cast<Clip*>(trimmed_track2->children().at(1).value);
cl0_trimmed_range = cl0_trimmed->trimmed_range();
cl1_trimmed_range = cl1_trimmed->trimmed_range();
assertEqual(cl0_trimmed_range,
TimeRange(RationalTime(2.55,24),
RationalTime(5,24)));
tw = dynamic_cast<LinearTimeWarp*>(cl0_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.51);
assertEqual(cl1_trimmed_range,
TimeRange(RationalTime(20,24),
RationalTime(2,24)));
tw = dynamic_cast<LinearTimeWarp*>(cl1_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.51);

// Try HonorTimeEffectsWithSnapping
auto trimmed_track3 =
track_trimmed_to_range(tr,
TimeRange(RationalTime(5,24),
RationalTime(7,24)),
&err,
HonorTimeEffectsWithSnapping);
assertFalse(is_error(err));

assertEqual(trimmed_track3->children().size(), 2);
cl0_trimmed = dynamic_cast<Clip*>(trimmed_track3->children().at(0).value);
cl1_trimmed = dynamic_cast<Clip*>(trimmed_track3->children().at(1).value);
cl0_trimmed_range = cl0_trimmed->trimmed_range();
cl1_trimmed_range = cl1_trimmed->trimmed_range();
assertEqual(cl0_trimmed_range,
TimeRange(RationalTime(2,24),
RationalTime(5,24)));
tw = dynamic_cast<LinearTimeWarp*>(cl1_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.5);
assertEqual(cl1_trimmed_range,
TimeRange(RationalTime(20,24),
RationalTime(2,24)));
tw = dynamic_cast<LinearTimeWarp*>(cl1_trimmed->effects()[0].value);
assertEqual(tw->time_scalar(), 0.5);
});
tests.run(argc, argv);
return 0;
}