-
Notifications
You must be signed in to change notification settings - Fork 1
V4L2_FormatTool
SweerItTer edited this page Feb 1, 2026
·
3 revisions
FormatTool 是 utilsCore V4L2 模块的命名空间,提供 V4L2 格式平面信息映射功能。
- 提供格式平面比例映射
- 平面信息查询
- 格式验证
- 平面计算
- 依赖: V4L2 库
- 被依赖: CameraController、Frame 等模块
struct PlaneScale {
float width_scale;
float height_scale;
};字段说明:
-
width_scale: 宽度比例(相对于完整分辨率) -
height_scale: 高度比例(相对于完整分辨率)
说明:
- 用于计算每个平面的实际大小
- 例如 NV12 格式:Y 平面为 (1.0, 1.0),UV 平面为 (1.0, 0.5)
const static std::unordered_map<uint32_t, std::vector<PlaneScale>> formatPlaneMap = {
// NV12/NV21: 也按双平面处理, 有的驱动只报告单平面
{ V4L2_PIX_FMT_NV12, { {1.0f, 1.0f}, {1.0f, 0.5f} } },
{ V4L2_PIX_FMT_NV21, { {1.0f, 1.0f}, {1.0f, 0.5f} } },
{ V4L2_PIX_FMT_NV16, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
{ V4L2_PIX_FMT_NV61, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
{ V4L2_PIX_FMT_NV24, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
{ V4L2_PIX_FMT_NV42, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
// NV12M / NV21M
{ V4L2_PIX_FMT_NV12M, { {1.0f, 1.0f}, {1.0f, 0.5f} } },
{ V4L2_PIX_FMT_NV21M, { {1.0f, 1.0f}, {1.0f, 0.5f} } },
// NV16M / NV61M
{ V4L2_PIX_FMT_NV16M, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
{ V4L2_PIX_FMT_NV61M, { {1.0f, 1.0f}, {1.0f, 1.0f} } },
// YUV420M / YVU420M
{ V4L2_PIX_FMT_YUV420M, { {1.0f, 1.0f}, {0.5f, 0.5f}, {0.5f, 0.5f} } },
{ V4L2_PIX_FMT_YVU420M, { {1.0f, 1.0f}, {0.5f, 0.5f}, {0.5f, 0.5f} } },
// YUV422M / YVU422M
{ V4L2_PIX_FMT_YUV422M, { {1.0f, 1.0f}, {0.5f, 1.0f}, {0.5f, 1.0f} } },
{ V4L2_PIX_FMT_YVU422M, { {1.0f, 1.0f}, {0.5f, 1.0f}, {0.5f, 1.0f} } },
// YUV444M
{ V4L2_PIX_FMT_YUV444M, { {1.0f, 1.0f}, {1.0f, 1.0f}, {1.0f, 1.0f} } },
};| 格式 | 平面数 | 平面信息 | 说明 |
|---|---|---|---|
| V4L2_PIX_FMT_NV12 | 2 | (1.0, 1.0), (1.0, 0.5) | YUV 420 Semi-Planar |
| V4L2_PIX_FMT_NV21 | 2 | (1.0, 1.0), (1.0, 0.5) | YUV 420 Semi-Planar |
| V4L2_PIX_FMT_NV16 | 2 | (1.0, 1.0), (1.0, 1.0) | YUV 422 Semi-Planar |
| V4L2_PIX_FMT_NV61 | 2 | (1.0, 1.0), (1.0, 1.0) | YUV 422 Semi-Planar |
| V4L2_PIX_FMT_NV12M | 2 | (1.0, 1.0), (1.0, 0.5) | YUV 420 Multi-Planar |
| V4L2_PIX_FMT_YUV420M | 3 | (1.0, 1.0), (0.5, 0.5), (0.5, 0.5) | YUV 420 Multi-Planar |
| V4L2_PIX_FMT_YUV422M | 3 | (1.0, 1.0), (0.5, 1.0), (0.5, 1.0) | YUV 422 Multi-Planar |
| V4L2_PIX_FMT_YUV444M | 3 | (1.0, 1.0), (1.0, 1.0), (1.0, 1.0) | YUV 444 Multi-Planar |
uint32_t format = V4L2_PIX_FMT_NV12;
auto it = FormatTool::formatPlaneMap.find(format);
if (it != FormatTool::formatPlaneMap.end()) {
const auto& planes = it->second;
printf("Format has %zu planes\n", planes.size());
for (size_t i = 0; i < planes.size(); ++i) {
printf("Plane %zu: width_scale=%.2f, height_scale=%.2f\n",
i, planes[i].width_scale, planes[i].height_scale);
}
}uint32_t width = 1920;
uint32_t height = 1080;
uint32_t format = V4L2_PIX_FMT_NV12;
auto it = FormatTool::formatPlaneMap.find(format);
if (it != FormatTool::formatPlaneMap.end()) {
const auto& planes = it->second;
for (size_t i = 0; i < planes.size(); ++i) {
uint32_t plane_width = width * planes[i].width_scale;
uint32_t plane_height = height * planes[i].height_scale;
printf("Plane %zu: %ux%u\n", i, plane_width, plane_height);
}
}uint32_t format = V4L2_PIX_FMT_NV12;
if (FormatTool::formatPlaneMap.find(format) != FormatTool::formatPlaneMap.end()) {
printf("Format supported\n");
} else {
printf("Format not supported\n");
}uint32_t width = 1920;
uint32_t height = 1080;
uint32_t format = V4L2_PIX_FMT_NV12M;
uint32_t bytes_per_pixel = 1; // 对于 YUV 格式
auto it = FormatTool::formatPlaneMap.find(format);
if (it != FormatTool::formatPlaneMap.end()) {
const auto& planes = it->second;
uint32_t offset = 0;
for (size_t i = 0; i < planes.size(); ++i) {
uint32_t plane_width = width * planes[i].width_scale;
uint32_t plane_height = height * planes[i].height_scale;
uint32_t plane_size = plane_width * plane_height * bytes_per_pixel;
printf("Plane %zu: offset=%u, size=%u\n", i, offset, plane_size);
offset += plane_size;
}
}uint32_t format = V4L2_PIX_FMT_NV12;
uint32_t expected_planes = 2;
auto it = FormatTool::formatPlaneMap.find(format);
if (it != FormatTool::formatPlaneMap.end()) {
if (it->second.size() == expected_planes) {
printf("Format has expected number of planes\n");
} else {
printf("Format has %zu planes, expected %u\n",
it->second.size(), expected_planes);
}
}- 静态映射表: formatPlaneMap 是静态常量映射表
- 命名空间: 使用 FormatTool 命名空间
- 平面比例: width_scale 和 height_scale 是相对于完整分辨率的比例
- 格式支持: 只支持常见格式,不支持所有格式
- 内存布局: 平面大小 = width * width_scale * height * height_scale * bytes_per_pixel
- M 格式: M 格式表示 Multi-Planar(多平面)
- 线程安全: 映射表是静态常量,线程安全
- CameraController - 相机控制器
- Frame - 帧数据
- V4L2 模块总览
主页
API 文档
DMA 模块
DRM 模块
- DRM 模块总览
- DeviceController - DRM 设备控制器
- DrmLayer - DRM 图层管理
- PlanesCompositor - DRM 平面合成器
- DrmBpp - DRM 格式定义
NET 模块
- NET 模块总览
- TcpServer - TCP 服务器
- SocketConnection - Socket 连接管理
- CommandHandler - 命令处理器
- DataPacket - 数据包
V4L2 模块
- V4L2 模块总览
- CameraController - V4L2 摄像头控制器
- Frame - V4L2 帧数据结构
- FormatTool - V4L2 格式工具
- Exception - V4L2 异常类
V4L2Param 模块
- V4L2Param 模块总览
- ParamControl - 参数控制
- ParamLogger - 参数日志
- ParamProcessor - 参数处理器
RGA 模块
- RGA 模块总览
- RgaConverter - RGA 转换器
- RgaProcessor - RGA 处理器
- FormatTool - RGA 格式工具
MPP 模块
- MPP 模块总览
- EncoderContext - 编码器上下文
- EncoderCore - 编码器核心
- JpegEncoder - JPEG 编码器
- StreamWriter - 流写入器
- MppResourceGuard - MPP 资源守护
- FileTools - 文件工具
- FormatTool - 格式工具
Sys 模块
- Sys 模块总览
- CpuMonitor - CPU 监控器
- MemoryMonitor - 内存监控器
- Base - 基础类
Mouse 模块
- Mouse 模块总览
- Watcher - 鼠标监视器
Utils 模块
- Utils 模块总览
- AsyncThreadPool - 异步线程池
- ConcurrentQueue - 并发队列
- FdWrapper - 文件描述符包装器
- FenceWatcher - 围栏监视器
- FixedSizePool - 固定大小对象池
- Logger - 日志记录器
- ObjectsPool - 对象池
- OrderedQueue - 有序队列
- ProgressBar - 进度条
- SafeQueue - 安全队列
- SharedBufferState - 共享缓冲区状态
- SimpleVariant - 简单变体类型
- ThreadPauser - 线程暂停器
- ThreadUtils - 线程工具
- Types - 类型定义
- UdevMonitor - Udev 监视器