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 strcpy(file_name, SYS_PARAMS_PREFIX_PATH);
250 #if defined(_ANDROID) || defined(__OPENHARMONY__)
252 strncat(file_name, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
255 strncat(file_name, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
257 strncat(file_name, KERNEL_NVME_MAX_SEG_PATH,
258 sizeof(file_name) - SYS_PREFIX_SZ - len);
260 p_file = fopen(file_name,
"r");
267 err = fscanf(p_file,
"%d", &max_segments);
277 memset(file_name, 0,
sizeof(file_name));
278 strcpy(file_name, SYS_PARAMS_PREFIX_PATH);
279 #if defined(_ANDROID) || defined(__OPENHARMONY__)
281 strncat(file_name, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
284 strncat(file_name, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
286 strncat(file_name, KERNEL_NVME_MIN_IO_SZ_PATH,
287 sizeof(file_name) - SYS_PREFIX_SZ - len);
289 p_file = fopen(file_name,
"r");
296 err = fscanf(p_file,
"%d", &min_io_size);
306 memset(file_name, 0,
sizeof(file_name));
307 strcpy(file_name, SYS_PARAMS_PREFIX_PATH);
308 #if defined(_ANDROID) || defined(__OPENHARMONY__)
310 strncat(file_name, (
char *)(p_dev + 11),
sizeof(file_name) - SYS_PREFIX_SZ);
313 strncat(file_name, (
char *)(p_dev + 5),
sizeof(file_name) - SYS_PREFIX_SZ);
315 strncat(file_name, KERNEL_NVME_MAX_HW_SEC_KB_PATH,
316 sizeof(file_name) - SYS_PREFIX_SZ - len);
318 p_file = fopen(file_name,
"r");
325 err = 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);
472 if (NULL == p_buffer_pool)
591 if (NULL != p_buffer_pool &&
595 memset(p_buffer, 0,
sizeof(
ni_buf_t));
603 p_buffer->
buf = p_buf;
604 p_buffer->
pool = p_buffer_pool;
627 int32_t number_of_buffers,
628 int width,
int height,
629 int height_align,
int factor)
639 width_aligned = ((((width * factor) + 127) / 128) * 128) / factor;
640 height_aligned = height;
643 width_aligned = ((width + 31) / 32) * 32;
644 height_aligned = ((height + 7) / 8) * 8;
647 height_aligned = ((height + 15) / 16) * 16;
651 int luma_size = width_aligned * height_aligned * factor;
656 int chroma_width_aligned =
657 ((((width / 2 * factor) + 127) / 128) * 128) / factor;
658 int chroma_height_aligned = height_aligned / 2;
659 chroma_b_size = chroma_r_size =
660 chroma_width_aligned * chroma_height_aligned * factor;
663 chroma_b_size = luma_size / 4;
664 chroma_r_size = chroma_b_size;
666 uint32_t buffer_size = luma_size + chroma_b_size + chroma_r_size +
679 "Warning init dec_fme Buf pool already with size %u\n",
685 "Warning resolution %dx%d memory buffer size %u "
686 "> %u (existing buffer size), re-allocating !\n",
687 width, height, buffer_size,
694 "INFO resolution %dx%d memory buffer size %u <= "
695 "%u (existing buffer size), continue !\n",
696 width, height, buffer_size,
715 "ni_dec_fme_buffer_pool_initialize: entries %d entry size "
717 number_of_buffers, buffer_size);
720 for (i = 0; i < number_of_buffers; i++)
762 int32_t count_free = 0;
837 if (NULL != p_buffer_pool &&
889 for (i = 0; i < number_of_buffers; i++)
906 for (i = 0; i < 200; i++)
911 "FATAL ERROR: Failed to allocate pool buffer for pool :%p\n",
924 if (NULL == p_buffer_pool)
976 if (!buf || !p_buffer_pool)
1041 static ni_retcode_t ni_search_file(
const char *p_dev,
char *cmd,
char *cmd_ret,
1047 if (access(p_dev, F_OK) == -1)
1053 cmd_fp = popen(cmd,
"r");
1059 if (fgets(cmd_ret, cmd_ret_len, cmd_fp) == 0)
1087 NI_UNUSED static uint32_t remove_substring_pattern(
char *main_str,
const char *pattern)
1090 uint32_t match_length;
1091 uint32_t matched_chr;
1092 char char_match_pattern[11] =
"";
1093 uint32_t pattern_matched = 0;
1094 uint32_t pattern_start = 0;
1095 const char digit_match_pattern[11] =
"0123456789";
1098 if (!main_str || !pattern || !*main_str || !*pattern)
1104 for (i = 0; i < strlen(main_str) && !pattern_matched; i++)
1106 pattern_matched = 0;
1109 for (j = 0; j < strlen(pattern); j++)
1113 if (pattern[j] ==
'+')
1118 else if (pattern[j] ==
'#')
1120 memcpy(char_match_pattern, digit_match_pattern, strlen(digit_match_pattern) + 1);
1124 memcpy(char_match_pattern, pattern + j, 1);
1125 memset(char_match_pattern + 1, 0, 1);
1128 if (pattern[j+1] ==
'+')
1130 while (main_str[i + match_length] && strchr(char_match_pattern, (
int) main_str[i + match_length]))
1137 else if (main_str[i + match_length] && strchr(char_match_pattern, (
int) main_str[i + match_length]))
1148 else if ((j + 1) >= strlen(pattern))
1150 pattern_matched = 1;
1157 if (pattern_matched)
1159 uint32_t orig_main_str_len = (uint32_t)strlen(main_str);
1160 memmove(main_str + pattern_start, main_str + pattern_start + match_length,
1161 strlen(main_str + pattern_start + match_length));
1162 main_str[orig_main_str_len - match_length] = 0;
1183 if (!p_dev || !p_out_buf)
1190 "Automatic namespaceID discovery not supported in Windows. Using "
1192 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1203 char cmd_ret[60] = {0};
1204 char command[128] = {0};
1205 snprintf(command,
sizeof(command) - 1,
1206 "diskutil info %s | grep 'Media Name'", p_dev);
1208 cmd_fp = popen(command,
"r");
1214 if (fgets(cmd_ret,
sizeof(cmd_ret) - 1, cmd_fp) == NULL)
1223 if (strcasestr(cmd_ret,
"Quadra") != NULL)
1225 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1233 #elif defined(XCODER_LINUX_VIRTIO_DRIVER_ENABLED)
1234 ni_log(
NI_LOG_TRACE,
"The device is already considered as a block divice in Linux virtual machine with VirtIO driver.\n");
1235 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1239 snprintf(p_out_buf, out_buf_len,
"%s", p_dev);
1268 char cmd_ret[60] = {0};
1269 char command[128] = {0};
1270 snprintf(command,
sizeof(command) - 1,
1271 "diskutil info %s | grep 'Media Name'", p_dev);
1273 cmd_fp = popen(command,
"r");
1279 if (fgets(cmd_ret,
sizeof(cmd_ret) - 1, cmd_fp) == NULL)
1288 if (strcasestr(cmd_ret,
"Quadra") != NULL)
1314 if (*pp_table != NULL)
1375 int64_t timestamp, uint64_t data_info)
1400 uint64_t frame_info, int64_t *p_timestamp,
1401 int32_t threshold, int32_t print,
1407 __func__, frame_info);
1410 print, p_buffer_pool);
1417 "%s: timestamp=%" PRId64
", frame_info=%" PRId64
", err=%d\n",
1418 __func__, *p_timestamp, frame_info, err);
1428 threshold, print, p_buffer_pool);
1435 if (!pts_list || !dts_list)
1444 time_t now = time(NULL);
1476 uint64_t frame_offset, int64_t *p_timestamp,
1482 if (!p_table || !p_timestamp || !p_buffer_pool)
1489 __func__, frame_offset);
1491 err =
ni_queue_pop(&p_table->
list, frame_offset, p_timestamp, threshold, 0, p_buffer_pool);
1498 "%s: timestamp=%" PRId64
", frame_offset=%" PRId64
", err=%d\n",
1499 __func__, *p_timestamp, frame_offset, err);
1517 if (!p_queue || !name)
1521 strcpy(p_queue->
name, name);
1587 "%s: queue overflow, remove oldest entry, count=%u\n",
1588 __func__, p_queue->
count);
1612 int64_t *p_timestamp, int32_t threshold,
1621 if (!p_queue || !p_timestamp)
1651 while (temp && !found)
1653 if (frame_info < temp->frame_info)
1672 temp_prev = temp->
p_prev;
1682 p_queue->
p_last = temp_prev;
1703 p_queue->
name, count);
1717 int64_t *p_timestamp, int32_t threshold,
1727 if (!p_queue || !p_timestamp)
1756 while ((temp) && (!found))
1758 if (llabs((
int)frame_info - (
int)temp->
frame_info) <= threshold)
1769 p_queue->
p_last = temp_prev;
1791 p_queue->
name, count);
1828 temp_next = temp->
p_next;
1835 left, p_queue->
count);
1854 struct tm *ltime = NULL;
1855 char buff[20] = {0};
1879 strftime(buff, 20,
"%Y-%m-%d %H:%M:%S", ltime);
1909 if (!strcmp(p_str,
"1") || !strcmp(p_str,
"true") || !strcmp(p_str,
"yes"))
1914 if (!strcmp(p_str,
"0") || !strcmp(p_str,
"false") || !strcmp(p_str,
"no"))
1933 int32_t v = strtol(p_str, &end, 0);
1935 if (end == p_str || *end !=
'\0')
1953 double v = strtod(p_str, &end);
1955 if (end == p_str || *end !=
'\0')
1973 for (i = 0; names[i]; i++)
1975 if (!strcmp(arg, names[i]))
1995 return (tv.tv_sec * 1000000LL + tv.tv_usec);
2001 LARGE_INTEGER frequency;
2002 LARGE_INTEGER count;
2003 uint64_t time_sec, time_nsec;
2006 QueryPerformanceFrequency(&frequency);
2008 QueryPerformanceCounter(&count);
2010 time_sec = count.QuadPart / frequency.QuadPart;
2011 time_nsec = (count.QuadPart - time_sec * frequency.QuadPart) *
2012 1000000000LL / frequency.QuadPart;
2014 return (time_sec * 1000000000LL + time_nsec);
2017 clock_gettime(CLOCK_REALTIME, &ts);
2018 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
2048 plane_stride[0] = ((
NI_MIN_WIDTH * factor + 127) / 128) * 128;
2050 (((
NI_MIN_WIDTH / (is_semiplanar ? 1 : 2) * factor) + 127) /
2053 plane_stride[2] = (is_semiplanar ? 0 : plane_stride[1]);
2056 width = ((width + 1) / 2) * 2;
2057 plane_stride[0] = ((width * factor + 127) / 128) * 128;
2059 (((width / (is_semiplanar ? 1 : 2) * factor) + 127) / 128) *
2061 plane_stride[2] = (is_semiplanar ? 0 : plane_stride[1]);
2070 plane_height[0] = ((height + 1) / 2) * 2;
2072 plane_height[1] = plane_height[2] = plane_height[0] / 2;
2097 plane_height[0] = ((height + 1) / 2) * 2;
2098 plane_height[1] = plane_height[2] = plane_height[0] / 2;
2106 plane_stride[2] = plane_stride[1];
2107 plane_stride[3] = 0;
2113 plane_stride[2] = plane_stride[1];
2114 plane_stride[3] = 0;
2119 plane_stride[1] = plane_stride[0];
2120 plane_stride[2] = 0;
2121 plane_stride[3] = 0;
2126 plane_stride[1] = plane_stride[0];
2127 plane_stride[2] = 0;
2128 plane_stride[3] = 0;
2134 plane_stride[1] = 0;
2135 plane_stride[2] = 0;
2136 plane_stride[3] = 0;
2141 plane_stride[1] = plane_stride[0];
2142 plane_stride[2] = 0;
2143 plane_stride[3] = 0;
2151 plane_height[1] = plane_height[2] = 0;
2154 plane_stride[1] = 0;
2155 plane_stride[2] = 0;
2156 plane_stride[3] = 0;
2197 width = ((width + 1) / 2) * 2;
2203 "%s dst_stride %d/%d/%d height %d/%d/%d pix_fmt %d\n",
2204 __func__, plane_stride[0], plane_stride[1], plane_stride[2],
2205 plane_height[0], plane_height[1], plane_height[2], pix_fmt);
2231 int frame_width,
int frame_height,
int factor,
2232 int is_semiplanar,
int conf_win_right,
2245 if (p_dst[i] == p_src[i])
2252 (src_height[i] < dst_height[i] ? src_height[i] : dst_height[i]);
2253 uint8_t *dst = p_dst[i];
2254 const uint8_t *src = (
const uint8_t *)p_src[i];
2259 if (0 == i || is_semiplanar)
2261 pad_len_bytes = dst_stride[i] - frame_width * factor;
2266 pad_len_bytes = dst_stride[i] - frame_width / 2 * factor;
2269 if (0 == pad_len_bytes && conf_win_right > 0)
2273 pad_len_bytes = conf_win_right * factor;
2278 pad_len_bytes = conf_win_right * factor / 2;
2283 "%s plane %d stride padding: %d pixel (%d bytes), copy height: "
2285 __func__, i, pad_len_bytes / factor, pad_len_bytes,
2288 for (; height > 0; height--)
2291 (src_stride[i] < dst_stride[i] ? src_stride[i] : dst_stride[i]));
2292 dst += dst_stride[i];
2302 uint8_t *tmp_dst = dst - pad_len_bytes;
2303 for (j = 0; j < pad_len_bytes / factor; j++)
2305 memcpy(tmp_dst, dst - pad_len_bytes - factor, factor);
2311 memset(dst - pad_len_bytes, *(dst - pad_len_bytes - 1),
2315 src += src_stride[i];
2319 int padding_height = dst_height[i] - src_height[i];
2320 if (padding_height > 0)
2324 src = dst - dst_stride[i];
2325 for (; padding_height > 0; padding_height--)
2327 memcpy(dst, src, dst_stride[i]);
2328 dst += dst_stride[i];
2354 int frame_width,
int frame_height,
int factor,
2355 int is_semiplanar,
int conf_win_right,
2362 "%s dst_stride %d/%d/%d src_stride %d/%d/%d dst_height "
2363 "%d/%d/%d src_height %d/%d/%d\n",
2364 __func__, dst_stride[0], dst_stride[1], dst_stride[2], src_stride[0],
2365 src_stride[1], src_stride[2], dst_height[0], dst_height[1],
2366 dst_height[2], src_height[0], src_height[1], src_height[2]);
2373 is_semiplanar, conf_win_right, dst_stride,
2374 dst_height, src_stride, src_height, i);
2400 int frame_width,
int frame_height,
2409 "%s frame_width %d frame_height %d factor %d conf_win_right %d "
2410 "dst_stride %d/%d/%d src_stride %d/%d/%d dst_height "
2411 "%d/%d/%d src_height %d/%d/%d pix_fmt %d\n",
2412 __func__, frame_width, frame_height, factor, conf_win_right,
2413 dst_stride[0], dst_stride[1], dst_stride[2], src_stride[0],
2414 src_stride[1], src_stride[2], dst_height[0], dst_height[1],
2415 dst_height[2], src_height[0], src_height[1], src_height[2],
2419 int is_semiplanar = 0;
2439 frame_width, frame_height, 4,
2440 is_semiplanar, conf_win_right,
2441 dst_stride, dst_height,
2442 src_stride, src_height,
2448 is_semiplanar, conf_win_right, dst_stride,
2449 dst_height, src_stride, src_height);
2481 int frame_width,
int frame_height,
2482 int factor,
int mode)
2485 int y_444p_linesize = frame_width * factor;
2486 int uv_444p_linesize = y_444p_linesize;
2491 if (p_dst0[0] == p_dst1[0] && p_dst0[1] == p_dst1[1] &&
2492 p_dst0[2] == p_dst1[2])
2499 for (i = 0; i < frame_height; i++)
2501 memcpy(&p_dst0[0][i * y_420p_linesize], &p_src[0][i * y_444p_linesize],
2510 for (i = 0; i < frame_height; i++)
2512 memcpy(&p_dst1[0][i * y_420p_linesize],
2513 &p_src[1][i * y_444p_linesize], y_444p_linesize);
2516 for (i = 0; i < frame_height / 2; i++)
2518 for (j = 0; j < frame_width * factor / 2; j += factor)
2522 memcpy(&p_dst0[1][i * uv_420p_linesize + j],
2523 &p_src[2][2 * i * uv_444p_linesize + 2 * j],
2525 memcpy(&p_dst0[2][i * uv_420p_linesize + j],
2526 &p_src[2][2 * i * uv_444p_linesize + (2 * j + factor)],
2529 memcpy(&p_dst1[1][i * uv_420p_linesize + j],
2530 &p_src[2][(2 * i + 1) * uv_444p_linesize + 2 * j],
2532 memcpy(&p_dst1[2][i * uv_420p_linesize + j],
2533 &p_src[2][(2 * i + 1) * uv_444p_linesize + (2 * j + factor)],
2541 for (i = 0; i < frame_height / 2; i++)
2543 for (j = 0; j < frame_width * factor / 2; j += factor)
2547 memcpy(&p_dst1[1][i * uv_420p_linesize + j],
2548 &p_src[1][2 * i * uv_444p_linesize + (2 * j + factor)],
2551 memcpy(&p_dst1[0][2 * i * uv_444p_linesize + 2 * j],
2552 &p_src[1][(2 * i + 1) * uv_444p_linesize + 2 * j],
2555 memcpy(&p_dst0[1][i * uv_420p_linesize + j],
2556 &p_src[1][2 * i * uv_444p_linesize + 2 * j],
2561 memcpy(&p_dst1[2][i * uv_420p_linesize + j],
2562 &p_src[2][2 * i * uv_444p_linesize + (2 * j + factor)],
2565 memcpy(&p_dst1[0][(2 * i + 1) * uv_444p_linesize + 2 * j],
2566 &p_src[2][(2 * i + 1) * uv_444p_linesize + 2 * j],
2569 memcpy(&p_dst0[2][i * uv_420p_linesize + j],
2570 &p_src[2][2 * i * uv_444p_linesize + 2 * j],
2593 int insert_bytes = 0;
2594 uint8_t *buf_curr = buf;
2595 uint8_t *buf_end = buf + size - 1;
2596 int zeros = 0, insert_ep3_byte;
2600 for (; buf_curr <= buf_end; buf_curr++)
2604 insert_ep3_byte = (*buf_curr <= 3);
2605 if (insert_ep3_byte)
2608 memmove(buf_curr + 1, buf_curr, buf_end - buf_curr + 1);
2629 return insert_bytes;
2646 int remove_bytes = 0;
2647 uint8_t *buf_curr = buf;
2648 uint8_t *buf_end = buf + size - 1;
2649 int zeros = 0, remove_ep3_byte;
2653 for (; buf_curr < buf_end; buf_curr++)
2657 remove_ep3_byte = (*buf_curr == 0x03 && *(buf_curr + 1) <= 3);
2658 if (remove_ep3_byte)
2661 memmove(buf_curr, buf_curr + 1, buf_end - buf_curr);
2681 return remove_bytes;
2689 static uint32_t ni_ai_type_get_bytes(
const uint32_t type)
2715 static void ni_ai_integer_convert(
const void *src,
void *dest,
2719 unsigned char all_zeros[] = {0x00, 0x00, 0x00, 0x00,
2720 0x00, 0x00, 0x00, 0x00};
2721 unsigned char all_ones[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2722 uint32_t src_sz = ni_ai_type_get_bytes(src_dtype);
2723 uint32_t dest_sz = ni_ai_type_get_bytes(dst_dtype);
2724 unsigned char *buffer = all_zeros;
2726 if (((int8_t *)src)[src_sz - 1] & 0x80)
2730 memcpy(buffer, src, src_sz);
2731 memcpy(dest, buffer, dest_sz);
2734 static float ni_ai_int8_to_fp32(
signed char val,
signed char fixedPointPos)
2736 float result = 0.0f;
2738 if (fixedPointPos > 0)
2740 result = (float)val * (1.0f / ((
float)(1 << fixedPointPos)));
2743 result = (float)val * ((
float)(1 << -fixedPointPos));
2749 static float ni_ai_int16_to_fp32(int16_t val,
signed char fixedPointPos)
2751 float result = 0.0f;
2753 if (fixedPointPos > 0)
2755 result = (float)val * (1.0f / ((
float)(1 << fixedPointPos)));
2758 result = (float)val * ((
float)(1 << -fixedPointPos));
2764 static float ni_ai_uint8_to_fp32(uint8_t val, int32_t zeroPoint,
float scale)
2766 float result = 0.0f;
2768 result = (float)(val - (uint8_t)zeroPoint) * scale;
2773 static float ni_ai_fp16_to_fp32(
const short in)
2781 const _fp32_t magic = {(254 - 15) << 23};
2782 const _fp32_t infnan = {(127 + 16) << 23};
2785 o.u = (in & 0x7fff) << 13;
2787 if (o.f >= infnan.f)
2792 o.u |= (in & 0x8000) << 16;
2796 static float ni_ai_affine_to_fp32(int32_t val, int32_t zeroPoint,
float scale)
2798 float result = 0.0f;
2799 result = ((float)val - zeroPoint) * scale;
2803 static uint32_t ni_ai_get_tensor_size(int32_t *shape, uint32_t dim_num,
2809 if (NULL == shape || 0 == dim_num)
2814 for (i = 0; i < dim_num; i++)
2818 sz *= ni_ai_type_get_bytes(type);
2823 static uint32_t ni_ai_get_element_num(int32_t *sizes, uint32_t num_of_dims,
2824 int32_t data_format)
2830 sz = ni_ai_get_tensor_size(sizes, num_of_dims, data_format);
2831 dsize = ni_ai_type_get_bytes(data_format);
2834 num = (uint32_t)(sz / dsize);
2843 static int32_t ni_ai_type_is_integer(
const int32_t type)
2863 static int32_t ni_ai_type_is_signed(
const int32_t type)
2883 static void ni_ai_type_get_range(int32_t type,
double *max_range,
2890 bits = ni_ai_type_get_bytes(type) * 8;
2891 if (ni_ai_type_is_integer(type))
2893 if (ni_ai_type_is_signed(type))
2895 from = (double)(-(1L << (bits - 1)));
2896 to = (double)((1UL << (bits - 1)) - 1);
2900 to = (double)((1UL << bits) - 1);
2903 if (NULL != max_range)
2907 if (NULL != min_range)
2913 static inline double ni_abs(
double x)
2915 return x < 0 ? -x : x;
2918 static double ni_ai_copy_sign(
double number,
double sign)
2920 double value = ni_abs(number);
2921 return (sign > 0) ? value : (-value);
2924 static inline int ni_ai_math_floorf(
double x)
2926 return x >= 0 ? (int)x : (
int)x - 1;
2929 static double ni_ai_rint(
double x)
2931 #define _EPSILON 1e-8
2937 decimal = x - intpart;
2938 inter = (double)intpart;
2940 if (ni_abs((ni_abs(decimal) - 0.5f)) <
_EPSILON)
2942 inter += (int32_t)(inter) % 2;
2945 return ni_ai_copy_sign(ni_ai_math_floorf(ni_abs(x) + 0.5f), x);
2951 static int32_t ni_ai_fp32_to_dfp(
const float in,
const signed char fl,
2957 ni_ai_type_get_range(type, &max_range, &min_range);
2960 data = (int32_t)ni_ai_rint(in * (
float)(1 << fl));
2963 data = (int32_t)ni_ai_rint(in * (1.0f / (
float)(1 << -fl)));
2965 data = ni_min(data, (int32_t)max_range);
2966 data = ni_max(data, (int32_t)min_range);
2971 static int32_t ni_ai_fp32_to_affine(
const float in,
const float scale,
2972 const int zero_point,
const int32_t type)
2977 ni_ai_type_get_range(type, &max_range, &min_range);
2978 data = (int32_t)(ni_ai_rint(in / scale) + zero_point);
2979 data = ni_max((int32_t)min_range, ni_min((int32_t)max_range, data));
2983 static unsigned short ni_ai_fp32_to_bfp16_rtne(
float in)
2991 memcpy(&fp32, &in,
sizeof(uint32_t));
2995 uint32_t rounding_bias = 0x7fff + lsb;
2997 if ((
float)0x7FC00000 == in)
3002 fp32 += rounding_bias;
3003 out = (
unsigned short)(fp32 >> 16);
3009 static unsigned short ni_ai_fp32_to_fp16(
float in)
3017 memcpy((uint8_t *)&fp32, (uint8_t *)&in,
sizeof(uint32_t));
3019 t1 = (fp32 & 0x80000000u) >> 16;
3020 t2 = (fp32 & 0x7F800000u) >> 13;
3021 t3 = (fp32 & 0x007FE000u) >> 13;
3023 if (t2 >= 0x023c00u)
3026 }
else if (t2 <= 0x01c000u)
3032 fp16 = t1 | t2 | t3;
3035 return (
unsigned short)fp16;
3038 static void ni_ai_float32_to_dtype(
float src,
unsigned char *dst,
3039 const int32_t data_type,
3040 const int32_t quant_format,
3041 signed char fixed_point_pos,
float tf_scale,
3047 *(
float *)((
void *)dst) = src;
3050 *(int16_t *)dst = ni_ai_fp32_to_fp16(src);
3053 *(int16_t *)dst = ni_ai_fp32_to_bfp16_rtne(src);
3059 int32_t dst_value = 0;
3060 switch (quant_format)
3064 ni_ai_fp32_to_dfp(src, fixed_point_pos, data_type);
3067 dst_value = ni_ai_fp32_to_affine(src, tf_scale, tf_zerop,
3071 dst_value = (int32_t)src;
3093 if (!p_network || !dst || dst_len == 0 || !p_packet || !p_packet->
p_data)
3117 static int open_tensor_rsrc_file(
struct tensor_rsrc *rsrc,
void *data,
3120 rsrc->
private = (
void *)fopen((
const char *)data,
"r");
3121 return rsrc->
private ? 0 : -1;
3124 static int get_tensor_rsrc_from_file(
struct tensor_rsrc *rsrc,
float *value)
3126 FILE *fp = (FILE *)rsrc->
private;
3129 if (fscanf(fp,
"%f ", &tensor) == 1)
3138 static void close_tensor_rsrc_file(
struct tensor_rsrc *rsrc)
3140 FILE *fp = (FILE *)rsrc->
private;
3144 static int open_tensor_rsrc_rawdata(
struct tensor_rsrc *rsrc,
void *data,
3153 static int get_tensor_rsrc_from_rawdata(
struct tensor_rsrc *rsrc,
float *value)
3155 float *tensors = (
float *)rsrc->
private;
3167 static void close_tensor_rsrc_rawdata(
struct tensor_rsrc *rsrc)
3172 uint8_t *dst, uint32_t dst_len,
void *tensor, uint32_t src_len,
3174 int (*open_tensor_rsrc)(
struct tensor_rsrc *,
void *, uint32_t),
3175 int (*get_tensor_rsrc)(
struct tensor_rsrc *,
float *),
3178 uint8_t *tensor_data;
3182 int32_t data_format;
3183 int32_t quant_format;
3184 int32_t fixed_point_pos;
3195 sz = ni_ai_get_element_num((int32_t *)p_param->
sizes, p_param->
num_of_dims,
3197 stride = ni_ai_type_get_bytes(data_format);
3199 if (sz * stride *
sizeof(uint8_t) != dst_len)
3210 if (open_tensor_rsrc(rsrc, tensor, src_len) != 0)
3216 memset(dst, 0, sz * stride *
sizeof(uint8_t));
3218 for (i = 0; i < sz; i++)
3220 if (get_tensor_rsrc(rsrc, &fval) == 0)
3222 ni_ai_float32_to_dtype(fval, &tensor_data[stride * i], data_format,
3223 quant_format, fixed_point_pos, tf_scale,
3231 close_tensor_rsrc(rsrc);
3237 const char *tensor_file,
3240 return ni_network_tensor_to_data(
3241 dst, dst_len, (
void *)tensor_file, 0, p_param, open_tensor_rsrc_file,
3242 get_tensor_rsrc_from_file, close_tensor_rsrc_file);
3250 return ni_network_tensor_to_data(
3251 dst, dst_len, src, src_len, p_param, open_tensor_rsrc_rawdata,
3252 get_tensor_rsrc_from_rawdata, close_tensor_rsrc_rawdata);
3263 float *dst_float = dst;
3266 if (!src || src_len == 0 || !dst || dst_len == 0 || !p_param)
3271 type_size = ni_ai_type_get_bytes(p_param->
data_format);
3278 ele_size *= p_param->
sizes[i];
3281 if (dst_len != ele_size *
sizeof(
float))
3297 uint8_t fix_pos = (uint8_t)p_param->
quant_data.
dfp.fixed_point_pos;
3299 for (i = 0; i < ele_size; i++, data += type_size)
3301 dst_float[i] = ni_ai_int8_to_fp32(*data, fix_pos);
3308 for (i = 0; i < ele_size; i++, data += type_size)
3310 int32_t src_value = 0;
3311 ni_ai_integer_convert(data, &src_value,
3315 ni_ai_affine_to_fp32(src_value, zero_point, scale);
3319 for (i = 0; i < ele_size; i++, data += type_size)
3321 void *float_data = (
void *)data;
3322 dst_float[i] = *((
float *)float_data);
3327 for (i = 0; i < ele_size; i++, data += type_size)
3329 dst_float[i] = ni_ai_fp16_to_fp32(*((
short *)data));
3336 for (i = 0; i < ele_size; i++, data += type_size)
3338 dst_float[i] = ni_ai_uint8_to_fp32(*data, zero_point, scale);
3344 for (i = 0; i < ele_size; i++, data += type_size)
3346 dst_float[i] = ni_ai_int16_to_fp32(*((
short *)data), fix_pos);
3350 for (i = 0; i < ele_size; i++, data += type_size)
3352 void *float_data = (
void *)data;
3353 dst_float[i] = *((
float *)float_data);
3365 return ni_ai_get_tensor_size((int32_t *)p_param->
sizes,
3371 uint32_t i, dims = 1;
3375 dims *= p_param->
sizes[i];
3383 #define SHA256_BLOCK_SIZE 32
3393 #define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
3394 #define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b))))
3396 #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
3397 #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
3398 #define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
3399 #define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
3400 #define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
3401 #define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))
3403 static const uint32_t ui32k[64] = {
3404 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
3405 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
3406 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
3407 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
3408 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
3409 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
3410 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
3411 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
3412 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
3413 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
3414 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
3418 uint32_t ui32a, ui32b, ui32c, ui32d, ui32e, ui32f, ui32g, ui32h, ui32i,
3419 ui32j, ui32t1, ui32t2, ui32m[64];
3421 for (ui32i = 0, ui32j = 0; ui32i < 16; ++ui32i, ui32j += 4)
3422 ui32m[ui32i] = (aui8Data[ui32j] << 24) | (aui8Data[ui32j + 1] << 16) |
3423 (aui8Data[ui32j + 2] << 8) | (aui8Data[ui32j + 3]);
3424 for (; ui32i < 64; ++ui32i)
3425 ui32m[ui32i] =
SIG1(ui32m[ui32i - 2]) + ui32m[ui32i - 7] +
3426 SIG0(ui32m[ui32i - 15]) + ui32m[ui32i - 16];
3437 for (ui32i = 0; ui32i < 64; ++ui32i)
3439 ui32t1 = ui32h +
EP1(ui32e) +
CH(ui32e, ui32f, ui32g) + ui32k[ui32i] +
3441 ui32t2 =
EP0(ui32a) +
MAJ(ui32a, ui32b, ui32c);
3445 ui32e = ui32d + ui32t1;
3449 ui32a = ui32t1 + ui32t2;
3481 for (ui32i = 0; ui32i < ui32Length; ++ui32i)
3529 for (ui32i = 0; ui32i < 4; ++ui32i)
3532 (psCtx->
aui32State[0] >> (24 - ui32i * 8)) & 0x000000ff;
3533 aui8Hash[ui32i + 4] =
3534 (psCtx->
aui32State[1] >> (24 - ui32i * 8)) & 0x000000ff;
3535 aui8Hash[ui32i + 8] =
3536 (psCtx->
aui32State[2] >> (24 - ui32i * 8)) & 0x000000ff;
3537 aui8Hash[ui32i + 12] =
3538 (psCtx->
aui32State[3] >> (24 - ui32i * 8)) & 0x000000ff;
3539 aui8Hash[ui32i + 16] =
3540 (psCtx->
aui32State[4] >> (24 - ui32i * 8)) & 0x000000ff;
3541 aui8Hash[ui32i + 20] =
3542 (psCtx->
aui32State[5] >> (24 - ui32i * 8)) & 0x000000ff;
3543 aui8Hash[ui32i + 24] =
3544 (psCtx->
aui32State[6] >> (24 - ui32i * 8)) & 0x000000ff;
3545 aui8Hash[ui32i + 28] =
3546 (psCtx->
aui32State[7] >> (24 - ui32i * 8)) & 0x000000ff;
3580 if (p_dst[0] == p_src[0] && p_dst[1] == p_src[1] && p_dst[2] == p_src[2] &&
3581 p_dst[3] == p_src[3])
3603 return libxcoder_api_ver;
3614 static char compat_fw_api_ver_str[5] =
"";
3616 if (!compat_fw_api_ver_str[0])
3618 compat_fw_api_ver_str[0] = \
3620 compat_fw_api_ver_str[1] =
'.';
3621 compat_fw_api_ver_str[2] = \
3624 compat_fw_api_ver_str[3] = \
3627 compat_fw_api_ver_str[3] = 0;
3628 compat_fw_api_ver_str[4] = 0;
3630 return &compat_fw_api_ver_str[0];
3646 if (!ver_str || !fmt_str) {
3650 fmt_str[0] = ver_str[0];
3652 fmt_str[2] = ver_str[1];
3654 if ((ver_str[0] <
'6' || (ver_str[0] ==
'6' && ver_str[1] <=
'q')) ||
3655 (ver_str[2] == 0)) {
3658 fmt_str[3] = ver_str[2];
3680 if (ver1[index] > ver2[index])
3682 else if (ver1[index] < ver2[index])
3687 if (ver1[index] > ver2[index])
3689 else if (ver1[index] < ver2[index])
3692 if ((ver1[index - 1] <
'6') || ((ver1[index - 1] ==
'6') && (ver1[index] <=
'q')))
3697 if (ver1[index] > ver2[index])
3699 else if (ver1[index] < ver2[index])
3712 static char release_ver_str[6] =
"";
3714 if (!release_ver_str[0])
3717 release_ver_str[1] =
'.';
3719 release_ver_str[3] =
'.';
3721 release_ver_str[5] = 0;
3723 return &release_ver_str[0];
3737 if (rc == ni_err_rc_description[i].rc)
3739 return ni_err_rc_description[i].
txt;
3742 return "rc not supported";
3757 if (!p_str || !key || !value)
3762 char *p = strchr(p_str,
'=');
3772 strcpy(value, p + 1);
3791 char key[64], value[64];
3792 char *curr = xcoderParams, *colon_pos;
3797 colon_pos = strchr(curr,
':');
3804 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
3808 "Error: xcoder-params p_config key/value not "
3837 curr = colon_pos + 1;
3840 curr += strlen(curr);
3865 char key[64], value[64];
3866 char *curr = xcoderGop, *colon_pos;
3871 colon_pos = strchr(curr,
':');
3878 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
3882 "Error: xcoder-params p_config key/value not "
3911 curr = colon_pos + 1;
3914 curr += strlen(curr);
3935 char key[64], value[64];
3936 char *curr = xcoderParams, *colon_pos;
3941 colon_pos = strchr(curr,
':');
3948 if (strlen(curr) >
sizeof(key) +
sizeof(value) - 1 ||
3952 "Error: decoder-params p_config key/value not "
3981 curr = colon_pos + 1;
3984 curr += strlen(curr);
4006 rc = InitializeCriticalSectionEx(mutex, 0, CRITICAL_SECTION_NO_DEBUG_INFO);
4017 ni_pthread_mutexattr_t attr;
4019 rc = pthread_mutexattr_init(&attr);
4025 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
4027 return pthread_mutex_init(mutex, &attr);
4042 DeleteCriticalSection(mutex);
4045 return pthread_mutex_destroy(mutex);
4063 EnterCriticalSection(mutex);
4065 rc = pthread_mutex_lock(mutex);
4089 LeaveCriticalSection(mutex);
4091 rc = pthread_mutex_unlock(mutex);
4102 static unsigned __stdcall __thread_worker(
void *arg)
4104 ni_pthread_t *t = (ni_pthread_t *)arg;
4105 t->rc = t->start_routine(t->arg);
4122 void *(*start_routine)(
void *),
void *arg)
4125 thread->start_routine = start_routine;
4129 (
void *)CreateThread(NULL, 0, win32thread_worker, thread, 0, NULL);
4131 (
void *)_beginthreadex(NULL, 0, __thread_worker, thread, 0, NULL);
4133 return !thread->handle;
4135 return pthread_create(thread, attr, start_routine, arg);
4151 DWORD rc = WaitForSingleObject(thread.handle, INFINITE);
4152 if (rc != WAIT_OBJECT_0)
4154 if (rc == WAIT_ABANDONED)
4160 *value_ptr = thread.rc;
4161 CloseHandle(thread.handle);
4164 return pthread_join(thread, value_ptr);
4178 const ni_pthread_condattr_t *attr)
4181 InitializeConditionVariable(cond);
4184 return pthread_cond_init(cond, attr);
4202 return pthread_cond_destroy(cond);
4217 WakeAllConditionVariable(cond);
4220 return pthread_cond_broadcast(cond);
4236 SleepConditionVariableCS(cond, mutex, INFINITE);
4239 return pthread_cond_wait(cond, mutex);
4254 WakeConditionVariable(cond);
4257 return pthread_cond_signal(cond);
4272 ni_pthread_mutex_t *mutex,
4273 const struct timespec *abstime)
4276 int64_t abs_ns = abstime->tv_sec * 1000000000LL + abstime->tv_nsec;
4279 if (!SleepConditionVariableCS(cond, mutex, t))
4281 DWORD err = GetLastError();
4282 if (err == ERROR_TIMEOUT)
4289 return pthread_cond_timedwait(cond, mutex, abstime);
4310 return pthread_sigmask(how, set, oldset);
4328 return "General Error";
4330 return "Not Initialized";
4332 return "Already Initialized";
4336 return "Resource Not Available";
4338 return "Create Network Failed";
4340 return "Input Buffer Full";
4342 return "Output Buffer Empty";
4344 return "Invalid Params";
4346 return "Error Start Network";
4348 return "Erorr Set Inout";
4350 return "Bad Option";
4354 return "Context Not Available";
4356 return "Model Not Found";
4360 return "Invalid Address";
4362 return "Out Of Memory";
4366 return "Invalid Instance";
4368 return "IO Not Allowed";
4370 return "Network Not Ready";
4372 return "Other Error";
4387 float current_ma,voltage_mv;
4394 snprintf(pcb_config,
sizeof(pcb_config),
"%.2s", &serial_number[2]);
4396 float current_value = (float)current_data;
4397 voltage_mv = 12000.0f;
4398 if (strncmp(pcb_config,
"A1", 2) == 0)
4402 else if ((strncmp(pcb_config,
"A2", 2) == 0) || (strncmp(pcb_config,
"A3", 2) == 0))
4406 else if ((strncmp(pcb_config,
"AA", 2) == 0) || (strncmp(pcb_config,
"AB", 2) == 0))
4410 else if (strncmp(pcb_config,
"U0", 2) == 0)
4415 else if ((strncmp(pcb_config,
"U1", 2) == 0) || (strncmp(pcb_config,
"U2", 2) == 0) || (strncmp(pcb_config,
"U3", 2) == 0))
4419 else if (strncmp(pcb_config,
"UA", 2) == 0)
4423 else if (strncmp(pcb_config,
"S0", 2) == 0)
4427 else if (strncmp(pcb_config,
"M0", 2) == 0)
4430 voltage_mv = 3300.0f;
4436 power_mw = (uint32_t)((voltage_mv * current_ma) / 1000.0);
4461 static int ni_device_size_precheck_blockdev(
const char *p_dev,
const uint64_t size_needed )
4471 const char blockdev_command_dev[] =
"blockdev --getsize64 %s 2>/dev/null";
4472 FILE *blockdev_file = NULL;
4473 char *blockdev_command = NULL;
4474 char blockdev_result [70] = {0};
4481 const size_t path_len = strlen(p_dev);
4483 blockdev_command = (
char *)calloc(1,
sizeof(blockdev_command_dev) + path_len);
4484 if (!blockdev_command)
4489 snprintf(blockdev_command,
sizeof(blockdev_command_dev) + path_len, blockdev_command_dev, p_dev);
4493 blockdev_file = popen(blockdev_command,
"r");
4500 if (fgets(blockdev_result,
sizeof(blockdev_result), blockdev_file) == NULL)
4505 int status = pclose(blockdev_file);
4506 blockdev_file = NULL;
4510 if (*blockdev_result !=
'\0')
4512 char *endptr = NULL;
4514 unsigned long long this_size = strtoull(blockdev_result, &endptr, 10);
4516 if (errno != 0 || endptr == blockdev_result)
4520 else if (this_size < size_needed)
4522 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() blockdev size check failed. size: %" PRIu64
"\n", __func__, this_size);
4533 if (blockdev_command)
4535 free(blockdev_command);
4540 fclose(blockdev_file);
4567 static int ni_device_size_precheck_lsblk(
const char *p_dev,
const uint64_t size_needed)
4576 const char lsblk_command_dev[] =
"lsblk -b -o SIZE %s 2>/dev/null";
4577 char *lsblk_command = NULL;
4578 FILE *lsblk_file = NULL;
4579 char lsblk_result [70] = {0};
4580 char *fgets_result = NULL;
4587 const size_t path_len = strlen(p_dev);
4589 lsblk_command = (
char *)calloc(1,
sizeof(lsblk_command_dev) + path_len);
4595 snprintf(lsblk_command,
sizeof(lsblk_command_dev) + path_len, lsblk_command_dev, p_dev);
4599 lsblk_file = popen(lsblk_command,
"r");
4605 fgets_result = fgets(lsblk_result,
sizeof(lsblk_result), lsblk_file);
4611 memset(lsblk_result, 0,
sizeof(lsblk_result));
4613 fgets_result = fgets(lsblk_result,
sizeof(lsblk_result), lsblk_file);
4619 int status = pclose(lsblk_file);
4624 if (*lsblk_result !=
'\0')
4626 char *endptr = NULL;
4628 unsigned long long this_size = strtoull(lsblk_result, &endptr, 10);
4630 if (errno != 0 || endptr == lsblk_result)
4634 else if (this_size < size_needed)
4636 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() lsblk size check failed. size: %" PRIu64
"\n", __func__, this_size);
4649 free(lsblk_command);
4680 static int ni_device_vendor_id_precheck(
const char *p_dev)
4694 const char *vendor_path_devs[] = {
"/sys/class/block/%s/device/vendor",
"/sys/class/block/%s/device/device/vendor"};
4696 const char *last_slash = strrchr(p_dev,
'/');
4697 const char *device_name = (last_slash ? (last_slash + 1) : p_dev);
4699 const size_t path_len = strlen(p_dev);
4701 for (
size_t i = 0; i <
sizeof(vendor_path_devs)/
sizeof(vendor_path_devs[0]) && ret == 0; ++i)
4703 size_t template_len = strlen(vendor_path_devs[i]);
4704 char *vendor_path = (
char *)calloc(1, template_len + path_len);
4709 snprintf(vendor_path, template_len + path_len, vendor_path_devs[i], device_name);
4711 int fd = open(vendor_path, O_RDONLY);
4714 char vendor_id[10] = {0};
4715 int read_size = read(fd, vendor_id,
sizeof(vendor_id));
4716 if (read_size >= 4 && (
size_t)read_size <
sizeof(vendor_id))
4718 char *find_1d82 = strstr(vendor_id,
"1d82");
4762 static int ni_device_size_precheck_system_information(
const char *p_dev,
const uint64_t size_needed)
4777 const char block_path_dev[] =
"/sys/class/block/%s/size";
4778 const char logical_block_size_path_dev[] =
"/sys/class/block/%s/queue/logical_block_size";
4780 const char *last_slash = strrchr(p_dev,
'/');
4781 const char *device_name = (last_slash ? (last_slash + 1) : p_dev);
4783 const size_t path_len = strlen(p_dev);
4786 int logical_block_fd = -1;
4788 char *block_path = (
char *)calloc(1,
sizeof(block_path_dev) + path_len);
4789 char *logical_block_size_path = (
char *)calloc(1,
sizeof(logical_block_size_path_dev) + path_len);
4791 if (!block_path || !logical_block_size_path)
4796 snprintf(block_path,
sizeof(block_path_dev) + path_len, block_path_dev, device_name);
4797 snprintf(logical_block_size_path,
sizeof(logical_block_size_path_dev) + path_len, logical_block_size_path_dev, device_name);
4799 size_fd = open(block_path, O_RDONLY);
4800 logical_block_fd = open(logical_block_size_path, O_RDONLY);
4802 if (size_fd < 0 || logical_block_fd < 0)
4807 char this_size_str [20] = {0};
4808 char this_block_size_str [20] = {0};
4810 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)
4816 char *endptr1 = NULL;
4817 char *endptr2 = NULL;
4819 unsigned long long this_size = strtoull(this_size_str, &endptr1, 10);
4820 unsigned long long this_block_size = strtoull(this_block_size_str, &endptr2, 10);
4822 if (errno == 0 && endptr1 != this_size_str && endptr2 != this_block_size_str)
4824 if (this_size * this_block_size >= size_needed)
4830 ni_log2(NULL,
NI_LOG_DEBUG,
"%s() read size check failed. size: %" PRIu64
"\n", __func__, this_size * this_block_size);
4841 if (logical_block_size_path)
4843 free(logical_block_size_path);
4850 if (logical_block_fd >= 0)
4852 close(logical_block_fd);
4889 const size_t path_len = strlen(p_dev);
4896 ret = ni_device_size_precheck_blockdev(p_dev, SIZE_NEEDED);
4906 ret = ni_device_size_precheck_lsblk(p_dev, SIZE_NEEDED);
4916 ret = ni_device_vendor_id_precheck(p_dev);
4926 ret = ni_device_size_precheck_system_information(p_dev, SIZE_NEEDED);