Path: blob/a-new-beginning/libavutil.xcframework/ios-arm64-simulator/libavutil.framework/Headers/hwcontext.h
2 views
/*1* This file is part of FFmpeg.2*3* FFmpeg is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2.1 of the License, or (at your option) any later version.7*8* FFmpeg is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with FFmpeg; if not, write to the Free Software15* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA16*/1718#ifndef AVUTIL_HWCONTEXT_H19#define AVUTIL_HWCONTEXT_H2021#include "buffer.h"22#include "frame.h"23#include "log.h"24#include "pixfmt.h"2526enum AVHWDeviceType {27AV_HWDEVICE_TYPE_NONE,28AV_HWDEVICE_TYPE_VDPAU,29AV_HWDEVICE_TYPE_CUDA,30AV_HWDEVICE_TYPE_VAAPI,31AV_HWDEVICE_TYPE_DXVA2,32AV_HWDEVICE_TYPE_QSV,33AV_HWDEVICE_TYPE_VIDEOTOOLBOX,34AV_HWDEVICE_TYPE_D3D11VA,35AV_HWDEVICE_TYPE_DRM,36AV_HWDEVICE_TYPE_OPENCL,37AV_HWDEVICE_TYPE_MEDIACODEC,38AV_HWDEVICE_TYPE_VULKAN,39};4041typedef struct AVHWDeviceInternal AVHWDeviceInternal;4243/**44* This struct aggregates all the (hardware/vendor-specific) "high-level" state,45* i.e. state that is not tied to a concrete processing configuration.46* E.g., in an API that supports hardware-accelerated encoding and decoding,47* this struct will (if possible) wrap the state that is common to both encoding48* and decoding and from which specific instances of encoders or decoders can be49* derived.50*51* This struct is reference-counted with the AVBuffer mechanism. The52* av_hwdevice_ctx_alloc() constructor yields a reference, whose data field53* points to the actual AVHWDeviceContext. Further objects derived from54* AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with55* specific properties) will hold an internal reference to it. After all the56* references are released, the AVHWDeviceContext itself will be freed,57* optionally invoking a user-specified callback for uninitializing the hardware58* state.59*/60typedef struct AVHWDeviceContext {61/**62* A class for logging. Set by av_hwdevice_ctx_alloc().63*/64const AVClass *av_class;6566/**67* Private data used internally by libavutil. Must not be accessed in any68* way by the caller.69*/70AVHWDeviceInternal *internal;7172/**73* This field identifies the underlying API used for hardware access.74*75* This field is set when this struct is allocated and never changed76* afterwards.77*/78enum AVHWDeviceType type;7980/**81* The format-specific data, allocated and freed by libavutil along with82* this context.83*84* Should be cast by the user to the format-specific context defined in the85* corresponding header (hwcontext_*.h) and filled as described in the86* documentation before calling av_hwdevice_ctx_init().87*88* After calling av_hwdevice_ctx_init() this struct should not be modified89* by the caller.90*/91void *hwctx;9293/**94* This field may be set by the caller before calling av_hwdevice_ctx_init().95*96* If non-NULL, this callback will be called when the last reference to97* this context is unreferenced, immediately before it is freed.98*99* @note when other objects (e.g an AVHWFramesContext) are derived from this100* struct, this callback will be invoked after all such child objects101* are fully uninitialized and their respective destructors invoked.102*/103void (*free)(struct AVHWDeviceContext *ctx);104105/**106* Arbitrary user data, to be used e.g. by the free() callback.107*/108void *user_opaque;109} AVHWDeviceContext;110111typedef struct AVHWFramesInternal AVHWFramesInternal;112113/**114* This struct describes a set or pool of "hardware" frames (i.e. those with115* data not located in normal system memory). All the frames in the pool are116* assumed to be allocated in the same way and interchangeable.117*118* This struct is reference-counted with the AVBuffer mechanism and tied to a119* given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor120* yields a reference, whose data field points to the actual AVHWFramesContext121* struct.122*/123typedef struct AVHWFramesContext {124/**125* A class for logging.126*/127const AVClass *av_class;128129/**130* Private data used internally by libavutil. Must not be accessed in any131* way by the caller.132*/133AVHWFramesInternal *internal;134135/**136* A reference to the parent AVHWDeviceContext. This reference is owned and137* managed by the enclosing AVHWFramesContext, but the caller may derive138* additional references from it.139*/140AVBufferRef *device_ref;141142/**143* The parent AVHWDeviceContext. This is simply a pointer to144* device_ref->data provided for convenience.145*146* Set by libavutil in av_hwframe_ctx_init().147*/148AVHWDeviceContext *device_ctx;149150/**151* The format-specific data, allocated and freed automatically along with152* this context.153*154* Should be cast by the user to the format-specific context defined in the155* corresponding header (hwframe_*.h) and filled as described in the156* documentation before calling av_hwframe_ctx_init().157*158* After any frames using this context are created, the contents of this159* struct should not be modified by the caller.160*/161void *hwctx;162163/**164* This field may be set by the caller before calling av_hwframe_ctx_init().165*166* If non-NULL, this callback will be called when the last reference to167* this context is unreferenced, immediately before it is freed.168*/169void (*free)(struct AVHWFramesContext *ctx);170171/**172* Arbitrary user data, to be used e.g. by the free() callback.173*/174void *user_opaque;175176/**177* A pool from which the frames are allocated by av_hwframe_get_buffer().178* This field may be set by the caller before calling av_hwframe_ctx_init().179* The buffers returned by calling av_buffer_pool_get() on this pool must180* have the properties described in the documentation in the corresponding hw181* type's header (hwcontext_*.h). The pool will be freed strictly before182* this struct's free() callback is invoked.183*184* This field may be NULL, then libavutil will attempt to allocate a pool185* internally. Note that certain device types enforce pools allocated at186* fixed size (frame count), which cannot be extended dynamically. In such a187* case, initial_pool_size must be set appropriately.188*/189AVBufferPool *pool;190191/**192* Initial size of the frame pool. If a device type does not support193* dynamically resizing the pool, then this is also the maximum pool size.194*195* May be set by the caller before calling av_hwframe_ctx_init(). Must be196* set if pool is NULL and the device type does not support dynamic pools.197*/198int initial_pool_size;199200/**201* The pixel format identifying the underlying HW surface type.202*203* Must be a hwaccel format, i.e. the corresponding descriptor must have the204* AV_PIX_FMT_FLAG_HWACCEL flag set.205*206* Must be set by the user before calling av_hwframe_ctx_init().207*/208enum AVPixelFormat format;209210/**211* The pixel format identifying the actual data layout of the hardware212* frames.213*214* Must be set by the caller before calling av_hwframe_ctx_init().215*216* @note when the underlying API does not provide the exact data layout, but217* only the colorspace/bit depth, this field should be set to the fully218* planar version of that format (e.g. for 8-bit 420 YUV it should be219* AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).220*/221enum AVPixelFormat sw_format;222223/**224* The allocated dimensions of the frames in this pool.225*226* Must be set by the user before calling av_hwframe_ctx_init().227*/228int width, height;229} AVHWFramesContext;230231/**232* Look up an AVHWDeviceType by name.233*234* @param name String name of the device type (case-insensitive).235* @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if236* not found.237*/238enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);239240/** Get the string name of an AVHWDeviceType.241*242* @param type Type from enum AVHWDeviceType.243* @return Pointer to a static string containing the name, or NULL if the type244* is not valid.245*/246const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);247248/**249* Iterate over supported device types.250*251* @param prev AV_HWDEVICE_TYPE_NONE initially, then the previous type252* returned by this function in subsequent iterations.253* @return The next usable device type from enum AVHWDeviceType, or254* AV_HWDEVICE_TYPE_NONE if there are no more.255*/256enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);257258/**259* Allocate an AVHWDeviceContext for a given hardware type.260*261* @param type the type of the hardware device to allocate.262* @return a reference to the newly created AVHWDeviceContext on success or NULL263* on failure.264*/265AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);266267/**268* Finalize the device context before use. This function must be called after269* the context is filled with all the required information and before it is270* used in any way.271*272* @param ref a reference to the AVHWDeviceContext273* @return 0 on success, a negative AVERROR code on failure274*/275int av_hwdevice_ctx_init(AVBufferRef *ref);276277/**278* Open a device of the specified type and create an AVHWDeviceContext for it.279*280* This is a convenience function intended to cover the simple cases. Callers281* who need to fine-tune device creation/management should open the device282* manually and then wrap it in an AVHWDeviceContext using283* av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().284*285* The returned context is already initialized and ready for use, the caller286* should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of287* the created AVHWDeviceContext are set by this function and should not be288* touched by the caller.289*290* @param device_ctx On success, a reference to the newly-created device context291* will be written here. The reference is owned by the caller292* and must be released with av_buffer_unref() when no longer293* needed. On failure, NULL will be written to this pointer.294* @param type The type of the device to create.295* @param device A type-specific string identifying the device to open.296* @param opts A dictionary of additional (type-specific) options to use in297* opening the device. The dictionary remains owned by the caller.298* @param flags currently unused299*300* @return 0 on success, a negative AVERROR code on failure.301*/302int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,303const char *device, AVDictionary *opts, int flags);304305/**306* Create a new device of the specified type from an existing device.307*308* If the source device is a device of the target type or was originally309* derived from such a device (possibly through one or more intermediate310* devices of other types), then this will return a reference to the311* existing device of the same type as is requested.312*313* Otherwise, it will attempt to derive a new device from the given source314* device. If direct derivation to the new type is not implemented, it will315* attempt the same derivation from each ancestor of the source device in316* turn looking for an implemented derivation method.317*318* @param dst_ctx On success, a reference to the newly-created319* AVHWDeviceContext.320* @param type The type of the new device to create.321* @param src_ctx A reference to an existing AVHWDeviceContext which will be322* used to create the new device.323* @param flags Currently unused; should be set to zero.324* @return Zero on success, a negative AVERROR code on failure.325*/326int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,327enum AVHWDeviceType type,328AVBufferRef *src_ctx, int flags);329330/**331* Create a new device of the specified type from an existing device.332*333* This function performs the same action as av_hwdevice_ctx_create_derived,334* however, it is able to set options for the new device to be derived.335*336* @param dst_ctx On success, a reference to the newly-created337* AVHWDeviceContext.338* @param type The type of the new device to create.339* @param src_ctx A reference to an existing AVHWDeviceContext which will be340* used to create the new device.341* @param options Options for the new device to create, same format as in342* av_hwdevice_ctx_create.343* @param flags Currently unused; should be set to zero.344* @return Zero on success, a negative AVERROR code on failure.345*/346int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ctx,347enum AVHWDeviceType type,348AVBufferRef *src_ctx,349AVDictionary *options, int flags);350351/**352* Allocate an AVHWFramesContext tied to a given device context.353*354* @param device_ctx a reference to a AVHWDeviceContext. This function will make355* a new reference for internal use, the one passed to the356* function remains owned by the caller.357* @return a reference to the newly created AVHWFramesContext on success or NULL358* on failure.359*/360AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);361362/**363* Finalize the context before use. This function must be called after the364* context is filled with all the required information and before it is attached365* to any frames.366*367* @param ref a reference to the AVHWFramesContext368* @return 0 on success, a negative AVERROR code on failure369*/370int av_hwframe_ctx_init(AVBufferRef *ref);371372/**373* Allocate a new frame attached to the given AVHWFramesContext.374*375* @param hwframe_ctx a reference to an AVHWFramesContext376* @param frame an empty (freshly allocated or unreffed) frame to be filled with377* newly allocated buffers.378* @param flags currently unused, should be set to zero379* @return 0 on success, a negative AVERROR code on failure380*/381int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);382383/**384* Copy data to or from a hw surface. At least one of dst/src must have an385* AVHWFramesContext attached.386*387* If src has an AVHWFramesContext attached, then the format of dst (if set)388* must use one of the formats returned by av_hwframe_transfer_get_formats(src,389* AV_HWFRAME_TRANSFER_DIRECTION_FROM).390* If dst has an AVHWFramesContext attached, then the format of src must use one391* of the formats returned by av_hwframe_transfer_get_formats(dst,392* AV_HWFRAME_TRANSFER_DIRECTION_TO)393*394* dst may be "clean" (i.e. with data/buf pointers unset), in which case the395* data buffers will be allocated by this function using av_frame_get_buffer().396* If dst->format is set, then this format will be used, otherwise (when397* dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.398*399* The two frames must have matching allocated dimensions (i.e. equal to400* AVHWFramesContext.width/height), since not all device types support401* transferring a sub-rectangle of the whole surface. The display dimensions402* (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but403* also have to be equal for both frames. When the display dimensions are404* smaller than the allocated dimensions, the content of the padding in the405* destination frame is unspecified.406*407* @param dst the destination frame. dst is not touched on failure.408* @param src the source frame.409* @param flags currently unused, should be set to zero410* @return 0 on success, a negative AVERROR error code on failure.411*/412int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);413414enum AVHWFrameTransferDirection {415/**416* Transfer the data from the queried hw frame.417*/418AV_HWFRAME_TRANSFER_DIRECTION_FROM,419420/**421* Transfer the data to the queried hw frame.422*/423AV_HWFRAME_TRANSFER_DIRECTION_TO,424};425426/**427* Get a list of possible source or target formats usable in428* av_hwframe_transfer_data().429*430* @param hwframe_ctx the frame context to obtain the information for431* @param dir the direction of the transfer432* @param formats the pointer to the output format list will be written here.433* The list is terminated with AV_PIX_FMT_NONE and must be freed434* by the caller when no longer needed using av_free().435* If this function returns successfully, the format list will436* have at least one item (not counting the terminator).437* On failure, the contents of this pointer are unspecified.438* @param flags currently unused, should be set to zero439* @return 0 on success, a negative AVERROR code on failure.440*/441int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,442enum AVHWFrameTransferDirection dir,443enum AVPixelFormat **formats, int flags);444445446/**447* This struct describes the constraints on hardware frames attached to448* a given device with a hardware-specific configuration. This is returned449* by av_hwdevice_get_hwframe_constraints() and must be freed by450* av_hwframe_constraints_free() after use.451*/452typedef struct AVHWFramesConstraints {453/**454* A list of possible values for format in the hw_frames_ctx,455* terminated by AV_PIX_FMT_NONE. This member will always be filled.456*/457enum AVPixelFormat *valid_hw_formats;458459/**460* A list of possible values for sw_format in the hw_frames_ctx,461* terminated by AV_PIX_FMT_NONE. Can be NULL if this information is462* not known.463*/464enum AVPixelFormat *valid_sw_formats;465466/**467* The minimum size of frames in this hw_frames_ctx.468* (Zero if not known.)469*/470int min_width;471int min_height;472473/**474* The maximum size of frames in this hw_frames_ctx.475* (INT_MAX if not known / no limit.)476*/477int max_width;478int max_height;479} AVHWFramesConstraints;480481/**482* Allocate a HW-specific configuration structure for a given HW device.483* After use, the user must free all members as required by the specific484* hardware structure being used, then free the structure itself with485* av_free().486*487* @param device_ctx a reference to the associated AVHWDeviceContext.488* @return The newly created HW-specific configuration structure on489* success or NULL on failure.490*/491void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);492493/**494* Get the constraints on HW frames given a device and the HW-specific495* configuration to be used with that device. If no HW-specific496* configuration is provided, returns the maximum possible capabilities497* of the device.498*499* @param ref a reference to the associated AVHWDeviceContext.500* @param hwconfig a filled HW-specific configuration structure, or NULL501* to return the maximum possible capabilities of the device.502* @return AVHWFramesConstraints structure describing the constraints503* on the device, or NULL if not available.504*/505AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,506const void *hwconfig);507508/**509* Free an AVHWFrameConstraints structure.510*511* @param constraints The (filled or unfilled) AVHWFrameConstraints structure.512*/513void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);514515516/**517* Flags to apply to frame mappings.518*/519enum {520/**521* The mapping must be readable.522*/523AV_HWFRAME_MAP_READ = 1 << 0,524/**525* The mapping must be writeable.526*/527AV_HWFRAME_MAP_WRITE = 1 << 1,528/**529* The mapped frame will be overwritten completely in subsequent530* operations, so the current frame data need not be loaded. Any values531* which are not overwritten are unspecified.532*/533AV_HWFRAME_MAP_OVERWRITE = 1 << 2,534/**535* The mapping must be direct. That is, there must not be any copying in536* the map or unmap steps. Note that performance of direct mappings may537* be much lower than normal memory.538*/539AV_HWFRAME_MAP_DIRECT = 1 << 3,540};541542/**543* Map a hardware frame.544*545* This has a number of different possible effects, depending on the format546* and origin of the src and dst frames. On input, src should be a usable547* frame with valid buffers and dst should be blank (typically as just created548* by av_frame_alloc()). src should have an associated hwframe context, and549* dst may optionally have a format and associated hwframe context.550*551* If src was created by mapping a frame from the hwframe context of dst,552* then this function undoes the mapping - dst is replaced by a reference to553* the frame that src was originally mapped from.554*555* If both src and dst have an associated hwframe context, then this function556* attempts to map the src frame from its hardware context to that of dst and557* then fill dst with appropriate data to be usable there. This will only be558* possible if the hwframe contexts and associated devices are compatible -559* given compatible devices, av_hwframe_ctx_create_derived() can be used to560* create a hwframe context for dst in which mapping should be possible.561*562* If src has a hwframe context but dst does not, then the src frame is563* mapped to normal memory and should thereafter be usable as a normal frame.564* If the format is set on dst, then the mapping will attempt to create dst565* with that format and fail if it is not possible. If format is unset (is566* AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate567* format to use is (probably the sw_format of the src hwframe context).568*569* A return value of AVERROR(ENOSYS) indicates that the mapping is not570* possible with the given arguments and hwframe setup, while other return571* values indicate that it failed somehow.572*573* On failure, the destination frame will be left blank, except for the574* hw_frames_ctx/format fields thay may have been set by the caller - those will575* be preserved as they were.576*577* @param dst Destination frame, to contain the mapping.578* @param src Source frame, to be mapped.579* @param flags Some combination of AV_HWFRAME_MAP_* flags.580* @return Zero on success, negative AVERROR code on failure.581*/582int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);583584585/**586* Create and initialise an AVHWFramesContext as a mapping of another existing587* AVHWFramesContext on a different device.588*589* av_hwframe_ctx_init() should not be called after this.590*591* @param derived_frame_ctx On success, a reference to the newly created592* AVHWFramesContext.593* @param format The AVPixelFormat for the derived context.594* @param derived_device_ctx A reference to the device to create the new595* AVHWFramesContext on.596* @param source_frame_ctx A reference to an existing AVHWFramesContext597* which will be mapped to the derived context.598* @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the599* mapping parameters to apply to frames which are allocated600* in the derived device.601* @return Zero on success, negative AVERROR code on failure.602*/603int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,604enum AVPixelFormat format,605AVBufferRef *derived_device_ctx,606AVBufferRef *source_frame_ctx,607int flags);608609#endif /* AVUTIL_HWCONTEXT_H */610611612