]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext.h
hwcontext: Fix documentation for av_hwdevice_ctx_alloc()
[ffmpeg] / libavutil / hwcontext.h
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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
27 enum AVHWDeviceType {
28     AV_HWDEVICE_TYPE_NONE,
29     AV_HWDEVICE_TYPE_VDPAU,
30     AV_HWDEVICE_TYPE_CUDA,
31     AV_HWDEVICE_TYPE_VAAPI,
32     AV_HWDEVICE_TYPE_DXVA2,
33     AV_HWDEVICE_TYPE_QSV,
34     AV_HWDEVICE_TYPE_D3D11VA,
35 };
36
37 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
38
39 /**
40  * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
41  * i.e. state that is not tied to a concrete processing configuration.
42  * E.g., in an API that supports hardware-accelerated encoding and decoding,
43  * this struct will (if possible) wrap the state that is common to both encoding
44  * and decoding and from which specific instances of encoders or decoders can be
45  * derived.
46  *
47  * This struct is reference-counted with the AVBuffer mechanism. The
48  * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
49  * points to the actual AVHWDeviceContext. Further objects derived from
50  * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
51  * specific properties) will hold an internal reference to it. After all the
52  * references are released, the AVHWDeviceContext itself will be freed,
53  * optionally invoking a user-specified callback for uninitializing the hardware
54  * state.
55  */
56 typedef struct AVHWDeviceContext {
57     /**
58      * A class for logging. Set by av_hwdevice_ctx_alloc().
59      */
60     const AVClass *av_class;
61
62     /**
63      * Private data used internally by libavutil. Must not be accessed in any
64      * way by the caller.
65      */
66     AVHWDeviceInternal *internal;
67
68     /**
69      * This field identifies the underlying API used for hardware access.
70      *
71      * This field is set when this struct is allocated and never changed
72      * afterwards.
73      */
74     enum AVHWDeviceType type;
75
76     /**
77      * The format-specific data, allocated and freed by libavutil along with
78      * this context.
79      *
80      * Should be cast by the user to the format-specific context defined in the
81      * corresponding header (hwcontext_*.h) and filled as described in the
82      * documentation before calling av_hwdevice_ctx_init().
83      *
84      * After calling av_hwdevice_ctx_init() this struct should not be modified
85      * by the caller.
86      */
87     void *hwctx;
88
89     /**
90      * This field may be set by the caller before calling av_hwdevice_ctx_init().
91      *
92      * If non-NULL, this callback will be called when the last reference to
93      * this context is unreferenced, immediately before it is freed.
94      *
95      * @note when other objects (e.g an AVHWFramesContext) are derived from this
96      *       struct, this callback will be invoked after all such child objects
97      *       are fully uninitialized and their respective destructors invoked.
98      */
99     void (*free)(struct AVHWDeviceContext *ctx);
100
101     /**
102      * Arbitrary user data, to be used e.g. by the free() callback.
103      */
104     void *user_opaque;
105 } AVHWDeviceContext;
106
107 typedef struct AVHWFramesInternal AVHWFramesInternal;
108
109 /**
110  * This struct describes a set or pool of "hardware" frames (i.e. those with
111  * data not located in normal system memory). All the frames in the pool are
112  * assumed to be allocated in the same way and interchangeable.
113  *
114  * This struct is reference-counted with the AVBuffer mechanism and tied to a
115  * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
116  * yields a reference, whose data field points to the actual AVHWFramesContext
117  * struct.
118  */
119 typedef struct AVHWFramesContext {
120     /**
121      * A class for logging.
122      */
123     const AVClass *av_class;
124
125     /**
126      * Private data used internally by libavutil. Must not be accessed in any
127      * way by the caller.
128      */
129     AVHWFramesInternal *internal;
130
131     /**
132      * A reference to the parent AVHWDeviceContext. This reference is owned and
133      * managed by the enclosing AVHWFramesContext, but the caller may derive
134      * additional references from it.
135      */
136     AVBufferRef *device_ref;
137
138     /**
139      * The parent AVHWDeviceContext. This is simply a pointer to
140      * device_ref->data provided for convenience.
141      *
142      * Set by libavutil in av_hwframe_ctx_init().
143      */
144     AVHWDeviceContext *device_ctx;
145
146     /**
147      * The format-specific data, allocated and freed automatically along with
148      * this context.
149      *
150      * Should be cast by the user to the format-specific context defined in the
151      * corresponding header (hwframe_*.h) and filled as described in the
152      * documentation before calling av_hwframe_ctx_init().
153      *
154      * After any frames using this context are created, the contents of this
155      * struct should not be modified by the caller.
156      */
157     void *hwctx;
158
159     /**
160      * This field may be set by the caller before calling av_hwframe_ctx_init().
161      *
162      * If non-NULL, this callback will be called when the last reference to
163      * this context is unreferenced, immediately before it is freed.
164      */
165     void (*free)(struct AVHWFramesContext *ctx);
166
167     /**
168      * Arbitrary user data, to be used e.g. by the free() callback.
169      */
170     void *user_opaque;
171
172     /**
173      * A pool from which the frames are allocated by av_hwframe_get_buffer().
174      * This field may be set by the caller before calling av_hwframe_ctx_init().
175      * The buffers returned by calling av_buffer_pool_get() on this pool must
176      * have the properties described in the documentation in the corresponding hw
177      * type's header (hwcontext_*.h). The pool will be freed strictly before
178      * this struct's free() callback is invoked.
179      *
180      * This field may be NULL, then libavutil will attempt to allocate a pool
181      * internally. Note that certain device types enforce pools allocated at
182      * fixed size (frame count), which cannot be extended dynamically. In such a
183      * case, initial_pool_size must be set appropriately.
184      */
185     AVBufferPool *pool;
186
187     /**
188      * Initial size of the frame pool. If a device type does not support
189      * dynamically resizing the pool, then this is also the maximum pool size.
190      *
191      * May be set by the caller before calling av_hwframe_ctx_init(). Must be
192      * set if pool is NULL and the device type does not support dynamic pools.
193      */
194     int initial_pool_size;
195
196     /**
197      * The pixel format identifying the underlying HW surface type.
198      *
199      * Must be a hwaccel format, i.e. the corresponding descriptor must have the
200      * AV_PIX_FMT_FLAG_HWACCEL flag set.
201      *
202      * Must be set by the user before calling av_hwframe_ctx_init().
203      */
204     enum AVPixelFormat format;
205
206     /**
207      * The pixel format identifying the actual data layout of the hardware
208      * frames.
209      *
210      * Must be set by the caller before calling av_hwframe_ctx_init().
211      *
212      * @note when the underlying API does not provide the exact data layout, but
213      * only the colorspace/bit depth, this field should be set to the fully
214      * planar version of that format (e.g. for 8-bit 420 YUV it should be
215      * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
216      */
217     enum AVPixelFormat sw_format;
218
219     /**
220      * The allocated dimensions of the frames in this pool.
221      *
222      * Must be set by the user before calling av_hwframe_ctx_init().
223      */
224     int width, height;
225 } AVHWFramesContext;
226
227 /**
228  * Look up an AVHWDeviceType by name.
229  *
230  * @param name String name of the device type (case-insensitive).
231  * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
232  *         not found.
233  */
234 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
235
236 /** Get the string name of an AVHWDeviceType.
237  *
238  * @param type Type from enum AVHWDeviceType.
239  * @return Pointer to a static string containing the name, or NULL if the type
240  *         is not valid.
241  */
242 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
243
244 /**
245  * Iterate over supported device types.
246  *
247  * @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type
248  *             returned by this function in subsequent iterations.
249  * @return The next usable device type from enum AVHWDeviceType, or
250  *         AV_HWDEVICE_TYPE_NONE if there are no more.
251  */
252 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
253
254 /**
255  * Allocate an AVHWDeviceContext for a given hardware type.
256  *
257  * @param type the type of the hardware device to allocate.
258  * @return a reference to the newly created AVHWDeviceContext on success or NULL
259  *         on failure.
260  */
261 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
262
263 /**
264  * Finalize the device context before use. This function must be called after
265  * the context is filled with all the required information and before it is
266  * used in any way.
267  *
268  * @param ref a reference to the AVHWDeviceContext
269  * @return 0 on success, a negative AVERROR code on failure
270  */
271 int av_hwdevice_ctx_init(AVBufferRef *ref);
272
273 /**
274  * Open a device of the specified type and create an AVHWDeviceContext for it.
275  *
276  * This is a convenience function intended to cover the simple cases. Callers
277  * who need to fine-tune device creation/management should open the device
278  * manually and then wrap it in an AVHWDeviceContext using
279  * av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().
280  *
281  * The returned context is already initialized and ready for use, the caller
282  * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of
283  * the created AVHWDeviceContext are set by this function and should not be
284  * touched by the caller.
285  *
286  * @param device_ctx On success, a reference to the newly-created device context
287  *                   will be written here. The reference is owned by the caller
288  *                   and must be released with av_buffer_unref() when no longer
289  *                   needed. On failure, NULL will be written to this pointer.
290  * @param type The type of the device to create.
291  * @param device A type-specific string identifying the device to open.
292  * @param opts A dictionary of additional (type-specific) options to use in
293  *             opening the device. The dictionary remains owned by the caller.
294  * @param flags currently unused
295  *
296  * @return 0 on success, a negative AVERROR code on failure.
297  */
298 int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
299                            const char *device, AVDictionary *opts, int flags);
300
301 /**
302  * Create a new device of the specified type from an existing device.
303  *
304  * If the source device is a device of the target type or was originally
305  * derived from such a device (possibly through one or more intermediate
306  * devices of other types), then this will return a reference to the
307  * existing device of the same type as is requested.
308  *
309  * Otherwise, it will attempt to derive a new device from the given source
310  * device.  If direct derivation to the new type is not implemented, it will
311  * attempt the same derivation from each ancestor of the source device in
312  * turn looking for an implemented derivation method.
313  *
314  * @param dst_ctx On success, a reference to the newly-created
315  *                AVHWDeviceContext.
316  * @param type    The type of the new device to create.
317  * @param src_ctx A reference to an existing AVHWDeviceContext which will be
318  *                used to create the new device.
319  * @param flags   Currently unused; should be set to zero.
320  * @return        Zero on success, a negative AVERROR code on failure.
321  */
322 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
323                                    enum AVHWDeviceType type,
324                                    AVBufferRef *src_ctx, int flags);
325
326
327 /**
328  * Allocate an AVHWFramesContext tied to a given device context.
329  *
330  * @param device_ctx a reference to a AVHWDeviceContext. This function will make
331  *                   a new reference for internal use, the one passed to the
332  *                   function remains owned by the caller.
333  * @return a reference to the newly created AVHWFramesContext on success or NULL
334  *         on failure.
335  */
336 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
337
338 /**
339  * Finalize the context before use. This function must be called after the
340  * context is filled with all the required information and before it is attached
341  * to any frames.
342  *
343  * @param ref a reference to the AVHWFramesContext
344  * @return 0 on success, a negative AVERROR code on failure
345  */
346 int av_hwframe_ctx_init(AVBufferRef *ref);
347
348 /**
349  * Allocate a new frame attached to the given AVHWFramesContext.
350  *
351  * @param hwframe_ctx a reference to an AVHWFramesContext
352  * @param frame an empty (freshly allocated or unreffed) frame to be filled with
353  *              newly allocated buffers.
354  * @param flags currently unused, should be set to zero
355  * @return 0 on success, a negative AVERROR code on failure
356  */
357 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
358
359 /**
360  * Copy data to or from a hw surface. At least one of dst/src must have an
361  * AVHWFramesContext attached.
362  *
363  * If src has an AVHWFramesContext attached, then the format of dst (if set)
364  * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
365  * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
366  * If dst has an AVHWFramesContext attached, then the format of src must use one
367  * of the formats returned by av_hwframe_transfer_get_formats(dst,
368  * AV_HWFRAME_TRANSFER_DIRECTION_TO)
369  *
370  * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
371  * data buffers will be allocated by this function using av_frame_get_buffer().
372  * If dst->format is set, then this format will be used, otherwise (when
373  * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
374  *
375  * The two frames must have matching allocated dimensions (i.e. equal to
376  * AVHWFramesContext.width/height), since not all device types support
377  * transferring a sub-rectangle of the whole surface. The display dimensions
378  * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
379  * also have to be equal for both frames. When the display dimensions are
380  * smaller than the allocated dimensions, the content of the padding in the
381  * destination frame is unspecified.
382  *
383  * @param dst the destination frame. dst is not touched on failure.
384  * @param src the source frame.
385  * @param flags currently unused, should be set to zero
386  * @return 0 on success, a negative AVERROR error code on failure.
387  */
388 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
389
390 enum AVHWFrameTransferDirection {
391     /**
392      * Transfer the data from the queried hw frame.
393      */
394     AV_HWFRAME_TRANSFER_DIRECTION_FROM,
395
396     /**
397      * Transfer the data to the queried hw frame.
398      */
399     AV_HWFRAME_TRANSFER_DIRECTION_TO,
400 };
401
402 /**
403  * Get a list of possible source or target formats usable in
404  * av_hwframe_transfer_data().
405  *
406  * @param hwframe_ctx the frame context to obtain the information for
407  * @param dir the direction of the transfer
408  * @param formats the pointer to the output format list will be written here.
409  *                The list is terminated with AV_PIX_FMT_NONE and must be freed
410  *                by the caller when no longer needed using av_free().
411  *                If this function returns successfully, the format list will
412  *                have at least one item (not counting the terminator).
413  *                On failure, the contents of this pointer are unspecified.
414  * @param flags currently unused, should be set to zero
415  * @return 0 on success, a negative AVERROR code on failure.
416  */
417 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
418                                     enum AVHWFrameTransferDirection dir,
419                                     enum AVPixelFormat **formats, int flags);
420
421
422 /**
423  * This struct describes the constraints on hardware frames attached to
424  * a given device with a hardware-specific configuration.  This is returned
425  * by av_hwdevice_get_hwframe_constraints() and must be freed by
426  * av_hwframe_constraints_free() after use.
427  */
428 typedef struct AVHWFramesConstraints {
429     /**
430      * A list of possible values for format in the hw_frames_ctx,
431      * terminated by AV_PIX_FMT_NONE.  This member will always be filled.
432      */
433     enum AVPixelFormat *valid_hw_formats;
434
435     /**
436      * A list of possible values for sw_format in the hw_frames_ctx,
437      * terminated by AV_PIX_FMT_NONE.  Can be NULL if this information is
438      * not known.
439      */
440     enum AVPixelFormat *valid_sw_formats;
441
442     /**
443      * The minimum size of frames in this hw_frames_ctx.
444      * (Zero if not known.)
445      */
446     int min_width;
447     int min_height;
448
449     /**
450      * The maximum size of frames in this hw_frames_ctx.
451      * (INT_MAX if not known / no limit.)
452      */
453     int max_width;
454     int max_height;
455 } AVHWFramesConstraints;
456
457 /**
458  * Allocate a HW-specific configuration structure for a given HW device.
459  * After use, the user must free all members as required by the specific
460  * hardware structure being used, then free the structure itself with
461  * av_free().
462  *
463  * @param device_ctx a reference to the associated AVHWDeviceContext.
464  * @return The newly created HW-specific configuration structure on
465  *         success or NULL on failure.
466  */
467 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
468
469 /**
470  * Get the constraints on HW frames given a device and the HW-specific
471  * configuration to be used with that device.  If no HW-specific
472  * configuration is provided, returns the maximum possible capabilities
473  * of the device.
474  *
475  * @param ref a reference to the associated AVHWDeviceContext.
476  * @param hwconfig a filled HW-specific configuration structure, or NULL
477  *        to return the maximum possible capabilities of the device.
478  * @return AVHWFramesConstraints structure describing the constraints
479  *         on the device, or NULL if not available.
480  */
481 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
482                                                            const void *hwconfig);
483
484 /**
485  * Free an AVHWFrameConstraints structure.
486  *
487  * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
488  */
489 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
490
491
492 /**
493  * Flags to apply to frame mappings.
494  */
495 enum {
496     /**
497      * The mapping must be readable.
498      */
499     AV_HWFRAME_MAP_READ      = 1 << 0,
500     /**
501      * The mapping must be writeable.
502      */
503     AV_HWFRAME_MAP_WRITE     = 1 << 1,
504     /**
505      * The mapped frame will be overwritten completely in subsequent
506      * operations, so the current frame data need not be loaded.  Any values
507      * which are not overwritten are unspecified.
508      */
509     AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
510     /**
511      * The mapping must be direct.  That is, there must not be any copying in
512      * the map or unmap steps.  Note that performance of direct mappings may
513      * be much lower than normal memory.
514      */
515     AV_HWFRAME_MAP_DIRECT    = 1 << 3,
516 };
517
518 /**
519  * Map a hardware frame.
520  *
521  * This has a number of different possible effects, depending on the format
522  * and origin of the src and dst frames.  On input, src should be a usable
523  * frame with valid buffers and dst should be blank (typically as just created
524  * by av_frame_alloc()).  src should have an associated hwframe context, and
525  * dst may optionally have a format and associated hwframe context.
526  *
527  * If src was created by mapping a frame from the hwframe context of dst,
528  * then this function undoes the mapping - dst is replaced by a reference to
529  * the frame that src was originally mapped from.
530  *
531  * If both src and dst have an associated hwframe context, then this function
532  * attempts to map the src frame from its hardware context to that of dst and
533  * then fill dst with appropriate data to be usable there.  This will only be
534  * possible if the hwframe contexts and associated devices are compatible -
535  * given compatible devices, av_hwframe_ctx_create_derived() can be used to
536  * create a hwframe context for dst in which mapping should be possible.
537  *
538  * If src has a hwframe context but dst does not, then the src frame is
539  * mapped to normal memory and should thereafter be usable as a normal frame.
540  * If the format is set on dst, then the mapping will attempt to create dst
541  * with that format and fail if it is not possible.  If format is unset (is
542  * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
543  * format to use is (probably the sw_format of the src hwframe context).
544  *
545  * A return value of AVERROR(ENOSYS) indicates that the mapping is not
546  * possible with the given arguments and hwframe setup, while other return
547  * values indicate that it failed somehow.
548  *
549  * @param dst Destination frame, to contain the mapping.
550  * @param src Source frame, to be mapped.
551  * @param flags Some combination of AV_HWFRAME_MAP_* flags.
552  * @return Zero on success, negative AVERROR code on failure.
553  */
554 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
555
556
557 /**
558  * Create and initialise an AVHWFramesContext as a mapping of another existing
559  * AVHWFramesContext on a different device.
560  *
561  * av_hwframe_ctx_init() should not be called after this.
562  *
563  * @param derived_frame_ctx  On success, a reference to the newly created
564  *                           AVHWFramesContext.
565  * @param derived_device_ctx A reference to the device to create the new
566  *                           AVHWFramesContext on.
567  * @param source_frame_ctx   A reference to an existing AVHWFramesContext
568  *                           which will be mapped to the derived context.
569  * @param flags  Some combination of AV_HWFRAME_MAP_* flags, defining the
570  *               mapping parameters to apply to frames which are allocated
571  *               in the derived device.
572  * @return       Zero on success, negative AVERROR code on failure.
573  */
574 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
575                                   enum AVPixelFormat format,
576                                   AVBufferRef *derived_device_ctx,
577                                   AVBufferRef *source_frame_ctx,
578                                   int flags);
579
580 #endif /* AVUTIL_HWCONTEXT_H */