PipeWire 1.6.5

Loading...
Searching...
No Matches
raw-utils.h
Go to the documentation of this file.
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
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
111 * \}
112 */
113
114#ifdef __cplusplus
115} /* extern "C" */
116#endif
117
118#endif /* SPA_AUDIO_RAW_UTILS_H */
builder.h
spa/pod/builder.h
format-utils.h
res
uint32_t int int res
Definition core.h:433
SPA_AUDIO_FLAG_UNPOSITIONED
#define SPA_AUDIO_FLAG_UNPOSITIONED
Definition raw.h:288
SPA_AUDIO_INFO_RAW_MAX_POSITION
#define SPA_AUDIO_INFO_RAW_MAX_POSITION(size)
Definition raw.h:307
spa_format_audio_raw_parse
SPA_API_AUDIO_RAW_UTILS int spa_format_audio_raw_parse(const struct spa_pod *format, struct spa_audio_info_raw *info)
Definition raw-utils.h:67
spa_format_audio_raw_ext_build
SPA_API_AUDIO_RAW_UTILS struct spa_pod * spa_format_audio_raw_ext_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_audio_info_raw *info, size_t size)
Definition raw-utils.h:73
SPA_AUDIO_INFO_RAW_VALID_SIZE
#define SPA_AUDIO_INFO_RAW_VALID_SIZE(size)
Definition raw.h:310
SPA_API_AUDIO_RAW_UTILS
#define SPA_API_AUDIO_RAW_UTILS
Definition raw-utils.h:34
spa_format_audio_raw_build
SPA_API_AUDIO_RAW_UTILS struct spa_pod * spa_format_audio_raw_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_audio_info_raw *info)
Definition raw-utils.h:111
spa_format_audio_raw_ext_parse
SPA_API_AUDIO_RAW_UTILS int spa_format_audio_raw_ext_parse(const struct spa_pod *format, struct spa_audio_info_raw *info, size_t size)
Definition raw-utils.h:39
SPA_MEDIA_TYPE_audio
@ SPA_MEDIA_TYPE_audio
Definition format.h:27
SPA_FORMAT_mediaType
@ SPA_FORMAT_mediaType
media type (Id enum spa_media_type)
Definition format.h:99
SPA_FORMAT_AUDIO_position
@ SPA_FORMAT_AUDIO_position
channel positions (Id enum spa_audio_position)
Definition format.h:108
SPA_FORMAT_AUDIO_rate
@ SPA_FORMAT_AUDIO_rate
sample rate (Int)
Definition format.h:106
SPA_FORMAT_mediaSubtype
@ SPA_FORMAT_mediaSubtype
media subtype (Id enum spa_media_subtype)
Definition format.h:100
SPA_FORMAT_AUDIO_format
@ SPA_FORMAT_AUDIO_format
audio format, (Id enum spa_audio_format)
Definition format.h:104
SPA_FORMAT_AUDIO_channels
@ SPA_FORMAT_AUDIO_channels
number of audio channels (Int)
Definition format.h:107
SPA_MEDIA_SUBTYPE_raw
@ SPA_MEDIA_SUBTYPE_raw
Definition format.h:38
SPA_AUDIO_FORMAT_UNKNOWN
@ SPA_AUDIO_FORMAT_UNKNOWN
Definition raw.h:34
spa_pod_builder_pop
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:213
SPA_POD_Array
#define SPA_POD_Array(csize, ctype, n_vals, vals)
Definition vararg.h:128
spa_pod_builder_add
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition builder.h:713
spa_pod_copy_array
#define spa_pod_copy_array(pod, type, values, max_values)
Definition iter.h:226
SPA_POD_Id
#define SPA_POD_Id(val)
Definition vararg.h:53
spa_pod_builder_push_object
SPA_API_POD_BUILDER int spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t id)
Definition builder.h:475
SPA_POD_OPT_Int
#define SPA_POD_OPT_Int(val)
Definition parser.h:866
SPA_POD_OPT_Id
#define SPA_POD_OPT_Id(val)
Definition parser.h:864
SPA_POD_OPT_Pod
#define SPA_POD_OPT_Pod(val)
Definition parser.h:890
spa_pod_parse_object
#define spa_pod_parse_object(pod, type, id,...)
Definition parser.h:935
SPA_POD_Int
#define SPA_POD_Int(val)
Definition vararg.h:58
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:44
SPA_TYPE_OBJECT_Format
@ SPA_TYPE_OBJECT_Format
Definition type.h:87
SPA_FLAG_SET
#define SPA_FLAG_SET(field, flag)
Definition defs.h:93
SPA_FLAG_IS_SET
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:90
spa_memzero
#define spa_memzero(x, l)
Definition defs.h:511
iter.h
spa/pod/iter.h
format.h
spa/param/audio/format.h
parser.h
spa/pod/parser.h
spa_audio_info_raw
Audio information description.
Definition raw.h:292
spa_pod_builder
Definition builder.h:63
spa_pod_frame
Definition body.h:38
spa_pod
Definition pod.h:57
spa_pod::size
uint32_t size
Definition pod.h:58