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