+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* Simple Plugin API */ |
| 2 | /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */ |
| 3 | /* SPDX-License-Identifier: MIT */ |
| 4 | |
| 5 | #ifndef SPA_BUFFER_ALLOC_H |
| 6 | #define SPA_BUFFER_ALLOC_H |
| 7 | |
| 8 | #include <spa/buffer/buffer.h> |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | #ifndef SPA_API_BUFFER_ALLOC |
| 15 | #ifdef SPA_API_IMPL |
| 16 | #define SPA_API_BUFFER_ALLOC SPA_API_IMPL |
| 17 | #else |
| 18 | #define SPA_API_BUFFER_ALLOC static inline |
| 19 | #endif |
| 20 | #endif |
| 21 | |
| 22 | /** |
| 23 | * \addtogroup spa_buffer |
| 24 | * \{ |
| 25 | */ |
| 26 | |
| 27 | /** information about the buffer layout */ |
| 28 | struct spa_buffer_alloc_info { |
| 29 | #define SPA_BUFFER_ALLOC_FLAG_INLINE_META (1<<0) /**< add metadata data in the skeleton */ |
| 30 | #define SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK (1<<1) /**< add chunk data in the skeleton */ |
| 31 | #define SPA_BUFFER_ALLOC_FLAG_INLINE_DATA (1<<2) /**< add buffer data to the skeleton */ |
| 32 | #define SPA_BUFFER_ALLOC_FLAG_INLINE_ALL 0b111 |
| 33 | #define SPA_BUFFER_ALLOC_FLAG_NO_DATA (1<<3) /**< don't set data pointers */ |
| 34 | uint32_t flags; |
| 35 | uint32_t max_align; /**< max of all alignments */ |
| 36 | uint32_t n_metas; |
| 37 | uint32_t n_datas; |
| 38 | struct spa_meta *metas; |
| 39 | struct spa_data *datas; |
| 40 | uint32_t *data_aligns; |
| 41 | size_t skel_size; /**< size of the struct spa_buffer and inlined meta/chunk/data */ |
| 42 | size_t meta_size; /**< size of the meta if not inlined */ |
| 43 | size_t chunk_size; /**< size of the chunk if not inlined */ |
| 44 | size_t data_size; /**< size of the data if not inlined */ |
| 45 | size_t mem_size; /**< size of the total memory if not inlined */ |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Fill buffer allocation information |
| 50 | * |
| 51 | * Fill \a info with allocation information needed to allocate buffers |
| 52 | * with the given number of metadata and data members. |
| 53 | * |
| 54 | * The required size of the skeleton (the struct spa_buffer) information |
| 55 | * and the memory (for the metadata, chunk and buffer memory) will be |
| 56 | * calculated. |
| 57 | * |
| 58 | * The flags member in \a info should be configured before calling this |
| 59 | * functions. |
| 60 | * |
| 61 | * \param info the information to fill |
| 62 | * \param n_metas the number of metadatas for the buffer |
| 63 | * \param metas an array of metadata items |
| 64 | * \param n_datas the number of datas for the buffer |
| 65 | * \param datas an array of \a n_datas items |
| 66 | * \param data_aligns \a n_datas alignments |
| 67 | * \return 0 on success. |
| 68 | * */ |
| 69 | SPA_API_BUFFER_ALLOC int spa_buffer_alloc_fill_info(struct spa_buffer_alloc_info *info, |
| 70 | uint32_t n_metas, struct spa_meta metas[], |
| 71 | uint32_t n_datas, struct spa_data datas[], |
| 72 | uint32_t data_aligns[]) |
| 73 | { |
| 74 | size_t size, *target; |
| 75 | uint32_t i; |
| 76 | |
| 77 | info->n_metas = n_metas; |
| 78 | info->metas = metas; |
| 79 | info->n_datas = n_datas; |
| 80 | info->datas = datas; |
| 81 | info->data_aligns = data_aligns; |
| 82 | info->max_align = 16; |
| 83 | info->mem_size = 0; |
| 84 | /* |
| 85 | * The buffer skeleton is placed in memory like below and can |
| 86 | * be accessed as a regular structure. |
| 87 | * |
| 88 | * +==============================+ |
| 89 | * | struct spa_buffer | |
| 90 | * | uint32_t n_metas | number of metas |
| 91 | * | uint32_t n_datas | number of datas |
| 92 | * +-| struct spa_meta *metas | pointer to array of metas |
| 93 | * +|-| struct spa_data *datas | pointer to array of datas |
| 94 | * || +------------------------------+ |
| 95 | * |+>| struct spa_meta | |
| 96 | * | | uint32_t type | metadata |
| 97 | * | | uint32_t size | size of metadata |
| 98 | * +|--| void *data | pointer to metadata |
| 99 | * || | ... <n_metas> | more spa_meta follow |
| 100 | * || +------------------------------+ |
| 101 | * |+->| struct spa_data | |
| 102 | * | | uint32_t type | memory type |
| 103 | * | | uint32_t flags | |
| 104 | * | | int fd | fd of shared memory block |
| 105 | * | | uint32_t mapoffset | offset in shared memory of data |
| 106 | * | | uint32_t maxsize | size of data block |
| 107 | * | +-| void *data | pointer to data |
| 108 | * |+|-| struct spa_chunk *chunk | pointer to chunk |
| 109 | * ||| | ... <n_datas> | more spa_data follow |
| 110 | * ||| +==============================+ |
| 111 | * VVV |
| 112 | * |
| 113 | * metadata, chunk and memory can either be placed right |
| 114 | * after the skeleton (inlined) or in a separate piece of memory. |
| 115 | * |
| 116 | * vvv |
| 117 | * ||| +==============================+ |
| 118 | * +-->| meta data memory | metadata memory, 8 byte aligned |
| 119 | * || | ... <n_metas> | |
| 120 | * || +------------------------------+ |
| 121 | * +->| struct spa_chunk | memory for n_datas chunks |
| 122 | * | | uint32_t offset | |
| 123 | * | | uint32_t size | |
| 124 | * | | int32_t stride | |
| 125 | * | | int32_t flags | |
| 126 | * | | ... <n_datas> chunks | |
| 127 | * | +------------------------------+ |
| 128 | * +>| data | memory for n_datas data, aligned |
| 129 | * | ... <n_datas> blocks | according to alignments |
| 130 | * +==============================+ |
| 131 | */ |
| 132 | info->skel_size = sizeof(struct spa_buffer); |
| 133 | info->skel_size += n_metas * sizeof(struct spa_meta); |
| 134 | info->skel_size += n_datas * sizeof(struct spa_data); |
| 135 | |
| 136 | for (i = 0, size = 0; i < n_metas; i++) |
| 137 | size += SPA_ROUND_UP_N(metas[i].size, 8); |
| 138 | info->meta_size = size; |
| 139 | |
| 140 | if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_META)) |
| 141 | target = &info->skel_size; |
| 142 | else |
| 143 | target = &info->mem_size; |
| 144 | *target += info->meta_size; |
| 145 | |
| 146 | info->chunk_size = n_datas * sizeof(struct spa_chunk); |
| 147 | if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK)) |
| 148 | target = &info->skel_size; |
| 149 | else |
| 150 | target = &info->mem_size; |
| 151 | *target += info->chunk_size; |
| 152 | |
| 153 | for (i = 0, size = 0; i < n_datas; i++) { |
| 154 | int64_t align = data_aligns[i]; |
| 155 | info->max_align = SPA_MAX(info->max_align, data_aligns[i]); |
| 156 | size = SPA_ROUND_UP_N(size, align); |
| 157 | size += datas[i].maxsize; |
| 158 | } |
| 159 | info->data_size = size; |
| 160 | |
| 161 | if (!SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_NO_DATA) && |
| 162 | SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_DATA)) |
| 163 | target = &info->skel_size; |
| 164 | else |
| 165 | target = &info->mem_size; |
| 166 | |
| 167 | *target = SPA_ROUND_UP_N(*target, n_datas ? data_aligns[0] : 1); |
| 168 | *target += info->data_size; |
| 169 | *target = SPA_ROUND_UP_N(*target, info->max_align); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Fill skeleton and data according to the allocation info |
| 176 | * |
| 177 | * Use the allocation info to create a struct \ref spa_buffer into |
| 178 | * \a skel_mem and \a data_mem. |
| 179 | * |
| 180 | * Depending on the flags given when calling \ref |
| 181 | * spa_buffer_alloc_fill_info(), the buffer meta, chunk and memory |
| 182 | * will be referenced in either skel_mem or data_mem. |
| 183 | * |
| 184 | * \param info an allocation info |
| 185 | * \param skel_mem memory to hold the struct \ref spa_buffer and the |
| 186 | * pointers to meta, chunk and memory. |
| 187 | * \param data_mem memory to hold the meta, chunk and memory |
| 188 | * \return a struct \ref spa_buffer in \a skel_mem |
| 189 | */ |
| 190 | SPA_API_BUFFER_ALLOC struct spa_buffer * |
| 191 | spa_buffer_alloc_layout(struct spa_buffer_alloc_info *info, |
| 192 | void *skel_mem, void *data_mem) |
| 193 | { |
| 194 | struct spa_buffer *b = (struct spa_buffer*)skel_mem; |
| 195 | size_t size; |
| 196 | uint32_t i; |
| 197 | void **dp, *skel, *data; |
| 198 | struct spa_chunk *cp; |
| 199 | |
| 200 | b->n_metas = info->n_metas; |
| 201 | b->metas = SPA_PTROFF(b, sizeof(struct spa_buffer), struct spa_meta); |
| 202 | b->n_datas = info->n_datas; |
| 203 | b->datas = SPA_PTROFF(b->metas, info->n_metas * sizeof(struct spa_meta), struct spa_data); |
| 204 | |
| 205 | skel = SPA_PTROFF(b->datas, info->n_datas * sizeof(struct spa_data), void); |
| 206 | data = data_mem; |
| 207 | |
| 208 | if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_META)) |
| 209 | dp = &skel; |
| 210 | else |
| 211 | dp = &data; |
| 212 | |
| 213 | for (i = 0; i < info->n_metas; i++) { |
| 214 | struct spa_meta *m = &b->metas[i]; |
| 215 | *m = info->metas[i]; |
| 216 | m->data = *dp; |
| 217 | *dp = SPA_PTROFF(*dp, SPA_ROUND_UP_N(m->size, 8), void); |
| 218 | } |
| 219 | |
| 220 | size = info->n_datas * sizeof(struct spa_chunk); |
| 221 | if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_CHUNK)) { |
| 222 | cp = (struct spa_chunk*)skel; |
| 223 | skel = SPA_PTROFF(skel, size, void); |
| 224 | } |
| 225 | else { |
| 226 | cp = (struct spa_chunk*)data; |
| 227 | data = SPA_PTROFF(data, size, void); |
| 228 | } |
| 229 | |
| 230 | if (SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_INLINE_DATA)) |
| 231 | dp = &skel; |
| 232 | else |
| 233 | dp = &data; |
| 234 | |
| 235 | for (i = 0; i < info->n_datas; i++) { |
| 236 | struct spa_data *d = &b->datas[i]; |
| 237 | |
| 238 | *d = info->datas[i]; |
| 239 | d->chunk = &cp[i]; |
| 240 | if (!SPA_FLAG_IS_SET(info->flags, SPA_BUFFER_ALLOC_FLAG_NO_DATA)) { |
| 241 | *dp = SPA_PTR_ALIGN(*dp, info->data_aligns[i], void); |
| 242 | d->data = *dp; |
| 243 | *dp = SPA_PTROFF(*dp, d->maxsize, void); |
| 244 | } |
| 245 | } |
| 246 | return b; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Layout an array of buffers |
| 251 | * |
| 252 | * Use the allocation info to layout the memory of an array of buffers. |
| 253 | * |
| 254 | * \a skel_mem should point to at least info->skel_size * \a n_buffers bytes |
| 255 | * of memory. |
| 256 | * \a data_mem should point to at least info->mem_size * \a n_buffers bytes |
| 257 | * of memory. |
| 258 | * |
| 259 | * \param info the allocation info for one buffer |
| 260 | * \param n_buffers the number of buffers to create |
| 261 | * \param buffers a array with space to hold \a n_buffers pointers to buffers |
| 262 | * \param skel_mem memory for the struct \ref spa_buffer |
| 263 | * \param data_mem memory for the meta, chunk, memory of the buffer if not |
| 264 | * inlined in the skeleton. |
| 265 | * \return 0 on success. |
| 266 | * |
| 267 | */ |
| 268 | SPA_API_BUFFER_ALLOC int |
| 269 | spa_buffer_alloc_layout_array(struct spa_buffer_alloc_info *info, |
| 270 | uint32_t n_buffers, struct spa_buffer *buffers[], |
| 271 | void *skel_mem, void *data_mem) |
| 272 | { |
| 273 | uint32_t i; |
| 274 | for (i = 0; i < n_buffers; i++) { |
| 275 | buffers[i] = spa_buffer_alloc_layout(info, skel_mem, data_mem); |
| 276 | skel_mem = SPA_PTROFF(skel_mem, info->skel_size, void); |
| 277 | data_mem = SPA_PTROFF(data_mem, info->mem_size, void); |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Allocate an array of buffers |
| 284 | * |
| 285 | * Allocate \a n_buffers with the given metadata, memory and alignment |
| 286 | * information. |
| 287 | * |
| 288 | * The buffer array, structures, data and metadata will all be allocated |
| 289 | * in one block of memory with the proper requested alignment. |
| 290 | * |
| 291 | * \param n_buffers the number of buffers to create |
| 292 | * \param flags extra flags |
| 293 | * \param n_metas number of metadatas |
| 294 | * \param metas \a n_metas metadata specification |
| 295 | * \param n_datas number of datas |
| 296 | * \param datas \a n_datas memory specification |
| 297 | * \param data_aligns \a n_datas alignment specifications |
| 298 | * \returns an array of \a n_buffers pointers to struct \ref spa_buffer |
| 299 | * with the given metadata, data and alignment or NULL when |
| 300 | * allocation failed. |
| 301 | * |
| 302 | */ |
| 303 | SPA_API_BUFFER_ALLOC struct spa_buffer ** |
| 304 | spa_buffer_alloc_array(uint32_t n_buffers, uint32_t flags, |
| 305 | uint32_t n_metas, struct spa_meta metas[], |
| 306 | uint32_t n_datas, struct spa_data datas[], |
| 307 | uint32_t data_aligns[]) |
| 308 | { |
| 309 | |
| 310 | struct spa_buffer **buffers; |
| 311 | struct spa_buffer_alloc_info info = { flags | SPA_BUFFER_ALLOC_FLAG_INLINE_ALL, |
| 312 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 313 | void *skel; |
| 314 | |
| 315 | spa_buffer_alloc_fill_info(&info, n_metas, metas, n_datas, datas, data_aligns); |
| 316 | |
| 317 | buffers = (struct spa_buffer **)calloc(1, info.max_align + |
| 318 | n_buffers * (sizeof(struct spa_buffer *) + info.skel_size)); |
| 319 | if (buffers == NULL) |
| 320 | return NULL; |
| 321 | |
| 322 | skel = SPA_PTROFF(buffers, sizeof(struct spa_buffer *) * n_buffers, void); |
| 323 | skel = SPA_PTR_ALIGN(skel, info.max_align, void); |
| 324 | |
| 325 | spa_buffer_alloc_layout_array(&info, n_buffers, buffers, skel, NULL); |
| 326 | |
| 327 | return buffers; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * \} |
| 332 | */ |
| 333 | |
| 334 | #ifdef __cplusplus |
| 335 | } /* extern "C" */ |
| 336 | #endif |
| 337 | |
| 338 | #endif /* SPA_BUFFER_ALLOC_H */ |