Skip to content

Commit 0aee1a9

Browse files
authored
Merge branch 'main' into feat/disable-preset-model-export
2 parents 29ba51b + 5fc47a2 commit 0aee1a9

23 files changed

Lines changed: 963 additions & 273 deletions

docker-compose.sophon.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
bash -lc "
1111
set -e;
1212
echo 'Starting cross-compilation...';
13-
./scripts/build.sh -m data/resource/aiboxresource;
13+
./scripts/build.sh -T -m data/resource/aiboxresource;
1414
mkdir -p /build_output;
1515
cp -f build/packages/* /build_output/ 2>/dev/null || echo 'Warning: No packages found to copy.';
1616
ls -lh /build_output;

src/flow/alarm/TaskAlarmLlmReview.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// TaskAlarmLlmReview.cc — LLM-based alarm review.
22
// Implementation partition of TaskAlarm (declared in flow/alarm/TaskAlarm.h).
33

4+
#include <algorithm>
5+
#include <cstdint>
46
#include <nlohmann/json.hpp>
57
#include <sstream>
68

@@ -142,18 +144,21 @@ bool TaskAlarm::LlmReviewAlarm(const DataAlarmUnit& alarmUnit, const VideoFrameP
142144
return true;
143145
}
144146

145-
int imgW = static_cast<int>(srcFrame->GetWidth());
146-
int imgH = static_cast<int>(srcFrame->GetHeight());
147-
const int pad = 48;
148-
int x0 = std::max(0, static_cast<int>(alarmUnit.box.x) - pad);
149-
int y0 = std::max(0, static_cast<int>(alarmUnit.box.y) - pad);
150-
int x1 = std::min(imgW, static_cast<int>(alarmUnit.box.x + alarmUnit.box.width) + pad);
151-
int y1 = std::min(imgH, static_cast<int>(alarmUnit.box.y + alarmUnit.box.height) + pad);
152-
int cropW = x1 - x0;
153-
int cropH = y1 - y0;
147+
const int imgW = static_cast<int>(srcFrame->GetWidth());
148+
const int imgH = static_cast<int>(srcFrame->GetHeight());
149+
constexpr int64_t kCropPadding = 48;
150+
const int64_t x0 = std::max<int64_t>(0, static_cast<int64_t>(alarmUnit.box.x) - kCropPadding);
151+
const int64_t y0 = std::max<int64_t>(0, static_cast<int64_t>(alarmUnit.box.y) - kCropPadding);
152+
const int64_t x1 =
153+
std::min<int64_t>(imgW, static_cast<int64_t>(alarmUnit.box.x) + alarmUnit.box.width + kCropPadding);
154+
const int64_t y1 =
155+
std::min<int64_t>(imgH, static_cast<int64_t>(alarmUnit.box.y) + alarmUnit.box.height + kCropPadding);
156+
const int64_t cropW = x1 - x0;
157+
const int64_t cropH = y1 - y0;
154158
VideoFramePtr inputFrame = srcFrame;
155159
if (cropW > 0 && cropH > 0 && (cropW < imgW || cropH < imgH)) {
156-
util::Box roi(x0, y0, cropW, cropH);
160+
util::Box roi(static_cast<int>(x0), static_cast<int>(y0), static_cast<int>(cropW),
161+
static_cast<int>(cropH));
157162
auto cropped =
158163
service::ServiceRegistry::Instance().Get<service::IVideoFrameTransform>().Crop(srcFrame, roi);
159164
if (VideoFrameValid(cropped))

src/flow/alarm/TaskAlarmPicture.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void TaskAlarm::HandBestInfoPicture(CMsgOnEventsReq& msg, AlgDataPtr /*algData*/
7070
scaleParam.scale_side = m_param.faceScaleParam;
7171
roi = DoScaleBox(bestInfo.box, scaleParam, m_width, m_height);
7272

73-
if (roi.width * roi.height > 0) {
73+
if (!roi.empty()) {
7474
auto cutImg = service::ServiceRegistry::Instance().Get<service::IVideoFrameTransform>().Crop(
7575
bestInfo.bestFrame, roi);
7676
auto cutJpeg =
@@ -171,7 +171,7 @@ void TaskAlarm::HandPicture(CMsgOnEventsReq& msg, AlgDataPtr algData, DataAlarmU
171171
auto timpointCrop = std::chrono::high_resolution_clock::now();
172172
auto timpointcutJpeg = std::chrono::high_resolution_clock::now();
173173
#endif
174-
if (roi.width * roi.height > 0) {
174+
if (!roi.empty()) {
175175
auto cutImg =
176176
service::ServiceRegistry::Instance().Get<service::IVideoFrameTransform>().Crop(origImg, roi);
177177
#ifdef DURATION_LOG

src/flow/alarm/TaskFaceAlarm.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void TaskFaceAlarm::HandPicture(CMsgFaceEventReq& msg, AlgDataPtr algData, DataA
275275
}
276276
LOG_INFO("{}.{} {}x{} {}x{} BoxCount:{}", alarmUnit.box.x, alarmUnit.box.y, alarmUnit.box.width,
277277
alarmUnit.box.height, cropBox.width, cropBox.height, alarmUnit.boxs.size());
278-
if (cropBox.width * cropBox.height > 0) {
278+
if (!cropBox.empty()) {
279279
auto cutImg =
280280
service::ServiceRegistry::Instance().Get<service::IVideoFrameTransform>().Crop(origImg, cropBox);
281281
auto cutJpeg =

src/flow/qwen3vl/Qwen3VLInference.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Split from Qwen3VLWorker.cc to reduce file size (DEBT-007).
33

44
#include <algorithm>
5+
#include <cstdint>
56
#include <map>
67
#include <set>
78
#include <sstream>
@@ -94,15 +95,26 @@ namespace {
9495
const auto& box = target.box;
9596
if (box.width <= 0 || box.height <= 0)
9697
continue;
97-
int expand_w = static_cast<int>(box.width * 0.2f);
98-
int expand_h = static_cast<int>(box.height * 0.2f);
99-
util::Box roi;
100-
roi.x = std::max(0, box.x - expand_w);
101-
roi.y = std::max(0, box.y - expand_h);
102-
roi.width = std::min(img_w - roi.x, box.width + 2 * expand_w);
103-
roi.height = std::min(img_h - roi.y, box.height + 2 * expand_h);
104-
if (roi.width <= 0 || roi.height <= 0)
98+
const int64_t expand_w = static_cast<int64_t>(box.width) / 5;
99+
const int64_t expand_h = static_cast<int64_t>(box.height) / 5;
100+
const int64_t roi_x = std::max<int64_t>(0, static_cast<int64_t>(box.x) - expand_w);
101+
const int64_t roi_y = std::max<int64_t>(0, static_cast<int64_t>(box.y) - expand_h);
102+
if (roi_x >= img_w || roi_y >= img_h) {
105103
continue;
104+
}
105+
106+
const int64_t expanded_width = static_cast<int64_t>(box.width) + 2 * expand_w;
107+
const int64_t expanded_height = static_cast<int64_t>(box.height) + 2 * expand_h;
108+
const int64_t roi_width =
109+
std::min<int64_t>(static_cast<int64_t>(img_w) - roi_x, expanded_width);
110+
const int64_t roi_height =
111+
std::min<int64_t>(static_cast<int64_t>(img_h) - roi_y, expanded_height);
112+
if (roi_width <= 0 || roi_height <= 0) {
113+
continue;
114+
}
115+
116+
util::Box roi{static_cast<int>(roi_x), static_cast<int>(roi_y), static_cast<int>(roi_width),
117+
static_cast<int>(roi_height)};
106118
auto cropped = service::ServiceRegistry::Instance().Get<service::IVideoFrameTransform>().Crop(
107119
srcFrame, roi);
108120
if (VideoFrameValid(cropped)) {

src/media/PixelFormatUtils.cc

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,76 @@
22

33
#include "media/PixelFormatUtils.h"
44

5+
#include <algorithm>
56
#include <cstdint>
7+
#include <limits>
8+
9+
#include "util/VideoInfo.h"
610

711
namespace cosmo {
812
namespace media {
913

14+
namespace {
15+
16+
int GetWidthAlignment(PixelFormat format) {
17+
switch (format) {
18+
case PixelFormat::PIXEL_YUYV:
19+
case PixelFormat::PIXEL_YVYU:
20+
case PixelFormat::PIXEL_UYVY:
21+
case PixelFormat::PIXEL_I420:
22+
case PixelFormat::PIXEL_NV12:
23+
case PixelFormat::PIXEL_NV21:
24+
return 2;
25+
default:
26+
return 1;
27+
}
28+
}
29+
30+
int GetHeightAlignment(PixelFormat format) {
31+
switch (format) {
32+
case PixelFormat::PIXEL_I420:
33+
case PixelFormat::PIXEL_NV12:
34+
case PixelFormat::PIXEL_NV21:
35+
return 2;
36+
default:
37+
return 1;
38+
}
39+
}
40+
41+
bool ExpandAndAlignSpan(int64_t* start, int64_t* end, int64_t limit, int min_dimension,
42+
int max_dimension, int alignment) {
43+
const auto current_length = *end - *start;
44+
if (current_length <= 0) {
45+
return false;
46+
}
47+
48+
int64_t target_length = std::max<int64_t>(current_length, min_dimension);
49+
const auto remainder = target_length % alignment;
50+
if (remainder != 0) {
51+
target_length += alignment - remainder;
52+
}
53+
if (target_length > max_dimension || target_length > limit) {
54+
return false;
55+
}
56+
57+
auto remaining_growth = target_length - current_length;
58+
const auto desired_left_growth = remaining_growth / 2;
59+
const auto grow_left = std::min(desired_left_growth, *start);
60+
*start -= grow_left;
61+
remaining_growth -= grow_left;
62+
63+
const auto grow_right = std::min(remaining_growth, limit - *end);
64+
*end += grow_right;
65+
remaining_growth -= grow_right;
66+
67+
const auto extra_left_growth = std::min(remaining_growth, *start);
68+
*start -= extra_left_growth;
69+
remaining_growth -= extra_left_growth;
70+
return remaining_growth == 0;
71+
}
72+
73+
} // namespace
74+
1075
PixelBaseType PixelFormatUtils::PixelFormatBaseType(PixelFormat format) {
1176
switch (format) {
1277
case PixelFormat::PIXEL_BGR32F:
@@ -83,6 +148,95 @@ namespace media {
83148
return 0;
84149
}
85150

151+
std::optional<size_t> PixelFormatUtils::CalculateFrameSize(int width, int height, PixelFormat format) {
152+
if (width <= 0 || height <= 0) {
153+
return std::nullopt;
154+
}
155+
156+
switch (format) {
157+
case PixelFormat::PIXEL_I420:
158+
case PixelFormat::PIXEL_NV12:
159+
case PixelFormat::PIXEL_NV21:
160+
if ((width % 2) != 0 || (height % 2) != 0) {
161+
return std::nullopt;
162+
}
163+
break;
164+
case PixelFormat::PIXEL_YUYV:
165+
case PixelFormat::PIXEL_YVYU:
166+
case PixelFormat::PIXEL_UYVY:
167+
if ((width % 2) != 0) {
168+
return std::nullopt;
169+
}
170+
break;
171+
default:
172+
break;
173+
}
174+
175+
const auto depth = PixelFormatDepth(format);
176+
if (depth == 0) {
177+
return std::nullopt;
178+
}
179+
180+
const auto width_size = static_cast<size_t>(width);
181+
const auto height_size = static_cast<size_t>(height);
182+
if (width_size > std::numeric_limits<size_t>::max() / height_size) {
183+
return std::nullopt;
184+
}
185+
const auto pixel_count = width_size * height_size;
186+
187+
if (pixel_count > std::numeric_limits<size_t>::max() / depth) {
188+
return std::nullopt;
189+
}
190+
const auto bit_count = pixel_count * depth;
191+
constexpr size_t kBitsPerByte = 8;
192+
if ((bit_count % kBitsPerByte) != 0) {
193+
return std::nullopt;
194+
}
195+
196+
const auto frame_size = bit_count / kBitsPerByte;
197+
if (frame_size == 0 || frame_size > static_cast<size_t>(kVideoFrameMaxSize)) {
198+
return std::nullopt;
199+
}
200+
return frame_size;
201+
}
202+
203+
std::optional<util::Box> PixelFormatUtils::NormalizeCropRoi(int source_width, int source_height,
204+
PixelFormat format,
205+
const util::Box& requested_roi,
206+
int min_dimension, int max_dimension) {
207+
if (!CalculateFrameSize(source_width, source_height, format) || min_dimension <= 0 ||
208+
max_dimension < min_dimension || requested_roi.width <= 0 || requested_roi.height <= 0 ||
209+
requested_roi.width > max_dimension || requested_roi.height > max_dimension) {
210+
return std::nullopt;
211+
}
212+
213+
const auto source_width_i64 = static_cast<int64_t>(source_width);
214+
const auto source_height_i64 = static_cast<int64_t>(source_height);
215+
int64_t left = std::max<int64_t>(0, requested_roi.x);
216+
int64_t top = std::max<int64_t>(0, requested_roi.y);
217+
int64_t right = std::min<int64_t>(source_width_i64, static_cast<int64_t>(requested_roi.x) +
218+
static_cast<int64_t>(requested_roi.width));
219+
int64_t bottom = std::min<int64_t>(source_height_i64, static_cast<int64_t>(requested_roi.y) +
220+
static_cast<int64_t>(requested_roi.height));
221+
if (left >= right || top >= bottom) {
222+
return std::nullopt;
223+
}
224+
225+
if (!ExpandAndAlignSpan(&left, &right, source_width_i64, min_dimension, max_dimension,
226+
GetWidthAlignment(format)) ||
227+
!ExpandAndAlignSpan(&top, &bottom, source_height_i64, min_dimension, max_dimension,
228+
GetHeightAlignment(format))) {
229+
return std::nullopt;
230+
}
231+
232+
util::Box normalized_roi{static_cast<int>(left), static_cast<int>(top),
233+
static_cast<int>(right - left), static_cast<int>(bottom - top)};
234+
if (!CalculateFrameSize(normalized_roi.width, normalized_roi.height, format)) {
235+
return std::nullopt;
236+
}
237+
return normalized_roi;
238+
}
239+
86240
bool PixelFormatUtils::PixelFormatIsRGB(PixelFormat f) {
87241
if (f >= PixelFormat::PIXEL_RGB8 && f <= PixelFormat::PIXEL_RGBA32F)
88242
return true;
@@ -105,4 +259,4 @@ namespace media {
105259
}
106260

107261
} // namespace media
108-
} // namespace cosmo
262+
} // namespace cosmo

src/media/PixelFormatUtils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

33
#include <cstddef>
4+
#include <optional>
45

56
#include "media/PixelFormat.h"
7+
#include "util/Rect.h"
68

79
namespace cosmo {
810
namespace media {
@@ -16,6 +18,14 @@ namespace media {
1618

1719
static size_t PixelFormatDepth(PixelFormat);
1820

21+
[[nodiscard]] static std::optional<size_t> CalculateFrameSize(int width, int height,
22+
PixelFormat format);
23+
24+
[[nodiscard]] static std::optional<util::Box> NormalizeCropRoi(int source_width, int source_height,
25+
PixelFormat format,
26+
const util::Box& requested_roi,
27+
int min_dimension, int max_dimension);
28+
1929
static bool PixelFormatIsRGB(PixelFormat);
2030

2131
static bool PixelFormatIsBGR(PixelFormat);

src/media/VideoFrame.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ namespace cosmo {
1414
namespace media {
1515

1616
VideoFrame::VideoFrame(int w, int h, PixelFormat fmt, uint64_t frameIndex, int64_t ts) {
17-
pixel_fmt_ = fmt;
18-
width_ = static_cast<size_t>(w);
19-
height_ = static_cast<size_t>(h);
20-
channel_ = PixelFormatUtils::PixelFormatChannels(fmt);
21-
size_ = static_cast<size_t>(w) * static_cast<size_t>(h) * PixelFormatUtils::PixelFormatDepth(fmt) / 8;
22-
if (size_ == 0) {
17+
pixel_fmt_ = fmt;
18+
const auto frame_size = PixelFormatUtils::CalculateFrameSize(w, h, fmt);
19+
if (!frame_size) {
20+
LOG_WARN("Invalid frame dimensions or format: {}x{}, format {}", w, h, static_cast<int>(fmt));
2321
return;
2422
}
2523

24+
width_ = static_cast<size_t>(w);
25+
height_ = static_cast<size_t>(h);
26+
channel_ = PixelFormatUtils::PixelFormatChannels(fmt);
27+
size_ = *frame_size;
28+
2629
int index = 0;
2730
while (index++ <= 10) {
2831
auto block__ = mem::GetMemoryPool().Acquire(size_);

0 commit comments

Comments
 (0)