forked from google/libultrahdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrahdr_app.cpp
1485 lines (1356 loc) · 58.7 KB
/
ultrahdr_app.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
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <sstream>
#include "ultrahdr_api.h"
const float BT601YUVtoRGBMatrix[9] = {
1, 0, 1.402, 1, (-0.202008 / 0.587), (-0.419198 / 0.587), 1.0, 1.772, 0.0};
const float BT709YUVtoRGBMatrix[9] = {
1, 0, 1.5748, 1, (-0.13397432 / 0.7152), (-0.33480248 / 0.7152), 1.0, 1.8556, 0.0};
const float BT2020YUVtoRGBMatrix[9] = {
1, 0, 1.4746, 1, (-0.11156702 / 0.6780), (-0.38737742 / 0.6780), 1, 1.8814, 0};
const float BT601RGBtoYUVMatrix[9] = {
0.299, 0.587, 0.114, (-0.299 / 1.772), (-0.587 / 1.772), 0.5, 0.5, (-0.587 / 1.402),
(-0.114 / 1.402)};
const float BT709RGBtoYUVMatrix[9] = {0.2126,
0.7152,
0.0722,
(-0.2126 / 1.8556),
(-0.7152 / 1.8556),
0.5,
0.5,
(-0.7152 / 1.5748),
(-0.0722 / 1.5748)};
const float BT2020RGBtoYUVMatrix[9] = {0.2627,
0.6780,
0.0593,
(-0.2627 / 1.8814),
(-0.6780 / 1.8814),
0.5,
0.5,
(-0.6780 / 1.4746),
(-0.0593 / 1.4746)};
// remove these once introduced in ultrahdr_api.h
const int UHDR_IMG_FMT_48bppYCbCr444 = 101;
int optind_s = 1;
int optopt_s = 0;
char* optarg_s = nullptr;
int getopt_s(int argc, char* const argv[], char* ostr) {
if (optind_s >= argc) return -1;
const char* arg = argv[optind_s];
if (arg[0] != '-' || !arg[1]) {
std::cerr << "invalid option " << arg << std::endl;
return '?';
}
optopt_s = arg[1];
char* oindex = strchr(ostr, optopt_s);
if (!oindex) {
std::cerr << "unsupported option " << arg << std::endl;
return '?';
}
if (oindex[1] != ':') {
optarg_s = nullptr;
return optopt_s;
}
if (argc > ++optind_s) {
optarg_s = (char*)argv[optind_s++];
} else {
std::cerr << "option " << arg << " requires an argument" << std::endl;
optarg_s = nullptr;
return '?';
}
return optopt_s;
}
// #define PROFILE_ENABLE 1
#ifdef _WIN32
class Profiler {
public:
void timerStart() { QueryPerformanceCounter(&mStartingTime); }
void timerStop() { QueryPerformanceCounter(&mEndingTime); }
int64_t elapsedTime() {
LARGE_INTEGER frequency;
LARGE_INTEGER elapsedMicroseconds;
QueryPerformanceFrequency(&frequency);
elapsedMicroseconds.QuadPart = mEndingTime.QuadPart - mStartingTime.QuadPart;
return (double)elapsedMicroseconds.QuadPart / (double)frequency.QuadPart * 1000000;
}
private:
LARGE_INTEGER mStartingTime;
LARGE_INTEGER mEndingTime;
};
#else
class Profiler {
public:
void timerStart() { gettimeofday(&mStartingTime, nullptr); }
void timerStop() { gettimeofday(&mEndingTime, nullptr); }
int64_t elapsedTime() {
struct timeval elapsedMicroseconds;
elapsedMicroseconds.tv_sec = mEndingTime.tv_sec - mStartingTime.tv_sec;
elapsedMicroseconds.tv_usec = mEndingTime.tv_usec - mStartingTime.tv_usec;
return elapsedMicroseconds.tv_sec * 1000000 + elapsedMicroseconds.tv_usec;
}
private:
struct timeval mStartingTime;
struct timeval mEndingTime;
};
#endif
#define READ_BYTES(DESC, ADDR, LEN) \
DESC.read(static_cast<char*>(ADDR), (LEN)); \
if (DESC.gcount() != (LEN)) { \
std::cerr << "failed to read : " << (LEN) << " bytes, read : " << DESC.gcount() << " bytes" \
<< std::endl; \
return false; \
}
static bool loadFile(const char* filename, void*& result, int length) {
std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
if (ifd.good()) {
int size = ifd.tellg();
if (size < length) {
std::cerr << "requested to read " << length << " bytes from file : " << filename
<< ", file contains only " << size << " bytes" << std::endl;
return false;
}
ifd.seekg(0, std::ios::beg);
result = malloc(length);
if (result == nullptr) {
std::cerr << "failed to allocate memory to store contents of file : " << filename
<< std::endl;
return false;
}
READ_BYTES(ifd, result, length)
return true;
}
std::cerr << "unable to open file : " << filename << std::endl;
return false;
}
static bool loadFile(const char* filename, uhdr_raw_image_t* handle) {
std::ifstream ifd(filename, std::ios::binary);
if (ifd.good()) {
if (handle->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
const int bpp = 2;
READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h * bpp)
READ_BYTES(ifd, handle->planes[UHDR_PLANE_UV], (handle->w / 2) * (handle->h / 2) * bpp * 2)
return true;
} else if (handle->fmt == UHDR_IMG_FMT_32bppRGBA1010102 ||
handle->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
const int bpp = 4;
READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], handle->w * handle->h * bpp)
return true;
} else if (handle->fmt == UHDR_IMG_FMT_12bppYCbCr420) {
READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h)
READ_BYTES(ifd, handle->planes[UHDR_PLANE_U], (handle->w / 2) * (handle->h / 2))
READ_BYTES(ifd, handle->planes[UHDR_PLANE_V], (handle->w / 2) * (handle->h / 2))
return true;
}
return false;
}
std::cerr << "unable to open file : " << filename << std::endl;
return false;
}
static bool writeFile(const char* filename, void*& result, int length) {
std::ofstream ofd(filename, std::ios::binary);
if (ofd.is_open()) {
ofd.write(static_cast<char*>(result), length);
return true;
}
std::cerr << "unable to write to file : " << filename << std::endl;
return false;
}
static bool writeFile(const char* filename, uhdr_raw_image_t* img) {
std::ofstream ofd(filename, std::ios::binary);
if (ofd.is_open()) {
if (img->fmt == UHDR_IMG_FMT_32bppRGBA8888 || img->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ||
img->fmt == UHDR_IMG_FMT_32bppRGBA1010102) {
char* data = static_cast<char*>(img->planes[UHDR_PLANE_PACKED]);
int bpp = img->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ? 8 : 4;
const size_t stride = img->stride[UHDR_PLANE_PACKED] * bpp;
const size_t length = img->w * bpp;
for (unsigned i = 0; i < img->h; i++, data += stride) {
ofd.write(data, length);
}
return true;
} else if ((int)img->fmt == UHDR_IMG_FMT_24bppYCbCr444 ||
(int)img->fmt == UHDR_IMG_FMT_48bppYCbCr444) {
char* data = static_cast<char*>(img->planes[UHDR_PLANE_Y]);
int bpp = (int)img->fmt == UHDR_IMG_FMT_48bppYCbCr444 ? 2 : 1;
size_t stride = img->stride[UHDR_PLANE_Y] * bpp;
size_t length = img->w * bpp;
for (unsigned i = 0; i < img->h; i++, data += stride) {
ofd.write(data, length);
}
data = static_cast<char*>(img->planes[UHDR_PLANE_U]);
stride = img->stride[UHDR_PLANE_U] * bpp;
for (unsigned i = 0; i < img->h; i++, data += stride) {
ofd.write(data, length);
}
data = static_cast<char*>(img->planes[UHDR_PLANE_V]);
stride = img->stride[UHDR_PLANE_V] * bpp;
for (unsigned i = 0; i < img->h; i++, data += stride) {
ofd.write(data, length);
}
return true;
}
return false;
}
std::cerr << "unable to write to file : " << filename << std::endl;
return false;
}
class UltraHdrAppInput {
public:
UltraHdrAppInput(const char* hdrIntentRawFile, const char* sdrIntentRawFile,
const char* sdrIntentCompressedFile, const char* gainmapCompressedFile,
const char* gainmapMetadataCfgFile, const char* outputFile, size_t width,
size_t height, uhdr_img_fmt_t hdrCf = UHDR_IMG_FMT_32bppRGBA1010102,
uhdr_img_fmt_t sdrCf = UHDR_IMG_FMT_32bppRGBA8888,
uhdr_color_gamut_t hdrCg = UHDR_CG_DISPLAY_P3,
uhdr_color_gamut_t sdrCg = UHDR_CG_BT_709,
uhdr_color_transfer_t hdrTf = UHDR_CT_HLG, int quality = 95,
uhdr_color_transfer_t oTf = UHDR_CT_HLG,
uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102, bool isHdrCrFull = false,
int gainmapScaleFactor = 4, int gainmapQuality = 85,
bool enableMultiChannelGainMap = false)
: mHdrIntentRawFile(hdrIntentRawFile),
mSdrIntentRawFile(sdrIntentRawFile),
mSdrIntentCompressedFile(sdrIntentCompressedFile),
mGainMapCompressedFile(gainmapCompressedFile),
mGainMapMetadataCfgFile(gainmapMetadataCfgFile),
mUhdrFile(nullptr),
mOutputFile(outputFile),
mWidth(width),
mHeight(height),
mHdrCf(hdrCf),
mSdrCf(sdrCf),
mHdrCg(hdrCg),
mSdrCg(sdrCg),
mHdrTf(hdrTf),
mQuality(quality),
mOTf(oTf),
mOfmt(oFmt),
mFullRange(isHdrCrFull),
mMapDimensionScaleFactor(gainmapScaleFactor),
mMapCompressQuality(gainmapQuality),
mUseMultiChannelGainMap(enableMultiChannelGainMap),
mMode(0){};
UltraHdrAppInput(const char* uhdrFile, const char* outputFile,
uhdr_color_transfer_t oTf = UHDR_CT_HLG,
uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102)
: mHdrIntentRawFile(nullptr),
mSdrIntentRawFile(nullptr),
mUhdrFile(uhdrFile),
mOutputFile(outputFile),
mWidth(0),
mHeight(0),
mHdrCf(UHDR_IMG_FMT_UNSPECIFIED),
mSdrCf(UHDR_IMG_FMT_UNSPECIFIED),
mHdrCg(UHDR_CG_UNSPECIFIED),
mSdrCg(UHDR_CG_UNSPECIFIED),
mHdrTf(UHDR_CT_UNSPECIFIED),
mQuality(95),
mOTf(oTf),
mOfmt(oFmt),
mFullRange(UHDR_CR_UNSPECIFIED),
mMapDimensionScaleFactor(4),
mMapCompressQuality(85),
mUseMultiChannelGainMap(false),
mMode(1){};
~UltraHdrAppInput() {
int count = sizeof mRawP010Image.planes / sizeof mRawP010Image.planes[UHDR_PLANE_Y];
for (int i = 0; i < count; i++) {
if (mRawP010Image.planes[i]) {
free(mRawP010Image.planes[i]);
mRawP010Image.planes[i] = nullptr;
}
if (mRawRgba1010102Image.planes[i]) {
free(mRawRgba1010102Image.planes[i]);
mRawRgba1010102Image.planes[i] = nullptr;
}
if (mRawYuv420Image.planes[i]) {
free(mRawYuv420Image.planes[i]);
mRawYuv420Image.planes[i] = nullptr;
}
if (mRawRgba8888Image.planes[i]) {
free(mRawRgba8888Image.planes[i]);
mRawRgba8888Image.planes[i] = nullptr;
}
if (mDecodedUhdrRgbImage.planes[i]) {
free(mDecodedUhdrRgbImage.planes[i]);
mDecodedUhdrRgbImage.planes[i] = nullptr;
}
if (mDecodedUhdrYuv444Image.planes[i]) {
free(mDecodedUhdrYuv444Image.planes[i]);
mDecodedUhdrYuv444Image.planes[i] = nullptr;
}
}
if (mUhdrImage.data) free(mUhdrImage.data);
}
bool fillUhdrImageHandle();
bool fillP010ImageHandle();
bool fillRGBA1010102ImageHandle();
bool convertP010ToRGBImage();
bool fillYuv420ImageHandle();
bool fillRGBA8888ImageHandle();
bool convertYuv420ToRGBImage();
bool fillSdrCompressedImageHandle();
bool fillGainMapCompressedImageHandle();
bool fillGainMapMetadataDescriptor();
bool convertRgba8888ToYUV444Image();
bool convertRgba1010102ToYUV444Image();
bool encode();
bool decode();
void computeRGBHdrPSNR();
void computeRGBSdrPSNR();
void computeYUVHdrPSNR();
void computeYUVSdrPSNR();
const char* mHdrIntentRawFile;
const char* mSdrIntentRawFile;
const char* mSdrIntentCompressedFile;
const char* mGainMapCompressedFile;
const char* mGainMapMetadataCfgFile;
const char* mUhdrFile;
const char* mOutputFile;
const int mWidth;
const int mHeight;
const uhdr_img_fmt_t mHdrCf;
const uhdr_img_fmt_t mSdrCf;
const uhdr_color_gamut_t mHdrCg;
const uhdr_color_gamut_t mSdrCg;
const uhdr_color_transfer_t mHdrTf;
const int mQuality;
const uhdr_color_transfer_t mOTf;
const uhdr_img_fmt_t mOfmt;
const bool mFullRange;
const size_t mMapDimensionScaleFactor;
const int mMapCompressQuality;
const bool mUseMultiChannelGainMap;
const int mMode;
uhdr_raw_image_t mRawP010Image{};
uhdr_raw_image_t mRawRgba1010102Image{};
uhdr_raw_image_t mRawYuv420Image{};
uhdr_raw_image_t mRawRgba8888Image{};
uhdr_compressed_image_t mSdrIntentCompressedImage{};
uhdr_compressed_image_t mGainMapCompressedImage{};
uhdr_gainmap_metadata mGainMapMetadata{};
uhdr_compressed_image_t mUhdrImage{};
uhdr_raw_image_t mDecodedUhdrRgbImage{};
uhdr_raw_image_t mDecodedUhdrYuv444Image{};
double mPsnr[3]{};
};
bool UltraHdrAppInput::fillP010ImageHandle() {
const int bpp = 2;
int p010Size = mWidth * mHeight * bpp * 1.5;
mRawP010Image.fmt = UHDR_IMG_FMT_24bppYCbCrP010;
mRawP010Image.cg = mHdrCg;
mRawP010Image.ct = mHdrTf;
mRawP010Image.range = mFullRange ? UHDR_CR_FULL_RANGE : UHDR_CR_LIMITED_RANGE;
mRawP010Image.w = mWidth;
mRawP010Image.h = mHeight;
mRawP010Image.planes[UHDR_PLANE_Y] = malloc(mWidth * mHeight * bpp);
mRawP010Image.planes[UHDR_PLANE_UV] = malloc((mWidth / 2) * (mHeight / 2) * bpp * 2);
mRawP010Image.planes[UHDR_PLANE_V] = nullptr;
mRawP010Image.stride[UHDR_PLANE_Y] = mWidth;
mRawP010Image.stride[UHDR_PLANE_UV] = mWidth;
mRawP010Image.stride[UHDR_PLANE_V] = 0;
return loadFile(mHdrIntentRawFile, &mRawP010Image);
}
bool UltraHdrAppInput::fillYuv420ImageHandle() {
int yuv420Size = mWidth * mHeight * 1.5;
mRawYuv420Image.fmt = UHDR_IMG_FMT_12bppYCbCr420;
mRawYuv420Image.cg = mSdrCg;
mRawYuv420Image.ct = UHDR_CT_SRGB;
mRawYuv420Image.range = UHDR_CR_FULL_RANGE;
mRawYuv420Image.w = mWidth;
mRawYuv420Image.h = mHeight;
mRawYuv420Image.planes[UHDR_PLANE_Y] = malloc(mWidth * mHeight);
mRawYuv420Image.planes[UHDR_PLANE_U] = malloc((mWidth / 2) * (mHeight / 2));
mRawYuv420Image.planes[UHDR_PLANE_V] = malloc((mWidth / 2) * (mHeight / 2));
mRawYuv420Image.stride[UHDR_PLANE_Y] = mWidth;
mRawYuv420Image.stride[UHDR_PLANE_U] = mWidth / 2;
mRawYuv420Image.stride[UHDR_PLANE_V] = mWidth / 2;
return loadFile(mSdrIntentRawFile, &mRawYuv420Image);
}
bool UltraHdrAppInput::fillRGBA1010102ImageHandle() {
const int bpp = 4;
mRawRgba1010102Image.fmt = UHDR_IMG_FMT_32bppRGBA1010102;
mRawRgba1010102Image.cg = mHdrCg;
mRawRgba1010102Image.ct = mHdrTf;
mRawRgba1010102Image.range = UHDR_CR_FULL_RANGE;
mRawRgba1010102Image.w = mWidth;
mRawRgba1010102Image.h = mHeight;
mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(mWidth * mHeight * bpp);
mRawRgba1010102Image.planes[UHDR_PLANE_UV] = nullptr;
mRawRgba1010102Image.planes[UHDR_PLANE_V] = nullptr;
mRawRgba1010102Image.stride[UHDR_PLANE_PACKED] = mWidth;
mRawRgba1010102Image.stride[UHDR_PLANE_UV] = 0;
mRawRgba1010102Image.stride[UHDR_PLANE_V] = 0;
return loadFile(mHdrIntentRawFile, &mRawRgba1010102Image);
}
bool UltraHdrAppInput::fillRGBA8888ImageHandle() {
const int bpp = 4;
mRawRgba8888Image.fmt = UHDR_IMG_FMT_32bppRGBA8888;
mRawRgba8888Image.cg = mSdrCg;
mRawRgba8888Image.ct = UHDR_CT_SRGB;
mRawRgba8888Image.range = UHDR_CR_FULL_RANGE;
mRawRgba8888Image.w = mWidth;
mRawRgba8888Image.h = mHeight;
mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(mWidth * mHeight * bpp);
mRawRgba8888Image.planes[UHDR_PLANE_U] = nullptr;
mRawRgba8888Image.planes[UHDR_PLANE_V] = nullptr;
mRawRgba8888Image.stride[UHDR_PLANE_Y] = mWidth;
mRawRgba8888Image.stride[UHDR_PLANE_U] = 0;
mRawRgba8888Image.stride[UHDR_PLANE_V] = 0;
return loadFile(mSdrIntentRawFile, &mRawRgba8888Image);
}
bool UltraHdrAppInput::fillSdrCompressedImageHandle() {
std::ifstream ifd(mSdrIntentCompressedFile, std::ios::binary | std::ios::ate);
if (ifd.good()) {
int size = ifd.tellg();
mSdrIntentCompressedImage.capacity = size;
mSdrIntentCompressedImage.data_sz = size;
mSdrIntentCompressedImage.data = nullptr;
mSdrIntentCompressedImage.cg = mSdrCg;
mSdrIntentCompressedImage.ct = UHDR_CT_UNSPECIFIED;
mSdrIntentCompressedImage.range = UHDR_CR_UNSPECIFIED;
ifd.close();
return loadFile(mSdrIntentCompressedFile, mSdrIntentCompressedImage.data, size);
}
return false;
}
bool UltraHdrAppInput::fillGainMapCompressedImageHandle() {
std::ifstream ifd(mGainMapCompressedFile, std::ios::binary | std::ios::ate);
if (ifd.good()) {
int size = ifd.tellg();
mGainMapCompressedImage.capacity = size;
mGainMapCompressedImage.data_sz = size;
mGainMapCompressedImage.data = nullptr;
mGainMapCompressedImage.cg = UHDR_CG_UNSPECIFIED;
mGainMapCompressedImage.ct = UHDR_CT_UNSPECIFIED;
mGainMapCompressedImage.range = UHDR_CR_UNSPECIFIED;
ifd.close();
return loadFile(mGainMapCompressedFile, mGainMapCompressedImage.data, size);
}
return false;
}
void parse_argument(uhdr_gainmap_metadata* metadata, char* argument, float* value) {
if (!strcmp(argument, "maxContentBoost"))
metadata->max_content_boost = *value;
else if (!strcmp(argument, "minContentBoost"))
metadata->min_content_boost = *value;
else if (!strcmp(argument, "gamma"))
metadata->gamma = *value;
else if (!strcmp(argument, "offsetSdr"))
metadata->offset_sdr = *value;
else if (!strcmp(argument, "offsetHdr"))
metadata->offset_hdr = *value;
else if (!strcmp(argument, "hdrCapacityMin"))
metadata->hdr_capacity_min = *value;
else if (!strcmp(argument, "hdrCapacityMax"))
metadata->hdr_capacity_max = *value;
else
std::cout << " Ignoring argument " << argument << std::endl;
}
bool UltraHdrAppInput::fillGainMapMetadataDescriptor() {
std::ifstream file(mGainMapMetadataCfgFile);
if (!file.is_open()) {
return false;
}
std::string line;
char argument[128];
float value;
while (std::getline(file, line)) {
if (sscanf(line.c_str(), "--%s %f", argument, &value) == 2) {
parse_argument(&mGainMapMetadata, argument, &value);
}
}
file.close();
return true;
}
bool UltraHdrAppInput::fillUhdrImageHandle() {
std::ifstream ifd(mUhdrFile, std::ios::binary | std::ios::ate);
if (ifd.good()) {
int size = ifd.tellg();
mUhdrImage.capacity = size;
mUhdrImage.data_sz = size;
mUhdrImage.data = nullptr;
mUhdrImage.cg = UHDR_CG_UNSPECIFIED;
mUhdrImage.ct = UHDR_CT_UNSPECIFIED;
mUhdrImage.range = UHDR_CR_UNSPECIFIED;
ifd.close();
return loadFile(mUhdrFile, mUhdrImage.data, size);
}
return false;
}
bool UltraHdrAppInput::encode() {
if (mHdrIntentRawFile != nullptr) {
if (mHdrCf == UHDR_IMG_FMT_24bppYCbCrP010) {
if (!fillP010ImageHandle()) {
std::cerr << " failed to load file " << mHdrIntentRawFile << std::endl;
return false;
}
} else if (mHdrCf == UHDR_IMG_FMT_32bppRGBA1010102) {
if (!fillRGBA1010102ImageHandle()) {
std::cerr << " failed to load file " << mHdrIntentRawFile << std::endl;
return false;
}
} else {
std::cerr << " invalid hdr intent color format " << mHdrCf << std::endl;
return false;
}
}
if (mSdrIntentRawFile != nullptr) {
if (mSdrCf == UHDR_IMG_FMT_12bppYCbCr420) {
if (!fillYuv420ImageHandle()) {
std::cerr << " failed to load file " << mSdrIntentRawFile << std::endl;
return false;
}
} else if (mSdrCf == UHDR_IMG_FMT_32bppRGBA8888) {
if (!fillRGBA8888ImageHandle()) {
std::cerr << " failed to load file " << mSdrIntentRawFile << std::endl;
return false;
}
} else {
std::cerr << " invalid sdr intent color format " << mSdrCf << std::endl;
return false;
}
}
if (mSdrIntentCompressedFile != nullptr) {
if (!fillSdrCompressedImageHandle()) {
std::cerr << " failed to load file " << mSdrIntentCompressedFile << std::endl;
return false;
}
}
if (mGainMapCompressedFile != nullptr && mGainMapMetadataCfgFile != nullptr) {
if (!fillGainMapCompressedImageHandle()) {
std::cerr << " failed to load file " << mGainMapCompressedFile << std::endl;
return false;
}
if (!fillGainMapMetadataDescriptor()) {
std::cerr << " failed to read config file " << mGainMapMetadataCfgFile << std::endl;
return false;
}
}
#define RET_IF_ERR(x) \
{ \
uhdr_error_info_t status = (x); \
if (status.error_code != UHDR_CODEC_OK) { \
if (status.has_detail) { \
std::cerr << status.detail << std::endl; \
} \
uhdr_release_encoder(handle); \
return false; \
} \
}
uhdr_codec_private_t* handle = uhdr_create_encoder();
if (mHdrIntentRawFile != nullptr) {
if (mHdrCf == UHDR_IMG_FMT_24bppYCbCrP010) {
RET_IF_ERR(uhdr_enc_set_raw_image(handle, &mRawP010Image, UHDR_HDR_IMG))
} else if (mHdrCf == UHDR_IMG_FMT_32bppRGBA1010102) {
RET_IF_ERR(uhdr_enc_set_raw_image(handle, &mRawRgba1010102Image, UHDR_HDR_IMG))
}
}
if (mSdrIntentRawFile != nullptr) {
if (mSdrCf == UHDR_IMG_FMT_12bppYCbCr420) {
RET_IF_ERR(uhdr_enc_set_raw_image(handle, &mRawYuv420Image, UHDR_SDR_IMG))
} else if (mSdrCf == UHDR_IMG_FMT_32bppRGBA8888) {
RET_IF_ERR(uhdr_enc_set_raw_image(handle, &mRawRgba8888Image, UHDR_SDR_IMG))
}
}
if (mSdrIntentCompressedFile != nullptr) {
RET_IF_ERR(uhdr_enc_set_compressed_image(
handle, &mSdrIntentCompressedImage,
(mGainMapCompressedFile != nullptr && mGainMapMetadataCfgFile != nullptr) ? UHDR_BASE_IMG
: UHDR_SDR_IMG))
}
if (mGainMapCompressedFile != nullptr && mGainMapMetadataCfgFile != nullptr) {
RET_IF_ERR(uhdr_enc_set_gainmap_image(handle, &mGainMapCompressedImage, &mGainMapMetadata))
}
RET_IF_ERR(uhdr_enc_set_quality(handle, mQuality, UHDR_BASE_IMG))
RET_IF_ERR(uhdr_enc_set_quality(handle, mMapCompressQuality, UHDR_GAIN_MAP_IMG))
RET_IF_ERR(uhdr_enc_set_using_multi_channel_gainmap(handle, mUseMultiChannelGainMap))
RET_IF_ERR(uhdr_enc_set_gainmap_scale_factor(handle, mMapDimensionScaleFactor))
#ifdef PROFILE_ENABLE
Profiler profileEncode;
profileEncode.timerStart();
#endif
RET_IF_ERR(uhdr_encode(handle))
#ifdef PROFILE_ENABLE
profileEncode.timerStop();
auto avgEncTime = profileEncode.elapsedTime() / 1000.f;
printf("Average encode time for res %d x %d is %f ms \n", mWidth, mHeight, avgEncTime);
#endif
#undef RET_IF_ERR
auto output = uhdr_get_encoded_stream(handle);
// for decoding
mUhdrImage.data = malloc(output->data_sz);
memcpy(mUhdrImage.data, output->data, output->data_sz);
mUhdrImage.capacity = mUhdrImage.data_sz = output->data_sz;
mUhdrImage.cg = output->cg;
mUhdrImage.ct = output->ct;
mUhdrImage.range = output->range;
uhdr_release_encoder(handle);
return writeFile(mOutputFile, mUhdrImage.data, mUhdrImage.data_sz);
}
bool UltraHdrAppInput::decode() {
if (mMode == 1 && !fillUhdrImageHandle()) {
std::cerr << " failed to load file " << mUhdrFile << std::endl;
return false;
}
#define RET_IF_ERR(x) \
{ \
uhdr_error_info_t status = (x); \
if (status.error_code != UHDR_CODEC_OK) { \
if (status.has_detail) { \
std::cerr << status.detail << std::endl; \
} \
uhdr_release_decoder(handle); \
return false; \
} \
}
uhdr_codec_private_t* handle = uhdr_create_decoder();
RET_IF_ERR(uhdr_dec_set_image(handle, &mUhdrImage))
RET_IF_ERR(uhdr_dec_set_out_color_transfer(handle, mOTf))
RET_IF_ERR(uhdr_dec_set_out_img_format(handle, mOfmt))
#ifdef PROFILE_ENABLE
Profiler profileDecode;
profileDecode.timerStart();
#endif
RET_IF_ERR(uhdr_decode(handle))
#ifdef PROFILE_ENABLE
profileDecode.timerStop();
auto avgDecTime = profileDecode.elapsedTime() / 1000.f;
printf("Average decode time for res %ld x %ld is %f ms \n", uhdr_dec_get_image_width(handle),
uhdr_dec_get_image_height(handle), avgDecTime);
#endif
#undef RET_IF_ERR
uhdr_raw_image_t* output = uhdr_get_decoded_image(handle);
mDecodedUhdrRgbImage.fmt = output->fmt;
mDecodedUhdrRgbImage.cg = output->cg;
mDecodedUhdrRgbImage.ct = output->ct;
mDecodedUhdrRgbImage.range = output->range;
mDecodedUhdrRgbImage.w = output->w;
mDecodedUhdrRgbImage.h = output->h;
int bpp = (output->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) ? 8 : 4;
mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED] = malloc(output->w * output->h * bpp);
char* inData = static_cast<char*>(output->planes[UHDR_PLANE_PACKED]);
char* outData = static_cast<char*>(mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED]);
const size_t inStride = output->stride[UHDR_PLANE_PACKED] * bpp;
const size_t outStride = output->w * bpp;
mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] = output->w;
const size_t length = output->w * bpp;
for (unsigned i = 0; i < output->h; i++, inData += inStride, outData += outStride) {
memcpy(outData, inData, length);
}
uhdr_release_decoder(handle);
return mMode == 1 ? writeFile(mOutputFile, &mDecodedUhdrRgbImage) : true;
}
#define CLIP3(x, min, max) ((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x)
bool UltraHdrAppInput::convertP010ToRGBImage() {
const float* coeffs = BT2020YUVtoRGBMatrix;
if (mHdrCg == UHDR_CG_BT_709) {
coeffs = BT709YUVtoRGBMatrix;
} else if (mHdrCg == UHDR_CG_BT_2100) {
coeffs = BT2020YUVtoRGBMatrix;
} else if (mHdrCg == UHDR_CG_DISPLAY_P3) {
coeffs = BT601YUVtoRGBMatrix;
} else {
std::cerr << "color matrix not present for gamut " << mHdrCg << " using BT2020Matrix"
<< std::endl;
}
mRawRgba1010102Image.fmt = UHDR_IMG_FMT_32bppRGBA1010102;
mRawRgba1010102Image.cg = mRawP010Image.cg;
mRawRgba1010102Image.ct = mRawP010Image.ct;
mRawRgba1010102Image.range = UHDR_CR_FULL_RANGE;
mRawRgba1010102Image.w = mRawP010Image.w;
mRawRgba1010102Image.h = mRawP010Image.h;
mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(mRawP010Image.w * mRawP010Image.h * 4);
mRawRgba1010102Image.planes[UHDR_PLANE_U] = nullptr;
mRawRgba1010102Image.planes[UHDR_PLANE_V] = nullptr;
mRawRgba1010102Image.stride[UHDR_PLANE_PACKED] = mWidth;
mRawRgba1010102Image.stride[UHDR_PLANE_U] = 0;
mRawRgba1010102Image.stride[UHDR_PLANE_V] = 0;
uint32_t* rgbData = static_cast<uint32_t*>(mRawRgba1010102Image.planes[UHDR_PLANE_PACKED]);
uint16_t* y = static_cast<uint16_t*>(mRawP010Image.planes[UHDR_PLANE_Y]);
uint16_t* u = static_cast<uint16_t*>(mRawP010Image.planes[UHDR_PLANE_UV]);
uint16_t* v = u + 1;
for (size_t i = 0; i < mRawP010Image.h; i++) {
for (size_t j = 0; j < mRawP010Image.w; j++) {
float y0 = float(y[mRawP010Image.stride[UHDR_PLANE_Y] * i + j] >> 6);
float u0 = float(u[mRawP010Image.stride[UHDR_PLANE_UV] * (i / 2) + (j / 2) * 2] >> 6);
float v0 = float(v[mRawP010Image.stride[UHDR_PLANE_UV] * (i / 2) + (j / 2) * 2] >> 6);
y0 = CLIP3(y0, 64.0f, 940.0f);
u0 = CLIP3(u0, 64.0f, 960.0f);
v0 = CLIP3(v0, 64.0f, 960.0f);
y0 = (y0 - 64.0f) / 876.0f;
u0 = (u0 - 512.0f) / 896.0f;
v0 = (v0 - 512.0f) / 896.0f;
float r = coeffs[0] * y0 + coeffs[1] * u0 + coeffs[2] * v0;
float g = coeffs[3] * y0 + coeffs[4] * u0 + coeffs[5] * v0;
float b = coeffs[6] * y0 + coeffs[7] * u0 + coeffs[8] * v0;
r = CLIP3(r * 1023.0f + 0.5f, 0.0f, 1023.0f);
g = CLIP3(g * 1023.0f + 0.5f, 0.0f, 1023.0f);
b = CLIP3(b * 1023.0f + 0.5f, 0.0f, 1023.0f);
int32_t r0 = int32_t(r);
int32_t g0 = int32_t(g);
int32_t b0 = int32_t(b);
*rgbData = (0x3ff & r0) | ((0x3ff & g0) << 10) | ((0x3ff & b0) << 20) |
(0x3 << 30); // Set alpha to 1.0
rgbData++;
}
}
#ifdef DUMP_DEBUG_DATA
writeFile("inRgba1010102.raw", &mRawRgba1010102Image);
#endif
return true;
}
bool UltraHdrAppInput::convertYuv420ToRGBImage() {
mRawRgba8888Image.fmt = UHDR_IMG_FMT_32bppRGBA8888;
mRawRgba8888Image.cg = mRawYuv420Image.cg;
mRawRgba8888Image.ct = mRawYuv420Image.ct;
mRawRgba8888Image.range = UHDR_CR_FULL_RANGE;
mRawRgba8888Image.w = mRawYuv420Image.w;
mRawRgba8888Image.h = mRawYuv420Image.h;
mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(mRawYuv420Image.w * mRawYuv420Image.h * 4);
mRawRgba8888Image.planes[UHDR_PLANE_U] = nullptr;
mRawRgba8888Image.planes[UHDR_PLANE_V] = nullptr;
mRawRgba8888Image.stride[UHDR_PLANE_PACKED] = mWidth;
mRawRgba8888Image.stride[UHDR_PLANE_U] = 0;
mRawRgba8888Image.stride[UHDR_PLANE_V] = 0;
uint32_t* rgbData = static_cast<uint32_t*>(mRawRgba8888Image.planes[UHDR_PLANE_PACKED]);
uint8_t* y = static_cast<uint8_t*>(mRawYuv420Image.planes[UHDR_PLANE_Y]);
uint8_t* u = static_cast<uint8_t*>(mRawYuv420Image.planes[UHDR_PLANE_U]);
uint8_t* v = static_cast<uint8_t*>(mRawYuv420Image.planes[UHDR_PLANE_V]);
const float* coeffs = BT601YUVtoRGBMatrix;
if (mSdrCg == UHDR_CG_BT_709) {
coeffs = BT709YUVtoRGBMatrix;
} else if (mSdrCg == UHDR_CG_BT_2100) {
coeffs = BT2020YUVtoRGBMatrix;
} else if (mSdrCg == UHDR_CG_DISPLAY_P3) {
coeffs = BT601YUVtoRGBMatrix;
} else {
std::cerr << "color matrix not present for gamut " << mSdrCg << " using BT601Matrix"
<< std::endl;
}
for (size_t i = 0; i < mRawYuv420Image.h; i++) {
for (size_t j = 0; j < mRawYuv420Image.w; j++) {
float y0 = float(y[mRawYuv420Image.stride[UHDR_PLANE_Y] * i + j]);
float u0 = float(u[mRawYuv420Image.stride[UHDR_PLANE_U] * (i / 2) + (j / 2)] - 128);
float v0 = float(v[mRawYuv420Image.stride[UHDR_PLANE_V] * (i / 2) + (j / 2)] - 128);
y0 /= 255.0f;
u0 /= 255.0f;
v0 /= 255.0f;
float r = coeffs[0] * y0 + coeffs[1] * u0 + coeffs[2] * v0;
float g = coeffs[3] * y0 + coeffs[4] * u0 + coeffs[5] * v0;
float b = coeffs[6] * y0 + coeffs[7] * u0 + coeffs[8] * v0;
r = r * 255.0f + 0.5f;
g = g * 255.0f + 0.5f;
b = b * 255.0f + 0.5f;
r = CLIP3(r, 0.0f, 255.0f);
g = CLIP3(g, 0.0f, 255.0f);
b = CLIP3(b, 0.0f, 255.0f);
int32_t r0 = int32_t(r);
int32_t g0 = int32_t(g);
int32_t b0 = int32_t(b);
*rgbData = r0 | (g0 << 8) | (b0 << 16) | (255 << 24); // Set alpha to 1.0
rgbData++;
}
}
#ifdef DUMP_DEBUG_DATA
writeFile("inRgba8888.raw", &mRawRgba8888Image);
#endif
return true;
}
bool UltraHdrAppInput::convertRgba8888ToYUV444Image() {
mDecodedUhdrYuv444Image.fmt = static_cast<uhdr_img_fmt_t>(UHDR_IMG_FMT_24bppYCbCr444);
mDecodedUhdrYuv444Image.cg = mDecodedUhdrRgbImage.cg;
mDecodedUhdrYuv444Image.ct = mDecodedUhdrRgbImage.ct;
mDecodedUhdrYuv444Image.range = UHDR_CR_FULL_RANGE;
mDecodedUhdrYuv444Image.w = mDecodedUhdrRgbImage.w;
mDecodedUhdrYuv444Image.h = mDecodedUhdrRgbImage.h;
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h);
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h);
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h);
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] = mWidth;
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] = mWidth;
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] = mWidth;
uint32_t* rgbData = static_cast<uint32_t*>(mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED]);
uint8_t* yData = static_cast<uint8_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y]);
uint8_t* uData = static_cast<uint8_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U]);
uint8_t* vData = static_cast<uint8_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V]);
const float* coeffs = BT601RGBtoYUVMatrix;
if (mDecodedUhdrRgbImage.cg == UHDR_CG_BT_709) {
coeffs = BT709RGBtoYUVMatrix;
} else if (mDecodedUhdrRgbImage.cg == UHDR_CG_BT_2100) {
coeffs = BT2020RGBtoYUVMatrix;
} else if (mDecodedUhdrRgbImage.cg == UHDR_CG_DISPLAY_P3) {
coeffs = BT601RGBtoYUVMatrix;
} else {
std::cerr << "color matrix not present for gamut " << mDecodedUhdrRgbImage.cg
<< " using BT601Matrix" << std::endl;
}
for (size_t i = 0; i < mDecodedUhdrRgbImage.h; i++) {
for (size_t j = 0; j < mDecodedUhdrRgbImage.w; j++) {
float r0 = float(rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] & 0xff);
float g0 =
float((rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] >> 8) & 0xff);
float b0 =
float((rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] >> 16) & 0xff);
r0 /= 255.0f;
g0 /= 255.0f;
b0 /= 255.0f;
float y = coeffs[0] * r0 + coeffs[1] * g0 + coeffs[2] * b0;
float u = coeffs[3] * r0 + coeffs[4] * g0 + coeffs[5] * b0;
float v = coeffs[6] * r0 + coeffs[7] * g0 + coeffs[8] * b0;
y = y * 255.0f + 0.5f;
u = u * 255.0f + 0.5f + 128.0f;
v = v * 255.0f + 0.5f + 128.0f;
y = CLIP3(y, 0.0f, 255.0f);
u = CLIP3(u, 0.0f, 255.0f);
v = CLIP3(v, 0.0f, 255.0f);
yData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] * i + j] = uint8_t(y);
uData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] * i + j] = uint8_t(u);
vData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] * i + j] = uint8_t(v);
}
}
#ifdef DUMP_DEBUG_DATA
writeFile("outyuv444.yuv", &mDecodedUhdrYuv444Image);
#endif
return true;
}
bool UltraHdrAppInput::convertRgba1010102ToYUV444Image() {
const float* coeffs = BT2020RGBtoYUVMatrix;
if (mDecodedUhdrRgbImage.cg == UHDR_CG_BT_709) {
coeffs = BT709RGBtoYUVMatrix;
} else if (mDecodedUhdrRgbImage.cg == UHDR_CG_BT_2100) {
coeffs = BT2020RGBtoYUVMatrix;
} else if (mDecodedUhdrRgbImage.cg == UHDR_CG_DISPLAY_P3) {
coeffs = BT601RGBtoYUVMatrix;
} else {
std::cerr << "color matrix not present for gamut " << mDecodedUhdrRgbImage.cg
<< " using BT2020Matrix" << std::endl;
}
mDecodedUhdrYuv444Image.fmt = static_cast<uhdr_img_fmt_t>(UHDR_IMG_FMT_48bppYCbCr444);
mDecodedUhdrYuv444Image.cg = mDecodedUhdrRgbImage.cg;
mDecodedUhdrYuv444Image.ct = mDecodedUhdrRgbImage.ct;
mDecodedUhdrYuv444Image.range = UHDR_CR_LIMITED_RANGE;
mDecodedUhdrYuv444Image.w = mDecodedUhdrRgbImage.w;
mDecodedUhdrYuv444Image.h = mDecodedUhdrRgbImage.h;
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2);
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2);
mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V] =
malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2);
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] = mWidth;
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] = mWidth;
mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] = mWidth;
uint32_t* rgbData = static_cast<uint32_t*>(mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED]);
uint16_t* yData = static_cast<uint16_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y]);
uint16_t* uData = static_cast<uint16_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U]);
uint16_t* vData = static_cast<uint16_t*>(mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V]);
for (size_t i = 0; i < mDecodedUhdrRgbImage.h; i++) {
for (size_t j = 0; j < mDecodedUhdrRgbImage.w; j++) {
float r0 = float(rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] & 0x3ff);
float g0 =
float((rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] >> 10) & 0x3ff);
float b0 =
float((rgbData[mDecodedUhdrRgbImage.stride[UHDR_PLANE_PACKED] * i + j] >> 20) & 0x3ff);
r0 /= 1023.0f;
g0 /= 1023.0f;
b0 /= 1023.0f;
float y = coeffs[0] * r0 + coeffs[1] * g0 + coeffs[2] * b0;
float u = coeffs[3] * r0 + coeffs[4] * g0 + coeffs[5] * b0;
float v = coeffs[6] * r0 + coeffs[7] * g0 + coeffs[8] * b0;
y = (y * 876.0f) + 64.0f + 0.5f;
u = (u * 896.0f) + 512.0f + 0.5f;
v = (v * 896.0f) + 512.0f + 0.5f;
y = CLIP3(y, 64.0f, 940.0f);
u = CLIP3(u, 64.0f, 960.0f);
v = CLIP3(v, 64.0f, 960.0f);
yData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] * i + j] = uint16_t(y);
uData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] * i + j] = uint16_t(u);
vData[mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] * i + j] = uint16_t(v);
}
}
#ifdef DUMP_DEBUG_DATA
writeFile("outyuv444.yuv", &mDecodedUhdrYuv444Image);
#endif
return true;
}
void UltraHdrAppInput::computeRGBHdrPSNR() {
if (mOfmt != UHDR_IMG_FMT_32bppRGBA1010102) {
std::cout << "psnr not supported for output format " << mOfmt << std::endl;