Skip to content

Commit 34737f9

Browse files
committed
update uvc 2ch to stream0 use yuy2, stream1 use mpeg
this help reduce sram requirement for example, also provide different format (uncompressed & mpeg)
1 parent 34870d3 commit 34737f9

File tree

4 files changed

+393
-414
lines changed

4 files changed

+393
-414
lines changed

examples/device/video_capture/src/usb_descriptors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ char const* string_desc_arr[] = {
5656
"TinyUSB", // 1: Manufacturer
5757
"TinyUSB Device", // 2: Product
5858
NULL, // 3: Serials will use unique ID if possible
59-
"TinyUSB UVC Control", // 4: UVC Interface
60-
"TinyUSB UVC Streaming", // 5: UVC Interface
59+
"UVC Control", // 4: UVC Interface
60+
"UVC Streaming", // 5: UVC Interface
6161
};
6262

6363
//--------------------------------------------------------------------+

examples/device/video_capture_2ch/src/images.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,18 +1652,11 @@ static const unsigned char frame_buffer[128 * (96 + 1) * 2] = {
16521652
0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80,
16531653
};
16541654

1655-
#else
1655+
#endif
16561656

1657-
// mpeg compressed data (not CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
1658-
#define color_bar_0_jpg_len 511
1659-
#define color_bar_1_jpg_len 512
1660-
#define color_bar_2_jpg_len 511
1661-
#define color_bar_3_jpg_len 511
1662-
#define color_bar_4_jpg_len 511
1663-
#define color_bar_5_jpg_len 512
1664-
#define color_bar_6_jpg_len 511
1665-
#define color_bar_7_jpg_len 511
1657+
#if 1
16661658

1659+
// mpeg compressed data (not CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
16671660
unsigned char color_bar_0_jpg[] = {
16681661
0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
16691662
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -1936,4 +1929,7 @@ unsigned char color_bar_7_jpg[] = {
19361929
0x42, 0x51, 0x59, 0x8c, 0xb1, 0x45, 0x00, 0x25, 0x15, 0x98, 0xc4, 0xa2, 0xb5, 0x32, 0x12, 0x8a,
19371930
0xcc, 0x63, 0x68, 0xad, 0x8d, 0x84, 0xa2, 0xb3, 0x18, 0x94, 0x56, 0xa6, 0x47, 0xff, 0xd9
19381931
};
1932+
1933+
1934+
19391935
#endif

examples/device/video_capture_2ch/src/main.c

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,27 @@ static unsigned frame_num[CFG_TUD_VIDEO_STREAMING] = {1};
116116
static unsigned tx_busy = 0;
117117
static unsigned interval_ms[CFG_TUD_VIDEO_STREAMING] = {1000 / FRAME_RATE};
118118

119-
#ifdef CFG_EXAMPLE_VIDEO_READONLY
120119
// For mcus that does not have enough SRAM for frame buffer, we use fixed frame data.
121120
// To further reduce the size, we use MJPEG format instead of YUY2.
122121
#include "images.h"
123122

124-
#if !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
125123
static struct {
126124
uint32_t size;
127125
uint8_t const *buffer;
128-
} const frames[] = {
129-
{color_bar_0_jpg_len, color_bar_0_jpg},
130-
{color_bar_1_jpg_len, color_bar_1_jpg},
131-
{color_bar_2_jpg_len, color_bar_2_jpg},
132-
{color_bar_3_jpg_len, color_bar_3_jpg},
133-
{color_bar_4_jpg_len, color_bar_4_jpg},
134-
{color_bar_5_jpg_len, color_bar_5_jpg},
135-
{color_bar_6_jpg_len, color_bar_6_jpg},
136-
{color_bar_7_jpg_len, color_bar_7_jpg},
126+
} const framebuf_mjpeg[] = {
127+
{sizeof(color_bar_0_jpg), color_bar_0_jpg},
128+
{sizeof(color_bar_1_jpg), color_bar_1_jpg},
129+
{sizeof(color_bar_2_jpg), color_bar_2_jpg},
130+
{sizeof(color_bar_3_jpg), color_bar_3_jpg},
131+
{sizeof(color_bar_4_jpg), color_bar_4_jpg},
132+
{sizeof(color_bar_5_jpg), color_bar_5_jpg},
133+
{sizeof(color_bar_6_jpg), color_bar_6_jpg},
134+
{sizeof(color_bar_7_jpg), color_bar_7_jpg},
137135
};
138-
#endif
139-
140-
#else
141136

142137
// YUY2 frame buffer
143-
static uint8_t frame_buffer[2][FRAME_WIDTH * FRAME_HEIGHT * 16 / 8];
138+
#define FRAMEBUF_SIZE (FRAME_WIDTH * FRAME_HEIGHT * 16 / 8)
139+
static uint8_t framebuf_yuy2[FRAMEBUF_SIZE];
144140

145141
static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
146142
/* EBU color bars: https://stackoverflow.com/questions/6939422 */
@@ -179,7 +175,26 @@ static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
179175
}
180176
}
181177

182-
#endif
178+
size_t get_framebuf(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, size_t fnum, void **fb) {
179+
uint32_t idx = ctl_idx + stm_idx;
180+
181+
if (idx == 0) {
182+
// stream 0 use uncompressed YUY2 frame
183+
fill_color_bar(framebuf_yuy2, frame_num[idx]);
184+
*fb = framebuf_yuy2;
185+
return FRAMEBUF_SIZE;
186+
}else {
187+
// stream 1 use MJPEG frame
188+
size_t const bar_id = fnum & 0x7;
189+
190+
*fb = (void*)(uintptr_t) framebuf_mjpeg[bar_id].buffer;
191+
return framebuf_mjpeg[bar_id].size;
192+
}
193+
}
194+
195+
//--------------------------------------------------------------------+
196+
//
197+
//--------------------------------------------------------------------+
183198

184199
void video_send_frame(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
185200
static unsigned start_ms[CFG_TUD_VIDEO_STREAMING] = {0, };
@@ -191,22 +206,16 @@ void video_send_frame(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
191206
frame_num[idx] = 0;
192207
return;
193208
}
209+
void* fp;
210+
size_t fb_size;
194211

195212
if (!(already_sent & (1u << idx))) {
196213
already_sent |= 1u << idx;
197214
tx_busy |= 1u << idx;
198215
start_ms[idx] = board_millis();
199-
#ifdef CFG_EXAMPLE_VIDEO_READONLY
200-
#if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
201-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*)(uintptr_t)&frame_buffer[(frame_num[idx] % (FRAME_WIDTH / 2)) * 4],
202-
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
203-
#else
204-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*)(uintptr_t)frames[frame_num[idx] % 8].buffer, frames[frame_num[idx] % 8].size);
205-
#endif
206-
#else
207-
fill_color_bar(frame_buffer[idx], frame_num[idx]);
208-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*) frame_buffer[idx], FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
209-
#endif
216+
217+
fb_size = get_framebuf(ctl_idx, stm_idx, frame_num[idx], &fp);
218+
tud_video_n_frame_xfer(ctl_idx, stm_idx, fp, fb_size);
210219
}
211220

212221
unsigned cur = board_millis();
@@ -215,17 +224,8 @@ void video_send_frame(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
215224
start_ms[idx] += interval_ms[idx];
216225
tx_busy |= 1u << idx;
217226

218-
#ifdef CFG_EXAMPLE_VIDEO_READONLY
219-
#if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPEG)
220-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*)(uintptr_t)&frame_buffer[(frame_num[idx] % (FRAME_WIDTH / 2)) * 4],
221-
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
222-
#else
223-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*)(uintptr_t)frames[frame_num[idx] % 8].buffer, frames[frame_num[idx] % 8].size);
224-
#endif
225-
#else
226-
fill_color_bar(frame_buffer[idx], frame_num[idx]);
227-
tud_video_n_frame_xfer(ctl_idx, stm_idx, (void*) frame_buffer[idx], FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
228-
#endif
227+
fb_size = get_framebuf(ctl_idx, stm_idx, frame_num[idx], &fp);
228+
tud_video_n_frame_xfer(ctl_idx, stm_idx, fp, fb_size);
229229
}
230230

231231

0 commit comments

Comments
 (0)