hwcontext.h 16 KB

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