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.

751 lines
21KB

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