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.

584 lines
22KB

  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_NONE,
  26. AV_HWDEVICE_TYPE_VDPAU,
  27. AV_HWDEVICE_TYPE_CUDA,
  28. AV_HWDEVICE_TYPE_VAAPI,
  29. AV_HWDEVICE_TYPE_DXVA2,
  30. AV_HWDEVICE_TYPE_QSV,
  31. AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
  32. AV_HWDEVICE_TYPE_D3D11VA,
  33. AV_HWDEVICE_TYPE_DRM,
  34. AV_HWDEVICE_TYPE_OPENCL,
  35. };
  36. typedef struct AVHWDeviceInternal AVHWDeviceInternal;
  37. /**
  38. * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
  39. * i.e. state that is not tied to a concrete processing configuration.
  40. * E.g., in an API that supports hardware-accelerated encoding and decoding,
  41. * this struct will (if possible) wrap the state that is common to both encoding
  42. * and decoding and from which specific instances of encoders or decoders can be
  43. * derived.
  44. *
  45. * This struct is reference-counted with the AVBuffer mechanism. The
  46. * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
  47. * points to the actual AVHWDeviceContext. Further objects derived from
  48. * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
  49. * specific properties) will hold an internal reference to it. After all the
  50. * references are released, the AVHWDeviceContext itself will be freed,
  51. * optionally invoking a user-specified callback for uninitializing the hardware
  52. * state.
  53. */
  54. typedef struct AVHWDeviceContext {
  55. /**
  56. * A class for logging. Set by av_hwdevice_ctx_alloc().
  57. */
  58. const AVClass *av_class;
  59. /**
  60. * Private data used internally by libavutil. Must not be accessed in any
  61. * way by the caller.
  62. */
  63. AVHWDeviceInternal *internal;
  64. /**
  65. * This field identifies the underlying API used for hardware access.
  66. *
  67. * This field is set when this struct is allocated and never changed
  68. * afterwards.
  69. */
  70. enum AVHWDeviceType type;
  71. /**
  72. * The format-specific data, allocated and freed by libavutil along with
  73. * this context.
  74. *
  75. * Should be cast by the user to the format-specific context defined in the
  76. * corresponding header (hwcontext_*.h) and filled as described in the
  77. * documentation before calling av_hwdevice_ctx_init().
  78. *
  79. * After calling av_hwdevice_ctx_init() this struct should not be modified
  80. * by the caller.
  81. */
  82. void *hwctx;
  83. /**
  84. * This field may be set by the caller before calling av_hwdevice_ctx_init().
  85. *
  86. * If non-NULL, this callback will be called when the last reference to
  87. * this context is unreferenced, immediately before it is freed.
  88. *
  89. * @note when other objects (e.g an AVHWFramesContext) are derived from this
  90. * struct, this callback will be invoked after all such child objects
  91. * are fully uninitialized and their respective destructors invoked.
  92. */
  93. void (*free)(struct AVHWDeviceContext *ctx);
  94. /**
  95. * Arbitrary user data, to be used e.g. by the free() callback.
  96. */
  97. void *user_opaque;
  98. } AVHWDeviceContext;
  99. typedef struct AVHWFramesInternal AVHWFramesInternal;
  100. /**
  101. * This struct describes a set or pool of "hardware" frames (i.e. those with
  102. * data not located in normal system memory). All the frames in the pool are
  103. * assumed to be allocated in the same way and interchangeable.
  104. *
  105. * This struct is reference-counted with the AVBuffer mechanism and tied to a
  106. * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
  107. * yields a reference, whose data field points to the actual AVHWFramesContext
  108. * struct.
  109. */
  110. typedef struct AVHWFramesContext {
  111. /**
  112. * A class for logging.
  113. */
  114. const AVClass *av_class;
  115. /**
  116. * Private data used internally by libavutil. Must not be accessed in any
  117. * way by the caller.
  118. */
  119. AVHWFramesInternal *internal;
  120. /**
  121. * A reference to the parent AVHWDeviceContext. This reference is owned and
  122. * managed by the enclosing AVHWFramesContext, but the caller may derive
  123. * additional references from it.
  124. */
  125. AVBufferRef *device_ref;
  126. /**
  127. * The parent AVHWDeviceContext. This is simply a pointer to
  128. * device_ref->data provided for convenience.
  129. *
  130. * Set by libavutil in av_hwframe_ctx_init().
  131. */
  132. AVHWDeviceContext *device_ctx;
  133. /**
  134. * The format-specific data, allocated and freed automatically along with
  135. * this context.
  136. *
  137. * Should be cast by the user to the format-specific context defined in the
  138. * corresponding header (hwframe_*.h) and filled as described in the
  139. * documentation before calling av_hwframe_ctx_init().
  140. *
  141. * After any frames using this context are created, the contents of this
  142. * struct should not be modified by the caller.
  143. */
  144. void *hwctx;
  145. /**
  146. * This field may be set by the caller before calling av_hwframe_ctx_init().
  147. *
  148. * If non-NULL, this callback will be called when the last reference to
  149. * this context is unreferenced, immediately before it is freed.
  150. */
  151. void (*free)(struct AVHWFramesContext *ctx);
  152. /**
  153. * Arbitrary user data, to be used e.g. by the free() callback.
  154. */
  155. void *user_opaque;
  156. /**
  157. * A pool from which the frames are allocated by av_hwframe_get_buffer().
  158. * This field may be set by the caller before calling av_hwframe_ctx_init().
  159. * The buffers returned by calling av_buffer_pool_get() on this pool must
  160. * have the properties described in the documentation in the corresponding hw
  161. * type's header (hwcontext_*.h). The pool will be freed strictly before
  162. * this struct's free() callback is invoked.
  163. *
  164. * This field may be NULL, then libavutil will attempt to allocate a pool
  165. * internally. Note that certain device types enforce pools allocated at
  166. * fixed size (frame count), which cannot be extended dynamically. In such a
  167. * case, initial_pool_size must be set appropriately.
  168. */
  169. AVBufferPool *pool;
  170. /**
  171. * Initial size of the frame pool. If a device type does not support
  172. * dynamically resizing the pool, then this is also the maximum pool size.
  173. *
  174. * May be set by the caller before calling av_hwframe_ctx_init(). Must be
  175. * set if pool is NULL and the device type does not support dynamic pools.
  176. */
  177. int initial_pool_size;
  178. /**
  179. * The pixel format identifying the underlying HW surface type.
  180. *
  181. * Must be a hwaccel format, i.e. the corresponding descriptor must have the
  182. * AV_PIX_FMT_FLAG_HWACCEL flag set.
  183. *
  184. * Must be set by the user before calling av_hwframe_ctx_init().
  185. */
  186. enum AVPixelFormat format;
  187. /**
  188. * The pixel format identifying the actual data layout of the hardware
  189. * frames.
  190. *
  191. * Must be set by the caller before calling av_hwframe_ctx_init().
  192. *
  193. * @note when the underlying API does not provide the exact data layout, but
  194. * only the colorspace/bit depth, this field should be set to the fully
  195. * planar version of that format (e.g. for 8-bit 420 YUV it should be
  196. * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
  197. */
  198. enum AVPixelFormat sw_format;
  199. /**
  200. * The allocated dimensions of the frames in this pool.
  201. *
  202. * Must be set by the user before calling av_hwframe_ctx_init().
  203. */
  204. int width, height;
  205. } AVHWFramesContext;
  206. /**
  207. * Look up an AVHWDeviceType by name.
  208. *
  209. * @param name String name of the device type (case-insensitive).
  210. * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
  211. * not found.
  212. */
  213. enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
  214. /** Get the string name of an AVHWDeviceType.
  215. *
  216. * @param type Type from enum AVHWDeviceType.
  217. * @return Pointer to a static string containing the name, or NULL if the type
  218. * is not valid.
  219. */
  220. const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
  221. /**
  222. * Iterate over supported device types.
  223. *
  224. * @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type
  225. * returned by this function in subsequent iterations.
  226. * @return The next usable device type from enum AVHWDeviceType, or
  227. * AV_HWDEVICE_TYPE_NONE if there are no more.
  228. */
  229. enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
  230. /**
  231. * Allocate an AVHWDeviceContext for a given hardware type.
  232. *
  233. * @param type the type of the hardware device to allocate.
  234. * @return a reference to the newly created AVHWDeviceContext on success or NULL
  235. * on failure.
  236. */
  237. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
  238. /**
  239. * Finalize the device context before use. This function must be called after
  240. * the context is filled with all the required information and before it is
  241. * used in any way.
  242. *
  243. * @param ref a reference to the AVHWDeviceContext
  244. * @return 0 on success, a negative AVERROR code on failure
  245. */
  246. int av_hwdevice_ctx_init(AVBufferRef *ref);
  247. /**
  248. * Open a device of the specified type and create an AVHWDeviceContext for it.
  249. *
  250. * This is a convenience function intended to cover the simple cases. Callers
  251. * who need to fine-tune device creation/management should open the device
  252. * manually and then wrap it in an AVHWDeviceContext using
  253. * av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().
  254. *
  255. * The returned context is already initialized and ready for use, the caller
  256. * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of
  257. * the created AVHWDeviceContext are set by this function and should not be
  258. * touched by the caller.
  259. *
  260. * @param device_ctx On success, a reference to the newly-created device context
  261. * will be written here. The reference is owned by the caller
  262. * and must be released with av_buffer_unref() when no longer
  263. * needed. On failure, NULL will be written to this pointer.
  264. * @param type The type of the device to create.
  265. * @param device A type-specific string identifying the device to open.
  266. * @param opts A dictionary of additional (type-specific) options to use in
  267. * opening the device. The dictionary remains owned by the caller.
  268. * @param flags currently unused
  269. *
  270. * @return 0 on success, a negative AVERROR code on failure.
  271. */
  272. int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
  273. const char *device, AVDictionary *opts, int flags);
  274. /**
  275. * Create a new device of the specified type from an existing device.
  276. *
  277. * If the source device is a device of the target type or was originally
  278. * derived from such a device (possibly through one or more intermediate
  279. * devices of other types), then this will return a reference to the
  280. * existing device of the same type as is requested.
  281. *
  282. * Otherwise, it will attempt to derive a new device from the given source
  283. * device. If direct derivation to the new type is not implemented, it will
  284. * attempt the same derivation from each ancestor of the source device in
  285. * turn looking for an implemented derivation method.
  286. *
  287. * @param dst_ctx On success, a reference to the newly-created
  288. * AVHWDeviceContext.
  289. * @param type The type of the new device to create.
  290. * @param src_ctx A reference to an existing AVHWDeviceContext which will be
  291. * used to create the new device.
  292. * @param flags Currently unused; should be set to zero.
  293. * @return Zero on success, a negative AVERROR code on failure.
  294. */
  295. int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
  296. enum AVHWDeviceType type,
  297. AVBufferRef *src_ctx, int flags);
  298. /**
  299. * Allocate an AVHWFramesContext tied to a given device context.
  300. *
  301. * @param device_ctx a reference to a AVHWDeviceContext. This function will make
  302. * a new reference for internal use, the one passed to the
  303. * function remains owned by the caller.
  304. * @return a reference to the newly created AVHWFramesContext on success or NULL
  305. * on failure.
  306. */
  307. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
  308. /**
  309. * Finalize the context before use. This function must be called after the
  310. * context is filled with all the required information and before it is attached
  311. * to any frames.
  312. *
  313. * @param ref a reference to the AVHWFramesContext
  314. * @return 0 on success, a negative AVERROR code on failure
  315. */
  316. int av_hwframe_ctx_init(AVBufferRef *ref);
  317. /**
  318. * Allocate a new frame attached to the given AVHWFramesContext.
  319. *
  320. * @param hwframe_ctx a reference to an AVHWFramesContext
  321. * @param frame an empty (freshly allocated or unreffed) frame to be filled with
  322. * newly allocated buffers.
  323. * @param flags currently unused, should be set to zero
  324. * @return 0 on success, a negative AVERROR code on failure
  325. */
  326. int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
  327. /**
  328. * Copy data to or from a hw surface. At least one of dst/src must have an
  329. * AVHWFramesContext attached.
  330. *
  331. * If src has an AVHWFramesContext attached, then the format of dst (if set)
  332. * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
  333. * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
  334. * If dst has an AVHWFramesContext attached, then the format of src must use one
  335. * of the formats returned by av_hwframe_transfer_get_formats(dst,
  336. * AV_HWFRAME_TRANSFER_DIRECTION_TO)
  337. *
  338. * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
  339. * data buffers will be allocated by this function using av_frame_get_buffer().
  340. * If dst->format is set, then this format will be used, otherwise (when
  341. * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
  342. *
  343. * The two frames must have matching allocated dimensions (i.e. equal to
  344. * AVHWFramesContext.width/height), since not all device types support
  345. * transferring a sub-rectangle of the whole surface. The display dimensions
  346. * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
  347. * also have to be equal for both frames. When the display dimensions are
  348. * smaller than the allocated dimensions, the content of the padding in the
  349. * destination frame is unspecified.
  350. *
  351. * @param dst the destination frame. dst is not touched on failure.
  352. * @param src the source frame.
  353. * @param flags currently unused, should be set to zero
  354. * @return 0 on success, a negative AVERROR error code on failure.
  355. */
  356. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
  357. enum AVHWFrameTransferDirection {
  358. /**
  359. * Transfer the data from the queried hw frame.
  360. */
  361. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  362. /**
  363. * Transfer the data to the queried hw frame.
  364. */
  365. AV_HWFRAME_TRANSFER_DIRECTION_TO,
  366. };
  367. /**
  368. * Get a list of possible source or target formats usable in
  369. * av_hwframe_transfer_data().
  370. *
  371. * @param hwframe_ctx the frame context to obtain the information for
  372. * @param dir the direction of the transfer
  373. * @param formats the pointer to the output format list will be written here.
  374. * The list is terminated with AV_PIX_FMT_NONE and must be freed
  375. * by the caller when no longer needed using av_free().
  376. * If this function returns successfully, the format list will
  377. * have at least one item (not counting the terminator).
  378. * On failure, the contents of this pointer are unspecified.
  379. * @param flags currently unused, should be set to zero
  380. * @return 0 on success, a negative AVERROR code on failure.
  381. */
  382. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
  383. enum AVHWFrameTransferDirection dir,
  384. enum AVPixelFormat **formats, int flags);
  385. /**
  386. * This struct describes the constraints on hardware frames attached to
  387. * a given device with a hardware-specific configuration. This is returned
  388. * by av_hwdevice_get_hwframe_constraints() and must be freed by
  389. * av_hwframe_constraints_free() after use.
  390. */
  391. typedef struct AVHWFramesConstraints {
  392. /**
  393. * A list of possible values for format in the hw_frames_ctx,
  394. * terminated by AV_PIX_FMT_NONE. This member will always be filled.
  395. */
  396. enum AVPixelFormat *valid_hw_formats;
  397. /**
  398. * A list of possible values for sw_format in the hw_frames_ctx,
  399. * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
  400. * not known.
  401. */
  402. enum AVPixelFormat *valid_sw_formats;
  403. /**
  404. * The minimum size of frames in this hw_frames_ctx.
  405. * (Zero if not known.)
  406. */
  407. int min_width;
  408. int min_height;
  409. /**
  410. * The maximum size of frames in this hw_frames_ctx.
  411. * (INT_MAX if not known / no limit.)
  412. */
  413. int max_width;
  414. int max_height;
  415. } AVHWFramesConstraints;
  416. /**
  417. * Allocate a HW-specific configuration structure for a given HW device.
  418. * After use, the user must free all members as required by the specific
  419. * hardware structure being used, then free the structure itself with
  420. * av_free().
  421. *
  422. * @param device_ctx a reference to the associated AVHWDeviceContext.
  423. * @return The newly created HW-specific configuration structure on
  424. * success or NULL on failure.
  425. */
  426. void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
  427. /**
  428. * Get the constraints on HW frames given a device and the HW-specific
  429. * configuration to be used with that device. If no HW-specific
  430. * configuration is provided, returns the maximum possible capabilities
  431. * of the device.
  432. *
  433. * @param ref a reference to the associated AVHWDeviceContext.
  434. * @param hwconfig a filled HW-specific configuration structure, or NULL
  435. * to return the maximum possible capabilities of the device.
  436. * @return AVHWFramesConstraints structure describing the constraints
  437. * on the device, or NULL if not available.
  438. */
  439. AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
  440. const void *hwconfig);
  441. /**
  442. * Free an AVHWFrameConstraints structure.
  443. *
  444. * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
  445. */
  446. void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
  447. /**
  448. * Flags to apply to frame mappings.
  449. */
  450. enum {
  451. /**
  452. * The mapping must be readable.
  453. */
  454. AV_HWFRAME_MAP_READ = 1 << 0,
  455. /**
  456. * The mapping must be writeable.
  457. */
  458. AV_HWFRAME_MAP_WRITE = 1 << 1,
  459. /**
  460. * The mapped frame will be overwritten completely in subsequent
  461. * operations, so the current frame data need not be loaded. Any values
  462. * which are not overwritten are unspecified.
  463. */
  464. AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
  465. /**
  466. * The mapping must be direct. That is, there must not be any copying in
  467. * the map or unmap steps. Note that performance of direct mappings may
  468. * be much lower than normal memory.
  469. */
  470. AV_HWFRAME_MAP_DIRECT = 1 << 3,
  471. };
  472. /**
  473. * Map a hardware frame.
  474. *
  475. * This has a number of different possible effects, depending on the format
  476. * and origin of the src and dst frames. On input, src should be a usable
  477. * frame with valid buffers and dst should be blank (typically as just created
  478. * by av_frame_alloc()). src should have an associated hwframe context, and
  479. * dst may optionally have a format and associated hwframe context.
  480. *
  481. * If src was created by mapping a frame from the hwframe context of dst,
  482. * then this function undoes the mapping - dst is replaced by a reference to
  483. * the frame that src was originally mapped from.
  484. *
  485. * If both src and dst have an associated hwframe context, then this function
  486. * attempts to map the src frame from its hardware context to that of dst and
  487. * then fill dst with appropriate data to be usable there. This will only be
  488. * possible if the hwframe contexts and associated devices are compatible -
  489. * given compatible devices, av_hwframe_ctx_create_derived() can be used to
  490. * create a hwframe context for dst in which mapping should be possible.
  491. *
  492. * If src has a hwframe context but dst does not, then the src frame is
  493. * mapped to normal memory and should thereafter be usable as a normal frame.
  494. * If the format is set on dst, then the mapping will attempt to create dst
  495. * with that format and fail if it is not possible. If format is unset (is
  496. * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
  497. * format to use is (probably the sw_format of the src hwframe context).
  498. *
  499. * A return value of AVERROR(ENOSYS) indicates that the mapping is not
  500. * possible with the given arguments and hwframe setup, while other return
  501. * values indicate that it failed somehow.
  502. *
  503. * @param dst Destination frame, to contain the mapping.
  504. * @param src Source frame, to be mapped.
  505. * @param flags Some combination of AV_HWFRAME_MAP_* flags.
  506. * @return Zero on success, negative AVERROR code on failure.
  507. */
  508. int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
  509. /**
  510. * Create and initialise an AVHWFramesContext as a mapping of another existing
  511. * AVHWFramesContext on a different device.
  512. *
  513. * av_hwframe_ctx_init() should not be called after this.
  514. *
  515. * @param derived_frame_ctx On success, a reference to the newly created
  516. * AVHWFramesContext.
  517. * @param derived_device_ctx A reference to the device to create the new
  518. * AVHWFramesContext on.
  519. * @param source_frame_ctx A reference to an existing AVHWFramesContext
  520. * which will be mapped to the derived context.
  521. * @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the
  522. * mapping parameters to apply to frames which are allocated
  523. * in the derived device.
  524. * @return Zero on success, negative AVERROR code on failure.
  525. */
  526. int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
  527. enum AVPixelFormat format,
  528. AVBufferRef *derived_device_ctx,
  529. AVBufferRef *source_frame_ctx,
  530. int flags);
  531. #endif /* AVUTIL_HWCONTEXT_H */