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.

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