-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps3eye.cpp
1466 lines (1231 loc) · 49.9 KB
/
ps3eye.cpp
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// source code from https://github.com/inspirit/PS3EYEDriver
#include "ps3eye.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#if defined WIN32 || defined _WIN32 || defined WINCE
#include <windows.h>
#include <algorithm>
#ifdef __MINGW32__
void SetThreadName(const char* threadName)
{
// Not sure how to implement this on mingw
}
#else
const DWORD MS_VC_EXCEPTION=0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName(uint32_t thread_id, const char* thread_name)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = thread_name;
info.dwThreadID = thread_id;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
}
void SetThreadName(const char* thread_name)
{
SetThreadName(GetCurrentThreadId(), thread_name);
}
#endif
#else
#include <sys/time.h>
#include <time.h>
#if defined __MACH__ && defined __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif
void SetThreadName(const char* threadName)
{
// Not sure how to implement this on linux/osx, so left empty...
}
#endif
#ifdef _MSC_VER
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': snprintf
#define snprintf _snprintf
#endif
namespace ps3eye {
#define TRANSFER_SIZE 16384
#define NUM_TRANSFERS 5
#define OV534_REG_ADDRESS 0xf1 /* sensor address */
#define OV534_REG_SUBADDR 0xf2
#define OV534_REG_WRITE 0xf3
#define OV534_REG_READ 0xf4
#define OV534_REG_OPERATION 0xf5
#define OV534_REG_STATUS 0xf6
#define OV534_OP_WRITE_3 0x37
#define OV534_OP_WRITE_2 0x33
#define OV534_OP_READ_2 0xf9
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(_A) (sizeof(_A) / sizeof((_A)[0]))
#endif
static const uint8_t ov534_reg_initdata[][2] = {
{ 0xe7, 0x3a },
{ OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
{ 0x92, 0x01 },
{ 0x93, 0x18 },
{ 0x94, 0x10 },
{ 0x95, 0x10 },
{ 0xE2, 0x00 },
{ 0xE7, 0x3E },
{ 0x96, 0x00 },
{ 0x97, 0x20 },
{ 0x97, 0x20 },
{ 0x97, 0x20 },
{ 0x97, 0x0A },
{ 0x97, 0x3F },
{ 0x97, 0x4A },
{ 0x97, 0x20 },
{ 0x97, 0x15 },
{ 0x97, 0x0B },
{ 0x8E, 0x40 },
{ 0x1F, 0x81 },
{ 0xC0, 0x50 },
{ 0xC1, 0x3C },
{ 0xC2, 0x01 },
{ 0xC3, 0x01 },
{ 0x50, 0x89 },
{ 0x88, 0x08 },
{ 0x8D, 0x00 },
{ 0x8E, 0x00 },
{ 0x1C, 0x00 }, /* video data start (V_FMT) */
{ 0x1D, 0x00 }, /* RAW8 mode */
{ 0x1D, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
{ 0x1D, 0x00 }, /* payload size */
{ 0x1D, 0x01 }, /* frame size = 0x012C00 * 4 = 307200 bytes (640 * 480 @ 8bpp) */
{ 0x1D, 0x2C }, /* frame size */
{ 0x1D, 0x00 }, /* frame size */
{ 0x1C, 0x0A }, /* video data start (V_CNTL0) */
{ 0x1D, 0x08 }, /* turn on UVC header */
{ 0x1D, 0x0E },
{ 0x34, 0x05 },
{ 0xE3, 0x04 },
{ 0x89, 0x00 },
{ 0x76, 0x00 },
{ 0xE7, 0x2E },
{ 0x31, 0xF9 },
{ 0x25, 0x42 },
{ 0x21, 0xF0 },
{ 0xE5, 0x04 }
};
static const uint8_t ov772x_reg_initdata[][2] = {
{ 0x12, 0x80 }, /* reset */
{ 0x3D, 0x00 },
{ 0x12, 0x01 }, /* Processed Bayer RAW (8bit) */
{ 0x11, 0x01 },
{ 0x14, 0x40 },
{ 0x15, 0x00 },
{ 0x63, 0xAA }, // AWB
{ 0x64, 0x87 },
{ 0x66, 0x00 },
{ 0x67, 0x02 },
{ 0x17, 0x26 },
{ 0x18, 0xA0 },
{ 0x19, 0x07 },
{ 0x1A, 0xF0 },
{ 0x29, 0xA0 },
{ 0x2A, 0x00 },
{ 0x2C, 0xF0 },
{ 0x20, 0x10 },
{ 0x4E, 0x0F },
{ 0x3E, 0xF3 },
{ 0x0D, 0x41 },
{ 0x32, 0x00 },
{ 0x13, 0xF0 }, // COM8 - jfrancois 0xf0 orig x0f7
{ 0x22, 0x7F },
{ 0x23, 0x03 },
{ 0x24, 0x40 },
{ 0x25, 0x30 },
{ 0x26, 0xA1 },
{ 0x2A, 0x00 },
{ 0x2B, 0x00 },
{ 0x13, 0xF7 },
{ 0x0C, 0xC0 },
{ 0x11, 0x00 },
{ 0x0D, 0x41 },
{ 0x8E, 0x00 }, // De-noise threshold - jfrancois 0x00 - orig 0x04
};
static const uint8_t bridge_start_vga[][2] = {
{0x1c, 0x00},
{0x1d, 0x00},
{0x1d, 0x02},
{0x1d, 0x00},
{0x1d, 0x01}, /* frame size = 0x012C00 * 4 = 307200 bytes (640 * 480 @ 8bpp) */
{0x1d, 0x2C}, /* frame size */
{0x1d, 0x00}, /* frame size */
{0xc0, 0x50},
{0xc1, 0x3c},
};
static const uint8_t sensor_start_vga[][2] = {
{0x12, 0x01},
{0x17, 0x26},
{0x18, 0xa0},
{0x19, 0x07},
{0x1a, 0xf0},
{0x29, 0xa0},
{0x2c, 0xf0},
{0x65, 0x20},
};
static const uint8_t bridge_start_qvga[][2] = {
{0x1c, 0x00},
{0x1d, 0x00},
{0x1d, 0x02},
{0x1d, 0x00},
{0x1d, 0x00}, /* frame size = 0x004B00 * 4 = 76800 bytes (320 * 240 @ 8bpp) */
{0x1d, 0x4b}, /* frame size */
{0x1d, 0x00}, /* frame size */
{0xc0, 0x28},
{0xc1, 0x1e},
};
static const uint8_t sensor_start_qvga[][2] = {
{0x12, 0x41},
{0x17, 0x3f},
{0x18, 0x50},
{0x19, 0x03},
{0x1a, 0x78},
{0x29, 0x50},
{0x2c, 0x78},
{0x65, 0x2f},
};
/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
#define UVC_STREAM_EOH (1 << 7)
#define UVC_STREAM_ERR (1 << 6)
#define UVC_STREAM_STI (1 << 5)
#define UVC_STREAM_RES (1 << 4)
#define UVC_STREAM_SCR (1 << 3)
#define UVC_STREAM_PTS (1 << 2)
#define UVC_STREAM_EOF (1 << 1)
#define UVC_STREAM_FID (1 << 0)
/* packet types when moving from iso buf to frame buf */
enum gspca_packet_type {
DISCARD_PACKET,
FIRST_PACKET,
INTER_PACKET,
LAST_PACKET
};
/*
* look for an input transfer endpoint in an alternate setting
* libusb_endpoint_descriptor
*/
static uint8_t find_ep(struct libusb_device *device)
{
const struct libusb_interface_descriptor *altsetting = NULL;
const struct libusb_endpoint_descriptor *ep;
struct libusb_config_descriptor *config = NULL;
int i;
uint8_t ep_addr = 0;
libusb_get_active_config_descriptor(device, &config);
if (!config) return 0;
for (i = 0; i < config->bNumInterfaces; i++) {
altsetting = config->interface[i].altsetting;
if (altsetting[0].bInterfaceNumber == 0) {
break;
}
}
for (i = 0; i < altsetting->bNumEndpoints; i++) {
ep = &altsetting->endpoint[i];
if ((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_BULK
&& ep->wMaxPacketSize != 0)
{
ep_addr = ep->bEndpointAddress;
break;
}
}
libusb_free_config_descriptor(config);
return ep_addr;
}
const uint16_t PS3EYECam::VENDOR_ID = 0x1415;
const uint16_t PS3EYECam::PRODUCT_ID = 0x2000;
class USBMgr
{
public:
USBMgr();
~USBMgr();
static std::shared_ptr<USBMgr> instance();
int listDevices(std::vector<PS3EYECam::PS3EYERef>& list);
void cameraStarted();
void cameraStopped();
static std::shared_ptr<USBMgr> sInstance;
static int sTotalDevices;
private:
libusb_context* usb_context;
std::thread update_thread;
std::atomic_bool exit_signaled;
std::atomic_int active_camera_count;
USBMgr(const USBMgr&);
void operator=(const USBMgr&);
void startTransferThread();
void stopTransferThread();
void transferThreadFunc();
};
std::shared_ptr<USBMgr> USBMgr::sInstance;
int USBMgr::sTotalDevices = 0;
USBMgr::USBMgr()
{
exit_signaled = false;
active_camera_count = 0;
libusb_init(&usb_context);
libusb_set_debug(usb_context, 1);
}
USBMgr::~USBMgr()
{
debug("USBMgr destructor\n");
libusb_exit(usb_context);
}
std::shared_ptr<USBMgr> USBMgr::instance()
{
if( !sInstance ) {
sInstance = std::shared_ptr<USBMgr>( new USBMgr );
}
return sInstance;
}
void USBMgr::cameraStarted()
{
if (active_camera_count++ == 0)
startTransferThread();
}
void USBMgr::cameraStopped()
{
if (--active_camera_count == 0)
stopTransferThread();
}
void USBMgr::startTransferThread()
{
update_thread = std::thread(&USBMgr::transferThreadFunc, this);
}
void USBMgr::stopTransferThread()
{
exit_signaled = true;
update_thread.join();
// Reset the exit signal flag.
// If we don't and we call startTransferThread() again, transferThreadFunc will exit immediately.
exit_signaled = false;
}
void USBMgr::transferThreadFunc()
{
SetThreadName("PS3EyeDriver Transfer Thread");
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 50 * 1000; // ms
while (!exit_signaled)
{
libusb_handle_events_timeout_completed(usb_context, &tv, NULL);
}
}
int USBMgr::listDevices( std::vector<PS3EYECam::PS3EYERef>& list )
{
libusb_device *dev;
libusb_device **devs;
libusb_device_handle *devhandle;
int i = 0;
int cnt;
cnt = (int)libusb_get_device_list(usb_context, &devs);
if (cnt < 0) {
debug("Error Device scan\n");
}
cnt = 0;
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(dev, &desc);
if (desc.idVendor == PS3EYECam::VENDOR_ID && desc.idProduct == PS3EYECam::PRODUCT_ID)
{
int err = libusb_open(dev, &devhandle);
if (err == 0)
{
libusb_close(devhandle);
list.push_back( PS3EYECam::PS3EYERef( new PS3EYECam(dev) ) );
libusb_ref_device(dev);
cnt++;
}
}
}
libusb_free_device_list(devs, 1);
return cnt;
}
static void LIBUSB_CALL transfer_completed_callback(struct libusb_transfer *xfr);
class FrameQueue
{
public:
FrameQueue(uint32_t frame_size) :
frame_size (frame_size),
num_frames (2),
frame_buffer ((uint8_t*)malloc(frame_size * num_frames)),
head (0),
tail (0),
available (0)
{
}
~FrameQueue()
{
free(frame_buffer);
}
uint8_t* GetFrameBufferStart()
{
return frame_buffer;
}
uint8_t* Enqueue()
{
uint8_t* new_frame = NULL;
std::lock_guard<std::mutex> lock(mutex);
// Unlike traditional producer/consumer, we don't block the producer if the buffer is full (ie. the consumer is not reading data fast enough).
// Instead, if the buffer is full, we simply return the current frame pointer, causing the producer to overwrite the previous frame.
// This allows performance to degrade gracefully: if the consumer is not fast enough (< Camera FPS), it will miss frames, but if it is fast enough (>= Camera FPS), it will see everything.
//
// Note that because the the producer is writing directly to the ring buffer, we can only ever be a maximum of num_frames-1 ahead of the consumer,
// otherwise the producer could overwrite the frame the consumer is currently reading (in case of a slow consumer)
if (available >= num_frames - 1)
{
return frame_buffer + head * frame_size;
}
// Note: we don't need to copy any data to the buffer since the USB packets are directly written to the frame buffer.
// We just need to update head and available count to signal to the consumer that a new frame is available
head = (head + 1) % num_frames;
available++;
// Determine the next frame pointer that the producer should write to
new_frame = frame_buffer + head * frame_size;
// Signal consumer that data became available
empty_condition.notify_one();
return new_frame;
}
void Dequeue(uint8_t* new_frame, int frame_width, int frame_height, PS3EYECam::EOutputFormat outputFormat)
{
std::unique_lock<std::mutex> lock(mutex);
// If there is no data in the buffer, wait until data becomes available
empty_condition.wait(lock, [this] () { return available != 0; });
// Copy from internal buffer
uint8_t* source = frame_buffer + frame_size * tail;
if (outputFormat == PS3EYECam::EOutputFormat::Bayer)
{
memcpy(new_frame, source, frame_size);
}
else if (outputFormat == PS3EYECam::EOutputFormat::BGR ||
outputFormat == PS3EYECam::EOutputFormat::RGB)
{
DebayerRGB(frame_width, frame_height, source, new_frame, outputFormat == PS3EYECam::EOutputFormat::BGR);
}
else if (outputFormat == PS3EYECam::EOutputFormat::Gray)
{
DebayerGray(frame_width, frame_height, source, new_frame);
}
// Update tail and available count
tail = (tail + 1) % num_frames;
available--;
}
void DebayerGray(int frame_width, int frame_height, const uint8_t* inBayer, uint8_t* outBuffer)
{
// PSMove output is in the following Bayer format (GRBG):
//
// G R G R G R
// B G B G B G
// G R G R G R
// B G B G B G
//
// This is the normal Bayer pattern shifted left one place.
int source_stride = frame_width;
const uint8_t* source_row = inBayer; // Start at first bayer pixel
int dest_stride = frame_width;
uint8_t* dest_row = outBuffer + dest_stride + 1; // We start outputting at the second pixel of the second row's G component
uint32_t R,G,B;
// Fill rows 1 to height-1 of the destination buffer. First and last row are filled separately (they are copied from the second row and second-to-last rows respectively)
for (int y = 0; y < frame_height-2; source_row += source_stride, dest_row += dest_stride, ++y)
{
const uint8_t* source = source_row;
const uint8_t* source_end = source + (source_stride-2); // -2 to deal with the fact that we're starting at the second pixel of the row and should end at the second-to-last pixel of the row (first and last are filled separately)
uint8_t* dest = dest_row;
// Row starting with Green
if (y % 2 == 0)
{
// Fill first pixel (green)
B = (source[source_stride] + source[source_stride + 2] + 1) >> 1;
G = source[source_stride + 1];
R = (source[1] + source[source_stride * 2 + 1] + 1) >> 1;
*dest = (uint8_t)((R*77 + G*151 + B*28)>>8);
source++;
dest++;
// Fill remaining pixel
for (; source <= source_end - 2; source += 2, dest += 2)
{
// Blue pixel
B = source[source_stride + 1];
G = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;
R = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;
dest[0] = (uint8_t)((R*77 + G*151 + B*28)>>8);
// Green pixel
B = (source[source_stride + 1] + source[source_stride + 3] + 1) >> 1;
G = source[source_stride + 2];
R = (source[2] + source[source_stride * 2 + 2] + 1) >> 1;
dest[1] = (uint8_t)((R*77 + G*151 + B*28)>>8);
}
}
else
{
for (; source <= source_end - 2; source += 2, dest += 2)
{
// Red pixel
B = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;;
G = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;;
R = source[source_stride + 1];
dest[0] = (uint8_t)((R*77 + G*151 + B*28)>>8);
// Green pixel
B = (source[2] + source[source_stride * 2 + 2] + 1) >> 1;
G = source[source_stride + 2];
R = (source[source_stride + 1] + source[source_stride + 3] + 1) >> 1;
dest[1] = (uint8_t)((R*77 + G*151 + B*28)>>8);
}
}
if (source < source_end)
{
B = source[source_stride + 1];
G = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;
R = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;;
dest[0] = (uint8_t)((R*77 + G*151 + B*28)>>8);
source++;
dest++;
}
// Fill first pixel of row (copy second pixel)
uint8_t* first_pixel = dest_row-1;
first_pixel[0] = dest_row[0];
// Fill last pixel of row (copy second-to-last pixel). Note: dest row starts at the *second* pixel of the row, so dest_row + (width-2) * num_output_channels puts us at the last pixel of the row
uint8_t* last_pixel = dest_row + (frame_width - 2);
uint8_t* second_to_last_pixel = last_pixel - 1;
last_pixel[0] = second_to_last_pixel[0];
}
// Fill first & last row
for (int i = 0; i < dest_stride; i++)
{
outBuffer[i] = outBuffer[i + dest_stride];
outBuffer[i + (frame_height - 1)*dest_stride] = outBuffer[i + (frame_height - 2)*dest_stride];
}
}
void DebayerRGB(int frame_width, int frame_height, const uint8_t* inBayer, uint8_t* outBuffer, bool inBGR)
{
// PSMove output is in the following Bayer format (GRBG):
//
// G R G R G R
// B G B G B G
// G R G R G R
// B G B G B G
//
// This is the normal Bayer pattern shifted left one place.
int num_output_channels = 3;
int source_stride = frame_width;
const uint8_t* source_row = inBayer; // Start at first bayer pixel
int dest_stride = frame_width * num_output_channels;
uint8_t* dest_row = outBuffer + dest_stride + num_output_channels + 1; // We start outputting at the second pixel of the second row's G component
int swap_br = inBGR ? 1 : -1;
// Fill rows 1 to height-1 of the destination buffer. First and last row are filled separately (they are copied from the second row and second-to-last rows respectively)
for (int y = 0; y < frame_height-2; source_row += source_stride, dest_row += dest_stride, ++y)
{
const uint8_t* source = source_row;
const uint8_t* source_end = source + (source_stride-2); // -2 to deal with the fact that we're starting at the second pixel of the row and should end at the second-to-last pixel of the row (first and last are filled separately)
uint8_t* dest = dest_row;
// Row starting with Green
if (y % 2 == 0)
{
// Fill first pixel (green)
dest[-1*swap_br] = (source[source_stride] + source[source_stride + 2] + 1) >> 1;
dest[0] = source[source_stride + 1];
dest[1*swap_br] = (source[1] + source[source_stride * 2 + 1] + 1) >> 1;
source++;
dest += num_output_channels;
// Fill remaining pixel
for (; source <= source_end - 2; source += 2, dest += num_output_channels * 2)
{
// Blue pixel
uint8_t* cur_pixel = dest;
cur_pixel[-1*swap_br] = source[source_stride + 1];
cur_pixel[0] = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;
cur_pixel[1*swap_br] = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;
// Green pixel
uint8_t* next_pixel = cur_pixel+num_output_channels;
next_pixel[-1*swap_br] = (source[source_stride + 1] + source[source_stride + 3] + 1) >> 1;
next_pixel[0] = source[source_stride + 2];
next_pixel[1*swap_br] = (source[2] + source[source_stride * 2 + 2] + 1) >> 1;
}
}
else
{
for (; source <= source_end - 2; source += 2, dest += num_output_channels * 2)
{
// Red pixel
uint8_t* cur_pixel = dest;
cur_pixel[-1*swap_br] = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;;
cur_pixel[0] = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;;
cur_pixel[1*swap_br] = source[source_stride + 1];
// Green pixel
uint8_t* next_pixel = cur_pixel+num_output_channels;
next_pixel[-1*swap_br] = (source[2] + source[source_stride * 2 + 2] + 1) >> 1;
next_pixel[0] = source[source_stride + 2];
next_pixel[1*swap_br] = (source[source_stride + 1] + source[source_stride + 3] + 1) >> 1;
}
}
if (source < source_end)
{
dest[-1*swap_br] = source[source_stride + 1];
dest[0] = (source[1] + source[source_stride] + source[source_stride + 2] + source[source_stride * 2 + 1] + 2) >> 2;
dest[1*swap_br] = (source[0] + source[2] + source[source_stride * 2] + source[source_stride * 2 + 2] + 2) >> 2;;
source++;
dest += num_output_channels;
}
// Fill first pixel of row (copy second pixel)
uint8_t* first_pixel = dest_row-num_output_channels;
first_pixel[-1*swap_br] = dest_row[-1*swap_br];
first_pixel[0] = dest_row[0];
first_pixel[1*swap_br] = dest_row[1*swap_br];
// Fill last pixel of row (copy second-to-last pixel). Note: dest row starts at the *second* pixel of the row, so dest_row + (width-2) * num_output_channels puts us at the last pixel of the row
uint8_t* last_pixel = dest_row + (frame_width - 2)*num_output_channels;
uint8_t* second_to_last_pixel = last_pixel - num_output_channels;
last_pixel[-1*swap_br] = second_to_last_pixel[-1*swap_br];
last_pixel[0] = second_to_last_pixel[0];
last_pixel[1*swap_br] = second_to_last_pixel[1*swap_br];
}
// Fill first & last row
for (int i = 0; i < dest_stride; i++)
{
outBuffer[i] = outBuffer[i + dest_stride];
outBuffer[i + (frame_height - 1)*dest_stride] = outBuffer[i + (frame_height - 2)*dest_stride];
}
}
private:
uint32_t frame_size;
uint32_t num_frames;
uint8_t* frame_buffer;
uint32_t head;
uint32_t tail;
uint32_t available;
std::mutex mutex;
std::condition_variable empty_condition;
};
// URBDesc
class URBDesc
{
public:
URBDesc() :
num_active_transfers (0),
last_packet_type (DISCARD_PACKET),
last_pts (0),
last_fid (0),
transfer_buffer (NULL),
cur_frame_start (NULL),
cur_frame_data_len (0),
frame_size (0),
frame_queue (NULL)
{
}
~URBDesc()
{
debug("URBDesc destructor\n");
close_transfers();
}
bool start_transfers(libusb_device_handle *handle, uint32_t curr_frame_size)
{
// Initialize the frame queue
frame_size = curr_frame_size;
frame_queue = new FrameQueue(frame_size);
// Initialize the current frame pointer to the start of the buffer; it will be updated as frames are completed and pushed onto the frame queue
cur_frame_start = frame_queue->GetFrameBufferStart();
cur_frame_data_len = 0;
// Find the bulk transfer endpoint
uint8_t bulk_endpoint = find_ep(libusb_get_device(handle));
libusb_clear_halt(handle, bulk_endpoint);
// Allocate the transfer buffer
transfer_buffer = (uint8_t*)malloc(TRANSFER_SIZE * NUM_TRANSFERS);
memset(transfer_buffer, 0, TRANSFER_SIZE * NUM_TRANSFERS);
int res = 0;
for (int index = 0; index < NUM_TRANSFERS; ++index)
{
// Create & submit the transfer
xfr[index] = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(xfr[index], handle, bulk_endpoint, transfer_buffer + index * TRANSFER_SIZE, TRANSFER_SIZE, transfer_completed_callback, reinterpret_cast<void*>(this), 0);
res |= libusb_submit_transfer(xfr[index]);
num_active_transfers++;
}
last_pts = 0;
last_fid = 0;
USBMgr::instance()->cameraStarted();
return res == 0;
}
void close_transfers()
{
std::unique_lock<std::mutex> lock(num_active_transfers_mutex);
if (num_active_transfers == 0)
return;
// Cancel any pending transfers
for (int index = 0; index < NUM_TRANSFERS; ++index)
{
libusb_cancel_transfer(xfr[index]);
}
// Wait for cancelation to finish
num_active_transfers_condition.wait(lock, [this]() { return num_active_transfers == 0; });
USBMgr::instance()->cameraStopped();
free(transfer_buffer);
transfer_buffer = NULL;
delete frame_queue;
frame_queue = NULL;
}
void transfer_canceled()
{
std::lock_guard<std::mutex> lock(num_active_transfers_mutex);
--num_active_transfers;
num_active_transfers_condition.notify_one();
}
void frame_add(enum gspca_packet_type packet_type, const uint8_t *data, int len)
{
if (packet_type == FIRST_PACKET)
{
cur_frame_data_len = 0;
}
else
{
switch(last_packet_type) // ignore warning.
{
case DISCARD_PACKET:
if (packet_type == LAST_PACKET) {
last_packet_type = packet_type;
cur_frame_data_len = 0;
}
return;
case LAST_PACKET:
return;
default:
break;
}
}
/* append the packet to the frame buffer */
if (len > 0)
{
if(cur_frame_data_len + len > frame_size)
{
packet_type = DISCARD_PACKET;
cur_frame_data_len = 0;
} else {
memcpy(cur_frame_start+cur_frame_data_len, data, len);
cur_frame_data_len += len;
}
}
last_packet_type = packet_type;
if (packet_type == LAST_PACKET) {
cur_frame_data_len = 0;
cur_frame_start = frame_queue->Enqueue();
//debug("frame completed %d\n", frame_complete_ind);
}
}
void pkt_scan(uint8_t *data, int len)
{
uint32_t this_pts;
uint16_t this_fid;
int remaining_len = len;
int payload_len;
payload_len = 2048; // bulk type
do {
len = (std::min)(remaining_len, payload_len);
/* Payloads are prefixed with a UVC-style header. We
consider a frame to start when the FID toggles, or the PTS
changes. A frame ends when EOF is set, and we've received
the correct number of bytes. */
/* Verify UVC header. Header length is always 12 */
if (data[0] != 12 || len < 12) {
debug("bad header\n");
goto discard;
}
/* Check errors */
if (data[1] & UVC_STREAM_ERR) {
debug("payload error\n");
goto discard;
}
/* Extract PTS and FID */
if (!(data[1] & UVC_STREAM_PTS)) {
debug("PTS not present\n");
goto discard;
}
this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
/* If PTS or FID has changed, start a new frame. */
if (this_pts != last_pts || this_fid != last_fid) {
if (last_packet_type == INTER_PACKET)
{
/* The last frame was incomplete, so don't keep it or we will glitch */
frame_add(DISCARD_PACKET, NULL, 0);
}
last_pts = this_pts;
last_fid = this_fid;
frame_add(FIRST_PACKET, data + 12, len - 12);
} /* If this packet is marked as EOF, end the frame */
else if (data[1] & UVC_STREAM_EOF)
{
last_pts = 0;
if(cur_frame_data_len + len - 12 != frame_size)
{
goto discard;
}
frame_add(LAST_PACKET, data + 12, len - 12);
} else {
/* Add the data from this payload */
frame_add(INTER_PACKET, data + 12, len - 12);
}
/* Done this payload */
goto scan_next;
discard:
/* Discard data until a new frame starts. */
frame_add(DISCARD_PACKET, NULL, 0);
scan_next:
remaining_len -= len;
data += len;
} while (remaining_len > 0);
}
uint8_t num_active_transfers;
std::mutex num_active_transfers_mutex;
std::condition_variable num_active_transfers_condition;
enum gspca_packet_type last_packet_type;
uint32_t last_pts;
uint16_t last_fid;
libusb_transfer* xfr[NUM_TRANSFERS];
uint8_t* transfer_buffer;
uint8_t* cur_frame_start;
uint32_t cur_frame_data_len;
uint32_t frame_size;
FrameQueue* frame_queue;
};
static void LIBUSB_CALL transfer_completed_callback(struct libusb_transfer *xfr)
{
URBDesc *urb = reinterpret_cast<URBDesc*>(xfr->user_data);
enum libusb_transfer_status status = xfr->status;
if (status != LIBUSB_TRANSFER_COMPLETED)
{
debug("transfer status %d\n", status);
libusb_free_transfer(xfr);
urb->transfer_canceled();
if(status != LIBUSB_TRANSFER_CANCELLED)
{
urb->close_transfers();
}
return;
}
//debug("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
urb->pkt_scan(xfr->buffer, xfr->actual_length);
if (libusb_submit_transfer(xfr) < 0) {
debug("error re-submitting URB\n");
urb->close_transfers();
}
}
// PS3EYECam
bool PS3EYECam::devicesEnumerated = false;
std::vector<PS3EYECam::PS3EYERef> PS3EYECam::devices;
const std::vector<PS3EYECam::PS3EYERef>& PS3EYECam::getDevices( bool forceRefresh )
{
if( devicesEnumerated && ( ! forceRefresh ) )
return devices;
devices.clear();
USBMgr::instance()->sTotalDevices = USBMgr::instance()->listDevices(devices);
devicesEnumerated = true;
return devices;
}