Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l.c @ 2912e87a

History | View | Annotate | Download (10.8 KB)

1
/*
2
 * Linux video grab interface
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#undef __STRICT_ANSI__ //workaround due to broken kernel headers
23
#include "config.h"
24
#include "libavutil/rational.h"
25
#include "libavutil/imgutils.h"
26
#include "libavformat/avformat.h"
27
#include "libavcodec/dsputil.h"
28
#include <unistd.h>
29
#include <fcntl.h>
30
#include <sys/ioctl.h>
31
#include <sys/mman.h>
32
#include <sys/time.h>
33
#define _LINUX_TIME_H 1
34
#include <linux/videodev.h>
35
#include <time.h>
36
#include <strings.h>
37

    
38
typedef struct {
39
    int fd;
40
    int frame_format; /* see VIDEO_PALETTE_xxx */
41
    int use_mmap;
42
    AVRational time_base;
43
    int64_t time_frame;
44
    int frame_size;
45
    struct video_capability video_cap;
46
    struct video_audio audio_saved;
47
    struct video_window video_win;
48
    uint8_t *video_buf;
49
    struct video_mbuf gb_buffers;
50
    struct video_mmap gb_buf;
51
    int gb_frame;
52
} VideoData;
53

    
54
static const struct {
55
    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
    {.palette = VIDEO_PALETTE_UYVY,    .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
62
    {.palette = VIDEO_PALETTE_YUYV,    .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
63
    /* NOTE: v4l uses BGR24, not RGB24 */
64
    {.palette = VIDEO_PALETTE_RGB24,   .depth = 24, .pix_fmt = PIX_FMT_BGR24   },
65
    {.palette = VIDEO_PALETTE_RGB565,  .depth = 16, .pix_fmt = PIX_FMT_BGR565  },
66
    {.palette = VIDEO_PALETTE_GREY,    .depth = 8,  .pix_fmt = PIX_FMT_GRAY8   },
67
};
68

    
69

    
70
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
71
{
72
    VideoData *s = s1->priv_data;
73
    AVStream *st;
74
    int video_fd;
75
    int desired_palette, desired_depth;
76
    struct video_tuner tuner;
77
    struct video_audio audio;
78
    struct video_picture pict;
79
    int j;
80
    int vformat_num = FF_ARRAY_ELEMS(video_formats);
81

    
82
    if (ap->time_base.den <= 0) {
83
        av_log(s1, AV_LOG_ERROR, "Wrong time base (%d)\n", ap->time_base.den);
84
        return -1;
85
    }
86
    s->time_base = ap->time_base;
87

    
88
    s->video_win.width = ap->width;
89
    s->video_win.height = ap->height;
90

    
91
    st = av_new_stream(s1, 0);
92
    if (!st)
93
        return AVERROR(ENOMEM);
94
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
95

    
96
    video_fd = open(s1->filename, O_RDWR);
97
    if (video_fd < 0) {
98
        av_log(s1, AV_LOG_ERROR, "%s: %s\n", s1->filename, strerror(errno));
99
        goto fail;
100
    }
101

    
102
    if (ioctl(video_fd, VIDIOCGCAP, &s->video_cap) < 0) {
103
        av_log(s1, AV_LOG_ERROR, "VIDIOCGCAP: %s\n", strerror(errno));
104
        goto fail;
105
    }
106

    
107
    if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
108
        av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
109
        goto fail;
110
    }
111

    
112
    /* 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
    if(av_image_check_size(s->video_win.width, s->video_win.height, 0, s1) < 0)
121
        return -1;
122

    
123
    desired_palette = -1;
124
    desired_depth = -1;
125
    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
    }
132

    
133
    /* set tv standard */
134
    if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
135
        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
    }
143

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

    
151
    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
    pict.depth= desired_depth;
163
    if (desired_palette == -1 || ioctl(video_fd, VIDIOCSPICT, &pict) < 0) {
164
        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
        }
170
        if (j >= vformat_num)
171
            goto fail1;
172
    }
173

    
174
    if (ioctl(video_fd, VIDIOCGMBUF, &s->gb_buffers) < 0) {
175
        /* try to use read based access */
176
        int val;
177

    
178
        s->video_win.x = 0;
179
        s->video_win.y = 0;
180
        s->video_win.chromakey = -1;
181
        s->video_win.flags = 0;
182

    
183
        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

    
188
        s->frame_format = pict.palette;
189

    
190
        val = 1;
191
        if (ioctl(video_fd, VIDIOCCAPTURE, &val) < 0) {
192
            av_log(s1, AV_LOG_ERROR, "VIDIOCCAPTURE: %s\n", strerror(errno));
193
            goto fail;
194
        }
195

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

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

    
216
        if (ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
217
            if (errno != EAGAIN) {
218
            fail1:
219
                av_log(s1, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
220
            } else {
221
                av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not receive any video signal\n");
222
            }
223
            goto fail;
224
        }
225
        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
        s->frame_format = s->gb_buf.format;
230
        s->use_mmap = 1;
231
    }
232

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

    
241
    if (j >= vformat_num)
242
        goto fail;
243

    
244
    s->fd = video_fd;
245

    
246
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247
    st->codec->codec_id = CODEC_ID_RAWVIDEO;
248
    st->codec->width = s->video_win.width;
249
    st->codec->height = s->video_win.height;
250
    st->codec->time_base = s->time_base;
251
    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
252

    
253
    return 0;
254
 fail:
255
    if (video_fd >= 0)
256
        close(video_fd);
257
    return AVERROR(EIO);
258
}
259

    
260
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
261
{
262
    uint8_t *ptr;
263

    
264
    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
    /* Setup to capture the next frame */
271
    s->gb_buf.frame = s->gb_frame;
272
    if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
273
        if (errno == EAGAIN)
274
            av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
275
        else
276
            av_log(NULL, AV_LOG_ERROR, "VIDIOCMCAPTURE: %s\n", strerror(errno));
277
        return AVERROR(EIO);
278
    }
279

    
280
    /* This is now the grabbing frame */
281
    s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
282

    
283
    return s->frame_size;
284
}
285

    
286
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
287
{
288
    VideoData *s = s1->priv_data;
289
    int64_t curtime, delay;
290
    struct timespec ts;
291

    
292
    /* Calculate the time of the next frame */
293
    s->time_frame += INT64_C(1000000);
294

    
295
    /* wait based on the frame rate */
296
    for(;;) {
297
        curtime = av_gettime();
298
        delay = s->time_frame * s->time_base.num / s->time_base.den - curtime;
299
        if (delay <= 0) {
300
            if (delay < INT64_C(-1000000) * s->time_base.num / s->time_base.den) {
301
                /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
302
                s->time_frame += INT64_C(1000000);
303
            }
304
            break;
305
        }
306
        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
        return AVERROR(EIO);
313

    
314
    pkt->pts = curtime;
315

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

    
326
static int grab_read_close(AVFormatContext *s1)
327
{
328
    VideoData *s = s1->priv_data;
329

    
330
    if (s->use_mmap)
331
        munmap(s->video_buf, s->gb_buffers.size);
332

    
333
    /* 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

    
338
    close(s->fd);
339
    return 0;
340
}
341

    
342
AVInputFormat ff_v4l_demuxer = {
343
    "video4linux",
344
    NULL_IF_CONFIG_SMALL("Video4Linux device grab"),
345
    sizeof(VideoData),
346
    NULL,
347
    grab_read_header,
348
    grab_read_packet,
349
    grab_read_close,
350
    .flags = AVFMT_NOFILE,
351
};