Statistics
| Branch: | Revision:

ffmpeg / libavformat / v4l2.c @ 756fb7fe

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