v4l2_context.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifndef AVCODEC_V4L2_CONTEXT_H
  24. #define AVCODEC_V4L2_CONTEXT_H
  25. #include <stdatomic.h>
  26. #include <linux/videodev2.h>
  27. #include "libavcodec/avcodec.h"
  28. #include "libavutil/pixfmt.h"
  29. #include "libavutil/frame.h"
  30. #include "libavutil/buffer.h"
  31. #include "v4l2_buffers.h"
  32. typedef struct V4L2Context {
  33. /**
  34. * context name.
  35. */
  36. const char* name;
  37. /**
  38. * Type of this buffer context.
  39. * See V4L2_BUF_TYPE_VIDEO_* in videodev2.h
  40. * Readonly after init.
  41. */
  42. enum v4l2_buf_type type;
  43. /**
  44. * AVPixelFormat corresponding to this buffer context.
  45. * AV_PIX_FMT_NONE means this is an encoded stream.
  46. */
  47. enum AVPixelFormat av_pix_fmt;
  48. /**
  49. * AVCodecID corresponding to this buffer context.
  50. * AV_CODEC_ID_RAWVIDEO means this is a raw stream and av_pix_fmt must be set to a valid value.
  51. */
  52. enum AVCodecID av_codec_id;
  53. /**
  54. * Format returned by the driver after initializing the buffer context.
  55. * Readonly after init.
  56. */
  57. struct v4l2_format format;
  58. /**
  59. * Width and height of the frames it produces (in case of a capture context, e.g. when decoding)
  60. * or accepts (in case of an output context, e.g. when encoding).
  61. */
  62. int width, height;
  63. AVRational sample_aspect_ratio;
  64. /**
  65. * Indexed array of V4L2Buffers
  66. */
  67. V4L2Buffer *buffers;
  68. /**
  69. * Readonly after init.
  70. */
  71. int num_buffers;
  72. /**
  73. * Whether the stream has been started (VIDIOC_STREAMON has been sent).
  74. */
  75. int streamon;
  76. /**
  77. * Either no more buffers available or an unrecoverable error was notified
  78. * by the V4L2 kernel driver: once set the context has to be exited.
  79. */
  80. int done;
  81. } V4L2Context;
  82. /**
  83. * Initializes a V4L2Context.
  84. *
  85. * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
  86. * @return 0 in case of success, a negative value representing the error otherwise.
  87. */
  88. int ff_v4l2_context_init(V4L2Context* ctx);
  89. /**
  90. * Sets the V4L2Context format in the v4l2 driver.
  91. *
  92. * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
  93. * @return 0 in case of success, a negative value representing the error otherwise.
  94. */
  95. int ff_v4l2_context_set_format(V4L2Context* ctx);
  96. /**
  97. * Queries the driver for a valid v4l2 format and copies it to the context.
  98. *
  99. * @param[in] ctx A pointer to a V4L2Context. See V4L2Context description for required variables.
  100. * @param[in] probe Probe only and ignore changes to the format.
  101. * @return 0 in case of success, a negative value representing the error otherwise.
  102. */
  103. int ff_v4l2_context_get_format(V4L2Context* ctx, int probe);
  104. /**
  105. * Releases a V4L2Context.
  106. *
  107. * @param[in] ctx A pointer to a V4L2Context.
  108. * The caller is reponsible for freeing it.
  109. * It must not be used after calling this function.
  110. */
  111. void ff_v4l2_context_release(V4L2Context* ctx);
  112. /**
  113. * Sets the status of a V4L2Context.
  114. *
  115. * @param[in] ctx A pointer to a V4L2Context.
  116. * @param[in] cmd The status to set (VIDIOC_STREAMON or VIDIOC_STREAMOFF).
  117. * Warning: If VIDIOC_STREAMOFF is sent to a buffer context that still has some frames buffered,
  118. * those frames will be dropped.
  119. * @return 0 in case of success, a negative value representing the error otherwise.
  120. */
  121. int ff_v4l2_context_set_status(V4L2Context* ctx, uint32_t cmd);
  122. /**
  123. * Dequeues a buffer from a V4L2Context to an AVPacket.
  124. *
  125. * The pkt must be non NULL.
  126. * @param[in] ctx The V4L2Context to dequeue from.
  127. * @param[inout] pkt The AVPacket to dequeue to.
  128. * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
  129. */
  130. int ff_v4l2_context_dequeue_packet(V4L2Context* ctx, AVPacket* pkt);
  131. /**
  132. * Dequeues a buffer from a V4L2Context to an AVFrame.
  133. *
  134. * The frame must be non NULL.
  135. * @param[in] ctx The V4L2Context to dequeue from.
  136. * @param[inout] f The AVFrame to dequeue to.
  137. * @param[in] timeout The timeout for dequeue (-1 to block, 0 to return immediately, or milliseconds)
  138. * @return 0 in case of success, AVERROR(EAGAIN) if no buffer was ready, another negative error in case of error.
  139. */
  140. int ff_v4l2_context_dequeue_frame(V4L2Context* ctx, AVFrame* f, int timeout);
  141. /**
  142. * Enqueues a buffer to a V4L2Context from an AVPacket
  143. *
  144. * The packet must be non NULL.
  145. * When the size of the pkt is null, the buffer is not queued but a V4L2_DEC_CMD_STOP command is sent instead to the driver.
  146. *
  147. * @param[in] ctx The V4L2Context to enqueue to.
  148. * @param[in] pkt A pointer to an AVPacket.
  149. * @return 0 in case of success, a negative error otherwise.
  150. */
  151. int ff_v4l2_context_enqueue_packet(V4L2Context* ctx, const AVPacket* pkt);
  152. /**
  153. * Enqueues a buffer to a V4L2Context from an AVFrame
  154. *
  155. * The frame must be non NULL.
  156. *
  157. * @param[in] ctx The V4L2Context to enqueue to.
  158. * @param[in] f A pointer to an AVFrame to enqueue.
  159. * @return 0 in case of success, a negative error otherwise.
  160. */
  161. int ff_v4l2_context_enqueue_frame(V4L2Context* ctx, const AVFrame* f);
  162. #endif // AVCODEC_V4L2_CONTEXT_H