# 创建 `superresolution` 插件的步骤

Source: [https://docs.qualcomm.com/doc/80-70015-15BY/topic/steps-to-create-superresolution-plugin.html](https://docs.qualcomm.com/doc/80-70015-15BY/topic/steps-to-create-superresolution-plugin.html)

1. 创建插件头文件"gst-plugins-mlvsuperresolution/mlvsuperresolution.h"
    - 来自现有插件的参考： [gst-plugin-mlvsegmentation/mlvsegmentation.h · imsdk.lnx.2.0.0.r2-rel · CodeLinaro / le / platform / vendor / qcom-opensource / gst-plugins-qti-oss · GitLab](https://git.codelinaro.org/clo/le/platform/vendor/qcom-opensource/gst-plugins-qti-oss/-/blob/imsdk.lnx.2.0.0.r2-rel/gst-plugin-mlvsegmentation/mlvsegmentation.h?ref_type=heads)
    - 导入所需的头文件
    - 为后续处理创建一个包含所需数据变量的结构体
        超分辨率示例 - 继承链：GObject 	 GstObject 	 GstElement 	 GstBaseTransform - GstBaseTransform 适用于输出大小和功能完全由输入大小和功能决定的元素

            struct _GstMLVideoSuperresolution {
                                  GstBaseTransform  parent;
                                  GstMLInfo          *mlinfo;
                                  GstVideoInfo      *vinfo;
                                  GstBufferPool     *outpool;
                                  GstMLModule    *module;
                                  gint                       mdlenum;
                                  GstStructure       *mlconstants;
            };
                      
            struct _GstMLVideoSuperresolutionClass {
                                  GstBaseTransformClass parent;
            };Copy to clipboard

        **注：**插件 superresolution 无需标签变量。确保在代码中删除标签变量及其引用。
2. 创建插件"gst-plugins-mlvsuperresolution/mlvsuperresolution.c"
    - 来自现有插件的参考： [gst-plugin-mlvsegmentation/mlvsegmentation.c · imsdk.lnx.2.0.0.r2-rel · CodeLinaro / le / platform / vendor / qcom-opensource / gst-plugins-qti-oss · GitLab](https://git.codelinaro.org/clo/le/platform/vendor/qcom-opensource/gst-plugins-qti-oss/-/blob/imsdk.lnx.2.0.0.r2-rel/gst-plugin-mlvsegmentation/mlvsegmentation.c?ref_type=heads)
    - 导入 mlvsuperresolution.h 和所需的其他头文件
    - 定义 caps
        - 视频格式

                #define GST_ML_VIDEO_SUPERRESOLUTION_VIDEO_FORMATS "{ RGBA,BGRA, ARGB, ABGR, RGBx, BGRx, xRGB, xBGR, RGB, BGR }"Copy to clipboard
        - 源端 caps

                #define GST_ML_VIDEO_SUPER_RESOLUTION_SRC_CAPS                            \
                    "video/x-raw, "                                                       \
                    "format = (string) " GST_ML_VIDEO_SUPER_RESOLUTION_VIDEO_FORMATS "; " \
                    "video/x-raw(" GST_CAPS_FEATURE_MEMORY_GBM "), "                      \
                    "format = (string) " GST_ML_VIDEO_SUPER_RESOLUTION_VIDEO_FORMATSCopy to clipboard
        - 接收端 caps

                #define GST_ML_VIDEO_SUPER_RESOLUTION_SINK_CAPS \
                        "neural-network/tensors"Copy to clipboard
    - 实现以下函数
        - 初始化

                static gboolean plugin_init(GstPlugin * plugin);Copy to clipboard

            - 如需注册定制插件，则如下所示。注册完成后，就可以使用 gst\_element\_factory\_make("plugin-name","plugin-name") 访问示例程序中的插件；

                static void gst_ml_video_super_resolution_init (GstMLVideoSuperResolution * super_resolution)            Copy to clipboard

            - 使用默认参数初始化 superresolution 插件，允许控制输出缓冲区。在接收到输入缓冲区时，将调用 gst\_ml\_video\_super\_resolution\_prepare\_output\_buffer 来分配输出缓冲区。

                static void gst_ml_video_super_resolution_class_init (GstMLVideoSuperResolutionClass * klass)Copy to clipboard

            - 将回调函数初始化为函数指针，设置 caps 和默认属性
        - 缓冲区管理

                static GstBufferPool * gst_ml_video_super_resolution_create_pool (GstMLVideoSuperResolution * super_resolution, 
                                                                                  GstCaps * caps)Copy to clipboard

            - 根据协商的 caps 分配 GBM（图形缓冲区管理器）或 ION 内存

                gst_ml_video_super_resolution_decide_allocation (GstBaseTransform * base, GstQuery * query)Copy to clipboard

            - 确保根据 caps 配置适当的缓冲池。

                static GstFlowReturn gst_ml_video_super_resolution_prepare_output_buffer (GstBaseTransform * base,
                                                                                          GstBuffer * inbuffer, 
                                                                                          GstBuffer ** outbuffer)Copy to clipboard

            - 确保根据 caps 从缓冲池中分配输出缓冲区
        - Caps

                static gboolean gst_ml_video_super_resolution_set_caps (GstBaseTransform * base, 
                                                                        GstCaps * incaps,
                                                                        GstCaps * outcaps)Copy to clipboard

            - 首先，对模块进行验证，然后从模块 caps 中收集输入和输出的 capabilities。

                static GstCaps * gst_ml_video_super_resolution_fixate_caps (GstBaseTransform * base,
                                                                            GstPadDirection direction, 
                                                                            GstCaps * incaps, 
                                                                            GstCaps * outcaps)Copy to clipboard

            - 经过协商的 caps 是固定的

                static gboolean caps_has_feature (const GstCaps * caps, 
                                                  const gchar * feature)Copy to clipboard

            - 检查指定的 caps 是否有效，如 video/x-raw、video/x-text、格式、高度和宽度

                static GstCaps * gst_ml_video_super_resolution_src_caps (void)Copy to clipboard

            - 将静态 caps 转换为常规 caps

                static GstCaps * gst_ml_video_super_resolution_sink_caps (void)Copy to clipboard

            - 将静态 caps 转换为常规 caps
        - 转换

                static GstCaps * gst_ml_video_super_resolution_transform_caps (GstBaseTransform * base,
                                                                               GstPadDirection direction, 
                                                                               GstCaps * caps, 
                                                                               GstCaps * filter)Copy to clipboard

            - 将接收端 caps 转换为源端 caps，反之亦然

                gst_ml_video_super_resolution_transform (GstBaseTransform * base,
                                                         GstBuffer * inbuffer, 
                                                         GstBuffer * outbuffer)Copy to clipboard

            - 推理完成后，会根据模块调用后处理函数 gst\_ml\_video\_superresolution\_module\_execute。
        - 属性

                static void gst_ml_video_super_resolution_set_property (GObject * object, 
                                                                        guint prop_id, 
                                                                        const GValue * value,  
                                                                        GParamSpec * pspec)Copy to clipboard

            - 设置插件属性，如模块和常量

                static void gst_ml_video_super_resolution_get_property (GObject * object, 
                                                                        guint prop_id, 
                                                                        GValue * value, 
                                                                        GParamSpec * pspec)Copy to clipboard

            - 读取插件属性，如模块和常量
        - 反初始化

                static void gst_ml_video_super_resolution_finalize (GObject * object)Copy to clipboard

            - 释放已分配的缓冲区。
        - 显示文本/边界框（仅基于用例，超分辨率用例无需此操作）

                static gboolean gst_ml_video_<plugin-name>_fill_video_output(GstMLVideoDetection * detection, GArray * predictions, GstBuffer * buffer);Copy to clipboard

            - 在输出帧上叠加结果
        - 更新"gst-plugins-mlvsuperresolution/CMakeLists.txt"中的插件名称
            - 来自现有插件的参考： [gst-plugin-mlvsegmentation/CMakeLists.txt · imsdk.lnx.2.0.0.r2-rel · CodeLinaro / le / platform / vendor / qcom-opensource / gst-plugins-qti-oss · GitLab](https://git.codelinaro.org/clo/le/platform/vendor/qcom-opensource/gst-plugins-qti-oss/-/blob/imsdk.lnx.2.0.0.r2-rel/gst-plugin-mlvsegmentation/CMakeLists.txt?ref_type=heads)
            - 从 [gst-plugin-mlvsegmentation/config.h.in ·
                                            imsdk.lnx.2.0.0.r2-rel · CodeLinaro / le / platform /
                                            vendor / qcom-opensource / gst-plugins-qti-oss · GitLab](https://git.codelinaro.org/clo/le/platform/vendor/qcom-opensource/gst-plugins-qti-oss/-/blob/imsdk.lnx.2.0.0.r2-rel/gst-plugin-mlvsegmentation/config.h.in?ref_type=heads)重用config.h.in

Last Published: Jan 26, 2026

[Previous Topic
插件目录结构](https://docs.qualcomm.com/bundle/publicresource/80-70015-15BY/topics/plugin-directory-structure.md) [Next Topic
模块概述](https://docs.qualcomm.com/bundle/publicresource/80-70015-15BY/topics/overview-of-module.md)