From 1e9f10487127845580c651613dca699c1af2fdba Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Tue, 5 Dec 2023 20:49:25 -0600 Subject: [PATCH] Add a loop range feature to MLT Producer --- src/framework/mlt.vers | 7 ++++++- src/framework/mlt_producer.c | 33 +++++++++++++++++++++++++++++++++ src/framework/mlt_producer.h | 1 + src/mlt++/MltProducer.cpp | 5 +++++ src/mlt++/MltProducer.h | 1 + src/mlt++/mlt++.vers | 7 +++++++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/framework/mlt.vers b/src/framework/mlt.vers index a55580dd4..093a638a0 100644 --- a/src/framework/mlt.vers +++ b/src/framework/mlt.vers @@ -687,4 +687,9 @@ MLT_7.22.0 { mlt_property_is_color; mlt_property_is_numeric; mlt_property_is_rect; -} MLT_7.18.0; \ No newline at end of file +} MLT_7.18.0; + +MLT_7.24.0 { + global: + mlt_producer_set_loop_range; +} MLT_7.22.0; diff --git a/src/framework/mlt_producer.c b/src/framework/mlt_producer.c index 904c9e029..a4bd6ccac 100644 --- a/src/framework/mlt_producer.c +++ b/src/framework/mlt_producer.c @@ -356,6 +356,14 @@ int mlt_producer_seek(mlt_producer self, mlt_position position) // Do not bounds check a link. } else if (position < 0 || mlt_producer_get_playtime(self) == 0) { position = 0; + } else if (mlt_properties_exists(MLT_PRODUCER_PROPERTIES(self), "_loop_start")) { + int loopStart = mlt_properties_get_int(MLT_PRODUCER_PROPERTIES(self), "_loop_start"); + int loopEnd = mlt_properties_get_int(MLT_PRODUCER_PROPERTIES(self), "_loop_end"); + if (loopStart >= 0 && loopEnd > 0 && loopStart < loopEnd) { + if (position > loopEnd || position < loopStart) { + position = loopStart; + } + } } else if (use_points && (eof == NULL || !strcmp(eof, "pause")) && position >= mlt_producer_get_playtime(self)) { mlt_producer_set_speed(self, 0); @@ -1304,3 +1312,28 @@ int mlt_producer_probe(mlt_producer self) } return 0; } + +/** Set the loop range for the producer. + * + * When the loop range is set, the producer will automatically seek to the start frame + * after it provides the end frame. Set start to -1 to disable looping. + * + * \public \memberof mlt_producer_s + * \param self a producer + * \param start the frame number for the beginning of the loop + * \param end the frame number for the end of the loop + */ + +void mlt_producer_set_loop_range(mlt_producer self, int start, int end) +{ + if (!self) + return; + mlt_properties properties = MLT_PRODUCER_PROPERTIES(self); + if (start < 0 || end < 0) { + mlt_properties_clear(properties, "_loop_start"); + mlt_properties_clear(properties, "_loop_end"); + } else { + mlt_properties_set_int(properties, "_loop_start", start); + mlt_properties_set_int(properties, "_loop_end", end); + } +} diff --git a/src/framework/mlt_producer.h b/src/framework/mlt_producer.h index 9b7bcff7b..7d4d6e2c5 100644 --- a/src/framework/mlt_producer.h +++ b/src/framework/mlt_producer.h @@ -145,5 +145,6 @@ extern void mlt_producer_close(mlt_producer self); int64_t mlt_producer_get_creation_time(mlt_producer self); void mlt_producer_set_creation_time(mlt_producer self, int64_t creation_time); extern int mlt_producer_probe(mlt_producer self); +extern void mlt_producer_set_loop_range(mlt_producer self, int start, int end); #endif diff --git a/src/mlt++/MltProducer.cpp b/src/mlt++/MltProducer.cpp index a837194df..56d851b31 100644 --- a/src/mlt++/MltProducer.cpp +++ b/src/mlt++/MltProducer.cpp @@ -266,3 +266,8 @@ bool Producer::probe() { return mlt_producer_probe(get_producer()); } + +void Producer::set_loop_range(int start, int end) +{ + return mlt_producer_set_loop_range(get_producer(), start, end); +} diff --git a/src/mlt++/MltProducer.h b/src/mlt++/MltProducer.h index 7afefbc36..d7a5ee525 100644 --- a/src/mlt++/MltProducer.h +++ b/src/mlt++/MltProducer.h @@ -78,6 +78,7 @@ class MLTPP_DECLSPEC Producer : public Service int64_t get_creation_time(); void set_creation_time(int64_t creation_time); bool probe(); + void set_loop_range(int start, int end); }; } // namespace Mlt diff --git a/src/mlt++/mlt++.vers b/src/mlt++/mlt++.vers index 5d5510d47..a2a92f314 100644 --- a/src/mlt++/mlt++.vers +++ b/src/mlt++/mlt++.vers @@ -733,3 +733,10 @@ MLT_7.14.0 { "Mlt::Chain::attach_normalizers()"; }; } MLT_7.12.0; + +MLT_7.24.0 { + global: + extern "C++" { + "Mlt::Producer::set_loop_range(int, int)"; + }; +} MLT_7.14.0; \ No newline at end of file