+ +
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_H |
| 6 | #define SPA_BUFFER_H |
| 7 | |
| 8 | #include <spa/utils/defs.h> |
| 9 | #include <spa/buffer/meta.h> |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | #ifndef SPA_API_BUFFER |
| 16 | #ifdef SPA_API_IMPL |
| 17 | #define SPA_API_BUFFER SPA_API_IMPL |
| 18 | #else |
| 19 | #define SPA_API_BUFFER static inline |
| 20 | #endif |
| 21 | #endif |
| 22 | |
| 23 | /** \defgroup spa_buffer Buffers |
| 24 | * |
| 25 | * Buffers describe the data and metadata that is exchanged between |
| 26 | * ports of a node. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * \addtogroup spa_buffer |
| 31 | * \{ |
| 32 | */ |
| 33 | |
| 34 | enum spa_data_type { |
| 35 | SPA_DATA_Invalid, |
| 36 | SPA_DATA_MemPtr, /**< pointer to memory, the data field in |
| 37 | * struct spa_data is set. */ |
| 38 | SPA_DATA_MemFd, /**< memfd, mmap to get to memory. */ |
| 39 | SPA_DATA_DmaBuf, /**< fd to dmabuf memory. This might not be readily |
| 40 | * mappable (unless the MAPPABLE flag is set) and should |
| 41 | * normally be handled with DMABUF apis. */ |
| 42 | SPA_DATA_MemId, /**< memory is identified with an id. The actual memory |
| 43 | * can be obtained in some other way and can be identified |
| 44 | * with this id. */ |
| 45 | SPA_DATA_SyncObj, /**< a syncobj, usually requires a spa_meta_sync_timeline metadata |
| 46 | * with timeline points. */ |
| 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 | #define SPA_CHUNK_FLAG_EMPTY (1u<<1) /**< chunk data is empty with media specific |
| 62 | * neutral data such as silence or black. This |
| 63 | * could be used to optimize processing. */ |
| 64 | int32_t flags; /**< chunk flags */ |
| 65 | }; |
| 66 | |
| 67 | /** Data for a buffer this stays constant for a buffer */ |
| 68 | struct spa_data { |
| 69 | uint32_t type; /**< memory type, one of enum spa_data_type, when |
| 70 | * allocating memory, the type contains a bitmask |
| 71 | * of allowed types. SPA_ID_INVALID is a special |
| 72 | * value for the allocator to indicate that the |
| 73 | * other side did not explicitly specify any |
| 74 | * supported data types. It should probably use |
| 75 | * a memory type that does not require special |
| 76 | * handling in addition to simple mmap/munmap. */ |
| 77 | #define SPA_DATA_FLAG_NONE 0 |
| 78 | #define SPA_DATA_FLAG_READABLE (1u<<0) /**< data is readable */ |
| 79 | #define SPA_DATA_FLAG_WRITABLE (1u<<1) /**< data is writable */ |
| 80 | #define SPA_DATA_FLAG_DYNAMIC (1u<<2) /**< data pointer can be changed */ |
| 81 | #define SPA_DATA_FLAG_READWRITE (SPA_DATA_FLAG_READABLE|SPA_DATA_FLAG_WRITABLE) |
| 82 | #define SPA_DATA_FLAG_MAPPABLE (1u<<3) /**< data is mappable with simple mmap/munmap. Some memory |
| 83 | * types are not simply mappable (DmaBuf) unless explicitly |
| 84 | * specified with this flag. */ |
| 85 | uint32_t flags; /**< data flags */ |
| 86 | int64_t fd; /**< optional fd for data */ |
| 87 | uint32_t mapoffset; /**< offset to map fd at, this is page aligned */ |
| 88 | uint32_t maxsize; /**< max size of data */ |
| 89 | void *data; /**< optional data pointer */ |
| 90 | struct spa_chunk *chunk; /**< valid chunk of memory */ |
| 91 | }; |
| 92 | |
| 93 | /** A Buffer */ |
| 94 | struct spa_buffer { |
| 95 | uint32_t n_metas; /**< number of metadata */ |
| 96 | uint32_t n_datas; /**< number of data members */ |
| 97 | struct spa_meta *metas; /**< array of metadata */ |
| 98 | struct spa_data *datas; /**< array of data members */ |
| 99 | }; |
| 100 | |
| 101 | /** Find metadata in a buffer */ |
| 102 | SPA_API_BUFFER struct spa_meta *spa_buffer_find_meta(const struct spa_buffer *b, uint32_t type) |
| 103 | { |
| 104 | uint32_t i; |
| 105 | |
| 106 | for (i = 0; i < b->n_metas; i++) |
| 107 | if (b->metas[i].type == type) |
| 108 | return &b->metas[i]; |
| 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | SPA_API_BUFFER void *spa_buffer_find_meta_data(const struct spa_buffer *b, uint32_t type, size_t size) |
| 114 | { |
| 115 | struct spa_meta *m; |
| 116 | if ((m = spa_buffer_find_meta(b, type)) && m->size >= size) |
| 117 | return m->data; |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | SPA_API_BUFFER bool spa_buffer_has_meta_features(const struct spa_buffer *b, uint32_t type, uint32_t features) |
| 122 | { |
| 123 | uint32_t i; |
| 124 | for (i = 0; i < b->n_metas; i++) { |
| 125 | uint32_t t = b->metas[i].type; |
| 126 | if ((t >> 16) == type && (t & features) == features) |
| 127 | return true; |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * \} |
| 134 | */ |
| 135 | |
| 136 | #ifdef __cplusplus |
| 137 | } /* extern "C" */ |
| 138 | #endif |
| 139 | |
| 140 | #endif /* SPA_BUFFER_H */ |