2 * Linux audio/video grab interface
3 * Copyright (c) 2000 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <netinet/in.h>
22 #include <linux/videodev.h>
23 #include <linux/soundcard.h>
26 #include <sys/ioctl.h>
33 #include "mpegvideo.h"
35 long long gettime(void)
38 gettimeofday(&tv,NULL);
39 return (long long)tv.tv_sec * 1000000 + tv.tv_usec;
44 const char *v4l_device = "/dev/video";
46 static struct video_capability video_cap;
48 UINT8 *video_buf, *picture_buf;
49 struct video_mbuf gb_buffers;
50 struct video_mmap gb_buf;
51 struct video_audio audio;
57 int v4l_init(int rate, int width, int height)
61 video_fd = open(v4l_device, O_RDWR);
63 if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
69 ioctl(video_fd, VIDIOCGAUDIO, &audio);
70 audio.flags &= ~VIDEO_AUDIO_MUTE;
71 ioctl(video_fd, VIDIOCSAUDIO, &audio);
73 if (!(video_cap.type & VID_TYPE_CAPTURE)) {
74 /* try to use read based access */
75 struct video_window win;
85 ioctl(video_fd, VIDIOCSWIN, &win);
88 ioctl(video_fd, VIDIOCCAPTURE, &val);
89 video_buf = malloc( width * height * 2);
90 picture_buf = malloc( (width * height * 3) / 2);
95 if (ioctl(video_fd,VIDIOCGMBUF,&gb_buffers) < 0) {
96 perror("ioctl VIDIOCGMBUF");
99 video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
100 if ((unsigned char*)-1 == video_buf) {
105 time_frame = gettime();
107 /* start to grab the first frame */
108 gb_buf.frame = 1 - gb_frame;
109 gb_buf.height = height;
110 gb_buf.width = width;
111 gb_buf.format = VIDEO_PALETTE_YUV420P;
113 if (ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
115 fprintf(stderr,"Cannot Sync\n");
117 perror("VIDIOCMCAPTURE");
124 /* test with read call and YUV422 stream */
125 static int v4l_basic_read_picture(UINT8 *picture[3],
126 int width, int height,
130 UINT8 *p, *lum, *cb, *cr;
132 if (read(video_fd, video_buf, width * height * 2) < 0)
135 picture[0] = picture_buf;
136 picture[1] = picture_buf + width * height;
137 picture[2] = picture_buf + (width * height) + (width * height) / 4;
144 for(y=0;y<height;y+=2) {
145 for(x=0;x<width;x+=2) {
155 for(x=0;x<width;x+=2) {
165 static int v4l_mm_read_picture(UINT8 *picture[3],
166 int width, int height,
173 /* wait based on the frame rate */
174 time_frame += 1000000 / frame_rate;
177 } while (curtime < time_frame);
179 gb_buf.frame = gb_frame;
180 if (ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
182 fprintf(stderr,"Cannot Sync\n");
184 perror("VIDIOCMCAPTURE");
187 gb_frame = 1 - gb_frame;
189 if (ioctl(video_fd, VIDIOCSYNC, &gb_frame) < 0) {
190 if (errno != EAGAIN) {
191 perror("VIDIOCSYNC");
195 size = width * height;
196 ptr = video_buf + gb_buffers.offsets[gb_frame];
198 picture[1] = ptr + size;
199 picture[2] = ptr + size + (size / 4);
204 int v4l_read_picture(UINT8 *picture[3],
205 int width, int height,
209 return v4l_mm_read_picture(picture, width, height, picture_number);
211 return v4l_basic_read_picture(picture, width, height, picture_number);
215 /* open audio device */
216 int audio_open(int freq, int channels)
218 int audio_fd, tmp, err;
220 audio_fd = open("/dev/dsp",O_RDONLY);
221 if (audio_fd == -1) {
225 /* non blocking mode */
226 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
229 tmp=(NB_FRAGMENTS << 16) | FRAGMENT_BITS;
230 err=ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
232 perror("SNDCTL_DSP_SETFRAGMENT");
236 /* always set to this size */
237 /* XXX: incorrect if big endian */
239 err=ioctl(audio_fd,SNDCTL_DSP_SETFMT,&tmp);
241 perror("SNDCTL_DSP_SETFMT");
244 tmp= (channels == 2);
245 err=ioctl(audio_fd,SNDCTL_DSP_STEREO,&tmp);
247 perror("SNDCTL_DSP_STEREO");
252 err=ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
254 perror("SNDCTL_DSP_SPEED");