cam_demo_ir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include "pthread.h"
  15. #include <sys/mman.h>
  16. #define LOG_LEVEL 2
  17. #define LOG_PREFIX "cam_demo_ir"
  18. #include <syslog.h>
  19. #include <csi_frame.h>
  20. #include <csi_camera.h>
  21. #include "csi_camera_dev_api.h"
  22. #define TEST_DEVICE_NAME "/dev/video12"
  23. typedef struct frame_fps {
  24. uint64_t frameNumber;
  25. struct timeval initTick;
  26. float fps;
  27. } frame_fps_t;
  28. static void dump_camera_meta(csi_frame_s *frame);
  29. static void dump_camera_frame(csi_frame_s *frame, int ch_id);
  30. typedef struct event_queue_item{
  31. struct csi_camera_event evet;
  32. struct event_queue_item *next;
  33. }event_queue_item_t;
  34. typedef struct channel_handle_ctx{
  35. struct event_queue{
  36. event_queue_item_t *head;
  37. event_queue_item_t *tail;
  38. int exit;
  39. pthread_mutex_t mutex;
  40. pthread_cond_t cond;
  41. }event_queue;
  42. csi_cam_handle_t cam_handle;
  43. pthread_t chan_thread;
  44. int frame_num;
  45. }channel_handle_ctx_t;
  46. static event_queue_item_t *dequeue_event(channel_handle_ctx_t *ctx)
  47. {
  48. event_queue_item_t *ev = ctx->event_queue.head;
  49. pthread_mutex_lock(&ctx->event_queue.mutex);
  50. if(!ev)
  51. {
  52. pthread_mutex_unlock(&ctx->event_queue.mutex);
  53. usleep(1000);
  54. return NULL;
  55. }
  56. if(ev == ctx->event_queue.tail)
  57. ctx->event_queue.tail = NULL;
  58. ctx->event_queue.head = ev->next;
  59. pthread_mutex_unlock(&ctx->event_queue.mutex);
  60. return ev;
  61. }
  62. static int test_flag =0;
  63. static void enqueue_event(channel_handle_ctx_t *ctx,event_queue_item_t * item)
  64. {
  65. if(!item || !ctx)
  66. return;
  67. item->next = NULL;
  68. LOG_D("%s enter \n", __func__);
  69. pthread_mutex_lock(&ctx->event_queue.mutex);
  70. if (ctx->event_queue.tail) {
  71. ctx->event_queue.tail->next = item;
  72. } else {
  73. ctx->event_queue.head = item;
  74. // pthread_cond_broadcast(&ctx->cond.cond);
  75. }
  76. ctx->event_queue.tail = item;
  77. pthread_mutex_unlock(&ctx->event_queue.mutex);
  78. }
  79. static void* camera_channle_process(void* arg)
  80. {
  81. struct timeval init_time, cur_time;
  82. frame_fps_t demo_fps;
  83. csi_frame_s frame;
  84. memset(&init_time, 0, sizeof(init_time));
  85. memset(&cur_time, 0, sizeof(cur_time));
  86. channel_handle_ctx_t * ctx = (channel_handle_ctx_t *)arg;
  87. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  88. LOG_D("Channel process running........\n");
  89. while(ctx->event_queue.exit == 0)
  90. {
  91. event_queue_item_t *item = dequeue_event(ctx);
  92. if(item==NULL)
  93. {
  94. continue;
  95. }
  96. switch (item->evet.type) {
  97. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  98. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  99. switch (item->evet.id) {
  100. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  101. csi_camera_channel_id_e ch_id = item->evet.type-CSI_CAMERA_EVENT_TYPE_CHANNEL0+CSI_CAMERA_CHANNEL_0;
  102. int read_frame_count = csi_camera_get_frame_count(ctx->cam_handle,
  103. ch_id);
  104. for (int i = 0; i < read_frame_count; i++) {
  105. csi_camera_get_frame(ctx->cam_handle, ch_id, &frame, timeout);
  106. demo_fps.frameNumber++;
  107. ctx->frame_num++;
  108. dump_camera_meta(&frame);
  109. if(test_flag ==0)
  110. {
  111. dump_camera_frame(&frame,ch_id);
  112. }
  113. // csi_camera_frame_unlock(cam_handle, &frame);
  114. csi_camera_put_frame(&frame);
  115. csi_frame_release(&frame);
  116. }
  117. unsigned long diff;
  118. if (init_time.tv_usec == 0)
  119. gettimeofday(&init_time,0);//osGetTick();
  120. gettimeofday(&cur_time, 0);
  121. diff = 1000000 * (cur_time.tv_sec-init_time.tv_sec)+ cur_time.tv_usec-init_time.tv_usec;
  122. if (diff != 0)
  123. demo_fps.fps = (float)demo_fps.frameNumber / diff * 1000000.0f;
  124. LOG_O("ch id:%d read_frame_count = %d, frame_count = %d, fps = %.2f diff = %ld\n",ch_id, read_frame_count, ctx->frame_num, demo_fps.fps, diff);
  125. break;
  126. }
  127. default:
  128. break;
  129. }
  130. default:
  131. break;
  132. }
  133. free(item);
  134. }
  135. LOG_O("exit\n");
  136. pthread_exit(0);
  137. }
  138. channel_handle_ctx_t channel_ctx[2];
  139. /***********************DSP ALGO *******************************/
  140. typedef struct cb_context{
  141. int id;
  142. csi_cam_handle_t cam_handle;
  143. int dsp_id;
  144. int dsp_path;
  145. void *ref_buf;
  146. int buf_size;
  147. }cb_context_t;
  148. typedef struct cb_args{
  149. char lib_name[8];
  150. char setting[16];
  151. uint32_t frame_id;
  152. uint64_t timestap;
  153. uint32_t temp_projector; //投射器温度
  154. uint32_t temp_sensor; //sensor 温度
  155. }cb_args_t;
  156. cb_context_t cb_context[2];
  157. void dsp_algo_result_cb(void*context,void*arg)
  158. {
  159. cb_context_t *ctx = (cb_context_t *)context;
  160. cb_args_t *cb_args= (cb_args_t*)arg;
  161. char update_setting[16];
  162. struct timeval times_value;
  163. uint32_t frame_id = cb_args->frame_id;
  164. uint64_t frame_timestap = cb_args->timestap;
  165. times_value.tv_sec = cb_args->timestap/1000000;
  166. times_value.tv_usec = cb_args->timestap%1000000;
  167. LOG_O("cb:%d,%s,setting:%s,frame :%d,timestad:(%ld s,%ld us)\n",ctx->id,cb_args->lib_name,cb_args->setting,
  168. frame_id,times_value.tv_sec,times_value.tv_usec);
  169. LOG_O("temperate projector:%d,sensor:%d\n",cb_args->temp_projector,cb_args->temp_sensor);
  170. if(test_flag!=0)
  171. {
  172. sprintf(update_setting, "update_%d", frame_id);
  173. csi_camera_update_dsp_algo_setting(ctx->cam_handle,ctx->dsp_id,ctx->dsp_path,update_setting);
  174. if(frame_id%20==0)
  175. {
  176. void *replace_buf;
  177. *((uint32_t*)ctx->ref_buf) = frame_id;
  178. csi_camera_update_dsp_algo_buf(ctx->cam_handle, ctx->dsp_id,ctx->dsp_path,ctx->ref_buf,&replace_buf);
  179. LOG_D("cb:%d,old buffer:0x%llx,new buffer:0x%llx\n",ctx->id,ctx->ref_buf,replace_buf);
  180. ctx->ref_buf = replace_buf;
  181. }
  182. }
  183. }
  184. static void usage(void)
  185. {
  186. printf(" 1 : video id cases\n");
  187. printf(" 2 : [0]dump frame or [1] algo loop test\n");
  188. printf(" 3 : frame num to stop ,0: for not stop\n");
  189. printf(" 4 : channel 0 algo\n");
  190. printf(" 5 : channel 1 algo\n");
  191. }
  192. int main(int argc, char *argv[])
  193. {
  194. uint32_t i;
  195. bool running = true;
  196. csi_camera_info_s camera_info;
  197. frame_fps_t demo_fps;
  198. char *algo_1;
  199. char *algo_2;
  200. int video_id = -1;
  201. char dev_name[64];
  202. int frame_num = 0;
  203. algo_2 = "dsp1_dummy_algo_flo";
  204. algo_1 = "dsp1_dummy_algo_flo_1";
  205. int ret = 0;
  206. switch(argc)
  207. {
  208. case 0:
  209. case 1:
  210. LOG_E("input ERROR:vidoe id is mandatory\n");
  211. usage();
  212. return -1;
  213. case 6:
  214. algo_2 = (argv[5]);
  215. case 5:
  216. algo_1 = (argv[4]);
  217. case 4:
  218. frame_num = atoi(argv[3]);
  219. case 3:
  220. test_flag = atoi(argv[2]);
  221. case 2:
  222. video_id = atoi(argv[1]);
  223. break;
  224. default:
  225. LOG_E("input ERROR:too many param\n");
  226. usage();
  227. return -1;
  228. }
  229. LOG_O("Demo: video id:%d,test type:%d,frame number:%d,algo1:%s,algo2:%s\n",video_id,test_flag,frame_num,algo_1,algo_2);
  230. sprintf(dev_name, "/dev/video%d", video_id);
  231. memset(&demo_fps, 0, sizeof(demo_fps));
  232. struct timeval init_time, cur_time;
  233. memset(&init_time, 0, sizeof(init_time));
  234. memset(&cur_time, 0, sizeof(cur_time));
  235. // 获取设备中,所有的Camera
  236. struct csi_camera_infos camera_infos;
  237. memset(&camera_infos, 0, sizeof(camera_infos));
  238. csi_camera_query_list(&camera_infos);
  239. LOG_O("csi_camera_query_list() OK\n");
  240. // 打印所有设备所支持的Camera
  241. for (i = 0; i < camera_infos.count; i++) {
  242. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  243. i, camera_infos.info[i].camera_name, camera_infos.info[i].device_name,
  244. camera_infos.info[i].bus_info, camera_infos.info[i].capabilities);
  245. }
  246. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  247. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  248. */
  249. bool found_camera = false;
  250. for (i = 0; i < camera_infos.count; i++) {
  251. if (strcmp(camera_infos.info[i].device_name, dev_name) == 0) {
  252. camera_info = camera_infos.info[i];
  253. printf("found device_name:'%s'\n", camera_info.device_name);
  254. found_camera = true;
  255. break;
  256. }
  257. }
  258. if (!found_camera) {
  259. LOG_E("Can't find camera_name:'%s'\n", dev_name);
  260. exit(-1);
  261. }
  262. printf("The caps are:\n");
  263. for (i = 1; i < 0x80000000; i = i << 1) {
  264. switch (camera_info.capabilities & i) {
  265. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  266. printf("\t Video capture\n");
  267. break;
  268. case CSI_CAMERA_CAP_META_CAPTURE:
  269. printf("\t metadata capture\n");
  270. break;
  271. default:
  272. if (camera_info.capabilities & i) {
  273. printf("\t unknown capabilities(0x%08x)\n", camera_info.capabilities & i);
  274. }
  275. break;
  276. }
  277. }
  278. /* The caps are: Video capture, metadata capture */
  279. // 打开Camera设备获取句柄,作为后续操对象
  280. csi_cam_handle_t cam_handle;
  281. ret = csi_camera_open(&cam_handle, camera_info.device_name);
  282. if (ret) {
  283. LOG_E("Failed to csi_camera_open %s\n", camera_info.device_name);
  284. exit(-1);
  285. }
  286. LOG_O("csi_camera_open() OK\n");
  287. // 获取Camera支持的工作模式
  288. struct csi_camera_modes camera_modes;
  289. camera_modes.count = 0;
  290. csi_camera_get_modes(cam_handle, &camera_modes);
  291. LOG_O("csi_camera_get_modes() OK\n");
  292. // 打印camera所支持的所有工作模式
  293. printf(" Camera:'%s' modes are:{\n", dev_name);
  294. for (i = 0; i < camera_modes.count; i++) {
  295. printf("\t mode_id=%d: description:'%s'\n",
  296. camera_modes.modes[i].mode_id, camera_modes.modes[i].description);
  297. }
  298. printf("}\n");
  299. csi_camera_channel_cfg_s chn_cfg;
  300. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  301. chn_cfg.frm_cnt = 4;
  302. chn_cfg.img_fmt.width = 1080;
  303. chn_cfg.img_fmt.height = 1280;
  304. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  305. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  306. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  307. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  308. CSI_CAMERA_CHANNEL_CAPTURE_META;
  309. ret = csi_camera_channel_open(cam_handle, &chn_cfg);
  310. if (ret) {
  311. exit(-1);
  312. }
  313. LOG_O("CSI_CAMERA_CHANNEL_0: csi_camera_channel_open() OK\n");
  314. // 打开输出channel_1
  315. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_1;
  316. chn_cfg.frm_cnt = 4;
  317. chn_cfg.img_fmt.width = 1080;
  318. chn_cfg.img_fmt.height = 1280;
  319. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  320. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  321. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  322. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  323. CSI_CAMERA_CHANNEL_CAPTURE_META;
  324. ret = csi_camera_channel_open(cam_handle, &chn_cfg);
  325. if (ret) {
  326. exit(-1);
  327. }
  328. LOG_O("CSI_CAMERA_CHANNEL_1: csi_camera_channel_open() OK\n");
  329. // 订阅Event
  330. csi_cam_event_handle_t event_handle;
  331. csi_camera_create_event(&event_handle, cam_handle);
  332. struct csi_camera_event_subscription subscribe;
  333. subscribe.type =
  334. CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  335. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  336. csi_camera_subscribe_event(event_handle, &subscribe);
  337. subscribe.type =
  338. CSI_CAMERA_EVENT_TYPE_CHANNEL0; // 订阅Channel0的FRAME_READY事件
  339. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  340. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  341. csi_camera_subscribe_event(event_handle, &subscribe);
  342. memset(&channel_ctx[0],0x0,sizeof(channel_handle_ctx_t));
  343. channel_ctx[0].cam_handle = cam_handle;
  344. pthread_mutex_init(&channel_ctx[0].event_queue.mutex,NULL);
  345. pthread_cond_init(&channel_ctx[0].event_queue.cond,NULL);
  346. pthread_create(&channel_ctx[0].chan_thread,NULL,camera_channle_process,&channel_ctx[0]);
  347. subscribe.type =
  348. CSI_CAMERA_EVENT_TYPE_CHANNEL1; // 订阅Channel0的FRAME_READY事件
  349. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  350. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  351. csi_camera_subscribe_event(event_handle, &subscribe);
  352. memset(&channel_ctx[1],0x0,sizeof(channel_handle_ctx_t));
  353. channel_ctx[1].cam_handle = cam_handle;
  354. pthread_mutex_init(&channel_ctx[1].event_queue.mutex,NULL);
  355. pthread_cond_init(&channel_ctx[1].event_queue.cond,NULL);
  356. pthread_create(&channel_ctx[1].chan_thread,NULL,camera_channle_process,&channel_ctx[1]);
  357. LOG_O("Event subscript OK\n");
  358. cb_context[0].id=0;
  359. cb_context[0].cam_handle = cam_handle;
  360. cb_context[0].dsp_id = 1;
  361. cb_context[0].dsp_path = 3;
  362. char test_set[16]="init";
  363. cb_context[0].buf_size = 1920*1080;
  364. cb_context[0].ref_buf=NULL;
  365. csi_camera_dsp_algo_param_t algo_param_1={
  366. .algo_name = algo_1,
  367. .algo_cb.cb = dsp_algo_result_cb,
  368. .algo_cb.context = &cb_context[0],
  369. .algo_cb.arg_size = sizeof(cb_args_t),
  370. .sett_ptr = test_set,
  371. .sett_size =sizeof(test_set),
  372. .extra_buf_num =1,
  373. .extra_buf_sizes = &cb_context[0].buf_size,
  374. .extra_bufs = &cb_context[0].ref_buf,
  375. };
  376. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[0].dsp_id,cb_context[0].dsp_path, &algo_param_1))
  377. {
  378. LOG_E("set DSP algo fail\n");
  379. }
  380. cb_context[1].id=1;
  381. cb_context[1].cam_handle = cam_handle;
  382. cb_context[1].dsp_id = 1;
  383. cb_context[1].dsp_path = 4;
  384. cb_context[1].buf_size = 1920*1080;
  385. cb_context[1].ref_buf=NULL;
  386. csi_camera_dsp_algo_param_t algo_param_2={
  387. .algo_name = algo_2,
  388. .algo_cb.cb = dsp_algo_result_cb,
  389. .algo_cb.context = &cb_context[1],
  390. .algo_cb.arg_size = sizeof(cb_args_t),
  391. .sett_ptr = test_set,
  392. .sett_size =sizeof(test_set),
  393. .extra_buf_num =1,
  394. .extra_buf_sizes = &cb_context[1].buf_size,
  395. .extra_bufs = &cb_context[1].ref_buf,
  396. };
  397. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[1].dsp_id,cb_context[1].dsp_path, &algo_param_2))
  398. {
  399. LOG_E("set DSP algo fail\n");
  400. }
  401. csi_camera_floodlight_led_set_flash_bright(cam_handle, 500); //500ma
  402. csi_camera_projection_led_set_flash_bright(cam_handle, 500); //500ma
  403. csi_camera_projection_led_set_mode(cam_handle, LED_IR_ENABLE);
  404. csi_camera_floodlight_led_set_mode(cam_handle, LED_IR_ENABLE);
  405. csi_camera_led_enable(cam_handle, LED_FLOODLIGHT_PROJECTION);
  406. csi_camera_led_set_switch_mode(cam_handle, SWITCH_MODE_PROJECTION_EVEN_FLOODLIGHT_ODD);
  407. // 开始从channel中取出准备好的frame
  408. ret = csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_0);
  409. if (ret) {
  410. exit(-1);
  411. }
  412. ret = csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_1);
  413. if (ret) {
  414. exit(-1);
  415. }
  416. LOG_O("Channel start OK\n");
  417. // 处理订阅的Event
  418. csi_frame_s frame;
  419. event_queue_item_t *event_item;
  420. LOG_O("Starting get frame...\n");
  421. while (running) {
  422. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  423. event_item = malloc(sizeof( event_queue_item_t));
  424. if(csi_camera_get_event(event_handle, &event_item->evet, timeout))
  425. {
  426. LOG_E("get event timeout\n");
  427. free(event_item);
  428. continue;
  429. }
  430. LOG_O("event.type = %d, event.id = %d\n",event_item->evet.type, event_item->evet.id);
  431. switch (event_item->evet.type) {
  432. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  433. switch (event_item->evet.id) {
  434. case CSI_CAMERA_EVENT_ERROR:
  435. LOG_E("CSI_CAMERA_EVENT_ERROR reprot\n");
  436. break;
  437. case CSI_CAMERA_EVENT_WARNING:
  438. LOG_W("CSI_CAMERA_EVENT_ERROR reprot\n");
  439. break;
  440. default:
  441. break;
  442. free(event_item);
  443. break;
  444. }
  445. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  446. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  447. switch (event_item->evet.id) {
  448. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  449. csi_camera_channel_id_e ch_id = event_item->evet.type-CSI_CAMERA_EVENT_TYPE_CHANNEL0+CSI_CAMERA_CHANNEL_0;
  450. if(event_item && ch_id<2)
  451. {
  452. enqueue_event(&channel_ctx[ch_id],event_item);
  453. }
  454. break;
  455. }
  456. default:
  457. free(event_item);
  458. break;
  459. }
  460. break;
  461. default:
  462. free(event_item);
  463. break;
  464. }
  465. // LOG_O("stop:%d,frame:(%d,%d)(%llx,%llx)\n",frame_num ,channel_ctx[0].frame_num ,channel_ctx[1].frame_num,&(channel_ctx[0].frame_num),&(channel_ctx[1].frame_num));
  466. if((frame_num >0) &&
  467. (channel_ctx[0].frame_num >frame_num) &&
  468. (channel_ctx[1].frame_num >frame_num))
  469. {
  470. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_0);
  471. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_1);
  472. channel_ctx[0].event_queue.exit = 1;
  473. channel_ctx[1].event_queue.exit = 1;
  474. pthread_join(channel_ctx[0].chan_thread,NULL);
  475. pthread_join(channel_ctx[1].chan_thread,NULL);
  476. running = false;
  477. }
  478. }
  479. // 取消订阅某一个event, 也可以直接调用csi_camera_destory_event,结束所有的订阅
  480. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0;
  481. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  482. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  483. csi_camera_unsubscribe_event(event_handle, &subscribe);
  484. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL1;
  485. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  486. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  487. csi_camera_unsubscribe_event(event_handle, &subscribe);
  488. subscribe.type = CSI_CAMERA_EVENT_TYPE_CAMERA;
  489. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  490. csi_camera_unsubscribe_event(event_handle, &subscribe);
  491. csi_camera_destory_event(event_handle);
  492. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_0);
  493. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_1);
  494. csi_camera_close(cam_handle);
  495. }
  496. static void dump_camera_meta(csi_frame_s *frame)
  497. {
  498. int i;
  499. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  500. return;
  501. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  502. csi_camrea_meta_unit_s meta_frame_id,meta_frame_ts;
  503. csi_camera_frame_get_meta_unit(
  504. &meta_frame_id, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  505. csi_camera_frame_get_meta_unit(
  506. &meta_frame_ts, meta_data, CSI_CAMERA_META_ID_TIMESTAMP);
  507. LOG_O("meta frame id=%d,time stap:(%ld,%ld)\n",
  508. meta_frame_id.int_value,meta_frame_ts.time_value.tv_sec,meta_frame_ts.time_value.tv_usec);
  509. }
  510. static void dump_camera_frame(csi_frame_s *frame, int ch_id)
  511. {
  512. char file[128];
  513. static int file_indx=0;
  514. int size,buf_size;
  515. uint32_t indexd, j;
  516. void *buf[3];
  517. sprintf(file,"demo_save_img_ch_%d_%d", ch_id,file_indx++%6);
  518. int fd = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
  519. if(fd == -1) {
  520. LOG_E("%s, %d, open file error!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  521. return ;
  522. }
  523. if (frame->img.type == CSI_IMG_TYPE_DMA_BUF) {
  524. buf_size = frame->img.strides[0]*frame->img.height;
  525. switch(frame->img.pix_format) {
  526. case CSI_PIX_FMT_NV12:
  527. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  528. if(frame->img.num_planes ==2)
  529. {
  530. buf_size+=frame->img.strides[1]*frame->img.height/2;
  531. }
  532. else{
  533. LOG_E("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  534. return;
  535. }
  536. break;
  537. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  538. if(frame->img.num_planes ==2)
  539. {
  540. buf_size+=frame->img.strides[1]*frame->img.height;
  541. }
  542. else{
  543. LOG_E("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  544. return;
  545. }
  546. break;
  547. default:
  548. break;
  549. }
  550. buf[0]= mmap(0, buf_size, PROT_READ | PROT_WRITE,
  551. MAP_SHARED, frame->img.dmabuf[0].fds, frame->img.dmabuf[0].offset);
  552. // plane_size[1] = frame->img.strides[1]*frame->img.height;
  553. // printf("frame plane 1 stride:%d,offset:%d\n", frame->img.strides[1],(uint32_t)frame->img.dmabuf[1].offset);
  554. if(frame->img.num_planes ==2)
  555. {
  556. // buf[1] = mmap(0, plane_size[1]/2, PROT_READ | PROT_WRITE,
  557. // MAP_SHARED, frame->img.dmabuf[1].fds, frame->img.dmabuf[1].offset);
  558. buf[1] = buf[0]+frame->img.dmabuf[1].offset;
  559. }
  560. }
  561. else{
  562. buf[0] = frame->img.usr_addr[0];
  563. buf[1] = frame->img.usr_addr[1];
  564. }
  565. LOG_O("save img from :%d, to %s, fmt:%d width:%d stride:%d height:%d\n",frame->img.dmabuf[0].fds, file, frame->img.pix_format , frame->img.width, frame->img.strides[0], frame->img.height);
  566. frame->img.pix_format = CSI_PIX_FMT_RAW_12BIT;
  567. switch(frame->img.pix_format) {
  568. case CSI_PIX_FMT_NV12:
  569. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  570. if (frame->img.strides[0] == 0) {
  571. frame->img.strides[0] = frame->img.width;
  572. }
  573. for (j = 0; j < frame->img.height; j++) { //Y
  574. indexd = j*frame->img.strides[0];
  575. write(fd, buf[0] + indexd, frame->img.width);
  576. }
  577. for (j = 0; j < frame->img.height / 2; j++) { //UV
  578. indexd = j*frame->img.strides[0];
  579. write(fd, buf[1] + indexd, frame->img.width);
  580. }
  581. break;
  582. case CSI_PIX_FMT_RGB_INTEVLEAVED_888:
  583. case CSI_PIX_FMT_YUV_TEVLEAVED_444:
  584. size = frame->img.width*3;
  585. for (j = 0; j < frame->img.height; j++) {
  586. indexd = j*frame->img.strides[0];
  587. write(fd, buf[0] + indexd, size);
  588. }
  589. break;
  590. case CSI_PIX_FMT_BGR:
  591. case CSI_PIX_FMT_RGB_PLANAR_888:
  592. case CSI_PIX_FMT_YUV_PLANAR_444:
  593. size = frame->img.width * frame->img.height * 3;
  594. write(fd, buf[0], size);
  595. break;
  596. case CSI_PIX_FMT_RAW_8BIT:
  597. size = frame->img.width;
  598. for (j = 0; j < frame->img.height; j++) {
  599. indexd = j*frame->img.strides[0];
  600. write(fd, buf[0] + indexd, size);
  601. }
  602. break;
  603. case CSI_PIX_FMT_YUV_TEVLEAVED_422:
  604. case CSI_PIX_FMT_RAW_10BIT:
  605. case CSI_PIX_FMT_RAW_12BIT:
  606. case CSI_PIX_FMT_RAW_14BIT:
  607. case CSI_PIX_FMT_RAW_16BIT:
  608. size = frame->img.width*2;
  609. for (j = 0; j < frame->img.height; j++) {
  610. indexd = j*frame->img.strides[0];
  611. write(fd, buf[0] + indexd, size);
  612. }
  613. break;
  614. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  615. if (frame->img.strides[0] == 0) {
  616. frame->img.strides[0] = frame->img.width;
  617. }
  618. for (j = 0; j < frame->img.height; j++) { //Y
  619. indexd = j*frame->img.strides[0];
  620. write(fd,buf[0] + indexd, frame->img.width);
  621. }
  622. for (j = 0; j < frame->img.height; j++) { //UV
  623. indexd = j*frame->img.strides[0];
  624. write(fd, buf[1]+ indexd, frame->img.width);
  625. }
  626. break;
  627. default:
  628. LOG_E("%s unsupported format to save\n", __func__);
  629. exit(-1);
  630. break;
  631. }
  632. close(fd);
  633. munmap(buf[0],buf_size);
  634. // munmap(buf[1],plane_size[1]);
  635. LOG_O("%s exit\n", __func__);
  636. }