1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/ioctl.h>
7 #include <sys/mman.h>
8 #include <linux/videodev2.h>
9 #include <linux/v4l2-subdev.h>
10 #include <string.h>
11 #include "rk_aiq_types.h"
12 #include "rk_aiq_offline_raw.h"
13 #include "rk_vi_user_api2_stream_cfg.h"
14 #include "uAPI2/rk_aiq_user_api2_sysctl.h"
15 #include "rk_aiq_offline_raw.h"
16 #include "rkcif-config.h"
17 //#include <iostream>
18 #include <getopt.h>
19
20 #define USE_LIBAIQ
21
22 #define SENSOR_ENTNAME "m01_b_ov50c40 4-0036"
23
24 #define VIDEO_DEVNAME "/dev/video24"
25 #define VIDEO_WIDTH 4096
26 #define VIDEO_HEIGHT 3072
27 #define VIDEO_WORKMODE RK_AIQ_WORKING_MODE_NORMAL
28
29 #define VIDEO_WIDTH_2 3840
30 #define VIDEO_HEIGHT_2 2160
31 #define VIDEO_WORKMODE_2 RK_AIQ_WORKING_MODE_NORMAL
32
33 #define RAWCAP_SAVE_NUM 30
34 #define STREAM_SAVE_NUM 30
35
36 struct rk_aiq_vbuf_info {
37 uint32_t frame_id;
38 uint32_t timestamp;
39 float exp_time;
40 float exp_gain;
41 uint32_t exp_time_reg;
42 uint32_t exp_gain_reg;
43 uint32_t data_fd;
44 uint8_t *data_addr;
45 uint32_t data_length;
46 bool valid;
47 };
48
49 struct rk_aiq_vbuf {
50 void *base_addr;
51 uint32_t frame_width;
52 uint32_t frame_height;
53 struct rk_aiq_vbuf_info buf_info[3];/*index: 0-short,1-medium,2-long*/
54 };
55
56
57 static rk_aiq_rawbuf_type_t _rawbuf_type;
58 static struct _st_addrinfo _st_addr[3];
59 static struct _raw_format _rawfmt;
60 static rk_aiq_frame_info_t _finfo;
61
62 static rkraw_vi_ctx_t *g_vi_ctx;
63
64 static int g_quit = 0;
65 static int g_aiq_quit = 0;
66 static int g_aiq_pause = 0;
67 static int g_aiq_test_mode = 0;
68
69 static int saved_num = 0;
70 static char g_sns_entity_name[64] = {0};
71 static char g_sns_name[64] = {0};
72
73 static bool use_isp_driver = false;
74 static int raw_width = 3840;
75 static int raw_height = 2160;
76 static int video_width = 640;
77 static int video_height = 480;
78 static int modul_select = 0;
79 static int hdr_mode = 0;
80 static const char *isp_vir;
81 static const char *real_sns;
82 static const char *iq_file_name;
83 static const char *video_name;
84 static uint32_t pix_fmt = V4L2_PIX_FMT_SBGGR10;
85
86 static FILE* g_rkraw_fp;
87 static uint8_t* g_rkraw_buffer;
88 static uint32_t g_rkraw_size;
89 static int g_rkraw_index;
90
91 static struct timeval start_t, cur_t, prev_t;
92
93 static rk_aiq_sys_ctx_t* g_aiq_ctx;
94
95 // enum v4l2_memory _tx_memory_type;
96 // enum v4l2_memory _rx_memory_type;
97 // SmartPtr<V4l2Device> g_vi_ctx->_mRawProcUnit->_mipi_rx_devs[3];
98 // SmartPtr<V4l2Device> _mipi_tx_devs[3];
99
capture_raw(uint8_t * rkraw_data,uint32_t rkraw_len)100 static int capture_raw(uint8_t *rkraw_data, uint32_t rkraw_len)
101 {
102 rkrawstream_rkraw2_t rkraw;
103 struct _block_header _header;
104 _header.block_id = NORMAL_RAW_TAG;
105 uint16_t end_tag = END_TAG;
106
107 rkrawstream_uapi_parase_rkraw2(rkraw_data, &rkraw);
108
109 char filname[32];
110 sprintf(filname, "/tmp/raw%d", saved_num);
111 FILE *fp = fopen(filname,"wb");
112 if(fp){
113 int ret = 0;
114 ret += fwrite((void *)rkraw.plane[0].addr, 1, rkraw.plane[0].size, fp);
115 printf("%s:save raw %s, ret %d\n", __func__, filname, ret);
116 fclose(fp);
117 }
118
119 return 0;
120 }
121
on_frame_ready_capture(uint8_t * rkraw_data,uint32_t rkraw_len)122 static int on_frame_ready_capture(uint8_t *rkraw_data, uint32_t rkraw_len)
123 {
124 if(g_quit == 0){
125
126 capture_raw(rkraw_data, rkraw_len);
127 saved_num++;
128 if(saved_num == RAWCAP_SAVE_NUM)
129 g_quit = 1;
130 }
131 return 0;
132 }
133
on_frame_ready_streaming(uint8_t * rkraw_data,uint32_t rkraw_len)134 static int on_frame_ready_streaming(uint8_t *rkraw_data, uint32_t rkraw_len)
135 {
136 printf("on_frame_ready_streaming11111\n");
137 //capture_raw(rkraw_data, rkraw_len);
138
139 rkrawstream_rkraw2_t rkraw;
140 rkrawstream_uapi_parase_rkraw2(rkraw_data, &rkraw);
141 rkrawstream_vicap_buf_take(g_vi_ctx);
142 rkrawstream_readback_set_rkraw2(g_vi_ctx, &rkraw);
143
144 saved_num++;
145 if(saved_num == STREAM_SAVE_NUM)
146 g_quit = 1;
147 return 0;
148 }
149
on_isp_process_done_streaming(int dev_index)150 static void on_isp_process_done_streaming(int dev_index)
151 {
152 printf("on_isp_process_done_streaming\n");
153 rkrawstream_vicap_buf_return(g_vi_ctx, dev_index);
154 }
155
on_isp_process_done_offline(int dev_index)156 static void on_isp_process_done_offline(int dev_index)
157 {
158 printf("%s:index %d\n", __func__, dev_index);
159 }
160
print_sensor_info(rk_aiq_static_info_t * s_info)161 void print_sensor_info(rk_aiq_static_info_t *s_info)
162 {
163 int i = 0;
164 printf("\n\n****** sensor %s infos: *********\n",s_info->sensor_info.sensor_name);
165 for(i=0; i<10; i++){
166 if(s_info->sensor_info.support_fmt[i].width >0)
167 printf("format%d: %dx%d, format 0x%x, fps %d hdr %d\n",i,
168 s_info->sensor_info.support_fmt[i].width,
169 s_info->sensor_info.support_fmt[i].height,
170 s_info->sensor_info.support_fmt[i].format,
171 s_info->sensor_info.support_fmt[i].fps,
172 s_info->sensor_info.support_fmt[i].hdr_mode);
173 }
174 }
175
176 #include "rkvi_demo_isp.c_part"
177
rawcap_test(void)178 int rawcap_test(void)
179 {
180 strcpy(g_sns_entity_name, SENSOR_ENTNAME);
181 printf("g_sns_entity_name:%s\n", g_sns_entity_name);
182 sscanf(&g_sns_entity_name[6], "%s", g_sns_name);
183 printf("sns_name:%s\n", g_sns_name);
184
185 rkraw_vi_init_params_t init_p;
186 rkraw_vi_prepare_params_t prepare_p;
187
188 /* init rkraw_vi user api */
189 g_vi_ctx = rkrawstream_uapi_init();
190 init_p.sns_ent_name = g_sns_entity_name;
191 init_p.use_offline = false;
192 rkrawstream_vicap_init(g_vi_ctx, &init_p);
193
194 prepare_p.width = VIDEO_WIDTH;
195 prepare_p.height = VIDEO_HEIGHT;
196 prepare_p.pix_fmt = V4L2_PIX_FMT_SGBRG10;
197 prepare_p.hdr_mode = VIDEO_WORKMODE;
198 prepare_p.mem_mode = CSI_LVDS_MEM_WORD_LOW_ALIGN;
199 prepare_p.buf_memory_type = 0;
200 rkrawstream_vicap_prepare(g_vi_ctx, &prepare_p);
201
202 rkrawstream_vicap_start(g_vi_ctx, on_frame_ready_capture);
203
204 while(g_quit == 0)
205 usleep(10000);
206
207 g_aiq_quit = 1;
208 rkrawstream_vicap_stop(g_vi_ctx);
209 /* clean up library. */
210 rkrawstream_uapi_deinit(g_vi_ctx);
211 return 0;
212 }
213
214
stream_test(void)215 int stream_test(void)
216 {
217 pthread_t isp_tid;
218 strcpy(g_sns_entity_name, SENSOR_ENTNAME);
219 printf("g_sns_entity_name:%s\n", g_sns_entity_name);
220 sscanf(&g_sns_entity_name[6], "%s", g_sns_name);
221 printf("sns_name:%s\n", g_sns_name);
222
223 rkraw_vi_init_params_t init_p;
224 rkraw_vi_prepare_params_t prepare_p;
225 int param[2];
226
227 g_ispfd = open(VIDEO_DEVNAME, O_RDWR /* required */ /*| O_NONBLOCK*/, 0);
228 if (-1 == g_ispfd) {
229 printf("Cannot open '%s'\n", VIDEO_DEVNAME);
230 return 0;
231 }
232
233 /* init rkraw_vi user api */
234 g_vi_ctx = rkrawstream_uapi_init();
235 init_p.sns_ent_name = g_sns_entity_name;
236 init_p.use_offline = false;
237 rkrawstream_vicap_init(g_vi_ctx, &init_p);
238 rkrawstream_readback_init(g_vi_ctx, &init_p);
239
240 prepare_p.width = VIDEO_WIDTH;
241 prepare_p.height = VIDEO_HEIGHT;
242 prepare_p.pix_fmt = V4L2_PIX_FMT_SGBRG10;
243 prepare_p.hdr_mode = VIDEO_WORKMODE;
244 prepare_p.mem_mode = 0;
245 prepare_p.buf_memory_type = 0;
246 rkrawstream_vicap_prepare(g_vi_ctx, &prepare_p);
247
248 prepare_p.width = 4096;
249 prepare_p.height = 3072;
250 prepare_p.buf_memory_type = V4L2_MEMORY_DMABUF;
251 rkrawstream_readback_prepare(g_vi_ctx, &prepare_p);
252
253 //g_aiq_ctx = rk_aiq_uapi2_sysctl_init(g_sns_entity_name, "/etc/iqfiles", NULL, NULL);
254
255 g_aiq_pause = 1;
256 g_aiq_quit = 0;
257 param[0] = 4096;
258 param[1] = 3072;
259 pthread_create(&isp_tid, NULL, isp_thread, param);
260
261
262 //rk_aiq_uapi2_sysctl_prepare(g_aiq_ctx, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WORKMODE);
263 rkrawstream_setup_pipline_fmt(g_vi_ctx, 4096, 3072);
264
265 //rk_aiq_uapi2_sysctl_start(g_aiq_ctx);
266
267 printf("rkisp_init_device %d %d\n", param[0], param[1]);
268 rkisp_init_device(param[0], param[1]);
269 rkisp_start_capturing();
270 g_aiq_pause = 0;
271 printf("sleeping...\n");
272 sleep(3);
273
274 rkrawstream_vicap_start(g_vi_ctx, on_frame_ready_streaming);
275 rkrawstream_readback_start(g_vi_ctx, on_isp_process_done_streaming);
276
277
278 while(g_quit == 0)
279 usleep(10000);
280
281 g_aiq_quit = 1;
282 rkrawstream_vicap_stop(g_vi_ctx);
283 rkrawstream_readback_stop(g_vi_ctx);
284 //rk_aiq_uapi2_sysctl_stop(g_aiq_ctx, false);
285 /* clean up library. */
286 rkrawstream_uapi_deinit(g_vi_ctx);
287
288 pthread_join(isp_tid, NULL);
289
290 rkisp_stop_capturing();
291 rkisp_uninit_device();
292 //rk_aiq_uapi2_sysctl_deinit(g_aiq_ctx);
293
294 close(g_ispfd);
295 return 0;
296 }
297
release_buffer(void * addr)298 void release_buffer(void *addr) {
299 printf("release buffer called: addr=%p\n", addr);
300 }
301
print_help()302 void print_help()
303 {
304 printf("\n\n****** librkraw_vi demo by ydb *********\n\n");
305 printf("--rawcap capture rkraw file in /tmp.\n");
306 printf("--stream live stream to isp.\n");
307 printf("--offaiq run aiq and isp offline.\n");
308 printf("you must choose one from rawcap, stream and offaiq, and you just can choose one of them\n");
309 printf("\nif you choose --offaiq, there are some params you can config\n");
310 printf("--width video width, default value 640\n");
311 printf("--height video height, default value 480\n");
312 printf("--rawfmt raw width and height, config it such as 640x480, use 'x', don't use 'X'\n");
313 printf("--iqfile enter iqfile name, iq file can be found at /etc/iqfiles/\n");
314 printf("--ispdriver isp driver module name, such as rkisp0-vir0\n");
315 printf("--video mainpath video on the isp driver you choosed, such as /dev/video8\n");
316 printf("--sensor what sensor name of raw, such as imx415\n");
317 printf("--pix raw pix fmt, such as GB10(SGBRG10), default value BG10(SBGGR10)\n");
318 printf(" all pix fmt BG10, GB10, BA10, RG10, BG12, GB12, RG12, BA12, BG14, GB14, RG14, BA14\n");
319 printf("--har optional, hdr mode, val 2 means hdrx2, 3 means hdrx3, default value 0(normal mode)");
320 printf("--help print help info\n");
321 }
322
parse_args(int argc,char ** argv)323 static void parse_args(int argc, char **argv)
324 {
325 int c;
326 int digit_optind = 0;
327 optind = 0;
328 while (1) {
329 int this_option_optind = optind ? optind : 1;
330 int option_index = 0;
331 static struct option long_options[] = {
332 {"width", required_argument, 0, 'w' },
333 {"height", required_argument, 0, 'h' },
334 {"offaiq", no_argument, 0, 'f' },
335 {"video", required_argument, 0, 'v' },
336 {"stream", no_argument, 0, 'o' },
337 {"rawcap", no_argument, 0, 'r' },
338 {"help", no_argument, 0, 'p' },
339 {"iqfile", required_argument, 0, 'i' },
340 {"sensor", required_argument, 0, 's' },
341 {"ispdriver", required_argument, 0, 'd' },
342 {"hdr", required_argument, 0, 'a'},
343 {"pix", required_argument, 0, 'x'},
344 {"rawfmt", required_argument, 0, 't'},
345 {0, 0, 0, 0 }
346 };
347
348 //c = getopt_long(argc, argv, "w:h:f:i:d:o:c:ps",
349 c = getopt_long(argc, argv, "w:h:v:d:i:s:t:orfp",
350 long_options, &option_index);
351 if (c == -1)
352 break;
353
354 switch (c)
355 {
356 case 'w':
357 video_width = atoi(optarg);
358 break;
359 case 'h':
360 video_height = atoi(optarg);
361 break;
362 case 'f':
363 {
364 use_isp_driver = true;
365 modul_select = 1;
366 }
367 break;
368 case 'v':
369 video_name = optarg;
370 break;
371 case 'd':
372 isp_vir = optarg;
373 break;
374 case 'i':
375 iq_file_name = optarg;
376 break;
377 case 's':
378 real_sns = optarg;
379 break;
380 case 'o':
381 modul_select = 2;
382 break;
383 case 'r':
384 modul_select = 3;
385 break;
386 case 'p':
387 print_help();
388 break;
389 case 'a':
390 {
391 int mode_val = atoi(optarg);
392 if (mode_val == 2) {
393 hdr_mode = RK_AIQ_WORKING_MODE_ISP_HDR2;
394 }else if (mode_val == 3) {
395 hdr_mode = RK_AIQ_WORKING_MODE_ISP_HDR3;
396 } else {
397 hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
398 }
399
400 }
401 break;
402 case 'x':
403 {
404 if (strcmp(optarg, "BG10") == 0)
405 pix_fmt = V4L2_PIX_FMT_SBGGR10;
406 else if (strcmp(optarg, "GB10") == 0)
407 pix_fmt = V4L2_PIX_FMT_SGBRG10;
408 else if (strcmp(optarg, "RG10") == 0)
409 pix_fmt = V4L2_PIX_FMT_SRGGB10;
410 else if (strcmp(optarg, "BA10") == 0)
411 pix_fmt = V4L2_PIX_FMT_SGRBG10;
412 else if (strcmp(optarg, "BG12") == 0)
413 pix_fmt = V4L2_PIX_FMT_SBGGR12;
414 else if (strcmp(optarg, "GB12") == 0)
415 pix_fmt = V4L2_PIX_FMT_SGBRG12;
416 else if (strcmp(optarg, "RG12") == 0)
417 pix_fmt = V4L2_PIX_FMT_SRGGB12;
418 else if (strcmp(optarg, "BA12") == 0)
419 pix_fmt = V4L2_PIX_FMT_SGRBG12;
420 else if (strcmp(optarg, "BG14") == 0)
421 pix_fmt = V4L2_PIX_FMT_SBGGR14;
422 else if (strcmp(optarg, "GB14") == 0)
423 pix_fmt = V4L2_PIX_FMT_SGBRG14;
424 else if (strcmp(optarg, "RG14") == 0)
425 pix_fmt = V4L2_PIX_FMT_SRGGB14;
426 else if (strcmp(optarg, "BA14") == 0)
427 pix_fmt = V4L2_PIX_FMT_SGRBG14;
428 else
429 pix_fmt = V4L2_PIX_FMT_SBGGR10;
430 }
431 break;
432 case 't':
433 {
434 raw_width = atoi(optarg);
435 char *raw_fmt = strstr(optarg, "x");
436 raw_height = atoi(raw_fmt + 1);
437 }
438 default:
439 break;
440 }
441 }
442 }
443
main(int argc,char ** argv)444 int main(int argc, char **argv)
445 {
446 bool use_aiq = false;
447 bool use_rawcap = false;
448 bool use_rawstream = false;
449 bool test_switch = false;
450 bool use_offline = false;
451 pthread_t isp_tid;
452 g_aiq_test_mode = 0;
453
454 //rkraw_vi_sensor_info_t m_info = {0};
455 rk_aiq_static_info_t s_info;
456
457 parse_args(argc, argv);
458 if (!modul_select)
459 return 0;
460 switch (modul_select)
461 {
462 case 2:
463 stream_test();
464 break;
465 case 3:
466 rawcap_test();
467 break;
468 default:
469 break;
470 }
471 return 0;
472 }
473
474