NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _GST_NVDSUDP_COMMON_H_
19 #define _GST_NVDSUDP_COMMON_H_
20 
21 #include <gst/gst.h>
22 #include <gio/gio.h>
23 #include <cuda.h>
24 #include <cuda_runtime.h>
25 #include <string.h>
26 
27 #define CHECK_CUDA(err, err_str) \
28  do { \
29  if ((err) != cudaSuccess) { \
30  GST_WARNING (err_str ", cuda err: %s", cudaGetErrorName (err)); \
31  return FALSE; \
32  } \
33  } while (0)
34 
35 #define INVALID_STREAM_ID ((rmx_stream_id)-1L)
36 #define MAX_ST2022_7_STREAMS 2
37 
38 #define round_up(num, round) \
39  ((((num) + (round) - 1) / (round)) * (round))
40 
41 typedef enum StreamType {
47 } StreamType;
48 
49 typedef enum VideoType {
52 } VideoType;
53 
54 typedef enum MemoryType {
59 } MemoryType;
60 
61 gboolean
62 gst_udp_parse_uri (const gchar * uristr, gchar ** host, guint16 * port);
63 
64 GInetAddress *
65 gst_udp_resolve_name (gpointer obj, const gchar * address);
66 
67 #define LEAP_SECONDS (37)
68 #define DEFAULT_PTP_SRC NULL
69 
70 // Define metadata structure
72 
73 struct _GstRTPTimestampMeta {
74  GstMeta meta;
75  guint32 rtp_timestamp;
76  gboolean leap_seconds_adjusted;
77 };
78 
79 // Declare functions
81 const GstMetaInfo *gst_rtp_timestamp_meta_get_info(void);
82 
83 // Helper functions for adding/getting metadata
86 
87 // GPU memory allocation related functions
88 void* gpu_allocate_memory (int gpuId, size_t size, size_t align);
89 size_t gpu_aligned_size (int gpuId, size_t allocSize);
90 gboolean cudaFreeMmap (uint64_t *ptr, size_t size);
91 
92 /*
93  * RFC 8331 / ST 2110-40 ancillary data constants
94  */
95 #define RTP_HEADER_SIZE (12)
96 #define ST2110_40_CLOCK_RATE (90000)
97 #define RFC8331_PAYLOAD_HDR_SIZE (8)
98 #define RTP_ST2110_40_HEADER_SIZE (RTP_HEADER_SIZE + RFC8331_PAYLOAD_HDR_SIZE)
99 #define ANC_MAX_DATA_COUNT (255)
100 #define ANC_MAX_PACKET_SIZE (328)
101 
102 typedef struct AncPacketInfo {
103  gboolean c_not_y_channel_flag;
104  guint16 line_number;
105  guint16 horizontal_offset;
106  guint16 did;
107  guint16 sdid;
108  guint8 data_count;
109  guint16 user_data[ANC_MAX_DATA_COUNT];
110  guint16 checksum;
111 } AncPacketInfo;
112 
113 /*
114  * Bit-level I/O helpers for RFC 8331 bit-packed ANC data
115  */
116 typedef struct {
117  const guint8 *data;
118  gsize byte_len;
119  gsize bit_pos;
120 } BitReader;
121 
122 typedef struct {
123  guint8 *data;
124  gsize byte_len;
125  gsize bit_pos;
126 } BitWriter;
127 
128 static inline void
129 bit_reader_init (BitReader *br, const guint8 *data, gsize byte_len)
130 {
131  br->data = data;
132  br->byte_len = byte_len;
133  br->bit_pos = 0;
134 }
135 
136 static inline gboolean
137 bit_reader_has_bits (const BitReader *br, guint n)
138 {
139  return (br->bit_pos + n) <= (br->byte_len * 8);
140 }
141 
142 static inline guint32
144 {
145  guint32 val = 0;
146  for (guint i = 0; i < n; i++) {
147  gsize byte_idx = br->bit_pos / 8;
148  guint bit_idx = 7 - (br->bit_pos % 8);
149  val = (val << 1) | ((br->data[byte_idx] >> bit_idx) & 1);
150  br->bit_pos++;
151  }
152  return val;
153 }
154 
155 static inline gboolean
157 {
158  return (br->bit_pos % 8) == 0;
159 }
160 
161 static inline gsize
163 {
164  return (br->bit_pos + 7) / 8;
165 }
166 
167 static inline void
168 bit_writer_init (BitWriter *bw, guint8 *data, gsize byte_len)
169 {
170  bw->data = data;
171  bw->byte_len = byte_len;
172  bw->bit_pos = 0;
173  memset (data, 0, byte_len);
174 }
175 
176 static inline void
177 bit_writer_init_at (BitWriter *bw, guint8 *data, gsize byte_len, gsize start_bit)
178 {
179  bw->data = data;
180  bw->byte_len = byte_len;
181  bw->bit_pos = start_bit;
182 }
183 
184 static inline gboolean
185 bit_writer_has_space (const BitWriter *bw, guint n)
186 {
187  return (bw->bit_pos + n) <= (bw->byte_len * 8);
188 }
189 
190 static inline void
191 bit_writer_write (BitWriter *bw, guint n, guint32 val)
192 {
193  for (guint i = 0; i < n; i++) {
194  gsize byte_idx = bw->bit_pos / 8;
195  guint bit_idx = 7 - (bw->bit_pos % 8);
196  if (val & (1u << (n - 1 - i)))
197  bw->data[byte_idx] |= (1u << bit_idx);
198  bw->bit_pos++;
199  }
200 }
201 
202 static inline gboolean
204 {
205  return (bw->bit_pos % 8) == 0;
206 }
207 
208 static inline gsize
210 {
211  return (bw->bit_pos + 7) / 8;
212 }
213 
214 static inline void
215 bit_writer_pad_to_byte (BitWriter *bw, guint8 pad_val)
216 {
217  while (!bit_writer_is_byte_aligned (bw)) {
218  bit_writer_write (bw, 1, pad_val);
219  }
220 }
221 
222 static inline void
224 {
225  while (bw->bit_pos % 32 != 0) {
226  bit_writer_write (bw, 1, 0);
227  }
228 }
229 
230 #endif
MEM_TYPE_SYSTEM
@ MEM_TYPE_SYSTEM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:57
BitReader
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:116
MEM_TYPE_HOST
@ MEM_TYPE_HOST
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:55
bit_writer_byte_pos
static gsize bit_writer_byte_pos(const BitWriter *bw)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:209
VideoType
VideoType
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:49
bit_reader_has_bits
static gboolean bit_reader_has_bits(const BitReader *br, guint n)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:137
BitWriter::bit_pos
gsize bit_pos
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:125
AUDIO_2110_30_31_STREAM
@ AUDIO_2110_30_31_STREAM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:44
bit_writer_is_byte_aligned
static gboolean bit_writer_is_byte_aligned(const BitWriter *bw)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:203
VIDEO_2110_22_STREAM
@ VIDEO_2110_22_STREAM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:43
ANCILLARY_2110_40_STREAM
@ ANCILLARY_2110_40_STREAM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:45
bit_writer_write
static void bit_writer_write(BitWriter *bw, guint n, guint32 val)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:191
bit_writer_pad_to_u32
static void bit_writer_pad_to_u32(BitWriter *bw)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:223
bit_reader_read
static guint32 bit_reader_read(BitReader *br, guint n)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:143
StreamType
StreamType
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:41
AncPacketInfo
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:102
AncPacketInfo::data_count
guint8 data_count
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:108
VideoType
VideoType
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:49
AncPacketInfo::checksum
guint16 checksum
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:110
_GstRTPTimestampMeta::leap_seconds_adjusted
gboolean leap_seconds_adjusted
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:76
_GstRTPTimestampMeta::meta
GstMeta meta
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:74
MemoryType
MemoryType
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:54
MEM_TYPE_UNKNOWN
@ MEM_TYPE_UNKNOWN
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:58
MemoryType
MemoryType
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:54
PROGRESSIVE
@ PROGRESSIVE
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:50
AncPacketInfo::c_not_y_channel_flag
gboolean c_not_y_channel_flag
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:103
APPLICATION_CUSTOM_STREAM
@ APPLICATION_CUSTOM_STREAM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:46
bit_reader_is_byte_aligned
static gboolean bit_reader_is_byte_aligned(const BitReader *br)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:156
AncPacketInfo
struct AncPacketInfo AncPacketInfo
bit_writer_has_space
static gboolean bit_writer_has_space(const BitWriter *bw, guint n)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:185
gpu_allocate_memory
void * gpu_allocate_memory(int gpuId, size_t size, size_t align)
gst_rtp_timestamp_meta_api_get_type
GType gst_rtp_timestamp_meta_api_get_type(void)
BitWriter
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:122
AncPacketInfo::horizontal_offset
guint16 horizontal_offset
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:105
BitWriter::data
guint8 * data
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:123
MEM_TYPE_DEVICE
@ MEM_TYPE_DEVICE
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:56
cudaFreeMmap
gboolean cudaFreeMmap(uint64_t *ptr, size_t size)
VIDEO_2110_20_STREAM
@ VIDEO_2110_20_STREAM
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:42
bit_reader_init
static void bit_reader_init(BitReader *br, const guint8 *data, gsize byte_len)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:129
gst_udp_parse_uri
gboolean gst_udp_parse_uri(const gchar *uristr, gchar **host, guint16 *port)
bit_writer_pad_to_byte
static void bit_writer_pad_to_byte(BitWriter *bw, guint8 pad_val)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:215
gst_buffer_add_rtp_timestamp_meta
GstRTPTimestampMeta * gst_buffer_add_rtp_timestamp_meta(GstBuffer *buffer, guint32 timestamp)
AncPacketInfo::user_data
guint16 user_data[ANC_MAX_DATA_COUNT]
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:109
_GstRTPTimestampMeta::rtp_timestamp
guint32 rtp_timestamp
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:75
gst_buffer_get_rtp_timestamp_meta
GstRTPTimestampMeta * gst_buffer_get_rtp_timestamp_meta(GstBuffer *buffer)
gst_udp_resolve_name
GInetAddress * gst_udp_resolve_name(gpointer obj, const gchar *address)
gst_rtp_timestamp_meta_get_info
const GstMetaInfo * gst_rtp_timestamp_meta_get_info(void)
AncPacketInfo::line_number
guint16 line_number
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:104
AncPacketInfo::sdid
guint16 sdid
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:107
_GstRTPTimestampMeta
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:73
bit_writer_init
static void bit_writer_init(BitWriter *bw, guint8 *data, gsize byte_len)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:168
bit_writer_init_at
static void bit_writer_init_at(BitWriter *bw, guint8 *data, gsize byte_len, gsize start_bit)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:177
BitReader::data
const guint8 * data
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:117
gpu_aligned_size
size_t gpu_aligned_size(int gpuId, size_t allocSize)
bit_reader_byte_pos
static gsize bit_reader_byte_pos(const BitReader *br)
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:162
BitReader::byte_len
gsize byte_len
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:118
AncPacketInfo::did
guint16 did
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:106
ANC_MAX_DATA_COUNT
#define ANC_MAX_DATA_COUNT
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:99
BitWriter::byte_len
gsize byte_len
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:124
StreamType
StreamType
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:41
BitReader::bit_pos
gsize bit_pos
Definition: sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:119
GstBuffer
struct _GstBuffer GstBuffer
Definition: sources/includes/ds3d/common/idatatype.h:24
INTERLACE
@ INTERLACE
Definition: 9.1/sources/gst-plugins/gst-nvdsudp/gstnvdsudpcommon.h:51