dw_fd_src_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* auto generate by HHB_VERSION "1.13.x" */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <libgen.h>
  25. #include <unistd.h>
  26. #include <sys/mman.h>
  27. #include <fcntl.h>
  28. #include <semaphore.h>
  29. #include <sys/stat.h>
  30. #include "io.h"
  31. #include "shl_ref.h"
  32. #include "process_linker_types.h"
  33. #include "process_linker.h"
  34. #include "process.h"
  35. #include "video_mem.h"
  36. #define MODULE_NAME "dw_test"
  37. #define _DMABUF_FD_SRC_
  38. #define FILE_LENGTH 1028
  39. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  40. #define IMG_WIDTH 300
  41. #define IMG_HEIGHT 300
  42. #define STRIDE_WIDTH 304
  43. #define STRIDE_HEIGHT 304
  44. #define RESIZE_WIDTH 304
  45. #define RESIZE_HEIGHT 304
  46. #define CROP_WIDTH 304
  47. #define CROP_HEGHT 304
  48. #define R_MEAN 127.5
  49. #define G_MEAN 127.5
  50. #define B_MEAN 127.5
  51. #define SCALE (1.0/127.5)
  52. int input_size[] = {1 * 3 * STRIDE_HEIGHT * STRIDE_WIDTH, };
  53. #define BASE_MEMORY 0xD0000000
  54. //#define BASE_MEMORY 0xc0c00000
  55. #ifndef NULL
  56. #define NULL ((void *)0)
  57. #endif
  58. #define NUM_OF_BUFFERS 5
  59. #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
  60. } while (0)
  61. typedef struct _ServerParams
  62. {
  63. char *plinkname;
  64. char *inputfile;
  65. PlinkColorFormat format;
  66. int width;
  67. int height;
  68. int stride;
  69. int frames;
  70. } ServerParams;
  71. typedef struct _PlinkChannel
  72. {
  73. PlinkChannelID id;
  74. PlinkHandle plink;
  75. PlinkPacket pkt;
  76. int sendid;
  77. int backid;
  78. int exit;
  79. int available_bufs;
  80. } PlinkChannel;
  81. typedef struct _PictureBuffer
  82. {
  83. unsigned int bus_address;
  84. void *virtual_address;
  85. unsigned int size;
  86. int fd;
  87. } PictureBuffer;
  88. void printUsage(char *name)
  89. {
  90. printf("usage: %s [options]\n"
  91. "\n"
  92. " Available options:\n"
  93. " -l plink file name (default: /tmp/plink.test)\n"
  94. " -i input YUV file name (mandatory)\n"
  95. " -f input color format (default: 2)\n"
  96. " 2 - I420\n"
  97. " 3 - NV12\n"
  98. " -w video width (mandatory)\n"
  99. " -h video height (mandatory)\n"
  100. " -s video buffer stride (default: video width)\n"
  101. " -n number of frames to send (default: 10)\n"
  102. "\n", name);
  103. }
  104. /*
  105. * Preprocess function
  106. */
  107. void preprocess(struct image_data *img, int is_rgb, int to_bgr)
  108. {
  109. uint32_t new_height, new_width;
  110. uint32_t min_side;
  111. if (is_rgb) {
  112. im2rgb(img);
  113. }
  114. if (RESIZE_WIDTH == 0) {
  115. min_side = MIN(img->shape[0], img->shape[1]);
  116. new_height = (uint32_t) (img->shape[0] * (((float)RESIZE_HEIGHT) / (float)min_side));
  117. new_width = (uint32_t) (img->shape[1] * (((float)RESIZE_HEIGHT) / (float)min_side));
  118. imresize(img, new_height, new_width);
  119. } else {
  120. imresize(img, RESIZE_HEIGHT, RESIZE_WIDTH);
  121. }
  122. data_crop(img, CROP_HEGHT, CROP_WIDTH);
  123. sub_mean(img, R_MEAN, G_MEAN, B_MEAN);
  124. data_scale(img, SCALE);
  125. if(to_bgr) {
  126. imrgb2bgr(img);
  127. }
  128. imhwc2chw(img);
  129. }
  130. void parseParams(int argc, char **argv, ServerParams *params)
  131. {
  132. int i = 1;
  133. memset(params, 0, sizeof(*params));
  134. params->plinkname = "/tmp/plink_npu_rgb.test";
  135. params->width = IMG_WIDTH;
  136. params->height = IMG_HEIGHT;
  137. params->stride = STRIDE_WIDTH;
  138. params->frames = 3;
  139. params->format = PLINK_COLOR_Format24BitBGR888Planar;
  140. while (i < argc)
  141. {
  142. if (argv[i][0] != '-' || strlen(argv[i]) < 2)
  143. {
  144. i++;
  145. continue;
  146. }
  147. if (argv[i][1] == 'l')
  148. {
  149. if (++i < argc)
  150. {
  151. params->plinkname = argv[i++];
  152. }
  153. }
  154. else if (argv[i][1] == 'i')
  155. {
  156. if (++i < argc)
  157. {
  158. params->inputfile = argv[i++];
  159. }
  160. }
  161. else if (argv[i][1] == 'f')
  162. {
  163. if (++i < argc)
  164. {
  165. params->format = atoi(argv[i++]);
  166. }
  167. }
  168. else if (argv[i][1] == 'w')
  169. {
  170. if (++i < argc)
  171. {
  172. params->width = atoi(argv[i++]);
  173. if (params->stride == 0)
  174. params->stride = params->width;
  175. }
  176. }
  177. else if (argv[i][1] == 'h')
  178. {
  179. if (++i < argc)
  180. {
  181. params->height = atoi(argv[i++]);
  182. }
  183. }
  184. else if (argv[i][1] == 's')
  185. {
  186. if (++i < argc)
  187. {
  188. params->stride = atoi(argv[i++]);
  189. }
  190. }
  191. else if (argv[i][1] == 'n')
  192. {
  193. if (++i < argc)
  194. {
  195. params->frames = atoi(argv[i++]);
  196. }
  197. }
  198. };
  199. }
  200. int checkParams(ServerParams *params)
  201. {
  202. if (params->plinkname == NULL ||
  203. params->inputfile == NULL ||
  204. params->format == PLINK_COLOR_FormatUnused ||
  205. params->width == 0 ||
  206. params->height == 0 ||
  207. params->stride == 0)
  208. return -1;
  209. return 0;
  210. }
  211. int getBufferSize(ServerParams *params)
  212. {
  213. int size = 0;
  214. switch (params->format)
  215. {
  216. case PLINK_COLOR_FormatYUV420Planar:
  217. case PLINK_COLOR_FormatYUV420SemiPlanar:
  218. size = params->stride * params->height * 3 / 2;
  219. break;
  220. case PLINK_COLOR_Format24BitRGB888Planar:
  221. case PLINK_COLOR_Format24BitBGR888Planar:
  222. size = params->stride * params->height * 3;
  223. break;
  224. default:
  225. size = 0;
  226. }
  227. return size;
  228. }
  229. void constructRGBInfo(PlinkRGBInfo *info, ServerParams *params, unsigned int bus_address, int id)
  230. {
  231. //int size = params->width * params->stride;
  232. int size_r = params->stride * params->stride;
  233. info->header.type = PLINK_TYPE_2D_RGB;
  234. info->header.size = DATA_SIZE(*info);
  235. info->header.id = id + 1;
  236. info->format = params->format;
  237. info->bus_address_b = bus_address;
  238. info->bus_address_g = info->bus_address_b + size_r;
  239. info->bus_address_r = info->bus_address_g + size_r;
  240. info->img_width = params->width;
  241. info->img_height = params->height;
  242. info->stride_r = params->stride;
  243. info->stride_g = params->stride;
  244. info->stride_b = params->stride;
  245. info->offset_r = 0;
  246. info->offset_g = 0;
  247. info->offset_b = 0;
  248. }
  249. int getBufferCount(PlinkPacket *pkt)
  250. {
  251. int ret = 0;
  252. for (int i = 0; i < pkt->num; i++)
  253. {
  254. PlinkDescHdr *hdr = (PlinkDescHdr *)(pkt->list[i]);
  255. if (hdr->type == PLINK_TYPE_MESSAGE)
  256. {
  257. int *data = (int *)(pkt->list[i] + DATA_HEADER_SIZE);
  258. if (*data == PLINK_EXIT_CODE)
  259. {
  260. ret |= 0x80000000; // set bit 31 to 1 to indicate 'exit'
  261. }
  262. else if (*data >= 0)
  263. ret++;
  264. }
  265. }
  266. return ret;
  267. }
  268. void retreiveSentBuffers(PlinkHandle plink, PlinkChannel *channel)
  269. {
  270. PlinkStatus sts = PLINK_STATUS_OK;
  271. while (channel->available_bufs < NUM_OF_BUFFERS)
  272. {
  273. do
  274. {
  275. sts = PLINK_recv(plink, channel->id, &channel->pkt);
  276. int count = getBufferCount(&channel->pkt);
  277. if (count > 0)
  278. {
  279. channel->available_bufs += count;
  280. }
  281. } while (sts == PLINK_STATUS_MORE_DATA);
  282. }
  283. }
  284. #ifdef _DMABUF_FD_SRC_
  285. void AllocateBuffers(PictureBuffer picbuffers[NUM_OF_BUFFERS], unsigned int size, void *vmem)
  286. {
  287. unsigned int buffer_size = (size + 0xFFF) & ~0xFFF;
  288. VmemParams params;
  289. params.size = buffer_size;
  290. params.flags = VMEM_FLAG_CONTIGUOUS | VMEM_FLAG_4GB_ADDR;
  291. for (int i = 0; i < NUM_OF_BUFFERS; i++)
  292. {
  293. VMEM_allocate(vmem, &params);
  294. VMEM_mmap(vmem, &params);
  295. VMEM_export(vmem, &params);
  296. printf("[SERVER] mmap %p from %x with size %d, dma-buf fd %d\n",
  297. params.vir_address, params.phy_address, params.size, params.fd);
  298. picbuffers[i].virtual_address = params.vir_address;
  299. picbuffers[i].bus_address = params.phy_address;
  300. picbuffers[i].size = buffer_size;
  301. picbuffers[i].fd = params.fd;
  302. }
  303. }
  304. void FreeBuffers(PictureBuffer picbuffers[NUM_OF_BUFFERS], void *vmem)
  305. {
  306. VmemParams params;
  307. memset(&params, 0, sizeof(params));
  308. for (int i = 0; i < NUM_OF_BUFFERS; i++)
  309. {
  310. close(picbuffers[i].fd);
  311. params.size = picbuffers[i].size;
  312. params.vir_address = picbuffers[i].virtual_address;
  313. params.phy_address = picbuffers[i].bus_address;
  314. VMEM_free(vmem, &params);
  315. }
  316. }
  317. #else
  318. void AllocateBuffers(PictureBuffer picbuffers[NUM_OF_BUFFERS], unsigned int size, int fd_mem) {
  319. unsigned int bus_address = BASE_MEMORY;
  320. unsigned int buffer_size = (size + 0xFFF) & ~0xFFF;
  321. for (int i = 0; i < NUM_OF_BUFFERS; i++) {
  322. picbuffers[i].virtual_address = mmap(0, buffer_size, PROT_READ | PROT_WRITE,
  323. MAP_SHARED, fd_mem,
  324. bus_address);
  325. printf("mmap %p from %x with size %d\n", picbuffers[i].virtual_address, bus_address, size);
  326. picbuffers[i].bus_address = bus_address;
  327. picbuffers[i].size = buffer_size;
  328. bus_address += buffer_size;
  329. }
  330. }
  331. void FreeBuffers(PictureBuffer picbuffers[NUM_OF_BUFFERS], unsigned int size, int fd_mem) {
  332. for (int i = 0; i < NUM_OF_BUFFERS; i++) {
  333. munmap(picbuffers[i].virtual_address, picbuffers[i].size);
  334. }
  335. }
  336. #endif
  337. int main(int argc, char **argv) {
  338. char **data_path = NULL;
  339. PlinkStatus sts = PLINK_STATUS_OK;
  340. ServerParams params;
  341. PlinkChannel channel[2];
  342. PlinkHandle plink = NULL;
  343. PlinkRGBInfo pic;
  344. PlinkMsg msg;
  345. int input_num = 1;
  346. int output_num = 1;
  347. int i;
  348. int index = 0;
  349. if (argc < (1 + input_num)) {
  350. printf("Please set valide args: ./dw_src_test image.rgb\n");
  351. return -1;
  352. } else {
  353. data_path = argv + 1;
  354. }
  355. parseParams(argc, argv, &params);
  356. if (checkParams(&params) != 0)
  357. {
  358. printUsage(argv[0]);
  359. //return 0;
  360. }
  361. FILE *fp;
  362. #if 0
  363. fp = fopen(params.inputfile, "rb");
  364. if (fp == NULL) {
  365. printf("failed to open %s\n", params.inputfile);
  366. errExit("fopen");
  367. }
  368. #endif
  369. #ifdef _DMABUF_FD_SRC_
  370. void *vmem = NULL;
  371. if (VMEM_create(&vmem) != VMEM_STATUS_OK)
  372. errExit("Failed to create VMEM.");
  373. #else
  374. int fd_mem = open("/dev/mem", O_RDWR | O_SYNC);
  375. if (fd_mem < 0) {
  376. printf("%s: failed to open /dev/mem", MODULE_NAME);
  377. return -1;
  378. }
  379. #endif
  380. int in_size = input_size[0];
  381. char filename[FILE_LENGTH] = {0};
  382. char filename_prefix[FILE_LENGTH] = {0};
  383. uint64_t start_time, end_time;
  384. int frames = params.frames;
  385. PictureBuffer picbuffers[NUM_OF_BUFFERS];
  386. #ifdef _DMABUF_FD_SRC_
  387. AllocateBuffers(picbuffers, in_size, vmem);
  388. #else
  389. AllocateBuffers(picbuffers, in_size, fd_mem);
  390. #endif
  391. sts = PLINK_create(&plink, params.plinkname, PLINK_MODE_SERVER);
  392. memset(&channel[0], 0, sizeof(channel[0]));
  393. channel[0].available_bufs = NUM_OF_BUFFERS;
  394. sts = PLINK_connect(plink, &channel[0].id);
  395. int frmcnt = 0;
  396. do {
  397. int sendid = channel[0].sendid;
  398. fill_buffer_from_file(data_path[0], picbuffers[sendid].virtual_address);
  399. //snprintf(filename, FILE_LENGTH, "%s_src_data%u.txt", filename_prefix, i);
  400. //save_uint8_to_file(filename, (uint8_t*)picbuffers[index].virtual_address, in_size);
  401. constructRGBInfo(&pic, &params, picbuffers[sendid].bus_address, sendid);
  402. printf("[SERVER] Processed frame %d 0x%010llx: %dx%d, stride = %d\n",
  403. sendid, pic.bus_address_b,
  404. pic.img_width, pic.img_height,
  405. pic.stride_b);
  406. channel[0].pkt.list[0] = &pic;
  407. channel[0].pkt.num = 1;
  408. #ifdef _DMABUF_FD_SRC_
  409. channel[0].pkt.fd = picbuffers[sendid].fd;
  410. #else
  411. channel[0].pkt.fd = PLINK_INVALID_FD; // physical address
  412. #endif
  413. sts = PLINK_send(plink, channel[0].id, &channel[0].pkt);
  414. channel[0].sendid = (channel[0].sendid + 1) % NUM_OF_BUFFERS;
  415. channel[0].available_bufs -= 1;
  416. // Notify npu one picture is ready for inference
  417. int timeout = 0;
  418. if (channel[0].available_bufs == 0)
  419. timeout = 60000; // wait up to 60 seconds if buffers are used up
  420. if (PLINK_wait(plink, channel[0].id, timeout) == PLINK_STATUS_OK)
  421. {
  422. do
  423. {
  424. sts = PLINK_recv(plink, channel[0].id, &channel[0].pkt);
  425. int count = getBufferCount(&channel[0].pkt);
  426. if (count < 0)
  427. channel[0].exit = 1;
  428. channel[0].available_bufs += count;
  429. } while (sts == PLINK_STATUS_MORE_DATA);
  430. }
  431. index = (index + 1) % NUM_OF_BUFFERS;
  432. } while (channel[0].exit == 0 && frmcnt < frames);
  433. retreiveSentBuffers(plink, &channel[0]);
  434. cleanup:
  435. msg.header.type = PLINK_TYPE_MESSAGE;
  436. msg.header.size = DATA_SIZE(PlinkMsg);
  437. msg.msg = PLINK_EXIT_CODE;
  438. channel[0].pkt.list[0] = &msg;
  439. channel[0].pkt.num = 1;
  440. channel[0].pkt.fd = PLINK_INVALID_FD;
  441. sts = PLINK_send(plink, channel[0].id, &channel[0].pkt);
  442. sleep(1); // Sleep one second to make sure client is ready for exit
  443. PLINK_close(plink, PLINK_CLOSE_ALL);
  444. #ifdef _DMABUF_FD_SRC_
  445. FreeBuffers(picbuffers, vmem);
  446. #else
  447. FreeBuffers(picbuffers, in_size, fd_mem);
  448. #endif
  449. if (fp != NULL)
  450. fclose(fp);
  451. return 0;
  452. }