2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVUTIL_HWCONTEXT_H
20 #define AVUTIL_HWCONTEXT_H
28 AV_HWDEVICE_TYPE_VDPAU,
29 AV_HWDEVICE_TYPE_CUDA,
30 AV_HWDEVICE_TYPE_VAAPI,
31 AV_HWDEVICE_TYPE_DXVA2,
34 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
37 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
38 * i.e. state that is not tied to a concrete processing configuration.
39 * E.g., in an API that supports hardware-accelerated encoding and decoding,
40 * this struct will (if possible) wrap the state that is common to both encoding
41 * and decoding and from which specific instances of encoders or decoders can be
44 * This struct is reference-counted with the AVBuffer mechanism. The
45 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
46 * points to the actual AVHWDeviceContext. Further objects derived from
47 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
48 * specific properties) will hold an internal reference to it. After all the
49 * references are released, the AVHWDeviceContext itself will be freed,
50 * optionally invoking a user-specified callback for uninitializing the hardware
53 typedef struct AVHWDeviceContext {
55 * A class for logging. Set by av_hwdevice_ctx_alloc().
57 const AVClass *av_class;
60 * Private data used internally by libavutil. Must not be accessed in any
63 AVHWDeviceInternal *internal;
66 * This field identifies the underlying API used for hardware access.
68 * This field is set when this struct is allocated and never changed
71 enum AVHWDeviceType type;
74 * The format-specific data, allocated and freed by libavutil along with
77 * Should be cast by the user to the format-specific context defined in the
78 * corresponding header (hwcontext_*.h) and filled as described in the
79 * documentation before calling av_hwdevice_ctx_init().
81 * After calling av_hwdevice_ctx_init() this struct should not be modified
87 * This field may be set by the caller before calling av_hwdevice_ctx_init().
89 * If non-NULL, this callback will be called when the last reference to
90 * this context is unreferenced, immediately before it is freed.
92 * @note when other objects (e.g an AVHWFramesContext) are derived from this
93 * struct, this callback will be invoked after all such child objects
94 * are fully uninitialized and their respective destructors invoked.
96 void (*free)(struct AVHWDeviceContext *ctx);
99 * Arbitrary user data, to be used e.g. by the free() callback.
104 typedef struct AVHWFramesInternal AVHWFramesInternal;
107 * This struct describes a set or pool of "hardware" frames (i.e. those with
108 * data not located in normal system memory). All the frames in the pool are
109 * assumed to be allocated in the same way and interchangeable.
111 * This struct is reference-counted with the AVBuffer mechanism and tied to a
112 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
113 * yields a reference, whose data field points to the actual AVHWFramesContext
116 typedef struct AVHWFramesContext {
118 * A class for logging.
120 const AVClass *av_class;
123 * Private data used internally by libavutil. Must not be accessed in any
126 AVHWFramesInternal *internal;
129 * A reference to the parent AVHWDeviceContext. This reference is owned and
130 * managed by the enclosing AVHWFramesContext, but the caller may derive
131 * additional references from it.
133 AVBufferRef *device_ref;
136 * The parent AVHWDeviceContext. This is simply a pointer to
137 * device_ref->data provided for convenience.
139 * Set by libavutil in av_hwframe_ctx_init().
141 AVHWDeviceContext *device_ctx;
144 * The format-specific data, allocated and freed automatically along with
147 * Should be cast by the user to the format-specific context defined in the
148 * corresponding header (hwframe_*.h) and filled as described in the
149 * documentation before calling av_hwframe_ctx_init().
151 * After any frames using this context are created, the contents of this
152 * struct should not be modified by the caller.
157 * This field may be set by the caller before calling av_hwframe_ctx_init().
159 * If non-NULL, this callback will be called when the last reference to
160 * this context is unreferenced, immediately before it is freed.
162 void (*free)(struct AVHWFramesContext *ctx);
165 * Arbitrary user data, to be used e.g. by the free() callback.
170 * A pool from which the frames are allocated by av_hwframe_get_buffer().
171 * This field may be set by the caller before calling av_hwframe_ctx_init().
172 * The buffers returned by calling av_buffer_pool_get() on this pool must
173 * have the properties described in the documentation in the corresponding hw
174 * type's header (hwcontext_*.h). The pool will be freed strictly before
175 * this struct's free() callback is invoked.
177 * This field may be NULL, then libavutil will attempt to allocate a pool
178 * internally. Note that certain device types enforce pools allocated at
179 * fixed size (frame count), which cannot be extended dynamically. In such a
180 * case, initial_pool_size must be set appropriately.
185 * Initial size of the frame pool. If a device type does not support
186 * dynamically resizing the pool, then this is also the maximum pool size.
188 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
189 * set if pool is NULL and the device type does not support dynamic pools.
191 int initial_pool_size;
194 * The pixel format identifying the underlying HW surface type.
196 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
197 * AV_PIX_FMT_FLAG_HWACCEL flag set.
199 * Must be set by the user before calling av_hwframe_ctx_init().
201 enum AVPixelFormat format;
204 * The pixel format identifying the actual data layout of the hardware
207 * Must be set by the caller before calling av_hwframe_ctx_init().
209 * @note when the underlying API does not provide the exact data layout, but
210 * only the colorspace/bit depth, this field should be set to the fully
211 * planar version of that format (e.g. for 8-bit 420 YUV it should be
212 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
214 enum AVPixelFormat sw_format;
217 * The allocated dimensions of the frames in this pool.
219 * Must be set by the user before calling av_hwframe_ctx_init().
225 * Allocate an AVHWDeviceContext for a given pixel format.
227 * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
228 * on the corresponding format descriptor)
229 * @return a reference to the newly created AVHWDeviceContext on success or NULL
232 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
235 * Finalize the device context before use. This function must be called after
236 * the context is filled with all the required information and before it is
239 * @param ref a reference to the AVHWDeviceContext
240 * @return 0 on success, a negative AVERROR code on failure
242 int av_hwdevice_ctx_init(AVBufferRef *ref);
245 * Allocate an AVHWFramesContext tied to a given device context.
247 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
248 * a new reference for internal use, the one passed to the
249 * function remains owned by the caller.
250 * @return a reference to the newly created AVHWFramesContext on success or NULL
253 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
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
260 * @param ref a reference to the AVHWFramesContext
261 * @return 0 on success, a negative AVERROR code on failure
263 int av_hwframe_ctx_init(AVBufferRef *ref);
266 * Allocate a new frame attached to the given AVHWFramesContext.
268 * @param hwframe_ctx a reference to an AVHWFramesContext
269 * @param frame an empty (freshly allocated or unreffed) frame to be filled with
270 * newly allocated buffers.
271 * @param flags currently unused, should be set to zero
272 * @return 0 on success, a negative AVERROR code on failure
274 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
277 * Copy data to or from a hw surface. At least one of dst/src must have an
278 * AVHWFramesContext attached.
280 * If src has an AVHWFramesContext attached, then the format of dst (if set)
281 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
282 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
283 * If dst has an AVHWFramesContext attached, then the format of src must use one
284 * of the formats returned by av_hwframe_transfer_get_formats(dst,
285 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
287 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
288 * data buffers will be allocated by this function using av_frame_get_buffer().
289 * If dst->format is set, then this format will be used, otherwise (when
290 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
292 * @param dst the destination frame. dst is not touched on failure.
293 * @param src the source frame.
294 * @param flags currently unused, should be set to zero
295 * @return 0 on success, a negative AVERROR error code on failure.
297 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
299 enum AVHWFrameTransferDirection {
301 * Transfer the data from the queried hw frame.
303 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
306 * Transfer the data to the queried hw frame.
308 AV_HWFRAME_TRANSFER_DIRECTION_TO,
312 * Get a list of possible source or target formats usable in
313 * av_hwframe_transfer_data().
315 * @param hwframe_ctx the frame context to obtain the information for
316 * @param dir the direction of the transfer
317 * @param formats the pointer to the output format list will be written here.
318 * The list is terminated with AV_PIX_FMT_NONE and must be freed
319 * by the caller when no longer needed using av_free().
320 * If this function returns successfully, the format list will
321 * have at least one item (not counting the terminator).
322 * On failure, the contents of this pointer are unspecified.
323 * @param flags currently unused, should be set to zero
324 * @return 0 on success, a negative AVERROR code on failure.
326 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
327 enum AVHWFrameTransferDirection dir,
328 enum AVPixelFormat **formats, int flags);
332 * This struct describes the constraints on hardware frames attached to
333 * a given device with a hardware-specific configuration. This is returned
334 * by av_hwdevice_get_hwframe_constraints() and must be freed by
335 * av_hwframe_constraints_free() after use.
337 typedef struct AVHWFramesConstraints {
339 * A list of possible values for format in the hw_frames_ctx,
340 * terminated by AV_PIX_FMT_NONE. This member will always be filled.
342 enum AVPixelFormat *valid_hw_formats;
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
349 enum AVPixelFormat *valid_sw_formats;
352 * The minimum size of frames in this hw_frames_ctx.
353 * (Zero if not known.)
359 * The maximum size of frames in this hw_frames_ctx.
360 * (INT_MAX if not known / no limit.)
364 } AVHWFramesConstraints;
367 * Allocate a HW-specific configuration structure for a given HW device.
368 * After use, the user must free all members as required by the specific
369 * hardware structure being used, then free the structure itself with
372 * @param device_ctx a reference to the associated AVHWDeviceContext.
373 * @return The newly created HW-specific configuration structure on
374 * success or NULL on failure.
376 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
379 * Get the constraints on HW frames given a device and the HW-specific
380 * configuration to be used with that device. If no HW-specific
381 * configuration is provided, returns the maximum possible capabilities
384 * @param device_ctx a reference to the associated AVHWDeviceContext.
385 * @param hwconfig a filled HW-specific configuration structure, or NULL
386 * to return the maximum possible capabilities of the device.
387 * @return AVHWFramesConstraints structure describing the constraints
388 * on the device, or NULL if not available.
390 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
391 const void *hwconfig);
394 * Free an AVHWFrameConstraints structure.
396 * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
398 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
400 #endif /* AVUTIL_HWCONTEXT_H */