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.

717 lines
20KB

  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. .flags = 0,
  188. };
  189. int ret;
  190. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DECODER_CMD, &cmd);
  191. if (ret) {
  192. /* DECODER_CMD is optional */
  193. if (errno == ENOTTY)
  194. return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
  195. else
  196. return AVERROR(errno);
  197. }
  198. return 0;
  199. }
  200. static int v4l2_stop_encode(V4L2Context *ctx)
  201. {
  202. struct v4l2_encoder_cmd cmd = {
  203. .cmd = V4L2_ENC_CMD_STOP,
  204. .flags = 0,
  205. };
  206. int ret;
  207. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENCODER_CMD, &cmd);
  208. if (ret) {
  209. /* ENCODER_CMD is optional */
  210. if (errno == ENOTTY)
  211. return ff_v4l2_context_set_status(ctx, VIDIOC_STREAMOFF);
  212. else
  213. return AVERROR(errno);
  214. }
  215. return 0;
  216. }
  217. static V4L2Buffer* v4l2_dequeue_v4l2buf(V4L2Context *ctx, int timeout)
  218. {
  219. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  220. struct v4l2_buffer buf = { 0 };
  221. V4L2Buffer* avbuf = NULL;
  222. struct pollfd pfd = {
  223. .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, /* default blocking capture */
  224. .fd = ctx_to_m2mctx(ctx)->fd,
  225. };
  226. int i, ret;
  227. /* if we are draining and there are no more capture buffers queued in the driver we are done */
  228. if (!V4L2_TYPE_IS_OUTPUT(ctx->type) && ctx_to_m2mctx(ctx)->draining) {
  229. for (i = 0; i < ctx->num_buffers; i++) {
  230. if (ctx->buffers[i].status == V4L2BUF_IN_DRIVER)
  231. goto start;
  232. }
  233. ctx->done = 1;
  234. return NULL;
  235. }
  236. start:
  237. if (V4L2_TYPE_IS_OUTPUT(ctx->type))
  238. pfd.events = POLLOUT | POLLWRNORM;
  239. else {
  240. /* no need to listen to requests for more input while draining */
  241. if (ctx_to_m2mctx(ctx)->draining)
  242. pfd.events = POLLIN | POLLRDNORM | POLLPRI;
  243. }
  244. for (;;) {
  245. ret = poll(&pfd, 1, timeout);
  246. if (ret > 0)
  247. break;
  248. if (errno == EINTR)
  249. continue;
  250. return NULL;
  251. }
  252. /* 0. handle errors */
  253. if (pfd.revents & POLLERR) {
  254. /* if we are trying to get free buffers but none have been queued yet
  255. no need to raise a warning */
  256. if (timeout == 0) {
  257. for (i = 0; i < ctx->num_buffers; i++) {
  258. if (ctx->buffers[i].status != V4L2BUF_AVAILABLE)
  259. av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
  260. }
  261. }
  262. else
  263. av_log(logger(ctx), AV_LOG_WARNING, "%s POLLERR\n", ctx->name);
  264. return NULL;
  265. }
  266. /* 1. handle resolution changes */
  267. if (pfd.revents & POLLPRI) {
  268. ret = v4l2_handle_event(ctx);
  269. if (ret < 0) {
  270. /* if re-init failed, abort */
  271. ctx->done = 1;
  272. return NULL;
  273. }
  274. if (ret) {
  275. /* if re-init was successful drop the buffer (if there was one)
  276. * since we had to reconfigure capture (unmap all buffers)
  277. */
  278. return NULL;
  279. }
  280. }
  281. /* 2. dequeue the buffer */
  282. if (pfd.revents & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) {
  283. if (!V4L2_TYPE_IS_OUTPUT(ctx->type)) {
  284. /* there is a capture buffer ready */
  285. if (pfd.revents & (POLLIN | POLLRDNORM))
  286. goto dequeue;
  287. /* the driver is ready to accept more input; instead of waiting for the capture
  288. * buffer to complete we return NULL so input can proceed (we are single threaded)
  289. */
  290. if (pfd.revents & (POLLOUT | POLLWRNORM))
  291. return NULL;
  292. }
  293. dequeue:
  294. memset(&buf, 0, sizeof(buf));
  295. buf.memory = V4L2_MEMORY_MMAP;
  296. buf.type = ctx->type;
  297. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  298. memset(planes, 0, sizeof(planes));
  299. buf.length = VIDEO_MAX_PLANES;
  300. buf.m.planes = planes;
  301. }
  302. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_DQBUF, &buf);
  303. if (ret) {
  304. if (errno != EAGAIN) {
  305. ctx->done = 1;
  306. if (errno != EPIPE)
  307. av_log(logger(ctx), AV_LOG_DEBUG, "%s VIDIOC_DQBUF, errno (%s)\n",
  308. ctx->name, av_err2str(AVERROR(errno)));
  309. }
  310. return NULL;
  311. }
  312. avbuf = &ctx->buffers[buf.index];
  313. avbuf->status = V4L2BUF_AVAILABLE;
  314. avbuf->buf = buf;
  315. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  316. memcpy(avbuf->planes, planes, sizeof(planes));
  317. avbuf->buf.m.planes = avbuf->planes;
  318. }
  319. return avbuf;
  320. }
  321. return NULL;
  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. /* get back as many output buffers as possible */
  328. if (V4L2_TYPE_IS_OUTPUT(ctx->type)) {
  329. do {
  330. } while (v4l2_dequeue_v4l2buf(ctx, timeout));
  331. }
  332. for (i = 0; i < ctx->num_buffers; i++) {
  333. if (ctx->buffers[i].status == V4L2BUF_AVAILABLE)
  334. return &ctx->buffers[i];
  335. }
  336. return NULL;
  337. }
  338. static int v4l2_release_buffers(V4L2Context* ctx)
  339. {
  340. struct v4l2_requestbuffers req = {
  341. .memory = V4L2_MEMORY_MMAP,
  342. .type = ctx->type,
  343. .count = 0, /* 0 -> unmaps buffers from the driver */
  344. };
  345. int i, j;
  346. for (i = 0; i < ctx->num_buffers; i++) {
  347. V4L2Buffer *buffer = &ctx->buffers[i];
  348. for (j = 0; j < buffer->num_planes; j++) {
  349. struct V4L2Plane_info *p = &buffer->plane_info[j];
  350. if (p->mm_addr && p->length)
  351. if (munmap(p->mm_addr, p->length) < 0)
  352. av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
  353. }
  354. }
  355. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
  356. }
  357. static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfmt)
  358. {
  359. struct v4l2_format *fmt = &ctx->format;
  360. uint32_t v4l2_fmt;
  361. int ret;
  362. v4l2_fmt = ff_v4l2_format_avfmt_to_v4l2(pixfmt);
  363. if (!v4l2_fmt)
  364. return AVERROR(EINVAL);
  365. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type))
  366. fmt->fmt.pix_mp.pixelformat = v4l2_fmt;
  367. else
  368. fmt->fmt.pix.pixelformat = v4l2_fmt;
  369. fmt->type = ctx->type;
  370. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, fmt);
  371. if (ret)
  372. return AVERROR(EINVAL);
  373. return 0;
  374. }
  375. static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p)
  376. {
  377. enum AVPixelFormat pixfmt = ctx->av_pix_fmt;
  378. struct v4l2_fmtdesc fdesc;
  379. int ret;
  380. memset(&fdesc, 0, sizeof(fdesc));
  381. fdesc.type = ctx->type;
  382. if (pixfmt != AV_PIX_FMT_NONE) {
  383. ret = v4l2_try_raw_format(ctx, pixfmt);
  384. if (!ret)
  385. return 0;
  386. }
  387. for (;;) {
  388. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
  389. if (ret)
  390. return AVERROR(EINVAL);
  391. pixfmt = ff_v4l2_format_v4l2_to_avfmt(fdesc.pixelformat, AV_CODEC_ID_RAWVIDEO);
  392. ret = v4l2_try_raw_format(ctx, pixfmt);
  393. if (ret){
  394. fdesc.index++;
  395. continue;
  396. }
  397. *p = pixfmt;
  398. return 0;
  399. }
  400. return AVERROR(EINVAL);
  401. }
  402. static int v4l2_get_coded_format(V4L2Context* ctx, uint32_t *p)
  403. {
  404. struct v4l2_fmtdesc fdesc;
  405. uint32_t v4l2_fmt;
  406. int ret;
  407. /* translate to a valid v4l2 format */
  408. v4l2_fmt = ff_v4l2_format_avcodec_to_v4l2(ctx->av_codec_id);
  409. if (!v4l2_fmt)
  410. return AVERROR(EINVAL);
  411. /* check if the driver supports this format */
  412. memset(&fdesc, 0, sizeof(fdesc));
  413. fdesc.type = ctx->type;
  414. for (;;) {
  415. ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_ENUM_FMT, &fdesc);
  416. if (ret)
  417. return AVERROR(EINVAL);
  418. if (fdesc.pixelformat == v4l2_fmt)
  419. break;
  420. fdesc.index++;
  421. }
  422. *p = v4l2_fmt;
  423. return 0;
  424. }
  425. /*****************************************************************************
  426. *
  427. * V4L2 Context Interface
  428. *
  429. *****************************************************************************/
  430. int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd)
  431. {
  432. int type = ctx->type;
  433. int ret;
  434. ret = ioctl(ctx_to_m2mctx(ctx)->fd, cmd, &type);
  435. if (ret < 0)
  436. return AVERROR(errno);
  437. ctx->streamon = (cmd == VIDIOC_STREAMON);
  438. return 0;
  439. }
  440. int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* frame)
  441. {
  442. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  443. V4L2Buffer* avbuf;
  444. int ret;
  445. if (!frame) {
  446. ret = v4l2_stop_encode(ctx);
  447. if (ret)
  448. av_log(logger(ctx), AV_LOG_ERROR, "%s stop_encode\n", ctx->name);
  449. s->draining= 1;
  450. return 0;
  451. }
  452. avbuf = v4l2_getfree_v4l2buf(ctx);
  453. if (!avbuf)
  454. return AVERROR(ENOMEM);
  455. ret = ff_v4l2_buffer_avframe_to_buf(frame, avbuf);
  456. if (ret)
  457. return ret;
  458. return ff_v4l2_buffer_enqueue(avbuf);
  459. }
  460. int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt)
  461. {
  462. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  463. V4L2Buffer* avbuf;
  464. int ret;
  465. if (!pkt->size) {
  466. ret = v4l2_stop_decode(ctx);
  467. if (ret)
  468. av_log(logger(ctx), AV_LOG_ERROR, "%s stop_decode\n", ctx->name);
  469. s->draining = 1;
  470. return 0;
  471. }
  472. avbuf = v4l2_getfree_v4l2buf(ctx);
  473. if (!avbuf)
  474. return AVERROR(ENOMEM);
  475. ret = ff_v4l2_buffer_avpkt_to_buf(pkt, avbuf);
  476. if (ret)
  477. return ret;
  478. return ff_v4l2_buffer_enqueue(avbuf);
  479. }
  480. int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* frame)
  481. {
  482. V4L2Buffer* avbuf = NULL;
  483. /*
  484. * blocks until:
  485. * 1. decoded frame available
  486. * 2. an input buffer is ready to be dequeued
  487. */
  488. avbuf = v4l2_dequeue_v4l2buf(ctx, -1);
  489. if (!avbuf) {
  490. if (ctx->done)
  491. return AVERROR_EOF;
  492. return AVERROR(EAGAIN);
  493. }
  494. return ff_v4l2_buffer_buf_to_avframe(frame, avbuf);
  495. }
  496. int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt)
  497. {
  498. V4L2Buffer* avbuf = NULL;
  499. /*
  500. * blocks until:
  501. * 1. encoded packet available
  502. * 2. an input buffer ready to be dequeued
  503. */
  504. avbuf = v4l2_dequeue_v4l2buf(ctx, -1);
  505. if (!avbuf) {
  506. if (ctx->done)
  507. return AVERROR_EOF;
  508. return AVERROR(EAGAIN);
  509. }
  510. return ff_v4l2_buffer_buf_to_avpkt(pkt, avbuf);
  511. }
  512. int ff_v4l2_context_get_format(V4L2Context* ctx)
  513. {
  514. struct v4l2_format_update fmt = { 0 };
  515. int ret;
  516. if (ctx->av_codec_id == AV_CODEC_ID_RAWVIDEO) {
  517. ret = v4l2_get_raw_format(ctx, &fmt.av_fmt);
  518. if (ret)
  519. return ret;
  520. fmt.update_avfmt = 1;
  521. v4l2_save_to_context(ctx, &fmt);
  522. /* format has been tried already */
  523. return ret;
  524. }
  525. ret = v4l2_get_coded_format(ctx, &fmt.v4l2_fmt);
  526. if (ret)
  527. return ret;
  528. fmt.update_v4l2 = 1;
  529. v4l2_save_to_context(ctx, &fmt);
  530. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_TRY_FMT, &ctx->format);
  531. }
  532. int ff_v4l2_context_set_format(V4L2Context* ctx)
  533. {
  534. return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_S_FMT, &ctx->format);
  535. }
  536. void ff_v4l2_context_release(V4L2Context* ctx)
  537. {
  538. int ret;
  539. if (!ctx->buffers)
  540. return;
  541. ret = v4l2_release_buffers(ctx);
  542. if (ret)
  543. av_log(logger(ctx), AV_LOG_WARNING, "V4L2 failed to unmap the %s buffers\n", ctx->name);
  544. av_free(ctx->buffers);
  545. ctx->buffers = NULL;
  546. }
  547. int ff_v4l2_context_init(V4L2Context* ctx)
  548. {
  549. V4L2m2mContext *s = ctx_to_m2mctx(ctx);
  550. struct v4l2_requestbuffers req;
  551. int ret, i;
  552. if (!v4l2_type_supported(ctx)) {
  553. av_log(logger(ctx), AV_LOG_ERROR, "type %i not supported\n", ctx->type);
  554. return AVERROR_PATCHWELCOME;
  555. }
  556. ret = ioctl(s->fd, VIDIOC_G_FMT, &ctx->format);
  557. if (ret)
  558. av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT failed\n", ctx->name);
  559. memset(&req, 0, sizeof(req));
  560. req.count = ctx->num_buffers;
  561. req.memory = V4L2_MEMORY_MMAP;
  562. req.type = ctx->type;
  563. ret = ioctl(s->fd, VIDIOC_REQBUFS, &req);
  564. if (ret < 0) {
  565. av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_REQBUFS failed: %s\n", ctx->name, strerror(errno));
  566. return AVERROR(errno);
  567. }
  568. ctx->num_buffers = req.count;
  569. ctx->buffers = av_mallocz(ctx->num_buffers * sizeof(V4L2Buffer));
  570. if (!ctx->buffers) {
  571. av_log(logger(ctx), AV_LOG_ERROR, "%s malloc enomem\n", ctx->name);
  572. return AVERROR(ENOMEM);
  573. }
  574. for (i = 0; i < req.count; i++) {
  575. ctx->buffers[i].context = ctx;
  576. ret = ff_v4l2_buffer_initialize(&ctx->buffers[i], i);
  577. if (ret < 0) {
  578. av_log(logger(ctx), AV_LOG_ERROR, "%s buffer initialization (%s)\n", ctx->name, av_err2str(ret));
  579. av_free(ctx->buffers);
  580. return ret;
  581. }
  582. }
  583. av_log(logger(ctx), AV_LOG_DEBUG, "%s: %s %02d buffers initialized: %04ux%04u, sizeimage %08u, bytesperline %08u\n", ctx->name,
  584. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? av_fourcc2str(ctx->format.fmt.pix_mp.pixelformat) : av_fourcc2str(ctx->format.fmt.pix.pixelformat),
  585. req.count,
  586. v4l2_get_width(&ctx->format),
  587. v4l2_get_height(&ctx->format),
  588. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage : ctx->format.fmt.pix.sizeimage,
  589. V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ? ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline : ctx->format.fmt.pix.bytesperline);
  590. return 0;
  591. }