Skip to content

MPP_FormatTool

SweerItTer edited this page Feb 1, 2026 · 3 revisions

FormatTool API 文档

概述

FormatTool 是 utilsCore MPP 模块的工具函数集合,提供 DRM 和 MPP 格式之间的转换功能。

职责

  • DRM 格式转 MPP 格式
  • MPP 格式转 DRM 格式

适用场景

  • 格式配置
  • 跨模块格式转换
  • 显示和编码格式匹配

依赖关系

  • 依赖: Rockchip MPP 库
  • 依赖: DRM 格式定义
  • 被依赖: EncoderContext、JpegEncoder 等模块

静态方法

convertDrmToMppFormat() - DRM 格式转 MPP 格式

inline MppFrameFormat convertDrmToMppFormat(uint32_t drm_fmt);

参数说明:

  • drm_fmt (输入): DRM 格式(如 DRM_FORMAT_NV12)

返回值: MPP 格式

所有权归属:

  • 无所有权转移

注意事项:

  1. 使用 unordered_map 作为查找表
  2. 不支持的格式默认返回 MPP_FMT_YUV420SP
  3. 线程安全

使用例程:

// DRM NV12 转 MPP 格式
MppFrameFormat mpp_fmt = convertDrmToMppFormat(DRM_FORMAT_NV12);
printf("MPP format: 0x%x\n", mpp_fmt);  // 输出: MPP_FMT_YUV420SP

// DRM RGB888 转 MPP 格式
mpp_fmt = convertDrmToMppFormat(DRM_FORMAT_RGB888);
printf("MPP format: 0x%x\n", mpp_fmt);  // 输出: MPP_FMT_RGB888

convertMppToDrmFormat() - MPP 格式转 DRM 格式

inline uint32_t convertMppToDrmFormat(MppFrameFormat mpp_fmt);

参数说明:

  • mpp_fmt (输入): MPP 格式(如 MPP_FMT_YUV420SP)

返回值: DRM 格式

所有权归属:

  • 无所有权转移

注意事项:

  1. 使用 unordered_map 作为查找表
  2. 不支持的格式默认返回 DRM_FORMAT_NV12
  3. 线程安全

使用例程:

// MPP YUV420SP 转 DRM 格式
uint32_t drm_fmt = convertMppToDrmFormat(MPP_FMT_YUV420SP);
printf("DRM format: 0x%x\n", drm_fmt);  // 输出: DRM_FORMAT_NV12

// MPP RGB888 转 DRM 格式
drm_fmt = convertMppToDrmFormat(MPP_FMT_RGB888);
printf("DRM format: 0x%x\n", drm_fmt);  // 输出: DRM_FORMAT_RGB888

支持的格式转换

DRM 到 MPP 转换表

DRM 格式 MPP 格式
DRM_FORMAT_NV12 MPP_FMT_YUV420SP
DRM_FORMAT_NV21 MPP_FMT_YUV420SP
DRM_FORMAT_YUV420 MPP_FMT_YUV420P
DRM_FORMAT_YVU420 MPP_FMT_YUV420P
DRM_FORMAT_NV16 MPP_FMT_YUV422SP
DRM_FORMAT_NV61 MPP_FMT_YUV422SP
DRM_FORMAT_YUYV MPP_FMT_YUV422_YUYV
DRM_FORMAT_UYVY MPP_FMT_YUV422_UYVY
DRM_FORMAT_RGB565 MPP_FMT_RGB565
DRM_FORMAT_BGR565 MPP_FMT_BGR565
DRM_FORMAT_RGB888 MPP_FMT_RGB888
DRM_FORMAT_BGR888 MPP_FMT_BGR888
DRM_FORMAT_ARGB8888 MPP_FMT_ARGB8888
DRM_FORMAT_ABGR8888 MPP_FMT_ABGR8888

MPP 到 DRM 转换表

MPP 格式 DRM 格式
MPP_FMT_YUV420SP DRM_FORMAT_NV12
MPP_FMT_YUV420P DRM_FORMAT_YUV420
MPP_FMT_YUV422SP DRM_FORMAT_NV16
MPP_FMT_YUV422_YUYV DRM_FORMAT_YUYV
MPP_FMT_YUV422_UYVY DRM_FORMAT_UYVY
MPP_FMT_RGB565 DRM_FORMAT_RGB565
MPP_FMT_BGR565 DRM_FORMAT_BGR565
MPP_FMT_RGB888 DRM_FORMAT_RGB888
MPP_FMT_BGR888 DRM_FORMAT_BGR888
MPP_FMT_ARGB8888 DRM_FORMAT_ARGB8888
MPP_FMT_ABGR8888 DRM_FORMAT_ABGR8888

典型使用场景

场景 1: 显示和编码格式匹配

// 获取显示格式(DRM)
uint32_t drm_fmt = get_drm_format_from_display();

// 转换为 MPP 格式用于编码
MppFrameFormat mpp_fmt = convertDrmToMppFormat(drm_fmt);

// 配置编码器
encoder_config.format = mpp_fmt;

场景 2: 编码和显示格式匹配

// 获取编码格式(MPP)
MppFrameFormat mpp_fmt = encoder_config.format;

// 转换为 DRM 格式用于显示
uint32_t drm_fmt = convertMppToDrmFormat(mpp_fmt);

// 配置显示层
drm_layer->format = drm_fmt;

场景 3: 格式验证

// 验证 DRM 格式是否支持
uint32_t drm_fmt = DRM_FORMAT_NV12;
MppFrameFormat mpp_fmt = convertDrmToMppFormat(drm_fmt);

if (mpp_fmt == MPP_FMT_YUV420SP) {
    printf("Format supported\n");
}

场景 4: 批量格式转换

std::vector<uint32_t> drm_formats = {
    DRM_FORMAT_NV12,
    DRM_FORMAT_YUV420,
    DRM_FORMAT_RGB888,
    DRM_FORMAT_ARGB8888
};

for (auto drm_fmt : drm_formats) {
    MppFrameFormat mpp_fmt = convertDrmToMppFormat(drm_fmt);
    printf("DRM 0x%x -> MPP 0x%x\n", drm_fmt, mpp_fmt);
}

场景 5: 格式回退

// 尝试获取 DRM 格式
uint32_t drm_fmt = get_preferred_drm_format();

// 转换为 MPP 格式
MppFrameFormat mpp_fmt = convertDrmToMppFormat(drm_fmt);

// 如果转换失败,使用默认格式
if (mpp_fmt == MPP_FMT_YUV420SP && drm_fmt != DRM_FORMAT_NV12) {
    printf("Using default format\n");
}

注意事项

  1. 默认格式: 不支持的格式默认返回 MPP_FMT_YUV420SP / DRM_FORMAT_NV12
  2. 查找表: 使用 static unordered_map 作为查找表,首次调用时初始化
  3. 线程安全: 函数是线程安全的
  4. 格式映射: 某些 DRM 格式映射到相同的 MPP 格式(如 NV12 和 NV21)
  5. 性能: 使用 unordered_map 查找,性能较好
  6. 格式支持: 只支持常用格式,不支持所有格式

相关文档


参考资料

主页

API 文档

DMA 模块

DRM 模块

NET 模块

V4L2 模块

V4L2Param 模块

RGA 模块

MPP 模块

Sys 模块

Mouse 模块

Utils 模块

Clone this wiki locally