Change the video image to scrolling color bars.

This commit is contained in:
kkitayam 2021-10-09 19:51:51 +09:00
parent a1788da410
commit a3a18c9ed3
4 changed files with 1717 additions and 7707 deletions

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,6 @@
#include "tusb.h"
#include "usb_descriptors.h"
#include "images.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
@ -104,15 +103,54 @@ void tud_resume_cb(void)
//--------------------------------------------------------------------+
// USB Video
//--------------------------------------------------------------------+
static unsigned char const *frames[] = {
white_128x96_yuv, white_128x96_yuv, white_128x96_yuv,
t_128x96_yuv, ti_128x96_yuv, tin_128x96_yuv, logo_128x96_yuv,
logo_128x96_yuv, logo_128x96_yuv, logo_128x96_yuv, logo_128x96_yuv
};
static unsigned current_frame = 0;
static unsigned frame_num = 0;
static unsigned tx_busy = 0;
static unsigned interval_ms = 1000 / FRAME_RATE;
/* YUY2 frame buffer */
#ifdef CFG_EXAMPLE_VIDEO_READONLY
#include "images.h"
#else
static uint8_t frame_buffer[FRAME_WIDTH * FRAME_HEIGHT * 16 / 8];
static void fill_color_bar(uint8_t *buffer, unsigned start_position)
{
/* EBU color bars
* See also https://stackoverflow.com/questions/6939422 */
static uint8_t const bar_color[8][4] = {
/* Y, U, Y, V */
{ 235, 128, 235, 128}, /* 100% White */
{ 219, 16, 219, 138}, /* Yellow */
{ 188, 154, 188, 16}, /* Cyan */
{ 173, 42, 173, 26}, /* Green */
{ 78, 214, 78, 230}, /* Magenta */
{ 63, 102, 63, 240}, /* Red */
{ 32, 240, 32, 118}, /* Blue */
{ 16, 128, 16, 128}, /* Black */
};
uint8_t *p;
/* Generate the 1st line */
uint8_t *end = &buffer[FRAME_WIDTH * 2];
unsigned idx = (FRAME_WIDTH / 2 - 1) - (start_position % (FRAME_WIDTH / 2));
p = &buffer[idx * 4];
for (unsigned i = 0; i < 8; ++i) {
for (int j = 0; j < FRAME_WIDTH / (2 * 8); ++j) {
memcpy(p, &bar_color[i], 4);
p += 4;
if (end <= p) {
p = buffer;
}
}
}
/* Duplicate the 1st line to the others */
p = &buffer[FRAME_WIDTH * 2];
for (unsigned i = 1; i < FRAME_HEIGHT; ++i) {
memcpy(p, buffer, FRAME_WIDTH * 2);
p += FRAME_WIDTH * 2;
}
}
#endif
void video_task(void)
{
static unsigned start_ms = 0;
@ -120,14 +158,20 @@ void video_task(void)
if (!tud_video_n_streaming(0, 0)) {
already_sent = 0;
current_frame = 0;
frame_num = 0;
return;
}
if (!already_sent) {
already_sent = 1;
start_ms = board_millis();
tud_video_n_frame_xfer(0, 0, (void*)frames[current_frame], FRAME_WIDTH * FRAME_HEIGHT * 12/8);
#ifdef CFG_EXAMPLE_VIDEO_READONLY
tud_video_n_frame_xfer(0, 0, (void*)&frame_buffer[(frame_num % (FRAME_WIDTH / 2)) * 4],
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
#else
fill_color_bar(frame_buffer, frame_num);
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);
#endif
}
unsigned cur = board_millis();
@ -135,7 +179,13 @@ void video_task(void)
if (tx_busy) return;
start_ms += interval_ms;
tud_video_n_frame_xfer(0, 0, (void*)frames[current_frame], FRAME_WIDTH * FRAME_HEIGHT * 12/8);
#ifdef CFG_EXAMPLE_VIDEO_READONLY
tud_video_n_frame_xfer(0, 0, (void*)&frame_buffer[(frame_num % (FRAME_WIDTH / 2)) * 4],
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
#else
fill_color_bar(frame_buffer, frame_num);
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);
#endif
}
void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
@ -143,9 +193,7 @@ void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx
(void)ctl_idx; (void)stm_idx;
tx_busy = 0;
/* flip buffer */
++current_frame;
if (current_frame == sizeof(frames)/sizeof(frames[0]))
current_frame = 0;
++frame_num;
}
int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,

View File

@ -61,7 +61,7 @@ enum {
+ 7/* Endpoint */\
)
/* Windows support YUV2 and NV12
/* Windows support YUY2 and NV12
* https://docs.microsoft.com/en-us/windows-hardware/drivers/stream/usb-video-class-driver-overview */
#define TUD_VIDEO_DESC_CS_VS_FMT_YUY2(_fmtidx, _numfmtdesc, _frmidx, _asrx, _asry, _interlace, _cp) \
@ -97,12 +97,12 @@ enum {
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
/*bmaControls(1)*/0), \
/* Video stream format */ \
TUD_VIDEO_DESC_CS_VS_FMT_NV12(/*bFormatIndex*/1, /*bNumFrameDescriptors*/1,\
TUD_VIDEO_DESC_CS_VS_FMT_YUY2(/*bFormatIndex*/1, /*bNumFrameDescriptors*/1, \
/*bDefaultFrameIndex*/1, 0, 0, 0, /*bCopyProtect*/0), \
/* Video stream frame format */ \
TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT(/*bFrameIndex */1, 0, _width, _height, \
_width * _height * 12, _width * _height * 12 * _fps, \
_width * _height * 12, \
_width * _height * 16, _width * _height * 16 * _fps, \
_width * _height * 16, \
(10000000/_fps), (10000000/_fps), 10000000, 100000), \
TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING(VIDEO_COLOR_PRIMARIES_BT709, VIDEO_COLOR_XFER_CH_BT709, VIDEO_COLOR_COEF_SMPTE170M), \
/* VS alt 1 */\

View File

@ -6,7 +6,8 @@ CFLAGS += \
-mabi=aapcs \
-mcpu=cortex-m0plus \
-DCPU_MKL25Z128VLK4 \
-DCFG_TUSB_MCU=OPT_MCU_MKL25ZXX
-DCFG_TUSB_MCU=OPT_MCU_MKL25ZXX \
-DCFG_EXAMPLE_VIDEO_READONLY
LDFLAGS += \
-Wl,--defsym,__stack_size__=0x400 \