Skip to content

Commit e6d8134

Browse files
committed
refactor(class/video/usbd_video): zero copy for video data transfer
Signed-off-by: sakumisu <[email protected]>
1 parent af6df63 commit e6d8134

File tree

6 files changed

+84
-106
lines changed

6 files changed

+84
-106
lines changed

class/video/usbd_video.c

Lines changed: 64 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ struct usbd_video_priv {
1818
uint8_t power_mode;
1919
uint8_t error_code;
2020
struct video_entity_info info[3];
21-
uint8_t *ep_buf0;
22-
uint8_t *ep_buf1;
23-
bool ep_buf0_ready;
24-
bool ep_buf1_ready;
25-
uint32_t ep_buf0_len;
26-
uint32_t ep_buf1_len;
27-
uint8_t ep_buf_idx;
21+
uint8_t *ep_buf;
2822
bool stream_finish;
29-
uint32_t max_packets;
3023
uint8_t *stream_buf;
3124
uint32_t stream_len;
3225
uint32_t stream_offset;
3326
uint8_t stream_frameid;
3427
uint32_t stream_headerlen;
28+
bool do_copy;
3529
} g_usbd_video[CONFIG_USBDEV_MAX_BUS];
3630

3731
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
756750
g_usbd_video[busid].stream_headerlen = 2;
757751
}
758752

759-
static uint32_t usbd_video_prepare_ep_buf_data(uint8_t busid, uint32_t remain, uint8_t *ep_buf)
760-
{
761-
struct video_payload_header *header;
762-
uint32_t len;
763-
uint32_t offset;
764-
765-
len = MIN(remain, (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - g_usbd_video[busid].stream_headerlen) * g_usbd_video[busid].max_packets);
766-
offset = 0;
767-
while (len > 0) {
768-
header = (struct video_payload_header *)&ep_buf[offset];
769-
header->bHeaderLength = g_usbd_video[busid].stream_headerlen;
770-
header->headerInfoUnion.bmheaderInfo = 0;
771-
header->headerInfoUnion.headerInfoBits.endOfHeader = 1;
772-
header->headerInfoUnion.headerInfoBits.endOfFrame = 0;
773-
header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid;
774-
775-
uint32_t len2 = MIN(len, g_usbd_video[busid].probe.dwMaxPayloadTransferSize - g_usbd_video[busid].stream_headerlen);
776-
777-
usb_memcpy(&ep_buf[offset + g_usbd_video[busid].stream_headerlen],
778-
&g_usbd_video[busid].stream_buf[g_usbd_video[busid].stream_offset],
779-
len2);
780-
781-
g_usbd_video[busid].stream_offset += len2;
782-
len -= len2;
783-
offset += (len2 + g_usbd_video[busid].stream_headerlen);
784-
785-
if (g_usbd_video[busid].stream_offset == g_usbd_video[busid].stream_len) {
786-
header->headerInfoUnion.headerInfoBits.endOfFrame = 1;
787-
}
788-
}
789-
790-
return offset;
791-
}
792-
793753
struct usbd_interface *usbd_video_init_intf(uint8_t busid,
794754
struct usbd_interface *intf,
795755
uint32_t dwFrameInterval,
@@ -817,69 +777,87 @@ struct usbd_interface *usbd_video_init_intf(uint8_t busid,
817777

818778
bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep)
819779
{
820-
uint32_t remain;
821-
822-
if (g_usbd_video[busid].ep_buf1_ready && (g_usbd_video[busid].ep_buf_idx == 0)) { /* callback: buf1 ready and buf0 was sent */
823-
g_usbd_video[busid].ep_buf0_ready = false;
824-
g_usbd_video[busid].ep_buf_idx = 1;
825-
usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf1, g_usbd_video[busid].ep_buf1_len);
826-
} else if (g_usbd_video[busid].ep_buf0_ready && (g_usbd_video[busid].ep_buf_idx == 1)) { /* callback: buf0 ready and buf1 was sent */
827-
g_usbd_video[busid].ep_buf1_ready = false;
828-
g_usbd_video[busid].ep_buf_idx = 0;
829-
usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf0, g_usbd_video[busid].ep_buf0_len);
780+
struct video_payload_header *header;
781+
static uint32_t offset = 0;
782+
static uint32_t len = 0;
783+
784+
if (g_usbd_video[busid].stream_finish) {
785+
g_usbd_video[busid].stream_finish = false;
786+
return true;
787+
}
788+
789+
offset = g_usbd_video[busid].stream_offset;
790+
791+
len = MIN(g_usbd_video[busid].stream_len,
792+
g_usbd_video[busid].probe.dwMaxPayloadTransferSize -
793+
g_usbd_video[busid].stream_headerlen);
794+
795+
if (g_usbd_video[busid].do_copy) {
796+
header = (struct video_payload_header *)&g_usbd_video[busid].ep_buf[0];
797+
usb_memcpy(&g_usbd_video[busid].ep_buf[g_usbd_video[busid].stream_headerlen], &g_usbd_video[busid].stream_buf[offset], len);
830798
} else {
831-
if (g_usbd_video[busid].stream_finish) {
832-
return true;
833-
}
799+
header = (struct video_payload_header *)&g_usbd_video[busid].stream_buf[offset - g_usbd_video[busid].stream_headerlen];
834800
}
835801

836-
if (!g_usbd_video[busid].ep_buf0_ready) {
837-
remain = g_usbd_video[busid].stream_len - g_usbd_video[busid].stream_offset;
838-
if (remain == 0) {
839-
g_usbd_video[busid].stream_frameid ^= 1;
840-
g_usbd_video[busid].stream_finish = true;
841-
} else {
842-
g_usbd_video[busid].ep_buf0_len = usbd_video_prepare_ep_buf_data(busid, remain, g_usbd_video[busid].ep_buf0);
843-
g_usbd_video[busid].ep_buf0_ready = true;
844-
if (!g_usbd_video[busid].ep_buf1_ready) {
845-
g_usbd_video[busid].ep_buf_idx = 0;
846-
usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buf0, g_usbd_video[busid].ep_buf0_len);
847-
}
848-
}
802+
header->bHeaderLength = g_usbd_video[busid].stream_headerlen;
803+
header->headerInfoUnion.bmheaderInfo = 0;
804+
header->headerInfoUnion.headerInfoBits.endOfHeader = 1;
805+
header->headerInfoUnion.headerInfoBits.endOfFrame = 0;
806+
header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid;
807+
808+
g_usbd_video[busid].stream_offset += len;
809+
g_usbd_video[busid].stream_len -= len;
810+
811+
if (g_usbd_video[busid].stream_len == 0) {
812+
header->headerInfoUnion.headerInfoBits.endOfFrame = 1;
813+
g_usbd_video[busid].stream_frameid ^= 1;
814+
g_usbd_video[busid].stream_finish = true;
849815
}
850816

851-
if (!g_usbd_video[busid].ep_buf1_ready) {
852-
remain = g_usbd_video[busid].stream_len - g_usbd_video[busid].stream_offset;
853-
if (remain == 0) {
854-
g_usbd_video[busid].stream_frameid ^= 1;
855-
g_usbd_video[busid].stream_finish = true;
856-
} else {
857-
g_usbd_video[busid].ep_buf1_len = usbd_video_prepare_ep_buf_data(busid, remain, g_usbd_video[busid].ep_buf1);
858-
g_usbd_video[busid].ep_buf1_ready = true;
859-
}
817+
if (g_usbd_video[busid].do_copy) {
818+
usbd_ep_start_write(busid, ep,
819+
g_usbd_video[busid].ep_buf,
820+
g_usbd_video[busid].stream_headerlen + len);
821+
} else {
822+
usbd_ep_start_write(busid, ep,
823+
&g_usbd_video[busid].stream_buf[offset - g_usbd_video[busid].stream_headerlen],
824+
g_usbd_video[busid].stream_headerlen + len);
860825
}
861826

862827
return false;
863828
}
864829

865-
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)
830+
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)
866831
{
832+
struct video_payload_header *header;
833+
867834
if ((usb_device_is_configured(busid) == 0) || (stream_len == 0)) {
868835
return -1;
869836
}
870837

871-
g_usbd_video[busid].ep_buf0 = ep_buf0;
872-
g_usbd_video[busid].ep_buf1 = ep_buf1;
873-
g_usbd_video[busid].ep_buf0_ready = false;
874-
g_usbd_video[busid].ep_buf1_ready = false;
875-
g_usbd_video[busid].ep_buf_idx = 0;
876-
g_usbd_video[busid].stream_finish = false;
877-
g_usbd_video[busid].max_packets = ep_bufsize / g_usbd_video[busid].probe.dwMaxPayloadTransferSize;
838+
g_usbd_video[busid].ep_buf = ep_buf;
878839
g_usbd_video[busid].stream_buf = stream_buf;
879840
g_usbd_video[busid].stream_len = stream_len;
880841
g_usbd_video[busid].stream_offset = 0;
842+
g_usbd_video[busid].stream_finish = false;
843+
g_usbd_video[busid].do_copy = do_copy;
844+
845+
uint32_t len = MIN(g_usbd_video[busid].stream_len,
846+
g_usbd_video[busid].probe.dwMaxPayloadTransferSize -
847+
g_usbd_video[busid].stream_headerlen);
848+
849+
header = (struct video_payload_header *)&ep_buf[0];
850+
header->bHeaderLength = g_usbd_video[busid].stream_headerlen;
851+
header->headerInfoUnion.bmheaderInfo = 0;
852+
header->headerInfoUnion.headerInfoBits.endOfHeader = 1;
853+
header->headerInfoUnion.headerInfoBits.endOfFrame = 0;
854+
header->headerInfoUnion.headerInfoBits.frameIdentifier = g_usbd_video[busid].stream_frameid;
855+
856+
usb_memcpy(&ep_buf[g_usbd_video[busid].stream_headerlen], stream_buf, len);
857+
g_usbd_video[busid].stream_offset += len;
858+
g_usbd_video[busid].stream_len -= len;
881859

882-
usbd_video_stream_split_transfer(busid, ep);
860+
usbd_ep_start_write(busid, ep, ep_buf, g_usbd_video[busid].stream_headerlen + len);
883861
return 0;
884862
}
885863

class/video/usbd_video.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void usbd_video_open(uint8_t busid, uint8_t intf);
2222
void usbd_video_close(uint8_t busid, uint8_t intf);
2323

2424
bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep);
25-
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);
25+
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);
2626

2727
#ifdef __cplusplus
2828
}

demo/video_static_h264_template.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "usbd_video.h"
88
#include "cherryusb_h264.h"
99

10-
#define MAX_PACKETS_IN_ONE_TRANSFER 1
11-
1210
#define VIDEO_IN_EP 0x81
1311
#define VIDEO_INT_EP 0x83
1412

@@ -282,7 +280,8 @@ void video_init(uint8_t busid, uintptr_t reg_base)
282280
usbd_initialize(busid, reg_base, usbd_event_handler);
283281
}
284282

285-
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][40 * 1024];
283+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
284+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
286285

287286
void video_test(uint8_t busid)
288287
{
@@ -294,7 +293,8 @@ void video_test(uint8_t busid)
294293
while (1) {
295294
if (tx_flag) {
296295
iso_tx_busy = true;
297-
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));
296+
memcpy(frame_buffer, cherryusb_h264, sizeof(cherryusb_h264)); // cherryusb_h264 is a static yuyv frame buffer, so we need copy it to frame_buffer
297+
usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_h264), false);
298298
while (iso_tx_busy) {
299299
if (tx_flag == 0) {
300300
break;

demo/video_static_mjpeg_template.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "usbd_video.h"
88
#include "cherryusb_mjpeg.h"
99

10-
#define MAX_PACKETS_IN_ONE_TRANSFER 1
11-
1210
#define VIDEO_IN_EP 0x81
1311
#define VIDEO_INT_EP 0x83
1412

@@ -282,7 +280,8 @@ void video_init(uint8_t busid, uintptr_t reg_base)
282280
usbd_initialize(busid, reg_base, usbd_event_handler);
283281
}
284282

285-
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
283+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
284+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
286285

287286
void video_test(uint8_t busid)
288287
{
@@ -291,7 +290,8 @@ void video_test(uint8_t busid)
291290
while (1) {
292291
if (tx_flag) {
293292
iso_tx_busy = true;
294-
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));
293+
memcpy(frame_buffer, cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); // cherryusb_mjpeg is a static MJPEG frame buffer, so we need copy it to frame_buffer
294+
usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_mjpeg), false);
295295
while (iso_tx_busy) {
296296
if (tx_flag == 0) {
297297
break;

demo/video_static_yuyv_template.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "usbd_video.h"
88
#include "cherryusb_yuyv.h"
99

10-
#define MAX_PACKETS_IN_ONE_TRANSFER 1
11-
1210
#define VIDEO_IN_EP 0x81
1311
#define VIDEO_INT_EP 0x83
1412

@@ -286,7 +284,8 @@ void video_init(uint8_t busid, uintptr_t reg_base)
286284
usbd_initialize(busid, reg_base, usbd_event_handler);
287285
}
288286

289-
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
287+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
288+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
290289

291290
void video_test(uint8_t busid)
292291
{
@@ -295,7 +294,8 @@ void video_test(uint8_t busid)
295294
while (1) {
296295
if (tx_flag) {
297296
iso_tx_busy = true;
298-
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));
297+
memcpy(frame_buffer, cherryusb_yuyv, sizeof(cherryusb_yuyv)); // cherryusb_yuyv is a static yuyv frame buffer, so we need copy it to frame_buffer
298+
usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_yuyv), false);
299299
while (iso_tx_busy) {
300300
if (tx_flag == 0) {
301301
break;

docs/source/demo/usbd_video.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v
4545
USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
4646
4747
48-
- 使用 `usbd_video_stream_start_write` 传输数据
48+
- 使用 `usbd_video_stream_start_write` 传输数据, 最后 **do_copy** 选项表示是否将数据 copy 到 packet_buffer,
49+
如果不选择 copy, 则会直接在原图像数据中填充头部信息,并直接发送,达到 zero copy 功能。
4950

50-
1,传输采用双缓冲的形式, **MAX_PACKETS_IN_ONE_TRANSFER** 表示一次传输可以携带多少个 **MAX_PAYLOAD_SIZE**,通常 IP 只能为 1
51+
- 因为提供的是静态数据,不能被修改,因此需要重新给一个 frame_buffer 用于图像传输,在实际对接 camera 场景中是动态数据,直接使用 camera 的数据缓冲区即可
5152

52-
2,在中断完成中,调用 `usbd_video_stream_split_transfer` 继续下一次传输,直到返回为 true 表示传输完成。这边的分裂传输只是表示将图片数据拆成 **MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE** 份传输。
53-
54-
3,通常 IP 不支持一次传输非常大的数据,比如传输 1MB,因此需要做分裂传输,但是会增加中断次数。并且一次传输非常大数据也是需要足够的 RAM。
5553

5654
.. code-block:: C
5755
@@ -63,7 +61,8 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v
6361
}
6462
}
6563
66-
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
64+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
65+
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
6766
6867
void video_test(uint8_t busid)
6968
{
@@ -72,7 +71,8 @@ demo 包含 **video_static_yuyv_template**, **video_static_mjpeg_template**, **v
7271
while (1) {
7372
if (tx_flag) {
7473
iso_tx_busy = true;
75-
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));
74+
memcpy(frame_buffer, cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); // cherryusb_mjpeg is a static MJPEG frame buffer, so we need copy it to frame_buffer
75+
usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_mjpeg), false);
7676
while (iso_tx_busy) {
7777
if (tx_flag == 0) {
7878
break;

0 commit comments

Comments
 (0)