# 创建 `srnet` 模块的步骤

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

1. 创建模块头“gst-plugins-qti-oss/gst-plugin-base/gst/ml/ml-module-video-super-resolution.h”
    - 参考现有插件： [st-plugin-base/gst/ml/ml-module-video-super-resolution.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-base/gst/ml/ml-module-video-super-resolution.h?ref_type=heads)
    - 导入所需的 gst 头文件
    - 创建

            GST_API gboolean gst_ml_module_video_super_resolution_execute (GstMLModule * module,
                                                                           GstMLFrame * mlframe,
                                                                           GstVideoFrame * vframe);Copy to clipboard

        - 在调用后处理函数 (gst\_ml\_module\_process) 时需要使用该函数，该后处理函数在 ml-vsuperresolution-&lt;module-name&gt;.c 中定义
2. 创建模块"gst-plugins-mlvsuperresolution/modules/ml-vsuperresolution-srnet.c"
    - 参考现有插件： [gst-plugin-mlvsegmentation/modules/ml-vsegmentation-deeplab-argmax.c · imsdk.lnx.2.0.0.r1-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.r1-rel/gst-plugin-mlvsegmentation/modules/ml-vsegmentation-deeplab-argmax.c?ref_type=heads)

    - 导入"modules/ml-video-superresolution-module.h"头文件
    - 定义模型输出 caps

            #define GST_ML_MODULE_CAPS \
                "neural-network/tensors, " \
                "type = (string) { INT8, UINT8, INT32, FLOAT32 }, " \
                "dimensions = (int) < <1, [32, 4096], [32, 4096]> >; " \
                "neural-network/tensors, " \
                "type = (string) { INT8, UINT8, INT32, FLOAT32 }, " \
                "dimensions = (int) < <1, [32, 4096], [32, 4096], [1, 3]> >"Copy to clipboard

        **例如**

        - 从 AI Hub 下载 [QuickSRNetLarge-Quantized Qualcomm AI Hub](https://aihub.qualcomm.com/iot/models/quicksrnetlarge_quantized) 模型
        - 检查 TFLite 模型的输出尺寸（参见 [在应用程序中集成 AI Hub 模型](https://docs.qualcomm.com/doc/80-70015-15BY/topic/integrate-aihub-model.html)章节）
            - 格式：dtype[N x H X W X C]

        - 在以上模块输出 caps &lt;1, [32, 4096], [32, 4096], [1, 3]&gt; 中
            - N = 1 -&gt; 模型输出 Batch 处理大小
            - H = [32,4096] -&gt; 模型输出高度应在此范围内
            - W = [32,4096] -&gt; 模型输出宽度应在此范围内
            - C = [1,3] -&gt; 模型输出通道数应在此范围内
            - dtype = int8（量化模型）| float32（非量化模型）

        - 对于量化模型，检查输出节点的缩放系数和偏置
            - 格式：scale\*(q-offset)
    - 实现以下函数
        - 初始化
            - `gpointer gst_ml_module_open (void);`

        - 反初始化
            - `void gst_ml_module_close (gpointer instance);`

        - Caps
            - `GstCaps *gst_ml_module_caps (void);`

        - 配置
            - `gboolean gst_ml_module_configure (gpointer instance, GstStructure * settings);`
                - 检查模块的数据格式、缩放系数和偏置

        - 反量化

                gdouble gst_ml_tensor_extract_value (GstMLType mltype, 
                                                     gpointer data, 
                                                     guint idx,
                                                     gdouble offset, 
                                                     gdouble scale)Copy to clipboard

            - 需要传递给量化模型的缩放系数和偏置值

        - 后处理
            - `gboolean gst_ml_module_process (gpointer instance, GstMLFrame*mlframe, gpointer output);`
        - 推理缓冲区

            - `indata = GST_ML_FRAME_BLOCK_DATA (mlframe, 0);`

            用后处理数据填充的输出缓冲区

            - `outdata = GST_VIDEO_FRAME_PLANE_DATA (vframe, 0);`

            模型的数据类型

            - `mltype = GST_ML_FRAME_TYPE (mlframe);`

            后处理（用于超分辨率）

                  for (row = 0; row < GST_VIDEO_FRAME_HEIGHT (vframe); row++) {
                    for (column = 0; column < GST_VIDEO_FRAME_WIDTH (vframe); column++) {
                      // Calculate the destination index.
                      idx = (((row * GST_VIDEO_FRAME_WIDTH (vframe)) + column) * bpp) +
                          (row * padding);
                
                      outdata[idx] = gst_ml_tensor_extract_value (mltype, indata, idx,
                            submodule->qoffsets[0], submodule->qscales[0]);
                      outdata[idx + 1] = gst_ml_tensor_extract_value (mltype, indata, (idx + 1),
                            submodule->qoffsets[0], submodule->qscales[0]);
                      outdata[idx + 2] = gst_ml_tensor_extract_value (mltype, indata, (idx + 2),
                            submodule->qoffsets[0], submodule->qscales[0]);
                
                      // If output has an alpha channel set it to opaque.
                      if (bpp == 4)
                        outdata[idx + 3] = 0xFF;
                    }
                  }Copy to clipboard

    - 更新"gst-plugins-mlvsuperresolution/modules/CMakeLists.txt"中的模块名称
        - 来自现有插件的参考： [gst-plugin-mlvsegmentation/modules/CMakeLists.txt · imsdk.lnx.2.0.0.r1-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.r1-rel/gst-plugin-mlvsegmentation/modules/CMakeLists.txt?ref_type=heads)

Last Published: Jan 26, 2026

[Previous Topic
模块概述](https://docs.qualcomm.com/bundle/publicresource/80-70015-15BY/topics/overview-of-module.md) [Next Topic
更新 BitBake 文件以进行插件编译的步骤](https://docs.qualcomm.com/bundle/publicresource/80-70015-15BY/topics/steps-to-update-bitbake-for-compilation.md)