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.

525 lines
20KB

  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. AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
  31. };
  32. typedef struct AVHWDeviceInternal AVHWDeviceInternal;
  33. /**
  34. * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
  35. * i.e. state that is not tied to a concrete processing configuration.
  36. * E.g., in an API that supports hardware-accelerated encoding and decoding,
  37. * this struct will (if possible) wrap the state that is common to both encoding
  38. * and decoding and from which specific instances of encoders or decoders can be
  39. * derived.
  40. *
  41. * This struct is reference-counted with the AVBuffer mechanism. The
  42. * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
  43. * points to the actual AVHWDeviceContext. Further objects derived from
  44. * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
  45. * specific properties) will hold an internal reference to it. After all the
  46. * references are released, the AVHWDeviceContext itself will be freed,
  47. * optionally invoking a user-specified callback for uninitializing the hardware
  48. * state.
  49. */
  50. typedef struct AVHWDeviceContext {
  51. /**
  52. * A class for logging. Set by av_hwdevice_ctx_alloc().
  53. */
  54. const AVClass *av_class;
  55. /**
  56. * Private data used internally by libavutil. Must not be accessed in any
  57. * way by the caller.
  58. */
  59. AVHWDeviceInternal *internal;
  60. /**
  61. * This field identifies the underlying API used for hardware access.
  62. *
  63. * This field is set when this struct is allocated and never changed
  64. * afterwards.
  65. */
  66. enum AVHWDeviceType type;
  67. /**
  68. * The format-specific data, allocated and freed by libavutil along with
  69. * this context.
  70. *
  71. * Should be cast by the user to the format-specific context defined in the
  72. * corresponding header (hwcontext_*.h) and filled as described in the
  73. * documentation before calling av_hwdevice_ctx_init().
  74. *
  75. * After calling av_hwdevice_ctx_init() this struct should not be modified
  76. * by the caller.
  77. */
  78. void *hwctx;
  79. /**
  80. * This field may be set by the caller before calling av_hwdevice_ctx_init().
  81. *
  82. * If non-NULL, this callback will be called when the last reference to
  83. * this context is unreferenced, immediately before it is freed.
  84. *
  85. * @note when other objects (e.g an AVHWFramesContext) are derived from this
  86. * struct, this callback will be invoked after all such child objects
  87. * are fully uninitialized and their respective destructors invoked.
  88. */
  89. void (*free)(struct AVHWDeviceContext *ctx);
  90. /**
  91. * Arbitrary user data, to be used e.g. by the free() callback.
  92. */
  93. void *user_opaque;
  94. } AVHWDeviceContext;
  95. typedef struct AVHWFramesInternal AVHWFramesInternal;
  96. /**
  97. * This struct describes a set or pool of "hardware" frames (i.e. those with
  98. * data not located in normal system memory). All the frames in the pool are
  99. * assumed to be allocated in the same way and interchangeable.
  100. *
  101. * This struct is reference-counted with the AVBuffer mechanism and tied to a
  102. * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
  103. * yields a reference, whose data field points to the actual AVHWFramesContext
  104. * struct.
  105. */
  106. typedef struct AVHWFramesContext {
  107. /**
  108. * A class for logging.
  109. */
  110. const AVClass *av_class;
  111. /**
  112. * Private data used internally by libavutil. Must not be accessed in any
  113. * way by the caller.
  114. */
  115. AVHWFramesInternal *internal;
  116. /**
  117. * A reference to the parent AVHWDeviceContext. This reference is owned and
  118. * managed by the enclosing AVHWFramesContext, but the caller may derive
  119. * additional references from it.
  120. */
  121. AVBufferRef *device_ref;
  122. /**
  123. * The parent AVHWDeviceContext. This is simply a pointer to
  124. * device_ref->data provided for convenience.
  125. *
  126. * Set by libavutil in av_hwframe_ctx_init().
  127. */
  128. AVHWDeviceContext *device_ctx;
  129. /**
  130. * The format-specific data, allocated and freed automatically along with
  131. * this context.
  132. *
  133. * Should be cast by the user to the format-specific context defined in the
  134. * corresponding header (hwframe_*.h) and filled as described in the
  135. * documentation before calling av_hwframe_ctx_init().
  136. *
  137. * After any frames using this context are created, the contents of this
  138. * struct should not be modified by the caller.
  139. */
  140. void *hwctx;
  141. /**
  142. * This field may be set by the caller before calling av_hwframe_ctx_init().
  143. *
  144. * If non-NULL, this callback will be called when the last reference to
  145. * this context is unreferenced, immediately before it is freed.
  146. */
  147. void (*free)(struct AVHWFramesContext *ctx);
  148. /**
  149. * Arbitrary user data, to be used e.g. by the free() callback.
  150. */
  151. void *user_opaque;
  152. /**
  153. * A pool from which the frames are allocated by av_hwframe_get_buffer().
  154. * This field may be set by the caller before calling av_hwframe_ctx_init().
  155. * The buffers returned by calling av_buffer_pool_get() on this pool must
  156. * have the properties described in the documentation in the corresponding hw
  157. * type's header (hwcontext_*.h). The pool will be freed strictly before
  158. * this struct's free() callback is invoked.
  159. *
  160. * This field may be NULL, then libavutil will attempt to allocate a pool
  161. * internally. Note that certain device types enforce pools allocated at
  162. * fixed size (frame count), which cannot be extended dynamically. In such a
  163. * case, initial_pool_size must be set appropriately.
  164. */
  165. AVBufferPool *pool;
  166. /**
  167. * Initial size of the frame pool. If a device type does not support
  168. * dynamically resizing the pool, then this is also the maximum pool size.
  169. *
  170. * May be set by the caller before calling av_hwframe_ctx_init(). Must be
  171. * set if pool is NULL and the device type does not support dynamic pools.
  172. */
  173. int initial_pool_size;
  174. /**
  175. * The pixel format identifying the underlying HW surface type.
  176. *
  177. * Must be a hwaccel format, i.e. the corresponding descriptor must have the
  178. * AV_PIX_FMT_FLAG_HWACCEL flag set.
  179. *
  180. * Must be set by the user before calling av_hwframe_ctx_init().
  181. */
  182. enum AVPixelFormat format;
  183. /**
  184. * The pixel format identifying the actual data layout of the hardware
  185. * frames.
  186. *
  187. * Must be set by the caller before calling av_hwframe_ctx_init().
  188. *
  189. * @note when the underlying API does not provide the exact data layout, but
  190. * only the colorspace/bit depth, this field should be set to the fully
  191. * planar version of that format (e.g. for 8-bit 420 YUV it should be
  192. * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
  193. */
  194. enum AVPixelFormat sw_format;
  195. /**
  196. * The allocated dimensions of the frames in this pool.
  197. *
  198. * Must be set by the user before calling av_hwframe_ctx_init().
  199. */
  200. int width, height;
  201. } AVHWFramesContext;
  202. /**
  203. * Allocate an AVHWDeviceContext for a given hardware type.
  204. *
  205. * @param type the type of the hardware device to allocate.
  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. * The two frames must have matching allocated dimensions (i.e. equal to
  292. * AVHWFramesContext.width/height), since not all device types support
  293. * transferring a sub-rectangle of the whole surface. The display dimensions
  294. * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
  295. * also have to be equal for both frames. When the display dimensions are
  296. * smaller than the allocated dimensions, the content of the padding in the
  297. * destination frame is unspecified.
  298. *
  299. * @param dst the destination frame. dst is not touched on failure.
  300. * @param src the source frame.
  301. * @param flags currently unused, should be set to zero
  302. * @return 0 on success, a negative AVERROR error code on failure.
  303. */
  304. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
  305. enum AVHWFrameTransferDirection {
  306. /**
  307. * Transfer the data from the queried hw frame.
  308. */
  309. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  310. /**
  311. * Transfer the data to the queried hw frame.
  312. */
  313. AV_HWFRAME_TRANSFER_DIRECTION_TO,
  314. };
  315. /**
  316. * Get a list of possible source or target formats usable in
  317. * av_hwframe_transfer_data().
  318. *
  319. * @param hwframe_ctx the frame context to obtain the information for
  320. * @param dir the direction of the transfer
  321. * @param formats the pointer to the output format list will be written here.
  322. * The list is terminated with AV_PIX_FMT_NONE and must be freed
  323. * by the caller when no longer needed using av_free().
  324. * If this function returns successfully, the format list will
  325. * have at least one item (not counting the terminator).
  326. * On failure, the contents of this pointer are unspecified.
  327. * @param flags currently unused, should be set to zero
  328. * @return 0 on success, a negative AVERROR code on failure.
  329. */
  330. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
  331. enum AVHWFrameTransferDirection dir,
  332. enum AVPixelFormat **formats, int flags);
  333. /**
  334. * This struct describes the constraints on hardware frames attached to
  335. * a given device with a hardware-specific configuration. This is returned
  336. * by av_hwdevice_get_hwframe_constraints() and must be freed by
  337. * av_hwframe_constraints_free() after use.
  338. */
  339. typedef struct AVHWFramesConstraints {
  340. /**
  341. * A list of possible values for format in the hw_frames_ctx,
  342. * terminated by AV_PIX_FMT_NONE. This member will always be filled.
  343. */
  344. enum AVPixelFormat *valid_hw_formats;
  345. /**
  346. * A list of possible values for sw_format in the hw_frames_ctx,
  347. * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
  348. * not known.
  349. */
  350. enum AVPixelFormat *valid_sw_formats;
  351. /**
  352. * The minimum size of frames in this hw_frames_ctx.
  353. * (Zero if not known.)
  354. */
  355. int min_width;
  356. int min_height;
  357. /**
  358. * The maximum size of frames in this hw_frames_ctx.
  359. * (INT_MAX if not known / no limit.)
  360. */
  361. int max_width;
  362. int max_height;
  363. } AVHWFramesConstraints;
  364. /**
  365. * Allocate a HW-specific configuration structure for a given HW device.
  366. * After use, the user must free all members as required by the specific
  367. * hardware structure being used, then free the structure itself with
  368. * av_free().
  369. *
  370. * @param device_ctx a reference to the associated AVHWDeviceContext.
  371. * @return The newly created HW-specific configuration structure on
  372. * success or NULL on failure.
  373. */
  374. void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
  375. /**
  376. * Get the constraints on HW frames given a device and the HW-specific
  377. * configuration to be used with that device. If no HW-specific
  378. * configuration is provided, returns the maximum possible capabilities
  379. * of the device.
  380. *
  381. * @param ref a reference to the associated AVHWDeviceContext.
  382. * @param hwconfig a filled HW-specific configuration structure, or NULL
  383. * to return the maximum possible capabilities of the device.
  384. * @return AVHWFramesConstraints structure describing the constraints
  385. * on the device, or NULL if not available.
  386. */
  387. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  388. const void *hwconfig);
  389. /**
  390. * Free an AVHWFrameConstraints structure.
  391. *
  392. * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
  393. */
  394. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
  395. /**
  396. * Flags to apply to frame mappings.
  397. */
  398. enum {
  399. /**
  400. * The mapping must be readable.
  401. */
  402. AV_HWFRAME_MAP_READ = 1 << 0,
  403. /**
  404. * The mapping must be writeable.
  405. */
  406. AV_HWFRAME_MAP_WRITE = 1 << 1,
  407. /**
  408. * The mapped frame will be overwritten completely in subsequent
  409. * operations, so the current frame data need not be loaded. Any values
  410. * which are not overwritten are unspecified.
  411. */
  412. AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
  413. /**
  414. * The mapping must be direct. That is, there must not be any copying in
  415. * the map or unmap steps. Note that performance of direct mappings may
  416. * be much lower than normal memory.
  417. */
  418. AV_HWFRAME_MAP_DIRECT = 1 << 3,
  419. };
  420. /**
  421. * Map a hardware frame.
  422. *
  423. * This has a number of different possible effects, depending on the format
  424. * and origin of the src and dst frames. On input, src should be a usable
  425. * frame with valid buffers and dst should be blank (typically as just created
  426. * by av_frame_alloc()). src should have an associated hwframe context, and
  427. * dst may optionally have a format and associated hwframe context.
  428. *
  429. * If src was created by mapping a frame from the hwframe context of dst,
  430. * then this function undoes the mapping - dst is replaced by a reference to
  431. * the frame that src was originally mapped from.
  432. *
  433. * If both src and dst have an associated hwframe context, then this function
  434. * attempts to map the src frame from its hardware context to that of dst and
  435. * then fill dst with appropriate data to be usable there. This will only be
  436. * possible if the hwframe contexts and associated devices are compatible -
  437. * given compatible devices, av_hwframe_ctx_create_derived() can be used to
  438. * create a hwframe context for dst in which mapping should be possible.
  439. *
  440. * If src has a hwframe context but dst does not, then the src frame is
  441. * mapped to normal memory and should thereafter be usable as a normal frame.
  442. * If the format is set on dst, then the mapping will attempt to create dst
  443. * with that format and fail if it is not possible. If format is unset (is
  444. * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
  445. * format to use is (probably the sw_format of the src hwframe context).
  446. *
  447. * A return value of AVERROR(ENOSYS) indicates that the mapping is not
  448. * possible with the given arguments and hwframe setup, while other return
  449. * values indicate that it failed somehow.
  450. *
  451. * @param dst Destination frame, to contain the mapping.
  452. * @param src Source frame, to be mapped.
  453. * @param flags Some combination of AV_HWFRAME_MAP_* flags.
  454. * @return Zero on success, negative AVERROR code on failure.
  455. */
  456. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
  457. /**
  458. * Create and initialise an AVHWFramesContext as a mapping of another existing
  459. * AVHWFramesContext on a different device.
  460. *
  461. * av_hwframe_ctx_init() should not be called after this.
  462. *
  463. * @param derived_frame_ctx On success, a reference to the newly created
  464. * AVHWFramesContext.
  465. * @param derived_device_ctx A reference to the device to create the new
  466. * AVHWFramesContext on.
  467. * @param source_frame_ctx A reference to an existing AVHWFramesContext
  468. * which will be mapped to the derived context.
  469. * @param flags Currently unused; should be set to zero.
  470. * @return Zero on success, negative AVERROR code on failure.
  471. */
  472. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  473. enum AVPixelFormat format,
  474. AVBufferRef *derived_device_ctx,
  475. AVBufferRef *source_frame_ctx,
  476. int flags);
  477. #endif /* AVUTIL_HWCONTEXT_H */