Skip to content
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
1 change: 1 addition & 0 deletions src/FilterData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct filter_data : public ORTModelData, public std::enable_shared_from_this<fi
gs_stagesurf_t *stagesurface;

cv::Mat inputBGRA;
std::atomic<bool> newFrameAvailable{false};

std::atomic<bool> isDisabled{false};

Expand Down
6 changes: 3 additions & 3 deletions src/background-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,11 @@ void background_filter_video_tick(void *data, float seconds)
// No data to process
return;
}
if (tf->inputBGRA.empty()) {
// No data to process
if (!tf->newFrameAvailable) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code:

		if (!tf->newFrameAvailable) {
			return;
		}
		imageBGRA = tf->inputBGRA.clone();
		cv::swap(imageBGRA, tf->inputBGRA);
		tf->newFrameAvailable = false;

should be like:

		if (tf->newFrameAvailable.exchange(false, std::memory_order_acq_rel)) {
			cv::swap(imageBGRA, tf->inputBGRA);
		}

for better performance with appropriate memory barrier.

return;
}
imageBGRA = tf->inputBGRA.clone();
cv::swap(imageBGRA, tf->inputBGRA);
tf->newFrameAvailable = false;
}

if (tf->enableImageSimilarity) {
Expand Down
10 changes: 5 additions & 5 deletions src/enhance-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,18 @@ void enhance_filter_video_tick(void *data, float seconds)
return;
}

if (tf->inputBGRA.empty()) {
return;
}

// Get input image from source rendering pipeline
cv::Mat imageBGRA;
{
std::unique_lock<std::mutex> lock(tf->inputBGRALock, std::try_to_lock);
if (!lock.owns_lock()) {
return;
}
imageBGRA = tf->inputBGRA.clone();
if (!tf->newFrameAvailable) {
return;
}
cv::swap(imageBGRA, tf->inputBGRA);
tf->newFrameAvailable = false;
Comment on lines +257 to +261

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!tf->newFrameAvailable) {
return;
}
cv::swap(imageBGRA, tf->inputBGRA);
tf->newFrameAvailable = false;
if (!tf->newFrameAvailable.exchange(false, std::memory_order_acq_rel)) {
cv::swap(imageBGRA, tf->inputBGRA);
}

}

cv::Mat outputImage;
Expand Down
7 changes: 4 additions & 3 deletions src/obs-utils/obs-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ bool getRGBAFromStageSurface(filter_data *tf, uint32_t &width, uint32_t &height)
std::lock_guard<std::mutex> lock(tf->inputBGRALock);
// Create a temporary Mat that wraps the video_data pointer
cv::Mat temp(height, width, CV_8UC4, video_data, linesize);
// Clone the data to ensure tf->inputBGRA has its own copy
// This prevents use-after-unmap race condition
tf->inputBGRA = temp.clone();
// Copy frame data into tf->inputBGRA, reusing its allocation
// when dimensions match. Ensures we own the pixels before unmap.
temp.copyTo(tf->inputBGRA);
tf->newFrameAvailable = true;
}
gs_stagesurface_unmap(tf->stagesurface);
return true;
Expand Down
Loading