buflen = 1024 bytes + added total and kb/s output for stream
[my-code/ivac.git] / v4lapi.c
1 /*
2  * v4lapi.c -- api to the video4linux interface
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include <linux/videodev.h>
17
18 int v4l_init(char *dev) {
19         int fd,i,res;
20         unsigned char buf[500*500];
21         struct video_capability vc;
22         struct video_buffer vb;
23         struct video_window vw;
24         struct video_picture vp;
25         
26         if((fd=open(dev,O_RDONLY))<0) {
27                 puts("open failed");
28                 return -1;
29         }
30
31         if(ioctl(fd,VIDIOCGCAP,&vc)<0) {
32                 puts("ioctl VIDIOCGCAP failed");
33                 return -1;
34         }
35
36         printf("video device information:\n");
37         printf("-------------------------\n\n");
38         printf("interface name: %s\n\n",vc.name);
39         printf("type:\n");
40         printf("capture:\t %c\n",(vc.type&1)?'y':'n');
41         printf("tuner:\t\t %c\n",((vc.type>>1)&1)?'y':'n');
42         printf("teletext:\t %c\n",((vc.type>>2)&1)?'y':'n');
43         printf("overlay:\t %c\n",((vc.type>>3)&1)?'y':'n');
44         printf("chromakey:\t %c\n",((vc.type>>4)&1)?'y':'n');
45         printf("clipping:\t %c\n",((vc.type>>5)&1)?'y':'n');
46         printf("fb memory:\t %c\n",((vc.type>>6)&1)?'y':'n');
47         printf("scalable:\t %c\n",((vc.type>>7)&1)?'y':'n');
48         printf("monochrome:\t %c\n",((vc.type>>8)&1)?'y':'n');
49         printf("subcapture:\t %c\n",((vc.type>>9)&1)?'y':'n');
50         printf("decode: ");
51         if((vc.type>>10)&1) printf("mpeg ");
52         if((vc.type>>12)&1) printf("mjpeg ");
53         printf("\n");
54         printf("encode: ");
55         if((vc.type>>11)&1) printf("mpeg ");
56         if((vc.type>>13)&1) printf("mjpeg ");
57         printf("\n\n");
58         printf("channels:\t %d\n",vc.channels);
59         printf("audios:\t\t %d\n",vc.audios);
60         printf("width:\t\t %d - %d\n",vc.minwidth,vc.maxwidth);
61         printf("height:\t\t %d - %d\n\n",vc.minheight,vc.maxheight);
62
63         if(ioctl(fd,VIDIOCGFBUF,&vb)<0) {
64                 puts("direct framebuffer access unsupported");
65                 puts("");
66         }
67         else {
68                 printf("\n");
69                 printf("framebuffer:\n");
70                 printf("buffer addr:\t %d\n",vb.base);
71                 printf("h: %d - w: %d - d: %d - bpl: %d\n\n",vb.height,vb.width,vb.depth,vb.bytesperline);
72         }
73
74         memset(&vw,0,sizeof(struct video_window));
75         vw.x=0;
76         vw.y=0;
77         vw.width=vc.maxwidth;
78         vw.height=vc.maxheight;
79
80         if(ioctl(fd,VIDIOCSWIN,&vw)<0) {
81                 puts("ioctl VIDIOCSWIN failed");
82                 return -1;
83         }
84
85         if(ioctl(fd,VIDIOCGWIN,&vw)<0) {
86                 puts("ioctl VIDIOCGWIN failed");
87                 return -1;
88         }
89
90         printf("real capture area:\n");
91         printf("x: %u - y: %u\n",vw.x,vw.y);
92         printf("w: %u - h: %u\n",vw.width,vw.height);
93         printf("flags: %08x\n",vw.flags);
94         
95         memset(&vp,0,sizeof(struct video_picture));
96         
97         if(ioctl(fd,VIDIOCGPICT,&vp)<0) {
98                 puts("ioctl VIDIOCGPICT failed");
99                 return -1;
100         }
101
102         printf("picture: b: %04x - h: %04x - c: %04x - c: %04x - w: %04x - d: %04x - p: %04x\n",vp.brightness,vp.hue,vp.colour,vp.contrast,vp.whiteness,vp.depth,vp.palette);
103         
104         if(ioctl(fd,VIDIOCSPICT,&vp)<0) {
105                 puts("ioctl VIDIOCSPICT failed");
106                 return -1;
107         }
108
109         if((res=read(fd,buf,(500*500)))<0) {
110                 puts("read failed");
111                 return -1;
112         }
113         printf("debug: wrote %d bytes:\n",i);
114         for(i=0;i<res;i++) printf("%c ",buf[i]);
115         printf("\n");
116
117         return 1;
118 }