Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l2.c @ 4f54d924

History | View | Annotate | Download (19.5 KB)

1 0a7b514f Luca Abeni
/*
2
 * Video4Linux2 grab interface
3 406792e7 Diego Biurrun
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * Copyright (c) 2006 Luca Abeni
5 0a7b514f Luca Abeni
 *
6
 * Part of this file is based on the V4L2 video capture example
7
 * (http://v4l2spec.bytesex.org/v4l2spec/capture.c)
8
 *
9
 * Thanks to Michael Niedermayer for providing the mapping between
10
 * V4L2_PIX_FMT_* and PIX_FMT_*
11
 *
12
 *
13 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
14
 *
15
 * FFmpeg is free software; you can redistribute it and/or
16 0a7b514f Luca Abeni
 * modify it under the terms of the GNU Lesser General Public
17
 * License as published by the Free Software Foundation; either
18 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
19 0a7b514f Luca Abeni
 *
20 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
21 0a7b514f Luca Abeni
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
 * Lesser General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Lesser General Public
26 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
27 0a7b514f Luca Abeni
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28
 */
29 b0067549 Diego Biurrun
30 e9d4b8a5 Michael Niedermayer
#undef __STRICT_ANSI__ //workaround due to broken kernel headers
31 b0067549 Diego Biurrun
#include "config.h"
32 245976da Diego Biurrun
#include "libavformat/avformat.h"
33 0a7b514f Luca Abeni
#include <unistd.h>
34
#include <fcntl.h>
35
#include <sys/ioctl.h>
36
#include <sys/mman.h>
37
#include <sys/time.h>
38 b250f9c6 Aurelien Jacobs
#if HAVE_SYS_VIDEOIO_H
39 1b6e8b73 Brad
#include <sys/videoio.h>
40
#else
41 f743a062 Luca Abeni
#include <asm/types.h>
42
#include <linux/videodev2.h>
43 1b6e8b73 Brad
#endif
44 0a7b514f Luca Abeni
#include <time.h>
45 ea452b54 Aurelien Jacobs
#include <strings.h>
46 0a7b514f Luca Abeni
47
static const int desired_video_buffers = 256;
48
49
enum io_method {
50
    io_read,
51
    io_mmap,
52
    io_userptr
53
};
54
55
struct video_data {
56
    int fd;
57
    int frame_format; /* V4L2_PIX_FMT_* */
58
    enum io_method io_method;
59
    int width, height;
60
    int frame_size;
61
    int top_field_first;
62
63
    int buffers;
64
    void **buf_start;
65
    unsigned int *buf_len;
66
};
67
68 41536a60 Luca Abeni
struct buff_data {
69
    int index;
70
    int fd;
71
};
72
73 0a7b514f Luca Abeni
struct fmt_map {
74
    enum PixelFormat ff_fmt;
75 9eb6e9d9 Luca Abeni
    enum CodecID codec_id;
76 9202218e Luca Abeni
    uint32_t v4l2_fmt;
77 0a7b514f Luca Abeni
};
78
79
static struct fmt_map fmt_conversion_table[] = {
80
    {
81
        .ff_fmt = PIX_FMT_YUV420P,
82 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
83 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_YUV420,
84
    },
85
    {
86
        .ff_fmt = PIX_FMT_YUV422P,
87 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
88 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_YUV422P,
89
    },
90
    {
91 71e445fc Diego Biurrun
        .ff_fmt = PIX_FMT_YUYV422,
92 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
93 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_YUYV,
94
    },
95
    {
96
        .ff_fmt = PIX_FMT_UYVY422,
97 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
98 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_UYVY,
99
    },
100
    {
101
        .ff_fmt = PIX_FMT_YUV411P,
102 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
103 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_YUV411P,
104
    },
105
    {
106
        .ff_fmt = PIX_FMT_YUV410P,
107 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
108 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_YUV410,
109
    },
110
    {
111 97a290ef Jean Delvare
        .ff_fmt = PIX_FMT_RGB555,
112 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
113 97a290ef Jean Delvare
        .v4l2_fmt = V4L2_PIX_FMT_RGB555,
114
    },
115
    {
116
        .ff_fmt = PIX_FMT_RGB565,
117 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
118 97a290ef Jean Delvare
        .v4l2_fmt = V4L2_PIX_FMT_RGB565,
119
    },
120
    {
121 0a7b514f Luca Abeni
        .ff_fmt = PIX_FMT_BGR24,
122 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
123 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_BGR24,
124
    },
125
    {
126
        .ff_fmt = PIX_FMT_RGB24,
127 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
128 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_RGB24,
129
    },
130
    {
131 92d04e76 Jean Delvare
        .ff_fmt = PIX_FMT_BGRA,
132 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
133 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_BGR32,
134
    },
135
    {
136
        .ff_fmt = PIX_FMT_GRAY8,
137 9eb6e9d9 Luca Abeni
        .codec_id = CODEC_ID_RAWVIDEO,
138 0a7b514f Luca Abeni
        .v4l2_fmt = V4L2_PIX_FMT_GREY,
139
    },
140 9eb6e9d9 Luca Abeni
    {
141
        .ff_fmt = PIX_FMT_NONE,
142
        .codec_id = CODEC_ID_MJPEG,
143
        .v4l2_fmt = V4L2_PIX_FMT_MJPEG,
144
    },
145
    {
146
        .ff_fmt = PIX_FMT_NONE,
147
        .codec_id = CODEC_ID_MJPEG,
148
        .v4l2_fmt = V4L2_PIX_FMT_JPEG,
149
    },
150 0a7b514f Luca Abeni
};
151
152 cc58300e Ramiro Polla
static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
153 0a7b514f Luca Abeni
{
154
    struct v4l2_capability cap;
155
    int fd;
156
    int res;
157 653387d8 Luca Abeni
    int flags = O_RDWR;
158 0a7b514f Luca Abeni
159 653387d8 Luca Abeni
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
160
        flags |= O_NONBLOCK;
161
    }
162
    fd = open(ctx->filename, flags, 0);
163 0a7b514f Luca Abeni
    if (fd < 0) {
164 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
165 cc58300e Ramiro Polla
                 ctx->filename, strerror(errno));
166 0a7b514f Luca Abeni
167 c57a8fef Stefano Sabatini
        return AVERROR(errno);
168 0a7b514f Luca Abeni
    }
169
170
    res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
171 c16601bb Alex Beregszaszi
    // ENOIOCTLCMD definition only availble on __KERNEL__
172
    if (res < 0 && errno == 515)
173
    {
174 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "QUERYCAP not implemented, probably V4L device but not supporting V4L2\n");
175 2b63c222 Flavio Pimentel Duarte
        close(fd);
176 c16601bb Alex Beregszaszi
177 c57a8fef Stefano Sabatini
        return AVERROR(errno);
178 c16601bb Alex Beregszaszi
    }
179 0a7b514f Luca Abeni
    if (res < 0) {
180 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
181 0a7b514f Luca Abeni
                 strerror(errno));
182 2b63c222 Flavio Pimentel Duarte
        close(fd);
183 0a7b514f Luca Abeni
184 c57a8fef Stefano Sabatini
        return AVERROR(errno);
185 0a7b514f Luca Abeni
    }
186
    if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
187 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
188 2b63c222 Flavio Pimentel Duarte
        close(fd);
189 0a7b514f Luca Abeni
190 c57a8fef Stefano Sabatini
        return AVERROR(ENODEV);
191 0a7b514f Luca Abeni
    }
192
    *capabilities = cap.capabilities;
193
194
    return fd;
195
}
196
197 9202218e Luca Abeni
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
198 0a7b514f Luca Abeni
{
199 c7238c72 Luca Abeni
    struct video_data *s = ctx->priv_data;
200
    int fd = s->fd;
201 0a7b514f Luca Abeni
    struct v4l2_format fmt;
202 24300af4 Luca Abeni
    int res;
203 0a7b514f Luca Abeni
204
    memset(&fmt, 0, sizeof(struct v4l2_format));
205
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
206 24300af4 Luca Abeni
    fmt.fmt.pix.width = *width;
207
    fmt.fmt.pix.height = *height;
208 0a7b514f Luca Abeni
    fmt.fmt.pix.pixelformat = pix_fmt;
209
    fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
210 24300af4 Luca Abeni
    res = ioctl(fd, VIDIOC_S_FMT, &fmt);
211
    if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
212 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_INFO, "The V4L2 driver changed the video from %dx%d to %dx%d\n", *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
213 24300af4 Luca Abeni
        *width = fmt.fmt.pix.width;
214
        *height = fmt.fmt.pix.height;
215
    }
216
217 3d0d9a5e Jens Rosenboom
    if (pix_fmt != fmt.fmt.pix.pixelformat) {
218
        av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver changed the pixel format from 0x%08X to 0x%08X\n", pix_fmt, fmt.fmt.pix.pixelformat);
219
        res = -1;
220
    }
221
222 24300af4 Luca Abeni
    return res;
223 0a7b514f Luca Abeni
}
224
225
static int first_field(int fd)
226
{
227
    int res;
228
    v4l2_std_id std;
229
230
    res = ioctl(fd, VIDIOC_G_STD, &std);
231
    if (res < 0) {
232
        return 0;
233
    }
234
    if (std & V4L2_STD_NTSC) {
235
        return 0;
236
    }
237
238
    return 1;
239
}
240
241 3db77ccf Luca Abeni
static uint32_t fmt_ff2v4l(enum PixelFormat pix_fmt, enum CodecID codec_id)
242 0a7b514f Luca Abeni
{
243
    int i;
244
245 37d3e066 Aurelien Jacobs
    for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
246 3db77ccf Luca Abeni
        if ((codec_id == CODEC_ID_NONE ||
247
             fmt_conversion_table[i].codec_id == codec_id) &&
248
            (pix_fmt == PIX_FMT_NONE ||
249
             fmt_conversion_table[i].ff_fmt == pix_fmt)) {
250 0a7b514f Luca Abeni
            return fmt_conversion_table[i].v4l2_fmt;
251
        }
252
    }
253
254
    return 0;
255
}
256
257 3db77ccf Luca Abeni
static enum PixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum CodecID codec_id)
258 0a7b514f Luca Abeni
{
259
    int i;
260
261 37d3e066 Aurelien Jacobs
    for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
262 3db77ccf Luca Abeni
        if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
263
            fmt_conversion_table[i].codec_id == codec_id) {
264 0a7b514f Luca Abeni
            return fmt_conversion_table[i].ff_fmt;
265
        }
266
    }
267
268 eacced45 Carl Eugen Hoyos
    return PIX_FMT_NONE;
269 0a7b514f Luca Abeni
}
270
271 3db77ccf Luca Abeni
static enum CodecID fmt_v4l2codec(uint32_t v4l2_fmt)
272
{
273
    int i;
274
275
    for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
276
        if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
277
            return fmt_conversion_table[i].codec_id;
278
        }
279
    }
280
281
    return CODEC_ID_NONE;
282
}
283
284 c7238c72 Luca Abeni
static int mmap_init(AVFormatContext *ctx)
285 0a7b514f Luca Abeni
{
286 c7238c72 Luca Abeni
    struct video_data *s = ctx->priv_data;
287 0a7b514f Luca Abeni
    struct v4l2_requestbuffers req;
288
    int i, res;
289
290
    memset(&req, 0, sizeof(struct v4l2_requestbuffers));
291
    req.count = desired_video_buffers;
292
    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
293
    req.memory = V4L2_MEMORY_MMAP;
294
    res = ioctl (s->fd, VIDIOC_REQBUFS, &req);
295
    if (res < 0) {
296
        if (errno == EINVAL) {
297 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
298 0a7b514f Luca Abeni
        } else {
299 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
300 0a7b514f Luca Abeni
        }
301
302 c57a8fef Stefano Sabatini
        return AVERROR(errno);
303 0a7b514f Luca Abeni
    }
304
305
    if (req.count < 2) {
306 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
307 0a7b514f Luca Abeni
308 c57a8fef Stefano Sabatini
        return AVERROR(ENOMEM);
309 0a7b514f Luca Abeni
    }
310
    s->buffers = req.count;
311
    s->buf_start = av_malloc(sizeof(void *) * s->buffers);
312
    if (s->buf_start == NULL) {
313 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
314 0a7b514f Luca Abeni
315 c57a8fef Stefano Sabatini
        return AVERROR(ENOMEM);
316 0a7b514f Luca Abeni
    }
317
    s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
318
    if (s->buf_len == NULL) {
319 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
320 0a7b514f Luca Abeni
        av_free(s->buf_start);
321
322 c57a8fef Stefano Sabatini
        return AVERROR(ENOMEM);
323 0a7b514f Luca Abeni
    }
324
325
    for (i = 0; i < req.count; i++) {
326
        struct v4l2_buffer buf;
327
328
        memset(&buf, 0, sizeof(struct v4l2_buffer));
329
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
330
        buf.memory = V4L2_MEMORY_MMAP;
331
        buf.index = i;
332
        res = ioctl (s->fd, VIDIOC_QUERYBUF, &buf);
333
        if (res < 0) {
334 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
335 0a7b514f Luca Abeni
336 c57a8fef Stefano Sabatini
            return AVERROR(errno);
337 0a7b514f Luca Abeni
        }
338
339
        s->buf_len[i] = buf.length;
340 3db77ccf Luca Abeni
        if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
341 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "Buffer len [%d] = %d != %d\n", i, s->buf_len[i], s->frame_size);
342 0a7b514f Luca Abeni
343
            return -1;
344
        }
345
        s->buf_start[i] = mmap (NULL, buf.length,
346
                        PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset);
347
        if (s->buf_start[i] == MAP_FAILED) {
348 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
349 0a7b514f Luca Abeni
350 c57a8fef Stefano Sabatini
            return AVERROR(errno);
351 0a7b514f Luca Abeni
        }
352
    }
353
354
    return 0;
355
}
356
357 c7238c72 Luca Abeni
static int read_init(AVFormatContext *ctx)
358 0a7b514f Luca Abeni
{
359
    return -1;
360
}
361
362 41536a60 Luca Abeni
static void mmap_release_buffer(AVPacket *pkt)
363
{
364
    struct v4l2_buffer buf;
365
    int res, fd;
366
    struct buff_data *buf_descriptor = pkt->priv;
367
368 5449a787 Luca Abeni
    if (pkt->data == NULL) {
369
         return;
370
    }
371
372 41536a60 Luca Abeni
    memset(&buf, 0, sizeof(struct v4l2_buffer));
373
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
374
    buf.memory = V4L2_MEMORY_MMAP;
375
    buf.index = buf_descriptor->index;
376
    fd = buf_descriptor->fd;
377
    av_free(buf_descriptor);
378
379
    res = ioctl (fd, VIDIOC_QBUF, &buf);
380
    if (res < 0) {
381
        av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
382
    }
383
    pkt->data = NULL;
384
    pkt->size = 0;
385
}
386
387
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
388 0a7b514f Luca Abeni
{
389 c7238c72 Luca Abeni
    struct video_data *s = ctx->priv_data;
390 0a7b514f Luca Abeni
    struct v4l2_buffer buf;
391 41536a60 Luca Abeni
    struct buff_data *buf_descriptor;
392 0a7b514f Luca Abeni
    int res;
393
394
    memset(&buf, 0, sizeof(struct v4l2_buffer));
395
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
396
    buf.memory = V4L2_MEMORY_MMAP;
397
398
    /* FIXME: Some special treatment might be needed in case of loss of signal... */
399 653387d8 Luca Abeni
    while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
400 0a7b514f Luca Abeni
    if (res < 0) {
401 653387d8 Luca Abeni
        if (errno == EAGAIN) {
402
            pkt->size = 0;
403
404
            return AVERROR(EAGAIN);
405
        }
406 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n", strerror(errno));
407 0a7b514f Luca Abeni
408 c57a8fef Stefano Sabatini
        return AVERROR(errno);
409 0a7b514f Luca Abeni
    }
410
    assert (buf.index < s->buffers);
411 3db77ccf Luca Abeni
    if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
412 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "The v4l2 frame is %d bytes, but %d bytes are expected\n", buf.bytesused, s->frame_size);
413 7c7e7464 Luca Abeni
414 c57a8fef Stefano Sabatini
        return AVERROR_INVALIDDATA;
415 7c7e7464 Luca Abeni
    }
416
417 0a7b514f Luca Abeni
    /* Image is at s->buff_start[buf.index] */
418 41536a60 Luca Abeni
    pkt->data= s->buf_start[buf.index];
419
    pkt->size = buf.bytesused;
420
    pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
421
    pkt->destruct = mmap_release_buffer;
422
    buf_descriptor = av_malloc(sizeof(struct buff_data));
423
    if (buf_descriptor == NULL) {
424
        /* Something went wrong... Since av_malloc() failed, we cannot even
425
         * allocate a buffer for memcopying into it
426
         */
427
        av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
428
        res = ioctl (s->fd, VIDIOC_QBUF, &buf);
429 0a7b514f Luca Abeni
430 c57a8fef Stefano Sabatini
        return AVERROR(ENOMEM);
431 0a7b514f Luca Abeni
    }
432 41536a60 Luca Abeni
    buf_descriptor->fd = s->fd;
433
    buf_descriptor->index = buf.index;
434
    pkt->priv = buf_descriptor;
435 0a7b514f Luca Abeni
436
    return s->buf_len[buf.index];
437
}
438
439 41536a60 Luca Abeni
static int read_frame(AVFormatContext *ctx, AVPacket *pkt)
440 0a7b514f Luca Abeni
{
441
    return -1;
442
}
443
444 c7238c72 Luca Abeni
static int mmap_start(AVFormatContext *ctx)
445 0a7b514f Luca Abeni
{
446 c7238c72 Luca Abeni
    struct video_data *s = ctx->priv_data;
447 0a7b514f Luca Abeni
    enum v4l2_buf_type type;
448
    int i, res;
449
450
    for (i = 0; i < s->buffers; i++) {
451
        struct v4l2_buffer buf;
452
453
        memset(&buf, 0, sizeof(struct v4l2_buffer));
454
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
455
        buf.memory = V4L2_MEMORY_MMAP;
456
        buf.index  = i;
457
458
        res = ioctl (s->fd, VIDIOC_QBUF, &buf);
459
        if (res < 0) {
460 c7238c72 Luca Abeni
            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", strerror(errno));
461 0a7b514f Luca Abeni
462 c57a8fef Stefano Sabatini
            return AVERROR(errno);
463 0a7b514f Luca Abeni
        }
464
    }
465
466
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
467
    res = ioctl (s->fd, VIDIOC_STREAMON, &type);
468
    if (res < 0) {
469 c7238c72 Luca Abeni
        av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n", strerror(errno));
470 0a7b514f Luca Abeni
471 c57a8fef Stefano Sabatini
        return AVERROR(errno);
472 0a7b514f Luca Abeni
    }
473
474
    return 0;
475
}
476
477
static void mmap_close(struct video_data *s)
478
{
479
    enum v4l2_buf_type type;
480
    int i;
481
482
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
483
    /* We do not check for the result, because we could
484
     * not do anything about it anyway...
485
     */
486
    ioctl(s->fd, VIDIOC_STREAMOFF, &type);
487
    for (i = 0; i < s->buffers; i++) {
488
        munmap(s->buf_start[i], s->buf_len[i]);
489
    }
490
    av_free(s->buf_start);
491
    av_free(s->buf_len);
492
}
493
494 f5ad81f5 Limin Wang
static int v4l2_set_parameters( AVFormatContext *s1, AVFormatParameters *ap )
495
{
496
    struct video_data *s = s1->priv_data;
497
    struct v4l2_input input;
498
    struct v4l2_standard standard;
499
    int i;
500
501 0d6c0732 Limin Wang
    if(ap->channel>=0) {
502 e4dd03f3 Luca Abeni
        /* set tv video input */
503
        memset (&input, 0, sizeof (input));
504
        input.index = ap->channel;
505
        if(ioctl (s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
506
            av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
507 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
508 e4dd03f3 Luca Abeni
        }
509 f5ad81f5 Limin Wang
510 e4dd03f3 Luca Abeni
        av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
511
               ap->channel, input.name);
512
        if(ioctl (s->fd, VIDIOC_S_INPUT, &input.index) < 0 ) {
513
            av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set input(%d) failed\n",
514
                   ap->channel);
515 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
516 e4dd03f3 Luca Abeni
        }
517 0d6c0732 Limin Wang
    }
518 f5ad81f5 Limin Wang
519 0d6c0732 Limin Wang
    if(ap->standard) {
520 e4dd03f3 Luca Abeni
        av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
521
               ap->standard );
522
        /* set tv standard */
523
        memset (&standard, 0, sizeof (standard));
524
        for(i=0;;i++) {
525
            standard.index = i;
526
            if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
527
                av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
528
                       ap->standard);
529 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
530 e4dd03f3 Luca Abeni
            }
531
532
            if(!strcasecmp(standard.name, ap->standard)) {
533
                break;
534
            }
535
        }
536
537
        av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
538 1c787b10 Luca Abeni
               ap->standard, (uint64_t)standard.id);
539 e4dd03f3 Luca Abeni
        if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
540 f5ad81f5 Limin Wang
            av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl set standard(%s) failed\n",
541
                   ap->standard);
542 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
543 f5ad81f5 Limin Wang
        }
544 0d6c0732 Limin Wang
    }
545 f5ad81f5 Limin Wang
546
    return 0;
547
}
548
549 8040c3b2 Reinhard Tartler
static uint32_t device_try_init(AVFormatContext *s1,
550
                                const AVFormatParameters *ap,
551
                                int *width,
552
                                int *height,
553
                                enum CodecID *codec_id)
554 3db77ccf Luca Abeni
{
555 90d0379f Jai Menon
    uint32_t desired_format = fmt_ff2v4l(ap->pix_fmt, s1->video_codec_id);
556 3db77ccf Luca Abeni
557
    if (desired_format == 0 ||
558
        device_init(s1, width, height, desired_format) < 0) {
559
        int i;
560
561
        desired_format = 0;
562
        for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
563 90d0379f Jai Menon
            if (s1->video_codec_id == CODEC_ID_NONE ||
564
                fmt_conversion_table[i].codec_id == s1->video_codec_id) {
565 3db77ccf Luca Abeni
                desired_format = fmt_conversion_table[i].v4l2_fmt;
566
                if (device_init(s1, width, height, desired_format) >= 0) {
567
                    break;
568
                }
569
                desired_format = 0;
570
            }
571
        }
572
    }
573
    if (desired_format != 0) {
574
        *codec_id = fmt_v4l2codec(desired_format);
575
        assert(*codec_id != CODEC_ID_NONE);
576
    }
577
578
    return desired_format;
579
}
580
581 0a7b514f Luca Abeni
static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap)
582
{
583
    struct video_data *s = s1->priv_data;
584
    AVStream *st;
585 e7b987dc Luca Abeni
    int res;
586 0a7b514f Luca Abeni
    uint32_t desired_format, capabilities;
587 3db77ccf Luca Abeni
    enum CodecID codec_id;
588 0a7b514f Luca Abeni
589
    st = av_new_stream(s1, 0);
590
    if (!st) {
591 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
592 0a7b514f Luca Abeni
    }
593
    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
594
595 18225025 Stefano Sabatini
    s->width  = ap->width;
596
    s->height = ap->height;
597 0a7b514f Luca Abeni
598
    capabilities = 0;
599 cc58300e Ramiro Polla
    s->fd = device_open(s1, &capabilities);
600 0a7b514f Luca Abeni
    if (s->fd < 0) {
601 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
602 0a7b514f Luca Abeni
    }
603 60023e32 Luca Abeni
    av_log(s1, AV_LOG_INFO, "[%d]Capabilities: %x\n", s->fd, capabilities);
604 0a7b514f Luca Abeni
605 932d775f Stefano Sabatini
    if (!s->width && !s->height) {
606
        struct v4l2_format fmt;
607
608 4f54d924 Luca Abeni
        av_log(s1, AV_LOG_INFO, "Querying the device for the current frame size\n");
609 932d775f Stefano Sabatini
        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
610
        if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
611
            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", strerror(errno));
612
            return AVERROR(errno);
613
        }
614
        s->width  = fmt.fmt.pix.width;
615
        s->height = fmt.fmt.pix.height;
616 4f54d924 Luca Abeni
        av_log(s1, AV_LOG_INFO, "Setting frame size to %dx%d\n", s->width, s->height);
617 932d775f Stefano Sabatini
    }
618
619 18225025 Stefano Sabatini
    desired_format = device_try_init(s1, ap, &s->width, &s->height, &codec_id);
620 932d775f Stefano Sabatini
    if (avcodec_check_dimensions(s1, s->width, s->height) < 0)
621
        return AVERROR(EINVAL);
622
623 0a7b514f Luca Abeni
    if (desired_format == 0) {
624 3db77ccf Luca Abeni
        av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
625 90d0379f Jai Menon
               "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, ap->pix_fmt);
626 0a7b514f Luca Abeni
        close(s->fd);
627
628 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
629 0a7b514f Luca Abeni
    }
630
    s->frame_format = desired_format;
631
632 f5ad81f5 Limin Wang
    if( v4l2_set_parameters( s1, ap ) < 0 )
633 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
634 f5ad81f5 Limin Wang
635 3db77ccf Luca Abeni
    st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
636 18225025 Stefano Sabatini
    s->frame_size = avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
637 0a7b514f Luca Abeni
    if (capabilities & V4L2_CAP_STREAMING) {
638
        s->io_method = io_mmap;
639 c7238c72 Luca Abeni
        res = mmap_init(s1);
640 eaddc471 R. Brian Anderson
        if (res == 0) {
641 c7238c72 Luca Abeni
            res = mmap_start(s1);
642 eaddc471 R. Brian Anderson
        }
643 0a7b514f Luca Abeni
    } else {
644
        s->io_method = io_read;
645 c7238c72 Luca Abeni
        res = read_init(s1);
646 0a7b514f Luca Abeni
    }
647
    if (res < 0) {
648
        close(s->fd);
649
650 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
651 0a7b514f Luca Abeni
    }
652
    s->top_field_first = first_field(s->fd);
653
654 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
655 3db77ccf Luca Abeni
    st->codec->codec_id = codec_id;
656 18225025 Stefano Sabatini
    st->codec->width = s->width;
657
    st->codec->height = s->height;
658 e7b987dc Luca Abeni
    st->codec->time_base.den = ap->time_base.den;
659
    st->codec->time_base.num = ap->time_base.num;
660 0a7b514f Luca Abeni
    st->codec->bit_rate = s->frame_size * 1/av_q2d(st->codec->time_base) * 8;
661
662
    return 0;
663
}
664
665
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
666
{
667
    struct video_data *s = s1->priv_data;
668
    int res;
669
670
    if (s->io_method == io_mmap) {
671 41536a60 Luca Abeni
        av_init_packet(pkt);
672
        res = mmap_read_frame(s1, pkt);
673 0a7b514f Luca Abeni
    } else if (s->io_method == io_read) {
674 41536a60 Luca Abeni
        if (av_new_packet(pkt, s->frame_size) < 0)
675 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
676 41536a60 Luca Abeni
677
        res = read_frame(s1, pkt);
678 0a7b514f Luca Abeni
    } else {
679 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
680 0a7b514f Luca Abeni
    }
681
    if (res < 0) {
682 411f5c6a Benoit Fouet
        return res;
683 0a7b514f Luca Abeni
    }
684
685
    if (s1->streams[0]->codec->coded_frame) {
686
        s1->streams[0]->codec->coded_frame->interlaced_frame = 1;
687
        s1->streams[0]->codec->coded_frame->top_field_first = s->top_field_first;
688
    }
689
690 158aa9f2 Luca Abeni
    return pkt->size;
691 0a7b514f Luca Abeni
}
692
693
static int v4l2_read_close(AVFormatContext *s1)
694
{
695
    struct video_data *s = s1->priv_data;
696
697
    if (s->io_method == io_mmap) {
698
        mmap_close(s);
699
    }
700
701
    close(s->fd);
702
    return 0;
703
}
704
705 ff70e601 Måns Rullgård
AVInputFormat v4l2_demuxer = {
706 0a7b514f Luca Abeni
    "video4linux2",
707 df2a3f27 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
708 0a7b514f Luca Abeni
    sizeof(struct video_data),
709
    NULL,
710
    v4l2_read_header,
711
    v4l2_read_packet,
712
    v4l2_read_close,
713
    .flags = AVFMT_NOFILE,
714
};