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