diff --git a/app/project/item/footage/audiostream.cpp b/app/project/item/footage/audiostream.cpp index 9182f135c..6b9e20e9d 100644 --- a/app/project/item/footage/audiostream.cpp +++ b/app/project/item/footage/audiostream.cpp @@ -20,7 +20,8 @@ #include "audiostream.h" -AudioStream::AudioStream() +AudioStream::AudioStream() : + index_done_(false) { set_type(kAudio); } @@ -61,3 +62,47 @@ void AudioStream::set_sample_rate(const int &sample_rate) { sample_rate_ = sample_rate; } + +const rational &AudioStream::index_length() +{ + QMutexLocker locker(&index_access_lock_); + + return index_length_; +} + +void AudioStream::set_index_length(const rational &index_length) +{ + { + QMutexLocker locker(&index_access_lock_); + + index_length_ = index_length; + } + + emit IndexChanged(); +} + +const bool &AudioStream::index_done() +{ + QMutexLocker locker(&index_access_lock_); + + return index_done_; +} + +void AudioStream::set_index_done(const bool& index_done) +{ + { + QMutexLocker locker(&index_access_lock_); + + index_done_ = index_done; + } + + emit IndexChanged(); +} + +void AudioStream::clear_index() +{ + QMutexLocker locker(&index_access_lock_); + + index_done_ = false; + index_length_ = 0; +} diff --git a/app/project/item/footage/audiostream.h b/app/project/item/footage/audiostream.h index c12093e8a..13e8f6202 100644 --- a/app/project/item/footage/audiostream.h +++ b/app/project/item/footage/audiostream.h @@ -43,10 +43,23 @@ public: const int& sample_rate() const; void set_sample_rate(const int& sample_rate); + const rational& index_length(); + void set_index_length(const rational& index_length); + + const bool& index_done(); + void set_index_done(const bool &index_done); + + void clear_index(); + private: int channels_; uint64_t layout_; int sample_rate_; + + QMutex index_access_lock_; + rational index_length_; + bool index_done_; + }; using AudioStreamPtr = std::shared_ptr; diff --git a/app/project/item/footage/videostream.cpp b/app/project/item/footage/videostream.cpp index ef08b1adc..5a0fb5df4 100644 --- a/app/project/item/footage/videostream.cpp +++ b/app/project/item/footage/videostream.cpp @@ -22,9 +22,12 @@ #include +#include "common/timecodefunctions.h" + const int64_t VideoStream::kEndTimestamp = AV_NOPTS_VALUE; -VideoStream::VideoStream() +VideoStream::VideoStream() : + start_time_(0) { set_type(kVideo); } @@ -46,7 +49,26 @@ void VideoStream::set_frame_rate(const rational &frame_rate) frame_rate_ = frame_rate; } -int64_t VideoStream::get_closest_timestamp_in_frame_index(const int64_t &ts) +const int64_t &VideoStream::start_time() const +{ + return start_time_; +} + +void VideoStream::set_start_time(const int64_t &start_time) +{ + start_time_ = start_time; +} + +int64_t VideoStream::get_closest_timestamp_in_frame_index(const rational &time) +{ + // Get rough approximation of what the timestamp would be in this timebase + int64_t target_ts = Timecode::time_to_timestamp(time, timebase()); + + // Find closest actual timebase in the file + return get_closest_timestamp_in_frame_index(target_ts); +} + +int64_t VideoStream::get_closest_timestamp_in_frame_index(int64_t timestamp) { QMutexLocker locker(&index_access_lock_); @@ -54,7 +76,10 @@ int64_t VideoStream::get_closest_timestamp_in_frame_index(const int64_t &ts) return -1; } - if (ts <= 0) { + // Adjust target by stream's start time + timestamp += start_time_; + + if (timestamp <= 0) { return frame_index_.first(); } @@ -68,9 +93,9 @@ int64_t VideoStream::get_closest_timestamp_in_frame_index(const int64_t &ts) for (int i=0;i ts) { + if (this_ts == timestamp) { + return timestamp; + } else if (this_ts > timestamp) { return frame_index_.at(i - 1); } } @@ -86,16 +111,24 @@ int64_t VideoStream::get_closest_timestamp_in_frame_index(const int64_t &ts) void VideoStream::clear_frame_index() { - QMutexLocker locker(&index_access_lock_); + { + QMutexLocker locker(&index_access_lock_); - frame_index_.clear(); + frame_index_.clear(); + } + + emit IndexChanged(); } void VideoStream::append_frame_index(const int64_t &ts) { - QMutexLocker locker(&index_access_lock_); + { + QMutexLocker locker(&index_access_lock_); - frame_index_.append(ts); + frame_index_.append(ts); + } + + emit IndexChanged(); } bool VideoStream::is_frame_index_ready() @@ -118,17 +151,21 @@ bool VideoStream::load_frame_index(const QString &s) QFile index_file(s); if (index_file.exists() && index_file.open(QFile::ReadOnly)) { - QMutexLocker locker(&index_access_lock_); + { + QMutexLocker locker(&index_access_lock_); - // Resize based on filesize - frame_index_.resize(static_cast(index_file.size()) / sizeof(int64_t)); + // Resize based on filesize + frame_index_.resize(static_cast(index_file.size()) / sizeof(int64_t)); - // Read frame index into vector - index_file.read(reinterpret_cast(frame_index_.data()), - index_file.size()); + // Read frame index into vector + index_file.read(reinterpret_cast(frame_index_.data()), + index_file.size()); + } index_file.close(); + emit IndexChanged(); + return true; } diff --git a/app/project/item/footage/videostream.h b/app/project/item/footage/videostream.h index ac899aa21..3fed4bfc4 100644 --- a/app/project/item/footage/videostream.h +++ b/app/project/item/footage/videostream.h @@ -40,10 +40,13 @@ public: const rational& frame_rate() const; void set_frame_rate(const rational& frame_rate); - int64_t get_closest_timestamp_in_frame_index(const int64_t& ts); + const int64_t& start_time() const; + void set_start_time(const int64_t& start_time); + + int64_t get_closest_timestamp_in_frame_index(const rational& time); + int64_t get_closest_timestamp_in_frame_index(int64_t timestamp); void clear_frame_index(); void append_frame_index(const int64_t& ts); - //bool is_frame_index_empty(); bool is_frame_index_ready(); int64_t last_frame_index_timestamp(); @@ -55,6 +58,8 @@ private: QVector frame_index_; + int64_t start_time_; + QMutex index_access_lock_; };