video_static_h264_template.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. const uint8_t video_descriptor[] = {
  41. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  42. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  43. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  44. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  45. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  46. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  47. VIDEO_VS_FORMAT_H264_DESCRIPTOR_INIT(0x01, 0x01),
  48. VIDEO_VS_FRAME_H264_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  49. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  50. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  51. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  52. ///////////////////////////////////////
  53. /// string0 descriptor
  54. ///////////////////////////////////////
  55. USB_LANGID_INIT(USBD_LANGID_STRING),
  56. ///////////////////////////////////////
  57. /// string1 descriptor
  58. ///////////////////////////////////////
  59. 0x14, /* bLength */
  60. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  61. 'C', 0x00, /* wcChar0 */
  62. 'h', 0x00, /* wcChar1 */
  63. 'e', 0x00, /* wcChar2 */
  64. 'r', 0x00, /* wcChar3 */
  65. 'r', 0x00, /* wcChar4 */
  66. 'y', 0x00, /* wcChar5 */
  67. 'U', 0x00, /* wcChar6 */
  68. 'S', 0x00, /* wcChar7 */
  69. 'B', 0x00, /* wcChar8 */
  70. ///////////////////////////////////////
  71. /// string2 descriptor
  72. ///////////////////////////////////////
  73. 0x26, /* bLength */
  74. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  75. 'C', 0x00, /* wcChar0 */
  76. 'h', 0x00, /* wcChar1 */
  77. 'e', 0x00, /* wcChar2 */
  78. 'r', 0x00, /* wcChar3 */
  79. 'r', 0x00, /* wcChar4 */
  80. 'y', 0x00, /* wcChar5 */
  81. 'U', 0x00, /* wcChar6 */
  82. 'S', 0x00, /* wcChar7 */
  83. 'B', 0x00, /* wcChar8 */
  84. ' ', 0x00, /* wcChar9 */
  85. 'U', 0x00, /* wcChar10 */
  86. 'V', 0x00, /* wcChar11 */
  87. 'C', 0x00, /* wcChar12 */
  88. ' ', 0x00, /* wcChar13 */
  89. 'D', 0x00, /* wcChar14 */
  90. 'E', 0x00, /* wcChar15 */
  91. 'M', 0x00, /* wcChar16 */
  92. 'O', 0x00, /* wcChar17 */
  93. ///////////////////////////////////////
  94. /// string3 descriptor
  95. ///////////////////////////////////////
  96. 0x16, /* bLength */
  97. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  98. '2', 0x00, /* wcChar0 */
  99. '0', 0x00, /* wcChar1 */
  100. '2', 0x00, /* wcChar2 */
  101. '1', 0x00, /* wcChar3 */
  102. '0', 0x00, /* wcChar4 */
  103. '3', 0x00, /* wcChar5 */
  104. '1', 0x00, /* wcChar6 */
  105. '0', 0x00, /* wcChar7 */
  106. '0', 0x00, /* wcChar8 */
  107. '0', 0x00, /* wcChar9 */
  108. #ifdef CONFIG_USB_HS
  109. ///////////////////////////////////////
  110. /// device qualifier descriptor
  111. ///////////////////////////////////////
  112. 0x0a,
  113. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  114. 0x00,
  115. 0x02,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x40,
  120. 0x00,
  121. 0x00,
  122. #endif
  123. 0x00
  124. };
  125. volatile bool tx_flag = 0;
  126. volatile bool iso_tx_busy = false;
  127. static void usbd_event_handler(uint8_t busid, uint8_t event)
  128. {
  129. switch (event) {
  130. case USBD_EVENT_RESET:
  131. break;
  132. case USBD_EVENT_CONNECTED:
  133. break;
  134. case USBD_EVENT_DISCONNECTED:
  135. break;
  136. case USBD_EVENT_RESUME:
  137. break;
  138. case USBD_EVENT_SUSPEND:
  139. break;
  140. case USBD_EVENT_CONFIGURED:
  141. tx_flag = 0;
  142. iso_tx_busy = false;
  143. break;
  144. case USBD_EVENT_SET_REMOTE_WAKEUP:
  145. break;
  146. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. void usbd_video_open(uint8_t busid, uint8_t intf)
  153. {
  154. tx_flag = 1;
  155. USB_LOG_RAW("OPEN\r\n");
  156. iso_tx_busy = false;
  157. }
  158. void usbd_video_close(uint8_t busid, uint8_t intf)
  159. {
  160. USB_LOG_RAW("CLOSE\r\n");
  161. tx_flag = 0;
  162. iso_tx_busy = false;
  163. }
  164. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  165. {
  166. //USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  167. iso_tx_busy = false;
  168. }
  169. static struct usbd_endpoint video_in_ep = {
  170. .ep_cb = usbd_video_iso_callback,
  171. .ep_addr = VIDEO_IN_EP
  172. };
  173. struct usbd_interface intf0;
  174. struct usbd_interface intf1;
  175. void video_init(uint8_t busid, uintptr_t reg_base)
  176. {
  177. usbd_desc_register(busid, video_descriptor);
  178. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  179. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  180. usbd_add_endpoint(busid, &video_in_ep);
  181. usbd_initialize(busid, reg_base, usbd_event_handler);
  182. }
  183. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[40 * 1024];
  184. void video_test(uint8_t busid)
  185. {
  186. uint32_t out_len;
  187. uint32_t packets;
  188. (void)packets;
  189. memset(packet_buffer, 0, 40 * 1024);
  190. while (1) {
  191. if (tx_flag) {
  192. packets = usbd_video_payload_fill(busid, (uint8_t *)cherryusb_h264, sizeof(cherryusb_h264), packet_buffer, &out_len);
  193. #if 1
  194. iso_tx_busy = true;
  195. usbd_ep_start_write(busid, VIDEO_IN_EP, packet_buffer, out_len);
  196. while (iso_tx_busy) {
  197. if (tx_flag == 0) {
  198. break;
  199. }
  200. }
  201. #else
  202. /* dwc2 must use this method */
  203. for (uint32_t i = 0; i < packets; i++) {
  204. if (i == (packets - 1)) {
  205. iso_tx_busy = true;
  206. usbd_ep_start_write(busid, VIDEO_IN_EP, &packet_buffer[i * MAX_PAYLOAD_SIZE], out_len - (packets - 1) * MAX_PAYLOAD_SIZE);
  207. while (iso_tx_busy) {
  208. if (tx_flag == 0) {
  209. break;
  210. }
  211. }
  212. } else {
  213. iso_tx_busy = true;
  214. usbd_ep_start_write(busid, VIDEO_IN_EP, &packet_buffer[i * MAX_PAYLOAD_SIZE], MAX_PAYLOAD_SIZE);
  215. while (iso_tx_busy) {
  216. if (tx_flag == 0) {
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. #endif
  223. }
  224. }
  225. }