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,
33 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
36 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
37 * i.e. state that is not tied to a concrete processing configuration.
38 * E.g., in an API that supports hardware-accelerated encoding and decoding,
39 * this struct will (if possible) wrap the state that is common to both encoding
40 * and decoding and from which specific instances of encoders or decoders can be
43 * This struct is reference-counted with the AVBuffer mechanism. The
44 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
45 * points to the actual AVHWDeviceContext. Further objects derived from
46 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
47 * specific properties) will hold an internal reference to it. After all the
48 * references are released, the AVHWDeviceContext itself will be freed,
49 * optionally invoking a user-specified callback for uninitializing the hardware
52 typedef struct AVHWDeviceContext {
54 * A class for logging. Set by av_hwdevice_ctx_alloc().
56 const AVClass *av_class;
59 * Private data used internally by libavutil. Must not be accessed in any
62 AVHWDeviceInternal *internal;
65 * This field identifies the underlying API used for hardware access.
67 * This field is set when this struct is allocated and never changed
70 enum AVHWDeviceType type;
73 * The format-specific data, allocated and freed by libavutil along with
76 * Should be cast by the user to the format-specific context defined in the
77 * corresponding header (hwcontext_*.h) and filled as described in the
78 * documentation before calling av_hwdevice_ctx_init().
80 * After calling av_hwdevice_ctx_init() this struct should not be modified
86 * This field may be set by the caller before calling av_hwdevice_ctx_init().
88 * If non-NULL, this callback will be called when the last reference to
89 * this context is unreferenced, immediately before it is freed.
91 * @note when other objects (e.g an AVHWFramesContext) are derived from this
92 * struct, this callback will be invoked after all such child objects
93 * are fully uninitialized and their respective destructors invoked.
95 void (*free)(struct AVHWDeviceContext *ctx);
98 * Arbitrary user data, to be used e.g. by the free() callback.
103 typedef struct AVHWFramesInternal AVHWFramesInternal;
106 * This struct describes a set or pool of "hardware" frames (i.e. those with
107 * data not located in normal system memory). All the frames in the pool are
108 * assumed to be allocated in the same way and interchangeable.
110 * This struct is reference-counted with the AVBuffer mechanism and tied to a
111 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
112 * yields a reference, whose data field points to the actual AVHWFramesContext
115 typedef struct AVHWFramesContext {
117 * A class for logging.
119 const AVClass *av_class;
122 * Private data used internally by libavutil. Must not be accessed in any
125 AVHWFramesInternal *internal;
128 * A reference to the parent AVHWDeviceContext. This reference is owned and
129 * managed by the enclosing AVHWFramesContext, but the caller may derive
130 * additional references from it.
132 AVBufferRef *device_ref;
135 * The parent AVHWDeviceContext. This is simply a pointer to
136 * device_ref->data provided for convenience.
138 * Set by libavutil in av_hwframe_ctx_init().
140 AVHWDeviceContext *device_ctx;
143 * The format-specific data, allocated and freed automatically along with
146 * Should be cast by the user to the format-specific context defined in the
147 * corresponding header (hwframe_*.h) and filled as described in the
148 * documentation before calling av_hwframe_ctx_init().
150 * After any frames using this context are created, the contents of this
151 * struct should not be modified by the caller.
156 * This field may be set by the caller before calling av_hwframe_ctx_init().
158 * If non-NULL, this callback will be called when the last reference to
159 * this context is unreferenced, immediately before it is freed.
161 void (*free)(struct AVHWFramesContext *ctx);
164 * Arbitrary user data, to be used e.g. by the free() callback.
169 * A pool from which the frames are allocated by av_hwframe_get_buffer().
170 * This field may be set by the caller before calling av_hwframe_ctx_init().
171 * The buffers returned by calling av_buffer_pool_get() on this pool must
172 * have the properties described in the documentation in the correponding hw
173 * type's header (hwcontext_*.h). The pool will be freed strictly before
174 * this struct's free() callback is invoked.
176 * This field may be NULL, then libavutil will attempt to allocate a pool
177 * internally. Note that certain device types enforce pools allocated at
178 * fixed size (frame count), which cannot be extended dynamically. In such a
179 * case, initial_pool_size must be set appropriately.
184 * Initial size of the frame pool. If a device type does not support
185 * dynamically resizing the pool, then this is also the maximum pool size.
187 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
188 * set if pool is NULL and the device type does not support dynamic pools.
190 int initial_pool_size;
193 * The pixel format identifying the underlying HW surface type.
195 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
196 * AV_PIX_FMT_FLAG_HWACCEL flag set.
198 * Must be set by the user before calling av_hwframe_ctx_init().
200 enum AVPixelFormat format;
203 * The pixel format identifying the actual data layout of the hardware
206 * Must be set by the caller before calling av_hwframe_ctx_init().
208 * @note when the underlying API does not provide the exact data layout, but
209 * only the colorspace/bit depth, this field should be set to the fully
210 * planar version of that format (e.g. for 8-bit 420 YUV it should be
211 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
213 enum AVPixelFormat sw_format;
216 * The allocated dimensions of the frames in this pool.
218 * Must be set by the user before calling av_hwframe_ctx_init().
224 * Allocate an AVHWDeviceContext for a given pixel format.
226 * @param format a hwaccel pixel format (AV_PIX_FMT_FLAG_HWACCEL must be set
227 * on the corresponding format descriptor)
228 * @return a reference to the newly created AVHWDeviceContext on success or NULL
231 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
234 * Finalize the device context before use. This function must be called after
235 * the context is filled with all the required information and before it is
238 * @param ref a reference to the AVHWDeviceContext
239 * @return 0 on success, a negative AVERROR code on failure
241 int av_hwdevice_ctx_init(AVBufferRef *ref);
244 * Allocate an AVHWFramesContext tied to a given device context.
246 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
247 * a new reference for internal use, the one passed to the
248 * function remains owned by the caller.
249 * @return a reference to the newly created AVHWFramesContext on success or NULL
252 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
255 * Finalize the context before use. This function must be called after the
256 * context is filled with all the required information and before it is attached
259 * @param ref a reference to the AVHWFramesContext
260 * @return 0 on success, a negative AVERROR code on failure
262 int av_hwframe_ctx_init(AVBufferRef *ref);
265 * Allocate a new frame attached to the given AVHWFramesContext.
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
273 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
276 * Copy data to or from a hw surface. At least one of dst/src must have an
277 * AVHWFramesContext attached.
279 * If src has an AVHWFramesContext attached, then the format of dst (if set)
280 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
281 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
282 * If dst has an AVHWFramesContext attached, then the format of src must use one
283 * of the formats returned by av_hwframe_transfer_get_formats(dst,
284 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
286 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
287 * data buffers will be allocated by this function using av_frame_get_buffer().
288 * If dst->format is set, then this format will be used, otherwise (when
289 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
291 * @param dst the destination frame. dst is not touched on failure.
292 * @param src the source frame.
293 * @param flags currently unused, should be set to zero
294 * @return 0 on success, a negative AVERROR error code on failure.
296 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
298 enum AVHWFrameTransferDirection {
300 * Transfer the data from the queried hw frame.
302 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
305 * Transfer the data to the queried hw frame.
307 AV_HWFRAME_TRANSFER_DIRECTION_TO,
311 * Get a list of possible source or target formats usable in
312 * av_hwframe_transfer_data().
314 * @param hwframe_ctx the frame context to obtain the information for
315 * @param dir the direction of the transfer
316 * @param formats the pointer to the output format list will be written here.
317 * The list is terminated with AV_PIX_FMT_NONE and must be freed
318 * by the caller when no longer needed using av_free().
319 * If this function returns successfully, the format list will
320 * have at least one item (not counting the terminator).
321 * On failure, the contents of this pointer are unspecified.
322 * @param flags currently unused, should be set to zero
323 * @return 0 on success, a negative AVERROR code on failure.
325 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
326 enum AVHWFrameTransferDirection dir,
327 enum AVPixelFormat **formats, int flags);
331 * This struct describes the constraints on hardware frames attached to
332 * a given device with a hardware-specific configuration. This is returned
333 * by av_hwdevice_get_hwframe_constraints() and must be freed by
334 * av_hwframe_constraints_free() after use.
336 typedef struct AVHWFramesConstraints {
338 * A list of possible values for format in the hw_frames_ctx,
339 * terminated by AV_PIX_FMT_NONE. This member will always be filled.
341 enum AVPixelFormat *valid_hw_formats;
344 * A list of possible values for sw_format in the hw_frames_ctx,
345 * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
348 enum AVPixelFormat *valid_sw_formats;
351 * The minimum size of frames in this hw_frames_ctx.
352 * (Zero if not known.)
358 * The maximum size of frames in this hw_frames_ctx.
359 * (INT_MAX if not known / no limit.)
363 } AVHWFramesConstraints;
366 * Allocate a HW-specific configuration structure for a given HW device.
367 * After use, the user must free all members as required by the specific
368 * hardware structure being used, then free the structure itself with
371 * @param device_ctx a reference to the associated AVHWDeviceContext.
372 * @return The newly created HW-specific configuration structure on
373 * success or NULL on failure.
375 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
378 * Get the constraints on HW frames given a device and the HW-specific
379 * configuration to be used with that device. If no HW-specific
380 * confgiuration is provided, returns the maximum possible capabilities
383 * @param device_ctx a reference to the associated AVHWDeviceContext.
384 * @param hwconfig a filled HW-specific configuration structure, or NULL
385 * to return the maximum possible capabilities of the device.
386 * @return AVHWFramesConstraints structure describing the constraints
387 * on the device, or NULL if not available.
389 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
390 const void *hwconfig);
393 * Free an AVHWFrameConstraints structure.
395 * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
397 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
399 #endif /* AVUTIL_HWCONTEXT_H */