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,
35 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
38 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
39 * i.e. state that is not tied to a concrete processing configuration.
40 * E.g., in an API that supports hardware-accelerated encoding and decoding,
41 * this struct will (if possible) wrap the state that is common to both encoding
42 * and decoding and from which specific instances of encoders or decoders can be
45 * This struct is reference-counted with the AVBuffer mechanism. The
46 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
47 * points to the actual AVHWDeviceContext. Further objects derived from
48 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
49 * specific properties) will hold an internal reference to it. After all the
50 * references are released, the AVHWDeviceContext itself will be freed,
51 * optionally invoking a user-specified callback for uninitializing the hardware
54 typedef struct AVHWDeviceContext {
56 * A class for logging. Set by av_hwdevice_ctx_alloc().
58 const AVClass *av_class;
61 * Private data used internally by libavutil. Must not be accessed in any
64 AVHWDeviceInternal *internal;
67 * This field identifies the underlying API used for hardware access.
69 * This field is set when this struct is allocated and never changed
72 enum AVHWDeviceType type;
75 * The format-specific data, allocated and freed by libavutil along with
78 * Should be cast by the user to the format-specific context defined in the
79 * corresponding header (hwcontext_*.h) and filled as described in the
80 * documentation before calling av_hwdevice_ctx_init().
82 * After calling av_hwdevice_ctx_init() this struct should not be modified
88 * This field may be set by the caller before calling av_hwdevice_ctx_init().
90 * If non-NULL, this callback will be called when the last reference to
91 * this context is unreferenced, immediately before it is freed.
93 * @note when other objects (e.g an AVHWFramesContext) are derived from this
94 * struct, this callback will be invoked after all such child objects
95 * are fully uninitialized and their respective destructors invoked.
97 void (*free)(struct AVHWDeviceContext *ctx);
100 * Arbitrary user data, to be used e.g. by the free() callback.
105 typedef struct AVHWFramesInternal AVHWFramesInternal;
108 * This struct describes a set or pool of "hardware" frames (i.e. those with
109 * data not located in normal system memory). All the frames in the pool are
110 * assumed to be allocated in the same way and interchangeable.
112 * This struct is reference-counted with the AVBuffer mechanism and tied to a
113 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
114 * yields a reference, whose data field points to the actual AVHWFramesContext
117 typedef struct AVHWFramesContext {
119 * A class for logging.
121 const AVClass *av_class;
124 * Private data used internally by libavutil. Must not be accessed in any
127 AVHWFramesInternal *internal;
130 * A reference to the parent AVHWDeviceContext. This reference is owned and
131 * managed by the enclosing AVHWFramesContext, but the caller may derive
132 * additional references from it.
134 AVBufferRef *device_ref;
137 * The parent AVHWDeviceContext. This is simply a pointer to
138 * device_ref->data provided for convenience.
140 * Set by libavutil in av_hwframe_ctx_init().
142 AVHWDeviceContext *device_ctx;
145 * The format-specific data, allocated and freed automatically along with
148 * Should be cast by the user to the format-specific context defined in the
149 * corresponding header (hwframe_*.h) and filled as described in the
150 * documentation before calling av_hwframe_ctx_init().
152 * After any frames using this context are created, the contents of this
153 * struct should not be modified by the caller.
158 * This field may be set by the caller before calling av_hwframe_ctx_init().
160 * If non-NULL, this callback will be called when the last reference to
161 * this context is unreferenced, immediately before it is freed.
163 void (*free)(struct AVHWFramesContext *ctx);
166 * Arbitrary user data, to be used e.g. by the free() callback.
171 * A pool from which the frames are allocated by av_hwframe_get_buffer().
172 * This field may be set by the caller before calling av_hwframe_ctx_init().
173 * The buffers returned by calling av_buffer_pool_get() on this pool must
174 * have the properties described in the documentation in the corresponding hw
175 * type's header (hwcontext_*.h). The pool will be freed strictly before
176 * this struct's free() callback is invoked.
178 * This field may be NULL, then libavutil will attempt to allocate a pool
179 * internally. Note that certain device types enforce pools allocated at
180 * fixed size (frame count), which cannot be extended dynamically. In such a
181 * case, initial_pool_size must be set appropriately.
186 * Initial size of the frame pool. If a device type does not support
187 * dynamically resizing the pool, then this is also the maximum pool size.
189 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
190 * set if pool is NULL and the device type does not support dynamic pools.
192 int initial_pool_size;
195 * The pixel format identifying the underlying HW surface type.
197 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
198 * AV_PIX_FMT_FLAG_HWACCEL flag set.
200 * Must be set by the user before calling av_hwframe_ctx_init().
202 enum AVPixelFormat format;
205 * The pixel format identifying the actual data layout of the hardware
208 * Must be set by the caller before calling av_hwframe_ctx_init().
210 * @note when the underlying API does not provide the exact data layout, but
211 * only the colorspace/bit depth, this field should be set to the fully
212 * planar version of that format (e.g. for 8-bit 420 YUV it should be
213 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
215 enum AVPixelFormat sw_format;
218 * The allocated dimensions of the frames in this pool.
220 * Must be set by the user before calling av_hwframe_ctx_init().
226 * Allocate an AVHWDeviceContext for a given hardware type.
228 * @param type the type of the hardware device to allocate.
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 * Open a device of the specified type and create an AVHWDeviceContext for it.
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().
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.
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
267 * @return 0 on success, a negative AVERROR code on failure.
269 int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
270 const char *device, AVDictionary *opts, int flags);
273 * Allocate an AVHWFramesContext tied to a given device context.
275 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
276 * a new reference for internal use, the one passed to the
277 * function remains owned by the caller.
278 * @return a reference to the newly created AVHWFramesContext on success or NULL
281 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
284 * Finalize the context before use. This function must be called after the
285 * context is filled with all the required information and before it is attached
288 * @param ref a reference to the AVHWFramesContext
289 * @return 0 on success, a negative AVERROR code on failure
291 int av_hwframe_ctx_init(AVBufferRef *ref);
294 * Allocate a new frame attached to the given AVHWFramesContext.
296 * @param hwframe_ctx a reference to an AVHWFramesContext
297 * @param frame an empty (freshly allocated or unreffed) frame to be filled with
298 * newly allocated buffers.
299 * @param flags currently unused, should be set to zero
300 * @return 0 on success, a negative AVERROR code on failure
302 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
305 * Copy data to or from a hw surface. At least one of dst/src must have an
306 * AVHWFramesContext attached.
308 * If src has an AVHWFramesContext attached, then the format of dst (if set)
309 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
310 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
311 * If dst has an AVHWFramesContext attached, then the format of src must use one
312 * of the formats returned by av_hwframe_transfer_get_formats(dst,
313 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
315 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
316 * data buffers will be allocated by this function using av_frame_get_buffer().
317 * If dst->format is set, then this format will be used, otherwise (when
318 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
320 * The two frames must have matching allocated dimensions (i.e. equal to
321 * AVHWFramesContext.width/height), since not all device types support
322 * transferring a sub-rectangle of the whole surface. The display dimensions
323 * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
324 * also have to be equal for both frames. When the display dimensions are
325 * smaller than the allocated dimensions, the content of the padding in the
326 * destination frame is unspecified.
328 * @param dst the destination frame. dst is not touched on failure.
329 * @param src the source frame.
330 * @param flags currently unused, should be set to zero
331 * @return 0 on success, a negative AVERROR error code on failure.
333 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
335 enum AVHWFrameTransferDirection {
337 * Transfer the data from the queried hw frame.
339 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
342 * Transfer the data to the queried hw frame.
344 AV_HWFRAME_TRANSFER_DIRECTION_TO,
348 * Get a list of possible source or target formats usable in
349 * av_hwframe_transfer_data().
351 * @param hwframe_ctx the frame context to obtain the information for
352 * @param dir the direction of the transfer
353 * @param formats the pointer to the output format list will be written here.
354 * The list is terminated with AV_PIX_FMT_NONE and must be freed
355 * by the caller when no longer needed using av_free().
356 * If this function returns successfully, the format list will
357 * have at least one item (not counting the terminator).
358 * On failure, the contents of this pointer are unspecified.
359 * @param flags currently unused, should be set to zero
360 * @return 0 on success, a negative AVERROR code on failure.
362 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
363 enum AVHWFrameTransferDirection dir,
364 enum AVPixelFormat **formats, int flags);
368 * This struct describes the constraints on hardware frames attached to
369 * a given device with a hardware-specific configuration. This is returned
370 * by av_hwdevice_get_hwframe_constraints() and must be freed by
371 * av_hwframe_constraints_free() after use.
373 typedef struct AVHWFramesConstraints {
375 * A list of possible values for format in the hw_frames_ctx,
376 * terminated by AV_PIX_FMT_NONE. This member will always be filled.
378 enum AVPixelFormat *valid_hw_formats;
381 * A list of possible values for sw_format in the hw_frames_ctx,
382 * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
385 enum AVPixelFormat *valid_sw_formats;
388 * The minimum size of frames in this hw_frames_ctx.
389 * (Zero if not known.)
395 * The maximum size of frames in this hw_frames_ctx.
396 * (INT_MAX if not known / no limit.)
400 } AVHWFramesConstraints;
403 * Allocate a HW-specific configuration structure for a given HW device.
404 * After use, the user must free all members as required by the specific
405 * hardware structure being used, then free the structure itself with
408 * @param device_ctx a reference to the associated AVHWDeviceContext.
409 * @return The newly created HW-specific configuration structure on
410 * success or NULL on failure.
412 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
415 * Get the constraints on HW frames given a device and the HW-specific
416 * configuration to be used with that device. If no HW-specific
417 * configuration is provided, returns the maximum possible capabilities
420 * @param device_ctx a reference to the associated AVHWDeviceContext.
421 * @param hwconfig a filled HW-specific configuration structure, or NULL
422 * to return the maximum possible capabilities of the device.
423 * @return AVHWFramesConstraints structure describing the constraints
424 * on the device, or NULL if not available.
426 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
427 const void *hwconfig);
430 * Free an AVHWFrameConstraints structure.
432 * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
434 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
436 #endif /* AVUTIL_HWCONTEXT_H */