-
Notifications
You must be signed in to change notification settings - Fork 0
/
NPPJpegCoder.cpp
1167 lines (1017 loc) · 37.9 KB
/
NPPJpegCoder.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
/*
@brief c++ source class for NPP jpeg coder
@author Shane Yuan
@date Oct 11, 2017
*/
// opencv
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/core/cuda_stream_accessor.hpp>
#include <cmath>
#include <string>
#include <fstream>
#include <iostream>
#include "Exceptions.h"
#include "NPPJpegCoder.h"
#include "helper_string.h"
#include "helper_cuda.h"
using namespace std;
//#define MEASURE_KERNEL_TIME
namespace npp {
template<class T>
T readBigEndian(const unsigned char *pData) {
if (sizeof(T) > 1)
{
unsigned char p[sizeof(T)];
reverse_copy(pData, pData + sizeof(T), p);
return *reinterpret_cast<T *>(p);
}
else
{
return *pData;
}
}
template<class T>
void writeBigEndian(unsigned char *pData, T value)
{
unsigned char *pValue = reinterpret_cast<unsigned char *>(&value);
reverse_copy(pValue, pValue + sizeof(T), pData);
}
int DivUp(int x, int d) {
return (x + d - 1) / d;
}
template<typename T>
T readAndAdvance(const unsigned char *&pData) {
T nElement = readBigEndian<T>(pData);
pData += sizeof(T);
return nElement;
}
template<typename T>
void writeAndAdvance(unsigned char *&pData, T nElement) {
writeBigEndian<T>(pData, nElement);
pData += sizeof(T);
}
int nextMarker(const unsigned char *pData, int &nPos, int nLength) {
unsigned char c = pData[nPos++];
do
{
while (c != 0xffu && nPos < nLength)
{
c = pData[nPos++];
}
if (nPos >= nLength)
return -1;
c = pData[nPos++];
} while (c == 0 || c == 0x0ffu);
return c;
}
void writeMarker(unsigned char nMarker, unsigned char *&pData) {
*pData++ = 0x0ff;
*pData++ = nMarker;
}
void writeJFIFTag(unsigned char *&pData) {
const char JFIF_TAG[] =
{
0x4a, 0x46, 0x49, 0x46, 0x00,
0x01, 0x02,
0x00,
0x00, 0x01, 0x00, 0x01,
0x00, 0x00
};
writeMarker(0x0e0, pData);
writeAndAdvance<unsigned short>(pData, sizeof(JFIF_TAG) + sizeof(unsigned short));
memcpy(pData, JFIF_TAG, sizeof(JFIF_TAG));
pData += sizeof(JFIF_TAG);
}
void loadJpeg(const char *input_file, unsigned char *&pJpegData, int &nInputLength) {
// Load file into CPU memory
ifstream stream(input_file, ifstream::binary);
if (!stream.good())
{
return;
}
stream.seekg(0, ios::end);
nInputLength = (int)stream.tellg();
stream.seekg(0, ios::beg);
pJpegData = new unsigned char[nInputLength];
stream.read(reinterpret_cast<char *>(pJpegData), nInputLength);
}
void readFrameHeader(const unsigned char *pData, FrameHeader &header) {
readAndAdvance<unsigned short>(pData);
header.nSamplePrecision = readAndAdvance<unsigned char>(pData);
header.nHeight = readAndAdvance<unsigned short>(pData);
header.nWidth = readAndAdvance<unsigned short>(pData);
header.nComponents = readAndAdvance<unsigned char>(pData);
for (int c = 0; c < header.nComponents; ++c)
{
header.aComponentIdentifier[c] = readAndAdvance<unsigned char>(pData);
header.aSamplingFactors[c] = readAndAdvance<unsigned char>(pData);
header.aQuantizationTableSelector[c] = readAndAdvance<unsigned char>(pData);
}
}
void writeFrameHeader(const FrameHeader &header, unsigned char *&pData) {
unsigned char aTemp[128];
unsigned char *pTemp = aTemp;
writeAndAdvance<unsigned char>(pTemp, header.nSamplePrecision);
writeAndAdvance<unsigned short>(pTemp, header.nHeight);
writeAndAdvance<unsigned short>(pTemp, header.nWidth);
writeAndAdvance<unsigned char>(pTemp, header.nComponents);
for (int c = 0; c < header.nComponents; ++c)
{
writeAndAdvance<unsigned char>(pTemp, header.aComponentIdentifier[c]);
writeAndAdvance<unsigned char>(pTemp, header.aSamplingFactors[c]);
writeAndAdvance<unsigned char>(pTemp, header.aQuantizationTableSelector[c]);
}
unsigned short nLength = (unsigned short)(pTemp - aTemp);
writeMarker(0x0C0, pData);
writeAndAdvance<unsigned short>(pData, nLength + 2);
memcpy(pData, aTemp, nLength);
pData += nLength;
}
void readScanHeader(const unsigned char *pData, ScanHeader &header) {
readAndAdvance<unsigned short>(pData);
header.nComponents = readAndAdvance<unsigned char>(pData);
for (int c = 0; c < header.nComponents; ++c)
{
header.aComponentSelector[c] = readAndAdvance<unsigned char>(pData);
header.aHuffmanTablesSelector[c] = readAndAdvance<unsigned char>(pData);
}
header.nSs = readAndAdvance<unsigned char>(pData);
header.nSe = readAndAdvance<unsigned char>(pData);
header.nA = readAndAdvance<unsigned char>(pData);
}
void writeScanHeader(const ScanHeader &header, unsigned char *&pData) {
unsigned char aTemp[128];
unsigned char *pTemp = aTemp;
writeAndAdvance<unsigned char>(pTemp, header.nComponents);
for (int c = 0; c < header.nComponents; ++c)
{
writeAndAdvance<unsigned char>(pTemp, header.aComponentSelector[c]);
writeAndAdvance<unsigned char>(pTemp, header.aHuffmanTablesSelector[c]);
}
writeAndAdvance<unsigned char>(pTemp, header.nSs);
writeAndAdvance<unsigned char>(pTemp, header.nSe);
writeAndAdvance<unsigned char>(pTemp, header.nA);
unsigned short nLength = (unsigned short)(pTemp - aTemp);
writeMarker(0x0DA, pData);
writeAndAdvance<unsigned short>(pData, nLength + 2);
memcpy(pData, aTemp, nLength);
pData += nLength;
}
void readQuantizationTables(const unsigned char *pData, QuantizationTable *pTables) {
unsigned short nLength = readAndAdvance<unsigned short>(pData) - 2;
while (nLength > 0)
{
unsigned char nPrecisionAndIdentifier = readAndAdvance<unsigned char>(pData);
int nIdentifier = nPrecisionAndIdentifier & 0x0f;
pTables[nIdentifier].nPrecisionAndIdentifier = nPrecisionAndIdentifier;
memcpy(pTables[nIdentifier].aTable, pData, 64);
pData += 64;
nLength -= 65;
}
}
void writeQuantizationTable(const QuantizationTable &table, unsigned char *&pData) {
writeMarker(0x0DB, pData);
writeAndAdvance<unsigned short>(pData, sizeof(QuantizationTable) + 2);
memcpy(pData, &table, sizeof(QuantizationTable));
pData += sizeof(QuantizationTable);
}
void readHuffmanTables(const unsigned char *pData, HuffmanTable *pTables) {
unsigned short nLength = readAndAdvance<unsigned short>(pData) - 2;
while (nLength > 0)
{
unsigned char nClassAndIdentifier = readAndAdvance<unsigned char>(pData);
int nClass = nClassAndIdentifier >> 4; // AC or DC
int nIdentifier = nClassAndIdentifier & 0x0f;
int nIdx = nClass * 2 + nIdentifier;
pTables[nIdx].nClassAndIdentifier = nClassAndIdentifier;
// Number of Codes for Bit Lengths [1..16]
int nCodeCount = 0;
for (int i = 0; i < 16; ++i)
{
pTables[nIdx].aCodes[i] = readAndAdvance<unsigned char>(pData);
nCodeCount += pTables[nIdx].aCodes[i];
}
memcpy(pTables[nIdx].aTable, pData, nCodeCount);
pData += nCodeCount;
nLength -= 17 + nCodeCount;
}
}
void writeHuffmanTable(const HuffmanTable &table, unsigned char *&pData) {
writeMarker(0x0C4, pData);
// Number of Codes for Bit Lengths [1..16]
int nCodeCount = 0;
for (int i = 0; i < 16; ++i)
{
nCodeCount += table.aCodes[i];
}
writeAndAdvance<unsigned short>(pData, 17 + nCodeCount + 2);
memcpy(pData, &table, 17 + nCodeCount);
pData += 17 + nCodeCount;
}
void readRestartInterval(const unsigned char *pData, int &nRestartInterval) {
readAndAdvance<unsigned short>(pData);
nRestartInterval = readAndAdvance<unsigned short>(pData);
}
bool printfNPPinfo(int cudaVerMajor, int cudaVerMinor) {
const NppLibraryVersion *libVer = nppGetLibVersion();
printf("NPP Library Version %d.%d.%d\n", libVer->major, libVer->minor, libVer->build);
int driverVersion, runtimeVersion;
cudaDriverGetVersion(&driverVersion);
cudaRuntimeGetVersion(&runtimeVersion);
printf(" CUDA Driver Version: %d.%d\n", driverVersion / 1000, (driverVersion % 100) / 10);
printf(" CUDA Runtime Version: %d.%d\n", runtimeVersion / 1000, (runtimeVersion % 100) / 10);
bool bVal = checkCudaCapabilities(cudaVerMajor, cudaVerMinor);
return bVal;
}
/**
@brief convert NppiBayerGridPosition code to OpenCV color conversion code
@param NppiBayerGridPosition bayerPattern: input bayer pattern code
@return int: output opencv color conversion code
*/
int bayerPatternNPP2CVRGB(NppiBayerGridPosition bayerPattern) {
int code;
switch (bayerPattern)
{
case NPPI_BAYER_BGGR:
code = cv::COLOR_BayerBG2RGB;
break;
case NPPI_BAYER_RGGB:
code = cv::COLOR_BayerRG2RGB;
break;
case NPPI_BAYER_GBRG:
code = cv::COLOR_BayerGB2RGB;
break;
case NPPI_BAYER_GRBG:
code = cv::COLOR_BayerGR2RGB;
break;
default:
std::cerr << "Function bayerPatternNPP2CVRGB, wrong bayer pattern is inputed.";
break;
}
return code;
}
/**
@brief convert NppiBayerGridPosition code to OpenCV color conversion code
@param NppiBayerGridPosition bayerPattern: input bayer pattern code
@return int: output opencv color conversion code
*/
int bayerPatternNPP2CVBGR(NppiBayerGridPosition bayerPattern) {
int code;
switch (bayerPattern)
{
case NPPI_BAYER_BGGR:
code = cv::COLOR_BayerBG2BGR;
break;
case NPPI_BAYER_RGGB:
code = cv::COLOR_BayerRG2BGR;
break;
case NPPI_BAYER_GBRG:
code = cv::COLOR_BayerGB2BGR;
break;
case NPPI_BAYER_GRBG:
code = cv::COLOR_BayerGR2BGR;
break;
default:
std::cerr << "Function bayerPatternNPP2CVBGR, wrong bayer pattern is inputed.";
break;
}
return code;
}
/**
@brief constructor
*/
NPPJpegCoder::NPPJpegCoder(): isWBRaw(false) {}
NPPJpegCoder::~NPPJpegCoder() {}
/**
@brief set bayer type
@param int cfaBayerType: cfa bayer type
@return int
*/
int NPPJpegCoder::setCfaBayerType(int cfaBayerType) {
this->cfaBayerType = static_cast<NppiBayerGridPosition>(cfaBayerType);
return 0;
}
/**
@brief set input raw data type
before auto white balance adjustment or
@param int cfaBayerType: cfa bayer type
@return int
*/
int NPPJpegCoder::setWBRawType(bool isWBRaw) {
this->isWBRaw = isWBRaw;
return 0;
}
/**
@brief set white balance gain
@param float redGain: gain of red channel
@param float greenGain: gain of green channel
@param float blueGain: gain of blue channel
@return int
*/
int NPPJpegCoder::setWhiteBalanceGain(float redGain, float greenGain, float blueGain) {
this->redGain = redGain;
this->greenGain = greenGain;
this->blueGain = blueGain;
wbTwist[0][0] = redGain;
wbTwist[1][1] = greenGain;
wbTwist[2][2] = blueGain;
return 0;
}
/**
@brief init jpeg encoder
@param int width: input image width
@param int height: input image height
@param int quality: jpeg encoding quality
@return
*/
int NPPJpegCoder::init(int width, int height, int quality) {
if (printfNPPinfo(2, 0) == false) {
cerr << "jpegNPP requires a GPU with Compute Capability 2.0 or higher" << endl;
exit(-1);
}
// calculate quantization table from quality
float s;
if (quality < 50)
s = 5000.0f / quality;
else s = 200.0f - 2 * quality;
for (size_t i = 0; i < 64; i++) {
// luminance
float luminVal = (float)quantiztionTableLuminance[i];
luminVal = floor((s * luminVal + 50.0f) / 100.0f);
if (luminVal < 1)
luminVal = 1;
else if (luminVal > 255)
luminVal = 255;
quantiztionTableLuminance[i] = (unsigned char)luminVal;
// chroma
float chromaVal = (float)quantiztionTableChroma[i];
chromaVal = floor((s * chromaVal + 50.0f) / 100.0f);
if (chromaVal < 1)
chromaVal = 1;
else if (chromaVal > 255)
chromaVal = 255;
quantiztionTableChroma[i] = (unsigned char)chromaVal;
}
// set width and height
this->width = width;
this->height = height;
// calculate size
aSrcSize[0].width = width;
aSrcSize[0].height = height;
aSrcSize[1].width = width;
aSrcSize[1].height = height;
aSrcSize[2].width = width;
aSrcSize[2].height = height;
aDstSize[0].width = width;
aDstSize[0].height = height;
aDstSize[1].width = width / 2;
aDstSize[1].height = height / 2;
aDstSize[2].width = width / 2;
aDstSize[2].height = height / 2;
// init output frame header
memset(&oFrameHeader, 0, sizeof(FrameHeader));
oFrameHeader.nWidth = width;
oFrameHeader.nHeight = height;
oFrameHeader.nSamplePrecision = 8;
oFrameHeader.nComponents = 3;
oFrameHeader.aComponentIdentifier[0] = 1;
oFrameHeader.aComponentIdentifier[1] = 2;
oFrameHeader.aComponentIdentifier[2] = 3;
oFrameHeader.aSamplingFactors[0] = 34;
oFrameHeader.aSamplingFactors[1] = 17;
oFrameHeader.aSamplingFactors[2] = 17;
oFrameHeader.aQuantizationTableSelector[0] = 0;
oFrameHeader.aQuantizationTableSelector[1] = 1;
oFrameHeader.aQuantizationTableSelector[2] = 1;
// init quantization table
memset(aQuantizationTables, 0, 4 * sizeof(QuantizationTable));
memset(aHuffmanTables, 0, 4 * sizeof(HuffmanTable));
aQuantizationTables[0].nPrecisionAndIdentifier = 0;
memcpy(aQuantizationTables[0].aTable, quantiztionTableLuminance, 64 * sizeof(unsigned char));
aQuantizationTables[1].nPrecisionAndIdentifier = 1;
memcpy(aQuantizationTables[1].aTable, quantiztionTableChroma, 64 * sizeof(unsigned char));
cudaMalloc(&pdQuantizationTables, 64 * 4);
// Copy DCT coefficients and Quantization Tables from host to device
for (int i = 0; i < 2; ++i) {
NPP_CHECK_CUDA(cudaMemcpy(pdQuantizationTables + i * 64,
aQuantizationTables[i].aTable, 64, cudaMemcpyHostToDevice));
}
// init huffman table
memcpy(aHuffmanTables[0].aCodes, huffmanCodeLuminanceDC, 16 * sizeof(unsigned char));
memcpy(aHuffmanTables[0].aTable, huffmanTableLuminanceDC, 256 * sizeof(unsigned char));
aHuffmanTables[0].nClassAndIdentifier = 0;
memcpy(aHuffmanTables[1].aCodes, huffmanCodeChromaDC, 16 * sizeof(unsigned char));
memcpy(aHuffmanTables[1].aTable, huffmanTableChromaDC, 256 * sizeof(unsigned char));
aHuffmanTables[1].nClassAndIdentifier = 1;
memcpy(aHuffmanTables[2].aCodes, huffmanCodeLuminanceAC, 16 * sizeof(unsigned char));
memcpy(aHuffmanTables[2].aTable, huffmanTableLuminanceAC, 256 * sizeof(unsigned char));
aHuffmanTables[2].nClassAndIdentifier = 16;
memcpy(aHuffmanTables[3].aCodes, huffmanCodeChromaAC, 16 * sizeof(unsigned char));
memcpy(aHuffmanTables[3].aTable, huffmanTableChromaAC, 256 * sizeof(unsigned char));
aHuffmanTables[3].nClassAndIdentifier = 17;
pHuffmanDCTables = aHuffmanTables;
pHuffmanACTables = &aHuffmanTables[2];
// init scanner header
oScanHeader.nA = 0;
oScanHeader.nComponents = 3;
oScanHeader.nSe = 63;
oScanHeader.nSs = 0;
oScanHeader.aComponentSelector[0] = 1;
oScanHeader.aComponentSelector[1] = 2;
oScanHeader.aComponentSelector[2] = 3;
oScanHeader.aHuffmanTablesSelector[0] = 0;
oScanHeader.aHuffmanTablesSelector[1] = 17;
oScanHeader.aHuffmanTablesSelector[2] = 17;
// init nppiEncodeHuffmanSpecInitAlloc_JPEG
for (int i = 0; i < 3; ++i) {
nppiEncodeHuffmanSpecInitAlloc_JPEG(pHuffmanDCTables[(oScanHeader.aHuffmanTablesSelector[i] >> 4)].aCodes, nppiDCTable, &apHuffmanDCTable[i]);
nppiEncodeHuffmanSpecInitAlloc_JPEG(pHuffmanACTables[(oScanHeader.aHuffmanTablesSelector[i] & 0x0f)].aCodes, nppiACTable, &apHuffmanACTable[i]);
}
// Compute channel sizes as stored in the JPEG (8x8 blocks & MCU block layout)
for (int i = 0; i < oFrameHeader.nComponents; ++i) {
nMCUBlocksV = max(nMCUBlocksV, oFrameHeader.aSamplingFactors[i] & 0x0f);
nMCUBlocksH = max(nMCUBlocksH, oFrameHeader.aSamplingFactors[i] >> 4);
}
for (int i = 0; i < 3; ++i) {
nppiDecodeHuffmanSpecInitAllocHost_JPEG(pHuffmanDCTables[(oScanHeader.aHuffmanTablesSelector[i] >> 4)].aCodes,
nppiDCTable, &apHuffmanDCTableDecode[i]);
nppiDecodeHuffmanSpecInitAllocHost_JPEG(pHuffmanACTables[(oScanHeader.aHuffmanTablesSelector[i] & 0x0f)].aCodes,
nppiACTable, &apHuffmanACTableDecode[i]);
}
for (int i = 0; i < oFrameHeader.nComponents; ++i) {
NppiSize oBlocks;
NppiSize oBlocksPerMCU = { oFrameHeader.aSamplingFactors[i] >> 4, oFrameHeader.aSamplingFactors[i] & 0x0f };
oBlocks.width = (int)ceil((oFrameHeader.nWidth + 7) / 8 *
static_cast<float>(oBlocksPerMCU.width) / nMCUBlocksH);
oBlocks.width = DivUp(oBlocks.width, oBlocksPerMCU.width) * oBlocksPerMCU.width;
oBlocks.height = (int)ceil((oFrameHeader.nHeight + 7) / 8 *
static_cast<float>(oBlocksPerMCU.height) / nMCUBlocksV);
oBlocks.height = DivUp(oBlocks.height, oBlocksPerMCU.height) * oBlocksPerMCU.height;
aDstSize[i].width = oBlocks.width * 8;
aDstSize[i].height = oBlocks.height * 8;
// Allocate Memory
size_t nPitch;
NPP_CHECK_CUDA(cudaMallocPitch(&apdDCT[i], &nPitch, oBlocks.width * 64 * sizeof(Npp16s), oBlocks.height));
aDCTStep[i] = static_cast<Npp32s>(nPitch);
NPP_CHECK_CUDA(cudaMallocPitch(&apDstImage[i], &nPitch, aDstSize[i].width, aDstSize[i].height));
aDstImageStep[i] = static_cast<Npp32s>(nPitch);
NPP_CHECK_CUDA(cudaHostAlloc(&aphDCT[i], aDCTStep[i] * oBlocks.height, cudaHostAllocDefault));
pitch[i] = nPitch;
}
for (int i = 0; i < oFrameHeader.nComponents; ++i) {
NppiSize oBlocks;
NppiSize oBlocksPerMCU = { oFrameHeader.aSamplingFactors[i] >> 4, oFrameHeader.aSamplingFactors[i] & 0x0f };
oBlocks.width = (int)ceil((oFrameHeader.nWidth + 7) / 8 *
static_cast<float>(oBlocksPerMCU.width) / nMCUBlocksH);
oBlocks.width = DivUp(oBlocks.width, oBlocksPerMCU.width) * oBlocksPerMCU.width;
oBlocks.height = (int)ceil((oFrameHeader.nHeight + 7) / 8 *
static_cast<float>(oBlocksPerMCU.height) / nMCUBlocksV);
oBlocks.height = DivUp(oBlocks.height, oBlocksPerMCU.height) * oBlocksPerMCU.height;
aSrcSize[i].width = oBlocks.width * 8;
aSrcSize[i].height = oBlocks.height * 8;
// Allocate Memory
size_t nPitch;
//NPP_CHECK_CUDA(cudaMallocPitch(&apdDCT[i], &nPitch, oBlocks.width * 64 * sizeof(Npp16s), oBlocks.height));
//aDCTStep[i] = static_cast<Npp32s>(nPitch);
NPP_CHECK_CUDA(cudaMallocPitch(&apSrcImage[i], &nPitch, aSrcSize[i].width, aSrcSize[i].height));
aSrcImageStep[i] = static_cast<Npp32s>(nPitch);
//NPP_CHECK_CUDA(cudaHostAlloc(&aphDCT[i], aDCTStep[i] * oBlocks.height, cudaHostAllocDefault));
}
// Huffman Encoding
NPP_CHECK_CUDA(cudaMalloc(&pdScan, sizeof(unsigned char) * 1024 * 1024 * 10));
NPP_CHECK_NPP(nppiEncodeHuffmanGetSize(aSrcSize[0], 3, &nTempSize));
NPP_CHECK_CUDA(cudaMalloc(&pJpegEncoderTemp, nTempSize));
// malloc rgb image buffer
rgb_img_mat_d.create(height, width, CV_8UC3);
rgb_img_d = rgb_img_mat_d.data;
step_rgb = rgb_img_mat_d.step;
// set defaut cfa bayer pattern
this->cfaBayerType = NPPI_BAYER_RGGB;
return 0;
}
/**
@brief release jpeg encode
@return int
*/
int NPPJpegCoder::release() {
// release memory
for (int i = 0; i < 3; ++i) {
nppiEncodeHuffmanSpecFree_JPEG(apHuffmanDCTable[i]);
nppiEncodeHuffmanSpecFree_JPEG(apHuffmanACTable[i]);
nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanDCTableDecode[i]);
nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanACTableDecode[i]);
cudaFree(apdDCT[i]);
cudaFreeHost(aphDCT[i]);
cudaFree(apDstImage[i]);
cudaFree(apSrcImage[i]);
}
cudaFree(pJpegEncoderTemp);
cudaFree(pdQuantizationTables);
cudaFree(pdScan);
//nppiFree(rgb_img_d);
rgb_img_mat_d.release();
return 0;
}
/**
@brief encode raw image data to jpeg
@param unsigned char* bayer_img_d: input bayer image
@param char* jpegdata: output jpeg data
@param size_t* datalength: output data length
@param size_t maxlength: max length (bytes) could be copied to in jpeg data
@param cudaStream_t stream: cudastream
@return
*/
int NPPJpegCoder::encode(unsigned char* bayer_img_d, unsigned char* jpegdata,
size_t* datalength, size_t maxlength, cudaStream_t stream) {
nppSetStream(stream);
NppiDCTState *pDCTState;
#ifdef MEASURE_KERNEL_TIME
cudaEvent_t start, stop;
float elapsedTime;
cudaEventCreate(&start);
cudaEventRecord(start, 0);
#endif
// debayer
NppiSize osize;
osize.width = this->width;
osize.height = this->height;
NppiRect orect;
orect.x = 0;
orect.y = 0;
orect.width = this->width;
orect.height = this->height;
//luminPitch = pitch[0];
//chromaPitchU = pitch[1];
//chromaPitchV = pitch[2];
//NPPJpegCoderKernel::bayerRG2patchYUV(bayerRGImg, apDstImage[0], apDstImage[1],
// apDstImage[2], luminPitch, chromaPitchU, chromaPitchV);
// bayer to rgb
NPP_CHECK_NPP(nppiCFAToRGB_8u_C1C3R(bayer_img_d, this->width, osize,
orect, rgb_img_d, step_rgb, cfaBayerType, NPPI_INTER_UNDEFINED));
if (isWBRaw == false) {
// apply white balance
NPP_CHECK_NPP(nppiColorTwist32f_8u_C3IR(rgb_img_d, step_rgb, osize, wbTwist));
}
// rgb to yuv420
NPP_CHECK_NPP(nppiRGBToYUV420_8u_C3P3R(rgb_img_d, step_rgb, apDstImage, aDstImageStep,
osize));
NPP_CHECK_NPP(nppiDCTInitAlloc(&pDCTState));
// Forward DCT
for (int i = 0; i < 3; ++i) {
NPP_CHECK_NPP(nppiDCTQuantFwd8x8LS_JPEG_8u16s_C1R_NEW(apDstImage[i], aDstImageStep[i],
apdDCT[i], aDCTStep[i],
pdQuantizationTables + oFrameHeader.aQuantizationTableSelector[i] * 64,
aDstSize[i],
pDCTState));
}
NPP_CHECK_NPP(nppiEncodeHuffmanScan_JPEG_8u16s_P3R(apdDCT, aDCTStep,
0, oScanHeader.nSs, oScanHeader.nSe, oScanHeader.nA >> 4, oScanHeader.nA & 0x0f,
pdScan, &nScanLength,
apHuffmanDCTable,
apHuffmanACTable,
aDstSize,
pJpegEncoderTemp));
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("JPEG encode step1: (file:%s, line:%d) elapsed time : %f ms\n", __FILE__, __LINE__, elapsedTime);
#endif
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&start);
cudaEventRecord(start, 0);
#endif
// Write JPEG
unsigned char *pDstOutput = jpegdata;
writeMarker(0x0D8, pDstOutput);
writeJFIFTag(pDstOutput);
writeQuantizationTable(aQuantizationTables[0], pDstOutput);
writeQuantizationTable(aQuantizationTables[1], pDstOutput);
writeFrameHeader(oFrameHeader, pDstOutput);
writeHuffmanTable(pHuffmanDCTables[0], pDstOutput);
writeHuffmanTable(pHuffmanACTables[0], pDstOutput);
writeHuffmanTable(pHuffmanDCTables[1], pDstOutput);
writeHuffmanTable(pHuffmanACTables[1], pDstOutput);
writeScanHeader(oScanHeader, pDstOutput);
NPP_CHECK_CUDA(cudaMemcpyAsync(pDstOutput, pdScan, nScanLength, cudaMemcpyDeviceToHost, stream));
if (static_cast<size_t>(pDstOutput + nScanLength + 2 - jpegdata) > maxlength) {
char info[256];
sprintf(info, "FATAL ERROR: Pre-malloced jpeg data size is too small ! nScanLength = %d, maxlength = %d", nScanLength, maxlength);
std::cout << info << std::endl;
exit(-1);
}
pDstOutput += nScanLength;
writeMarker(0x0D9, pDstOutput);
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("JPEG encode step2: (file:%s, line:%d) elapsed time : %f ms\n", __FILE__, __LINE__, elapsedTime);
#endif
// calculate compressed jpeg data length
*datalength = static_cast<size_t>(pDstOutput - jpegdata);
// release gpu memory
nppiDCTFree(pDCTState);
return 0;
}
/**
@brief encode raw image data to jpeg
@param cv::cuda::GpuMat bayer_img_d: input bayer image
@param unsigned char* jpegdata: output jpeg data
@param size_t* datalength: output data length
@param size_t maxlength: max length (bytes) could be copied to in jpeg data
@param cudaStream_t stream: cudastream
@return int
*/
int NPPJpegCoder::encode(cv::cuda::GpuMat bayer_img_d, unsigned char* jpegdata,
size_t* datalength, size_t maxlength, cv::cuda::Stream & cvstream) {
cudaStream_t stream = cv::cuda::StreamAccessor::getStream(cvstream);
nppSetStream(stream);
NppiDCTState *pDCTState;
#ifdef MEASURE_KERNEL_TIME
cudaEvent_t start, stop;
float elapsedTime;
cudaEventCreate(&start);
cudaEventRecord(start, 0);
#endif
// debayer
NppiSize osize;
osize.width = this->width;
osize.height = this->height;
NppiRect orect;
orect.x = 0;
orect.y = 0;
orect.width = this->width;
orect.height = this->height;
//luminPitch = pitch[0];
//chromaPitchU = pitch[1];
//chromaPitchV = pitch[2];
//NPPJpegCoderKernel::bayerRG2patchYUV(bayerRGImg, apDstImage[0], apDstImage[1],
// apDstImage[2], luminPitch, chromaPitchU, chromaPitchV);
// bayer to rgb
//cv::cuda::Stream cvstream = cv::cuda::StreamAccessor::wrapStream(stream);
#ifdef USE_NPP_DEBAYER
NPP_CHECK_NPP(nppiCFAToRGB_8u_C1C3R(bayer_img_d.data, bayer_img_d.step, osize,
orect, rgb_img_d, step_rgb, cfaBayerType, NPPI_INTER_UNDEFINED));
#else
cv::cuda::demosaicing(bayer_img_d, rgb_img_mat_d, npp::bayerPatternNPP2CVRGB(cfaBayerType), -1
,std::ref(cvstream));
#endif
//cudaStreamSynchronize(stream);
//cv::Mat img;
//rgb_img_mat_d.download(img);
if (isWBRaw == false) {
// apply white balance
NPP_CHECK_NPP(nppiColorTwist32f_8u_C3IR(rgb_img_d, step_rgb, osize, wbTwist));
}
// rgb to yuv420
NPP_CHECK_NPP(nppiRGBToYUV420_8u_C3P3R(rgb_img_d, step_rgb, apDstImage, aDstImageStep,
osize));
NPP_CHECK_NPP(nppiDCTInitAlloc(&pDCTState));
// Forward DCT
for (int i = 0; i < 3; ++i) {
NPP_CHECK_NPP(nppiDCTQuantFwd8x8LS_JPEG_8u16s_C1R_NEW(apDstImage[i], aDstImageStep[i],
apdDCT[i], aDCTStep[i],
pdQuantizationTables + oFrameHeader.aQuantizationTableSelector[i] * 64,
aDstSize[i],
pDCTState));
}
NPP_CHECK_NPP(nppiEncodeHuffmanScan_JPEG_8u16s_P3R(apdDCT, aDCTStep,
0, oScanHeader.nSs, oScanHeader.nSe, oScanHeader.nA >> 4, oScanHeader.nA & 0x0f,
pdScan, &nScanLength,
apHuffmanDCTable,
apHuffmanACTable,
aDstSize,
pJpegEncoderTemp));
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("JPEG encode step1: (file:%s, line:%d) elapsed time : %f ms\n", __FILE__, __LINE__, elapsedTime);
#endif
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&start);
cudaEventRecord(start, 0);
#endif
// Write JPEG
unsigned char *pDstOutput = jpegdata;
writeMarker(0x0D8, pDstOutput);
writeJFIFTag(pDstOutput);
writeQuantizationTable(aQuantizationTables[0], pDstOutput);
writeQuantizationTable(aQuantizationTables[1], pDstOutput);
writeFrameHeader(oFrameHeader, pDstOutput);
writeHuffmanTable(pHuffmanDCTables[0], pDstOutput);
writeHuffmanTable(pHuffmanACTables[0], pDstOutput);
writeHuffmanTable(pHuffmanDCTables[1], pDstOutput);
writeHuffmanTable(pHuffmanACTables[1], pDstOutput);
writeScanHeader(oScanHeader, pDstOutput);
NPP_CHECK_CUDA(cudaMemcpyAsync(pDstOutput, pdScan, nScanLength, cudaMemcpyDeviceToHost, stream));
if (static_cast<size_t>(pDstOutput + nScanLength + 2 - jpegdata) > maxlength) {
char info[256];
sprintf(info, "FATAL ERROR: Pre-malloced jpeg data size is too small ! nScanLength = %d, maxlength = %d", nScanLength, maxlength);
std::cout << info << std::endl;
exit(-1);
}
pDstOutput += nScanLength;
writeMarker(0x0D9, pDstOutput);
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("JPEG encode step2: (file:%s, line:%d) elapsed time : %f ms\n", __FILE__, __LINE__, elapsedTime);
#endif
// calculate compressed jpeg data length
*datalength = static_cast<size_t>(pDstOutput - jpegdata);
// release gpu memory
nppiDCTFree(pDCTState);
return 0;
}
/**
@brief decode jpeg image to raw image data (full)
@param unsigned char* jpegdata: input jpeg data
@param size_t input_datalength: input jpeg data length
@param cv::cuda::GpuMat: output gpu mat image
@param int type: output pixel format type:
0: BGR
1: RGB (default)
@return int
*/
int NPPJpegCoder::decode(unsigned char* jpegdata, size_t input_datalength,
cv::cuda::GpuMat & outimg, int type) {
// init state
NppiDCTState *pDCTState;
NPP_CHECK_NPP(nppiDCTInitAlloc(&pDCTState));
#ifdef MEASURE_KERNEL_TIME
cudaEvent_t start, stop;
float elapsedTime;
cudaEventCreate(&start);
cudaEventRecord(start, 0);
#endif
// check if this is a vlid JPEG file
int nPos = 0;
int nMarker = nextMarker(jpegdata, nPos, input_datalength);
if (nMarker != 0x0D8) {
std::cerr << "Invalid Jpeg Image" << std::endl;
return EXIT_FAILURE;
}
nMarker = nextMarker(jpegdata, nPos, input_datalength);
nMCUBlocksH = 0;
nMCUBlocksV = 0;
int nRestartInterval = -1;
while (nMarker != -1) {
if (nMarker == 0x0D8) {
// Embedded Thumbnail, skip it
int nNextMarker = nextMarker(jpegdata, nPos, input_datalength);
while (nNextMarker != -1 && nNextMarker != 0x0D9) {
nNextMarker = nextMarker(jpegdata, nPos, input_datalength);
}
}
if (nMarker == 0x0DD) {
readRestartInterval(jpegdata + nPos, nRestartInterval);
}
if ((nMarker == 0x0C0) | (nMarker == 0x0C2)) {
//Assert Baseline for this Sample
//Note: NPP does support progressive jpegs for both encode and decode
if (nMarker != 0x0C0) {
cerr << "The sample does only support baseline JPEG images" << endl;
return EXIT_SUCCESS;
}
// Baseline or Progressive Frame Header
readFrameHeader(jpegdata + nPos, oFrameHeader);
#ifdef MEASURE_KERNEL_TIME
cout << "Image Size: " << oFrameHeader.nWidth << "x" << oFrameHeader.nHeight << "x" << static_cast<int>(oFrameHeader.nComponents) << endl;
#endif
//Assert 3-Channel Image for this Sample
if (oFrameHeader.nComponents != 3) {
cerr << "The sample does only support color JPEG images" << endl;
return EXIT_SUCCESS;
}
// Compute channel sizes as stored in the JPEG (8x8 blocks & MCU block layout)
for (int i = 0; i < oFrameHeader.nComponents; ++i) {
nMCUBlocksV = max(nMCUBlocksV, oFrameHeader.aSamplingFactors[i] & 0x0f);
nMCUBlocksH = max(nMCUBlocksH, oFrameHeader.aSamplingFactors[i] >> 4);
}
//for (int i = 0; i < oFrameHeader.nComponents; ++i) {
// NppiSize oBlocks;
// NppiSize oBlocksPerMCU = { oFrameHeader.aSamplingFactors[i] >> 4, oFrameHeader.aSamplingFactors[i] & 0x0f };
// oBlocks.width = (int)ceil((oFrameHeader.nWidth + 7) / 8 *
// static_cast<float>(oBlocksPerMCU.width) / nMCUBlocksH);
// oBlocks.width = DivUp(oBlocks.width, oBlocksPerMCU.width) * oBlocksPerMCU.width;
// oBlocks.height = (int)ceil((oFrameHeader.nHeight + 7) / 8 *
// static_cast<float>(oBlocksPerMCU.height) / nMCUBlocksV);
// oBlocks.height = DivUp(oBlocks.height, oBlocksPerMCU.height) * oBlocksPerMCU.height;
// aSrcSize[i].width = oBlocks.width * 8;
// aSrcSize[i].height = oBlocks.height * 8;
// // Allocate Memory
// size_t nPitch;
// NPP_CHECK_CUDA(cudaMallocPitch(&apdDCT[i], &nPitch, oBlocks.width * 64 * sizeof(Npp16s), oBlocks.height));
// aDCTStep[i] = static_cast<Npp32s>(nPitch);
// NPP_CHECK_CUDA(cudaMallocPitch(&apSrcImage[i], &nPitch, aSrcSize[i].width, aSrcSize[i].height));
// aSrcImageStep[i] = static_cast<Npp32s>(nPitch);
// NPP_CHECK_CUDA(cudaHostAlloc(&aphDCT[i], aDCTStep[i] * oBlocks.height, cudaHostAllocDefault));
//}
}
if (nMarker == 0x0DB) {
// Quantization Tables
readQuantizationTables(jpegdata + nPos, aQuantizationTables);
}
if (nMarker == 0x0C4) {
// Huffman Tables
readHuffmanTables(jpegdata + nPos, aHuffmanTables);
}
if (nMarker == 0x0DA) {
// Scan
readScanHeader(jpegdata + nPos, oScanHeader);
nPos += 6 + oScanHeader.nComponents * 2;
int nAfterNextMarkerPos = nPos;
int nAfterScanMarker = nextMarker(jpegdata, nAfterNextMarkerPos, input_datalength);
if (nRestartInterval > 0) {
while (nAfterScanMarker >= 0x0D0 && nAfterScanMarker <= 0x0D7) {
// This is a restart marker, go on
nAfterScanMarker = nextMarker(jpegdata, nAfterNextMarkerPos, input_datalength);
}
}
//for (int i = 0; i < 3; ++i) {
// nppiDecodeHuffmanSpecInitAllocHost_JPEG(pHuffmanDCTables[(oScanHeader.aHuffmanTablesSelector[i] >> 4)].aCodes,
// nppiDCTable, &apHuffmanDCTableDecode[i]);
// nppiDecodeHuffmanSpecInitAllocHost_JPEG(pHuffmanACTables[(oScanHeader.aHuffmanTablesSelector[i] & 0x0f)].aCodes,
// nppiACTable, &apHuffmanACTableDecode[i]);
//}
NPP_CHECK_NPP(nppiDecodeHuffmanScanHost_JPEG_8u16s_P3R(jpegdata + nPos, nAfterNextMarkerPos - nPos - 2,
nRestartInterval, oScanHeader.nSs, oScanHeader.nSe, oScanHeader.nA >> 4, oScanHeader.nA & 0x0f,
aphDCT, aDCTStep,
apHuffmanDCTableDecode,
apHuffmanACTableDecode,
aSrcSize));
//for (int i = 0; i < 3; ++i){
// nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanDCTableDecode[i]);
// nppiDecodeHuffmanSpecFreeHost_JPEG(apHuffmanACTableDecode[i]);
//}
}
nMarker = nextMarker(jpegdata, nPos, input_datalength);
}
#ifdef MEASURE_KERNEL_TIME
cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);