-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathccap_convert_avx2.cpp
More file actions
1548 lines (1285 loc) · 65.5 KB
/
Copy pathccap_convert_avx2.cpp
File metadata and controls
1548 lines (1285 loc) · 65.5 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
/**
* @file ccap_convert_avx2.cpp
* @author wysaid (this@wysaid.org)
* @date 2025-05
*
*/
#include "ccap_convert_avx2.h"
#include <cassert>
#include <cstring>
#if ENABLE_AVX2_IMP
/// On macOS, use Accelerate.framework directly, no separate implementation needed for now
// Add target attribute support for GCC/MinGW
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
#define AVX2_TARGET __attribute__((target("avx2,fma")))
#else
#define AVX2_TARGET
#endif
#include <immintrin.h> // AVX2
#if defined(_MSC_VER)
#include <intrin.h>
inline bool hasAVX2_() {
int cpuInfo[4];
__cpuid(cpuInfo, 1);
bool osxsave = (cpuInfo[2] & (1 << 27)) != 0;
bool avx = (cpuInfo[2] & (1 << 28)) != 0;
if (!(osxsave && avx)) return false;
// Check XGETBV to confirm OS supports YMM
unsigned long long xcrFeatureMask = _xgetbv(0);
if ((xcrFeatureMask & 0x6) != 0x6) return false;
// Check AVX2
__cpuid(cpuInfo, 7);
return (cpuInfo[1] & (1 << 5)) != 0;
}
#elif defined(__GNUC__) || defined(__clang__)
#include <cpuid.h>
inline bool hasAVX2_() {
unsigned int eax, ebx, ecx, edx;
// 1. Check basic CPUID support
if (!__get_cpuid(0, &eax, &ebx, &ecx, &edx)) return false;
if (eax < 1) return false; // Need support for CPUID function 1
// 2. Check AVX and OSXSAVE
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) return false;
bool osxsave = (ecx & (1 << 27)) != 0;
bool avx = (ecx & (1 << 28)) != 0;
if (!(osxsave && avx)) return false;
// 3. Check XGETBV to confirm OS supports YMM
// Only safe to call XGETBV when OSXSAVE is true
unsigned int xcr0_lo = 0, xcr0_hi = 0;
asm volatile("xgetbv"
: "=a"(xcr0_lo), "=d"(xcr0_hi)
: "c"(0));
if ((xcr0_lo & 0x6) != 0x6) return false; // Both XMM and YMM states must be saved
// 4. Check extended feature support
if (!__get_cpuid(0, &eax, &ebx, &ecx, &edx)) return false;
if (eax < 7) return false; // Need support for CPUID function 7
// 5. Check AVX2
if (!__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx)) return false;
return (ebx & (1 << 5)) != 0; // AVX2 bit
}
#else
inline bool hasAVX2_() { return false; }
#endif
#endif
namespace ccap {
bool sEnableAVX2 = true;
bool enableAVX2(bool enable) {
sEnableAVX2 = enable;
return hasAVX2(); // Re-check AVX2 support
}
bool hasAVX2() {
#if ENABLE_AVX2_IMP
static bool s_hasAVX2 = hasAVX2_();
return s_hasAVX2;
#else
return false;
#endif
}
bool canUseAVX2() {
return hasAVX2() && sEnableAVX2;
}
const char* getAVX2SupportInfo() {
#if ENABLE_AVX2_IMP
static const char* info = nullptr;
if (info == nullptr) {
if (hasAVX2()) {
if (sEnableAVX2) {
info = "AVX2: Hardware supported and enabled";
} else {
info = "AVX2: Hardware supported but disabled by software";
}
} else {
info = "AVX2: Not supported by hardware or OS";
}
}
return info;
#else
return "AVX2: Disabled at compile time";
#endif
}
#if ENABLE_AVX2_IMP
template <int inputChannels, int outputChannels, int swapRB>
AVX2_TARGET void colorShuffle_avx2(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width,
int height) { // Implement a general colorShuffle, accelerated by AVX2
static_assert((inputChannels == 3 || inputChannels == 4) && (outputChannels == 3 || outputChannels == 4),
"inputChannels and outputChannels must be 3 or 4");
static_assert(inputChannels != outputChannels || swapRB, "swapRB must be true when inputChannels == outputChannels");
if (height < 0) {
height = -height;
dst = dst + (height - 1) * dstStride;
dstStride = -dstStride;
}
alignas(32) uint8_t shuffleData[32];
constexpr uint32_t inputPatchSize = inputChannels == 4 ? 8 : (inputChannels == 3 && outputChannels == 3 ? 5 : 10);
constexpr uint32_t outputPatchSize = outputChannels == 4 ? 8 : (inputChannels == 3 && outputChannels == 3 ? 5 : 10);
constexpr uint32_t patchSize = inputPatchSize < outputPatchSize ? inputPatchSize : outputPatchSize;
for (int i = 0; i < patchSize; ++i) {
auto idx1 = i * outputChannels;
auto idx2 = i * inputChannels;
if constexpr (swapRB) {
shuffleData[idx1] = 2 + idx2; // B
shuffleData[idx1 + 1] = 1 + idx2; // G
shuffleData[idx1 + 2] = 0 + idx2; // R
} else {
shuffleData[idx1] = 0 + idx2; // R
shuffleData[idx1 + 1] = 1 + idx2; // G
shuffleData[idx1 + 2] = 2 + idx2; // B
}
if constexpr (outputChannels == 4) {
if constexpr (inputChannels == 4)
shuffleData[idx1 + 3] = idx2 + 3; // A is always at the end, other cases not supported for now.
else
shuffleData[idx1 + 3] = 0xFF; // no alpha
}
}
#if 0
// Print shuffleData for debugging
printf("shuffleData: \n");
for (int i = 0; i < patchSize; ++i)
{
for (int j = 0; j < outputChannels; ++j)
{
printf("%d ", shuffleData[i * outputChannels + j]);
}
printf("\n");
}
printf("\n");
#endif
__m256i shuffle256; // = _mm256_load_si256((const __m256i*)shuffleData);
__m128i shuffle128; // = _mm_load_si128((__m128i*)shuffleData);
if constexpr (inputChannels == 4 && outputChannels == 4) { // Only 4 -> 4 can use 256-bit AVX2 instructions
shuffle256 = _mm256_load_si256((const __m256i*)shuffleData);
} else {
shuffle128 = _mm_load_si128((__m128i*)shuffleData);
}
for (int y = 0; y < height; ++y) {
const uint8_t* srcRow = src + y * srcStride;
uint8_t* dstRow = dst + y * dstStride;
uint32_t x = 0;
while (x + patchSize <= (uint32_t)width) {
// _mm256_shuffle_epi8 can’t move these bytes across 16-byte lanes of the vector.
// @see issue <https://stackoverflow.com/questions/77149094/how-to-use-mm256-shuffle-epi8-to-order-elements>
if constexpr (outputChannels == 4 && inputChannels == 3) { // 3 -> 4, need to split channels
/// Split into 12 + 12, reading 24 bytes each time
__m128i pixels_lo = _mm_loadu_si128((__m128i*)(srcRow + x * inputChannels));
__m128i pixels_hi = _mm_loadu_si128((__m128i*)(srcRow + x * inputChannels + 12));
__m128i result_lo = _mm_shuffle_epi8(pixels_lo, shuffle128);
__m128i result_hi = _mm_shuffle_epi8(pixels_hi, shuffle128);
// Create alpha channel mask, set the 4th byte (alpha channel) of each pixel to 0xFF
// For RGBA format in little-endian, need to set alpha at correct position
__m128i alpha_mask = _mm_set1_epi32(0xFF000000);
result_lo = _mm_or_si128(result_lo, alpha_mask);
result_hi = _mm_or_si128(result_hi, alpha_mask);
// outputChannels is 4, patchSize is 8, align to 4 x 8 bytes
_mm_storeu_si128((__m128i*)(dstRow + x * outputChannels), result_lo);
_mm_storeu_si128((__m128i*)(dstRow + x * outputChannels + 16), result_hi);
} else if constexpr (outputChannels == 3 && inputChannels == 4) { // 4 -> 3
/// Split into 16 + 16, reading 32 bytes each time
__m128i pixels_lo = _mm_load_si128((__m128i*)(srcRow + x * inputChannels));
__m128i pixels_hi = _mm_load_si128((__m128i*)(srcRow + x * inputChannels + 16));
__m128i result_lo = _mm_shuffle_epi8(pixels_lo, shuffle128); // Only the first 12 bytes are useful
__m128i result_hi = _mm_shuffle_epi8(pixels_hi, shuffle128); // Only the first 12 bytes are useful
_mm_storeu_si128((__m128i*)(dstRow + x * outputChannels), result_lo); // Write 16 bytes, but only the first 12 bytes are useful
alignas(16) uint8_t remainBuffer[16];
_mm_store_si128((__m128i*)remainBuffer, result_hi); // Temporarily store, 16 bytes
memcpy(dstRow + x * outputChannels + 12, remainBuffer, 12); // Manual alignment, overwrite extra 4 bytes, fill remaining 12 bytes, exactly 24 bytes
} else if constexpr (inputChannels == 3 && outputChannels == 3) { // 3 -> 3
/// Process 5 pixels at a time (15 bytes), reading 16 bytes each time
__m128i pixels = _mm_loadu_si128((__m128i*)(srcRow + x * inputChannels));
__m128i result = _mm_shuffle_epi8(pixels, shuffle128); // Only the first 15 bytes are useful
_mm_storeu_si128((__m128i*)(dstRow + x * outputChannels), result); // Write 16 bytes, but only the first 15 bytes are useful
} else { // 4 -> 4
__m256i pixels = _mm256_loadu_si256((const __m256i*)(srcRow + x * inputChannels));
__m256i result = _mm256_shuffle_epi8(pixels, shuffle256);
_mm256_storeu_si256((__m256i*)(dstRow + x * outputChannels), result);
}
x += patchSize;
}
// Handle remaining pixels
for (; x < (uint32_t)width; ++x) {
for (int c = 0; c < outputChannels; ++c) {
if (inputChannels == 3 && c == 3) {
dstRow[x * outputChannels + c] = 0xFF; // fill alpha
} else {
dstRow[x * outputChannels + c] = srcRow[x * inputChannels + shuffleData[c]];
assert(shuffleData[c] <= 3);
}
}
}
}
}
template void colorShuffle_avx2<4, 4, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
template void colorShuffle_avx2<4, 3, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
template void colorShuffle_avx2<4, 3, false>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
template void colorShuffle_avx2<3, 4, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
template void colorShuffle_avx2<3, 4, false>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
template void colorShuffle_avx2<3, 3, true>(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height);
inline void getYuvToRgbCoefficients(bool isBT601, bool isFullRange, int& cy, int& cr, int& cgu, int& cgv, int& cb) {
if (isBT601) {
if (isFullRange) { // BT.601 Full Range: 256, 351, 86, 179, 443 (divided by 4)
cy = 64;
cr = 88;
cgu = 22;
cgv = 45;
cb = 111;
} else { // BT.601 Video Range: 298, 409, 100, 208, 516 (divided by 4)
cy = 75;
cr = 102;
cgu = 25;
cgv = 52;
cb = 129;
}
} else {
if (isFullRange) { // BT.709 Full Range: 256, 403, 48, 120, 475 (divided by 4)
cy = 64;
cr = 101;
cgu = 12;
cgv = 30;
cb = 119;
} else { // BT.709 Video Range: 298, 459, 55, 136, 541 (divided by 4)
cy = 75;
cr = 115;
cgu = 14;
cgv = 34;
cb = 135;
}
}
}
template <bool isBGRA, bool isFullRange>
AVX2_TARGET void nv12ToRgbaColor_avx2_imp(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride,
int width, int height, bool is601) {
if (height < 0) {
height = -height;
dst = dst + (height - 1) * dstStride;
dstStride = -dstStride;
}
// Select coefficients based on flags
int cy, cr, cgu, cgv, cb;
getYuvToRgbCoefficients(is601, isFullRange, cy, cr, cgu, cgv, cb);
__m256i c_y = _mm256_set1_epi16(cy);
__m256i c_r = _mm256_set1_epi16(cr);
__m256i c_gu = _mm256_set1_epi16(cgu);
__m256i c_gv = _mm256_set1_epi16(cgv);
__m256i c_b = _mm256_set1_epi16(cb);
__m256i c128 = _mm256_set1_epi16(128);
__m128i a8 = _mm_set1_epi8((char)255);
YuvToRgbFunc convertFunc = getYuvToRgbFunc(is601, isFullRange);
for (int y = 0; y < height; ++y) {
const uint8_t* yRow = srcY + y * srcYStride;
const uint8_t* uvRow = srcUV + (y / 2) * srcUVStride;
uint8_t* dstRow = dst + y * dstStride;
int x = 0;
for (; x + 16 <= width; x += 16) {
// 1. Load 16 Y values
__m128i y_vals = _mm_loadu_si128((const __m128i*)(yRow + x));
// 2. Load 16 bytes UV (8 pairs)
__m128i uv_vals = _mm_loadu_si128((const __m128i*)(uvRow + x));
// 3. Split U/V
__m128i u8 = _mm_and_si128(uv_vals, _mm_set1_epi16(0x00FF));
__m128i v8 = _mm_srli_epi16(uv_vals, 8);
// 4. Pack into 8-byte U/V
u8 = _mm_packus_epi16(u8, _mm_setzero_si128());
v8 = _mm_packus_epi16(v8, _mm_setzero_si128());
// 5. Expand each U/V to 2 pixels
__m128i u_lo = _mm_unpacklo_epi8(u8, u8);
__m128i v_lo = _mm_unpacklo_epi8(v8, v8);
// 6. Combine into 16 bytes
__m256i u_16 = _mm256_cvtepu8_epi16(u_lo);
__m256i v_16 = _mm256_cvtepu8_epi16(v_lo);
__m256i y_16 = _mm256_cvtepu8_epi16(y_vals);
// 7. Dynamic offset calculation
// UV always subtract 128 in all cases
u_16 = _mm256_sub_epi16(u_16, c128);
v_16 = _mm256_sub_epi16(v_16, c128);
// Y offset depends on range type
if constexpr (!isFullRange) { // Video Range: Y - 16
y_16 = _mm256_sub_epi16(y_16, _mm256_set1_epi16(16));
}
// Full Range: Y remains unchanged
__m256i y_scaled = _mm256_mullo_epi16(y_16, c_y);
__m256i r = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(v_16, c_r));
r = _mm256_add_epi16(r, _mm256_set1_epi16(32));
r = _mm256_srai_epi16(r, 6);
__m256i g = _mm256_sub_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_gu));
g = _mm256_sub_epi16(g, _mm256_mullo_epi16(v_16, c_gv));
g = _mm256_add_epi16(g, _mm256_set1_epi16(32));
g = _mm256_srai_epi16(g, 6);
__m256i b = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_b));
b = _mm256_add_epi16(b, _mm256_set1_epi16(32));
b = _mm256_srai_epi16(b, 6);
// Clamp to 0~255
__m256i zero = _mm256_setzero_si256();
__m256i maxv = _mm256_set1_epi16(255);
r = _mm256_max_epi16(zero, _mm256_min_epi16(r, maxv));
g = _mm256_max_epi16(zero, _mm256_min_epi16(g, maxv));
b = _mm256_max_epi16(zero, _mm256_min_epi16(b, maxv));
// First compress 16x16bit to 16x8bit, only use lower 128 bits
__m128i r8 = _mm_packus_epi16(_mm256_castsi256_si128(r), _mm256_extracti128_si256(r, 1));
__m128i g8 = _mm_packus_epi16(_mm256_castsi256_si128(g), _mm256_extracti128_si256(g, 1));
__m128i b8 = _mm_packus_epi16(_mm256_castsi256_si128(b), _mm256_extracti128_si256(b, 1));
if constexpr (isBGRA) { // Interleave pack in BGRA order
__m128i bg0 = _mm_unpacklo_epi8(b8, g8); // B0 G0 B1 G1 ...
__m128i ra0 = _mm_unpacklo_epi8(r8, a8); // R0 A0 R1 A1 ...
__m128i bgra0 = _mm_unpacklo_epi16(bg0, ra0); // B0 G0 R0 A0 ...
__m128i bgra1 = _mm_unpackhi_epi16(bg0, ra0); // B4 G4 R4 A4 ...
__m128i bg1 = _mm_unpackhi_epi8(b8, g8);
__m128i ra1 = _mm_unpackhi_epi8(r8, a8);
__m128i bgra2 = _mm_unpacklo_epi16(bg1, ra1);
__m128i bgra3 = _mm_unpackhi_epi16(bg1, ra1);
// Write 16*4=64 bytes, exactly 16 pixels
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 0), bgra0);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 16), bgra1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 32), bgra2);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 48), bgra3);
} else { // to RGBA
// Interleave pack in RGBA order
__m128i rg0 = _mm_unpacklo_epi8(r8, g8); // R0 G0 R1 G1 ...
__m128i ba0 = _mm_unpacklo_epi8(b8, a8); // B0 A0 B1 A1 ...
__m128i rgba0 = _mm_unpacklo_epi16(rg0, ba0); // R0 G0 B0 A0 ...
__m128i rgba1 = _mm_unpackhi_epi16(rg0, ba0); // R4 G4 B4 A4 ...
__m128i rg1 = _mm_unpackhi_epi8(r8, g8);
__m128i ba1 = _mm_unpackhi_epi8(b8, a8);
__m128i rgba2 = _mm_unpacklo_epi16(rg1, ba1);
__m128i rgba3 = _mm_unpackhi_epi16(rg1, ba1);
// Write 16*4=64 bytes, exactly 16 pixels
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 0), rgba0);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 16), rgba1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 32), rgba2);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 48), rgba3);
}
}
for (; x < width; x += 2) {
int y0 = yRow[x];
int y1 = yRow[x + 1];
int u = uvRow[x];
int v = uvRow[x + 1];
int r0, g0, b0, r1, g1, b1;
convertFunc(y0, u, v, r0, g0, b0);
convertFunc(y1, u, v, r1, g1, b1);
if constexpr (isBGRA) {
dstRow[x * 4 + 0] = b0;
dstRow[x * 4 + 1] = g0;
dstRow[x * 4 + 2] = r0;
dstRow[x * 4 + 3] = 255;
dstRow[(x + 1) * 4 + 0] = b1;
dstRow[(x + 1) * 4 + 1] = g1;
dstRow[(x + 1) * 4 + 2] = r1;
dstRow[(x + 1) * 4 + 3] = 255;
} else {
dstRow[x * 4 + 0] = r0;
dstRow[x * 4 + 1] = g0;
dstRow[x * 4 + 2] = b0;
dstRow[x * 4 + 3] = 255;
dstRow[(x + 1) * 4 + 0] = r1;
dstRow[(x + 1) * 4 + 1] = g1;
dstRow[(x + 1) * 4 + 2] = b1;
dstRow[(x + 1) * 4 + 3] = 255;
}
}
}
}
template <bool isBGR, bool isFullRange>
AVX2_TARGET void _nv12ToRgbColor_avx2_imp(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride,
int width, int height, bool is601) {
if (height < 0) {
height = -height;
dst = dst + (height - 1) * dstStride;
dstStride = -dstStride;
}
// Select coefficients based on flags
int cy, cr, cgu, cgv, cb;
getYuvToRgbCoefficients(is601, isFullRange, cy, cr, cgu, cgv, cb);
__m256i c_y = _mm256_set1_epi16(cy);
__m256i c_r = _mm256_set1_epi16(cr);
__m256i c_gu = _mm256_set1_epi16(cgu);
__m256i c_gv = _mm256_set1_epi16(cgv);
__m256i c_b = _mm256_set1_epi16(cb);
__m256i c128 = _mm256_set1_epi16(128);
YuvToRgbFunc convertFunc = getYuvToRgbFunc(is601, isFullRange);
for (int y = 0; y < height; ++y) {
const uint8_t* yRow = srcY + y * srcYStride;
const uint8_t* uvRow = srcUV + (y / 2) * srcUVStride;
uint8_t* dstRow = dst + y * dstStride;
int x = 0;
for (; x + 16 <= width; x += 16) {
// 1. Load 16 Y values
__m128i y_vals = _mm_loadu_si128((const __m128i*)(yRow + x));
// 2. Load 16 bytes UV (8 pairs)
__m128i uv_vals = _mm_loadu_si128((const __m128i*)(uvRow + x));
// 3. Split U/V
__m128i u8 = _mm_and_si128(uv_vals, _mm_set1_epi16(0x00FF)); // U: 0,2,4...
__m128i v8 = _mm_srli_epi16(uv_vals, 8); // V: 1,3,5...
// 4. Pack into 8-byte U/V
u8 = _mm_packus_epi16(u8, _mm_setzero_si128()); // Lower 8 bytes are U
v8 = _mm_packus_epi16(v8, _mm_setzero_si128()); // Lower 8 bytes are V
// 5. Expand each U/V to 2 pixels
__m128i u_lo = _mm_unpacklo_epi8(u8, u8); // U0,U0,U1,U1,...
__m128i v_lo = _mm_unpacklo_epi8(v8, v8); // V0,V0,V1,V1,...
// 6. Combine into 16 bytes
__m256i u_16 = _mm256_cvtepu8_epi16(u_lo);
__m256i v_16 = _mm256_cvtepu8_epi16(v_lo);
__m256i y_16 = _mm256_cvtepu8_epi16(y_vals);
// 7. Dynamic offset calculation
// UV always subtract 128 in all cases
u_16 = _mm256_sub_epi16(u_16, c128);
v_16 = _mm256_sub_epi16(v_16, c128);
// Y offset depends on range type
if constexpr (!isFullRange) { // Video Range: Y - 16
y_16 = _mm256_sub_epi16(y_16, _mm256_set1_epi16(16));
}
// Full Range: Y remains unchanged
__m256i y_scaled = _mm256_mullo_epi16(y_16, c_y);
__m256i r = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(v_16, c_r));
r = _mm256_add_epi16(r, _mm256_set1_epi16(32));
r = _mm256_srai_epi16(r, 6);
__m256i g = _mm256_sub_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_gu));
g = _mm256_sub_epi16(g, _mm256_mullo_epi16(v_16, c_gv));
g = _mm256_add_epi16(g, _mm256_set1_epi16(32));
g = _mm256_srai_epi16(g, 6);
__m256i b = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_b));
b = _mm256_add_epi16(b, _mm256_set1_epi16(32));
b = _mm256_srai_epi16(b, 6);
// clamp 0~255
__m256i zero = _mm256_setzero_si256();
__m256i maxv = _mm256_set1_epi16(255);
r = _mm256_max_epi16(zero, _mm256_min_epi16(r, maxv));
g = _mm256_max_epi16(zero, _mm256_min_epi16(g, maxv));
b = _mm256_max_epi16(zero, _mm256_min_epi16(b, maxv));
// Pack BGR24
alignas(32) uint16_t b_arr[16], g_arr[16], r_arr[16];
_mm256_store_si256((__m256i*)b_arr, b);
_mm256_store_si256((__m256i*)g_arr, g);
_mm256_store_si256((__m256i*)r_arr, r);
for (int i = 0; i < 16; ++i) {
if constexpr (isBGR) {
dstRow[(x + i) * 3 + 0] = (uint8_t)b_arr[i];
dstRow[(x + i) * 3 + 1] = (uint8_t)g_arr[i];
dstRow[(x + i) * 3 + 2] = (uint8_t)r_arr[i];
} else {
dstRow[(x + i) * 3 + 0] = (uint8_t)r_arr[i];
dstRow[(x + i) * 3 + 1] = (uint8_t)g_arr[i];
dstRow[(x + i) * 3 + 2] = (uint8_t)b_arr[i];
}
}
}
// Handle remaining pixels
for (; x < width; x += 2) {
int y0 = yRow[x + 0];
int y1 = yRow[x + 1];
// Correct UV index calculation
int u = uvRow[x]; // U at even position
int v = uvRow[x + 1]; // V at odd position
int r0, g0, b0, r1, g1, b1;
convertFunc(y0, u, v, r0, g0, b0);
convertFunc(y1, u, v, r1, g1, b1);
if constexpr (isBGR) {
dstRow[x * 3 + 0] = b0;
dstRow[x * 3 + 1] = g0;
dstRow[x * 3 + 2] = r0;
dstRow[(x + 1) * 3 + 0] = b1;
dstRow[(x + 1) * 3 + 1] = g1;
dstRow[(x + 1) * 3 + 2] = r1;
} else {
dstRow[x * 3 + 0] = r0;
dstRow[x * 3 + 1] = g0;
dstRow[x * 3 + 2] = b0;
dstRow[(x + 1) * 3 + 0] = r1;
dstRow[(x + 1) * 3 + 1] = g1;
dstRow[(x + 1) * 3 + 2] = b1;
}
}
}
}
template <bool isBGRA, bool isFullRange>
AVX2_TARGET void _i420ToRgba_avx2_imp(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, bool is601) {
// If height < 0, write dst in reverse order while reading src in normal order
if (height < 0) {
height = -height;
dst = dst + (height - 1) * dstStride;
dstStride = -dstStride;
}
// Select coefficients based on flags
int cy, cr, cgu, cgv, cb;
getYuvToRgbCoefficients(is601, isFullRange, cy, cr, cgu, cgv, cb);
__m256i c_y = _mm256_set1_epi16(cy);
__m256i c_r = _mm256_set1_epi16(cr);
__m256i c_gu = _mm256_set1_epi16(cgu);
__m256i c_gv = _mm256_set1_epi16(cgv);
__m256i c_b = _mm256_set1_epi16(cb);
__m256i c128 = _mm256_set1_epi16(128);
YuvToRgbFunc convertFunc = getYuvToRgbFunc(is601, isFullRange);
for (int y = 0; y < height; ++y) {
const uint8_t* yRow = srcY + y * srcYStride;
const uint8_t* uRow = srcU + (y / 2) * srcUStride;
const uint8_t* vRow = srcV + (y / 2) * srcVStride;
uint8_t* dstRow = dst + y * dstStride;
int x = 0;
for (; x + 16 <= width; x += 16) {
// 1. Load 16 Y values
__m128i y_vals = _mm_loadu_si128((const __m128i*)(yRow + x));
// 2. Load 8 U/V values
__m128i u8 = _mm_loadl_epi64((const __m128i*)(uRow + x / 2));
__m128i v8 = _mm_loadl_epi64((const __m128i*)(vRow + x / 2));
// 3. Expand each U/V to 2 pixels
__m128i u16 = _mm_unpacklo_epi8(u8, u8); // U0,U0,U1,U1,...
__m128i v16 = _mm_unpacklo_epi8(v8, v8); // V0,V0,V1,V1,...
// 4. Combine into 16 bytes
__m256i u_16 = _mm256_cvtepu8_epi16(u16);
__m256i v_16 = _mm256_cvtepu8_epi16(v16);
__m256i y_16 = _mm256_cvtepu8_epi16(y_vals);
// 5. Dynamic offset calculation
// UV always subtract 128 in all cases
u_16 = _mm256_sub_epi16(u_16, _mm256_set1_epi16(128));
v_16 = _mm256_sub_epi16(v_16, _mm256_set1_epi16(128));
// Y offset depends on range type
if constexpr (!isFullRange) { // Video Range: Y - 16
y_16 = _mm256_sub_epi16(y_16, _mm256_set1_epi16(16));
}
// Full Range: Y remains unchanged
__m256i y_scaled = _mm256_mullo_epi16(y_16, c_y);
__m256i r = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(v_16, c_r));
r = _mm256_add_epi16(r, _mm256_set1_epi16(32));
r = _mm256_srai_epi16(r, 6);
__m256i g = _mm256_sub_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_gu));
g = _mm256_sub_epi16(g, _mm256_mullo_epi16(v_16, c_gv));
g = _mm256_add_epi16(g, _mm256_set1_epi16(32));
g = _mm256_srai_epi16(g, 6);
__m256i b = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_b));
b = _mm256_add_epi16(b, _mm256_set1_epi16(32));
b = _mm256_srai_epi16(b, 6);
// clamp 0~255
__m256i zero = _mm256_setzero_si256();
__m256i maxv = _mm256_set1_epi16(255);
r = _mm256_max_epi16(zero, _mm256_min_epi16(r, maxv));
g = _mm256_max_epi16(zero, _mm256_min_epi16(g, maxv));
b = _mm256_max_epi16(zero, _mm256_min_epi16(b, maxv));
// Pack BGRA32
__m128i b8 = _mm_packus_epi16(_mm256_castsi256_si128(b), _mm256_extracti128_si256(b, 1));
__m128i g8 = _mm_packus_epi16(_mm256_castsi256_si128(g), _mm256_extracti128_si256(g, 1));
__m128i r8 = _mm_packus_epi16(_mm256_castsi256_si128(r), _mm256_extracti128_si256(r, 1));
__m128i a8 = _mm_set1_epi8((char)255);
if constexpr (isBGRA) {
__m128i bg0 = _mm_unpacklo_epi8(b8, g8);
__m128i ra0 = _mm_unpacklo_epi8(r8, a8);
__m128i bgra0 = _mm_unpacklo_epi16(bg0, ra0);
__m128i bgra1 = _mm_unpackhi_epi16(bg0, ra0);
__m128i bg1 = _mm_unpackhi_epi8(b8, g8);
__m128i ra1 = _mm_unpackhi_epi8(r8, a8);
__m128i bgra2 = _mm_unpacklo_epi16(bg1, ra1);
__m128i bgra3 = _mm_unpackhi_epi16(bg1, ra1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 0), bgra0);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 16), bgra1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 32), bgra2);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 48), bgra3);
} else {
__m128i rg0 = _mm_unpacklo_epi8(r8, g8);
__m128i ba0 = _mm_unpacklo_epi8(b8, a8);
__m128i rgba0 = _mm_unpacklo_epi16(rg0, ba0);
__m128i rgba1 = _mm_unpackhi_epi16(rg0, ba0);
__m128i rg1 = _mm_unpackhi_epi8(r8, g8);
__m128i ba1 = _mm_unpackhi_epi8(b8, a8);
__m128i rgba2 = _mm_unpacklo_epi16(rg1, ba1);
__m128i rgba3 = _mm_unpackhi_epi16(rg1, ba1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 0), rgba0);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 16), rgba1);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 32), rgba2);
_mm_storeu_si128((__m128i*)(dstRow + x * 4 + 48), rgba3);
}
}
// Handle remaining pixels (_i420ToRgba_avx2_imp)
for (; x < width; x += 2) {
int y0 = yRow[x + 0];
int y1 = yRow[x + 1];
int u = uRow[x / 2];
int v = vRow[x / 2];
int r0, g0, b0, r1, g1, b1;
convertFunc(y0, u, v, r0, g0, b0);
convertFunc(y1, u, v, r1, g1, b1);
if constexpr (isBGRA) {
dstRow[x * 4 + 0] = b0;
dstRow[x * 4 + 1] = g0;
dstRow[x * 4 + 2] = r0;
dstRow[x * 4 + 3] = 255;
dstRow[(x + 1) * 4 + 0] = b1;
dstRow[(x + 1) * 4 + 1] = g1;
dstRow[(x + 1) * 4 + 2] = r1;
dstRow[(x + 1) * 4 + 3] = 255;
} else {
dstRow[x * 4 + 0] = r0;
dstRow[x * 4 + 1] = g0;
dstRow[x * 4 + 2] = b0;
dstRow[x * 4 + 3] = 255;
dstRow[(x + 1) * 4 + 0] = r1;
dstRow[(x + 1) * 4 + 1] = g1;
dstRow[(x + 1) * 4 + 2] = b1;
dstRow[(x + 1) * 4 + 3] = 255;
}
}
}
}
template <bool isBGR, bool isFullRange>
AVX2_TARGET void _i420ToRgb_avx2_imp(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, bool is601) {
// If height < 0, write dst in reverse order while reading src in normal order
if (height < 0) {
height = -height;
dst = dst + (height - 1) * dstStride;
dstStride = -dstStride;
}
// Select coefficients based on flags
int cy, cr, cgu, cgv, cb;
getYuvToRgbCoefficients(is601, isFullRange, cy, cr, cgu, cgv, cb);
__m256i c_y = _mm256_set1_epi16(cy);
__m256i c_r = _mm256_set1_epi16(cr);
__m256i c_gu = _mm256_set1_epi16(cgu);
__m256i c_gv = _mm256_set1_epi16(cgv);
__m256i c_b = _mm256_set1_epi16(cb);
__m256i c128 = _mm256_set1_epi16(128);
YuvToRgbFunc convertFunc = getYuvToRgbFunc(is601, isFullRange);
for (int y = 0; y < height; ++y) {
const uint8_t* yRow = srcY + y * srcYStride;
const uint8_t* uRow = srcU + (y / 2) * srcUStride;
const uint8_t* vRow = srcV + (y / 2) * srcVStride;
uint8_t* dstRow = dst + y * dstStride;
int x = 0;
for (; x + 16 <= width; x += 16) {
// 1. Load 16 Y values
__m128i y_vals = _mm_loadu_si128((const __m128i*)(yRow + x));
// 2. Load 8 U/V values
__m128i u8 = _mm_loadl_epi64((const __m128i*)(uRow + x / 2));
__m128i v8 = _mm_loadl_epi64((const __m128i*)(vRow + x / 2));
// 3. Expand each U/V to 2 pixels
__m128i u16 = _mm_unpacklo_epi8(u8, u8); // U0,U0,U1,U1,...
__m128i v16 = _mm_unpacklo_epi8(v8, v8); // V0,V0,V1,V1,...
// 4. Combine into 16 bytes
__m256i u_16 = _mm256_cvtepu8_epi16(u16);
__m256i v_16 = _mm256_cvtepu8_epi16(v16);
__m256i y_16 = _mm256_cvtepu8_epi16(y_vals);
// 5. Dynamic offset calculation
// UV always subtract 128 in all cases
u_16 = _mm256_sub_epi16(u_16, c128);
v_16 = _mm256_sub_epi16(v_16, c128);
// Y offset depends on range type
if constexpr (!isFullRange) {
// Video Range: Y - 16
y_16 = _mm256_sub_epi16(y_16, _mm256_set1_epi16(16));
}
// Full Range: Y remains unchanged
__m256i y_scaled = _mm256_mullo_epi16(y_16, c_y);
__m256i r = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(v_16, c_r));
r = _mm256_add_epi16(r, _mm256_set1_epi16(32));
r = _mm256_srai_epi16(r, 6);
__m256i g = _mm256_sub_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_gu));
g = _mm256_sub_epi16(g, _mm256_mullo_epi16(v_16, c_gv));
g = _mm256_add_epi16(g, _mm256_set1_epi16(32));
g = _mm256_srai_epi16(g, 6);
__m256i b = _mm256_add_epi16(y_scaled, _mm256_mullo_epi16(u_16, c_b));
b = _mm256_add_epi16(b, _mm256_set1_epi16(32));
b = _mm256_srai_epi16(b, 6);
// clamp 0~255
__m256i zero = _mm256_setzero_si256();
__m256i maxv = _mm256_set1_epi16(255);
r = _mm256_max_epi16(zero, _mm256_min_epi16(r, maxv));
g = _mm256_max_epi16(zero, _mm256_min_epi16(g, maxv));
b = _mm256_max_epi16(zero, _mm256_min_epi16(b, maxv));
// Pack BGR24
alignas(32) uint16_t b_arr[16], g_arr[16], r_arr[16];
_mm256_store_si256((__m256i*)b_arr, b);
_mm256_store_si256((__m256i*)g_arr, g);
_mm256_store_si256((__m256i*)r_arr, r);
for (int i = 0; i < 16; ++i) {
if constexpr (isBGR) {
dstRow[(x + i) * 3 + 0] = (uint8_t)b_arr[i];
dstRow[(x + i) * 3 + 1] = (uint8_t)g_arr[i];
dstRow[(x + i) * 3 + 2] = (uint8_t)r_arr[i];
} else {
dstRow[(x + i) * 3 + 0] = (uint8_t)r_arr[i];
dstRow[(x + i) * 3 + 1] = (uint8_t)g_arr[i];
dstRow[(x + i) * 3 + 2] = (uint8_t)b_arr[i];
}
}
}
// Handle remaining pixels
for (; x < width; x += 2) {
int y0 = yRow[x + 0];
int y1 = yRow[x + 1];
int u = uRow[x / 2];
int v = vRow[x / 2];
int r0, g0, b0, r1, g1, b1;
// Use pre-selected conversion function
convertFunc(y0, u, v, r0, g0, b0);
convertFunc(y1, u, v, r1, g1, b1);
if constexpr (isBGR) {
dstRow[x * 3 + 0] = b0;
dstRow[x * 3 + 1] = g0;
dstRow[x * 3 + 2] = r0;
dstRow[(x + 1) * 3 + 0] = b1;
dstRow[(x + 1) * 3 + 1] = g1;
dstRow[(x + 1) * 3 + 2] = r1;
} else {
dstRow[x * 3 + 0] = r0;
dstRow[x * 3 + 1] = g0;
dstRow[x * 3 + 2] = b0;
dstRow[(x + 1) * 3 + 0] = r1;
dstRow[(x + 1) * 3 + 1] = g1;
dstRow[(x + 1) * 3 + 2] = b1;
}
}
}
}
// AVX2-based acceleration
AVX2_TARGET
void nv12ToBgra32_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride, int width,
int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
nv12ToRgbaColor_avx2_imp<true, true>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
} else {
nv12ToRgbaColor_avx2_imp<true, false>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void nv12ToRgba32_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride, int width,
int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
nv12ToRgbaColor_avx2_imp<false, true>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
} else {
nv12ToRgbaColor_avx2_imp<false, false>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void nv12ToBgr24_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride, int width,
int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_nv12ToRgbColor_avx2_imp<true, true>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
} else {
_nv12ToRgbColor_avx2_imp<true, false>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void nv12ToRgb24_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcUV, int srcUVStride, uint8_t* dst, int dstStride, int width,
int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_nv12ToRgbColor_avx2_imp<false, true>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
} else {
_nv12ToRgbColor_avx2_imp<false, false>(srcY, srcYStride, srcUV, srcUVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void i420ToBgra32_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_i420ToRgba_avx2_imp<true, true>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
} else {
_i420ToRgba_avx2_imp<true, false>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void i420ToRgba32_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_i420ToRgba_avx2_imp<false, true>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
} else {
_i420ToRgba_avx2_imp<false, false>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void i420ToBgr24_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_i420ToRgb_avx2_imp<true, true>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
} else {
_i420ToRgb_avx2_imp<true, false>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
}
}
AVX2_TARGET
void i420ToRgb24_avx2(const uint8_t* srcY, int srcYStride, const uint8_t* srcU, int srcUStride, const uint8_t* srcV, int srcVStride,
uint8_t* dst, int dstStride, int width, int height, ConvertFlag flag) {
const bool is601 = (flag & ConvertFlag::BT601) != 0;
const bool isFullRange = (flag & ConvertFlag::FullRange) != 0;
if (isFullRange) {
_i420ToRgb_avx2_imp<false, true>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
} else {
_i420ToRgb_avx2_imp<false, false>(srcY, srcYStride, srcU, srcUStride, srcV, srcVStride, dst, dstStride, width, height, is601);
}
}
///////////// YUYV/UYVY to RGB functions /////////////
template <bool isBgrColor, bool hasAlpha, bool isFullRange>
AVX2_TARGET void yuyvToRgb_avx2_imp(const uint8_t* src, int srcStride, uint8_t* dst, int dstStride, int width, int height, bool is601) {
// If height < 0, write dst in reverse order while reading src in normal order
if (height < 0) {
height = -height;