-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathframe.c
475 lines (409 loc) · 13.7 KB
/
frame.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (C) 2010-2012 Ken Tossell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the author nor other contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/**
* @defgroup frame Frame processing
* @brief Tools for managing frame buffers and converting between image formats
*/
#include "libuvc/libuvc.h"
#include "libuvc/libuvc_internal.h"
/** @internal */
uvc_error_t uvc_ensure_frame_size(uvc_frame_t *frame, size_t need_bytes) {
if (frame->library_owns_data) {
if (!frame->data || frame->data_bytes != need_bytes) {
frame->data_bytes = need_bytes;
frame->data = realloc(frame->data, frame->data_bytes);
}
if (!frame->data)
return UVC_ERROR_NO_MEM;
return UVC_SUCCESS;
} else {
if (!frame->data || frame->data_bytes < need_bytes)
return UVC_ERROR_NO_MEM;
return UVC_SUCCESS;
}
}
/** @brief Allocate a frame structure
* @ingroup frame
*
* @param data_bytes Number of bytes to allocate, or zero
* @return New frame, or NULL on error
*/
uvc_frame_t *uvc_allocate_frame(size_t data_bytes) {
uvc_frame_t *frame = malloc(sizeof(*frame));
if (!frame)
return NULL;
memset(frame, 0, sizeof(*frame));
frame->library_owns_data = 1;
if (data_bytes > 0) {
frame->data_bytes = data_bytes;
frame->data = malloc(data_bytes);
if (!frame->data) {
free(frame);
return NULL;
}
}
return frame;
}
/** @brief Free a frame structure
* @ingroup frame
*
* @param frame Frame to destroy
*/
void uvc_free_frame(uvc_frame_t *frame) {
if (frame->library_owns_data)
{
if (frame->data_bytes > 0)
free(frame->data);
if (frame->metadata_bytes > 0)
free(frame->metadata);
}
free(frame);
}
static inline unsigned char sat(int i) {
return (unsigned char)( i >= 255 ? 255 : (i < 0 ? 0 : i));
}
/** @brief Duplicate a frame, preserving color format
* @ingroup frame
*
* @param in Original frame
* @param out Duplicate frame
*/
uvc_error_t uvc_duplicate_frame(uvc_frame_t *in, uvc_frame_t *out) {
if (uvc_ensure_frame_size(out, in->data_bytes) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = in->frame_format;
out->step = in->step;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
memcpy(out->data, in->data, in->data_bytes);
if (in->metadata && in->metadata_bytes > 0)
{
if (out->metadata_bytes < in->metadata_bytes)
{
out->metadata = realloc(out->metadata, in->metadata_bytes);
}
out->metadata_bytes = in->metadata_bytes;
memcpy(out->metadata, in->metadata, in->metadata_bytes);
}
return UVC_SUCCESS;
}
#define YUYV2RGB_2(pyuv, prgb) { \
float r = 1.402f * ((pyuv)[3]-128); \
float g = -0.34414f * ((pyuv)[1]-128) - 0.71414f * ((pyuv)[3]-128); \
float b = 1.772f * ((pyuv)[1]-128); \
(prgb)[0] = sat(pyuv[0] + r); \
(prgb)[1] = sat(pyuv[0] + g); \
(prgb)[2] = sat(pyuv[0] + b); \
(prgb)[3] = sat(pyuv[2] + r); \
(prgb)[4] = sat(pyuv[2] + g); \
(prgb)[5] = sat(pyuv[2] + b); \
}
#define IYUYV2RGB_2(pyuv, prgb) { \
int r = (22987 * ((pyuv)[3] - 128)) >> 14; \
int g = (-5636 * ((pyuv)[1] - 128) - 11698 * ((pyuv)[3] - 128)) >> 14; \
int b = (29049 * ((pyuv)[1] - 128)) >> 14; \
(prgb)[0] = sat(*(pyuv) + r); \
(prgb)[1] = sat(*(pyuv) + g); \
(prgb)[2] = sat(*(pyuv) + b); \
(prgb)[3] = sat((pyuv)[2] + r); \
(prgb)[4] = sat((pyuv)[2] + g); \
(prgb)[5] = sat((pyuv)[2] + b); \
}
#define IYUYV2RGB_16(pyuv, prgb) IYUYV2RGB_8(pyuv, prgb); IYUYV2RGB_8(pyuv + 16, prgb + 24);
#define IYUYV2RGB_8(pyuv, prgb) IYUYV2RGB_4(pyuv, prgb); IYUYV2RGB_4(pyuv + 8, prgb + 12);
#define IYUYV2RGB_4(pyuv, prgb) IYUYV2RGB_2(pyuv, prgb); IYUYV2RGB_2(pyuv + 4, prgb + 6);
/** @brief Convert a frame from YUYV to RGB
* @ingroup frame
*
* @param in YUYV frame
* @param out RGB frame
*/
uvc_error_t uvc_yuyv2rgb(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_YUYV)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height * 3) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_RGB;
out->step = in->width * 3;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *prgb = out->data;
uint8_t *prgb_end = prgb + out->data_bytes;
while (prgb < prgb_end) {
IYUYV2RGB_8(pyuv, prgb);
prgb += 3 * 8;
pyuv += 2 * 8;
}
return UVC_SUCCESS;
}
#define IYUYV2BGR_2(pyuv, pbgr) { \
int r = (22987 * ((pyuv)[3] - 128)) >> 14; \
int g = (-5636 * ((pyuv)[1] - 128) - 11698 * ((pyuv)[3] - 128)) >> 14; \
int b = (29049 * ((pyuv)[1] - 128)) >> 14; \
(pbgr)[0] = sat(*(pyuv) + b); \
(pbgr)[1] = sat(*(pyuv) + g); \
(pbgr)[2] = sat(*(pyuv) + r); \
(pbgr)[3] = sat((pyuv)[2] + b); \
(pbgr)[4] = sat((pyuv)[2] + g); \
(pbgr)[5] = sat((pyuv)[2] + r); \
}
#define IYUYV2BGR_16(pyuv, pbgr) IYUYV2BGR_8(pyuv, pbgr); IYUYV2BGR_8(pyuv + 16, pbgr + 24);
#define IYUYV2BGR_8(pyuv, pbgr) IYUYV2BGR_4(pyuv, pbgr); IYUYV2BGR_4(pyuv + 8, pbgr + 12);
#define IYUYV2BGR_4(pyuv, pbgr) IYUYV2BGR_2(pyuv, pbgr); IYUYV2BGR_2(pyuv + 4, pbgr + 6);
/** @brief Convert a frame from YUYV to BGR
* @ingroup frame
*
* @param in YUYV frame
* @param out BGR frame
*/
uvc_error_t uvc_yuyv2bgr(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_YUYV)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height * 3) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_BGR;
out->step = in->width * 3;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *pbgr = out->data;
uint8_t *pbgr_end = pbgr + out->data_bytes;
while (pbgr < pbgr_end) {
IYUYV2BGR_8(pyuv, pbgr);
pbgr += 3 * 8;
pyuv += 2 * 8;
}
return UVC_SUCCESS;
}
#define IYUYV2Y(pyuv, py) { \
(py)[0] = (pyuv[0]); \
}
/** @brief Convert a frame from YUYV to Y (GRAY8)
* @ingroup frame
*
* @param in YUYV frame
* @param out GRAY8 frame
*/
uvc_error_t uvc_yuyv2y(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_YUYV)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_GRAY8;
out->step = in->width;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *py = out->data;
uint8_t *py_end = py + out->data_bytes;
while (py < py_end) {
IYUYV2Y(pyuv, py);
py += 1;
pyuv += 2;
}
return UVC_SUCCESS;
}
#define IYUYV2UV(pyuv, puv) { \
(puv)[0] = (pyuv[1]); \
}
/** @brief Convert a frame from YUYV to UV (GRAY8)
* @ingroup frame
*
* @param in YUYV frame
* @param out GRAY8 frame
*/
uvc_error_t uvc_yuyv2uv(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_YUYV)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_GRAY8;
out->step = in->width;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *puv = out->data;
uint8_t *puv_end = puv + out->data_bytes;
while (puv < puv_end) {
IYUYV2UV(pyuv, puv);
puv += 1;
pyuv += 2;
}
return UVC_SUCCESS;
}
#define IUYVY2RGB_2(pyuv, prgb) { \
int r = (22987 * ((pyuv)[2] - 128)) >> 14; \
int g = (-5636 * ((pyuv)[0] - 128) - 11698 * ((pyuv)[2] - 128)) >> 14; \
int b = (29049 * ((pyuv)[0] - 128)) >> 14; \
(prgb)[0] = sat((pyuv)[1] + r); \
(prgb)[1] = sat((pyuv)[1] + g); \
(prgb)[2] = sat((pyuv)[1] + b); \
(prgb)[3] = sat((pyuv)[3] + r); \
(prgb)[4] = sat((pyuv)[3] + g); \
(prgb)[5] = sat((pyuv)[3] + b); \
}
#define IUYVY2RGB_16(pyuv, prgb) IUYVY2RGB_8(pyuv, prgb); IUYVY2RGB_8(pyuv + 16, prgb + 24);
#define IUYVY2RGB_8(pyuv, prgb) IUYVY2RGB_4(pyuv, prgb); IUYVY2RGB_4(pyuv + 8, prgb + 12);
#define IUYVY2RGB_4(pyuv, prgb) IUYVY2RGB_2(pyuv, prgb); IUYVY2RGB_2(pyuv + 4, prgb + 6);
/** @brief Convert a frame from UYVY to RGB
* @ingroup frame
* @param ini UYVY frame
* @param out RGB frame
*/
uvc_error_t uvc_uyvy2rgb(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_UYVY)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height * 3) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_RGB;
out->step = in->width *3;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *prgb = out->data;
uint8_t *prgb_end = prgb + out->data_bytes;
while (prgb < prgb_end) {
IUYVY2RGB_8(pyuv, prgb);
prgb += 3 * 8;
pyuv += 2 * 8;
}
return UVC_SUCCESS;
}
#define IUYVY2BGR_2(pyuv, pbgr) { \
int r = (22987 * ((pyuv)[2] - 128)) >> 14; \
int g = (-5636 * ((pyuv)[0] - 128) - 11698 * ((pyuv)[2] - 128)) >> 14; \
int b = (29049 * ((pyuv)[0] - 128)) >> 14; \
(pbgr)[0] = sat((pyuv)[1] + b); \
(pbgr)[1] = sat((pyuv)[1] + g); \
(pbgr)[2] = sat((pyuv)[1] + r); \
(pbgr)[3] = sat((pyuv)[3] + b); \
(pbgr)[4] = sat((pyuv)[3] + g); \
(pbgr)[5] = sat((pyuv)[3] + r); \
}
#define IUYVY2BGR_16(pyuv, pbgr) IUYVY2BGR_8(pyuv, pbgr); IUYVY2BGR_8(pyuv + 16, pbgr + 24);
#define IUYVY2BGR_8(pyuv, pbgr) IUYVY2BGR_4(pyuv, pbgr); IUYVY2BGR_4(pyuv + 8, pbgr + 12);
#define IUYVY2BGR_4(pyuv, pbgr) IUYVY2BGR_2(pyuv, pbgr); IUYVY2BGR_2(pyuv + 4, pbgr + 6);
/** @brief Convert a frame from UYVY to BGR
* @ingroup frame
* @param ini UYVY frame
* @param out BGR frame
*/
uvc_error_t uvc_uyvy2bgr(uvc_frame_t *in, uvc_frame_t *out) {
if (in->frame_format != UVC_FRAME_FORMAT_UYVY)
return UVC_ERROR_INVALID_PARAM;
if (uvc_ensure_frame_size(out, in->width * in->height * 3) < 0)
return UVC_ERROR_NO_MEM;
out->width = in->width;
out->height = in->height;
out->frame_format = UVC_FRAME_FORMAT_BGR;
out->step = in->width *3;
out->sequence = in->sequence;
out->capture_time = in->capture_time;
out->capture_time_finished = in->capture_time_finished;
out->source = in->source;
uint8_t *pyuv = in->data;
uint8_t *pbgr = out->data;
uint8_t *pbgr_end = pbgr + out->data_bytes;
while (pbgr < pbgr_end) {
IUYVY2BGR_8(pyuv, pbgr);
pbgr += 3 * 8;
pyuv += 2 * 8;
}
return UVC_SUCCESS;
}
/** @brief Convert a frame to RGB
* @ingroup frame
*
* @param in non-RGB frame
* @param out RGB frame
*/
uvc_error_t uvc_any2rgb(uvc_frame_t *in, uvc_frame_t *out) {
switch (in->frame_format) {
#ifdef LIBUVC_HAS_JPEG
case UVC_FRAME_FORMAT_MJPEG:
return uvc_mjpeg2rgb(in, out);
#endif
case UVC_FRAME_FORMAT_YUYV:
return uvc_yuyv2rgb(in, out);
case UVC_FRAME_FORMAT_UYVY:
return uvc_uyvy2rgb(in, out);
case UVC_FRAME_FORMAT_RGB:
return uvc_duplicate_frame(in, out);
default:
return UVC_ERROR_NOT_SUPPORTED;
}
}
/** @brief Convert a frame to BGR
* @ingroup frame
*
* @param in non-BGR frame
* @param out BGR frame
*/
uvc_error_t uvc_any2bgr(uvc_frame_t *in, uvc_frame_t *out) {
switch (in->frame_format) {
case UVC_FRAME_FORMAT_YUYV:
return uvc_yuyv2bgr(in, out);
case UVC_FRAME_FORMAT_UYVY:
return uvc_uyvy2bgr(in, out);
case UVC_FRAME_FORMAT_BGR:
return uvc_duplicate_frame(in, out);
default:
return UVC_ERROR_NOT_SUPPORTED;
}
}