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