Statistics
| Branch: | Revision:

ffmpeg / libav / grab.c @ 5a56c87c

History | View | Annotate | Download (8.08 KB)

1
/*
2
 * Linux video grab interface
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 *
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.
9
 *
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.
14
 *
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.
18
 */
19
#include "avformat.h"
20
#include <linux/videodev.h>
21
#include <unistd.h>
22
#include <fcntl.h>
23
#include <sys/ioctl.h>
24
#include <sys/mman.h>
25
#include <sys/time.h>
26

    
27

    
28
typedef struct {
29
    int fd;
30
    int frame_format; /* see VIDEO_PALETTE_xxx */
31
    int use_mmap;
32
    int width, height;
33
    float rate;
34
    INT64 time_frame;
35
} VideoData;
36

    
37
const char *v4l_device = "/dev/video";
38

    
39
/* XXX: move all that to the context */
40

    
41
static struct video_capability  video_cap;
42
static UINT8 *video_buf;
43
static struct video_mbuf gb_buffers;
44
static struct video_mmap gb_buf;
45
static struct video_audio audio, audio_saved;
46
static int gb_frame = 0;
47

    
48
static int v4l_init(URLContext *h)
49
{
50
    VideoData *s = h->priv_data;
51
    int width, height;
52
    int ret;
53
    int video_fd, frame_size;
54
    
55
    width = s->width;
56
    height = s->height;
57

    
58
    video_fd = open(v4l_device, O_RDWR);
59
    if (video_fd < 0) {
60
        perror(v4l_device);
61
        return -EIO;
62
    }
63
    
64
    if (ioctl(video_fd,VIDIOCGCAP,&video_cap) < 0) {
65
        perror("VIDIOCGCAP");
66
        goto fail;
67
    }
68

    
69
    if (!(video_cap.type & VID_TYPE_CAPTURE)) {
70
        fprintf(stderr, "Fatal: grab device does not handle capture\n");
71
        goto fail;
72
    }
73
    
74
    /* unmute audio */
75
    ioctl(video_fd, VIDIOCGAUDIO, &audio);
76
    memcpy(&audio_saved, &audio, sizeof(audio));
77
    audio.flags &= ~VIDEO_AUDIO_MUTE;
78
    ioctl(video_fd, VIDIOCSAUDIO, &audio);
79

    
80
    ret = ioctl(video_fd,VIDIOCGMBUF,&gb_buffers);
81
    if (ret < 0) {
82
        /* try to use read based access */
83
        struct video_window win;
84
        struct video_picture pict;
85
        int val;
86

    
87
        win.x = 0;
88
        win.y = 0;
89
        win.width = width;
90
        win.height = height;
91
        win.chromakey = -1;
92
        win.flags = 0;
93

    
94
        ioctl(video_fd, VIDIOCSWIN, &win);
95

    
96
        ioctl(video_fd, VIDIOCGPICT, &pict);
97
#if 0
98
        printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
99
               pict.colour,
100
               pict.hue,
101
               pict.brightness,
102
               pict.contrast,
103
               pict.whiteness);
104
#endif        
105
        /* try to choose a suitable video format */
106
        pict.palette=VIDEO_PALETTE_YUV420P;
107
        ret = ioctl(video_fd, VIDIOCSPICT, &pict);
108
        if (ret < 0) {
109
            pict.palette=VIDEO_PALETTE_YUV422;
110
            ret = ioctl(video_fd, VIDIOCSPICT, &pict);
111
            if (ret < 0) {
112
                pict.palette=VIDEO_PALETTE_RGB24;
113
                ret = ioctl(video_fd, VIDIOCSPICT, &pict);
114
                if (ret < 0) 
115
                    goto fail1;
116
            }
117
        }
118

    
119
        s->frame_format = pict.palette;
120

    
121
        val = 1;
122
        ioctl(video_fd, VIDIOCCAPTURE, &val);
123

    
124
        s->time_frame = gettime();
125
        s->use_mmap = 0;
126
    } else {
127
        video_buf = mmap(0,gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
128
        if ((unsigned char*)-1 == video_buf) {
129
            perror("mmap");
130
            goto fail;
131
        }
132
        gb_frame = 0;
133
        s->time_frame = gettime();
134
        
135
        /* start to grab the first frame */
136
        gb_buf.frame = (gb_frame + 1) % gb_buffers.frames;
137
        gb_buf.height = height;
138
        gb_buf.width = width;
139
        gb_buf.format = VIDEO_PALETTE_YUV420P;
140
        
141
        ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
142
        if (ret < 0 && errno != EAGAIN) {
143
            /* try YUV422 */
144
            gb_buf.format = VIDEO_PALETTE_YUV422;
145
            
146
            ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
147
            if (ret < 0 && errno != EAGAIN) {
148
                /* try RGB24 */
149
                gb_buf.format = VIDEO_PALETTE_RGB24;
150
                ret = ioctl(video_fd, VIDIOCMCAPTURE, &gb_buf);
151
            }
152
        }
153
        if (ret < 0) {
154
            if (errno != EAGAIN) {
155
            fail1:
156
                fprintf(stderr, "Fatal: grab device does not support suitable format\n");
157
            } else {
158
                fprintf(stderr,"Fatal: grab device does not receive any video signal\n");
159
            }
160
            goto fail;
161
        }
162
        s->frame_format = gb_buf.format;
163
        s->use_mmap = 1;
164
    }
165

    
166
    switch(s->frame_format) {
167
    case VIDEO_PALETTE_YUV420P:
168
        frame_size = (width * height * 3) / 2;
169
        break;
170
    case VIDEO_PALETTE_YUV422:
171
        frame_size = width * height * 2;
172
        break;
173
    case VIDEO_PALETTE_RGB24:
174
        frame_size = width * height * 3;
175
        break;
176
    default:
177
        goto fail;
178
    }
179
    s->fd = video_fd;
180
    h->packet_size = frame_size;
181
    return 0;
182
 fail:
183
    close(video_fd);
184
    return -EIO;
185
}
186

    
187
static int v4l_mm_read_picture(URLContext *h, UINT8 *buf)
188
{
189
    VideoData *s = h->priv_data;
190
    UINT8 *ptr;
191

    
192
    gb_buf.frame = gb_frame;
193
    if (ioctl(s->fd, VIDIOCMCAPTURE, &gb_buf) < 0) {
194
        if (errno == EAGAIN)
195
            fprintf(stderr,"Cannot Sync\n");
196
        else
197
            perror("VIDIOCMCAPTURE");
198
        return -EIO;
199
    }
200
    gb_frame = (gb_frame + 1) % gb_buffers.frames;
201

    
202
    while (ioctl(s->fd, VIDIOCSYNC, &gb_frame) < 0 &&
203
           (errno == EAGAIN || errno == EINTR));
204

    
205
    ptr = video_buf + gb_buffers.offsets[gb_frame];
206
    memcpy(buf, ptr, h->packet_size);
207
    return h->packet_size;
208
}
209

    
210
/* note: we support only one picture read at a time */
211
static int video_read(URLContext *h, UINT8 *buf, int size)
212
{
213
    VideoData *s = h->priv_data;
214
    INT64 curtime;
215

    
216
    if (size != h->packet_size)
217
        return -EINVAL;
218

    
219
    /* wait based on the frame rate */
220
    s->time_frame += (int)(1000000 / s->rate);
221
    do {
222
        curtime = gettime();
223
    } while (curtime < s->time_frame);
224

    
225
    /* read one frame */
226
    if (s->use_mmap) {
227
        return v4l_mm_read_picture(h, buf);
228
    } else {
229
        if (read(s->fd, buf, size) != size)
230
            return -EIO;
231
        return h->packet_size;
232
    }
233
}
234

    
235
static int video_get_format(URLContext *h, URLFormat *f)
236
{
237
    VideoData *s = h->priv_data;
238

    
239
    f->width = s->width;
240
    f->height = s->height;
241
    f->frame_rate = (int)(s->rate * FRAME_RATE_BASE);
242
    strcpy(f->format_name, "rawvideo");
243

    
244
    switch(s->frame_format) {
245
    case VIDEO_PALETTE_YUV420P:
246
        f->pix_fmt = PIX_FMT_YUV420P;
247
        break;
248
    case VIDEO_PALETTE_YUV422:
249
        f->pix_fmt = PIX_FMT_YUV422;
250
        break;
251
    case VIDEO_PALETTE_RGB24:
252
        f->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
253
        break;
254
    default:
255
        abort();
256
    }
257
    return 0;
258
}
259

    
260
/* URI syntax: 'video:width,height,rate'
261
 */
262
static int video_open(URLContext *h, const char *uri, int flags)
263
{
264
    VideoData *s;
265
    const char *p;
266
    int width, height;
267
    int ret;
268
    float rate;
269

    
270
    /* extract parameters */
271
    p = uri;
272
    strstart(p, "video:", &p);
273
    width = strtol(p, (char **)&p, 0);
274
    if (width <= 0)
275
        return -EINVAL;
276
    if (*p == ',')
277
        p++;
278
    height = strtol(p, (char **)&p, 0);
279
    if (height <= 0)
280
        return -EINVAL;
281
    if (*p == ',')
282
        p++;
283
    rate = strtod(p, (char **)&p);
284
    if (rate <= 0)
285
        return -EINVAL;
286

    
287
    s = malloc(sizeof(VideoData));
288
    if (!s)
289
        return -ENOMEM;
290
    h->priv_data = s;
291
    h->is_streamed = 1;
292
    s->width = width;
293
    s->height = height;
294
    s->rate = rate;
295
    ret = v4l_init(h);
296
    if (ret)
297
        free(s);
298
    return ret;
299
}
300

    
301
static int video_close(URLContext *h)
302
{
303
    VideoData *s = h->priv_data;
304

    
305
    /* restore audio settings */
306
    ioctl(s->fd, VIDIOCSAUDIO, &audio_saved);
307

    
308
    close(s->fd);
309
    free(s);
310
    return 0;
311
}
312

    
313
URLProtocol video_protocol = {
314
    "video",
315
    video_open,
316
    video_read,
317
    NULL,
318
    NULL, /* seek */
319
    video_close,
320
    video_get_format,
321
};