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.

182 lines
5.7KB

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