Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / media / ffvpx / libavutil / hwcontext.h
blob2b33721a973ba60e310245c51667248192c3246a
1 /*
2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVUTIL_HWCONTEXT_H
20 #define AVUTIL_HWCONTEXT_H
22 #include "buffer.h"
23 #include "frame.h"
24 #include "log.h"
25 #include "pixfmt.h"
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 AV_HWDEVICE_TYPE_VULKAN,
40 AV_HWDEVICE_TYPE_D3D12VA,
43 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
45 /**
46 * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
47 * i.e. state that is not tied to a concrete processing configuration.
48 * E.g., in an API that supports hardware-accelerated encoding and decoding,
49 * this struct will (if possible) wrap the state that is common to both encoding
50 * and decoding and from which specific instances of encoders or decoders can be
51 * derived.
53 * This struct is reference-counted with the AVBuffer mechanism. The
54 * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
55 * points to the actual AVHWDeviceContext. Further objects derived from
56 * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
57 * specific properties) will hold an internal reference to it. After all the
58 * references are released, the AVHWDeviceContext itself will be freed,
59 * optionally invoking a user-specified callback for uninitializing the hardware
60 * state.
62 typedef struct AVHWDeviceContext {
63 /**
64 * A class for logging. Set by av_hwdevice_ctx_alloc().
66 const AVClass *av_class;
68 /**
69 * Private data used internally by libavutil. Must not be accessed in any
70 * way by the caller.
72 AVHWDeviceInternal *internal;
74 /**
75 * This field identifies the underlying API used for hardware access.
77 * This field is set when this struct is allocated and never changed
78 * afterwards.
80 enum AVHWDeviceType type;
82 /**
83 * The format-specific data, allocated and freed by libavutil along with
84 * this context.
86 * Should be cast by the user to the format-specific context defined in the
87 * corresponding header (hwcontext_*.h) and filled as described in the
88 * documentation before calling av_hwdevice_ctx_init().
90 * After calling av_hwdevice_ctx_init() this struct should not be modified
91 * by the caller.
93 void *hwctx;
95 /**
96 * This field may be set by the caller before calling av_hwdevice_ctx_init().
98 * If non-NULL, this callback will be called when the last reference to
99 * this context is unreferenced, immediately before it is freed.
101 * @note when other objects (e.g an AVHWFramesContext) are derived from this
102 * struct, this callback will be invoked after all such child objects
103 * are fully uninitialized and their respective destructors invoked.
105 void (*free)(struct AVHWDeviceContext *ctx);
108 * Arbitrary user data, to be used e.g. by the free() callback.
110 void *user_opaque;
111 } AVHWDeviceContext;
113 typedef struct AVHWFramesInternal AVHWFramesInternal;
116 * This struct describes a set or pool of "hardware" frames (i.e. those with
117 * data not located in normal system memory). All the frames in the pool are
118 * assumed to be allocated in the same way and interchangeable.
120 * This struct is reference-counted with the AVBuffer mechanism and tied to a
121 * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
122 * yields a reference, whose data field points to the actual AVHWFramesContext
123 * struct.
125 typedef struct AVHWFramesContext {
127 * A class for logging.
129 const AVClass *av_class;
132 * Private data used internally by libavutil. Must not be accessed in any
133 * way by the caller.
135 AVHWFramesInternal *internal;
138 * A reference to the parent AVHWDeviceContext. This reference is owned and
139 * managed by the enclosing AVHWFramesContext, but the caller may derive
140 * additional references from it.
142 AVBufferRef *device_ref;
145 * The parent AVHWDeviceContext. This is simply a pointer to
146 * device_ref->data provided for convenience.
148 * Set by libavutil in av_hwframe_ctx_init().
150 AVHWDeviceContext *device_ctx;
153 * The format-specific data, allocated and freed automatically along with
154 * this context.
156 * Should be cast by the user to the format-specific context defined in the
157 * corresponding header (hwframe_*.h) and filled as described in the
158 * documentation before calling av_hwframe_ctx_init().
160 * After any frames using this context are created, the contents of this
161 * struct should not be modified by the caller.
163 void *hwctx;
166 * This field may be set by the caller before calling av_hwframe_ctx_init().
168 * If non-NULL, this callback will be called when the last reference to
169 * this context is unreferenced, immediately before it is freed.
171 void (*free)(struct AVHWFramesContext *ctx);
174 * Arbitrary user data, to be used e.g. by the free() callback.
176 void *user_opaque;
179 * A pool from which the frames are allocated by av_hwframe_get_buffer().
180 * This field may be set by the caller before calling av_hwframe_ctx_init().
181 * The buffers returned by calling av_buffer_pool_get() on this pool must
182 * have the properties described in the documentation in the corresponding hw
183 * type's header (hwcontext_*.h). The pool will be freed strictly before
184 * this struct's free() callback is invoked.
186 * This field may be NULL, then libavutil will attempt to allocate a pool
187 * internally. Note that certain device types enforce pools allocated at
188 * fixed size (frame count), which cannot be extended dynamically. In such a
189 * case, initial_pool_size must be set appropriately.
191 AVBufferPool *pool;
194 * Initial size of the frame pool. If a device type does not support
195 * dynamically resizing the pool, then this is also the maximum pool size.
197 * May be set by the caller before calling av_hwframe_ctx_init(). Must be
198 * set if pool is NULL and the device type does not support dynamic pools.
200 int initial_pool_size;
203 * The pixel format identifying the underlying HW surface type.
205 * Must be a hwaccel format, i.e. the corresponding descriptor must have the
206 * AV_PIX_FMT_FLAG_HWACCEL flag set.
208 * Must be set by the user before calling av_hwframe_ctx_init().
210 enum AVPixelFormat format;
213 * The pixel format identifying the actual data layout of the hardware
214 * frames.
216 * Must be set by the caller before calling av_hwframe_ctx_init().
218 * @note when the underlying API does not provide the exact data layout, but
219 * only the colorspace/bit depth, this field should be set to the fully
220 * planar version of that format (e.g. for 8-bit 420 YUV it should be
221 * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
223 enum AVPixelFormat sw_format;
226 * The allocated dimensions of the frames in this pool.
228 * Must be set by the user before calling av_hwframe_ctx_init().
230 int width, height;
231 } AVHWFramesContext;
234 * Look up an AVHWDeviceType by name.
236 * @param name String name of the device type (case-insensitive).
237 * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
238 * not found.
240 enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
242 /** Get the string name of an AVHWDeviceType.
244 * @param type Type from enum AVHWDeviceType.
245 * @return Pointer to a static string containing the name, or NULL if the type
246 * is not valid.
248 const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
251 * Iterate over supported device types.
253 * @param prev AV_HWDEVICE_TYPE_NONE initially, then the previous type
254 * returned by this function in subsequent iterations.
255 * @return The next usable device type from enum AVHWDeviceType, or
256 * AV_HWDEVICE_TYPE_NONE if there are no more.
258 enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
261 * Allocate an AVHWDeviceContext for a given hardware type.
263 * @param type the type of the hardware device to allocate.
264 * @return a reference to the newly created AVHWDeviceContext on success or NULL
265 * on failure.
267 AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
270 * Finalize the device context before use. This function must be called after
271 * the context is filled with all the required information and before it is
272 * used in any way.
274 * @param ref a reference to the AVHWDeviceContext
275 * @return 0 on success, a negative AVERROR code on failure
277 int av_hwdevice_ctx_init(AVBufferRef *ref);
280 * Open a device of the specified type and create an AVHWDeviceContext for it.
282 * This is a convenience function intended to cover the simple cases. Callers
283 * who need to fine-tune device creation/management should open the device
284 * manually and then wrap it in an AVHWDeviceContext using
285 * av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().
287 * The returned context is already initialized and ready for use, the caller
288 * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of
289 * the created AVHWDeviceContext are set by this function and should not be
290 * touched by the caller.
292 * @param device_ctx On success, a reference to the newly-created device context
293 * will be written here. The reference is owned by the caller
294 * and must be released with av_buffer_unref() when no longer
295 * needed. On failure, NULL will be written to this pointer.
296 * @param type The type of the device to create.
297 * @param device A type-specific string identifying the device to open.
298 * @param opts A dictionary of additional (type-specific) options to use in
299 * opening the device. The dictionary remains owned by the caller.
300 * @param flags currently unused
302 * @return 0 on success, a negative AVERROR code on failure.
304 int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
305 const char *device, AVDictionary *opts, int flags);
308 * Create a new device of the specified type from an existing device.
310 * If the source device is a device of the target type or was originally
311 * derived from such a device (possibly through one or more intermediate
312 * devices of other types), then this will return a reference to the
313 * existing device of the same type as is requested.
315 * Otherwise, it will attempt to derive a new device from the given source
316 * device. If direct derivation to the new type is not implemented, it will
317 * attempt the same derivation from each ancestor of the source device in
318 * turn looking for an implemented derivation method.
320 * @param dst_ctx On success, a reference to the newly-created
321 * AVHWDeviceContext.
322 * @param type The type of the new device to create.
323 * @param src_ctx A reference to an existing AVHWDeviceContext which will be
324 * used to create the new device.
325 * @param flags Currently unused; should be set to zero.
326 * @return Zero on success, a negative AVERROR code on failure.
328 int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
329 enum AVHWDeviceType type,
330 AVBufferRef *src_ctx, int flags);
333 * Create a new device of the specified type from an existing device.
335 * This function performs the same action as av_hwdevice_ctx_create_derived,
336 * however, it is able to set options for the new device to be derived.
338 * @param dst_ctx On success, a reference to the newly-created
339 * AVHWDeviceContext.
340 * @param type The type of the new device to create.
341 * @param src_ctx A reference to an existing AVHWDeviceContext which will be
342 * used to create the new device.
343 * @param options Options for the new device to create, same format as in
344 * av_hwdevice_ctx_create.
345 * @param flags Currently unused; should be set to zero.
346 * @return Zero on success, a negative AVERROR code on failure.
348 int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ctx,
349 enum AVHWDeviceType type,
350 AVBufferRef *src_ctx,
351 AVDictionary *options, int flags);
354 * Allocate an AVHWFramesContext tied to a given device context.
356 * @param device_ctx a reference to a AVHWDeviceContext. This function will make
357 * a new reference for internal use, the one passed to the
358 * function remains owned by the caller.
359 * @return a reference to the newly created AVHWFramesContext on success or NULL
360 * on failure.
362 AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
365 * Finalize the context before use. This function must be called after the
366 * context is filled with all the required information and before it is attached
367 * to any frames.
369 * @param ref a reference to the AVHWFramesContext
370 * @return 0 on success, a negative AVERROR code on failure
372 int av_hwframe_ctx_init(AVBufferRef *ref);
375 * Allocate a new frame attached to the given AVHWFramesContext.
377 * @param hwframe_ctx a reference to an AVHWFramesContext
378 * @param frame an empty (freshly allocated or unreffed) frame to be filled with
379 * newly allocated buffers.
380 * @param flags currently unused, should be set to zero
381 * @return 0 on success, a negative AVERROR code on failure
383 int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
386 * Copy data to or from a hw surface. At least one of dst/src must have an
387 * AVHWFramesContext attached.
389 * If src has an AVHWFramesContext attached, then the format of dst (if set)
390 * must use one of the formats returned by av_hwframe_transfer_get_formats(src,
391 * AV_HWFRAME_TRANSFER_DIRECTION_FROM).
392 * If dst has an AVHWFramesContext attached, then the format of src must use one
393 * of the formats returned by av_hwframe_transfer_get_formats(dst,
394 * AV_HWFRAME_TRANSFER_DIRECTION_TO)
396 * dst may be "clean" (i.e. with data/buf pointers unset), in which case the
397 * data buffers will be allocated by this function using av_frame_get_buffer().
398 * If dst->format is set, then this format will be used, otherwise (when
399 * dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.
401 * The two frames must have matching allocated dimensions (i.e. equal to
402 * AVHWFramesContext.width/height), since not all device types support
403 * transferring a sub-rectangle of the whole surface. The display dimensions
404 * (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but
405 * also have to be equal for both frames. When the display dimensions are
406 * smaller than the allocated dimensions, the content of the padding in the
407 * destination frame is unspecified.
409 * @param dst the destination frame. dst is not touched on failure.
410 * @param src the source frame.
411 * @param flags currently unused, should be set to zero
412 * @return 0 on success, a negative AVERROR error code on failure.
414 int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
416 enum AVHWFrameTransferDirection {
418 * Transfer the data from the queried hw frame.
420 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
423 * Transfer the data to the queried hw frame.
425 AV_HWFRAME_TRANSFER_DIRECTION_TO,
429 * Get a list of possible source or target formats usable in
430 * av_hwframe_transfer_data().
432 * @param hwframe_ctx the frame context to obtain the information for
433 * @param dir the direction of the transfer
434 * @param formats the pointer to the output format list will be written here.
435 * The list is terminated with AV_PIX_FMT_NONE and must be freed
436 * by the caller when no longer needed using av_free().
437 * If this function returns successfully, the format list will
438 * have at least one item (not counting the terminator).
439 * On failure, the contents of this pointer are unspecified.
440 * @param flags currently unused, should be set to zero
441 * @return 0 on success, a negative AVERROR code on failure.
443 int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
444 enum AVHWFrameTransferDirection dir,
445 enum AVPixelFormat **formats, int flags);
449 * This struct describes the constraints on hardware frames attached to
450 * a given device with a hardware-specific configuration. This is returned
451 * by av_hwdevice_get_hwframe_constraints() and must be freed by
452 * av_hwframe_constraints_free() after use.
454 typedef struct AVHWFramesConstraints {
456 * A list of possible values for format in the hw_frames_ctx,
457 * terminated by AV_PIX_FMT_NONE. This member will always be filled.
459 enum AVPixelFormat *valid_hw_formats;
462 * A list of possible values for sw_format in the hw_frames_ctx,
463 * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
464 * not known.
466 enum AVPixelFormat *valid_sw_formats;
469 * The minimum size of frames in this hw_frames_ctx.
470 * (Zero if not known.)
472 int min_width;
473 int min_height;
476 * The maximum size of frames in this hw_frames_ctx.
477 * (INT_MAX if not known / no limit.)
479 int max_width;
480 int max_height;
481 } AVHWFramesConstraints;
484 * Allocate a HW-specific configuration structure for a given HW device.
485 * After use, the user must free all members as required by the specific
486 * hardware structure being used, then free the structure itself with
487 * av_free().
489 * @param device_ctx a reference to the associated AVHWDeviceContext.
490 * @return The newly created HW-specific configuration structure on
491 * success or NULL on failure.
493 void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
496 * Get the constraints on HW frames given a device and the HW-specific
497 * configuration to be used with that device. If no HW-specific
498 * configuration is provided, returns the maximum possible capabilities
499 * of the device.
501 * @param ref a reference to the associated AVHWDeviceContext.
502 * @param hwconfig a filled HW-specific configuration structure, or NULL
503 * to return the maximum possible capabilities of the device.
504 * @return AVHWFramesConstraints structure describing the constraints
505 * on the device, or NULL if not available.
507 AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
508 const void *hwconfig);
511 * Free an AVHWFrameConstraints structure.
513 * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
515 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
519 * Flags to apply to frame mappings.
521 enum {
523 * The mapping must be readable.
525 AV_HWFRAME_MAP_READ = 1 << 0,
527 * The mapping must be writeable.
529 AV_HWFRAME_MAP_WRITE = 1 << 1,
531 * The mapped frame will be overwritten completely in subsequent
532 * operations, so the current frame data need not be loaded. Any values
533 * which are not overwritten are unspecified.
535 AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
537 * The mapping must be direct. That is, there must not be any copying in
538 * the map or unmap steps. Note that performance of direct mappings may
539 * be much lower than normal memory.
541 AV_HWFRAME_MAP_DIRECT = 1 << 3,
545 * Map a hardware frame.
547 * This has a number of different possible effects, depending on the format
548 * and origin of the src and dst frames. On input, src should be a usable
549 * frame with valid buffers and dst should be blank (typically as just created
550 * by av_frame_alloc()). src should have an associated hwframe context, and
551 * dst may optionally have a format and associated hwframe context.
553 * If src was created by mapping a frame from the hwframe context of dst,
554 * then this function undoes the mapping - dst is replaced by a reference to
555 * the frame that src was originally mapped from.
557 * If both src and dst have an associated hwframe context, then this function
558 * attempts to map the src frame from its hardware context to that of dst and
559 * then fill dst with appropriate data to be usable there. This will only be
560 * possible if the hwframe contexts and associated devices are compatible -
561 * given compatible devices, av_hwframe_ctx_create_derived() can be used to
562 * create a hwframe context for dst in which mapping should be possible.
564 * If src has a hwframe context but dst does not, then the src frame is
565 * mapped to normal memory and should thereafter be usable as a normal frame.
566 * If the format is set on dst, then the mapping will attempt to create dst
567 * with that format and fail if it is not possible. If format is unset (is
568 * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
569 * format to use is (probably the sw_format of the src hwframe context).
571 * A return value of AVERROR(ENOSYS) indicates that the mapping is not
572 * possible with the given arguments and hwframe setup, while other return
573 * values indicate that it failed somehow.
575 * On failure, the destination frame will be left blank, except for the
576 * hw_frames_ctx/format fields thay may have been set by the caller - those will
577 * be preserved as they were.
579 * @param dst Destination frame, to contain the mapping.
580 * @param src Source frame, to be mapped.
581 * @param flags Some combination of AV_HWFRAME_MAP_* flags.
582 * @return Zero on success, negative AVERROR code on failure.
584 int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
588 * Create and initialise an AVHWFramesContext as a mapping of another existing
589 * AVHWFramesContext on a different device.
591 * av_hwframe_ctx_init() should not be called after this.
593 * @param derived_frame_ctx On success, a reference to the newly created
594 * AVHWFramesContext.
595 * @param format The AVPixelFormat for the derived context.
596 * @param derived_device_ctx A reference to the device to create the new
597 * AVHWFramesContext on.
598 * @param source_frame_ctx A reference to an existing AVHWFramesContext
599 * which will be mapped to the derived context.
600 * @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the
601 * mapping parameters to apply to frames which are allocated
602 * in the derived device.
603 * @return Zero on success, negative AVERROR code on failure.
605 int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
606 enum AVPixelFormat format,
607 AVBufferRef *derived_device_ctx,
608 AVBufferRef *source_frame_ctx,
609 int flags);
611 #endif /* AVUTIL_HWCONTEXT_H */