cam_demo_ir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. return NULL;
  54. }
  55. if(ev == ctx->event_queue.tail)
  56. ctx->event_queue.tail = NULL;
  57. ctx->event_queue.head = ev->next;
  58. pthread_mutex_unlock(&ctx->event_queue.mutex);
  59. return ev;
  60. }
  61. static int test_flag =0;
  62. static void enqueue_event(channel_handle_ctx_t *ctx,event_queue_item_t * item)
  63. {
  64. if(!item || !ctx)
  65. return;
  66. item->next = NULL;
  67. LOG_D("%s enter \n", __func__);
  68. pthread_mutex_lock(&ctx->event_queue.mutex);
  69. if (ctx->event_queue.tail) {
  70. ctx->event_queue.tail->next = item;
  71. } else {
  72. ctx->event_queue.head = item;
  73. // pthread_cond_broadcast(&ctx->cond.cond);
  74. }
  75. ctx->event_queue.tail = item;
  76. pthread_mutex_unlock(&ctx->event_queue.mutex);
  77. }
  78. static void* camera_channle_process(void* arg)
  79. {
  80. struct timeval init_time, cur_time;
  81. frame_fps_t demo_fps;
  82. csi_frame_s frame;
  83. memset(&init_time, 0, sizeof(init_time));
  84. memset(&cur_time, 0, sizeof(cur_time));
  85. channel_handle_ctx_t * ctx = (channel_handle_ctx_t *)arg;
  86. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  87. LOG_D("Channel process running........\n");
  88. while(ctx->event_queue.exit == 0)
  89. {
  90. event_queue_item_t *item = dequeue_event(ctx);
  91. if(item==NULL)
  92. {
  93. continue;
  94. }
  95. switch (item->evet.type) {
  96. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  97. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  98. switch (item->evet.id) {
  99. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  100. csi_camera_channel_id_e ch_id = item->evet.type-CSI_CAMERA_EVENT_TYPE_CHANNEL0+CSI_CAMERA_CHANNEL_0;
  101. int read_frame_count = csi_camera_get_frame_count(ctx->cam_handle,
  102. ch_id);
  103. for (int i = 0; i < read_frame_count; i++) {
  104. csi_camera_get_frame(ctx->cam_handle, ch_id, &frame, timeout);
  105. demo_fps.frameNumber++;
  106. ctx->frame_num++;
  107. dump_camera_meta(&frame);
  108. if(test_flag ==0)
  109. {
  110. dump_camera_frame(&frame,ch_id);
  111. }
  112. // csi_camera_frame_unlock(cam_handle, &frame);
  113. csi_camera_put_frame(&frame);
  114. csi_frame_release(&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. switch(argc)
  205. {
  206. case 0:
  207. case 1:
  208. LOG_E("input ERROR:vidoe id is mandatory\n");
  209. usage();
  210. return -1;
  211. case 6:
  212. algo_2 = (argv[5]);
  213. case 5:
  214. algo_1 = (argv[4]);
  215. case 4:
  216. frame_num = atoi(argv[3]);
  217. case 3:
  218. test_flag = atoi(argv[2]);
  219. case 2:
  220. video_id = atoi(argv[1]);
  221. break;
  222. default:
  223. LOG_E("input ERROR:too many param\n");
  224. usage();
  225. return -1;
  226. }
  227. 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);
  228. sprintf(dev_name, "/dev/video%d", video_id);
  229. memset(&demo_fps, 0, sizeof(demo_fps));
  230. struct timeval init_time, cur_time;
  231. memset(&init_time, 0, sizeof(init_time));
  232. memset(&cur_time, 0, sizeof(cur_time));
  233. // 获取设备中,所有的Camera
  234. struct csi_camera_infos camera_infos;
  235. memset(&camera_infos, 0, sizeof(camera_infos));
  236. csi_camera_query_list(&camera_infos);
  237. LOG_O("csi_camera_query_list() OK\n");
  238. // 打印所有设备所支持的Camera
  239. for (i = 0; i < camera_infos.count; i++) {
  240. printf("Camera[%d]: camera_name='%s', device_name='%s', bus_info='%s', capabilities=0x%08x\n",
  241. i, camera_infos.info[i].camera_name, camera_infos.info[i].device_name,
  242. camera_infos.info[i].bus_info, camera_infos.info[i].capabilities);
  243. }
  244. /* Camera[0]: camera_name='RGB_Camera', device_name='/dev/video0', bus_info='CSI-MIPI', capabilities=0x00800001
  245. * Camera[1]: camera_name:'Mono_Camera', device_name:'/dev/video8', bus_info='USB', capabilities=0x00000001
  246. */
  247. bool found_camera = false;
  248. for (i = 0; i < camera_infos.count; i++) {
  249. if (strcmp(camera_infos.info[i].device_name, dev_name) == 0) {
  250. camera_info = camera_infos.info[i];
  251. printf("found device_name:'%s'\n", camera_info.device_name);
  252. found_camera = true;
  253. break;
  254. }
  255. }
  256. if (!found_camera) {
  257. LOG_E("Can't find camera_name:'%s'\n", dev_name);
  258. exit(-1);
  259. }
  260. printf("The caps are:\n");
  261. for (i = 1; i < 0x80000000; i = i << 1) {
  262. switch (camera_info.capabilities & i) {
  263. case CSI_CAMERA_CAP_VIDEO_CAPTURE:
  264. printf("\t Video capture\n");
  265. break;
  266. case CSI_CAMERA_CAP_META_CAPTURE:
  267. printf("\t metadata capture\n");
  268. break;
  269. default:
  270. if (camera_info.capabilities & i) {
  271. printf("\t unknown capabilities(0x%08x)\n", camera_info.capabilities & i);
  272. }
  273. break;
  274. }
  275. }
  276. /* The caps are: Video capture, metadata capture */
  277. // 打开Camera设备获取句柄,作为后续操对象
  278. csi_cam_handle_t cam_handle;
  279. csi_camera_open(&cam_handle, camera_info.device_name);
  280. LOG_O("csi_camera_open() OK\n");
  281. // 获取Camera支持的工作模式
  282. struct csi_camera_modes camera_modes;
  283. camera_modes.count = 0;
  284. csi_camera_get_modes(cam_handle, &camera_modes);
  285. LOG_O("csi_camera_get_modes() OK\n");
  286. // 打印camera所支持的所有工作模式
  287. printf(" Camera:'%s' modes are:{\n", dev_name);
  288. for (i = 0; i < camera_modes.count; i++) {
  289. printf("\t mode_id=%d: description:'%s'\n",
  290. camera_modes.modes[i].mode_id, camera_modes.modes[i].description);
  291. }
  292. printf("}\n");
  293. csi_camera_channel_cfg_s chn_cfg;
  294. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_0;
  295. chn_cfg.frm_cnt = 4;
  296. chn_cfg.img_fmt.width = 1080;
  297. chn_cfg.img_fmt.height = 1280;
  298. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  299. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  300. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  301. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  302. CSI_CAMERA_CHANNEL_CAPTURE_META;
  303. csi_camera_channel_open(cam_handle, &chn_cfg);
  304. LOG_O("CSI_CAMERA_CHANNEL_0: csi_camera_channel_open() OK\n");
  305. // 打开输出channel_1
  306. chn_cfg.chn_id = CSI_CAMERA_CHANNEL_1;
  307. chn_cfg.frm_cnt = 4;
  308. chn_cfg.img_fmt.width = 1080;
  309. chn_cfg.img_fmt.height = 1280;
  310. chn_cfg.img_fmt.pix_fmt = CSI_PIX_FMT_RAW_12BIT;
  311. chn_cfg.img_type = CSI_IMG_TYPE_DMA_BUF;
  312. chn_cfg.meta_fields = CSI_CAMERA_META_DEFAULT_FIELDS;
  313. chn_cfg.capture_type = CSI_CAMERA_CHANNEL_CAPTURE_VIDEO |
  314. CSI_CAMERA_CHANNEL_CAPTURE_META;
  315. csi_camera_channel_open(cam_handle, &chn_cfg);
  316. LOG_O("CSI_CAMERA_CHANNEL_1: csi_camera_channel_open() OK\n");
  317. // 订阅Event
  318. csi_cam_event_handle_t event_handle;
  319. csi_camera_create_event(&event_handle, cam_handle);
  320. struct csi_camera_event_subscription subscribe;
  321. subscribe.type =
  322. CSI_CAMERA_EVENT_TYPE_CAMERA; // 订阅Camera的ERROR事件
  323. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  324. csi_camera_subscribe_event(event_handle, &subscribe);
  325. subscribe.type =
  326. CSI_CAMERA_EVENT_TYPE_CHANNEL0; // 订阅Channel0的FRAME_READY事件
  327. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  328. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  329. csi_camera_subscribe_event(event_handle, &subscribe);
  330. memset(&channel_ctx[0],0x0,sizeof(channel_handle_ctx_t));
  331. channel_ctx[0].cam_handle = cam_handle;
  332. pthread_mutex_init(&channel_ctx[0].event_queue.mutex,NULL);
  333. pthread_cond_init(&channel_ctx[0].event_queue.cond,NULL);
  334. pthread_create(&channel_ctx[0].chan_thread,NULL,camera_channle_process,&channel_ctx[0]);
  335. subscribe.type =
  336. CSI_CAMERA_EVENT_TYPE_CHANNEL1; // 订阅Channel0的FRAME_READY事件
  337. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  338. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;
  339. csi_camera_subscribe_event(event_handle, &subscribe);
  340. memset(&channel_ctx[1],0x0,sizeof(channel_handle_ctx_t));
  341. channel_ctx[1].cam_handle = cam_handle;
  342. pthread_mutex_init(&channel_ctx[1].event_queue.mutex,NULL);
  343. pthread_cond_init(&channel_ctx[1].event_queue.cond,NULL);
  344. pthread_create(&channel_ctx[1].chan_thread,NULL,camera_channle_process,&channel_ctx[1]);
  345. LOG_O("Event subscript OK\n");
  346. cb_context[0].id=0;
  347. cb_context[0].cam_handle = cam_handle;
  348. cb_context[0].dsp_id = 1;
  349. cb_context[0].dsp_path = 3;
  350. char test_set[16]="init";
  351. cb_context[0].buf_size = 1920*1080;
  352. cb_context[0].ref_buf=NULL;
  353. csi_camera_dsp_algo_param_t algo_param_1={
  354. .algo_name = algo_1,
  355. .algo_cb.cb = dsp_algo_result_cb,
  356. .algo_cb.context = &cb_context[0],
  357. .algo_cb.arg_size = sizeof(cb_args_t),
  358. .sett_ptr = test_set,
  359. .sett_size =sizeof(test_set),
  360. .extra_buf_num =1,
  361. .extra_buf_sizes = &cb_context[0].buf_size,
  362. .extra_bufs = &cb_context[0].ref_buf,
  363. };
  364. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[0].dsp_id,cb_context[0].dsp_path, &algo_param_1))
  365. {
  366. LOG_E("set DSP algo fail\n");
  367. }
  368. cb_context[1].id=1;
  369. cb_context[1].cam_handle = cam_handle;
  370. cb_context[1].dsp_id = 1;
  371. cb_context[1].dsp_path = 4;
  372. cb_context[1].buf_size = 1920*1080;
  373. cb_context[1].ref_buf=NULL;
  374. csi_camera_dsp_algo_param_t algo_param_2={
  375. .algo_name = algo_2,
  376. .algo_cb.cb = dsp_algo_result_cb,
  377. .algo_cb.context = &cb_context[1],
  378. .algo_cb.arg_size = sizeof(cb_args_t),
  379. .sett_ptr = test_set,
  380. .sett_size =sizeof(test_set),
  381. .extra_buf_num =1,
  382. .extra_buf_sizes = &cb_context[1].buf_size,
  383. .extra_bufs = &cb_context[1].ref_buf,
  384. };
  385. if(csi_camera_set_dsp_algo_param(cam_handle, cb_context[1].dsp_id,cb_context[1].dsp_path, &algo_param_2))
  386. {
  387. LOG_E("set DSP algo fail\n");
  388. }
  389. csi_camera_floodlight_led_set_flash_bright(cam_handle, 500); //500ma
  390. csi_camera_projection_led_set_flash_bright(cam_handle, 500); //500ma
  391. csi_camera_projection_led_set_mode(cam_handle, LED_IR_ENABLE);
  392. csi_camera_floodlight_led_set_mode(cam_handle, LED_IR_ENABLE);
  393. csi_camera_led_enable(cam_handle, LED_FLOODLIGHT_PROJECTION);
  394. // 开始从channel中取出准备好的frame
  395. csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_0);
  396. csi_camera_channel_start(cam_handle, CSI_CAMERA_CHANNEL_1);
  397. LOG_O("Channel start OK\n");
  398. // 处理订阅的Event
  399. csi_frame_s frame;
  400. event_queue_item_t *event_item;
  401. LOG_O("Starting get frame...\n");
  402. while (running) {
  403. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  404. event_item = malloc(sizeof( event_queue_item_t));
  405. csi_camera_get_event(event_handle, &event_item->evet, timeout);
  406. LOG_O("event.type = %d, event.id = %d\n",event_item->evet.type, event_item->evet.id);
  407. switch (event_item->evet.type) {
  408. case CSI_CAMERA_EVENT_TYPE_CAMERA:
  409. switch (event_item->evet.id) {
  410. case CSI_CAMERA_EVENT_ERROR:
  411. // do sth.
  412. break;
  413. default:
  414. break;
  415. free(event_item);
  416. break;
  417. }
  418. case CSI_CAMERA_EVENT_TYPE_CHANNEL0:
  419. case CSI_CAMERA_EVENT_TYPE_CHANNEL1:
  420. switch (event_item->evet.id) {
  421. case CSI_CAMERA_CHANNEL_EVENT_FRAME_READY: {
  422. csi_camera_channel_id_e ch_id = event_item->evet.type-CSI_CAMERA_EVENT_TYPE_CHANNEL0+CSI_CAMERA_CHANNEL_0;
  423. if(event_item && ch_id<2)
  424. {
  425. enqueue_event(&channel_ctx[ch_id],event_item);
  426. }
  427. break;
  428. }
  429. default:
  430. free(event_item);
  431. break;
  432. }
  433. break;
  434. default:
  435. free(event_item);
  436. break;
  437. }
  438. // 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));
  439. if((frame_num >0) &&
  440. (channel_ctx[0].frame_num >frame_num) &&
  441. (channel_ctx[1].frame_num >frame_num))
  442. {
  443. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_0);
  444. csi_camera_channel_stop(cam_handle, CSI_CAMERA_CHANNEL_1);
  445. channel_ctx[0].event_queue.exit = 1;
  446. channel_ctx[1].event_queue.exit = 1;
  447. pthread_join(channel_ctx[0].chan_thread,NULL);
  448. pthread_join(channel_ctx[1].chan_thread,NULL);
  449. running = false;
  450. }
  451. }
  452. // 取消订阅某一个event, 也可以直接调用csi_camera_destory_event,结束所有的订阅
  453. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL0;
  454. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  455. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  456. csi_camera_unsubscribe_event(event_handle, &subscribe);
  457. subscribe.type = CSI_CAMERA_EVENT_TYPE_CHANNEL1;
  458. subscribe.id = CSI_CAMERA_CHANNEL_EVENT_FRAME_READY |
  459. CSI_CAMERA_CHANNEL_EVENT_OVERFLOW;;
  460. csi_camera_unsubscribe_event(event_handle, &subscribe);
  461. subscribe.type = CSI_CAMERA_EVENT_TYPE_CAMERA;
  462. subscribe.id = CSI_CAMERA_EVENT_WARNING | CSI_CAMERA_EVENT_ERROR;
  463. csi_camera_unsubscribe_event(event_handle, &subscribe);
  464. csi_camera_destory_event(event_handle);
  465. csi_camera_channel_close(cam_handle, CSI_CAMERA_CHANNEL_0);
  466. csi_camera_close(cam_handle);
  467. }
  468. static void dump_camera_meta(csi_frame_s *frame)
  469. {
  470. int i;
  471. if (frame->meta.type != CSI_META_TYPE_CAMERA)
  472. return;
  473. csi_camera_meta_s *meta_data = (csi_camera_meta_s *)frame->meta.data;
  474. csi_camrea_meta_unit_s meta_frame_id,meta_frame_ts;
  475. csi_camera_frame_get_meta_unit(
  476. &meta_frame_id, meta_data, CSI_CAMERA_META_ID_FRAME_ID);
  477. csi_camera_frame_get_meta_unit(
  478. &meta_frame_ts, meta_data, CSI_CAMERA_META_ID_TIMESTAMP);
  479. LOG_O("meta frame id=%d,time stap:(%ld,%ld)\n",
  480. meta_frame_id.int_value,meta_frame_ts.time_value.tv_sec,meta_frame_ts.time_value.tv_usec);
  481. }
  482. static void dump_camera_frame(csi_frame_s *frame, int ch_id)
  483. {
  484. char file[128];
  485. static int file_indx=0;
  486. int size,buf_size;
  487. uint32_t indexd, j;
  488. void *buf[3];
  489. sprintf(file,"demo_save_img_ch_%d_%d", ch_id,file_indx++%6);
  490. int fd = open(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
  491. if(fd == -1) {
  492. LOG_E("%s, %d, open file error!!!!!!!!!!!!!!!\n", __func__, __LINE__);
  493. return ;
  494. }
  495. if (frame->img.type == CSI_IMG_TYPE_DMA_BUF) {
  496. buf_size = frame->img.strides[0]*frame->img.height;
  497. switch(frame->img.pix_format) {
  498. case CSI_PIX_FMT_NV12:
  499. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  500. if(frame->img.num_planes ==2)
  501. {
  502. buf_size+=frame->img.strides[1]*frame->img.height/2;
  503. }
  504. else{
  505. LOG_E("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  506. return;
  507. }
  508. break;
  509. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  510. if(frame->img.num_planes ==2)
  511. {
  512. buf_size+=frame->img.strides[1]*frame->img.height;
  513. }
  514. else{
  515. LOG_E("img fomrat is not match with frame planes:%d\n",frame->img.num_planes);
  516. return;
  517. }
  518. break;
  519. default:
  520. break;
  521. }
  522. buf[0]= mmap(0, buf_size, PROT_READ | PROT_WRITE,
  523. MAP_SHARED, frame->img.dmabuf[0].fds, frame->img.dmabuf[0].offset);
  524. // plane_size[1] = frame->img.strides[1]*frame->img.height;
  525. // printf("frame plane 1 stride:%d,offset:%d\n", frame->img.strides[1],(uint32_t)frame->img.dmabuf[1].offset);
  526. if(frame->img.num_planes ==2)
  527. {
  528. // buf[1] = mmap(0, plane_size[1]/2, PROT_READ | PROT_WRITE,
  529. // MAP_SHARED, frame->img.dmabuf[1].fds, frame->img.dmabuf[1].offset);
  530. buf[1] = buf[0]+frame->img.dmabuf[1].offset;
  531. }
  532. }
  533. else{
  534. buf[0] = frame->img.usr_addr[0];
  535. buf[1] = frame->img.usr_addr[1];
  536. }
  537. 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);
  538. frame->img.pix_format = CSI_PIX_FMT_RAW_12BIT;
  539. switch(frame->img.pix_format) {
  540. case CSI_PIX_FMT_NV12:
  541. case CSI_PIX_FMT_YUV_SEMIPLANAR_420:
  542. if (frame->img.strides[0] == 0) {
  543. frame->img.strides[0] = frame->img.width;
  544. }
  545. for (j = 0; j < frame->img.height; j++) { //Y
  546. indexd = j*frame->img.strides[0];
  547. write(fd, buf[0] + indexd, frame->img.width);
  548. }
  549. for (j = 0; j < frame->img.height / 2; j++) { //UV
  550. indexd = j*frame->img.strides[0];
  551. write(fd, buf[1] + indexd, frame->img.width);
  552. }
  553. break;
  554. case CSI_PIX_FMT_RGB_INTEVLEAVED_888:
  555. case CSI_PIX_FMT_YUV_TEVLEAVED_444:
  556. size = frame->img.width*3;
  557. for (j = 0; j < frame->img.height; j++) {
  558. indexd = j*frame->img.strides[0];
  559. write(fd, buf[0] + indexd, size);
  560. }
  561. break;
  562. case CSI_PIX_FMT_BGR:
  563. case CSI_PIX_FMT_RGB_PLANAR_888:
  564. case CSI_PIX_FMT_YUV_PLANAR_444:
  565. size = frame->img.width * frame->img.height * 3;
  566. write(fd, buf[0], size);
  567. break;
  568. case CSI_PIX_FMT_RAW_8BIT:
  569. size = frame->img.width;
  570. for (j = 0; j < frame->img.height; j++) {
  571. indexd = j*frame->img.strides[0];
  572. write(fd, buf[0] + indexd, size);
  573. }
  574. break;
  575. case CSI_PIX_FMT_YUV_TEVLEAVED_422:
  576. case CSI_PIX_FMT_RAW_10BIT:
  577. case CSI_PIX_FMT_RAW_12BIT:
  578. case CSI_PIX_FMT_RAW_14BIT:
  579. case CSI_PIX_FMT_RAW_16BIT:
  580. size = frame->img.width*2;
  581. for (j = 0; j < frame->img.height; j++) {
  582. indexd = j*frame->img.strides[0];
  583. write(fd, buf[0] + indexd, size);
  584. }
  585. break;
  586. case CSI_PIX_FMT_YUV_SEMIPLANAR_422:
  587. if (frame->img.strides[0] == 0) {
  588. frame->img.strides[0] = frame->img.width;
  589. }
  590. for (j = 0; j < frame->img.height; j++) { //Y
  591. indexd = j*frame->img.strides[0];
  592. write(fd,buf[0] + indexd, frame->img.width);
  593. }
  594. for (j = 0; j < frame->img.height; j++) { //UV
  595. indexd = j*frame->img.strides[0];
  596. write(fd, buf[1]+ indexd, frame->img.width);
  597. }
  598. break;
  599. default:
  600. LOG_E("%s unsupported format to save\n", __func__);
  601. exit(-1);
  602. break;
  603. }
  604. close(fd);
  605. munmap(buf[0],buf_size);
  606. // munmap(buf[1],plane_size[1]);
  607. LOG_O("%s exit\n", __func__);
  608. }