Skip to content

Commit aed8051

Browse files
committed
Add motion jpeg
1 parent 4be7ffd commit aed8051

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

examples/device/video_capture/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
1212

1313
add_executable(${PROJECT})
1414

15+
if (FORCE_READONLY)
16+
target_compile_definitions(${PROJECT} PRIVATE
17+
CFG_EXAMPLE_VIDEO_READONLY
18+
)
19+
endif()
20+
1521
# Example source
1622
target_sources(${PROJECT} PUBLIC
1723
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c

examples/device/video_capture/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
include ../../../tools/top.mk
22
include ../../make.mk
33

4+
ifeq ($(DISABLE_MJPG),1)
5+
CFLAGS += -DCFG_EXAMPLE_VIDEO_DISABLE_MJPG
6+
endif
7+
ifeq ($(FORCE_READONLY),1)
8+
CFLAGS += -DCFG_EXAMPLE_VIDEO_READONLY
9+
endif
10+
411
INC += \
512
src \
613
$(TOP)/hw \

examples/device/video_capture/src/images.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
12
static const unsigned char frame_buffer[128 * (96 + 1) * 2] = {
23
/* 0 */
34
0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80, 0xeb, 0x80,
@@ -1649,6 +1650,7 @@ static const unsigned char frame_buffer[128 * (96 + 1) * 2] = {
16491650
0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80,
16501651
0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80, 0x10, 0x80,
16511652
};
1653+
#else
16521654

16531655
#define color_bar_0_jpg_len 511
16541656
#define color_bar_1_jpg_len 512
@@ -1931,3 +1933,4 @@ unsigned char color_bar_7_jpg[] = {
19311933
0x42, 0x51, 0x59, 0x8c, 0xb1, 0x45, 0x00, 0x25, 0x15, 0x98, 0xc4, 0xa2, 0xb5, 0x32, 0x12, 0x8a,
19321934
0xcc, 0x63, 0x68, 0xad, 0x8d, 0x84, 0xa2, 0xb3, 0x18, 0x94, 0x56, 0xa6, 0x47, 0xff, 0xd9
19331935
};
1936+
#endif

examples/device/video_capture/src/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ static unsigned interval_ms = 1000 / FRAME_RATE;
112112
/* YUY2 frame buffer */
113113
#ifdef CFG_EXAMPLE_VIDEO_READONLY
114114
#include "images.h"
115+
116+
# if !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
115117
static struct {
116118
uint32_t size;
117119
uint8_t const *buffer;
@@ -125,6 +127,8 @@ static struct {
125127
{color_bar_6_jpg_len, color_bar_6_jpg},
126128
{color_bar_7_jpg_len, color_bar_7_jpg},
127129
};
130+
# endif
131+
128132
#else
129133
static uint8_t frame_buffer[FRAME_WIDTH * FRAME_HEIGHT * 16 / 8];
130134
static void fill_color_bar(uint8_t *buffer, unsigned start_position)
@@ -181,7 +185,12 @@ void video_task(void)
181185
already_sent = 1;
182186
start_ms = board_millis();
183187
#ifdef CFG_EXAMPLE_VIDEO_READONLY
188+
# if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
189+
tud_video_n_frame_xfer(0, 0, (void*)(uintptr_t)&frame_buffer[(frame_num % (FRAME_WIDTH / 2)) * 4],
190+
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
191+
# else
184192
tud_video_n_frame_xfer(0, 0, (void*)(uintptr_t)frames[frame_num % 8].buffer, frames[frame_num % 8].size);
193+
# endif
185194
#else
186195
fill_color_bar(frame_buffer, frame_num);
187196
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);
@@ -194,7 +203,12 @@ void video_task(void)
194203
start_ms += interval_ms;
195204

196205
#ifdef CFG_EXAMPLE_VIDEO_READONLY
206+
# if defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
207+
tud_video_n_frame_xfer(0, 0, (void*)(uintptr_t)&frame_buffer[(frame_num % (FRAME_WIDTH / 2)) * 4],
208+
FRAME_WIDTH * FRAME_HEIGHT * 16/8);
209+
# else
197210
tud_video_n_frame_xfer(0, 0, (void*)(uintptr_t)frames[frame_num % 8].buffer, frames[frame_num % 8].size);
211+
# endif
198212
#else
199213
fill_color_bar(frame_buffer, frame_num);
200214
tud_video_n_frame_xfer(0, 0, (void*)frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16/8);

examples/device/video_capture/src/usb_descriptors.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ uint8_t const * tud_descriptor_device_cb(void)
7575
// Configuration Descriptor
7676
//--------------------------------------------------------------------+
7777

78+
#if defined(CFG_EXAMPLE_VIDEO_READONLY) && !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
79+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_VIDEO_CAPTURE_DESC_MJPG_LEN)
80+
#else
7881
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_VIDEO_CAPTURE_DESC_LEN)
82+
#endif
7983

8084
#if TU_CHECK_MCU(OPT_MCU_LPC175X_6X, OPT_MCU_LPC177X_8X, OPT_MCU_LPC40XX)
8185
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
@@ -96,9 +100,15 @@ uint8_t const desc_fs_configuration[] =
96100
// Config number, interface count, string index, total length, attribute, power in mA
97101
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0, 500),
98102
// IAD for Video Control
103+
#if defined(CFG_EXAMPLE_VIDEO_READONLY) && !defined(CFG_EXAMPLE_VIDEO_DISABLE_MJPG)
104+
TUD_VIDEO_CAPTURE_DESCRIPTOR_MJPG(4, EPNUM_VIDEO_IN,
105+
FRAME_WIDTH, FRAME_HEIGHT, FRAME_RATE,
106+
CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE)
107+
#else
99108
TUD_VIDEO_CAPTURE_DESCRIPTOR(4, EPNUM_VIDEO_IN,
100109
FRAME_WIDTH, FRAME_HEIGHT, FRAME_RATE,
101110
CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE)
111+
#endif
102112
};
103113

104114
// Invoked when received GET CONFIGURATION DESCRIPTOR

examples/device/video_capture/src/usb_descriptors.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ enum {
4444
};
4545

4646
#define TUD_VIDEO_CAPTURE_DESC_LEN (\
47+
TUD_VIDEO_DESC_IAD_LEN\
48+
/* control */\
49+
+ TUD_VIDEO_DESC_STD_VC_LEN\
50+
+ (TUD_VIDEO_DESC_CS_VC_LEN + 1/*bInCollection*/)\
51+
+ TUD_VIDEO_DESC_CAMERA_TERM_LEN\
52+
+ TUD_VIDEO_DESC_OUTPUT_TERM_LEN\
53+
/* Interface 1, Alternate 0 */\
54+
+ TUD_VIDEO_DESC_STD_VS_LEN\
55+
+ (TUD_VIDEO_DESC_CS_VS_IN_LEN + 1/*bNumFormats x bControlSize*/)\
56+
+ TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN\
57+
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN\
58+
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN\
59+
/* Interface 1, Alternate 1 */\
60+
+ TUD_VIDEO_DESC_STD_VS_LEN\
61+
+ 7/* Endpoint */\
62+
)
63+
64+
#define TUD_VIDEO_CAPTURE_DESC_MJPG_LEN (\
4765
TUD_VIDEO_DESC_IAD_LEN\
4866
/* control */\
4967
+ TUD_VIDEO_DESC_STD_VC_LEN\
@@ -74,6 +92,43 @@ enum {
7492
TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR(_fmtidx, _numfmtdesc, TUD_VIDEO_GUID_I420, 12, _frmidx, _asrx, _asry, _interlace, _cp)
7593

7694
#define TUD_VIDEO_CAPTURE_DESCRIPTOR(_stridx, _epin, _width, _height, _fps, _epsize) \
95+
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
96+
/* Video control 0 */ \
97+
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
98+
TUD_VIDEO_DESC_CS_VC( /* UVC 1.5*/ 0x0150, \
99+
/* wTotalLength - bLength */ \
100+
TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, \
101+
UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \
102+
TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0,\
103+
/*wObjectiveFocalLengthMin*/0, /*wObjectiveFocalLengthMax*/0,\
104+
/*wObjectiveFocalLength*/0, /*bmControls*/0), \
105+
TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \
106+
/* Video stream alt. 0 */ \
107+
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 0, _stridx), \
108+
/* Video stream header for without still image capture */ \
109+
TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \
110+
/*wTotalLength - bLength */\
111+
TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN\
112+
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN\
113+
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
114+
_epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \
115+
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
116+
/*bmaControls(1)*/0), \
117+
/* Video stream format */ \
118+
TUD_VIDEO_DESC_CS_VS_FMT_YUY2(/*bFormatIndex*/1, /*bNumFrameDescriptors*/1, \
119+
/*bDefaultFrameIndex*/1, 0, 0, 0, /*bCopyProtect*/0), \
120+
/* Video stream frame format */ \
121+
TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT(/*bFrameIndex */1, 0, _width, _height, \
122+
_width * _height * 16, _width * _height * 16 * _fps, \
123+
_width * _height * 16, \
124+
(10000000/_fps), (10000000/_fps), (10000000/_fps)*_fps, (10000000/_fps)), \
125+
TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING(VIDEO_COLOR_PRIMARIES_BT709, VIDEO_COLOR_XFER_CH_BT709, VIDEO_COLOR_COEF_SMPTE170M), \
126+
/* VS alt 1 */\
127+
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 1, 1, _stridx), \
128+
/* EP */ \
129+
TUD_VIDEO_DESC_EP_ISO(_epin, _epsize, 1)
130+
131+
#define TUD_VIDEO_CAPTURE_DESCRIPTOR_MJPG(_stridx, _epin, _width, _height, _fps, _epsize) \
77132
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
78133
/* Video control 0 */ \
79134
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
@@ -102,7 +157,7 @@ enum {
102157
/* Video stream frame format */ \
103158
TUD_VIDEO_DESC_CS_VS_FRM_MJPEG_CONT(/*bFrameIndex */1, 0, _width, _height, \
104159
_width * _height * 16, _width * _height * 16 * _fps, \
105-
_width * _height * 16, \
160+
_width * _height * 16 / 8, \
106161
(10000000/_fps), (10000000/_fps), (10000000/_fps)*_fps, (10000000/_fps)), \
107162
TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING(VIDEO_COLOR_PRIMARIES_BT709, VIDEO_COLOR_XFER_CH_BT709, VIDEO_COLOR_COEF_SMPTE170M), \
108163
/* VS alt 1 */\

0 commit comments

Comments
 (0)