cam_demo_ir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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_ex_s *frame);
  29. static void dump_camera_frame(csi_frame_ex_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_ex_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. }
  116. unsigned long diff;
  117. if (init_time.tv_usec == 0)
  118. gettimeofday(&init_time,0);//osGetTick();
  119. gettimeofday(&cur_time, 0);
  120. diff = 1000000 * (cur_time.tv_sec-init_time.tv_sec)+ cur_time.tv_usec-init_time.tv_usec;
  121. if (diff != 0)
  122. demo_fps.fps = (float)demo_fps.frameNumber / diff * 1000000.0f;
  123. 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);
  124. break;
  125. }
  126. default:
  127. break;
  128. }
  129. default:
  130. break;
  131. }
  132. free(item);
  133. }
  134. LOG_O("exit\n");
  135. pthread_exit(0);
  136. }
  137. channel_handle_ctx_t channel_ctx[2];
  138. /***********************DSP ALGO *******************************/
  139. typedef struct cb_context{
  140. int id;
  141. csi_cam_handle_t cam_handle;
  142. int dsp_id;
  143. int dsp_path;
  144. void *ref_buf;
  145. int buf_size;
  146. }cb_context_t;
  147. typedef struct cb_args{
  148. char lib_name[8];
  149. char setting[16];
  150. uint32_t frame_id;
  151. uint64_t timestap;
  152. uint32_t temp_projector; //投射器温度
  153. uint32_t temp_sensor; //sensor 温度
  154. }cb_args_t;
  155. cb_context_t cb_context[2];
  156. void dsp_algo_result_cb(void*context,void*arg)
  157. {
  158. cb_context_t *ctx = (cb_context_t *)context;
  159. cb_args_t *cb_args= (cb_args_t*)arg;
  160. char update_setting[16];
  161. struct timeval times_value;
  162. uint32_t frame_id = cb_args->frame_id;
  163. uint64_t frame_timestap = cb_args->timestap;
  164. times_value.tv_sec = cb_args->timestap/1000000;
  165. times_value.tv_usec = cb_args->timestap%1000000;
  166. LOG_O("cb:%d,%s,setting:%s,frame :%d,timestad:(%ld s,%ld us)\n",ctx->id,cb_args->lib_name,cb_args->setting,
  167. frame_id,times_value.tv_sec,times_value.tv_usec);
  168. LOG_O("temperate projector:%d,sensor:%d\n",cb_args->temp_projector,cb_args->temp_sensor);
  169. if(test_flag!=0)
  170. {
  171. sprintf(update_setting, "update_%d", frame_id);
  172. csi_camera_update_dsp_algo_setting(ctx->cam_handle,ctx->dsp_id,ctx->dsp_path,update_setting);
  173. if(frame_id%20==0)
  174. {
  175. void *replace_buf;
  176. *((uint32_t*)ctx->ref_buf) = frame_id;
  177. csi_camera_update_dsp_algo_buf(ctx->cam_handle, ctx->dsp_id,ctx->dsp_path,ctx->ref_buf,&replace_buf);
  178. LOG_D("cb:%d,old buffer:0x%llx,new buffer:0x%llx\n",ctx->id,ctx->ref_buf,replace_buf);
  179. ctx->ref_buf = replace_buf;
  180. }
  181. }
  182. }
  183. static void usage(void)
  184. {
  185. printf(" 1 : video id cases\n");
  186. printf(" 2 : [0]dump frame or [1] algo loop test\n");
  187. printf(" 3 : frame num to stop ,0: for not stop\n");
  188. printf(" 4 : channel 0 algo\n");
  189. printf(" 5 : channel 1 algo\n");
  190. }
  191. int main(int argc, char *argv[])
  192. {
  193. uint32_t i;
  194. bool running = true;
  195. csi_camera_info_s camera_info;
  196. frame_fps_t demo_fps;
  197. char *algo_1;
  198. char *algo_2;
  199. int video_id = -1;
  200. char dev_name[64];
  201. int frame_num = 0;
  202. algo_2 = "dsp1_dummy_algo_flo";
  203. algo_1 = "dsp1_dummy_algo_flo_1";
  204. int ret = 0;
  205. switch(argc)
  206. {
  207. case 0:
  208. case 1:
  209. LOG_E("input ERROR:vidoe id is mandatory\n");
  210. usage();
  211. return -1;
  212. case 6:
  213. algo_2 = (argv[5]);
  214. case 5:
  215. algo_1 = (argv[4]);
  216. case 4:
  217. frame_num = atoi(argv[3]);
  218. case 3:
  219. test_flag = atoi(argv[2]);
  220. case 2:
  221. video_id = atoi(argv[1]);
  222. break;
  223. default:
  224. LOG_E("input ERROR:too many param\n");
  225. usage();
  226. return -1;
  227. }
  228. 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);
  229. sprintf(dev_name, "/dev/video%d", video_id);
  230. memset(&demo_fps, 0, sizeof(demo_fps));
  231. struct timeval init_time, cur_time;
  232. memset(&init_time, 0, sizeof(init_time));
  233. memset(&cur_time, 0, sizeof(cur_time));
  234. // 获取设备中,所有的Camera
  235. struct csi_camera_infos camera_infos;
  236. memset(&camera_infos, 0, sizeof(camera_infos));
  237. csi_camera_query_list(&camera_infos);
  238. LOG_O("csi_camera_query_list() OK\n");
  239. // 打印所有设备所支持的Camera
  240. for (i = 0; i < camera_infos.count; i++) {
  241. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  242. i, camera_infos.info[i].camera_name, camera_infos.info[i].device_name,
  243. camera_infos.info[i].bus_info, camera_infos.info[i].capabilities);
  244. }
  245. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  246. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  247. */
  248. bool found_camera = false;
  249. for (i = 0; i < camera_infos.count; i++) {
  250. if (strcmp(camera_infos.info[i].device_name, dev_name) == 0) {
  251. camera_info = camera_infos.info[i];
  252. printf("found device_name:'%s'\n", camera_info.device_name);
  253. found_camera = true;
  254. break;
  255. }
  256. }
  257. if (!found_camera) {
  258. LOG_E("Can't find camera_name:'%s'\n", dev_name);
  259. exit(-1);
  260. }
  261. printf("The caps are:\n");
  262. for (i = 1; i < 0x80000000; i = i << 1) {
  263. switch (camera_info.capabilities & i) {
  264. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  265. printf("\t Video capture\n");
  266. break;
  267. case CSI_CAMERA_CAP_META_CAPTURE:
  268. printf("\t metadata capture\n");
  269. break;
  270. default:
  271. if (camera_info.capabilities & i) {
  272. printf("\t unknown capabilities(0x%08x)\n", camera_info.capabilities & i);
  273. }
  274. break;
  275. }
  276. }
  277. /* The caps are: Video capture, metadata capture */
  278. // 打开Camera设备获取句柄,作为后续操对象
  279. csi_cam_handle_t cam_handle;
  280. ret = csi_camera_open(&cam_handle, camera_info.device_name);
  281. if (ret) {
  282. LOG_E("Failed to csi_camera_open %s\n", camera_info.device_name);
  283. exit(-1);
  284. }
  285. LOG_O("csi_camera_open() OK\n");
  286. // 获取Camera支持的工作模式
  287. struct csi_camera_modes camera_modes;
  288. camera_modes.count = 0;
  289. csi_camera_get_modes(cam_handle, &camera_modes);
  290. LOG_O("csi_camera_get_modes() OK\n");
  291. // 打印camera所支持的所有工作模式
  292. printf(" Camera:'%s' modes are:{\n", dev_name);
  293. for (i = 0; i < camera_modes.count; i++) {
  294. printf("\t mode_id=%d: description:'%s'\n",
  295. camera_modes.modes[i].mode_id, camera_modes.modes[i].description);
  296. }
  297. printf("}\n");
  298. csi_camera_channel_cfg_s chn_cfg;
  299. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  300. chn_cfg.frm_cnt = 4;
  301. chn_cfg.img_fmt.width = 1080;
  302. chn_cfg.img_fmt.height = 1280;
  303. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  304. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  305. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  306. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  307. CSI_CAMERA_CHANNEL_CAPTURE_META;
  308. ret = csi_camera_channel_open(cam_handle, &chn_cfg);
  309. if (ret) {
  310. exit(-1);
  311. }
  312. LOG_O("CSI_CAMERA_CHANNEL_0: csi_camera_channel_open() OK\n");
  313. // 打开输出channel_1
  314. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_1;
  315. chn_cfg.frm_cnt = 4;
  316. chn_cfg.img_fmt.width = 1080;
  317. chn_cfg.img_fmt.height = 1280;
  318. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  319. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  320. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  321. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  322. CSI_CAMERA_CHANNEL_CAPTURE_META;
  323. ret = csi_camera_channel_open(cam_handle, &chn_cfg);
  324. if (ret) {
  325. exit(-1);
  326. }
  327. LOG_O("CSI_CAMERA_CHANNEL_1: csi_camera_channel_open() OK\n");
  328. // 订阅Event
  329. csi_cam_event_handle_t event_handle;
  330. csi_camera_create_event(&event_handle, cam_handle);
  331. struct csi_camera_event_subscription subscribe;
  332. subscribe.type =
  333. CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  334. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  335. csi_camera_subscribe_event(event_handle, &subscribe);
  336. subscribe.type =
  337. CSI_CAMERA_EVENT_TYPE_CHANNEL0; // 订阅Channel0的FRAME_READY事件
  338. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  339. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  340. csi_camera_subscribe_event(event_handle, &subscribe);
  341. memset(&channel_ctx[0],0x0,sizeof(channel_handle_ctx_t));
  342. channel_ctx[0].cam_handle = cam_handle;
  343. pthread_mutex_init(&channel_ctx[0].event_queue.mutex,NULL);
  344. pthread_cond_init(&channel_ctx[0].event_queue.cond,NULL);
  345. pthread_create(&channel_ctx[0].chan_thread,NULL,camera_channle_process,&channel_ctx[0]);
  346. subscribe.type =
  347. CSI_CAMERA_EVENT_TYPE_CHANNEL1; // 订阅Channel0的FRAME_READY事件
  348. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  349. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  350. csi_camera_subscribe_event(event_handle, &subscribe);
  351. memset(&channel_ctx[1],0x0,sizeof(channel_handle_ctx_t));
  352. channel_ctx[1].cam_handle = cam_handle;
  353. pthread_mutex_init(&channel_ctx[1].event_queue.mutex,NULL);
  354. pthread_cond_init(&channel_ctx[1].event_queue.cond,NULL);
  355. pthread_create(&channel_ctx[1].chan_thread,NULL,camera_channle_process,&channel_ctx[1]);
  356. LOG_O("Event subscript OK\n");
  357. cb_context[0].id=0;
  358. cb_context[0].cam_handle = cam_handle;
  359. cb_context[0].dsp_id = 1;
  360. cb_context[0].dsp_path = 3;
  361. char test_set[16]="init";
  362. cb_context[0].buf_size = 1920*1080;
  363. cb_context[0].ref_buf=NULL;
  364. csi_camera_dsp_algo_param_t algo_param_1={
  365. .algo_name = algo_1,
  366. .algo_cb.cb = dsp_algo_result_cb,
  367. .algo_cb.context = &cb_context[0],
  368. .algo_cb.arg_size = sizeof(cb_args_t),
  369. .sett_ptr = test_set,
  370. .sett_size =sizeof(test_set),
  371. .extra_buf_num =1,
  372. .extra_buf_sizes = &cb_context[0].buf_size,
  373. .extra_bufs = &cb_context[0].ref_buf,
  374. };
  375. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[0].dsp_id,cb_context[0].dsp_path, &algo_param_1))
  376. {
  377. LOG_E("set DSP algo fail\n");
  378. }
  379. cb_context[1].id=1;
  380. cb_context[1].cam_handle = cam_handle;
  381. cb_context[1].dsp_id = 1;
  382. cb_context[1].dsp_path = 4;
  383. cb_context[1].buf_size = 1920*1080;
  384. cb_context[1].ref_buf=NULL;
  385. csi_camera_dsp_algo_param_t algo_param_2={
  386. .algo_name = algo_2,
  387. .algo_cb.cb = dsp_algo_result_cb,
  388. .algo_cb.context = &cb_context[1],
  389. .algo_cb.arg_size = sizeof(cb_args_t),
  390. .sett_ptr = test_set,
  391. .sett_size =sizeof(test_set),
  392. .extra_buf_num =1,
  393. .extra_buf_sizes = &cb_context[1].buf_size,
  394. .extra_bufs = &cb_context[1].ref_buf,
  395. };
  396. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[1].dsp_id,cb_context[1].dsp_path, &algo_param_2))
  397. {
  398. LOG_E("set DSP algo fail\n");
  399. }
  400. csi_camera_floodlight_led_set_flash_bright(cam_handle, 500); //500ma
  401. csi_camera_projection_led_set_flash_bright(cam_handle, 500); //500ma
  402. csi_camera_projection_led_set_mode(cam_handle, LED_IR_ENABLE);
  403. csi_camera_floodlight_led_set_mode(cam_handle, LED_IR_ENABLE);
  404. csi_camera_led_enable(cam_handle, LED_FLOODLIGHT_PROJECTION);
  405. csi_camera_led_set_switch_mode(cam_handle, SWITCH_MODE_PROJECTION_EVEN_FLOODLIGHT_ODD);
  406. // 开始从channel中取出准备好的frame
  407. ret = csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_0);
  408. if (ret) {
  409. exit(-1);
  410. }
  411. ret = csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_1);
  412. if (ret) {
  413. exit(-1);
  414. }
  415. LOG_O("Channel start OK\n");
  416. // 处理订阅的Event
  417. csi_frame_s frame;
  418. event_queue_item_t *event_item;
  419. LOG_O("Starting get frame...\n");
  420. while (running) {
  421. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  422. event_item = malloc(sizeof( event_queue_item_t));
  423. if(csi_camera_get_event(event_handle, &event_item->evet, timeout))
  424. {
  425. LOG_E("get event timeout\n");
  426. free(event_item);
  427. continue;
  428. }
  429. LOG_O("event.type = %d, event.id = %d\n",event_item->evet.type, event_item->evet.id);
  430. switch (event_item->evet.type) {
  431. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  432. switch (event_item->evet.id) {
  433. case CSI_CAMERA_EVENT_ERROR:
  434. LOG_E("CSI_CAMERA_EVENT_ERROR reprot\n");
  435. break;
  436. case CSI_CAMERA_EVENT_WARNING:
  437. LOG_W("CSI_CAMERA_EVENT_ERROR reprot\n");
  438. break;
  439. default:
  440. break;
  441. free(event_item);
  442. break;
  443. }
  444. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  445. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  446. switch (event_item->evet.id) {
  447. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  448. csi_camera_channel_id_e ch_id = event_item->evet.type-CSI_CAMERA_EVENT_TYPE_CHANNEL0+CSI_CAMERA_CHANNEL_0;
  449. if(event_item && ch_id<2)
  450. {
  451. enqueue_event(&channel_ctx[ch_id],event_item);
  452. }
  453. break;
  454. }
  455. default:
  456. free(event_item);
  457. break;
  458. }
  459. break;
  460. default:
  461. free(event_item);
  462. break;
  463. }
  464. // 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));
  465. if((frame_num >0) &&
  466. (channel_ctx[0].frame_num >frame_num) &&
  467. (channel_ctx[1].frame_num >frame_num))
  468. {
  469. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_0);
  470. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_1);
  471. channel_ctx[0].event_queue.exit = 1;
  472. channel_ctx[1].event_queue.exit = 1;
  473. pthread_join(channel_ctx[0].chan_thread,NULL);
  474. pthread_join(channel_ctx[1].chan_thread,NULL);
  475. running = false;
  476. }
  477. }
  478. // 取消订阅某一个event, 也可以直接调用csi_camera_destory_event,结束所有的订阅
  479. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0;
  480. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  481. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  482. csi_camera_unsubscribe_event(event_handle, &subscribe);
  483. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL1;
  484. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  485. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  486. csi_camera_unsubscribe_event(event_handle, &subscribe);
  487. subscribe.type = CSI_CAMERA_EVENT_TYPE_CAMERA;
  488. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  489. csi_camera_unsubscribe_event(event_handle, &subscribe);
  490. csi_camera_destory_event(event_handle);
  491. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_0);
  492. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_1);
  493. csi_camera_close(cam_handle);
  494. }
  495. static void dump_camera_meta(csi_frame_ex_s *frame)
  496. {
  497. int i;
  498. if (frame->frame_meta.type != CSI_META_TYPE_CAMERA)
  499. return;
  500. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->frame_meta.data;
  501. csi_camera_meta_unit_s meta_frame_id,meta_frame_ts;
  502. csi_camera_frame_get_meta_unit(
  503. &meta_frame_id, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  504. csi_camera_frame_get_meta_unit(
  505. &meta_frame_ts, meta_data, CSI_CAMERA_META_ID_TIMESTAMP);
  506. LOG_O("meta frame id=%d,time stap:(%ld,%ld)\n",
  507. meta_frame_id.int_value,meta_frame_ts.time_value.tv_sec,meta_frame_ts.time_value.tv_usec);
  508. }
  509. int file_id = 0;
  510. static void dump_camera_frame(csi_frame_ex_s *frame, int ch_id)
  511. {
  512. char file[128];
  513. static int file_indx=0;
  514. int size;
  515. uint32_t indexd, j;
  516. void *buf[3]={NULL,NULL,NULL};
  517. int buf_size;
  518. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->frame_meta.data;
  519. csi_camera_meta_unit_s meta_unit;
  520. csi_camera_frame_get_meta_unit(&meta_unit, meta_data, CSI_CAMERA_META_ID_CAMERA_NAME);
  521. sprintf(file, "demo_save_img_%s_ch%d_%d_%d", meta_unit.str_value, ch_id,file_id, file_indx++%10);
  522. int fd = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
  523. if(fd == -1) {
  524. LOG_E("%s, %d, open file error!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  525. return;
  526. }
  527. for(j =0;j<frame->frame_data.num_plane;j++)
  528. {
  529. buf[j] = frame->frame_data.vir_addr[j];
  530. }
  531. LOG_I("save img from: addr:%p,to %s, fd:%d,fmt:%d width:%d stride:%d height:%d\n", buf[0], file, frame->frame_data.fd[0],
  532. frame->frame_info.pixel_format, frame->frame_info.width, frame->frame_data.stride[0], frame->frame_info.height);
  533. switch(frame->frame_info.pixel_format) {
  534. case CSI_PIX_FMT_NV12:
  535. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  536. for (j = 0; j < frame->frame_info.height; j++) { //Y
  537. indexd = j*frame->frame_data.stride[0];
  538. write(fd, buf[0] + indexd, frame->frame_info.width);
  539. }
  540. for (j = 0; j < frame->frame_info.height / 2; j++) { //UV
  541. indexd = j*frame->frame_data.stride[0];
  542. write(fd, buf[1] + indexd, frame->frame_info.width);
  543. }
  544. break;
  545. case CSI_PIX_FMT_RGB_INTEVLEAVED_888:
  546. case CSI_PIX_FMT_YUV_TEVLEAVED_444:
  547. size = frame->frame_info.width*3;
  548. for (j = 0; j < frame->frame_info.height; j++) {
  549. indexd = j*frame->frame_data.stride[0];
  550. write(fd, buf[0] + indexd, size);
  551. }
  552. break;
  553. case CSI_PIX_FMT_BGR:
  554. case CSI_PIX_FMT_RGB_PLANAR_888:
  555. case CSI_PIX_FMT_YUV_PLANAR_444:
  556. size = frame->frame_info.width * frame->frame_info.height * 3;
  557. write(fd, buf[0], size);
  558. break;
  559. case CSI_PIX_FMT_RAW_8BIT:
  560. size = frame->frame_info.width;
  561. for (j = 0; j < frame->frame_info.height; j++) {
  562. indexd = j*frame->frame_data.stride[0];
  563. write(fd, buf[0] + indexd, size);
  564. }
  565. break;
  566. case CSI_PIX_FMT_YUV_TEVLEAVED_422:
  567. case CSI_PIX_FMT_RAW_10BIT:
  568. case CSI_PIX_FMT_RAW_12BIT:
  569. case CSI_PIX_FMT_RAW_14BIT:
  570. case CSI_PIX_FMT_RAW_16BIT:
  571. size = frame->frame_info.width*2;
  572. for (j = 0; j < frame->frame_info.height; j++) {
  573. indexd = j*frame->frame_data.stride[0];
  574. write(fd, buf[0] + indexd, size);
  575. }
  576. break;
  577. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  578. if (frame->frame_data.stride[0] == 0) {
  579. frame->frame_data.stride[0] = frame->frame_info.width;
  580. }
  581. for (j = 0; j < frame->frame_info.height; j++) { //Y
  582. indexd = j*frame->frame_data.stride[0];
  583. write(fd,buf[0] + indexd, frame->frame_info.width);
  584. }
  585. for (j = 0; j < frame->frame_info.height; j++) { //UV
  586. indexd = j*frame->frame_data.stride[0];
  587. write(fd, buf[1]+ indexd, frame->frame_info.width);
  588. }
  589. break;
  590. default:
  591. LOG_E("%s unsupported format to save\n", __func__);
  592. break;
  593. }
  594. close(fd);
  595. }