Skip to content

drivers: video: allow allocation with a header preceding the buffer #79168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions drivers/video/video_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct mem_block {

static struct mem_block video_block[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX];

struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
struct video_buffer *z_video_buffer_alloc(size_t header_size, size_t buffer_size, size_t align)
{
struct video_buffer *vbuf = NULL;
struct mem_block *block;
Expand All @@ -48,23 +48,19 @@ struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
}

/* Alloc buffer memory */
block->data = VIDEO_COMMON_HEAP_ALLOC(align, size, K_FOREVER);
block->data = VIDEO_COMMON_HEAP_ALLOC(align, header_size + buffer_size, K_FOREVER);
if (block->data == NULL) {
return NULL;
}

vbuf->buffer = block->data;
vbuf->size = size;
vbuf->header = block->data;
vbuf->buffer = block->data + header_size;
vbuf->size = buffer_size;
vbuf->bytesused = 0;

return vbuf;
}

struct video_buffer *video_buffer_alloc(size_t size)
{
return video_buffer_aligned_alloc(size, sizeof(void *));
}

void video_buffer_release(struct video_buffer *vbuf)
{
struct mem_block *block = NULL;
Expand Down
32 changes: 29 additions & 3 deletions include/zephyr/drivers/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ struct video_caps {
struct video_buffer {
/** pointer to driver specific data. */
void *driver_data;
/** pointer to the start of the buffer. */
/** pointer to the start of the buffer header. */
uint8_t *header;
/** pointer to the start of the buffer active region. */
uint8_t *buffer;
/** size of the buffer in bytes. */
uint32_t size;
Expand Down Expand Up @@ -739,6 +741,8 @@ static inline int video_set_signal(const struct device *dev,
return api->set_signal(dev, ep, signal);
}

struct video_buffer *z_video_buffer_alloc(size_t header_size, size_t buffer_size, size_t align);

/**
* @brief Allocate aligned video buffer.
*
Expand All @@ -747,7 +751,26 @@ static inline int video_set_signal(const struct device *dev,
*
* @retval pointer to allocated video buffer
*/
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align);
static struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
{
return z_video_buffer_alloc(0, size, align);
}

/**
* @brief Allocate video buffer with a header space available before it.
*
* This allows to access an extra @c vbuf->header field with @c headroom bytes
* before the start of @c vbuf->buffer.
*
* @param header_size Size available before the payload (in bytes).
* @param buffer_size Size of the video buffer payload (in bytes).
*
* @retval pointer to allocated video buffer
*/
static struct video_buffer *video_buffer_header_alloc(size_t header_size, size_t buffer_size)
{
return z_video_buffer_alloc(header_size, buffer_size, sizeof(void *));
}

/**
* @brief Allocate video buffer.
Expand All @@ -756,7 +779,10 @@ struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align);
*
* @retval pointer to allocated video buffer
*/
struct video_buffer *video_buffer_alloc(size_t size);
static struct video_buffer *video_buffer_alloc(size_t size)
{
return z_video_buffer_alloc(0, size, sizeof(void *));
}

/**
* @brief Release a video buffer.
Expand Down
Loading