+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 465 lines · 12.5 KB · SPDX: MIT
main
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4  
5 #ifndef SPA_POD_BODY_H
6 #define SPA_POD_BODY_H
7  
8 #include <errno.h>
9 #include <sys/types.h>
10  
11 #include <spa/pod/pod.h>
12 #include <spa/utils/atomic.h>
13  
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17  
18 #ifndef SPA_API_POD_BODY
19 #ifdef SPA_API_IMPL
20 #define SPA_API_POD_BODY SPA_API_IMPL
21 #else
22 #define SPA_API_POD_BODY static inline
23 #endif
24 #endif
25  
26 /**
27 * \addtogroup spa_pod
28 * \{
29 */
30  
31 struct spa_pod_frame {
32 struct spa_pod pod;
33 struct spa_pod_frame *parent;
34 uint32_t offset;
35 uint32_t flags;
36 };
37  
38 SPA_API_POD_BODY uint32_t spa_pod_type_size(uint32_t type)
39 {
40 switch (type) {
41 case SPA_TYPE_None:
42 case SPA_TYPE_Bytes:
43 case SPA_TYPE_Struct:
44 case SPA_TYPE_Pod:
45 return 0;
46 case SPA_TYPE_String:
47 return 1;
48 case SPA_TYPE_Bool:
49 case SPA_TYPE_Int:
50 return sizeof(int32_t);
51 case SPA_TYPE_Id:
52 return sizeof(uint32_t);
53 case SPA_TYPE_Long:
54 return sizeof(int64_t);
55 case SPA_TYPE_Float:
56 return sizeof(float);
57 case SPA_TYPE_Double:
58 return sizeof(double);
59 case SPA_TYPE_Rectangle:
60 return sizeof(struct spa_rectangle);
61 case SPA_TYPE_Fraction:
62 return sizeof(struct spa_fraction);
63 case SPA_TYPE_Bitmap:
64 return sizeof(uint8_t);
65 case SPA_TYPE_Array:
66 return sizeof(struct spa_pod_array_body);
67 case SPA_TYPE_Object:
68 return sizeof(struct spa_pod_object_body);
69 case SPA_TYPE_Sequence:
70 return sizeof(struct spa_pod_sequence_body);
71 case SPA_TYPE_Pointer:
72 return sizeof(struct spa_pod_pointer_body);
73 case SPA_TYPE_Fd:
74 return sizeof(int64_t);
75 case SPA_TYPE_Choice:
76 return sizeof(struct spa_pod_choice_body);
77 }
78 return 0;
79 }
80  
81 SPA_API_POD_BODY int spa_pod_choice_n_values(uint32_t choice_type, uint32_t *min, uint32_t *max)
82 {
83 switch (choice_type) {
84 case SPA_CHOICE_Enum:
85 *min = 2;
86 *max = UINT32_MAX;
87 break;
88 case SPA_CHOICE_Range:
89 *min = *max = 3;
90 break;
91 case SPA_CHOICE_Step:
92 *min = *max = 4;
93 break;
94 case SPA_CHOICE_None:
95 case SPA_CHOICE_Flags:
96 *min = *max = 1;
97 break;
98 default:
99 /*
100 * This must always return at least 1, because callers
101 * assume that n_vals >= spa_pod_choice_n_values()
102 * mean that n_vals is at least 1.
103 */
104 *min = 1;
105 *max = UINT32_MAX;
106 return 0;
107 }
108 return 1;
109 }
110  
111 SPA_API_POD_BODY int spa_pod_body_from_data(void *data, size_t maxsize, off_t offset, size_t size,
112 struct spa_pod *pod, const void **body)
113 {
114 if (offset < 0 || offset > (int64_t)UINT32_MAX)
115 return -EINVAL;
116 if (size < sizeof(struct spa_pod) ||
117 size > maxsize ||
118 maxsize - size < (uint32_t)offset)
119 return -EINVAL;
120 memcpy(pod, SPA_PTROFF(data, offset, void), sizeof(struct spa_pod));
121 if (!SPA_POD_IS_VALID(pod))
122 return -EINVAL;
123 if (pod->size > size - sizeof(struct spa_pod))
124 return -EINVAL;
125 *body = SPA_PTROFF(data, offset + sizeof(struct spa_pod), void);
126 return 0;
127 }
128  
129 SPA_API_POD_BODY int spa_pod_is_none(const struct spa_pod *pod)
130 {
131 return SPA_POD_CHECK_TYPE(pod, SPA_TYPE_None);
132 }
133  
134 SPA_API_POD_BODY int spa_pod_is_bool(const struct spa_pod *pod)
135 {
136 return SPA_POD_CHECK(pod, SPA_TYPE_Bool, sizeof(int32_t));
137 }
138  
139 #define SPA_POD_BODY_LOAD_ONCE(a, b) (*(a) = SPA_LOAD_ONCE(*(__typeof__(a))(b)))
140 #define SPA_POD_BODY_LOAD_FIELD_ONCE(a, b, field) ((a)->field = SPA_LOAD_ONCE(((__typeof__(a))(b))->field))
141  
142 SPA_API_POD_BODY int spa_pod_body_get_bool(const struct spa_pod *pod, const void *body, bool *value)
143 {
144 if (!spa_pod_is_bool(pod))
145 return -EINVAL;
146 *value = !!__atomic_load_n((const int32_t *)body, __ATOMIC_RELAXED);
147 return 0;
148 }
149  
150 SPA_API_POD_BODY int spa_pod_is_id(const struct spa_pod *pod)
151 {
152 return SPA_POD_CHECK(pod, SPA_TYPE_Id, sizeof(uint32_t));
153 }
154  
155 SPA_API_POD_BODY int spa_pod_body_get_id(const struct spa_pod *pod, const void *body, uint32_t *value)
156 {
157 if (!spa_pod_is_id(pod))
158 return -EINVAL;
159 SPA_POD_BODY_LOAD_ONCE(value, body);
160 return 0;
161 }
162  
163 SPA_API_POD_BODY int spa_pod_is_int(const struct spa_pod *pod)
164 {
165 return SPA_POD_CHECK(pod, SPA_TYPE_Int, sizeof(int32_t));
166 }
167  
168 SPA_API_POD_BODY int spa_pod_body_get_int(const struct spa_pod *pod, const void *body, int32_t *value)
169 {
170 if (!spa_pod_is_int(pod))
171 return -EINVAL;
172 SPA_POD_BODY_LOAD_ONCE(value, body);
173 return 0;
174 }
175  
176 SPA_API_POD_BODY int spa_pod_is_long(const struct spa_pod *pod)
177 {
178 return SPA_POD_CHECK(pod, SPA_TYPE_Long, sizeof(int64_t));
179 }
180  
181 SPA_API_POD_BODY int spa_pod_body_get_long(const struct spa_pod *pod, const void *body, int64_t *value)
182 {
183 if (!spa_pod_is_long(pod))
184 return -EINVAL;
185 /* TODO this is wrong per C standard, but if it breaks so does the Linux kernel. */
186 SPA_BARRIER;
187 memcpy(value, body, sizeof *value);
188 SPA_BARRIER;
189 return 0;
190 }
191  
192 SPA_API_POD_BODY int spa_pod_is_float(const struct spa_pod *pod)
193 {
194 return SPA_POD_CHECK(pod, SPA_TYPE_Float, sizeof(float));
195 }
196  
197 SPA_API_POD_BODY int spa_pod_body_get_float(const struct spa_pod *pod, const void *body, float *value)
198 {
199 if (!spa_pod_is_float(pod))
200 return -EINVAL;
201 SPA_BARRIER;
202 memcpy(value, body, sizeof *value);
203 SPA_BARRIER;
204 return 0;
205 }
206  
207 SPA_API_POD_BODY int spa_pod_is_double(const struct spa_pod *pod)
208 {
209 return SPA_POD_CHECK(pod, SPA_TYPE_Double, sizeof(double));
210 }
211  
212 SPA_API_POD_BODY int spa_pod_body_get_double(const struct spa_pod *pod, const void *body, double *value)
213 {
214 if (!spa_pod_is_double(pod))
215 return -EINVAL;
216 SPA_BARRIER;
217 memcpy(value, body, sizeof *value);
218 SPA_BARRIER;
219 return 0;
220 }
221  
222 SPA_API_POD_BODY int spa_pod_is_string(const struct spa_pod *pod)
223 {
224 return SPA_POD_CHECK(pod, SPA_TYPE_String, 1);
225 }
226  
227 SPA_API_POD_BODY int spa_pod_body_get_string(const struct spa_pod *pod,
228 const void *body, const char **value)
229 {
230 const char *s;
231 if (!spa_pod_is_string(pod))
232 return -EINVAL;
233 s = (const char *)body;
234 if (((const volatile char *)s)[pod->size-1] != '\0')
235 return -EINVAL;
236 *value = s;
237 return 0;
238 }
239  
240 SPA_API_POD_BODY int spa_pod_body_copy_string(const struct spa_pod *pod, const void *body,
241 char *dest, size_t maxlen)
242 {
243 const char *s;
244 if (spa_pod_body_get_string(pod, body, &s) < 0 || maxlen < 1)
245 return -EINVAL;
246 /* pod->size includes the null terminator; a string that does not fit
247 * is reported as -ENOSPC instead of being silently truncated. */
248 if (pod->size > maxlen) {
249 dest[0] = '\0';
250 return -ENOSPC;
251 }
252 SPA_BARRIER;
253 strncpy(dest, s, maxlen-1);
254 SPA_BARRIER;
255 dest[maxlen-1]= '\0';
256 return 0;
257 }
258  
259 SPA_API_POD_BODY int spa_pod_is_bytes(const struct spa_pod *pod)
260 {
261 return SPA_POD_CHECK_TYPE(pod, SPA_TYPE_Bytes);
262 }
263  
264 SPA_API_POD_BODY int spa_pod_body_get_bytes(const struct spa_pod *pod, const void *body,
265 const void **value, uint32_t *len)
266 {
267 if (!spa_pod_is_bytes(pod))
268 return -EINVAL;
269 *value = (const void *)body;
270 *len = pod->size;
271 return 0;
272 }
273  
274 SPA_API_POD_BODY int spa_pod_is_pointer(const struct spa_pod *pod)
275 {
276 return SPA_POD_CHECK(pod, SPA_TYPE_Pointer, sizeof(struct spa_pod_pointer_body));
277 }
278  
279 SPA_API_POD_BODY int spa_pod_body_get_pointer(const struct spa_pod *pod, const void *body,
280 uint32_t *type, const void **value)
281 {
282 struct spa_pod_pointer_body b;
283 if (!spa_pod_is_pointer(pod))
284 return -EINVAL;
285 SPA_POD_BODY_LOAD_FIELD_ONCE(&b, body, type);
286 SPA_POD_BODY_LOAD_FIELD_ONCE(&b, body, value);
287 *type = b.type;
288 *value = b.value;
289 return 0;
290 }
291  
292 SPA_API_POD_BODY int spa_pod_is_fd(const struct spa_pod *pod)
293 {
294 return SPA_POD_CHECK(pod, SPA_TYPE_Fd, sizeof(int64_t));
295 }
296  
297 SPA_API_POD_BODY int spa_pod_body_get_fd(const struct spa_pod *pod, const void *body,
298 int64_t *value)
299 {
300 if (!spa_pod_is_fd(pod))
301 return -EINVAL;
302 SPA_BARRIER;
303 memcpy(value, body, sizeof *value);
304 SPA_BARRIER;
305 return 0;
306 }
307  
308 SPA_API_POD_BODY int spa_pod_is_rectangle(const struct spa_pod *pod)
309 {
310 return SPA_POD_CHECK(pod, SPA_TYPE_Rectangle, sizeof(struct spa_rectangle));
311 }
312  
313 SPA_API_POD_BODY int spa_pod_body_get_rectangle(const struct spa_pod *pod, const void *body,
314 struct spa_rectangle *value)
315 {
316 if (!spa_pod_is_rectangle(pod))
317 return -EINVAL;
318 SPA_POD_BODY_LOAD_FIELD_ONCE(value, body, width);
319 SPA_POD_BODY_LOAD_FIELD_ONCE(value, body, height);
320 return 0;
321 }
322  
323 SPA_API_POD_BODY int spa_pod_is_fraction(const struct spa_pod *pod)
324 {
325 return SPA_POD_CHECK(pod, SPA_TYPE_Fraction, sizeof(struct spa_fraction));
326 }
327 SPA_API_POD_BODY int spa_pod_body_get_fraction(const struct spa_pod *pod, const void *body,
328 struct spa_fraction *value)
329 {
330 if (!spa_pod_is_fraction(pod))
331 return -EINVAL;
332 SPA_POD_BODY_LOAD_FIELD_ONCE(value, body, num);
333 SPA_POD_BODY_LOAD_FIELD_ONCE(value, body, denom);
334 return 0;
335 }
336  
337 SPA_API_POD_BODY int spa_pod_is_bitmap(const struct spa_pod *pod)
338 {
339 return SPA_POD_CHECK(pod, SPA_TYPE_Bitmap, sizeof(uint8_t));
340 }
341 SPA_API_POD_BODY int spa_pod_body_get_bitmap(const struct spa_pod *pod, const void *body,
342 const uint8_t **value)
343 {
344 if (!spa_pod_is_bitmap(pod))
345 return -EINVAL;
346 *value = (const uint8_t *)body;
347 return 0;
348 }
349  
350 SPA_API_POD_BODY int spa_pod_is_array(const struct spa_pod *pod)
351 {
352 return SPA_POD_CHECK(pod, SPA_TYPE_Array, sizeof(struct spa_pod_array_body));
353 }
354 SPA_API_POD_BODY int spa_pod_body_get_array(const struct spa_pod *pod, const void *body,
355 struct spa_pod_array *arr, const void **arr_body)
356 {
357 if (!spa_pod_is_array(pod))
358 return -EINVAL;
359 arr->pod = *pod;
360 SPA_POD_BODY_LOAD_FIELD_ONCE(&arr->body.child, body, type);
361 SPA_POD_BODY_LOAD_FIELD_ONCE(&arr->body.child, body, size);
362 *arr_body = SPA_PTROFF(body, sizeof(struct spa_pod_array_body), void);
363 return 0;
364 }
365 SPA_API_POD_BODY const void *spa_pod_array_body_get_values(const struct spa_pod_array *arr,
366 const void *body, uint32_t *n_values, uint32_t *val_size, uint32_t *val_type)
367 {
368 uint32_t child_size = arr->body.child.size;
369 *n_values = child_size ? (arr->pod.size - sizeof(arr->body)) / child_size : 0;
370 *val_size = child_size;
371 *val_type = arr->body.child.type;
372 if (*val_size < spa_pod_type_size(*val_type))
373 *n_values = 0;
374 return body;
375 }
376  
377 SPA_API_POD_BODY const void *spa_pod_body_get_array_values(const struct spa_pod *pod,
378 const void *body, uint32_t *n_values, uint32_t *val_size, uint32_t *val_type)
379 {
380 struct spa_pod_array arr;
381 if (spa_pod_body_get_array(pod, body, &arr, &body) < 0)
382 return NULL;
383 return spa_pod_array_body_get_values(&arr, body, n_values, val_size, val_type);
384 }
385  
386 SPA_API_POD_BODY int spa_pod_is_choice(const struct spa_pod *pod)
387 {
388 return SPA_POD_CHECK(pod, SPA_TYPE_Choice, sizeof(struct spa_pod_choice_body));
389 }
390 SPA_API_POD_BODY int spa_pod_body_get_choice(const struct spa_pod *pod, const void *body,
391 struct spa_pod_choice *choice, const void **choice_body)
392 {
393 if (!spa_pod_is_choice(pod))
394 return -EINVAL;
395 choice->pod = *pod;
396 SPA_POD_BODY_LOAD_FIELD_ONCE(&choice->body, body, type);
397 SPA_POD_BODY_LOAD_FIELD_ONCE(&choice->body, body, flags);
398 SPA_POD_BODY_LOAD_FIELD_ONCE(&choice->body, body, child.size);
399 SPA_POD_BODY_LOAD_FIELD_ONCE(&choice->body, body, child.type);
400 *choice_body = SPA_PTROFF(body, sizeof(struct spa_pod_choice_body), void);
401 return 0;
402 }
403 SPA_API_POD_BODY const void *spa_pod_choice_body_get_values(const struct spa_pod_choice *pod,
404 const void *body, uint32_t *n_values, uint32_t *choice,
405 uint32_t *val_size, uint32_t *val_type)
406 {
407 uint32_t child_size = pod->body.child.size, min, max;
408 *val_size = child_size;
409 *val_type = pod->body.child.type;
410 *n_values = child_size ? (pod->pod.size - sizeof(pod->body)) / child_size : 0;
411 *choice = pod->body.type;
412 spa_pod_choice_n_values(*choice, &min, &max);
413 if (*n_values < min || *val_size < spa_pod_type_size(*val_type))
414 *n_values = 0;
415 else if (*n_values > max)
416 *n_values = max;
417 return body;
418 }
419  
420 SPA_API_POD_BODY int spa_pod_is_struct(const struct spa_pod *pod)
421 {
422 return SPA_POD_CHECK_TYPE(pod, SPA_TYPE_Struct);
423 }
424  
425 SPA_API_POD_BODY int spa_pod_is_object(const struct spa_pod *pod)
426 {
427 return SPA_POD_CHECK(pod, SPA_TYPE_Object, sizeof(struct spa_pod_object_body));
428 }
429 SPA_API_POD_BODY int spa_pod_body_get_object(const struct spa_pod *pod, const void *body,
430 struct spa_pod_object *object, const void **object_body)
431 {
432 if (!spa_pod_is_object(pod))
433 return -EINVAL;
434 object->pod = *pod;
435 SPA_POD_BODY_LOAD_FIELD_ONCE(&object->body, body, type);
436 SPA_POD_BODY_LOAD_FIELD_ONCE(&object->body, body, id);
437 *object_body = SPA_PTROFF(body, sizeof(struct spa_pod_object_body), void);
438 return 0;
439 }
440  
441 SPA_API_POD_BODY int spa_pod_is_sequence(const struct spa_pod *pod)
442 {
443 return SPA_POD_CHECK(pod, SPA_TYPE_Sequence, sizeof(struct spa_pod_sequence_body));
444 }
445 SPA_API_POD_BODY int spa_pod_body_get_sequence(const struct spa_pod *pod, const void *body,
446 struct spa_pod_sequence *seq, const void **seq_body)
447 {
448 if (!spa_pod_is_sequence(pod))
449 return -EINVAL;
450 seq->pod = *pod;
451 SPA_POD_BODY_LOAD_FIELD_ONCE(&seq->body, body, unit);
452 SPA_POD_BODY_LOAD_FIELD_ONCE(&seq->body, body, pad);
453 *seq_body = SPA_PTROFF(body, sizeof(struct spa_pod_sequence_body), void);
454 return 0;
455 }
456  
457 /**
458 * \}
459 */
460  
461 #ifdef __cplusplus
462 } /* extern "C" */
463 #endif
464  
465 #endif /* SPA_POD_BODY_H */