v4l2_buffers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * V4L2 buffer 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_context.h"
  32. #include "v4l2_buffers.h"
  33. #include "v4l2_m2m.h"
  34. #define USEC_PER_SEC 1000000
  35. static inline V4L2m2mContext *buf_to_m2mctx(V4L2Buffer *buf)
  36. {
  37. return V4L2_TYPE_IS_OUTPUT(buf->context->type) ?
  38. container_of(buf->context, V4L2m2mContext, output) :
  39. container_of(buf->context, V4L2m2mContext, capture);
  40. }
  41. static inline AVCodecContext *logger(V4L2Buffer *buf)
  42. {
  43. return buf_to_m2mctx(buf)->avctx;
  44. }
  45. static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts)
  46. {
  47. V4L2m2mContext *s = buf_to_m2mctx(out);
  48. AVRational v4l2_timebase = { 1, USEC_PER_SEC };
  49. int64_t v4l2_pts;
  50. if (pts == AV_NOPTS_VALUE)
  51. pts = 0;
  52. /* convert pts to v4l2 timebase */
  53. v4l2_pts = av_rescale_q(pts, s->avctx->time_base, v4l2_timebase);
  54. out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC;
  55. out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC;
  56. }
  57. static inline uint64_t v4l2_get_pts(V4L2Buffer *avbuf)
  58. {
  59. V4L2m2mContext *s = buf_to_m2mctx(avbuf);
  60. AVRational v4l2_timebase = { 1, USEC_PER_SEC };
  61. int64_t v4l2_pts;
  62. /* convert pts back to encoder timebase */
  63. v4l2_pts = (int64_t)avbuf->buf.timestamp.tv_sec * USEC_PER_SEC +
  64. avbuf->buf.timestamp.tv_usec;
  65. return av_rescale_q(v4l2_pts, v4l2_timebase, s->avctx->time_base);
  66. }
  67. static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf)
  68. {
  69. enum v4l2_ycbcr_encoding ycbcr;
  70. enum v4l2_colorspace cs;
  71. cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  72. buf->context->format.fmt.pix_mp.colorspace :
  73. buf->context->format.fmt.pix.colorspace;
  74. ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  75. buf->context->format.fmt.pix_mp.ycbcr_enc:
  76. buf->context->format.fmt.pix.ycbcr_enc;
  77. switch(ycbcr) {
  78. case V4L2_YCBCR_ENC_XV709:
  79. case V4L2_YCBCR_ENC_709: return AVCOL_PRI_BT709;
  80. case V4L2_YCBCR_ENC_XV601:
  81. case V4L2_YCBCR_ENC_601:return AVCOL_PRI_BT470M;
  82. default:
  83. break;
  84. }
  85. switch(cs) {
  86. case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_PRI_BT470BG;
  87. case V4L2_COLORSPACE_SMPTE170M: return AVCOL_PRI_SMPTE170M;
  88. case V4L2_COLORSPACE_SMPTE240M: return AVCOL_PRI_SMPTE240M;
  89. case V4L2_COLORSPACE_BT2020: return AVCOL_PRI_BT2020;
  90. default:
  91. break;
  92. }
  93. return AVCOL_PRI_UNSPECIFIED;
  94. }
  95. static enum AVColorRange v4l2_get_color_range(V4L2Buffer *buf)
  96. {
  97. enum v4l2_quantization qt;
  98. qt = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  99. buf->context->format.fmt.pix_mp.quantization :
  100. buf->context->format.fmt.pix.quantization;
  101. switch (qt) {
  102. case V4L2_QUANTIZATION_LIM_RANGE: return AVCOL_RANGE_MPEG;
  103. case V4L2_QUANTIZATION_FULL_RANGE: return AVCOL_RANGE_JPEG;
  104. default:
  105. break;
  106. }
  107. return AVCOL_RANGE_UNSPECIFIED;
  108. }
  109. static enum AVColorSpace v4l2_get_color_space(V4L2Buffer *buf)
  110. {
  111. enum v4l2_ycbcr_encoding ycbcr;
  112. enum v4l2_colorspace cs;
  113. cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  114. buf->context->format.fmt.pix_mp.colorspace :
  115. buf->context->format.fmt.pix.colorspace;
  116. ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  117. buf->context->format.fmt.pix_mp.ycbcr_enc:
  118. buf->context->format.fmt.pix.ycbcr_enc;
  119. switch(cs) {
  120. case V4L2_COLORSPACE_SRGB: return AVCOL_SPC_RGB;
  121. case V4L2_COLORSPACE_REC709: return AVCOL_SPC_BT709;
  122. case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_SPC_FCC;
  123. case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_SPC_BT470BG;
  124. case V4L2_COLORSPACE_SMPTE170M: return AVCOL_SPC_SMPTE170M;
  125. case V4L2_COLORSPACE_SMPTE240M: return AVCOL_SPC_SMPTE240M;
  126. case V4L2_COLORSPACE_BT2020:
  127. if (ycbcr == V4L2_YCBCR_ENC_BT2020_CONST_LUM)
  128. return AVCOL_SPC_BT2020_CL;
  129. else
  130. return AVCOL_SPC_BT2020_NCL;
  131. default:
  132. break;
  133. }
  134. return AVCOL_SPC_UNSPECIFIED;
  135. }
  136. static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf)
  137. {
  138. enum v4l2_ycbcr_encoding ycbcr;
  139. enum v4l2_xfer_func xfer;
  140. enum v4l2_colorspace cs;
  141. cs = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  142. buf->context->format.fmt.pix_mp.colorspace :
  143. buf->context->format.fmt.pix.colorspace;
  144. ycbcr = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  145. buf->context->format.fmt.pix_mp.ycbcr_enc:
  146. buf->context->format.fmt.pix.ycbcr_enc;
  147. xfer = V4L2_TYPE_IS_MULTIPLANAR(buf->buf.type) ?
  148. buf->context->format.fmt.pix_mp.xfer_func:
  149. buf->context->format.fmt.pix.xfer_func;
  150. switch (xfer) {
  151. case V4L2_XFER_FUNC_709: return AVCOL_TRC_BT709;
  152. case V4L2_XFER_FUNC_SRGB: return AVCOL_TRC_IEC61966_2_1;
  153. default:
  154. break;
  155. }
  156. switch (cs) {
  157. case V4L2_COLORSPACE_470_SYSTEM_M: return AVCOL_TRC_GAMMA22;
  158. case V4L2_COLORSPACE_470_SYSTEM_BG: return AVCOL_TRC_GAMMA28;
  159. case V4L2_COLORSPACE_SMPTE170M: return AVCOL_TRC_SMPTE170M;
  160. case V4L2_COLORSPACE_SMPTE240M: return AVCOL_TRC_SMPTE240M;
  161. default:
  162. break;
  163. }
  164. switch (ycbcr) {
  165. case V4L2_YCBCR_ENC_XV709:
  166. case V4L2_YCBCR_ENC_XV601: return AVCOL_TRC_BT1361_ECG;
  167. default:
  168. break;
  169. }
  170. return AVCOL_TRC_UNSPECIFIED;
  171. }
  172. static void v4l2_free_buffer(void *opaque, uint8_t *unused)
  173. {
  174. V4L2Buffer* avbuf = opaque;
  175. V4L2m2mContext *s = buf_to_m2mctx(avbuf);
  176. if (atomic_fetch_sub(&avbuf->context_refcount, 1) == 1) {
  177. atomic_fetch_sub_explicit(&s->refcount, 1, memory_order_acq_rel);
  178. if (s->reinit) {
  179. if (!atomic_load(&s->refcount))
  180. sem_post(&s->refsync);
  181. } else {
  182. if (s->draining) {
  183. /* no need to queue more buffers to the driver */
  184. avbuf->status = V4L2BUF_AVAILABLE;
  185. }
  186. else if (avbuf->context->streamon)
  187. ff_v4l2_buffer_enqueue(avbuf);
  188. }
  189. av_buffer_unref(&avbuf->context_ref);
  190. }
  191. }
  192. static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
  193. {
  194. V4L2m2mContext *s = buf_to_m2mctx(in);
  195. if (plane >= in->num_planes)
  196. return AVERROR(EINVAL);
  197. /* even though most encoders return 0 in data_offset encoding vp8 does require this value */
  198. *buf = av_buffer_create((char *)in->plane_info[plane].mm_addr + in->planes[plane].data_offset,
  199. in->plane_info[plane].length, v4l2_free_buffer, in, 0);
  200. if (!*buf)
  201. return AVERROR(ENOMEM);
  202. if (in->context_ref)
  203. atomic_fetch_add(&in->context_refcount, 1);
  204. else {
  205. in->context_ref = av_buffer_ref(s->self_ref);
  206. if (!in->context_ref) {
  207. av_buffer_unref(buf);
  208. return AVERROR(ENOMEM);
  209. }
  210. in->context_refcount = 1;
  211. }
  212. in->status = V4L2BUF_RET_USER;
  213. atomic_fetch_add_explicit(&s->refcount, 1, memory_order_relaxed);
  214. return 0;
  215. }
  216. static int v4l2_bufref_to_buf(V4L2Buffer *out, int plane, const uint8_t* data, int size, AVBufferRef* bref)
  217. {
  218. unsigned int bytesused, length;
  219. if (plane >= out->num_planes)
  220. return AVERROR(EINVAL);
  221. bytesused = FFMIN(size, out->plane_info[plane].length);
  222. length = out->plane_info[plane].length;
  223. memcpy(out->plane_info[plane].mm_addr, data, FFMIN(size, out->plane_info[plane].length));
  224. if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
  225. out->planes[plane].bytesused = bytesused;
  226. out->planes[plane].length = length;
  227. } else {
  228. out->buf.bytesused = bytesused;
  229. out->buf.length = length;
  230. }
  231. return 0;
  232. }
  233. /******************************************************************************
  234. *
  235. * V4L2uffer interface
  236. *
  237. ******************************************************************************/
  238. int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer* out)
  239. {
  240. int i, ret;
  241. for(i = 0; i < out->num_planes; i++) {
  242. ret = v4l2_bufref_to_buf(out, i, frame->buf[i]->data, frame->buf[i]->size, frame->buf[i]);
  243. if (ret)
  244. return ret;
  245. }
  246. v4l2_set_pts(out, frame->pts);
  247. return 0;
  248. }
  249. int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
  250. {
  251. V4L2m2mContext *s = buf_to_m2mctx(avbuf);
  252. int i, ret;
  253. av_frame_unref(frame);
  254. /* 1. get references to the actual data */
  255. for (i = 0; i < avbuf->num_planes; i++) {
  256. ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
  257. if (ret)
  258. return ret;
  259. frame->linesize[i] = avbuf->plane_info[i].bytesperline;
  260. frame->data[i] = frame->buf[i]->data;
  261. }
  262. /* 1.1 fixup special cases */
  263. switch (avbuf->context->av_pix_fmt) {
  264. case AV_PIX_FMT_NV12:
  265. if (avbuf->num_planes > 1)
  266. break;
  267. frame->linesize[1] = avbuf->plane_info[0].bytesperline;
  268. frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
  269. break;
  270. default:
  271. break;
  272. }
  273. /* 2. get frame information */
  274. frame->key_frame = !!(avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME);
  275. frame->format = avbuf->context->av_pix_fmt;
  276. frame->color_primaries = v4l2_get_color_primaries(avbuf);
  277. frame->colorspace = v4l2_get_color_space(avbuf);
  278. frame->color_range = v4l2_get_color_range(avbuf);
  279. frame->color_trc = v4l2_get_color_trc(avbuf);
  280. frame->pts = v4l2_get_pts(avbuf);
  281. /* these two values are updated also during re-init in v4l2_process_driver_event */
  282. frame->height = s->output.height;
  283. frame->width = s->output.width;
  284. /* 3. report errors upstream */
  285. if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
  286. av_log(logger(avbuf), AV_LOG_ERROR, "%s: driver decode error\n", avbuf->context->name);
  287. frame->decode_error_flags |= FF_DECODE_ERROR_INVALID_BITSTREAM;
  288. }
  289. return 0;
  290. }
  291. int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *avbuf)
  292. {
  293. int ret;
  294. av_packet_unref(pkt);
  295. ret = v4l2_buf_to_bufref(avbuf, 0, &pkt->buf);
  296. if (ret)
  297. return ret;
  298. pkt->size = V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type) ? avbuf->buf.m.planes[0].bytesused : avbuf->buf.bytesused;
  299. pkt->data = pkt->buf->data;
  300. if (avbuf->buf.flags & V4L2_BUF_FLAG_KEYFRAME)
  301. pkt->flags |= AV_PKT_FLAG_KEY;
  302. if (avbuf->buf.flags & V4L2_BUF_FLAG_ERROR) {
  303. av_log(logger(avbuf), AV_LOG_ERROR, "%s driver encode error\n", avbuf->context->name);
  304. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  305. }
  306. pkt->dts = pkt->pts = v4l2_get_pts(avbuf);
  307. return 0;
  308. }
  309. int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out)
  310. {
  311. int ret;
  312. ret = v4l2_bufref_to_buf(out, 0, pkt->data, pkt->size, pkt->buf);
  313. if (ret)
  314. return ret;
  315. v4l2_set_pts(out, pkt->pts);
  316. if (pkt->flags & AV_PKT_FLAG_KEY)
  317. out->flags = V4L2_BUF_FLAG_KEYFRAME;
  318. return 0;
  319. }
  320. int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
  321. {
  322. V4L2Context *ctx = avbuf->context;
  323. int ret, i;
  324. avbuf->buf.memory = V4L2_MEMORY_MMAP;
  325. avbuf->buf.type = ctx->type;
  326. avbuf->buf.index = index;
  327. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  328. avbuf->buf.length = VIDEO_MAX_PLANES;
  329. avbuf->buf.m.planes = avbuf->planes;
  330. }
  331. ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QUERYBUF, &avbuf->buf);
  332. if (ret < 0)
  333. return AVERROR(errno);
  334. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  335. avbuf->num_planes = 0;
  336. for (;;) {
  337. /* in MP, the V4L2 API states that buf.length means num_planes */
  338. if (avbuf->num_planes >= avbuf->buf.length)
  339. break;
  340. if (avbuf->buf.m.planes[avbuf->num_planes].length)
  341. avbuf->num_planes++;
  342. }
  343. } else
  344. avbuf->num_planes = 1;
  345. for (i = 0; i < avbuf->num_planes; i++) {
  346. avbuf->plane_info[i].bytesperline = V4L2_TYPE_IS_MULTIPLANAR(ctx->type) ?
  347. ctx->format.fmt.pix_mp.plane_fmt[i].bytesperline :
  348. ctx->format.fmt.pix.bytesperline;
  349. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  350. avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length;
  351. avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
  352. PROT_READ | PROT_WRITE, MAP_SHARED,
  353. buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
  354. } else {
  355. avbuf->plane_info[i].length = avbuf->buf.length;
  356. avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
  357. PROT_READ | PROT_WRITE, MAP_SHARED,
  358. buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
  359. }
  360. if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
  361. return AVERROR(ENOMEM);
  362. }
  363. avbuf->status = V4L2BUF_AVAILABLE;
  364. if (V4L2_TYPE_IS_OUTPUT(ctx->type))
  365. return 0;
  366. if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
  367. avbuf->buf.m.planes = avbuf->planes;
  368. avbuf->buf.length = avbuf->num_planes;
  369. } else {
  370. avbuf->buf.bytesused = avbuf->planes[0].bytesused;
  371. avbuf->buf.length = avbuf->planes[0].length;
  372. }
  373. return ff_v4l2_buffer_enqueue(avbuf);
  374. }
  375. int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf)
  376. {
  377. int ret;
  378. avbuf->buf.flags = avbuf->flags;
  379. ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_QBUF, &avbuf->buf);
  380. if (ret < 0)
  381. return AVERROR(errno);
  382. avbuf->status = V4L2BUF_IN_DRIVER;
  383. return 0;
  384. }