/* * ======== api_exit ======== */ void api_exit(void) { DBC_REQUIRE(api_c_refs > 0); api_c_refs--; if (api_c_refs == 0) { /* Release all modules initialized in api_init(). */ cod_exit(); dev_exit(); chnl_exit(); msg_exit(); io_exit(); strm_exit(); disp_exit(); node_exit(); proc_exit(); mgr_exit(); rmm_exit(); drv_exit(); } DBC_ENSURE(api_c_refs >= 0); }
static void disp_exit() { disp_exit(); exit(0); }
int main(int argc, char* argv[]) { int i; int key_fd = -1; struct input_event data; struct sigaction sigact; memset(&sigact, 0, sizeof(sigact)); sigact.sa_handler = handle_int; //format=V4L2_PIX_FMT_NV12; //V4L2_PIX_FMT_NV16 format = V4L2_PIX_FMT_NV12; disp_format=DISP_FORMAT_YUV420; //DISP_FORMAT_YUV422 disp_seq=DISP_SEQ_UVUV; struct v4l2_format fmt; struct v4l2_format fmt_priv; printf("*********************\n"); printf("TVD demo start!\n"); printf("Press KEY_ESC for exit\n"); printf("*********************\n"); /*if((key_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK)) < 0){//key: linux=event0, android=event2 printf("open event fail!\n"); return -1; }*/ fd = open ("/dev/video1", O_RDWR /* required */ | O_NONBLOCK, 0); int ret = -1; CLEAR (fmt_priv); fmt_priv.type = V4L2_BUF_TYPE_PRIVATE; fmt_priv.fmt.raw_data[0] =0;//interface fmt_priv.fmt.raw_data[1] =0;//system fmt_priv.fmt.raw_data[2] =0;//format 1=mb, for test only fmt_priv.fmt.raw_data[8] =2;//row fmt_priv.fmt.raw_data[9] =2;//column fmt_priv.fmt.raw_data[10] =1;//channel_index fmt_priv.fmt.raw_data[11] =2;//channel_index fmt_priv.fmt.raw_data[12] =3;//channel_index fmt_priv.fmt.raw_data[13] =4;//channel_index if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt_priv)) //设置自定义 { printf("VIDIOC_S_FMT error! a\n"); ret = -1; return ret; } // CLEAR (fmt); // fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; // fmt.fmt.pix.width = 720*fmt_priv.fmt.raw_data[8];//720; // fmt.fmt.pix.height = 480*fmt_priv.fmt.raw_data[9];//576;//480; // fmt.fmt.pix.pixelformat = format;//V4L2_PIX_FMT_YUV422P;//V4L2_PIX_FMT_NV12;//V4L2_PIX_FMT_YUYV; // fmt.fmt.pix.field = V4L2_FIELD_NONE; // if (-1 == ioctl (fd, VIDIOC_S_FMT, &fmt)) //设置图像格式 // { // printf("VIDIOC_S_FMT error! b\n"); // ret = -1; // return ret; // } disp_mode=fmt_priv.fmt.raw_data[2]?DISP_MOD_MB_UV_COMBINED:DISP_MOD_NON_MB_UV_COMBINED;//DISP_MOD_NON_MB_UV_COMBINED DISP_MOD_MB_UV_COMBINED disp_size.width = fmt_priv.fmt.raw_data[8]*(fmt_priv.fmt.raw_data[2]?704:720);//width disp_size.height = fmt_priv.fmt.raw_data[9]*(fmt_priv.fmt.raw_data[1]?576:480);//height printf("disp_size.width=%d\n", disp_size.width); printf("disp_size.height=%d\n", disp_size.height); usleep(100000);//delay 100ms if you want to check the status after set fmt CLEAR (fmt_priv); fmt_priv.type = V4L2_BUF_TYPE_PRIVATE; if (-1 == ioctl (fd, VIDIOC_G_FMT, &fmt_priv)) //设置自定义 { printf("VIDIOC_G_FMT error! a\n"); ret = -1; return ret; } printf("interface=%d\n", fmt_priv.fmt.raw_data[0]); printf("system=%d\n", fmt_priv.fmt.raw_data[1]); printf("format=%d\n", fmt_priv.fmt.raw_data[2]); printf("row=%d\n", fmt_priv.fmt.raw_data[8]); printf("column=%d\n", fmt_priv.fmt.raw_data[9]); printf("channel_index[0]=%d\n", fmt_priv.fmt.raw_data[10]); printf("channel_index[1]=%d\n", fmt_priv.fmt.raw_data[11]); printf("channel_index[2]=%d\n", fmt_priv.fmt.raw_data[12]); printf("channel_index[3]=%d\n", fmt_priv.fmt.raw_data[13]); printf("status[0]=%d\n", fmt_priv.fmt.raw_data[16]); printf("status[1]=%d\n", fmt_priv.fmt.raw_data[17]); printf("status[2]=%d\n", fmt_priv.fmt.raw_data[18]); printf("status[3]=%d\n", fmt_priv.fmt.raw_data[19]); struct v4l2_requestbuffers req; CLEAR (req); req.count = 5; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; ioctl (fd, VIDIOC_REQBUFS, &req); //申请缓冲,count是申请的数量,注意,释放缓冲实际在VIDIOC_STREAMOFF内完成了。 buffers = calloc (req.count, sizeof (*buffers));//内存中建立对应空间 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { struct v4l2_buffer buf; //驱动中的一帧 CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = n_buffers; if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)) //映射用户空间 printf ("VIDIOC_QUERYBUF error\n"); buffers[n_buffers].length = buf.length; buffers[n_buffers].start = mmap (NULL /* start anywhere */, //通过mmap建立映射关系 buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, fd, buf.m.offset); printf("MMAP: %p OFF: %p\n", buffers[n_buffers].start, buf.m.offset); if (MAP_FAILED == buffers[n_buffers].start) printf ("mmap failed\n"); } for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; if (-1 == ioctl (fd, VIDIOC_QBUF, &buf))//申请到的缓冲进入列队 printf ("VIDIOC_QBUF failed\n"); } #ifdef DISPLAY disp_init(disp_size.width,disp_size.height); disp_start(); disp_on(); #endif enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) //开始捕捉图像数据 printf ("VIDIOC_STREAMON failed\n"); sigaction(SIGINT, &sigact, NULL); while(!quit) { read(key_fd, &data, sizeof(data)); if( (data.type == EV_KEY)&& (data.code == KEY_ESC)&& (data.value == 1) ){ quit = 1; } CLEAR (fmt_priv); fmt_priv.type = V4L2_BUF_TYPE_PRIVATE; if (-1 == ioctl (fd, VIDIOC_G_FMT, &fmt_priv)) //监视状态 { printf("VIDIOC_G_FMT error! a\n"); ret = -1; return ret; } //printf("status=0x%02x\n", fmt_priv.fmt.raw_data[16]); for (;;) //这一段涉及到异步IO { fd_set fds; struct timeval tv; int r; FD_ZERO (&fds);//将指定的文件描述符集清空 FD_SET (fd, &fds);//在文件描述符集合中增加一个新的文件描述符 /* Timeout. */ tv.tv_sec = 2; tv.tv_usec = 0; r = select (fd + 1, &fds, NULL, NULL, &tv);//判断是否可读(即摄像头是否准备好),tv是定时 if (-1 == r) { if (EINTR == errno) continue; printf ("select err\n"); } if (0 == r) { fprintf (stderr, "select timeout\n"); //exit (EXIT_FAILURE); //goto close; } if (read_frame ())//如果可读,执行read_frame ()函数,并跳出循环 break; } } close: if (-1 == ioctl (fd, VIDIOC_STREAMOFF, &type)) //停止捕捉图像数据,注意,此动作同时会释放VIDIOC_REQBUFS申请的缓冲 printf ("VIDIOC_STREAMOFF failed\n"); unmap: for (i = 0; i < n_buffers; ++i) { if (-1 == munmap (buffers[i].start, buffers[i].length)) { printf ("munmap error"); } } disp_stop(); disp_exit(); close (fd); printf("TVD demo bye!\n"); return 0; }
/* * ======== api_init ======== * Purpose: * Module initialization used by Bridge API. */ bool api_init(void) { bool ret = true; bool fdrv, fdev, fcod, fchnl, fmsg, fio; bool fmgr, fproc, fnode, fdisp, fstrm, frmm; if (api_c_refs == 0) { /* initialize driver and other modules */ fdrv = drv_init(); fmgr = mgr_init(); fproc = proc_init(); fnode = node_init(); fdisp = disp_init(); fstrm = strm_init(); frmm = rmm_init(); fchnl = chnl_init(); fmsg = msg_mod_init(); fio = io_init(); fdev = dev_init(); fcod = cod_init(); ret = fdrv && fdev && fchnl && fcod && fmsg && fio; ret = ret && fmgr && fproc && frmm; if (!ret) { if (fdrv) drv_exit(); if (fmgr) mgr_exit(); if (fstrm) strm_exit(); if (fproc) proc_exit(); if (fnode) node_exit(); if (fdisp) disp_exit(); if (fchnl) chnl_exit(); if (fmsg) msg_exit(); if (fio) io_exit(); if (fdev) dev_exit(); if (fcod) cod_exit(); if (frmm) rmm_exit(); } } if (ret) api_c_refs++; return ret; }
/* display piu isr handler */ void disp_handler(void *ctx, piu_msg_p msg) { uint16_t cmd_id; memcpy(&disp_rx, msg, sizeof(piu_msg_t)); cmd_id = DISP_U8_TO_U16(disp_rx.p); dbg("V %x\n", cmd_id); if ((cmd_id & 0xFF) == DISP_GET) { switch (cmd_id&0xFF00) { case DISP_IMAGE_PROPERTY: { disp_tx.len = 6; DISP_U8_TO_U16(disp_tx.p) = cmd_id; DISP_U8_TO_U16(disp_tx.p+2) = cfg.output.size.w; DISP_U8_TO_U16(disp_tx.p+4) = cfg.output.size.h; dbg("DISP: rx DISP_GET_IMAGE_PROPERTY ->tx [%d,%d] \n", cfg.output.size.w, cfg.output.size.h); break; } case DISP_IMAGE_START: { disp_tx.len = 6; DISP_U8_TO_U16(disp_tx.p) = cmd_id; DISP_U8_TO_U16(disp_tx.p+2) = 0; //offset[0]; DISP_U8_TO_U16(disp_tx.p+4) = 0; //offset[1]; dbg("DISP: rx DISP_GET_IMAGE_START -> tx [%d,%d] \n", 0,0); //offset[0], offset[1]); break; } default: { printf("rx Unkwown DISP_GET\n"); return; } piu_tx(PIU_DISP_QID, &disp_tx); } } else if (cmd_id == (DISP_CFG | DISP_CFG_BUF)) { // DO NOT send back response because ceva can't receive it due to nested interrupt uint32_t *addr = (uint32_t *)(disp_rx.p+4); cfg.input.ybuf[0] = addr[0]; cfg.input.cbbuf[0] = addr[1]; cfg.input.crbuf[0] = addr[2]; // check if there is partial display mode or not if((g_disp_mode == DISP_IMAGE_MODE) &&(g_partial_disp_mode == 1)) { //update VPP output buffer pointor cfg.output.ybuf[0] = vpp_output[0] + g_jpg_slices_index; cfg.output.cbbuf[0] = 0; cfg.output.crbuf[0] = 0; g_jpg_slices_index += g_jpg_slices_height*g_rgb_elem_size*cfg.output.size.w; dbg("DISP: output ybuf: %x, buf index: %x\n", cfg.output.ybuf[0], g_jpg_slices_index); } cfg.flag = VPP_F_ADDR; dbg("DISP: rx DISP_CFG_BUF: (%x, %x, %x)\n", addr[0], addr[1], addr[2]); if (vpp_cfg(&vpp, &cfg, DISP_VPP_CH)) { printf("DISP: VPP_CFG failed\n"); return; } dbg("DISP: DISP_START\n"); if (vpp_start(&vpp, DISP_VPP_CH)) { printf("DISP: VPP_START failed\n"); return; } dbg("DISP: DISP_START done\n"); } else { if ((cmd_id & 0xFF) == DISP_CFG) { if (piu_vpp_cfg(cmd_id&0xFF00, (char *)disp_rx.p + 4)) { printf("DISP: VPP_CFG failed\n"); } } else if ((cmd_id & 0xFF) == DISP_SET) { switch (cmd_id&0xFF00) { case DISP_IMAGE_PROPERTY: { uint16_t *p = (uint16_t *)(disp_rx.p+2); cfg.input.size.w = p[0]; cfg.input.size.h = p[1]; cfg.input.stride_y = p[2]; cfg.input.stride_cb = p[3]; cfg.input.stride_cr = p[3]; // check for partial image display mode if((g_disp_mode == DISP_IMAGE_MODE) && (cfg.input.size.h < g_jpg_height)) { g_partial_disp_mode = 1; g_jpg_slices_height = (fbp[1]*cfg.input.size.h)/g_jpg_height; dbg("DISP: partial input height = %d, output height = %d, orig image height = %d\n",cfg.input.size.h,g_jpg_slices_height,g_jpg_height); } //clean 080104 if (disp_rx.len >= 12) //added 2 bytes for yuv format { switch (p[4]) { case 0: cfg.input.format = VPP_PIC_YUV444P;break; case 1: cfg.input.format = VPP_PIC_YUV422P;break; case 2: cfg.input.format = VPP_PIC_YUV420P;break; case 3: cfg.input.format = VPP_PIC_YUV411P;break; case 4: cfg.input.format = VPP_PIC_YUV400P;break; default:printf("error yuv format\n"); } dbg("DISP: output format = %d\n", cfg.input.format); } if (disp_rx.len == 16) { cfg.input.pixel.w = p[5]; cfg.input.pixel.h = p[6]; if (p[5] == 0 || p[6] == 0) { cfg.input.pixel.w = 1; cfg.input.pixel.h = 1; } } //endclean cfg.flag |= VPP_F_IN; dbg("DISP: rx DISP_SET_IMAGE_PROPERTY: size=[%d,%d], stride=[%d %d]\n", p[0], p[1], p[2], p[3]); #ifdef TV_UNDERRUN_BUG { void *stat = mapm(0x8009008, 36); uint32_t val= 0; if (stat == (void *)-1) { printf("mapm fail\n"); return; } val = *((uint32_t *)stat + 8); printf("image un=%x\n", val); if (val == 3) { int i= 500; *(uint32_t *)stat = 3; while (i--); *((uint32_t *)stat + 1) = 0x38004F; *((uint32_t *)stat + 5) = 0x93e00000; *((uint32_t *)stat + 6) = 0x93e54600; *((uint32_t *)stat + 7) = 0x93e7e900; *(uint32_t *)stat = 5; printf("image un=%x\n", *((uint32_t *)stat + 8) ); } unmapm(stat, 36); } #endif break; } default: { printf("DISP: rx Unknown DISP_SET\n"); return; } } } else { switch (cmd_id) { case DISP_INIT: { dbg("DISP: rx DISP_INIT\n"); #if 0 if (disp_init()) { printf("DISP_INIT failed\n"); return; } #endif if (piu_vpp_init()) { printf("DISP: VPP_INIT failed\n"); return; } dbg("DISP: VPP OPEN\n"); disp_tx.len = 6; DISP_U8_TO_U16(disp_tx.p) = cmd_id; DISP_U8_TO_U16(disp_tx.p+2) = cfg.output.size.w; DISP_U8_TO_U16(disp_tx.p+4) = cfg.output.size.h; piu_tx(PIU_DISP_QID, &disp_tx); return; } case DISP_START: { dbg("DISP: rx DISP_START\n"); if (vpp_start(&vpp, DISP_VPP_CH)) { printf("DISP: VPP_START failed\n"); return; } break; } case DISP_CLOSE: { g_disp_mode = DISP_VIDEO_MODE; g_partial_disp_mode = 0; g_jpg_slices_index = 0; //vpp_exit(&vpp); disp_done = 1; #ifdef TV_UNDERRUN_BUG { void *stat = mapm(0x8009028, 4); uint32_t val= 0; if (stat == (void *)-1) { printf("mapm fail\n"); return; } val = *(uint32_t *)stat; printf("close un=%x\n", val); if (val == 3) { *(uint32_t *)stat = 3; } unmapm(stat, 4); } #endif printf("DISP: VPP CLOSE\n"); #if 0 disp_exit(); printf("???disp_exit() done???\n"); #endif break; } default: { printf("Unkwown DISP command 0x%x\n", cmd_id); return; } } } #ifdef NEED_RESP printf("\n%x\n", cmd_id); disp_tx.len = 2; DISP_U8_TO_U16(disp_tx.p) = cmd_id; piu_tx(PIU_DISP_QID, &disp_tx); #endif } }