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