FFmpeg之HWContextType

HWContextType算是ffmpeg中为硬解码第三方接口的一个辅助类,它自己有两个辅助子类
AVHWDeviceContext和AVHWFramesContext。
AVHWDeviceContext主要表示硬件上下文
AVHWFramesContext主要表示硬件Frame的一些参数,比如你解码后的YUV数据还在硬件上,那么就通过这个类去存储相关参数。

那么它辅助硬解码器做什么呢?我们还是通过英伟达的例子来看

const HWContextType ff_hwcontext_type_cuda = {.type                 = AV_HWDEVICE_TYPE_CUDA,.name                 = "CUDA",.device_hwctx_size    = sizeof(AVCUDADeviceContext),.frames_priv_size     = sizeof(CUDAFramesContext),.device_create        = cuda_device_create,.device_derive        = cuda_device_derive,.device_init          = cuda_device_init,.device_uninit        = cuda_device_uninit,.frames_get_constraints = cuda_frames_get_constraints,.frames_init          = cuda_frames_init,.frames_get_buffer    = cuda_get_buffer,.transfer_get_formats = cuda_transfer_get_formats,.transfer_data_to     = cuda_transfer_data,.transfer_data_from   = cuda_transfer_data,.pix_fmts             = (const enum AVPixelFormat[]){ AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE },
};

仔细看上面函数就发现,全部是device mem操作,大白话说就是ffmpeg通过这套机制来实现D2H或者H2D的操作,别无其它。

具体看看结构体定义吧,一大堆函数指针,这些就是你要实现的,不一定全部要实现,实现你自己想要的就可以了,主要的有transfer_data_to/transfer_data_from,map_to/map_from。


typedef struct HWContextType {enum AVHWDeviceType type;const char         *name;/*** An array of pixel formats supported by the AVHWFramesContext instances* Terminated by AV_PIX_FMT_NONE.*/const enum AVPixelFormat *pix_fmts;/*** size of the public hardware-specific context,* i.e. AVHWDeviceContext.hwctx*/size_t             device_hwctx_size;/*** size of the private data, i.e.* AVHWDeviceInternal.priv*/size_t             device_priv_size;/*** Size of the hardware-specific device configuration.* (Used to query hwframe constraints.)*/size_t             device_hwconfig_size;/*** size of the public frame pool hardware-specific context,* i.e. AVHWFramesContext.hwctx*/size_t             frames_hwctx_size;/*** size of the private data, i.e.* AVHWFramesInternal.priv*/size_t             frames_priv_size;int              (*device_create)(AVHWDeviceContext *ctx, const char *device,AVDictionary *opts, int flags);int              (*device_derive)(AVHWDeviceContext *dst_ctx,AVHWDeviceContext *src_ctx,AVDictionary *opts, int flags);int              (*device_init)(AVHWDeviceContext *ctx);void             (*device_uninit)(AVHWDeviceContext *ctx);int              (*frames_get_constraints)(AVHWDeviceContext *ctx,const void *hwconfig,AVHWFramesConstraints *constraints);int              (*frames_init)(AVHWFramesContext *ctx);void             (*frames_uninit)(AVHWFramesContext *ctx);int              (*frames_get_buffer)(AVHWFramesContext *ctx, AVFrame *frame);int              (*transfer_get_formats)(AVHWFramesContext *ctx,enum AVHWFrameTransferDirection dir,enum AVPixelFormat **formats);int              (*transfer_data_to)(AVHWFramesContext *ctx, AVFrame *dst,const AVFrame *src);int              (*transfer_data_from)(AVHWFramesContext *ctx, AVFrame *dst,const AVFrame *src);int              (*map_to)(AVHWFramesContext *ctx, AVFrame *dst,const AVFrame *src, int flags);int              (*map_from)(AVHWFramesContext *ctx, AVFrame *dst,const AVFrame *src, int flags);int              (*frames_derive_to)(AVHWFramesContext *dst_ctx,AVHWFramesContext *src_ctx, int flags);int              (*frames_derive_from)(AVHWFramesContext *dst_ctx,AVHWFramesContext *src_ctx, int flags);
} HWContextType;

AVHWFramesContext结构体表示


/*** 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;

AVHWDeviceContext的结构体表示,硬件的抽象


/*** 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;

常用函数

这些函数其实挺重要的,因为都是用户函数,通过这些函数去从解码器硬件上获取到你要的数据,比如av_hwframe_transfer_data/av_hwframe_get_buffer,就是说你在ffmpeg中不要显示的调用你自己的cuMemD2H或者H2D的函数,ffmpeg已经给你预留好了接口了,用这些接口去获取更加简单。


/*** 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);/** Get the string name of an AVHWDeviceType.** @param type Type from enum AVHWDeviceType.* @return Pointer to a static string containing the name, or NULL if the type*         is not valid.*/
const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);/*** Iterate over supported device types.** @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type*             returned by this function in subsequent iterations.* @return The next usable device type from enum AVHWDeviceType, or*         AV_HWDEVICE_TYPE_NONE if there are no more.*/
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);/*** Open a device of the specified type and create an AVHWDeviceContext for it.** This is a convenience function intended to cover the simple cases. Callers* who need to fine-tune device creation/management should open the device* manually and then wrap it in an AVHWDeviceContext using* av_hwdevice_ctx_alloc()/av_hwdevice_ctx_init().** 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 On success, a reference to the newly-created device context*                   will be written here. The reference is owned by the caller*                   and must be released with av_buffer_unref() when no longer*                   needed. On failure, NULL will be written to this pointer.* @param type The type of the device to create.* @param device A type-specific string identifying the device to open.* @param opts A dictionary of additional (type-specific) options to use in*             opening the device. The dictionary remains owned by the caller.* @param flags currently unused** @return 0 on success, a negative AVERROR code on failure.*/
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);/*** Copy data to or from a hw surface. At least one of dst/src must have an* AVHWFramesContext attached.** If src has an AVHWFramesContext attached, then the format of dst (if set)* must use one of the formats returned by av_hwframe_transfer_get_formats(src,* AV_HWFRAME_TRANSFER_DIRECTION_FROM).* If dst has an AVHWFramesContext attached, then the format of src must use one* of the formats returned by av_hwframe_transfer_get_formats(dst,* AV_HWFRAME_TRANSFER_DIRECTION_TO)** dst may be "clean" (i.e. with data/buf pointers unset), in which case the* data buffers will be allocated by this function using av_frame_get_buffer().* If dst->format is set, then this format will be used, otherwise (when* dst->format is AV_PIX_FMT_NONE) the first acceptable format will be chosen.** The two frames must have matching allocated dimensions (i.e. equal to* AVHWFramesContext.width/height), since not all device types support* transferring a sub-rectangle of the whole surface. The display dimensions* (i.e. AVFrame.width/height) may be smaller than the allocated dimensions, but* also have to be equal for both frames. When the display dimensions are* smaller than the allocated dimensions, the content of the padding in the* destination frame is unspecified.** @param dst the destination frame. dst is not touched on failure.* @param src the source frame.* @param flags currently unused, should be set to zero* @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);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/221343.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

el-form表单校验输入框值为0时 提示校验不通过

el-form表单校验输入框值为0时提示校验不通过 配置validator自定义校验方法 这里举例在结构代码里加入校验规则 <el-form-item:prop"num":rules"[{required: true,message: 请输入数量,trigger: change,},{validator,trigger: blur}]" ><el-inpu…

软件工程:软件需求规格说明书

常用软件需求规格说明书模板 ISO2006模板示例 示例1

IP属地变化背后的原因

随着互联网的普及和技术的不断发展&#xff0c;IP属地变化的现象越来越受到人们的关注。近日&#xff0c;有网友发现自己的IP属地发生了变化&#xff0c;引发了广泛讨论。那么&#xff0c;IP属地为什么会发生变化呢&#xff1f; 首先&#xff0c;网络环境的变化是导致IP属地变化…

uniapp 之 图片 视频 文件上传

<view class"" style"padding: 24rpx 0"><text>相关资料 <text class"fs-26 color-666">&#xff08;图片、视频、文档不超过9个&#xff09;</text> </text><view class"flex align-center" style&…

Python中的内存泄漏及其检测方法

一、引言 内存泄漏是编程中常见的问题之一&#xff0c;它会导致程序在运行过程中不断消耗内存&#xff0c;最终可能导致程序崩溃或性能下降。在Python中&#xff0c;内存泄漏也是一个需要关注的问题。本文将详细介绍Python中的内存泄漏及其检测方法&#xff0c;以帮助读者更好…

转发一篇CAN过滤器配置的文章

一&#xff1a;转发链接 “目前网络上看到CAN过滤器讲得最详细的文章” 二&#xff1a;CAN过滤器是CAN总线系统中的一种设备&#xff0c;它用于过滤和选择总线上的数据帧。 在CAN总线系统中&#xff0c;每个设备都可以发送和接收数据帧。然而&#xff0c;在某些情况下&#xf…

亚马逊云科技re_Invent 2023产品体验:亚马逊云科技产品应用实践 王炸产品Amazon Q,你的AI助手

本篇文章授权活动官方亚马逊云科技文章转发、改写权&#xff0c;包括不限于在 亚马逊云科技开发者社区, 知乎&#xff0c;自媒体平台&#xff0c;第三方开发者媒体等亚马逊云科技官方渠道 意料之中 2023年9月25日&#xff0c;亚马逊宣布与 Anthropic 正式展开战略合作&#x…

蓝牙指纹定位技术介绍以及代码演示

蓝牙指纹定位技术 蓝牙指纹定位技术是一种基于蓝牙信号强度&#xff08;Bluetooth Signal Strength&#xff09;来进行位置定位的方法。这种技术主要应用于室内定位系统&#xff08;Indoor Positioning System, IPS&#xff09;&#xff0c;因为室内环境对GPS信号的阻隔导致其…

Globalsign—— SSL证书中的LV

一&#xff1a;SSL证书 SSL 证书可以实现网站的 https 加密&#xff0c;保证从客户端到服务端传输的数据是加密的。越来越多的网络信息泄露事件也给我们敲响了警钟&#xff0c;信息安全不容小觑。网站建设者们也应该要把网络信息安全放在首位&#xff0c;给网站部署 SSL …

第一个“hello Android”程序

1、首先安装Android studio&#xff08;跳过&#xff09; Android Studio是由Google推出的官方集成开发环境&#xff08;IDE&#xff09;&#xff0c;专门用于Android应用程序的开发。它是基于JetBrains的IntelliJ IDEA IDE构建的&#xff0c;提供了丰富的功能和工具&#xff0…

8V-24V升降12V2A升降压芯片WT3205

8V-24V升降12V2A升降压芯片WT3205 WT3205是一款专为升压开关电源设计的DC-DC直流转换控制器。 WT3205的输入电压范围是5V至32v&#xff0c;电路元器件少&#xff0c;应用简单。WT3205采用固定频率的PWM控制方式&#xff0c;330KHz的振荡器&#xff0c;电流模式控制单元&#x…

_pickle.PicklingError: Can‘t pickle : import of module failed

有问题 没问题的 python - pickle cant import a module that exists? - Stack Overflow

Apifox 最新更新:迭代分支功能上线、在线文档支持多格式导出!

Apifox 新版本上线啦&#xff01; 看看本次版本更新主要涵盖的重点内容&#xff0c;有没有你所关注的功能特性&#xff1a; HTTP 项目新增「迭代分支」功能支持通过数据库表直接生成 API 文档的数据模型在线文档支持多方式导出用户反馈问题优化 数据库支持「测试连接」保持自…

可数集合(可列集合、可列无限集)

凡是和全体正整数所构成的集合对等的集合都称为可数集合、或者叫可列集合、可列无限集。 由于可以按大小顺序排成一个无穷序列&#xff0c;因此一个集合A是可数集合的充要条件为&#xff1a;A可以排成一个无穷序列 可数集合是无限集合。

NorFlash 知识点总结

一、介绍 NorFlash&#xff08;也称为 NOR 型闪存&#xff09;是一种非易失性存储器&#xff0c;常用于嵌入式系统和存储设备中。NorFlash 是一种闪存类型&#xff0c;可以用于存储程序代码、固件、操作系统以及其他数据。与 NAND Flash 相比&#xff0c;NorFlash 具有较低的存…

8性能测试

性能测试 jmeter &#xff08;大量用户&#xff09; 效率分为 时间 &#xff08;处理请求&#xff09; 资源&#xff08;占用cpu 内存 磁盘&#xff09; 性能测试概念:使用自动化工具&#xff0c;模拟不同的场景&#xff0c;对软件各项性能指标进行测试和评估的过程 性…

【infiniband】ibdump抓包

ibdump用于捕获和转储InfiniBand网络的流量。 这种工具通常用于调试和分析InfiniBand网络问题&#xff0c;包括性能瓶颈和配置错误。ibdump工具在Mellanox InfiniBand环境中较为常用&#xff0c;现由NVIDIA提供支持。 使用ibdump的基本步骤 请注意&#xff0c;您需要在安装了…

详解接口测试

目录 什么是接口&#xff1f; 接口协议的类型 接口测试是什么 HTTP接口的测试用例设计 HTTP接口的测试方法 什么是接口&#xff1f; 在面向对象编程中&#xff0c;接口是一个抽象的概念&#xff0c;用于定义类应该具有的方法和属性。一个类可以实现一个或多个接口&#xf…

【Jmeter】Jmeter基础8-Jmeter元件介绍之断言

断言主要用于对服务器响应的数据做验证。Jmeter提供了多个断言元件&#xff0c;其中最常用的是响应断言。 2.8.1、响应断言 作用&#xff1a;对Jmeter取样器返回值进行断言。参数说明&#xff1a; 测试字段 响应文本&#xff1a;从服务器返回的响应文本&#xff0c;Response B…

大数据云计算之OpenStack

大数据云计算之OpenStack 1.什么是OpenStack&#xff0c;其作用是什么&#xff1f;OpenStack主要的组成模块有哪些&#xff1f;各自的主要作用是什么&#xff1f; OpenStack是一个开源的云计算平台&#xff0c;旨在为企业和服务提供商提供私有云和公有云的建设和管理解决方案…