Path: blob/master/dep/ffmpeg/include/libavutil/hwcontext.h
7489 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,39AV_HWDEVICE_TYPE_D3D12VA,40AV_HWDEVICE_TYPE_AMF,41/* OpenHarmony Codec device */42AV_HWDEVICE_TYPE_OHCODEC,43};4445/**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 encoding50* and decoding and from which specific instances of encoders or decoders can be51* derived.52*53* This struct is reference-counted with the AVBuffer mechanism. The54* av_hwdevice_ctx_alloc() constructor yields a reference, whose data field55* points to the actual AVHWDeviceContext. Further objects derived from56* AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with57* specific properties) will hold an internal reference to it. After all the58* references are released, the AVHWDeviceContext itself will be freed,59* optionally invoking a user-specified callback for uninitializing the hardware60* state.61*/62typedef struct AVHWDeviceContext {63/**64* A class for logging. Set by av_hwdevice_ctx_alloc().65*/66const AVClass *av_class;6768/**69* This field identifies the underlying API used for hardware access.70*71* This field is set when this struct is allocated and never changed72* afterwards.73*/74enum AVHWDeviceType type;7576/**77* The format-specific data, allocated and freed by libavutil along with78* this context.79*80* Should be cast by the user to the format-specific context defined in the81* corresponding header (hwcontext_*.h) and filled as described in the82* documentation before calling av_hwdevice_ctx_init().83*84* After calling av_hwdevice_ctx_init() this struct should not be modified85* by the caller.86*/87void *hwctx;8889/**90* This field may be set by the caller before calling av_hwdevice_ctx_init().91*92* If non-NULL, this callback will be called when the last reference to93* this context is unreferenced, immediately before it is freed.94*95* @note when other objects (e.g an AVHWFramesContext) are derived from this96* struct, this callback will be invoked after all such child objects97* are fully uninitialized and their respective destructors invoked.98*/99void (*free)(struct AVHWDeviceContext *ctx);100101/**102* Arbitrary user data, to be used e.g. by the free() callback.103*/104void *user_opaque;105} AVHWDeviceContext;106107/**108* This struct describes a set or pool of "hardware" frames (i.e. those with109* data not located in normal system memory). All the frames in the pool are110* assumed to be allocated in the same way and interchangeable.111*112* This struct is reference-counted with the AVBuffer mechanism and tied to a113* given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor114* yields a reference, whose data field points to the actual AVHWFramesContext115* struct.116*/117typedef struct AVHWFramesContext {118/**119* A class for logging.120*/121const AVClass *av_class;122123/**124* A reference to the parent AVHWDeviceContext. This reference is owned and125* managed by the enclosing AVHWFramesContext, but the caller may derive126* additional references from it.127*/128AVBufferRef *device_ref;129130/**131* The parent AVHWDeviceContext. This is simply a pointer to132* device_ref->data provided for convenience.133*134* Set by libavutil in av_hwframe_ctx_init().135*/136AVHWDeviceContext *device_ctx;137138/**139* The format-specific data, allocated and freed automatically along with140* this context.141*142* The user shall ignore this field if the corresponding format-specific143* header (hwcontext_*.h) does not define a context to be used as144* AVHWFramesContext.hwctx.145*146* Otherwise, it should be cast by the user to said context and filled147* as described in the documentation before calling av_hwframe_ctx_init().148*149* After any frames using this context are created, the contents of this150* struct should not be modified by the caller.151*/152void *hwctx;153154/**155* This field may be set by the caller before calling av_hwframe_ctx_init().156*157* If non-NULL, this callback will be called when the last reference to158* this context is unreferenced, immediately before it is freed.159*/160void (*free)(struct AVHWFramesContext *ctx);161162/**163* Arbitrary user data, to be used e.g. by the free() callback.164*/165void *user_opaque;166167/**168* A pool from which the frames are allocated by av_hwframe_get_buffer().169* This field may be set by the caller before calling av_hwframe_ctx_init().170* The buffers returned by calling av_buffer_pool_get() on this pool must171* have the properties described in the documentation in the corresponding hw172* type's header (hwcontext_*.h). The pool will be freed strictly before173* this struct's free() callback is invoked.174*175* This field may be NULL, then libavutil will attempt to allocate a pool176* internally. Note that certain device types enforce pools allocated at177* fixed size (frame count), which cannot be extended dynamically. In such a178* case, initial_pool_size must be set appropriately.179*/180AVBufferPool *pool;181182/**183* Initial size of the frame pool. If a device type does not support184* dynamically resizing the pool, then this is also the maximum pool size.185*186* May be set by the caller before calling av_hwframe_ctx_init(). Must be187* set if pool is NULL and the device type does not support dynamic pools.188*/189int initial_pool_size;190191/**192* The pixel format identifying the underlying HW surface type.193*194* Must be a hwaccel format, i.e. the corresponding descriptor must have the195* AV_PIX_FMT_FLAG_HWACCEL flag set.196*197* Must be set by the user before calling av_hwframe_ctx_init().198*/199enum AVPixelFormat format;200201/**202* The pixel format identifying the actual data layout of the hardware203* frames.204*205* Must be set by the caller before calling av_hwframe_ctx_init().206*207* @note when the underlying API does not provide the exact data layout, but208* only the colorspace/bit depth, this field should be set to the fully209* planar version of that format (e.g. for 8-bit 420 YUV it should be210* AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).211*/212enum AVPixelFormat sw_format;213214/**215* The allocated dimensions of the frames in this pool.216*217* Must be set by the user before calling av_hwframe_ctx_init().218*/219int width, height;220} AVHWFramesContext;221222/**223* Look up an AVHWDeviceType by name.224*225* @param name String name of the device type (case-insensitive).226* @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if227* not found.228*/229enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);230231/** Get the string name of an AVHWDeviceType.232*233* @param type Type from enum AVHWDeviceType.234* @return Pointer to a static string containing the name, or NULL if the type235* is not valid.236*/237const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);238239/**240* Iterate over supported device types.241*242* @param prev AV_HWDEVICE_TYPE_NONE initially, then the previous type243* returned by this function in subsequent iterations.244* @return The next usable device type from enum AVHWDeviceType, or245* AV_HWDEVICE_TYPE_NONE if there are no more.246*/247enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);248249/**250* Allocate an AVHWDeviceContext for a given hardware type.251*252* @param type the type of the hardware device to allocate.253* @return a reference to the newly created AVHWDeviceContext on success or NULL254* on failure.255*/256AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);257258/**259* Finalize the device context before use. This function must be called after260* the context is filled with all the required information and before it is261* used in any way.262*263* @param ref a reference to the AVHWDeviceContext264* @return 0 on success, a negative AVERROR code on failure265*/266int av_hwdevice_ctx_init(AVBufferRef *ref);267268/**269* Open a device of the specified type and create an AVHWDeviceContext for it.270*271* This is a convenience function intended to cover the simple cases. Callers272* who need to fine-tune device creation/management should open the device273* manually and then wrap it in an AVHWDeviceContext using274* av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().275*276* The returned context is already initialized and ready for use, the caller277* should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of278* the created AVHWDeviceContext are set by this function and should not be279* touched by the caller.280*281* @param device_ctx On success, a reference to the newly-created device context282* will be written here. The reference is owned by the caller283* and must be released with av_buffer_unref() when no longer284* needed. On failure, NULL will be written to this pointer.285* @param type The type of the device to create.286* @param device A type-specific string identifying the device to open.287* @param opts A dictionary of additional (type-specific) options to use in288* opening the device. The dictionary remains owned by the caller.289* @param flags currently unused290*291* @return 0 on success, a negative AVERROR code on failure.292*/293int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,294const char *device, AVDictionary *opts, int flags);295296/**297* Create a new device of the specified type from an existing device.298*299* If the source device is a device of the target type or was originally300* derived from such a device (possibly through one or more intermediate301* devices of other types), then this will return a reference to the302* existing device of the same type as is requested.303*304* Otherwise, it will attempt to derive a new device from the given source305* device. If direct derivation to the new type is not implemented, it will306* attempt the same derivation from each ancestor of the source device in307* turn looking for an implemented derivation method.308*309* @param dst_ctx On success, a reference to the newly-created310* AVHWDeviceContext.311* @param type The type of the new device to create.312* @param src_ctx A reference to an existing AVHWDeviceContext which will be313* used to create the new device.314* @param flags Currently unused; should be set to zero.315* @return Zero on success, a negative AVERROR code on failure.316*/317int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,318enum AVHWDeviceType type,319AVBufferRef *src_ctx, int flags);320321/**322* Create a new device of the specified type from an existing device.323*324* This function performs the same action as av_hwdevice_ctx_create_derived,325* however, it is able to set options for the new device to be derived.326*327* @param dst_ctx On success, a reference to the newly-created328* AVHWDeviceContext.329* @param type The type of the new device to create.330* @param src_ctx A reference to an existing AVHWDeviceContext which will be331* used to create the new device.332* @param options Options for the new device to create, same format as in333* av_hwdevice_ctx_create.334* @param flags Currently unused; should be set to zero.335* @return Zero on success, a negative AVERROR code on failure.336*/337int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ctx,338enum AVHWDeviceType type,339AVBufferRef *src_ctx,340AVDictionary *options, int flags);341342/**343* Allocate an AVHWFramesContext tied to a given device context.344*345* @param device_ctx a reference to a AVHWDeviceContext. This function will make346* a new reference for internal use, the one passed to the347* function remains owned by the caller.348* @return a reference to the newly created AVHWFramesContext on success or NULL349* on failure.350*/351AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);352353/**354* Finalize the context before use. This function must be called after the355* context is filled with all the required information and before it is attached356* to any frames.357*358* @param ref a reference to the AVHWFramesContext359* @return 0 on success, a negative AVERROR code on failure360*/361int av_hwframe_ctx_init(AVBufferRef *ref);362363/**364* Allocate a new frame attached to the given AVHWFramesContext.365*366* @param hwframe_ctx a reference to an AVHWFramesContext367* @param frame an empty (freshly allocated or unreffed) frame to be filled with368* newly allocated buffers.369* @param flags currently unused, should be set to zero370* @return 0 on success, a negative AVERROR code on failure371*/372int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);373374/**375* Copy data to or from a hw surface. At least one of dst/src must have an376* AVHWFramesContext attached.377*378* If src has an AVHWFramesContext attached, then the format of dst (if set)379* must use one of the formats returned by av_hwframe_transfer_get_formats(src,380* AV_HWFRAME_TRANSFER_DIRECTION_FROM).381* If dst has an AVHWFramesContext attached, then the format of src must use one382* of the formats returned by av_hwframe_transfer_get_formats(dst,383* AV_HWFRAME_TRANSFER_DIRECTION_TO)384*385* dst may be "clean" (i.e. with data/buf pointers unset), in which case the386* data buffers will be allocated by this function using av_frame_get_buffer().387* If dst->format is set, then this format will be used, otherwise (when388* dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.389*390* The two frames must have matching allocated dimensions (i.e. equal to391* AVHWFramesContext.width/height), since not all device types support392* transferring a sub-rectangle of the whole surface. The display dimensions393* (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but394* also have to be equal for both frames. When the display dimensions are395* smaller than the allocated dimensions, the content of the padding in the396* destination frame is unspecified.397*398* @param dst the destination frame. dst is not touched on failure.399* @param src the source frame.400* @param flags currently unused, should be set to zero401* @return 0 on success, a negative AVERROR error code on failure.402*/403int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);404405enum AVHWFrameTransferDirection {406/**407* Transfer the data from the queried hw frame.408*/409AV_HWFRAME_TRANSFER_DIRECTION_FROM,410411/**412* Transfer the data to the queried hw frame.413*/414AV_HWFRAME_TRANSFER_DIRECTION_TO,415};416417/**418* Get a list of possible source or target formats usable in419* av_hwframe_transfer_data().420*421* @param hwframe_ctx the frame context to obtain the information for422* @param dir the direction of the transfer423* @param formats the pointer to the output format list will be written here.424* The list is terminated with AV_PIX_FMT_NONE and must be freed425* by the caller when no longer needed using av_free().426* If this function returns successfully, the format list will427* have at least one item (not counting the terminator).428* On failure, the contents of this pointer are unspecified.429* @param flags currently unused, should be set to zero430* @return 0 on success, a negative AVERROR code on failure.431*/432int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,433enum AVHWFrameTransferDirection dir,434enum AVPixelFormat **formats, int flags);435436437/**438* This struct describes the constraints on hardware frames attached to439* a given device with a hardware-specific configuration. This is returned440* by av_hwdevice_get_hwframe_constraints() and must be freed by441* av_hwframe_constraints_free() after use.442*/443typedef struct AVHWFramesConstraints {444/**445* A list of possible values for format in the hw_frames_ctx,446* terminated by AV_PIX_FMT_NONE. This member will always be filled.447*/448enum AVPixelFormat *valid_hw_formats;449450/**451* A list of possible values for sw_format in the hw_frames_ctx,452* terminated by AV_PIX_FMT_NONE. Can be NULL if this information is453* not known.454*/455enum AVPixelFormat *valid_sw_formats;456457/**458* The minimum size of frames in this hw_frames_ctx.459* (Zero if not known.)460*/461int min_width;462int min_height;463464/**465* The maximum size of frames in this hw_frames_ctx.466* (INT_MAX if not known / no limit.)467*/468int max_width;469int max_height;470} AVHWFramesConstraints;471472/**473* Allocate a HW-specific configuration structure for a given HW device.474* After use, the user must free all members as required by the specific475* hardware structure being used, then free the structure itself with476* av_free().477*478* @param device_ctx a reference to the associated AVHWDeviceContext.479* @return The newly created HW-specific configuration structure on480* success or NULL on failure.481*/482void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);483484/**485* Get the constraints on HW frames given a device and the HW-specific486* configuration to be used with that device. If no HW-specific487* configuration is provided, returns the maximum possible capabilities488* of the device.489*490* @param ref a reference to the associated AVHWDeviceContext.491* @param hwconfig a filled HW-specific configuration structure, or NULL492* to return the maximum possible capabilities of the device.493* @return AVHWFramesConstraints structure describing the constraints494* on the device, or NULL if not available.495*/496AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,497const void *hwconfig);498499/**500* Free an AVHWFrameConstraints structure.501*502* @param constraints The (filled or unfilled) AVHWFrameConstraints structure.503*/504void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);505506507/**508* Flags to apply to frame mappings.509*/510enum {511/**512* The mapping must be readable.513*/514AV_HWFRAME_MAP_READ = 1 << 0,515/**516* The mapping must be writeable.517*/518AV_HWFRAME_MAP_WRITE = 1 << 1,519/**520* The mapped frame will be overwritten completely in subsequent521* operations, so the current frame data need not be loaded. Any values522* which are not overwritten are unspecified.523*/524AV_HWFRAME_MAP_OVERWRITE = 1 << 2,525/**526* The mapping must be direct. That is, there must not be any copying in527* the map or unmap steps. Note that performance of direct mappings may528* be much lower than normal memory.529*/530AV_HWFRAME_MAP_DIRECT = 1 << 3,531};532533/**534* Map a hardware frame.535*536* This has a number of different possible effects, depending on the format537* and origin of the src and dst frames. On input, src should be a usable538* frame with valid buffers and dst should be blank (typically as just created539* by av_frame_alloc()). src should have an associated hwframe context, and540* dst may optionally have a format and associated hwframe context.541*542* If src was created by mapping a frame from the hwframe context of dst,543* then this function undoes the mapping - dst is replaced by a reference to544* the frame that src was originally mapped from.545*546* If both src and dst have an associated hwframe context, then this function547* attempts to map the src frame from its hardware context to that of dst and548* then fill dst with appropriate data to be usable there. This will only be549* possible if the hwframe contexts and associated devices are compatible -550* given compatible devices, av_hwframe_ctx_create_derived() can be used to551* create a hwframe context for dst in which mapping should be possible.552*553* If src has a hwframe context but dst does not, then the src frame is554* mapped to normal memory and should thereafter be usable as a normal frame.555* If the format is set on dst, then the mapping will attempt to create dst556* with that format and fail if it is not possible. If format is unset (is557* AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate558* format to use is (probably the sw_format of the src hwframe context).559*560* A return value of AVERROR(ENOSYS) indicates that the mapping is not561* possible with the given arguments and hwframe setup, while other return562* values indicate that it failed somehow.563*564* On failure, the destination frame will be left blank, except for the565* hw_frames_ctx/format fields they may have been set by the caller - those will566* be preserved as they were.567*568* @param dst Destination frame, to contain the mapping.569* @param src Source frame, to be mapped.570* @param flags Some combination of AV_HWFRAME_MAP_* flags.571* @return Zero on success, negative AVERROR code on failure.572*/573int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);574575576/**577* Create and initialise an AVHWFramesContext as a mapping of another existing578* AVHWFramesContext on a different device.579*580* av_hwframe_ctx_init() should not be called after this.581*582* @param derived_frame_ctx On success, a reference to the newly created583* AVHWFramesContext.584* @param format The AVPixelFormat for the derived context.585* @param derived_device_ctx A reference to the device to create the new586* AVHWFramesContext on.587* @param source_frame_ctx A reference to an existing AVHWFramesContext588* which will be mapped to the derived context.589* @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the590* mapping parameters to apply to frames which are allocated591* in the derived device.592* @return Zero on success, negative AVERROR code on failure.593*/594int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,595enum AVPixelFormat format,596AVBufferRef *derived_device_ctx,597AVBufferRef *source_frame_ctx,598int flags);599600#endif /* AVUTIL_HWCONTEXT_H */601602603