You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

692 lines
19KB

  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. #include <linux/videodev2.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/mman.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <poll.h>
  29. #include "libavcodec/avcodec.h"
  30. #include "libavcodec/internal.h"
  31. #include "v4l2_buffers.h"
  32. #include "v4l2_fmt.h"
  33. #include "v4l2_m2m.h"
  34. struct v4l2_format_update {
  35. uint32_t v4l2_fmt;
  36. int update_v4l2;
  37. enum AVPixelFormat av_fmt;
  38. int update_avfmt;
  39. };
  40. static inline V4L2m2mContext *ctx_to_m2mctx(V4L2Context *ctx)
  41. {
  42. return V4L2_TYPE_IS_OUTPUT(ctx->type) ?
  43. container_of(ctx, V4L2m2mContext, output) :
  44. container_of(ctx, V4L2m2mContext, capture);
  45. }
  46. static inline AVCodecContext *logger(V4L2Context *ctx)
  47. {
  48. return ctx_to_m2mctx(ctx)->avctx;
  49. }
  50. static inline unsigned int v4l2_get_width(struct v4l2_format *fmt)
  51. {
  52. return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.width : fmt->fmt.pix.width;
  53. }
  54. static inline unsigned int v4l2_get_height(struct v4l2_format *fmt)
  55. {
  56. return V4L2_TYPE_IS_MULTIPLANAR(fmt->type) ? fmt->fmt.pix_mp.height : fmt->fmt.pix.height;
  57. }
  58. static inline unsigned int v4l2_resolution_changed(V4L2Context *ctx, struct v4l2_format *fmt2)
  59. {
  60. struct v4l2_format *fmt1 = &ctx->format;
  61. int ret = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
  62. fmt1->fmt.pix_mp.width != fmt2->fmt.pix_mp.width ||
  63. fmt1->fmt.pix_mp.height != fmt2->fmt.pix_mp.height
  64. :
  65. fmt1->fmt.pix.width != fmt2->fmt.pix.width ||
  66. fmt1->fmt.pix.height != fmt2->fmt.pix.height;
  67. if (ret)
  68. av_log(logger(ctx), AV_LOG_DEBUG, "%s changed (%dx%d) -> (%dx%d)\n",
  69. ctx->name,
  70. v4l2_get_width(fmt1), v4l2_get_height(fmt1),
  71. v4l2_get_width(fmt2), v4l2_get_height(fmt2));
  72. return ret;
  73. }
  74. static inline int v4l2_type_supported(V4L2Context *ctx)
  75. {
  76. return ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
  77. ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
  78. ctx->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  79. ctx->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  80. }
  81. static inline int v4l2_get_framesize_compressed(V4L2Context* ctx, int width, int height)
  82. {
  83. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  84. const int SZ_4K = 0x1000;
  85. int size;
  86. if (av_codec_is_decoder(s->avctx->codec))
  87. return ((width * height * 3 / 2) / 2) + 128;
  88. /* encoder */
  89. size = FFALIGN(height, 32) * FFALIGN(width, 32) * 3 / 2 / 2;
  90. return FFALIGN(size, SZ_4K);
  91. }
  92. static inline void v4l2_save_to_context(V4L2Context* ctx, struct v4l2_format_update *fmt)
  93. {
  94. ctx->format.type = ctx->type;
  95. if (fmt->update_avfmt)
  96. ctx->av_pix_fmt = fmt->av_fmt;
  97. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  98. /* update the sizes to handle the reconfiguration of the capture stream at runtime */
  99. ctx->format.fmt.pix_mp.height = ctx->height;
  100. ctx->format.fmt.pix_mp.width = ctx->width;
  101. if (fmt->update_v4l2) {
  102. ctx->format.fmt.pix_mp.pixelformat = fmt->v4l2_fmt;
  103. /* s5p-mfc requires the user to specify a buffer size */
  104. ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage =
  105. v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
  106. }
  107. } else {
  108. ctx->format.fmt.pix.height = ctx->height;
  109. ctx->format.fmt.pix.width = ctx->width;
  110. if (fmt->update_v4l2) {
  111. ctx->format.fmt.pix.pixelformat = fmt->v4l2_fmt;
  112. /* s5p-mfc requires the user to specify a buffer size */
  113. ctx->format.fmt.pix.sizeimage =
  114. v4l2_get_framesize_compressed(ctx, ctx->width, ctx->height);
  115. }
  116. }
  117. }
  118. /**
  119. * returns 1 if reinit was successful, negative if it failed
  120. * returns 0 if reinit was not executed
  121. */
  122. static int v4l2_handle_event(V4L2Context *ctx)
  123. {
  124. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  125. struct v4l2_format cap_fmt = s->capture.format;
  126. struct v4l2_format out_fmt = s->output.format;
  127. struct v4l2_event evt = { 0 };
  128. int full_reinit, reinit, ret;
  129. ret = ioctl(s->fd, VIDIOC_DQEVENT, &evt);
  130. if (ret < 0) {
  131. av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_DQEVENT\n", ctx->name);
  132. return 0;
  133. }
  134. if (evt.type != V4L2_EVENT_SOURCE_CHANGE)
  135. return 0;
  136. ret = ioctl(s->fd, VIDIOC_G_FMT, &out_fmt);
  137. if (ret) {
  138. av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->output.name);
  139. return 0;
  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. full_reinit = v4l2_resolution_changed(&s->output, &out_fmt);
  147. if (full_reinit) {
  148. s->output.height = v4l2_get_height(&out_fmt);
  149. s->output.width = v4l2_get_width(&out_fmt);
  150. }
  151. reinit = v4l2_resolution_changed(&s->capture, &cap_fmt);
  152. if (reinit) {
  153. s->capture.height = v4l2_get_height(&cap_fmt);
  154. s->capture.width = v4l2_get_width(&cap_fmt);
  155. }
  156. if (full_reinit || reinit)
  157. s->reinit = 1;
  158. if (full_reinit) {
  159. ret = ff_v4l2_m2m_codec_full_reinit(s);
  160. if (ret) {
  161. av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_full_reinit\n");
  162. return -EINVAL;
  163. }
  164. goto reinit_run;
  165. }
  166. if (reinit) {
  167. ret = ff_set_dimensions(s->avctx, s->capture.width, s->capture.height);
  168. if (ret < 0)
  169. av_log(logger(ctx), AV_LOG_WARNING, "update avcodec height and width\n");
  170. ret = ff_v4l2_m2m_codec_reinit(s);
  171. if (ret) {
  172. av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_reinit\n");
  173. return -EINVAL;
  174. }
  175. goto reinit_run;
  176. }
  177. /* dummy event received */
  178. return 0;
  179. /* reinit executed */
  180. reinit_run:
  181. return 1;
  182. }
  183. static int v4l2_stop_decode(V4L2Context *ctx)
  184. {
  185. struct v4l2_decoder_cmd cmd = {
  186. .cmd = V4L2_DEC_CMD_STOP,
  187. };
  188. int ret;
  189. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd);
  190. if (ret) {
  191. /* DECODER_CMD is optional */
  192. if (errno == ENOTTY)
  193. return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
  194. }
  195. return 0;
  196. }
  197. static int v4l2_stop_encode(V4L2Context *ctx)
  198. {
  199. struct v4l2_encoder_cmd cmd = {
  200. .cmd = V4L2_ENC_CMD_STOP,
  201. };
  202. int ret;
  203. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENCODER_CMD, &cmd);
  204. if (ret) {
  205. /* ENCODER_CMD is optional */
  206. if (errno == ENOTTY)
  207. return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
  208. }
  209. return 0;
  210. }
  211. static V4L2Buffer* v4l2_dequeue_v4l2buf(V4L2Context *ctx, int timeout)
  212. {
  213. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  214. struct v4l2_buffer buf = { 0 };
  215. V4L2Buffer* avbuf = NULL;
  216. struct pollfd pfd = {
  217. .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, /* default blocking capture */
  218. .fd = ctx_to_m2mctx(ctx)->fd,
  219. };
  220. int ret;
  221. if (V4L2_TYPE_IS_OUTPUT(ctx->type))
  222. pfd.events = POLLOUT | POLLWRNORM;
  223. for (;;) {
  224. ret = poll(&pfd, 1, timeout);
  225. if (ret > 0)
  226. break;
  227. if (errno == EINTR)
  228. continue;
  229. /* timeout is being used to indicate last valid bufer when draining */
  230. if (ctx_to_m2mctx(ctx)->draining)
  231. ctx->done = 1;
  232. return NULL;
  233. }
  234. /* 0. handle errors */
  235. if (pfd.revents & POLLERR) {
  236. av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
  237. return NULL;
  238. }
  239. /* 1. handle resolution changes */
  240. if (pfd.revents & POLLPRI) {
  241. ret = v4l2_handle_event(ctx);
  242. if (ret < 0) {
  243. /* if re-init failed, abort */
  244. ctx->done = EINVAL;
  245. return NULL;
  246. }
  247. if (ret) {
  248. /* if re-init was successful drop the buffer (if there was one)
  249. * since we had to reconfigure capture (unmap all buffers)
  250. */
  251. return NULL;
  252. }
  253. }
  254. /* 2. dequeue the buffer */
  255. if (pfd.revents & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) {
  256. if (!V4L2_TYPE_IS_OUTPUT(ctx->type)) {
  257. /* there is a capture buffer ready */
  258. if (pfd.revents & (POLLIN | POLLRDNORM))
  259. goto dequeue;
  260. /* the driver is ready to accept more input; instead of waiting for the capture
  261. * buffer to complete we return NULL so input can proceed (we are single threaded)
  262. */
  263. if (pfd.revents & (POLLOUT | POLLWRNORM))
  264. return NULL;
  265. }
  266. dequeue:
  267. memset(&buf, 0, sizeof(buf));
  268. buf.memory = V4L2_MEMORY_MMAP;
  269. buf.type = ctx->type;
  270. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  271. memset(planes, 0, sizeof(planes));
  272. buf.length = VIDEO_MAX_PLANES;
  273. buf.m.planes = planes;
  274. }
  275. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DQBUF, &buf);
  276. if (ret) {
  277. if (errno != EAGAIN) {
  278. ctx->done = errno;
  279. if (errno != EPIPE)
  280. av_log(logger(ctx), AV_LOG_DEBUG, "%s VIDIOC_DQBUF, errno (%s)\n",
  281. ctx->name, av_err2str(AVERROR(errno)));
  282. }
  283. } else {
  284. avbuf = &ctx->buffers[buf.index];
  285. avbuf->status = V4L2BUF_AVAILABLE;
  286. avbuf->buf = buf;
  287. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  288. memcpy(avbuf->planes, planes, sizeof(planes));
  289. avbuf->buf.m.planes = avbuf->planes;
  290. }
  291. }
  292. }
  293. return avbuf;
  294. }
  295. static V4L2Buffer* v4l2_getfree_v4l2buf(V4L2Context *ctx)
  296. {
  297. int timeout = 0; /* return when no more buffers to dequeue */
  298. int i;
  299. /* get back as many output buffers as possible */
  300. if (V4L2_TYPE_IS_OUTPUT(ctx->type)) {
  301. do {
  302. } while (v4l2_dequeue_v4l2buf(ctx, timeout));
  303. }
  304. for (i = 0; i < ctx->num_buffers; i++) {
  305. if (ctx->buffers[i].status == V4L2BUF_AVAILABLE)
  306. return &ctx->buffers[i];
  307. }
  308. return NULL;
  309. }
  310. static int v4l2_release_buffers(V4L2Context* ctx)
  311. {
  312. struct v4l2_requestbuffers req = {
  313. .memory = V4L2_MEMORY_MMAP,
  314. .type = ctx->type,
  315. .count = 0, /* 0 -> unmaps buffers from the driver */
  316. };
  317. int i, j;
  318. for (i = 0; i < ctx->num_buffers; i++) {
  319. V4L2Buffer *buffer = &ctx->buffers[i];
  320. for (j = 0; j < buffer->num_planes; j++) {
  321. struct V4L2Plane_info *p = &buffer->plane_info[j];
  322. if (p->mm_addr && p->length)
  323. if (munmap(p->mm_addr, p->length) < 0)
  324. av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
  325. }
  326. }
  327. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
  328. }
  329. static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfmt)
  330. {
  331. struct v4l2_format *fmt = &ctx->format;
  332. uint32_t v4l2_fmt;
  333. int ret;
  334. v4l2_fmt = ff_v4l2_format_avfmt_to_v4l2(pixfmt);
  335. if (!v4l2_fmt)
  336. return AVERROR(EINVAL);
  337. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type))
  338. fmt->fmt.pix_mp.pixelformat = v4l2_fmt;
  339. else
  340. fmt->fmt.pix.pixelformat = v4l2_fmt;
  341. fmt->type = ctx->type;
  342. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, fmt);
  343. if (ret)
  344. return AVERROR(EINVAL);
  345. return 0;
  346. }
  347. static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p)
  348. {
  349. enum AVPixelFormat pixfmt = ctx->av_pix_fmt;
  350. struct v4l2_fmtdesc fdesc;
  351. int ret;
  352. memset(&fdesc, 0, sizeof(fdesc));
  353. fdesc.type = ctx->type;
  354. if (pixfmt != AV_PIX_FMT_NONE) {
  355. ret = v4l2_try_raw_format(ctx, pixfmt);
  356. if (ret)
  357. pixfmt = AV_PIX_FMT_NONE;
  358. else
  359. return 0;
  360. }
  361. for (;;) {
  362. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
  363. if (ret)
  364. return AVERROR(EINVAL);
  365. pixfmt = ff_v4l2_format_v4l2_to_avfmt(fdesc.pixelformat, AV_CODEC_ID_RAWVIDEO);
  366. ret = v4l2_try_raw_format(ctx, pixfmt);
  367. if (ret){
  368. fdesc.index++;
  369. continue;
  370. }
  371. *p = pixfmt;
  372. return 0;
  373. }
  374. return AVERROR(EINVAL);
  375. }
  376. static int v4l2_get_coded_format(V4L2Context* ctx, uint32_t *p)
  377. {
  378. struct v4l2_fmtdesc fdesc;
  379. uint32_t v4l2_fmt;
  380. int ret;
  381. /* translate to a valid v4l2 format */
  382. v4l2_fmt = ff_v4l2_format_avcodec_to_v4l2(ctx->av_codec_id);
  383. if (!v4l2_fmt)
  384. return AVERROR(EINVAL);
  385. /* check if the driver supports this format */
  386. memset(&fdesc, 0, sizeof(fdesc));
  387. fdesc.type = ctx->type;
  388. for (;;) {
  389. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
  390. if (ret)
  391. return AVERROR(EINVAL);
  392. if (fdesc.pixelformat == v4l2_fmt)
  393. break;
  394. fdesc.index++;
  395. }
  396. *p = v4l2_fmt;
  397. return 0;
  398. }
  399. /*****************************************************************************
  400. *
  401. * V4L2 Context Interface
  402. *
  403. *****************************************************************************/
  404. int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd)
  405. {
  406. int type = ctx->type;
  407. int ret;
  408. ret = ioctl(ctx_to_m2mctx(ctx)->fd, cmd, &type);
  409. if (ret < 0)
  410. return AVERROR(errno);
  411. ctx->streamon = (cmd == VIDIOC_STREAMON);
  412. return 0;
  413. }
  414. int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* frame)
  415. {
  416. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  417. V4L2Buffer* avbuf;
  418. int ret;
  419. if (!frame) {
  420. ret = v4l2_stop_encode(ctx);
  421. if (ret)
  422. av_log(logger(ctx), AV_LOG_ERROR, "%s stop_encode\n", ctx->name);
  423. s->draining= 1;
  424. return 0;
  425. }
  426. avbuf = v4l2_getfree_v4l2buf(ctx);
  427. if (!avbuf)
  428. return AVERROR(ENOMEM);
  429. ret = ff_v4l2_buffer_avframe_to_buf(frame, avbuf);
  430. if (ret)
  431. return ret;
  432. return ff_v4l2_buffer_enqueue(avbuf);
  433. }
  434. int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt)
  435. {
  436. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  437. V4L2Buffer* avbuf;
  438. int ret;
  439. if (!pkt->size) {
  440. ret = v4l2_stop_decode(ctx);
  441. if (ret)
  442. av_log(logger(ctx), AV_LOG_ERROR, "%s stop_decode\n", ctx->name);
  443. s->draining = 1;
  444. return 0;
  445. }
  446. avbuf = v4l2_getfree_v4l2buf(ctx);
  447. if (!avbuf)
  448. return AVERROR(ENOMEM);
  449. ret = ff_v4l2_buffer_avpkt_to_buf(pkt, avbuf);
  450. if (ret)
  451. return ret;
  452. return ff_v4l2_buffer_enqueue(avbuf);
  453. }
  454. int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* frame)
  455. {
  456. V4L2Buffer* avbuf = NULL;
  457. /* if we are draining, we are no longer inputing data, therefore enable a
  458. * timeout so we can dequeue and flag the last valid buffer.
  459. *
  460. * blocks until:
  461. * 1. decoded frame available
  462. * 2. an input buffer is ready to be dequeued
  463. */
  464. avbuf = v4l2_dequeue_v4l2buf(ctx, ctx_to_m2mctx(ctx)->draining ? 200 : -1);
  465. if (!avbuf) {
  466. if (ctx->done)
  467. return AVERROR_EOF;
  468. return AVERROR(EAGAIN);
  469. }
  470. return ff_v4l2_buffer_buf_to_avframe(frame, avbuf);
  471. }
  472. int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt)
  473. {
  474. V4L2Buffer* avbuf = NULL;
  475. /* if we are draining, we are no longer inputing data, therefore enable a
  476. * timeout so we can dequeue and flag the last valid buffer.
  477. *
  478. * blocks until:
  479. * 1. encoded packet available
  480. * 2. an input buffer ready to be dequeued
  481. */
  482. avbuf = v4l2_dequeue_v4l2buf(ctx, ctx_to_m2mctx(ctx)->draining ? 200 : -1);
  483. if (!avbuf) {
  484. if (ctx->done)
  485. return AVERROR_EOF;
  486. return AVERROR(EAGAIN);
  487. }
  488. return ff_v4l2_buffer_buf_to_avpkt(pkt, avbuf);
  489. }
  490. int ff_v4l2_context_get_format(V4L2Context* ctx)
  491. {
  492. struct v4l2_format_update fmt = { 0 };
  493. int ret;
  494. if (ctx->av_codec_id == AV_CODEC_ID_RAWVIDEO) {
  495. ret = v4l2_get_raw_format(ctx, &fmt.av_fmt);
  496. if (ret)
  497. return ret;
  498. fmt.update_avfmt = 1;
  499. v4l2_save_to_context(ctx, &fmt);
  500. /* format has been tried already */
  501. return ret;
  502. }
  503. ret = v4l2_get_coded_format(ctx, &fmt.v4l2_fmt);
  504. if (ret)
  505. return ret;
  506. fmt.update_v4l2 = 1;
  507. v4l2_save_to_context(ctx, &fmt);
  508. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, &ctx->format);
  509. }
  510. int ff_v4l2_context_set_format(V4L2Context* ctx)
  511. {
  512. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_S_FMT, &ctx->format);
  513. }
  514. void ff_v4l2_context_release(V4L2Context* ctx)
  515. {
  516. int ret;
  517. if (!ctx->buffers)
  518. return;
  519. ret = v4l2_release_buffers(ctx);
  520. if (ret)
  521. av_log(logger(ctx), AV_LOG_WARNING, "V4L2 failed to unmap the %s buffers\n", ctx->name);
  522. av_free(ctx->buffers);
  523. ctx->buffers = NULL;
  524. }
  525. int ff_v4l2_context_init(V4L2Context* ctx)
  526. {
  527. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  528. struct v4l2_requestbuffers req;
  529. int ret, i;
  530. if (!v4l2_type_supported(ctx)) {
  531. av_log(logger(ctx), AV_LOG_ERROR, "type %i not supported\n", ctx->type);
  532. return AVERROR_PATCHWELCOME;
  533. }
  534. ret = ioctl(s->fd, VIDIOC_G_FMT, &ctx->format);
  535. if (ret)
  536. av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT failed\n", ctx->name);
  537. memset(&req, 0, sizeof(req));
  538. req.count = ctx->num_buffers;
  539. req.memory = V4L2_MEMORY_MMAP;
  540. req.type = ctx->type;
  541. ret = ioctl(s->fd, VIDIOC_REQBUFS, &req);
  542. if (ret < 0)
  543. return AVERROR(errno);
  544. ctx->num_buffers = req.count;
  545. ctx->buffers = av_mallocz(ctx->num_buffers * sizeof(V4L2Buffer));
  546. if (!ctx->buffers) {
  547. av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name);
  548. return AVERROR(ENOMEM);
  549. }
  550. for (i = 0; i < req.count; i++) {
  551. ctx->buffers[i].context = ctx;
  552. ret = ff_v4l2_buffer_initialize(&ctx->buffers[i], i);
  553. if (ret < 0) {
  554. av_log(logger(ctx), AV_LOG_ERROR, "%s buffer initialization (%s)\n", ctx->name, av_err2str(ret));
  555. av_free(ctx->buffers);
  556. return ret;
  557. }
  558. }
  559. av_log(logger(ctx), AV_LOG_DEBUG, "%s: %s %02d buffers initialized: %04ux%04u, sizeimage %08u, bytesperline %08u\n", ctx->name,
  560. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? av_fourcc2str(ctx->format.fmt.pix_mp.pixelformat) : av_fourcc2str(ctx->format.fmt.pix.pixelformat),
  561. req.count,
  562. v4l2_get_width(&ctx->format),
  563. v4l2_get_height(&ctx->format),
  564. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage : ctx->format.fmt.pix.sizeimage,
  565. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline : ctx->format.fmt.pix.bytesperline);
  566. return 0;
  567. }