Skip to content

Commit 50240eb

Browse files
committed
drivers: video: sw_generator: add support for bayer and YUV formats
This refactors the pattern generator functions to also offer bayer formats as input. 4 variant of bayer formats are proposed. The pixel packing is also now split from the color selection: only a single RGB and single YUV array used by all the pattern generators. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent d803873 commit 50240eb

File tree

1 file changed

+119
-48
lines changed

1 file changed

+119
-48
lines changed

drivers/video/video_sw_generator.c

Lines changed: 119 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ struct video_sw_generator_data {
4040
uint32_t frame_rate;
4141
};
4242

43+
#define VIDEO_SW_GENERATOR_FORMAT_CAP(pixfmt) \
44+
{ \
45+
.pixelformat = pixfmt, \
46+
.width_min = 64, \
47+
.width_max = 1920, \
48+
.height_min = 64, \
49+
.height_max = 1080, \
50+
.width_step = 1, \
51+
.height_step = 1, \
52+
}
53+
4354
static const struct video_format_cap fmts[] = {
44-
{
45-
.pixelformat = VIDEO_PIX_FMT_RGB565,
46-
.width_min = 64,
47-
.width_max = 1920,
48-
.height_min = 64,
49-
.height_max = 1080,
50-
.width_step = 1,
51-
.height_step = 1,
52-
},
53-
{
54-
.pixelformat = VIDEO_PIX_FMT_XRGB32,
55-
.width_min = 64,
56-
.width_max = 1920,
57-
.height_min = 64,
58-
.height_max = 1080,
59-
.width_step = 1,
60-
.height_step = 1,
61-
},
55+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_YUYV),
56+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_RGB565),
57+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_XRGB32),
58+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_RGGB8),
59+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_GRBG8),
60+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_BGGR8),
61+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_GBRG8),
6262
{0},
6363
};
6464

@@ -117,51 +117,122 @@ static int video_sw_generator_set_stream(const struct device *dev, bool enable)
117117
return 0;
118118
}
119119

120-
/* Black, Blue, Red, Purple, Green, Aqua, Yellow, White */
121-
uint16_t rgb565_colorbar_value[] = {
122-
0x0000, 0x001F, 0xF800, 0xF81F, 0x07E0, 0x07FF, 0xFFE0, 0xFFFF,
120+
static const uint8_t pattern_rggb8_idx[] = {0, 1, 1, 2};
121+
static const uint8_t pattern_bggr8_idx[] = {2, 1, 1, 0};
122+
static const uint8_t pattern_gbrg8_idx[] = {1, 2, 0, 1};
123+
static const uint8_t pattern_grbg8_idx[] = {1, 0, 2, 1};
124+
125+
/* White, Yellow, Cyan, Green, Magenta, Red, Blue, Black */
126+
127+
static const uint16_t pattern_8bars_yuv[8][3] = {
128+
{0xFF, 0x7F, 0x7F}, {0xFF, 0x00, 0xFF}, {0xFF, 0xFF, 0x00}, {0x7F, 0x00, 0x00},
129+
{0x00, 0xFF, 0xFF}, {0x00, 0x00, 0xFF}, {0x00, 0xFF, 0x00}, {0x00, 0x7F, 0x7F},
123130
};
124131

125-
uint32_t xrgb32_colorbar_value[] = {
126-
0xFF000000, 0xFF0000FF, 0xFFFF0000, 0xFFFF00FF,
127-
0xFF00FF00, 0xFF00FFFF, 0xFFFFFF00, 0xFFFFFFFF,
132+
static const uint16_t pattern_8bars_rgb[8][3] = {
133+
{0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0x00}, {0x00, 0xFF, 0xFF}, {0x00, 0xFF, 0x00},
134+
{0xFF, 0x00, 0xFF}, {0xFF, 0x00, 0x00}, {0x00, 0x00, 0xFF}, {0x00, 0x00, 0x00},
128135
};
129136

130-
static void video_sw_generator_fill_colorbar(struct video_sw_generator_data *data,
131-
struct video_buffer *vbuf)
137+
static int video_sw_generator_fill_yuyv(uint8_t *buffer, uint16_t pitch)
138+
{
139+
for (size_t i = 0; i + 4 <= pitch; i += 4) {
140+
buffer[i + 0] = pattern_8bars_yuv[8 * i / pitch][0];
141+
buffer[i + 1] = pattern_8bars_yuv[8 * i / pitch][1];
142+
buffer[i + 2] = pattern_8bars_yuv[8 * i / pitch][0];
143+
buffer[i + 3] = pattern_8bars_yuv[8 * i / pitch][2];
144+
}
145+
return 1;
146+
}
147+
148+
static int video_sw_generator_fill_xrgb32(uint8_t *buffer, uint16_t pitch)
149+
{
150+
for (size_t i = 0; i < pitch; i += 4) {
151+
buffer[i + 0] = 0xff;
152+
buffer[i + 1] = pattern_8bars_rgb[8 * i / pitch][0];
153+
buffer[i + 2] = pattern_8bars_rgb[8 * i / pitch][1];
154+
buffer[i + 3] = pattern_8bars_rgb[8 * i / pitch][2];
155+
}
156+
return 1;
157+
}
158+
159+
static int video_sw_generator_fill_rgb565(uint8_t *buffer, uint16_t pitch)
160+
{
161+
for (size_t i = 0; i < pitch; i += 1) {
162+
uint8_t r = pattern_8bars_rgb[16 * i / pitch][0] >> (8 - 5);
163+
uint8_t g = pattern_8bars_rgb[16 * i / pitch][1] >> (8 - 6);
164+
uint8_t b = pattern_8bars_rgb[16 * i / pitch][2] >> (8 - 5);
165+
166+
((uint16_t *)buffer)[i] = sys_cpu_to_le16((r << 11) | (g << 6) | (b << 0));
167+
}
168+
return 1;
169+
}
170+
171+
static int video_sw_generator_fill_bayer(uint8_t *buffer, uint16_t pitch, const uint8_t *idx)
132172
{
133-
int bw = data->fmt.width / 8;
173+
uint8_t *row0 = buffer + 0;
174+
uint8_t *row1 = buffer + pitch;
175+
176+
for (size_t i = 0; i + 2 <= pitch; i += 2) {
177+
row0[i + 0] = pattern_8bars_rgb[8 * i / pitch][idx[0]];
178+
row0[i + 1] = pattern_8bars_rgb[8 * i / pitch][idx[1]];
179+
row1[i + 0] = pattern_8bars_rgb[8 * i / pitch][idx[2]];
180+
row1[i + 1] = pattern_8bars_rgb[8 * i / pitch][idx[3]];
181+
}
182+
return 2;
183+
}
134184

135-
vbuf->bytesused = 0;
185+
static void video_sw_generator_fill(const struct device *const dev, struct video_buffer *vbuf)
186+
{
187+
struct video_sw_generator_data *data = dev->data;
188+
struct video_format *fmt = &data->fmt;
189+
int lines = 1;
136190

137-
if (vbuf->size < data->fmt.pitch) {
138-
LOG_WRN("Buffer %p has only %u bytes, shorter than pitch %u",
191+
if (vbuf->size < data->fmt.pitch * 2) {
192+
LOG_WRN("Buffer %p has only %u bytes, shorter than twice the pitch %u",
139193
vbuf, vbuf->size, data->fmt.pitch);
140194
return;
141195
}
142196

143-
/* Generate the first line of data */
144-
for (int w = 0, i = 0; w < data->fmt.width; w++) {
145-
int color_idx = data->ctrl_vflip ? 7 - w / bw : w / bw;
146-
147-
if (data->fmt.pixelformat == VIDEO_PIX_FMT_RGB565) {
148-
uint16_t *pixel = (uint16_t *)&vbuf->buffer[i];
149-
*pixel = sys_cpu_to_le16(rgb565_colorbar_value[color_idx]);
150-
} else if (data->fmt.pixelformat == VIDEO_PIX_FMT_XRGB32) {
151-
uint32_t *pixel = (uint32_t *)&vbuf->buffer[i];
152-
*pixel = sys_cpu_to_le32(xrgb32_colorbar_value[color_idx]);
153-
}
154-
i += video_bits_per_pixel(data->fmt.pixelformat) / BITS_PER_BYTE;
197+
/* Fill the first row of the emulated framebuffer */
198+
switch (data->fmt.pixelformat) {
199+
case VIDEO_PIX_FMT_YUYV:
200+
lines = video_sw_generator_fill_yuyv(vbuf->buffer, fmt->pitch);
201+
break;
202+
case VIDEO_PIX_FMT_XRGB32:
203+
lines = video_sw_generator_fill_xrgb32(vbuf->buffer, fmt->pitch);
204+
break;
205+
case VIDEO_PIX_FMT_RGB565:
206+
lines = video_sw_generator_fill_rgb565(vbuf->buffer, fmt->pitch);
207+
break;
208+
case VIDEO_PIX_FMT_RGGB8:
209+
lines = video_sw_generator_fill_bayer(vbuf->buffer, fmt->pitch, pattern_rggb8_idx);
210+
break;
211+
case VIDEO_PIX_FMT_GBRG8:
212+
lines = video_sw_generator_fill_bayer(vbuf->buffer, fmt->pitch, pattern_gbrg8_idx);
213+
break;
214+
case VIDEO_PIX_FMT_BGGR8:
215+
lines = video_sw_generator_fill_bayer(vbuf->buffer, fmt->pitch, pattern_bggr8_idx);
216+
break;
217+
case VIDEO_PIX_FMT_GRBG8:
218+
lines = video_sw_generator_fill_bayer(vbuf->buffer, fmt->pitch, pattern_grbg8_idx);
219+
break;
220+
default:
221+
LOG_WRN("Unsupported pixel format %x, filling with 0x55", data->fmt.pixelformat);
222+
memset(vbuf->buffer, 0x55, data->fmt.pitch);
223+
break;
155224
}
156225

226+
/* How much was filled insofar */
227+
vbuf->bytesused = data->fmt.pitch * lines;
228+
157229
/* Duplicate the first line all over the buffer */
158-
for (int h = 1; h < data->fmt.height; h++) {
159-
if (vbuf->size < vbuf->bytesused + data->fmt.pitch) {
230+
for (int h = lines; h < data->fmt.height; h += lines) {
231+
if (vbuf->size < vbuf->bytesused + data->fmt.pitch * lines) {
160232
break;
161233
}
162-
163-
memcpy(vbuf->buffer + h * data->fmt.pitch, vbuf->buffer, data->fmt.pitch);
164-
vbuf->bytesused += data->fmt.pitch;
234+
memcpy(vbuf->buffer + h * data->fmt.pitch, vbuf->buffer, data->fmt.pitch * lines);
235+
vbuf->bytesused += data->fmt.pitch * lines;
165236
}
166237

167238
vbuf->timestamp = k_uptime_get_32();
@@ -185,7 +256,7 @@ static void video_sw_generator_worker(struct k_work *work)
185256

186257
switch (data->pattern) {
187258
case VIDEO_PATTERN_COLOR_BAR:
188-
video_sw_generator_fill_colorbar(data, vbuf);
259+
video_sw_generator_fill(data->dev, vbuf);
189260
break;
190261
}
191262

0 commit comments

Comments
 (0)