v4l2_context.c 20 KB

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