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.

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