-
Notifications
You must be signed in to change notification settings - Fork 43
/
display-leap-data-sdl.c
243 lines (193 loc) · 5.14 KB
/
display-leap-data-sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
** Author: Elina Lijouvni
** License: GPL v3
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <SDL.h>
typedef struct ctx_s ctx_t;
struct ctx_s
{
SDL_Surface *screen;
};
void SDL_drawpixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: { /* Probably 15-bpp or 16-bpp */
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: { /* Slow 24-bpp mode, usually not used */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*(bufp+screen->format->Rshift/8) = R;
*(bufp+screen->format->Gshift/8) = G;
*(bufp+screen->format->Bshift/8) = B;
}
break;
case 4: { /* Probably 32-bpp */
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
}
void SDL_addpixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp += color;
}
break;
case 2: { /* Probably 15-bpp or 16-bpp */
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp += color;
}
break;
case 3: { /* Slow 24-bpp mode, usually not used */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*(bufp+screen->format->Rshift/8) += R;
*(bufp+screen->format->Gshift/8) += G;
*(bufp+screen->format->Bshift/8) += B;
}
break;
case 4: { /* Probably 32-bpp */
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp += color;
}
break;
}
}
#if 0
static void
fprintf_data(FILE *fp, const char * title, unsigned char *data, size_t size)
{
int i;
printf("%s", title);
for (i = 0; i < size; i++) {
if ( ! (i % 16) )
printf("\n");
printf("%02x ", data[i]);
}
printf("\n");
}
#endif
#define VFRAME_WIDTH 640
#define VFRAME_HEIGHT 240
#define VFRAME_SIZE (VFRAME_WIDTH * VFRAME_HEIGHT)
typedef struct frame_s frame_t;
struct frame_s
{
unsigned char left[ VFRAME_SIZE ];
unsigned char right[ VFRAME_SIZE ];
uint32_t id;
uint32_t data_len;
uint32_t state;
};
#define UVC_STREAM_EOF (1 << 1)
static void
process_video_frame(ctx_t *ctx, frame_t *frame)
{
int x, y;
SDL_Surface *screen = ctx->screen;
/* if ( SDL_MUSTLOCK(screen) ) { */
/* if ( SDL_LockSurface(screen) < 0 ) { */
/* return; */
/* } */
/* } */
for (y = 0; y < (VFRAME_HEIGHT * 2); y++) {
for (x = 0; x < VFRAME_WIDTH; x++) {
SDL_drawpixel(screen, x, y, frame->left[(y/2) * 640 + x], 0, 0);
SDL_addpixel(screen, x, y, 0, frame->right[(y/2) * 640 + x], 0);
}
}
/* if ( SDL_MUSTLOCK(screen) ) { */
/* SDL_UnlockSurface(screen); */
/* } */
SDL_UpdateRect(screen, 0, 0, 640, 480);
}
static void
process_usb_frame(ctx_t *ctx, frame_t *frame, unsigned char *data, int size)
{
int i;
int bHeaderLen = data[0];
int bmHeaderInfo = data[1];
uint32_t dwPresentationTime = *( (uint32_t *) &data[2] );
//printf("frame time: %u\n", dwPresentationTime);
if (frame->id == 0)
frame->id = dwPresentationTime;
for (i = bHeaderLen; i < size ; i += 2) {
if (frame->data_len >= VFRAME_SIZE)
break ;
frame->left[ frame->data_len ] = data[i];
frame->right[ frame->data_len ] = data[i+1];
frame->data_len++;
}
if (bmHeaderInfo & UVC_STREAM_EOF) {
//printf("End-of-Frame. Got %i\n", frame->data_len);
if (frame->data_len != VFRAME_SIZE) {
//printf("wrong frame size got %i expected %i\n", frame->data_len, VFRAME_SIZE);
frame->data_len = 0;
frame->id = 0;
return ;
}
process_video_frame(ctx, frame);
frame->data_len = 0;
frame->id = 0;
}
else {
if (dwPresentationTime != frame->id && frame->id > 0) {
//printf("mixed frame TS: dropping frame\n");
frame->id = dwPresentationTime;
/* frame->data_len = 0; */
/* frame->id = 0; */
/* return ; */
}
}
}
int
main(int argc, char *argv[])
{
ctx_t ctx_data;
memset(&ctx_data, 0, sizeof (ctx_data));
ctx_t *ctx = &ctx_data;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
ctx->screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( ctx->screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
frame_t frame;
memset(&frame, 0, sizeof (frame));
for ( ; ; ) {
unsigned char data[16384];
int usb_frame_size;
if ( feof(stdin) )
break ;
fread(&usb_frame_size, sizeof (usb_frame_size), 1, stdin);
fread(data, usb_frame_size, 1, stdin);
process_usb_frame(ctx, &frame, data, usb_frame_size);
}
return (0);
}