FFmpeg  4.4.9
v4l2_context.c
Go to the documentation of this file.
1 /*
2  * V4L2 context helper functions.
3  *
4  * Copyright (C) 2017 Alexis Ballier <aballier@gentoo.org>
5  * Copyright (C) 2017 Jorge Ramirez <jorge.ramirez-ortiz@linaro.org>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <linux/videodev2.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <poll.h>
30 #include "libavcodec/avcodec.h"
31 #include "libavcodec/internal.h"
32 #include "v4l2_buffers.h"
33 #include "v4l2_fmt.h"
34 #include "v4l2_m2m.h"
35 
37  uint32_t v4l2_fmt;
39 
40  enum AVPixelFormat av_fmt;
42 };
43 
45 {
46  return V4L2_TYPE_IS_OUTPUT(ctx->type) ?
47  container_of(ctx, V4L2m2mContext, output) :
48  container_of(ctx, V4L2m2mContext, capture);
49 }
50 
52 {
53  return ctx_to_m2mctx(ctx)->avctx;
54 }
55 
56 static inline unsigned int v4l2_get_width(struct v4l2_format *fmt)
57 {
58  return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.width : fmt->fmt.pix.width;
59 }
60 
61 static inline unsigned int v4l2_get_height(struct v4l2_format *fmt)
62 {
63  return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.height : fmt->fmt.pix.height;
64 }
65 
67 {
68  struct AVRational sar = { 0, 1 };
69  struct v4l2_cropcap cropcap;
70  int ret;
71 
72  memset(&cropcap, 0, sizeof(cropcap));
73  cropcap.type = ctx->type;
74 
75  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_CROPCAP, &cropcap);
76  if (ret)
77  return sar;
78 
79  sar.num = cropcap.pixelaspect.numerator;
80  sar.den = cropcap.pixelaspect.denominator;
81  return sar;
82 }
83 
84 static inline unsigned int v4l2_resolution_changed(V4L2Context *ctx, struct v4l2_format *fmt2)
85 {
86  struct v4l2_format *fmt1 = &ctx->format;
87  int ret = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
88  fmt1->fmt.pix_mp.width != fmt2->fmt.pix_mp.width ||
89  fmt1->fmt.pix_mp.height != fmt2->fmt.pix_mp.height
90  :
91  fmt1->fmt.pix.width != fmt2->fmt.pix.width ||
92  fmt1->fmt.pix.height != fmt2->fmt.pix.height;
93 
94  if (ret)
95  av_log(logger(ctx), AV_LOG_DEBUG, "%s changed (%dx%d) -> (%dx%d)\n",
96  ctx->name,
97  v4l2_get_width(fmt1), v4l2_get_height(fmt1),
98  v4l2_get_width(fmt2), v4l2_get_height(fmt2));
99 
100  return ret;
101 }
102 
104 {
105  return ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
106  ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
107  ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
108  ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
109 }
110 
112 {
114  const int SZ_4K = 0x1000;
115  int size;
116 
117  if (s->avctx && av_codec_is_decoder(s->avctx->codec))
118  return ((width * height * 3 / 2) / 2) + 128;
119 
120  /* encoder */
121  size = FFALIGN(height, 32) * FFALIGN(width, 32) * 3 / 2 / 2;
122  return FFALIGN(size, SZ_4K);
123 }
124 
125 static inline void v4l2_save_to_context(V4L2Context* ctx, struct v4l2_format_update *fmt)
126 {
127  ctx->format.type = ctx->type;
128 
129  if (fmt->update_avfmt)
130  ctx->av_pix_fmt = fmt->av_fmt;
131 
132  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
133  /* update the sizes to handle the reconfiguration of the capture stream at runtime */
134  ctx->format.fmt.pix_mp.height = ctx->height;
135  ctx->format.fmt.pix_mp.width = ctx->width;
136  if (fmt->update_v4l2) {
137  ctx->format.fmt.pix_mp.pixelformat = fmt->v4l2_fmt;
138 
139  /* s5p-mfc requires the user to specify a buffer size */
140  ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage =
141  v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
142  }
143  } else {
144  ctx->format.fmt.pix.height = ctx->height;
145  ctx->format.fmt.pix.width = ctx->width;
146  if (fmt->update_v4l2) {
147  ctx->format.fmt.pix.pixelformat = fmt->v4l2_fmt;
148 
149  /* s5p-mfc requires the user to specify a buffer size */
150  ctx->format.fmt.pix.sizeimage =
151  v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
152  }
153  }
154 }
155 
157 {
158  struct v4l2_decoder_cmd cmd = {
159  .cmd = V4L2_DEC_CMD_START,
160  .flags = 0,
161  };
162  int ret;
163 
164  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd);
165  if (ret)
166  return AVERROR(errno);
167 
168  return 0;
169 }
170 
171 /**
172  * handle resolution change event and end of stream event
173  * returns 1 if reinit was successful, negative if it failed
174  * returns 0 if reinit was not executed
175  */
177 {
179  struct v4l2_format cap_fmt = s->capture.format;
180  struct v4l2_event evt = { 0 };
181  int reinit, ret;
182 
183  ret = ioctl(s->fd, VIDIOC_DQEVENT, &evt);
184  if (ret < 0) {
185  av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_DQEVENT\n", ctx->name);
186  return 0;
187  }
188 
189  if (evt.type == V4L2_EVENT_EOS) {
190  ctx->done = 1;
191  return 0;
192  }
193 
194  if (evt.type != V4L2_EVENT_SOURCE_CHANGE)
195  return 0;
196 
197  ret = ioctl(s->fd, VIDIOC_G_FMT, &cap_fmt);
198  if (ret) {
199  av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->capture.name);
200  return 0;
201  }
202 
203  reinit = v4l2_resolution_changed(&s->capture, &cap_fmt);
204  if (reinit) {
205  s->capture.height = v4l2_get_height(&cap_fmt);
206  s->capture.width = v4l2_get_width(&cap_fmt);
207  s->capture.sample_aspect_ratio = v4l2_get_sar(&s->capture);
208  } else {
210  return 0;
211  }
212 
213  if (reinit)
214  s->reinit = 1;
215 
216  if (reinit) {
217  if (s->avctx)
218  ret = ff_set_dimensions(s->avctx, s->capture.width, s->capture.height);
219  if (ret < 0)
220  av_log(logger(ctx), AV_LOG_WARNING, "update avcodec height and width\n");
221 
223  if (ret) {
224  av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_reinit\n");
225  return AVERROR(EINVAL);
226  }
227  goto reinit_run;
228  }
229 
230  /* dummy event received */
231  return 0;
232 
233  /* reinit executed */
234 reinit_run:
235  return 1;
236 }
237 
239 {
240  struct v4l2_decoder_cmd cmd = {
241  .cmd = V4L2_DEC_CMD_STOP,
242  .flags = 0,
243  };
244  int ret;
245 
246  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd);
247  if (ret) {
248  /* DECODER_CMD is optional */
249  if (errno == ENOTTY)
250  return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
251  else
252  return AVERROR(errno);
253  }
254 
255  return 0;
256 }
257 
259 {
260  struct v4l2_encoder_cmd cmd = {
261  .cmd = V4L2_ENC_CMD_STOP,
262  .flags = 0,
263  };
264  int ret;
265 
266  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENCODER_CMD, &cmd);
267  if (ret) {
268  /* ENCODER_CMD is optional */
269  if (errno == ENOTTY)
270  return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
271  else
272  return AVERROR(errno);
273  }
274 
275  return 0;
276 }
277 
279 {
280  struct v4l2_plane planes[VIDEO_MAX_PLANES];
281  struct v4l2_buffer buf = { 0 };
282  V4L2Buffer *avbuf;
283  struct pollfd pfd = {
284  .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, /* default blocking capture */
285  .fd = ctx_to_m2mctx(ctx)->fd,
286  };
287  int i, ret;
288 
289  if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx->buffers) {
290  for (i = 0; i < ctx->num_buffers; i++) {
291  if (ctx->buffers[i].status == V4L2BUF_IN_DRIVER)
292  break;
293  }
294  if (i == ctx->num_buffers)
295  av_log(logger(ctx), AV_LOG_WARNING, "All capture buffers returned to "
296  "userspace. Increase num_capture_buffers "
297  "to prevent device deadlock or dropped "
298  "packets/frames.\n");
299  }
300 
301  /* if we are draining and there are no more capture buffers queued in the driver we are done */
302  if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx_to_m2mctx(ctx)->draining) {
303  for (i = 0; i < ctx->num_buffers; i++) {
304  /* capture buffer initialization happens during decode hence
305  * detection happens at runtime
306  */
307  if (!ctx->buffers)
308  break;
309 
310  if (ctx->buffers[i].status == V4L2BUF_IN_DRIVER)
311  goto start;
312  }
313  ctx->done = 1;
314  return NULL;
315  }
316 
317 start:
318  if (V4L2_TYPE_IS_OUTPUT(ctx->type))
319  pfd.events = POLLOUT | POLLWRNORM;
320  else {
321  /* no need to listen to requests for more input while draining */
322  if (ctx_to_m2mctx(ctx)->draining)
323  pfd.events = POLLIN | POLLRDNORM | POLLPRI;
324  }
325 
326  for (;;) {
327  ret = poll(&pfd, 1, timeout);
328  if (ret > 0)
329  break;
330  if (errno == EINTR)
331  continue;
332  return NULL;
333  }
334 
335  /* 0. handle errors */
336  if (pfd.revents & POLLERR) {
337  /* if we are trying to get free buffers but none have been queued yet,
338  * or if no buffers have been allocated yet, no need to raise a warning
339  */
340  if (timeout == 0) {
341  if (!ctx->buffers)
342  return NULL;
343 
344  for (i = 0; i < ctx->num_buffers; i++) {
345  if (ctx->buffers[i].status != V4L2BUF_AVAILABLE)
346  av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
347  }
348  }
349  else
350  av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
351 
352  return NULL;
353  }
354 
355  /* 1. handle resolution changes */
356  if (pfd.revents & POLLPRI) {
357  ret = v4l2_handle_event(ctx);
358  if (ret < 0) {
359  /* if re-init failed, abort */
360  ctx->done = 1;
361  return NULL;
362  }
363  if (ret) {
364  /* if re-init was successful drop the buffer (if there was one)
365  * since we had to reconfigure capture (unmap all buffers)
366  */
367  return NULL;
368  }
369  }
370 
371  /* 2. dequeue the buffer */
372  if (pfd.revents & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) {
373 
374  if (!V4L2_TYPE_IS_OUTPUT(ctx->type)) {
375  /* there is a capture buffer ready */
376  if (pfd.revents & (POLLIN | POLLRDNORM))
377  goto dequeue;
378 
379  /* the driver is ready to accept more input; instead of waiting for the capture
380  * buffer to complete we return NULL so input can proceed (we are single threaded)
381  */
382  if (pfd.revents & (POLLOUT | POLLWRNORM))
383  return NULL;
384  }
385 
386 dequeue:
387  memset(&buf, 0, sizeof(buf));
388  buf.memory = V4L2_MEMORY_MMAP;
389  buf.type = ctx->type;
390  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
391  memset(planes, 0, sizeof(planes));
392  buf.length = VIDEO_MAX_PLANES;
393  buf.m.planes = planes;
394  }
395 
396  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DQBUF, &buf);
397  if (ret) {
398  if (errno != EAGAIN) {
399  ctx->done = 1;
400  if (errno != EPIPE)
401  av_log(logger(ctx), AV_LOG_DEBUG, "%s VIDIOC_DQBUF, errno (%s)\n",
402  ctx->name, av_err2str(AVERROR(errno)));
403  }
404  return NULL;
405  }
406 
407  if (ctx_to_m2mctx(ctx)->draining && !V4L2_TYPE_IS_OUTPUT(ctx->type)) {
408  int bytesused = V4L2_TYPE_IS_MULTIPLANAR(buf.type) ?
409  buf.m.planes[0].bytesused : buf.bytesused;
410  if (bytesused == 0) {
411  ctx->done = 1;
412  return NULL;
413  }
414 #ifdef V4L2_BUF_FLAG_LAST
415  if (buf.flags & V4L2_BUF_FLAG_LAST)
416  ctx->done = 1;
417 #endif
418  }
419 
420  avbuf = &ctx->buffers[buf.index];
421  avbuf->status = V4L2BUF_AVAILABLE;
422  avbuf->buf = buf;
423  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
424  memcpy(avbuf->planes, planes, sizeof(planes));
425  avbuf->buf.m.planes = avbuf->planes;
426  }
427  return avbuf;
428  }
429 
430  return NULL;
431 }
432 
434 {
435  int timeout = 0; /* return when no more buffers to dequeue */
436  int i;
437 
438  /* get back as many output buffers as possible */
439  if (V4L2_TYPE_IS_OUTPUT(ctx->type)) {
440  do {
441  } while (v4l2_dequeue_v4l2buf(ctx, timeout));
442  }
443 
444  for (i = 0; i < ctx->num_buffers; i++) {
445  if (ctx->buffers[i].status == V4L2BUF_AVAILABLE)
446  return &ctx->buffers[i];
447  }
448 
449  return NULL;
450 }
451 
453 {
454  struct v4l2_requestbuffers req = {
455  .memory = V4L2_MEMORY_MMAP,
456  .type = ctx->type,
457  .count = 0, /* 0 -> unmaps buffers from the driver */
458  };
459  int i, j;
460 
461  for (i = 0; i < ctx->num_buffers; i++) {
462  V4L2Buffer *buffer = &ctx->buffers[i];
463 
464  for (j = 0; j < buffer->num_planes; j++) {
465  struct V4L2Plane_info *p = &buffer->plane_info[j];
466  if (p->mm_addr && p->length)
467  if (munmap(p->mm_addr, p->length) < 0)
468  av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
469  }
470  }
471 
472  return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
473 }
474 
476 {
477  struct v4l2_format *fmt = &ctx->format;
478  uint32_t v4l2_fmt;
479  int ret;
480 
482  if (!v4l2_fmt)
483  return AVERROR(EINVAL);
484 
485  if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type))
486  fmt->fmt.pix_mp.pixelformat = v4l2_fmt;
487  else
488  fmt->fmt.pix.pixelformat = v4l2_fmt;
489 
490  fmt->type = ctx->type;
491 
492  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, fmt);
493  if (ret)
494  return AVERROR(EINVAL);
495 
496  return 0;
497 }
498 
500 {
501  enum AVPixelFormat pixfmt = ctx->av_pix_fmt;
502  struct v4l2_fmtdesc fdesc;
503  int ret;
504 
505  memset(&fdesc, 0, sizeof(fdesc));
506  fdesc.type = ctx->type;
507 
508  if (pixfmt != AV_PIX_FMT_NONE) {
510  if (!ret)
511  return 0;
512  }
513 
514  for (;;) {
515  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
516  if (ret)
517  return AVERROR(EINVAL);
518 
521  if (ret){
522  fdesc.index++;
523  continue;
524  }
525 
526  *p = pixfmt;
527 
528  return 0;
529  }
530 
531  return AVERROR(EINVAL);
532 }
533 
534 static int v4l2_get_coded_format(V4L2Context* ctx, uint32_t *p)
535 {
536  struct v4l2_fmtdesc fdesc;
537  uint32_t v4l2_fmt;
538  int ret;
539 
540  /* translate to a valid v4l2 format */
541  v4l2_fmt = ff_v4l2_format_avcodec_to_v4l2(ctx->av_codec_id);
542  if (!v4l2_fmt)
543  return AVERROR(EINVAL);
544 
545  /* check if the driver supports this format */
546  memset(&fdesc, 0, sizeof(fdesc));
547  fdesc.type = ctx->type;
548 
549  for (;;) {
550  ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
551  if (ret)
552  return AVERROR(EINVAL);
553 
554  if (fdesc.pixelformat == v4l2_fmt)
555  break;
556 
557  fdesc.index++;
558  }
559 
560  *p = v4l2_fmt;
561 
562  return 0;
563 }
564 
565  /*****************************************************************************
566  *
567  * V4L2 Context Interface
568  *
569  *****************************************************************************/
570 
572 {
573  int type = ctx->type;
574  int ret;
575 
576  ret = ioctl(ctx_to_m2mctx(ctx)->fd, cmd, &type);
577  if (ret < 0)
578  return AVERROR(errno);
579 
580  ctx->streamon = (cmd == VIDIOC_STREAMON);
581 
582  return 0;
583 }
584 
586 {
588  V4L2Buffer* avbuf;
589  int ret;
590 
591  if (!frame) {
592  ret = v4l2_stop_encode(ctx);
593  if (ret)
594  av_log(logger(ctx), AV_LOG_ERROR, "%s stop_encode\n", ctx->name);
595  s->draining= 1;
596  return 0;
597  }
598 
599  avbuf = v4l2_getfree_v4l2buf(ctx);
600  if (!avbuf)
601  return AVERROR(EAGAIN);
602 
603  ret = ff_v4l2_buffer_avframe_to_buf(frame, avbuf);
604  if (ret)
605  return ret;
606 
607  return ff_v4l2_buffer_enqueue(avbuf);
608 }
609 
611 {
613  V4L2Buffer* avbuf;
614  int ret;
615 
616  if (!pkt->size) {
617  ret = v4l2_stop_decode(ctx);
618  if (ret)
619  av_log(logger(ctx), AV_LOG_ERROR, "%s stop_decode\n", ctx->name);
620  s->draining = 1;
621  return 0;
622  }
623 
624  avbuf = v4l2_getfree_v4l2buf(ctx);
625  if (!avbuf)
626  return AVERROR(EAGAIN);
627 
628  ret = ff_v4l2_buffer_avpkt_to_buf(pkt, avbuf);
629  if (ret)
630  return ret;
631 
632  return ff_v4l2_buffer_enqueue(avbuf);
633 }
634 
636 {
637  V4L2Buffer *avbuf;
638 
639  /*
640  * timeout=-1 blocks until:
641  * 1. decoded frame available
642  * 2. an input buffer is ready to be dequeued
643  */
644  avbuf = v4l2_dequeue_v4l2buf(ctx, timeout);
645  if (!avbuf) {
646  if (ctx->done)
647  return AVERROR_EOF;
648 
649  return AVERROR(EAGAIN);
650  }
651 
652  return ff_v4l2_buffer_buf_to_avframe(frame, avbuf);
653 }
654 
656 {
657  V4L2Buffer *avbuf;
658 
659  /*
660  * blocks until:
661  * 1. encoded packet available
662  * 2. an input buffer ready to be dequeued
663  */
664  avbuf = v4l2_dequeue_v4l2buf(ctx, -1);
665  if (!avbuf) {
666  if (ctx->done)
667  return AVERROR_EOF;
668 
669  return AVERROR(EAGAIN);
670  }
671 
672  return ff_v4l2_buffer_buf_to_avpkt(pkt, avbuf);
673 }
674 
676 {
677  struct v4l2_format_update fmt = { 0 };
678  int ret;
679 
680  if (ctx->av_codec_id == AV_CODEC_ID_RAWVIDEO) {
681  ret = v4l2_get_raw_format(ctx, &fmt.av_fmt);
682  if (ret)
683  return ret;
684 
685  fmt.update_avfmt = !probe;
686  v4l2_save_to_context(ctx, &fmt);
687 
688  /* format has been tried already */
689  return ret;
690  }
691 
692  ret = v4l2_get_coded_format(ctx, &fmt.v4l2_fmt);
693  if (ret)
694  return ret;
695 
696  fmt.update_v4l2 = 1;
697  v4l2_save_to_context(ctx, &fmt);
698 
699  return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, &ctx->format);
700 }
701 
703 {
704  return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_S_FMT, &ctx->format);
705 }
706 
708 {
709  int ret;
710 
711  if (!ctx->buffers)
712  return;
713 
714  ret = v4l2_release_buffers(ctx);
715  if (ret)
716  av_log(logger(ctx), AV_LOG_WARNING, "V4L2 failed to unmap the %s buffers\n", ctx->name);
717 
718  av_freep(&ctx->buffers);
719 }
720 
722 {
724  struct v4l2_requestbuffers req;
725  int ret, i;
726 
727  if (!v4l2_type_supported(ctx)) {
728  av_log(logger(ctx), AV_LOG_ERROR, "type %i not supported\n", ctx->type);
729  return AVERROR_PATCHWELCOME;
730  }
731 
732  ret = ioctl(s->fd, VIDIOC_G_FMT, &ctx->format);
733  if (ret)
734  av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT failed\n", ctx->name);
735 
736  memset(&req, 0, sizeof(req));
737  req.count = ctx->num_buffers;
738  req.memory = V4L2_MEMORY_MMAP;
739  req.type = ctx->type;
740  ret = ioctl(s->fd, VIDIOC_REQBUFS, &req);
741  if (ret < 0) {
742  av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_REQBUFS failed: %s\n", ctx->name, strerror(errno));
743  return AVERROR(errno);
744  }
745 
746  ctx->num_buffers = req.count;
747  ctx->buffers = av_mallocz(ctx->num_buffers * sizeof(V4L2Buffer));
748  if (!ctx->buffers) {
749  av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name);
750  return AVERROR(ENOMEM);
751  }
752 
753  for (i = 0; i < req.count; i++) {
754  ctx->buffers[i].context = ctx;
755  ret = ff_v4l2_buffer_initialize(&ctx->buffers[i], i);
756  if (ret < 0) {
757  av_log(logger(ctx), AV_LOG_ERROR, "%s buffer[%d] initialization (%s)\n", ctx->name, i, av_err2str(ret));
758  goto error;
759  }
760  }
761 
762  av_log(logger(ctx), AV_LOG_DEBUG, "%s: %s %02d buffers initialized: %04ux%04u, sizeimage %08u, bytesperline %08u\n", ctx->name,
763  V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? av_fourcc2str(ctx->format.fmt.pix_mp.pixelformat) : av_fourcc2str(ctx->format.fmt.pix.pixelformat),
764  req.count,
765  v4l2_get_width(&ctx->format),
766  v4l2_get_height(&ctx->format),
767  V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage : ctx->format.fmt.pix.sizeimage,
768  V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline : ctx->format.fmt.pix.bytesperline);
769 
770  return 0;
771 
772 error:
774 
775  av_freep(&ctx->buffers);
776 
777  return ret;
778 }
static int probe(const AVProbeData *p)
Definition: act.c:36
Libavcodec external API header.
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
static AVFrame * frame
int av_codec_is_decoder(const AVCodec *codec)
Definition: utils.c:79
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:62
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
cl_device_type type
int i
Definition: input.c:407
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:1522
enum AVPixelFormat pixfmt
Definition: kmsgrab.c:365
common internal api header.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
static const struct @323 planes[]
#define FFALIGN(x, a)
Definition: macros.h:48
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
static char buffer[20]
Definition: seek.c:32
main external API structure.
Definition: avcodec.h:536
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
V4L2Buffer (wrapper for v4l2_buffer management)
Definition: v4l2_buffers.h:41
enum V4L2Buffer_status status
Definition: v4l2_buffers.h:64
struct v4l2_buffer buf
Definition: v4l2_buffers.h:60
struct v4l2_plane planes[VIDEO_MAX_PLANES]
Definition: v4l2_buffers.h:61
AVCodecContext * avctx
Definition: v4l2_m2m.h:52
enum AVPixelFormat av_fmt
Definition: v4l2_context.c:40
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
int size
int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
Extracts the data from an AVFrame to a V4L2Buffer.
Definition: v4l2_buffers.c:412
int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
Extracts the data from an AVPacket to a V4L2Buffer.
Definition: v4l2_buffers.c:478
int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
Extracts the data from a V4L2Buffer to an AVFrame.
Definition: v4l2_buffers.c:419
int ff_v4l2_buffer_enqueue(V4L2Buffer *avbuf)
Enqueues a V4L2Buffer.
Definition: v4l2_buffers.c:561
int ff_v4l2_buffer_initialize(V4L2Buffer *avbuf, int index)
Initializes a V4L2Buffer.
Definition: v4l2_buffers.c:494
int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf)
Extracts the data from a V4L2Buffer to an AVPacket.
Definition: v4l2_buffers.c:453
@ V4L2BUF_AVAILABLE
Definition: v4l2_buffers.h:33
@ V4L2BUF_IN_DRIVER
Definition: v4l2_buffers.h:34
void ff_v4l2_context_release(V4L2Context *ctx)
Releases a V4L2Context.
Definition: v4l2_context.c:707
static void v4l2_save_to_context(V4L2Context *ctx, struct v4l2_format_update *fmt)
Definition: v4l2_context.c:125
static int v4l2_release_buffers(V4L2Context *ctx)
Definition: v4l2_context.c:452
static V4L2Buffer * v4l2_getfree_v4l2buf(V4L2Context *ctx)
Definition: v4l2_context.c:433
int ff_v4l2_context_get_format(V4L2Context *ctx, int probe)
Queries the driver for a valid v4l2 format and copies it to the context.
Definition: v4l2_context.c:675
static int v4l2_get_framesize_compressed(V4L2Context *ctx, int width, int height)
Definition: v4l2_context.c:111
static unsigned int v4l2_get_height(struct v4l2_format *fmt)
Definition: v4l2_context.c:61
static AVRational v4l2_get_sar(V4L2Context *ctx)
Definition: v4l2_context.c:66
int ff_v4l2_context_dequeue_frame(V4L2Context *ctx, AVFrame *frame, int timeout)
Dequeues a buffer from a V4L2Context to an AVFrame.
Definition: v4l2_context.c:635
static unsigned int v4l2_get_width(struct v4l2_format *fmt)
Definition: v4l2_context.c:56
int ff_v4l2_context_set_status(V4L2Context *ctx, uint32_t cmd)
Sets the status of a V4L2Context.
Definition: v4l2_context.c:571
int ff_v4l2_context_enqueue_frame(V4L2Context *ctx, const AVFrame *frame)
Enqueues a buffer to a V4L2Context from an AVFrame.
Definition: v4l2_context.c:585
static AVCodecContext * logger(V4L2Context *ctx)
Definition: v4l2_context.c:51
int ff_v4l2_context_enqueue_packet(V4L2Context *ctx, const AVPacket *pkt)
Enqueues a buffer to a V4L2Context from an AVPacket.
Definition: v4l2_context.c:610
static int v4l2_stop_encode(V4L2Context *ctx)
Definition: v4l2_context.c:258
static int v4l2_handle_event(V4L2Context *ctx)
handle resolution change event and end of stream event returns 1 if reinit was successful,...
Definition: v4l2_context.c:176
static int v4l2_get_coded_format(V4L2Context *ctx, uint32_t *p)
Definition: v4l2_context.c:534
static int v4l2_stop_decode(V4L2Context *ctx)
Definition: v4l2_context.c:238
int ff_v4l2_context_dequeue_packet(V4L2Context *ctx, AVPacket *pkt)
Dequeues a buffer from a V4L2Context to an AVPacket.
Definition: v4l2_context.c:655
int ff_v4l2_context_set_format(V4L2Context *ctx)
Sets the V4L2Context format in the v4l2 driver.
Definition: v4l2_context.c:702
static V4L2m2mContext * ctx_to_m2mctx(V4L2Context *ctx)
Definition: v4l2_context.c:44
static int v4l2_type_supported(V4L2Context *ctx)
Definition: v4l2_context.c:103
static V4L2Buffer * v4l2_dequeue_v4l2buf(V4L2Context *ctx, int timeout)
Definition: v4l2_context.c:278
static int v4l2_start_decode(V4L2Context *ctx)
Definition: v4l2_context.c:156
static int v4l2_try_raw_format(V4L2Context *ctx, enum AVPixelFormat pixfmt)
Definition: v4l2_context.c:475
static int v4l2_get_raw_format(V4L2Context *ctx, enum AVPixelFormat *p)
Definition: v4l2_context.c:499
static unsigned int v4l2_resolution_changed(V4L2Context *ctx, struct v4l2_format *fmt2)
Definition: v4l2_context.c:84
int ff_v4l2_context_init(V4L2Context *ctx)
Initializes a V4L2Context.
Definition: v4l2_context.c:721
enum AVPixelFormat ff_v4l2_format_v4l2_to_avfmt(uint32_t v4l2_fmt, enum AVCodecID avcodec)
Definition: v4l2_fmt.c:132
uint32_t ff_v4l2_format_avcodec_to_v4l2(enum AVCodecID avcodec)
Definition: v4l2_fmt.c:112
uint32_t ff_v4l2_format_avfmt_to_v4l2(enum AVPixelFormat avfmt)
Definition: v4l2_fmt.c:122
int ff_v4l2_m2m_codec_reinit(V4L2m2mContext *s)
Reinitializes the V4L2m2mContext when the driver cannot continue processing with the capture paramete...
Definition: v4l2_m2m.c:205
#define container_of(ptr, type, member)
Definition: v4l2_m2m.h:35