]> git.sesse.net Git - ffmpeg/blob - libavcodec/v4l2_context.c
c28d46d77745c5aeb3f7602356f4b5f48b997b27
[ffmpeg] / libavcodec / v4l2_context.c
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
36 struct v4l2_format_update {
37     uint32_t v4l2_fmt;
38     int update_v4l2;
39
40     enum AVPixelFormat av_fmt;
41     int update_avfmt;
42 };
43
44 static inline V4L2m2mContext *ctx_to_m2mctx(V4L2Context *ctx)
45 {
46     return V4L2_TYPE_IS_OUTPUT(ctx->type) ?
47         container_of(ctx, V4L2m2mContext, output) :
48         container_of(ctx, V4L2m2mContext, capture);
49 }
50
51 static inline AVCodecContext *logger(V4L2Context *ctx)
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
66 static inline unsigned int v4l2_resolution_changed(V4L2Context *ctx, struct v4l2_format *fmt2)
67 {
68     struct v4l2_format *fmt1 = &ctx->format;
69     int ret =  V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
70         fmt1->fmt.pix_mp.width != fmt2->fmt.pix_mp.width ||
71         fmt1->fmt.pix_mp.height != fmt2->fmt.pix_mp.height
72         :
73         fmt1->fmt.pix.width != fmt2->fmt.pix.width ||
74         fmt1->fmt.pix.height != fmt2->fmt.pix.height;
75
76     if (ret)
77         av_log(logger(ctx), AV_LOG_DEBUG, "%s changed (%dx%d) -> (%dx%d)\n",
78             ctx->name,
79             v4l2_get_width(fmt1), v4l2_get_height(fmt1),
80             v4l2_get_width(fmt2), v4l2_get_height(fmt2));
81
82     return ret;
83 }
84
85 static inline int v4l2_type_supported(V4L2Context *ctx)
86 {
87     return ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
88         ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
89         ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
90         ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
91 }
92
93 static inline int v4l2_get_framesize_compressed(V4L2Context* ctx, int width, int height)
94 {
95     V4L2m2mContext *s = ctx_to_m2mctx(ctx);
96     const int SZ_4K = 0x1000;
97     int size;
98
99     if (av_codec_is_decoder(s->avctx->codec))
100         return ((width * height * 3 / 2) / 2) + 128;
101
102     /* encoder */
103     size = FFALIGN(height, 32) * FFALIGN(width, 32) * 3 / 2 / 2;
104     return FFALIGN(size, SZ_4K);
105 }
106
107 static inline void v4l2_save_to_context(V4L2Context* ctx, struct v4l2_format_update *fmt)
108 {
109     ctx->format.type = ctx->type;
110
111     if (fmt->update_avfmt)
112         ctx->av_pix_fmt = fmt->av_fmt;
113
114     if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
115         /* update the sizes to handle the reconfiguration of the capture stream at runtime */
116         ctx->format.fmt.pix_mp.height = ctx->height;
117         ctx->format.fmt.pix_mp.width = ctx->width;
118         if (fmt->update_v4l2) {
119             ctx->format.fmt.pix_mp.pixelformat = fmt->v4l2_fmt;
120
121             /* s5p-mfc requires the user to specify a buffer size */
122             ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage =
123                 v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
124         }
125     } else {
126         ctx->format.fmt.pix.height = ctx->height;
127         ctx->format.fmt.pix.width = ctx->width;
128         if (fmt->update_v4l2) {
129             ctx->format.fmt.pix.pixelformat = fmt->v4l2_fmt;
130
131             /* s5p-mfc requires the user to specify a buffer size */
132             ctx->format.fmt.pix.sizeimage =
133                 v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
134         }
135     }
136 }
137
138 /**
139  * returns 1 if reinit was successful, negative if it failed
140  * returns 0 if reinit was not executed
141  */
142 static int v4l2_handle_event(V4L2Context *ctx)
143 {
144     V4L2m2mContext *s = ctx_to_m2mctx(ctx);
145     struct v4l2_format cap_fmt = s->capture.format;
146     struct v4l2_format out_fmt = s->output.format;
147     struct v4l2_event evt = { 0 };
148     int full_reinit, reinit, ret;
149
150     ret = ioctl(s->fd, VIDIOC_DQEVENT, &evt);
151     if (ret < 0) {
152         av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_DQEVENT\n", ctx->name);
153         return 0;
154     }
155
156     if (evt.type != V4L2_EVENT_SOURCE_CHANGE)
157         return 0;
158
159     ret = ioctl(s->fd, VIDIOC_G_FMT, &out_fmt);
160     if (ret) {
161         av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->output.name);
162         return 0;
163     }
164
165     ret = ioctl(s->fd, VIDIOC_G_FMT, &cap_fmt);
166     if (ret) {
167         av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->capture.name);
168         return 0;
169     }
170
171     full_reinit = v4l2_resolution_changed(&s->output, &out_fmt);
172     if (full_reinit) {
173         s->output.height = v4l2_get_height(&out_fmt);
174         s->output.width = v4l2_get_width(&out_fmt);
175     }
176
177     reinit = v4l2_resolution_changed(&s->capture, &cap_fmt);
178     if (reinit) {
179         s->capture.height = v4l2_get_height(&cap_fmt);
180         s->capture.width = v4l2_get_width(&cap_fmt);
181     }
182
183     if (full_reinit || reinit)
184         s->reinit = 1;
185
186     if (full_reinit) {
187         ret = ff_v4l2_m2m_codec_full_reinit(s);
188         if (ret) {
189             av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_full_reinit\n");
190             return -EINVAL;
191         }
192         goto reinit_run;
193     }
194
195     if (reinit) {
196         ret = ff_set_dimensions(s->avctx, s->capture.width, s->capture.height);
197         if (ret < 0)
198             av_log(logger(ctx), AV_LOG_WARNING, "update avcodec height and width\n");
199
200         ret = ff_v4l2_m2m_codec_reinit(s);
201         if (ret) {
202             av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_reinit\n");
203             return -EINVAL;
204         }
205         goto reinit_run;
206     }
207
208     /* dummy event received */
209     return 0;
210
211     /* reinit executed */
212 reinit_run:
213     return 1;
214 }
215
216 static int v4l2_stop_decode(V4L2Context *ctx)
217 {
218     struct v4l2_decoder_cmd cmd = {
219         .cmd = V4L2_DEC_CMD_STOP,
220         .flags = 0,
221     };
222     int ret;
223
224     ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd);
225     if (ret) {
226         /* DECODER_CMD is optional */
227         if (errno == ENOTTY)
228             return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
229         else
230             return AVERROR(errno);
231     }
232
233     return 0;
234 }
235
236 static int v4l2_stop_encode(V4L2Context *ctx)
237 {
238     struct v4l2_encoder_cmd cmd = {
239         .cmd = V4L2_ENC_CMD_STOP,
240         .flags = 0,
241     };
242     int ret;
243
244     ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENCODER_CMD, &cmd);
245     if (ret) {
246         /* ENCODER_CMD is optional */
247         if (errno == ENOTTY)
248             return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
249         else
250             return AVERROR(errno);
251     }
252
253     return 0;
254 }
255
256 static V4L2Buffer* v4l2_dequeue_v4l2buf(V4L2Context *ctx, int timeout)
257 {
258     struct v4l2_plane planes[VIDEO_MAX_PLANES];
259     struct v4l2_buffer buf = { 0 };
260     V4L2Buffer* avbuf = NULL;
261     struct pollfd pfd = {
262         .events =  POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, /* default blocking capture */
263         .fd = ctx_to_m2mctx(ctx)->fd,
264     };
265     int i, ret;
266
267     /* if we are draining and there are no more capture buffers queued in the driver we are done */
268     if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx_to_m2mctx(ctx)->draining) {
269         for (i = 0; i < ctx->num_buffers; i++) {
270             /* capture buffer initialization happens during decode hence
271              * detection happens at runtime
272              */
273             if (!ctx->buffers)
274                 break;
275
276             if (ctx->buffers[i].status == V4L2BUF_IN_DRIVER)
277                 goto start;
278         }
279         ctx->done = 1;
280         return NULL;
281     }
282
283 start:
284     if (V4L2_TYPE_IS_OUTPUT(ctx->type))
285         pfd.events =  POLLOUT | POLLWRNORM;
286     else {
287         /* no need to listen to requests for more input while draining */
288         if (ctx_to_m2mctx(ctx)->draining)
289             pfd.events =  POLLIN | POLLRDNORM | POLLPRI;
290     }
291
292     for (;;) {
293         ret = poll(&pfd, 1, timeout);
294         if (ret > 0)
295             break;
296         if (errno == EINTR)
297             continue;
298         return NULL;
299     }
300
301     /* 0. handle errors */
302     if (pfd.revents & POLLERR) {
303         /* if we are trying to get free buffers but none have been queued yet
304            no need to raise a warning */
305         if (timeout == 0) {
306             for (i = 0; i < ctx->num_buffers; i++) {
307                 if (ctx->buffers[i].status != V4L2BUF_AVAILABLE)
308                     av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
309             }
310         }
311         else
312             av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
313
314         return NULL;
315     }
316
317     /* 1. handle resolution changes */
318     if (pfd.revents & POLLPRI) {
319         ret = v4l2_handle_event(ctx);
320         if (ret < 0) {
321             /* if re-init failed, abort */
322             ctx->done = 1;
323             return NULL;
324         }
325         if (ret) {
326             /* if re-init was successful drop the buffer (if there was one)
327              * since we had to reconfigure capture (unmap all buffers)
328              */
329             return NULL;
330         }
331     }
332
333     /* 2. dequeue the buffer */
334     if (pfd.revents & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) {
335
336         if (!V4L2_TYPE_IS_OUTPUT(ctx->type)) {
337             /* there is a capture buffer ready */
338             if (pfd.revents & (POLLIN | POLLRDNORM))
339                 goto dequeue;
340
341             /* the driver is ready to accept more input; instead of waiting for the capture
342              * buffer to complete we return NULL so input can proceed (we are single threaded)
343              */
344             if (pfd.revents & (POLLOUT | POLLWRNORM))
345                 return NULL;
346         }
347
348 dequeue:
349         memset(&buf, 0, sizeof(buf));
350         buf.memory = V4L2_MEMORY_MMAP;
351         buf.type = ctx->type;
352         if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
353             memset(planes, 0, sizeof(planes));
354             buf.length = VIDEO_MAX_PLANES;
355             buf.m.planes = planes;
356         }
357
358         ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DQBUF, &buf);
359         if (ret) {
360             if (errno != EAGAIN) {
361                 ctx->done = 1;
362                 if (errno != EPIPE)
363                     av_log(logger(ctx), AV_LOG_DEBUG, "%s VIDIOC_DQBUF, errno (%s)\n",
364                         ctx->name, av_err2str(AVERROR(errno)));
365             }
366             return NULL;
367         }
368
369         avbuf = &ctx->buffers[buf.index];
370         avbuf->status = V4L2BUF_AVAILABLE;
371         avbuf->buf = buf;
372         if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
373             memcpy(avbuf->planes, planes, sizeof(planes));
374             avbuf->buf.m.planes = avbuf->planes;
375         }
376         return avbuf;
377     }
378
379     return NULL;
380 }
381
382 static V4L2Buffer* v4l2_getfree_v4l2buf(V4L2Context *ctx)
383 {
384     int timeout = 0; /* return when no more buffers to dequeue */
385     int i;
386
387     /* get back as many output buffers as possible */
388     if (V4L2_TYPE_IS_OUTPUT(ctx->type)) {
389           do {
390           } while (v4l2_dequeue_v4l2buf(ctx, timeout));
391     }
392
393     for (i = 0; i < ctx->num_buffers; i++) {
394         if (ctx->buffers[i].status == V4L2BUF_AVAILABLE)
395             return &ctx->buffers[i];
396     }
397
398     return NULL;
399 }
400
401 static int v4l2_release_buffers(V4L2Context* ctx)
402 {
403     struct v4l2_requestbuffers req = {
404         .memory = V4L2_MEMORY_MMAP,
405         .type = ctx->type,
406         .count = 0, /* 0 -> unmaps buffers from the driver */
407     };
408     int i, j;
409
410     for (i = 0; i < ctx->num_buffers; i++) {
411         V4L2Buffer *buffer = &ctx->buffers[i];
412
413         for (j = 0; j < buffer->num_planes; j++) {
414             struct V4L2Plane_info *p = &buffer->plane_info[j];
415             if (p->mm_addr && p->length)
416                 if (munmap(p->mm_addr, p->length) < 0)
417                     av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
418         }
419     }
420
421     return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
422 }
423
424 static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfmt)
425 {
426     struct v4l2_format *fmt = &ctx->format;
427     uint32_t v4l2_fmt;
428     int ret;
429
430     v4l2_fmt = ff_v4l2_format_avfmt_to_v4l2(pixfmt);
431     if (!v4l2_fmt)
432         return AVERROR(EINVAL);
433
434     if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type))
435         fmt->fmt.pix_mp.pixelformat = v4l2_fmt;
436     else
437         fmt->fmt.pix.pixelformat = v4l2_fmt;
438
439     fmt->type = ctx->type;
440
441     ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, fmt);
442     if (ret)
443         return AVERROR(EINVAL);
444
445     return 0;
446 }
447
448 static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p)
449 {
450     enum AVPixelFormat pixfmt = ctx->av_pix_fmt;
451     struct v4l2_fmtdesc fdesc;
452     int ret;
453
454     memset(&fdesc, 0, sizeof(fdesc));
455     fdesc.type = ctx->type;
456
457     if (pixfmt != AV_PIX_FMT_NONE) {
458         ret = v4l2_try_raw_format(ctx, pixfmt);
459         if (!ret)
460             return 0;
461     }
462
463     for (;;) {
464         ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
465         if (ret)
466             return AVERROR(EINVAL);
467
468         pixfmt = ff_v4l2_format_v4l2_to_avfmt(fdesc.pixelformat, AV_CODEC_ID_RAWVIDEO);
469         ret = v4l2_try_raw_format(ctx, pixfmt);
470         if (ret){
471             fdesc.index++;
472             continue;
473         }
474
475         *p = pixfmt;
476
477         return 0;
478     }
479
480     return AVERROR(EINVAL);
481 }
482
483 static int v4l2_get_coded_format(V4L2Context* ctx, uint32_t *p)
484 {
485     struct v4l2_fmtdesc fdesc;
486     uint32_t v4l2_fmt;
487     int ret;
488
489     /* translate to a valid v4l2 format */
490     v4l2_fmt = ff_v4l2_format_avcodec_to_v4l2(ctx->av_codec_id);
491     if (!v4l2_fmt)
492         return AVERROR(EINVAL);
493
494     /* check if the driver supports this format */
495     memset(&fdesc, 0, sizeof(fdesc));
496     fdesc.type = ctx->type;
497
498     for (;;) {
499         ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
500         if (ret)
501             return AVERROR(EINVAL);
502
503         if (fdesc.pixelformat == v4l2_fmt)
504             break;
505
506         fdesc.index++;
507     }
508
509     *p = v4l2_fmt;
510
511     return 0;
512 }
513
514  /*****************************************************************************
515   *
516   *             V4L2 Context Interface
517   *
518   *****************************************************************************/
519
520 int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd)
521 {
522     int type = ctx->type;
523     int ret;
524
525     ret = ioctl(ctx_to_m2mctx(ctx)->fd, cmd, &type);
526     if (ret < 0)
527         return AVERROR(errno);
528
529     ctx->streamon = (cmd == VIDIOC_STREAMON);
530
531     return 0;
532 }
533
534 int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* frame)
535 {
536     V4L2m2mContext *s = ctx_to_m2mctx(ctx);
537     V4L2Buffer* avbuf;
538     int ret;
539
540     if (!frame) {
541         ret = v4l2_stop_encode(ctx);
542         if (ret)
543             av_log(logger(ctx), AV_LOG_ERROR, "%s stop_encode\n", ctx->name);
544         s->draining= 1;
545         return 0;
546     }
547
548     avbuf = v4l2_getfree_v4l2buf(ctx);
549     if (!avbuf)
550         return AVERROR(ENOMEM);
551
552     ret = ff_v4l2_buffer_avframe_to_buf(frame, avbuf);
553     if (ret)
554         return ret;
555
556     return ff_v4l2_buffer_enqueue(avbuf);
557 }
558
559 int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt)
560 {
561     V4L2m2mContext *s = ctx_to_m2mctx(ctx);
562     V4L2Buffer* avbuf;
563     int ret;
564
565     if (!pkt->size) {
566         ret = v4l2_stop_decode(ctx);
567         if (ret)
568             av_log(logger(ctx), AV_LOG_ERROR, "%s stop_decode\n", ctx->name);
569         s->draining = 1;
570         return 0;
571     }
572
573     avbuf = v4l2_getfree_v4l2buf(ctx);
574     if (!avbuf)
575         return AVERROR(ENOMEM);
576
577     ret = ff_v4l2_buffer_avpkt_to_buf(pkt, avbuf);
578     if (ret)
579         return ret;
580
581     return ff_v4l2_buffer_enqueue(avbuf);
582 }
583
584 int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* frame)
585 {
586     V4L2Buffer* avbuf = NULL;
587
588     /*
589      * blocks until:
590      *  1. decoded frame available
591      *  2. an input buffer is ready to be dequeued
592      */
593     avbuf = v4l2_dequeue_v4l2buf(ctx, -1);
594     if (!avbuf) {
595         if (ctx->done)
596             return AVERROR_EOF;
597
598         return AVERROR(EAGAIN);
599     }
600
601     return ff_v4l2_buffer_buf_to_avframe(frame, avbuf);
602 }
603
604 int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt)
605 {
606     V4L2Buffer* avbuf = NULL;
607
608     /*
609      * blocks until:
610      *  1. encoded packet available
611      *  2. an input buffer ready to be dequeued
612      */
613     avbuf = v4l2_dequeue_v4l2buf(ctx, -1);
614     if (!avbuf) {
615         if (ctx->done)
616             return AVERROR_EOF;
617
618         return AVERROR(EAGAIN);
619     }
620
621     return ff_v4l2_buffer_buf_to_avpkt(pkt, avbuf);
622 }
623
624 int ff_v4l2_context_get_format(V4L2Context* ctx)
625 {
626     struct v4l2_format_update fmt = { 0 };
627     int ret;
628
629     if  (ctx->av_codec_id == AV_CODEC_ID_RAWVIDEO) {
630         ret = v4l2_get_raw_format(ctx, &fmt.av_fmt);
631         if (ret)
632             return ret;
633
634         fmt.update_avfmt = 1;
635         v4l2_save_to_context(ctx, &fmt);
636
637         /* format has been tried already */
638         return ret;
639     }
640
641     ret = v4l2_get_coded_format(ctx, &fmt.v4l2_fmt);
642     if (ret)
643         return ret;
644
645     fmt.update_v4l2 = 1;
646     v4l2_save_to_context(ctx, &fmt);
647
648     return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, &ctx->format);
649 }
650
651 int ff_v4l2_context_set_format(V4L2Context* ctx)
652 {
653     return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_S_FMT, &ctx->format);
654 }
655
656 void ff_v4l2_context_release(V4L2Context* ctx)
657 {
658     int ret;
659
660     if (!ctx->buffers)
661         return;
662
663     ret = v4l2_release_buffers(ctx);
664     if (ret)
665         av_log(logger(ctx), AV_LOG_WARNING, "V4L2 failed to unmap the %s buffers\n", ctx->name);
666
667     av_free(ctx->buffers);
668     ctx->buffers = NULL;
669 }
670
671 int ff_v4l2_context_init(V4L2Context* ctx)
672 {
673     V4L2m2mContext *s = ctx_to_m2mctx(ctx);
674     struct v4l2_requestbuffers req;
675     int ret, i;
676
677     if (!v4l2_type_supported(ctx)) {
678         av_log(logger(ctx), AV_LOG_ERROR, "type %i not supported\n", ctx->type);
679         return AVERROR_PATCHWELCOME;
680     }
681
682     ret = ioctl(s->fd, VIDIOC_G_FMT, &ctx->format);
683     if (ret)
684         av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT failed\n", ctx->name);
685
686     memset(&req, 0, sizeof(req));
687     req.count = ctx->num_buffers;
688     req.memory = V4L2_MEMORY_MMAP;
689     req.type = ctx->type;
690     ret = ioctl(s->fd, VIDIOC_REQBUFS, &req);
691     if (ret < 0) {
692         av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_REQBUFS failed: %s\n", ctx->name, strerror(errno));
693         return AVERROR(errno);
694     }
695
696     ctx->num_buffers = req.count;
697     ctx->buffers = av_mallocz(ctx->num_buffers * sizeof(V4L2Buffer));
698     if (!ctx->buffers) {
699             av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name);
700             return AVERROR(ENOMEM);
701     }
702
703     for (i = 0; i < req.count; i++) {
704         ctx->buffers[i].context = ctx;
705         ret = ff_v4l2_buffer_initialize(&ctx->buffers[i], i);
706         if (ret < 0) {
707             av_log(logger(ctx), AV_LOG_ERROR, "%s buffer[%d] initialization (%s)\n", ctx->name, i, av_err2str(ret));
708             goto error;
709         }
710     }
711
712     av_log(logger(ctx), AV_LOG_DEBUG, "%s: %s %02d buffers initialized: %04ux%04u, sizeimage %08u, bytesperline %08u\n", ctx->name,
713         V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? av_fourcc2str(ctx->format.fmt.pix_mp.pixelformat) : av_fourcc2str(ctx->format.fmt.pix.pixelformat),
714         req.count,
715         v4l2_get_width(&ctx->format),
716         v4l2_get_height(&ctx->format),
717         V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage : ctx->format.fmt.pix.sizeimage,
718         V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline : ctx->format.fmt.pix.bytesperline);
719
720     return 0;
721
722 error:
723     v4l2_release_buffers(ctx);
724
725     av_free(ctx->buffers);
726     ctx->buffers = NULL;
727
728     return ret;
729 }