1
/**************************************************************************
3
* Copyright 2010 VMware, Inc.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
* USE OR OTHER DEALINGS IN THE SOFTWARE.
22
* The above copyright notice and this permission notice (including the
23
* next paragraph) shall be included in all copies or substantial portions
26
**************************************************************************/
31
* YUV and RGB subsampled formats conversion.
33
* @author Jose Fonseca <jfonseca@vmware.com>
37
#include "util/u_format_yuv.h"
41
util_format_r8g8_b8g8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
42
const uint8_t *src_row, unsigned src_stride,
43
unsigned width, unsigned height)
47
for (y = 0; y < height; y += 1) {
49
const uint32_t *src = (const uint32_t *)src_row;
53
for (x = 0; x + 1 < width; x += 2) {
56
#ifdef PIPE_ARCH_BIG_ENDIAN
57
value = util_bswap32(value);
60
r = ubyte_to_float((value >> 0) & 0xff);
61
g0 = ubyte_to_float((value >> 8) & 0xff);
62
b = ubyte_to_float((value >> 16) & 0xff);
63
g1 = ubyte_to_float((value >> 24) & 0xff);
68
dst[3] = 1.0f; /* a */
74
dst[3] = 1.0f; /* a */
81
#ifdef PIPE_ARCH_BIG_ENDIAN
82
value = util_bswap32(value);
85
r = ubyte_to_float((value >> 0) & 0xff);
86
g0 = ubyte_to_float((value >> 8) & 0xff);
87
b = ubyte_to_float((value >> 16) & 0xff);
88
g1 = ubyte_to_float((value >> 24) & 0xff);
93
dst[3] = 1.0f; /* a */
96
src_row += src_stride/sizeof(*src_row);
97
dst_row += dst_stride/sizeof(*dst_row);
103
util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
104
const uint8_t *src_row, unsigned src_stride,
105
unsigned width, unsigned height)
109
for (y = 0; y < height; y += 1) {
110
uint8_t *dst = dst_row;
111
const uint32_t *src = (const uint32_t *)src_row;
113
uint8_t r, g0, g1, b;
115
for (x = 0; x + 1 < width; x += 2) {
118
#ifdef PIPE_ARCH_BIG_ENDIAN
119
value = util_bswap32(value);
122
r = (value >> 0) & 0xff;
123
g0 = (value >> 8) & 0xff;
124
b = (value >> 16) & 0xff;
125
g1 = (value >> 24) & 0xff;
130
dst[3] = 0xff; /* a */
136
dst[3] = 0xff; /* a */
143
#ifdef PIPE_ARCH_BIG_ENDIAN
144
value = util_bswap32(value);
147
r = (value >> 0) & 0xff;
148
g0 = (value >> 8) & 0xff;
149
b = (value >> 16) & 0xff;
150
g1 = (value >> 24) & 0xff;
155
dst[3] = 0xff; /* a */
158
src_row += src_stride/sizeof(*src_row);
159
dst_row += dst_stride/sizeof(*dst_row);
165
util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
166
const float *src_row, unsigned src_stride,
167
unsigned width, unsigned height)
171
for (y = 0; y < height; y += 1) {
172
const float *src = src_row;
173
uint32_t *dst = (uint32_t *)dst_row;
177
for (x = 0; x + 1 < width; x += 2) {
178
r = 0.5f*(src[0] + src[4]);
181
b = 0.5f*(src[2] + src[6]);
183
value = float_to_ubyte(r);
184
value |= float_to_ubyte(g0) << 8;
185
value |= float_to_ubyte(b) << 16;
186
value |= float_to_ubyte(g1) << 24;
188
#ifdef PIPE_ARCH_BIG_ENDIAN
189
value = util_bswap32(value);
203
value = float_to_ubyte(r);
204
value |= float_to_ubyte(g0) << 8;
205
value |= float_to_ubyte(b) << 16;
206
value |= float_to_ubyte(g1) << 24;
208
#ifdef PIPE_ARCH_BIG_ENDIAN
209
value = util_bswap32(value);
215
dst_row += dst_stride/sizeof(*dst_row);
216
src_row += src_stride/sizeof(*src_row);
222
util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
223
const uint8_t *src_row, unsigned src_stride,
224
unsigned width, unsigned height)
228
for (y = 0; y < height; y += 1) {
229
const uint8_t *src = src_row;
230
uint32_t *dst = (uint32_t *)dst_row;
231
uint32_t r, g0, g1, b;
234
for (x = 0; x + 1 < width; x += 2) {
235
r = (src[0] + src[4] + 1) >> 1;
238
b = (src[2] + src[6] + 1) >> 1;
245
#ifdef PIPE_ARCH_BIG_ENDIAN
246
value = util_bswap32(value);
265
#ifdef PIPE_ARCH_BIG_ENDIAN
266
value = util_bswap32(value);
272
dst_row += dst_stride/sizeof(*dst_row);
273
src_row += src_stride/sizeof(*src_row);
279
util_format_r8g8_b8g8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
280
unsigned i, unsigned j)
285
dst[0] = ubyte_to_float(src[0]); /* r */
286
dst[1] = ubyte_to_float(src[1 + 2*i]); /* g */
287
dst[2] = ubyte_to_float(src[2]); /* b */
288
dst[3] = 1.0f; /* a */
293
util_format_g8r8_g8b8_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
294
const uint8_t *src_row, unsigned src_stride,
295
unsigned width, unsigned height)
299
for (y = 0; y < height; y += 1) {
300
float *dst = dst_row;
301
const uint32_t *src = (const uint32_t *)src_row;
305
for (x = 0; x + 1 < width; x += 2) {
308
#ifdef PIPE_ARCH_BIG_ENDIAN
309
value = util_bswap32(value);
312
g0 = ubyte_to_float((value >> 0) & 0xff);
313
r = ubyte_to_float((value >> 8) & 0xff);
314
g1 = ubyte_to_float((value >> 16) & 0xff);
315
b = ubyte_to_float((value >> 24) & 0xff);
320
dst[3] = 1.0f; /* a */
326
dst[3] = 1.0f; /* a */
333
#ifdef PIPE_ARCH_BIG_ENDIAN
334
value = util_bswap32(value);
337
g0 = ubyte_to_float((value >> 0) & 0xff);
338
r = ubyte_to_float((value >> 8) & 0xff);
339
g1 = ubyte_to_float((value >> 16) & 0xff);
340
b = ubyte_to_float((value >> 24) & 0xff);
345
dst[3] = 1.0f; /* a */
348
src_row += src_stride/sizeof(*src_row);
349
dst_row += dst_stride/sizeof(*dst_row);
355
util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
356
const uint8_t *src_row, unsigned src_stride,
357
unsigned width, unsigned height)
361
for (y = 0; y < height; y += 1) {
362
uint8_t *dst = dst_row;
363
const uint32_t *src = (const uint32_t *)src_row;
365
uint8_t r, g0, g1, b;
367
for (x = 0; x + 1 < width; x += 2) {
370
#ifdef PIPE_ARCH_BIG_ENDIAN
371
value = util_bswap32(value);
374
g0 = (value >> 0) & 0xff;
375
r = (value >> 8) & 0xff;
376
g1 = (value >> 16) & 0xff;
377
b = (value >> 24) & 0xff;
382
dst[3] = 0xff; /* a */
388
dst[3] = 0xff; /* a */
395
#ifdef PIPE_ARCH_BIG_ENDIAN
396
value = util_bswap32(value);
399
g0 = (value >> 0) & 0xff;
400
r = (value >> 8) & 0xff;
401
g1 = (value >> 16) & 0xff;
402
b = (value >> 24) & 0xff;
407
dst[3] = 0xff; /* a */
410
src_row += src_stride/sizeof(*src_row);
411
dst_row += dst_stride/sizeof(*dst_row);
417
util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
418
const float *src_row, unsigned src_stride,
419
unsigned width, unsigned height)
423
for (y = 0; y < height; y += 1) {
424
const float *src = src_row;
425
uint32_t *dst = (uint32_t *)dst_row;
429
for (x = 0; x + 1 < width; x += 2) {
430
r = 0.5f*(src[0] + src[4]);
433
b = 0.5f*(src[2] + src[6]);
435
value = float_to_ubyte(g0);
436
value |= float_to_ubyte(r) << 8;
437
value |= float_to_ubyte(g1) << 16;
438
value |= float_to_ubyte(b) << 24;
440
#ifdef PIPE_ARCH_BIG_ENDIAN
441
value = util_bswap32(value);
455
value = float_to_ubyte(g0);
456
value |= float_to_ubyte(r) << 8;
457
value |= float_to_ubyte(g1) << 16;
458
value |= float_to_ubyte(b) << 24;
460
#ifdef PIPE_ARCH_BIG_ENDIAN
461
value = util_bswap32(value);
467
dst_row += dst_stride/sizeof(*dst_row);
468
src_row += src_stride/sizeof(*src_row);
474
util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
475
const uint8_t *src_row, unsigned src_stride,
476
unsigned width, unsigned height)
480
for (y = 0; y < height; y += 1) {
481
const uint8_t *src = src_row;
482
uint32_t *dst = (uint32_t *)dst_row;
483
uint32_t r, g0, g1, b;
486
for (x = 0; x + 1 < width; x += 2) {
487
r = (src[0] + src[4] + 1) >> 1;
490
b = (src[2] + src[6] + 1) >> 1;
497
#ifdef PIPE_ARCH_BIG_ENDIAN
498
value = util_bswap32(value);
517
#ifdef PIPE_ARCH_BIG_ENDIAN
518
value = util_bswap32(value);
524
dst_row += dst_stride/sizeof(*dst_row);
525
src_row += src_stride/sizeof(*src_row);
531
util_format_g8r8_g8b8_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
532
unsigned i, unsigned j)
537
dst[0] = ubyte_to_float(src[1]); /* r */
538
dst[1] = ubyte_to_float(src[0 + 2*i]); /* g */
539
dst[2] = ubyte_to_float(src[3]); /* b */
540
dst[3] = 1.0f; /* a */
545
util_format_uyvy_unpack_rgba_float(float *dst_row, unsigned dst_stride,
546
const uint8_t *src_row, unsigned src_stride,
547
unsigned width, unsigned height)
551
for (y = 0; y < height; y += 1) {
552
float *dst = dst_row;
553
const uint32_t *src = (const uint32_t *)src_row;
555
uint8_t y0, y1, u, v;
557
for (x = 0; x + 1 < width; x += 2) {
560
#ifdef PIPE_ARCH_BIG_ENDIAN
561
value = util_bswap32(value);
564
u = (value >> 0) & 0xff;
565
y0 = (value >> 8) & 0xff;
566
v = (value >> 16) & 0xff;
567
y1 = (value >> 24) & 0xff;
569
util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);
570
dst[3] = 1.0f; /* a */
573
util_format_yuv_to_rgb_float(y1, u, v, &dst[0], &dst[1], &dst[2]);
574
dst[3] = 1.0f; /* a */
581
#ifdef PIPE_ARCH_BIG_ENDIAN
582
value = util_bswap32(value);
585
u = (value >> 0) & 0xff;
586
y0 = (value >> 8) & 0xff;
587
v = (value >> 16) & 0xff;
588
y1 = (value >> 24) & 0xff;
590
util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);
591
dst[3] = 1.0f; /* a */
594
src_row += src_stride/sizeof(*src_row);
595
dst_row += dst_stride/sizeof(*dst_row);
601
util_format_uyvy_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
602
const uint8_t *src_row, unsigned src_stride,
603
unsigned width, unsigned height)
607
for (y = 0; y < height; y += 1) {
608
uint8_t *dst = dst_row;
609
const uint32_t *src = (const uint32_t *)src_row;
611
uint8_t y0, y1, u, v;
613
for (x = 0; x + 1 < width; x += 2) {
616
#ifdef PIPE_ARCH_BIG_ENDIAN
617
value = util_bswap32(value);
620
u = (value >> 0) & 0xff;
621
y0 = (value >> 8) & 0xff;
622
v = (value >> 16) & 0xff;
623
y1 = (value >> 24) & 0xff;
625
util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);
626
dst[3] = 0xff; /* a */
629
util_format_yuv_to_rgb_8unorm(y1, u, v, &dst[0], &dst[1], &dst[2]);
630
dst[3] = 0xff; /* a */
637
#ifdef PIPE_ARCH_BIG_ENDIAN
638
value = util_bswap32(value);
641
u = (value >> 0) & 0xff;
642
y0 = (value >> 8) & 0xff;
643
v = (value >> 16) & 0xff;
644
y1 = (value >> 24) & 0xff;
646
util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);
647
dst[3] = 0xff; /* a */
650
src_row += src_stride/sizeof(*src_row);
651
dst_row += dst_stride/sizeof(*dst_row);
657
util_format_uyvy_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
658
const float *src_row, unsigned src_stride,
659
unsigned width, unsigned height)
663
for (y = 0; y < height; y += 1) {
664
const float *src = src_row;
665
uint32_t *dst = (uint32_t *)dst_row;
666
uint8_t y0, y1, u, v;
669
for (x = 0; x + 1 < width; x += 2) {
670
uint8_t y0, y1, u0, u1, v0, v1, u, v;
672
util_format_rgb_float_to_yuv(src[0], src[1], src[2],
674
util_format_rgb_float_to_yuv(src[4], src[5], src[6],
677
u = (u0 + u1 + 1) >> 1;
678
v = (v0 + v1 + 1) >> 1;
685
#ifdef PIPE_ARCH_BIG_ENDIAN
686
value = util_bswap32(value);
695
util_format_rgb_float_to_yuv(src[0], src[1], src[2],
704
#ifdef PIPE_ARCH_BIG_ENDIAN
705
value = util_bswap32(value);
711
dst_row += dst_stride/sizeof(*dst_row);
712
src_row += src_stride/sizeof(*src_row);
718
util_format_uyvy_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
719
const uint8_t *src_row, unsigned src_stride,
720
unsigned width, unsigned height)
724
for (y = 0; y < height; y += 1) {
725
const uint8_t *src = src_row;
726
uint32_t *dst = (uint32_t *)dst_row;
727
uint8_t y0, y1, u, v;
730
for (x = 0; x + 1 < width; x += 2) {
731
uint8_t y0, y1, u0, u1, v0, v1, u, v;
733
util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],
735
util_format_rgb_8unorm_to_yuv(src[4], src[5], src[6],
738
u = (u0 + u1 + 1) >> 1;
739
v = (v0 + v1 + 1) >> 1;
746
#ifdef PIPE_ARCH_BIG_ENDIAN
747
value = util_bswap32(value);
756
util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],
765
#ifdef PIPE_ARCH_BIG_ENDIAN
766
value = util_bswap32(value);
772
dst_row += dst_stride/sizeof(*dst_row);
773
src_row += src_stride/sizeof(*src_row);
779
util_format_uyvy_fetch_rgba_float(float *dst, const uint8_t *src,
780
unsigned i, unsigned j)
791
util_format_yuv_to_rgb_float(y, u, v, &dst[0], &dst[1], &dst[2]);
798
util_format_yuyv_unpack_rgba_float(float *dst_row, unsigned dst_stride,
799
const uint8_t *src_row, unsigned src_stride,
800
unsigned width, unsigned height)
804
for (y = 0; y < height; y += 1) {
805
float *dst = dst_row;
806
const uint32_t *src = (const uint32_t *)src_row;
808
uint8_t y0, y1, u, v;
810
for (x = 0; x + 1 < width; x += 2) {
813
#ifdef PIPE_ARCH_BIG_ENDIAN
814
value = util_bswap32(value);
817
y0 = (value >> 0) & 0xff;
818
u = (value >> 8) & 0xff;
819
y1 = (value >> 16) & 0xff;
820
v = (value >> 24) & 0xff;
822
util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);
823
dst[3] = 1.0f; /* a */
826
util_format_yuv_to_rgb_float(y1, u, v, &dst[0], &dst[1], &dst[2]);
827
dst[3] = 1.0f; /* a */
834
#ifdef PIPE_ARCH_BIG_ENDIAN
835
value = util_bswap32(value);
838
y0 = (value >> 0) & 0xff;
839
u = (value >> 8) & 0xff;
840
y1 = (value >> 16) & 0xff;
841
v = (value >> 24) & 0xff;
843
util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);
844
dst[3] = 1.0f; /* a */
847
src_row += src_stride/sizeof(*src_row);
848
dst_row += dst_stride/sizeof(*dst_row);
854
util_format_yuyv_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
855
const uint8_t *src_row, unsigned src_stride,
856
unsigned width, unsigned height)
860
for (y = 0; y < height; y += 1) {
861
uint8_t *dst = dst_row;
862
const uint32_t *src = (const uint32_t *)src_row;
864
uint8_t y0, y1, u, v;
866
for (x = 0; x + 1 < width; x += 2) {
869
#ifdef PIPE_ARCH_BIG_ENDIAN
870
value = util_bswap32(value);
873
y0 = (value >> 0) & 0xff;
874
u = (value >> 8) & 0xff;
875
y1 = (value >> 16) & 0xff;
876
v = (value >> 24) & 0xff;
878
util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);
879
dst[3] = 0xff; /* a */
882
util_format_yuv_to_rgb_8unorm(y1, u, v, &dst[0], &dst[1], &dst[2]);
883
dst[3] = 0xff; /* a */
890
#ifdef PIPE_ARCH_BIG_ENDIAN
891
value = util_bswap32(value);
894
y0 = (value >> 0) & 0xff;
895
u = (value >> 8) & 0xff;
896
y1 = (value >> 16) & 0xff;
897
v = (value >> 24) & 0xff;
899
util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);
900
dst[3] = 0xff; /* a */
903
src_row += src_stride/sizeof(*src_row);
904
dst_row += dst_stride/sizeof(*dst_row);
910
util_format_yuyv_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
911
const float *src_row, unsigned src_stride,
912
unsigned width, unsigned height)
916
for (y = 0; y < height; y += 1) {
917
const float *src = src_row;
918
uint32_t *dst = (uint32_t *)dst_row;
919
uint8_t y0, y1, u, v;
922
for (x = 0; x + 1 < width; x += 2) {
923
uint8_t y0, y1, u0, u1, v0, v1, u, v;
925
util_format_rgb_float_to_yuv(src[0], src[1], src[2],
927
util_format_rgb_float_to_yuv(src[4], src[5], src[6],
930
u = (u0 + u1 + 1) >> 1;
931
v = (v0 + v1 + 1) >> 1;
938
#ifdef PIPE_ARCH_BIG_ENDIAN
939
value = util_bswap32(value);
948
util_format_rgb_float_to_yuv(src[0], src[1], src[2],
957
#ifdef PIPE_ARCH_BIG_ENDIAN
958
value = util_bswap32(value);
964
dst_row += dst_stride/sizeof(*dst_row);
965
src_row += src_stride/sizeof(*src_row);
971
util_format_yuyv_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
972
const uint8_t *src_row, unsigned src_stride,
973
unsigned width, unsigned height)
977
for (y = 0; y < height; y += 1) {
978
const uint8_t *src = src_row;
979
uint32_t *dst = (uint32_t *)dst_row;
980
uint8_t y0, y1, u, v;
983
for (x = 0; x + 1 < width; x += 2) {
984
uint8_t y0, y1, u0, u1, v0, v1, u, v;
986
util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],
988
util_format_rgb_8unorm_to_yuv(src[4], src[5], src[6],
991
u = (u0 + u1 + 1) >> 1;
992
v = (v0 + v1 + 1) >> 1;
999
#ifdef PIPE_ARCH_BIG_ENDIAN
1000
value = util_bswap32(value);
1009
util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],
1018
#ifdef PIPE_ARCH_BIG_ENDIAN
1019
value = util_bswap32(value);
1025
dst_row += dst_stride/sizeof(*dst_row);
1026
src_row += src_stride/sizeof(*src_row);
1032
util_format_yuyv_fetch_rgba_float(float *dst, const uint8_t *src,
1033
unsigned i, unsigned j)
1044
util_format_yuv_to_rgb_float(y, u, v, &dst[0], &dst[1], &dst[2]);