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.

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