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
|
#include <drm_fourcc.h>
#include <stdexcept>
#include "framebuffer.h"
#include "color.h"
#include "conv.h"
namespace kms
{
static RGB read_rgb(const Framebuffer& fb, int x, int y)
{
uint32_t *pc = (uint32_t *)(fb.map(0) + fb.stride(0) * y);
uint32_t c = pc[x];
return RGB((c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
}
static YUV read_rgb_as_yuv(const Framebuffer& fb, int x, int y)
{
RGB rgb = read_rgb(fb, x, y);
return YUV(rgb);
}
static void fb_rgb_to_packed_yuv(Framebuffer& dst_fb, const Framebuffer& src_fb)
{
unsigned w = src_fb.width();
unsigned h = src_fb.height();
uint8_t *dst = dst_fb.map(0);
for (unsigned y = 0; y < h; ++y) {
for (unsigned x = 0; x < w; x += 2) {
YUV yuv1 = read_rgb_as_yuv(src_fb, x + 0, y);
YUV yuv2 = read_rgb_as_yuv(src_fb, x + 1, y);
switch (dst_fb.format()) {
case DRM_FORMAT_UYVY:
dst[x * 2 + 0] = (yuv1.u + yuv2.u) / 2;
dst[x * 2 + 1] = yuv1.y;
dst[x * 2 + 2] = (yuv1.v + yuv2.v) / 2;
dst[x * 2 + 3] = yuv2.y;
break;
case DRM_FORMAT_YUYV:
dst[x * 2 + 0] = yuv1.y;
dst[x * 2 + 1] = (yuv1.u + yuv2.u) / 2;
dst[x * 2 + 2] = yuv2.y;
dst[x * 2 + 3] = (yuv1.v + yuv2.v) / 2;
break;
default:
throw std::invalid_argument("fo");
}
}
dst += dst_fb.stride(0);
}
}
static void fb_rgb_to_semiplanar_yuv(Framebuffer& dst_fb, const Framebuffer& src_fb)
{
unsigned w = src_fb.width();
unsigned h = src_fb.height();
uint8_t *dst_y = dst_fb.map(0);
uint8_t *dst_uv = dst_fb.map(1);
for (unsigned y = 0; y < h; ++y) {
for (unsigned x = 0; x < w; ++x) {
YUV yuv = read_rgb_as_yuv(src_fb, x, y);
dst_y[x] = yuv.y;
}
dst_y += dst_fb.stride(0);
}
for (unsigned y = 0; y < h; y += 2) {
for (unsigned x = 0; x < w; x += 2) {
YUV yuv00 = read_rgb_as_yuv(src_fb, x + 0, y + 0);
YUV yuv01 = read_rgb_as_yuv(src_fb, x + 1, y + 0);
YUV yuv10 = read_rgb_as_yuv(src_fb, x + 0, y + 1);
YUV yuv11 = read_rgb_as_yuv(src_fb, x + 1, y + 1);
unsigned u = (yuv00.u + yuv01.u + yuv10.u + yuv11.u) / 4;
unsigned v = (yuv00.v + yuv01.v + yuv10.v + yuv11.v) / 4;
dst_uv[x + 0] = u;
dst_uv[x + 1] = v;
}
dst_uv += dst_fb.stride(1);
}
}
static void fb_rgb_to_rgb565(Framebuffer& dst_fb, const Framebuffer& src_fb)
{
unsigned w = src_fb.width();
unsigned h = src_fb.height();
uint8_t *dst = dst_fb.map(0);
for (unsigned y = 0; y < h; ++y) {
for (unsigned x = 0; x < w; ++x) {
RGB rgb = read_rgb(src_fb, x, y);
unsigned r = rgb.r * 32 / 256;
unsigned g = rgb.g * 64 / 256;
unsigned b = rgb.b * 32 / 256;
((uint16_t *)dst)[x] = (r << 11) | (g << 5) | (b << 0);
}
dst += dst_fb.stride(0);
}
}
void color_convert(Framebuffer& dst, const Framebuffer &src)
{
switch (dst.format()) {
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
fb_rgb_to_semiplanar_yuv(dst, src);
break;
case DRM_FORMAT_YUYV:
case DRM_FORMAT_UYVY:
fb_rgb_to_packed_yuv(dst, src);
break;
case DRM_FORMAT_RGB565:
fb_rgb_to_rgb565(dst, src);
break;
default:
throw std::invalid_argument("fo");
}
}
}
|