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.

400 lines
14KB

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