/drivers/media/video/sun4i_csi/test/app_test_ok.c
C | 1215 lines | 887 code | 154 blank | 174 comment | 86 complexity | 1aab98cbc9844df902ea157932fd2b95 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
1/* 2 * drivers/media/video/sun4i_csi/test/app_test_ok.c 3 * 4 * (C) Copyright 2007-2012 5 * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 20 * MA 02111-1307 USA 21 */ 22 23 24//#????? 25 26//#Rockie Cheng 27 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <assert.h> 32 33#include <getopt.h> 34 35#include <fcntl.h> 36#include <unistd.h> 37#include <errno.h> 38#include <malloc.h> 39#include <sys/stat.h> 40#include <sys/types.h> 41#include <sys/time.h> 42#include <sys/mman.h> 43#include <sys/ioctl.h> 44 45#include <asm/types.h> 46#include <linux/videodev2.h> 47#include <time.h> 48#include <linux/fb.h> 49//#include "../../../../video/sunxi/drv_display.h"//modify this 50#include "./../../../../video/sunxi/drv_display.h"//modify this 51 52//#define READ_NUM 5000 53#define DISPLAY 54#define LCD_WIDTH 800 55#define LCD_HEIGHT 480 56 57#define CLEAR(x) memset (&(x), 0, sizeof (x)) 58 59int count; 60 61struct buffer { 62 void * start; 63 size_t length; 64}; 65 66struct size{ 67 int width; 68 int height; 69}; 70 71static char * dev_name = "/dev/video0";//?????? 72static int fd = -1; 73struct buffer * buffers = NULL; 74static unsigned int n_buffers = 0; 75 76FILE *file_fd; 77static unsigned long file_length; 78static unsigned char *file_name; 79 80int disphd; 81unsigned int hlay; 82int sel = 0;//which screen 0/1 83__disp_layer_info_t layer_para; 84__disp_video_fb_t video_fb; 85__u32 arg[4]; 86 87//struct timeval time_test; 88//struct timezone tz; 89 90struct size input_size; 91struct size disp_size; 92int csi_format; 93__disp_pixel_fmt_t disp_format; 94__disp_pixel_mod_t disp_mode; 95__disp_pixel_seq_t disp_seq; 96int read_num=100; 97int test_num=10; 98int req_frame_num; 99int fps=30; 100int fps_test=0; 101int invalid_ops=0; 102int invalid_fmt_test=0; 103int control_test=0; 104int ioctl_test=0; 105int lost_frame_test=0; 106struct test_case{ 107 int input_width; 108 int input_height; 109 int disp_width; 110 int disp_height; 111 int csi_format; 112 __disp_pixel_fmt_t disp_format; 113 __disp_pixel_mod_t disp_mode; 114 __disp_pixel_seq_t disp_seq; 115}; 116 117struct test_case test_case_set[]={ 118 { 119 .input_width = 640, 120 .input_height = 480, 121 .csi_format = V4L2_PIX_FMT_YUV422P, 122 .disp_format = DISP_FORMAT_YUV422, 123 .disp_mode = DISP_MOD_NON_MB_PLANAR, 124 .disp_seq = DISP_SEQ_UVUV, 125 }, 126 { 127 .input_width = 640, 128 .input_height = 480, 129 .csi_format = V4L2_PIX_FMT_YUV420, 130 .disp_format = DISP_FORMAT_YUV420, 131 .disp_mode = DISP_MOD_NON_MB_PLANAR, 132 .disp_seq = DISP_SEQ_UVUV, 133 }, 134 { 135 .input_width = 320, 136 .input_height = 240, 137 .csi_format = V4L2_PIX_FMT_YUV422P, 138 .disp_format = DISP_FORMAT_YUV422, 139 .disp_mode = DISP_MOD_NON_MB_PLANAR, 140 .disp_seq = DISP_SEQ_UVUV, 141 }, 142 { 143 .input_width = 328, 144 .input_height = 240, 145 .csi_format = V4L2_PIX_FMT_YUV422P, 146 .disp_format = DISP_FORMAT_YUV422, 147 .disp_mode = DISP_MOD_NON_MB_PLANAR, 148 .disp_seq = DISP_SEQ_UVUV, 149 }, 150 { 151 .input_width = 320, 152 .input_height = 248, 153 .csi_format = V4L2_PIX_FMT_YUV422P, 154 .disp_format = DISP_FORMAT_YUV422, 155 .disp_mode = DISP_MOD_NON_MB_PLANAR, 156 .disp_seq = DISP_SEQ_UVUV, 157 }, 158 { 159 .input_width = 312, 160 .input_height = 240, 161 .csi_format = V4L2_PIX_FMT_YUV422P, 162 .disp_format = DISP_FORMAT_YUV422, 163 .disp_mode = DISP_MOD_NON_MB_PLANAR, 164 .disp_seq = DISP_SEQ_UVUV, 165 }, 166 { 167 .input_width = 320, 168 .input_height = 232, 169 .csi_format = V4L2_PIX_FMT_YUV422P, 170 .disp_format = DISP_FORMAT_YUV422, 171 .disp_mode = DISP_MOD_NON_MB_PLANAR, 172 .disp_seq = DISP_SEQ_UVUV, 173 }, 174 { 175 .input_width = 640, 176 .input_height = 480, 177 .csi_format = V4L2_PIX_FMT_YUV422P, 178 .disp_format = DISP_FORMAT_YUV422, 179 .disp_mode = DISP_MOD_NON_MB_PLANAR, 180 .disp_seq = DISP_SEQ_UVUV, 181 }, 182 { 183 .input_width = 648, 184 .input_height = 480, 185 .csi_format = V4L2_PIX_FMT_YUV422P, 186 .disp_format = DISP_FORMAT_YUV422, 187 .disp_mode = DISP_MOD_NON_MB_PLANAR, 188 .disp_seq = DISP_SEQ_UVUV, 189 }, 190 { 191 .input_width = 640, 192 .input_height = 488, 193 .csi_format = V4L2_PIX_FMT_YUV422P, 194 .disp_format = DISP_FORMAT_YUV422, 195 .disp_mode = DISP_MOD_NON_MB_PLANAR, 196 .disp_seq = DISP_SEQ_UVUV, 197 }, 198 { 199 .input_width = 632, 200 .input_height = 480, 201 .csi_format = V4L2_PIX_FMT_YUV422P, 202 .disp_format = DISP_FORMAT_YUV422, 203 .disp_mode = DISP_MOD_NON_MB_PLANAR, 204 .disp_seq = DISP_SEQ_UVUV, 205 }, 206 { 207 .input_width = 640, 208 .input_height = 472, 209 .csi_format = V4L2_PIX_FMT_YUV422P, 210 .disp_format = DISP_FORMAT_YUV422, 211 .disp_mode = DISP_MOD_NON_MB_PLANAR, 212 .disp_seq = DISP_SEQ_UVUV, 213 }, 214 { 215 .input_width = 320, 216 .input_height = 240, 217 .csi_format = V4L2_PIX_FMT_YUV420, 218 .disp_format = DISP_FORMAT_YUV420, 219 .disp_mode = DISP_MOD_NON_MB_PLANAR, 220 .disp_seq = DISP_SEQ_UVUV, 221 }, 222 { 223 .input_width = 320, 224 .input_height = 240, 225 .csi_format = V4L2_PIX_FMT_NV16, 226 .disp_format = DISP_FORMAT_YUV422, 227 .disp_mode = DISP_MOD_NON_MB_UV_COMBINED, 228 .disp_seq = DISP_SEQ_UVUV, 229 }, 230 { 231 .input_width = 320, 232 .input_height = 240, 233 .csi_format = V4L2_PIX_FMT_NV12, 234 .disp_format = DISP_FORMAT_YUV420, 235 .disp_mode = DISP_MOD_NON_MB_UV_COMBINED, 236 .disp_seq = DISP_SEQ_UVUV, 237 }, 238 { 239 .input_width = 640, 240 .input_height = 480, 241 .csi_format = V4L2_PIX_FMT_YUV420, 242 .disp_format = DISP_FORMAT_YUV420, 243 .disp_mode = DISP_MOD_NON_MB_PLANAR, 244 .disp_seq = DISP_SEQ_UVUV, 245 }, 246 { 247 .input_width = 640, 248 .input_height = 480, 249 .csi_format = V4L2_PIX_FMT_NV16, 250 .disp_format = DISP_FORMAT_YUV422, 251 .disp_mode = DISP_MOD_NON_MB_UV_COMBINED, 252 .disp_seq = DISP_SEQ_UVUV, 253 }, 254 { 255 .input_width = 640, 256 .input_height = 480, 257 .csi_format = V4L2_PIX_FMT_NV12, 258 .disp_format = DISP_FORMAT_YUV420, 259 .disp_mode = DISP_MOD_NON_MB_UV_COMBINED, 260 .disp_seq = DISP_SEQ_UVUV, 261 }, 262 { 263 .input_width = 320, 264 .input_height = 240, 265 .csi_format = V4L2_PIX_FMT_YUYV, 266 .disp_format = DISP_FORMAT_YUV422, 267 .disp_mode = DISP_MOD_INTERLEAVED, 268 .disp_seq = DISP_SEQ_YUYV, 269 }, 270 { 271 .input_width = 328, 272 .input_height = 240, 273 .csi_format = V4L2_PIX_FMT_YUYV, 274 .disp_format = DISP_FORMAT_YUV422, 275 .disp_mode = DISP_MOD_INTERLEAVED, 276 .disp_seq = DISP_SEQ_YUYV, 277 }, 278 { 279 .input_width = 320, 280 .input_height = 248, 281 .csi_format = V4L2_PIX_FMT_YUYV, 282 .disp_format = DISP_FORMAT_YUV422, 283 .disp_mode = DISP_MOD_INTERLEAVED, 284 .disp_seq = DISP_SEQ_YUYV, 285 }, 286 { 287 .input_width = 312, 288 .input_height = 240, 289 .csi_format = V4L2_PIX_FMT_YUYV, 290 .disp_format = DISP_FORMAT_YUV422, 291 .disp_mode = DISP_MOD_INTERLEAVED, 292 .disp_seq = DISP_SEQ_YUYV, 293 }, 294 { 295 .input_width = 320, 296 .input_height = 232, 297 .csi_format = V4L2_PIX_FMT_YUYV, 298 .disp_format = DISP_FORMAT_YUV422, 299 .disp_mode = DISP_MOD_INTERLEAVED, 300 .disp_seq = DISP_SEQ_YUYV, 301 }, 302 { 303 .input_width = 640, 304 .input_height = 480, 305 .csi_format = V4L2_PIX_FMT_YUYV, 306 .disp_format = DISP_FORMAT_YUV422, 307 .disp_mode = DISP_MOD_INTERLEAVED, 308 .disp_seq = DISP_SEQ_YUYV, 309 }, 310 { 311 .input_width = 648, 312 .input_height = 480, 313 .csi_format = V4L2_PIX_FMT_YUYV, 314 .disp_format = DISP_FORMAT_YUV422, 315 .disp_mode = DISP_MOD_INTERLEAVED, 316 .disp_seq = DISP_SEQ_YUYV, 317 }, 318 { 319 .input_width = 640, 320 .input_height = 488, 321 .csi_format = V4L2_PIX_FMT_YUYV, 322 .disp_format = DISP_FORMAT_YUV422, 323 .disp_mode = DISP_MOD_INTERLEAVED, 324 .disp_seq = DISP_SEQ_YUYV, 325 }, 326 { 327 .input_width = 632, 328 .input_height = 480, 329 .csi_format = V4L2_PIX_FMT_YUYV, 330 .disp_format = DISP_FORMAT_YUV422, 331 .disp_mode = DISP_MOD_INTERLEAVED, 332 .disp_seq = DISP_SEQ_YUYV, 333 }, 334 { 335 .input_width = 640, 336 .input_height = 472, 337 .csi_format = V4L2_PIX_FMT_YUYV, 338 .disp_format = DISP_FORMAT_YUV422, 339 .disp_mode = DISP_MOD_INTERLEAVED, 340 .disp_seq = DISP_SEQ_YUYV, 341 }, 342 { 343 .input_width = 640, 344 .input_height = 480, 345 .csi_format = V4L2_PIX_FMT_YVYU, 346 .disp_format = DISP_FORMAT_YUV422, 347 .disp_mode = DISP_MOD_INTERLEAVED, 348 .disp_seq = DISP_SEQ_YVYU, 349 }, 350 { 351 .input_width = 640, 352 .input_height = 480, 353 .csi_format = V4L2_PIX_FMT_UYVY, 354 .disp_format = DISP_FORMAT_YUV422, 355 .disp_mode = DISP_MOD_INTERLEAVED, 356 .disp_seq = DISP_SEQ_UYVY, 357 }, 358 { 359 .input_width = 640, 360 .input_height = 480, 361 .csi_format = V4L2_PIX_FMT_VYUY, 362 .disp_format = DISP_FORMAT_YUV422, 363 .disp_mode = DISP_MOD_INTERLEAVED, 364 .disp_seq = DISP_SEQ_VYUY, 365 }, 366}; 367 368enum v4l2_ctrl_type qc_ctrl[]= 369{ 370 V4L2_CID_BRIGHTNESS, 371 V4L2_CID_CONTRAST, 372 V4L2_CID_SATURATION, 373 V4L2_CID_HUE, 374 V4L2_CID_VFLIP, 375 V4L2_CID_HFLIP, 376 V4L2_CID_GAIN, 377 V4L2_CID_AUTOGAIN, 378 V4L2_CID_EXPOSURE, 379 V4L2_CID_EXPOSURE_AUTO, 380 V4L2_CID_DO_WHITE_BALANCE, 381 V4L2_CID_AUTO_WHITE_BALANCE, 382 (V4L2_CID_BASE+31) 383}; 384 385////////////////////////////////////////////////////// 386//?????? 387////////////////////////////////////////////////////// 388static int read_frame (void) 389{ 390 struct v4l2_buffer buf; 391 unsigned int i; 392 393 394 CLEAR (buf); 395 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 396 buf.memory = V4L2_MEMORY_MMAP; 397 398 ioctl (fd, VIDIOC_DQBUF, &buf); //???????? 399 400 401 if((fps_test==1)||(lost_frame_test==1)) 402 printf("process image %d sec %d usec\n",buf.timestamp.tv_sec,buf.timestamp.tv_usec); 403 404 405 assert (buf.index < n_buffers); 406// printf ("buf.index dq is %d,\n",buf.index); 407// printf ("buf.m.offset = 0x%x\n",buf.m.offset); 408 //disp_set_addr(320,240,&buf.m.offset); 409 disp_set_addr(disp_size.width, disp_size.height,&buf.m.offset); 410 411 //printf ("press ENTER to continue!\n"); 412 //getchar(); 413 //fwrite(buffers[buf.index].start, buffers[buf.index].length, 1, file_fd); //??????? 414 415 416 417 if(lost_frame_test==1) 418 { 419 if(count%31==0) 420 { 421 printf("count = %d,delay\n",count); 422 for(i=0;i<0x1ffffff;i++) 423 { 424 425 426 } 427 } 428 } 429 430 ioctl (fd, VIDIOC_QBUF, &buf); //????? 431 432 return 1; 433} 434 435int disp_int(int w,int h) 436{ 437 /*display start*/ 438 //unsigned int h,w; 439 __u32 id = 0; 440 441 //h= 480; 442 //w= 640; 443 444 if((disphd = open("/dev/disp",O_RDWR)) == -1) 445 { 446 printf("open file /dev/disp fail. \n"); 447 return 0; 448 } 449 450 arg[0] = 0; 451 ioctl(disphd, DISP_CMD_LCD_ON, (void*)arg); 452 453 //layer0 454 arg[0] = 0; 455 arg[1] = DISP_LAYER_WORK_MODE_SCALER; 456 hlay = ioctl(disphd, DISP_CMD_LAYER_REQUEST, (void*)arg); 457 if(hlay == 0) 458 { 459 printf("request layer0 fail\n"); 460 return 0; 461 } 462 printf("video layer hdl:%d\n", hlay); 463 464 layer_para.mode = DISP_LAYER_WORK_MODE_SCALER; 465 layer_para.pipe = 0; 466 layer_para.fb.addr[0] = 0;//your Y address,modify this 467 layer_para.fb.addr[1] = 0; //your C address,modify this 468 layer_para.fb.addr[2] = 0; 469 layer_para.fb.size.width = w; 470 layer_para.fb.size.height = h; 471 layer_para.fb.mode = disp_mode;///DISP_MOD_INTERLEAVED;//DISP_MOD_NON_MB_PLANAR;//DISP_MOD_NON_MB_PLANAR;//DISP_MOD_NON_MB_UV_COMBINED; 472 layer_para.fb.format = disp_format;//DISP_FORMAT_YUV420;//DISP_FORMAT_YUV422;//DISP_FORMAT_YUV420; 473 layer_para.fb.br_swap = 0; 474 layer_para.fb.seq = disp_seq;//DISP_SEQ_UVUV;//DISP_SEQ_YUYV;//DISP_SEQ_YVYU;//DISP_SEQ_UYVY;//DISP_SEQ_VYUY//DISP_SEQ_UVUV 475 layer_para.ck_enable = 0; 476 layer_para.alpha_en = 1; 477 layer_para.alpha_val = 0xff; 478 layer_para.src_win.x = 0; 479 layer_para.src_win.y = 0; 480 layer_para.src_win.width = w; 481 layer_para.src_win.height = h; 482 layer_para.scn_win.x = 0; 483 layer_para.scn_win.y = 0; 484 layer_para.scn_win.width = LCD_WIDTH;//800; 485 layer_para.scn_win.height = LCD_HEIGHT;//480; 486 arg[0] = sel; 487 arg[1] = hlay; 488 arg[2] = (__u32)&layer_para; 489 ioctl(disphd,DISP_CMD_LAYER_SET_PARA,(void*)arg); 490#if 0 491 arg[0] = sel; 492 arg[1] = hlay; 493 ioctl(disphd,DISP_CMD_LAYER_TOP,(void*)arg); 494#endif 495 arg[0] = sel; 496 arg[1] = hlay; 497 ioctl(disphd,DISP_CMD_LAYER_OPEN,(void*)arg); 498 499#if 1 500 int fb_fd; 501 unsigned long fb_layer; 502 void *addr = NULL; 503 fb_fd = open("/dev/fb0", O_RDWR); 504 if (ioctl(fb_fd, FBIOGET_LAYER_HDL_0, &fb_layer) == -1) { 505 printf("get fb layer handel\n"); 506 } 507 508 addr = malloc(LCD_WIDTH*LCD_HEIGHT*3); 509 memset(addr, 0xff, LCD_WIDTH*LCD_HEIGHT*3); 510 write(fb_fd, addr, LCD_WIDTH*LCD_HEIGHT*3); 511 //memset(addr, 0x12, 800*480*3); 512 513 printf("fb_layer hdl: %ld\n", fb_layer); 514 close(fb_fd); 515 516 arg[0] = 0; 517 arg[1] = fb_layer; 518 ioctl(disphd, DISP_CMD_LAYER_BOTTOM, (void *)arg); 519#endif 520} 521 522void disp_start(void) 523{ 524 arg[0] = sel; 525 arg[1] = hlay; 526 ioctl(disphd, DISP_CMD_VIDEO_START, (void*)arg); 527} 528 529void disp_stop(void) 530{ 531 arg[0] = sel; 532 arg[1] = hlay; 533 ioctl(disphd, DISP_CMD_VIDEO_STOP, (void*)arg); 534} 535 536int disp_on() 537{ 538 arg[0] = 0; 539 ioctl(disphd, DISP_CMD_LCD_ON, (void*)arg); 540} 541 542int disp_set_addr(int w,int h,int *addr) 543{ 544#if 0 545 layer_para.fb.addr[0] = *addr;//your Y address,modify this 546 layer_para.fb.addr[1] = *addr+w*h; //your C address,modify this 547 layer_para.fb.addr[2] = *addr+w*h*3/2; 548 549 arg[0] = sel; 550 arg[1] = hlay; 551 arg[2] = (__u32)&layer_para; 552 ioctl(disphd,DISP_CMD_LAYER_SET_PARA,(void*)arg); 553#endif 554 __disp_video_fb_t fb_addr; 555 memset(&fb_addr, 0, sizeof(__disp_video_fb_t)); 556 557 fb_addr.interlace = 0; 558 fb_addr.top_field_first = 0; 559 fb_addr.frame_rate = 25; 560 fb_addr.addr[0] = *addr; 561// fb_addr.addr[1] = *addr + w * h; 562// fb_addr.addr[2] = *addr + w*h*3/2; 563 564 565 switch(csi_format){ 566 case V4L2_PIX_FMT_YUV422P: 567 case V4L2_PIX_FMT_YUYV: 568 case V4L2_PIX_FMT_YVYU: 569 case V4L2_PIX_FMT_UYVY: 570 case V4L2_PIX_FMT_VYUY: 571 fb_addr.addr[1] = *addr+w*h; //your C address,modify this 572 fb_addr.addr[2] = *addr+w*h*3/2; 573 break; 574 case V4L2_PIX_FMT_YUV420: 575 fb_addr.addr[1] = *addr+w*h; //your C address,modify this 576 fb_addr.addr[2] = *addr+w*h*5/4; 577 break; 578 case V4L2_PIX_FMT_NV16: 579 case V4L2_PIX_FMT_NV12: 580 case V4L2_PIX_FMT_HM12: 581 fb_addr.addr[1] = *addr+w*h; //your C address,modify this 582 fb_addr.addr[2] = layer_para.fb.addr[1]; 583 break; 584 585 default: 586 printf("csi_format is not found!\n"); 587 break; 588 589 } 590 591 fb_addr.id = 0; //TODO 592 593 arg[0] = sel; 594 arg[1] = hlay; 595 arg[2] = (__u32)&fb_addr; 596 ioctl(disphd, DISP_CMD_VIDEO_SET_FB, (void*)arg); 597} 598 599int disp_quit() 600{ 601 __u32 arg[4]; 602 arg[0] = 0; 603 ioctl(disphd, DISP_CMD_LCD_OFF, (void*)arg); 604 605 arg[0] = sel; 606 arg[1] = hlay; 607 ioctl(disphd, DISP_CMD_LAYER_CLOSE, (void*)arg); 608 609 arg[0] = sel; 610 arg[1] = hlay; 611 ioctl(disphd, DISP_CMD_LAYER_RELEASE, (void*)arg); 612 close (disphd); 613} 614 615int main_test (void) 616{ 617 struct v4l2_capability cap; 618 struct v4l2_format fmt; 619 unsigned int i; 620 enum v4l2_buf_type type; 621 struct v4l2_cropcap cropcap; 622 623 //fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);//???? 624 //fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);//???? 625 //close (fd); 626 fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);//???? 627 628 if(invalid_ops) 629 { 630 if(-1 == ioctl (fd, 0xff, &cropcap)) 631 printf("invalid_ops return error\n"); 632 } 633 if(ioctl_test==1) 634 { 635 //Test VIDIOC_QUERYCAP 636 if (-1 == ioctl (fd, VIDIOC_QUERYCAP, &cap))//??????? 637 printf("VIDIOC_QUERYCAP error!\n"); 638 639 printf("cap.driver=%s\n",cap.driver); 640 printf("cap.card=%s\n",cap.card); 641 printf("cap.bus_info=%s\n",cap.bus_info); 642 printf("cap.version=%d\n",cap.version); 643 printf("cap.capabilities=%d\n",cap.capabilities); 644 645 //Test VIDIOC_ENUMINPUT,VIDIOC_S_INPUT,VIDIOC_G_INPUT 646 struct v4l2_input inp; 647 648 for(i=0;i<2;i++) 649 { 650 inp.index = i; 651 if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &inp))//?????? 652 printf("VIDIOC_ENUMINPUT error!\n"); 653 if (inp.type == V4L2_INPUT_TYPE_CAMERA) 654 printf("enuminput type is V4L2_INPUT_TYPE_CAMERA!\n"); 655 656 if (-1 == ioctl (fd, VIDIOC_S_INPUT, &inp)) //????index 657 printf("VIDIOC_S_INPUT error!\n"); 658 659 if (-1 == ioctl (fd, VIDIOC_G_INPUT, &inp)) //????index 660 printf("VIDIOC_G_INPUT error!\n"); 661 printf("input index is %d\n",inp.index); 662 } 663 //Test VIDIOC_ENUM_FMT 664 struct v4l2_fmtdesc fmtdesc; 665 666 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 667 for(i=0;i<12;i++) 668 { 669 fmtdesc.index = i; 670 if (-1 == ioctl (fd, VIDIOC_ENUM_FMT, &fmtdesc))//?????? 671 { 672 printf("VIDIOC_ENUM_FMT error!\n"); 673 continue; 674 } 675 printf("**************************************************************\n"); 676 printf("format index = %d, name = %s, v4l2 pixel format = %x\n",i,fmtdesc.description,fmtdesc.pixelformat); 677 } 678 } 679 680// printf("%s %d\n",__FILE__,__LINE__); 681 //Test VIDIOC_S_FMT 682 CLEAR (fmt); 683 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 684 fmt.fmt.pix.width = input_size.width; //320; 685 fmt.fmt.pix.height = input_size.height; //240; 686 fmt.fmt.pix.pixelformat = csi_format;//V4L2_PIX_FMT_YUV422P;//V4L2_PIX_FMT_NV12;//V4L2_PIX_FMT_YUYV; 687 fmt.fmt.pix.field = V4L2_FIELD_NONE; 688 int ret = -1; 689 690 691 if(invalid_fmt_test==1) 692 { 693 printf("Try V4L2_PIX_FMT_YUV410\n"); 694 fmt.fmt.pix.pixelformat=V4L2_PIX_FMT_YUV410; 695 if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt)) //???????? 696 { 697 printf("VIDIOC_S_FMT error!\n"); 698 } 699 700 printf("Try V4L2_PIX_FMT_YVU420\n"); 701 fmt.fmt.pix.pixelformat=V4L2_PIX_FMT_YVU420; 702 if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt)) //???????? 703 { 704 printf("VIDIOC_S_FMT error!\n"); 705 } 706 707 printf("Try V4L2_PIX_FMT_NV16\n"); 708 fmt.fmt.pix.pixelformat=V4L2_PIX_FMT_NV16; 709 } 710 711 712 713 714 715 if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt)) //?????? 716 { 717 printf("VIDIOC_S_FMT error!\n"); 718 ret = -1; 719 return ret; 720 //goto close; 721 } 722 723 disp_size.width = fmt.fmt.pix.width; 724 disp_size.height = fmt.fmt.pix.height; 725 726// printf("%s %d\n",__FILE__,__LINE__); 727 if(ioctl_test==1) 728 { 729 printf("**************************************************************\n"); 730 printf("fmt.type = %d\n",fmt.type); 731 printf("fmt.fmt.pix.width = %d\n",fmt.fmt.pix.width); 732 printf("fmt.fmt.pix.height = %d\n",fmt.fmt.pix.height); 733 printf("fmt.fmt.pix.pixelformat = %x\n",fmt.fmt.pix.pixelformat); 734 printf("fmt.fmt.pix.field = %d\n",fmt.fmt.pix.field); 735 } 736 //Test VIDIOC_G_FMT 737 738 739 740 if (-1 == ioctl (fd, VIDIOC_G_FMT, &fmt)) //?????? 741 { 742 printf("VIDIOC_G_FMT error!\n"); 743 } 744 else 745 { 746 printf("**************************************************************\n"); 747 printf("resolution got from sensor = %d*%d\n",fmt.fmt.pix.width,fmt.fmt.pix.height); 748 printf("**************************************************************\n"); 749 } 750 751 752 if(ioctl_test==1) 753 { 754 printf("**************************************************************\n"); 755 printf("fmt.fmt.pix.width = %d\n",fmt.fmt.pix.width); 756 printf("fmt.fmt.pix.height = %d\n",fmt.fmt.pix.height); 757 printf("fmt.fmt.pix.pixelformat = %x\n",fmt.fmt.pix.pixelformat); 758 printf("fmt.fmt.pix.field = %d\n",fmt.fmt.pix.field); 759 printf("fmt.fmt.pix.bytesperline = %d\n",fmt.fmt.pix.bytesperline); 760 printf("fmt.fmt.pix.sizeimage = %d\n",fmt.fmt.pix.sizeimage); 761 } 762 763 if(fps_test==1) 764 { 765 //Test VIDIOC_G_PARM 766 struct v4l2_streamparm parms; 767 parms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 768 769 if (-1 == ioctl (fd, VIDIOC_G_PARM, &parms)) //???? 770 printf ("VIDIOC_G_PARM error\n"); 771 772 printf("numerator = %d\n",parms.parm.capture.timeperframe.numerator); 773 printf("denominator = %d\n",parms.parm.capture.timeperframe.denominator); 774 775 776// //Test VIDIOC_S_PARM 777// parms.parm.capture.timeperframe.denominator = fps;// 778// 779// if (-1 == ioctl (fd, VIDIOC_S_PARM, &parms)) //???? 780// printf ("VIDIOC_G_PARM error\n"); 781// 782// //Test VIDIOC_G_PARM 783// if (-1 == ioctl (fd, VIDIOC_G_PARM, &parms)) //???? 784// printf ("VIDIOC_G_PARM error\n"); 785// 786// printf("numerator = %d\n",parms.parm.capture.timeperframe.numerator); 787// printf("denominator = %d\n",parms.parm.capture.timeperframe.denominator); 788 } 789 790 //count=read_num; 791 792 struct v4l2_requestbuffers req; 793 CLEAR (req); 794 req.count = req_frame_num; 795 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 796 req.memory = V4L2_MEMORY_MMAP; 797 798 799 ioctl (fd, VIDIOC_REQBUFS, &req); //?????count?????? 800 801 buffers = calloc (req.count, sizeof (*buffers));//????????? 802 803 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) 804 { 805 struct v4l2_buffer buf; //?????? 806 CLEAR (buf); 807 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 808 buf.memory = V4L2_MEMORY_MMAP; 809 buf.index = n_buffers; 810 811 if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)) //?????? 812 printf ("VIDIOC_QUERYBUF error\n"); 813 814 buffers[n_buffers].length = buf.length; 815 buffers[n_buffers].start = mmap (NULL /* start anywhere */, //??mmap?????? 816 buf.length, 817 PROT_READ | PROT_WRITE /* required */, 818 MAP_SHARED /* recommended */, 819 fd, buf.m.offset); 820 821 if (MAP_FAILED == buffers[n_buffers].start) 822 printf ("mmap failed\n"); 823 } 824 825 for (i = 0; i < n_buffers; ++i) 826 { 827 struct v4l2_buffer buf; 828 CLEAR (buf); 829 830 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 831 buf.memory = V4L2_MEMORY_MMAP; 832 buf.index = i; 833 834 if (-1 == ioctl (fd, VIDIOC_QBUF, &buf))//?????????? 835 printf ("VIDIOC_QBUF failed\n"); 836 } 837 838#ifdef DISPLAY 839 disp_int(disp_size.width,disp_size.height); 840 disp_start(); 841#endif 842 843 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 844 845 if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) //???????? 846 printf ("VIDIOC_STREAMON failed\n"); 847 else 848 printf ("VIDIOC_STREAMON ok\n"); 849 850 if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) //???????? 851 printf ("VIDIOC_STREAMON failed\n"); 852 else 853 printf ("VIDIOC_STREAMON ok\n"); 854 855 count = read_num; 856 857 while(count-->0) 858// while(1) 859 { 860 //gettimeofday(&time_test,&tz); 861 862 863 864 for (;;) //????????IO 865 { 866 fd_set fds; 867 struct timeval tv; 868 int r; 869 870 FD_ZERO (&fds);//???????????? 871 FD_SET (fd, &fds);//???????????????????? 872 873 /* Timeout. */ 874 tv.tv_sec = 2; 875 tv.tv_usec = 0; 876 877 r = select (fd + 1, &fds, NULL, NULL, &tv);//??????????????????tv??? 878 879 if (-1 == r) { 880 if (EINTR == errno) 881 continue; 882 printf ("select err\n"); 883 } 884 if (0 == r) { 885 fprintf (stderr, "select timeout\n"); 886 exit (EXIT_FAILURE); 887 } 888 889#ifdef DISPLAY 890 if(count==read_num-1) 891 disp_on(); 892#endif 893 if (read_frame ())//???????read_frame ()???????? 894 break; 895 } 896 } 897 898 if(control_test==1) 899 { 900 struct v4l2_queryctrl qc; 901 struct v4l2_control ctrl; 902 903 for(i=0;i<sizeof(qc_ctrl);i++) 904 { 905 CLEAR(qc); 906 qc.id = qc_ctrl[i]; 907 if (-1 == ioctl (fd, VIDIOC_QUERYCTRL, &qc)) 908 { 909 printf("VIDIOC_QUERYCTRL %s failed!\n",qc.name); 910 continue; 911 } 912 else 913 { 914 printf("**************************************************************\n"); 915 printf("Name:%s\n",qc.name); 916 printf("qc.min = %d\n",qc.minimum); 917 printf("qc.max = %d\n",qc.maximum); 918 printf("qc.step = %d\n",qc.step); 919 printf("qc.default_value = %d\n",qc.default_value); 920 } 921 922 ctrl.id = qc.id; 923 ctrl.value = qc.maximum; 924 if (-1 == ioctl (fd, VIDIOC_S_CTRL, &ctrl)) 925 printf("VIDIOC_S_CTRL %s failed!\n",qc.name); 926 else 927 { 928 printf("%s set to max\n",qc.name); 929 930 } 931 932 ctrl.value = 0; 933 if (-1 == ioctl (fd, VIDIOC_G_CTRL, &ctrl)) 934 printf("VIDIOC_G_CTRL %s failed!\n",qc.name); 935 else 936 { 937 printf("Name:%s\n",qc.name); 938 printf("read ctrl.value = %d\n",ctrl.value); 939 } 940 941 printf("press ENTER to continue!\n"); 942 getchar(); 943 944 ctrl.value = qc.minimum; 945 if (-1 == ioctl (fd, VIDIOC_S_CTRL, &ctrl)) 946 printf("VIDIOC_S_CTRL %s failed!\n",qc.name); 947 else 948 { 949 printf("%s set to min\n",qc.name); 950 } 951 952 ctrl.value = 0; 953 if (-1 == ioctl (fd, VIDIOC_G_CTRL, &ctrl)) 954 printf("VIDIOC_G_CTRL %s failed!\n",qc.name); 955 else 956 { 957 printf("Name:%s\n",qc.name); 958 printf("read ctrl.value = %d\n",ctrl.value); 959 } 960 961 printf("press ENTER to continue!\n"); 962 getchar(); 963 964 ctrl.value = qc.default_value; 965 if (-1 == ioctl (fd, VIDIOC_S_CTRL, &ctrl)) 966 printf("VIDIOC_S_CTRL %s failed!\n",qc.name); 967 else 968 { 969 printf("%s set to default_value\n",qc.name); 970 } 971 972 } 973 } 974 975close: 976 if (-1 == ioctl (fd, VIDIOC_STREAMOFF, &type)) //???????? 977 printf ("VIDIOC_STREAMOFF failed\n"); 978 else 979 printf ("VIDIOC_STREAMOFF ok\n"); 980 981 if (-1 == ioctl (fd, VIDIOC_STREAMOFF, &type)) //???????? 982 printf ("VIDIOC_STREAMOFF failed\n"); 983 else 984 printf ("VIDIOC_STREAMOFF ok\n"); 985 986 if(read_num==1) 987 { 988 printf("press ENTER key to continue!\n"); 989 getchar(); 990 } 991 992unmap: 993 for (i = 0; i < n_buffers; ++i) { 994 if (-1 == munmap (buffers[i].start, buffers[i].length)) { 995 printf ("munmap error"); 996 } 997 } 998 disp_stop(); 999 disp_quit(); 1000 1001 1002 1003 close (fd); 1004 1005 return 0; 1006} 1007 1008int 1009main(void) 1010{ 1011 int i; 1012 struct test_case *test_ptr; 1013 1014 test_num=1; 1015 read_num=200; 1016 1017 req_frame_num = 5; 1018 input_size.width = 1280;//1600;//640; 1019 input_size.height = 1024;//1200;//480; 1020// disp_size.width = 1280;//1600;//640; 1021// disp_size.height = 1024;//1200;//480; 1022 csi_format=V4L2_PIX_FMT_NV12; 1023 disp_format=DISP_FORMAT_YUV420; 1024 disp_mode=DISP_MOD_NON_MB_UV_COMBINED; 1025 disp_seq=DISP_SEQ_UVUV; 1026 1027printf("********************************************************************Read stream test start,capture 1000 frames,press to continue\n"); 1028 getchar(); 1029 1030 read_num = 1000; 1031 main_test(); 1032 1033//printf("********************************************************************fps test start,press to continue\n"); 1034// getchar(); 1035// 1036// fps_test=1; 1037// read_num=30; 1038// main_test(); 1039// fps_test=0; 1040 1041//printf("********************************************************************IOCTL invalid test start,press to continue\n"); 1042// getchar(); 1043// invalid_ops=1; 1044// main_test(); 1045// invalid_ops=0; 1046// 1047//printf("********************************************************************Try and set invalid format test start,press to continue\n"); 1048// getchar(); 1049// 1050// invalid_fmt_test=1; 1051// main_test(); 1052// invalid_fmt_test=0; 1053// 1054// 1055//printf("********************************************************************ENUMFMT,SETFMT,GETFMT test start,press to continue\n"); 1056// getchar(); 1057// ioctl_test=1; 1058// main_test(); 1059// ioctl_test=0; 1060// 1061// 1062//printf("********************************************************************Read one frame test start,capture 1 frame,press to continue\n"); 1063// getchar(); 1064// read_num=1; 1065// main_test(); 1066// 1067//printf("********************************************************************Read stream test start,capture 1000 frames,press to continue\n"); 1068// getchar(); 1069// 1070// read_num = 1000; 1071// main_test(); 1072 1073//printf("********************************************************************Req buffer test start,press to continue\n"); 1074// getchar(); 1075// 1076// read_num = 100; 1077// for(i=1;i<7;i++) 1078// { 1079// printf("Req buffer count = %d, capture 100 frames\n",i); 1080// req_frame_num = i; 1081// main_test(); 1082// printf("press to continue\n"); 1083// getchar(); 1084// } 1085// 1086// 1087// 1088//printf("********************************************************************V4L2 control test start,press to continue\n"); 1089// getchar(); 1090// control_test=1; 1091// csi_format=V4L2_PIX_FMT_NV16; 1092// disp_format=DISP_FORMAT_YUV422; 1093// disp_mode=DISP_MOD_NON_MB_UV_COMBINED; 1094// disp_seq=DISP_SEQ_UVUV; 1095// main_test(); 1096// control_test=0; 1097// 1098printf("********************************************************************resolution and format test start,press to continue\n"); 1099 getchar(); 1100 1101 read_num=200; 1102 1103 for(i=0;i<30;i++) //16 //30 1104 { 1105 test_ptr = &test_case_set[i]; 1106 input_size.width = test_ptr->input_width; 1107 input_size.height = test_ptr->input_height; 1108// disp_size.width = test_ptr->disp_width; 1109// disp_size.height = test_ptr->disp_height; 1110 csi_format = test_ptr->csi_format; 1111 disp_format = test_ptr->disp_format; 1112 disp_mode=test_ptr->disp_mode; 1113 disp_seq=test_ptr->disp_seq; 1114 1115 printf("***************************************************************************************\ninput size:%dx%d\n", 1116 input_size.width,input_size.height); 1117 1118 switch(csi_format){ 1119 case V4L2_PIX_FMT_YUV422P: 1120 printf("format: V4L2_PIX_FMT_YUV422P\n"); 1121 break; 1122 case V4L2_PIX_FMT_YUV420: 1123 printf("format: V4L2_PIX_FMT_YUV420\n"); 1124 break; 1125 case V4L2_PIX_FMT_NV16: 1126 printf("format: V4L2_PIX_FMT_NV16\n"); 1127 break; 1128 case V4L2_PIX_FMT_NV12: 1129 printf("format: V4L2_PIX_FMT_NV12\n"); 1130 break; 1131 case V4L2_PIX_FMT_HM12: 1132 printf("format: V4L2_PIX_FMT_HM12\n"); 1133 break; 1134 case V4L2_PIX_FMT_YUYV: 1135 printf("format: V4L2_PIX_FMT_YUYV\n"); 1136 break; 1137 case V4L2_PIX_FMT_YVYU: 1138 printf("format: V4L2_PIX_FMT_YVYU\n"); 1139 break; 1140 case V4L2_PIX_FMT_UYVY: 1141 printf("format: V4L2_PIX_FMT_UYVY\n"); 1142 break; 1143 case V4L2_PIX_FMT_VYUY: 1144 printf("format: V4L2_PIX_FMT_VYUY\n"); 1145 break; 1146 default: 1147 printf("format: error\n"); 1148 break; 1149 } 1150 1151 printf("***************************************************************************************\n"); 1152 main_test(); 1153 printf("press to continue\n"); 1154 getchar(); 1155 } 1156 1157printf("********************************************************************lost frame test start,press to continue\n"); 1158 getchar(); 1159 lost_frame_test=1; 1160 fps=30; 1161 read_num=200; 1162 req_frame_num = 4; 1163 input_size.width = 640; 1164 input_size.height = 480; 1165 disp_size.width = 640; 1166 disp_size.height = 480; 1167 csi_format=V4L2_PIX_FMT_NV16; 1168 disp_format=DISP_FORMAT_YUV422; 1169 disp_mode=DISP_MOD_NON_MB_UV_COMBINED; 1170 disp_seq=DISP_SEQ_UVUV; 1171 main_test(); 1172// 1173// 1174//printf("********************************************************************fps test start,press to continue\n"); 1175// getchar(); 1176// 1177// printf("set fps to 30fps\n"); 1178// fps_test=1; 1179// fps=30; 1180// read_num=30; 1181// req_frame_num = 4; 1182// input_size.width = 640; 1183// input_size.height = 480; 1184// disp_size.width = 640; 1185// disp_size.height = 480; 1186// csi_format=V4L2_PIX_FMT_NV16; 1187// disp_format=DISP_FORMAT_YUV422; 1188// disp_mode=DISP_MOD_NON_MB_UV_COMBINED; 1189// disp_seq=DISP_SEQ_UVUV; 1190// main_test(); 1191// 1192// 1193// 1194// printf("set fps to 15fps\n"); 1195// fps=15; 1196// read_num=30; 1197// req_frame_num = 4; 1198// input_size.width = 640; 1199// input_size.height = 480; 1200// disp_size.width = 640; 1201// disp_size.height = 480; 1202// csi_format=V4L2_PIX_FMT_NV16; 1203// disp_format=DISP_FORMAT_YUV422; 1204// disp_mode=DISP_MOD_NON_MB_UV_COMBINED; 1205// disp_seq=DISP_SEQ_UVUV; 1206// main_test(); 1207 1208printf("********************************************************************test done,press to end\n"); 1209 getchar(); 1210 1211 1212 1213 exit (EXIT_SUCCESS); 1214 return 0; 1215}