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 "video_framebuff.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
vbuf_container_t *tmp;
|
|
|
|
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_empty = fbuf->vbuf_alloced;
|
|
|
|
fbuf->vbuf_next = NULL;
|
|
|
|
fbuf->vbuf_curr = NULL;
|
|
|
|
fbuf->vbuf_top = NULL;
|
|
|
|
fbuf->vbuf_tail = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
|
|
|
tmp = fbuf->vbuf_alloced;
|
|
|
|
for (i = 0; i < fbuf->container_size - 1; i++)
|
|
|
|
{
|
|
|
|
tmp->next = &tmp[1];
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
if (fbuf->vbuf_alloced)
|
|
|
|
{
|
|
|
|
memset(fbuf->vbuf_alloced,
|
|
|
|
0,
|
|
|
|
sizeof(vbuf_container_t)*fbuf->container_size);
|
|
|
|
init_buf_chain(fbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int is_last_one(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
return fbuf->vbuf_top == fbuf->vbuf_tail ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline vbuf_container_t *dequeue_vbuf_unsafe(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret = fbuf->vbuf_top;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (fbuf->mode == V4L2_BUF_MODE_RING)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = fbuf->vbuf_top->next;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
fbuf->vbuf_top = fbuf->vbuf_top->next;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->mode = V4L2_BUF_MODE_RING;
|
|
|
|
fbuf->vbuf_empty = NULL;
|
|
|
|
fbuf->vbuf_top = NULL;
|
|
|
|
fbuf->vbuf_tail = NULL;
|
|
|
|
fbuf->vbuf_next = NULL;
|
2019-07-04 15:30:50 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&fbuf->lock_empty, 0, 1);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_uninit(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
video_framebuff_realloc_container(fbuf, 0);
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int video_framebuff_realloc_container(video_framebuff_t *fbuf, int sz)
|
|
|
|
{
|
|
|
|
if (sz > V4L2_REQBUFS_COUNT_MAX)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbuf->vbuf_alloced == NULL || fbuf->container_size != sz)
|
|
|
|
{
|
2021-01-26 09:14:35 +01:00
|
|
|
if (fbuf->container_size != sz)
|
|
|
|
{
|
|
|
|
if (fbuf->vbuf_alloced != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(fbuf->vbuf_alloced);
|
|
|
|
}
|
|
|
|
|
|
|
|
fbuf->vbuf_alloced = NULL;
|
|
|
|
fbuf->container_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sz > 0)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_alloced
|
2021-09-15 05:02:33 +02:00
|
|
|
= (vbuf_container_t *)kmm_malloc(sizeof(vbuf_container_t)*sz);
|
2021-01-26 09:14:35 +01:00
|
|
|
if (fbuf->vbuf_alloced == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fbuf->container_size = sz;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_container(fbuf);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
vbuf_container_t *video_framebuff_get_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
vbuf_container_t *ret;
|
|
|
|
|
2021-09-15 05:02:33 +02:00
|
|
|
nxsem_wait_uninterruptible(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
ret = fbuf->vbuf_empty;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_empty = ret->next;
|
|
|
|
ret->next = NULL;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&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)
|
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
nxsem_wait_uninterruptible(&fbuf->lock_empty);
|
2019-07-04 15:30:50 +02:00
|
|
|
cnt->next = fbuf->vbuf_empty;
|
|
|
|
fbuf->vbuf_empty = cnt;
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&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;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (fbuf->vbuf_top)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = tgt;
|
|
|
|
fbuf->vbuf_tail = tgt;
|
2021-09-15 05:02:33 +02: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 = tgt;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fbuf->vbuf_top = fbuf->vbuf_tail = tgt;
|
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)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = fbuf->vbuf_top;
|
|
|
|
}
|
|
|
|
else /* Case of V4L2_BUF_MODE_FIFO */
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = NULL;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
vbuf_container_t *video_framebuff_dq_valid_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
vbuf_container_t *ret = NULL;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
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
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-15 05:02:33 +02:00
|
|
|
vbuf_container_t *video_framebuff_get_vacant_container
|
|
|
|
(video_framebuff_t *fbuf)
|
2019-07-04 15:30:50 +02:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
vbuf_container_t *ret;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
2021-09-15 05:02:33 +02:00
|
|
|
ret = fbuf->vbuf_curr = fbuf->vbuf_next;
|
2019-07-04 15:30:50 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_curr = NULL;
|
|
|
|
if (fbuf->vbuf_next)
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void video_framebuff_change_mode(video_framebuff_t *fbuf,
|
|
|
|
enum v4l2_buf_mode mode)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (fbuf->mode != mode)
|
|
|
|
{
|
|
|
|
if (fbuf->vbuf_tail)
|
|
|
|
{
|
|
|
|
if (mode == V4L2_BUF_MODE_RING)
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = fbuf->vbuf_top;
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_next = fbuf->vbuf_top;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fbuf->vbuf_tail->next = NULL;
|
2021-09-15 05:02:33 +02:00
|
|
|
fbuf->vbuf_next = fbuf->vbuf_top;
|
2019-07-04 15:30:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
fbuf->mode = mode;
|
|
|
|
}
|
2021-01-26 09:14:35 +01:00
|
|
|
|
2019-07-04 15:30:50 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
vbuf_container_t *video_framebuff_pop_curr_container(video_framebuff_t *fbuf)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
vbuf_container_t *ret = NULL;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (fbuf->vbuf_top != NULL)
|
|
|
|
{
|
|
|
|
ret = dequeue_vbuf_unsafe(fbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|