-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathjpegr.cpp
1883 lines (1704 loc) · 76.3 KB
/
jpegr.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 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/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 {
#define USE_SRGB_INVOETF_LUT 1
#define USE_HLG_OETF_LUT 1
#define USE_PQ_OETF_LUT 1
#define USE_HLG_INVOETF_LUT 1
#define USE_PQ_INVOETF_LUT 1
#define USE_APPLY_GAIN_LUT 1
// JPEG compress quality (0 ~ 100) for gain map
static const int kMapCompressQuality = 85;
// Gain map metadata
static const bool kWriteXmpMetadata = true;
static const bool kWriteIso21496_1Metadata = false;
// Gain map calculation
static const bool kUseMultiChannelGainMap = false;
int GetCPUCoreCount() {
int cpuCoreCount = 1;
#if defined(_WIN32)
SYSTEM_INFO system_info;
ZeroMemory(&system_info, sizeof(system_info));
GetSystemInfo(&system_info);
cpuCoreCount = (size_t)system_info.dwNumberOfProcessors;
#elif defined(_SC_NPROCESSORS_ONLN)
cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(_SC_NPROCESSORS_CONF)
cpuCoreCount = sysconf(_SC_NPROCESSORS_CONF);
#else
#error platform-specific implementation for GetCPUCoreCount() missing.
#endif
if (cpuCoreCount <= 0) cpuCoreCount = 1;
return cpuCoreCount;
}
/*
* 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());
}
};
const string kXmpNameSpace = "http://ns.adobe.com/xap/1.0/";
const string kIsoNameSpace = "urn:iso:std:iso:ts:21496:-1";
/*
* 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(jr_compressed_ptr pDest, jr_compressed_ptr 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->length = pSource->length - exif_size - exif_offset;
pDest->data = new uint8_t[pDest->length];
pDest->maxLength = pDest->length;
pDest->colorGamut = pSource->colorGamut;
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->length - exif_pos - exif_size);
}
status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
jr_uncompressed_ptr yuv420_image_ptr,
ultrahdr_transfer_function hdr_tf,
jr_compressed_ptr dest_ptr) {
if (p010_image_ptr == nullptr || p010_image_ptr->data == nullptr) {
ALOGE("Received nullptr for input p010 image");
return ERROR_JPEGR_BAD_PTR;
}
if (p010_image_ptr->width % 2 != 0 || p010_image_ptr->height % 2 != 0) {
ALOGE("Image dimensions cannot be odd, image dimensions %zux%zu", p010_image_ptr->width,
p010_image_ptr->height);
return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT;
}
if (p010_image_ptr->width < kMinWidth || p010_image_ptr->height < kMinHeight) {
ALOGE("Image dimensions cannot be less than %dx%d, image dimensions %zux%zu", kMinWidth,
kMinHeight, p010_image_ptr->width, p010_image_ptr->height);
return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT;
}
if (p010_image_ptr->width > kMaxWidth || p010_image_ptr->height > kMaxHeight) {
ALOGE("Image dimensions cannot be larger than %dx%d, image dimensions %zux%zu", kMaxWidth,
kMaxHeight, p010_image_ptr->width, p010_image_ptr->height);
return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT;
}
if (p010_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
p010_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
ALOGE("Unrecognized p010 color gamut %d", p010_image_ptr->colorGamut);
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
if (p010_image_ptr->luma_stride != 0 && p010_image_ptr->luma_stride < p010_image_ptr->width) {
ALOGE("Luma stride must not be smaller than width, stride=%zu, width=%zu",
p010_image_ptr->luma_stride, p010_image_ptr->width);
return ERROR_JPEGR_INVALID_STRIDE;
}
if (p010_image_ptr->chroma_data != nullptr &&
p010_image_ptr->chroma_stride < p010_image_ptr->width) {
ALOGE("Chroma stride must not be smaller than width, stride=%zu, width=%zu",
p010_image_ptr->chroma_stride, p010_image_ptr->width);
return ERROR_JPEGR_INVALID_STRIDE;
}
if (dest_ptr == nullptr || dest_ptr->data == nullptr) {
ALOGE("Received nullptr for destination");
return ERROR_JPEGR_BAD_PTR;
}
if (hdr_tf <= ULTRAHDR_TF_UNSPECIFIED || hdr_tf > ULTRAHDR_TF_MAX || hdr_tf == ULTRAHDR_TF_SRGB) {
ALOGE("Invalid hdr transfer function %d", hdr_tf);
return ERROR_JPEGR_INVALID_TRANS_FUNC;
}
if (yuv420_image_ptr == nullptr) {
return JPEGR_NO_ERROR;
}
if (yuv420_image_ptr->data == nullptr) {
ALOGE("Received nullptr for uncompressed 420 image");
return ERROR_JPEGR_BAD_PTR;
}
if (yuv420_image_ptr->luma_stride != 0 &&
yuv420_image_ptr->luma_stride < yuv420_image_ptr->width) {
ALOGE("Luma stride must not be smaller than width, stride=%zu, width=%zu",
yuv420_image_ptr->luma_stride, yuv420_image_ptr->width);
return ERROR_JPEGR_INVALID_STRIDE;
}
if (yuv420_image_ptr->chroma_data != nullptr &&
yuv420_image_ptr->chroma_stride < yuv420_image_ptr->width / 2) {
ALOGE("Chroma stride must not be smaller than (width / 2), stride=%zu, width=%zu",
yuv420_image_ptr->chroma_stride, yuv420_image_ptr->width);
return ERROR_JPEGR_INVALID_STRIDE;
}
if (p010_image_ptr->width != yuv420_image_ptr->width ||
p010_image_ptr->height != yuv420_image_ptr->height) {
ALOGE("Image resolutions mismatch: P010: %zux%zu, YUV420: %zux%zu", p010_image_ptr->width,
p010_image_ptr->height, yuv420_image_ptr->width, yuv420_image_ptr->height);
return ERROR_JPEGR_RESOLUTION_MISMATCH;
}
if (yuv420_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
yuv420_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
ALOGE("Unrecognized 420 color gamut %d", yuv420_image_ptr->colorGamut);
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
return JPEGR_NO_ERROR;
}
status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr,
jr_uncompressed_ptr yuv420_image_ptr,
ultrahdr_transfer_function hdr_tf,
jr_compressed_ptr dest_ptr, int quality) {
if (quality < 0 || quality > 100) {
ALOGE("quality factor is out side range [0-100], quality factor : %d", quality);
return ERROR_JPEGR_INVALID_QUALITY_FACTOR;
}
return areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest_ptr);
}
/* Encode API-0 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, ultrahdr_transfer_function hdr_tf,
jr_compressed_ptr dest, int quality, jr_exif_ptr exif) {
// validate input arguments
JPEGR_CHECK(areInputArgumentsValid(p010_image_ptr, nullptr, hdr_tf, dest, quality));
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr for exif metadata");
return ERROR_JPEGR_BAD_PTR;
}
// clean up input structure for later usage
jpegr_uncompressed_struct p010_image = *p010_image_ptr;
if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
if (!p010_image.chroma_data) {
uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
p010_image.chroma_stride = p010_image.luma_stride;
}
const size_t yu420_luma_stride = ALIGNM(p010_image.width, 16);
unique_ptr<uint8_t[]> yuv420_image_data =
make_unique<uint8_t[]>(yu420_luma_stride * p010_image.height * 3 / 2);
jpegr_uncompressed_struct yuv420_image;
yuv420_image.data = yuv420_image_data.get();
yuv420_image.width = p010_image.width;
yuv420_image.height = p010_image.height;
yuv420_image.colorGamut = p010_image.colorGamut;
yuv420_image.chroma_data = nullptr;
yuv420_image.luma_stride = yu420_luma_stride;
yuv420_image.chroma_stride = yu420_luma_stride >> 1;
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
// tone map
JPEGR_CHECK(toneMap(&p010_image, &yuv420_image, hdr_tf));
// gain map
ultrahdr_metadata_struct metadata;
metadata.version = kJpegrVersion;
jpegr_uncompressed_struct gainmap_image;
JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
jpegr_compressed_struct compressed_map;
compressed_map.data = jpeg_enc_obj_gm.getCompressedImagePtr();
compressed_map.length = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
compressed_map.maxLength = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
std::shared_ptr<DataStruct> icc =
IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420_image.colorGamut);
// convert to Bt601 YUV encoding for JPEG encode
if (yuv420_image.colorGamut != ULTRAHDR_COLORGAMUT_P3) {
JPEGR_CHECK(convertYuv(&yuv420_image, yuv420_image.colorGamut, ULTRAHDR_COLORGAMUT_P3));
}
// compress 420 image
JpegEncoderHelper jpeg_enc_obj_yuv420;
const uint8_t* planes[3]{reinterpret_cast<uint8_t*>(yuv420_image.data),
reinterpret_cast<uint8_t*>(yuv420_image.chroma_data),
reinterpret_cast<uint8_t*>(yuv420_image.chroma_data) +
yuv420_image.chroma_stride * yuv420_image.height / 2};
const size_t strides[3]{yuv420_image.luma_stride, yuv420_image.chroma_stride,
yuv420_image.chroma_stride};
if (!jpeg_enc_obj_yuv420.compressImage(planes, strides, yuv420_image.width, yuv420_image.height,
UHDR_IMG_FMT_12bppYCbCr420, quality, icc->getData(),
icc->getLength())) {
return ERROR_JPEGR_ENCODE_ERROR;
}
jpegr_compressed_struct jpeg;
jpeg.data = jpeg_enc_obj_yuv420.getCompressedImagePtr();
jpeg.length = static_cast<int>(jpeg_enc_obj_yuv420.getCompressedImageSize());
jpeg.maxLength = static_cast<int>(jpeg_enc_obj_yuv420.getCompressedImageSize());
jpeg.colorGamut = yuv420_image.colorGamut;
// append gain map, no ICC since JPEG encode already did it
JPEGR_CHECK(appendGainMap(&jpeg, &compressed_map, exif, /* icc */ nullptr, /* icc size */ 0,
&metadata, dest));
return JPEGR_NO_ERROR;
}
/* Encode API-1 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
jr_uncompressed_ptr yuv420_image_ptr, ultrahdr_transfer_function hdr_tf,
jr_compressed_ptr dest, int quality, jr_exif_ptr exif) {
// validate input arguments
if (yuv420_image_ptr == nullptr) {
ALOGE("received nullptr for uncompressed 420 image");
return ERROR_JPEGR_BAD_PTR;
}
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr for exif metadata");
return ERROR_JPEGR_BAD_PTR;
}
JPEGR_CHECK(areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest, quality))
// clean up input structure for later usage
jpegr_uncompressed_struct p010_image = *p010_image_ptr;
if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
if (!p010_image.chroma_data) {
uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
p010_image.chroma_stride = p010_image.luma_stride;
}
jpegr_uncompressed_struct yuv420_image = *yuv420_image_ptr;
if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
if (!yuv420_image.chroma_data) {
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
}
// gain map
ultrahdr_metadata_struct metadata;
metadata.version = kJpegrVersion;
jpegr_uncompressed_struct gainmap_image;
JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
jpegr_compressed_struct compressed_map;
compressed_map.data = jpeg_enc_obj_gm.getCompressedImagePtr();
compressed_map.length = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
compressed_map.maxLength = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
compressed_map.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
std::shared_ptr<DataStruct> icc =
IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420_image.colorGamut);
jpegr_uncompressed_struct yuv420_bt601_image = yuv420_image;
unique_ptr<uint8_t[]> yuv_420_bt601_data;
// Convert to bt601 YUV encoding for JPEG encode
if (yuv420_image.colorGamut != ULTRAHDR_COLORGAMUT_P3) {
const size_t yuv_420_bt601_luma_stride = ALIGNM(yuv420_image.width, 16);
yuv_420_bt601_data =
make_unique<uint8_t[]>(yuv_420_bt601_luma_stride * yuv420_image.height * 3 / 2);
yuv420_bt601_image.data = yuv_420_bt601_data.get();
yuv420_bt601_image.colorGamut = yuv420_image.colorGamut;
yuv420_bt601_image.luma_stride = yuv_420_bt601_luma_stride;
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_bt601_image.data);
yuv420_bt601_image.chroma_data = data + yuv_420_bt601_luma_stride * yuv420_image.height;
yuv420_bt601_image.chroma_stride = yuv_420_bt601_luma_stride >> 1;
{
// copy luma
uint8_t* y_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.data);
uint8_t* y_src = reinterpret_cast<uint8_t*>(yuv420_image.data);
if (yuv420_bt601_image.luma_stride == yuv420_image.luma_stride) {
memcpy(y_dst, y_src, yuv420_bt601_image.luma_stride * yuv420_image.height);
} else {
for (size_t i = 0; i < yuv420_image.height; i++) {
memcpy(y_dst, y_src, yuv420_image.width);
if (yuv420_image.width != yuv420_bt601_image.luma_stride) {
memset(y_dst + yuv420_image.width, 0,
yuv420_bt601_image.luma_stride - yuv420_image.width);
}
y_dst += yuv420_bt601_image.luma_stride;
y_src += yuv420_image.luma_stride;
}
}
}
if (yuv420_bt601_image.chroma_stride == yuv420_image.chroma_stride) {
// copy luma
uint8_t* ch_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data);
uint8_t* ch_src = reinterpret_cast<uint8_t*>(yuv420_image.chroma_data);
memcpy(ch_dst, ch_src, yuv420_bt601_image.chroma_stride * yuv420_image.height);
} else {
// copy cb & cr
uint8_t* cb_dst = reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data);
uint8_t* cb_src = reinterpret_cast<uint8_t*>(yuv420_image.chroma_data);
uint8_t* cr_dst = cb_dst + (yuv420_bt601_image.chroma_stride * yuv420_bt601_image.height / 2);
uint8_t* cr_src = cb_src + (yuv420_image.chroma_stride * yuv420_image.height / 2);
for (size_t i = 0; i < yuv420_image.height / 2; i++) {
memcpy(cb_dst, cb_src, yuv420_image.width / 2);
memcpy(cr_dst, cr_src, yuv420_image.width / 2);
if (yuv420_bt601_image.width / 2 != yuv420_bt601_image.chroma_stride) {
memset(cb_dst + yuv420_image.width / 2, 0,
yuv420_bt601_image.chroma_stride - yuv420_image.width / 2);
memset(cr_dst + yuv420_image.width / 2, 0,
yuv420_bt601_image.chroma_stride - yuv420_image.width / 2);
}
cb_dst += yuv420_bt601_image.chroma_stride;
cb_src += yuv420_image.chroma_stride;
cr_dst += yuv420_bt601_image.chroma_stride;
cr_src += yuv420_image.chroma_stride;
}
}
JPEGR_CHECK(convertYuv(&yuv420_bt601_image, yuv420_image.colorGamut, ULTRAHDR_COLORGAMUT_P3));
}
// compress 420 image
JpegEncoderHelper jpeg_enc_obj_yuv420;
const uint8_t* planes[3]{reinterpret_cast<uint8_t*>(yuv420_bt601_image.data),
reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data),
reinterpret_cast<uint8_t*>(yuv420_bt601_image.chroma_data) +
yuv420_bt601_image.chroma_stride * yuv420_bt601_image.height / 2};
const size_t strides[3]{yuv420_bt601_image.luma_stride, yuv420_bt601_image.chroma_stride,
yuv420_bt601_image.chroma_stride};
if (!jpeg_enc_obj_yuv420.compressImage(planes, strides, yuv420_bt601_image.width,
yuv420_bt601_image.height, UHDR_IMG_FMT_12bppYCbCr420,
quality, icc->getData(), icc->getLength())) {
return ERROR_JPEGR_ENCODE_ERROR;
}
jpegr_compressed_struct jpeg;
jpeg.data = jpeg_enc_obj_yuv420.getCompressedImagePtr();
jpeg.length = static_cast<int>(jpeg_enc_obj_yuv420.getCompressedImageSize());
jpeg.maxLength = static_cast<int>(jpeg_enc_obj_yuv420.getCompressedImageSize());
jpeg.colorGamut = yuv420_image.colorGamut;
// append gain map, no ICC since JPEG encode already did it
JPEGR_CHECK(appendGainMap(&jpeg, &compressed_map, exif, /* icc */ nullptr, /* icc size */ 0,
&metadata, dest));
return JPEGR_NO_ERROR;
}
/* Encode API-2 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
jr_uncompressed_ptr yuv420_image_ptr,
jr_compressed_ptr yuv420jpg_image_ptr,
ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest) {
// validate input arguments
if (yuv420_image_ptr == nullptr) {
ALOGE("received nullptr for uncompressed 420 image");
return ERROR_JPEGR_BAD_PTR;
}
if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_BAD_PTR;
}
JPEGR_CHECK(areInputArgumentsValid(p010_image_ptr, yuv420_image_ptr, hdr_tf, dest))
// clean up input structure for later usage
jpegr_uncompressed_struct p010_image = *p010_image_ptr;
if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
if (!p010_image.chroma_data) {
uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
p010_image.chroma_stride = p010_image.luma_stride;
}
jpegr_uncompressed_struct yuv420_image = *yuv420_image_ptr;
if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
if (!yuv420_image.chroma_data) {
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
yuv420_image.chroma_data = data + yuv420_image.luma_stride * p010_image.height;
yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
}
// gain map
ultrahdr_metadata_struct metadata;
metadata.version = kJpegrVersion;
jpegr_uncompressed_struct gainmap_image;
JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image));
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
jpegr_compressed_struct gainmapjpg_image;
gainmapjpg_image.data = jpeg_enc_obj_gm.getCompressedImagePtr();
gainmapjpg_image.length = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
gainmapjpg_image.maxLength = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
gainmapjpg_image.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
return encodeJPEGR(yuv420jpg_image_ptr, &gainmapjpg_image, &metadata, dest);
}
/* Encode API-3 */
status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr,
jr_compressed_ptr yuv420jpg_image_ptr,
ultrahdr_transfer_function hdr_tf, jr_compressed_ptr dest) {
// validate input arguments
if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_BAD_PTR;
}
JPEGR_CHECK(areInputArgumentsValid(p010_image_ptr, nullptr, hdr_tf, dest))
// clean up input structure for later usage
jpegr_uncompressed_struct p010_image = *p010_image_ptr;
if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width;
if (!p010_image.chroma_data) {
uint16_t* data = reinterpret_cast<uint16_t*>(p010_image.data);
p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height;
p010_image.chroma_stride = p010_image.luma_stride;
}
// decode input jpeg, gamut is going to be bt601.
JpegDecoderHelper jpeg_dec_obj_yuv420;
if (!jpeg_dec_obj_yuv420.decompressImage(yuv420jpg_image_ptr->data,
yuv420jpg_image_ptr->length)) {
return ERROR_JPEGR_DECODE_ERROR;
}
jpegr_uncompressed_struct yuv420_image{};
yuv420_image.data = jpeg_dec_obj_yuv420.getDecompressedImagePtr();
yuv420_image.width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
yuv420_image.height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
if (jpeg_dec_obj_yuv420.getICCSize() > 0) {
ultrahdr_color_gamut cg = IccHelper::readIccColorGamut(jpeg_dec_obj_yuv420.getICCPtr(),
jpeg_dec_obj_yuv420.getICCSize());
if (cg == ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
(yuv420jpg_image_ptr->colorGamut != ULTRAHDR_COLORGAMUT_UNSPECIFIED &&
yuv420jpg_image_ptr->colorGamut != cg)) {
ALOGE("configured color gamut %d does not match with color gamut specified in icc box %d",
yuv420jpg_image_ptr->colorGamut, cg);
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
yuv420_image.colorGamut = cg;
} else {
if (yuv420jpg_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
yuv420jpg_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
ALOGE("Unrecognized 420 color gamut %d", yuv420jpg_image_ptr->colorGamut);
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
yuv420_image.colorGamut = yuv420jpg_image_ptr->colorGamut;
}
if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width;
if (!yuv420_image.chroma_data) {
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
yuv420_image.chroma_data = data + yuv420_image.luma_stride * p010_image.height;
yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1;
}
if (p010_image_ptr->width != yuv420_image.width ||
p010_image_ptr->height != yuv420_image.height) {
return ERROR_JPEGR_RESOLUTION_MISMATCH;
}
// gain map
ultrahdr_metadata_struct metadata;
metadata.version = kJpegrVersion;
jpegr_uncompressed_struct gainmap_image;
JPEGR_CHECK(generateGainMap(&yuv420_image, &p010_image, hdr_tf, &metadata, &gainmap_image,
true /* sdr_is_601 */));
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(gainmap_image.data));
// compress gain map
JpegEncoderHelper jpeg_enc_obj_gm;
JPEGR_CHECK(compressGainMap(&gainmap_image, &jpeg_enc_obj_gm));
jpegr_compressed_struct gainmapjpg_image;
gainmapjpg_image.data = jpeg_enc_obj_gm.getCompressedImagePtr();
gainmapjpg_image.length = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
gainmapjpg_image.maxLength = static_cast<int>(jpeg_enc_obj_gm.getCompressedImageSize());
gainmapjpg_image.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
return encodeJPEGR(yuv420jpg_image_ptr, &gainmapjpg_image, &metadata, dest);
}
/* Encode API-4 */
status_t JpegR::encodeJPEGR(jr_compressed_ptr yuv420jpg_image_ptr,
jr_compressed_ptr gainmapjpg_image_ptr, ultrahdr_metadata_ptr metadata,
jr_compressed_ptr dest) {
if (yuv420jpg_image_ptr == nullptr || yuv420jpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpeg image");
return ERROR_JPEGR_BAD_PTR;
}
if (gainmapjpg_image_ptr == nullptr || gainmapjpg_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed gain map");
return ERROR_JPEGR_BAD_PTR;
}
if (dest == nullptr || dest->data == nullptr) {
ALOGE("received nullptr for destination");
return ERROR_JPEGR_BAD_PTR;
}
// 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;
if (!decoder.parseImage(yuv420jpg_image_ptr->data, yuv420jpg_image_ptr->length)) {
return ERROR_JPEGR_DECODE_ERROR;
}
// Add ICC if not already present.
if (decoder.getICCSize() > 0) {
JPEGR_CHECK(appendGainMap(yuv420jpg_image_ptr, gainmapjpg_image_ptr, /* exif */ nullptr,
/* icc */ nullptr, /* icc size */ 0, metadata, dest));
} else {
if (yuv420jpg_image_ptr->colorGamut <= ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
yuv420jpg_image_ptr->colorGamut > ULTRAHDR_COLORGAMUT_MAX) {
ALOGE("Unrecognized 420 color gamut %d", yuv420jpg_image_ptr->colorGamut);
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
std::shared_ptr<DataStruct> newIcc =
IccHelper::writeIccProfile(ULTRAHDR_TF_SRGB, yuv420jpg_image_ptr->colorGamut);
JPEGR_CHECK(appendGainMap(yuv420jpg_image_ptr, gainmapjpg_image_ptr, /* exif */ nullptr,
newIcc->getData(), newIcc->getLength(), metadata, dest));
}
return JPEGR_NO_ERROR;
}
status_t JpegR::getJPEGRInfo(jr_compressed_ptr jpegr_image_ptr, jr_info_ptr jpegr_image_info_ptr) {
if (jpegr_image_ptr == nullptr || jpegr_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpegr image");
return ERROR_JPEGR_BAD_PTR;
}
if (jpegr_image_info_ptr == nullptr) {
ALOGE("received nullptr for compressed jpegr info struct");
return ERROR_JPEGR_BAD_PTR;
}
jpegr_compressed_struct primary_image, gainmap_image;
JPEGR_CHECK(extractPrimaryImageAndGainMap(jpegr_image_ptr, &primary_image, &gainmap_image))
JPEGR_CHECK(parseJpegInfo(&primary_image, jpegr_image_info_ptr->primaryImgInfo,
&jpegr_image_info_ptr->width, &jpegr_image_info_ptr->height))
if (jpegr_image_info_ptr->gainmapImgInfo != nullptr) {
JPEGR_CHECK(parseJpegInfo(&gainmap_image, jpegr_image_info_ptr->gainmapImgInfo))
}
return JPEGR_NO_ERROR;
}
/* Decode API */
status_t JpegR::decodeJPEGR(jr_compressed_ptr jpegr_image_ptr, jr_uncompressed_ptr dest,
float max_display_boost, jr_exif_ptr exif,
ultrahdr_output_format output_format,
jr_uncompressed_ptr gainmap_image_ptr, ultrahdr_metadata_ptr metadata) {
if (jpegr_image_ptr == nullptr || jpegr_image_ptr->data == nullptr) {
ALOGE("received nullptr for compressed jpegr image");
return ERROR_JPEGR_BAD_PTR;
}
if (dest == nullptr || dest->data == nullptr) {
ALOGE("received nullptr for dest image");
return ERROR_JPEGR_BAD_PTR;
}
if (max_display_boost < 1.0f) {
ALOGE("received bad value for max_display_boost %f", max_display_boost);
return ERROR_JPEGR_INVALID_DISPLAY_BOOST;
}
if (exif != nullptr && exif->data == nullptr) {
ALOGE("received nullptr address for exif data");
return ERROR_JPEGR_BAD_PTR;
}
if (gainmap_image_ptr != nullptr && gainmap_image_ptr->data == nullptr) {
ALOGE("received nullptr address for gainmap data");
return ERROR_JPEGR_BAD_PTR;
}
if (output_format <= ULTRAHDR_OUTPUT_UNSPECIFIED || output_format > ULTRAHDR_OUTPUT_MAX) {
ALOGE("received bad value for output format %d", output_format);
return ERROR_JPEGR_INVALID_OUTPUT_FORMAT;
}
jpegr_compressed_struct primary_jpeg_image, gainmap_jpeg_image;
JPEGR_CHECK(
extractPrimaryImageAndGainMap(jpegr_image_ptr, &primary_jpeg_image, &gainmap_jpeg_image))
JpegDecoderHelper jpeg_dec_obj_yuv420;
if (!jpeg_dec_obj_yuv420.decompressImage(
primary_jpeg_image.data, primary_jpeg_image.length,
(output_format == ULTRAHDR_OUTPUT_SDR) ? DECODE_TO_RGB_CS : DECODE_TO_YCBCR_CS)) {
return ERROR_JPEGR_DECODE_ERROR;
}
if (output_format == ULTRAHDR_OUTPUT_SDR) {
#ifdef JCS_ALPHA_EXTENSIONS
if ((jpeg_dec_obj_yuv420.getDecompressedImageWidth() *
jpeg_dec_obj_yuv420.getDecompressedImageHeight() * 4) >
jpeg_dec_obj_yuv420.getDecompressedImageSize()) {
return ERROR_JPEGR_DECODE_ERROR;
}
#else
if ((jpeg_dec_obj_yuv420.getDecompressedImageWidth() *
jpeg_dec_obj_yuv420.getDecompressedImageHeight() * 3) >
jpeg_dec_obj_yuv420.getDecompressedImageSize()) {
return ERROR_JPEGR_DECODE_ERROR;
}
#endif
} else {
if ((jpeg_dec_obj_yuv420.getDecompressedImageWidth() *
jpeg_dec_obj_yuv420.getDecompressedImageHeight() * 3 / 2) >
jpeg_dec_obj_yuv420.getDecompressedImageSize()) {
return ERROR_JPEGR_DECODE_ERROR;
}
}
if (exif != nullptr) {
if (exif->length < jpeg_dec_obj_yuv420.getEXIFSize()) {
return ERROR_JPEGR_BUFFER_TOO_SMALL;
}
memcpy(exif->data, jpeg_dec_obj_yuv420.getEXIFPtr(), jpeg_dec_obj_yuv420.getEXIFSize());
exif->length = jpeg_dec_obj_yuv420.getEXIFSize();
}
JpegDecoderHelper jpeg_dec_obj_gm;
jpegr_uncompressed_struct gainmap_image;
if (gainmap_image_ptr != nullptr || output_format != ULTRAHDR_OUTPUT_SDR) {
if (!jpeg_dec_obj_gm.decompressImage(gainmap_jpeg_image.data, gainmap_jpeg_image.length,
DECODE_STREAM)) {
return ERROR_JPEGR_DECODE_ERROR;
}
gainmap_image.data = jpeg_dec_obj_gm.getDecompressedImagePtr();
gainmap_image.width = jpeg_dec_obj_gm.getDecompressedImageWidth();
gainmap_image.height = jpeg_dec_obj_gm.getDecompressedImageHeight();
gainmap_image.pixelFormat = jpeg_dec_obj_gm.getDecompressedImageFormat();
if (gainmap_image_ptr != nullptr) {
gainmap_image_ptr->width = gainmap_image.width;
gainmap_image_ptr->height = gainmap_image.height;
gainmap_image_ptr->pixelFormat = gainmap_image.pixelFormat;
memcpy(gainmap_image_ptr->data, gainmap_image.data,
gainmap_image_ptr->width * gainmap_image_ptr->height);
}
}
ultrahdr_metadata_struct uhdr_metadata;
if (metadata != nullptr || output_format != ULTRAHDR_OUTPUT_SDR) {
uint8_t* iso_ptr = static_cast<uint8_t*>(jpeg_dec_obj_gm.getIsoMetadataPtr());
if (iso_ptr != nullptr) {
size_t iso_size = jpeg_dec_obj_gm.getIsoMetadataSize();
if (iso_size < kIsoNameSpace.size() + 1) {
return ERROR_JPEGR_METADATA_ERROR;
}
gain_map_metadata decodedMetadata;
std::vector<uint8_t> iso_vec;
for (size_t i = kIsoNameSpace.size() + 1; i < iso_size; i++) {
iso_vec.push_back(iso_ptr[i]);
}
JPEGR_CHECK(gain_map_metadata::decodeGainmapMetadata(iso_vec, &decodedMetadata));
JPEGR_CHECK(
gain_map_metadata::gainmapMetadataFractionToFloat(&decodedMetadata, &uhdr_metadata));
} else {
if (!getMetadataFromXMP(static_cast<uint8_t*>(jpeg_dec_obj_gm.getXMPPtr()),
jpeg_dec_obj_gm.getXMPSize(), &uhdr_metadata)) {
return ERROR_JPEGR_METADATA_ERROR;
}
}
if (metadata != nullptr) {
metadata->version = uhdr_metadata.version;
metadata->minContentBoost = uhdr_metadata.minContentBoost;
metadata->maxContentBoost = uhdr_metadata.maxContentBoost;
metadata->gamma = uhdr_metadata.gamma;
metadata->offsetSdr = uhdr_metadata.offsetSdr;
metadata->offsetHdr = uhdr_metadata.offsetHdr;
metadata->hdrCapacityMin = uhdr_metadata.hdrCapacityMin;
metadata->hdrCapacityMax = uhdr_metadata.hdrCapacityMax;
}
}
if (output_format == ULTRAHDR_OUTPUT_SDR) {
dest->width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
dest->height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
#ifdef JCS_ALPHA_EXTENSIONS
memcpy(dest->data, jpeg_dec_obj_yuv420.getDecompressedImagePtr(),
dest->width * dest->height * 4);
#else
uint32_t* pixelDst = static_cast<uint32_t*>(dest->data);
uint8_t* pixelSrc = static_cast<uint8_t*>(jpeg_dec_obj_yuv420.getDecompressedImagePtr());
for (int i = 0; i < dest->width * dest->height; i++) {
*pixelDst = pixelSrc[0] | (pixelSrc[1] << 8) | (pixelSrc[2] << 16) | (0xff << 24);
pixelSrc += 3;
pixelDst += 1;
}
#endif
dest->colorGamut = IccHelper::readIccColorGamut(jpeg_dec_obj_yuv420.getICCPtr(),
jpeg_dec_obj_yuv420.getICCSize());
return JPEGR_NO_ERROR;
}
jpegr_uncompressed_struct yuv420_image;
yuv420_image.data = jpeg_dec_obj_yuv420.getDecompressedImagePtr();
yuv420_image.width = jpeg_dec_obj_yuv420.getDecompressedImageWidth();
yuv420_image.height = jpeg_dec_obj_yuv420.getDecompressedImageHeight();
yuv420_image.colorGamut = IccHelper::readIccColorGamut(jpeg_dec_obj_yuv420.getICCPtr(),
jpeg_dec_obj_yuv420.getICCSize());
yuv420_image.luma_stride = yuv420_image.width;
uint8_t* data = reinterpret_cast<uint8_t*>(yuv420_image.data);
yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height;
yuv420_image.chroma_stride = yuv420_image.width >> 1;
JPEGR_CHECK(applyGainMap(&yuv420_image, &gainmap_image, &uhdr_metadata, output_format,
max_display_boost, dest));
return JPEGR_NO_ERROR;
}
status_t JpegR::compressGainMap(jr_uncompressed_ptr gainmap_image_ptr,
JpegEncoderHelper* jpeg_enc_obj_ptr) {
if (gainmap_image_ptr == nullptr || jpeg_enc_obj_ptr == nullptr) {
return ERROR_JPEGR_BAD_PTR;
}
const uint8_t* planes[]{reinterpret_cast<uint8_t*>(gainmap_image_ptr->data)};
if (kUseMultiChannelGainMap) {
const size_t strides[]{gainmap_image_ptr->width * 3};
if (!jpeg_enc_obj_ptr->compressImage(planes, strides, gainmap_image_ptr->width,
gainmap_image_ptr->height, UHDR_IMG_FMT_24bppRGB888,
kMapCompressQuality, nullptr, 0)) {
return ERROR_JPEGR_ENCODE_ERROR;
}
} else {
const size_t strides[]{gainmap_image_ptr->width};
// Don't need to convert YUV to Bt601 since single channel
if (!jpeg_enc_obj_ptr->compressImage(planes, strides, gainmap_image_ptr->width,
gainmap_image_ptr->height, UHDR_IMG_FMT_8bppYCbCr400,
kMapCompressQuality, nullptr, 0)) {
return ERROR_JPEGR_ENCODE_ERROR;
}
}
return JPEGR_NO_ERROR;
}
const int kJobSzInRows = 16;
static_assert(kJobSzInRows > 0 && kJobSzInRows % kMapDimensionScaleFactor == 0,
"align job size to kMapDimensionScaleFactor");
class JobQueue {
public:
bool dequeueJob(size_t& rowStart, size_t& rowEnd);
void enqueueJob(size_t rowStart, size_t rowEnd);
void markQueueForEnd();
void reset();
private:
bool mQueuedAllJobs = false;
std::deque<std::tuple<size_t, size_t>> mJobs;
std::mutex mMutex;
std::condition_variable mCv;
};
bool JobQueue::dequeueJob(size_t& rowStart, size_t& 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(size_t rowStart, size_t 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;
}
status_t JpegR::generateGainMap(jr_uncompressed_ptr yuv420_image_ptr,
jr_uncompressed_ptr p010_image_ptr,
ultrahdr_transfer_function hdr_tf, ultrahdr_metadata_ptr metadata,
jr_uncompressed_ptr dest, bool sdr_is_601) {
/*if (kUseMultiChannelGainMap) {
static_assert(kWriteIso21496_1Metadata && !kWriteXmpMetadata,
"Multi-channel gain map now is only supported for ISO 21496-1 metadata");
}*/
int gainMapChannelCount = kUseMultiChannelGainMap ? 3 : 1;
if (yuv420_image_ptr == nullptr || p010_image_ptr == nullptr || metadata == nullptr ||
dest == nullptr || yuv420_image_ptr->data == nullptr ||
yuv420_image_ptr->chroma_data == nullptr || p010_image_ptr->data == nullptr ||
p010_image_ptr->chroma_data == nullptr) {
return ERROR_JPEGR_BAD_PTR;
}
if (yuv420_image_ptr->width != p010_image_ptr->width ||
yuv420_image_ptr->height != p010_image_ptr->height) {
return ERROR_JPEGR_RESOLUTION_MISMATCH;
}
if (yuv420_image_ptr->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED ||
p010_image_ptr->colorGamut == ULTRAHDR_COLORGAMUT_UNSPECIFIED) {
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
size_t image_width = yuv420_image_ptr->width;
size_t image_height = yuv420_image_ptr->height;
size_t map_width = image_width / kMapDimensionScaleFactor;
size_t map_height = image_height / kMapDimensionScaleFactor;
dest->data = new uint8_t[map_width * map_height * gainMapChannelCount];
dest->width = map_width;
dest->height = map_height;
dest->colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
dest->luma_stride = map_width;
dest->chroma_data = nullptr;
dest->chroma_stride = 0;
std::unique_ptr<uint8_t[]> map_data;
map_data.reset(reinterpret_cast<uint8_t*>(dest->data));
ColorTransformFn hdrInvOetf = nullptr;
float hdr_white_nits;
switch (hdr_tf) {
case ULTRAHDR_TF_LINEAR:
hdrInvOetf = identityConversion;
// Note: this will produce clipping if the input exceeds kHlgMaxNits.
// TODO: TF LINEAR will be deprecated.
hdr_white_nits = kHlgMaxNits;
break;
case ULTRAHDR_TF_HLG:
#if USE_HLG_INVOETF_LUT
hdrInvOetf = hlgInvOetfLUT;
#else
hdrInvOetf = hlgInvOetf;
#endif
hdr_white_nits = kHlgMaxNits;
break;
case ULTRAHDR_TF_PQ:
#if USE_PQ_INVOETF_LUT
hdrInvOetf = pqInvOetfLUT;
#else
hdrInvOetf = pqInvOetf;
#endif
hdr_white_nits = kPqMaxNits;
break;
default:
// Should be impossible to hit after input validation.
return ERROR_JPEGR_INVALID_TRANS_FUNC;
}
metadata->maxContentBoost = hdr_white_nits / kSdrWhiteNits;
metadata->minContentBoost = 1.0f;
metadata->gamma = 1.0f;
metadata->offsetSdr = 0.0f;
metadata->offsetHdr = 0.0f;
metadata->hdrCapacityMin = 1.0f;
metadata->hdrCapacityMax = metadata->maxContentBoost;
float log2MinBoost = log2(metadata->minContentBoost);
float log2MaxBoost = log2(metadata->maxContentBoost);
ColorTransformFn hdrGamutConversionFn =
getHdrConversionFn(yuv420_image_ptr->colorGamut, p010_image_ptr->colorGamut);
ColorCalculationFn luminanceFn = nullptr;
ColorTransformFn sdrYuvToRgbFn = nullptr;
switch (yuv420_image_ptr->colorGamut) {
case ULTRAHDR_COLORGAMUT_BT709:
luminanceFn = srgbLuminance;
sdrYuvToRgbFn = srgbYuvToRgb;
break;
case ULTRAHDR_COLORGAMUT_P3:
luminanceFn = p3Luminance;
sdrYuvToRgbFn = p3YuvToRgb;
break;
case ULTRAHDR_COLORGAMUT_BT2100:
luminanceFn = bt2100Luminance;
sdrYuvToRgbFn = bt2100YuvToRgb;
break;
case ULTRAHDR_COLORGAMUT_UNSPECIFIED:
// Should be impossible to hit after input validation.
return ERROR_JPEGR_INVALID_COLORGAMUT;
}
if (sdr_is_601) {
sdrYuvToRgbFn = p3YuvToRgb;
}
ColorTransformFn hdrYuvToRgbFn = nullptr;
switch (p010_image_ptr->colorGamut) {
case ULTRAHDR_COLORGAMUT_BT709:
hdrYuvToRgbFn = srgbYuvToRgb;
break;
case ULTRAHDR_COLORGAMUT_P3:
hdrYuvToRgbFn = p3YuvToRgb;
break;
case ULTRAHDR_COLORGAMUT_BT2100:
hdrYuvToRgbFn = bt2100YuvToRgb;
break;