1 /* Simple Plugin API
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_PARAM_AUDIO_FORMAT_UTILS_H
26 #define SPA_PARAM_AUDIO_FORMAT_UTILS_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 
33 #include <spa/pod/parser.h>
34 #include <spa/pod/builder.h>
35 #include <spa/param/audio/format.h>
36 #include <spa/param/format-utils.h>
37 
38 static inline int
spa_format_audio_raw_parse(const struct spa_pod * format,struct spa_audio_info_raw * info)39 spa_format_audio_raw_parse(const struct spa_pod *format, struct spa_audio_info_raw *info)
40 {
41 	struct spa_pod *position = NULL;
42 	int res;
43 	info->flags = 0;
44 	res = spa_pod_parse_object(format,
45 			SPA_TYPE_OBJECT_Format, NULL,
46 			SPA_FORMAT_AUDIO_format,	SPA_POD_Id(&info->format),
47 			SPA_FORMAT_AUDIO_rate,		SPA_POD_Int(&info->rate),
48 			SPA_FORMAT_AUDIO_channels,	SPA_POD_Int(&info->channels),
49 			SPA_FORMAT_AUDIO_position,	SPA_POD_OPT_Pod(&position));
50 	if (position == NULL ||
51 	    !spa_pod_copy_array(position, SPA_TYPE_Id, info->position, SPA_AUDIO_MAX_CHANNELS))
52 		SPA_FLAG_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
53 
54 	return res;
55 }
56 
57 static inline int
spa_format_audio_dsp_parse(const struct spa_pod * format,struct spa_audio_info_dsp * info)58 spa_format_audio_dsp_parse(const struct spa_pod *format, struct spa_audio_info_dsp *info)
59 {
60 	int res;
61 	res = spa_pod_parse_object(format,
62 			SPA_TYPE_OBJECT_Format, NULL,
63 			SPA_FORMAT_AUDIO_format,	SPA_POD_Id(&info->format));
64 	return res;
65 }
66 
67 static inline struct spa_pod *
spa_format_audio_raw_build(struct spa_pod_builder * builder,uint32_t id,struct spa_audio_info_raw * info)68 spa_format_audio_raw_build(struct spa_pod_builder *builder, uint32_t id, struct spa_audio_info_raw *info)
69 {
70 	struct spa_pod_frame f;
71 	spa_pod_builder_push_object(builder, &f, SPA_TYPE_OBJECT_Format, id);
72 	spa_pod_builder_add(builder,
73 			SPA_FORMAT_mediaType,		SPA_POD_Id(SPA_MEDIA_TYPE_audio),
74 			SPA_FORMAT_mediaSubtype,	SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
75 			SPA_FORMAT_AUDIO_format,	SPA_POD_Id(info->format),
76 			SPA_FORMAT_AUDIO_rate,		SPA_POD_Int(info->rate),
77 			SPA_FORMAT_AUDIO_channels,	SPA_POD_Int(info->channels),
78 			0);
79 
80 	if (!SPA_FLAG_IS_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED)) {
81 		spa_pod_builder_prop(builder, SPA_FORMAT_AUDIO_position, 0);
82 		spa_pod_builder_array(builder, sizeof(uint32_t), SPA_TYPE_Id,
83 				info->channels, info->position);
84 	}
85 	return (struct spa_pod*)spa_pod_builder_pop(builder, &f);
86 }
87 
88 static inline struct spa_pod *
spa_format_audio_dsp_build(struct spa_pod_builder * builder,uint32_t id,struct spa_audio_info_dsp * info)89 spa_format_audio_dsp_build(struct spa_pod_builder *builder, uint32_t id, struct spa_audio_info_dsp *info)
90 {
91 	struct spa_pod_frame f;
92 	spa_pod_builder_push_object(builder, &f, SPA_TYPE_OBJECT_Format, id);
93 	spa_pod_builder_add(builder,
94 			SPA_FORMAT_mediaType,		SPA_POD_Id(SPA_MEDIA_TYPE_audio),
95 			SPA_FORMAT_mediaSubtype,	SPA_POD_Id(SPA_MEDIA_SUBTYPE_dsp),
96 			SPA_FORMAT_AUDIO_format,	SPA_POD_Id(info->format),
97 			0);
98 	return (struct spa_pod*)spa_pod_builder_pop(builder, &f);
99 }
100 
101 #ifdef __cplusplus
102 }  /* extern "C" */
103 #endif
104 
105 #endif /* SPA_PARAM_AUDIO_FORMAT_UTILS_H */
106