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