123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- /*
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #ifndef AVUTIL_HWCONTEXT_H
- #define AVUTIL_HWCONTEXT_H
- #include "buffer.h"
- #include "frame.h"
- #include "log.h"
- #include "pixfmt.h"
- enum AVHWDeviceType {
- AV_HWDEVICE_TYPE_NONE,
- AV_HWDEVICE_TYPE_VDPAU,
- AV_HWDEVICE_TYPE_CUDA,
- AV_HWDEVICE_TYPE_VAAPI,
- AV_HWDEVICE_TYPE_DXVA2,
- AV_HWDEVICE_TYPE_QSV,
- AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
- AV_HWDEVICE_TYPE_D3D11VA,
- AV_HWDEVICE_TYPE_DRM,
- AV_HWDEVICE_TYPE_OPENCL,
- AV_HWDEVICE_TYPE_MEDIACODEC,
- AV_HWDEVICE_TYPE_VULKAN,
- };
- typedef struct AVHWDeviceInternal AVHWDeviceInternal;
- /**
- * This struct aggregates all the (hardware/vendor-specific) "high-level" state,
- * i.e. state that is not tied to a concrete processing configuration.
- * E.g., in an API that supports hardware-accelerated encoding and decoding,
- * this struct will (if possible) wrap the state that is common to both encoding
- * and decoding and from which specific instances of encoders or decoders can be
- * derived.
- *
- * This struct is reference-counted with the AVBuffer mechanism. The
- * av_hwdevice_ctx_alloc() constructor yields a reference, whose data field
- * points to the actual AVHWDeviceContext. Further objects derived from
- * AVHWDeviceContext (such as AVHWFramesContext, describing a frame pool with
- * specific properties) will hold an internal reference to it. After all the
- * references are released, the AVHWDeviceContext itself will be freed,
- * optionally invoking a user-specified callback for uninitializing the hardware
- * state.
- */
- typedef struct AVHWDeviceContext {
- /**
- * A class for logging. Set by av_hwdevice_ctx_alloc().
- */
- const AVClass *av_class;
- /**
- * Private data used internally by libavutil. Must not be accessed in any
- * way by the caller.
- */
- AVHWDeviceInternal *internal;
- /**
- * This field identifies the underlying API used for hardware access.
- *
- * This field is set when this struct is allocated and never changed
- * afterwards.
- */
- enum AVHWDeviceType type;
- /**
- * The format-specific data, allocated and freed by libavutil along with
- * this context.
- *
- * Should be cast by the user to the format-specific context defined in the
- * corresponding header (hwcontext_*.h) and filled as described in the
- * documentation before calling av_hwdevice_ctx_init().
- *
- * After calling av_hwdevice_ctx_init() this struct should not be modified
- * by the caller.
- */
- void *hwctx;
- /**
- * This field may be set by the caller before calling av_hwdevice_ctx_init().
- *
- * If non-NULL, this callback will be called when the last reference to
- * this context is unreferenced, immediately before it is freed.
- *
- * @note when other objects (e.g an AVHWFramesContext) are derived from this
- * struct, this callback will be invoked after all such child objects
- * are fully uninitialized and their respective destructors invoked.
- */
- void (*free)(struct AVHWDeviceContext *ctx);
- /**
- * Arbitrary user data, to be used e.g. by the free() callback.
- */
- void *user_opaque;
- } AVHWDeviceContext;
- typedef struct AVHWFramesInternal AVHWFramesInternal;
- /**
- * This struct describes a set or pool of "hardware" frames (i.e. those with
- * data not located in normal system memory). All the frames in the pool are
- * assumed to be allocated in the same way and interchangeable.
- *
- * This struct is reference-counted with the AVBuffer mechanism and tied to a
- * given AVHWDeviceContext instance. The av_hwframe_ctx_alloc() constructor
- * yields a reference, whose data field points to the actual AVHWFramesContext
- * struct.
- */
- typedef struct AVHWFramesContext {
- /**
- * A class for logging.
- */
- const AVClass *av_class;
- /**
- * Private data used internally by libavutil. Must not be accessed in any
- * way by the caller.
- */
- AVHWFramesInternal *internal;
- /**
- * A reference to the parent AVHWDeviceContext. This reference is owned and
- * managed by the enclosing AVHWFramesContext, but the caller may derive
- * additional references from it.
- */
- AVBufferRef *device_ref;
- /**
- * The parent AVHWDeviceContext. This is simply a pointer to
- * device_ref->data provided for convenience.
- *
- * Set by libavutil in av_hwframe_ctx_init().
- */
- AVHWDeviceContext *device_ctx;
- /**
- * The format-specific data, allocated and freed automatically along with
- * this context.
- *
- * Should be cast by the user to the format-specific context defined in the
- * corresponding header (hwframe_*.h) and filled as described in the
- * documentation before calling av_hwframe_ctx_init().
- *
- * After any frames using this context are created, the contents of this
- * struct should not be modified by the caller.
- */
- void *hwctx;
- /**
- * This field may be set by the caller before calling av_hwframe_ctx_init().
- *
- * If non-NULL, this callback will be called when the last reference to
- * this context is unreferenced, immediately before it is freed.
- */
- void (*free)(struct AVHWFramesContext *ctx);
- /**
- * Arbitrary user data, to be used e.g. by the free() callback.
- */
- void *user_opaque;
- /**
- * A pool from which the frames are allocated by av_hwframe_get_buffer().
- * This field may be set by the caller before calling av_hwframe_ctx_init().
- * The buffers returned by calling av_buffer_pool_get() on this pool must
- * have the properties described in the documentation in the corresponding hw
- * type's header (hwcontext_*.h). The pool will be freed strictly before
- * this struct's free() callback is invoked.
- *
- * This field may be NULL, then libavutil will attempt to allocate a pool
- * internally. Note that certain device types enforce pools allocated at
- * fixed size (frame count), which cannot be extended dynamically. In such a
- * case, initial_pool_size must be set appropriately.
- */
- AVBufferPool *pool;
- /**
- * Initial size of the frame pool. If a device type does not support
- * dynamically resizing the pool, then this is also the maximum pool size.
- *
- * May be set by the caller before calling av_hwframe_ctx_init(). Must be
- * set if pool is NULL and the device type does not support dynamic pools.
- */
- int initial_pool_size;
- /**
- * The pixel format identifying the underlying HW surface type.
- *
- * Must be a hwaccel format, i.e. the corresponding descriptor must have the
- * AV_PIX_FMT_FLAG_HWACCEL flag set.
- *
- * Must be set by the user before calling av_hwframe_ctx_init().
- */
- enum AVPixelFormat format;
- /**
- * The pixel format identifying the actual data layout of the hardware
- * frames.
- *
- * Must be set by the caller before calling av_hwframe_ctx_init().
- *
- * @note when the underlying API does not provide the exact data layout, but
- * only the colorspace/bit depth, this field should be set to the fully
- * planar version of that format (e.g. for 8-bit 420 YUV it should be
- * AV_PIX_FMT_YUV420P, not AV_PIX_FMT_NV12 or anything else).
- */
- enum AVPixelFormat sw_format;
- /**
- * The allocated dimensions of the frames in this pool.
- *
- * Must be set by the user before calling av_hwframe_ctx_init().
- */
- int width, height;
- } AVHWFramesContext;
- /**
- * Look up an AVHWDeviceType by name.
- *
- * @param name String name of the device type (case-insensitive).
- * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
- * not found.
- */
- enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
- /** 获取AVHWDeviceType的字符串名称。
- *
- * @param 枚举AVHWDeviceType中的type类型。
- * @return 指向包含名称的静态字符串的指针,如果类型无效,则为NULL。
- */
- const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
- /**
- * 遍历支持的设备类型。
- *
- * @param type 首先是AV_HWDEVICE_TYPE_NONE,然后是该函数在后续迭代中返回的前一个类型。
- * @return 枚举中下一个可用的设备类型为AVHWDeviceType,如果没有,则为AV_HWDEVICE_TYPE_NONE。
- */
- enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
- /**
- * Allocate an AVHWDeviceContext for a given hardware type.
- *
- * @param type the type of the hardware device to allocate.
- * @return a reference to the newly created AVHWDeviceContext on success or NULL
- * on failure.
- */
- AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type);
- /**
- * Finalize the device context before use. This function must be called after
- * the context is filled with all the required information and before it is
- * used in any way.
- *
- * @param ref a reference to the AVHWDeviceContext
- * @return 0 on success, a negative AVERROR code on failure
- */
- int av_hwdevice_ctx_init(AVBufferRef *ref);
- /**
- * 打开指定类型的设备,并为其创建AVHWDeviceContext。
- *
- * 这是一个方便的函数,旨在涵盖简单的情况。
- * 需要优化设备创建/管理的调用者应该手动打开设备,
- * 然后使用av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init()将其包装在AVHWDeviceContext中。
- *
- * The returned context is already initialized and ready for use, the caller
- * should not call av_hwdevice_ctx_init() on it. The user_opaque/free fields of
- * the created AVHWDeviceContext are set by this function and should not be
- * touched by the caller.
- *
- * @param device_ctx 如果成功,将在这里写入对新创建的设备上下文的引用。
- * 引用由调用者拥有,当不再需要时,必须使用av_buffer_unref()释放它。
- * 如果失败,NULL将写入此指针。
- * @param type 要创建的设备的类型。
- * @param device 要创建的设备的类型。标识要打开的设备的类型特定字符串。
- * @param opts 打开设备时使用的附加(类型特定)选项的字典。词典仍归调用者所有。
- * @param flags 当前未使用
- *
- * @return 成功时为0,失败时为负AVERROR代码。
- */
- int av_hwdevice_ctx_create(AVBufferRef **device_ctx, enum AVHWDeviceType type,
- const char *device, AVDictionary *opts, int flags);
- /**
- * Create a new device of the specified type from an existing device.
- *
- * If the source device is a device of the target type or was originally
- * derived from such a device (possibly through one or more intermediate
- * devices of other types), then this will return a reference to the
- * existing device of the same type as is requested.
- *
- * Otherwise, it will attempt to derive a new device from the given source
- * device. If direct derivation to the new type is not implemented, it will
- * attempt the same derivation from each ancestor of the source device in
- * turn looking for an implemented derivation method.
- *
- * @param dst_ctx On success, a reference to the newly-created
- * AVHWDeviceContext.
- * @param type The type of the new device to create.
- * @param src_ctx A reference to an existing AVHWDeviceContext which will be
- * used to create the new device.
- * @param flags Currently unused; should be set to zero.
- * @return Zero on success, a negative AVERROR code on failure.
- */
- int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ctx,
- enum AVHWDeviceType type,
- AVBufferRef *src_ctx, int flags);
- /**
- * Create a new device of the specified type from an existing device.
- *
- * This function performs the same action as av_hwdevice_ctx_create_derived,
- * however, it is able to set options for the new device to be derived.
- *
- * @param dst_ctx On success, a reference to the newly-created
- * AVHWDeviceContext.
- * @param type The type of the new device to create.
- * @param src_ctx A reference to an existing AVHWDeviceContext which will be
- * used to create the new device.
- * @param options Options for the new device to create, same format as in
- * av_hwdevice_ctx_create.
- * @param flags Currently unused; should be set to zero.
- * @return Zero on success, a negative AVERROR code on failure.
- */
- int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ctx,
- enum AVHWDeviceType type,
- AVBufferRef *src_ctx,
- AVDictionary *options, int flags);
- /**
- * Allocate an AVHWFramesContext tied to a given device context.
- *
- * @param device_ctx a reference to a AVHWDeviceContext. This function will make
- * a new reference for internal use, the one passed to the
- * function remains owned by the caller.
- * @return a reference to the newly created AVHWFramesContext on success or NULL
- * on failure.
- */
- AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ctx);
- /**
- * Finalize the context before use. This function must be called after the
- * context is filled with all the required information and before it is attached
- * to any frames.
- *
- * @param ref a reference to the AVHWFramesContext
- * @return 0 on success, a negative AVERROR code on failure
- */
- int av_hwframe_ctx_init(AVBufferRef *ref);
- /**
- * Allocate a new frame attached to the given AVHWFramesContext.
- *
- * @param hwframe_ctx a reference to an AVHWFramesContext
- * @param frame an empty (freshly allocated or unreffed) frame to be filled with
- * newly allocated buffers.
- * @param flags currently unused, should be set to zero
- * @return 0 on success, a negative AVERROR code on failure
- */
- int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags);
- /**
- * 将数据复制到硬件表面或从硬件表面复制数据。dst/src中必须至少有一个附加了AVHWFramesContext。
- *
- * 如果src附加了AVHWFramesContext,那么dst的格式(如果设置了)必须使用av_hwframe_transfer_get_formats(src, AV_HWFRAME_TRANSFER_DIRECTION_FROM)返回的格式之一。
- * 如果dst附加了AVHWFramesContext,那么src的格式必须使用av_hwframe_transfer_get_formats(dst, AV_HWFRAME_TRANSFER_DIRECTION_TO)返回的格式之一。
- *
- * DST可能是“干净的”(即data/buf指针未设置),在这种情况下,数据缓冲区将由该函数使用av_frame_get_buffer()分配。
- * 如果dst->格式已设置,则将使用此格式,否则(当dst->格式为AV_PIX_FMT_NONE时)将选择第一个可接受的格式。
- *
- * 两个帧必须有匹配的分配尺寸(即等于AVHWFramesContext.width/height),因为不是所有的设备类型都支持传输整个表面的子矩形。
- * 显示尺寸(即AVFrame.width/height)可以小于分配的尺寸,但也必须为两个帧相等。
- * 当显示尺寸小于分配的尺寸时,将不指定目标帧中的填充内容
- *
- * @param dst the destination frame. dst is not touched on failure.
- * @param src the source frame.
- * @param flags 当前未使用,应设置为零
- * @return 0 on success, a negative AVERROR error code on failure.
- */
- int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags);
- enum AVHWFrameTransferDirection {
- /**
- * Transfer the data from the queried hw frame.
- */
- AV_HWFRAME_TRANSFER_DIRECTION_FROM,
- /**
- * Transfer the data to the queried hw frame.
- */
- AV_HWFRAME_TRANSFER_DIRECTION_TO,
- };
- /**
- * Get a list of possible source or target formats usable in
- * av_hwframe_transfer_data().
- *
- * @param hwframe_ctx the frame context to obtain the information for
- * @param dir the direction of the transfer
- * @param formats the pointer to the output format list will be written here.
- * The list is terminated with AV_PIX_FMT_NONE and must be freed
- * by the caller when no longer needed using av_free().
- * If this function returns successfully, the format list will
- * have at least one item (not counting the terminator).
- * On failure, the contents of this pointer are unspecified.
- * @param flags currently unused, should be set to zero
- * @return 0 on success, a negative AVERROR code on failure.
- */
- int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ctx,
- enum AVHWFrameTransferDirection dir,
- enum AVPixelFormat **formats, int flags);
- /**
- * This struct describes the constraints on hardware frames attached to
- * a given device with a hardware-specific configuration. This is returned
- * by av_hwdevice_get_hwframe_constraints() and must be freed by
- * av_hwframe_constraints_free() after use.
- */
- typedef struct AVHWFramesConstraints {
- /**
- * A list of possible values for format in the hw_frames_ctx,
- * terminated by AV_PIX_FMT_NONE. This member will always be filled.
- */
- enum AVPixelFormat *valid_hw_formats;
- /**
- * A list of possible values for sw_format in the hw_frames_ctx,
- * terminated by AV_PIX_FMT_NONE. Can be NULL if this information is
- * not known.
- */
- enum AVPixelFormat *valid_sw_formats;
- /**
- * The minimum size of frames in this hw_frames_ctx.
- * (Zero if not known.)
- */
- int min_width;
- int min_height;
- /**
- * The maximum size of frames in this hw_frames_ctx.
- * (INT_MAX if not known / no limit.)
- */
- int max_width;
- int max_height;
- } AVHWFramesConstraints;
- /**
- * Allocate a HW-specific configuration structure for a given HW device.
- * After use, the user must free all members as required by the specific
- * hardware structure being used, then free the structure itself with
- * av_free().
- *
- * @param device_ctx a reference to the associated AVHWDeviceContext.
- * @return The newly created HW-specific configuration structure on
- * success or NULL on failure.
- */
- void *av_hwdevice_hwconfig_alloc(AVBufferRef *device_ctx);
- /**
- * Get the constraints on HW frames given a device and the HW-specific
- * configuration to be used with that device. If no HW-specific
- * configuration is provided, returns the maximum possible capabilities
- * of the device.
- *
- * @param ref a reference to the associated AVHWDeviceContext.
- * @param hwconfig a filled HW-specific configuration structure, or NULL
- * to return the maximum possible capabilities of the device.
- * @return AVHWFramesConstraints structure describing the constraints
- * on the device, or NULL if not available.
- */
- AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
- const void *hwconfig);
- /**
- * Free an AVHWFrameConstraints structure.
- *
- * @param constraints The (filled or unfilled) AVHWFrameConstraints structure.
- */
- void av_hwframe_constraints_free(AVHWFramesConstraints **constraints);
- /**
- * Flags to apply to frame mappings.
- */
- enum {
- /**
- * The mapping must be readable.
- */
- AV_HWFRAME_MAP_READ = 1 << 0,
- /**
- * The mapping must be writeable.
- */
- AV_HWFRAME_MAP_WRITE = 1 << 1,
- /**
- * The mapped frame will be overwritten completely in subsequent
- * operations, so the current frame data need not be loaded. Any values
- * which are not overwritten are unspecified.
- */
- AV_HWFRAME_MAP_OVERWRITE = 1 << 2,
- /**
- * The mapping must be direct. That is, there must not be any copying in
- * the map or unmap steps. Note that performance of direct mappings may
- * be much lower than normal memory.
- */
- AV_HWFRAME_MAP_DIRECT = 1 << 3,
- };
- /**
- * Map a hardware frame.
- *
- * This has a number of different possible effects, depending on the format
- * and origin of the src and dst frames. On input, src should be a usable
- * frame with valid buffers and dst should be blank (typically as just created
- * by av_frame_alloc()). src should have an associated hwframe context, and
- * dst may optionally have a format and associated hwframe context.
- *
- * If src was created by mapping a frame from the hwframe context of dst,
- * then this function undoes the mapping - dst is replaced by a reference to
- * the frame that src was originally mapped from.
- *
- * If both src and dst have an associated hwframe context, then this function
- * attempts to map the src frame from its hardware context to that of dst and
- * then fill dst with appropriate data to be usable there. This will only be
- * possible if the hwframe contexts and associated devices are compatible -
- * given compatible devices, av_hwframe_ctx_create_derived() can be used to
- * create a hwframe context for dst in which mapping should be possible.
- *
- * If src has a hwframe context but dst does not, then the src frame is
- * mapped to normal memory and should thereafter be usable as a normal frame.
- * If the format is set on dst, then the mapping will attempt to create dst
- * with that format and fail if it is not possible. If format is unset (is
- * AV_PIX_FMT_NONE) then dst will be mapped with whatever the most appropriate
- * format to use is (probably the sw_format of the src hwframe context).
- *
- * A return value of AVERROR(ENOSYS) indicates that the mapping is not
- * possible with the given arguments and hwframe setup, while other return
- * values indicate that it failed somehow.
- *
- * On failure, the destination frame will be left blank, except for the
- * hw_frames_ctx/format fields thay may have been set by the caller - those will
- * be preserved as they were.
- *
- * @param dst Destination frame, to contain the mapping.
- * @param src Source frame, to be mapped.
- * @param flags Some combination of AV_HWFRAME_MAP_* flags.
- * @return Zero on success, negative AVERROR code on failure.
- */
- int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags);
- /**
- * Create and initialise an AVHWFramesContext as a mapping of another existing
- * AVHWFramesContext on a different device.
- *
- * av_hwframe_ctx_init() should not be called after this.
- *
- * @param derived_frame_ctx On success, a reference to the newly created
- * AVHWFramesContext.
- * @param derived_device_ctx A reference to the device to create the new
- * AVHWFramesContext on.
- * @param source_frame_ctx A reference to an existing AVHWFramesContext
- * which will be mapped to the derived context.
- * @param flags Some combination of AV_HWFRAME_MAP_* flags, defining the
- * mapping parameters to apply to frames which are allocated
- * in the derived device.
- * @return Zero on success, negative AVERROR code on failure.
- */
- int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
- enum AVPixelFormat format,
- AVBufferRef *derived_device_ctx,
- AVBufferRef *source_frame_ctx,
- int flags);
- #endif /* AVUTIL_HWCONTEXT_H */
|