video_static_h264_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_video.h"
  8. #include "cherryusb_h264.h"
  9. #define VIDEO_IN_EP 0x81
  10. #define VIDEO_INT_EP 0x83
  11. #ifdef CONFIG_USB_HS
  12. #define MAX_PAYLOAD_SIZE 1024 // for high speed with one transcations every one micro frame
  13. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  14. // #define MAX_PAYLOAD_SIZE 2048 // for high speed with two transcations every one micro frame
  15. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 2)) | (0x01 << 11))
  16. // #define MAX_PAYLOAD_SIZE 3072 // for high speed with three transcations every one micro frame
  17. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 3)) | (0x02 << 11))
  18. #else
  19. #define MAX_PAYLOAD_SIZE 1020
  20. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  21. #endif
  22. #define WIDTH (unsigned int)(640)
  23. #define HEIGHT (unsigned int)(480)
  24. #define CAM_FPS (30)
  25. #define INTERVAL (unsigned long)(10000000 / CAM_FPS)
  26. #define MIN_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS) //16 bit
  27. #define MAX_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS)
  28. #define MAX_FRAME_SIZE (unsigned long)(WIDTH * HEIGHT * 2)
  29. #define VS_HEADER_SIZ (unsigned int)(VIDEO_SIZEOF_VS_INPUT_HEADER_DESC(1,1) + VIDEO_SIZEOF_VS_FORMAT_H264_DESC + VIDEO_SIZEOF_VS_FRAME_H264_DESC(1))
  30. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  31. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  32. 9 + \
  33. VS_HEADER_SIZ + \
  34. 9 + \
  35. 7)
  36. #define USBD_VID 0xffff
  37. #define USBD_PID 0xffff
  38. #define USBD_MAX_POWER 100
  39. #define USBD_LANGID_STRING 1033
  40. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  41. static const uint8_t device_descriptor[] = {
  42. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  43. };
  44. static const uint8_t config_descriptor[] = {
  45. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  46. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  47. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  48. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  49. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  50. VIDEO_VS_FORMAT_H264_DESCRIPTOR_INIT(0x01, 0x01),
  51. VIDEO_VS_FRAME_H264_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  52. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  53. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  54. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01)
  55. };
  56. static const uint8_t device_quality_descriptor[] = {
  57. ///////////////////////////////////////
  58. /// device qualifier descriptor
  59. ///////////////////////////////////////
  60. 0x0a,
  61. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  62. 0x00,
  63. 0x02,
  64. 0x00,
  65. 0x00,
  66. 0x00,
  67. 0x40,
  68. 0x00,
  69. 0x00,
  70. };
  71. static const char *string_descriptors[] = {
  72. (const char[]){ 0x09, 0x04 }, /* Langid */
  73. "CherryUSB", /* Manufacturer */
  74. "CherryUSB UVC DEMO", /* Product */
  75. "2022123456", /* Serial Number */
  76. };
  77. static const uint8_t *device_descriptor_callback(uint8_t speed)
  78. {
  79. return device_descriptor;
  80. }
  81. static const uint8_t *config_descriptor_callback(uint8_t speed)
  82. {
  83. return config_descriptor;
  84. }
  85. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  86. {
  87. return device_quality_descriptor;
  88. }
  89. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  90. {
  91. if (index > 3) {
  92. return NULL;
  93. }
  94. return string_descriptors[index];
  95. }
  96. const struct usb_descriptor video_descriptor = {
  97. .device_descriptor_callback = device_descriptor_callback,
  98. .config_descriptor_callback = config_descriptor_callback,
  99. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  100. .string_descriptor_callback = string_descriptor_callback
  101. };
  102. #else
  103. const uint8_t video_descriptor[] = {
  104. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  105. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  106. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  107. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  108. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  109. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  110. VIDEO_VS_FORMAT_H264_DESCRIPTOR_INIT(0x01, 0x01),
  111. VIDEO_VS_FRAME_H264_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  112. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  113. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  114. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  115. ///////////////////////////////////////
  116. /// string0 descriptor
  117. ///////////////////////////////////////
  118. USB_LANGID_INIT(USBD_LANGID_STRING),
  119. ///////////////////////////////////////
  120. /// string1 descriptor
  121. ///////////////////////////////////////
  122. 0x14, /* bLength */
  123. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  124. 'C', 0x00, /* wcChar0 */
  125. 'h', 0x00, /* wcChar1 */
  126. 'e', 0x00, /* wcChar2 */
  127. 'r', 0x00, /* wcChar3 */
  128. 'r', 0x00, /* wcChar4 */
  129. 'y', 0x00, /* wcChar5 */
  130. 'U', 0x00, /* wcChar6 */
  131. 'S', 0x00, /* wcChar7 */
  132. 'B', 0x00, /* wcChar8 */
  133. ///////////////////////////////////////
  134. /// string2 descriptor
  135. ///////////////////////////////////////
  136. 0x26, /* bLength */
  137. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  138. 'C', 0x00, /* wcChar0 */
  139. 'h', 0x00, /* wcChar1 */
  140. 'e', 0x00, /* wcChar2 */
  141. 'r', 0x00, /* wcChar3 */
  142. 'r', 0x00, /* wcChar4 */
  143. 'y', 0x00, /* wcChar5 */
  144. 'U', 0x00, /* wcChar6 */
  145. 'S', 0x00, /* wcChar7 */
  146. 'B', 0x00, /* wcChar8 */
  147. ' ', 0x00, /* wcChar9 */
  148. 'U', 0x00, /* wcChar10 */
  149. 'V', 0x00, /* wcChar11 */
  150. 'C', 0x00, /* wcChar12 */
  151. ' ', 0x00, /* wcChar13 */
  152. 'D', 0x00, /* wcChar14 */
  153. 'E', 0x00, /* wcChar15 */
  154. 'M', 0x00, /* wcChar16 */
  155. 'O', 0x00, /* wcChar17 */
  156. ///////////////////////////////////////
  157. /// string3 descriptor
  158. ///////////////////////////////////////
  159. 0x16, /* bLength */
  160. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  161. '2', 0x00, /* wcChar0 */
  162. '0', 0x00, /* wcChar1 */
  163. '2', 0x00, /* wcChar2 */
  164. '1', 0x00, /* wcChar3 */
  165. '0', 0x00, /* wcChar4 */
  166. '3', 0x00, /* wcChar5 */
  167. '1', 0x00, /* wcChar6 */
  168. '0', 0x00, /* wcChar7 */
  169. '0', 0x00, /* wcChar8 */
  170. '0', 0x00, /* wcChar9 */
  171. #ifdef CONFIG_USB_HS
  172. ///////////////////////////////////////
  173. /// device qualifier descriptor
  174. ///////////////////////////////////////
  175. 0x0a,
  176. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  177. 0x00,
  178. 0x02,
  179. 0x00,
  180. 0x00,
  181. 0x00,
  182. 0x40,
  183. 0x00,
  184. 0x00,
  185. #endif
  186. 0x00
  187. };
  188. #endif
  189. volatile bool tx_flag = 0;
  190. volatile bool iso_tx_busy = false;
  191. static void usbd_event_handler(uint8_t busid, uint8_t event)
  192. {
  193. switch (event) {
  194. case USBD_EVENT_RESET:
  195. break;
  196. case USBD_EVENT_CONNECTED:
  197. break;
  198. case USBD_EVENT_DISCONNECTED:
  199. break;
  200. case USBD_EVENT_RESUME:
  201. break;
  202. case USBD_EVENT_SUSPEND:
  203. break;
  204. case USBD_EVENT_CONFIGURED:
  205. tx_flag = 0;
  206. iso_tx_busy = false;
  207. break;
  208. case USBD_EVENT_SET_REMOTE_WAKEUP:
  209. break;
  210. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. void usbd_video_open(uint8_t busid, uint8_t intf)
  217. {
  218. tx_flag = 1;
  219. USB_LOG_RAW("OPEN\r\n");
  220. iso_tx_busy = false;
  221. }
  222. void usbd_video_close(uint8_t busid, uint8_t intf)
  223. {
  224. USB_LOG_RAW("CLOSE\r\n");
  225. tx_flag = 0;
  226. iso_tx_busy = false;
  227. }
  228. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  229. {
  230. if (nbytes == 0) {
  231. return;
  232. }
  233. if (usbd_video_stream_split_transfer(busid, ep)) {
  234. /* one frame has done */
  235. iso_tx_busy = false;
  236. }
  237. }
  238. static struct usbd_endpoint video_in_ep = {
  239. .ep_cb = usbd_video_iso_callback,
  240. .ep_addr = VIDEO_IN_EP
  241. };
  242. struct usbd_interface intf0;
  243. struct usbd_interface intf1;
  244. void video_init(uint8_t busid, uintptr_t reg_base)
  245. {
  246. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  247. usbd_desc_register(busid, &video_descriptor);
  248. #else
  249. usbd_desc_register(busid, video_descriptor);
  250. #endif
  251. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  252. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  253. usbd_add_endpoint(busid, &video_in_ep);
  254. usbd_initialize(busid, reg_base, usbd_event_handler);
  255. }
  256. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
  257. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
  258. void video_test(uint8_t busid)
  259. {
  260. uint32_t out_len;
  261. uint32_t packets;
  262. (void)packets;
  263. memset(packet_buffer, 0, sizeof(packet_buffer));
  264. while (1) {
  265. if (tx_flag) {
  266. iso_tx_busy = true;
  267. memcpy(frame_buffer, cherryusb_h264, sizeof(cherryusb_h264)); // cherryusb_h264 is a static yuyv frame buffer, so we need copy it to frame_buffer
  268. usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_h264), false);
  269. while (iso_tx_busy) {
  270. if (tx_flag == 0) {
  271. break;
  272. }
  273. }
  274. }
  275. }
  276. }