# USB 视频（UVC）API 参考

## 概述

UVC 接口用于 USB 摄像头采集（Host）或向 USB 主机提供视频（Device）。头文件为 `mpi_uvc_api.h`，链接 `libuvc.a`。实际角色和设备支持取决于固件 USB 配置。

## API 参考

### UVC Host

典型流程：选择设备 → 初始化格式 → 启动 → 获取/归还帧 → 退出。

#### 设备与帧管理

**描述**

选择 USB 摄像头、启动采集、获取和归还帧。

**语法**

```c
int uvc_host_select_device(unsigned int index);
int uvc_host_init(struct uvc_format *fmt);
int uvc_host_start_stream(void);
int uvc_host_get_frame(struct uvc_frame *frame, unsigned int timeout_ms);
int uvc_host_put_frame(struct uvc_frame *frame);
void uvc_host_exit(void);
```

**参数**

| 参数名称 | 描述 | 输入/输出 |
| --- | --- | --- |
| index | 对应 `/dev/video<index>`，必须小于 `UVC_HOST_MAX_DEVICES`，当前为 `8`。 | 输入 |
| fmt | 采集格式，建议从设备支持的格式列表选择。 | 输入 |
| frame | 获取时输出帧信息，归还时传回同一帧。 | 输入/输出 |
| timeout_ms | 等待时间，单位为毫秒。 | 输入 |

**返回值**

状态接口返回 `0` 表示成功、负值表示失败；`exit()` 无返回值。选择无效索引返回 `-EINVAL`，已初始化时选择返回 `-EBUSY`。

**需求**

- 头文件：`mpi_uvc_api.h`
- 库文件：`libuvc.a`

**注意事项**

默认选择 `video0`，仅能在关闭状态选择设备；选择在退出及重新初始化后保留。有效索引不保证节点存在。成功获取帧后必须配对归还，归还后不要继续访问。

#### 设备和格式查询

**描述**

读取设备信息并枚举可用格式。

**语法**

```c
int uvc_host_get_devinfo(char *info, int len);
int uvc_host_get_formats(struct uvc_format **fmts);
void uvc_host_free_formats(struct uvc_format **fmts);
```

**参数**

| 参数名称 | 描述 | 输入/输出 |
| --- | --- | --- |
| info、len | 调用者提供的字符串缓冲区及字节容量。 | 输入/输出 |
| fmts | 输出格式数组，使用后传给 `free_formats()` 释放。 | 输出 |

**返回值**

`get_devinfo()` 成功返回 `0`；`get_formats()` 返回格式数量或错误值；`free_formats()` 无返回值。

**需求**

- 头文件：`mpi_uvc_api.h`
- 库文件：`libuvc.a`

#### 原始帧转换

**描述**

将支持的原始 UVC 图像转换为 NV12、RGB565 或 YUYV。

**语法**

```c
int uvc_host_raw_to_nv12(const struct uvc_frame *frame, void *dst, size_t dst_len);
int uvc_host_raw_to_rgb565(const struct uvc_frame *frame, void *dst, size_t dst_len);
int uvc_host_raw_to_yuyv(const struct uvc_frame *frame, void *dst, size_t dst_len);
```

**参数**

| 参数名称 | 描述 | 输入/输出 |
| --- | --- | --- |
| frame | 已获取且尚未归还的源帧。 | 输入 |
| dst | 调用者提供的目标缓冲区。 | 输出 |
| dst_len | 目标字节容量。 | 输入 |

**返回值**

`0` 表示成功，负值表示失败。

**需求**

- 头文件：`mpi_uvc_api.h`
- 库文件：`libuvc.a`

**注意事项**

转换完成后再归还源帧。MJPEG 压缩帧应先解码，不能直接使用这些转换接口。

### UVC Device

#### 设备与发送缓冲区

**描述**

初始化 USB 视频设备，创建发送池并向主机提交视频。

**语法**

```c
int uvc_device_init(void);
void uvc_device_deinit(void);
int uvc_device_create_buffer_pool(uint32_t buffer_size, uint32_t buffer_count);
int uvc_device_conf(struct uvc_device_conf_t *cfg);
int uvc_device_start();
int uvc_device_stop();
int uvc_device_get_state(int *opened);
int uvc_device_get_buf(uint32_t **buffer, uint32_t *max_size);
int uvc_device_put_buf(uint32_t *buffer, uint32_t size);
```

**参数**

| 参数名称 | 描述 | 输入/输出 |
| --- | --- | --- |
| buffer_size、buffer_count | 单块字节数、缓冲块数。 | 输入 |
| cfg | 配置结构，当前包含 `int frame_rate`。 | 输入 |
| opened | 主机打开状态，成功后读取输出。 | 输出 |
| buffer、max_size | `get_buf` 输出可写指针和最大字节容量。 | 输出 |
| buffer、size | `put_buf` 提交填好的缓冲和有效字节数。 | 输入 |

**返回值**

状态接口成功返回 `0`，失败返回负值；`deinit()` 无返回值。

**需求**

- 头文件：`mpi_uvc_api.h`
- 库文件：`libuvc.a`

**注意事项**

初始化后创建发送池并配置，启动后获取缓冲、填入主机协商格式的数据并提交。写入和提交不得超过获取到的容量。结束时先停止，再去初始化。

## 数据类型

**成员**

| 类型 | 字段与说明 |
| --- | --- |
| `struct uvc_format` | `width`、`height`、`fourcc`、`frameinterval`。帧间隔使用 100 ns 单位。可用 FOURCC 宏包括 YUY2、UYVY、NV12、I420、MJPEG。 |
| `struct uvc_frame` | 缓冲索引 `index`、有效字节数 `bytesused`、数据指针 `userptr`，以及 `v_info` / `v_stream` 联合体。 |
| `struct uvc_device_conf_t` | `int frame_rate`。 |
