diff --git a/class/video/usbd_video.c b/class/video/usbd_video.c index f3a28dbd..b94379df 100644 --- a/class/video/usbd_video.c +++ b/class/video/usbd_video.c @@ -18,20 +18,14 @@ struct usbd_video_priv { uint8_t power_mode; uint8_t error_code; struct video_entity_info info[3]; - uint8_t *ep_buf0; - uint8_t *ep_buf1; - bool ep_buf0_ready; - bool ep_buf1_ready; - uint32_t ep_buf0_len; - uint32_t ep_buf1_len; - uint8_t ep_buf_idx; + uint8_t *ep_buf; bool stream_finish; - uint32_t max_packets; uint8_t *stream_buf; uint32_t stream_len; uint32_t stream_offset; uint8_t stream_frameid; uint32_t stream_headerlen; + bool do_copy; } g_usbd_video[CONFIG_USBDEV_MAX_BUS]; static int usbd_video_control_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) @@ -756,40 +750,6 @@ static void usbd_video_probe_and_commit_controls_init(uint8_t busid, uint32_t dw g_usbd_video[busid].stream_headerlen = 2; } -static uint32_t usbd_video_prepare_ep_buf_data(uint8_t busid, uint32_t remain, uint8_t *ep_buf) -{ - struct video_payload_header *header; - uint32_t len; - uint32_t offset; - - len = MIN(remain, (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - g_usbd_video[busid].stream_headerlen) * g_usbd_video[busid].max_packets); - offset = 0; - while (len > 0) { - header = (struct video_payload_header *)&ep_buf[offset]; - header->bHeaderLength = g_usbd_video[busid].stream_headerlen; - header->headerInfoUnion.bmheaderInfo = 0; - header->headerInfoUnion.headerInfoBits.endOfHeader = 1; - header->headerInfoUnion.headerInfoBits.endOfFrame = 0; - header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid; - - uint32_t len2 = MIN(len, g_usbd_video[busid].probe.dwMaxPayloadTransferSize - g_usbd_video[busid].stream_headerlen); - - usb_memcpy(&ep_buf[offset + g_usbd_video[busid].stream_headerlen], - &g_usbd_video[busid].stream_buf[g_usbd_video[busid].stream_offset], - len2); - - g_usbd_video[busid].stream_offset += len2; - len -= len2; - offset += (len2 + g_usbd_video[busid].stream_headerlen); - - if (g_usbd_video[busid].stream_offset == g_usbd_video[busid].stream_len) { - header->headerInfoUnion.headerInfoBits.endOfFrame = 1; - } - } - - return offset; -} - struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface *intf, uint32_t dwFrameInterval, @@ -817,69 +777,87 @@ struct usbd_interface *usbd_video_init_intf(uint8_t busid, bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep) { - uint32_t remain; + struct video_payload_header *header; + static uint32_t offset = 0; + static uint32_t len = 0; - if (g_usbd_video[busid].ep_buf1_ready && (g_usbd_video[busid].ep_buf_idx == 0)) { /* callback: buf1 ready and buf0 was sent */ - g_usbd_video[busid].ep_buf0_ready = false; - g_usbd_video[busid].ep_buf_idx = 1; - usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf1, g_usbd_video[busid].ep_buf1_len); - } else if (g_usbd_video[busid].ep_buf0_ready && (g_usbd_video[busid].ep_buf_idx == 1)) { /* callback: buf0 ready and buf1 was sent */ - g_usbd_video[busid].ep_buf1_ready = false; - g_usbd_video[busid].ep_buf_idx = 0; - usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf0, g_usbd_video[busid].ep_buf0_len); + if (g_usbd_video[busid].stream_finish) { + g_usbd_video[busid].stream_finish = false; + return true; + } + + offset = g_usbd_video[busid].stream_offset; + + len = MIN(g_usbd_video[busid].stream_len, + g_usbd_video[busid].probe.dwMaxPayloadTransferSize - + g_usbd_video[busid].stream_headerlen); + + if (g_usbd_video[busid].do_copy) { + header = (struct video_payload_header *)&g_usbd_video[busid].ep_buf[0]; + usb_memcpy(&g_usbd_video[busid].ep_buf[g_usbd_video[busid].stream_headerlen], &g_usbd_video[busid].stream_buf[offset], len); } else { - if (g_usbd_video[busid].stream_finish) { - return true; - } + header = (struct video_payload_header *)&g_usbd_video[busid].stream_buf[offset - g_usbd_video[busid].stream_headerlen]; } - if (!g_usbd_video[busid].ep_buf0_ready) { - remain = g_usbd_video[busid].stream_len - g_usbd_video[busid].stream_offset; - if (remain == 0) { - g_usbd_video[busid].stream_frameid ^= 1; - g_usbd_video[busid].stream_finish = true; - } else { - g_usbd_video[busid].ep_buf0_len = usbd_video_prepare_ep_buf_data(busid, remain, g_usbd_video[busid].ep_buf0); - g_usbd_video[busid].ep_buf0_ready = true; - if (!g_usbd_video[busid].ep_buf1_ready) { - g_usbd_video[busid].ep_buf_idx = 0; - usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf0, g_usbd_video[busid].ep_buf0_len); - } - } + header->bHeaderLength = g_usbd_video[busid].stream_headerlen; + header->headerInfoUnion.bmheaderInfo = 0; + header->headerInfoUnion.headerInfoBits.endOfHeader = 1; + header->headerInfoUnion.headerInfoBits.endOfFrame = 0; + header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid; + + g_usbd_video[busid].stream_offset += len; + g_usbd_video[busid].stream_len -= len; + + if (g_usbd_video[busid].stream_len == 0) { + header->headerInfoUnion.headerInfoBits.endOfFrame = 1; + g_usbd_video[busid].stream_frameid ^= 1; + g_usbd_video[busid].stream_finish = true; } - if (!g_usbd_video[busid].ep_buf1_ready) { - remain = g_usbd_video[busid].stream_len - g_usbd_video[busid].stream_offset; - if (remain == 0) { - g_usbd_video[busid].stream_frameid ^= 1; - g_usbd_video[busid].stream_finish = true; - } else { - g_usbd_video[busid].ep_buf1_len = usbd_video_prepare_ep_buf_data(busid, remain, g_usbd_video[busid].ep_buf1); - g_usbd_video[busid].ep_buf1_ready = true; - } + if (g_usbd_video[busid].do_copy) { + usbd_ep_start_write(busid, ep, + g_usbd_video[busid].ep_buf, + g_usbd_video[busid].stream_headerlen + len); + } else { + usbd_ep_start_write(busid, ep, + &g_usbd_video[busid].stream_buf[offset - g_usbd_video[busid].stream_headerlen], + g_usbd_video[busid].stream_headerlen + len); } return false; } -int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buf0, uint8_t *ep_buf1, uint32_t ep_bufsize, uint8_t *stream_buf, uint32_t stream_len) +int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buf, uint8_t *stream_buf, uint32_t stream_len, bool do_copy) { + struct video_payload_header *header; + if ((usb_device_is_configured(busid) == 0) || (stream_len == 0)) { return -1; } - g_usbd_video[busid].ep_buf0 = ep_buf0; - g_usbd_video[busid].ep_buf1 = ep_buf1; - g_usbd_video[busid].ep_buf0_ready = false; - g_usbd_video[busid].ep_buf1_ready = false; - g_usbd_video[busid].ep_buf_idx = 0; - g_usbd_video[busid].stream_finish = false; - g_usbd_video[busid].max_packets = ep_bufsize / g_usbd_video[busid].probe.dwMaxPayloadTransferSize; + g_usbd_video[busid].ep_buf = ep_buf; g_usbd_video[busid].stream_buf = stream_buf; g_usbd_video[busid].stream_len = stream_len; g_usbd_video[busid].stream_offset = 0; + g_usbd_video[busid].stream_finish = false; + g_usbd_video[busid].do_copy = do_copy; - usbd_video_stream_split_transfer(busid, ep); + uint32_t len = MIN(g_usbd_video[busid].stream_len, + g_usbd_video[busid].probe.dwMaxPayloadTransferSize - + g_usbd_video[busid].stream_headerlen); + + header = (struct video_payload_header *)&ep_buf[0]; + header->bHeaderLength = g_usbd_video[busid].stream_headerlen; + header->headerInfoUnion.bmheaderInfo = 0; + header->headerInfoUnion.headerInfoBits.endOfHeader = 1; + header->headerInfoUnion.headerInfoBits.endOfFrame = 0; + header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid; + + usb_memcpy(&ep_buf[g_usbd_video[busid].stream_headerlen], stream_buf, len); + g_usbd_video[busid].stream_offset += len; + g_usbd_video[busid].stream_len -= len; + + usbd_ep_start_write(busid, ep, ep_buf, g_usbd_video[busid].stream_headerlen + len); return 0; } diff --git a/class/video/usbd_video.h b/class/video/usbd_video.h index a2a98bb8..a2061fdc 100644 --- a/class/video/usbd_video.h +++ b/class/video/usbd_video.h @@ -22,7 +22,7 @@ void usbd_video_open(uint8_t busid, uint8_t intf); void usbd_video_close(uint8_t busid, uint8_t intf); bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep); -int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buf0, uint8_t *ep_buf1, uint32_t ep_bufsize, uint8_t *stream_buf, uint32_t stream_len); +int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buf, uint8_t *stream_buf, uint32_t stream_len, bool do_copy); #ifdef __cplusplus } diff --git a/demo/video_static_h264_template.c b/demo/video_static_h264_template.c index 60737672..a472244b 100644 --- a/demo/video_static_h264_template.c +++ b/demo/video_static_h264_template.c @@ -7,8 +7,6 @@ #include "usbd_video.h" #include "cherryusb_h264.h" -#define MAX_PACKETS_IN_ONE_TRANSFER 1 - #define VIDEO_IN_EP 0x81 #define VIDEO_INT_EP 0x83 @@ -282,7 +280,8 @@ void video_init(uint8_t busid, uintptr_t reg_base) usbd_initialize(busid, reg_base, usbd_event_handler); } -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][40 * 1024]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024]; void video_test(uint8_t busid) { @@ -294,7 +293,8 @@ void video_test(uint8_t busid) while (1) { if (tx_flag) { iso_tx_busy = true; - usbd_video_stream_start_write(busid, VIDEO_IN_EP, &packet_buffer[0][0], &packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_h264, sizeof(cherryusb_h264)); + memcpy(frame_buffer, cherryusb_h264, sizeof(cherryusb_h264)); // cherryusb_h264 is a static yuyv frame buffer, so we need copy it to frame_buffer + usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_h264), false); while (iso_tx_busy) { if (tx_flag == 0) { break; diff --git a/demo/video_static_mjpeg_template.c b/demo/video_static_mjpeg_template.c index 0e77b38d..667a2fbb 100644 --- a/demo/video_static_mjpeg_template.c +++ b/demo/video_static_mjpeg_template.c @@ -7,8 +7,6 @@ #include "usbd_video.h" #include "cherryusb_mjpeg.h" -#define MAX_PACKETS_IN_ONE_TRANSFER 1 - #define VIDEO_IN_EP 0x81 #define VIDEO_INT_EP 0x83 @@ -282,7 +280,8 @@ void video_init(uint8_t busid, uintptr_t reg_base) usbd_initialize(busid, reg_base, usbd_event_handler); } -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024]; void video_test(uint8_t busid) { @@ -291,7 +290,8 @@ void video_test(uint8_t busid) while (1) { if (tx_flag) { iso_tx_busy = true; - usbd_video_stream_start_write(busid, VIDEO_IN_EP, &packet_buffer[0][0], &packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); + memcpy(frame_buffer, cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); // cherryusb_mjpeg is a static MJPEG frame buffer, so we need copy it to frame_buffer + usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_mjpeg), false); while (iso_tx_busy) { if (tx_flag == 0) { break; diff --git a/demo/video_static_yuyv_template.c b/demo/video_static_yuyv_template.c index 1aca76d0..6b5da39d 100644 --- a/demo/video_static_yuyv_template.c +++ b/demo/video_static_yuyv_template.c @@ -7,8 +7,6 @@ #include "usbd_video.h" #include "cherryusb_yuyv.h" -#define MAX_PACKETS_IN_ONE_TRANSFER 1 - #define VIDEO_IN_EP 0x81 #define VIDEO_INT_EP 0x83 @@ -286,7 +284,8 @@ void video_init(uint8_t busid, uintptr_t reg_base) usbd_initialize(busid, reg_base, usbd_event_handler); } -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024]; void video_test(uint8_t busid) { @@ -295,7 +294,8 @@ void video_test(uint8_t busid) while (1) { if (tx_flag) { iso_tx_busy = true; - usbd_video_stream_start_write(busid, VIDEO_IN_EP, &packet_buffer[0][0], &packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_yuyv, sizeof(cherryusb_yuyv)); + memcpy(frame_buffer, cherryusb_yuyv, sizeof(cherryusb_yuyv)); // cherryusb_yuyv is a static yuyv frame buffer, so we need copy it to frame_buffer + usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_yuyv), false); while (iso_tx_busy) { if (tx_flag == 0) { break; diff --git a/docs/source/demo/usbd_video.rst b/docs/source/demo/usbd_video.rst index a3e085d3..e215abde 100644 --- a/docs/source/demo/usbd_video.rst +++ b/docs/source/demo/usbd_video.rst @@ -45,13 +45,11 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01), -- 使用 `usbd_video_stream_start_write` 传输数据 +- 使用 `usbd_video_stream_start_write` 传输数据, 最后 **do_copy** 选项表示是否将数据 copy 到 packet_buffer, +如果不选择 copy, 则会直接在原图像数据中填充头部信息,并直接发送,达到 zero copy 功能。 -1,传输采用双缓冲的形式, **MAX_PACKETS_IN_ONE_TRANSFER** 表示一次传输可以携带多少个 **MAX_PAYLOAD_SIZE**,通常 IP 只能为 1。 +- 因为提供的是静态数据,不能被修改,因此需要重新给一个 frame_buffer 用于图像传输,在实际对接 camera 场景中是动态数据,直接使用 camera 的数据缓冲区即可。 -2,在中断完成中,调用 `usbd_video_stream_split_transfer` 继续下一次传输,直到返回为 true 表示传输完成。这边的分裂传输只是表示将图片数据拆成 **MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE** 份传输。 - -3,通常 IP 不支持一次传输非常大的数据,比如传输 1MB,因此需要做分裂传输,但是会增加中断次数。并且一次传输非常大数据也是需要足够的 RAM。 .. code-block:: C @@ -63,7 +61,8 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v } } - USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE]; + USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE]; + USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024]; void video_test(uint8_t busid) { @@ -72,7 +71,8 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v while (1) { if (tx_flag) { iso_tx_busy = true; - usbd_video_stream_start_write(busid, VIDEO_IN_EP, &packet_buffer[0][0], &packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); + memcpy(frame_buffer, cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); // cherryusb_mjpeg is a static MJPEG frame buffer, so we need copy it to frame_buffer + usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_mjpeg), false); while (iso_tx_busy) { if (tx_flag == 0) { break;