-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.c
More file actions
1567 lines (1369 loc) · 55.3 KB
/
main.c
File metadata and controls
1567 lines (1369 loc) · 55.3 KB
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
#include <psp2/kernel/modulemgr.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/kernel/clib.h>
#include <psp2/camera.h>
#include <taihen.h>
//#include "log.h"
#ifdef ENABLE_BMP
#include <psp2/io/fcntl.h>
#include <psp2/appmgr.h>
#include <psp2/kernel/sysmem.h>
#ifdef READ_WITH_KUIO
#include <kuio.h>
#endif
#include <DSMotionLibrary.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Structure to simulate SceCamera API alternative behavior
typedef struct SceCameraRead2 {
SceSize size; //!< sizeof(SceCameraRead2)
int mode;
int pad;
int status;
uint64_t frame;
uint64_t timestamp;
int unknown0;
int unknown1;
int unknown2;
void* unknownNullCheck;
SceSize sizeIBase;
SceSize sizeUBase;
SceSize sizeVBase;
void *pIBase;
void *pUBase;
void *pVBase;
} SceCameraRead2;
// Bitmap reading inspired from:
// https://github.com/xerpi/libvita2d/blob/master/libvita2d/source/vita2d_image_bmp.c
#define BMP_SIGNATURE (0x4D42)
typedef struct {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} __attribute__((packed)) BITMAPFILEHEADER;
typedef struct {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} __attribute__((packed)) BITMAPINFOHEADER;
unsigned int alignSizeForMemBlock(unsigned int size)
{
if (size & 0xFFF) {
// Align to 4kB pages
size += ((~size) & 0xFFF) + 1;
}
return size;
}
typedef struct {
SceUID blockIDs[3];
void* blocksData[3];
uint16_t texelBits[3];
uint16_t rowStride[3];
uint16_t rowDepend[3];
uint16_t widthAlign;
uint16_t heightAlign;
uint16_t imageWidth;
uint16_t imageHeight;
int ready;
} ImageBuffers;
typedef void (*BufferWriteFunc)(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor);
// Camera formats support
static void ABGRWrite(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor)
{
((unsigned int*)oBuffers->blocksData[0])[iGlobalPos] = iColor;
}
static void ARGBWrite(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor)
{
((unsigned int*)oBuffers->blocksData[0])[iGlobalPos] = (iColor&0xFF00FF00) | (iColor&0xFF)<<16 | (iColor&0xFF0000)>>16;
}
float convMat[3][3] = { {0.299f, 0.587f, 0.114f}, {-0.14317f, -0.28886f, 0.436f}, {0.615f, -0.51499f, -0.10001f} };
typedef struct {
unsigned int globalPos;
unsigned int colors[2];
} YUV422Data;
static void RGBToYUV422(YUV422Data* iData, float Y[2], float Cb[2], float Cr[2])
{
for (int i = 0; i < 2; i++)
{
unsigned int color = iData->colors[i];
float nR = (float)(color&0xFF);
float nG = (float)((color&0xFF00)>>8);
float nB = (float)((color&0xFF0000)>>16);
Y[i] = convMat[0][0] * nR + convMat[0][1] * nG + convMat[0][2] * nB;
Cb[i] = convMat[1][0] * nR + convMat[1][1] * nG + convMat[1][2] * nB + 128.f;
Cr[i] = convMat[2][0] * nR + convMat[2][1] * nG + convMat[2][2] * nB + 128.f;
}
}
static void YUV422PackedWrite(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor)
{
YUV422Data* data = (YUV422Data*)iFuncData;
if (0 == iWidthPos%2)
{
data->globalPos = iGlobalPos;
data->colors[0] = iColor;
return;
}
data->colors[1] = iColor;
float Y[2];
float Cb[2];
float Cr[2];
RGBToYUV422(data, Y, Cb, Cr);
((unsigned short*)oBuffers->blocksData[0])[data->globalPos] = (((unsigned char)Y[0])<<8) | (unsigned char)((Cb[0]+Cb[1])/2.f);
((unsigned short*)oBuffers->blocksData[0])[iGlobalPos] = (((unsigned char)Y[1])<<8) | (unsigned char)((Cr[0]+Cr[1])/2.f);
}
static void YUV422PlaneWrite(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor)
{
YUV422Data* data = (YUV422Data*)iFuncData;
if (0 == iWidthPos%2)
{
data->globalPos = iGlobalPos;
data->colors[0] = iColor;
return;
}
data->colors[1] = iColor;
float Y[2];
float Cb[2];
float Cr[2];
RGBToYUV422(data, Y, Cb, Cr);
((unsigned char*)oBuffers->blocksData[0])[data->globalPos] = (unsigned char)Y[0];
((unsigned char*)oBuffers->blocksData[0])[iGlobalPos] = (unsigned char)Y[1];
((unsigned char*)oBuffers->blocksData[1])[data->globalPos/2] = (unsigned char)((Cb[0]+Cb[1])/2.f);
((unsigned char*)oBuffers->blocksData[2])[data->globalPos/2] = (unsigned char)((Cr[0]+Cr[1])/2.f);
}
typedef struct {
unsigned int globalPos[2];
unsigned int colors[4];
} YUV420Data;
static void RGBToYUV420(YUV420Data* iData, float Y[4], float Cb[4], float Cr[4])
{
for (int i = 0; i < 4; i++)
{
unsigned int color = iData->colors[i];
float nR = (float)(color&0xFF);
float nG = (float)((color&0xFF00)>>8);
float nB = (float)((color&0xFF0000)>>16);
Y[i] = convMat[0][0] * nR + convMat[0][1] * nG + convMat[0][2] * nB;
Cb[i] = convMat[1][0] * nR + convMat[1][1] * nG + convMat[1][2] * nB + 128.f;
Cr[i] = convMat[2][0] * nR + convMat[2][1] * nG + convMat[2][2] * nB + 128.f;
}
}
static void YUV420PlaneWrite(void* iFuncData, ImageBuffers* oBuffers, unsigned int iGlobalPos, uint16_t iWidthPos, uint16_t iHeightPos, unsigned int iColor)
{
YUV420Data* data = (YUV420Data*)iFuncData;
unsigned int pixelPosInBlock = iWidthPos%2+((iHeightPos%2)<<1);
if (0 == iWidthPos%2)
data->globalPos[iHeightPos%2] = iGlobalPos;
data->colors[pixelPosInBlock] = iColor;
if (pixelPosInBlock < 3)
return;
float Y[4];
float Cb[4];
float Cr[4];
RGBToYUV420(data, Y, Cb, Cr);
((unsigned short*)oBuffers->blocksData[0])[data->globalPos[0]/2] = (unsigned char)Y[0] | (((unsigned char)Y[1])<<8);
((unsigned short*)oBuffers->blocksData[0])[data->globalPos[1]/2] = (unsigned char)Y[2] | (((unsigned char)Y[3])<<8);
((unsigned char*)oBuffers->blocksData[1])[(data->globalPos[1]+(iWidthPos-1))/4] = (unsigned char)((Cb[0]+Cb[1]+Cb[2]+Cb[3])/4.f);
((unsigned char*)oBuffers->blocksData[2])[(data->globalPos[1]+(iWidthPos-1))/4] = (unsigned char)((Cr[0]+Cr[1]+Cr[2]+Cr[3])/4.f);
}
// Bitmap reading functions
static unsigned int ReadColor(void* buffer, unsigned short bitCount, unsigned int row_stride, int col, int row)
{
unsigned int imgColor = 0;
if (bitCount == 32) { //BGRA8888
unsigned int color = *(unsigned int *)(buffer + row*row_stride + col*4);
imgColor = ((color>>24)&0xFF)<<24 | (color&0xFF)<<16 |
((color>>8)&0xFF)<<8 | ((color>>16)&0xFF);
} else if (bitCount == 24) { //BGR888
unsigned char *address = buffer + row*row_stride + col*3;
imgColor = (*address)<<16 | (*(address+1))<<8 |
(*(address+2)) | (0xFF<<24);
} else if (bitCount == 16) { //BGR565
unsigned int color = *(unsigned short *)(buffer + row*row_stride + col*2);
unsigned char r = (color & 0x1F) *((float)255/31);
unsigned char g = ((color>>5) & 0x3F) *((float)255/63);
unsigned char b = ((color>>11) & 0x1F) *((float)255/31);
imgColor = ((r<<16) | (g<<8) | b | (0xFF<<24));
}
return imgColor;
}
static int LoadBMPGeneric(BITMAPFILEHEADER *bmp_fh, BITMAPINFOHEADER *bmp_ih, SceUID iFile,
ImageBuffers* oBuffers, BufferWriteFunc iWriteFunc, void* iFuncData)
{
unsigned int row_stride = bmp_ih->biWidth * (bmp_ih->biBitCount/8);
if (row_stride%4 != 0) {
row_stride += 4-(row_stride%4);
}
unsigned int block_stride = row_stride * oBuffers->heightAlign;
unsigned int size = alignSizeForMemBlock(block_stride);
SceUID bufferID = sceKernelAllocMemBlock("bitmap_block", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, size, NULL);
void *buffer = NULL;
sceKernelGetMemBlockBase(bufferID, (void **)&buffer);
if (!buffer) {
return -1;
}
#ifdef READ_WITH_KUIO
kuIoLseek(iFile, bmp_fh->bfOffBits, SCE_SEEK_SET);
#else
sceIoLseek(iFile, bmp_fh->bfOffBits, SCE_SEEK_SET);
#endif
unsigned int alignedWidth = oBuffers->imageWidth;
unsigned int alignedHeight = oBuffers->imageHeight;
unsigned int blocksCount = alignedHeight / oBuffers->heightAlign;
int b, i, j, x, y;
if (oBuffers->heightAlign > 1)
{
for (b = 0; b < blocksCount; b++)
{
#ifdef READ_WITH_KUIO
kuIoRead(iFile, buffer, block_stride);
#else
sceIoRead(iFile, buffer, block_stride);
#endif
for (i = 0; i < alignedWidth; i+=oBuffers->widthAlign)
{
for (j = 0; j < oBuffers->heightAlign ; j++)
{
y = b*oBuffers->heightAlign + j;
for (x = i; x < i+oBuffers->widthAlign; x++)
{
unsigned int globalPos = (alignedHeight - 1 - y)*alignedWidth + x;
unsigned int imgColor = ReadColor(buffer, bmp_ih->biBitCount, row_stride, x, j);
iWriteFunc(iFuncData, oBuffers, globalPos, x, y, imgColor);
}
}
}
}
}
else
{
for (i = 0; i < blocksCount; i++)
{
#ifdef READ_WITH_KUIO
kuIoRead(iFile, buffer, block_stride);
#else
sceIoRead(iFile, buffer, block_stride);
#endif
for (j = 0; j < oBuffers->heightAlign ; j++)
{
y = i*oBuffers->heightAlign + j;
unsigned int globalPos = (alignedHeight - 1 - y)*alignedWidth;
for (x = 0; x < alignedWidth; x++)
{
unsigned int imgColor = ReadColor(buffer, bmp_ih->biBitCount, row_stride, x, 0);
iWriteFunc(iFuncData, oBuffers, globalPos, x, y, imgColor);
globalPos++;
}
}
}
}
sceKernelFreeMemBlock(bufferID);
return 1;
}
static int LoadBMPFile(SceUID iFile, SceCameraFormat iFormat, char* iMemName, ImageBuffers* oBuffers)
{
BITMAPFILEHEADER bmp_fh;
#ifdef READ_WITH_KUIO
kuIoRead(iFile, (void *)&bmp_fh, sizeof(BITMAPFILEHEADER));
#else
sceIoRead(iFile, (void *)&bmp_fh, sizeof(BITMAPFILEHEADER));
#endif
if (bmp_fh.bfType != BMP_SIGNATURE)
return -1;
BITMAPINFOHEADER bmp_ih;
#ifdef READ_WITH_KUIO
kuIoRead(iFile, (void *)&bmp_ih, sizeof(BITMAPINFOHEADER));
#else
sceIoRead(iFile, (void *)&bmp_ih, sizeof(BITMAPINFOHEADER));
#endif
BufferWriteFunc writeFunc = NULL;
char funcData[24];
oBuffers->imageWidth = bmp_ih.biWidth;
oBuffers->imageHeight = bmp_ih.biHeight;
oBuffers->rowStride[0] = 0;
oBuffers->rowStride[1] = 0;
oBuffers->rowStride[2] = 0;
oBuffers->rowDepend[0] = 1;
oBuffers->rowDepend[1] = 1;
oBuffers->rowDepend[2] = 1;
oBuffers->widthAlign = 1;
oBuffers->heightAlign = 1;
switch (iFormat)
{
case SCE_CAMERA_FORMAT_ARGB:
oBuffers->texelBits[0] = 32;
writeFunc = &ARGBWrite;
break;
case SCE_CAMERA_FORMAT_ABGR:
oBuffers->texelBits[0] = 32;
writeFunc = &ABGRWrite;
break;
case SCE_CAMERA_FORMAT_YUV422_PACKED:
oBuffers->texelBits[0] = 16;
oBuffers->widthAlign = 2;
writeFunc = &YUV422PackedWrite;
break;
case SCE_CAMERA_FORMAT_YUV422_PLANE:
oBuffers->texelBits[0] = 8;
oBuffers->texelBits[1] = 4;
oBuffers->texelBits[2] = 4;
oBuffers->widthAlign = 2;
writeFunc = &YUV422PlaneWrite;
break;
case SCE_CAMERA_FORMAT_YUV420_PLANE:
oBuffers->texelBits[0] = 8;
oBuffers->texelBits[1] = 2;
oBuffers->rowDepend[1] = 2;
oBuffers->texelBits[2] = 2;
oBuffers->rowDepend[2] = 2;
oBuffers->widthAlign = 2;
oBuffers->heightAlign = 2;
writeFunc = &YUV420PlaneWrite;
break;
case SCE_CAMERA_FORMAT_RAW8:
case SCE_CAMERA_FORMAT_INVALID:
return -1;
}
if (NULL == writeFunc)
return -1;
oBuffers->imageWidth = (bmp_ih.biWidth/oBuffers->widthAlign)*oBuffers->widthAlign;
oBuffers->imageHeight = (bmp_ih.biHeight/oBuffers->heightAlign)*oBuffers->heightAlign;
char memname[48];
for (int i = 0; i < 3; i++)
{
oBuffers->rowStride[i] = (oBuffers->imageWidth*oBuffers->texelBits[i]*oBuffers->rowDepend[i])/8;
if (oBuffers->rowStride[i] > 0)
{
sprintf(memname, "%s_%d", iMemName, i);
unsigned int size = alignSizeForMemBlock(oBuffers->rowStride[i]*oBuffers->imageHeight/oBuffers->rowDepend[i]);
oBuffers->blockIDs[i] = sceKernelAllocMemBlock(memname, SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, size, NULL);
oBuffers->blocksData[i] = NULL;
sceKernelGetMemBlockBase(oBuffers->blockIDs[i], (void **)&oBuffers->blocksData[i]);
if (!oBuffers->blocksData[i])
{
sceKernelFreeMemBlock(oBuffers->blockIDs[i]);
oBuffers->blockIDs[i] = -1;
return -1;
}
}
}
return LoadBMPGeneric(&bmp_fh, &bmp_ih, iFile, oBuffers, writeFunc, funcData);
}
//#define bitSize(size, bits) ((size*bits)/8)
unsigned int bitSize(unsigned int size, unsigned int bits)
{
return (size*bits) / 8;
}
// Math functions (used by motion detection)
#define abs(val) ((val < 0) ? -val : val)
#define sign(val) ((val > 0) ? 1 : ((val < 0) ? -1 : 0))
#define clamp(val, min, max) ((val > max) ? max : ((val < min) ? min : val))
#define M_PI 3.14159265359f
float atan2_approx(float y, float x)
{
static float ONEQTR_PI = M_PI / 4.0;
static float THRQTR_PI = 3.0 * M_PI / 4.0;
float r, angle;
float abs_y = abs(y) + 1e-10f;
if ( x < 0.0f )
{
r = (x + abs_y) / (abs_y - x);
angle = THRQTR_PI;
}
else
{
r = (x - abs_y) / (x + abs_y);
angle = ONEQTR_PI;
}
angle += (0.1963f * r * r - 0.9817f) * r;
if ( y < 0.0f )
return -angle;
return angle;
}
static char titleid[16] = {'\0'};
#endif
static SceUID g_hooks[39];
// Open - Close
#define NB_CAM 2
#ifdef ENABLE_BMP
static uint16_t width[NB_CAM] = {0, 0};
static uint16_t height[NB_CAM] = {0, 0};
static SceCameraFormat format[NB_CAM] = {0, 0};
static void* IBufferOnOpen[NB_CAM] = {NULL, NULL};
static void* UBufferOnOpen[NB_CAM] = {NULL, NULL};
static void* VBufferOnOpen[NB_CAM] = {NULL, NULL};
static ImageBuffers imageBuffers[NB_CAM] = { { {-1, -1, -1}, {NULL, NULL, NULL}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, 0, 0, 0, -1} ,
{ {-1, -1, -1}, {NULL, NULL, NULL}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, 0, 0, 0, -1} };
static SceCameraFormat imageFormat[NB_CAM] = {0, 0};
static int prevWidthOffset[NB_CAM] = {-1, -1};
static int prevHeightOffset[NB_CAM] = {-1, -1};
static void* prevBuffers[NB_CAM][3] = { {NULL, NULL, NULL}, {NULL, NULL, NULL} };
#endif
static int cameraOpened[NB_CAM] = {0, 0};
static uint16_t framerate[NB_CAM];
static uint64_t prevFrame[NB_CAM] = {0, 0};
static tai_hook_ref_t ref_hook0;
static int hook_sceCameraOpen(int devnum, SceCameraInfo *pInfo)
{
int res = TAI_CONTINUE(int, ref_hook0, devnum, pInfo);
if ((unsigned int)devnum < NB_CAM && NULL != pInfo && !cameraOpened[devnum])
{
cameraOpened[devnum] = 1;
framerate[devnum] = pInfo->framerate;
if (res < 0 && pInfo->resolution > SCE_CAMERA_RESOLUTION_0_0 && pInfo->resolution <= SCE_CAMERA_RESOLUTION_640_360)
{
if (pInfo->resolution < SCE_CAMERA_RESOLUTION_352_288)
{
pInfo->width = (640 >> (pInfo->resolution-1));
pInfo->height = (480 >> (pInfo->resolution-1));
}
else if (pInfo->resolution < SCE_CAMERA_RESOLUTION_480_272)
{
pInfo->width = (352 >> (pInfo->resolution-4));
pInfo->height = (288 >> (pInfo->resolution-4));
}
else if (SCE_CAMERA_RESOLUTION_480_272 == pInfo->resolution)
{
pInfo->width = 480;
pInfo->height = 272;
}
else if (SCE_CAMERA_RESOLUTION_640_360 == pInfo->resolution)
{
pInfo->width = 640;
pInfo->height = 360;
}
#ifdef ENABLE_BMP
width[devnum] = pInfo->width;
height[devnum] = pInfo->height;
if (0 == pInfo->buffer)
{
IBufferOnOpen[devnum] = pInfo->pIBase;
UBufferOnOpen[devnum] = pInfo->pUBase;
VBufferOnOpen[devnum] = pInfo->pVBase;
}
format[devnum] = pInfo->format;
//LOG("Camera opened %d with format %d\n", devnum, pInfo->format);
//LOG("Buffers pointers %x, %x, %x\n", (unsigned int)pInfo->pIBase, (unsigned int)pInfo->pUBase, (unsigned int)pInfo->pVBase);
//log_flush();
ImageBuffers* imageBuf = &imageBuffers[devnum];
if (imageBuf->ready < 0 || imageFormat[devnum] != pInfo->format)
{
imageBuf->ready = 0;
for (int i = 0; i < 3; i++)
{
if (imageBuf->blockIDs[i] >= 0 )
{
sceKernelFreeMemBlock(imageBuf->blockIDs[i]);
imageBuf->blockIDs[i] = -1;
}
}
char memname[32];
char pathname[256];
char* camname = (1 == devnum)?"Back":"Front";
sprintf(memname, "%s_%s", titleid, camname);
sprintf(pathname, "ux0:/data/FakeCamera/%s.bmp", memname);
#ifdef READ_WITH_KUIO
SceUID fd = -1;
kuIoOpen(pathname, SCE_O_RDONLY, &fd);
#else
SceUID fd = sceIoOpen(pathname, SCE_O_RDONLY, 0666);
#endif
if (fd < 0)
{
sprintf(pathname, "ux0:/data/FakeCamera/%s.bmp", titleid);
#ifdef READ_WITH_KUIO
kuIoOpen(pathname, SCE_O_RDONLY, &fd);
#else
fd = sceIoOpen(pathname, SCE_O_RDONLY, 0666);
#endif
}
if (fd < 0)
{
sprintf(pathname, "ux0:/data/FakeCamera/ALL_%s.bmp", camname);
#ifdef READ_WITH_KUIO
kuIoOpen(pathname, SCE_O_RDONLY, &fd);
#else
fd = sceIoOpen(pathname, SCE_O_RDONLY, 0666);
#endif
}
if (fd < 0)
{
sprintf(pathname, "ux0:/data/FakeCamera/ALL.bmp");
#ifdef READ_WITH_KUIO
kuIoOpen(pathname, SCE_O_RDONLY, &fd);
#else
fd = sceIoOpen(pathname, SCE_O_RDONLY, 0666);
#endif
}
if (fd >= 0)
{
//LOG("Try to load file %s\n", pathname);
if (LoadBMPFile(fd, pInfo->format, memname, imageBuf) >= 0)
{
imageFormat[devnum] = pInfo->format;
imageBuf->ready = 1;
//LOG(" => Success\n");
}
else
{
imageFormat[devnum] = SCE_CAMERA_FORMAT_INVALID;
imageBuf->ready = -1;
//LOG(" => Failed\n");
}
//log_flush();
#ifdef READ_WITH_KUIO
kuIoClose(fd);
#else
sceIoClose(fd);
#endif
}
}
#endif
res = 0;
}
}
return res;
}
static tai_hook_ref_t ref_hook1;
static int hook_sceCameraClose(int devnum)
{
int res = TAI_CONTINUE(int, ref_hook1, devnum);
if ((unsigned int)devnum < NB_CAM)
{
cameraOpened[devnum] = 0;
#ifdef ENABLE_BMP
width[devnum] = 0;
height[devnum] = 0;
format[devnum] = 0;
IBufferOnOpen[devnum] = NULL;
UBufferOnOpen[devnum] = NULL;
VBufferOnOpen[devnum] = NULL;
#endif
if (res < 0) res = 0;
}
return res;
}
// Start - Stop
static int cameraActive[NB_CAM] = {0, 0};
static uint64_t initTimeStamp[NB_CAM];
static uint64_t prevTimeStamp[NB_CAM];
static tai_hook_ref_t ref_hook2;
static int hook_sceCameraStart(int devnum)
{
int res = TAI_CONTINUE(int, ref_hook2, devnum);
if ((unsigned int)devnum < NB_CAM && cameraOpened[devnum])
{
cameraActive[devnum] = 1;
initTimeStamp[devnum] = sceKernelGetProcessTimeWide();
prevTimeStamp[devnum] = initTimeStamp[devnum];
if (res < 0) res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook3;
static int hook_sceCameraStop(int devnum)
{
int res = TAI_CONTINUE(int, ref_hook3, devnum);
if ((unsigned int)devnum < NB_CAM)
{
prevFrame[devnum] = 0;
#ifdef ENABLE_BMP
prevWidthOffset[devnum] = -1;
prevHeightOffset[devnum] = -1;
prevBuffers[devnum][0] = NULL;
prevBuffers[devnum][1] = NULL;
prevBuffers[devnum][2] = NULL;
#endif
cameraActive[devnum] = 0;
if (res < 0) res = 0;
}
return res;
}
// Read
static tai_hook_ref_t ref_hook4;
static int hook_sceCameraRead(int devnum, SceCameraRead *pRead)
{
int res = TAI_CONTINUE(int, ref_hook4, devnum, pRead);
if ((unsigned int)devnum < NB_CAM && NULL != pRead && cameraActive[devnum])
{
uint64_t newTimeStamp = sceKernelGetProcessTimeWide();
if (res < 0)
{
sceKernelDelayThread(1); // Release current thread time quantum to avoid freeze in some games (Frobisher Says)
uint64_t fakeTimeStamp = (newTimeStamp+prevTimeStamp[devnum])>>1;
uint64_t fakeFrame = (((fakeTimeStamp-initTimeStamp[devnum])*framerate[devnum])>>21) + 1;
pRead->status = 0;
if (0 == pRead->mode)
{
// Simulate "wait next frame" time
while (prevFrame[devnum] >= fakeFrame)
{
sceKernelDelayThread(1000);
newTimeStamp = sceKernelGetProcessTimeWide();
fakeTimeStamp = (newTimeStamp+prevTimeStamp[devnum])>>1;
fakeFrame = (((fakeTimeStamp-initTimeStamp[devnum])*framerate[devnum])>>21) + 1;
}
}
else if (prevFrame[devnum] >= fakeFrame)
pRead->status = 2;
#ifdef ENABLE_BMP
ImageBuffers* imageBuf = &imageBuffers[devnum];
char* buffers[3] = {NULL, NULL, NULL};
if (NULL == ((SceCameraRead2*)pRead)->unknownNullCheck && sizeof(SceCameraRead2) == pRead->size)
{
SceCameraRead2* pRead2 = (SceCameraRead2*)pRead;
buffers[0] = (NULL != IBufferOnOpen[devnum]) ? IBufferOnOpen[devnum] : pRead2->pIBase;
buffers[1] = (NULL != UBufferOnOpen[devnum]) ? UBufferOnOpen[devnum] : pRead2->pUBase;
buffers[2] = (NULL != VBufferOnOpen[devnum]) ? VBufferOnOpen[devnum] : pRead2->pVBase;
}
else
{
buffers[0] = (NULL != IBufferOnOpen[devnum]) ? IBufferOnOpen[devnum] : pRead->pIBase;
buffers[1] = (NULL != UBufferOnOpen[devnum]) ? UBufferOnOpen[devnum] : pRead->pUBase;
buffers[2] = (NULL != VBufferOnOpen[devnum]) ? VBufferOnOpen[devnum] : pRead->pVBase;
}
int buffersTest = (prevBuffers[devnum][0] != buffers[0] || prevBuffers[devnum][1] != buffers[1] || prevBuffers[devnum][2] != buffers[2]);
if (imageBuf->ready > 0 && width[devnum] > 0 && height[devnum] > 0 && (prevFrame[devnum] < fakeFrame || buffersTest))
{
prevFrame[devnum] = fakeFrame;
float widthOffsetRate = 0.f;
float heightOffsetRate = 0.f;
signed short accel[3];
signed short gyro[3];
if (dsGetSampledAccelGyro(100, accel, gyro) >= 0)
{
SceFVector3 accelVec = {-(float)accel[2] / 0x2000, (float)accel[0] / 0x2000, -(float)accel[1] / 0x2000};
float pitch = atan2_approx(accelVec.z, -accelVec.y);
float roll = atan2_approx(-accelVec.x, -accelVec.z*sign(-pitch));
widthOffsetRate = clamp(-roll, -1.f, 1.f);
heightOffsetRate = clamp(-(pitch+M_PI/2.f), -1.f, 1.f);
}
unsigned int imgRowTexels = imageBuf->imageWidth;
unsigned int imgRowCount = imageBuf->imageHeight;
unsigned int bufRowTexels = width[devnum];
unsigned int bufRowCount = height[devnum];
unsigned int minRowTexels = (imgRowTexels < bufRowTexels) ? imgRowTexels : bufRowTexels;
unsigned int minRowCount = (imgRowCount < bufRowCount) ? imgRowCount : bufRowCount;
int widthLeft = imgRowTexels - bufRowTexels;
int heightLeft = imgRowCount - bufRowCount;
unsigned int widthOffset = (unsigned int)((1.f + widthOffsetRate) * (float)abs(widthLeft) / 2.f);
unsigned int heightOffset = (unsigned int)((1.f + heightOffsetRate) * (float)abs(heightLeft) / 2.f);
widthOffset = (widthOffset/imageBuf->widthAlign)*imageBuf->widthAlign;
heightOffset = (heightOffset/imageBuf->heightAlign)*imageBuf->heightAlign;
unsigned int bufWidthOffset = 0;
unsigned int imgWidthOffset = 0;
if (widthLeft > 0)
imgWidthOffset = widthOffset;
else
bufWidthOffset = widthOffset;
unsigned int bufHeightOffset = 0;
unsigned int imgHeightOffset = 0;
if (heightLeft > 0)
imgHeightOffset = heightOffset;
else
bufHeightOffset = heightOffset;
if (prevWidthOffset[devnum] != widthOffset || prevHeightOffset[devnum] != heightOffset || buffersTest)
{
prevWidthOffset[devnum] = widthOffset;
prevHeightOffset[devnum] = heightOffset;
prevBuffers[devnum][0] = buffers[0];
prevBuffers[devnum][1] = buffers[1];
prevBuffers[devnum][2] = buffers[2];
for (int i = 0; i < 3; i++)
{
char* image = (imageBuf->blockIDs[i] >= 0) ? imageBuf->blocksData[i] : NULL;
if (NULL != buffers[i] && NULL != image)
{
unsigned int rowDepend = imageBuf->rowDepend[i];
unsigned int bufOffset = bufWidthOffset + bufHeightOffset*bufRowTexels/rowDepend;
unsigned int imgOffset = imgWidthOffset + imgHeightOffset*imgRowTexels/rowDepend;
unsigned int texelDependBits = imageBuf->texelBits[i]*rowDepend;
for (int row = 0; row < bufHeightOffset/rowDepend; row++)
memset(buffers[i]+bitSize(row*bufRowTexels,texelDependBits), 0, bitSize(bufRowTexels,texelDependBits));
for (int row = 0 ; row < minRowCount/rowDepend ; row++)
memcpy(buffers[i]+bitSize(row*bufRowTexels+bufOffset,texelDependBits), image+bitSize(row*imgRowTexels+imgOffset,texelDependBits), bitSize(minRowTexels,texelDependBits));
if (imgRowTexels < bufRowTexels)
{
for (int row = bufHeightOffset; row < (bufHeightOffset+minRowCount)/rowDepend ; row++)
{
memset(buffers[i]+bitSize(row*bufRowTexels,texelDependBits), 0, bitSize(bufWidthOffset,texelDependBits));
memset(buffers[i]+bitSize(row*bufRowTexels+bufWidthOffset+imgRowTexels,texelDependBits), 0, bitSize(bufRowTexels-bufWidthOffset-imgRowTexels,texelDependBits));
}
}
for (int row = bufHeightOffset+minRowCount; row < bufRowCount/rowDepend; row++)
memset(buffers[i]+bitSize(row*bufRowTexels,texelDependBits), 0, bitSize(bufRowTexels,texelDependBits));
}
}
}
}
#endif
pRead->frame = fakeFrame;
pRead->timestamp = fakeTimeStamp;
res = 0;
}
prevTimeStamp[devnum] = newTimeStamp;
}
return res;
}
// Active
static tai_hook_ref_t ref_hook5;
static int hook_sceCameraIsActive(int devnum)
{
int res = TAI_CONTINUE(int, ref_hook5, devnum);
if ((unsigned int)devnum < NB_CAM && res <= 0) res = cameraActive[devnum];
return res;
}
// Location
static tai_hook_ref_t ref_hook6;
static int hook_sceCameraGetDeviceLocation(int devnum, SceFVector3 *pLocation)
{
int res = TAI_CONTINUE(int, ref_hook6, devnum, pLocation);
if ((unsigned int)devnum < NB_CAM && NULL != pLocation && res < 0) res = 0;
return res;
}
// Saturation
static int saturation[NB_CAM] = {SCE_CAMERA_SATURATION_0, SCE_CAMERA_SATURATION_0};
static tai_hook_ref_t ref_hook7;
static int hook_sceCameraGetSaturation(int devnum, int *pLevel)
{
int res = TAI_CONTINUE(int, ref_hook7, devnum, pLevel);
if ((unsigned int)devnum < NB_CAM && NULL != pLevel && res < 0)
{
*pLevel = saturation[devnum];
res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook8;
static int hook_sceCameraSetSaturation(int devnum, int level)
{
int res = TAI_CONTINUE(int, ref_hook8, devnum, level);
if ((unsigned int)devnum < NB_CAM && res < 0)
{
saturation[devnum] = level;
res = 0;
}
return res;
}
// Brightness
static int brightness[NB_CAM] = {127, 127};
static tai_hook_ref_t ref_hook9;
static int hook_sceCameraGetBrightness(int devnum, int *pLevel)
{
int res = TAI_CONTINUE(int, ref_hook9, devnum, pLevel);
if ((unsigned int)devnum < NB_CAM && NULL != pLevel && res < 0)
{
*pLevel = brightness[devnum];
res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook10;
static int hook_sceCameraSetBrightness(int devnum, int level)
{
int res = TAI_CONTINUE(int, ref_hook10, devnum, level);
if ((unsigned int)devnum < NB_CAM && res < 0)
{
brightness[devnum] = level;
res = 0;
}
return res;
}
// Contrast
static int contrast[NB_CAM] = {127, 127};
static tai_hook_ref_t ref_hook11;
static int hook_sceCameraGetContrast(int devnum, int *pLevel)
{
int res = TAI_CONTINUE(int, ref_hook11, devnum, pLevel);
if ((unsigned int)devnum < NB_CAM && NULL != pLevel && res < 0)
{
*pLevel = contrast[devnum];
res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook12;
static int hook_sceCameraSetContrast(int devnum, int level)
{
int res = TAI_CONTINUE(int, ref_hook12, devnum, level);
if ((unsigned int)devnum < NB_CAM && res < 0)
{
contrast[devnum] = level;
res = 0;
}
return res;
}
// Sharpness
static int sharpness[NB_CAM] = {SCE_CAMERA_SHARPNESS_100, SCE_CAMERA_SHARPNESS_100};
static tai_hook_ref_t ref_hook13;
static int hook_sceCameraGetSharpness(int devnum, int *pLevel)
{
int res = TAI_CONTINUE(int, ref_hook13, devnum, pLevel);
if ((unsigned int)devnum < NB_CAM && NULL != pLevel && res < 0)
{
*pLevel = sharpness[devnum];
res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook14;
static int hook_sceCameraSetSharpness(int devnum, int level)
{
int res = TAI_CONTINUE(int, ref_hook14, devnum, level);
if ((unsigned int)devnum < NB_CAM && res < 0)
{
sharpness[devnum] = level;
res = 0;
}
return res;
}
// Reverse
static int reverse[NB_CAM] = {SCE_CAMERA_REVERSE_OFF, SCE_CAMERA_REVERSE_OFF};
static tai_hook_ref_t ref_hook15;
static int hook_sceCameraGetReverse(int devnum, int *pMode)
{
int res = TAI_CONTINUE(int, ref_hook15, devnum, pMode);
if ((unsigned int)devnum < NB_CAM && NULL != pMode && res < 0)
{
*pMode = reverse[devnum];
res = 0;
}
return res;
}
static tai_hook_ref_t ref_hook16;
static int hook_sceCameraSetReverse(int devnum, int mode)
{
int res = TAI_CONTINUE(int, ref_hook16, devnum, mode);
if ((unsigned int)devnum < NB_CAM && res < 0)
{
reverse[devnum] = mode;
res = 0;
}
return res;
}