Libav
v4l2.c
Go to the documentation of this file.
1 /*
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 AV_PIX_FMT_*
11  *
12  *
13  * This file is part of Libav.
14  *
15  * Libav is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * Libav is distributed in the hope that it will be useful,
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
26  * License along with Libav; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #undef __STRICT_ANSI__ //workaround due to broken kernel headers
31 #include "config.h"
32 #include "libavformat/avformat.h"
33 #include "libavformat/internal.h"
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <sys/time.h>
39 #include <poll.h>
40 #if HAVE_SYS_VIDEOIO_H
41 #include <sys/videoio.h>
42 #else
43 #include <linux/videodev2.h>
44 #endif
45 #include "libavutil/atomic.h"
46 #include "libavutil/avassert.h"
47 #include "libavutil/imgutils.h"
48 #include "libavutil/internal.h"
49 #include "libavutil/log.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/parseutils.h"
52 #include "libavutil/pixdesc.h"
53 #include "libavutil/avstring.h"
54 #include "libavutil/mathematics.h"
55 
56 static const int desired_video_buffers = 256;
57 
58 #define V4L_ALLFORMATS 3
59 #define V4L_RAWFORMATS 1
60 #define V4L_COMPFORMATS 2
61 
62 struct video_data {
63  AVClass *class;
64  int fd;
65  int frame_format; /* V4L2_PIX_FMT_* */
66  int width, height;
68  int timeout;
71 
72  int buffers;
73  volatile int buffers_queued;
74  void **buf_start;
75  unsigned int *buf_len;
76  char *standard;
77  int channel;
78  char *video_size;
80  char *pixel_format;
82  char *framerate;
83 };
84 
85 struct buff_data {
86  struct video_data *s;
87  int index;
88  int fd;
89 };
90 
91 struct fmt_map {
94  uint32_t v4l2_fmt;
95 };
96 
97 static struct fmt_map fmt_conversion_table[] = {
98  //ff_fmt codec_id v4l2_fmt
99  { AV_PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
100  { AV_PIX_FMT_YUV422P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV422P },
101  { AV_PIX_FMT_YUYV422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUYV },
102  { AV_PIX_FMT_UYVY422, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_UYVY },
103  { AV_PIX_FMT_YUV411P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV411P },
104  { AV_PIX_FMT_YUV410P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV410 },
105  { AV_PIX_FMT_RGB555, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB555 },
106  { AV_PIX_FMT_RGB565, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB565 },
107  { AV_PIX_FMT_BGR24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR24 },
108  { AV_PIX_FMT_RGB24, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB24 },
109  { AV_PIX_FMT_BGRA, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
110  { AV_PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
111  { AV_PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
112  { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
113  { AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
114 };
115 
116 static int device_open(AVFormatContext *ctx)
117 {
118  struct v4l2_capability cap;
119  int fd;
120  int res, err;
121  int flags = O_RDWR;
122 
123  if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
124  flags |= O_NONBLOCK;
125  }
126 
127  fd = avpriv_open(ctx->filename, flags);
128  if (fd < 0) {
129  err = errno;
130 
131  av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
132  ctx->filename, strerror(err));
133 
134  return AVERROR(err);
135  }
136 
137  res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
138  if (res < 0) {
139  err = errno;
140  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
141  strerror(err));
142 
143  goto fail;
144  }
145 
146  av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
147  fd, cap.capabilities);
148 
149  if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
150  av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
151  err = ENODEV;
152 
153  goto fail;
154  }
155 
156  if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
157  av_log(ctx, AV_LOG_ERROR,
158  "The device does not support the streaming I/O method.\n");
159  err = ENOSYS;
160 
161  goto fail;
162  }
163 
164  return fd;
165 
166 fail:
167  close(fd);
168  return AVERROR(err);
169 }
170 
171 static int device_init(AVFormatContext *ctx, int *width, int *height,
172  uint32_t pix_fmt)
173 {
174  struct video_data *s = ctx->priv_data;
175  int fd = s->fd;
176  struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
177  struct v4l2_pix_format *pix = &fmt.fmt.pix;
178 
179  int res;
180 
181  pix->width = *width;
182  pix->height = *height;
183  pix->pixelformat = pix_fmt;
184  pix->field = V4L2_FIELD_ANY;
185 
186  res = ioctl(fd, VIDIOC_S_FMT, &fmt);
187 
188  if ((*width != fmt.fmt.pix.width) || (*height != fmt.fmt.pix.height)) {
189  av_log(ctx, AV_LOG_INFO,
190  "The V4L2 driver changed the video from %dx%d to %dx%d\n",
191  *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height);
192  *width = fmt.fmt.pix.width;
193  *height = fmt.fmt.pix.height;
194  }
195 
196  if (pix_fmt != fmt.fmt.pix.pixelformat) {
197  av_log(ctx, AV_LOG_DEBUG,
198  "The V4L2 driver changed the pixel format "
199  "from 0x%08X to 0x%08X\n",
200  pix_fmt, fmt.fmt.pix.pixelformat);
201  res = -1;
202  }
203 
204  if (fmt.fmt.pix.field == V4L2_FIELD_INTERLACED) {
205  av_log(ctx, AV_LOG_DEBUG, "The V4L2 driver using the interlaced mode");
206  s->interlaced = 1;
207  }
208 
209  return res;
210 }
211 
212 static int first_field(int fd)
213 {
214  int res;
215  v4l2_std_id std;
216 
217  res = ioctl(fd, VIDIOC_G_STD, &std);
218  if (res < 0) {
219  return 0;
220  }
221  if (std & V4L2_STD_NTSC) {
222  return 0;
223  }
224 
225  return 1;
226 }
227 
228 static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
229 {
230  int i;
231 
232  for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
233  if ((codec_id == AV_CODEC_ID_NONE ||
234  fmt_conversion_table[i].codec_id == codec_id) &&
235  (pix_fmt == AV_PIX_FMT_NONE ||
236  fmt_conversion_table[i].ff_fmt == pix_fmt)) {
237  return fmt_conversion_table[i].v4l2_fmt;
238  }
239  }
240 
241  return 0;
242 }
243 
244 static enum AVPixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
245 {
246  int i;
247 
248  for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
249  if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt &&
250  fmt_conversion_table[i].codec_id == codec_id) {
251  return fmt_conversion_table[i].ff_fmt;
252  }
253  }
254 
255  return AV_PIX_FMT_NONE;
256 }
257 
258 static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
259 {
260  int i;
261 
262  for (i = 0; i < FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
263  if (fmt_conversion_table[i].v4l2_fmt == v4l2_fmt) {
264  return fmt_conversion_table[i].codec_id;
265  }
266  }
267 
268  return AV_CODEC_ID_NONE;
269 }
270 
271 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
272 static void list_framesizes(AVFormatContext *ctx, int fd, uint32_t pixelformat)
273 {
274  struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
275 
276  while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
277  switch (vfse.type) {
278  case V4L2_FRMSIZE_TYPE_DISCRETE:
279  av_log(ctx, AV_LOG_INFO, " %ux%u",
280  vfse.discrete.width, vfse.discrete.height);
281  break;
282  case V4L2_FRMSIZE_TYPE_CONTINUOUS:
283  case V4L2_FRMSIZE_TYPE_STEPWISE:
284  av_log(ctx, AV_LOG_INFO, " {%u-%u, %u}x{%u-%u, %u}",
285  vfse.stepwise.min_width,
286  vfse.stepwise.max_width,
287  vfse.stepwise.step_width,
288  vfse.stepwise.min_height,
289  vfse.stepwise.max_height,
290  vfse.stepwise.step_height);
291  }
292  vfse.index++;
293  }
294 }
295 #endif
296 
297 static void list_formats(AVFormatContext *ctx, int fd, int type)
298 {
299  struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
300 
301  while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
302  enum AVCodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
303  enum AVPixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
304 
305  vfd.index++;
306 
307  if (!(vfd.flags & V4L2_FMT_FLAG_COMPRESSED) &&
308  type & V4L_RAWFORMATS) {
309  const char *fmt_name = av_get_pix_fmt_name(pix_fmt);
310  av_log(ctx, AV_LOG_INFO, "R : %9s : %20s :",
311  fmt_name ? fmt_name : "Unsupported",
312  vfd.description);
313  } else if (vfd.flags & V4L2_FMT_FLAG_COMPRESSED &&
314  type & V4L_COMPFORMATS) {
315  AVCodec *codec = avcodec_find_encoder(codec_id);
316  av_log(ctx, AV_LOG_INFO, "C : %9s : %20s :",
317  codec ? codec->name : "Unsupported",
318  vfd.description);
319  } else {
320  continue;
321  }
322 
323 #ifdef V4L2_FMT_FLAG_EMULATED
324  if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
325  av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
326  continue;
327  }
328 #endif
329 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
330  list_framesizes(ctx, fd, vfd.pixelformat);
331 #endif
332  av_log(ctx, AV_LOG_INFO, "\n");
333  }
334 }
335 
336 static int mmap_init(AVFormatContext *ctx)
337 {
338  int i, res;
339  struct video_data *s = ctx->priv_data;
340  struct v4l2_requestbuffers req = {
341  .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
342  .count = desired_video_buffers,
343  .memory = V4L2_MEMORY_MMAP
344  };
345 
346  res = ioctl(s->fd, VIDIOC_REQBUFS, &req);
347  if (res < 0) {
348  if (errno == EINVAL) {
349  av_log(ctx, AV_LOG_ERROR, "Device does not support mmap\n");
350  } else {
351  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_REQBUFS)\n");
352  }
353 
354  return AVERROR(errno);
355  }
356 
357  if (req.count < 2) {
358  av_log(ctx, AV_LOG_ERROR, "Insufficient buffer memory\n");
359 
360  return AVERROR(ENOMEM);
361  }
362  s->buffers = req.count;
363  s->buf_start = av_malloc(sizeof(void *) * s->buffers);
364  if (!s->buf_start) {
365  av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
366 
367  return AVERROR(ENOMEM);
368  }
369  s->buf_len = av_malloc(sizeof(unsigned int) * s->buffers);
370  if (!s->buf_len) {
371  av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
372  av_free(s->buf_start);
373 
374  return AVERROR(ENOMEM);
375  }
376 
377  for (i = 0; i < req.count; i++) {
378  struct v4l2_buffer buf = {
379  .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
380  .index = i,
381  .memory = V4L2_MEMORY_MMAP
382  };
383 
384  res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf);
385  if (res < 0) {
386  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n");
387 
388  return AVERROR(errno);
389  }
390 
391  s->buf_len[i] = buf.length;
392  if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
393  av_log(ctx, AV_LOG_ERROR,
394  "Buffer len [%d] = %d != %d\n",
395  i, s->buf_len[i], s->frame_size);
396 
397  return -1;
398  }
399  s->buf_start[i] = mmap(NULL, buf.length,
400  PROT_READ | PROT_WRITE, MAP_SHARED,
401  s->fd, buf.m.offset);
402 
403  if (s->buf_start[i] == MAP_FAILED) {
404  av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", strerror(errno));
405 
406  return AVERROR(errno);
407  }
408  }
409 
410  return 0;
411 }
412 
413 #if FF_API_DESTRUCT_PACKET
414 static void dummy_release_buffer(AVPacket *pkt)
415 {
416  av_assert0(0);
417 }
418 #endif
419 
420 static void mmap_release_buffer(void *opaque, uint8_t *data)
421 {
422  struct v4l2_buffer buf = { 0 };
423  int res, fd;
424  struct buff_data *buf_descriptor = opaque;
425  struct video_data *s = buf_descriptor->s;
426 
427  buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
428  buf.memory = V4L2_MEMORY_MMAP;
429  buf.index = buf_descriptor->index;
430  fd = buf_descriptor->fd;
431  av_free(buf_descriptor);
432 
433  res = ioctl(fd, VIDIOC_QBUF, &buf);
434  if (res < 0)
435  av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
436  strerror(errno));
438 }
439 
441 {
442  struct video_data *s = ctx->priv_data;
443  struct v4l2_buffer buf = {
444  .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
445  .memory = V4L2_MEMORY_MMAP
446  };
447  struct pollfd p = { .fd = s->fd, .events = POLLIN };
448  int res;
449 
450  res = poll(&p, 1, s->timeout);
451  if (res < 0)
452  return AVERROR(errno);
453 
454  if (!(p.revents & (POLLIN | POLLERR | POLLHUP)))
455  return AVERROR(EAGAIN);
456 
457  /* FIXME: Some special treatment might be needed in case of loss of signal... */
458  while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR));
459  if (res < 0) {
460  if (errno == EAGAIN) {
461  pkt->size = 0;
462 
463  return AVERROR(EAGAIN);
464  }
465  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_DQBUF): %s\n",
466  strerror(errno));
467 
468  return AVERROR(errno);
469  }
470 
471  if (buf.index >= s->buffers) {
472  av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n");
473  return AVERROR(EINVAL);
474  }
476  // always keep at least one buffer queued
478 
479  if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
480  av_log(ctx, AV_LOG_ERROR,
481  "The v4l2 frame is %d bytes, but %d bytes are expected\n",
482  buf.bytesused, s->frame_size);
483 
484  return AVERROR_INVALIDDATA;
485  }
486 
487  /* Image is at s->buff_start[buf.index] */
488  if (avpriv_atomic_int_get(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
489  /* when we start getting low on queued buffers, fall back on copying data */
490  res = av_new_packet(pkt, buf.bytesused);
491  if (res < 0) {
492  av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
493  return res;
494  }
495  memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
496 
497  res = ioctl(s->fd, VIDIOC_QBUF, &buf);
498  if (res < 0) {
499  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF)\n");
500  av_free_packet(pkt);
501  return AVERROR(errno);
502  }
504  } else {
505  struct buff_data *buf_descriptor;
506 
507  pkt->data = s->buf_start[buf.index];
508  pkt->size = buf.bytesused;
509 #if FF_API_DESTRUCT_PACKET
511  pkt->destruct = dummy_release_buffer;
513 #endif
514 
515  buf_descriptor = av_malloc(sizeof(struct buff_data));
516  if (!buf_descriptor) {
517  /* Something went wrong... Since av_malloc() failed, we cannot even
518  * allocate a buffer for memcpying into it
519  */
520  av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
521  res = ioctl(s->fd, VIDIOC_QBUF, &buf);
522 
523  return AVERROR(ENOMEM);
524  }
525  buf_descriptor->fd = s->fd;
526  buf_descriptor->index = buf.index;
527  buf_descriptor->s = s;
528 
529  pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer,
530  buf_descriptor, 0);
531  if (!pkt->buf) {
532  av_freep(&buf_descriptor);
533  return AVERROR(ENOMEM);
534  }
535  }
536  pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
537 
538  return s->buf_len[buf.index];
539 }
540 
541 static int mmap_start(AVFormatContext *ctx)
542 {
543  struct video_data *s = ctx->priv_data;
544  enum v4l2_buf_type type;
545  int i, res;
546 
547  for (i = 0; i < s->buffers; i++) {
548  struct v4l2_buffer buf = {
549  .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
550  .index = i,
551  .memory = V4L2_MEMORY_MMAP
552  };
553 
554  res = ioctl(s->fd, VIDIOC_QBUF, &buf);
555  if (res < 0) {
556  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
557  strerror(errno));
558 
559  return AVERROR(errno);
560  }
561  }
562  s->buffers_queued = s->buffers;
563 
564  type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
565  res = ioctl(s->fd, VIDIOC_STREAMON, &type);
566  if (res < 0) {
567  av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
568  strerror(errno));
569 
570  return AVERROR(errno);
571  }
572 
573  return 0;
574 }
575 
576 static void mmap_close(struct video_data *s)
577 {
578  enum v4l2_buf_type type;
579  int i;
580 
581  type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
582  /* We do not check for the result, because we could
583  * not do anything about it anyway...
584  */
585  ioctl(s->fd, VIDIOC_STREAMOFF, &type);
586  for (i = 0; i < s->buffers; i++) {
587  munmap(s->buf_start[i], s->buf_len[i]);
588  }
589  av_free(s->buf_start);
590  av_free(s->buf_len);
591 }
592 
594 {
595  struct video_data *s = s1->priv_data;
596  struct v4l2_input input = { 0 };
597  struct v4l2_standard standard = { 0 };
598  struct v4l2_streamparm streamparm = { 0 };
599  struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe;
600  AVRational framerate_q = { 0 };
601  int i, ret;
602 
603  streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
604 
605  if (s->framerate &&
606  (ret = av_parse_video_rate(&framerate_q, s->framerate)) < 0) {
607  av_log(s1, AV_LOG_ERROR, "Could not parse framerate '%s'.\n",
608  s->framerate);
609  return ret;
610  }
611 
612  /* set tv video input */
613  input.index = s->channel;
614  if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
615  av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n");
616  return AVERROR(EIO);
617  }
618 
619  av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set input_id: %d, input: %s\n",
620  s->channel, input.name);
621  if (ioctl(s->fd, VIDIOC_S_INPUT, &input.index) < 0) {
622  av_log(s1, AV_LOG_ERROR,
623  "The V4L2 driver ioctl set input(%d) failed\n",
624  s->channel);
625  return AVERROR(EIO);
626  }
627 
628  if (s->standard) {
629  av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n",
630  s->standard);
631  /* set tv standard */
632  for(i=0;;i++) {
633  standard.index = i;
634  if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) {
635  av_log(s1, AV_LOG_ERROR,
636  "The V4L2 driver ioctl set standard(%s) failed\n",
637  s->standard);
638  return AVERROR(EIO);
639  }
640 
641  if (!av_strcasecmp(standard.name, s->standard)) {
642  break;
643  }
644  }
645 
646  av_log(s1, AV_LOG_DEBUG,
647  "The V4L2 driver set standard: %s, id: %"PRIu64"\n",
648  s->standard, (uint64_t)standard.id);
649  if (ioctl(s->fd, VIDIOC_S_STD, &standard.id) < 0) {
650  av_log(s1, AV_LOG_ERROR,
651  "The V4L2 driver ioctl set standard(%s) failed\n",
652  s->standard);
653  return AVERROR(EIO);
654  }
655  }
656 
657  if (framerate_q.num && framerate_q.den) {
658  av_log(s1, AV_LOG_DEBUG, "Setting time per frame to %d/%d\n",
659  framerate_q.den, framerate_q.num);
660  tpf->numerator = framerate_q.den;
661  tpf->denominator = framerate_q.num;
662 
663  if (ioctl(s->fd, VIDIOC_S_PARM, &streamparm) != 0) {
664  av_log(s1, AV_LOG_ERROR,
665  "ioctl set time per frame(%d/%d) failed\n",
666  framerate_q.den, framerate_q.num);
667  return AVERROR(EIO);
668  }
669 
670  if (framerate_q.num != tpf->denominator ||
671  framerate_q.den != tpf->numerator) {
672  av_log(s1, AV_LOG_INFO,
673  "The driver changed the time per frame from "
674  "%d/%d to %d/%d\n",
675  framerate_q.den, framerate_q.num,
676  tpf->numerator, tpf->denominator);
677  }
678  } else {
679  if (ioctl(s->fd, VIDIOC_G_PARM, &streamparm) != 0) {
680  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_PARM): %s\n",
681  strerror(errno));
682  return AVERROR(errno);
683  }
684  }
685  s1->streams[0]->avg_frame_rate.num = tpf->denominator;
686  s1->streams[0]->avg_frame_rate.den = tpf->numerator;
687 
688  s->timeout = 100 +
690  (AVRational){1, 1000});
691 
692  return 0;
693 }
694 
695 static uint32_t device_try_init(AVFormatContext *s1,
696  enum AVPixelFormat pix_fmt,
697  int *width,
698  int *height,
699  enum AVCodecID *codec_id)
700 {
701  uint32_t desired_format = fmt_ff2v4l(pix_fmt, s1->video_codec_id);
702 
703  if (desired_format == 0 ||
704  device_init(s1, width, height, desired_format) < 0) {
705  int i;
706 
707  desired_format = 0;
708  for (i = 0; i<FF_ARRAY_ELEMS(fmt_conversion_table); i++) {
709  if (s1->video_codec_id == AV_CODEC_ID_NONE ||
710  fmt_conversion_table[i].codec_id == s1->video_codec_id) {
711  desired_format = fmt_conversion_table[i].v4l2_fmt;
712  if (device_init(s1, width, height, desired_format) >= 0) {
713  break;
714  }
715  desired_format = 0;
716  }
717  }
718  }
719 
720  if (desired_format != 0) {
721  *codec_id = fmt_v4l2codec(desired_format);
722  assert(*codec_id != AV_CODEC_ID_NONE);
723  }
724 
725  return desired_format;
726 }
727 
729 {
730  struct video_data *s = s1->priv_data;
731  AVStream *st;
732  int res = 0;
733  uint32_t desired_format;
734  enum AVCodecID codec_id;
736 
737  st = avformat_new_stream(s1, NULL);
738  if (!st)
739  return AVERROR(ENOMEM);
740 
741  s->fd = device_open(s1);
742  if (s->fd < 0)
743  return s->fd;
744 
745  if (s->list_format) {
746  list_formats(s1, s->fd, s->list_format);
747  return AVERROR_EXIT;
748  }
749 
750  avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
751 
752  if (s->video_size &&
753  (res = av_parse_video_size(&s->width, &s->height, s->video_size)) < 0) {
754  av_log(s1, AV_LOG_ERROR, "Could not parse video size '%s'.\n",
755  s->video_size);
756  return res;
757  }
758 
759  if (s->pixel_format) {
761 
762  if (codec)
763  s1->video_codec_id = codec->id;
764 
765  pix_fmt = av_get_pix_fmt(s->pixel_format);
766 
767  if (pix_fmt == AV_PIX_FMT_NONE && !codec) {
768  av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n",
769  s->pixel_format);
770 
771  return AVERROR(EINVAL);
772  }
773  }
774 
775  if (!s->width && !s->height) {
776  struct v4l2_format fmt;
777 
779  "Querying the device for the current frame size\n");
780  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781  if (ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
782  av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n",
783  strerror(errno));
784  return AVERROR(errno);
785  }
786 
787  s->width = fmt.fmt.pix.width;
788  s->height = fmt.fmt.pix.height;
790  "Setting frame size to %dx%d\n", s->width, s->height);
791  }
792 
793  desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
794  &codec_id);
795  if (desired_format == 0) {
796  av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
797  "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);
798  close(s->fd);
799 
800  return AVERROR(EIO);
801  }
802 
803  if ((res = av_image_check_size(s->width, s->height, 0, s1) < 0))
804  return res;
805 
806  s->frame_format = desired_format;
807 
808  if ((res = v4l2_set_parameters(s1) < 0))
809  return res;
810 
811  st->codec->pix_fmt = fmt_v4l2ff(desired_format, codec_id);
812  s->frame_size =
814 
815  if ((res = mmap_init(s1)) ||
816  (res = mmap_start(s1)) < 0) {
817  close(s->fd);
818  return res;
819  }
820 
821  s->top_field_first = first_field(s->fd);
822 
824  st->codec->codec_id = codec_id;
825  if (codec_id == AV_CODEC_ID_RAWVIDEO)
826  st->codec->codec_tag =
828  st->codec->width = s->width;
829  st->codec->height = s->height;
830  st->codec->bit_rate = s->frame_size * av_q2d(st->avg_frame_rate) * 8;
831 
832  return 0;
833 }
834 
836 {
837  struct video_data *s = s1->priv_data;
838  AVFrame *frame = s1->streams[0]->codec->coded_frame;
839  int res;
840 
841  av_init_packet(pkt);
842  if ((res = mmap_read_frame(s1, pkt)) < 0) {
843  return res;
844  }
845 
846  if (frame && s->interlaced) {
847  frame->interlaced_frame = 1;
848  frame->top_field_first = s->top_field_first;
849  }
850 
851  return pkt->size;
852 }
853 
855 {
856  struct video_data *s = s1->priv_data;
857 
859  av_log(s1, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
860  "close.\n");
861 
862  mmap_close(s);
863 
864  close(s->fd);
865  return 0;
866 }
867 
868 #define OFFSET(x) offsetof(struct video_data, x)
869 #define DEC AV_OPT_FLAG_DECODING_PARAM
870 static const AVOption options[] = {
871  { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
872  { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
873  { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
874  { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
875  { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
876  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
877  { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC, "list_formats" },
878  { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" },
879  { "raw", "Show only non-compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_RAWFORMATS }, 0, INT_MAX, DEC, "list_formats" },
880  { "compressed", "Show only compressed formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.i64 = V4L_COMPFORMATS }, 0, INT_MAX, DEC, "list_formats" },
881  { NULL },
882 };
883 
884 static const AVClass v4l2_class = {
885  .class_name = "V4L2 indev",
886  .item_name = av_default_item_name,
887  .option = options,
888  .version = LIBAVUTIL_VERSION_INT,
889 };
890 
892  .name = "video4linux2",
893  .long_name = NULL_IF_CONFIG_SMALL("Video4Linux2 device grab"),
894  .priv_data_size = sizeof(struct video_data),
895  .read_header = v4l2_read_header,
896  .read_packet = v4l2_read_packet,
897  .read_close = v4l2_read_close,
898  .flags = AVFMT_NOFILE,
899  .priv_class = &v4l2_class,
900 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:84
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define avpriv_atomic_int_add_and_fetch
Definition: atomic_gcc.h:42
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int frame_size
Definition: v4l2.c:67
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:1782
AVOption.
Definition: opt.h:234
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:95
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2829
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2548
char * pixel_format
Set by a private option.
Definition: v4l2.c:80
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
enum AVCodecID codec_id
Definition: v4l2.c:93
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
#define FF_ARRAY_ELEMS(a)
unsigned int * buf_len
Definition: v4l2.c:75
AVCodec.
Definition: avcodec.h:2812
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
int channel
Definition: v4l2.c:77
volatile int buffers_queued
Definition: v4l2.c:73
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
static uint32_t device_try_init(AVFormatContext *s1, enum AVPixelFormat pix_fmt, int *width, int *height, enum AVCodecID *codec_id)
Definition: v4l2.c:695
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1036
uint8_t
static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
Definition: v4l2.c:440
AVOptions.
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:994
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt, or 0 if no associated fourCC code can be found.
Definition: raw.c:175
static int mmap_start(AVFormatContext *ctx)
Definition: v4l2.c:541
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static void list_formats(AVFormatContext *ctx, int fd, int type)
Definition: v4l2.c:297
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:71
char * standard
Definition: v4l2.c:76
static const int desired_video_buffers
Definition: v4l2.c:56
const char data[16]
Definition: mxf.c:70
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
int frame_format
Definition: v4l2.c:65
static int device_open(AVFormatContext *ctx)
Definition: v4l2.c:116
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1076
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
int height
Definition: v4l2.c:66
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
int interlaced
Definition: v4l2.c:69
enum AVCodecID id
Definition: avcodec.h:2826
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static int v4l2_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2.c:835
#define AVERROR(e)
Definition: error.h:43
#define avpriv_atomic_int_get
Definition: atomic_gcc.h:28
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
static const AVOption options[]
Definition: v4l2.c:870
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int fd
Definition: v4l2.c:88
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:92
void ** buf_start
Definition: v4l2.c:74
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
enum AVCodecID codec_id
Definition: mov_chan.c:432
#define OFFSET(x)
Definition: v4l2.c:868
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:780
static uint32_t fmt_ff2v4l(enum AVPixelFormat pix_fmt, enum AVCodecID codec_id)
Definition: v4l2.c:228
#define FFMAX(a, b)
Definition: common.h:55
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
common internal API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:222
int bit_rate
the average bitrate
Definition: avcodec.h:1114
char filename[1024]
input or output filename
Definition: avformat.h:998
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
int width
picture width / height.
Definition: avcodec.h:1229
static void mmap_close(struct video_data *s)
Definition: v4l2.c:576
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pix_fmt)
Definition: v4l2.c:171
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:52
int list_format
Set by a private option.
Definition: v4l2.c:81
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static enum AVPixelFormat fmt_v4l2ff(uint32_t v4l2_fmt, enum AVCodecID codec_id)
Definition: v4l2.c:244
Stream structure.
Definition: avformat.h:699
char * video_size
String describing video size, set by a private option.
Definition: v4l2.c:78
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
enum AVMediaType codec_type
Definition: avcodec.h:1058
#define V4L_ALLFORMATS
Definition: v4l2.c:58
enum AVCodecID codec_id
Definition: avcodec.h:1067
av_default_item_name
Definition: dnxhdenc.c:52
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:66
int fd
Definition: v4l2.c:64
struct video_data * s
Definition: v4l2.c:86
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
int timeout
Definition: v4l2.c:68
rational number numerator/denominator
Definition: rational.h:43
int width
Definition: v4l2.c:66
Definition: v4l2.c:91
misc parsing utilities
static int mmap_init(AVFormatContext *ctx)
Definition: v4l2.c:336
static const AVClass v4l2_class
Definition: v4l2.c:884
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition: utils.c:1806
int height
Definition: gxfenc.c:72
int index
Definition: v4l2.c:87
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
int buffers
Definition: v4l2.c:72
static int v4l2_set_parameters(AVFormatContext *s1)
Definition: v4l2.c:593
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
char * framerate
Set by a private option.
Definition: v4l2.c:82
#define V4L_COMPFORMATS
Definition: v4l2.c:60
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:231
static enum AVCodecID fmt_v4l2codec(uint32_t v4l2_fmt)
Definition: v4l2.c:258
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int den
denominator
Definition: rational.h:45
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
#define V4L_RAWFORMATS
Definition: v4l2.c:59
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
static int v4l2_read_header(AVFormatContext *s1)
Definition: v4l2.c:728
void * priv_data
Format private data.
Definition: avformat.h:950
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:85
enum AVPixelFormat ff_fmt
Definition: v4l2.c:92
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:230
AVInputFormat ff_v4l2_demuxer
Definition: v4l2.c:891
#define DEC
Definition: v4l2.c:869
static int v4l2_read_close(AVFormatContext *s1)
Definition: v4l2.c:854
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
uint32_t v4l2_fmt
Definition: v4l2.c:94
static void mmap_release_buffer(void *opaque, uint8_t *data)
Definition: v4l2.c:420
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1552
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1540
static int first_field(int fd)
Definition: v4l2.c:212
static struct fmt_map fmt_conversion_table[]
Definition: v4l2.c:97
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
Definition: v4l2.c:85
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
int top_field_first
Definition: v4l2.c:70