Statistics
| Branch: | Revision:

ffmpeg / libavdevice / v4l2.c @ ad40b153

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