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.

330 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_HWCONTEXT_H
  19. #define AVUTIL_HWCONTEXT_H
  20. #include "buffer.h"
  21. #include "frame.h"
  22. #include "log.h"
  23. #include "pixfmt.h"
  24. enum AVHWDeviceType {
  25. AV_HWDEVICE_TYPE_VDPAU,
  26. AV_HWDEVICE_TYPE_CUDA,
  27. };
  28. typedef struct AVHWDeviceInternal AVHWDeviceInternal;
  29. /**
  30. * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
  31. * i.e. state that is not tied to a concrete processing configuration.
  32. * E.g., in an API that supports hardware-accelerated encoding and decoding,
  33. * this struct will (if possible) wrap the state that is common to both encoding
  34. * and decoding and from which specific instances of encoders or decoders can be
  35. * derived.
  36. *
  37. * This struct is reference-counted with the AVBuffer mechanism. The
  38. * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
  39. * points to the actual AVHWDeviceContext. Further objects derived from
  40. * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
  41. * specific properties) will hold an internal reference to it. After all the
  42. * references are released, the AVHWDeviceContext itself will be freed,
  43. * optionally invoking a user-specified callback for uninitializing the hardware
  44. * state.
  45. */
  46. typedef struct AVHWDeviceContext {
  47. /**
  48. * A class for logging. Set by av_hwdevice_ctx_alloc().
  49. */
  50. const AVClass *av_class;
  51. /**
  52. * Private data used internally by libavutil. Must not be accessed in any
  53. * way by the caller.
  54. */
  55. AVHWDeviceInternal *internal;
  56. /**
  57. * This field identifies the underlying API used for hardware access.
  58. *
  59. * This field is set when this struct is allocated and never changed
  60. * afterwards.
  61. */
  62. enum AVHWDeviceType type;
  63. /**
  64. * The format-specific data, allocated and freed by libavutil along with
  65. * this context.
  66. *
  67. * Should be cast by the user to the format-specific context defined in the
  68. * corresponding header (hwcontext_*.h) and filled as described in the
  69. * documentation before calling av_hwdevice_ctx_init().
  70. *
  71. * After calling av_hwdevice_ctx_init() this struct should not be modified
  72. * by the caller.
  73. */
  74. void *hwctx;
  75. /**
  76. * This field may be set by the caller before calling av_hwdevice_ctx_init().
  77. *
  78. * If non-NULL, this callback will be called when the last reference to
  79. * this context is unreferenced, immediately before it is freed.
  80. *
  81. * @note when other objects (e.g an AVHWFramesContext) are derived from this
  82. * struct, this callback will be invoked after all such child objects
  83. * are fully uninitialized and their respective destructors invoked.
  84. */
  85. void (*free)(struct AVHWDeviceContext *ctx);
  86. /**
  87. * Arbitrary user data, to be used e.g. by the free() callback.
  88. */
  89. void *user_opaque;
  90. } AVHWDeviceContext;
  91. typedef struct AVHWFramesInternal AVHWFramesInternal;
  92. /**
  93. * This struct describes a set or pool of "hardware" frames (i.e. those with
  94. * data not located in normal system memory). All the frames in the pool are
  95. * assumed to be allocated in the same way and interchangeable.
  96. *
  97. * This struct is reference-counted with the AVBuffer mechanism and tied to a
  98. * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
  99. * yields a reference, whose data field points to the actual AVHWFramesContext
  100. * struct.
  101. */
  102. typedef struct AVHWFramesContext {
  103. /**
  104. * A class for logging.
  105. */
  106. const AVClass *av_class;
  107. /**
  108. * Private data used internally by libavutil. Must not be accessed in any
  109. * way by the caller.
  110. */
  111. AVHWFramesInternal *internal;
  112. /**
  113. * A reference to the parent AVHWDeviceContext. This reference is owned and
  114. * managed by the enclosing AVHWFramesContext, but the caller may derive
  115. * additional references from it.
  116. */
  117. AVBufferRef *device_ref;
  118. /**
  119. * The parent AVHWDeviceContext. This is simply a pointer to
  120. * device_ref->data provided for convenience.
  121. *
  122. * Set by libavutil in av_hwframe_ctx_init().
  123. */
  124. AVHWDeviceContext *device_ctx;
  125. /**
  126. * The format-specific data, allocated and freed automatically along with
  127. * this context.
  128. *
  129. * Should be cast by the user to the format-specific context defined in the
  130. * corresponding header (hwframe_*.h) and filled as described in the
  131. * documentation before calling av_hwframe_ctx_init().
  132. *
  133. * After any frames using this context are created, the contents of this
  134. * struct should not be modified by the caller.
  135. */
  136. void *hwctx;
  137. /**
  138. * This field may be set by the caller before calling av_hwframe_ctx_init().
  139. *
  140. * If non-NULL, this callback will be called when the last reference to
  141. * this context is unreferenced, immediately before it is freed.
  142. */
  143. void (*free)(struct AVHWFramesContext *ctx);
  144. /**
  145. * Arbitrary user data, to be used e.g. by the free() callback.
  146. */
  147. void *user_opaque;
  148. /**
  149. * A pool from which the frames are allocated by av_hwframe_get_buffer().
  150. * This field may be set by the caller before calling av_hwframe_ctx_init().
  151. * The buffers returned by calling av_buffer_pool_get() on this pool must
  152. * have the properties described in the documentation in the correponding hw
  153. * type's header (hwcontext_*.h). The pool will be freed strictly before
  154. * this struct's free() callback is invoked.
  155. *
  156. * This field may be NULL, then libavutil will attempt to allocate a pool
  157. * internally. Note that certain device types enforce pools allocated at
  158. * fixed size (frame count), which cannot be extended dynamically. In such a
  159. * case, initial_pool_size must be set appropriately.
  160. */
  161. AVBufferPool *pool;
  162. /**
  163. * Initial size of the frame pool. If a device type does not support
  164. * dynamically resizing the pool, then this is also the maximum pool size.
  165. *
  166. * May be set by the caller before calling av_hwframe_ctx_init(). Must be
  167. * set if pool is NULL and the device type does not support dynamic pools.
  168. */
  169. int initial_pool_size;
  170. /**
  171. * The pixel format identifying the underlying HW surface type.
  172. *
  173. * Must be a hwaccel format, i.e. the corresponding descriptor must have the
  174. * AV_PIX_FMT_FLAG_HWACCEL flag set.
  175. *
  176. * Must be set by the user before calling av_hwframe_ctx_init().
  177. */
  178. enum AVPixelFormat format;
  179. /**
  180. * The pixel format identifying the actual data layout of the hardware
  181. * frames.
  182. *
  183. * Must be set by the caller before calling av_hwframe_ctx_init().
  184. *
  185. * @note when the underlying API does not provide the exact data layout, but
  186. * only the colorspace/bit depth, this field should be set to the fully
  187. * planar version of that format (e.g. for 8-bit 420 YUV it should be
  188. * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
  189. */
  190. enum AVPixelFormat sw_format;
  191. /**
  192. * The allocated dimensions of the frames in this pool.
  193. *
  194. * Must be set by the user before calling av_hwframe_ctx_init().
  195. */
  196. int width, height;
  197. } AVHWFramesContext;
  198. /**
  199. * Allocate an AVHWDeviceContext for a given pixel format.
  200. *
  201. * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
  202. * on the corresponding format descriptor)
  203. * @return a reference to the newly created AVHWDeviceContext on success or NULL
  204. * on failure.
  205. */
  206. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
  207. /**
  208. * Finalize the device context before use. This function must be called after
  209. * the context is filled with all the required information and before it is
  210. * used in any way.
  211. *
  212. * @param ref a reference to the AVHWDeviceContext
  213. * @return 0 on success, a negative AVERROR code on failure
  214. */
  215. int av_hwdevice_ctx_init(AVBufferRef *ref);
  216. /**
  217. * Allocate an AVHWFramesContext tied to a given device context.
  218. *
  219. * @param device_ctx a reference to a AVHWDeviceContext. This function will make
  220. * a new reference for internal use, the one passed to the
  221. * function remains owned by the caller.
  222. * @return a reference to the newly created AVHWFramesContext on success or NULL
  223. * on failure.
  224. */
  225. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
  226. /**
  227. * Finalize the context before use. This function must be called after the
  228. * context is filled with all the required information and before it is attached
  229. * to any frames.
  230. *
  231. * @param ref a reference to the AVHWFramesContext
  232. * @return 0 on success, a negative AVERROR code on failure
  233. */
  234. int av_hwframe_ctx_init(AVBufferRef *ref);
  235. /**
  236. * Allocate a new frame attached to the given AVHWFramesContext.
  237. *
  238. * @param hwframe_ctx a reference to an AVHWFramesContext
  239. * @param frame an empty (freshly allocated or unreffed) frame to be filled with
  240. * newly allocated buffers.
  241. * @param flags currently unused, should be set to zero
  242. * @return 0 on success, a negative AVERROR code on failure
  243. */
  244. int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
  245. /**
  246. * Copy data to or from a hw surface. At least one of dst/src must have an
  247. * AVHWFramesContext attached.
  248. *
  249. * If src has an AVHWFramesContext attached, then the format of dst (if set)
  250. * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
  251. * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
  252. * If dst has an AVHWFramesContext attached, then the format of src must use one
  253. * of the formats returned by av_hwframe_transfer_get_formats(dst,
  254. * AV_HWFRAME_TRANSFER_DIRECTION_TO)
  255. *
  256. * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
  257. * data buffers will be allocated by this function using av_frame_get_buffer().
  258. * If dst->format is set, then this format will be used, otherwise (when
  259. * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
  260. *
  261. * @param dst the destination frame. dst is not touched on failure.
  262. * @param src the source frame.
  263. * @param flags currently unused, should be set to zero
  264. * @return 0 on success, a negative AVERROR error code on failure.
  265. */
  266. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
  267. enum AVHWFrameTransferDirection {
  268. /**
  269. * Transfer the data from the queried hw frame.
  270. */
  271. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  272. /**
  273. * Transfer the data to the queried hw frame.
  274. */
  275. AV_HWFRAME_TRANSFER_DIRECTION_TO,
  276. };
  277. /**
  278. * Get a list of possible source or target formats usable in
  279. * av_hwframe_transfer_data().
  280. *
  281. * @param hwframe_ctx the frame context to obtain the information for
  282. * @param dir the direction of the transfer
  283. * @param formats the pointer to the output format list will be written here.
  284. * The list is terminated with AV_PIX_FMT_NONE and must be freed
  285. * by the caller when no longer needed using av_free().
  286. * If this function returns successfully, the format list will
  287. * have at least one item (not counting the terminator).
  288. * On failure, the contents of this pointer are unspecified.
  289. * @param flags currently unused, should be set to zero
  290. * @return 0 on success, a negative AVERROR code on failure.
  291. */
  292. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
  293. enum AVHWFrameTransferDirection dir,
  294. enum AVPixelFormat **formats, int flags);
  295. #endif /* AVUTIL_HWCONTEXT_H */