Skip to content

Commit 76b9375

Browse files
committed
Release v2024.2.1
1 parent 350a6a4 commit 76b9375

File tree

40 files changed

+1045
-172
lines changed

40 files changed

+1045
-172
lines changed

include/dlstreamer/gst/context.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ class GSTContextQuery : public BaseContext {
8484
return VA_CONTEXT_NAME;
8585
}
8686

87-
if (memory_type == MemoryType::VAAPI)
87+
if (memory_type == MemoryType::VAAPI) {
88+
GST_ELEMENT_WARNING(_context, LIBRARY, INIT, ("VASurface and Gst-VAAPI is deprecated."),
89+
("%s", "Please use VAMemory na Gst-VA instead."));
8890
return VAAPI_CONTEXT_NAME;
89-
else
91+
} else
9092
return memory_type_to_string(memory_type);
9193
}
9294

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2018-2024 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
******************************************************************************/
6+
7+
#pragma once
8+
9+
#include <gst/analytics/analytics-meta-prelude.h>
10+
#include <gst/analytics/gstanalyticsmeta.h>
11+
#include <gst/gst.h>
12+
13+
inline const bool NEW_METADATA = false;
14+
15+
typedef struct _GstAnalyticsMtd GstAnalyticsODExtMtd;
16+
17+
typedef struct _GstAnalyticsODExtMtdData GstAnalyticsODExtMtdData;
18+
19+
struct _GstAnalyticsODExtMtdData {
20+
gint class_id;
21+
gdouble rotation;
22+
GList *params;
23+
};
24+
25+
inline const GstAnalyticsMtdImpl od_ext_impl = {"object-detection-extended", NULL, {NULL}};
26+
27+
inline GstAnalyticsMtdType gst_analytics_od_ext_mtd_get_mtd_type(void) {
28+
return (GstAnalyticsMtdType)&od_ext_impl;
29+
}
30+
31+
inline gboolean gst_analytics_od_ext_mtd_get_rotation(const GstAnalyticsODExtMtd *handle, gdouble *rotation) {
32+
GstAnalyticsODExtMtdData *data;
33+
34+
g_return_val_if_fail(handle && rotation, false);
35+
data = (GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_get_mtd_data(handle->meta, handle->id);
36+
g_return_val_if_fail(data, false);
37+
38+
*rotation = data->rotation;
39+
40+
return TRUE;
41+
}
42+
43+
inline gboolean gst_analytics_od_ext_mtd_get_class_id(const GstAnalyticsODExtMtd *handle, gint *class_id) {
44+
GstAnalyticsODExtMtdData *data;
45+
46+
g_return_val_if_fail(handle && class_id, false);
47+
data = (GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_get_mtd_data(handle->meta, handle->id);
48+
g_return_val_if_fail(data, false);
49+
50+
*class_id = data->class_id;
51+
52+
return TRUE;
53+
}
54+
55+
inline GList *gst_analytics_od_ext_mtd_get_params(const GstAnalyticsODExtMtd *handle) {
56+
GstAnalyticsODExtMtdData *data;
57+
g_return_val_if_fail(handle, nullptr);
58+
data = (GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_get_mtd_data(handle->meta, handle->id);
59+
g_return_val_if_fail(data, nullptr);
60+
return data->params;
61+
}
62+
63+
inline gboolean gst_analytics_od_ext_mtd_add_param(const GstAnalyticsODExtMtd *handle, GstStructure *s) {
64+
g_return_val_if_fail(handle, false);
65+
g_return_val_if_fail(s, false);
66+
67+
GstAnalyticsODExtMtdData *data;
68+
data = (GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_get_mtd_data(handle->meta, handle->id);
69+
if (data) {
70+
data->params = g_list_append(data->params, s);
71+
}
72+
return data != nullptr;
73+
}
74+
75+
inline GstStructure *gst_analytics_od_ext_mtd_get_param(const GstAnalyticsODExtMtd *handle, const gchar *name) {
76+
g_return_val_if_fail(handle, nullptr);
77+
g_return_val_if_fail(name, nullptr);
78+
79+
GList *l;
80+
GstAnalyticsODExtMtdData *data;
81+
data = (GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_get_mtd_data(handle->meta, handle->id);
82+
g_return_val_if_fail(data, nullptr);
83+
84+
for (l = data->params; l; l = g_list_next(l)) {
85+
GstStructure *s = (GstStructure *)l->data;
86+
87+
if (gst_structure_has_name(s, name))
88+
return s;
89+
}
90+
91+
return nullptr;
92+
}
93+
94+
inline gboolean gst_analytics_relation_meta_add_od_ext_mtd(GstAnalyticsRelationMeta *instance, gdouble rotation,
95+
gint class_id, GstAnalyticsODExtMtd *od_ext_mtd) {
96+
g_return_val_if_fail(instance, false);
97+
gsize size = sizeof(GstAnalyticsODExtMtdData);
98+
GstAnalyticsODExtMtdData *od_ext_mtd_data =
99+
(GstAnalyticsODExtMtdData *)gst_analytics_relation_meta_add_mtd(instance, &od_ext_impl, size, od_ext_mtd);
100+
if (od_ext_mtd_data) {
101+
od_ext_mtd_data->rotation = rotation;
102+
od_ext_mtd_data->class_id = class_id;
103+
od_ext_mtd_data->params = nullptr;
104+
}
105+
return od_ext_mtd_data != nullptr;
106+
}
107+
108+
inline gboolean gst_analytics_relation_meta_get_od_ext_mtd(GstAnalyticsRelationMeta *meta, gint an_meta_id,
109+
GstAnalyticsODExtMtd *rlt) {
110+
return gst_analytics_relation_meta_get_mtd(meta, an_meta_id, gst_analytics_od_ext_mtd_get_mtd_type(),
111+
(GstAnalyticsODExtMtd *)rlt);
112+
}

include/dlstreamer/gst/videoanalytics/region_of_interest.h

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#pragma once
1414

15+
#include "objectdetectionmtdext.h"
1516
#include "tensor.h"
1617

1718
#include <cstdint>
@@ -69,7 +70,6 @@ class RegionOfInterest {
6970
* @return Bounding box coordinates of the RegionOfInterest
7071
*/
7172
Rect<double> normalized_rect() {
72-
assert(_gst_meta != nullptr);
7373
Tensor det = detection();
7474
return {det.get_double("x_min"), det.get_double("y_min"), det.get_double("x_max") - det.get_double("x_min"),
7575
det.get_double("y_max") - det.get_double("y_min")};
@@ -80,7 +80,15 @@ class RegionOfInterest {
8080
* @return Bounding box rotation of the RegionOfInterest
8181
*/
8282
double rotation() const {
83-
return _detection ? _detection->get_double("rotation", 0.0) : 0.0;
83+
if (_gst_meta) {
84+
return _detection ? _detection->get_double("rotation", 0.0) : 0.0;
85+
}
86+
87+
gdouble rotation;
88+
if (!gst_analytics_od_ext_mtd_get_rotation(&_od_ext_meta, &rotation)) {
89+
throw std::runtime_error("Error when trying to read the rotation of the RegionOfInterest");
90+
}
91+
return rotation;
8492
}
8593

8694
/**
@@ -118,17 +126,17 @@ class RegionOfInterest {
118126
* @return Unique id, or zero value if not found
119127
*/
120128
int32_t object_id() const {
129+
GstStructure *object_id_struct = nullptr;
121130
if (_gst_meta) {
122-
GstStructure *object_id_struct = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
123-
if (!object_id_struct)
124-
return 0;
125-
int id = 0;
126-
gst_structure_get_int(object_id_struct, "id", &id);
127-
return id;
131+
object_id_struct = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
132+
} else {
133+
object_id_struct = gst_analytics_od_ext_mtd_get_param(&_od_ext_meta, "object_id");
128134
}
129-
130-
// TODO - where do we use object_id?
131-
return 0;
135+
if (!object_id_struct)
136+
return 0;
137+
int id = 0;
138+
gst_structure_get_int(object_id_struct, "id", &id);
139+
return id;
132140
}
133141

134142
/**
@@ -148,7 +156,11 @@ class RegionOfInterest {
148156
*/
149157
Tensor add_tensor(const std::string &name) {
150158
GstStructure *tensor = gst_structure_new_empty(name.c_str());
151-
gst_video_region_of_interest_meta_add_param(_gst_meta, tensor);
159+
if (_gst_meta) {
160+
gst_video_region_of_interest_meta_add_param(_gst_meta, tensor);
161+
} else {
162+
gst_analytics_od_ext_mtd_add_param(&_od_ext_meta, tensor);
163+
}
152164
_tensors.emplace_back(tensor);
153165
if (_tensors.back().is_detection())
154166
_detection = &_tensors.back();
@@ -166,7 +178,7 @@ class RegionOfInterest {
166178
* this method was called
167179
*/
168180
Tensor detection() {
169-
if (_gst_meta && !_detection) {
181+
if (!_detection) {
170182
add_tensor("detection");
171183
}
172184
return _detection ? *_detection : nullptr;
@@ -177,7 +189,14 @@ class RegionOfInterest {
177189
* @return last added detection Tensor label_id if exists, otherwise 0
178190
*/
179191
int label_id() const {
180-
return _detection ? _detection->label_id() : 0;
192+
if (_gst_meta) {
193+
return _detection ? _detection->label_id() : 0;
194+
}
195+
gint label_id;
196+
if (!gst_analytics_od_ext_mtd_get_class_id(&_od_ext_meta, &label_id)) {
197+
throw std::runtime_error("Error when trying to read the label id of the RegionOfInterest");
198+
}
199+
return label_id;
181200
}
182201

183202
/**
@@ -201,7 +220,20 @@ class RegionOfInterest {
201220
}
202221
}
203222

204-
RegionOfInterest(GstAnalyticsODMtd meta) : _gst_meta(nullptr), _detection(nullptr), _od_meta(meta) {
223+
RegionOfInterest(GstAnalyticsODMtd od_meta, GstAnalyticsODExtMtd od_ext_meta)
224+
: _gst_meta(nullptr), _detection(nullptr), _od_meta(od_meta), _od_ext_meta(od_ext_meta) {
225+
GList *params = gst_analytics_od_ext_mtd_get_params(&od_ext_meta);
226+
227+
_tensors.reserve(g_list_length(params));
228+
229+
for (GList *l = params; l; l = g_list_next(l)) {
230+
GstStructure *s = GST_STRUCTURE(l->data);
231+
if (not gst_structure_has_name(s, "object_id")) {
232+
_tensors.emplace_back(s);
233+
if (_tensors.back().is_detection())
234+
_detection = &_tensors.back();
235+
}
236+
}
205237
}
206238

207239
/**
@@ -231,14 +263,45 @@ class RegionOfInterest {
231263
* @param id ID to set
232264
*/
233265
void set_object_id(int32_t id) {
234-
assert(_gst_meta != nullptr);
235-
GstStructure *object_id = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
266+
if (_gst_meta) {
267+
GstStructure *object_id = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
268+
if (object_id) {
269+
gst_structure_set(object_id, "id", G_TYPE_INT, id, NULL);
270+
} else {
271+
object_id = gst_structure_new("object_id", "id", G_TYPE_INT, id, NULL);
272+
gst_video_region_of_interest_meta_add_param(_gst_meta, object_id);
273+
}
274+
return;
275+
}
276+
GstStructure *object_id = gst_analytics_od_ext_mtd_get_param(&_od_ext_meta, "object_id");
236277
if (object_id) {
237278
gst_structure_set(object_id, "id", G_TYPE_INT, id, NULL);
238279
} else {
239280
object_id = gst_structure_new("object_id", "id", G_TYPE_INT, id, NULL);
240-
gst_video_region_of_interest_meta_add_param(_gst_meta, object_id);
281+
gst_analytics_od_ext_mtd_add_param(&_od_ext_meta, object_id);
282+
}
283+
}
284+
285+
GList *get_params() const {
286+
if (_gst_meta) {
287+
return _gst_meta->params;
288+
}
289+
return gst_analytics_od_ext_mtd_get_params(&_od_ext_meta);
290+
}
291+
292+
GstStructure *get_param(const char *name) const {
293+
if (_gst_meta) {
294+
return gst_video_region_of_interest_meta_get_param(_gst_meta, name);
295+
}
296+
return gst_analytics_od_ext_mtd_get_param(&_od_ext_meta, name);
297+
}
298+
299+
void add_param(GstStructure *s) {
300+
if (_gst_meta) {
301+
gst_video_region_of_interest_meta_add_param(_gst_meta, s);
302+
return;
241303
}
304+
gst_analytics_od_ext_mtd_add_param(&_od_ext_meta, s);
242305
}
243306

244307
/**
@@ -272,6 +335,7 @@ class RegionOfInterest {
272335
* that region of interest.
273336
*/
274337
GstAnalyticsODMtd _od_meta;
338+
GstAnalyticsODExtMtd _od_ext_meta;
275339
};
276340

277341
} // namespace GVA

include/dlstreamer/gst/videoanalytics/video_frame.h

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#pragma once
1414

15+
#include "objectdetectionmtdext.h"
1516
#include "region_of_interest.h"
1617

1718
#include "../metadata/gva_json_meta.h"
@@ -214,20 +215,51 @@ class VideoFrame {
214215
if (!gst_buffer_is_writable(buffer))
215216
throw std::runtime_error("Buffer is not writable.");
216217

217-
GstVideoRegionOfInterestMeta *meta = gst_buffer_add_video_region_of_interest_meta(
218-
buffer, label.c_str(), double_to_uint(_x), double_to_uint(_y), double_to_uint(_w), double_to_uint(_h));
219-
meta->id = gst_util_seqnum_next();
220-
221-
// Add detection tensor
222218
GstStructure *detection =
223219
gst_structure_new("detection", "x_min", G_TYPE_DOUBLE, x, "x_max", G_TYPE_DOUBLE, x + w, "y_min",
224220
G_TYPE_DOUBLE, y, "y_max", G_TYPE_DOUBLE, y + h, NULL);
221+
225222
if (confidence) {
226223
gst_structure_set(detection, "confidence", G_TYPE_DOUBLE, confidence, NULL);
227224
}
228-
gst_video_region_of_interest_meta_add_param(meta, detection);
229225

230-
return RegionOfInterest(meta);
226+
if (NEW_METADATA) {
227+
GstAnalyticsRelationMeta *relation_meta = gst_buffer_add_analytics_relation_meta(buffer);
228+
229+
if (!relation_meta) {
230+
throw std::runtime_error("Failed to add GstAnalyticsRelationMeta to buffer");
231+
}
232+
233+
GstAnalyticsODMtd od_mtd;
234+
if (!gst_analytics_relation_meta_add_od_mtd(relation_meta, g_quark_from_string(label.c_str()),
235+
double_to_int(_x), double_to_int(_y), double_to_int(_w),
236+
double_to_int(_h), confidence, &od_mtd)) {
237+
throw std::runtime_error("Failed to add detection data to meta");
238+
}
239+
240+
GstAnalyticsODExtMtd od_ext_mtd;
241+
if (!gst_analytics_relation_meta_add_od_ext_mtd(relation_meta, 0, 0, &od_ext_mtd)) {
242+
throw std::runtime_error("Failed to add detection extended data to meta");
243+
}
244+
245+
gst_analytics_od_ext_mtd_add_param(&od_ext_mtd, detection);
246+
247+
if (!gst_analytics_relation_meta_set_relation(relation_meta, GST_ANALYTICS_REL_TYPE_RELATE_TO, od_mtd.id,
248+
od_ext_mtd.id)) {
249+
throw std::runtime_error(
250+
"Failed to set relation between object detection metadata and extended metadata");
251+
}
252+
253+
return RegionOfInterest(od_mtd, od_ext_mtd);
254+
} else {
255+
GstVideoRegionOfInterestMeta *meta = gst_buffer_add_video_region_of_interest_meta(
256+
buffer, label.c_str(), double_to_uint(_x), double_to_uint(_y), double_to_uint(_w), double_to_uint(_h));
257+
meta->id = gst_util_seqnum_next();
258+
259+
gst_video_region_of_interest_meta_add_param(meta, detection);
260+
261+
return RegionOfInterest(meta);
262+
}
231263
}
232264

233265
/**
@@ -311,6 +343,12 @@ class VideoFrame {
311343
return (val < min) ? min : ((val > max) ? max : static_cast<unsigned int>(val));
312344
}
313345

346+
int double_to_int(double val) {
347+
int max = std::numeric_limits<int>::max();
348+
int min = std::numeric_limits<int>::min();
349+
return (val < min) ? min : ((val > max) ? max : static_cast<int>(val));
350+
}
351+
314352
std::vector<RegionOfInterest> get_regions() const {
315353
std::vector<RegionOfInterest> regions;
316354
gpointer state = NULL;
@@ -322,7 +360,14 @@ class VideoFrame {
322360
GstAnalyticsODMtd od_meta;
323361
while (gst_analytics_relation_meta_iterate(relation_meta, &state, gst_analytics_od_mtd_get_mtd_type(),
324362
&od_meta)) {
325-
regions.emplace_back(od_meta);
363+
GstAnalyticsODExtMtd od_ext_meta;
364+
if (!gst_analytics_relation_meta_get_direct_related(
365+
relation_meta, od_meta.id, GST_ANALYTICS_REL_TYPE_RELATE_TO, GST_ANALYTICS_MTD_TYPE_ANY,
366+
nullptr, &od_ext_meta)) {
367+
throw std::runtime_error("Object detection extended metadata not found");
368+
}
369+
370+
regions.emplace_back(od_meta, od_ext_meta);
326371
}
327372
return regions;
328373
}

0 commit comments

Comments
 (0)