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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include "backend/common/imageOps.hpp"
#include <stdint.h>
namespace VideoStitch {
namespace Image {
inline __device__ uint32_t YRGBDiffToRGBA(unsigned char y, const int3& rgbDiff) {
const int32_t ya = (1164 * (y - 16)) / 1000;
return RGBA::pack(clamp8(ya + rgbDiff.x), clamp8(ya + rgbDiff.y), clamp8(ya + rgbDiff.z), 0xff);
}
#define nv12_surface_write surface_write_i
#include "../gpuKernelDef.h"
#include "backend/common/image/unpack.gpu"
// ---------------------------- Output -----------------------------
__global__ void unpackKernelGrayscale(unsigned char* dst, unsigned pitch, const cudaSurfaceObject_t src, unsigned width,
unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
uint32_t val;
surf2Dread(&val, src, x * sizeof(uint32_t), y);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
dst[y * pitch + x] = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
}
}
__global__ void unpackSourceKernelRGBA(uint32_t* dst, unsigned pitch, const cudaSurfaceObject_t src, unsigned width,
unsigned height) {
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
// yeah, we could use a memcpy
uint32_t val;
surf2Dread(&val, src, x * sizeof(uint32_t), y);
dst[y * pitch + x] = val;
}
}
__global__ void unpackKernelRGB(unsigned char* __restrict__ dst, unsigned pitch, const uint32_t* __restrict__ src,
unsigned width, unsigned height) {
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
const uint32_t val = src[y * width + x];
dst[y * pitch + 3 * x] = RGBA::r(val);
dst[y * pitch + 3 * x + 1] = RGBA::g(val);
dst[y * pitch + 3 * x + 2] = RGBA::b(val);
}
}
__global__ void unpackSourceKernelRGB(unsigned char* dst, unsigned pitch, const cudaSurfaceObject_t src, unsigned width,
unsigned height) {
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
uint32_t val;
surf2Dread(&val, src, x * sizeof(uint32_t), y);
dst[y * pitch + 3 * x] = RGBA::r(val);
dst[y * pitch + 3 * x + 1] = RGBA::g(val);
dst[y * pitch + 3 * x + 2] = RGBA::b(val);
}
}
__global__ void unpackSourceKernelF32C1(float* dst, unsigned pitch, const cudaSurfaceObject_t src, unsigned width,
unsigned height) {
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
// yeah, we could use a memcpy
float val;
surf2Dread(&val, src, x * sizeof(float), y);
dst[y * pitch + x] = val;
}
}
__global__ void unpackSourceKernelGrayscale16(uint16_t* dst, unsigned pitch, const cudaSurfaceObject_t src,
unsigned width, unsigned height) {
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
float val;
surf2Dread(&val, src, x * sizeof(float), y);
const float inMilliMeters = val * 1000.f;
const uint16_t u16 = (uint16_t)max(0.f, min((float)USHRT_MAX, round(inMilliMeters)));
dst[y * pitch + x] = u16;
}
}
__global__ void unpackKernelDepth(unsigned char* __restrict__ yDst, unsigned yPitch, unsigned char* __restrict__ uDst,
unsigned uPitch, unsigned char* __restrict__ vDst, unsigned vPitch,
const float* __restrict__ src, unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned x = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned y = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (x < width && y < height) {
int32_t u = 0;
int32_t v = 0;
#pragma unroll
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
const float depth = src[(y + j) * width + x + i];
// convert to millimeters and truncate
unsigned int val = min(__float2uint_rn(depth * 1000.f), 65279);
// encode
yDst[(y + j) * yPitch + x + i] = (unsigned char)(val / 256);
int cu = val % 512;
int cv = (val + 384) % 512;
if (cu >= 256) {
u += (unsigned char)(511 - cu);
} else {
u += (unsigned char)cu;
}
if (cv >= 256) {
v += (unsigned char)(511 - cv);
} else {
v += (unsigned char)cv;
}
}
}
uDst[(y * uPitch + x) / 2] = (u + 2) / 4;
vDst[(y * vPitch + x) / 2] = (v + 2) / 4;
}
}
__global__ void unpackSourceKernelDepth(unsigned char* __restrict__ yDst, unsigned yPitch,
unsigned char* __restrict__ uDst, unsigned uPitch,
unsigned char* __restrict__ vDst, unsigned vPitch,
const cudaSurfaceObject_t src, unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned x = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned y = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (x < width && y < height) {
int32_t u = 0;
int32_t v = 0;
#pragma unroll
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
float depth;
surf2Dread(&depth, src, (x + i) * sizeof(float), y + j);
// convert to millimeters and truncate
unsigned int val = min(__float2uint_rn(depth * 1000.f), 65279);
// encode
yDst[(y + j) * yPitch + x + i] = (unsigned char)(val / 256);
int cu = val % 512;
int cv = (val + 384) % 512;
if (cu >= 256) {
u += (unsigned char)(511 - cu);
} else {
u += (unsigned char)cu;
}
if (cv >= 256) {
v += (unsigned char)(511 - cv);
} else {
v += (unsigned char)cv;
}
}
}
uDst[(y * uPitch + x) / 2] = (u + 2) / 4;
vDst[(y * vPitch + x) / 2] = (v + 2) / 4;
}
}
/**
* This kernel converts the buffer from RGBA to planar 12 bits 4:2:0 (YV12) out-of-place.
* The conversion is undefined for pixels with 0 alpha.
*
* Y0 Y1 Y2 Y3
* ...
* U0 U1
* ...
* V0 V1
* ...
*/
__global__ void unpackKernelYV12(unsigned char* __restrict__ yDst, unsigned yPitch, unsigned char* __restrict__ uDst,
unsigned uPitch, unsigned char* __restrict__ vDst, unsigned vPitch,
const uint32_t* __restrict__ src, unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned sy = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (sx < width && sy < height) {
int32_t u = 0;
int32_t v = 0;
{
uint32_t val = src[sy * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
if (sx + 1 < width && sy + 1 < height) {
// general case
{
uint32_t val = src[sy * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val = src[(sy + 1) * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val = src[(sy + 1) * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
uDst[(sy * uPitch + sx) / 2] = u / 4;
vDst[(sy * vPitch + sx) / 2] = v / 4;
} else {
// border case with odd width / height
if (sx + 1 < width) {
uint32_t val = src[sy * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uDst[(sy * uPitch + sx) / 2] = u / 2;
vDst[(sy * vPitch + sx) / 2] = v / 2;
}
__syncthreads();
if (sy + 1 < height) {
uint32_t val = src[(sy + 1) * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uDst[(sy * uPitch + sx) / 2] = u / 2;
vDst[(sy * vPitch + sx) / 2] = v / 2;
}
}
}
}
__global__ void unpackSourceKernelYV12(unsigned char* __restrict__ yDst, unsigned yPitch,
unsigned char* __restrict__ uDst, unsigned uPitch,
unsigned char* __restrict__ vDst, unsigned vPitch, const cudaSurfaceObject_t src,
unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned sy = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (sx < width && sy < height) {
int32_t u = 0;
int32_t v = 0;
{
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
if (sx + 1 < width && sy + 1 < height) {
// general case
{
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy + 1);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy + 1);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
uDst[(sy * uPitch + sx) / 2] = u / 4;
vDst[(sy * vPitch + sx) / 2] = v / 4;
} else {
// border case with odd width / height
if (sx + 1 < width) {
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uDst[(sy * uPitch + sx) / 2] = u / 2;
vDst[(sy * vPitch + sx) / 2] = v / 2;
}
__syncthreads();
if (sy + 1 < height) {
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy + 1);
int32_t r = clamp8(RGBA::r(val));
int32_t g = clamp8(RGBA::g(val));
int32_t b = clamp8(RGBA::b(val));
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uDst[(sy * uPitch + sx) / 2] = u / 2;
vDst[(sy * vPitch + sx) / 2] = v / 2;
}
}
}
}
/**
* This kernel converts the buffer from RGBA to interleaved YUV420 (NV12) out-of-place.
* The conversion is undefined for pixels with 0 alpha.
*
* Y0 Y1 Y2 Y3
* ...
* U0 V0 U1 V1
* ...
*/
__global__ void unpackKernelNV12(unsigned char* __restrict__ yDst, unsigned yPitch, unsigned char* __restrict__ uvDst,
unsigned uvPitch, const uint32_t* __restrict__ src, unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned sy = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (sx < width && sy < height) {
int32_t u = 0;
int32_t v = 0;
{
uint32_t val = src[sy * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
if (sx + 1 < width && sy + 1 < height) {
// general case
{
uint32_t val = src[sy * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val = src[(sy + 1) * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val = src[(sy + 1) * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
uvDst[(sy * uvPitch) / 2 + sx] = u / 4;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 4;
} else {
// border case with odd width / height
if (sx + 1 < width) {
uint32_t val = src[sy * width + sx + 1];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uvDst[(sy * uvPitch) / 2 + sx] = u / 2;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 2;
} else if (sy + 1 < height) {
uint32_t val = src[(sy + 1) * width + sx];
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uvDst[(sy * uvPitch) / 2 + sx] = u / 2;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 2;
}
}
}
}
/**
* This kernel converts the buffer from RGBA to interleaved YUV420 (NV12) out-of-place.
* The conversion is undefined for pixels with 0 alpha.
*
* Y0 Y1 Y2 Y3
* ...
* U0 V0 U1 V1
* ...
*/
__global__ void unpackSourceKernelNV12(unsigned char* __restrict__ yDst, unsigned yPitch,
unsigned char* __restrict__ uvDst, unsigned uvPitch,
const cudaSurfaceObject_t src, unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
unsigned sy = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
if (sx < width && sy < height) {
int32_t u = 0;
int32_t v = 0;
{
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
if (sx + 1 < width && sy + 1 < height) {
// general case
{
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy + 1);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
{
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy + 1);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
uvDst[(sy * uvPitch) / 2 + sx] = u / 4;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 4;
} else {
// border case with odd width / height
if (sx + 1 < width) {
uint32_t val;
surf2Dread(&val, src, (sx + 1) * sizeof(uint32_t), sy);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[sy * yPitch + sx + 1] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uvDst[(sy * uvPitch) / 2 + sx] = u / 2;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 2;
} else if (sy + 1 < height) {
uint32_t val;
surf2Dread(&val, src, sx * sizeof(uint32_t), sy + 1);
int32_t r = RGBA::r(val);
int32_t g = RGBA::g(val);
int32_t b = RGBA::b(val);
int32_t y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
yDst[(sy + 1) * yPitch + sx] = y;
u += ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v += ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
uvDst[(sy * uvPitch) / 2 + sx] = u / 2;
uvDst[(sy * uvPitch) / 2 + sx + 1] = v / 2;
}
}
}
}
/**
* This kernel converts the buffer from RGBA to YUY2 out-of-place.
* Pixels are all given full solidness (max alpha).
*/
__global__ void unpackYUY2Kernel(unsigned char* __restrict__ dst, unsigned pitch, const uint32_t* __restrict__ src,
unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width / 2 && y < height) {
uint32_t val0 = src[y * width + 2 * x];
int32_t r0 = RGBA::r(val0);
int32_t g0 = RGBA::g(val0);
int32_t b0 = RGBA::b(val0);
uint32_t val1 = src[y * width + 2 * x + 1];
int32_t r1 = RGBA::r(val1);
int32_t g1 = RGBA::g(val1);
int32_t b1 = RGBA::b(val1);
unsigned char y0 = ((66 * r0 + 129 * g0 + 25 * b0 + 128) >> 8) + 16;
unsigned char y1 = ((66 * r1 + 129 * g1 + 25 * b1 + 128) >> 8) + 16;
unsigned char u = ((-38 * r0 - 74 * g0 + 112 * b0 + 128) >> 8) + 128;
unsigned char v = ((112 * r0 - 94 * g0 - 18 * b0 + 128) >> 8) + 128;
dst[y * pitch + 4 * x] = y0;
dst[y * pitch + 4 * x + 1] = u;
dst[y * pitch + 4 * x + 2] = y1;
dst[y * pitch + 4 * x + 3] = v;
}
}
/**
* This kernel converts the buffer from RGBA to UYVY out-of-place.
* Pixels are all given full solidness (max alpha).
*/
__global__ void unpackUYVYKernel(unsigned char* __restrict__ dst, unsigned pitch, const uint32_t* __restrict__ src,
unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width / 2 && y < height) {
uint32_t val0 = src[y * width + 2 * x];
int32_t r0 = RGBA::r(val0);
int32_t g0 = RGBA::g(val0);
int32_t b0 = RGBA::b(val0);
uint32_t val1 = src[y * width + 2 * x + 1];
int32_t r1 = RGBA::r(val1);
int32_t g1 = RGBA::g(val1);
int32_t b1 = RGBA::b(val1);
unsigned char y0 = ((66 * r0 + 129 * g0 + 25 * b0 + 128) >> 8) + 16;
unsigned char y1 = ((66 * r1 + 129 * g1 + 25 * b1 + 128) >> 8) + 16;
unsigned char u = ((-38 * r0 - 74 * g0 + 112 * b0 + 128) >> 8) + 128;
unsigned char v = ((112 * r0 - 94 * g0 - 18 * b0 + 128) >> 8) + 128;
dst[y * pitch + 4 * x] = u;
dst[y * pitch + 4 * x + 1] = y0;
dst[y * pitch + 4 * x + 2] = v;
dst[y * pitch + 4 * x + 3] = y1;
}
}
/**
* This kernel converts the buffer from RGBA to 10 bits planar YUV422 out-of-place.
* Pixels are all given full solidness (max alpha).
* 10 bits values are padded to 16 bits.
*/
__global__ void unpackYUV422P10Kernel(uint16_t* __restrict__ yDst, unsigned yPitch, uint16_t* __restrict__ uDst,
unsigned uPitch, uint16_t* __restrict__ vDst, unsigned vPitch,
const uint32_t* __restrict__ src, unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width / 2 && y < height) {
uint32_t val0 = src[y * width + 2 * x];
int32_t r0 = RGBA::r(val0);
int32_t g0 = RGBA::g(val0);
int32_t b0 = RGBA::b(val0);
uint32_t val1 = src[y * width + 2 * x + 1];
int32_t r1 = RGBA::r(val1);
int32_t g1 = RGBA::g(val1);
int32_t b1 = RGBA::b(val1);
uint32_t u = 0, v = 0;
int32_t y0 = ((66 * r0 + 129 * g0 + 25 * b0 + 128) >> 8) + 16 << 2;
int32_t y1 = ((66 * r1 + 129 * g1 + 25 * b1 + 128) >> 8) + 16 << 2;
u += ((-38 * r0 - 74 * g0 + 112 * b0 + 128) >> 8) + 128 << 2;
u += ((-38 * r1 - 74 * g1 + 112 * b1 + 128) >> 8) + 128 << 2;
v += ((112 * r0 - 94 * g0 - 18 * b0 + 128) >> 8) + 128 << 2;
v += ((112 * r1 - 94 * g1 - 18 * b1 + 128) >> 8) + 128 << 2;
yDst[y * yPitch + 2 * x] = y0;
yDst[y * yPitch + 2 * x + 1] = y1;
uDst[y * uPitch + x] = u / 2;
vDst[y * vPitch + x] = v / 2;
}
}
__global__ void unpackMonoKernelYUV420P(unsigned char* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = blockIdx.x * blockDim.x + threadIdx.x;
unsigned sy = blockIdx.y * blockDim.y + threadIdx.y;
if (sx < width / 2 && sy < height / 2) {
{
const unsigned i = (2 * sy) * width + 2 * sx;
dst[i] = src[i];
}
{
const unsigned i = (2 * sy) * width + 2 * sx + 1;
dst[i] = src[i];
}
{
const unsigned i = (2 * sy + 1) * width + 2 * sx;
dst[i] = src[i];
}
{
const unsigned i = (2 * sy + 1) * width + 2 * sx + 1;
dst[i] = src[i];
}
}
}
// ---------------------------- Input -----------------------------
/**
* This kernel converts the buffer from BGRU8888 (where 'U' stands for 'unused') to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha)
*/
__global__ void convertBGRUToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
unsigned i = y * width + x;
dst[i] = RGBA::pack(src[4 * i + 2], src[4 * i + 1], src[4 * i], 0xff);
}
}
/**
* This kernel converts the buffer from RGB to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha)
*/
__global__ void convertRGBToRGBAKernel(cudaSurfaceObject_t dst, const unsigned char* src, unsigned width,
unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
unsigned i = y * width + x;
surf2Dwrite(RGBA::pack(src[3 * i], src[3 * i + 1], src[3 * i + 2], 0xff), dst, x * sizeof(uint32_t), y);
}
}
__global__ void convertRGB210ToRGBAKernel(cudaSurfaceObject_t dst, const uint32_t* src, unsigned width,
unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
uint32_t v = src[y * width + x];
surf2Dwrite(RGBA::pack(clamp8(RGB210::r(v)), clamp8(RGB210::g(v)), clamp8(RGB210::b(v)), RGB210::a(v)), dst,
x * sizeof(uint32_t), y);
}
}
__device__ unsigned loadBayerPattern(const unsigned char* __restrict__ src, unsigned width, unsigned height,
unsigned char* sharedSrc, unsigned srcX, unsigned srcY) {
// The shared memory uses the same pattern as src.
// There are (2 * blockDim.x + 2) * (2 * blockDim.y + 2) bytes to load
// (we need an extra layer outside the current zone for interpolation).
const unsigned sharedBlockWidth = blockDim.x + 1; // +1: one half block left and one half-block right.
const unsigned sharedWidth = 2 * sharedBlockWidth;
// Start with interior blocks.
if (srcX < width && srcY < height) {
// The access pattern is the same as during interpolation, meaning that each thread issues two (coalesced) reads: RG
// then GB.
// TODO: try out a different loading pattern: each thread loads 4 consecutive bytes in memory.
// This would reduce the number of coalesced reads to 1 instead of 2.
// Note that this would not be the same pattern as during interpolation.
const int srcBase = width * srcY + srcX;
const int sharedBase = sharedWidth + 1 + 2 * (sharedWidth * threadIdx.y + threadIdx.x);
// The compiler should be able to optimize that in only 2 coalesced single word reads.
// If it can't, accesses are still coalesced, but there are 4 accesses instead of one.
sharedSrc[sharedBase] = src[srcBase];
sharedSrc[sharedBase + 1] = src[srcBase + 1];
sharedSrc[sharedBase + sharedWidth] = src[srcBase + width];
sharedSrc[sharedBase + sharedWidth + 1] = src[srcBase + width + 1];
}
// Now load the boundary
if (threadIdx.y == 0 && srcX < width) {
// Top
{
const int sharedBase = 1 + 2 * threadIdx.x;
const int srcBase = srcY > 0 ?
// Normal case.
width * (srcY - 1) + srcX
:
// The previous row is outside the image, constant boundary condition.
width + srcX;
sharedSrc[sharedBase] = src[srcBase];
sharedSrc[sharedBase + 1] = src[srcBase + 1];
}
// Bottom
{
int srcBoundaryRow;
int sharedBoundaryRow;
if (srcY + 2 * blockDim.y < height) {
// Normal case, extra row is within image.
srcBoundaryRow = srcY + 2 * blockDim.y;
sharedBoundaryRow = 2 * blockDim.y;
} else {
// The next row is outside the image, constant boundary condition.
srcBoundaryRow = height - 2;
sharedBoundaryRow = height - srcY;
}
const int srcBase = width * srcBoundaryRow + srcX;
const int sharedBase = sharedWidth + 1 + sharedWidth * sharedBoundaryRow + 2 * threadIdx.x;
sharedSrc[sharedBase] = src[srcBase];
sharedSrc[sharedBase + 1] = src[srcBase + 1];
}
}
if (threadIdx.x == 0 && srcY < height) {
// Left
{
const int sharedBase = sharedWidth + 2 * sharedWidth * threadIdx.y;
const int srcBase = srcX > 0 ?
// Normal case.
width * srcY + srcX - 1
:
// The previous col is outside the image, constant boundary condition.
width * srcY + 1;
sharedSrc[sharedBase] = src[srcBase];
sharedSrc[sharedBase + sharedWidth] = src[srcBase + width];
}
// Right
{
int srcBoundaryCol;
int sharedBoundaryCol;
if (srcX + 2 * blockDim.x < width) {
// Normal case, extra col is within image.
srcBoundaryCol = srcX + 2 * blockDim.x;
sharedBoundaryCol = 2 * blockDim.x;
} else {
// The next col is outside the image, constant boundary condition.
srcBoundaryCol = width - 2;
sharedBoundaryCol = width - srcX;
}
const int srcBase = width * srcY + srcBoundaryCol;
const int sharedBase = sharedWidth + 1 + 2 * sharedWidth * threadIdx.y + sharedBoundaryCol;
sharedSrc[sharedBase] = src[srcBase];
sharedSrc[sharedBase + sharedWidth] = src[srcBase + width];
}
}
// And the corners.
if (threadIdx.x == 0 && threadIdx.y == 0) {
// Due to the assymetry, only the top left and bottom right corner are ever used (see the test for an example).
// Top left
{
const int srcBoundaryCol = srcX > 0 ? srcX - 1 : 1;
const int srcBoundaryRow = srcY > 0 ? srcY - 1 : 1;
const int srcBase = width * srcBoundaryRow + srcBoundaryCol;
sharedSrc[0] = src[srcBase];
}
// Bottom right.
{
int srcBoundaryCol;
int sharedBoundaryCol;
if (srcX + 2 * blockDim.x < width) {
// Normal case, extra col is within image.
srcBoundaryCol = srcX + 2 * blockDim.x;
sharedBoundaryCol = 2 * blockDim.x;
} else {
// The next col is outside the image, constant boundary condition.
srcBoundaryCol = width - 2;
sharedBoundaryCol = width - srcX;
}
int srcBoundaryRow;
int sharedBoundaryRow;
if (srcY + 2 * blockDim.y < height) {
// Normal case, extra row is within image.
srcBoundaryRow = srcY + 2 * blockDim.y;
sharedBoundaryRow = 2 * blockDim.y;
} else {
// The next row is outside the image, constant boundary condition.
srcBoundaryRow = height - 2;
sharedBoundaryRow = height - srcY;
}
const int srcBase = width * srcBoundaryRow + srcBoundaryCol;
const int sharedBase = sharedWidth + 1 + sharedWidth * sharedBoundaryRow + sharedBoundaryCol;
sharedSrc[sharedBase] = src[srcBase];
}
}
return sharedWidth;
}
/**
* This kernel converts the buffer from Bayer-filtered RGGB to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha).
* Uses bilinear interpolation within color planes.
*
* Each thread handles a 2*2 RGGB pixel block. The interpolation support is the 4*4 pixel block centered around the 2*2
* block. Globally each thread block needs an extra pixel around itself.
*
*/
__global__ void convertBayerRGGBToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
// x and y are the 2*2 block ids.
const unsigned srcX = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned srcY = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
// Load the data to shared memory.
extern __shared__ unsigned char sharedSrc[];
const unsigned sharedWidth = loadBayerPattern(src, width, height, sharedSrc, srcX, srcY);
__syncthreads();
if (srcX < width && srcY < height) {
const int sharedBase = sharedWidth + 1 + 2 * (sharedWidth * threadIdx.y + threadIdx.x);
// Top-left component;
dst[srcY * width + srcX] = RGBA::pack(
sharedSrc[sharedBase], // Red is given
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1] +
(int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + sharedWidth]) /
4, // Green is 4-tap straight (+)
((int32_t)sharedSrc[sharedBase - 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 - sharedWidth] +
(int32_t)sharedSrc[sharedBase - 1 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
4, // Blue is 4-tap 45° rotated (x)
255);
// Top-right component;
dst[srcY * width + srcX + 1] = RGBA::pack(
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2]) / 2, // Red is 2-tap horizontal
sharedSrc[sharedBase + 1], // Green is given
((int32_t)sharedSrc[sharedBase + 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
2, // Blue is 2-tap vertical
255);
// Bottom-left component;
dst[(srcY + 1) * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2 * sharedWidth]) /
2, // Red is 2-tap vertical
sharedSrc[sharedBase + sharedWidth], // Green is given
((int32_t)sharedSrc[sharedBase + sharedWidth - 1] + (int32_t)sharedSrc[sharedBase + sharedWidth + 1]) /
2, // Blue is 2-tap horizontal
255);
// Bottom-right component
dst[(srcY + 1) * width + srcX + 1] = RGBA::pack(
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2] +
(int32_t)sharedSrc[sharedBase + 2 * sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + 2 * sharedWidth]) /
4, // Red is is 4-tap 45° rotated (x)
((int32_t)sharedSrc[sharedBase + 1] + (int32_t)sharedSrc[sharedBase + sharedWidth] +
(int32_t)sharedSrc[sharedBase + 2 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
4, // Green is 4-tap straight (+)
sharedSrc[sharedBase + sharedWidth + 1], // Blue is given
255);
}
}
/**
* This kernel converts the buffer from Bayer-filtered BGGR to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha).
* Uses bilinear interpolation within color planes.
*
* Each thread handles a 2*2 BGGR pixel block. The interpolation support is the 4*4 pixel block centered around the 2*2
* block. Globally each thread block needs an extra pixel around itself.
*
*/
__global__ void convertBayerBGGRToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
// x and y are the 2*2 block ids.
const unsigned srcX = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned srcY = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
// Load the data to shared memory.
extern __shared__ unsigned char sharedSrc[];
const unsigned sharedWidth = loadBayerPattern(src, width, height, sharedSrc, srcX, srcY);
__syncthreads();
if (srcX < width && srcY < height) {
const int sharedBase = sharedWidth + 1 + 2 * (sharedWidth * threadIdx.y + threadIdx.x);
// Top-left component;
dst[srcY * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase - 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 - sharedWidth] +
(int32_t)sharedSrc[sharedBase - 1 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
4, // Red is 4-tap 45° rotated (x)
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1] +
(int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + sharedWidth]) /
4, // Green is 4-tap straight (+)
sharedSrc[sharedBase], // Blue is given
255);
// Top-right component;
dst[srcY * width + srcX + 1] = RGBA::pack(
((int32_t)sharedSrc[sharedBase + 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
2, // Red is 2-tap vertical
sharedSrc[sharedBase + 1], // Green is given
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2]) / 2, // Blue is 2-tap horizontal
255);
// Bottom-left component;
dst[(srcY + 1) * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase + sharedWidth - 1] + (int32_t)sharedSrc[sharedBase + sharedWidth + 1]) /
2, // Red is 2-tap horizontal
sharedSrc[sharedBase + sharedWidth], // Green is given
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2 * sharedWidth]) /
2, // Blue is 2-tap vertical
255);
// Bottom-right component
dst[(srcY + 1) * width + srcX + 1] = RGBA::pack(
sharedSrc[sharedBase + sharedWidth + 1], // Red is given
((int32_t)sharedSrc[sharedBase + 1] + (int32_t)sharedSrc[sharedBase + sharedWidth] +
(int32_t)sharedSrc[sharedBase + 2 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
4, // Green is 4-tap straight (+)
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2] +
(int32_t)sharedSrc[sharedBase + 2 * sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + 2 * sharedWidth]) /
4, // Blue is is 4-tap 45° rotated (x)
255);
}
}
/**
* This kernel converts the buffer from Bayer-filtered GRBG to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha).
* Uses bilinear interpolation within color planes.
*
* Each thread handles a 2*2 GRBG pixel block. The interpolation support is the 4*4 pixel block centered around the 2*2
* block. Globally each thread block needs an extra pixel around itself.
*
*/
__global__ void convertBayerGRBGToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
// x and y are the 2*2 block ids.
const unsigned srcX = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned srcY = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
// Load the data to shared memory.
extern __shared__ unsigned char sharedSrc[];
const unsigned sharedWidth = loadBayerPattern(src, width, height, sharedSrc, srcX, srcY);
__syncthreads();
if (srcX < width && srcY < height) {
const int sharedBase = sharedWidth + 1 + 2 * (sharedWidth * threadIdx.y + threadIdx.x);
// Top-left component;
dst[srcY * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1]) / 2, // Red is 2-tap horizontal
sharedSrc[sharedBase], // Green is given
((int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + sharedWidth]) /
2, // Blue is 2-tap vertical
255);
// Top-right component;
dst[srcY * width + srcX + 1] = RGBA::pack(
sharedSrc[sharedBase + 1], // Red is given
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2] +
(int32_t)sharedSrc[sharedBase + 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
4, // Green is 4-tap straight (+)
((int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 - sharedWidth] +
(int32_t)sharedSrc[sharedBase + sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + sharedWidth]) /
4, // Blue is 4-tap 45° rotated (x)
255);
// Bottom-left component;
dst[(srcY + 1) * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1] +
(int32_t)sharedSrc[sharedBase - 1 + 2 * sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
4, // Red is 4-tap 45° rotated (x)
((int32_t)sharedSrc[sharedBase - 1 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth] +
(int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2 * sharedWidth]) /
4, // Green is 4-tap straight (+)
sharedSrc[sharedBase + sharedWidth], // Blue is given
255);
// Bottom-right component
dst[(srcY + 1) * width + srcX + 1] =
RGBA::pack(((int32_t)sharedSrc[sharedBase + 1] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
2, // Red is 2-tap vertical
sharedSrc[sharedBase + sharedWidth + 1], // Green is given
((int32_t)sharedSrc[sharedBase + sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + sharedWidth]) /
2, // Blue is 2-tap horizontal
255);
}
}
/**
* This kernel converts the buffer from Bayer-filtered GBRG to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha).
* Uses bilinear interpolation within color planes.
*
* Each thread handles a 2*2 GBRG pixel block. The interpolation support is the 4*4 pixel block centered around the 2*2
* block. Globally each thread block needs an extra pixel around itself.
*
*/
__global__ void convertBayerGBRGToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
// x and y are the 2*2 block ids.
const unsigned srcX = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned srcY = 2 * (blockIdx.y * blockDim.y + threadIdx.y);
// Load the data to shared memory.
extern __shared__ unsigned char sharedSrc[];
const unsigned sharedWidth = loadBayerPattern(src, width, height, sharedSrc, srcX, srcY);
__syncthreads();
if (srcX < width && srcY < height) {
const int sharedBase = sharedWidth + 1 + 2 * (sharedWidth * threadIdx.y + threadIdx.x);
// Top-left component;
dst[srcY * width + srcX] = RGBA::pack(
((int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + sharedWidth]) /
2, // Red is 2-tap vertical
sharedSrc[sharedBase], // Green is given
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1]) / 2, // Blue is 2-tap horizontal
255);
// Top-right component;
dst[srcY * width + srcX + 1] = RGBA::pack(
((int32_t)sharedSrc[sharedBase - sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 - sharedWidth] +
(int32_t)sharedSrc[sharedBase + sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + sharedWidth]) /
4, // Red is 4-tap 45° rotated (x)
((int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2] +
(int32_t)sharedSrc[sharedBase + 1 - sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth]) /
4, // Green is 4-tap straight (+)
sharedSrc[sharedBase + 1], // Blue is given
255);
// Bottom-left component;
dst[(srcY + 1) * width + srcX] = RGBA::pack(
sharedSrc[sharedBase + sharedWidth], // Red is given
((int32_t)sharedSrc[sharedBase - 1 + sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + sharedWidth] +
(int32_t)sharedSrc[sharedBase] + (int32_t)sharedSrc[sharedBase + 2 * sharedWidth]) /
4, // Green is 4-tap straight (+)
((int32_t)sharedSrc[sharedBase - 1] + (int32_t)sharedSrc[sharedBase + 1] +
(int32_t)sharedSrc[sharedBase - 1 + 2 * sharedWidth] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
4, // Blue is 4-tap 45° rotated (x)
255);
// Bottom-right component
dst[(srcY + 1) * width + srcX + 1] =
RGBA::pack(((int32_t)sharedSrc[sharedBase + sharedWidth] + (int32_t)sharedSrc[sharedBase + 2 + sharedWidth]) /
2, // Red is 2-tap horizontal
sharedSrc[sharedBase + sharedWidth + 1], // Green is given
((int32_t)sharedSrc[sharedBase + 1] + (int32_t)sharedSrc[sharedBase + 1 + 2 * sharedWidth]) /
2, // Blue is 2-tap vertical
255);
}
}
/**
* This kernel converts the buffer from BGR888 to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha)
*/
__global__ void convertBGRToRGBAKernel(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
unsigned i = y * width + x;
dst[i] = RGBA::pack(src[3 * i + 2], src[3 * i + 1], src[3 * i], 0xff);
}
}
/**
* These kernels converts the buffer from various YUV422 representations to RGBA8888 out-of-place.
* Pixels are all given full solidness (max alpha).
*/
__global__ void convertUYVYToRGBAKernel(cudaSurfaceObject_t dst, const unsigned char* __restrict__ src, unsigned width,
unsigned height) {
// each thread is responsible for a 2x1 pixel group
// Two bytes per pixel. Y0 U Y1 V
// Read 2x (y0), 2x+1 (u), 2x+2 (y1) 2x+3 (v)
// Write x, x+1
// Repeat for every line
const unsigned pitch = width * 2;
const unsigned x = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
const unsigned char u = src[y * pitch + 2 * x]; // Two bytes per pixel. U Y0 V Y1
const unsigned char y0 = src[y * pitch + 2 * x + 1];
const unsigned char v = src[y * pitch + 2 * x + 2];
const unsigned char y1 = src[y * pitch + 2 * x + 3];
const RGBDiff rgbDiff(yuv444ToRGBDiff(u, v));
surf2Dwrite(YRGBDiffToRGBA(y0, rgbDiff), dst, x * 4, y);
surf2Dwrite(YRGBDiffToRGBA(y1, rgbDiff), dst, (x + 1) * 4, y);
}
}
__global__ void convertYUY2ToRGBAKernel(cudaSurfaceObject_t dst, const unsigned char* src, unsigned width,
unsigned height) {
// each thread is responsible for a 2x1 pixel group
// Two bytes per pixel. Y0 U Y1 V
// Read 2x (y0), 2x+1 (u), 2x+2 (y1) 2x+3 (v)
// Write x, x+1
// Repeat for every line
const unsigned pitch = width * 2;
const unsigned x = 2 * (blockIdx.x * blockDim.x + threadIdx.x);
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
const unsigned char y0 = src[y * pitch + 2 * x]; // Two bytes per pixel. Y0 U Y1 V
const unsigned char u = src[y * pitch + 2 * x + 1];
const unsigned char y1 = src[y * pitch + 2 * x + 2];
const unsigned char v = src[y * pitch + 2 * x + 3];
const RGBDiff rgbDiff(yuv444ToRGBDiff(u, v));
surf2Dwrite(YRGBDiffToRGBA(y0, rgbDiff), dst, x * 4, y);
surf2Dwrite(YRGBDiffToRGBA(y1, rgbDiff), dst, (x + 1) * 4, y);
}
}
/**
* This kernel converts the buffer from 10 bits planar YUV422 to packed RGBA8888 out-of-place.
* Each thread manages 2 pixels.
* 10 bits values are padded to 16 bits, and are clamped to 8 bits during conversion
* All pixels are solid.
*/
__global__ void convertYUV422P10ToRGBAKernel(cudaSurfaceObject_t dst, const uint16_t* src, unsigned width,
unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
const uint16_t* uSrc = src + width * height;
const uint16_t* vSrc = uSrc + width * height / 2;
if (x < width / 2 && y < height) {
uint32_t y0 = src[y * width + 2 * x] >> 2;
uint32_t y1 = src[y * width + 2 * x + 1] >> 2;
uint32_t u = uSrc[y * (width / 2) + x] >> 2;
uint32_t v = vSrc[y * (width / 2) + x] >> 2;
const RGBDiff rgbDiff = yuv444ToRGBDiff(u, v);
surf2Dwrite(YRGBDiffToRGBA(y0, rgbDiff), dst, (2 * x) * 4, y);
surf2Dwrite(YRGBDiffToRGBA(y1, rgbDiff), dst, (2 * x + 1) * 4, y);
}
}
/**
* This kernel converts the buffer from planar 12 bits 4:2:0 (YV12) to packed RGBA8888 out-of-place.
* All pixels are solid.
*/
__global__ void convertYV12ToRGBAKernel(cudaSurfaceObject_t dst, const unsigned char* src, unsigned width,
unsigned height) {
// each thread is responsible for a 2x2 pixel group
unsigned sx = blockIdx.x * blockDim.x + threadIdx.x;
unsigned sy = blockIdx.y * blockDim.y + threadIdx.y;
const unsigned char* uSrc = src + width * height;
const unsigned char* vSrc = uSrc + (width * height) / 4;
if (sx < width / 2 && sy < height / 2) {
const RGBDiff rgbDiff(yuv444ToRGBDiff(uSrc[sy * (width / 2) + sx], vSrc[sy * (width / 2) + sx]));
surf2Dwrite(YRGBDiffToRGBA(src[(2 * sy) * width + 2 * sx], rgbDiff), dst, (2 * sx) * 4, 2 * sy);
surf2Dwrite(YRGBDiffToRGBA(src[(2 * sy) * width + 2 * sx + 1], rgbDiff), dst, (2 * sx + 1) * 4, 2 * sy);
surf2Dwrite(YRGBDiffToRGBA(src[(2 * sy + 1) * width + 2 * sx], rgbDiff), dst, (2 * sx) * 4, 2 * sy + 1);
surf2Dwrite(YRGBDiffToRGBA(src[(2 * sy + 1) * width + 2 * sx + 1], rgbDiff), dst, (2 * sx + 1) * 4, 2 * sy + 1);
}
}
__global__ void convertKernelGrayscale(uint32_t* __restrict__ dst, const unsigned char* __restrict__ src,
unsigned width, unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
unsigned i = y * width + x;
dst[i] = RGBA::pack((uint32_t)src[i], (uint32_t)src[i], (uint32_t)src[i], 0xff);
}
}
__global__ void convertGrayscaleKernel(cudaSurfaceObject_t dst, const unsigned char* __restrict__ src, unsigned width,
unsigned height) {
unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
unsigned i = y * width + x;
surf2Dwrite(RGBA::pack((uint32_t)src[i], (uint32_t)src[i], (uint32_t)src[i], 0xff), dst, x * 4, y);
}
}
} // namespace Image
} // namespace VideoStitch