2019-07-04 15:30:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/video/video_framebuff.c
|
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-07-04 15:30:50 +02:00
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-07-04 15:30:50 +02:00
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-07-04 15:30:50 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2022-12-03 20:46:15 +01:00
|
|
|
#include "video_framebuff.h"
|
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
static void init_buf_chain(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *tmp;
|
2022-12-03 20:46:15 +01:00
|
|
|
int i;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_empty = fbuf->vbuf_alloced;
|
|
|
|
fbuf->vbuf_next = NULL;
|
|
|
|
fbuf->vbuf_top = NULL;
|
|
|
|
fbuf->vbuf_tail = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
|
|
|
tmp = fbuf->vbuf_alloced;
|
2022-12-04 16:21:56 +01:00
|
|
|
for (i = 1; i < fbuf->container_size; i++)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
tmp->next = &tmp[1];
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:46:15 +01:00
|
|
|
static inline bool is_last_one(video_framebuff_t *fbuf)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
2022-12-03 20:46:15 +01:00
|
|
|
return fbuf->vbuf_top == fbuf->vbuf_tail;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline vbuf_container_t *dequeue_vbuf_unsafe(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret = fbuf->vbuf_top;
|
2022-12-03 20:46:15 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
if (is_last_one(fbuf))
|
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_top = NULL;
|
|
|
|
fbuf->vbuf_tail = NULL;
|
|
|
|
fbuf->vbuf_next = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-26 10:32:06 +02:00
|
|
|
if (fbuf->mode == V4L2_BUF_MODE_RING)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = fbuf->vbuf_top->next;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
fbuf->vbuf_top = fbuf->vbuf_top->next;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2022-12-04 18:39:41 +01:00
|
|
|
ret->next = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
void video_framebuff_init(video_framebuff_t *fbuf)
|
|
|
|
{
|
2022-12-04 15:36:26 +01:00
|
|
|
memset(fbuf, 0, sizeof(video_framebuff_t));
|
|
|
|
fbuf->mode = V4L2_BUF_MODE_RING;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_uninit(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
video_framebuff_realloc_container(fbuf, 0);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int video_framebuff_realloc_container(video_framebuff_t *fbuf, int sz)
|
|
|
|
{
|
2022-12-04 16:21:56 +01:00
|
|
|
vbuf_container_t *vbuf;
|
|
|
|
|
2024-03-17 08:23:33 +01:00
|
|
|
nxmutex_lock(&fbuf->lock_empty);
|
2022-12-04 16:10:35 +01:00
|
|
|
if (fbuf->container_size == sz)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
2024-03-17 08:23:33 +01:00
|
|
|
nxmutex_unlock(&fbuf->lock_empty);
|
2022-12-04 16:10:35 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2023-07-11 15:21:34 +02:00
|
|
|
if (sz > 0)
|
2022-12-04 16:10:35 +01:00
|
|
|
{
|
2023-07-11 15:21:34 +02:00
|
|
|
vbuf = kmm_realloc(fbuf->vbuf_alloced, sizeof(vbuf_container_t) * sz);
|
|
|
|
if (vbuf != NULL)
|
|
|
|
{
|
|
|
|
memset(vbuf, 0, sizeof(vbuf_container_t) * sz);
|
|
|
|
fbuf->vbuf_alloced = vbuf;
|
|
|
|
fbuf->container_size = sz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-17 08:23:33 +01:00
|
|
|
nxmutex_unlock(&fbuf->lock_empty);
|
2023-07-11 15:21:34 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2022-12-04 16:10:35 +01:00
|
|
|
}
|
2023-07-11 15:21:34 +02:00
|
|
|
else
|
2022-12-04 16:10:35 +01:00
|
|
|
{
|
2023-07-11 15:21:34 +02:00
|
|
|
kmm_free(fbuf->vbuf_alloced);
|
|
|
|
fbuf->vbuf_alloced = NULL;
|
|
|
|
fbuf->container_size = 0;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 16:21:56 +01:00
|
|
|
init_buf_chain(fbuf);
|
2024-03-17 08:23:33 +01:00
|
|
|
nxmutex_unlock(&fbuf->lock_empty);
|
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-01-31 10:23:28 +01:00
|
|
|
int video_framebuff_is_empty(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
return fbuf->vbuf_top == NULL || fbuf->vbuf_top == fbuf->vbuf_next;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
vbuf_container_t *video_framebuff_get_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
ret = fbuf->vbuf_empty;
|
2022-12-03 20:46:15 +01:00
|
|
|
if (ret != NULL)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
fbuf->vbuf_empty = ret->next;
|
|
|
|
ret->next = NULL;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_free_container(video_framebuff_t *fbuf,
|
|
|
|
vbuf_container_t *cnt)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
cnt->next = fbuf->vbuf_empty;
|
|
|
|
fbuf->vbuf_empty = cnt;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_queue_container(video_framebuff_t *fbuf,
|
|
|
|
vbuf_container_t *tgt)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2022-12-03 20:46:15 +01:00
|
|
|
if (fbuf->vbuf_top != NULL)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = tgt;
|
|
|
|
fbuf->vbuf_tail = tgt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fbuf->vbuf_top = fbuf->vbuf_tail = tgt;
|
2022-12-04 17:03:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fbuf->vbuf_next == NULL)
|
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_next = tgt;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fbuf->mode == V4L2_BUF_MODE_RING)
|
|
|
|
{
|
2022-12-04 17:03:22 +01:00
|
|
|
tgt->next = fbuf->vbuf_top;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
else /* Case of V4L2_BUF_MODE_FIFO */
|
|
|
|
{
|
2022-12-04 17:03:22 +01:00
|
|
|
tgt->next = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vbuf_container_t *video_framebuff_dq_valid_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret = NULL;
|
2022-12-03 20:46:15 +01:00
|
|
|
irqstate_t flags;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2021-09-15 05:02:33 +02:00
|
|
|
if (fbuf->vbuf_top != NULL && fbuf->vbuf_top != fbuf->vbuf_next)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
ret = dequeue_vbuf_unsafe(fbuf);
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:46:15 +01:00
|
|
|
vbuf_container_t *
|
|
|
|
video_framebuff_get_vacant_container(video_framebuff_t *fbuf)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
vbuf_container_t *ret;
|
2022-12-03 20:46:15 +01:00
|
|
|
irqstate_t flags;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2024-03-17 08:14:06 +01:00
|
|
|
ret = fbuf->vbuf_next;
|
2024-03-17 08:28:13 +01:00
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-15 05:02:33 +02:00
|
|
|
void video_framebuff_capture_done(video_framebuff_t *fbuf)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
2024-03-17 08:14:06 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2022-12-03 20:46:15 +01:00
|
|
|
if (fbuf->vbuf_next != NULL)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_next = fbuf->vbuf_next->next;
|
|
|
|
if (fbuf->vbuf_next == fbuf->vbuf_top) /* RING mode case. */
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
fbuf->vbuf_top = fbuf->vbuf_top->next;
|
|
|
|
fbuf->vbuf_tail = fbuf->vbuf_tail->next;
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 08:28:13 +01:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_change_mode(video_framebuff_t *fbuf,
|
|
|
|
enum v4l2_buf_mode mode)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2019-07-04 15:30:50 +02:00
|
|
|
if (fbuf->mode != mode)
|
|
|
|
{
|
|
|
|
if (fbuf->vbuf_tail)
|
|
|
|
{
|
|
|
|
if (mode == V4L2_BUF_MODE_RING)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = fbuf->vbuf_top;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2022-12-04 17:03:22 +01:00
|
|
|
fbuf->vbuf_next = fbuf->vbuf_top;
|
2019-07-04 15:30:50 +02:00
|
|
|
fbuf->mode = mode;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vbuf_container_t *video_framebuff_pop_curr_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret = NULL;
|
2022-12-03 20:46:15 +01:00
|
|
|
irqstate_t flags;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
flags = spin_lock_irqsave(&fbuf->lock_queue);
|
2019-07-04 15:30:50 +02:00
|
|
|
if (fbuf->vbuf_top != NULL)
|
|
|
|
{
|
|
|
|
ret = dequeue_vbuf_unsafe(fbuf);
|
|
|
|
}
|
|
|
|
|
2024-03-17 08:28:13 +01:00
|
|
|
spin_unlock_irqrestore(&fbuf->lock_queue, flags);
|
2019-07-04 15:30:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|