-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathpixfmt_conv.c
More file actions
2930 lines (2654 loc) · 111 KB
/
pixfmt_conv.c
File metadata and controls
2930 lines (2654 loc) · 111 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 pixfmt_conv.c
* @author Martin Benes <martinbenesh@gmail.com>
* @author Lukas Hejtmanek <xhejtman@ics.muni.cz>
* @author Petr Holub <hopet@ics.muni.cz>
* @author Milos Liska <xliska@fi.muni.cz>
* @author Jiri Matela <matela@ics.muni.cz>
* @author Dalibor Matura <255899@mail.muni.cz>
* @author Ian Wesley-Smith <iwsmith@cct.lsu.edu>
*
* @brief This file contains video codec-related functions.
*
* This file contains video codecs' metadata and helper
* functions as well as pixelformat converting functions.
*/
/* Copyright (c) 2005-2023 CESNET z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by CESNET z.s.p.o.
*
* 4. Neither the name of the CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#define __STDC_WANT_LIB_EXT1__ 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#include "config_unix.h"
#include "config_win32.h"
#include <assert.h>
#include "color.h"
#include "compat/qsort_s.h"
#include "debug.h"
#include "pixfmt_conv.h"
#include "utils/macros.h" // to_fourcc, OPTIMEZED_FOR, CLAMP
#include "video_codec.h"
#ifdef __SSSE3__
#include "tmmintrin.h"
#endif
#ifdef WORDS_BIGENDIAN
#define BYTE_SWAP(x) (3 - x)
#else
#define BYTE_SWAP(x) x
#endif
/**
* @brief Converts v210 to UYVY
* @param[out] dst 4-byte aligned output buffer where UYVY will be stored
* @param[in] src 4-byte aligned input buffer containing v210 (by definition of v210
* should be even aligned to 16B boundary)
* @param[in] dst_len length of data that should be writen to dst buffer (in bytes)
*/
static void vc_copylinev210(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift,
int gshift, int bshift)
{
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
struct {
unsigned a:10;
unsigned b:10;
unsigned c:10;
unsigned p1:2;
} const *s;
register uint32_t *d;
register uint32_t tmp;
d = (uint32_t *)(void *) dst;
s = (const void *)src;
while (dst_len >= 12) {
tmp = (s->a >> 2) | (s->b >> 2) << 8 | (((s)->c >> 2) << 16);
s++;
*(d++) = tmp | ((s->a >> 2) << 24);
tmp = (s->b >> 2) | (((s)->c >> 2) << 8);
s++;
*(d++) = tmp | ((s->a >> 2) << 16) | ((s->b >> 2) << 24);
tmp = (s->c >> 2);
s++;
*(d++) =
tmp | ((s->a >> 2) << 8) | ((s->b >> 2) << 16) |
((s->c >> 2) << 24);
s++;
dst_len -= 12;
}
if (dst_len >= 4) {
tmp = (s->a >> 2) | (s->b >> 2) << 8 | (((s)->c >> 2) << 16);
s++;
*(d++) = tmp | ((s->a >> 2) << 24);
}
if (dst_len >= 8) {
tmp = (s->b >> 2) | (((s)->c >> 2) << 8);
s++;
*(d++) = tmp | ((s->a >> 2) << 16) | ((s->b >> 2) << 24);
}
}
/**
* @brief Converts from YUYV to UYVY.
* @copydetails vc_copylinev210
*/
static void vc_copylineYUYV(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift,
int gshift, int bshift)
{
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
#if defined __SSE2__
register uint32_t *d;
register const uint32_t *s;
const uint32_t * const end = (uint32_t *)(void *) dst + dst_len / 4;
uint32_t mask[4] = {
0xff00ff00ul,
0xff00ff00ul,
0xff00ff00ul,
0xff00ff00ul};
d = (uint32_t *)(void *) dst;
s = (const uint32_t *)(const void *) src;
assert(dst_len % 4 == 0);
if((dst_len % 16 == 0)) {
asm("movdqu (%0), %%xmm4\n"
"movdqa %%xmm4, %%xmm5\n"
"psrldq $1, %%xmm5\n"
: :"r"(mask));
while(d < end) {
asm volatile ("movdqu (%0), %%xmm0\n"
"movdqu %%xmm0, %%xmm1\n"
"pand %%xmm4, %%xmm0\n"
"psrldq $1, %%xmm0\n"
"pand %%xmm5, %%xmm1\n"
"pslldq $1, %%xmm1\n"
"por %%xmm0, %%xmm1\n"
"movdqu %%xmm1, (%1)\n"::"r" (s), "r"(d));
s += 4;
d += 4;
}
} else {
while(d < end) {
register uint32_t tmp = *s;
*d = ((tmp & 0x00ff0000) << 8) | ((tmp & 0xff000000) >> 8) |
((tmp & 0x000000ff) << 8) | ((tmp & 0x0000ff00) >> 8);
s++;
d++;
}
}
#else
char u, y1, v, y2;
OPTIMIZED_FOR (int x = 0; x <= dst_len - 4; x += 4) {
y1 = *src++;
u = *src++;
y2 = *src++;
v = *src++;
*dst++ = u;
*dst++ = y1;
*dst++ = v;
*dst++ = y2;
}
#endif
}
/**
* @brief Converts from R10k to RGBA
*
* @param[out] dst 4B-aligned buffer that will contain result
* @param[in] src 4B-aligned buffer containing pixels in R10k
* @param[in] dst_len length of data that should be writen to dst buffer (in bytes)
* @param[in] rshift destination red shift
* @param[in] gshift destination green shift
* @param[in] bshift destination blue shift
*/
static void
vc_copyliner10k(unsigned char * __restrict dst, const unsigned char * __restrict src, int len, int rshift,
int gshift, int bshift)
{
struct {
unsigned r:8;
unsigned gh:6;
unsigned p1:2;
unsigned bh:4;
unsigned p2:2;
unsigned gl:2;
unsigned p3:2;
unsigned p4:2;
unsigned bl:4;
} const *s;
register uint32_t *d;
register uint32_t tmp;
uint32_t alpha_mask = 0xFFFFFFFFU ^ (0xFFU << rshift) ^ (0xFFU << gshift) ^ (0xFFU << bshift);
d = (uint32_t *)(void *) dst;
s = (const void *) src;
while (len >= 16) {
tmp =
(alpha_mask | s->
r << rshift) | (((s->gh << 2) | s->
gl) << gshift) | (((s->bh << 4) | s->
bl) << bshift);
s++;
*(d++) = tmp;
tmp =
(alpha_mask | s->
r << rshift) | (((s->gh << 2) | s->
gl) << gshift) | (((s->bh << 4) | s->
bl) << bshift);
s++;
*(d++) = tmp;
tmp =
(alpha_mask | s->
r << rshift) | (((s->gh << 2) | s->
gl) << gshift) | (((s->bh << 4) | s->
bl) << bshift);
s++;
*(d++) = tmp;
tmp =
(alpha_mask | s->
r << rshift) | (((s->gh << 2) | s->
gl) << gshift) | (((s->bh << 4) | s->
bl) << bshift);
s++;
*(d++) = tmp;
len -= 16;
}
while (len >= 4) {
tmp =
(alpha_mask | s->
r << rshift) | (((s->gh << 2) | s->
gl) << gshift) | (((s->bh << 4) | s->
bl) << bshift);
s++;
*(d++) = tmp;
len -= 4;
}
}
static void
vc_copyliner10ktoRG48(unsigned char * __restrict dst, const unsigned char * __restrict src, int dstlen, int rshift,
int gshift, int bshift) {
UNUSED(rshift), UNUSED(gshift), UNUSED(bshift);
while (dstlen > 0) {
dst[1] = *src++; // Rhi
unsigned int byte2 = *src++;
unsigned int byte3 = *src++;
unsigned int byte4 = *src++;
dst[0] = byte2 & 0xC0U; // Rlo
dst[3] = byte2 << 2U | byte3 >> 6U; // Ghi
dst[2] = (byte3 & 0x30U) << 2U; // Glo
dst[5] = (byte3 & 0xFU) << 4U | byte4 >> 4U; // Bhi
dst[4] = (byte4 & 0xCU) << 4U; // Blo
dst += 6;
dstlen -= 6;
}
}
static void vc_copyliner10ktoY416(unsigned char * __restrict dst, const unsigned char * __restrict src, int dstlen, int rshift,
int gshift, int bshift) {
UNUSED(rshift), UNUSED(gshift), UNUSED(bshift);
assert((uintptr_t) dst % 2 == 0);
uint16_t *d = (void *) dst;
OPTIMIZED_FOR (int x = 0; x < dstlen; x += 8) {
unsigned int byte1 = *src++;
unsigned int byte2 = *src++;
unsigned int byte3 = *src++;
unsigned int byte4 = *src++;
comp_type_t r, g, b;
r = byte1 << 8U | (byte2 & 0xC0U);
g = (byte2 & 0x3FU) << 10U | (byte3 & 0xF0U) << 2U;
b = (byte3 & 0xFU) << 12U | (byte4 & 0xFCU) << 4U;
comp_type_t u = (RGB_TO_CB_709_SCALED(r, g, b) >> COMP_BASE) + (1<<15);
*d++ = CLAMP_LIMITED_CBCR(u, 16);
comp_type_t y = (RGB_TO_Y_709_SCALED(r, g, b) >> COMP_BASE) + (1<<12);
*d++ = CLAMP_LIMITED_Y(y, 16);
comp_type_t v = (RGB_TO_CR_709_SCALED(r, g, b) >> COMP_BASE) + (1<<15);
*d++ = CLAMP_LIMITED_CBCR(v, 16);
*d++ = 0xFFFFU;
}
}
static void vc_copyliner10ktoRGB(unsigned char * __restrict dst, const unsigned char * __restrict src, int dstlen, int rshift,
int gshift, int bshift) {
UNUSED(rshift), UNUSED(gshift), UNUSED(bshift);
OPTIMIZED_FOR (int x = 0; x < dstlen; x += 3) {
*(dst++) = src[0];
*(dst++) = src[1] << 2 | src[2] >> 6;
*(dst++) = src[2] << 4 | src[3] >> 4;
src += 4;
}
}
/**
* @brief Converts from R12L to RGB
*
* @param[out] dst 4B-aligned buffer that will contain result
* @param[in] src buffer containing pixels in R12L
* @param[in] dst_len length of data that should be writen to dst buffer (in bytes)
* @param[in] rshift ignored
* @param[in] gshift ignored
* @param[in] bshift ignored
*/
static void
vc_copylineR12LtoRGB(unsigned char * __restrict dst, const unsigned char * __restrict src, int dstlen, int rshift,
int gshift, int bshift)
{
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
OPTIMIZED_FOR (int x = 0; x <= dstlen - 24; x += 24) {
uint8_t tmp;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
*dst++ = tmp; // r0
*dst++ = src[BYTE_SWAP(2)]; // g0
tmp = src[BYTE_SWAP(3)]>> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
*dst++ = tmp; // b0
*dst++ = src[BYTE_SWAP(1)]; // r1
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
*dst++ = tmp; // g1
*dst++ = src[BYTE_SWAP(0)]; // b1
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
*dst++ = tmp; // r2
*dst++ = src[BYTE_SWAP(3)]; // g2
src += 4;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
*dst++ = tmp; // b2
*dst++ = src[BYTE_SWAP(2)]; // r3
tmp = src[BYTE_SWAP(3)] >> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
*dst++ = tmp; // g3
*dst++ = src[BYTE_SWAP(1)]; // b3
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
*dst++ = tmp; // r4
*dst++ = src[BYTE_SWAP(0)]; // g4
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
*dst++ = tmp; // b4
*dst++ = src[BYTE_SWAP(3)]; // r5
src += 4;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
*dst++ = tmp; // g5
*dst++ = src[BYTE_SWAP(2)]; // b5
tmp = src[BYTE_SWAP(3)] >> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
*dst++ = tmp; // r6
*dst++ = src[BYTE_SWAP(1)]; // g6
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
*dst++ = tmp; // b6
*dst++ = src[BYTE_SWAP(0)]; // r7
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
*dst++ = tmp; // g7
*dst++ = src[BYTE_SWAP(3)]; // b7
src += 4;
}
}
/**
* @brief Converts from R12L to RGBA
*
* @param[out] dst 4B-aligned buffer that will contain result
* @param[in] src buffer containing pixels in R12L
* @param[in] dstlen length of data that should be writen to dst buffer (in bytes)
* @param[in] rshift destination red shift
* @param[in] gshift destination green shift
* @param[in] bshift destination blue shift
*/
static void
vc_copylineR12L(unsigned char *dst, const unsigned char *src, int dstlen, int rshift,
int gshift, int bshift)
{
assert((uintptr_t) dst % sizeof(uint32_t) == 0);
uint32_t *d = (uint32_t *)(void *) dst;
uint32_t alpha_mask = 0xFFFFFFFFU ^ (0xFFU << rshift) ^ (0xFFU << gshift) ^ (0xFFU << bshift);
OPTIMIZED_FOR (int x = 0; x <= dstlen - 32; x += 32) {
uint8_t tmp;
uint8_t r, g, b;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
r = tmp; // r0
g = src[BYTE_SWAP(2)]; // g0
tmp = src[BYTE_SWAP(3)]>> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
b = tmp; // b0
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
r = src[BYTE_SWAP(1)]; // r1
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
g = tmp; // g1
b = src[BYTE_SWAP(0)]; // b1
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
r = tmp; // r2
g = src[BYTE_SWAP(3)]; // g2
src += 4;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
b = tmp; // b2
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
r = src[BYTE_SWAP(2)]; // r3
tmp = src[BYTE_SWAP(3)] >> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
g = tmp; // g3
b = src[BYTE_SWAP(1)]; // b3
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
r = tmp; // r4
g = src[BYTE_SWAP(0)]; // g4
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
b = tmp; // b4
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
r = src[BYTE_SWAP(3)]; // r5
src += 4;
tmp = src[BYTE_SWAP(0)] >> 4;
tmp |= src[BYTE_SWAP(1)] << 4;
g = tmp; // g5
b = src[BYTE_SWAP(2)]; // b5
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
tmp = src[BYTE_SWAP(3)] >> 4;
src += 4;
tmp |= src[BYTE_SWAP(0)] << 4;
r = tmp; // r6
g = src[BYTE_SWAP(1)]; // g6
tmp = src[BYTE_SWAP(2)] >> 4;
tmp |= src[BYTE_SWAP(3)] << 4;
src += 4;
b = tmp; // b6
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
r = src[BYTE_SWAP(0)]; // r7
tmp = src[BYTE_SWAP(1)] >> 4;
tmp |= src[BYTE_SWAP(2)] << 4;
g = tmp; // g7
b = src[BYTE_SWAP(3)]; // b7
src += 4;
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
}
}
/**
* @brief Changes color channels' order in RGBA
*
* @param[out] dst 4B-aligned buffer that will contain result
* @param[in] src 4B-aligned buffer containing pixels in RGBA
* @param[in] dst_len length of data that should be writen to dst buffer (in bytes)
* @param[in] rshift destination red shift
* @param[in] gshift destination green shift
* @param[in] bshift destination blue shift
*/
void
vc_copylineRGBA(unsigned char * __restrict dst, const unsigned char * __restrict src, int len, int rshift,
int gshift, int bshift)
{
register uint32_t *d = (uint32_t *)(void *) dst;
register const uint32_t *s = (const uint32_t *)(const void *) src;
register uint32_t tmp;
if (rshift == 0 && gshift == 8 && bshift == 16) {
memcpy(dst, src, len);
} else {
uint32_t alpha_mask = 0xFFFFFFFFU ^ (0xFFU << rshift) ^ (0xFFU << gshift) ^ (0xFFU << bshift);
while (len >= 16) {
register unsigned int r, g, b;
tmp = *(s++);
r = tmp & 0xff;
g = (tmp >> 8) & 0xff;
b = (tmp >> 16) & 0xff;
tmp = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
*(d++) = tmp;
tmp = *(s++);
r = tmp & 0xff;
g = (tmp >> 8) & 0xff;
b = (tmp >> 16) & 0xff;
tmp = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
*(d++) = tmp;
tmp = *(s++);
r = tmp & 0xff;
g = (tmp >> 8) & 0xff;
b = (tmp >> 16) & 0xff;
tmp = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
*(d++) = tmp;
tmp = *(s++);
r = tmp & 0xff;
g = (tmp >> 8) & 0xff;
b = (tmp >> 16) & 0xff;
tmp = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
*(d++) = tmp;
len -= 16;
}
while (len >= 4) {
register unsigned int r, g, b;
tmp = *(s++);
r = tmp & 0xff;
g = (tmp >> 8) & 0xff;
b = (tmp >> 16) & 0xff;
tmp = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
*(d++) = tmp;
len -= 4;
}
}
}
/**
* @brief Converts from DVS10 to v210
* @copydetails vc_copylinev210
*/
static void vc_copylineDVS10toV210(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift,
int gshift, int bshift)
{
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
unsigned int *d;
const unsigned int *s1;
register unsigned int a,b;
d = (unsigned int *)(void *) dst;
s1 = (const unsigned int *)(const void *) src;
OPTIMIZED_FOR (int x = 0; x <= dst_len - 4; x += 4) {
a = b = *s1++;
b = ((b >> 24) * 0x00010101) & 0x00300c03;
a <<= 2;
b |= a & (0xff<<2);
a <<= 2;
b |= a & (0xff00<<4);
a <<= 2;
b |= a & (0xff0000<<6);
*d++ = b;
}
}
/* convert 10bits Cb Y Cr A Y Cb Y A to 8bits Cb Y Cr Y Cb Y */
/* TODO: undo it - currently this decoder is broken */
#if 0 /* !(HAVE_MACOSX || HAVE_32B_LINUX) */
void vc_copylineDVS10(unsigned char *dst, unsigned char *src, int src_len)
{
register unsigned char *_d = dst, *_s = src;
while (src_len > 0) {
asm("movd %0, %%xmm4\n": :"r"(0xffffff));
asm volatile ("movdqa (%0), %%xmm0\n"
"movdqa 16(%0), %%xmm5\n"
"movdqa %%xmm0, %%xmm1\n"
"movdqa %%xmm0, %%xmm2\n"
"movdqa %%xmm0, %%xmm3\n"
"pand %%xmm4, %%xmm0\n"
"movdqa %%xmm5, %%xmm6\n"
"movdqa %%xmm5, %%xmm7\n"
"movdqa %%xmm5, %%xmm8\n"
"pand %%xmm4, %%xmm5\n"
"pslldq $4, %%xmm4\n"
"pand %%xmm4, %%xmm1\n"
"pand %%xmm4, %%xmm6\n"
"pslldq $4, %%xmm4\n"
"psrldq $1, %%xmm1\n"
"psrldq $1, %%xmm6\n"
"pand %%xmm4, %%xmm2\n"
"pand %%xmm4, %%xmm7\n"
"pslldq $4, %%xmm4\n"
"psrldq $2, %%xmm2\n"
"psrldq $2, %%xmm7\n"
"pand %%xmm4, %%xmm3\n"
"pand %%xmm4, %%xmm8\n"
"por %%xmm1, %%xmm0\n"
"psrldq $3, %%xmm3\n"
"psrldq $3, %%xmm8\n"
"por %%xmm2, %%xmm0\n"
"por %%xmm6, %%xmm5\n"
"por %%xmm3, %%xmm0\n"
"por %%xmm7, %%xmm5\n"
"movdq2q %%xmm0, %%mm0\n"
"por %%xmm8, %%xmm5\n"
"movdqa %%xmm5, %%xmm1\n"
"pslldq $12, %%xmm5\n"
"psrldq $4, %%xmm1\n"
"por %%xmm5, %%xmm0\n"
"psrldq $8, %%xmm0\n"
"movq %%mm0, (%1)\n"
"movdq2q %%xmm0, %%mm1\n"
"movdq2q %%xmm1, %%mm2\n"
"movq %%mm1, 8(%1)\n"
"movq %%mm2, 16(%1)\n"::"r" (_s), "r"(_d));
_s += 32;
_d += 24;
src_len -= 32;
}
}
#else
/**
* @brief Converts from DVS10 to UYVY
* @copydetails vc_copylinev210
*/
static void vc_copylineDVS10(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift,
int gshift, int bshift)
{
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
int src_len = dst_len / 1.5; /* right units */
register const uint64_t *s;
register uint64_t *d;
register uint64_t a1, a2, a3, a4;
d = (uint64_t *)(void *) dst;
s = (const uint64_t *)(const void *) src;
OPTIMIZED_FOR (int x = 0; x <= src_len - 16; x += 16) {
a1 = *(s++);
a2 = *(s++);
a3 = *(s++);
a4 = *(s++);
a1 = (a1 & 0xffffff) | ((a1 >> 8) & 0xffffff000000LL);
a2 = (a2 & 0xffffff) | ((a2 >> 8) & 0xffffff000000LL);
a3 = (a3 & 0xffffff) | ((a3 >> 8) & 0xffffff000000LL);
a4 = (a4 & 0xffffff) | ((a4 >> 8) & 0xffffff000000LL);
*(d++) = a1 | (a2 << 48); /* 0xa2|a2|a1|a1|a1|a1|a1|a1 */
*(d++) = (a2 >> 16) | (a3 << 32); /* 0xa3|a3|a3|a3|a2|a2|a2|a2 */
*(d++) = (a3 >> 32) | (a4 << 16); /* 0xa4|a4|a4|a4|a4|a4|a3|a3 */
}
}
#endif /* !(HAVE_MACOSX || HAVE_32B_LINUX) */
/**
* @brief Changes color order of an RGB
*
* @note
* Unlike most of the non-RGBA conversions, RGB shifts are respected.
*
* @copydetails vc_copyliner10k
*/
static void vc_copylineRGB(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift, int gshift, int bshift)
{
register unsigned int r, g, b;
union {
unsigned int out;
unsigned char c[4];
} u;
if (rshift == 0 && gshift == 8 && bshift == 16) {
memcpy(dst, src, dst_len);
} else {
OPTIMIZED_FOR (int x = 0; x <= dst_len - 3; x += 3) {
r = *src++;
g = *src++;
b = *src++;
u.out = (r << rshift) | (g << gshift) | (b << bshift);
*dst++ = u.c[0];
*dst++ = u.c[1];
*dst++ = u.c[2];
}
}
}
/**
* @brief Converts from RGBA to RGB. Channels in RGBA can be differently ordered.
*
* @param[out] dst 4B-aligned buffer that will contain result
* @param[in] src 4B-aligned buffer containing pixels in RGBA
* @param[in] dst_len length of data that should be writen to dst buffer (in bytes)
* @param[in] rshift source red shift
* @param[in] gshift source green shift
* @param[in] bshift source blue shift
*
* @note
* In opposite to the defined semantic of {r,g,b}shift, here instead of destination
* shifts the shifts define the source codec properties.
*/
static void vc_copylineRGBAtoRGBwithShift(unsigned char * __restrict dst2, const unsigned char * __restrict src2, int dst_len, int rshift, int gshift, int bshift)
{
register const uint32_t * src = (const uint32_t *)(const void *) src2;
register uint32_t * dst = (uint32_t *)(void *) dst2;
int x;
OPTIMIZED_FOR (x = 0; x <= dst_len - 12; x += 12) {
register uint32_t in1 = *src++;
register uint32_t in2 = *src++;
register uint32_t in3 = *src++;
register uint32_t in4 = *src++;
*dst++ = ((in2 >> rshift)) << 24 |
((in1 >> bshift) & 0xff) << 16 |
((in1 >> gshift) & 0xff) << 8 |
((in1 >> rshift) & 0xff);
*dst++ = ((in3 >> gshift)) << 24 |
((in3 >> rshift) & 0xff) << 16 |
((in2 >> bshift) & 0xff) << 8 |
((in2 >> gshift) & 0xff);
*dst++ = ((in4 >> bshift)) << 24 |
((in4 >> gshift) & 0xff) << 16 |
((in4 >> rshift) & 0xff) << 8 |
((in3 >> bshift) & 0xff);
}
uint8_t *dst_c = (uint8_t *) dst;
for (; x <= dst_len - 3; x += 3) {
register uint32_t in = *src++;
*dst_c++ = (in >> rshift) & 0xff;
*dst_c++ = (in >> gshift) & 0xff;
*dst_c++ = (in >> bshift) & 0xff;
}
}
/**
* @brief Converts from AGBR to RGB
* @copydetails vc_copylinev210
* @see vc_copylineRGBAtoRGBwithShift
* @see vc_copylineRGBAtoRGB
*/
void vc_copylineABGRtoRGB(unsigned char * __restrict dst2, const unsigned char * __restrict src2, int dst_len, int rshift, int gshift, int bshift)
{
#ifdef __SSSE3__
__m128i shuf = _mm_setr_epi8(3, 2, 1, 7, 6, 5, 11, 10, 9, 15, 14, 13, 0xff, 0xff, 0xff, 0xff);
__m128i loaded;
int x;
for (x = 0; x <= dst_len - 12; x += 12) {
loaded = _mm_lddqu_si128((const __m128i_u *) src2);
loaded = _mm_shuffle_epi8(loaded, shuf);
_mm_storeu_si128((__m128i_u *)dst2, loaded);
src2 += 16;
dst2 += 12;
}
rshift = 16; gshift = 8; bshift = 0;
uint8_t *dst_c = (uint8_t *) dst2;
for (; x <= dst_len - 3; x += 3) {
register uint32_t in = *src2++;
*dst_c++ = (in >> rshift) & 0xff;
*dst_c++ = (in >> gshift) & 0xff;
*dst_c++ = (in >> bshift) & 0xff;
}
#else
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
vc_copylineRGBAtoRGBwithShift(dst, src, dst_len, 16, 8, 0);
#endif
}
/**
* @brief Converts from RGBA to RGB
* @copydetails vc_copylineRGBAtoRGBwithShift
*/
static void vc_copylineRGBAtoRGB(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift, int gshift, int bshift)
{
#ifdef __SSSE3__
__m128i shuf = _mm_setr_epi8(0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 0xff, 0xff, 0xff, 0xff);
__m128i loaded;
int x;
for (x = 0; x <= dst_len - 12; x += 12) {
loaded = _mm_lddqu_si128((const __m128i_u *) src);
loaded = _mm_shuffle_epi8(loaded, shuf);
_mm_storeu_si128((__m128i_u *)dst, loaded);
src += 16;
dst += 12;
}
rshift = 0; gshift = 8; bshift = 16;
uint8_t *dst_c = (uint8_t *) dst;
for (; x <= dst_len - 3; x += 3) {
register uint32_t in = *src++;
*dst_c++ = (in >> rshift) & 0xff;
*dst_c++ = (in >> gshift) & 0xff;
*dst_c++ = (in >> bshift) & 0xff;
}
#else
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
vc_copylineRGBAtoRGBwithShift(dst, src, dst_len, 0, 8, 16);
#endif
}
/**
* @brief Converts RGBA with different shifts to RGBA
*
* dst and src may overlap
*/
void vc_copylineToRGBA_inplace(unsigned char *dst, const unsigned char *src, int dst_len,
int src_rshift, int src_gshift, int src_bshift)
{
register const uint32_t * in = (const uint32_t *)(const void *) src;
register uint32_t * out = (uint32_t *)(void *) dst;
while (dst_len >= 4) {
register uint32_t in_val = *in++;
*out++ = ((in_val >> src_rshift) & 0xff) |
((in_val >> src_gshift) & 0xff) << 8 |
((in_val >> src_bshift) & 0xff) << 16;
dst_len -= 4;
}
}
/**
* @brief Converts UYVY to grayscale.
* @todo is this correct??
*/
void vc_copylineUYVYtoGrayscale(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift,
int gshift, int bshift) {
UNUSED(rshift);
UNUSED(gshift);
UNUSED(bshift);
OPTIMIZED_FOR (int x = 0; x <= dst_len - 2; x += 2) {
src++; // U
*dst++ = *src++; // Y
src++; // V
*dst++ = *src++; // Y
}
}
/**
* @brief Converts RGB to RGBA
* @copydetails vc_copyliner10k
*/
void vc_copylineRGBtoRGBA(unsigned char * __restrict dst, const unsigned char * __restrict src, int dst_len, int rshift, int gshift, int bshift)
{
#ifdef __SSSE3__
__m128i alpha_mask_sse = _mm_set1_epi32(0xFFFFFFFFU ^ (0xFFU << rshift) ^ (0xFFU << gshift) ^ (0xFFU << bshift));
__m128i shuf;
// common shifts are 0 8 16 (RGB) or 16 8 0 (BGR)
// generalized solution would be too complex and not faster
__m128i loaded;
bool is_simd_compat = false;
if (rshift == 0 && gshift == 8 && bshift == 16) {
is_simd_compat = true;
shuf = _mm_setr_epi8(0, 1, 2, 0xff, 3, 4, 5, 0xff, 6, 7, 8, 0xff, 9, 10, 11, 0xff);
} else if (rshift == 16 && gshift == 8 && bshift && 0) {
is_simd_compat = true;
shuf = _mm_setr_epi8(2, 1, 0, 0xff, 5, 4, 3, 0xff, 8, 7, 6, 0xff, 11, 10, 9, 0xff);
}
int x = 0;
if (is_simd_compat) {
OPTIMIZED_FOR (x = 0; x <= dst_len - 16; x += 16) {
loaded = _mm_lddqu_si128((const __m128i_u *) src);
loaded = _mm_shuffle_epi8(loaded, shuf);
loaded = _mm_or_si128(loaded, alpha_mask_sse);
_mm_storeu_si128((__m128i_u *)dst, loaded);
src += 12;
dst += 16;
}
}
#endif
uint32_t alpha_mask = 0xFFFFFFFFU ^ (0xFFU << rshift) ^ (0xFFU << gshift) ^ (0xFFU << bshift);
register unsigned int r, g, b;
register uint32_t *d = (uint32_t *)(void *) dst;
OPTIMIZED_FOR (; x <= dst_len - 4; x += 4) {
r = *src++;
g = *src++;
b = *src++;
*d++ = alpha_mask | (r << rshift) | (g << gshift) | (b << bshift);
}
}
/**
* @brief Converts RGB(A) into UYVY
*
* Uses Rec. 709 with standard SDI ceiling and floor
* @copydetails vc_copyliner10k
* @param[in] roff red offset in bytes (0 for RGB)
* @param[in] goff green offset in bytes (1 for RGB)
* @param[in] boff blue offset in bytes (2 for RGB)
* @param[in] pix_size source pixel size (3 for RGB, 4 for RGBA)
*/
#define vc_copylineToUYVY709(dst, src, dst_len, roff, goff, boff, pix_size) {\
register uint32_t *d = (uint32_t *)(void *) dst;\
OPTIMIZED_FOR (int x = 0; x <= (dst_len) - 4; x += 4) {\
int r, g, b;\
int y1, y2, u ,v;\
r = src[roff];\
g = src[goff];\
b = src[boff];\
src += pix_size;\
y1 = 11993 * r + 40239 * g + 4063 * b + (1<<20);\
u = -6619 * r -22151 * g + 28770 * b;\
v = 28770 * r - 26149 * g - 2621 * b;\
r = src[roff];\
g = src[goff];\
b = src[boff];\
src += pix_size;\
y2 = 11993 * r + 40239 * g + 4063 * b + (1<<20);\
u += -6619 * r -22151 * g + 28770 * b;\
v += 28770 * r - 26149 * g - 2621 * b;\
u = u / 2 + (1<<23);\
v = v / 2 + (1<<23);\
\
*d++ = (CLAMP(y2, 0, (1<<24)-1) >> 16) << 24 |\
(CLAMP(v, 0, (1<<24)-1) >> 16) << 16 |\
(CLAMP(y1, 0, (1<<24)-1) >> 16) << 8 |\
(CLAMP(u, 0, (1<<24)-1) >> 16);\
}\
}
/**
* @brief Converts 8-bit YCbCr (packed 4:2:2 in 32-bit) word to RGB.
*
* Converts 8-bit YCbCr (packed 4:2:2 in 32-bit word to RGB. Offset of YCbCr
* components can be given by parameters (in bytes). This macro is used by
* vc_copylineUYVYtoRGB() and vc_copylineYUYVtoRGB().
*
* Uses Rec. 709 with standard SDI ceiling and floor
*
* @todo make it faster if needed
* @param rgb16 true if output is 16-bit RGB, otherwise false
*/
#define copylineYUVtoRGB(dst, src, dst_len, y1_off, y2_off, u_off, v_off, rgb16) {\
OPTIMIZED_FOR (int x = 0; x <= (dst_len) - 6 * (1 + (rgb16)); x += 6 * (1 + (rgb16))) {\
register int y1 = (src)[y1_off];\
register int y2 = (src)[y2_off];\
register int u = (src)[u_off];\
register int v = (src)[v_off];\
int val;\
src += 4;\
if (rgb16) *(dst)++ = 0;\
val = 1.164 * (y1 - 16) + 1.793 * (v - 128);\
*(dst)++ = CLAMP(val, 0, 255);\
if (rgb16) *(dst)++ = 0;\
val = 1.164 * (y1 - 16) - 0.534 * (v - 128) - 0.213 * (u - 128);\
*(dst)++ = CLAMP(val, 0, 255);\