libxcoder  5.2.0
ni_util.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23  * \file ni_util.h
24  *
25  * \brief Utility definitions
26  ******************************************************************************/
27 
28 #pragma once
29 
30 #ifdef _WIN32
31 #define _SC_PAGESIZE 4096
32 #define sysconf(x) x
33 #endif
34 
35 #include "ni_device_api.h"
36 #include "ni_log.h"
37 #include "ni_defs.h"
38 
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43 
44 #define NI_INVALID_POWER 0xFFFFFFFF
45 /* Value and formula retrieved from tps25940 EFUSE manual */
46 #define TPS25940_IMON_OS 0.8f
47 #define TPS25940_GAIN_IMON 52.0f
48 #define TPS25940_R_IMON 5.6f
49 
50 /* Value and formula retrieved from tps25946 EFUSE manual */
51 #define TPS25946_GAIN_IMON 182.0f
52 #define TPS25946_GAIN_IMON_T1M 105.5f
53 #define TPS25946_R_IMON_T2A 1000.0f
54 #define TPS25946_R_IMON_T1U_UA 1130.0f
55 #define TPS25946_R_IMON_T1S 562.0f
56 #define TPS25946_R_IMON_T1M 750.0f
57 
58 /* Value and formula retrieved from MAX15162AAWE+T EFUSE manual */
59 #define MAX15162AAWE_C_IRATIO 4000.0f
60 #define MAX15162AAWE_R_IMON 1500.0f
61 
62 /* Value and formula retrieved from MAX17613B EFUSE manual */
63 #define MAX17613B_R_ISET_TOTAL 1510.0f /* Voltage Divider at ADC */
64 #define MAX17613B_R_ISET_R2 1000.0f
65 #define MAX17613B_C_IRATIO 3000.0f
66 
67 #define MCU_FSR_ADC 4096.0f
68 #define MCU_REF_VOLTAGE 2500.0f //mV
69 
70 static inline int ni_min(int a, int b)
71 {
72  return a < b ? a : b;
73 }
74 
75 static inline int ni_max(int a, int b)
76 {
77  return a > b ? a : b;
78 }
79 
80 static inline float ni_minf(float a, float b)
81 {
82  return a < b ? a : b;
83 }
84 
85 static inline float ni_maxf(float a, float b)
86 {
87  return a > b ? a : b;
88 }
89 
90 static inline int clip3(int min, int max, int a)
91 {
92  return ni_min(ni_max(min, a), max);
93 }
94 
95 static inline float clip3f(float min, float max, float a)
96 {
97  return ni_minf(ni_maxf(min, a), max);
98 }
99 
100 #define NIALIGN(x, a) (((x) + (a)-1) & ~((a)-1))
101 // Compile time assert macro
102 #define COMPILE_ASSERT(condition) ((void)sizeof(char[1 - 2 * !(condition)]))
103 
104 #define XCODER_MAX_NUM_TS_TABLE 32
105 #define XCODER_FRAME_OFFSET_DIFF_THRES 100
106 #define XCODER_MAX_ENC_PACKETS_PER_READ 16
107 #define XCODER_MAX_NUM_QUEUE_ENTRIES 6000
108 #define XCODER_MAX_NUM_TEMPORAL_LAYER 7
109 #define BUFFER_POOL_SZ_PER_CONTEXT 300
110 
111 // for _T400_ENC
112 #define XCODER_MIN_ENC_PIC_WIDTH 144
113 #define XCODER_MIN_ENC_PIC_HEIGHT 128
114 #define XCODER_MAX_ENC_PIC_WIDTH 8192
115 #define XCODER_MAX_ENC_PIC_HEIGHT 8192
116 
117 #define NI_DEC_FRAME_BUF_POOL_SIZE_INIT 20
118 #define NI_DEC_FRAME_BUF_POOL_SIZE_EXPAND 20
119 
120 
121 // memory buffer pool operations (one use is for decoder frame buffer pool)
123 
124 void ni_buf_pool_return_buffer(ni_buf_t *buf, ni_buf_pool_t *p_buffer_pool);
125 
126 ni_buf_t *ni_buf_pool_allocate_buffer(ni_buf_pool_t *p_buffer_pool, int buffer_size);
127 
128 // decoder frame buffer pool init & free
129 int32_t ni_dec_fme_buffer_pool_initialize(ni_session_context_t* p_ctx, int32_t number_of_buffers, int width, int height, int height_align, int factor);
130 void ni_dec_fme_buffer_pool_free(ni_buf_pool_t *p_buffer_pool);
131 
132 // timestamp buffer pool operations
133 void ni_buffer_pool_free(ni_queue_buffer_pool_t *p_buffer_pool);
134 
135 ni_retcode_t ni_find_blk_name(const char *p_dev, char *p_out_buf, int out_buf_len);
136 ni_retcode_t ni_check_dev_name(const char *p_dev);
137 ni_retcode_t ni_timestamp_init(ni_session_context_t* p_ctx, ni_timestamp_table_t **pp_table, const char *name);
139 ni_retcode_t ni_timestamp_register(ni_queue_buffer_pool_t *p_buffer_pool, ni_timestamp_table_t *p_table, int64_t timestamp, uint64_t data_info);
140 ni_retcode_t ni_timestamp_get(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool);
141 ni_retcode_t ni_timestamp_get_v2(ni_timestamp_table_t *p_table, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, ni_queue_buffer_pool_t *p_buffer_pool);
142 
143 ni_retcode_t ni_timestamp_get_with_threshold(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool);
144 
146  ni_timestamp_table_t *dts_list,
147  ni_queue_buffer_pool_t *p_buffer_pool);
148 
149 ni_retcode_t ni_queue_init(ni_session_context_t* p_ctx, ni_queue_t *p_queue, const char *name);
150 ni_retcode_t ni_queue_push(ni_queue_buffer_pool_t *p_buffer_pool, ni_queue_t *p_queue, uint64_t frame_offset, int64_t timestamp);
151 ni_retcode_t ni_queue_pop(ni_queue_t *p_queue, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool);
152 ni_retcode_t ni_queue_pop_threshold(ni_queue_t *p_queue, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool);
155 
156 int32_t ni_atobool(const char *p_str, bool *b_error);
157 int32_t ni_atoi(const char *p_str, bool *b_error);
158 double ni_atof(const char *p_str, bool *b_error);
159 int32_t ni_parse_name(const char *arg, const char *const *names, bool *b_error);
160 uint32_t ni_decode_power_measurement(uint32_t power_value, const uint8_t *serial_number);
161 
162 /*!******************************************************************************
163  * \brief precheck a device can be read by ni_device_capability_query()
164  * INFO OR ERROR logs will not be printed in this function
165  * \param[in] p_dev device path string. eg: "/dev/nvme1n2"
166  *
167  * \return
168  * returns NI_RETCODE_FAILURE
169  * when the device can not be read by ni_device_capability_query()
170  *
171  * returns NI_RETCODE_SUCCESS
172  * when the device can not be read by ni_device_capability_query() or
173  * the result can not be determined to prevent query failures due to
174  * some reasons such as missing commands on the system
175  *******************************************************************************/
177 
178 // Netint HW YUV420p data layout related utility functions
179 
180 /*!*****************************************************************************
181  * \brief Get dimension information of Netint HW YUV420p frame to be sent
182  * to encoder for encoding. Caller usually retrieves this info and
183  * uses it in the call to ni_encoder_frame_buffer_alloc for buffer
184  * allocation.
185  *
186  * \param[in] width source YUV frame width
187  * \param[in] height source YUV frame height
188  * \param[in] bit_depth_factor 1 for 8 bit, 2 for 10 bit
189  * \param[in] is_semiplanar non-0 for semiplnar frame, 0 otherwise
190  * \param[out] plane_stride size (in bytes) of each plane width
191  * \param[out] plane_height size of each plane height
192  *
193  * \return Y/Cb/Cr stride and height info
194  *
195  ******************************************************************************/
196 LIB_API void ni_get_hw_yuv420p_dim(int width, int height, int bit_depth_factor,
197  int is_semiplanar,
198  int plane_stride[NI_MAX_NUM_DATA_POINTERS],
199  int plane_height[NI_MAX_NUM_DATA_POINTERS]);
200 
201 /*!*****************************************************************************
202  * \brief Get dimension information of frame to be sent
203  * to encoder for encoding. Caller usually retrieves this info and
204  * uses it in the call to ni_encoder_frame_buffer_alloc for buffer
205  * allocation.
206  * The returned stride and height info will take alignment
207  * requirements into account.
208  *
209  * \param[in] width source frame width
210  * \param[in] height source frame height
211  * \param[in] pix_fmt ni pixel format
212  * \param[out] plane_stride size (in bytes) of each plane width
213  * \param[out] plane_height size of each plane height
214  *
215  * \return stride and height info
216  *
217  ******************************************************************************/
218 LIB_API void ni_get_frame_dim(int width, int height,
219  ni_pix_fmt_t pix_fmt,
220  int plane_stride[NI_MAX_NUM_DATA_POINTERS],
221  int plane_height[NI_MAX_NUM_DATA_POINTERS]);
222 
223 /*!*****************************************************************************
224  * \brief Get dimension information of frame to be sent
225  * to encoder for encoding. Caller usually retrieves this info and
226  * uses it in the call to ni_encoder_frame_buffer_alloc for buffer
227  * allocation.
228  * The returned stride and height info will take into account both min
229  * resolution and alignment requirements.
230  *
231  * \param[in] width source frame width
232  * \param[in] height source frame height
233  * \param[in] pix_fmt ni pixel format
234  * \param[out] plane_stride size (in bytes) of each plane width
235  * \param[out] plane_height size of each plane height
236  *
237  * \return stride and height info
238  *
239  ******************************************************************************/
240 LIB_API void ni_get_min_frame_dim(int width, int height,
241  ni_pix_fmt_t pix_fmt,
242  int plane_stride[NI_MAX_NUM_DATA_POINTERS],
243  int plane_height[NI_MAX_NUM_DATA_POINTERS]);
244 
245 /*!*****************************************************************************
246  * \brief Copy YUV data to Netint HW YUV420p frame layout to be sent
247  * to encoder for encoding. Data buffer (dst) is usually allocated by
248  * ni_encoder_frame_buffer_alloc.
249  *
250  * \param[out] p_dst pointers of Y/Cb/Cr to which data is copied
251  * \param[in] p_src pointers of Y/Cb/Cr from which data is copied
252  * \param[in] width source YUV frame width
253  * \param[in] height source YUV frame height
254  * \param[in] bit_depth_factor 1 for 8 bit, 2 for 10 bit
255  * \param[in] is_semiplanar non-0 for semiplanar frame, 0 otherwise
256  * \param[in] conf_win_right right offset of conformance window
257  * \param[in] dst_stride size (in bytes) of each plane width in destination
258  * \param[in] dst_height size of each plane height in destination
259  * \param[in] src_stride size (in bytes) of each plane width in source
260  * \param[in] src_height size of each plane height in source
261  *
262  * \return Y/Cb/Cr data
263  *
264  ******************************************************************************/
266  uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS],
267  int width, int height, int bit_depth_factor,
268  int is_semiplanar, int conf_win_right,
269  int dst_stride[NI_MAX_NUM_DATA_POINTERS],
270  int dst_height[NI_MAX_NUM_DATA_POINTERS],
271  int src_stride[NI_MAX_NUM_DATA_POINTERS],
272  int src_height[NI_MAX_NUM_DATA_POINTERS]);
273 
274 /*!*****************************************************************************
275  * \brief Copy RGBA or YUV data to Netint HW frame layout to be sent
276  * to encoder for encoding. Data buffer (dst) is usually allocated by
277  * ni_encoder_frame_buffer_alloc.
278  *
279  * \param[out] p_dst pointers to which data is copied
280  * \param[in] p_src pointers from which data is copied
281  * \param[in] width source frame width
282  * \param[in] height source frame height
283  * \param[in] factor 1 for 8 bit, 2 for 10 bit
284  * \param[in] pix_fmt pixel format to distinguish between planar types and/or components
285  * \param[in] conf_win_right right offset of conformance window
286  * \param[in] dst_stride size (in bytes) of each plane width in destination
287  * \param[in] dst_height size of each plane height in destination
288  * \param[in] src_stride size (in bytes) of each plane width in source
289  * \param[in] src_height size of each plane height in source
290  *
291  * \return copied data
292  *
293  ******************************************************************************/
295  uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS],
296  int frame_width, int frame_height,
297  int factor, ni_pix_fmt_t pix_fmt,
298  int conf_win_right,
299  int dst_stride[NI_MAX_NUM_DATA_POINTERS],
300  int dst_height[NI_MAX_NUM_DATA_POINTERS],
301  int src_stride[NI_MAX_NUM_DATA_POINTERS],
302  int src_height[NI_MAX_NUM_DATA_POINTERS]);
303 
304 /*!*****************************************************************************
305  * \brief Copy yuv444p data to yuv420p frame layout to be sent
306  * to encoder for encoding. Data buffer (dst) is usually allocated by
307  * ni_encoder_frame_buffer_alloc.
308  *
309  * \param[out] p_dst0 pointers of Y/Cb/Cr as yuv420p output0
310  * \param[out] p_dst1 pointers of Y/Cb/Cr as yuv420p output1
311  * \param[in] p_src pointers of Y/Cb/Cr as yuv444p intput
312  * \param[in] width source YUV frame width
313  * \param[in] height source YUV frame height
314  * \param[in] factor 1 for 8 bit, 2 for 10 bit
315  * \param[in] mode 0 for
316  * out0 is Y+1/2V, with the original input as the out0, 1/4V
317  * copy to data[1] 1/4V copy to data[2]
318  * out1 is U+1/2V, U copy to data[0], 1/4V copy to data[1], 1/4V
319  * copy to data[2]
320  * mode 1 for
321  * out0 is Y+1/2u+1/2v, with the original input as the output0,
322  * 1/4U copy to data[1] 1/4V copy to data[2]
323  * out1 is (1/2U+1/2V)+1/4U+1/4V, 1/2U & 1/2V copy to data[0],
324  * 1/4U copy to data[1], 1/4V copy to data[2]
325  *
326  * \return Y/Cb/Cr data
327  *
328  ******************************************************************************/
330  uint8_t *p_dst1[NI_MAX_NUM_DATA_POINTERS],
331  uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS],
332  int width, int height, int factor,
333  int mode);
334 
335 // NAL operations
336 
337 /*!*****************************************************************************
338  * \brief Insert emulation prevention byte(s) as needed into the data buffer
339  *
340  * \param buf data buffer to be worked on - new byte(s) will be inserted
341  * size number of bytes starting from buf to check
342  *
343  * \return the number of emulation prevention bytes inserted into buf, 0 if
344  * none.
345  *
346  * Note: caller *MUST* ensure for newly inserted bytes, buf has enough free
347  * space starting from buf + size
348  ******************************************************************************/
349 LIB_API int ni_insert_emulation_prevent_bytes(uint8_t *buf, int size);
350 
351 /*!*****************************************************************************
352  * \brief Remove emulation prevention byte(s) as needed from the data buffer
353  *
354  * \param buf data buffer to be worked on - emu prevent byte(s) will be
355  * removed from.
356  * size number of bytes starting from buf to check
357  *
358  * \return the number of emulation prevention bytes removed from buf, 0 if
359  * none.
360  *
361  * Note: buf will be modified if emu prevent byte(s) found and removed.
362  ******************************************************************************/
363 LIB_API int ni_remove_emulation_prevent_bytes(uint8_t *buf, int size);
364 
365 /*!*****************************************************************************
366  * \brief Get time for logs with microsecond timestamps
367  *
368  * \param[in/out] p_tp timeval struct
369  * \param[in] p_tzp void *
370  *
371  * \return return 0 for success, -1 for error
372  ******************************************************************************/
373 LIB_API int32_t ni_gettimeofday(struct timeval *p_tp, void *p_tzp);
374 
375 /*!*****************************************************************************
376  * \brief Allocate aligned memory
377  *
378  * \param[in/out] memptr The address of the allocated memory will be a
379  * multiple of alignment, which must be a power of two
380  * and a multiple of sizeof(void *). If size is 0, then
381  * the value placed is either NULL, or a unique pointer
382  * value that can later be successfully passed to free.
383  * \param[in] alignment The alignment value of the allocated value.
384  * \param[in] size The allocated memory size.
385  *
386  * \return 0 for success, ENOMEM for error
387  ******************************************************************************/
388 LIB_API int ni_posix_memalign(void **memptr, size_t alignment, size_t size);
389 
390 uint32_t ni_round_up(uint32_t number_to_round, uint32_t multiple);
391 
392 #ifdef _WIN32
393 #define ni_aligned_free(p_memptr) \
394 { \
395  _aligned_free(p_memptr); \
396  p_memptr = NULL; \
397 }
398 #else
399 #define ni_aligned_free(p_memptr) \
400 { \
401  free(p_memptr); \
402  p_memptr = NULL; \
403 }
404 #endif
405 
406 // This method is used in device session close function to unset all the
407 // pointers in the session context.
408 #define ni_memfree(p_memptr) \
409 { \
410  free(p_memptr); \
411  p_memptr = NULL; \
412 }
413 
414 #if __linux__ || __APPLE__
415 uint32_t ni_get_kernel_max_io_size(const char * p_dev);
416 #endif
417 
418 LIB_API uint64_t ni_gettime_ns(void);
419 LIB_API void ni_usleep(int64_t usec);
420 LIB_API char *ni_strtok(char *s, const char *delim, char **saveptr);
422 ni_network_layer_convert_output(float *dst, uint32_t num, ni_packet_t *p_packet,
423  ni_network_data_t *p_network, uint32_t layer);
427  uint8_t *dst, uint32_t dst_len, const char *tensor_file,
428  ni_network_layer_params_t *p_param);
430  uint8_t *dst, uint32_t dst_len, float *src, uint32_t src_len,
431  ni_network_layer_params_t *p_param);
433  float *dst, uint32_t dst_len, uint8_t *src, uint32_t src_len,
434  ni_network_layer_params_t *p_param);
435 
436 LIB_API void ni_calculate_sha256(const uint8_t aui8Data[],
437  size_t ui32DataLength, uint8_t aui8Hash[]);
438 /*!*****************************************************************************
439 * \brief Copy Descriptor data to Netint HW descriptor frame layout to be sent
440 * to encoder for encoding. Data buffer (dst) is usually allocated by
441 * ni_encoder_frame_buffer_alloc. Only necessary when metadata size in
442 * source is insufficient
443 *
444 * \param[out] p_dst pointers of Y/Cb/Cr to which data is copied
445 * \param[in] p_src pointers of Y/Cb/Cr from which data is copied
446 *
447 * \return descriptor data
448 *
449 ******************************************************************************/
451  uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS]);
452 
453 /*!*****************************************************************************
454  * \brief Get libxcoder API version
455  *
456  * \return char pointer to libxcoder API version
457  ******************************************************************************/
459 
460 /*!*****************************************************************************
461  * \brief Get FW API version libxcoder is compatible with
462  *
463  * \return char pointer to FW API version libxcoder is compatible with
464  ******************************************************************************/
466 
467 /*!*****************************************************************************
468  * \brief Get formatted FW API version string from unformatted FW API version
469  * string
470  *
471  * \param[in] ver_str pointer to string containing FW API. Only up to 3
472  * characters will be read
473  * \param[out] fmt_str pointer to string buffer of at least size 5 to output
474  * formated version string to
475  *
476  * \return none
477  ******************************************************************************/
478 LIB_API void ni_fmt_fw_api_ver_str(const char ver_str[], char fmt_str[]);
479 
480 /*!*****************************************************************************
481  * \brief Compare two 3 character strings containing a FW API version. Handle
482  * comparision when FW API version format length changed from 2 to 3.
483  *
484  * \param[in] ver1 pointer to string containing FW API. Only up to 3
485  * characters will be read
486  * \param[in] ver2 pointer to string containing FW API. Only up to 3
487  * characters will be read
488  *
489  * \return 0 if ver1 == ver2, 1 if ver1 > ver2, -1 if ver1 < ver2
490  ******************************************************************************/
491 LIB_API int ni_cmp_fw_api_ver(const char ver1[], const char ver2[]);
492 
493 /*!*****************************************************************************
494  * \brief Get libxcoder SW release version
495  *
496  * \return char pointer to libxcoder SW release version
497  ******************************************************************************/
499 
500 /*!*****************************************************************************
501  * \brief initialize a mutex
502  *
503  * \param[in] thread mutex
504  *
505  * \return On success returns 0
506  * On failure returns <0
507  ******************************************************************************/
508 LIB_API int ni_pthread_mutex_init(ni_pthread_mutex_t *mutex);
509 
510 /*!*****************************************************************************
511  * \brief destory a mutex
512  *
513  * \param[in] thread mutex
514  *
515  * \return On success returns 0
516  * On failure returns <0
517  ******************************************************************************/
518 LIB_API int ni_pthread_mutex_destroy(ni_pthread_mutex_t *mutex);
519 
520 /*!*****************************************************************************
521  * \brief thread mutex lock
522  *
523  * \param[in] thread mutex
524  *
525  * \return On success returns 0
526  * On failure returns <0
527  ******************************************************************************/
528 LIB_API int ni_pthread_mutex_lock(ni_pthread_mutex_t *mutex);
529 
530 /*!*****************************************************************************
531  * \brief thread mutex unlock
532  *
533  * \param[in] thread mutex
534  *
535  * \return On success returns 0
536  * On failure returns <0
537  ******************************************************************************/
538 LIB_API int ni_pthread_mutex_unlock(ni_pthread_mutex_t *mutex);
539 
540 /*!*****************************************************************************
541  * \brief create a new thread
542  *
543  * \param[in] thread thread id
544  * \param[in] attr attributes to the new thread
545  * \param[in] start_routine entry of the thread routine
546  * \param[in] arg sole argument of the routine
547  *
548  * \return On success returns 0
549  * On failure returns <0
550  ******************************************************************************/
551 LIB_API int ni_pthread_create(ni_pthread_t *thread,
552  const ni_pthread_attr_t *attr,
553  void *(*start_routine)(void *), void *arg);
554 
555 /*!*****************************************************************************
556  * \brief join with a terminated thread
557  *
558  * \param[in] thread thread id
559  * \param[out] value_ptr return status
560  *
561  * \return On success returns 0
562  * On failure returns <0
563  ******************************************************************************/
564 LIB_API int ni_pthread_join(ni_pthread_t thread, void **value_ptr);
565 
566 /*!*****************************************************************************
567  * \brief initialize condition variables
568  *
569  * \param[in] cond condition variable
570  * \param[in] attr attribute to the condvar
571  *
572  * \return On success returns 0
573  * On failure returns <0
574  ******************************************************************************/
575 LIB_API int ni_pthread_cond_init(ni_pthread_cond_t *cond,
576  const ni_pthread_condattr_t *attr);
577 
578 /*!*****************************************************************************
579  * \brief destroy condition variables
580  *
581  * \param[in] cond condition variable
582  *
583  * \return On success returns 0
584  * On failure returns <0
585  ******************************************************************************/
586 LIB_API int ni_pthread_cond_destroy(ni_pthread_cond_t *cond);
587 
588 /*!*****************************************************************************
589  * \brief broadcast a condition
590  *
591  * \param[in] cond condition variable
592  *
593  * \return On success returns 0
594  * On failure returns <0
595  ******************************************************************************/
596 LIB_API int ni_pthread_cond_broadcast(ni_pthread_cond_t *cond);
597 
598 /*!*****************************************************************************
599  * \brief wait on a condition
600  *
601  * \param[in] cond condition variable
602  * \param[in] mutex mutex related to the condvar
603  *
604  * \return On success returns 0
605  * On failure returns <0
606  ******************************************************************************/
607 LIB_API int ni_pthread_cond_wait(ni_pthread_cond_t *cond,
608  ni_pthread_mutex_t *mutex);
609 
610 /*!******************************************************************************
611  * \brief signal a condition
612  *
613  * \param[in] cond condition variable
614  *
615  * \return On success returns 0
616  * On failure returns <0
617  *******************************************************************************/
618 LIB_API int ni_pthread_cond_signal(ni_pthread_cond_t *cond);
619 
620 /*!*****************************************************************************
621  * \brief wait on a condition
622  *
623  * \param[in] cond condition variable
624  * \param[in] mutex mutex related to the condvar
625  * \param[in[ abstime abstract value of timeout
626  *
627  * \return On success returns 0
628  * On failure returns <0
629  ******************************************************************************/
630 LIB_API int ni_pthread_cond_timedwait(ni_pthread_cond_t *cond,
631  ni_pthread_mutex_t *mutex,
632  const struct timespec *abstime);
633 
634 /*!*****************************************************************************
635  * \brief examine and change mask of blocked signals
636  *
637  * \param[in] how behavior of this call, can be value of SIG_BLOCK,
638  * SIG_UNBLOCK and SIG_SETMASK
639  * \param[in] set current value of the signal mask. If NULL, the mask keeps
640  * unchanged.
641  * \param[in] old_set previous value of the signal mask, can be NULL.
642  *
643  * \return On success returns 0
644  * On failure returns <0
645  ******************************************************************************/
646 LIB_API int ni_pthread_sigmask(int how, const ni_sigset_t *set,
647  ni_sigset_t *oldset);
648 
649 /*!*****************************************************************************
650  * \brief Get text string for the provided error
651  *
652  * \return char pointer for the provided error
653  ******************************************************************************/
654 LIB_API const char *ni_get_rc_txt(ni_retcode_t rc);
655 
656 /*!*****************************************************************************
657  * \brief Retrieve key and value from 'key=value' pair
658  *
659  * \param[in] p_str pointer to string to extract pair from
660  * \param[out] key pointer to key
661  * \param[out] value pointer to value
662  *
663  * \return return 0 if successful, otherwise 1
664  *
665  ******************************************************************************/
666 LIB_API int ni_param_get_key_value(char *p_str, char *key, char *value);
667 
668 /*!*****************************************************************************
669  * \brief Retrieve encoder config parameter values from --xcoder-params
670  *
671  * \param[in] xcoderParams pointer to string containing xcoder params
672  * \param[out] params pointer to xcoder params to fill out
673  * \param[out] ctx pointer to session context
674  *
675  * \return return 0 if successful, -1 otherwise
676  *
677  ******************************************************************************/
678 LIB_API int ni_retrieve_xcoder_params(char xcoderParams[],
679  ni_xcoder_params_t *params,
680  ni_session_context_t *ctx);
681 
682 /*!*****************************************************************************
683  * \brief Retrieve custom gop config values from --xcoder-gop
684  *
685  * \param[in] xcoderGop pointer to string containing xcoder gop
686  * \param[out] params pointer to xcoder params to fill out
687  * \param[out] ctx pointer to session context
688  *
689  * \return return 0 if successful, -1 otherwise
690  *
691  ******************************************************************************/
692 LIB_API int ni_retrieve_xcoder_gop(char xcoderGop[],
693  ni_xcoder_params_t *params,
694  ni_session_context_t *ctx);
695 
696 /*!*****************************************************************************
697  * \brief Retrieve decoder config parameter values from --decoder-params
698  *
699  * \param[in] xcoderParams pointer to string containing xcoder params
700  * \param[out] params pointer to xcoder params to fill out
701  * \param[out] ctx pointer to session context
702  *
703  * \return return 0 if successful, -1 otherwise
704  *
705  ******************************************************************************/
706 LIB_API int ni_retrieve_decoder_params(char xcoderParams[],
707  ni_xcoder_params_t *params,
708  ni_session_context_t *ctx);
709 
710 /*!*****************************************************************************
711  * \brief return error string according to error code from firmware
712  *
713  * \param[in] rc error code return from firmware
714  *
715  * \return error string
716  ******************************************************************************/
717 LIB_API const char *ni_ai_errno_to_str(int rc);
718 #ifdef __cplusplus
719 }
720 #endif
ni_quadra_card_identify_precheck
ni_retcode_t ni_quadra_card_identify_precheck(const char *p_dev)
precheck a device can be read by ni_device_capability_query() INFO OR ERROR logs will not be printed ...
Definition: ni_util.c:4874
ni_gettime_ns
LIB_API uint64_t ni_gettime_ns(void)
Definition: ni_util.c:1998
ni_copy_yuv_444p_to_420p
LIB_API void ni_copy_yuv_444p_to_420p(uint8_t *p_dst0[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_dst1[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS], int width, int height, int factor, int mode)
Copy yuv444p data to yuv420p frame layout to be sent to encoder for encoding. Data buffer (dst) is us...
Definition: ni_util.c:2478
ni_strtok
LIB_API char * ni_strtok(char *s, const char *delim, char **saveptr)
Definition: ni_util.c:420
_ni_network_data
Definition: ni_device_api.h:1363
ni_network_layer_convert_output
LIB_API ni_retcode_t ni_network_layer_convert_output(float *dst, uint32_t num, ni_packet_t *p_packet, ni_network_data_t *p_network, uint32_t layer)
Definition: ni_util.c:3084
ni_get_rc_txt
const LIB_API char * ni_get_rc_txt(ni_retcode_t rc)
Get text string for the provided error.
Definition: ni_util.c:3731
ni_pix_fmt_t
ni_pix_fmt_t
Definition: ni_device_api.h:260
ni_check_dev_name
ni_retcode_t ni_check_dev_name(const char *p_dev)
check dev name
Definition: ni_util.c:1252
ni_queue_pop_threshold
ni_retcode_t ni_queue_pop_threshold(ni_queue_t *p_queue, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1716
ni_timestamp_scan_cleanup
void ni_timestamp_scan_cleanup(ni_timestamp_table_t *pts_list, ni_timestamp_table_t *dts_list, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1431
ni_timestamp_get
ni_retcode_t ni_timestamp_get(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Retrieve timestamp from table based on frameoffset info.
Definition: ni_util.c:1399
ni_queue_init
ni_retcode_t ni_queue_init(ni_session_context_t *p_ctx, ni_queue_t *p_queue, const char *name)
Initialize xcoder queue.
Definition: ni_util.c:1513
ni_buffer_pool_free
void ni_buffer_pool_free(ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:789
ni_get_compat_fw_api_ver
LIB_API NI_DEPRECATED char * ni_get_compat_fw_api_ver(void)
Get FW API version libxcoder is compatible with.
Definition: ni_util.c:3612
ni_pthread_mutex_destroy
LIB_API int ni_pthread_mutex_destroy(ni_pthread_mutex_t *mutex)
destory a mutex
Definition: ni_util.c:4039
ni_decode_power_measurement
uint32_t ni_decode_power_measurement(uint32_t power_value, const uint8_t *serial_number)
decode the raw current obtained and determine power
Definition: ni_util.c:4384
ni_timestamp_get_v2
ni_retcode_t ni_timestamp_get_v2(ni_timestamp_table_t *p_table, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, ni_queue_buffer_pool_t *p_buffer_pool)
Retrieve timestamp from table based on frameoffset info.
Definition: ni_util.c:1475
ni_fmt_fw_api_ver_str
LIB_API void ni_fmt_fw_api_ver_str(const char ver_str[], char fmt_str[])
Get formatted FW API version string from unformatted FW API version string.
Definition: ni_util.c:3644
ni_find_blk_name
ni_retcode_t ni_find_blk_name(const char *p_dev, char *p_out_buf, int out_buf_len)
Find NVMe name space block from device name If none is found, assume nvme multi-pathing is disabled a...
Definition: ni_util.c:1181
ni_pthread_sigmask
LIB_API int ni_pthread_sigmask(int how, const ni_sigset_t *set, ni_sigset_t *oldset)
examine and change mask of blocked signals
Definition: ni_util.c:4305
ni_atof
double ni_atof(const char *p_str, bool *b_error)
Convert string to floating.
Definition: ni_util.c:1950
ni_ai_network_layer_dims
LIB_API uint32_t ni_ai_network_layer_dims(ni_network_layer_params_t *p_param)
Definition: ni_util.c:3369
ni_copy_hw_yuv420p
LIB_API void ni_copy_hw_yuv420p(uint8_t *p_dst[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS], int width, int height, int bit_depth_factor, int is_semiplanar, int conf_win_right, int dst_stride[NI_MAX_NUM_DATA_POINTERS], int dst_height[NI_MAX_NUM_DATA_POINTERS], int src_stride[NI_MAX_NUM_DATA_POINTERS], int src_height[NI_MAX_NUM_DATA_POINTERS])
Copy YUV data to Netint HW YUV420p frame layout to be sent to encoder for encoding....
Definition: ni_util.c:2352
ni_pthread_mutex_init
LIB_API int ni_pthread_mutex_init(ni_pthread_mutex_t *mutex)
initialize a mutex
Definition: ni_util.c:4001
ni_pthread_cond_timedwait
LIB_API int ni_pthread_cond_timedwait(ni_pthread_cond_t *cond, ni_pthread_mutex_t *mutex, const struct timespec *abstime)
wait on a condition
Definition: ni_util.c:4271
ni_network_convert_data_to_tensor
LIB_API ni_retcode_t ni_network_convert_data_to_tensor(float *dst, uint32_t dst_len, uint8_t *src, uint32_t src_len, ni_network_layer_params_t *p_param)
Definition: ni_util.c:3256
ni_get_libxcoder_api_ver
LIB_API char * ni_get_libxcoder_api_ver(void)
Get libxcoder API version.
Definition: ni_util.c:3600
ni_pthread_create
LIB_API int ni_pthread_create(ni_pthread_t *thread, const ni_pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
create a new thread
Definition: ni_util.c:4121
ni_timestamp_init
ni_retcode_t ni_timestamp_init(ni_session_context_t *p_ctx, ni_timestamp_table_t **pp_table, const char *name)
Initialize timestamp handling.
Definition: ni_util.c:1308
_ni_network_layer_params_t
Definition: ni_device_api.h:1326
ni_retcode_t
ni_retcode_t
Definition: ni_defs.h:425
ni_param_get_key_value
LIB_API int ni_param_get_key_value(char *p_str, char *key, char *value)
Retrieve key and value from 'key=value' pair.
Definition: ni_util.c:3755
ni_timestamp_get_with_threshold
ni_retcode_t ni_timestamp_get_with_threshold(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1423
ni_log.h
Logging definitions.
ni_timestamp_register
ni_retcode_t ni_timestamp_register(ni_queue_buffer_pool_t *p_buffer_pool, ni_timestamp_table_t *p_table, int64_t timestamp, uint64_t data_info)
Register timestamp in timestamp/frameoffset table.
Definition: ni_util.c:1373
ni_usleep
LIB_API void ni_usleep(int64_t usec)
Definition: ni_util.c:358
ni_defs.h
Common NETINT definitions used by all modules.
ni_dec_fme_buffer_pool_free
void ni_dec_fme_buffer_pool_free(ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:735
ni_pthread_mutex_lock
LIB_API int ni_pthread_mutex_lock(ni_pthread_mutex_t *mutex)
thread mutex lock
Definition: ni_util.c:4057
ni_timestamp_done
ni_retcode_t ni_timestamp_done(ni_timestamp_table_t *p_table, ni_queue_buffer_pool_t *p_buffer_pool)
Clean up timestamp handling.
Definition: ni_util.c:1348
ni_buf_pool_return_buffer
void ni_buf_pool_return_buffer(ni_buf_t *buf, ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:529
_ni_buf_t
Definition: ni_device_api.h:1272
ni_queue_free
ni_retcode_t ni_queue_free(ni_queue_t *p_queue, ni_queue_buffer_pool_t *p_buffer_pool)
Free xcoder queue.
Definition: ni_util.c:1811
ni_pthread_cond_broadcast
LIB_API int ni_pthread_cond_broadcast(ni_pthread_cond_t *cond)
broadcast a condition
Definition: ni_util.c:4214
ni_insert_emulation_prevent_bytes
LIB_API int ni_insert_emulation_prevent_bytes(uint8_t *buf, int size)
Insert emulation prevention byte(s) as needed into the data buffer.
Definition: ni_util.c:2591
ni_copy_frame_data
LIB_API void ni_copy_frame_data(uint8_t *p_dst[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS], int frame_width, int frame_height, int factor, ni_pix_fmt_t pix_fmt, int conf_win_right, int dst_stride[NI_MAX_NUM_DATA_POINTERS], int dst_height[NI_MAX_NUM_DATA_POINTERS], int src_stride[NI_MAX_NUM_DATA_POINTERS], int src_height[NI_MAX_NUM_DATA_POINTERS])
Copy RGBA or YUV data to Netint HW frame layout to be sent to encoder for encoding....
Definition: ni_util.c:2398
ni_cmp_fw_api_ver
LIB_API int ni_cmp_fw_api_ver(const char ver1[], const char ver2[])
Compare two 3 character strings containing a FW API version. Handle comparision when FW API version f...
Definition: ni_util.c:3674
ni_buf_pool_get_buffer
ni_buf_t * ni_buf_pool_get_buffer(ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:468
ni_pthread_mutex_unlock
LIB_API int ni_pthread_mutex_unlock(ni_pthread_mutex_t *mutex)
thread mutex unlock
Definition: ni_util.c:4083
ni_gettimeofday
LIB_API int32_t ni_gettimeofday(struct timeval *p_tp, void *p_tzp)
Get time for logs with microsecond timestamps.
Definition: ni_util.c:139
ni_queue_push
ni_retcode_t ni_queue_push(ni_queue_buffer_pool_t *p_buffer_pool, ni_queue_t *p_queue, uint64_t frame_offset, int64_t timestamp)
Push into xcoder queue.
Definition: ni_util.c:1540
_ni_packet
Definition: ni_device_api.h:2825
_ni_buf_pool_t
Definition: ni_device_api.h:1282
ni_posix_memalign
LIB_API int ni_posix_memalign(void **memptr, size_t alignment, size_t size)
Allocate aligned memory.
Definition: ni_util.c:198
ni_pthread_cond_init
LIB_API int ni_pthread_cond_init(ni_pthread_cond_t *cond, const ni_pthread_condattr_t *attr)
initialize condition variables
Definition: ni_util.c:4177
ni_retrieve_xcoder_params
LIB_API int ni_retrieve_xcoder_params(char xcoderParams[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
Retrieve encoder config parameter values from –xcoder-params.
Definition: ni_util.c:3787
ni_queue_print
ni_retcode_t ni_queue_print(ni_queue_t *p_queue)
Print xcoder queue info.
Definition: ni_util.c:1851
_ni_queue_buffer_pool_t
Definition: ni_device_api.h:1304
_ni_session_context
Definition: ni_device_api.h:1408
ni_network_layer_convert_tensor
LIB_API ni_retcode_t ni_network_layer_convert_tensor(uint8_t *dst, uint32_t dst_len, const char *tensor_file, ni_network_layer_params_t *p_param)
Definition: ni_util.c:3236
NI_MAX_NUM_DATA_POINTERS
#define NI_MAX_NUM_DATA_POINTERS
Definition: ni_defs.h:232
ni_atoi
int32_t ni_atoi(const char *p_str, bool *b_error)
Convert string to integer.
Definition: ni_util.c:1930
ni_buf_pool_allocate_buffer
ni_buf_t * ni_buf_pool_allocate_buffer(ni_buf_pool_t *p_buffer_pool, int buffer_size)
Definition: ni_util.c:585
ni_ai_errno_to_str
const LIB_API char * ni_ai_errno_to_str(int rc)
return error string according to error code from firmware
Definition: ni_util.c:4321
NI_DEPRECATED
#define NI_DEPRECATED
Definition: ni_defs.h:77
ni_ai_network_layer_size
LIB_API uint32_t ni_ai_network_layer_size(ni_network_layer_params_t *p_param)
Definition: ni_util.c:3363
ni_atobool
int32_t ni_atobool(const char *p_str, bool *b_error)
Convert string to boolean.
Definition: ni_util.c:1907
_ni_xcoder_params
Definition: ni_device_api.h:2713
ni_calculate_sha256
LIB_API void ni_calculate_sha256(const uint8_t aui8Data[], size_t ui32DataLength, uint8_t aui8Hash[])
Definition: ni_util.c:3550
ni_parse_name
int32_t ni_parse_name(const char *arg, const char *const *names, bool *b_error)
Parse name.
Definition: ni_util.c:1970
ni_dec_fme_buffer_pool_initialize
int32_t ni_dec_fme_buffer_pool_initialize(ni_session_context_t *p_ctx, int32_t number_of_buffers, int width, int height, int height_align, int factor)
Definition: ni_util.c:626
ni_retrieve_xcoder_gop
LIB_API int ni_retrieve_xcoder_gop(char xcoderGop[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
Retrieve custom gop config values from –xcoder-gop.
Definition: ni_util.c:3861
ni_copy_hw_descriptors
LIB_API void ni_copy_hw_descriptors(uint8_t *p_dst[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS])
Copy Descriptor data to Netint HW descriptor frame layout to be sent to encoder for encoding....
Definition: ni_util.c:3576
ni_round_up
uint32_t ni_round_up(uint32_t number_to_round, uint32_t multiple)
Definition: ni_util.c:169
ni_retrieve_decoder_params
LIB_API int ni_retrieve_decoder_params(char xcoderParams[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
Retrieve decoder config parameter values from –decoder-params.
Definition: ni_util.c:3931
ni_remove_emulation_prevent_bytes
LIB_API int ni_remove_emulation_prevent_bytes(uint8_t *buf, int size)
Remove emulation prevention byte(s) as needed from the data buffer.
Definition: ni_util.c:2644
ni_pthread_cond_signal
LIB_API int ni_pthread_cond_signal(ni_pthread_cond_t *cond)
signal a condition
Definition: ni_util.c:4251
ni_get_libxcoder_release_ver
LIB_API char * ni_get_libxcoder_release_ver(void)
Get libxcoder SW release version.
Definition: ni_util.c:3710
ni_pthread_join
LIB_API int ni_pthread_join(ni_pthread_t thread, void **value_ptr)
join with a terminated thread
Definition: ni_util.c:4148
ni_get_frame_dim
LIB_API void ni_get_frame_dim(int width, int height, ni_pix_fmt_t pix_fmt, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of frame to be sent to encoder for encoding. Caller usually retrieves this ...
Definition: ni_util.c:2092
ni_queue_pop
ni_retcode_t ni_queue_pop(ni_queue_t *p_queue, uint64_t frame_offset, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Pop from the xcoder queue.
Definition: ni_util.c:1611
ni_device_api.h
Public definitions for operating NETINT video processing devices for video processing.
_ni_timestamp_table_t
Definition: ni_device_api.h:1321
ni_network_convert_tensor_to_data
LIB_API ni_retcode_t ni_network_convert_tensor_to_data(uint8_t *dst, uint32_t dst_len, float *src, uint32_t src_len, ni_network_layer_params_t *p_param)
Definition: ni_util.c:3246
LIB_API
#define LIB_API
Definition: ni_libxcoder_dynamic_loading.h:52
ni_get_min_frame_dim
LIB_API void ni_get_min_frame_dim(int width, int height, ni_pix_fmt_t pix_fmt, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of frame to be sent to encoder for encoding. Caller usually retrieves this ...
Definition: ni_util.c:2181
ni_pthread_cond_destroy
LIB_API int ni_pthread_cond_destroy(ni_pthread_cond_t *cond)
destroy condition variables
Definition: ni_util.c:4196
ni_get_hw_yuv420p_dim
LIB_API void ni_get_hw_yuv420p_dim(int width, int height, int bit_depth_factor, int is_semiplanar, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of Netint HW YUV420p frame to be sent to encoder for encoding....
Definition: ni_util.c:2040
_ni_queue_t
Definition: ni_device_api.h:1313
ni_pthread_cond_wait
LIB_API int ni_pthread_cond_wait(ni_pthread_cond_t *cond, ni_pthread_mutex_t *mutex)
wait on a condition
Definition: ni_util.c:4233