Skip to content
Open
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
29 changes: 28 additions & 1 deletion base/include/TestSignalGeneratorSrc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#pragma once
#include "Module.h"
#include <string>

enum class OverlayType
{
NONE = 0,
FRAME_INDEX = 1,
TIMESTAMP = 2,
BOTH = 3
};

class TestSignalGeneratorProps : public ModuleProps
{
Expand All @@ -12,6 +21,16 @@ class TestSignalGeneratorProps : public ModuleProps

int width = 0;
int height = 0;

// Overlay configuration
OverlayType overlayType = OverlayType::NONE;
std::string overlayFgColor = "00FF00"; // Green
std::string overlayBgColor = "000000"; // Black
std::string timestampFormat = "%H:%M:%S"; // hh:mm:ss
bool timestampAppendMilliseconds = true; // Append .nnn to timestamp
int overlayX = -1; // -1 for auto-center
int overlayY = -1; // -1 for vertical middle
double overlayFontSize = -1.0; // -1 for auto-sizing

private:
friend class boost::serialization::access;
Expand All @@ -22,6 +41,14 @@ class TestSignalGeneratorProps : public ModuleProps
ar &boost::serialization::base_object<ModuleProps>(*this);
ar &width;
ar &height;
ar &overlayType;
ar &overlayFgColor;
ar &overlayBgColor;
ar &timestampFormat;
ar &timestampAppendMilliseconds;
ar &overlayX;
ar &overlayY;
ar &overlayFontSize;
}
};

Expand All @@ -35,7 +62,7 @@ class TestSignalGenerator : public Module
bool term();
void setProps(TestSignalGeneratorProps &props);
TestSignalGeneratorProps getProps();

protected:
bool produce();
bool validateOutputPins();
Expand Down
149 changes: 148 additions & 1 deletion base/src/TestSignalGeneratorSrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,160 @@
#include "Module.h"
#include <cstdlib>
#include <cstdint>
#include <opencv2/opencv.hpp>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>

class TestSignalGenerator::Detail
{
public:
Detail(TestSignalGeneratorProps &_props)
: mProps(_props), start_shade(0), end_shade(255), current_shade(start_shade) {}
: mProps(_props), start_shade(0), end_shade(255), current_shade(start_shade), frameCounter(0) {}

~Detail() {}

// Calculate optimal font size based on image dimensions
double calculateFontSize()
{
if (mProps.overlayFontSize > 0)
{
return mProps.overlayFontSize;
}
// Auto-size: use 1/20th of image height as base
return mProps.height / 600.0; // Scale factor for readable text
}

// Format current timestamp with milliseconds
std::string formatTimestamp()
{
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;

std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm;
localtime_s(&tm, &t);
char buffer[64];
std::strftime(buffer, sizeof(buffer), mProps.timestampFormat.c_str(), &tm);

// Append milliseconds if configured to do so
if (mProps.timestampAppendMilliseconds)
{
char finalBuffer[128];
sprintf_s(finalBuffer, sizeof(finalBuffer), "%s.%03d", buffer, (int)ms.count());
return std::string(finalBuffer);
}

return std::string(buffer);
}

// Render overlay text on the frame
void renderOverlay(uint8_t* frameData)
{
if (mProps.overlayType == OverlayType::NONE)
{
return;
}

// Convert YUV420 to BGR for OpenCV processing
cv::Mat yuvImg(mProps.height * 3 / 2, mProps.width, CV_8UC1, frameData);
cv::Mat bgrImg;
cv::cvtColor(yuvImg, bgrImg, cv::COLOR_YUV2BGR_I420);

// Parse colors
int r, g, b, backR, backG, backB;
sscanf_s(mProps.overlayFgColor.c_str(), "%02x%02x%02x", &r, &g, &b);
sscanf_s(mProps.overlayBgColor.c_str(), "%02x%02x%02x", &backR, &backG, &backB);

double fontSize = calculateFontSize();
int fontFace = cv::FONT_HERSHEY_SIMPLEX;
int thickness = std::max(1, (int)(fontSize * 2));

std::vector<std::string> lines;
std::vector<std::string> refLines; // For stable layout calculation

// Prepare text and reference lines
if (mProps.overlayType == OverlayType::FRAME_INDEX)
{
char buffer[32];
sprintf_s(buffer, sizeof(buffer), "Frame:%llu", frameCounter);
lines.push_back(std::string(buffer));
refLines.push_back("Frame: 0000000000"); // Max width ref
}
else if (mProps.overlayType == OverlayType::TIMESTAMP)
{
lines.push_back(formatTimestamp());
refLines.push_back("00:00:00.000"); // Max width ref
}
else if (mProps.overlayType == OverlayType::BOTH)
{
char buffer[32];
sprintf_s(buffer, sizeof(buffer), "Frame:%llu", frameCounter);
lines.push_back(std::string(buffer));
lines.push_back(formatTimestamp());

refLines.push_back("Frame: 0000000000");
refLines.push_back("00:00:00.000");
}

int maxWidth = 0;
int lineHeight = 0;
int baseline = 0;

for (const auto& line : refLines)
{
cv::Size textSize = cv::getTextSize(line, fontFace, fontSize, thickness, &baseline);
maxWidth = std::max(maxWidth, textSize.width);
lineHeight = std::max(lineHeight, textSize.height);
}

int lineSpacing = 20;
int totalHeight = (lineHeight * (int)lines.size()) + (lineSpacing * ((int)lines.size() - 1));


int x = mProps.overlayX >= 0 ? mProps.overlayX : (mProps.width - maxWidth) / 2;


int startY;
if (mProps.overlayY >= 0)
{
startY = mProps.overlayY;
}
else
{
startY = (mProps.height - totalHeight) / 2 + lineHeight;
}


int padding = 10;
int boxTop = startY - lineHeight;
int boxBottom = boxTop + totalHeight + padding + baseline;

cv::Point topLeft(x - padding, boxTop - padding);
cv::Point bottomRight(x + maxWidth + padding, boxBottom);

cv::rectangle(bgrImg, topLeft, bottomRight, cv::Scalar(backB, backG, backR), cv::FILLED);

int currentY = startY;
for (const auto& line : lines)
{
cv::putText(bgrImg, line, cv::Point(x, currentY),
fontFace, fontSize, cv::Scalar(b, g, r), thickness, cv::LINE_AA);
currentY += lineHeight + lineSpacing;
}

// Convert back to YUV420
cv::cvtColor(bgrImg, yuvImg, cv::COLOR_BGR2YUV_I420);
}

bool generate(frame_sp &frame)
{
auto frame_ptr = frame->data();
uint8_t* x = static_cast<uint8_t*>(frame_ptr);

// Generate gradient pattern
for (int height = 0; height < mProps.height * 1.5; height++)
{
memset(x, current_shade, mProps.width);
Expand All @@ -26,6 +166,10 @@ class TestSignalGenerator::Detail
current_shade = start_shade;
}
}

renderOverlay(static_cast<uint8_t*>(frame_ptr));
frameCounter++;

return true;
}

Expand All @@ -34,15 +178,18 @@ class TestSignalGenerator::Detail
mProps = _props;
reset();
}

void reset()
{
current_shade = start_shade;
frameCounter = 0;
}

TestSignalGeneratorProps mProps;
uint8_t start_shade = 0;
uint8_t end_shade = 255;
uint8_t current_shade = 0;
uint64_t frameCounter = 0;
};

TestSignalGenerator::TestSignalGenerator(TestSignalGeneratorProps _props)
Expand Down
103 changes: 103 additions & 0 deletions base/test/testSignalGeneratorSrc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SinkModule : public Module
return true;
}
};

BOOST_AUTO_TEST_CASE(Basic)
{
auto source = boost::shared_ptr<TestSignalGenerator>(new TestSignalGenerator(TestSignalGeneratorProps(400, 400)));
Expand Down Expand Up @@ -83,4 +84,106 @@ BOOST_AUTO_TEST_CASE(getSetProps)
Test_Utils::saveOrCompare("./data/TestSample2.raw",pReadDataTest,readDataSizeTest, 0);
}

BOOST_AUTO_TEST_CASE(FrameIndexOverlay)
{
TestSignalGeneratorProps props(640, 360);
props.overlayType = OverlayType::FRAME_INDEX;
props.overlayFgColor = "00FF00"; // Green
props.overlayBgColor = "000000"; // Black

auto source = boost::shared_ptr<TestSignalGenerator>(new TestSignalGenerator(props));
auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
source->setNext(sink);
BOOST_TEST(source->init());
BOOST_TEST(sink->init());

// Generate 5 frames
for (int i = 0; i < 5; i++)
{
source->step();
auto frames = sink->try_pop();
BOOST_TEST(frames.size() == 1);
auto outputFrame = frames.cbegin()->second;
BOOST_TEST(outputFrame->getMetadata()->getFrameType() == FrameMetadata::RAW_IMAGE_PLANAR);
const uint8_t* pData = static_cast<const uint8_t*>(outputFrame->data());
unsigned int dataSize = outputFrame->size();
std::string filename = "./data/frame_overlay_" + std::to_string(i) + ".raw";
Test_Utils::saveOrCompare(filename.c_str(), pData, dataSize, 0);
}
}

BOOST_AUTO_TEST_CASE(TimestampOverlay)
{
TestSignalGeneratorProps props(640, 360);
props.overlayType = OverlayType::TIMESTAMP;
props.timestampFormat = "%H:%M:%S"; // hh:mm:ss format

auto source = boost::shared_ptr<TestSignalGenerator>(new TestSignalGenerator(props));
auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
source->setNext(sink);
BOOST_TEST(source->init());
BOOST_TEST(sink->init());

source->step();
auto frames = sink->try_pop();
BOOST_TEST(frames.size() == 1);
auto outputFrame = frames.cbegin()->second;
BOOST_TEST(outputFrame->getMetadata()->getFrameType() == FrameMetadata::RAW_IMAGE_PLANAR);


const uint8_t* pReadDataTest = const_cast<const uint8_t *>(static_cast<uint8_t *>(outputFrame->data()));
unsigned int readDataSizeTest = outputFrame->size();
Test_Utils::saveOrCompare("./data/TestSample_Timestamp.raw", pReadDataTest, readDataSizeTest, 0);
}

BOOST_AUTO_TEST_CASE(BothOverlays)
{
TestSignalGeneratorProps props(640, 360);
props.overlayType = OverlayType::BOTH;
props.overlayFgColor = "FFFF00"; // Yellow
props.overlayBgColor = "000080"; // Dark blue

auto source = boost::shared_ptr<TestSignalGenerator>(new TestSignalGenerator(props));
auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
source->setNext(sink);
BOOST_TEST(source->init());
BOOST_TEST(sink->init());

source->step();
auto frames = sink->try_pop();
BOOST_TEST(frames.size() == 1);
auto outputFrame = frames.cbegin()->second;
BOOST_TEST(outputFrame->getMetadata()->getFrameType() == FrameMetadata::RAW_IMAGE_PLANAR);

const uint8_t* pReadDataTest = const_cast<const uint8_t *>(static_cast<uint8_t *>(outputFrame->data()));
unsigned int readDataSizeTest = outputFrame->size();
Test_Utils::saveOrCompare("./data/TestSample_Both.raw", pReadDataTest, readDataSizeTest, 0);
}

BOOST_AUTO_TEST_CASE(CustomOverlayConfig)
{
TestSignalGeneratorProps props(800, 600);
props.overlayType = OverlayType::FRAME_INDEX;
props.overlayFgColor = "FF0000";
props.overlayBgColor = "FFFFFF";
props.overlayFontSize = 2.0;
props.overlayX = 50;
props.overlayY = 100;

auto source = boost::shared_ptr<TestSignalGenerator>(new TestSignalGenerator(props));
auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
source->setNext(sink);
BOOST_TEST(source->init());
BOOST_TEST(sink->init());

source->step();
auto frames = sink->try_pop();
BOOST_TEST(frames.size() == 1);
auto outputFrame = frames.cbegin()->second;
BOOST_TEST(outputFrame->getMetadata()->getFrameType() == FrameMetadata::RAW_IMAGE_PLANAR);
const uint8_t* pReadDataTest = const_cast<const uint8_t *>(static_cast<uint8_t *>(outputFrame->data()));
unsigned int readDataSizeTest = outputFrame->size();
Test_Utils::saveOrCompare("./data/TestSample_CustomOverlay.raw", pReadDataTest, readDataSizeTest, 0);
}

BOOST_AUTO_TEST_SUITE_END()
12 changes: 2 additions & 10 deletions base/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@
}
],
"dependencies": [
{
"name": "whisper",
"default-features": false,
"features": [
"cuda"
]
},
{
"name": "opencv4",
"default-features": false,
"features": [
"contrib",
"cuda",
"cudnn",
"dnn",
"jpeg",
"nonfree",
Expand Down Expand Up @@ -61,6 +52,7 @@
"zlib",
"sfml",
"brotli",
"whisper",
{
"name": "gtk3",
"platform": "!windows"
Expand Down Expand Up @@ -99,4 +91,4 @@
"name": "libmp4"
}
]
}
}
Loading
Loading