1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/*
* Linux audio/video grab interface
* Copyright (c) 2000 Gerard Lantau.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <linux/videodev.h>
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/time.h>
#include <getopt.h>
#include "mpegenc.h"
#include "mpegvideo.h"
long long gettime(void)
{
struct timeval tv;
gettimeofday(&tv,NULL);
return (long long)tv.tv_sec * 1000000 + tv.tv_usec;
}
/* v4l capture */
const char *v4l_device = "/dev/video";
static struct video_capability video_cap;
int video_fd = -1;
UINT8 *video_buf, *picture_buf;
struct video_mbuf gb_buffers;
struct video_mmap gb_buf;
struct video_audio audio;
int gb_frame = 0;
long long time_frame;
int frame_rate;
int use_mmap = 0;
int v4l_init(int rate, int width, int height)
{
frame_rate = rate;
video_fd = open(v4l_device, O_RDWR);
if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
perror("VIDIOCGCAP");
return -1;
}
/* unmute audio */
ioctl(video_fd, VIDIOCGAUDIO, &audio);
audio.flags &= ~VIDEO_AUDIO_MUTE;
ioctl(video_fd, VIDIOCSAUDIO, &audio);
if (!(video_cap.type & VID_TYPE_CAPTURE)) {
/* try to use read based access */
struct video_window win;
int val;
win.x = 0;
win.y = 0;
win.width = width;
win.height = height;
win.chromakey = -1;
win.flags = 0;
ioctl(video_fd, VIDIOCSWIN, &win);
val = 1;
ioctl(video_fd, VIDIOCCAPTURE, &val);
video_buf = malloc( width * height * 2);
picture_buf = malloc( (width * height * 3) / 2);
use_mmap = 0;
return 0;
}
if (ioctl(video_fd,VIDIOCGMBUF,&gb_buffers) < 0) {
perror("ioctl VIDIOCGMBUF");
}
video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
if ((unsigned char*)-1 == video_buf) {
perror("mmap");
return -1;
}
gb_frame = 0;
time_frame = gettime();
/* start to grab the first frame */
gb_buf.frame = 1 - gb_frame;
gb_buf.height = height;
gb_buf.width = width;
gb_buf.format = VIDEO_PALETTE_YUV420P;
if (ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
if (errno == EAGAIN)
fprintf(stderr,"Cannot Sync\n");
else
perror("VIDIOCMCAPTURE");
return -1;
}
use_mmap = 1;
return 0;
}
/* test with read call and YUV422 stream */
static int v4l_basic_read_picture(UINT8 *picture[3],
int width, int height,
int picture_number)
{
int x, y;
UINT8 *p, *lum, *cb, *cr;
if (read(video_fd, video_buf, width * height * 2) < 0)
perror("read");
picture[0] = picture_buf;
picture[1] = picture_buf + width * height;
picture[2] = picture_buf + (width * height) + (width * height) / 4;
/* XXX: optimize */
lum = picture[0];
cb = picture[1];
cr = picture[2];
p = video_buf;
for(y=0;y<height;y+=2) {
for(x=0;x<width;x+=2) {
lum[0] = p[0];
cb[0] = p[1];
lum[1] = p[2];
cr[0] = p[3];
p += 4;
lum += 2;
cb++;
cr++;
}
for(x=0;x<width;x+=2) {
lum[0] = p[0];
lum[1] = p[2];
p += 4;
lum += 2;
}
}
return 0;
}
static int v4l_mm_read_picture(UINT8 *picture[3],
int width, int height,
int picture_number)
{
UINT8 *ptr;
int size;
long long curtime;
/* wait based on the frame rate */
time_frame += 1000000 / frame_rate;
do {
curtime = gettime();
} while (curtime < time_frame);
gb_buf.frame = gb_frame;
if (ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
if (errno == EAGAIN)
fprintf(stderr,"Cannot Sync\n");
else
perror("VIDIOCMCAPTURE");
return -1;
}
gb_frame = 1 - gb_frame;
if (ioctl(video_fd, VIDIOCSYNC, &gb_frame) < 0) {
if (errno != EAGAIN) {
perror("VIDIOCSYNC");
}
}
size = width * height;
ptr = video_buf + gb_buffers.offsets[gb_frame];
picture[0] = ptr;
picture[1] = ptr + size;
picture[2] = ptr + size + (size / 4);
return 0;
}
int v4l_read_picture(UINT8 *picture[3],
int width, int height,
int picture_number)
{
if (use_mmap) {
return v4l_mm_read_picture(picture, width, height, picture_number);
} else {
return v4l_basic_read_picture(picture, width, height, picture_number);
}
}
/* open audio device */
int audio_open(int freq, int channels)
{
int audio_fd, tmp, err;
audio_fd = open("/dev/dsp",O_RDONLY);
if (audio_fd == -1) {
perror("/dev/dsp");
return -1;
}
/* non blocking mode */
fcntl(audio_fd, F_SETFL, O_NONBLOCK);
#if 0
tmp=(NB_FRAGMENTS << 16) | FRAGMENT_BITS;
err=ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
if (err < 0) {
perror("SNDCTL_DSP_SETFRAGMENT");
}
#endif
/* always set to this size */
/* XXX: incorrect if big endian */
tmp=AFMT_S16_LE;
err=ioctl(audio_fd,SNDCTL_DSP_SETFMT,&tmp);
if (err < 0) {
perror("SNDCTL_DSP_SETFMT");
}
tmp= (channels == 2);
err=ioctl(audio_fd,SNDCTL_DSP_STEREO,&tmp);
if (err < 0) {
perror("SNDCTL_DSP_STEREO");
}
/* should be last */
tmp = freq;
err=ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
if (err < 0) {
perror("SNDCTL_DSP_SPEED");
}
return audio_fd;
}
|