FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hwcontext.h
Go to the documentation of this file.
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 
19 #ifndef AVUTIL_HWCONTEXT_H
20 #define AVUTIL_HWCONTEXT_H
21 
22 #include "buffer.h"
23 #include "frame.h"
24 #include "log.h"
25 #include "pixfmt.h"
26 
32 };
33 
35 
36 /**
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
42  * derived.
43  *
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
51  * state.
52  */
53 typedef struct AVHWDeviceContext {
54  /**
55  * A class for logging. Set by av_hwdevice_ctx_alloc().
56  */
57  const AVClass *av_class;
58 
59  /**
60  * Private data used internally by libavutil. Must not be accessed in any
61  * way by the caller.
62  */
63  AVHWDeviceInternal *internal;
64 
65  /**
66  * This field identifies the underlying API used for hardware access.
67  *
68  * This field is set when this struct is allocated and never changed
69  * afterwards.
70  */
72 
73  /**
74  * The format-specific data, allocated and freed by libavutil along with
75  * this context.
76  *
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().
80  *
81  * After calling av_hwdevice_ctx_init() this struct should not be modified
82  * by the caller.
83  */
84  void *hwctx;
85 
86  /**
87  * This field may be set by the caller before calling av_hwdevice_ctx_init().
88  *
89  * If non-NULL, this callback will be called when the last reference to
90  * this context is unreferenced, immediately before it is freed.
91  *
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.
95  */
97 
98  /**
99  * Arbitrary user data, to be used e.g. by the free() callback.
100  */
101  void *user_opaque;
103 
105 
106 /**
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.
110  *
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
114  * struct.
115  */
116 typedef struct AVHWFramesContext {
117  /**
118  * A class for logging.
119  */
121 
122  /**
123  * Private data used internally by libavutil. Must not be accessed in any
124  * way by the caller.
125  */
127 
128  /**
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.
132  */
134 
135  /**
136  * The parent AVHWDeviceContext. This is simply a pointer to
137  * device_ref->data provided for convenience.
138  *
139  * Set by libavutil in av_hwframe_ctx_init().
140  */
142 
143  /**
144  * The format-specific data, allocated and freed automatically along with
145  * this context.
146  *
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().
150  *
151  * After any frames using this context are created, the contents of this
152  * struct should not be modified by the caller.
153  */
154  void *hwctx;
155 
156  /**
157  * This field may be set by the caller before calling av_hwframe_ctx_init().
158  *
159  * If non-NULL, this callback will be called when the last reference to
160  * this context is unreferenced, immediately before it is freed.
161  */
163 
164  /**
165  * Arbitrary user data, to be used e.g. by the free() callback.
166  */
167  void *user_opaque;
168 
169  /**
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.
176  *
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.
181  */
183 
184  /**
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.
187  *
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.
190  */
192 
193  /**
194  * The pixel format identifying the underlying HW surface type.
195  *
196  * Must be a hwaccel format, i.e. the corresponding descriptor must have the
197  * AV_PIX_FMT_FLAG_HWACCEL flag set.
198  *
199  * Must be set by the user before calling av_hwframe_ctx_init().
200  */
202 
203  /**
204  * The pixel format identifying the actual data layout of the hardware
205  * frames.
206  *
207  * Must be set by the caller before calling av_hwframe_ctx_init().
208  *
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).
213  */
215 
216  /**
217  * The allocated dimensions of the frames in this pool.
218  *
219  * Must be set by the user before calling av_hwframe_ctx_init().
220  */
221  int width, height;
223 
224 /**
225  * Allocate an AVHWDeviceContext for a given pixel format.
226  *
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
230  * on failure.
231  */
233 
234 /**
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
237  * used in any way.
238  *
239  * @param ref a reference to the AVHWDeviceContext
240  * @return 0 on success, a negative AVERROR code on failure
241  */
243 
244 /**
245  * Open a device of the specified type and create an AVHWDeviceContext for it.
246  *
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().
251  *
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.
256  *
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
266  *
267  * @return 0 on success, a negative AVERROR code on failure.
268  */
270  const char *device, AVDictionary *opts, int flags);
271 
272 /**
273  * Allocate an AVHWFramesContext tied to a given device context.
274  *
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
279  * on failure.
280  */
282 
283 /**
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
286  * to any frames.
287  *
288  * @param ref a reference to the AVHWFramesContext
289  * @return 0 on success, a negative AVERROR code on failure
290  */
292 
293 /**
294  * Allocate a new frame attached to the given AVHWFramesContext.
295  *
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
301  */
302 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
303 
304 /**
305  * Copy data to or from a hw surface. At least one of dst/src must have an
306  * AVHWFramesContext attached.
307  *
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)
314  *
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.
319  *
320  * @param dst the destination frame. dst is not touched on failure.
321  * @param src the source frame.
322  * @param flags currently unused, should be set to zero
323  * @return 0 on success, a negative AVERROR error code on failure.
324  */
325 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
326 
328  /**
329  * Transfer the data from the queried hw frame.
330  */
332 
333  /**
334  * Transfer the data to the queried hw frame.
335  */
337 };
338 
339 /**
340  * Get a list of possible source or target formats usable in
341  * av_hwframe_transfer_data().
342  *
343  * @param hwframe_ctx the frame context to obtain the information for
344  * @param dir the direction of the transfer
345  * @param formats the pointer to the output format list will be written here.
346  * The list is terminated with AV_PIX_FMT_NONE and must be freed
347  * by the caller when no longer needed using av_free().
348  * If this function returns successfully, the format list will
349  * have at least one item (not counting the terminator).
350  * On failure, the contents of this pointer are unspecified.
351  * @param flags currently unused, should be set to zero
352  * @return 0 on success, a negative AVERROR code on failure.
353  */
356  enum AVPixelFormat **formats, int flags);
357 
358 
359 /**
360  * This struct describes the constraints on hardware frames attached to
361  * a given device with a hardware-specific configuration. This is returned
362  * by av_hwdevice_get_hwframe_constraints() and must be freed by
363  * av_hwframe_constraints_free() after use.
364  */
365 typedef struct AVHWFramesConstraints {
366  /**
367  * A list of possible values for format in the hw_frames_ctx,
368  * terminated by AV_PIX_FMT_NONE. This member will always be filled.
369  */
371 
372  /**
373  * A list of possible values for sw_format in the hw_frames_ctx,
374  * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
375  * not known.
376  */
378 
379  /**
380  * The minimum size of frames in this hw_frames_ctx.
381  * (Zero if not known.)
382  */
385 
386  /**
387  * The maximum size of frames in this hw_frames_ctx.
388  * (INT_MAX if not known / no limit.)
389  */
393 
394 /**
395  * Allocate a HW-specific configuration structure for a given HW device.
396  * After use, the user must free all members as required by the specific
397  * hardware structure being used, then free the structure itself with
398  * av_free().
399  *
400  * @param device_ctx a reference to the associated AVHWDeviceContext.
401  * @return The newly created HW-specific configuration structure on
402  * success or NULL on failure.
403  */
404 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
405 
406 /**
407  * Get the constraints on HW frames given a device and the HW-specific
408  * configuration to be used with that device. If no HW-specific
409  * configuration is provided, returns the maximum possible capabilities
410  * of the device.
411  *
412  * @param device_ctx a reference to the associated AVHWDeviceContext.
413  * @param hwconfig a filled HW-specific configuration structure, or NULL
414  * to return the maximum possible capabilities of the device.
415  * @return AVHWFramesConstraints structure describing the constraints
416  * on the device, or NULL if not available.
417  */
419  const void *hwconfig);
420 
421 /**
422  * Free an AVHWFrameConstraints structure.
423  *
424  * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
425  */
427 
428 #endif /* AVUTIL_HWCONTEXT_H */
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:53
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:447
static enum AVSampleFormat formats[]
Definition: avresample.c:163
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:221
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:201
void * user_opaque
Arbitrary user data, to be used e.g.
Definition: hwcontext.h:167
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:390
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ctx)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:174
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:361
AVHWFramesConstraints * av_hwdevice_get_hwframe_constraints(AVBufferRef *ref, const void *hwconfig)
Get the constraints on HW frames given a device and the HW-specific configuration to be used with tha...
Definition: hwcontext.c:422
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:84
int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats, int flags)
Get a list of possible source or target formats usable in av_hwframe_transfer_data().
Definition: hwcontext.c:307
The buffer pool.
const AVClass * av_class
A class for logging.
Definition: hwcontext.h:120
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition: hwcontext.h:162
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:96
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given pixel format.
Definition: hwcontext.c:71
int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:387
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:71
reference-counted frame API
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:191
AVDictionary * opts
Definition: movenc.c:50
Transfer the data from the queried hw frame.
Definition: hwcontext.h:331
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:141
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
AVFormatContext * ctx
Definition: movenc.c:48
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:260
#define src
Definition: vp9dsp.c:530
int min_width
The minimum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:383
Transfer the data to the queried hw frame.
Definition: hwcontext.h:336
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:365
int av_hwdevice_ctx_init(AVBufferRef *ref)
Finalize the device context before use.
Definition: hwcontext.c:129
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:154
const AVClass * av_class
A class for logging.
Definition: hwcontext.h:57
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:116
refcounted data buffer API
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:370
static int flags
Definition: cpu.c:47
void * user_opaque
Arbitrary user data, to be used e.g.
Definition: hwcontext.h:101
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:133
A reference to a data buffer.
Definition: buffer.h:81
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVHWFrameTransferDirection
Definition: hwcontext.h:327
pixel format definitions
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:182
AVHWDeviceType
Definition: hwcontext.h:27
int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:456
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:377
void * av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx)
Allocate a HW-specific configuration structure for a given HW device.
Definition: hwcontext.c:411
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:214
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60