Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l2.c @ 92d04e76

History | View | Annotate | Download (17.1 KB)

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