1// Copyright 2016 Adrien Descamps
2// Distributed under BSD 3-Clause License
3#include "yuv_rgb.h"
4
5#define PRECISION 6
6#define PRECISION_FACTOR (1<<PRECISION)
7
8typedef struct
9{
10 uint8_t y_shift;
11 int16_t matrix[3][3];
12} RGB2YUVParam;
13// |Y| |y_shift| |matrix[0][0] matrix[0][1] matrix[0][2]| |R|
14// |U| = | 128 | + 1/PRECISION_FACTOR * |matrix[1][0] matrix[1][1] matrix[1][2]| * |G|
15// |V| | 128 | |matrix[2][0] matrix[2][1] matrix[2][2]| |B|
16
17typedef struct
18{
19 uint8_t y_shift;
20 int16_t y_factor;
21 int16_t v_r_factor;
22 int16_t u_g_factor;
23 int16_t v_g_factor;
24 int16_t u_b_factor;
25} YUV2RGBParam;
26// |R| |y_factor 0 v_r_factor| |Y-y_shift|
27// |G| = 1/PRECISION_FACTOR * |y_factor u_g_factor v_g_factor| * | U-128 |
28// |B| |y_factor u_b_factor 0 | | V-128 |
29
30#ifdef _MSC_VER
31#pragma warning(push)
32#pragma warning(disable : 26451)
33#endif
34
35#define V(value) (int16_t)((value*PRECISION_FACTOR)+0.5)
36
37// for ITU-T T.871, values can be found in section 7
38// for ITU-R BT.601-7 values are derived from equations in sections 2.5.1-2.5.3, assuming RGB is encoded using full range ([0-1]<->[0-255])
39// for ITU-R BT.709-6 values are derived from equations in sections 3.2-3.4, assuming RGB is encoded using full range ([0-1]<->[0-255])
40// for ITU-R BT.2020 values are assuming RGB is encoded using full 10-bit range ([0-1]<->[0-1023])
41// all values are rounded to the fourth decimal
42
43static const YUV2RGBParam YUV2RGB[] = {
44 // ITU-T T.871 (JPEG)
45 {/*.y_shift=*/ 0, /*.y_factor=*/ V(1.0), /*.v_r_factor=*/ V(1.402), /*.u_g_factor=*/ -V(0.3441), /*.v_g_factor=*/ -V(0.7141), /*.u_b_factor=*/ V(1.772)},
46 // ITU-R BT.601-7
47 {/*.y_shift=*/ 16, /*.y_factor=*/ V(1.1644), /*.v_r_factor=*/ V(1.596), /*.u_g_factor=*/ -V(0.3918), /*.v_g_factor=*/ -V(0.813), /*.u_b_factor=*/ V(2.0172)},
48 // ITU-R BT.709-6 full range
49 {/*.y_shift=*/ 0, /*.y_factor=*/ V(1.0), /*.v_r_factor=*/ V(1.581), /*.u_g_factor=*/ -V(0.1881), /*.v_g_factor=*/ -V(0.47), /*.u_b_factor=*/ V(1.8629)},
50 // ITU-R BT.709-6
51 {/*.y_shift=*/ 16, /*.y_factor=*/ V(1.1644), /*.v_r_factor=*/ V(1.7927), /*.u_g_factor=*/ -V(0.2132), /*.v_g_factor=*/ -V(0.5329), /*.u_b_factor=*/ V(2.1124)},
52 // ITU-R BT.2020 10-bit full range
53 {/*.y_shift=*/ 0, /*.y_factor=*/ V(1.0), /*.v_r_factor=*/ V(1.4760), /*.u_g_factor=*/ -V(0.1647), /*.v_g_factor=*/ -V(0.5719), /*.u_b_factor=*/ V(1.8832) }
54};
55
56static const RGB2YUVParam RGB2YUV[] = {
57 // ITU-T T.871 (JPEG)
58 {/*.y_shift=*/ 0, /*.matrix=*/ {{V(0.299), V(0.587), V(0.114)}, {-V(0.1687), -V(0.3313), V(0.5)}, {V(0.5), -V(0.4187), -V(0.0813)}}},
59 // ITU-R BT.601-7
60 {/*.y_shift=*/ 16, /*.matrix=*/ {{V(0.2568), V(0.5041), V(0.0979)}, {-V(0.1482), -V(0.291), V(0.4392)}, {V(0.4392), -V(0.3678), -V(0.0714)}}},
61 // ITU-R BT.709-6 full range
62 {/*.y_shift=*/ 0, /*.matrix=*/ {{V(0.2126), V(0.7152), V(0.0722)}, {-V(0.1141), -V(0.3839), V(0.498)}, {V(0.498), -V(0.4524), -V(0.0457)}}},
63 // ITU-R BT.709-6
64 {/*.y_shift=*/ 16, /*.matrix=*/ {{V(0.1826), V(0.6142), V(0.062)}, {-V(0.1006), -V(0.3386), V(0.4392)}, {V(0.4392), -V(0.3989), -V(0.0403)}}},
65 // ITU-R BT.2020 10-bit full range
66 {/*.y_shift=*/ 0, /*.matrix=*/ {{V(0.2627), V(0.6780), V(0.0593)}, {-V(0.1395), -V(0.3600), V(0.4995)}, {V(0.4995), -V(0.4593), -V(0.0402)}}},
67};
68
69#ifdef _MSC_VER
70#pragma warning(pop)
71#endif
72
73/* The various layouts of YUV data we support */
74#define YUV_FORMAT_420 1
75#define YUV_FORMAT_422 2
76#define YUV_FORMAT_NV12 3
77
78/* The various formats of RGB pixel that we support */
79#define RGB_FORMAT_RGB565 1
80#define RGB_FORMAT_RGB24 2
81#define RGB_FORMAT_RGBA 3
82#define RGB_FORMAT_BGRA 4
83#define RGB_FORMAT_ARGB 5
84#define RGB_FORMAT_ABGR 6
85#define RGB_FORMAT_XBGR2101010 7
86