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.

329 lines
12KB

  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. };
  27. typedef struct AVHWDeviceInternal AVHWDeviceInternal;
  28. /**
  29. * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
  30. * i.e. state that is not tied to a concrete processing configuration.
  31. * E.g., in an API that supports hardware-accelerated encoding and decoding,
  32. * this struct will (if possible) wrap the state that is common to both encoding
  33. * and decoding and from which specific instances of encoders or decoders can be
  34. * derived.
  35. *
  36. * This struct is reference-counted with the AVBuffer mechanism. The
  37. * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
  38. * points to the actual AVHWDeviceContext. Further objects derived from
  39. * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
  40. * specific properties) will hold an internal reference to it. After all the
  41. * references are released, the AVHWDeviceContext itself will be freed,
  42. * optionally invoking a user-specified callback for uninitializing the hardware
  43. * state.
  44. */
  45. typedef struct AVHWDeviceContext {
  46. /**
  47. * A class for logging. Set by av_hwdevice_ctx_alloc().
  48. */
  49. const AVClass *av_class;
  50. /**
  51. * Private data used internally by libavutil. Must not be accessed in any
  52. * way by the caller.
  53. */
  54. AVHWDeviceInternal *internal;
  55. /**
  56. * This field identifies the underlying API used for hardware access.
  57. *
  58. * This field is set when this struct is allocated and never changed
  59. * afterwards.
  60. */
  61. enum AVHWDeviceType type;
  62. /**
  63. * The format-specific data, allocated and freed by libavutil along with
  64. * this context.
  65. *
  66. * Should be cast by the user to the format-specific context defined in the
  67. * corresponding header (hwcontext_*.h) and filled as described in the
  68. * documentation before calling av_hwdevice_ctx_init().
  69. *
  70. * After calling av_hwdevice_ctx_init() this struct should not be modified
  71. * by the caller.
  72. */
  73. void *hwctx;
  74. /**
  75. * This field may be set by the caller before calling av_hwdevice_ctx_init().
  76. *
  77. * If non-NULL, this callback will be called when the last reference to
  78. * this context is unreferenced, immediately before it is freed.
  79. *
  80. * @note when other objects (e.g an AVHWFramesContext) are derived from this
  81. * struct, this callback will be invoked after all such child objects
  82. * are fully uninitialized and their respective destructors invoked.
  83. */
  84. void (*free)(struct AVHWDeviceContext *ctx);
  85. /**
  86. * Arbitrary user data, to be used e.g. by the free() callback.
  87. */
  88. void *user_opaque;
  89. } AVHWDeviceContext;
  90. typedef struct AVHWFramesInternal AVHWFramesInternal;
  91. /**
  92. * This struct describes a set or pool of "hardware" frames (i.e. those with
  93. * data not located in normal system memory). All the frames in the pool are
  94. * assumed to be allocated in the same way and interchangeable.
  95. *
  96. * This struct is reference-counted with the AVBuffer mechanism and tied to a
  97. * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
  98. * yields a reference, whose data field points to the actual AVHWFramesContext
  99. * struct.
  100. */
  101. typedef struct AVHWFramesContext {
  102. /**
  103. * A class for logging.
  104. */
  105. const AVClass *av_class;
  106. /**
  107. * Private data used internally by libavutil. Must not be accessed in any
  108. * way by the caller.
  109. */
  110. AVHWFramesInternal *internal;
  111. /**
  112. * A reference to the parent AVHWDeviceContext. This reference is owned and
  113. * managed by the enclosing AVHWFramesContext, but the caller may derive
  114. * additional references from it.
  115. */
  116. AVBufferRef *device_ref;
  117. /**
  118. * The parent AVHWDeviceContext. This is simply a pointer to
  119. * device_ref->data provided for convenience.
  120. *
  121. * Set by libavutil in av_hwframe_ctx_init().
  122. */
  123. AVHWDeviceContext *device_ctx;
  124. /**
  125. * The format-specific data, allocated and freed automatically along with
  126. * this context.
  127. *
  128. * Should be cast by the user to the format-specific context defined in the
  129. * corresponding header (hwframe_*.h) and filled as described in the
  130. * documentation before calling av_hwframe_ctx_init().
  131. *
  132. * After any frames using this context are created, the contents of this
  133. * struct should not be modified by the caller.
  134. */
  135. void *hwctx;
  136. /**
  137. * This field may be set by the caller before calling av_hwframe_ctx_init().
  138. *
  139. * If non-NULL, this callback will be called when the last reference to
  140. * this context is unreferenced, immediately before it is freed.
  141. */
  142. void (*free)(struct AVHWFramesContext *ctx);
  143. /**
  144. * Arbitrary user data, to be used e.g. by the free() callback.
  145. */
  146. void *user_opaque;
  147. /**
  148. * A pool from which the frames are allocated by av_hwframe_get_buffer().
  149. * This field may be set by the caller before calling av_hwframe_ctx_init().
  150. * The buffers returned by calling av_buffer_pool_get() on this pool must
  151. * have the properties described in the documentation in the correponding hw
  152. * type's header (hwcontext_*.h). The pool will be freed strictly before
  153. * this struct's free() callback is invoked.
  154. *
  155. * This field may be NULL, then libavutil will attempt to allocate a pool
  156. * internally. Note that certain device types enforce pools allocated at
  157. * fixed size (frame count), which cannot be extended dynamically. In such a
  158. * case, initial_pool_size must be set appropriately.
  159. */
  160. AVBufferPool *pool;
  161. /**
  162. * Initial size of the frame pool. If a device type does not support
  163. * dynamically resizing the pool, then this is also the maximum pool size.
  164. *
  165. * May be set by the caller before calling av_hwframe_ctx_init(). Must be
  166. * set if pool is NULL and the device type does not support dynamic pools.
  167. */
  168. int initial_pool_size;
  169. /**
  170. * The pixel format identifying the underlying HW surface type.
  171. *
  172. * Must be a hwaccel format, i.e. the corresponding descriptor must have the
  173. * AV_PIX_FMT_FLAG_HWACCEL flag set.
  174. *
  175. * Must be set by the user before calling av_hwframe_ctx_init().
  176. */
  177. enum AVPixelFormat format;
  178. /**
  179. * The pixel format identifying the actual data layout of the hardware
  180. * frames.
  181. *
  182. * Must be set by the caller before calling av_hwframe_ctx_init().
  183. *
  184. * @note when the underlying API does not provide the exact data layout, but
  185. * only the colorspace/bit depth, this field should be set to the fully
  186. * planar version of that format (e.g. for 8-bit 420 YUV it should be
  187. * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
  188. */
  189. enum AVPixelFormat sw_format;
  190. /**
  191. * The allocated dimensions of the frames in this pool.
  192. *
  193. * Must be set by the user before calling av_hwframe_ctx_init().
  194. */
  195. int width, height;
  196. } AVHWFramesContext;
  197. /**
  198. * Allocate an AVHWDeviceContext for a given pixel format.
  199. *
  200. * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
  201. * on the corresponding format descriptor)
  202. * @return a reference to the newly created AVHWDeviceContext on success or NULL
  203. * on failure.
  204. */
  205. AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
  206. /**
  207. * Finalize the device context before use. This function must be called after
  208. * the context is filled with all the required information and before it is
  209. * used in any way.
  210. *
  211. * @param ref a reference to the AVHWDeviceContext
  212. * @return 0 on success, a negative AVERROR code on failure
  213. */
  214. int av_hwdevice_ctx_init(AVBufferRef *ref);
  215. /**
  216. * Allocate an AVHWFramesContext tied to a given device context.
  217. *
  218. * @param device_ctx a reference to a AVHWDeviceContext. This function will make
  219. * a new reference for internal use, the one passed to the
  220. * function remains owned by the caller.
  221. * @return a reference to the newly created AVHWFramesContext on success or NULL
  222. * on failure.
  223. */
  224. AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
  225. /**
  226. * Finalize the context before use. This function must be called after the
  227. * context is filled with all the required information and before it is attached
  228. * to any frames.
  229. *
  230. * @param ref a reference to the AVHWFramesContext
  231. * @return 0 on success, a negative AVERROR code on failure
  232. */
  233. int av_hwframe_ctx_init(AVBufferRef *ref);
  234. /**
  235. * Allocate a new frame attached to the given AVHWFramesContext.
  236. *
  237. * @param hwframe_ctx a reference to an AVHWFramesContext
  238. * @param frame an empty (freshly allocated or unreffed) frame to be filled with
  239. * newly allocated buffers.
  240. * @param flags currently unused, should be set to zero
  241. * @return 0 on success, a negative AVERROR code on failure
  242. */
  243. int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
  244. /**
  245. * Copy data to or from a hw surface. At least one of dst/src must have an
  246. * AVHWFramesContext attached.
  247. *
  248. * If src has an AVHWFramesContext attached, then the format of dst (if set)
  249. * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
  250. * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
  251. * If dst has an AVHWFramesContext attached, then the format of src must use one
  252. * of the formats returned by av_hwframe_transfer_get_formats(dst,
  253. * AV_HWFRAME_TRANSFER_DIRECTION_TO)
  254. *
  255. * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
  256. * data buffers will be allocated by this function using av_frame_get_buffer().
  257. * If dst->format is set, then this format will be used, otherwise (when
  258. * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
  259. *
  260. * @param dst the destination frame. dst is not touched on failure.
  261. * @param src the source frame.
  262. * @param flags currently unused, should be set to zero
  263. * @return 0 on success, a negative AVERROR error code on failure.
  264. */
  265. int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
  266. enum AVHWFrameTransferDirection {
  267. /**
  268. * Transfer the data from the queried hw frame.
  269. */
  270. AV_HWFRAME_TRANSFER_DIRECTION_FROM,
  271. /**
  272. * Transfer the data to the queried hw frame.
  273. */
  274. AV_HWFRAME_TRANSFER_DIRECTION_TO,
  275. };
  276. /**
  277. * Get a list of possible source or target formats usable in
  278. * av_hwframe_transfer_data().
  279. *
  280. * @param hwframe_ctx the frame context to obtain the information for
  281. * @param dir the direction of the transfer
  282. * @param formats the pointer to the output format list will be written here.
  283. * The list is terminated with AV_PIX_FMT_NONE and must be freed
  284. * by the caller when no longer needed using av_free().
  285. * If this function returns successfully, the format list will
  286. * have at least one item (not counting the terminator).
  287. * On failure, the contents of this pointer are unspecified.
  288. * @param flags currently unused, should be set to zero
  289. * @return 0 on success, a negative AVERROR code on failure.
  290. */
  291. int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
  292. enum AVHWFrameTransferDirection dir,
  293. enum AVPixelFormat **formats, int flags);
  294. #endif /* AVUTIL_HWCONTEXT_H */