v4l2_buffers.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef AVCODEC_V4L2_BUFFERS_H
  24. #define AVCODEC_V4L2_BUFFERS_H
  25. #include <stdatomic.h>
  26. #include <linux/videodev2.h>
  27. #include "avcodec.h"
  28. enum V4L2Buffer_status {
  29. V4L2BUF_AVAILABLE,
  30. V4L2BUF_IN_DRIVER,
  31. V4L2BUF_RET_USER,
  32. };
  33. /**
  34. * V4L2Buffer (wrapper for v4l2_buffer management)
  35. */
  36. typedef struct V4L2Buffer {
  37. /* each buffer needs to have a reference to its context */
  38. struct V4L2Context *context;
  39. /* This object is refcounted per-plane, so we need to keep track
  40. * of how many context-refs we are holding. */
  41. AVBufferRef *context_ref;
  42. atomic_uint context_refcount;
  43. /* keep track of the mmap address and mmap length */
  44. struct V4L2Plane_info {
  45. int bytesperline;
  46. void * mm_addr;
  47. size_t length;
  48. } plane_info[VIDEO_MAX_PLANES];
  49. int num_planes;
  50. /* the v4l2_buffer buf.m.planes pointer uses the planes[] mem */
  51. struct v4l2_buffer buf;
  52. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  53. int flags;
  54. enum V4L2Buffer_status status;
  55. } V4L2Buffer;
  56. /**
  57. * Extracts the data from a V4L2Buffer to an AVFrame
  58. *
  59. * @param[in] frame The AVFRame to push the information to
  60. * @param[in] buf The V4L2Buffer to get the information from
  61. *
  62. * @returns 0 in case of success, AVERROR(EINVAL) if the number of planes is incorrect,
  63. * AVERROR(ENOMEM) if the AVBufferRef can't be created.
  64. */
  65. int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *buf);
  66. /**
  67. * Extracts the data from a V4L2Buffer to an AVPacket
  68. *
  69. * @param[in] pkt The AVPacket to push the information to
  70. * @param[in] buf The V4L2Buffer to get the information from
  71. *
  72. * @returns 0 in case of success, AVERROR(EINVAL) if the number of planes is incorrect,
  73. * AVERROR(ENOMEM) if the AVBufferRef can't be created.
  74. *
  75. */
  76. int ff_v4l2_buffer_buf_to_avpkt(AVPacket *pkt, V4L2Buffer *buf);
  77. /**
  78. * Extracts the data from an AVPacket to a V4L2Buffer
  79. *
  80. * @param[in] frame AVPacket to get the data from
  81. * @param[in] avbuf V4L2Bfuffer to push the information to
  82. *
  83. * @returns 0 in case of success, a negative AVERROR code otherwise
  84. */
  85. int ff_v4l2_buffer_avpkt_to_buf(const AVPacket *pkt, V4L2Buffer *out);
  86. /**
  87. * Extracts the data from an AVFrame to a V4L2Buffer
  88. *
  89. * @param[in] frame AVFrame to get the data from
  90. * @param[in] avbuf V4L2Bfuffer to push the information to
  91. *
  92. * @returns 0 in case of success, a negative AVERROR code otherwise
  93. */
  94. int ff_v4l2_buffer_avframe_to_buf(const AVFrame *frame, V4L2Buffer *out);
  95. /**
  96. * Initializes a V4L2Buffer
  97. *
  98. * @param[in] avbuf V4L2Bfuffer to initialize
  99. * @param[in] index v4l2 buffer id
  100. *
  101. * @returns 0 in case of success, a negative AVERROR code otherwise
  102. */
  103. int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index);
  104. /**
  105. * Enqueues a V4L2Buffer
  106. *
  107. * @param[in] avbuf V4L2Bfuffer to push to the driver
  108. *
  109. * @returns 0 in case of success, a negative AVERROR code otherwise
  110. */
  111. int ff_v4l2_buffer_enqueue(V4L2Buffer* avbuf);
  112. #endif // AVCODEC_V4L2_BUFFERS_H