video_static_h264_template.c 9.1 KB

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