Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l.c @ 7c1d608e

History | View | Annotate | Download (10.7 KB)

1
/*
2
 * Linux video grab interface
3
 * Copyright (c) 2000,2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "config.h"
23
#include "libavformat/avformat.h"
24
#include "libavcodec/dsputil.h"
25
#include <unistd.h>
26
#include <fcntl.h>
27
#include <sys/ioctl.h>
28
#include <sys/mman.h>
29
#include <sys/time.h>
30
#define _LINUX_TIME_H 1
31
#include <linux/videodev.h>
32
#include <time.h>
33

    
34
typedef struct {
35
    int fd;
36
    int frame_format; /* see VIDEO_PALETTE_xxx */
37
    int use_mmap;
38
    int width, height;
39
    int frame_rate;
40
    int frame_rate_base;
41
    int64_t time_frame;
42
    int frame_size;
43
    struct video_capability video_cap;
44
    struct video_audio audio_saved;
45
    uint8_t *video_buf;
46
    struct video_mbuf gb_buffers;
47
    struct video_mmap gb_buf;
48
    int gb_frame;
49
} VideoData;
50

    
51
static const struct {
52
    int palette;
53
    int depth;
54
    enum PixelFormat pix_fmt;
55
} video_formats [] = {
56
    {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
57
    {.palette = VIDEO_PALETTE_YUV422,  .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
58
    {.palette = VIDEO_PALETTE_UYVY,    .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
59
    {.palette = VIDEO_PALETTE_YUYV,    .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
60
    /* NOTE: v4l uses BGR24, not RGB24 */
61
    {.palette = VIDEO_PALETTE_RGB24,   .depth = 24, .pix_fmt = PIX_FMT_BGR24   },
62
    {.palette = VIDEO_PALETTE_RGB565,  .depth = 16, .pix_fmt = PIX_FMT_BGR565  },
63
    {.palette = VIDEO_PALETTE_GREY,    .depth = 8,  .pix_fmt = PIX_FMT_GRAY8   },
64
};
65

    
66

    
67
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
68
{
69
    VideoData *s = s1->priv_data;
70
    AVStream *st;
71
    int width, height;
72
    int video_fd, frame_size;
73
    int ret, frame_rate, frame_rate_base;
74
    int desired_palette, desired_depth;
75
    struct video_tuner tuner;
76
    struct video_audio audio;
77
    struct video_picture pict;
78
    int j;
79
    int vformat_num = sizeof(video_formats) / sizeof(video_formats[0]);
80

    
81
    if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
82
        av_log(s1, AV_LOG_ERROR, "Bad capture size (%dx%d) or wrong time base (%d)\n",
83
            ap->width, ap->height, ap->time_base.den);
84

    
85
        return -1;
86
    }
87

    
88
    width = ap->width;
89
    height = ap->height;
90
    frame_rate      = ap->time_base.den;
91
    frame_rate_base = ap->time_base.num;
92

    
93
    if((unsigned)width > 32767 || (unsigned)height > 32767) {
94
        av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
95
            width, height);
96

    
97
        return -1;
98
    }
99

    
100
    st = av_new_stream(s1, 0);
101
    if (!st)
102
        return AVERROR(ENOMEM);
103
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
104

    
105
    s->width = width;
106
    s->height = height;
107
    s->frame_rate      = frame_rate;
108
    s->frame_rate_base = frame_rate_base;
109

    
110
    video_fd = open(s1->filename, O_RDWR);
111
    if (video_fd < 0) {
112
        av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
113
        goto fail;
114
    }
115

    
116
    if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
117
        av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
118
        goto fail;
119
    }
120

    
121
    if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
122
        av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
123
        goto fail;
124
    }
125

    
126
    desired_palette = -1;
127
    desired_depth = -1;
128
    for (j = 0; j < vformat_num; j++) {
129
        if (ap->pix_fmt == video_formats[j].pix_fmt) {
130
            desired_palette = video_formats[j].palette;
131
            desired_depth = video_formats[j].depth;
132
            break;
133
        }
134
    }
135

    
136
    /* set tv standard */
137
    if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
138
        if (!strcasecmp(ap->standard, "pal"))
139
            tuner.mode = VIDEO_MODE_PAL;
140
        else if (!strcasecmp(ap->standard, "secam"))
141
            tuner.mode = VIDEO_MODE_SECAM;
142
        else
143
            tuner.mode = VIDEO_MODE_NTSC;
144
        ioctl(video_fd, VIDIOCSTUNER, &tuner);
145
    }
146

    
147
    /* unmute audio */
148
    audio.audio = 0;
149
    ioctl(video_fd, VIDIOCGAUDIO, &audio);
150
    memcpy(&s->audio_saved, &audio, sizeof(audio));
151
    audio.flags &= ~VIDEO_AUDIO_MUTE;
152
    ioctl(video_fd, VIDIOCSAUDIO, &audio);
153

    
154
    ioctl(video_fd, VIDIOCGPICT, &pict);
155
#if 0
156
    printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
157
           pict.colour,
158
           pict.hue,
159
           pict.brightness,
160
           pict.contrast,
161
           pict.whiteness);
162
#endif
163
    /* try to choose a suitable video format */
164
    pict.palette = desired_palette;
165
    pict.depth= desired_depth;
166
    if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
167
        for (j = 0; j < vformat_num; j++) {
168
            pict.palette = video_formats[j].palette;
169
            pict.depth = video_formats[j].depth;
170
            if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
171
                break;
172
        }
173
        if (j >= vformat_num)
174
            goto fail1;
175
    }
176

    
177
    ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
178
    if (ret < 0) {
179
        /* try to use read based access */
180
        struct video_window win;
181
        int val;
182

    
183
        win.x = 0;
184
        win.y = 0;
185
        win.width = width;
186
        win.height = height;
187
        win.chromakey = -1;
188
        win.flags = 0;
189

    
190
        ioctl(video_fd, VIDIOCSWIN, &win);
191

    
192
        s->frame_format = pict.palette;
193

    
194
        val = 1;
195
        ioctl(video_fd, VIDIOCCAPTURE, &val);
196

    
197
        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
198
        s->use_mmap = 0;
199
    } else {
200
        s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
201
        if ((unsigned char*)-1 == s->video_buf) {
202
            s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
203
            if ((unsigned char*)-1 == s->video_buf) {
204
                av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
205
                goto fail;
206
            }
207
        }
208
        s->gb_frame = 0;
209
        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
210

    
211
        /* start to grab the first frame */
212
        s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
213
        s->gb_buf.height = height;
214
        s->gb_buf.width = width;
215
        s->gb_buf.format = pict.palette;
216

    
217
        ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
218
        if (ret < 0) {
219
            if (errno != EAGAIN) {
220
            fail1:
221
                av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
222
            } else {
223
                av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
224
            }
225
            goto fail;
226
        }
227
        for (j = 1; j < s->gb_buffers.frames; j++) {
228
          s->gb_buf.frame = j;
229
          ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
230
        }
231
        s->frame_format = s->gb_buf.format;
232
        s->use_mmap = 1;
233
    }
234

    
235
    for (j = 0; j < vformat_num; j++) {
236
        if (s->frame_format == video_formats[j].palette) {
237
            frame_size = width * height * video_formats[j].depth / 8;
238
            st->codec->pix_fmt = video_formats[j].pix_fmt;
239
            break;
240
        }
241
    }
242

    
243
    if (j >= vformat_num)
244
        goto fail;
245

    
246
    s->fd = video_fd;
247
    s->frame_size = frame_size;
248

    
249
    st->codec->codec_type = CODEC_TYPE_VIDEO;
250
    st->codec->codec_id = CODEC_ID_RAWVIDEO;
251
    st->codec->width = width;
252
    st->codec->height = height;
253
    st->codec->time_base.den      = frame_rate;
254
    st->codec->time_base.num = frame_rate_base;
255
    st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
256

    
257
    return 0;
258
 fail:
259
    if (video_fd >= 0)
260
        close(video_fd);
261
    return AVERROR(EIO);
262
}
263

    
264
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
265
{
266
    uint8_t *ptr;
267

    
268
    while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
269
           (errno == EAGAIN || errno == EINTR));
270

    
271
    ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
272
    memcpy(buf, ptr, s->frame_size);
273

    
274
    /* Setup to capture the next frame */
275
    s->gb_buf.frame = s->gb_frame;
276
    if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
277
        if (errno == EAGAIN)
278
            av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
279
        else
280
            av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
281
        return AVERROR(EIO);
282
    }
283

    
284
    /* This is now the grabbing frame */
285
    s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
286

    
287
    return s->frame_size;
288
}
289

    
290
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
291
{
292
    VideoData *s = s1->priv_data;
293
    int64_t curtime, delay;
294
    struct timespec ts;
295

    
296
    /* Calculate the time of the next frame */
297
    s->time_frame += INT64_C(1000000);
298

    
299
    /* wait based on the frame rate */
300
    for(;;) {
301
        curtime = av_gettime();
302
        delay = s->time_frame  * s->frame_rate_base / s->frame_rate - curtime;
303
        if (delay <= 0) {
304
            if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
305
                /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
306
                s->time_frame += INT64_C(1000000);
307
            }
308
            break;
309
        }
310
        ts.tv_sec = delay / 1000000;
311
        ts.tv_nsec = (delay % 1000000) * 1000;
312
        nanosleep(&ts, NULL);
313
    }
314

    
315
    if (av_new_packet(pkt, s->frame_size) < 0)
316
        return AVERROR(EIO);
317

    
318
    pkt->pts = curtime;
319

    
320
    /* read one frame */
321
    if (s->use_mmap) {
322
        return v4l_mm_read_picture(s, pkt->data);
323
    } else {
324
        if (read(s->fd, pkt->data, pkt->size) != pkt->size)
325
            return AVERROR(EIO);
326
        return s->frame_size;
327
    }
328
}
329

    
330
static int grab_read_close(AVFormatContext *s1)
331
{
332
    VideoData *s = s1->priv_data;
333

    
334
    if (s->use_mmap)
335
        munmap(s->video_buf, s->gb_buffers.size);
336

    
337
    /* mute audio. we must force it because the BTTV driver does not
338
       return its state correctly */
339
    s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
340
    ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
341

    
342
    close(s->fd);
343
    return 0;
344
}
345

    
346
AVInputFormat v4l_demuxer = {
347
    "video4linux",
348
    NULL_IF_CONFIG_SMALL("video grab"),
349
    sizeof(VideoData),
350
    NULL,
351
    grab_read_header,
352
    grab_read_packet,
353
    grab_read_close,
354
    .flags = AVFMT_NOFILE,
355
};