Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l2.c @ 075a5560

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