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 
33 };
34 
36 
37 /**
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
43  * derived.
44  *
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
52  * state.
53  */
54 typedef struct AVHWDeviceContext {
55  /**
56  * A class for logging. Set by av_hwdevice_ctx_alloc().
57  */
58  const AVClass *av_class;
59 
60  /**
61  * Private data used internally by libavutil. Must not be accessed in any
62  * way by the caller.
63  */
64  AVHWDeviceInternal *internal;
65 
66  /**
67  * This field identifies the underlying API used for hardware access.
68  *
69  * This field is set when this struct is allocated and never changed
70  * afterwards.
71  */
73 
74  /**
75  * The format-specific data, allocated and freed by libavutil along with
76  * this context.
77  *
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().
81  *
82  * After calling av_hwdevice_ctx_init() this struct should not be modified
83  * by the caller.
84  */
85  void *hwctx;
86 
87  /**
88  * This field may be set by the caller before calling av_hwdevice_ctx_init().
89  *
90  * If non-NULL, this callback will be called when the last reference to
91  * this context is unreferenced, immediately before it is freed.
92  *
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.
96  */
98 
99  /**
100  * Arbitrary user data, to be used e.g. by the free() callback.
101  */
102  void *user_opaque;
104 
106 
107 /**
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.
111  *
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
115  * struct.
116  */
117 typedef struct AVHWFramesContext {
118  /**
119  * A class for logging.
120  */
122 
123  /**
124  * Private data used internally by libavutil. Must not be accessed in any
125  * way by the caller.
126  */
128 
129  /**
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.
133  */
135 
136  /**
137  * The parent AVHWDeviceContext. This is simply a pointer to
138  * device_ref->data provided for convenience.
139  *
140  * Set by libavutil in av_hwframe_ctx_init().
141  */
143 
144  /**
145  * The format-specific data, allocated and freed automatically along with
146  * this context.
147  *
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().
151  *
152  * After any frames using this context are created, the contents of this
153  * struct should not be modified by the caller.
154  */
155  void *hwctx;
156 
157  /**
158  * This field may be set by the caller before calling av_hwframe_ctx_init().
159  *
160  * If non-NULL, this callback will be called when the last reference to
161  * this context is unreferenced, immediately before it is freed.
162  */
164 
165  /**
166  * Arbitrary user data, to be used e.g. by the free() callback.
167  */
168  void *user_opaque;
169 
170  /**
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.
177  *
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.
182  */
184 
185  /**
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.
188  *
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.
191  */
193 
194  /**
195  * The pixel format identifying the underlying HW surface type.
196  *
197  * Must be a hwaccel format, i.e. the corresponding descriptor must have the
198  * AV_PIX_FMT_FLAG_HWACCEL flag set.
199  *
200  * Must be set by the user before calling av_hwframe_ctx_init().
201  */
203 
204  /**
205  * The pixel format identifying the actual data layout of the hardware
206  * frames.
207  *
208  * Must be set by the caller before calling av_hwframe_ctx_init().
209  *
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).
214  */
216 
217  /**
218  * The allocated dimensions of the frames in this pool.
219  *
220  * Must be set by the user before calling av_hwframe_ctx_init().
221  */
222  int width, height;
224 
225 /**
226  * Allocate an AVHWDeviceContext for a given hardware type.
227  *
228  * @param type the type of the hardware device to allocate.
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  * 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.
327  *
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.
332  */
333 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
334 
336  /**
337  * Transfer the data from the queried hw frame.
338  */
340 
341  /**
342  * Transfer the data to the queried hw frame.
343  */
345 };
346 
347 /**
348  * Get a list of possible source or target formats usable in
349  * av_hwframe_transfer_data().
350  *
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.
361  */
364  enum AVPixelFormat **formats, int flags);
365 
366 
367 /**
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.
372  */
373 typedef struct AVHWFramesConstraints {
374  /**
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.
377  */
379 
380  /**
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
383  * not known.
384  */
386 
387  /**
388  * The minimum size of frames in this hw_frames_ctx.
389  * (Zero if not known.)
390  */
393 
394  /**
395  * The maximum size of frames in this hw_frames_ctx.
396  * (INT_MAX if not known / no limit.)
397  */
401 
402 /**
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
406  * av_free().
407  *
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.
411  */
412 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
413 
414 /**
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
418  * of the device.
419  *
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.
425  */
427  const void *hwconfig);
428 
429 /**
430  * Free an AVHWFrameConstraints structure.
431  *
432  * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
433  */
435 
436 
437 /**
438  * Flags to apply to frame mappings.
439  */
440 enum {
441  /**
442  * The mapping must be readable.
443  */
445  /**
446  * The mapping must be writeable.
447  */
449  /**
450  * The mapped frame will be overwritten completely in subsequent
451  * operations, so the current frame data need not be loaded. Any values
452  * which are not overwritten are unspecified.
453  */
455  /**
456  * The mapping must be direct. That is, there must not be any copying in
457  * the map or unmap steps. Note that performance of direct mappings may
458  * be much lower than normal memory.
459  */
461 };
462 
463 /**
464  * Map a hardware frame.
465  *
466  * This has a number of different possible effects, depending on the format
467  * and origin of the src and dst frames. On input, src should be a usable
468  * frame with valid buffers and dst should be blank (typically as just created
469  * by av_frame_alloc()). src should have an associated hwframe context, and
470  * dst may optionally have a format and associated hwframe context.
471  *
472  * If src was created by mapping a frame from the hwframe context of dst,
473  * then this function undoes the mapping - dst is replaced by a reference to
474  * the frame that src was originally mapped from.
475  *
476  * If both src and dst have an associated hwframe context, then this function
477  * attempts to map the src frame from its hardware context to that of dst and
478  * then fill dst with appropriate data to be usable there. This will only be
479  * possible if the hwframe contexts and associated devices are compatible -
480  * given compatible devices, av_hwframe_ctx_create_derived() can be used to
481  * create a hwframe context for dst in which mapping should be possible.
482  *
483  * If src has a hwframe context but dst does not, then the src frame is
484  * mapped to normal memory and should thereafter be usable as a normal frame.
485  * If the format is set on dst, then the mapping will attempt to create dst
486  * with that format and fail if it is not possible. If format is unset (is
487  * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
488  * format to use is (probably the sw_format of the src hwframe context).
489  *
490  * A return value of AVERROR(ENOSYS) indicates that the mapping is not
491  * possible with the given arguments and hwframe setup, while other return
492  * values indicate that it failed somehow.
493  *
494  * @param dst Destination frame, to contain the mapping.
495  * @param src Source frame, to be mapped.
496  * @param flags Some combination of AV_HWFRAME_MAP_* flags.
497  * @return Zero on success, negative AVERROR code on failure.
498  */
499 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
500 
501 
502 /**
503  * Create and initialise an AVHWFramesContext as a mapping of another existing
504  * AVHWFramesContext on a different device.
505  *
506  * av_hwframe_ctx_init() should not be called after this.
507  *
508  * @param derived_frame_ctx On success, a reference to the newly created
509  * AVHWFramesContext.
510  * @param derived_device_ctx A reference to the device to create the new
511  * AVHWFramesContext on.
512  * @param source_frame_ctx A reference to an existing AVHWFramesContext
513  * which will be mapped to the derived context.
514  * @param flags Currently unused; should be set to zero.
515  * @return Zero on success, negative AVERROR code on failure.
516  */
517 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
518  enum AVPixelFormat format,
519  AVBufferRef *derived_device_ctx,
520  AVBufferRef *source_frame_ctx,
521  int flags);
522 
523 #endif /* AVUTIL_HWCONTEXT_H */
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
The mapped frame will be overwritten completely in subsequent operations, so the current frame data n...
Definition: hwcontext.h:454
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:493
The mapping must be writeable.
Definition: hwcontext.h:448
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:222
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:202
void * user_opaque
Arbitrary user data, to be used e.g.
Definition: hwcontext.h:168
#define src
Definition: vp8dsp.c:254
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:398
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ctx)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:182
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:378
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:468
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
static int flags
Definition: log.c:57
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:320
The buffer pool.
const AVClass * av_class
A class for logging.
Definition: hwcontext.h:121
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition: hwcontext.h:163
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:97
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given hardware type.
Definition: hwcontext.c:74
int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:404
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:72
int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, enum AVPixelFormat format, AVBufferRef *derived_device_ctx, AVBufferRef *source_frame_ctx, int flags)
Create and initialise an AVHWFramesContext as a mapping of another existing AVHWFramesContext on a di...
Definition: hwcontext.c:663
reference-counted frame API
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:192
AVDictionary * opts
Definition: movenc.c:50
Transfer the data from the queried hw frame.
Definition: hwcontext.h:339
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:142
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:268
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition: hwcontext.c:605
int min_width
The minimum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:391
Transfer the data to the queried hw frame.
Definition: hwcontext.h:344
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:373
int av_hwdevice_ctx_init(AVBufferRef *ref)
Finalize the device context before use.
Definition: hwcontext.c:132
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:155
const AVClass * av_class
A class for logging.
Definition: hwcontext.h:58
GLint GLenum type
Definition: opengl_enc.c:105
static const char * format
Definition: movenc.c:47
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:117
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:378
The mapping must be readable.
Definition: hwcontext.h:444
void * user_opaque
Arbitrary user data, to be used e.g.
Definition: hwcontext.h:102
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:134
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:335
pixel format definitions
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:183
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:502
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:385
The mapping must be direct.
Definition: hwcontext.h:460
formats
Definition: signature.h:48
void * av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx)
Allocate a HW-specific configuration structure for a given HW device.
Definition: hwcontext.c:457
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:215
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60