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