Skip to content

Commit 366a6bd

Browse files
committed
driver: video: sw_generator: support h264 pixel format
1 parent fa5a00f commit 366a6bd

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

drivers/video/video_sw_generator.c

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "video_ctrls.h"
1717
#include "video_device.h"
1818

19+
#include "video_h264_test.h"
20+
1921
LOG_MODULE_REGISTER(video_sw_generator, CONFIG_VIDEO_LOG_LEVEL);
2022

2123
#define VIDEO_PATTERN_COLOR_BAR 0
@@ -66,6 +68,7 @@ static const struct video_format_cap fmts[] = {
6668
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_SGRBG8),
6769
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_SBGGR8),
6870
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_SGBRG8),
71+
VIDEO_SW_GENERATOR_FORMAT_CAP(VIDEO_PIX_FMT_H264),
6972
{0},
7073
};
7174

@@ -215,6 +218,31 @@ static uint16_t video_sw_generator_fill_bayer8(uint8_t *buffer, uint16_t width,
215218
return 2;
216219
}
217220

221+
static uint16_t video_sw_generator_fill_h264(uint8_t *buffer, size_t buffer_size, uint32_t *bytes_filled)
222+
{
223+
/* Static counter to track position */
224+
static size_t position = 0;
225+
size_t copy_size;
226+
227+
/* Calculate copy size */
228+
size_t remaining = h264_test_data_len - position;
229+
copy_size = (remaining > buffer_size) ? buffer_size : remaining;
230+
231+
/* Copy H.264 data chunk to buffer */
232+
if (copy_size > 0) {
233+
memcpy(buffer, &h264_test_data[position], copy_size);
234+
position += copy_size;
235+
236+
/* Loop back to beginning when we reach the end */
237+
if (position >= h264_test_data_len) {
238+
position = 0;
239+
}
240+
}
241+
242+
*bytes_filled = copy_size;
243+
return 1;
244+
}
245+
218246
static int video_sw_generator_fill(const struct device *const dev, struct video_buffer *vbuf)
219247
{
220248
struct video_sw_generator_data *data = dev->data;
@@ -258,6 +286,9 @@ static int video_sw_generator_fill(const struct device *const dev, struct video_
258286
lines = video_sw_generator_fill_bayer8(vbuf->buffer, fmt->width, hflip,
259287
pattern_grbg_idx);
260288
break;
289+
case VIDEO_PIX_FMT_H264:
290+
lines = video_sw_generator_fill_h264(vbuf->buffer, vbuf->size, &(vbuf->bytesused));
291+
break;
261292
default:
262293
CODE_UNREACHABLE;
263294
break;
@@ -267,17 +298,20 @@ static int video_sw_generator_fill(const struct device *const dev, struct video_
267298
return -EINVAL;
268299
}
269300

270-
/* How much was filled in so far */
271-
vbuf->bytesused = data->fmt.pitch * lines;
301+
/* Skip duplication for compressed formats */
302+
if (video_bits_per_pixel(fmt->pixelformat) > 0) {
303+
/* How much was filled in so far */
304+
vbuf->bytesused = data->fmt.pitch * lines;
272305

273-
/* Duplicate the first line(s) all over the buffer */
274-
for (int h = lines; h < data->fmt.height; h += lines) {
275-
if (vbuf->size < vbuf->bytesused + pitch * lines) {
276-
LOG_WRN("Generation stopped early: buffer too small");
277-
break;
306+
/* Duplicate the first line(s) all over the buffer */
307+
for (int h = lines; h < data->fmt.height; h += lines) {
308+
if (vbuf->size < vbuf->bytesused + pitch * lines) {
309+
LOG_WRN("Generation stopped early: buffer too small");
310+
break;
311+
}
312+
memcpy(vbuf->buffer + h * pitch, vbuf->buffer, pitch * lines);
313+
vbuf->bytesused += pitch * lines;
278314
}
279-
memcpy(vbuf->buffer + h * pitch, vbuf->buffer, pitch * lines);
280-
vbuf->bytesused += pitch * lines;
281315
}
282316

283317
vbuf->timestamp = k_uptime_get_32();
@@ -473,10 +507,10 @@ static int video_sw_generator_init(const struct device *dev)
473507

474508
#define VIDEO_SW_GENERATOR_DEFINE(n) \
475509
static struct video_sw_generator_data video_sw_generator_data_##n = { \
476-
.fmt.width = 320, \
477-
.fmt.height = 160, \
478-
.fmt.pitch = 320 * 2, \
479-
.fmt.pixelformat = VIDEO_PIX_FMT_RGB565, \
510+
.fmt.width = 640, \
511+
.fmt.height = 320, \
512+
.fmt.pitch = 0, \
513+
.fmt.pixelformat = VIDEO_PIX_FMT_H264, \
480514
.frame_rate = DEFAULT_FRAME_RATE, \
481515
}; \
482516
\

0 commit comments

Comments
 (0)