Path: blob/master/dep/ffmpeg/include/libavutil/hwcontext.h
4216 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,40};4142/**43* This struct aggregates all the (hardware/vendor-specific) "high-level" state,44* i.e. state that is not tied to a concrete processing configuration.45* E.g., in an API that supports hardware-accelerated encoding and decoding,46* this struct will (if possible) wrap the state that is common to both encoding47* and decoding and from which specific instances of encoders or decoders can be48* derived.49*50* This struct is reference-counted with the AVBuffer mechanism. The51* av_hwdevice_ctx_alloc() constructor yields a reference, whose data field52* points to the actual AVHWDeviceContext. Further objects derived from53* AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with54* specific properties) will hold an internal reference to it. After all the55* references are released, the AVHWDeviceContext itself will be freed,56* optionally invoking a user-specified callback for uninitializing the hardware57* state.58*/59typedef struct AVHWDeviceContext {60/**61* A class for logging. Set by av_hwdevice_ctx_alloc().62*/63const AVClass *av_class;6465/**66* This field identifies the underlying API used for hardware access.67*68* This field is set when this struct is allocated and never changed69* afterwards.70*/71enum AVHWDeviceType type;7273/**74* The format-specific data, allocated and freed by libavutil along with75* this context.76*77* Should be cast by the user to the format-specific context defined in the78* corresponding header (hwcontext_*.h) and filled as described in the79* documentation before calling av_hwdevice_ctx_init().80*81* After calling av_hwdevice_ctx_init() this struct should not be modified82* by the caller.83*/84void *hwctx;8586/**87* This field may be set by the caller before calling av_hwdevice_ctx_init().88*89* If non-NULL, this callback will be called when the last reference to90* this context is unreferenced, immediately before it is freed.91*92* @note when other objects (e.g an AVHWFramesContext) are derived from this93* struct, this callback will be invoked after all such child objects94* are fully uninitialized and their respective destructors invoked.95*/96void (*free)(struct AVHWDeviceContext *ctx);9798/**99* Arbitrary user data, to be used e.g. by the free() callback.100*/101void *user_opaque;102} AVHWDeviceContext;103104/**105* This struct describes a set or pool of "hardware" frames (i.e. those with106* data not located in normal system memory). All the frames in the pool are107* assumed to be allocated in the same way and interchangeable.108*109* This struct is reference-counted with the AVBuffer mechanism and tied to a110* given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor111* yields a reference, whose data field points to the actual AVHWFramesContext112* struct.113*/114typedef struct AVHWFramesContext {115/**116* A class for logging.117*/118const AVClass *av_class;119120/**121* A reference to the parent AVHWDeviceContext. This reference is owned and122* managed by the enclosing AVHWFramesContext, but the caller may derive123* additional references from it.124*/125AVBufferRef *device_ref;126127/**128* The parent AVHWDeviceContext. This is simply a pointer to129* device_ref->data provided for convenience.130*131* Set by libavutil in av_hwframe_ctx_init().132*/133AVHWDeviceContext *device_ctx;134135/**136* The format-specific data, allocated and freed automatically along with137* this context.138*139* The user shall ignore this field if the corresponding format-specific140* header (hwcontext_*.h) does not define a context to be used as141* AVHWFramesContext.hwctx.142*143* Otherwise, it should be cast by the user to said context and filled144* as described in the documentation before calling av_hwframe_ctx_init().145*146* After any frames using this context are created, the contents of this147* struct should not be modified by the caller.148*/149void *hwctx;150151/**152* This field may be set by the caller before calling av_hwframe_ctx_init().153*154* If non-NULL, this callback will be called when the last reference to155* this context is unreferenced, immediately before it is freed.156*/157void (*free)(struct AVHWFramesContext *ctx);158159/**160* Arbitrary user data, to be used e.g. by the free() callback.161*/162void *user_opaque;163164/**165* A pool from which the frames are allocated by av_hwframe_get_buffer().166* This field may be set by the caller before calling av_hwframe_ctx_init().167* The buffers returned by calling av_buffer_pool_get() on this pool must168* have the properties described in the documentation in the corresponding hw169* type's header (hwcontext_*.h). The pool will be freed strictly before170* this struct's free() callback is invoked.171*172* This field may be NULL, then libavutil will attempt to allocate a pool173* internally. Note that certain device types enforce pools allocated at174* fixed size (frame count), which cannot be extended dynamically. In such a175* case, initial_pool_size must be set appropriately.176*/177AVBufferPool *pool;178179/**180* Initial size of the frame pool. If a device type does not support181* dynamically resizing the pool, then this is also the maximum pool size.182*183* May be set by the caller before calling av_hwframe_ctx_init(). Must be184* set if pool is NULL and the device type does not support dynamic pools.185*/186int initial_pool_size;187188/**189* The pixel format identifying the underlying HW surface type.190*191* Must be a hwaccel format, i.e. the corresponding descriptor must have the192* AV_PIX_FMT_FLAG_HWACCEL flag set.193*194* Must be set by the user before calling av_hwframe_ctx_init().195*/196enum AVPixelFormat format;197198/**199* The pixel format identifying the actual data layout of the hardware200* frames.201*202* Must be set by the caller before calling av_hwframe_ctx_init().203*204* @note when the underlying API does not provide the exact data layout, but205* only the colorspace/bit depth, this field should be set to the fully206* planar version of that format (e.g. for 8-bit 420 YUV it should be207* AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).208*/209enum AVPixelFormat sw_format;210211/**212* The allocated dimensions of the frames in this pool.213*214* Must be set by the user before calling av_hwframe_ctx_init().215*/216int width, height;217} AVHWFramesContext;218219/**220* Look up an AVHWDeviceType by name.221*222* @param name String name of the device type (case-insensitive).223* @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if224* not found.225*/226enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);227228/** Get the string name of an AVHWDeviceType.229*230* @param type Type from enum AVHWDeviceType.231* @return Pointer to a static string containing the name, or NULL if the type232* is not valid.233*/234const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);235236/**237* Iterate over supported device types.238*239* @param prev AV_HWDEVICE_TYPE_NONE initially, then the previous type240* returned by this function in subsequent iterations.241* @return The next usable device type from enum AVHWDeviceType, or242* AV_HWDEVICE_TYPE_NONE if there are no more.243*/244enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);245246/**247* Allocate an AVHWDeviceContext for a given hardware type.248*249* @param type the type of the hardware device to allocate.250* @return a reference to the newly created AVHWDeviceContext on success or NULL251* on failure.252*/253AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);254255/**256* Finalize the device context before use. This function must be called after257* the context is filled with all the required information and before it is258* used in any way.259*260* @param ref a reference to the AVHWDeviceContext261* @return 0 on success, a negative AVERROR code on failure262*/263int av_hwdevice_ctx_init(AVBufferRef *ref);264265/**266* Open a device of the specified type and create an AVHWDeviceContext for it.267*268* This is a convenience function intended to cover the simple cases. Callers269* who need to fine-tune device creation/management should open the device270* manually and then wrap it in an AVHWDeviceContext using271* av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().272*273* The returned context is already initialized and ready for use, the caller274* should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of275* the created AVHWDeviceContext are set by this function and should not be276* touched by the caller.277*278* @param device_ctx On success, a reference to the newly-created device context279* will be written here. The reference is owned by the caller280* and must be released with av_buffer_unref() when no longer281* needed. On failure, NULL will be written to this pointer.282* @param type The type of the device to create.283* @param device A type-specific string identifying the device to open.284* @param opts A dictionary of additional (type-specific) options to use in285* opening the device. The dictionary remains owned by the caller.286* @param flags currently unused287*288* @return 0 on success, a negative AVERROR code on failure.289*/290int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,291const char *device, AVDictionary *opts, int flags);292293/**294* Create a new device of the specified type from an existing device.295*296* If the source device is a device of the target type or was originally297* derived from such a device (possibly through one or more intermediate298* devices of other types), then this will return a reference to the299* existing device of the same type as is requested.300*301* Otherwise, it will attempt to derive a new device from the given source302* device. If direct derivation to the new type is not implemented, it will303* attempt the same derivation from each ancestor of the source device in304* turn looking for an implemented derivation method.305*306* @param dst_ctx On success, a reference to the newly-created307* AVHWDeviceContext.308* @param type The type of the new device to create.309* @param src_ctx A reference to an existing AVHWDeviceContext which will be310* used to create the new device.311* @param flags Currently unused; should be set to zero.312* @return Zero on success, a negative AVERROR code on failure.313*/314int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,315enum AVHWDeviceType type,316AVBufferRef *src_ctx, int flags);317318/**319* Create a new device of the specified type from an existing device.320*321* This function performs the same action as av_hwdevice_ctx_create_derived,322* however, it is able to set options for the new device to be derived.323*324* @param dst_ctx On success, a reference to the newly-created325* AVHWDeviceContext.326* @param type The type of the new device to create.327* @param src_ctx A reference to an existing AVHWDeviceContext which will be328* used to create the new device.329* @param options Options for the new device to create, same format as in330* av_hwdevice_ctx_create.331* @param flags Currently unused; should be set to zero.332* @return Zero on success, a negative AVERROR code on failure.333*/334int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ctx,335enum AVHWDeviceType type,336AVBufferRef *src_ctx,337AVDictionary *options, int flags);338339/**340* Allocate an AVHWFramesContext tied to a given device context.341*342* @param device_ctx a reference to a AVHWDeviceContext. This function will make343* a new reference for internal use, the one passed to the344* function remains owned by the caller.345* @return a reference to the newly created AVHWFramesContext on success or NULL346* on failure.347*/348AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);349350/**351* Finalize the context before use. This function must be called after the352* context is filled with all the required information and before it is attached353* to any frames.354*355* @param ref a reference to the AVHWFramesContext356* @return 0 on success, a negative AVERROR code on failure357*/358int av_hwframe_ctx_init(AVBufferRef *ref);359360/**361* Allocate a new frame attached to the given AVHWFramesContext.362*363* @param hwframe_ctx a reference to an AVHWFramesContext364* @param frame an empty (freshly allocated or unreffed) frame to be filled with365* newly allocated buffers.366* @param flags currently unused, should be set to zero367* @return 0 on success, a negative AVERROR code on failure368*/369int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);370371/**372* Copy data to or from a hw surface. At least one of dst/src must have an373* AVHWFramesContext attached.374*375* If src has an AVHWFramesContext attached, then the format of dst (if set)376* must use one of the formats returned by av_hwframe_transfer_get_formats(src,377* AV_HWFRAME_TRANSFER_DIRECTION_FROM).378* If dst has an AVHWFramesContext attached, then the format of src must use one379* of the formats returned by av_hwframe_transfer_get_formats(dst,380* AV_HWFRAME_TRANSFER_DIRECTION_TO)381*382* dst may be "clean" (i.e. with data/buf pointers unset), in which case the383* data buffers will be allocated by this function using av_frame_get_buffer().384* If dst->format is set, then this format will be used, otherwise (when385* dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.386*387* The two frames must have matching allocated dimensions (i.e. equal to388* AVHWFramesContext.width/height), since not all device types support389* transferring a sub-rectangle of the whole surface. The display dimensions390* (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but391* also have to be equal for both frames. When the display dimensions are392* smaller than the allocated dimensions, the content of the padding in the393* destination frame is unspecified.394*395* @param dst the destination frame. dst is not touched on failure.396* @param src the source frame.397* @param flags currently unused, should be set to zero398* @return 0 on success, a negative AVERROR error code on failure.399*/400int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);401402enum AVHWFrameTransferDirection {403/**404* Transfer the data from the queried hw frame.405*/406AV_HWFRAME_TRANSFER_DIRECTION_FROM,407408/**409* Transfer the data to the queried hw frame.410*/411AV_HWFRAME_TRANSFER_DIRECTION_TO,412};413414/**415* Get a list of possible source or target formats usable in416* av_hwframe_transfer_data().417*418* @param hwframe_ctx the frame context to obtain the information for419* @param dir the direction of the transfer420* @param formats the pointer to the output format list will be written here.421* The list is terminated with AV_PIX_FMT_NONE and must be freed422* by the caller when no longer needed using av_free().423* If this function returns successfully, the format list will424* have at least one item (not counting the terminator).425* On failure, the contents of this pointer are unspecified.426* @param flags currently unused, should be set to zero427* @return 0 on success, a negative AVERROR code on failure.428*/429int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,430enum AVHWFrameTransferDirection dir,431enum AVPixelFormat **formats, int flags);432433434/**435* This struct describes the constraints on hardware frames attached to436* a given device with a hardware-specific configuration. This is returned437* by av_hwdevice_get_hwframe_constraints() and must be freed by438* av_hwframe_constraints_free() after use.439*/440typedef struct AVHWFramesConstraints {441/**442* A list of possible values for format in the hw_frames_ctx,443* terminated by AV_PIX_FMT_NONE. This member will always be filled.444*/445enum AVPixelFormat *valid_hw_formats;446447/**448* A list of possible values for sw_format in the hw_frames_ctx,449* terminated by AV_PIX_FMT_NONE. Can be NULL if this information is450* not known.451*/452enum AVPixelFormat *valid_sw_formats;453454/**455* The minimum size of frames in this hw_frames_ctx.456* (Zero if not known.)457*/458int min_width;459int min_height;460461/**462* The maximum size of frames in this hw_frames_ctx.463* (INT_MAX if not known / no limit.)464*/465int max_width;466int max_height;467} AVHWFramesConstraints;468469/**470* Allocate a HW-specific configuration structure for a given HW device.471* After use, the user must free all members as required by the specific472* hardware structure being used, then free the structure itself with473* av_free().474*475* @param device_ctx a reference to the associated AVHWDeviceContext.476* @return The newly created HW-specific configuration structure on477* success or NULL on failure.478*/479void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);480481/**482* Get the constraints on HW frames given a device and the HW-specific483* configuration to be used with that device. If no HW-specific484* configuration is provided, returns the maximum possible capabilities485* of the device.486*487* @param ref a reference to the associated AVHWDeviceContext.488* @param hwconfig a filled HW-specific configuration structure, or NULL489* to return the maximum possible capabilities of the device.490* @return AVHWFramesConstraints structure describing the constraints491* on the device, or NULL if not available.492*/493AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,494const void *hwconfig);495496/**497* Free an AVHWFrameConstraints structure.498*499* @param constraints The (filled or unfilled) AVHWFrameConstraints structure.500*/501void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);502503504/**505* Flags to apply to frame mappings.506*/507enum {508/**509* The mapping must be readable.510*/511AV_HWFRAME_MAP_READ = 1 << 0,512/**513* The mapping must be writeable.514*/515AV_HWFRAME_MAP_WRITE = 1 << 1,516/**517* The mapped frame will be overwritten completely in subsequent518* operations, so the current frame data need not be loaded. Any values519* which are not overwritten are unspecified.520*/521AV_HWFRAME_MAP_OVERWRITE = 1 << 2,522/**523* The mapping must be direct. That is, there must not be any copying in524* the map or unmap steps. Note that performance of direct mappings may525* be much lower than normal memory.526*/527AV_HWFRAME_MAP_DIRECT = 1 << 3,528};529530/**531* Map a hardware frame.532*533* This has a number of different possible effects, depending on the format534* and origin of the src and dst frames. On input, src should be a usable535* frame with valid buffers and dst should be blank (typically as just created536* by av_frame_alloc()). src should have an associated hwframe context, and537* dst may optionally have a format and associated hwframe context.538*539* If src was created by mapping a frame from the hwframe context of dst,540* then this function undoes the mapping - dst is replaced by a reference to541* the frame that src was originally mapped from.542*543* If both src and dst have an associated hwframe context, then this function544* attempts to map the src frame from its hardware context to that of dst and545* then fill dst with appropriate data to be usable there. This will only be546* possible if the hwframe contexts and associated devices are compatible -547* given compatible devices, av_hwframe_ctx_create_derived() can be used to548* create a hwframe context for dst in which mapping should be possible.549*550* If src has a hwframe context but dst does not, then the src frame is551* mapped to normal memory and should thereafter be usable as a normal frame.552* If the format is set on dst, then the mapping will attempt to create dst553* with that format and fail if it is not possible. If format is unset (is554* AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate555* format to use is (probably the sw_format of the src hwframe context).556*557* A return value of AVERROR(ENOSYS) indicates that the mapping is not558* possible with the given arguments and hwframe setup, while other return559* values indicate that it failed somehow.560*561* On failure, the destination frame will be left blank, except for the562* hw_frames_ctx/format fields thay may have been set by the caller - those will563* be preserved as they were.564*565* @param dst Destination frame, to contain the mapping.566* @param src Source frame, to be mapped.567* @param flags Some combination of AV_HWFRAME_MAP_* flags.568* @return Zero on success, negative AVERROR code on failure.569*/570int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);571572573/**574* Create and initialise an AVHWFramesContext as a mapping of another existing575* AVHWFramesContext on a different device.576*577* av_hwframe_ctx_init() should not be called after this.578*579* @param derived_frame_ctx On success, a reference to the newly created580* AVHWFramesContext.581* @param format The AVPixelFormat for the derived context.582* @param derived_device_ctx A reference to the device to create the new583* AVHWFramesContext on.584* @param source_frame_ctx A reference to an existing AVHWFramesContext585* which will be mapped to the derived context.586* @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the587* mapping parameters to apply to frames which are allocated588* in the derived device.589* @return Zero on success, negative AVERROR code on failure.590*/591int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,592enum AVPixelFormat format,593AVBufferRef *derived_device_ctx,594AVBufferRef *source_frame_ctx,595int flags);596597#endif /* AVUTIL_HWCONTEXT_H */598599600