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