Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l.c @ ca402f32

History | View | Annotate | Download (10.8 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Linux video grab interface
3 406792e7 Diego Biurrun
 * Copyright (c) 2000,2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 b0067549 Diego Biurrun
22 e9d4b8a5 Michael Niedermayer
#undef __STRICT_ANSI__ //workaround due to broken kernel headers
23 b0067549 Diego Biurrun
#include "config.h"
24 9580ba26 Stefano Sabatini
#include "libavutil/rational.h"
25 737eb597 Reinhard Tartler
#include "libavutil/imgutils.h"
26 245976da Diego Biurrun
#include "libavformat/avformat.h"
27
#include "libavcodec/dsputil.h"
28 de6d9b64 Fabrice Bellard
#include <unistd.h>
29
#include <fcntl.h>
30
#include <sys/ioctl.h>
31
#include <sys/mman.h>
32
#include <sys/time.h>
33 72a476c1 Michael Niedermayer
#define _LINUX_TIME_H 1
34
#include <linux/videodev.h>
35 608d0dee Zdenek Kabelac
#include <time.h>
36 ea452b54 Aurelien Jacobs
#include <strings.h>
37 de6d9b64 Fabrice Bellard
38
typedef struct {
39
    int fd;
40
    int frame_format; /* see VIDEO_PALETTE_xxx */
41
    int use_mmap;
42 9580ba26 Stefano Sabatini
    AVRational time_base;
43 0c1a9eda Zdenek Kabelac
    int64_t time_frame;
44 4972b26f Fabrice Bellard
    int frame_size;
45 8949367e Fabrice Bellard
    struct video_capability video_cap;
46
    struct video_audio audio_saved;
47 406c5808 Stefano Sabatini
    struct video_window video_win;
48 0c1a9eda Zdenek Kabelac
    uint8_t *video_buf;
49 8949367e Fabrice Bellard
    struct video_mbuf gb_buffers;
50
    struct video_mmap gb_buf;
51
    int gb_frame;
52 de6d9b64 Fabrice Bellard
} VideoData;
53
54 1411f55b Diego Pettenò
static const struct {
55 c7c64e9e Marc Hoffman
    int palette;
56
    int depth;
57
    enum PixelFormat pix_fmt;
58
} video_formats [] = {
59
    {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
60
    {.palette = VIDEO_PALETTE_YUV422,  .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
61 69621690 Marc Hoffman
    {.palette = VIDEO_PALETTE_UYVY,    .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
62
    {.palette = VIDEO_PALETTE_YUYV,    .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
63 c7c64e9e Marc Hoffman
    /* NOTE: v4l uses BGR24, not RGB24 */
64
    {.palette = VIDEO_PALETTE_RGB24,   .depth = 24, .pix_fmt = PIX_FMT_BGR24   },
65 69621690 Marc Hoffman
    {.palette = VIDEO_PALETTE_RGB565,  .depth = 16, .pix_fmt = PIX_FMT_BGR565  },
66 c7c64e9e Marc Hoffman
    {.palette = VIDEO_PALETTE_GREY,    .depth = 8,  .pix_fmt = PIX_FMT_GRAY8   },
67
};
68
69
70 4972b26f Fabrice Bellard
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
71 de6d9b64 Fabrice Bellard
{
72 c9a65ca8 Fabrice Bellard
    VideoData *s = s1->priv_data;
73 4972b26f Fabrice Bellard
    AVStream *st;
74 f233d348 Stefano Sabatini
    int video_fd;
75 76a77ff2 Luca Abeni
    int desired_palette, desired_depth;
76 e3ee3283 Alex Beregszaszi
    struct video_tuner tuner;
77 8949367e Fabrice Bellard
    struct video_audio audio;
78 c787ba85 Flavio Pimentel Duarte
    struct video_picture pict;
79 7aae3168 Michael Niedermayer
    int j;
80 37d3e066 Aurelien Jacobs
    int vformat_num = FF_ARRAY_ELEMS(video_formats);
81 4972b26f Fabrice Bellard
82 e78d651f Stefano Sabatini
    if (ap->time_base.den <= 0) {
83
        av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
84 4972b26f Fabrice Bellard
        return -1;
85 11b9c0f4 Luca Abeni
    }
86 9580ba26 Stefano Sabatini
    s->time_base = ap->time_base;
87 115329f1 Diego Biurrun
88 406c5808 Stefano Sabatini
    s->video_win.width = ap->width;
89
    s->video_win.height = ap->height;
90 115329f1 Diego Biurrun
91 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s1, 0);
92
    if (!st)
93 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
94 0a7b514f Luca Abeni
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
95 4972b26f Fabrice Bellard
96 cc58300e Ramiro Polla
    video_fd = open(s1->filename, O_RDWR);
97 de6d9b64 Fabrice Bellard
    if (video_fd < 0) {
98 9f74582c Luca Abeni
        av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
99 4972b26f Fabrice Bellard
        goto fail;
100 de6d9b64 Fabrice Bellard
    }
101 115329f1 Diego Biurrun
102 c40432d3 Stefano Sabatini
    if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
103 9f74582c Luca Abeni
        av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
104 de6d9b64 Fabrice Bellard
        goto fail;
105
    }
106
107 8949367e Fabrice Bellard
    if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
108 bb270c08 Diego Biurrun
        av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
109 de6d9b64 Fabrice Bellard
        goto fail;
110
    }
111 4606ac8d Zdenek Kabelac
112 7274a480 Stefano Sabatini
    /* no values set, autodetect them */
113
    if (s->video_win.width <= 0 || s->video_win.height <= 0) {
114
        if (ioctl(video_fd, VIDIOCGWIN, &s->video_win, sizeof(s->video_win)) < 0) {
115
            av_log(s1, AV_LOG_ERROR, "VIDIOCGWIN: %s\n", strerror(errno));
116
            goto fail;
117
        }
118
    }
119
120 e16f217c Stefano Sabatini
    if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
121 5d91e928 Stefano Sabatini
        return -1;
122
123 4606ac8d Zdenek Kabelac
    desired_palette = -1;
124 76a77ff2 Luca Abeni
    desired_depth = -1;
125 c7c64e9e Marc Hoffman
    for (j = 0; j < vformat_num; j++) {
126
        if (ap->pix_fmt == video_formats[j].pix_fmt) {
127
            desired_palette = video_formats[j].palette;
128
            desired_depth = video_formats[j].depth;
129
            break;
130
        }
131 115329f1 Diego Biurrun
    }
132 e3ee3283 Alex Beregszaszi
133
    /* set tv standard */
134
    if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
135 bb270c08 Diego Biurrun
        if (!strcasecmp(ap->standard, "pal"))
136
            tuner.mode = VIDEO_MODE_PAL;
137
        else if (!strcasecmp(ap->standard, "secam"))
138
            tuner.mode = VIDEO_MODE_SECAM;
139
        else
140
            tuner.mode = VIDEO_MODE_NTSC;
141
        ioctl(video_fd, VIDIOCSTUNER, &tuner);
142 e3ee3283 Alex Beregszaszi
    }
143 115329f1 Diego Biurrun
144 de6d9b64 Fabrice Bellard
    /* unmute audio */
145 8949367e Fabrice Bellard
    audio.audio = 0;
146 de6d9b64 Fabrice Bellard
    ioctl(video_fd, VIDIOCGAUDIO, &audio);
147 8949367e Fabrice Bellard
    memcpy(&s->audio_saved, &audio, sizeof(audio));
148 de6d9b64 Fabrice Bellard
    audio.flags &= ~VIDEO_AUDIO_MUTE;
149
    ioctl(video_fd, VIDIOCSAUDIO, &audio);
150
151 c787ba85 Flavio Pimentel Duarte
    ioctl(video_fd, VIDIOCGPICT, &pict);
152
#if 0
153
    printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
154
           pict.colour,
155
           pict.hue,
156
           pict.brightness,
157
           pict.contrast,
158
           pict.whiteness);
159
#endif
160
    /* try to choose a suitable video format */
161
    pict.palette = desired_palette;
162 76a77ff2 Luca Abeni
    pict.depth= desired_depth;
163 b9f382c8 Stefano Sabatini
    if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
164 c7c64e9e Marc Hoffman
        for (j = 0; j < vformat_num; j++) {
165
            pict.palette = video_formats[j].palette;
166
            pict.depth = video_formats[j].depth;
167
            if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
168
                break;
169 c787ba85 Flavio Pimentel Duarte
        }
170 c7c64e9e Marc Hoffman
        if (j >= vformat_num)
171
            goto fail1;
172 c787ba85 Flavio Pimentel Duarte
    }
173
174 c40432d3 Stefano Sabatini
    if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
175 de6d9b64 Fabrice Bellard
        /* try to use read based access */
176
        int val;
177
178 406c5808 Stefano Sabatini
        s->video_win.x = 0;
179
        s->video_win.y = 0;
180
        s->video_win.chromakey = -1;
181
        s->video_win.flags = 0;
182 de6d9b64 Fabrice Bellard
183 faf67322 Stefano Sabatini
        if (ioctl(video_fd, VIDIOCSWIN, s->video_win) < 0) {
184
            av_log(s1, AV_LOG_ERROR, "VIDIOCSWIN: %s\n", strerror(errno));
185
            goto fail;
186
        }
187 de6d9b64 Fabrice Bellard
188
        s->frame_format = pict.palette;
189
190
        val = 1;
191 fd867ffd Stefano Sabatini
        if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
192
            av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
193
            goto fail;
194
        }
195 de6d9b64 Fabrice Bellard
196 9580ba26 Stefano Sabatini
        s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
197 de6d9b64 Fabrice Bellard
        s->use_mmap = 0;
198
    } else {
199 c40432d3 Stefano Sabatini
        s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_SHARED, video_fd, 0);
200 8949367e Fabrice Bellard
        if ((unsigned char*)-1 == s->video_buf) {
201 c40432d3 Stefano Sabatini
            s->video_buf = mmap(0, s->gb_buffers.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, video_fd, 0);
202 d037d797 Mike Frysinger
            if ((unsigned char*)-1 == s->video_buf) {
203 9f74582c Luca Abeni
                av_log(s1, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
204 d037d797 Mike Frysinger
                goto fail;
205
            }
206 de6d9b64 Fabrice Bellard
        }
207 8949367e Fabrice Bellard
        s->gb_frame = 0;
208 9580ba26 Stefano Sabatini
        s->time_frame = av_gettime() * s->time_base.den / s->time_base.num;
209 115329f1 Diego Biurrun
210 de6d9b64 Fabrice Bellard
        /* start to grab the first frame */
211 8949367e Fabrice Bellard
        s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
212 406c5808 Stefano Sabatini
        s->gb_buf.height = s->video_win.height;
213
        s->gb_buf.width = s->video_win.width;
214 c787ba85 Flavio Pimentel Duarte
        s->gb_buf.format = pict.palette;
215
216 b9f382c8 Stefano Sabatini
        if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
217 de6d9b64 Fabrice Bellard
            if (errno != EAGAIN) {
218
            fail1:
219 df77a6da Stefano Sabatini
                av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
220 de6d9b64 Fabrice Bellard
            } else {
221 c40432d3 Stefano Sabatini
                av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
222 de6d9b64 Fabrice Bellard
            }
223
            goto fail;
224
        }
225 bb270c08 Diego Biurrun
        for (j = 1; j < s->gb_buffers.frames; j++) {
226
          s->gb_buf.frame = j;
227
          ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
228
        }
229 8949367e Fabrice Bellard
        s->frame_format = s->gb_buf.format;
230 de6d9b64 Fabrice Bellard
        s->use_mmap = 1;
231
    }
232
233 c7c64e9e Marc Hoffman
    for (j = 0; j < vformat_num; j++) {
234
        if (s->frame_format == video_formats[j].palette) {
235 f233d348 Stefano Sabatini
            s->frame_size = s->video_win.width * s->video_win.height * video_formats[j].depth / 8;
236 c7c64e9e Marc Hoffman
            st->codec->pix_fmt = video_formats[j].pix_fmt;
237
            break;
238
        }
239 de6d9b64 Fabrice Bellard
    }
240 c7c64e9e Marc Hoffman
241
    if (j >= vformat_num)
242
        goto fail;
243
244 de6d9b64 Fabrice Bellard
    s->fd = video_fd;
245 115329f1 Diego Biurrun
246 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247 01f4895c Michael Niedermayer
    st->codec->codec_id = CODEC_ID_RAWVIDEO;
248 406c5808 Stefano Sabatini
    st->codec->width = s->video_win.width;
249
    st->codec->height = s->video_win.height;
250 9580ba26 Stefano Sabatini
    st->codec->time_base = s->time_base;
251 f233d348 Stefano Sabatini
    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
252 45dd5c69 Fabrice Bellard
253 de6d9b64 Fabrice Bellard
    return 0;
254
 fail:
255 4972b26f Fabrice Bellard
    if (video_fd >= 0)
256
        close(video_fd);
257 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
258 de6d9b64 Fabrice Bellard
}
259
260 0c1a9eda Zdenek Kabelac
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
261 de6d9b64 Fabrice Bellard
{
262 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
263 de6d9b64 Fabrice Bellard
264 7aae3168 Michael Niedermayer
    while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
265
           (errno == EAGAIN || errno == EINTR));
266
267
    ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
268
    memcpy(buf, ptr, s->frame_size);
269
270 4606ac8d Zdenek Kabelac
    /* Setup to capture the next frame */
271 7aae3168 Michael Niedermayer
    s->gb_buf.frame = s->gb_frame;
272 8949367e Fabrice Bellard
    if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
273 28c66901 Philip Gladstone
        if (errno == EAGAIN)
274 bc874dae Michel Bardiaux
            av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
275 28c66901 Philip Gladstone
        else
276 9f74582c Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
277 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
278 de6d9b64 Fabrice Bellard
    }
279 4606ac8d Zdenek Kabelac
280
    /* This is now the grabbing frame */
281 7aae3168 Michael Niedermayer
    s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
282 4606ac8d Zdenek Kabelac
283 4972b26f Fabrice Bellard
    return s->frame_size;
284 de6d9b64 Fabrice Bellard
}
285
286 4972b26f Fabrice Bellard
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
287 de6d9b64 Fabrice Bellard
{
288 4972b26f Fabrice Bellard
    VideoData *s = s1->priv_data;
289 0c1a9eda Zdenek Kabelac
    int64_t curtime, delay;
290 4972b26f Fabrice Bellard
    struct timespec ts;
291 4606ac8d Zdenek Kabelac
292
    /* Calculate the time of the next frame */
293 8da9266c Måns Rullgård
    s->time_frame += INT64_C(1000000);
294 de6d9b64 Fabrice Bellard
295
    /* wait based on the frame rate */
296 45dd5c69 Fabrice Bellard
    for(;;) {
297 a11bf0bd Fabrice Bellard
        curtime = av_gettime();
298 9580ba26 Stefano Sabatini
        delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
299 4606ac8d Zdenek Kabelac
        if (delay <= 0) {
300 9580ba26 Stefano Sabatini
            if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
301 4606ac8d Zdenek Kabelac
                /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
302 8da9266c Måns Rullgård
                s->time_frame += INT64_C(1000000);
303 4606ac8d Zdenek Kabelac
            }
304 4972b26f Fabrice Bellard
            break;
305 115329f1 Diego Biurrun
        }
306 4972b26f Fabrice Bellard
        ts.tv_sec = delay / 1000000;
307
        ts.tv_nsec = (delay % 1000000) * 1000;
308
        nanosleep(&ts, NULL);
309
    }
310
311
    if (av_new_packet(pkt, s->frame_size) < 0)
312 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
313 de6d9b64 Fabrice Bellard
314 0a7b514f Luca Abeni
    pkt->pts = curtime;
315 45dd5c69 Fabrice Bellard
316 de6d9b64 Fabrice Bellard
    /* read one frame */
317 3ab64e46 Michael Niedermayer
    if (s->use_mmap) {
318 4972b26f Fabrice Bellard
        return v4l_mm_read_picture(s, pkt->data);
319 de6d9b64 Fabrice Bellard
    } else {
320 4972b26f Fabrice Bellard
        if (read(s->fd, pkt->data, pkt->size) != pkt->size)
321 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
322 4972b26f Fabrice Bellard
        return s->frame_size;
323 de6d9b64 Fabrice Bellard
    }
324
}
325
326 4972b26f Fabrice Bellard
static int grab_read_close(AVFormatContext *s1)
327 de6d9b64 Fabrice Bellard
{
328 4972b26f Fabrice Bellard
    VideoData *s = s1->priv_data;
329 e61efa24 Lennert Buytenhek
330
    if (s->use_mmap)
331 8949367e Fabrice Bellard
        munmap(s->video_buf, s->gb_buffers.size);
332 e61efa24 Lennert Buytenhek
333 8949367e Fabrice Bellard
    /* mute audio. we must force it because the BTTV driver does not
334
       return its state correctly */
335
    s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
336
    ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
337 5a56c87c Fabrice Bellard
338 de6d9b64 Fabrice Bellard
    close(s->fd);
339
    return 0;
340
}
341
342 c6610a21 Diego Elio Pettenò
AVInputFormat ff_v4l_demuxer = {
343 8aa3ee32 Max Krasnyansky
    "video4linux",
344 df2a3f27 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
345 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
346 4972b26f Fabrice Bellard
    NULL,
347
    grab_read_header,
348
    grab_read_packet,
349
    grab_read_close,
350 bb76a117 Måns Rullgård
    .flags = AVFMT_NOFILE,
351 de6d9b64 Fabrice Bellard
};