forked from AOMediaCodec/libavif
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreformat.c
1822 lines (1627 loc) · 82.4 KB
/
reformat.c
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 2019 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause
#include "avif/internal.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
#if defined(_WIN32)
#include <process.h>
#include <windows.h>
#else
#include <pthread.h>
#endif
struct YUVBlock
{
float y;
float u;
float v;
};
avifBool avifGetRGBColorSpaceInfo(const avifRGBImage * rgb, avifRGBColorSpaceInfo * info)
{
AVIF_CHECK(rgb->depth == 8 || rgb->depth == 10 || rgb->depth == 12 || rgb->depth == 16);
if (rgb->isFloat) {
AVIF_CHECK(rgb->depth == 16);
}
if (rgb->format == AVIF_RGB_FORMAT_RGB_565) {
AVIF_CHECK(rgb->depth == 8);
}
// Cast to silence "comparison of unsigned expression is always true" warning.
AVIF_CHECK((int)rgb->format >= AVIF_RGB_FORMAT_RGB && rgb->format < AVIF_RGB_FORMAT_COUNT);
info->channelBytes = (rgb->depth > 8) ? 2 : 1;
info->pixelBytes = avifRGBImagePixelSize(rgb);
switch (rgb->format) {
case AVIF_RGB_FORMAT_RGB:
info->offsetBytesR = info->channelBytes * 0;
info->offsetBytesG = info->channelBytes * 1;
info->offsetBytesB = info->channelBytes * 2;
info->offsetBytesA = 0;
break;
case AVIF_RGB_FORMAT_RGBA:
info->offsetBytesR = info->channelBytes * 0;
info->offsetBytesG = info->channelBytes * 1;
info->offsetBytesB = info->channelBytes * 2;
info->offsetBytesA = info->channelBytes * 3;
break;
case AVIF_RGB_FORMAT_ARGB:
info->offsetBytesA = info->channelBytes * 0;
info->offsetBytesR = info->channelBytes * 1;
info->offsetBytesG = info->channelBytes * 2;
info->offsetBytesB = info->channelBytes * 3;
break;
case AVIF_RGB_FORMAT_BGR:
info->offsetBytesB = info->channelBytes * 0;
info->offsetBytesG = info->channelBytes * 1;
info->offsetBytesR = info->channelBytes * 2;
info->offsetBytesA = 0;
break;
case AVIF_RGB_FORMAT_BGRA:
info->offsetBytesB = info->channelBytes * 0;
info->offsetBytesG = info->channelBytes * 1;
info->offsetBytesR = info->channelBytes * 2;
info->offsetBytesA = info->channelBytes * 3;
break;
case AVIF_RGB_FORMAT_ABGR:
info->offsetBytesA = info->channelBytes * 0;
info->offsetBytesB = info->channelBytes * 1;
info->offsetBytesG = info->channelBytes * 2;
info->offsetBytesR = info->channelBytes * 3;
break;
case AVIF_RGB_FORMAT_RGB_565:
// Since RGB_565 consists of two bytes per RGB pixel, we simply use
// the pointer to the red channel to populate the entire pixel value
// as a uint16_t. As a result only offsetBytesR is used and the
// other offsets are unused.
info->offsetBytesR = 0;
info->offsetBytesG = 0;
info->offsetBytesB = 0;
info->offsetBytesA = 0;
break;
case AVIF_RGB_FORMAT_COUNT:
return AVIF_FALSE;
}
info->maxChannel = (1 << rgb->depth) - 1;
info->maxChannelF = (float)info->maxChannel;
return AVIF_TRUE;
}
avifBool avifGetYUVColorSpaceInfo(const avifImage * image, avifYUVColorSpaceInfo * info)
{
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
const avifBool useYCgCo = (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO_RE) ||
(image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO_RO);
#endif
AVIF_CHECK(image->depth == 8 || image->depth == 10 || image->depth == 12 || image->depth == 16);
AVIF_CHECK(image->yuvFormat >= AVIF_PIXEL_FORMAT_YUV444 && image->yuvFormat < AVIF_PIXEL_FORMAT_COUNT);
AVIF_CHECK(image->yuvRange == AVIF_RANGE_LIMITED || image->yuvRange == AVIF_RANGE_FULL);
// These matrix coefficients values are currently unsupported. Revise this list as more support is added.
//
// YCgCo performs limited-full range adjustment on R,G,B but the current implementation performs range adjustment
// on Y,U,V. So YCgCo with limited range is unsupported.
if ((image->matrixCoefficients == 3 /* CICP reserved */) ||
((image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
|| useYCgCo
#endif
) &&
(image->yuvRange == AVIF_RANGE_LIMITED)) ||
(image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_BT2020_CL) ||
(image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_SMPTE2085) ||
(image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_CHROMA_DERIVED_CL) ||
(image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_ICTCP) || (image->matrixCoefficients >= AVIF_MATRIX_COEFFICIENTS_LAST)) {
return AVIF_FALSE;
}
if ((image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_IDENTITY) && (image->yuvFormat != AVIF_PIXEL_FORMAT_YUV444) &&
(image->yuvFormat != AVIF_PIXEL_FORMAT_YUV400)) {
return AVIF_FALSE;
}
avifGetPixelFormatInfo(image->yuvFormat, &info->formatInfo);
avifCalcYUVCoefficients(image, &info->kr, &info->kg, &info->kb);
info->channelBytes = (image->depth > 8) ? 2 : 1;
info->depth = image->depth;
info->range = image->yuvRange;
info->maxChannel = (1 << image->depth) - 1;
info->biasY = (info->range == AVIF_RANGE_LIMITED) ? (float)(16 << (info->depth - 8)) : 0.0f;
info->biasUV = (float)(1 << (info->depth - 1));
info->rangeY = (float)((info->range == AVIF_RANGE_LIMITED) ? (219 << (info->depth - 8)) : info->maxChannel);
info->rangeUV = (float)((info->range == AVIF_RANGE_LIMITED) ? (224 << (info->depth - 8)) : info->maxChannel);
return AVIF_TRUE;
}
static avifBool avifPrepareReformatState(const avifImage * image, const avifRGBImage * rgb, avifReformatState * state)
{
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
const avifBool useYCgCoRe = (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO_RE);
const avifBool useYCgCoRo = (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO_RO);
if (useYCgCoRe || useYCgCoRo) {
const int bitOffset = (useYCgCoRe) ? 2 : 1;
if (image->depth - bitOffset != rgb->depth) {
return AVIF_FALSE;
}
}
#endif
AVIF_CHECK(avifGetRGBColorSpaceInfo(rgb, &state->rgb));
AVIF_CHECK(avifGetYUVColorSpaceInfo(image, &state->yuv));
state->yuv.mode = AVIF_REFORMAT_MODE_YUV_COEFFICIENTS;
if (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_IDENTITY) {
state->yuv.mode = AVIF_REFORMAT_MODE_IDENTITY;
} else if (image->matrixCoefficients == AVIF_MATRIX_COEFFICIENTS_YCGCO) {
state->yuv.mode = AVIF_REFORMAT_MODE_YCGCO;
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
} else if (useYCgCoRe) {
state->yuv.mode = AVIF_REFORMAT_MODE_YCGCO_RE;
} else if (useYCgCoRo) {
state->yuv.mode = AVIF_REFORMAT_MODE_YCGCO_RO;
#endif
}
if (state->yuv.mode != AVIF_REFORMAT_MODE_YUV_COEFFICIENTS) {
state->yuv.kr = 0.0f;
state->yuv.kg = 0.0f;
state->yuv.kb = 0.0f;
}
return AVIF_TRUE;
}
// Formulas 20-31 from https://www.itu.int/rec/T-REC-H.273-201612-S
static int avifYUVColorSpaceInfoYToUNorm(avifYUVColorSpaceInfo * info, float v)
{
int unorm = (int)avifRoundf(v * info->rangeY + info->biasY);
return AVIF_CLAMP(unorm, 0, info->maxChannel);
}
static int avifYUVColorSpaceInfoUVToUNorm(avifYUVColorSpaceInfo * info, float v)
{
int unorm;
// YCgCo performs limited-full range adjustment on R,G,B but the current implementation performs range adjustment
// on Y,U,V. So YCgCo with limited range is unsupported.
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
assert((info->mode != AVIF_REFORMAT_MODE_YCGCO && info->mode != AVIF_REFORMAT_MODE_YCGCO_RE && info->mode != AVIF_REFORMAT_MODE_YCGCO_RO) ||
(info->range == AVIF_RANGE_FULL));
#else
assert((info->mode != AVIF_REFORMAT_MODE_YCGCO) || (info->range == AVIF_RANGE_FULL));
#endif
if (info->mode == AVIF_REFORMAT_MODE_IDENTITY) {
unorm = (int)avifRoundf(v * info->rangeY + info->biasY);
} else {
unorm = (int)avifRoundf(v * info->rangeUV + info->biasUV);
}
return AVIF_CLAMP(unorm, 0, info->maxChannel);
}
avifResult avifImageRGBToYUV(avifImage * image, const avifRGBImage * rgb)
{
if (!rgb->pixels || rgb->format == AVIF_RGB_FORMAT_RGB_565) {
return AVIF_RESULT_REFORMAT_FAILED;
}
avifReformatState state;
if (!avifPrepareReformatState(image, rgb, &state)) {
return AVIF_RESULT_REFORMAT_FAILED;
}
if (rgb->isFloat) {
return AVIF_RESULT_NOT_IMPLEMENTED;
}
const avifBool hasAlpha = avifRGBFormatHasAlpha(rgb->format) && !rgb->ignoreAlpha;
avifResult allocationResult = avifImageAllocatePlanes(image, hasAlpha ? AVIF_PLANES_ALL : AVIF_PLANES_YUV);
if (allocationResult != AVIF_RESULT_OK) {
return allocationResult;
}
avifAlphaMultiplyMode alphaMode = AVIF_ALPHA_MULTIPLY_MODE_NO_OP;
if (hasAlpha) {
if (!rgb->alphaPremultiplied && image->alphaPremultiplied) {
alphaMode = AVIF_ALPHA_MULTIPLY_MODE_MULTIPLY;
} else if (rgb->alphaPremultiplied && !image->alphaPremultiplied) {
alphaMode = AVIF_ALPHA_MULTIPLY_MODE_UNMULTIPLY;
}
}
avifBool converted = AVIF_FALSE;
// Try converting with libsharpyuv.
if ((rgb->chromaDownsampling == AVIF_CHROMA_DOWNSAMPLING_SHARP_YUV) && (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) {
const avifResult libSharpYUVResult = avifImageRGBToYUVLibSharpYUV(image, rgb, &state);
if (libSharpYUVResult != AVIF_RESULT_OK) {
// Return the error if sharpyuv was requested but failed for any reason, including libsharpyuv not being available.
return libSharpYUVResult;
}
converted = AVIF_TRUE;
}
if (!converted && !rgb->avoidLibYUV && (alphaMode == AVIF_ALPHA_MULTIPLY_MODE_NO_OP)) {
avifResult libyuvResult = avifImageRGBToYUVLibYUV(image, rgb);
if (libyuvResult == AVIF_RESULT_OK) {
converted = AVIF_TRUE;
} else if (libyuvResult != AVIF_RESULT_NOT_IMPLEMENTED) {
return libyuvResult;
}
}
if (!converted) {
const float kr = state.yuv.kr;
const float kg = state.yuv.kg;
const float kb = state.yuv.kb;
struct YUVBlock yuvBlock[2][2];
float rgbPixel[3];
const uint32_t rgbPixelBytes = state.rgb.pixelBytes;
const uint32_t offsetBytesR = state.rgb.offsetBytesR;
const uint32_t offsetBytesG = state.rgb.offsetBytesG;
const uint32_t offsetBytesB = state.rgb.offsetBytesB;
const uint32_t offsetBytesA = state.rgb.offsetBytesA;
const uint32_t rgbRowBytes = rgb->rowBytes;
const float rgbMaxChannelF = state.rgb.maxChannelF;
uint8_t * yPlane = image->yuvPlanes[AVIF_CHAN_Y];
uint8_t * uPlane = image->yuvPlanes[AVIF_CHAN_U];
uint8_t * vPlane = image->yuvPlanes[AVIF_CHAN_V];
const uint32_t yRowBytes = image->yuvRowBytes[AVIF_CHAN_Y];
const uint32_t uRowBytes = image->yuvRowBytes[AVIF_CHAN_U];
const uint32_t vRowBytes = image->yuvRowBytes[AVIF_CHAN_V];
for (uint32_t outerJ = 0; outerJ < image->height; outerJ += 2) {
for (uint32_t outerI = 0; outerI < image->width; outerI += 2) {
int blockW = 2, blockH = 2;
if ((outerI + 1) >= image->width) {
blockW = 1;
}
if ((outerJ + 1) >= image->height) {
blockH = 1;
}
// Convert an entire 2x2 block to YUV, and populate any fully sampled channels as we go
for (int bJ = 0; bJ < blockH; ++bJ) {
for (int bI = 0; bI < blockW; ++bI) {
int i = outerI + bI;
int j = outerJ + bJ;
// Unpack RGB into normalized float
if (state.rgb.channelBytes > 1) {
rgbPixel[0] = *((uint16_t *)(&rgb->pixels[offsetBytesR + (i * rgbPixelBytes) + (j * rgbRowBytes)])) /
rgbMaxChannelF;
rgbPixel[1] = *((uint16_t *)(&rgb->pixels[offsetBytesG + (i * rgbPixelBytes) + (j * rgbRowBytes)])) /
rgbMaxChannelF;
rgbPixel[2] = *((uint16_t *)(&rgb->pixels[offsetBytesB + (i * rgbPixelBytes) + (j * rgbRowBytes)])) /
rgbMaxChannelF;
} else {
rgbPixel[0] = rgb->pixels[offsetBytesR + (i * rgbPixelBytes) + (j * rgbRowBytes)] / rgbMaxChannelF;
rgbPixel[1] = rgb->pixels[offsetBytesG + (i * rgbPixelBytes) + (j * rgbRowBytes)] / rgbMaxChannelF;
rgbPixel[2] = rgb->pixels[offsetBytesB + (i * rgbPixelBytes) + (j * rgbRowBytes)] / rgbMaxChannelF;
}
if (alphaMode != AVIF_ALPHA_MULTIPLY_MODE_NO_OP) {
float a;
if (state.rgb.channelBytes > 1) {
a = *((uint16_t *)(&rgb->pixels[offsetBytesA + (i * rgbPixelBytes) + (j * rgbRowBytes)])) / rgbMaxChannelF;
} else {
a = rgb->pixels[offsetBytesA + (i * rgbPixelBytes) + (j * rgbRowBytes)] / rgbMaxChannelF;
}
if (alphaMode == AVIF_ALPHA_MULTIPLY_MODE_MULTIPLY) {
if (a == 0) {
rgbPixel[0] = 0;
rgbPixel[1] = 0;
rgbPixel[2] = 0;
} else if (a < 1.0f) {
rgbPixel[0] *= a;
rgbPixel[1] *= a;
rgbPixel[2] *= a;
}
} else {
// alphaMode == AVIF_ALPHA_MULTIPLY_MODE_UNMULTIPLY
if (a == 0) {
rgbPixel[0] = 0;
rgbPixel[1] = 0;
rgbPixel[2] = 0;
} else if (a < 1.0f) {
rgbPixel[0] /= a;
rgbPixel[1] /= a;
rgbPixel[2] /= a;
rgbPixel[0] = AVIF_MIN(rgbPixel[0], 1.0f);
rgbPixel[1] = AVIF_MIN(rgbPixel[1], 1.0f);
rgbPixel[2] = AVIF_MIN(rgbPixel[2], 1.0f);
}
}
}
// RGB -> YUV conversion
if (state.yuv.mode == AVIF_REFORMAT_MODE_IDENTITY) {
// Formulas 41,42,43 from https://www.itu.int/rec/T-REC-H.273-201612-S
yuvBlock[bI][bJ].y = rgbPixel[1]; // G
yuvBlock[bI][bJ].u = rgbPixel[2]; // B
yuvBlock[bI][bJ].v = rgbPixel[0]; // R
} else if (state.yuv.mode == AVIF_REFORMAT_MODE_YCGCO) {
// Formulas 44,45,46 from https://www.itu.int/rec/T-REC-H.273-201612-S
yuvBlock[bI][bJ].y = 0.5f * rgbPixel[1] + 0.25f * (rgbPixel[0] + rgbPixel[2]);
yuvBlock[bI][bJ].u = 0.5f * rgbPixel[1] - 0.25f * (rgbPixel[0] + rgbPixel[2]);
yuvBlock[bI][bJ].v = 0.5f * (rgbPixel[0] - rgbPixel[2]);
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
} else if (state.yuv.mode == AVIF_REFORMAT_MODE_YCGCO_RE || state.yuv.mode == AVIF_REFORMAT_MODE_YCGCO_RO) {
// Formulas 58,59,60,61 from https://www.itu.int/rec/T-REC-H.273-202407-P
const int R = (int)avifRoundf(AVIF_CLAMP(rgbPixel[0] * rgbMaxChannelF, 0.0f, rgbMaxChannelF));
const int G = (int)avifRoundf(AVIF_CLAMP(rgbPixel[1] * rgbMaxChannelF, 0.0f, rgbMaxChannelF));
const int B = (int)avifRoundf(AVIF_CLAMP(rgbPixel[2] * rgbMaxChannelF, 0.0f, rgbMaxChannelF));
const int Co = R - B;
const int t = B + (Co >> 1);
const int Cg = G - t;
yuvBlock[bI][bJ].y = (t + (Cg >> 1)) / state.yuv.rangeY;
yuvBlock[bI][bJ].u = Cg / state.yuv.rangeUV;
yuvBlock[bI][bJ].v = Co / state.yuv.rangeUV;
#endif
} else {
float Y = (kr * rgbPixel[0]) + (kg * rgbPixel[1]) + (kb * rgbPixel[2]);
yuvBlock[bI][bJ].y = Y;
yuvBlock[bI][bJ].u = (rgbPixel[2] - Y) / (2 * (1 - kb));
yuvBlock[bI][bJ].v = (rgbPixel[0] - Y) / (2 * (1 - kr));
}
if (state.yuv.channelBytes > 1) {
uint16_t * pY = (uint16_t *)&yPlane[(i * 2) + (j * yRowBytes)];
*pY = (uint16_t)avifYUVColorSpaceInfoYToUNorm(&state.yuv, yuvBlock[bI][bJ].y);
if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) {
// YUV444, full chroma
uint16_t * pU = (uint16_t *)&uPlane[(i * 2) + (j * uRowBytes)];
*pU = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, yuvBlock[bI][bJ].u);
uint16_t * pV = (uint16_t *)&vPlane[(i * 2) + (j * vRowBytes)];
*pV = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, yuvBlock[bI][bJ].v);
}
} else {
yPlane[i + (j * yRowBytes)] = (uint8_t)avifYUVColorSpaceInfoYToUNorm(&state.yuv, yuvBlock[bI][bJ].y);
if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) {
// YUV444, full chroma
uPlane[i + (j * uRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, yuvBlock[bI][bJ].u);
vPlane[i + (j * vRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, yuvBlock[bI][bJ].v);
}
}
}
}
// Populate any subsampled channels with averages from the 2x2 block
if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV400) {
// Do nothing on chroma planes.
} else if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {
// YUV420, average 4 samples (2x2)
float sumU = 0.0f;
float sumV = 0.0f;
for (int bJ = 0; bJ < blockH; ++bJ) {
for (int bI = 0; bI < blockW; ++bI) {
sumU += yuvBlock[bI][bJ].u;
sumV += yuvBlock[bI][bJ].v;
}
}
float totalSamples = (float)(blockW * blockH);
float avgU = sumU / totalSamples;
float avgV = sumV / totalSamples;
const int chromaShiftX = 1;
const int chromaShiftY = 1;
int uvI = outerI >> chromaShiftX;
int uvJ = outerJ >> chromaShiftY;
if (state.yuv.channelBytes > 1) {
uint16_t * pU = (uint16_t *)&uPlane[(uvI * 2) + (uvJ * uRowBytes)];
*pU = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgU);
uint16_t * pV = (uint16_t *)&vPlane[(uvI * 2) + (uvJ * vRowBytes)];
*pV = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgV);
} else {
uPlane[uvI + (uvJ * uRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgU);
vPlane[uvI + (uvJ * vRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgV);
}
} else if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) {
// YUV422, average 2 samples (1x2), twice
for (int bJ = 0; bJ < blockH; ++bJ) {
float sumU = 0.0f;
float sumV = 0.0f;
for (int bI = 0; bI < blockW; ++bI) {
sumU += yuvBlock[bI][bJ].u;
sumV += yuvBlock[bI][bJ].v;
}
float totalSamples = (float)blockW;
float avgU = sumU / totalSamples;
float avgV = sumV / totalSamples;
const int chromaShiftX = 1;
int uvI = outerI >> chromaShiftX;
int uvJ = outerJ + bJ;
if (state.yuv.channelBytes > 1) {
uint16_t * pU = (uint16_t *)&uPlane[(uvI * 2) + (uvJ * uRowBytes)];
*pU = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgU);
uint16_t * pV = (uint16_t *)&vPlane[(uvI * 2) + (uvJ * vRowBytes)];
*pV = (uint16_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgV);
} else {
uPlane[uvI + (uvJ * uRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgU);
vPlane[uvI + (uvJ * vRowBytes)] = (uint8_t)avifYUVColorSpaceInfoUVToUNorm(&state.yuv, avgV);
}
}
}
}
}
}
if (image->alphaPlane && image->alphaRowBytes) {
avifAlphaParams params;
params.width = image->width;
params.height = image->height;
params.dstDepth = image->depth;
params.dstPlane = image->alphaPlane;
params.dstRowBytes = image->alphaRowBytes;
params.dstOffsetBytes = 0;
params.dstPixelBytes = state.yuv.channelBytes;
if (avifRGBFormatHasAlpha(rgb->format) && !rgb->ignoreAlpha) {
params.srcDepth = rgb->depth;
params.srcPlane = rgb->pixels;
params.srcRowBytes = rgb->rowBytes;
params.srcOffsetBytes = state.rgb.offsetBytesA;
params.srcPixelBytes = state.rgb.pixelBytes;
avifReformatAlpha(¶ms);
} else {
// libyuv does not fill alpha when converting from RGB to YUV so
// fill it regardless of the value of convertedWithLibYUV.
avifFillAlpha(¶ms);
}
}
return AVIF_RESULT_OK;
}
// Allocates and fills look-up tables for going from YUV limited/full unorm -> full range RGB FP32.
// Review this when implementing YCgCo limited range support.
static avifBool avifCreateYUVToRGBLookUpTables(float ** unormFloatTableY, float ** unormFloatTableUV, uint32_t depth, const avifReformatState * state)
{
const size_t cpCount = (size_t)1 << depth;
assert(unormFloatTableY);
*unormFloatTableY = (float *)avifAlloc(cpCount * sizeof(float));
AVIF_CHECK(*unormFloatTableY);
for (uint32_t cp = 0; cp < cpCount; ++cp) {
(*unormFloatTableY)[cp] = ((float)cp - state->yuv.biasY) / state->yuv.rangeY;
}
if (unormFloatTableUV) {
if (state->yuv.mode == AVIF_REFORMAT_MODE_IDENTITY) {
// Just reuse the luma table since the chroma values are the same.
*unormFloatTableUV = *unormFloatTableY;
} else {
*unormFloatTableUV = (float *)avifAlloc(cpCount * sizeof(float));
if (!*unormFloatTableUV) {
avifFree(*unormFloatTableY);
*unormFloatTableY = NULL;
return AVIF_FALSE;
}
for (uint32_t cp = 0; cp < cpCount; ++cp) {
(*unormFloatTableUV)[cp] = ((float)cp - state->yuv.biasUV) / state->yuv.rangeUV;
}
}
}
return AVIF_TRUE;
}
// Frees look-up tables allocated with avifCreateYUVToRGBLookUpTables().
static void avifFreeYUVToRGBLookUpTables(float ** unormFloatTableY, float ** unormFloatTableUV)
{
if (unormFloatTableUV) {
if (*unormFloatTableUV != *unormFloatTableY) {
avifFree(*unormFloatTableUV);
}
*unormFloatTableUV = NULL;
}
avifFree(*unormFloatTableY);
*unormFloatTableY = NULL;
}
#define RGB565(R, G, B) ((uint16_t)(((B) >> 3) | (((G) >> 2) << 5) | (((R) >> 3) << 11)))
static void avifStoreRGB8Pixel(avifRGBFormat format, uint8_t R, uint8_t G, uint8_t B, uint8_t * ptrR, uint8_t * ptrG, uint8_t * ptrB)
{
if (format == AVIF_RGB_FORMAT_RGB_565) {
// References for RGB565 color conversion:
// * https://docs.microsoft.com/en-us/windows/win32/directshow/working-with-16-bit-rgb
// * https://chromium.googlesource.com/libyuv/libyuv/+/9892d70c965678381d2a70a1c9002d1cf136ee78/source/row_common.cc#2362
*(uint16_t *)ptrR = RGB565(R, G, B);
return;
}
*ptrR = R;
*ptrG = G;
*ptrB = B;
}
static void avifGetRGB565(const uint8_t * ptrR, uint8_t * R, uint8_t * G, uint8_t * B)
{
// References for RGB565 color conversion:
// * https://docs.microsoft.com/en-us/windows/win32/directshow/working-with-16-bit-rgb
// * https://chromium.googlesource.com/libyuv/libyuv/+/331c361581896292fb46c8c6905e41262b7ca95f/source/row_common.cc#185
const uint16_t rgb656 = ((const uint16_t *)ptrR)[0];
const uint16_t r5 = (rgb656 & 0xF800) >> 11;
const uint16_t g6 = (rgb656 & 0x07E0) >> 5;
const uint16_t b5 = (rgb656 & 0x001F);
*R = (uint8_t)((r5 << 3) | (r5 >> 2));
*G = (uint8_t)((g6 << 2) | (g6 >> 4));
*B = (uint8_t)((b5 << 3) | (b5 >> 2));
}
// Note: This function handles alpha (un)multiply.
static avifResult avifImageYUVAnyToRGBAnySlow(const avifImage * image,
avifRGBImage * rgb,
const avifReformatState * state,
avifAlphaMultiplyMode alphaMultiplyMode)
{
// Aliases for some state
const float kr = state->yuv.kr;
const float kg = state->yuv.kg;
const float kb = state->yuv.kb;
float * unormFloatTableY = NULL;
float * unormFloatTableUV = NULL;
AVIF_CHECKERR(avifCreateYUVToRGBLookUpTables(&unormFloatTableY, &unormFloatTableUV, image->depth, state), AVIF_RESULT_OUT_OF_MEMORY);
const uint32_t yuvChannelBytes = state->yuv.channelBytes;
const uint32_t rgbPixelBytes = state->rgb.pixelBytes;
// Aliases for plane data
const uint8_t * yPlane = image->yuvPlanes[AVIF_CHAN_Y];
const uint8_t * uPlane = image->yuvPlanes[AVIF_CHAN_U];
const uint8_t * vPlane = image->yuvPlanes[AVIF_CHAN_V];
const uint8_t * aPlane = image->alphaPlane;
const uint32_t yRowBytes = image->yuvRowBytes[AVIF_CHAN_Y];
const uint32_t uRowBytes = image->yuvRowBytes[AVIF_CHAN_U];
const uint32_t vRowBytes = image->yuvRowBytes[AVIF_CHAN_V];
const uint32_t aRowBytes = image->alphaRowBytes;
// Various observations and limits
const avifBool hasColor = (uPlane && vPlane && (image->yuvFormat != AVIF_PIXEL_FORMAT_YUV400));
const uint16_t yuvMaxChannel = (uint16_t)state->yuv.maxChannel;
const float rgbMaxChannelF = state->rgb.maxChannelF;
// If toRGBAlphaMode is active (not no-op), assert that the alpha plane is present. The end of
// the avifPrepareReformatState() function should ensure this, but this assert makes it clear
// to clang's analyzer.
assert((alphaMultiplyMode == AVIF_ALPHA_MULTIPLY_MODE_NO_OP) || aPlane);
for (uint32_t j = 0; j < image->height; ++j) {
// uvJ is used only when hasColor is true.
const uint32_t uvJ = hasColor ? (j >> state->yuv.formatInfo.chromaShiftY) : 0;
const uint8_t * ptrY8 = &yPlane[j * yRowBytes];
const uint8_t * ptrU8 = uPlane ? &uPlane[(uvJ * uRowBytes)] : NULL;
const uint8_t * ptrV8 = vPlane ? &vPlane[(uvJ * vRowBytes)] : NULL;
const uint8_t * ptrA8 = aPlane ? &aPlane[j * aRowBytes] : NULL;
const uint16_t * ptrY16 = (const uint16_t *)ptrY8;
const uint16_t * ptrU16 = (const uint16_t *)ptrU8;
const uint16_t * ptrV16 = (const uint16_t *)ptrV8;
const uint16_t * ptrA16 = (const uint16_t *)ptrA8;
uint8_t * ptrR = &rgb->pixels[state->rgb.offsetBytesR + (j * rgb->rowBytes)];
uint8_t * ptrG = &rgb->pixels[state->rgb.offsetBytesG + (j * rgb->rowBytes)];
uint8_t * ptrB = &rgb->pixels[state->rgb.offsetBytesB + (j * rgb->rowBytes)];
for (uint32_t i = 0; i < image->width; ++i) {
float Y, Cb = 0.5f, Cr = 0.5f;
// Calculate Y
uint16_t unormY;
if (image->depth == 8) {
unormY = ptrY8[i];
} else {
// clamp incoming data to protect against bad LUT lookups
unormY = AVIF_MIN(ptrY16[i], yuvMaxChannel);
}
Y = unormFloatTableY[unormY];
// Calculate Cb and Cr
if (hasColor) {
const uint32_t uvI = i >> state->yuv.formatInfo.chromaShiftX;
if (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV444) {
uint16_t unormU, unormV;
if (image->depth == 8) {
unormU = ptrU8[uvI];
unormV = ptrV8[uvI];
} else {
// clamp incoming data to protect against bad LUT lookups
unormU = AVIF_MIN(ptrU16[uvI], yuvMaxChannel);
unormV = AVIF_MIN(ptrV16[uvI], yuvMaxChannel);
}
Cb = unormFloatTableUV[unormU];
Cr = unormFloatTableUV[unormV];
} else {
// Upsample to 444:
//
// * * * *
// A B
// * 1 2 *
//
// * 3 4 *
// C D
// * * * *
//
// When converting from YUV420 to RGB, for any given "high-resolution" RGB
// coordinate (1,2,3,4,*), there are up to four "low-resolution" UV samples
// (A,B,C,D) that are "nearest" to the pixel. For RGB pixel #1, A is the closest
// UV sample, B and C are "adjacent" to it on the same row and column, and D is
// the diagonal. For RGB pixel 3, C is the closest UV sample, A and D are
// adjacent, and B is the diagonal. Sometimes the adjacent pixel on the same row
// is to the left or right, and sometimes the adjacent pixel on the same column
// is up or down. For any edge or corner, there might only be only one or two
// samples nearby, so they'll be duplicated.
//
// The following code attempts to find all four nearest UV samples and put them
// in the following unormU and unormV grid as follows:
//
// unorm[0][0] = closest ( weights: bilinear: 9/16, nearest: 1 )
// unorm[1][0] = adjacent col ( weights: bilinear: 3/16, nearest: 0 )
// unorm[0][1] = adjacent row ( weights: bilinear: 3/16, nearest: 0 )
// unorm[1][1] = diagonal ( weights: bilinear: 1/16, nearest: 0 )
//
// It then weights them according to the requested upsampling set in avifRGBImage.
uint16_t unormU[2][2], unormV[2][2];
// How many bytes to add to a uint8_t pointer index to get to the adjacent (lesser) sample in a given direction
int uAdjCol, vAdjCol, uAdjRow, vAdjRow;
if ((i == 0) || ((i == (image->width - 1)) && ((i % 2) != 0))) {
uAdjCol = 0;
vAdjCol = 0;
} else {
if ((i % 2) != 0) {
uAdjCol = yuvChannelBytes;
vAdjCol = yuvChannelBytes;
} else {
uAdjCol = -1 * yuvChannelBytes;
vAdjCol = -1 * yuvChannelBytes;
}
}
// For YUV422, uvJ will always be a fresh value (always corresponds to j), so
// we'll simply duplicate the sample as if we were on the top or bottom row and
// it'll behave as plain old linear (1D) upsampling, which is all we want.
if ((j == 0) || ((j == (image->height - 1)) && ((j % 2) != 0)) || (image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422)) {
uAdjRow = 0;
vAdjRow = 0;
} else {
if ((j % 2) != 0) {
uAdjRow = (int)uRowBytes;
vAdjRow = (int)vRowBytes;
} else {
uAdjRow = -1 * (int)uRowBytes;
vAdjRow = -1 * (int)vRowBytes;
}
}
if (image->depth == 8) {
unormU[0][0] = uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes)];
unormV[0][0] = vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes)];
unormU[1][0] = uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjCol];
unormV[1][0] = vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjCol];
unormU[0][1] = uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjRow];
unormV[0][1] = vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjRow];
unormU[1][1] = uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjCol + uAdjRow];
unormV[1][1] = vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjCol + vAdjRow];
} else {
unormU[0][0] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes)]);
unormV[0][0] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes)]);
unormU[1][0] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjCol]);
unormV[1][0] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjCol]);
unormU[0][1] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjRow]);
unormV[0][1] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjRow]);
unormU[1][1] = *((const uint16_t *)&uPlane[(uvJ * uRowBytes) + (uvI * yuvChannelBytes) + uAdjCol + uAdjRow]);
unormV[1][1] = *((const uint16_t *)&vPlane[(uvJ * vRowBytes) + (uvI * yuvChannelBytes) + vAdjCol + vAdjRow]);
// clamp incoming data to protect against bad LUT lookups
for (int bJ = 0; bJ < 2; ++bJ) {
for (int bI = 0; bI < 2; ++bI) {
unormU[bI][bJ] = AVIF_MIN(unormU[bI][bJ], yuvMaxChannel);
unormV[bI][bJ] = AVIF_MIN(unormV[bI][bJ], yuvMaxChannel);
}
}
}
if ((rgb->chromaUpsampling == AVIF_CHROMA_UPSAMPLING_FASTEST) ||
(rgb->chromaUpsampling == AVIF_CHROMA_UPSAMPLING_NEAREST)) {
// Nearest neighbor; ignore all UVs but the closest one
Cb = unormFloatTableUV[unormU[0][0]];
Cr = unormFloatTableUV[unormV[0][0]];
} else {
// Bilinear filtering with weights
Cb = (unormFloatTableUV[unormU[0][0]] * (9.0f / 16.0f)) + (unormFloatTableUV[unormU[1][0]] * (3.0f / 16.0f)) +
(unormFloatTableUV[unormU[0][1]] * (3.0f / 16.0f)) + (unormFloatTableUV[unormU[1][1]] * (1.0f / 16.0f));
Cr = (unormFloatTableUV[unormV[0][0]] * (9.0f / 16.0f)) + (unormFloatTableUV[unormV[1][0]] * (3.0f / 16.0f)) +
(unormFloatTableUV[unormV[0][1]] * (3.0f / 16.0f)) + (unormFloatTableUV[unormV[1][1]] * (1.0f / 16.0f));
}
}
}
float R, G, B;
if (hasColor) {
if (state->yuv.mode == AVIF_REFORMAT_MODE_IDENTITY) {
// Identity (GBR): Formulas 41,42,43 from https://www.itu.int/rec/T-REC-H.273-201612-S
G = Y;
B = Cb;
R = Cr;
} else if (state->yuv.mode == AVIF_REFORMAT_MODE_YCGCO) {
// YCgCo: Formulas 47,48,49,50 from https://www.itu.int/rec/T-REC-H.273-201612-S
const float t = Y - Cb;
G = Y + Cb;
B = t - Cr;
R = t + Cr;
#if defined(AVIF_ENABLE_EXPERIMENTAL_YCGCO_R)
} else if (state->yuv.mode == AVIF_REFORMAT_MODE_YCGCO_RE || state->yuv.mode == AVIF_REFORMAT_MODE_YCGCO_RO) {
// YCgCoRe/YCgCoRo: Formulas 62,63,64,65 from https://www.itu.int/rec/T-REC-H.273-202407-P
const int YY = unormY;
const int Cg = (int)avifRoundf(Cb * yuvMaxChannel);
const int Co = (int)avifRoundf(Cr * yuvMaxChannel);
const int t = YY - (Cg >> 1);
G = (float)AVIF_CLAMP(t + Cg, 0, state->rgb.maxChannel);
B = (float)AVIF_CLAMP(t - (Co >> 1), 0, state->rgb.maxChannel);
R = (float)AVIF_CLAMP(B + Co, 0, state->rgb.maxChannel);
G /= rgbMaxChannelF;
B /= rgbMaxChannelF;
R /= rgbMaxChannelF;
#endif
} else {
// Normal YUV
R = Y + (2 * (1 - kr)) * Cr;
B = Y + (2 * (1 - kb)) * Cb;
G = Y - ((2 * ((kr * (1 - kr) * Cr) + (kb * (1 - kb) * Cb))) / kg);
}
} else {
// Monochrome: just populate all channels with luma (state->yuv.mode is irrelevant)
R = Y;
G = Y;
B = Y;
}
float Rc = AVIF_CLAMP(R, 0.0f, 1.0f);
float Gc = AVIF_CLAMP(G, 0.0f, 1.0f);
float Bc = AVIF_CLAMP(B, 0.0f, 1.0f);
if (alphaMultiplyMode != AVIF_ALPHA_MULTIPLY_MODE_NO_OP) {
// Calculate A
uint16_t unormA;
if (image->depth == 8) {
unormA = ptrA8[i];
} else {
unormA = AVIF_MIN(ptrA16[i], yuvMaxChannel);
}
const float A = unormA / ((float)state->yuv.maxChannel);
const float Ac = AVIF_CLAMP(A, 0.0f, 1.0f);
if (alphaMultiplyMode == AVIF_ALPHA_MULTIPLY_MODE_MULTIPLY) {
if (Ac == 0.0f) {
Rc = 0.0f;
Gc = 0.0f;
Bc = 0.0f;
} else if (Ac < 1.0f) {
Rc *= Ac;
Gc *= Ac;
Bc *= Ac;
}
} else {
// alphaMultiplyMode == AVIF_ALPHA_MULTIPLY_MODE_UNMULTIPLY
if (Ac == 0.0f) {
Rc = 0.0f;
Gc = 0.0f;
Bc = 0.0f;
} else if (Ac < 1.0f) {
Rc /= Ac;
Gc /= Ac;
Bc /= Ac;
Rc = AVIF_MIN(Rc, 1.0f);
Gc = AVIF_MIN(Gc, 1.0f);
Bc = AVIF_MIN(Bc, 1.0f);
}
}
}
if (rgb->depth == 8) {
avifStoreRGB8Pixel(rgb->format,
(uint8_t)(0.5f + (Rc * rgbMaxChannelF)),
(uint8_t)(0.5f + (Gc * rgbMaxChannelF)),
(uint8_t)(0.5f + (Bc * rgbMaxChannelF)),
ptrR,
ptrG,
ptrB);
} else {
*((uint16_t *)ptrR) = (uint16_t)(0.5f + (Rc * rgbMaxChannelF));
*((uint16_t *)ptrG) = (uint16_t)(0.5f + (Gc * rgbMaxChannelF));
*((uint16_t *)ptrB) = (uint16_t)(0.5f + (Bc * rgbMaxChannelF));
}
ptrR += rgbPixelBytes;
ptrG += rgbPixelBytes;
ptrB += rgbPixelBytes;
}
}
avifFreeYUVToRGBLookUpTables(&unormFloatTableY, &unormFloatTableUV);
return AVIF_RESULT_OK;
}
static avifResult avifImageYUV16ToRGB16Color(const avifImage * image, avifRGBImage * rgb, avifReformatState * state)
{
const float kr = state->yuv.kr;
const float kg = state->yuv.kg;
const float kb = state->yuv.kb;
const uint32_t rgbPixelBytes = state->rgb.pixelBytes;
float * unormFloatTableY = NULL;
float * unormFloatTableUV = NULL;
AVIF_CHECKERR(avifCreateYUVToRGBLookUpTables(&unormFloatTableY, &unormFloatTableUV, image->depth, state), AVIF_RESULT_OUT_OF_MEMORY);
const uint16_t yuvMaxChannel = (uint16_t)state->yuv.maxChannel;
const float rgbMaxChannelF = state->rgb.maxChannelF;
for (uint32_t j = 0; j < image->height; ++j) {
const uint32_t uvJ = j >> state->yuv.formatInfo.chromaShiftY;
const uint16_t * const ptrY = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_Y][(j * image->yuvRowBytes[AVIF_CHAN_Y])];
const uint16_t * const ptrU = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_U][(uvJ * image->yuvRowBytes[AVIF_CHAN_U])];
const uint16_t * const ptrV = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_V][(uvJ * image->yuvRowBytes[AVIF_CHAN_V])];
uint8_t * ptrR = &rgb->pixels[state->rgb.offsetBytesR + (j * rgb->rowBytes)];
uint8_t * ptrG = &rgb->pixels[state->rgb.offsetBytesG + (j * rgb->rowBytes)];
uint8_t * ptrB = &rgb->pixels[state->rgb.offsetBytesB + (j * rgb->rowBytes)];
for (uint32_t i = 0; i < image->width; ++i) {
uint32_t uvI = i >> state->yuv.formatInfo.chromaShiftX;
// clamp incoming data to protect against bad LUT lookups
const uint16_t unormY = AVIF_MIN(ptrY[i], yuvMaxChannel);
const uint16_t unormU = AVIF_MIN(ptrU[uvI], yuvMaxChannel);
const uint16_t unormV = AVIF_MIN(ptrV[uvI], yuvMaxChannel);
// Convert unorm to float
const float Y = unormFloatTableY[unormY];
const float Cb = unormFloatTableUV[unormU];
const float Cr = unormFloatTableUV[unormV];
const float R = Y + (2 * (1 - kr)) * Cr;
const float B = Y + (2 * (1 - kb)) * Cb;
const float G = Y - ((2 * ((kr * (1 - kr) * Cr) + (kb * (1 - kb) * Cb))) / kg);
const float Rc = AVIF_CLAMP(R, 0.0f, 1.0f);
const float Gc = AVIF_CLAMP(G, 0.0f, 1.0f);
const float Bc = AVIF_CLAMP(B, 0.0f, 1.0f);
*((uint16_t *)ptrR) = (uint16_t)(0.5f + (Rc * rgbMaxChannelF));
*((uint16_t *)ptrG) = (uint16_t)(0.5f + (Gc * rgbMaxChannelF));
*((uint16_t *)ptrB) = (uint16_t)(0.5f + (Bc * rgbMaxChannelF));
ptrR += rgbPixelBytes;
ptrG += rgbPixelBytes;
ptrB += rgbPixelBytes;
}
}
avifFreeYUVToRGBLookUpTables(&unormFloatTableY, &unormFloatTableUV);
return AVIF_RESULT_OK;
}
static avifResult avifImageYUV16ToRGB16Mono(const avifImage * image, avifRGBImage * rgb, avifReformatState * state)
{
const float kr = state->yuv.kr;
const float kg = state->yuv.kg;
const float kb = state->yuv.kb;
const uint32_t rgbPixelBytes = state->rgb.pixelBytes;
float * unormFloatTableY = NULL;
AVIF_CHECKERR(avifCreateYUVToRGBLookUpTables(&unormFloatTableY, NULL, image->depth, state), AVIF_RESULT_OUT_OF_MEMORY);
const uint16_t maxChannel = (uint16_t)state->yuv.maxChannel;
const float maxChannelF = state->rgb.maxChannelF;
for (uint32_t j = 0; j < image->height; ++j) {
const uint16_t * const ptrY = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_Y][(j * image->yuvRowBytes[AVIF_CHAN_Y])];
uint8_t * ptrR = &rgb->pixels[state->rgb.offsetBytesR + (j * rgb->rowBytes)];
uint8_t * ptrG = &rgb->pixels[state->rgb.offsetBytesG + (j * rgb->rowBytes)];
uint8_t * ptrB = &rgb->pixels[state->rgb.offsetBytesB + (j * rgb->rowBytes)];
for (uint32_t i = 0; i < image->width; ++i) {
// clamp incoming data to protect against bad LUT lookups
const uint16_t unormY = AVIF_MIN(ptrY[i], maxChannel);
// Convert unorm to float
const float Y = unormFloatTableY[unormY];
const float Cb = 0.0f;
const float Cr = 0.0f;
const float R = Y + (2 * (1 - kr)) * Cr;
const float B = Y + (2 * (1 - kb)) * Cb;
const float G = Y - ((2 * ((kr * (1 - kr) * Cr) + (kb * (1 - kb) * Cb))) / kg);
const float Rc = AVIF_CLAMP(R, 0.0f, 1.0f);
const float Gc = AVIF_CLAMP(G, 0.0f, 1.0f);
const float Bc = AVIF_CLAMP(B, 0.0f, 1.0f);
*((uint16_t *)ptrR) = (uint16_t)(0.5f + (Rc * maxChannelF));
*((uint16_t *)ptrG) = (uint16_t)(0.5f + (Gc * maxChannelF));
*((uint16_t *)ptrB) = (uint16_t)(0.5f + (Bc * maxChannelF));
ptrR += rgbPixelBytes;
ptrG += rgbPixelBytes;
ptrB += rgbPixelBytes;
}
}
avifFreeYUVToRGBLookUpTables(&unormFloatTableY, NULL);
return AVIF_RESULT_OK;
}
static avifResult avifImageYUV16ToRGB8Color(const avifImage * image, avifRGBImage * rgb, avifReformatState * state)
{
const float kr = state->yuv.kr;
const float kg = state->yuv.kg;
const float kb = state->yuv.kb;
const uint32_t rgbPixelBytes = state->rgb.pixelBytes;
float * unormFloatTableY = NULL;
float * unormFloatTableUV = NULL;
AVIF_CHECKERR(avifCreateYUVToRGBLookUpTables(&unormFloatTableY, &unormFloatTableUV, image->depth, state), AVIF_RESULT_OUT_OF_MEMORY);
const uint16_t yuvMaxChannel = (uint16_t)state->yuv.maxChannel;
const float rgbMaxChannelF = state->rgb.maxChannelF;
for (uint32_t j = 0; j < image->height; ++j) {
const uint32_t uvJ = j >> state->yuv.formatInfo.chromaShiftY;
const uint16_t * const ptrY = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_Y][(j * image->yuvRowBytes[AVIF_CHAN_Y])];
const uint16_t * const ptrU = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_U][(uvJ * image->yuvRowBytes[AVIF_CHAN_U])];
const uint16_t * const ptrV = (uint16_t *)&image->yuvPlanes[AVIF_CHAN_V][(uvJ * image->yuvRowBytes[AVIF_CHAN_V])];
uint8_t * ptrR = &rgb->pixels[state->rgb.offsetBytesR + (j * rgb->rowBytes)];
uint8_t * ptrG = &rgb->pixels[state->rgb.offsetBytesG + (j * rgb->rowBytes)];
uint8_t * ptrB = &rgb->pixels[state->rgb.offsetBytesB + (j * rgb->rowBytes)];
for (uint32_t i = 0; i < image->width; ++i) {
uint32_t uvI = i >> state->yuv.formatInfo.chromaShiftX;
// clamp incoming data to protect against bad LUT lookups
const uint16_t unormY = AVIF_MIN(ptrY[i], yuvMaxChannel);
const uint16_t unormU = AVIF_MIN(ptrU[uvI], yuvMaxChannel);
const uint16_t unormV = AVIF_MIN(ptrV[uvI], yuvMaxChannel);
// Convert unorm to float
const float Y = unormFloatTableY[unormY];
const float Cb = unormFloatTableUV[unormU];
const float Cr = unormFloatTableUV[unormV];
const float R = Y + (2 * (1 - kr)) * Cr;
const float B = Y + (2 * (1 - kb)) * Cb;
const float G = Y - ((2 * ((kr * (1 - kr) * Cr) + (kb * (1 - kb) * Cb))) / kg);
const float Rc = AVIF_CLAMP(R, 0.0f, 1.0f);