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.

401 lines
14KB

  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. AV_HWDEVICE_TYPE_VAAPI,
  28. AV_HWDEVICE_TYPE_DXVA2,
  29. };
  30. typedef struct AVHWDeviceInternal AVHWDeviceInternal;
  31. /**
  32. * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
  33. * i.e. state that is not tied to a concrete processing configuration.
  34. * E.g., in an API that supports hardware-accelerated encoding and decoding,
  35. * this struct will (if possible) wrap the state that is common to both encoding
  36. * and decoding and from which specific instances of encoders or decoders can be
  37. * derived.
  38. *
  39. * This struct is reference-counted with the AVBuffer mechanism. The
  40. * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
  41. * points to the actual AVHWDeviceContext. Further objects derived from
  42. * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
  43. * specific properties) will hold an internal reference to it. After all the
  44. * references are released, the AVHWDeviceContext itself will be freed,
  45. * optionally invoking a user-specified callback for uninitializing the hardware
  46. * state.
  47. */
  48. typedef struct AVHWDeviceContext {
  49. /**
  50. * A class for logging. Set by av_hwdevice_ctx_alloc().
  51. */
  52. const AVClass *av_class;
  53. /**
  54. * Private data used internally by libavutil. Must not be accessed in any
  55. * way by the caller.
  56. */
  57. AVHWDeviceInternal *internal;
  58. /**
  59. * This field identifies the underlying API used for hardware access.
  60. *
  61. * This field is set when this struct is allocated and never changed
  62. * afterwards.
  63. */
  64. enum AVHWDeviceType type;
  65. /**
  66. * The format-specific data, allocated and freed by libavutil along with
  67. * this context.
  68. *
  69. * Should be cast by the user to the format-specific context defined in the
  70. * corresponding header (hwcontext_*.h) and filled as described in the
  71. * documentation before calling av_hwdevice_ctx_init().
  72. *
  73. * After calling av_hwdevice_ctx_init() this struct should not be modified
  74. * by the caller.
  75. */
  76. void *hwctx;
  77. /**
  78. * This field may be set by the caller before calling av_hwdevice_ctx_init().
  79. *
  80. * If non-NULL, this callback will be called when the last reference to
  81. * this context is unreferenced, immediately before it is freed.
  82. *
  83. * @note when other objects (e.g an AVHWFramesContext) are derived from this
  84. * struct, this callback will be invoked after all such child objects
  85. * are fully uninitialized and their respective destructors invoked.
  86. */
  87. void (*free)(struct AVHWDeviceContext *ctx);
  88. /**
  89. * Arbitrary user data, to be used e.g. by the free() callback.
  90. */
  91. void *user_opaque;
  92. } AVHWDeviceContext;
  93. typedef struct AVHWFramesInternal AVHWFramesInternal;
  94. /**
  95. * This struct describes a set or pool of "hardware" frames (i.e. those with
  96. * data not located in normal system memory). All the frames in the pool are
  97. * assumed to be allocated in the same way and interchangeable.
  98. *
  99. * This struct is reference-counted with the AVBuffer mechanism and tied to a
  100. * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
  101. * yields a reference, whose data field points to the actual AVHWFramesContext
  102. * struct.
  103. */
  104. typedef struct AVHWFramesContext {
  105. /**
  106. * A class for logging.
  107. */
  108. const AVClass *av_class;
  109. /**
  110. * Private data used internally by libavutil. Must not be accessed in any
  111. * way by the caller.
  112. */
  113. AVHWFramesInternal *internal;
  114. /**
  115. * A reference to the parent AVHWDeviceContext. This reference is owned and
  116. * managed by the enclosing AVHWFramesContext, but the caller may derive
  117. * additional references from it.
  118. */
  119. AVBufferRef *device_ref;
  120. /**
  121. * The parent AVHWDeviceContext. This is simply a pointer to
  122. * device_ref->data provided for convenience.
  123. *
  124. * Set by libavutil in av_hwframe_ctx_init().
  125. */
  126. AVHWDeviceContext *device_ctx;
  127. /**
  128. * The format-specific data, allocated and freed automatically along with
  129. * this context.
  130. *
  131. * Should be cast by the user to the format-specific context defined in the
  132. * corresponding header (hwframe_*.h) and filled as described in the
  133. * documentation before calling av_hwframe_ctx_init().
  134. *
  135. * After any frames using this context are created, the contents of this
  136. * struct should not be modified by the caller.
  137. */
  138. void *hwctx;
  139. /**
  140. * This field may be set by the caller before calling av_hwframe_ctx_init().
  141. *
  142. * If non-NULL, this callback will be called when the last reference to
  143. * this context is unreferenced, immediately before it is freed.
  144. */
  145. void (*free)(struct AVHWFramesContext *ctx);
  146. /**
  147. * Arbitrary user data, to be used e.g. by the free() callback.
  148. */
  149. void *user_opaque;
  150. /**
  151. * A pool from which the frames are allocated by av_hwframe_get_buffer().
  152. * This field may be set by the caller before calling av_hwframe_ctx_init().
  153. * The buffers returned by calling av_buffer_pool_get() on this pool must
  154. * have the properties described in the documentation in the corresponding hw
  155. * type's header (hwcontext_*.h). The pool will be freed strictly before
  156. * this struct's free() callback is invoked.
  157. *
  158. * This field may be NULL, then libavutil will attempt to allocate a pool
  159. * internally. Note that certain device types enforce pools allocated at
  160. * fixed size (frame count), which cannot be extended dynamically. In such a
  161. * case, initial_pool_size must be set appropriately.
  162. */
  163. AVBufferPool *pool;
  164. /**
  165. * Initial size of the frame pool. If a device type does not support
  166. * dynamically resizing the pool, then this is also the maximum pool size.
  167. *
  168. * May be set by the caller before calling av_hwframe_ctx_init(). Must be
  169. * set if pool is NULL and the device type does not support dynamic pools.
  170. */
  171. int initial_pool_size;
  172. /**
  173. * The pixel format identifying the underlying HW surface type.
  174. *
  175. * Must be a hwaccel format, i.e. the corresponding descriptor must have the
  176. * AV_PIX_FMT_FLAG_HWACCEL flag set.
  177. *
  178. * Must be set by the user before calling av_hwframe_ctx_init().
  179. */
  180. enum AVPixelFormat format;
  181. /**
  182. * The pixel format identifying the actual data layout of the hardware
  183. * frames.
  184. *
  185. * Must be set by the caller before calling av_hwframe_ctx_init().
  186. *
  187. * @note when the underlying API does not provide the exact data layout, but
  188. * only the colorspace/bit depth, this field should be set to the fully
  189. * planar version of that format (e.g. for 8-bit 420 YUV it should be
  190. * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
  191. */
  192. enum AVPixelFormat sw_format;
  193. /**
  194. * The allocated dimensions of the frames in this pool.
  195. *
  196. * Must be set by the user before calling av_hwframe_ctx_init().
  197. */
  198. int width, height;
  199. } AVHWFramesContext;
  200. /**
  201. * Allocate an AVHWDeviceContext for a given pixel format.
  202. *
  203. * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
  204. * on the corresponding format descriptor)
  205. * @return a reference to the newly created AVHWDeviceContext on success or NULL
  206. * on failure.
  207. */
  208. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
  209. /**
  210. * Finalize the device context before use. This function must be called after
  211. * the context is filled with all the required information and before it is
  212. * used in any way.
  213. *
  214. * @param ref a reference to the AVHWDeviceContext
  215. * @return 0 on success, a negative AVERROR code on failure
  216. */
  217. int av_hwdevice_ctx_init(AVBufferRef *ref);
  218. /**
  219. * Allocate an AVHWFramesContext tied to a given device context.
  220. *
  221. * @param device_ctx a reference to a AVHWDeviceContext. This function will make
  222. * a new reference for internal use, the one passed to the
  223. * function remains owned by the caller.
  224. * @return a reference to the newly created AVHWFramesContext on success or NULL
  225. * on failure.
  226. */
  227. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
  228. /**
  229. * Finalize the context before use. This function must be called after the
  230. * context is filled with all the required information and before it is attached
  231. * to any frames.
  232. *
  233. * @param ref a reference to the AVHWFramesContext
  234. * @return 0 on success, a negative AVERROR code on failure
  235. */
  236. int av_hwframe_ctx_init(AVBufferRef *ref);
  237. /**
  238. * Allocate a new frame attached to the given AVHWFramesContext.
  239. *
  240. * @param hwframe_ctx a reference to an AVHWFramesContext
  241. * @param frame an empty (freshly allocated or unreffed) frame to be filled with
  242. * newly allocated buffers.
  243. * @param flags currently unused, should be set to zero
  244. * @return 0 on success, a negative AVERROR code on failure
  245. */
  246. int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
  247. /**
  248. * Copy data to or from a hw surface. At least one of dst/src must have an
  249. * AVHWFramesContext attached.
  250. *
  251. * If src has an AVHWFramesContext attached, then the format of dst (if set)
  252. * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
  253. * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
  254. * If dst has an AVHWFramesContext attached, then the format of src must use one
  255. * of the formats returned by av_hwframe_transfer_get_formats(dst,
  256. * AV_HWFRAME_TRANSFER_DIRECTION_TO)
  257. *
  258. * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
  259. * data buffers will be allocated by this function using av_frame_get_buffer().
  260. * If dst->format is set, then this format will be used, otherwise (when
  261. * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
  262. *
  263. * @param dst the destination frame. dst is not touched on failure.
  264. * @param src the source frame.
  265. * @param flags currently unused, should be set to zero
  266. * @return 0 on success, a negative AVERROR error code on failure.
  267. */
  268. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
  269. enum AVHWFrameTransferDirection {
  270. /**
  271. * Transfer the data from the queried hw frame.
  272. */
  273. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  274. /**
  275. * Transfer the data to the queried hw frame.
  276. */
  277. AV_HWFRAME_TRANSFER_DIRECTION_TO,
  278. };
  279. /**
  280. * Get a list of possible source or target formats usable in
  281. * av_hwframe_transfer_data().
  282. *
  283. * @param hwframe_ctx the frame context to obtain the information for
  284. * @param dir the direction of the transfer
  285. * @param formats the pointer to the output format list will be written here.
  286. * The list is terminated with AV_PIX_FMT_NONE and must be freed
  287. * by the caller when no longer needed using av_free().
  288. * If this function returns successfully, the format list will
  289. * have at least one item (not counting the terminator).
  290. * On failure, the contents of this pointer are unspecified.
  291. * @param flags currently unused, should be set to zero
  292. * @return 0 on success, a negative AVERROR code on failure.
  293. */
  294. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
  295. enum AVHWFrameTransferDirection dir,
  296. enum AVPixelFormat **formats, int flags);
  297. /**
  298. * This struct describes the constraints on hardware frames attached to
  299. * a given device with a hardware-specific configuration. This is returned
  300. * by av_hwdevice_get_hwframe_constraints() and must be freed by
  301. * av_hwframe_constraints_free() after use.
  302. */
  303. typedef struct AVHWFramesConstraints {
  304. /**
  305. * A list of possible values for format in the hw_frames_ctx,
  306. * terminated by AV_PIX_FMT_NONE. This member will always be filled.
  307. */
  308. enum AVPixelFormat *valid_hw_formats;
  309. /**
  310. * A list of possible values for sw_format in the hw_frames_ctx,
  311. * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
  312. * not known.
  313. */
  314. enum AVPixelFormat *valid_sw_formats;
  315. /**
  316. * The minimum size of frames in this hw_frames_ctx.
  317. * (Zero if not known.)
  318. */
  319. int min_width;
  320. int min_height;
  321. /**
  322. * The maximum size of frames in this hw_frames_ctx.
  323. * (INT_MAX if not known / no limit.)
  324. */
  325. int max_width;
  326. int max_height;
  327. } AVHWFramesConstraints;
  328. /**
  329. * Allocate a HW-specific configuration structure for a given HW device.
  330. * After use, the user must free all members as required by the specific
  331. * hardware structure being used, then free the structure itself with
  332. * av_free().
  333. *
  334. * @param device_ctx a reference to the associated AVHWDeviceContext.
  335. * @return The newly created HW-specific configuration structure on
  336. * success or NULL on failure.
  337. */
  338. void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
  339. /**
  340. * Get the constraints on HW frames given a device and the HW-specific
  341. * configuration to be used with that device. If no HW-specific
  342. * configuration is provided, returns the maximum possible capabilities
  343. * of the device.
  344. *
  345. * @param device_ctx a reference to the associated AVHWDeviceContext.
  346. * @param hwconfig a filled HW-specific configuration structure, or NULL
  347. * to return the maximum possible capabilities of the device.
  348. * @return AVHWFramesConstraints structure describing the constraints
  349. * on the device, or NULL if not available.
  350. */
  351. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  352. const void *hwconfig);
  353. /**
  354. * Free an AVHWFrameConstraints structure.
  355. *
  356. * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
  357. */
  358. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
  359. #endif /* AVUTIL_HWCONTEXT_H */