blob: 20138e1fef0381dba9e807a4df8cafdb8618aa8e [file] [log] [blame]
xuesong.jiangae1548e2022-05-06 16:38:46 +08001/* GStreamer
2 * Copyright (C) 2022 <xuesong.jiang@amlogic.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19
20#ifndef __GST_AML_V4L2_ALLOCATOR_H__
21#define __GST_AML_V4L2_ALLOCATOR_H__
22
23#include "ext/videodev2.h"
24#include <gst/gst.h>
25#include <gst/gstatomicqueue.h>
26
27G_BEGIN_DECLS
28
29#define GST_TYPE_AML_V4L2_ALLOCATOR (gst_aml_v4l2_allocator_get_type())
30#define GST_IS_AML_V4L2_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AML_V4L2_ALLOCATOR))
31#define GST_IS_AML_V4L2_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AML_V4L2_ALLOCATOR))
32#define GST_AML_V4L2_ALLOCATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2AllocatorClass))
33#define GST_AML_V4L2_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2Allocator))
34#define GST_AML_V4L2_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2AllocatorClass))
35#define GST_AML_V4L2_ALLOCATOR_CAST(obj) ((GstAmlV4l2Allocator *)(obj))
36
37#define GST_AML_V4L2_ALLOCATOR_CAN_REQUEST(obj, type) \
38 (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_##type##_REQBUFS))
39#define GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(obj, type) \
40 (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_##type##_CREATE_BUFS))
41#define GST_AML_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(obj) \
42 (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS))
43#define GST_AML_V4L2_ALLOCATOR_IS_ORPHANED(obj) \
44 (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_ORPHANED))
45
46#define GST_AML_V4L2_MEMORY_QUARK gst_aml_v4l2_memory_quark()
47
48typedef struct _GstAmlV4l2Allocator GstAmlV4l2Allocator;
49typedef struct _GstAmlV4l2AllocatorClass GstAmlV4l2AllocatorClass;
50typedef struct _GstAmlV4l2MemoryGroup GstAmlV4l2MemoryGroup;
51typedef struct _GstAmlV4l2Memory GstAmlV4l2Memory;
52typedef enum _GstAmlV4l2Capabilities GstAmlV4l2Capabilities;
53typedef enum _GstAmlV4l2Return GstAmlV4l2Return;
54
55enum _GstAmlV4l2AllocatorFlags
56{
bo.xiao857b8682024-09-12 16:40:32 +080057 GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 0),
58 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 1),
59 GST_V4L2_ALLOCATOR_FLAG_USERPTR_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 2),
60 GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 3),
61 GST_V4L2_ALLOCATOR_FLAG_DMABUF_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 4),
62 GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 5),
63 GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS = (GST_ALLOCATOR_FLAG_LAST << 6),
64 GST_V4L2_ALLOCATOR_FLAG_ORPHANED = (GST_ALLOCATOR_FLAG_LAST << 7),
xuesong.jiangae1548e2022-05-06 16:38:46 +080065};
66
67enum _GstAmlV4l2Return
68{
bo.xiao857b8682024-09-12 16:40:32 +080069 GST_AML_V4L2_OK = 0,
70 GST_AML_V4L2_ERROR = -1,
71 GST_AML_V4L2_BUSY = -2
xuesong.jiangae1548e2022-05-06 16:38:46 +080072};
73
74struct _GstAmlV4l2Memory
75{
bo.xiao857b8682024-09-12 16:40:32 +080076 GstMemory mem;
77 gint plane;
78 GstAmlV4l2MemoryGroup *group;
79 gpointer data;
80 gint dmafd;
xuesong.jiangae1548e2022-05-06 16:38:46 +080081};
82
83struct _GstAmlV4l2MemoryGroup
84{
bo.xiao857b8682024-09-12 16:40:32 +080085 gint n_mem;
86 GstMemory * mem[VIDEO_MAX_PLANES];
87 gint mems_allocated;
88 struct v4l2_buffer buffer;
89 struct v4l2_plane planes[VIDEO_MAX_PLANES];
xuesong.jiangae1548e2022-05-06 16:38:46 +080090};
91
92struct _GstAmlV4l2Allocator
93{
bo.xiao857b8682024-09-12 16:40:32 +080094 GstAllocator parent;
95 GstAmlV4l2Object *obj;
96 guint32 count;
97 guint32 memory;
98 gboolean can_allocate;
99 gboolean active;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800100
bo.xiao857b8682024-09-12 16:40:32 +0800101 GstAmlV4l2MemoryGroup *groups[VIDEO_MAX_FRAME];
102 GstAtomicQueue *free_queue;
103 GstAtomicQueue *pending_queue;
104
xuesong.jiangae1548e2022-05-06 16:38:46 +0800105};
106
107struct _GstAmlV4l2AllocatorClass
108{
bo.xiao857b8682024-09-12 16:40:32 +0800109 GstAllocatorClass parent_class;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800110};
111
112GType gst_aml_v4l2_allocator_get_type(void);
113
bo.xiao857b8682024-09-12 16:40:32 +0800114gboolean gst_aml_is_v4l2_memory(GstMemory *mem);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800115
116GQuark gst_aml_v4l2_memory_quark(void);
117
118gboolean gst_aml_v4l2_allocator_is_active(GstAmlV4l2Allocator *allocator);
119
120guint gst_aml_v4l2_allocator_get_size(GstAmlV4l2Allocator *allocator);
121
122GstAmlV4l2Allocator *gst_aml_v4l2_allocator_new(GstObject *parent, GstAmlV4l2Object *obj);
123
124guint gst_aml_v4l2_allocator_start(GstAmlV4l2Allocator *allocator,
125 guint32 count, guint32 memory);
126
127GstAmlV4l2Return gst_aml_v4l2_allocator_stop(GstAmlV4l2Allocator *allocator);
128
129gboolean gst_aml_v4l2_allocator_orphan(GstAmlV4l2Allocator *allocator);
130
131GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_mmap(GstAmlV4l2Allocator *allocator);
132
133GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_dmabuf(GstAmlV4l2Allocator *allocator,
134 GstAllocator *dmabuf_allocator);
135
136GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_dmabufin(GstAmlV4l2Allocator *allocator);
137
138GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_userptr(GstAmlV4l2Allocator *allocator);
139
140gboolean gst_aml_v4l2_allocator_import_dmabuf(GstAmlV4l2Allocator *allocator,
141 GstAmlV4l2MemoryGroup *group,
142 gint n_mem, GstMemory **dma_mem);
143
144gboolean gst_aml_v4l2_allocator_import_userptr(GstAmlV4l2Allocator *allocator,
145 GstAmlV4l2MemoryGroup *group,
146 gsize img_size, int n_planes,
147 gpointer *data, gsize *size);
148
149void gst_aml_v4l2_allocator_flush(GstAmlV4l2Allocator *allocator);
150
151gboolean gst_aml_v4l2_allocator_qbuf(GstAmlV4l2Allocator *allocator,
152 GstAmlV4l2MemoryGroup *group);
153
154GstFlowReturn gst_aml_v4l2_allocator_dqbuf(GstAmlV4l2Allocator *allocator,
155 GstAmlV4l2MemoryGroup **group);
156
157void gst_aml_v4l2_allocator_reset_group(GstAmlV4l2Allocator *allocator,
158 GstAmlV4l2MemoryGroup *group);
159
160G_END_DECLS
161
162#endif /* __GST_AML_V4L2_ALLOCATOR_H__ */