int song(){
int g_out_width = 720;
int g_out_height = 576;
int fb_fd;
int i,x,y,cur_id;
char buff[3];
char *fb_mem;
unsigned char r,g,b,count;
unsigned char y1,u1,v1;
int vd_fd;
struct v4l2_format fmt;
struct v4l2_streamparm parm;
struct v4l2_fmtdesc fmtdesc;
struct v4l2_capability cap;
struct v4l2_requestbuffers req;
VideoBuffer *buffers;
struct v4l2_buffer buf;
struct v4l2_plane planes = { 0 };
if ((fb_fd = open("/dev/graphics/fb0", O_RDWR, 0)) < 0) {
printf("song can not open fb0\n");
return 1;
}
fb_mem = mmap (NULL, 1920*720*4, PROT_READ|PROT_WRITE,MAP_SHARED,fb_fd,0);
if ((vd_fd = open("/dev/video0", O_RDWR, 0)) < 0) {
printf("song can not open video0\n");
return 1;
}
printf("\n song VIDIOC_QUERYCAP\n");
if (ioctl(vd_fd, VIDIOC_QUERYCAP, &cap) == 0) {
printf("cap=0x%x\n", cap.capabilities);
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) {
printf("song not support v4l2 capture device.\n");
return -1;
}
} else {
close(vd_fd);
printf("VIDIOC_QUERYCAP fail\n");
return -1;
}
printf("\n song VIDIOC_ENUM_FMT\n");
fmtdesc.index=0;
fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
while(ioctl(vd_fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1)
{
printf("/t%d.%s/n",fmtdesc.index+1,fmtdesc.description);
fmtdesc.index++;
}
printf("\n song VIDIOC_S_FMT\n");
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
//fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_BGR24;
//fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_RGB24;
fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix_mp.width = g_out_width;
fmt.fmt.pix_mp.height = g_out_height;
fmt.fmt.pix_mp.num_planes = 1; /* RGB */
if (ioctl(vd_fd, VIDIOC_S_FMT, &fmt) < 0) {
printf("set format failed\n");
goto fail;
}
printf("\n song VIDIOC_G_FMT\n");
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
if (ioctl(vd_fd, VIDIOC_G_FMT, &fmt) < 0) {
printf("get format failed\n");
goto fail;
}
printf(" width=%d, height=%d, \n",fmt.fmt.pix_mp.width, fmt.fmt.pix_mp.height);
print_pixelformat2("***** pixelformat", fmt.fmt.pix_mp.pixelformat);
printf("\n song VIDIOC_G_PARM\n");
memset(&parm, 0, sizeof(parm));
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
if (ioctl(vd_fd, VIDIOC_G_PARM, &parm) < 0) {
printf("VIDIOC_G_PARM failed\n");
}
printf("\n song VIDIOC_REQBUFS\n");
memset(&req, 0, sizeof(req));
req.count = 3;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(vd_fd, VIDIOC_REQBUFS, &req) < 0) {
printf("VIDIOC_REQBUFS failed\n");
goto fail;
}
buffers = calloc(req.count, sizeof(VideoBuffer));
for(i=0;i<3;i++){
printf("\n song VIDIOC_QUERYBUF\n");
memset( &buf, 0, sizeof(buf));
memset(&planes, 0, sizeof(planes));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = V4L2_MEMORY_MMAP;
buf.m.planes = &planes;
buf.length = 1; /* plane num */
buf.index = i;
if (ioctl(vd_fd, VIDIOC_QUERYBUF, &buf) < 0){
printf("VIDIOC_QUERYBUF error\n");
goto fail;
}
buffers[i].length = buf.m.planes->length;
printf("\n buffers[i].length :%d\n", buffers[i].length);
buffers[i].offset = (size_t) buf.m.planes->m.mem_offset;
buffers[i].start = mmap(NULL, buf.m.planes->length, PROT_READ | PROT_WRITE, MAP_SHARED, vd_fd, buf.m.planes->m.mem_offset);
if (buffers[i].start == MAP_FAILED){
printf("buffers error\n");
goto fail;
}
}
printf("\n 11111 buffers[i].length :%d\n", buffers[0].length);
for(i=0;i<3;i++){
printf("\n song VIDIOC_QBUF\n");
memset( &buf, 0, sizeof(buf));
memset(&planes, 0, sizeof(planes));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = V4L2_MEMORY_MMAP;
buf.m.planes = &planes;
buf.m.planes->length = buffers[i].length;
buf.length = 1; /* plane num */
buf.index = i;
buf.m.planes->m.mem_offset = buffers[i].offset;
if (ioctl(vd_fd, VIDIOC_QBUF, &buf) < 0){
printf("VIDIOC_QBUF error\n");
goto fail;
}
}
printf("\n 2222 buffers[i].length :%d\n", buffers[0].length);
printf("\n song VIDIOC_STREAMON\n");
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
if (ioctl(vd_fd, VIDIOC_STREAMON, &type) < 0){
printf("VIDIOC_STREAMON error\n");
goto fail;
}
printf("\n 3333 buffers[i].length :%d\n", buffers[0].length);
count =0;
while(1){
//count++;
printf("\n song VIDIOC_DQBUF\n");
memset( &buf, 0, sizeof(buf));
memset(&planes, 0, sizeof(planes));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = V4L2_MEMORY_MMAP;
buf.m.planes = &planes;
buf.length = 1; /* plane num */
if (ioctl(vd_fd, VIDIOC_DQBUF, &buf) < 0){
printf("VIDIOC_DQBUF failed.\n");
goto fail;
}
cur_id = buf.index;
printf("\n cur_id:%d\n",cur_id );
printf("\n buffers[cur_id].length :%d\n",buffers[cur_id].length );
for (x = 0; x < g_out_height; ++x) {
for (y = 0; y < g_out_width; ++y){
b = buffers[cur_id].start[x*g_out_width*4+4*y];
g = buffers[cur_id].start[x*g_out_width*4+4*y+1];
r = buffers[cur_id].start[x*g_out_width*4+4*y+2];
//r = buffers[cur_id].start[x*g_out_width*4+4*y];
//g = buffers[cur_id].start[x*g_out_width*4+4*y+1];
//b = buffers[cur_id].start[x*g_out_width*4+4*y+2];
y1 = buffers[cur_id].start[x*g_out_width*4+4*y];
v1 = buffers[cur_id].start[x*g_out_width*4+4*y+1];
u1 = buffers[cur_id].start[x*g_out_width*4+4*y+2];
y1 = buffers[cur_id].start[x*g_out_width*2+2*y];
if(y%2==0){
v1 = buffers[cur_id].start[x*g_out_width*2+2*y+1];
u1 = buffers[cur_id].start[x*g_out_width*2+2*y+3];
}else{
v1 = buffers[cur_id].start[x*g_out_width*2+2*y-1];
u1 = buffers[cur_id].start[x*g_out_width*2+2*y+1];
}
/*
y1 = buffers[cur_id].start[x*g_out_width*2+2*y+1];
if(y%2==0){
v1 = buffers[cur_id].start[x*g_out_width*2+2*y+2];
u1 = buffers[cur_id].start[x*g_out_width*2+2*y];
}else{
v1 = buffers[cur_id].start[x*g_out_width*2+2*y];
u1 = buffers[cur_id].start[x*g_out_width*2+2*y-2];
}*/
r = (y1) + 1.732 *(v1 - 128);
g = (y1) - 0.698 *(u1 - 128) - 0.703 *(v1 - 128);
b = (y1) + 1.37 *(u1 - 128) ;
r = (y1) + 1.403 *(v1 - 128);
g = (y1) - 0.343 *(u1 - 128) - 0.714 *(v1 - 128);
b = (y1) + 1.77 *(u1 - 128) ;
//r = (v1) ;
//g = (v1) ;
//b = (v1);
fb_mem[x*1920*4+4*y+0] = b;
fb_mem[x*1920*4+4*y+1] = g;
fb_mem[x*1920*4+4*y+2] = r;
}
}
printf("\n song VIDIOC_QBUF\n");
memset( &buf, 0, sizeof(buf));
memset(&planes, 0, sizeof(planes));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory = V4L2_MEMORY_MMAP;
buf.m.planes = &planes;
buf.m.planes->length = buffers[cur_id].length;
buf.length = 1; /* plane num */
buf.index = cur_id;
buf.m.planes->m.mem_offset = buffers[cur_id].offset;
if (ioctl(vd_fd, VIDIOC_QBUF, &buf) < 0){
printf("VIDIOC_QBUF error\n");
goto fail;
}
}
fail:
close(fb_fd);
close(vd_fd);
return 1;
}