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