Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l.c @ 647148c6

History | View | Annotate | Download (10.8 KB)

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