-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathjpegr.cpp
More file actions
2764 lines (2503 loc) · 115 KB
/
jpegr.cpp
File metadata and controls
2764 lines (2503 loc) · 115 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 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>
#include <sysinfoapi.h>
#else
#include <unistd.h>
#endif
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <thread>
#include "ultrahdr/editorhelper.h"
#include "ultrahdr/gainmapmetadata.h"
#include "ultrahdr/ultrahdrcommon.h"
#include "ultrahdr/jpegr.h"
#include "ultrahdr/icc.h"
#include "ultrahdr/multipictureformat.h"
#include "image_io/base/data_segment_data_source.h"
#include "image_io/jpeg/jpeg_info.h"
#include "image_io/jpeg/jpeg_info_builder.h"
#include "image_io/jpeg/jpeg_marker.h"
#include "image_io/jpeg/jpeg_scanner.h"
using namespace std;
using namespace photos_editing_formats::image_io;
namespace ultrahdr {
#ifdef UHDR_ENABLE_GLES
uhdr_error_info_t applyGainMapGLES(uhdr_raw_image_t* sdr_intent, uhdr_raw_image_t* gainmap_img,
uhdr_gainmap_metadata_ext_t* gainmap_metadata,
uhdr_color_transfer_t output_ct, float display_boost,
uhdr_color_gamut_t sdr_cg, uhdr_color_gamut_t hdr_cg,
uhdr_opengl_ctxt_t* opengl_ctxt);
#endif
// Gain map metadata
#ifdef UHDR_WRITE_XMP
static const bool kWriteXmpMetadata = true;
#else
static const bool kWriteXmpMetadata = false;
#endif
#ifdef UHDR_WRITE_ISO
static const bool kWriteIso21496_1Metadata = true;
#else
static const bool kWriteIso21496_1Metadata = false;
#endif
static const string kXmpNameSpace = "http://ns.adobe.com/xap/1.0/";
static const string kIsoNameSpace = "urn:iso:std:iso:ts:21496:-1";
static_assert(kWriteXmpMetadata || kWriteIso21496_1Metadata,
"Must write gain map metadata in XMP format, or iso 21496-1 format, or both.");
class JobQueue {
public:
bool dequeueJob(unsigned int& rowStart, unsigned int& rowEnd);
void enqueueJob(unsigned int rowStart, unsigned int rowEnd);
void markQueueForEnd();
void reset();
private:
bool mQueuedAllJobs = false;
std::deque<std::tuple<unsigned int, unsigned int>> mJobs;
std::mutex mMutex;
std::condition_variable mCv;
};
bool JobQueue::dequeueJob(unsigned int& rowStart, unsigned int& rowEnd) {
std::unique_lock<std::mutex> lock{mMutex};
while (true) {
if (mJobs.empty()) {
if (mQueuedAllJobs) {
return false;
} else {
mCv.wait_for(lock, std::chrono::milliseconds(100));
}
} else {
auto it = mJobs.begin();
rowStart = std::get<0>(*it);
rowEnd = std::get<1>(*it);
mJobs.erase(it);
return true;
}
}
return false;
}
void JobQueue::enqueueJob(unsigned int rowStart, unsigned int rowEnd) {
std::unique_lock<std::mutex> lock{mMutex};
mJobs.push_back(std::make_tuple(rowStart, rowEnd));
lock.unlock();
mCv.notify_one();
}
void JobQueue::markQueueForEnd() {
std::unique_lock<std::mutex> lock{mMutex};
mQueuedAllJobs = true;
lock.unlock();
mCv.notify_all();
}
void JobQueue::reset() {
std::unique_lock<std::mutex> lock{mMutex};
mJobs.clear();
mQueuedAllJobs = false;
}
/*
* MessageWriter implementation for ALOG functions.
*/
class AlogMessageWriter : public MessageWriter {
public:
void WriteMessage(const Message& message) override {
std::string log = GetFormattedMessage(message);
ALOGD("%s", log.c_str());
}
};
unsigned int GetCPUCoreCount() { return (std::max)(1u, std::thread::hardware_concurrency()); }
JpegR::JpegR(void* uhdrGLESCtxt, int mapDimensionScaleFactor, int mapCompressQuality,
bool useMultiChannelGainMap, float gamma, uhdr_enc_preset_t preset,
float minContentBoost, float maxContentBoost, float targetDispPeakBrightness) {
mUhdrGLESCtxt = uhdrGLESCtxt;
mMapDimensionScaleFactor = mapDimensionScaleFactor;
mMapCompressQuality = mapCompressQuality;
mUseMultiChannelGainMap = useMultiChannelGainMap;
mGamma = gamma;
mEncPreset = preset;
mMinContentBoost = minContentBoost;
mMaxContentBoost = maxContentBoost;
mTargetDispPeakBrightness = targetDispPeakBrightness;
}
/*
* Helper function copies the JPEG image from without EXIF.
*
* @param pDest destination of the data to be written.
* @param pSource source of data being written.
* @param exif_pos position of the EXIF package, which is aligned with jpegdecoder.getEXIFPos().
* (4 bytes offset to FF sign, the byte after FF E1 XX XX <this byte>).
* @param exif_size exif size without the initial 4 bytes, aligned with jpegdecoder.getEXIFSize().
*/
static void copyJpegWithoutExif(uhdr_compressed_image_t* pDest, uhdr_compressed_image_t* pSource,
size_t exif_pos, size_t exif_size) {
const size_t exif_offset = 4; // exif_pos has 4 bytes offset to the FF sign
pDest->data_sz = pSource->data_sz - exif_size - exif_offset;
pDest->data = new uint8_t[pDest->data_sz];
pDest->capacity = pDest->data_sz;
pDest->cg = pSource->cg;
pDest->ct = pSource->ct;
pDest->range = pSource->range;
memcpy(pDest->data, pSource->data, exif_pos - exif_offset);
memcpy((uint8_t*)pDest->data + exif_pos - exif_offset,
(uint8_t*)pSource->data + exif_pos + exif_size, pSource->data_sz - exif_pos - exif_size);
}
/* Encode API-0 */
uhdr_error_info_t JpegR::encodeJPEGR(uhdr_raw_image_t* hdr_intent, uhdr_compressed_image_t* dest,
int quality, uhdr_mem_block_t* exif) {
uhdr_img_fmt_t sdr_intent_fmt;
if (hdr_intent->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
sdr_intent_fmt = UHDR_IMG_FMT_12bppYCbCr420;
} else if (hdr_intent->fmt == UHDR_IMG_FMT_30bppYCbCr444) {
sdr_intent_fmt = UHDR_IMG_FMT_24bppYCbCr444;
} else if (hdr_intent->fmt == UHDR_IMG_FMT_32bppRGBA1010102 ||
hdr_intent->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) {
sdr_intent_fmt = UHDR_IMG_FMT_32bppRGBA8888;
} else {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "unsupported hdr intent color format %d",
hdr_intent->fmt);
return status;
}
std::unique_ptr<uhdr_raw_image_ext_t> sdr_intent = std::make_unique<uhdr_raw_image_ext_t>(
sdr_intent_fmt, UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED, UHDR_CR_UNSPECIFIED, hdr_intent->w,
hdr_intent->h, 64);
// tone map
UHDR_ERR_CHECK(toneMap(hdr_intent, sdr_intent.get()));
// If hdr intent is tonemapped internally, it is observed from quality pov,
// generateGainMapOnePass() is sufficient
mEncPreset = UHDR_USAGE_REALTIME; // overriding the config option
// generate gain map
uhdr_gainmap_metadata_ext_t metadata(kJpegrVersion);
std::unique_ptr<uhdr_raw_image_ext_t> gainmap;
UHDR_ERR_CHECK(generateGainMap(sdr_intent.get(), hdr_intent, &metadata, gainmap,
/* sdr_is_601 */ false,
/* use_luminance */ false));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
UHDR_ERR_CHECK(compressGainMap(gainmap.get(), &jpeg_enc_obj_gm));
uhdr_compressed_image_t gainmap_compressed = jpeg_enc_obj_gm.getCompressedImage();
std::shared_ptr<DataStruct> icc = IccHelper::writeIccProfile(UHDR_CT_SRGB, sdr_intent->cg);
// compress sdr image
std::unique_ptr<uhdr_raw_image_ext_t> sdr_intent_yuv_ext;
uhdr_raw_image_t* sdr_intent_yuv = sdr_intent.get();
if (isPixelFormatRgb(sdr_intent->fmt)) {
#if (defined(UHDR_ENABLE_INTRINSICS) && (defined(__ARM_NEON__) || defined(__ARM_NEON)))
sdr_intent_yuv_ext = convert_raw_input_to_ycbcr_neon(sdr_intent.get());
#else
sdr_intent_yuv_ext = convert_raw_input_to_ycbcr(sdr_intent.get());
#endif
sdr_intent_yuv = sdr_intent_yuv_ext.get();
}
JpegEncoderHelper jpeg_enc_obj_sdr;
UHDR_ERR_CHECK(
jpeg_enc_obj_sdr.compressImage(sdr_intent_yuv, quality, icc->getData(), icc->getLength()));
uhdr_compressed_image_t sdr_intent_compressed = jpeg_enc_obj_sdr.getCompressedImage();
sdr_intent_compressed.cg = sdr_intent_yuv->cg;
// append gain map, no ICC since JPEG encode already did it
UHDR_ERR_CHECK(appendGainMap(&sdr_intent_compressed, &gainmap_compressed, exif, /* icc */ nullptr,
/* icc size */ 0, &metadata, dest));
return g_no_error;
}
/* Encode API-1 */
uhdr_error_info_t JpegR::encodeJPEGR(uhdr_raw_image_t* hdr_intent, uhdr_raw_image_t* sdr_intent,
uhdr_compressed_image_t* dest, int quality,
uhdr_mem_block_t* exif) {
// generate gain map
uhdr_gainmap_metadata_ext_t metadata(kJpegrVersion);
std::unique_ptr<uhdr_raw_image_ext_t> gainmap;
UHDR_ERR_CHECK(generateGainMap(sdr_intent, hdr_intent, &metadata, gainmap));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
UHDR_ERR_CHECK(compressGainMap(gainmap.get(), &jpeg_enc_obj_gm));
uhdr_compressed_image_t gainmap_compressed = jpeg_enc_obj_gm.getCompressedImage();
std::shared_ptr<DataStruct> icc = IccHelper::writeIccProfile(UHDR_CT_SRGB, sdr_intent->cg);
std::unique_ptr<uhdr_raw_image_ext_t> sdr_intent_yuv_ext;
uhdr_raw_image_t* sdr_intent_yuv = sdr_intent;
if (isPixelFormatRgb(sdr_intent->fmt)) {
#if (defined(UHDR_ENABLE_INTRINSICS) && (defined(__ARM_NEON__) || defined(__ARM_NEON)))
sdr_intent_yuv_ext = convert_raw_input_to_ycbcr_neon(sdr_intent);
#else
sdr_intent_yuv_ext = convert_raw_input_to_ycbcr(sdr_intent);
#endif
sdr_intent_yuv = sdr_intent_yuv_ext.get();
}
// convert to bt601 YUV encoding for JPEG encode
#if (defined(UHDR_ENABLE_INTRINSICS) && (defined(__ARM_NEON__) || defined(__ARM_NEON)))
UHDR_ERR_CHECK(convertYuv_neon(sdr_intent_yuv, sdr_intent_yuv->cg, UHDR_CG_DISPLAY_P3));
#else
UHDR_ERR_CHECK(convertYuv(sdr_intent_yuv, sdr_intent_yuv->cg, UHDR_CG_DISPLAY_P3));
#endif
// compress sdr image
JpegEncoderHelper jpeg_enc_obj_sdr;
UHDR_ERR_CHECK(
jpeg_enc_obj_sdr.compressImage(sdr_intent_yuv, quality, icc->getData(), icc->getLength()));
uhdr_compressed_image_t sdr_intent_compressed = jpeg_enc_obj_sdr.getCompressedImage();
sdr_intent_compressed.cg = sdr_intent_yuv->cg;
// append gain map, no ICC since JPEG encode already did it
UHDR_ERR_CHECK(appendGainMap(&sdr_intent_compressed, &gainmap_compressed, exif, /* icc */ nullptr,
/* icc size */ 0, &metadata, dest));
return g_no_error;
}
/* Encode API-2 */
uhdr_error_info_t JpegR::encodeJPEGR(uhdr_raw_image_t* hdr_intent, uhdr_raw_image_t* sdr_intent,
uhdr_compressed_image_t* sdr_intent_compressed,
uhdr_compressed_image_t* dest) {
JpegDecoderHelper jpeg_dec_obj_sdr;
UHDR_ERR_CHECK(jpeg_dec_obj_sdr.decompressImage(sdr_intent_compressed->data,
sdr_intent_compressed->data_sz, PARSE_STREAM));
if (hdr_intent->w != jpeg_dec_obj_sdr.getDecompressedImageWidth() ||
hdr_intent->h != jpeg_dec_obj_sdr.getDecompressedImageHeight()) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(
status.detail, sizeof status.detail,
"sdr intent resolution %dx%d and compressed image sdr intent resolution %dx%d do not match",
sdr_intent->w, sdr_intent->h, (int)jpeg_dec_obj_sdr.getDecompressedImageWidth(),
(int)jpeg_dec_obj_sdr.getDecompressedImageHeight());
return status;
}
// generate gain map
uhdr_gainmap_metadata_ext_t metadata(kJpegrVersion);
std::unique_ptr<uhdr_raw_image_ext_t> gainmap;
UHDR_ERR_CHECK(generateGainMap(sdr_intent, hdr_intent, &metadata, gainmap));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
UHDR_ERR_CHECK(compressGainMap(gainmap.get(), &jpeg_enc_obj_gm));
uhdr_compressed_image_t gainmap_compressed = jpeg_enc_obj_gm.getCompressedImage();
return encodeJPEGR(sdr_intent_compressed, &gainmap_compressed, &metadata, dest);
}
/* Encode API-3 */
uhdr_error_info_t JpegR::encodeJPEGR(uhdr_raw_image_t* hdr_intent,
uhdr_compressed_image_t* sdr_intent_compressed,
uhdr_compressed_image_t* dest) {
// decode input jpeg, gamut is going to be bt601.
JpegDecoderHelper jpeg_dec_obj_sdr;
UHDR_ERR_CHECK(jpeg_dec_obj_sdr.decompressImage(sdr_intent_compressed->data,
sdr_intent_compressed->data_sz));
uhdr_raw_image_t sdr_intent = jpeg_dec_obj_sdr.getDecompressedImage();
if (jpeg_dec_obj_sdr.getICCSize() > 0) {
uhdr_color_gamut_t cg =
IccHelper::readIccColorGamut(jpeg_dec_obj_sdr.getICCPtr(), jpeg_dec_obj_sdr.getICCSize());
if (cg == UHDR_CG_UNSPECIFIED ||
(sdr_intent_compressed->cg != UHDR_CG_UNSPECIFIED && sdr_intent_compressed->cg != cg)) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"configured color gamut %d does not match with color gamut specified in icc box %d",
sdr_intent_compressed->cg, cg);
return status;
}
sdr_intent.cg = cg;
} else {
if (sdr_intent_compressed->cg <= UHDR_CG_UNSPECIFIED ||
sdr_intent_compressed->cg > UHDR_CG_BT_2100) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized 420 color gamut %d",
sdr_intent_compressed->cg);
return status;
}
sdr_intent.cg = sdr_intent_compressed->cg;
}
if (hdr_intent->w != sdr_intent.w || hdr_intent->h != sdr_intent.h) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"sdr intent resolution %dx%d and hdr intent resolution %dx%d do not match",
sdr_intent.w, sdr_intent.h, hdr_intent->w, hdr_intent->h);
return status;
}
// generate gain map
uhdr_gainmap_metadata_ext_t metadata(kJpegrVersion);
std::unique_ptr<uhdr_raw_image_ext_t> gainmap;
UHDR_ERR_CHECK(
generateGainMap(&sdr_intent, hdr_intent, &metadata, gainmap, true /* sdr_is_601 */));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
UHDR_ERR_CHECK(compressGainMap(gainmap.get(), &jpeg_enc_obj_gm));
uhdr_compressed_image_t gainmap_compressed = jpeg_enc_obj_gm.getCompressedImage();
return encodeJPEGR(sdr_intent_compressed, &gainmap_compressed, &metadata, dest);
}
/* Encode API-4 */
uhdr_error_info_t JpegR::encodeJPEGR(uhdr_compressed_image_t* base_img_compressed,
uhdr_compressed_image_t* gainmap_img_compressed,
uhdr_gainmap_metadata_ext_t* metadata,
uhdr_compressed_image_t* dest) {
// We just want to check if ICC is present, so don't do a full decode. Note,
// this doesn't verify that the ICC is valid.
JpegDecoderHelper decoder;
UHDR_ERR_CHECK(decoder.parseImage(base_img_compressed->data, base_img_compressed->data_sz));
if (!metadata->use_base_cg) {
JpegDecoderHelper gainmap_decoder;
UHDR_ERR_CHECK(
gainmap_decoder.parseImage(gainmap_img_compressed->data, gainmap_img_compressed->data_sz));
if (!(gainmap_decoder.getICCSize() > 0)) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"For gainmap application space to be alternate image space, gainmap image is "
"expected to contain alternate image color space in the form of ICC. The ICC marker "
"in gainmap jpeg is missing.");
return status;
}
}
// Add ICC if not already present.
if (decoder.getICCSize() > 0) {
UHDR_ERR_CHECK(appendGainMap(base_img_compressed, gainmap_img_compressed, /* exif */ nullptr,
/* icc */ nullptr, /* icc size */ 0, metadata, dest));
} else {
if (base_img_compressed->cg <= UHDR_CG_UNSPECIFIED ||
base_img_compressed->cg > UHDR_CG_BT_2100) {
uhdr_error_info_t status;
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized 420 color gamut %d",
base_img_compressed->cg);
return status;
}
std::shared_ptr<DataStruct> newIcc =
IccHelper::writeIccProfile(UHDR_CT_SRGB, base_img_compressed->cg);
UHDR_ERR_CHECK(appendGainMap(base_img_compressed, gainmap_img_compressed, /* exif */ nullptr,
newIcc->getData(), newIcc->getLength(), metadata, dest));
}
return g_no_error;
}
uhdr_error_info_t JpegR::convertYuv(uhdr_raw_image_t* image, uhdr_color_gamut_t src_encoding,
uhdr_color_gamut_t dst_encoding) {
const std::array<float, 9>* coeffs_ptr = nullptr;
uhdr_error_info_t status = g_no_error;
switch (src_encoding) {
case UHDR_CG_BT_709:
switch (dst_encoding) {
case UHDR_CG_BT_709:
return status;
case UHDR_CG_DISPLAY_P3:
coeffs_ptr = &kYuvBt709ToBt601;
break;
case UHDR_CG_BT_2100:
coeffs_ptr = &kYuvBt709ToBt2100;
break;
default:
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized dest color gamut %d",
dst_encoding);
return status;
}
break;
case UHDR_CG_DISPLAY_P3:
switch (dst_encoding) {
case UHDR_CG_BT_709:
coeffs_ptr = &kYuvBt601ToBt709;
break;
case UHDR_CG_DISPLAY_P3:
return status;
case UHDR_CG_BT_2100:
coeffs_ptr = &kYuvBt601ToBt2100;
break;
default:
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized dest color gamut %d",
dst_encoding);
return status;
}
break;
case UHDR_CG_BT_2100:
switch (dst_encoding) {
case UHDR_CG_BT_709:
coeffs_ptr = &kYuvBt2100ToBt709;
break;
case UHDR_CG_DISPLAY_P3:
coeffs_ptr = &kYuvBt2100ToBt601;
break;
case UHDR_CG_BT_2100:
return status;
default:
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized dest color gamut %d",
dst_encoding);
return status;
}
break;
default:
status.error_code = UHDR_CODEC_INVALID_PARAM;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail, "Unrecognized src color gamut %d",
src_encoding);
return status;
}
if (image->fmt == UHDR_IMG_FMT_12bppYCbCr420) {
transformYuv420(image, *coeffs_ptr);
} else if (image->fmt == UHDR_IMG_FMT_24bppYCbCr444) {
transformYuv444(image, *coeffs_ptr);
} else {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for performing gamut conversion for color format %d",
image->fmt);
return status;
}
return status;
}
uhdr_error_info_t JpegR::compressGainMap(uhdr_raw_image_t* gainmap_img,
JpegEncoderHelper* jpeg_enc_obj) {
if (!kWriteXmpMetadata) {
std::shared_ptr<DataStruct> icc = IccHelper::writeIccProfile(gainmap_img->ct, gainmap_img->cg);
return jpeg_enc_obj->compressImage(gainmap_img, mMapCompressQuality, icc->getData(),
icc->getLength());
}
return jpeg_enc_obj->compressImage(gainmap_img, mMapCompressQuality, nullptr, 0);
}
uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_image_t* hdr_intent,
uhdr_gainmap_metadata_ext_t* gainmap_metadata,
std::unique_ptr<uhdr_raw_image_ext_t>& gainmap_img,
bool sdr_is_601, bool use_luminance) {
uhdr_error_info_t status = g_no_error;
if (sdr_intent->fmt != UHDR_IMG_FMT_24bppYCbCr444 &&
sdr_intent->fmt != UHDR_IMG_FMT_16bppYCbCr422 &&
sdr_intent->fmt != UHDR_IMG_FMT_12bppYCbCr420 &&
sdr_intent->fmt != UHDR_IMG_FMT_32bppRGBA8888) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"generate gainmap method expects sdr intent color format to be one of "
"{UHDR_IMG_FMT_24bppYCbCr444, UHDR_IMG_FMT_16bppYCbCr422, "
"UHDR_IMG_FMT_12bppYCbCr420, UHDR_IMG_FMT_32bppRGBA8888}. Received %d",
sdr_intent->fmt);
return status;
}
if (hdr_intent->fmt != UHDR_IMG_FMT_24bppYCbCrP010 &&
hdr_intent->fmt != UHDR_IMG_FMT_30bppYCbCr444 &&
hdr_intent->fmt != UHDR_IMG_FMT_32bppRGBA1010102 &&
hdr_intent->fmt != UHDR_IMG_FMT_64bppRGBAHalfFloat) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"generate gainmap method expects hdr intent color format to be one of "
"{UHDR_IMG_FMT_24bppYCbCrP010, UHDR_IMG_FMT_30bppYCbCr444, "
"UHDR_IMG_FMT_32bppRGBA1010102, UHDR_IMG_FMT_64bppRGBAHalfFloat}. Received %d",
hdr_intent->fmt);
return status;
}
ColorTransformFn hdrInvOetf = getInverseOetfFn(hdr_intent->ct);
if (hdrInvOetf == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for converting transfer characteristics %d to linear",
hdr_intent->ct);
return status;
}
LuminanceFn hdrLuminanceFn = getLuminanceFn(hdr_intent->cg);
if (hdrLuminanceFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for calculating luminance for color gamut %d",
hdr_intent->cg);
return status;
}
SceneToDisplayLuminanceFn hdrOotfFn = getOotfFn(hdr_intent->ct);
if (hdrOotfFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for calculating Ootf for color transfer %d",
hdr_intent->ct);
return status;
}
float hdr_white_nits = getReferenceDisplayPeakLuminanceInNits(hdr_intent->ct);
if (hdr_white_nits == -1.0f) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"received invalid peak brightness %f nits for hdr reference display with color "
"transfer %d ",
hdr_white_nits, hdr_intent->ct);
return status;
}
ColorTransformFn hdrGamutConversionFn;
ColorTransformFn sdrGamutConversionFn;
bool use_sdr_cg = true;
if (sdr_intent->cg != hdr_intent->cg) {
use_sdr_cg = kWriteXmpMetadata ||
!(hdr_intent->cg == UHDR_CG_BT_2100 ||
(hdr_intent->cg == UHDR_CG_DISPLAY_P3 && sdr_intent->cg != UHDR_CG_BT_2100));
if (use_sdr_cg) {
hdrGamutConversionFn = getGamutConversionFn(sdr_intent->cg, hdr_intent->cg);
if (hdrGamutConversionFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for gamut conversion from %d to %d", hdr_intent->cg,
sdr_intent->cg);
return status;
}
sdrGamutConversionFn = identityConversion;
} else {
hdrGamutConversionFn = identityConversion;
sdrGamutConversionFn = getGamutConversionFn(hdr_intent->cg, sdr_intent->cg);
if (sdrGamutConversionFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for gamut conversion from %d to %d", sdr_intent->cg,
hdr_intent->cg);
return status;
}
}
} else {
hdrGamutConversionFn = sdrGamutConversionFn = identityConversion;
}
gainmap_metadata->use_base_cg = use_sdr_cg;
ColorTransformFn sdrYuvToRgbFn = getYuvToRgbFn(sdr_intent->cg);
if (sdrYuvToRgbFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for converting yuv to rgb for color gamut %d",
sdr_intent->cg);
return status;
}
ColorTransformFn hdrYuvToRgbFn = getYuvToRgbFn(hdr_intent->cg);
if (hdrYuvToRgbFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for converting yuv to rgb for color gamut %d",
hdr_intent->cg);
return status;
}
LuminanceFn luminanceFn = getLuminanceFn(sdr_intent->cg);
if (luminanceFn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for computing luminance for color gamut %d",
sdr_intent->cg);
return status;
}
SamplePixelFn sdr_sample_pixel_fn = getSamplePixelFn(sdr_intent->fmt);
if (sdr_sample_pixel_fn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for reading pixels for color format %d", sdr_intent->fmt);
return status;
}
SamplePixelFn hdr_sample_pixel_fn = getSamplePixelFn(hdr_intent->fmt);
if (hdr_sample_pixel_fn == nullptr) {
status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
status.has_detail = 1;
snprintf(status.detail, sizeof status.detail,
"No implementation available for reading pixels for color format %d", hdr_intent->fmt);
return status;
}
if (sdr_is_601) {
sdrYuvToRgbFn = p3YuvToRgb;
}
unsigned int image_width = sdr_intent->w;
unsigned int image_height = sdr_intent->h;
unsigned int map_width = image_width / mMapDimensionScaleFactor;
unsigned int map_height = image_height / mMapDimensionScaleFactor;
if (map_width == 0 || map_height == 0) {
int scaleFactor = (std::min)(image_width, image_height);
scaleFactor = (scaleFactor >= DCTSIZE) ? (scaleFactor / DCTSIZE) : 1;
ALOGW(
"configured gainmap scale factor is resulting in gainmap width and/or height to be zero, "
"image width %u, image height %u, scale factor %d. Modifying gainmap scale factor to %d ",
image_width, image_height, mMapDimensionScaleFactor, scaleFactor);
setMapDimensionScaleFactor(scaleFactor);
map_width = image_width / mMapDimensionScaleFactor;
map_height = image_height / mMapDimensionScaleFactor;
}
// NOTE: Even though gainmap image raw descriptor is being initialized with hdr intent's color
// aspects, one should not associate gainmap image to this color profile. gain map image gamut
// space can be hdr intent's or sdr intent's space (a decision made during gainmap generation).
// Its color transfer is dependent on the gainmap encoding gamma. The reason to initialize with
// hdr color aspects is compressGainMap method will use this to write hdr intent color profile in
// the bitstream.
gainmap_img = std::make_unique<uhdr_raw_image_ext_t>(
mUseMultiChannelGainMap ? UHDR_IMG_FMT_24bppRGB888 : UHDR_IMG_FMT_8bppYCbCr400,
hdr_intent->cg, hdr_intent->ct, hdr_intent->range, map_width, map_height, 64);
uhdr_raw_image_ext_t* dest = gainmap_img.get();
auto generateGainMapOnePass = [this, sdr_intent, hdr_intent, gainmap_metadata, dest, map_height,
hdrInvOetf, hdrLuminanceFn, hdrOotfFn, hdrGamutConversionFn,
sdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn, hdrYuvToRgbFn,
sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits,
use_luminance]() -> void {
std::fill_n(gainmap_metadata->max_content_boost, 3, hdr_white_nits / kSdrWhiteNits);
std::fill_n(gainmap_metadata->min_content_boost, 3, 1.0f);
std::fill_n(gainmap_metadata->gamma, 3, mGamma);
std::fill_n(gainmap_metadata->offset_sdr, 3, 0.0f);
std::fill_n(gainmap_metadata->offset_hdr, 3, 0.0f);
gainmap_metadata->hdr_capacity_min = 1.0f;
if (this->mTargetDispPeakBrightness != -1.0f) {
gainmap_metadata->hdr_capacity_max = this->mTargetDispPeakBrightness / kSdrWhiteNits;
} else {
gainmap_metadata->hdr_capacity_max = gainmap_metadata->max_content_boost[0];
}
float log2MinBoost = log2(gainmap_metadata->min_content_boost[0]);
float log2MaxBoost = log2(gainmap_metadata->max_content_boost[0]);
const int threads = (std::min)(GetCPUCoreCount(), 4u);
const int jobSizeInRows = 1;
unsigned int rowStep = threads == 1 ? map_height : jobSizeInRows;
JobQueue jobQueue;
std::function<void()> generateMap =
[this, sdr_intent, hdr_intent, gainmap_metadata, dest, hdrInvOetf, hdrLuminanceFn,
hdrOotfFn, hdrGamutConversionFn, sdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn,
hdrYuvToRgbFn, sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits, log2MinBoost,
log2MaxBoost, use_luminance, &jobQueue]() -> void {
unsigned int rowStart, rowEnd;
const bool isHdrIntentRgb = isPixelFormatRgb(hdr_intent->fmt);
const bool isSdrIntentRgb = isPixelFormatRgb(sdr_intent->fmt);
const float hdrSampleToNitsFactor =
hdr_intent->ct == UHDR_CT_LINEAR ? kSdrWhiteNits : hdr_white_nits;
while (jobQueue.dequeueJob(rowStart, rowEnd)) {
for (size_t y = rowStart; y < rowEnd; ++y) {
for (size_t x = 0; x < dest->w; ++x) {
Color sdr_rgb_gamma;
if (isSdrIntentRgb) {
sdr_rgb_gamma = sdr_sample_pixel_fn(sdr_intent, mMapDimensionScaleFactor, x, y);
} else {
Color sdr_yuv_gamma = sdr_sample_pixel_fn(sdr_intent, mMapDimensionScaleFactor, x, y);
sdr_rgb_gamma = sdrYuvToRgbFn(sdr_yuv_gamma);
}
// We are assuming the SDR input is always sRGB transfer.
#if USE_SRGB_INVOETF_LUT
Color sdr_rgb = srgbInvOetfLUT(sdr_rgb_gamma);
#else
Color sdr_rgb = srgbInvOetf(sdr_rgb_gamma);
#endif
sdr_rgb = sdrGamutConversionFn(sdr_rgb);
sdr_rgb = clipNegatives(sdr_rgb);
Color hdr_rgb_gamma;
if (isHdrIntentRgb) {
hdr_rgb_gamma = hdr_sample_pixel_fn(hdr_intent, mMapDimensionScaleFactor, x, y);
} else {
Color hdr_yuv_gamma = hdr_sample_pixel_fn(hdr_intent, mMapDimensionScaleFactor, x, y);
hdr_rgb_gamma = hdrYuvToRgbFn(hdr_yuv_gamma);
}
Color hdr_rgb = hdrInvOetf(hdr_rgb_gamma);
hdr_rgb = hdrOotfFn(hdr_rgb, hdrLuminanceFn);
hdr_rgb = hdrGamutConversionFn(hdr_rgb);
hdr_rgb = clipNegatives(hdr_rgb);
if (mUseMultiChannelGainMap) {
Color sdr_rgb_nits = sdr_rgb * kSdrWhiteNits;
Color hdr_rgb_nits = hdr_rgb * hdrSampleToNitsFactor;
size_t pixel_idx = (x + y * dest->stride[UHDR_PLANE_PACKED]) * 3;
reinterpret_cast<uint8_t*>(dest->planes[UHDR_PLANE_PACKED])[pixel_idx] = encodeGain(
sdr_rgb_nits.r, hdr_rgb_nits.r, gainmap_metadata, log2MinBoost, log2MaxBoost, 0);
reinterpret_cast<uint8_t*>(dest->planes[UHDR_PLANE_PACKED])[pixel_idx + 1] =
encodeGain(sdr_rgb_nits.g, hdr_rgb_nits.g, gainmap_metadata, log2MinBoost,
log2MaxBoost, 1);
reinterpret_cast<uint8_t*>(dest->planes[UHDR_PLANE_PACKED])[pixel_idx + 2] =
encodeGain(sdr_rgb_nits.b, hdr_rgb_nits.b, gainmap_metadata, log2MinBoost,
log2MaxBoost, 2);
} else {
float sdr_y_nits;
float hdr_y_nits;
if (use_luminance) {
sdr_y_nits = luminanceFn(sdr_rgb) * kSdrWhiteNits;
hdr_y_nits = luminanceFn(hdr_rgb) * hdrSampleToNitsFactor;
} else {
sdr_y_nits = fmax(sdr_rgb.r, fmax(sdr_rgb.g, sdr_rgb.b)) * kSdrWhiteNits;
hdr_y_nits = fmax(hdr_rgb.r, fmax(hdr_rgb.g, hdr_rgb.b)) * hdrSampleToNitsFactor;
}
size_t pixel_idx = x + y * dest->stride[UHDR_PLANE_Y];
reinterpret_cast<uint8_t*>(dest->planes[UHDR_PLANE_Y])[pixel_idx] = encodeGain(
sdr_y_nits, hdr_y_nits, gainmap_metadata, log2MinBoost, log2MaxBoost, 0);
}
}
}
}
};
// generate map
std::vector<std::thread> workers;
for (int th = 0; th < threads - 1; th++) {
workers.push_back(std::thread(generateMap));
}
for (unsigned int rowStart = 0; rowStart < map_height;) {
unsigned int rowEnd = (std::min)(rowStart + rowStep, map_height);
jobQueue.enqueueJob(rowStart, rowEnd);
rowStart = rowEnd;
}
jobQueue.markQueueForEnd();
generateMap();
std::for_each(workers.begin(), workers.end(), [](std::thread& t) { t.join(); });
};
auto generateGainMapTwoPass = [this, sdr_intent, hdr_intent, gainmap_metadata, dest, map_width,
map_height, hdrInvOetf, hdrLuminanceFn, hdrOotfFn,
hdrGamutConversionFn, sdrGamutConversionFn, luminanceFn,
sdrYuvToRgbFn, hdrYuvToRgbFn, sdr_sample_pixel_fn,
hdr_sample_pixel_fn, hdr_white_nits, use_luminance]() -> void {
uhdr_memory_block_t gainmap_mem((size_t)map_width * map_height * sizeof(float) *
(mUseMultiChannelGainMap ? 3 : 1));
float* gainmap_data = reinterpret_cast<float*>(gainmap_mem.m_buffer.get());
float gainmap_min[3] = {127.0f, 127.0f, 127.0f};
float gainmap_max[3] = {-128.0f, -128.0f, -128.0f};
std::mutex gainmap_minmax;
const int threads = (std::min)(GetCPUCoreCount(), 4u);
const int jobSizeInRows = 1;
unsigned int rowStep = threads == 1 ? map_height : jobSizeInRows;
JobQueue jobQueue;
std::function<void()> generateMap =
[this, sdr_intent, hdr_intent, gainmap_data, map_width, hdrInvOetf, hdrLuminanceFn,
hdrOotfFn, hdrGamutConversionFn, sdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn,
hdrYuvToRgbFn, sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits, use_luminance,
&gainmap_min, &gainmap_max, &gainmap_minmax, &jobQueue]() -> void {
unsigned int rowStart, rowEnd;
const bool isHdrIntentRgb = isPixelFormatRgb(hdr_intent->fmt);
const bool isSdrIntentRgb = isPixelFormatRgb(sdr_intent->fmt);
const float hdrSampleToNitsFactor =
hdr_intent->ct == UHDR_CT_LINEAR ? kSdrWhiteNits : hdr_white_nits;
float gainmap_min_th[3] = {127.0f, 127.0f, 127.0f};
float gainmap_max_th[3] = {-128.0f, -128.0f, -128.0f};
while (jobQueue.dequeueJob(rowStart, rowEnd)) {
for (size_t y = rowStart; y < rowEnd; ++y) {
for (size_t x = 0; x < map_width; ++x) {
Color sdr_rgb_gamma;
if (isSdrIntentRgb) {
sdr_rgb_gamma = sdr_sample_pixel_fn(sdr_intent, mMapDimensionScaleFactor, x, y);
} else {
Color sdr_yuv_gamma = sdr_sample_pixel_fn(sdr_intent, mMapDimensionScaleFactor, x, y);
sdr_rgb_gamma = sdrYuvToRgbFn(sdr_yuv_gamma);
}
// We are assuming the SDR input is always sRGB transfer.
#if USE_SRGB_INVOETF_LUT
Color sdr_rgb = srgbInvOetfLUT(sdr_rgb_gamma);
#else
Color sdr_rgb = srgbInvOetf(sdr_rgb_gamma);
#endif
sdr_rgb = sdrGamutConversionFn(sdr_rgb);
sdr_rgb = clipNegatives(sdr_rgb);
Color hdr_rgb_gamma;
if (isHdrIntentRgb) {
hdr_rgb_gamma = hdr_sample_pixel_fn(hdr_intent, mMapDimensionScaleFactor, x, y);
} else {
Color hdr_yuv_gamma = hdr_sample_pixel_fn(hdr_intent, mMapDimensionScaleFactor, x, y);
hdr_rgb_gamma = hdrYuvToRgbFn(hdr_yuv_gamma);
}
Color hdr_rgb = hdrInvOetf(hdr_rgb_gamma);
hdr_rgb = hdrOotfFn(hdr_rgb, hdrLuminanceFn);
hdr_rgb = hdrGamutConversionFn(hdr_rgb);
hdr_rgb = clipNegatives(hdr_rgb);
if (mUseMultiChannelGainMap) {
Color sdr_rgb_nits = sdr_rgb * kSdrWhiteNits;
Color hdr_rgb_nits = hdr_rgb * hdrSampleToNitsFactor;
size_t pixel_idx = (x + y * map_width) * 3;
gainmap_data[pixel_idx] = computeGain(sdr_rgb_nits.r, hdr_rgb_nits.r);
gainmap_data[pixel_idx + 1] = computeGain(sdr_rgb_nits.g, hdr_rgb_nits.g);
gainmap_data[pixel_idx + 2] = computeGain(sdr_rgb_nits.b, hdr_rgb_nits.b);
for (int i = 0; i < 3; i++) {
gainmap_min_th[i] = (std::min)(gainmap_data[pixel_idx + i], gainmap_min_th[i]);
gainmap_max_th[i] = (std::max)(gainmap_data[pixel_idx + i], gainmap_max_th[i]);
}
} else {
float sdr_y_nits;
float hdr_y_nits;
if (use_luminance) {
sdr_y_nits = luminanceFn(sdr_rgb) * kSdrWhiteNits;
hdr_y_nits = luminanceFn(hdr_rgb) * hdrSampleToNitsFactor;
} else {
sdr_y_nits = fmax(sdr_rgb.r, fmax(sdr_rgb.g, sdr_rgb.b)) * kSdrWhiteNits;
hdr_y_nits = fmax(hdr_rgb.r, fmax(hdr_rgb.g, hdr_rgb.b)) * hdrSampleToNitsFactor;
}
size_t pixel_idx = x + y * map_width;
gainmap_data[pixel_idx] = computeGain(sdr_y_nits, hdr_y_nits);
gainmap_min_th[0] = (std::min)(gainmap_data[pixel_idx], gainmap_min_th[0]);
gainmap_max_th[0] = (std::max)(gainmap_data[pixel_idx], gainmap_max_th[0]);
}
}
}
}
{
std::unique_lock<std::mutex> lock{gainmap_minmax};
for (int index = 0; index < (mUseMultiChannelGainMap ? 3 : 1); index++) {
gainmap_min[index] = (std::min)(gainmap_min[index], gainmap_min_th[index]);
gainmap_max[index] = (std::max)(gainmap_max[index], gainmap_max_th[index]);
}
}
};
// generate map
std::vector<std::thread> workers;
for (int th = 0; th < threads - 1; th++) {
workers.push_back(std::thread(generateMap));
}
for (unsigned int rowStart = 0; rowStart < map_height;) {
unsigned int rowEnd = (std::min)(rowStart + rowStep, map_height);
jobQueue.enqueueJob(rowStart, rowEnd);
rowStart = rowEnd;
}
jobQueue.markQueueForEnd();
generateMap();
std::for_each(workers.begin(), workers.end(), [](std::thread& t) { t.join(); });
// xmp metadata current implementation does not support writing multichannel metadata
// so merge them in to one
if (kWriteXmpMetadata) {
float min_content_boost_log2 = gainmap_min[0];
float max_content_boost_log2 = gainmap_max[0];
for (int index = 1; index < (mUseMultiChannelGainMap ? 3 : 1); index++) {
min_content_boost_log2 = (std::min)(gainmap_min[index], min_content_boost_log2);
max_content_boost_log2 = (std::max)(gainmap_max[index], max_content_boost_log2);
}
std::fill_n(gainmap_min, 3, min_content_boost_log2);
std::fill_n(gainmap_max, 3, max_content_boost_log2);
}
for (int index = 0; index < (mUseMultiChannelGainMap ? 3 : 1); index++) {
// gain coefficient range [-14.3, 15.6] is capable of representing hdr pels from sdr pels.
// Allowing further excursion might not offer any benefit and on the downside can cause bigger
// error during affine map and inverse affine map.
gainmap_min[index] = (std::clamp)(gainmap_min[index], -14.3f, 15.6f);
gainmap_max[index] = (std::clamp)(gainmap_max[index], -14.3f, 15.6f);
if (this->mMaxContentBoost != FLT_MAX) {
float suggestion = log2(this->mMaxContentBoost);
gainmap_max[index] = (std::min)(gainmap_max[index], suggestion);
}
if (this->mMinContentBoost != FLT_MIN) {
float suggestion = log2(this->mMinContentBoost);
gainmap_min[index] = (std::max)(gainmap_min[index], suggestion);
}
if (fabs(gainmap_max[index] - gainmap_min[index]) < FLT_EPSILON) {
gainmap_max[index] += 0.1f; // to avoid div by zero during affine transform
}
}
std::function<void()> encodeMap = [this, gainmap_data, map_width, dest, gainmap_min,
gainmap_max, &jobQueue]() -> void {
unsigned int rowStart, rowEnd;
while (jobQueue.dequeueJob(rowStart, rowEnd)) {
if (mUseMultiChannelGainMap) {
for (size_t j = rowStart; j < rowEnd; j++) {
size_t dst_pixel_idx = j * dest->stride[UHDR_PLANE_PACKED] * 3;
size_t src_pixel_idx = j * map_width * 3;
for (size_t i = 0; i < map_width * 3; i++) {
reinterpret_cast<uint8_t*>(dest->planes[UHDR_PLANE_PACKED])[dst_pixel_idx + i] =
affineMapGain(gainmap_data[src_pixel_idx + i], gainmap_min[i % 3],
gainmap_max[i % 3], this->mGamma);
}