+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 118 lines · 3.4 KB · SPDX: MIT
main
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4  
5 #ifndef SPA_AUDIO_RAW_UTILS_H
6 #define SPA_AUDIO_RAW_UTILS_H
7  
8 #include <spa/pod/parser.h>
9 #include <spa/pod/iter.h>
10 #include <spa/pod/builder.h>
11 #include <spa/param/audio/format.h>
12 #include <spa/param/format-utils.h>
13  
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17  
18 /**
19 * \addtogroup spa_param
20 * \{
21 */
22  
23 #ifndef SPA_API_AUDIO_RAW_UTILS
24 #ifdef SPA_API_IMPL
25 #define SPA_API_AUDIO_RAW_UTILS SPA_API_IMPL
26 #else
27 #define SPA_API_AUDIO_RAW_UTILS static inline
28 #endif
29 #endif
30  
31 SPA_API_AUDIO_RAW_UTILS int
32 spa_format_audio_raw_ext_parse(const struct spa_pod *format, struct spa_audio_info_raw *info, size_t size)
33 {
34 struct spa_pod *position = NULL;
35 int res;
36 uint32_t max_position = SPA_AUDIO_INFO_RAW_MAX_POSITION(size);
37  
38 if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size))
39 return -EINVAL;
40  
41 info->flags = 0;
42 res = spa_pod_parse_object(format,
43 SPA_TYPE_OBJECT_Format, NULL,
44 SPA_FORMAT_AUDIO_format, SPA_POD_OPT_Id(&info->format),
45 SPA_FORMAT_AUDIO_rate, SPA_POD_OPT_Int(&info->rate),
46 SPA_FORMAT_AUDIO_channels, SPA_POD_OPT_Int(&info->channels),
47 SPA_FORMAT_AUDIO_position, SPA_POD_OPT_Pod(&position));
48 if (info->channels > max_position)
49 return -ECHRNG;
50 if (position == NULL ||
51 spa_pod_copy_array(position, SPA_TYPE_Id, info->position, max_position) != info->channels) {
52 SPA_FLAG_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
53 spa_memzero(info->position, max_position * sizeof(info->position[0]));
54 }
55  
56 return res;
57 }
58  
59 SPA_API_AUDIO_RAW_UTILS int
60 spa_format_audio_raw_parse(const struct spa_pod *format, struct spa_audio_info_raw *info)
61 {
62 return spa_format_audio_raw_ext_parse(format, info, sizeof(*info));
63 }
64  
65 SPA_API_AUDIO_RAW_UTILS struct spa_pod *
66 spa_format_audio_raw_ext_build(struct spa_pod_builder *builder, uint32_t id,
67 const struct spa_audio_info_raw *info, size_t size)
68 {
69 struct spa_pod_frame f;
70 uint32_t max_position = SPA_AUDIO_INFO_RAW_MAX_POSITION(size);
71  
72 if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size)) {
73 errno = EINVAL;
74 return NULL;
75 }
76  
77 spa_pod_builder_push_object(builder, &f, SPA_TYPE_OBJECT_Format, id);
78 spa_pod_builder_add(builder,
79 SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_audio),
80 SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
81 0);
82 if (info->format != SPA_AUDIO_FORMAT_UNKNOWN)
83 spa_pod_builder_add(builder,
84 SPA_FORMAT_AUDIO_format, SPA_POD_Id(info->format), 0);
85 if (info->rate != 0)
86 spa_pod_builder_add(builder,
87 SPA_FORMAT_AUDIO_rate, SPA_POD_Int(info->rate), 0);
88 if (info->channels != 0) {
89 spa_pod_builder_add(builder,
90 SPA_FORMAT_AUDIO_channels, SPA_POD_Int(info->channels), 0);
91 /* we drop the positions here when we can't read all of them. This is
92 * really a malformed spa_audio_info structure. */
93 if (!SPA_FLAG_IS_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED) &&
94 info->channels <= max_position) {
95 spa_pod_builder_add(builder, SPA_FORMAT_AUDIO_position,
96 SPA_POD_Array(sizeof(uint32_t), SPA_TYPE_Id,
97 info->channels, info->position), 0);
98 }
99 }
100 return (struct spa_pod*)spa_pod_builder_pop(builder, &f);
101 }
102  
103 SPA_API_AUDIO_RAW_UTILS struct spa_pod *
104 spa_format_audio_raw_build(struct spa_pod_builder *builder, uint32_t id,
105 const struct spa_audio_info_raw *info)
106 {
107 return spa_format_audio_raw_ext_build(builder, id, info, sizeof(*info));
108 }
109  
110 /**
111 * \}
112 */
113  
114 #ifdef __cplusplus
115 } /* extern "C" */
116 #endif
117  
118 #endif /* SPA_AUDIO_RAW_UTILS_H */