Debian Codegraph @

3rdparty/pipewire/spa/buffer/buffer.h

raw
1 /* Simple Plugin API
2 * Copyright © 2018 Wim Taymans
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef SPA_BUFFER_H
25 #define SPA_BUFFER_H
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include <spa/utils/defs.h>
32 #include <spa/buffer/meta.h>
33
34 /** \page page_buffer Buffers
35 *
36 * Buffers describe the data and metadata that is exchanged between
37 * ports of a node.
38 */
39
40 enum spa_data_type {
41 SPA_DATA_Invalid,
42 SPA_DATA_MemPtr, /**< pointer to memory, the data field in
43 * struct spa_data is set. */
44 SPA_DATA_MemFd, /**< generic fd, mmap to get to memory */
45 SPA_DATA_DmaBuf, /**< fd to dmabuf memory */
46 SPA_DATA_MemId, /**< memory is identified with an id */
47
48 SPA_DATA_LAST, /**< not part of ABI */
49 };
50
51 /** Chunk of memory, can change for each buffer */
52 struct spa_chunk {
53 uint32_t offset; /**< offset of valid data. Should be taken
54 * modulo the data maxsize to get the offset
55 * in the data memory. */
56 uint32_t size; /**< size of valid data. Should be clamped to
57 * maxsize. */
58 int32_t stride; /**< stride of valid data */
59 #define SPA_CHUNK_FLAG_NONE 0
60 #define SPA_CHUNK_FLAG_CORRUPTED (1u<<0) /**< chunk data is corrupted in some way */
61 int32_t flags; /**< chunk flags */
62 };
63
64 /** Data for a buffer this stays constant for a buffer */
65 struct spa_data {
66 uint32_t type; /**< memory type, one of enum spa_data_type, when
67 * allocating memory, the type contains a bitmask
68 * of allowed types. SPA_ID_INVALID is a special
69 * value for the allocator to indicate that the
70 * other side did not explicitly specify any
71 * supported data types. It should probably use
72 * a memory type that does not require special
73 * handling in addition to simple mmap/munmap. */
74 #define SPA_DATA_FLAG_NONE 0
75 #define SPA_DATA_FLAG_READABLE (1u<<0) /**< data is readable */
76 #define SPA_DATA_FLAG_WRITABLE (1u<<1) /**< data is writable */
77 #define SPA_DATA_FLAG_DYNAMIC (1u<<2) /**< data pointer can be changed */
78 #define SPA_DATA_FLAG_READWRITE (SPA_DATA_FLAG_READABLE|SPA_DATA_FLAG_WRITABLE)
79 uint32_t flags; /**< data flags */
80 int64_t fd; /**< optional fd for data */
81 uint32_t mapoffset; /**< offset to map fd at */
82 uint32_t maxsize; /**< max size of data */
83 void *data; /**< optional data pointer */
84 struct spa_chunk *chunk; /**< valid chunk of memory */
85 };
86
87 /** A Buffer */
88 struct spa_buffer {
89 uint32_t n_metas; /**< number of metadata */
90 uint32_t n_datas; /**< number of data members */
91 struct spa_meta *metas; /**< array of metadata */
92 struct spa_data *datas; /**< array of data members */
93 };
94
95 /** Find metadata in a buffer */
96 static inline struct spa_meta *spa_buffer_find_meta(const struct spa_buffer *b, uint32_t type)
97 {
98 uint32_t i;
99
100 for (i = 0; i < b->n_metas; i++)
101 if (b->metas[i].type == type)
102 return &b->metas[i];
103
104 return NULL;
105 }
106
107 static inline void *spa_buffer_find_meta_data(const struct spa_buffer *b, uint32_t type, size_t size)
108 {
109 struct spa_meta *m;
110 if ((m = spa_buffer_find_meta(b, type)) && m->size >= size)
111 return m->data;
112 return NULL;
113 }
114
115 #ifdef __cplusplus
116 } /* extern "C" */
117 #endif
118
119 #endif /* SPA_BUFFER_H */