Skip to content

Commit 7b4051c

Browse files
committed
Cleanup Flatbuffer processing
1 parent caa8234 commit 7b4051c

File tree

5 files changed

+54
-90
lines changed

5 files changed

+54
-90
lines changed

include/utils/ImageResampler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ImageResampler
1717
void setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom);
1818
void setVideoMode(VideoMode mode) { _videoMode = mode; }
1919
void setFlipMode(FlipMode mode) { _flipMode = mode; }
20-
void processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat, Image<ColorRgb> & outputImage) const;
20+
void processImage(const uint8_t * data, int width, int height, size_t lineLength, PixelFormat pixelFormat, Image<ColorRgb> & outputImage) const;
2121

2222
private:
2323
int _horizontalDecimation;

libsrc/flatbufserver/FlatBufferClient.cpp

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -53,67 +53,31 @@ void FlatBufferClient::readyRead()
5353
_timeoutTimer->start();
5454
_receiveBuffer += _socket->readAll();
5555

56-
processNextMessage();
57-
}
58-
59-
bool FlatBufferClient::processNextMessage()
60-
{
61-
if (_processingMessage) { return false; } // Avoid re-entrancy
62-
63-
// Wait for at least 4 bytes to read the message size
64-
if (_receiveBuffer.size() < 4)
65-
{
66-
return false;
67-
}
68-
69-
_processingMessage = true;
70-
71-
// Directly read message size (no memcpy)
72-
const uint8_t* raw = reinterpret_cast<const uint8_t*>(_receiveBuffer.constData());
73-
uint32_t const messageSize = (raw[0] << 24) | (raw[1] << 16) | (raw[2] << 8) | raw[3];
74-
75-
// // Validate message size
76-
if (messageSize == 0)
56+
// check if we can read a header
57+
while(_receiveBuffer.size() >= 4)
7758
{
78-
Warning(_log, "Invalid message size: %u - dropping received data", messageSize);
79-
_receiveBuffer.clear();
80-
_processingMessage = false;
81-
return true;
82-
}
59+
// Directly read message size
60+
const uint8_t* raw = reinterpret_cast<const uint8_t*>(_receiveBuffer.constData());
61+
uint32_t const messageSize = (raw[0] << 24) | (raw[1] << 16) | (raw[2] << 8) | raw[3];
8362

84-
// Wait for full message
85-
if (_receiveBuffer.size() < static_cast<int>(messageSize + 4))
86-
{
87-
_processingMessage = false;
88-
return false;
89-
}
63+
// check if we can read a complete message
64+
if((uint32_t) _receiveBuffer.size() < messageSize + 4) { return; }
9065

91-
// Remove the processed message from the buffer (header + body)
92-
_receiveBuffer.remove(0, messageSize + 4);
66+
// extract message without header and remove header + msg from buffer :: QByteArray::remove() does not return the removed data
67+
const uint8_t* msgData = reinterpret_cast<const uint8_t*>(_receiveBuffer.constData() + 4);
68+
_receiveBuffer.remove(0, messageSize + 4);
9369

94-
// Extract the message and remove it from the buffer (no copying)
95-
const uint8_t* msgData = reinterpret_cast<const uint8_t*>(_receiveBuffer.constData() + 4);
96-
flatbuffers::Verifier verifier(msgData, messageSize);
70+
flatbuffers::Verifier verifier(msgData, messageSize);
9771

98-
if (!hyperionnet::VerifyRequestBuffer(verifier)) {
99-
Error(_log, "Invalid FlatBuffer message received");
100-
sendErrorReply("Invalid FlatBuffer message received");
101-
_processingMessage = false;
72+
if (!hyperionnet::VerifyRequestBuffer(verifier)) {
73+
Error(_log, "Invalid FlatBuffer message received");
74+
sendErrorReply("Invalid FlatBuffer message received");
75+
continue;
76+
}
10277

103-
QMetaObject::invokeMethod(this, &FlatBufferClient::processNextMessage, Qt::QueuedConnection);
104-
return true;
78+
const auto *message = hyperionnet::GetRequest(msgData);
79+
handleMessage(message);
10580
}
106-
107-
// Invoke message handling
108-
QMetaObject::invokeMethod(this, [this, msgData]() {
109-
handleMessage(hyperionnet::GetRequest(msgData));
110-
_processingMessage = false;
111-
112-
// Continue processing the next message
113-
QMetaObject::invokeMethod(this, &FlatBufferClient::processNextMessage, Qt::QueuedConnection);
114-
});
115-
116-
return true;
11781
}
11882

11983
void FlatBufferClient::noDataReceived()
@@ -213,8 +177,8 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
213177
const auto* img = static_cast<const hyperionnet::RawImage*>(image->data_as_RawImage());
214178

215179
// Read image properties directly from FlatBuffer
216-
const int width = img->width();
217-
const int height = img->height();
180+
int32_t const width = img->width();
181+
int32_t const height = img->height();
218182
const auto* data = img->data();
219183

220184
if (width <= 0 || height <= 0 || data == nullptr || data->size() == 0)
@@ -224,8 +188,8 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
224188
}
225189

226190
// Check consistency of image data size
227-
const int dataSize = data->size();
228-
const int bytesPerPixel = dataSize / (width * height);
191+
auto dataSize = data->size();
192+
int const bytesPerPixel = dataSize / (width * height);
229193
if (bytesPerPixel != 3 && bytesPerPixel != 4)
230194
{
231195
sendErrorReply("Size of image data does not match with the width and height");
@@ -244,9 +208,9 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
244208
{
245209
const auto* img = static_cast<const hyperionnet::NV12Image*>(image->data_as_NV12Image());
246210

247-
const int width = img->width();
248-
const int height = img->height();
249-
const auto* data_y = img->data_y();
211+
int32_t const width = img->width();
212+
int32_t const height = img->height();
213+
const auto* const data_y = img->data_y();
250214
const auto* data_uv = img->data_uv();
251215

252216
if (width <= 0 || height <= 0 || data_y == nullptr || data_uv == nullptr ||
@@ -257,10 +221,10 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
257221
}
258222

259223
// Combine Y and UV into one contiguous buffer (reuse class member buffer)
260-
const size_t y_size = data_y->size();
261-
const size_t uv_size = data_uv->size();
224+
size_t const y_size = data_y->size();
225+
size_t const uv_size = data_uv->size();
262226

263-
size_t required_size = y_size + uv_size;
227+
size_t const required_size = y_size + uv_size;
264228
if (_combinedNv12Buffer.capacity() < required_size)
265229
{
266230
_combinedNv12Buffer.reserve(required_size);
@@ -269,7 +233,7 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
269233
std::memcpy(_combinedNv12Buffer.data() + y_size, data_uv->data(), uv_size);
270234

271235
// Determine stride for Y
272-
const int stride_y = img->stride_y() > 0 ? img->stride_y() : width;
236+
int32_t const stride_y = img->stride_y() > 0 ? img->stride_y() : width;
273237

274238
// Resize only when needed
275239
if (_imageOutputBuffer.width() != width || _imageOutputBuffer.height() != height)
@@ -326,7 +290,7 @@ void FlatBufferClient::sendMessage(const uint8_t* data, size_t size)
326290

327291
// write message
328292
_socket->write(reinterpret_cast<const char*>(header), sizeof(header));
329-
_socket->write(reinterpret_cast<const char *>(data), size);
293+
_socket->write(reinterpret_cast<const char *>(data), static_cast<qint64>(size));
330294
_socket->flush();
331295
}
332296

@@ -349,13 +313,13 @@ void FlatBufferClient::sendErrorReply(const QString& error)
349313
}
350314

351315
inline void FlatBufferClient::processRawImage(const uint8_t* buffer,
352-
int width,
353-
int height,
316+
int32_t width,
317+
int32_t height,
354318
int bytesPerPixel,
355-
ImageResampler& resampler,
319+
const ImageResampler& resampler,
356320
Image<ColorRgb>& outputImage)
357321
{
358-
int const lineLength = width * bytesPerPixel;
322+
const size_t lineLength = static_cast<size_t>(width) * bytesPerPixel;
359323
PixelFormat const pixelFormat = (bytesPerPixel == 4) ? PixelFormat::RGB32 : PixelFormat::RGB24;
360324

361325
resampler.processImage(
@@ -369,13 +333,13 @@ inline void FlatBufferClient::processRawImage(const uint8_t* buffer,
369333
}
370334

371335
inline void FlatBufferClient::processNV12Image(const uint8_t* nv12_data,
372-
int width,
373-
int height,
374-
int stride_y,
375-
ImageResampler& resampler,
336+
int32_t width,
337+
int32_t height,
338+
int32_t stride_y,
339+
const ImageResampler& resampler,
376340
Image<ColorRgb>& outputImage)
377341
{
378-
PixelFormat pixelFormat = PixelFormat::NV12;
342+
PixelFormat const pixelFormat = PixelFormat::NV12;
379343

380344
resampler.processImage(
381345
nv12_data, // Combined NV12 buffer

libsrc/flatbufserver/FlatBufferClient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private slots:
143143
///
144144
void sendErrorReply(const QString& error);
145145

146-
void processRawImage(const uint8_t* buffer, int width, int height, int bytesPerPixel, ImageResampler& resampler, Image<ColorRgb>& outputImage);
147-
void processNV12Image(const uint8_t* nv12_data, int width, int height, int stride_y, ImageResampler& resampler, Image<ColorRgb>& outputImage);
146+
void processRawImage(const uint8_t* buffer, int32_t width, int32_t height, int bytesPerPixel, const ImageResampler& resampler, Image<ColorRgb>& outputImage);
147+
void processNV12Image(const uint8_t* nv12_data, int32_t width, int32_t height, int32_t stride_y, const ImageResampler& resampler, Image<ColorRgb>& outputImage);
148148

149149
private:
150150
Logger * _log;
@@ -157,8 +157,8 @@ private slots:
157157

158158
QByteArray _receiveBuffer;
159159

160-
Image<ColorRgb> _imageOutputBuffer;
161160
ImageResampler _imageResampler;
161+
Image<ColorRgb> _imageOutputBuffer;
162162
std::vector<uint8_t> _combinedNv12Buffer;
163163

164164
// Flatbuffers builder

libsrc/flatbufserver/FlatBufferConnection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ void FlatBufferConnection::setImage(const Image<ColorRgb> &image)
124124
if (!isClientRegistered()) return;
125125

126126
const uint8_t* buffer = reinterpret_cast<const uint8_t*>(image.memptr());
127-
size_t bufferSize = image.size();
127+
qsizetype bufferSize = image.size();
128128

129129
// Convert the buffer into QByteArray
130-
QByteArray imageData = QByteArray::fromRawData(reinterpret_cast<const char*>(buffer), static_cast<int>(bufferSize));
130+
QByteArray imageData = QByteArray::fromRawData(reinterpret_cast<const char*>(buffer), bufferSize);
131131
setImage(imageData, image.width(), image.height());
132132
}
133133

libsrc/utils/ImageResampler.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void ImageResampler::setCropping(int cropLeft, int cropRight, int cropTop, int c
2222
_cropBottom = cropBottom;
2323
}
2424

25-
void ImageResampler::processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat, Image<ColorRgb> &outputImage) const
25+
void ImageResampler::processImage(const uint8_t * data, int width, int height, size_t lineLength, PixelFormat pixelFormat, Image<ColorRgb> &outputImage) const
2626
{
2727
int cropLeft = _cropLeft;
2828
int cropRight = _cropRight;
@@ -89,7 +89,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
8989
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
9090
{
9191
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
92-
int index = lineLength * ySource + (xSource << 1);
92+
size_t index = lineLength * ySource + (xSource << 1);
9393
uint8_t y = data[index+1];
9494
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
9595
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
@@ -106,7 +106,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
106106
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
107107
{
108108
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
109-
int index = lineLength * ySource + (xSource << 1);
109+
size_t index = lineLength * ySource + (xSource << 1);
110110
uint8_t y = data[index];
111111
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
112112
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
@@ -123,7 +123,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
123123
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
124124
{
125125
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
126-
int index = lineLength * ySource + (xSource << 1);
126+
size_t index = lineLength * ySource + (xSource << 1);
127127
rgb.blue = (data[index] & 0x1f) << 3;
128128
rgb.green = (((data[index+1] & 0x7) << 3) | (data[index] & 0xE0) >> 5) << 2;
129129
rgb.red = (data[index+1] & 0xF8);
@@ -139,7 +139,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
139139
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
140140
{
141141
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
142-
int index = lineLength * ySource + (xSource << 1) + xSource;
142+
size_t index = lineLength * ySource + (xSource << 1) + xSource;
143143
rgb.red = data[index ];
144144
rgb.green = data[index+1];
145145
rgb.blue = data[index+2];
@@ -155,7 +155,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
155155
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
156156
{
157157
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
158-
int index = lineLength * ySource + (xSource << 1) + xSource;
158+
size_t index = lineLength * ySource + (xSource << 1) + xSource;
159159
rgb.blue = data[index ];
160160
rgb.green = data[index+1];
161161
rgb.red = data[index+2];
@@ -171,7 +171,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
171171
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
172172
{
173173
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
174-
int index = lineLength * ySource + (xSource << 2);
174+
size_t index = lineLength * ySource + (xSource << 2);
175175
rgb.red = data[index ];
176176
rgb.green = data[index+1];
177177
rgb.blue = data[index+2];
@@ -187,7 +187,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
187187
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
188188
{
189189
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
190-
int index = lineLength * ySource + (xSource << 2);
190+
size_t index = lineLength * ySource + (xSource << 2);
191191
rgb.blue = data[index ];
192192
rgb.green = data[index+1];
193193
rgb.red = data[index+2];
@@ -200,7 +200,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
200200
{
201201
for (int yDest = yDestStart, ySource = cropTop + (_verticalDecimation >> 1); yDest <= yDestEnd; ySource += _verticalDecimation, ++yDest)
202202
{
203-
int uOffset = (height + ySource / 2) * lineLength;
203+
size_t uOffset = (height + ySource / 2) * lineLength;
204204
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
205205
{
206206
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));

0 commit comments

Comments
 (0)