28 #if __linux__ || __APPLE__
29 #include <sys/ioctl.h>
31 #include <sys/ioctl.h>
33 #include <sys/types.h>
143 SYSTEMTIME system_time;
144 ULARGE_INTEGER ularge;
146 static const unsigned __int64 epoch =
147 ((
unsigned __int64)116444736000000000ULL);
153 GetSystemTime(&system_time);
154 SystemTimeToFileTime(&system_time, &file_time);
155 ularge.LowPart = file_time.dwLowDateTime;
156 ularge.HighPart = file_time.dwHighDateTime;
158 (void)ularge.LowPart;
159 (
void)ularge.HighPart;
160 p_tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
161 p_tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
165 return gettimeofday(p_tp, p_tzp);
173 return number_to_round;
176 uint32_t remainder = number_to_round % multiple;
179 return number_to_round;
182 return (number_to_round + multiple - remainder);
201 *memptr = _aligned_malloc(size, alignment);
207 ZeroMemory(*memptr, size);
211 return posix_memalign(memptr, alignment, size);
223 uint32_t ni_get_kernel_max_io_size(
const char * p_dev)
226 char file_name[KERNEL_NVME_FILE_NAME_MAX_SZ];
227 int max_segments = 0, min_io_size = 0, max_hw_sectors_kb = 0;
228 uint32_t io_size = DEFAULT_IO_TRANSFER_SIZE;
232 memset(file_name, 0, KERNEL_NVME_FILE_NAME_MAX_SZ);
240 len = strlen(p_dev) - 5;
241 if (len < MIN_NVME_DEV_NAME_LEN)
248 memset(file_name, 0,
sizeof(file_name));
249 ni_strcpy(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, SYS_PARAMS_PREFIX_PATH);
250 #if defined(_ANDROID) || defined(__OPENHARMONY__)
252 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
255 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
257 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, KERNEL_NVME_MAX_SEG_PATH,
258 sizeof(file_name) - SYS_PREFIX_SZ - len);
267 err =
ni_fscanf(p_file,
"%d", &max_segments);
277 memset(file_name, 0,
sizeof(file_name));
278 ni_strcpy(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, SYS_PARAMS_PREFIX_PATH);
279 #if defined(_ANDROID) || defined(__OPENHARMONY__)
281 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
284 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
286 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, KERNEL_NVME_MIN_IO_SZ_PATH,
287 sizeof(file_name) - SYS_PREFIX_SZ - len);
296 err =
ni_fscanf(p_file,
"%d", &min_io_size);
306 memset(file_name, 0,
sizeof(file_name));
307 ni_strcpy(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, SYS_PARAMS_PREFIX_PATH);
308 #if defined(_ANDROID) || defined(__OPENHARMONY__)
310 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
313 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
315 ni_strncat(file_name, KERNEL_NVME_FILE_NAME_MAX_SZ, KERNEL_NVME_MAX_HW_SEC_KB_PATH,
316 sizeof(file_name) - SYS_PREFIX_SZ - len);
325 err =
ni_fscanf(p_file,
"%d", &max_hw_sectors_kb);
332 if (ni_min(min_io_size * max_segments, max_hw_sectors_kb * 1024) >
333 MAX_IO_TRANSFER_SIZE)
335 io_size = MAX_IO_TRANSFER_SIZE;
339 io_size = ni_min(min_io_size * max_segments, max_hw_sectors_kb * 1024);
363 LARGE_INTEGER StartCount;
364 LARGE_INTEGER StopCount;
365 LARGE_INTEGER Frequency;
366 QueryPerformanceCounter(&StartCount);
367 QueryPerformanceFrequency(&Frequency);
369 StartCount.QuadPart + usec * (Frequency.QuadPart / 1000000);
372 QueryPerformanceCounter(&StartCount);
373 }
while (StartCount.QuadPart < StopCount.QuadPart);
377 LARGE_INTEGER ft = {0};
384 timer = CreateWaitableTimer(NULL, TRUE, NULL);
387 retval = SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
390 WaitForSingleObject(timer, INFINITE);
396 LARGE_INTEGER StartCount;
397 LARGE_INTEGER StopCount;
398 LARGE_INTEGER Frequency;
399 QueryPerformanceCounter(&StartCount);
400 QueryPerformanceFrequency(&Frequency);
402 StartCount.QuadPart + usec * (Frequency.QuadPart / 1000000);
405 QueryPerformanceCounter(&StartCount);
406 }
while (StartCount.QuadPart < StopCount.QuadPart);
410 if (usec < 0xFFFFFFFF)
420 char *
ni_strtok(
char *s,
const char *delim,
char **saveptr)
424 if (!s && !(s = *saveptr))
428 s += strspn(s, delim);
438 s += strcspn(s, delim);
451 #if defined(_MSC_VER)
452 if(strcpy_s(dest, dmax, src))
457 if (!dest || !dmax || !src)
465 const char *overlap_bumper;
471 overlap_bumper = src;
473 if (dest == overlap_bumper) {
487 overlap_bumper = dest;
489 if (src == overlap_bumper) {
515 const char *src,
size_t slen)
517 #if defined(_MSC_VER)
518 if(strncpy_s(dest, dmax, src, slen))
523 if (slen == 0 && dest && dmax) {
528 if (!dest || !dmax || !src)
532 const char *overlap_bumper;
538 overlap_bumper = src;
541 if (dest == overlap_bumper) {
552 #ifdef SAFECLIB_STR_NULL_SLACK
554 memset(dest, 0, dmax);
570 #ifdef SAFECLIB_STR_NULL_SLACK
572 memset(dest, 0, dmax);
590 overlap_bumper = dest;
593 if (src == overlap_bumper) {
604 #ifdef SAFECLIB_STR_NULL_SLACK
606 memset(dest, 0, dmax);
622 #ifdef SAFECLIB_STR_NULL_SLACK
624 memset(dest, 0, dmax);
654 #if defined(_MSC_VER)
655 if(strerror_s(dest, dmax, errnum))
660 if (!dest || !dmax || (dmax >= (
size_t)(-1)))
663 const char *tmpbuf = strerror(errnum);
668 len = strerrorlen_s(errnum);
670 const char *tmpbuf = strerror(errnum);
672 }
else if (dmax > 3) {
673 const char *tmpbuf = strerror(errnum);
687 #if defined(_MSC_VER)
688 if(strcat_s(dest, dmax, src))
693 if (!dest || !dmax || !src)
698 const char *overlap_bumper;
704 overlap_bumper = src;
706 while (*dest !=
'\0') {
707 if (dest == overlap_bumper) {
722 if (dest == overlap_bumper) {
730 #ifdef SAFECLIB_STR_NULL_SLACK
732 memset(dest, 0, dmax);
749 overlap_bumper = dest;
752 while (*dest !=
'\0') {
767 if (src == overlap_bumper) {
776 #ifdef SAFECLIB_STR_NULL_SLACK
778 memset(dest, 0, dmax);
804 const char *src,
size_t slen)
808 const char *overlap_bumper;
810 if (slen == 0 && !dest && !dmax) {
814 if (!dest || !dmax || !src)
816 else if (slen == 0) {
829 overlap_bumper = src;
832 while (*dest !=
'\0') {
834 if (dest == overlap_bumper) {
850 if (dest == overlap_bumper) {
860 #ifdef SAFECLIB_STR_NULL_SLACK
863 memset(dest, 0, dmax);
879 #ifdef SAFECLIB_STR_NULL_SLACK
882 memset(dest, 0, dmax);
900 overlap_bumper = dest;
903 while (*dest !=
'\0') {
919 if (src == overlap_bumper) {
929 #ifdef SAFECLIB_STR_NULL_SLACK
932 memset(dest, 0, dmax);
948 #ifdef SAFECLIB_STR_NULL_SLACK
951 memset(dest, 0, dmax);
981 #if defined(_MSC_VER)
982 if(fopen_s(fp, filename, mode))
987 if (!fp || !filename || !mode)
990 *fp = fopen(filename, mode);
1001 #if defined(_MSC_VER)
1002 if (localtime_s(dest, src))
1012 dest = localtime(src);
1020 #if defined(_MSC_VER)
1022 va_start(args, fmt);
1023 ret = vfscanf_s(stream, fmt, args);
1026 if (!stream || !fmt)
1029 va_start(args, fmt);
1030 ret = vfscanf(stream, fmt, args);
1036 int ni_vsprintf(
char *dest,
const size_t dmax,
const char *fmt, va_list args)
1038 #if defined(_MSC_VER)
1039 int ret = vsprintf_s(dest, dmax, fmt, args);
1042 if (!dest || !dmax || !fmt)
1045 int ret = vsnprintf(dest, dmax, fmt, args);
1049 }
else if ((
size_t)ret >= dmax) {
1051 char *overrun = dest + dmax - 1;
1062 va_start(args, fmt);
1091 if (NULL == p_buffer_pool)
1210 if (NULL != p_buffer_pool &&
1214 memset(p_buffer, 0,
sizeof(
ni_buf_t));
1222 p_buffer->
buf = p_buf;
1223 p_buffer->
pool = p_buffer_pool;
1246 int32_t number_of_buffers,
1247 int width,
int height,
1248 int height_align,
int factor)
1258 width_aligned = ((((width * factor) + 127) / 128) * 128) / factor;
1259 height_aligned = height;
1262 width_aligned = ((width + 31) / 32) * 32;
1263 height_aligned = ((height + 7) / 8) * 8;
1266 height_aligned = ((height + 15) / 16) * 16;
1270 int luma_size = width_aligned * height_aligned * factor;
1275 int chroma_width_aligned =
1276 ((((width / 2 * factor) + 127) / 128) * 128) / factor;
1277 int chroma_height_aligned = height_aligned / 2;
1278 chroma_b_size = chroma_r_size =
1279 chroma_width_aligned * chroma_height_aligned * factor;
1282 chroma_b_size = luma_size / 4;
1283 chroma_r_size = chroma_b_size;
1285 uint32_t buffer_size = luma_size + chroma_b_size + chroma_r_size +
1298 "Warning init dec_fme Buf pool already with size %u\n",
1304 "Warning resolution %dx%d memory buffer size %u "
1305 "> %u (existing buffer size), re-allocating !\n",
1306 width, height, buffer_size,
1313 "INFO resolution %dx%d memory buffer size %u <= "
1314 "%u (existing buffer size), continue !\n",
1315 width, height, buffer_size,
1334 "ni_dec_fme_buffer_pool_initialize: entries %d entry size "
1336 number_of_buffers, buffer_size);
1339 for (i = 0; i < number_of_buffers; i++)
1381 int32_t count_free = 0;
1400 free(p_buffer_pool);
1444 free(p_buffer_pool);
1456 if (NULL != p_buffer_pool &&
1508 for (i = 0; i < number_of_buffers; i++)
1525 for (i = 0; i < 200; i++)
1530 "FATAL ERROR: Failed to allocate pool buffer for pool :%p\n",
1543 if (NULL == p_buffer_pool)
1595 if (!buf || !p_buffer_pool)
1660 static ni_retcode_t ni_search_file(
const char *p_dev,
char *cmd,
char *cmd_ret,
1666 if (access(p_dev, F_OK) == -1)
1672 cmd_fp = popen(cmd,
"r");
1678 if (fgets(cmd_ret, cmd_ret_len, cmd_fp) == 0)
1706 NI_UNUSED static uint32_t remove_substring_pattern(
char *main_str,
const char *pattern)
1709 uint32_t match_length;
1710 uint32_t matched_chr;
1711 char char_match_pattern[11] =
"";
1712 uint32_t pattern_matched = 0;
1713 uint32_t pattern_start = 0;
1714 const char digit_match_pattern[11] =
"0123456789";
1717 if (!main_str || !pattern || !*main_str || !*pattern)
1723 for (i = 0; i < strlen(main_str) && !pattern_matched; i++)
1725 pattern_matched = 0;
1728 for (j = 0; j < strlen(pattern); j++)
1732 if (pattern[j] ==
'+')
1737 else if (pattern[j] ==
'#')
1739 memcpy(char_match_pattern, digit_match_pattern, strlen(digit_match_pattern) + 1);
1743 memcpy(char_match_pattern, pattern + j, 1);
1744 memset(char_match_pattern + 1, 0, 1);
1747 if (pattern[j+1] ==
'+')
1749 while (main_str[i + match_length] && strchr(char_match_pattern, (
int) main_str[i + match_length]))
1756 else if (main_str[i + match_length] && strchr(char_match_pattern, (
int) main_str[i + match_length]))
1767 else if ((j + 1) >= strlen(pattern))
1769 pattern_matched = 1;
1776 if (pattern_matched)
1778 uint32_t orig_main_str_len = (uint32_t)strlen(main_str);
1779 memmove(main_str + pattern_start, main_str + pattern_start + match_length,
1780 strlen(main_str + pattern_start + match_length));
1781 main_str[orig_main_str_len - match_length] = 0;
1802 if (!p_dev || !p_out_buf)
1809 "Automatic namespaceID discovery not supported in Windows. Using "
1811 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1822 char cmd_ret[60] = {0};
1823 char command[128] = {0};
1824 snprintf(command,
sizeof(command) - 1,
1825 "diskutil info %s | grep 'Media Name'", p_dev);
1827 cmd_fp = popen(command,
"r");
1833 if (fgets(cmd_ret,
sizeof(cmd_ret) - 1, cmd_fp) == NULL)
1842 if (strcasestr(cmd_ret,
"Quadra") != NULL)
1844 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1852 #elif defined(XCODER_LINUX_VIRTIO_DRIVER_ENABLED)
1853 ni_log(
NI_LOG_TRACE,
"The device is already considered as a block divice in Linux virtual machine with VirtIO driver.\n");
1854 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1858 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1887 char cmd_ret[60] = {0};
1888 char command[128] = {0};
1889 snprintf(command,
sizeof(command) - 1,
1890 "diskutil info %s | grep 'Media Name'", p_dev);
1892 cmd_fp = popen(command,
"r");
1898 if (fgets(cmd_ret,
sizeof(cmd_ret) - 1, cmd_fp) == NULL)
1907 if (strcasestr(cmd_ret,
"Quadra") != NULL)
1933 if (*pp_table != NULL)
1994 int64_t timestamp, uint64_t data_info)
2019 uint64_t frame_info, int64_t *p_timestamp,
2020 int32_t threshold, int32_t print,
2026 __func__, frame_info);
2029 print, p_buffer_pool);
2036 "%s: timestamp=%" PRId64
", frame_info=%" PRId64
", err=%d\n",
2037 __func__, *p_timestamp, frame_info, err);
2047 threshold, print, p_buffer_pool);
2054 if (!pts_list || !dts_list)
2063 time_t now = time(NULL);
2095 uint64_t frame_offset, int64_t *p_timestamp,
2101 if (!p_table || !p_timestamp || !p_buffer_pool)
2108 __func__, frame_offset);
2110 err =
ni_queue_pop(&p_table->
list, frame_offset, p_timestamp, threshold, 0, p_buffer_pool);
2117 "%s: timestamp=%" PRId64
", frame_offset=%" PRId64
", err=%d\n",
2118 __func__, *p_timestamp, frame_offset, err);
2136 if (!p_queue || !name)
2206 "%s: queue overflow, remove oldest entry, count=%u\n",
2207 __func__, p_queue->
count);
2231 int64_t *p_timestamp, int32_t threshold,
2240 if (!p_queue || !p_timestamp)
2270 while (temp && !found)
2272 if (frame_info < temp->frame_info)
2291 temp_prev = temp->
p_prev;
2301 p_queue->
p_last = temp_prev;
2322 p_queue->
name, count);
2336 int64_t *p_timestamp, int32_t threshold,
2346 if (!p_queue || !p_timestamp)
2375 while ((temp) && (!found))
2377 if (llabs((
int)frame_info - (
int)temp->
frame_info) <= threshold)
2388 p_queue->
p_last = temp_prev;
2410 p_queue->
name, count);
2447 temp_next = temp->
p_next;
2454 left, p_queue->
count);
2473 char buff[20] = {0};
2492 struct tm *ltime = NULL;
2493 struct tm temp_time;
2499 strftime(buff, 20,
"%Y-%m-%d %H:%M:%S", ltime);
2529 if (!strcmp(p_str,
"1") || !strcmp(p_str,
"true") || !strcmp(p_str,
"yes"))
2534 if (!strcmp(p_str,
"0") || !strcmp(p_str,
"false") || !strcmp(p_str,
"no"))
2553 int32_t v = strtol(p_str, &end, 0);
2555 if (end == p_str || *end !=
'\0')
2573 double v = strtod(p_str, &end);
2575 if (end == p_str || *end !=
'\0')
2593 for (i = 0; names[i]; i++)
2595 if (!strcmp(arg, names[i]))
2615 return (tv.tv_sec * 1000000LL + tv.tv_usec);
2621 LARGE_INTEGER frequency;
2622 LARGE_INTEGER count;
2623 uint64_t time_sec, time_nsec;
2626 QueryPerformanceFrequency(&frequency);
2628 QueryPerformanceCounter(&count);
2630 time_sec = count.QuadPart / frequency.QuadPart;
2631 time_nsec = (count.QuadPart - time_sec * frequency.QuadPart) *
2632 1000000000LL / frequency.QuadPart;
2634 return (time_sec * 1000000000LL + time_nsec);
2637 clock_gettime(CLOCK_REALTIME, &ts);
2638 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
2668 plane_stride[0] = ((
NI_MIN_WIDTH * factor + 127) / 128) * 128;
2670 (((
NI_MIN_WIDTH / (is_semiplanar ? 1 : 2) * factor) + 127) /
2673 plane_stride[2] = (is_semiplanar ? 0 : plane_stride[1]);
2676 width = ((width + 1) / 2) * 2;
2677 plane_stride[0] = ((width * factor + 127) / 128) * 128;
2679 (((width / (is_semiplanar ? 1 : 2) * factor) + 127) / 128) *
2681 plane_stride[2] = (is_semiplanar ? 0 : plane_stride[1]);
2690 plane_height[0] = ((height + 1) / 2) * 2;
2692 plane_height[1] = plane_height[2] = plane_height[0] / 2;
2717 plane_height[0] = ((height + 1) / 2) * 2;
2718 plane_height[1] = plane_height[2] = plane_height[0] / 2;
2726 plane_stride[2] = plane_stride[1];
2727 plane_stride[3] = 0;
2733 plane_stride[2] = plane_stride[1];
2734 plane_stride[3] = 0;
2739 plane_stride[1] = plane_stride[0];
2740 plane_stride[2] = 0;
2741 plane_stride[3] = 0;
2746 plane_stride[1] = plane_stride[0];
2747 plane_stride[2] = 0;
2748 plane_stride[3] = 0;
2754 plane_stride[1] = 0;
2755 plane_stride[2] = 0;
2756 plane_stride[3] = 0;
2761 plane_stride[1] = plane_stride[0];
2762 plane_stride[2] = 0;
2763 plane_stride[3] = 0;
2771 plane_height[1] = plane_height[2] = 0;
2774 plane_stride[1] = 0;
2775 plane_stride[2] = 0;
2776 plane_stride[3] = 0;
2817 width = ((width + 1) / 2) * 2;
2823 "%s dst_stride %d/%d/%d height %d/%d/%d pix_fmt %d\n",
2824 __func__, plane_stride[0], plane_stride[1], plane_stride[2],
2825 plane_height[0], plane_height[1], plane_height[2], pix_fmt);
2851 int frame_width,
int frame_height,
int factor,
2852 int is_semiplanar,
int conf_win_right,
2865 if (p_dst[i] == p_src[i])
2872 (src_height[i] < dst_height[i] ? src_height[i] : dst_height[i]);
2873 uint8_t *dst = p_dst[i];
2874 const uint8_t *src = (
const uint8_t *)p_src[i];
2879 if (0 == i || is_semiplanar)
2881 pad_len_bytes = dst_stride[i] - frame_width * factor;
2886 pad_len_bytes = dst_stride[i] - frame_width / 2 * factor;
2889 if (0 == pad_len_bytes && conf_win_right > 0)
2893 pad_len_bytes = conf_win_right * factor;
2898 pad_len_bytes = conf_win_right * factor / 2;
2903 "%s plane %d stride padding: %d pixel (%d bytes), copy height: "
2905 __func__, i, pad_len_bytes / factor, pad_len_bytes,
2908 for (; height > 0; height--)
2911 (src_stride[i] < dst_stride[i] ? src_stride[i] : dst_stride[i]));
2912 dst += dst_stride[i];
2922 uint8_t *tmp_dst = dst - pad_len_bytes;
2923 for (j = 0; j < pad_len_bytes / factor; j++)
2925 memcpy(tmp_dst, dst - pad_len_bytes - factor, factor);
2931 memset(dst - pad_len_bytes, *(dst - pad_len_bytes - 1),
2935 src += src_stride[i];
2939 int padding_height = dst_height[i] - src_height[i];
2940 if (padding_height > 0)
2944 src = dst - dst_stride[i];
2945 for (; padding_height > 0; padding_height--)
2947 memcpy(dst, src, dst_stride[i]);
2948 dst += dst_stride[i];
2974 int frame_width,
int frame_height,
int factor,
2975 int is_semiplanar,
int conf_win_right,
2982 "%s dst_stride %d/%d/%d src_stride %d/%d/%d dst_height "
2983 "%d/%d/%d src_height %d/%d/%d\n",
2984 __func__, dst_stride[0], dst_stride[1], dst_stride[2], src_stride[0],
2985 src_stride[1], src_stride[2], dst_height[0], dst_height[1],
2986 dst_height[2], src_height[0], src_height[1], src_height[2]);
2993 is_semiplanar, conf_win_right, dst_stride,
2994 dst_height, src_stride, src_height, i);
3020 int frame_width,
int frame_height,
3029 "%s frame_width %d frame_height %d factor %d conf_win_right %d "
3030 "dst_stride %d/%d/%d src_stride %d/%d/%d dst_height "
3031 "%d/%d/%d src_height %d/%d/%d pix_fmt %d\n",
3032 __func__, frame_width, frame_height, factor, conf_win_right,
3033 dst_stride[0], dst_stride[1], dst_stride[2], src_stride[0],
3034 src_stride[1], src_stride[2], dst_height[0], dst_height[1],
3035 dst_height[2], src_height[0], src_height[1], src_height[2],
3039 int is_semiplanar = 0;
3059 frame_width, frame_height, 4,
3060 is_semiplanar, conf_win_right,
3061 dst_stride, dst_height,
3062 src_stride, src_height,
3068 is_semiplanar, conf_win_right, dst_stride,
3069 dst_height, src_stride, src_height);
3101 int frame_width,
int frame_height,
3102 int factor,
int mode)
3105 int y_444p_linesize = frame_width * factor;
3106 int uv_444p_linesize = y_444p_linesize;
3111 if (p_dst0[0] == p_dst1[0] && p_dst0[1] == p_dst1[1] &&
3112 p_dst0[2] == p_dst1[2])
3119 for (i = 0; i < frame_height; i++)
3121 memcpy(&p_dst0[0][i * y_420p_linesize], &p_src[0][i * y_444p_linesize],
3130 for (i = 0; i < frame_height; i++)
3132 memcpy(&p_dst1[0][i * y_420p_linesize],
3133 &p_src[1][i * y_444p_linesize], y_444p_linesize);
3136 for (i = 0; i < frame_height / 2; i++)
3138 for (j = 0; j < frame_width * factor / 2; j += factor)
3142 memcpy(&p_dst0[1][i * uv_420p_linesize + j],
3143 &p_src[2][2 * i * uv_444p_linesize + 2 * j],
3145 memcpy(&p_dst0[2][i * uv_420p_linesize + j],
3146 &p_src[2][2 * i * uv_444p_linesize + (2 * j + factor)],
3149 memcpy(&p_dst1[1][i * uv_420p_linesize + j],
3150 &p_src[2][(2 * i + 1) * uv_444p_linesize + 2 * j],
3152 memcpy(&p_dst1[2][i * uv_420p_linesize + j],
3153 &p_src[2][(2 * i + 1) * uv_444p_linesize + (2 * j + factor)],
3161 for (i = 0; i < frame_height / 2; i++)
3163 for (j = 0; j < frame_width * factor / 2; j += factor)
3167 memcpy(&p_dst1[1][i * uv_420p_linesize + j],
3168 &p_src[1][2 * i * uv_444p_linesize + (2 * j + factor)],
3171 memcpy(&p_dst1[0][2 * i * uv_444p_linesize + 2 * j],
3172 &p_src[1][(2 * i + 1) * uv_444p_linesize + 2 * j],
3175 memcpy(&p_dst0[1][i * uv_420p_linesize + j],
3176 &p_src[1][2 * i * uv_444p_linesize + 2 * j],
3181 memcpy(&p_dst1[2][i * uv_420p_linesize + j],
3182 &p_src[2][2 * i * uv_444p_linesize + (2 * j + factor)],
3185 memcpy(&p_dst1[0][(2 * i + 1) * uv_444p_linesize + 2 * j],
3186 &p_src[2][(2 * i + 1) * uv_444p_linesize + 2 * j],
3189 memcpy(&p_dst0[2][i * uv_420p_linesize + j],
3190 &p_src[2][2 * i * uv_444p_linesize + 2 * j],
3213 int insert_bytes = 0;
3214 uint8_t *buf_curr = buf;
3215 uint8_t *buf_end = buf + size - 1;
3216 int zeros = 0, insert_ep3_byte;
3220 for (; buf_curr <= buf_end; buf_curr++)
3224 insert_ep3_byte = (*buf_curr <= 3);
3225 if (insert_ep3_byte)
3228 memmove(buf_curr + 1, buf_curr, buf_end - buf_curr + 1);
3249 return insert_bytes;
3266 int remove_bytes = 0;
3267 uint8_t *buf_curr = buf;
3268 uint8_t *buf_end = buf + size - 1;
3269 int zeros = 0, remove_ep3_byte;
3273 for (; buf_curr < buf_end; buf_curr++)
3277 remove_ep3_byte = (*buf_curr == 0x03 && *(buf_curr + 1) <= 3);
3278 if (remove_ep3_byte)
3281 memmove(buf_curr, buf_curr + 1, buf_end - buf_curr);
3301 return remove_bytes;
3309 static uint32_t ni_ai_type_get_bytes(
const uint32_t type)
3335 static void ni_ai_integer_convert(
const void *src,
void *dest,
3339 unsigned char all_zeros[] = {0x00, 0x00, 0x00, 0x00,
3340 0x00, 0x00, 0x00, 0x00};
3341 unsigned char all_ones[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3342 uint32_t src_sz = ni_ai_type_get_bytes(src_dtype);
3343 uint32_t dest_sz = ni_ai_type_get_bytes(dst_dtype);
3344 unsigned char *buffer = all_zeros;
3346 if (((int8_t *)src)[src_sz - 1] & 0x80)
3350 memcpy(buffer, src, src_sz);
3351 memcpy(dest, buffer, dest_sz);
3354 static float ni_ai_int8_to_fp32(
signed char val,
signed char fixedPointPos)
3356 float result = 0.0f;
3358 if (fixedPointPos > 0)
3360 result = (float)val * (1.0f / ((
float)(1 << fixedPointPos)));
3363 result = (float)val * ((
float)(1 << -fixedPointPos));
3369 static float ni_ai_int16_to_fp32(int16_t val,
signed char fixedPointPos)
3371 float result = 0.0f;
3373 if (fixedPointPos > 0)
3375 result = (float)val * (1.0f / ((
float)(1 << fixedPointPos)));
3378 result = (float)val * ((
float)(1 << -fixedPointPos));
3384 static float ni_ai_uint8_to_fp32(uint8_t val, int32_t zeroPoint,
float scale)
3386 float result = 0.0f;
3388 result = (float)(val - (uint8_t)zeroPoint) * scale;
3393 static float ni_ai_fp16_to_fp32(
const short in)
3401 const _fp32_t magic = {(254 - 15) << 23};
3402 const _fp32_t infnan = {(127 + 16) << 23};
3405 o.u = (in & 0x7fff) << 13;
3407 if (o.f >= infnan.f)
3412 o.u |= (in & 0x8000) << 16;
3416 static float ni_ai_affine_to_fp32(int32_t val, int32_t zeroPoint,
float scale)
3418 float result = 0.0f;
3419 result = ((float)val - zeroPoint) * scale;
3423 static uint32_t ni_ai_get_tensor_size(int32_t *shape, uint32_t dim_num,
3429 if (NULL == shape || 0 == dim_num)
3434 for (i = 0; i < dim_num; i++)
3438 sz *= ni_ai_type_get_bytes(type);
3443 static uint32_t ni_ai_get_element_num(int32_t *sizes, uint32_t num_of_dims,
3444 int32_t data_format)
3450 sz = ni_ai_get_tensor_size(sizes, num_of_dims, data_format);
3451 dsize = ni_ai_type_get_bytes(data_format);
3454 num = (uint32_t)(sz / dsize);
3463 static int32_t ni_ai_type_is_integer(
const int32_t type)
3483 static int32_t ni_ai_type_is_signed(
const int32_t type)
3503 static void ni_ai_type_get_range(int32_t type,
double *max_range,
3510 bits = ni_ai_type_get_bytes(type) * 8;
3511 if (ni_ai_type_is_integer(type))
3513 if (ni_ai_type_is_signed(type))
3515 from = (double)(-(1L << (bits - 1)));
3516 to = (double)((1UL << (bits - 1)) - 1);
3520 to = (double)((1UL << bits) - 1);
3523 if (NULL != max_range)
3527 if (NULL != min_range)
3533 static inline double ni_abs(
double x)
3535 return x < 0 ? -x : x;
3538 static double ni_ai_copy_sign(
double number,
double sign)
3540 double value = ni_abs(number);
3541 return (sign > 0) ? value : (-value);
3544 static inline int ni_ai_math_floorf(
double x)
3546 return x >= 0 ? (int)x : (
int)x - 1;
3549 static double ni_ai_rint(
double x)
3551 #define _EPSILON 1e-8
3557 decimal = x - intpart;
3558 inter = (double)intpart;
3560 if (ni_abs((ni_abs(decimal) - 0.5f)) <
_EPSILON)
3562 inter += (int32_t)(inter) % 2;
3565 return ni_ai_copy_sign(ni_ai_math_floorf(ni_abs(x) + 0.5f), x);
3571 static int32_t ni_ai_fp32_to_dfp(
const float in,
const signed char fl,
3577 ni_ai_type_get_range(type, &max_range, &min_range);
3580 data = (int32_t)ni_ai_rint(in * (
float)(1 << fl));
3583 data = (int32_t)ni_ai_rint(in * (1.0f / (
float)(1 << -fl)));
3585 data = ni_min(data, (int32_t)max_range);
3586 data = ni_max(data, (int32_t)min_range);
3591 static int32_t ni_ai_fp32_to_affine(
const float in,
const float scale,
3592 const int zero_point,
const int32_t type)
3597 ni_ai_type_get_range(type, &max_range, &min_range);
3598 data = (int32_t)(ni_ai_rint(in / scale) + zero_point);
3599 data = ni_max((int32_t)min_range, ni_min((int32_t)max_range, data));
3603 static unsigned short ni_ai_fp32_to_bfp16_rtne(
float in)
3611 memcpy(&fp32, &in,
sizeof(uint32_t));
3615 uint32_t rounding_bias = 0x7fff + lsb;
3617 if ((
float)0x7FC00000 == in)
3622 fp32 += rounding_bias;
3623 out = (
unsigned short)(fp32 >> 16);
3629 static unsigned short ni_ai_fp32_to_fp16(
float in)
3637 memcpy((uint8_t *)&fp32, (uint8_t *)&in,
sizeof(uint32_t));
3639 t1 = (fp32 & 0x80000000u) >> 16;
3640 t2 = (fp32 & 0x7F800000u) >> 13;
3641 t3 = (fp32 & 0x007FE000u) >> 13;
3643 if (t2 >= 0x023c00u)
3646 }
else if (t2 <= 0x01c000u)
3652 fp16 = t1 | t2 | t3;
3655 return (
unsigned short)fp16;
3658 static void ni_ai_float32_to_dtype(
float src,
unsigned char *dst,
3659 const int32_t data_type,
3660 const int32_t quant_format,
3661 signed char fixed_point_pos,
float tf_scale,
3667 memcpy(dst, &src,
sizeof(
float));
3670 *(int16_t *)dst = ni_ai_fp32_to_fp16(src);
3673 *(int16_t *)dst = ni_ai_fp32_to_bfp16_rtne(src);
3679 int32_t dst_value = 0;
3680 switch (quant_format)
3684 ni_ai_fp32_to_dfp(src, fixed_point_pos, data_type);
3687 dst_value = ni_ai_fp32_to_affine(src, tf_scale, tf_zerop,
3691 dst_value = (int32_t)src;
3713 if (!p_network || !dst || dst_len == 0 || !p_packet || !p_packet->
p_data)
3737 static int open_tensor_rsrc_file(
struct tensor_rsrc *rsrc,
void *data,
3741 ni_fopen(&f, (
const char *)data,
"r");
3743 return rsrc->
private ? 0 : -1;
3746 static int get_tensor_rsrc_from_file(
struct tensor_rsrc *rsrc,
float *value)
3748 FILE *fp = (FILE *)rsrc->
private;
3760 static void close_tensor_rsrc_file(
struct tensor_rsrc *rsrc)
3762 FILE *fp = (FILE *)rsrc->
private;
3766 static int open_tensor_rsrc_rawdata(
struct tensor_rsrc *rsrc,
void *data,
3775 static int get_tensor_rsrc_from_rawdata(
struct tensor_rsrc *rsrc,
float *value)
3777 float *tensors = (
float *)rsrc->
private;
3789 static void close_tensor_rsrc_rawdata(
struct tensor_rsrc *rsrc)
3794 uint8_t *dst, uint32_t dst_len,
void *tensor, uint32_t src_len,
3796 int (*open_tensor_rsrc)(
struct tensor_rsrc *,
void *, uint32_t),
3797 int (*get_tensor_rsrc)(
struct tensor_rsrc *,
float *),
3800 uint8_t *tensor_data;
3804 int32_t data_format;
3805 int32_t quant_format;
3806 int32_t fixed_point_pos;
3817 sz = ni_ai_get_element_num((int32_t *)p_param->
sizes, p_param->
num_of_dims,
3819 stride = ni_ai_type_get_bytes(data_format);
3821 if (sz * stride *
sizeof(uint8_t) != dst_len)
3832 if (open_tensor_rsrc(rsrc, tensor, src_len) != 0)
3838 memset(dst, 0, sz * stride *
sizeof(uint8_t));
3840 for (i = 0; i < sz; i++)
3842 if (get_tensor_rsrc(rsrc, &fval) == 0)
3844 ni_ai_float32_to_dtype(fval, &tensor_data[stride * i], data_format,
3845 quant_format, fixed_point_pos, tf_scale,
3853 close_tensor_rsrc(rsrc);
3859 const char *tensor_file,
3862 return ni_network_tensor_to_data(
3863 dst, dst_len, (
void *)tensor_file, 0, p_param, open_tensor_rsrc_file,
3864 get_tensor_rsrc_from_file, close_tensor_rsrc_file);
3872 return ni_network_tensor_to_data(
3873 dst, dst_len, src, src_len, p_param, open_tensor_rsrc_rawdata,
3874 get_tensor_rsrc_from_rawdata, close_tensor_rsrc_rawdata);
3885 float *dst_float = dst;
3888 if (!src || src_len == 0 || !dst || dst_len == 0 || !p_param)
3893 type_size = ni_ai_type_get_bytes(p_param->
data_format);
3900 ele_size *= p_param->
sizes[i];
3903 if (dst_len != ele_size *
sizeof(
float))
3919 uint8_t fix_pos = (uint8_t)p_param->
quant_data.
dfp.fixed_point_pos;
3921 for (i = 0; i < ele_size; i++, data += type_size)
3923 dst_float[i] = ni_ai_int8_to_fp32(*data, fix_pos);
3930 for (i = 0; i < ele_size; i++, data += type_size)
3932 int32_t src_value = 0;
3933 ni_ai_integer_convert(data, &src_value,
3937 ni_ai_affine_to_fp32(src_value, zero_point, scale);
3941 for (i = 0; i < ele_size; i++, data += type_size)
3943 void *float_data = (
void *)data;
3944 dst_float[i] = *((
float *)float_data);
3949 for (i = 0; i < ele_size; i++, data += type_size)
3951 dst_float[i] = ni_ai_fp16_to_fp32(*((
short *)data));
3958 for (i = 0; i < ele_size; i++, data += type_size)
3960 dst_float[i] = ni_ai_uint8_to_fp32(*data, zero_point, scale);
3966 for (i = 0; i < ele_size; i++, data += type_size)
3968 dst_float[i] = ni_ai_int16_to_fp32(*((
short *)data), fix_pos);
3972 for (i = 0; i < ele_size; i++, data += type_size)
3974 void *float_data = (
void *)data;
3975 dst_float[i] = *((
float *)float_data);
3987 return ni_ai_get_tensor_size((int32_t *)p_param->
sizes,
3993 uint32_t i, dims = 1;
3997 dims *= p_param->
sizes[i];
4005 #define SHA256_BLOCK_SIZE 32
4015 #define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
4016 #define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b))))
4018 #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
4019 #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
4020 #define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
4021 #define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
4022 #define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
4023 #define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))
4025 static const uint32_t ui32k[64] = {
4026 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
4027 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
4028 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
4029 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
4030 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
4031 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
4032 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
4033 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
4034 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
4035 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
4036 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
4040 uint32_t ui32a, ui32b, ui32c, ui32d, ui32e, ui32f, ui32g, ui32h, ui32i,
4041 ui32j, ui32t1, ui32t2, ui32m[64];
4043 for (ui32i = 0, ui32j = 0; ui32i < 16; ++ui32i, ui32j += 4)
4044 ui32m[ui32i] = (aui8Data[ui32j] << 24) | (aui8Data[ui32j + 1] << 16) |
4045 (aui8Data[ui32j + 2] << 8) | (aui8Data[ui32j + 3]);
4046 for (; ui32i < 64; ++ui32i)
4047 ui32m[ui32i] =
SIG1(ui32m[ui32i - 2]) + ui32m[ui32i - 7] +
4048 SIG0(ui32m[ui32i - 15]) + ui32m[ui32i - 16];
4059 for (ui32i = 0; ui32i < 64; ++ui32i)
4061 ui32t1 = ui32h +
EP1(ui32e) +
CH(ui32e, ui32f, ui32g) + ui32k[ui32i] +
4063 ui32t2 =
EP0(ui32a) +
MAJ(ui32a, ui32b, ui32c);
4067 ui32e = ui32d + ui32t1;
4071 ui32a = ui32t1 + ui32t2;
4103 for (ui32i = 0; ui32i < ui32Length; ++ui32i)
4151 for (ui32i = 0; ui32i < 4; ++ui32i)
4154 (psCtx->
aui32State[0] >> (24 - ui32i * 8)) & 0x000000ff;
4155 aui8Hash[ui32i + 4] =
4156 (psCtx->
aui32State[1] >> (24 - ui32i * 8)) & 0x000000ff;
4157 aui8Hash[ui32i + 8] =
4158 (psCtx->
aui32State[2] >> (24 - ui32i * 8)) & 0x000000ff;
4159 aui8Hash[ui32i + 12] =
4160 (psCtx->
aui32State[3] >> (24 - ui32i * 8)) & 0x000000ff;
4161 aui8Hash[ui32i + 16] =
4162 (psCtx->
aui32State[4] >> (24 - ui32i * 8)) & 0x000000ff;
4163 aui8Hash[ui32i + 20] =
4164 (psCtx->
aui32State[5] >> (24 - ui32i * 8)) & 0x000000ff;
4165 aui8Hash[ui32i + 24] =
4166 (psCtx->
aui32State[6] >> (24 - ui32i * 8)) & 0x000000ff;
4167 aui8Hash[ui32i + 28] =
4168 (psCtx->
aui32State[7] >> (24 - ui32i * 8)) & 0x000000ff;
4202 if (p_dst[0] == p_src[0] && p_dst[1] == p_src[1] && p_dst[2] == p_src[2] &&
4203 p_dst[3] == p_src[3])
4225 return libxcoder_api_ver;
4228 #ifndef DEPRECATION_AS_ERROR
4237 static char compat_fw_api_ver_str[5] =
"";
4239 if (!compat_fw_api_ver_str[0])
4241 compat_fw_api_ver_str[0] = \
4243 compat_fw_api_ver_str[1] =
'.';
4244 compat_fw_api_ver_str[2] = \
4247 compat_fw_api_ver_str[3] = \
4250 compat_fw_api_ver_str[3] = 0;
4251 compat_fw_api_ver_str[4] = 0;
4253 return &compat_fw_api_ver_str[0];
4270 if (!ver_str || !fmt_str) {
4274 fmt_str[0] = ver_str[0];
4276 fmt_str[2] = ver_str[1];
4278 if ((ver_str[0] <
'6' || (ver_str[0] ==
'6' && ver_str[1] <=
'q')) ||
4279 (ver_str[2] == 0)) {
4282 fmt_str[3] = ver_str[2];
4304 if (ver1[index] > ver2[index])
4306 else if (ver1[index] < ver2[index])
4311 if (ver1[index] > ver2[index])
4313 else if (ver1[index] < ver2[index])
4316 if ((ver1[index - 1] <
'6') || ((ver1[index - 1] ==
'6') && (ver1[index] <=
'q')))
4321 if (ver1[index] > ver2[index])
4323 else if (ver1[index] < ver2[index])
4336 static char release_ver_str[6] =
"";
4338 if (!release_ver_str[0])
4341 release_ver_str[1] =
'.';
4343 release_ver_str[3] =
'.';
4345 release_ver_str[5] = 0;
4347 return &release_ver_str[0];
4361 if (rc == ni_err_rc_description[i].rc)
4363 return ni_err_rc_description[i].
txt;
4366 return "rc not supported";
4381 if (!p_str || !key || !value)
4386 char *p = strchr(p_str,
'=');
4395 int64_t key_len = p - p_str;
4416 char key[64], value[64];
4417 char *curr = xcoderParams, *colon_pos;
4422 colon_pos = strchr(curr,
':');
4429 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
4433 "Error: xcoder-params p_config key/value not "
4462 curr = colon_pos + 1;
4465 curr += strlen(curr);
4490 char key[64], value[64];
4491 char *curr = xcoderGop, *colon_pos;
4496 colon_pos = strchr(curr,
':');
4503 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
4507 "Error: xcoder-params p_config key/value not "
4536 curr = colon_pos + 1;
4539 curr += strlen(curr);
4560 char key[64], value[64];
4561 char *curr = xcoderParams, *colon_pos;
4566 colon_pos = strchr(curr,
':');
4573 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
4577 "Error: decoder-params p_config key/value not "
4606 curr = colon_pos + 1;
4609 curr += strlen(curr);
4631 rc = InitializeCriticalSectionEx(mutex, 0, CRITICAL_SECTION_NO_DEBUG_INFO);
4642 ni_pthread_mutexattr_t attr;
4644 rc = pthread_mutexattr_init(&attr);
4650 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
4652 return pthread_mutex_init(mutex, &attr);
4667 DeleteCriticalSection(mutex);
4670 return pthread_mutex_destroy(mutex);
4688 EnterCriticalSection(mutex);
4690 rc = pthread_mutex_lock(mutex);
4714 LeaveCriticalSection(mutex);
4716 rc = pthread_mutex_unlock(mutex);
4727 static unsigned __stdcall __thread_worker(
void *arg)
4729 ni_pthread_t *t = (ni_pthread_t *)arg;
4730 t->rc = t->start_routine(t->arg);
4747 void *(*start_routine)(
void *),
void *arg)
4750 thread->start_routine = start_routine;
4754 (
void *)CreateThread(NULL, 0, win32thread_worker, thread, 0, NULL);
4756 (
void *)_beginthreadex(NULL, 0, __thread_worker, thread, 0, NULL);
4758 return !thread->handle;
4760 return pthread_create(thread, attr, start_routine, arg);
4776 DWORD rc = WaitForSingleObject(thread.handle, INFINITE);
4777 if (rc != WAIT_OBJECT_0)
4779 if (rc == WAIT_ABANDONED)
4785 *value_ptr = thread.rc;
4786 CloseHandle(thread.handle);
4789 return pthread_join(thread, value_ptr);
4803 const ni_pthread_condattr_t *attr)
4806 InitializeConditionVariable(cond);
4809 return pthread_cond_init(cond, attr);
4827 return pthread_cond_destroy(cond);
4842 WakeAllConditionVariable(cond);
4845 return pthread_cond_broadcast(cond);
4861 SleepConditionVariableCS(cond, mutex, INFINITE);
4864 return pthread_cond_wait(cond, mutex);
4879 WakeConditionVariable(cond);
4882 return pthread_cond_signal(cond);
4897 ni_pthread_mutex_t *mutex,
4898 const struct timespec *abstime)
4901 int64_t abs_ns = abstime->tv_sec * 1000000000LL + abstime->tv_nsec;
4904 if (!SleepConditionVariableCS(cond, mutex, t))
4906 DWORD err = GetLastError();
4907 if (err == ERROR_TIMEOUT)
4914 return pthread_cond_timedwait(cond, mutex, abstime);
4935 return pthread_sigmask(how, set, oldset);
4953 return "General Error";
4955 return "Not Initialized";
4957 return "Already Initialized";
4961 return "Resource Not Available";
4963 return "Create Network Failed";
4965 return "Input Buffer Full";
4967 return "Output Buffer Empty";
4969 return "Invalid Params";
4971 return "Error Start Network";
4973 return "Erorr Set Inout";
4975 return "Bad Option";
4979 return "Context Not Available";
4981 return "Model Not Found";
4985 return "Invalid Address";
4987 return "Out Of Memory";
4991 return "Invalid Instance";
4993 return "IO Not Allowed";
4995 return "Network Not Ready";
4997 return "Other Error";
5012 float current_ma,voltage_mv;
5019 snprintf(pcb_config,
sizeof(pcb_config),
"%.2s", &serial_number[2]);
5021 float current_value = (float)current_data;
5022 voltage_mv = 12000.0f;
5023 if (strncmp(pcb_config,
"A1", 2) == 0)
5027 else if ((strncmp(pcb_config,
"A2", 2) == 0) || (strncmp(pcb_config,
"A3", 2) == 0))
5031 else if ((strncmp(pcb_config,
"AA", 2) == 0) || (strncmp(pcb_config,
"AB", 2) == 0))
5035 else if (strncmp(pcb_config,
"U0", 2) == 0)
5040 else if ((strncmp(pcb_config,
"U1", 2) == 0) || (strncmp(pcb_config,
"U2", 2) == 0) || (strncmp(pcb_config,
"U3", 2) == 0))
5044 else if (strncmp(pcb_config,
"UA", 2) == 0)
5048 else if (strncmp(pcb_config,
"S0", 2) == 0)
5052 else if ((strncmp(pcb_config,
"M0", 2) == 0) || (strncmp(pcb_config,
"M1", 2) == 0))
5055 voltage_mv = 3300.0f;
5061 power_mw = (uint32_t)((voltage_mv * current_ma) / 1000.0);
5086 static int ni_device_size_precheck_blockdev(
const char *p_dev,
const uint64_t size_needed )
5096 const char blockdev_command_dev[] =
"blockdev --getsize64 %s 2>/dev/null";
5097 FILE *blockdev_file = NULL;
5098 char *blockdev_command = NULL;
5099 char blockdev_result [70] = {0};
5106 const size_t path_len = strlen(p_dev);
5108 blockdev_command = (
char *)calloc(1,
sizeof(blockdev_command_dev) + path_len);
5109 if (!blockdev_command)
5114 snprintf(blockdev_command,
sizeof(blockdev_command_dev) + path_len, blockdev_command_dev, p_dev);
5118 blockdev_file = popen(blockdev_command,
"r");
5125 if (fgets(blockdev_result,
sizeof(blockdev_result), blockdev_file) == NULL)
5130 int status = pclose(blockdev_file);
5131 blockdev_file = NULL;
5135 if (*blockdev_result !=
'\0')
5137 char *endptr = NULL;
5139 unsigned long long this_size = strtoull(blockdev_result, &endptr, 10);
5141 if (errno != 0 || endptr == blockdev_result)
5145 else if (this_size < size_needed)
5147 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() blockdev size check failed. size: %" PRIu64
"\n", __func__, this_size);
5158 if (blockdev_command)
5160 free(blockdev_command);
5165 pclose(blockdev_file);
5192 static int ni_device_size_precheck_lsblk(
const char *p_dev,
const uint64_t size_needed)
5201 const char lsblk_command_dev[] =
"lsblk -b -o SIZE %s 2>/dev/null";
5202 char *lsblk_command = NULL;
5203 FILE *lsblk_file = NULL;
5204 char lsblk_result [70] = {0};
5205 char *fgets_result = NULL;
5212 const size_t path_len = strlen(p_dev);
5214 lsblk_command = (
char *)calloc(1,
sizeof(lsblk_command_dev) + path_len);
5220 snprintf(lsblk_command,
sizeof(lsblk_command_dev) + path_len, lsblk_command_dev, p_dev);
5224 lsblk_file = popen(lsblk_command,
"r");
5230 fgets_result = fgets(lsblk_result,
sizeof(lsblk_result), lsblk_file);
5236 memset(lsblk_result, 0,
sizeof(lsblk_result));
5238 fgets_result = fgets(lsblk_result,
sizeof(lsblk_result), lsblk_file);
5244 int status = pclose(lsblk_file);
5249 if (*lsblk_result !=
'\0')
5251 char *endptr = NULL;
5253 unsigned long long this_size = strtoull(lsblk_result, &endptr, 10);
5255 if (errno != 0 || endptr == lsblk_result)
5259 else if (this_size < size_needed)
5261 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() lsblk size check failed. size: %" PRIu64
"\n", __func__, this_size);
5274 free(lsblk_command);
5305 static int ni_device_vendor_id_precheck(
const char *p_dev)
5319 const char *vendor_path_devs[] = {
"/sys/class/block/%s/device/vendor",
"/sys/class/block/%s/device/device/vendor"};
5321 const char *last_slash = strrchr(p_dev,
'/');
5322 const char *device_name = (last_slash ? (last_slash + 1) : p_dev);
5324 const size_t path_len = strlen(p_dev);
5326 for (
size_t i = 0; i <
sizeof(vendor_path_devs)/
sizeof(vendor_path_devs[0]) && ret == 0; ++i)
5328 size_t template_len = strlen(vendor_path_devs[i]);
5329 char *vendor_path = (
char *)calloc(1, template_len + path_len);
5334 snprintf(vendor_path, template_len + path_len, vendor_path_devs[i], device_name);
5336 int fd = open(vendor_path, O_RDONLY);
5339 char vendor_id[10] = {0};
5340 int read_size = read(fd, vendor_id,
sizeof(vendor_id));
5341 if (read_size >= 4 && (
size_t)read_size <
sizeof(vendor_id))
5343 char *find_1d82 = strstr(vendor_id,
"1d82");
5387 static int ni_device_size_precheck_system_information(
const char *p_dev,
const uint64_t size_needed)
5402 const char block_path_dev[] =
"/sys/class/block/%s/size";
5403 const char logical_block_size_path_dev[] =
"/sys/class/block/%s/queue/logical_block_size";
5405 const char *last_slash = strrchr(p_dev,
'/');
5406 const char *device_name = (last_slash ? (last_slash + 1) : p_dev);
5408 const size_t path_len = strlen(p_dev);
5411 int logical_block_fd = -1;
5413 char *block_path = (
char *)calloc(1,
sizeof(block_path_dev) + path_len);
5414 char *logical_block_size_path = (
char *)calloc(1,
sizeof(logical_block_size_path_dev) + path_len);
5416 if (!block_path || !logical_block_size_path)
5421 snprintf(block_path,
sizeof(block_path_dev) + path_len, block_path_dev, device_name);
5422 snprintf(logical_block_size_path,
sizeof(logical_block_size_path_dev) + path_len, logical_block_size_path_dev, device_name);
5424 size_fd = open(block_path, O_RDONLY);
5425 logical_block_fd = open(logical_block_size_path, O_RDONLY);
5427 if (size_fd < 0 || logical_block_fd < 0)
5432 char this_size_str [20] = {0};
5433 char this_block_size_str [20] = {0};
5435 if (read(size_fd, this_size_str,
sizeof(this_size_str)) <= 0 || read(logical_block_fd, this_block_size_str,
sizeof(this_block_size_str)) <= 0)
5441 char *endptr1 = NULL;
5442 char *endptr2 = NULL;
5444 unsigned long long this_size = strtoull(this_size_str, &endptr1, 10);
5445 unsigned long long this_block_size = strtoull(this_block_size_str, &endptr2, 10);
5447 if (errno == 0 && endptr1 != this_size_str && endptr2 != this_block_size_str)
5449 if (this_size * this_block_size >= size_needed)
5455 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() read size check failed. size: %" PRIu64
"\n", __func__, this_size * this_block_size);
5466 if (logical_block_size_path)
5468 free(logical_block_size_path);
5475 if (logical_block_fd >= 0)
5477 close(logical_block_fd);
5514 const size_t path_len = strlen(p_dev);
5521 ret = ni_device_size_precheck_blockdev(p_dev, SIZE_NEEDED);
5531 ret = ni_device_size_precheck_lsblk(p_dev, SIZE_NEEDED);
5541 ret = ni_device_vendor_id_precheck(p_dev);
5551 ret = ni_device_size_precheck_system_information(p_dev, SIZE_NEEDED);