PipeWire 1.5.0
Loading...
Searching...
No Matches
compare.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_POD_COMPARE_H
6#define SPA_POD_COMPARE_H
7
8#include <stdarg.h>
9#include <errno.h>
10#include <stdint.h>
11#include <stddef.h>
12#include <stdio.h>
13#include <string.h>
14
15#include <spa/param/props.h>
16#include <spa/pod/iter.h>
17#include <spa/pod/builder.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#ifndef SPA_API_POD_COMPARE
24 #ifdef SPA_API_IMPL
25 #define SPA_API_POD_COMPARE SPA_API_IMPL
26 #else
27 #define SPA_API_POD_COMPARE static inline
28 #endif
29#endif
30
36SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
37{
38 switch (type) {
39 case SPA_TYPE_None:
40 return 0;
41 case SPA_TYPE_Bool:
42 return SPA_CMP(!!*(int32_t *)r1, !!*(int32_t *)r2);
44 return SPA_CMP(*(uint32_t *)r1, *(uint32_t *)r2);
45 case SPA_TYPE_Int:
46 return SPA_CMP(*(int32_t *)r1, *(int32_t *)r2);
47 case SPA_TYPE_Long:
48 return SPA_CMP(*(int64_t *)r1, *(int64_t *)r2);
49 case SPA_TYPE_Float:
50 return SPA_CMP(*(float *)r1, *(float *)r2);
51 case SPA_TYPE_Double:
52 return SPA_CMP(*(double *)r1, *(double *)r2);
53 case SPA_TYPE_String:
54 return strncmp((char *)r1, (char *)r2, size);
56 {
57 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
58 *rec2 = (struct spa_rectangle *) r2;
59 uint64_t a1, a2;
60 a1 = ((uint64_t) rec1->width) * rec1->height;
61 a2 = ((uint64_t) rec2->width) * rec2->height;
62 if (a1 < a2)
63 return -1;
64 if (a1 > a2)
65 return 1;
66 return SPA_CMP(rec1->width, rec2->width);
67 }
69 {
70 const struct spa_fraction *f1 = (struct spa_fraction *) r1,
71 *f2 = (struct spa_fraction *) r2;
72 uint64_t n1, n2;
73 n1 = ((uint64_t) f1->num) * f2->denom;
74 n2 = ((uint64_t) f2->num) * f1->denom;
75 return SPA_CMP(n1, n2);
76 }
77 default:
78 return memcmp(r1, r2, size);
79 }
80 return 0;
81}
82
83SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1,
84 const struct spa_pod *pod2)
85{
86 int res = 0;
87 uint32_t n_vals1, n_vals2;
88 uint32_t choice1, choice2;
89
90 spa_return_val_if_fail(pod1 != NULL, -EINVAL);
91 spa_return_val_if_fail(pod2 != NULL, -EINVAL);
92
93 pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1);
94 pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2);
95
96 if (n_vals1 != n_vals2)
97 return -EINVAL;
98
99 if (pod1->type != pod2->type)
100 return -EINVAL;
101
102 if (n_vals1 < 1)
103 return -EINVAL; /* empty choice */
104
105 switch (pod1->type) {
106 case SPA_TYPE_Struct:
107 {
108 const struct spa_pod *p1, *p2;
109 size_t p1s, p2s;
110
111 p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
112 p1s = SPA_POD_BODY_SIZE(pod1);
113 p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
114 p2s = SPA_POD_BODY_SIZE(pod2);
115
116 while (true) {
117 if (!spa_pod_is_inside(pod1, p1s, p1) ||
118 !spa_pod_is_inside(pod2, p2s, p2))
119 return -EINVAL;
120
121 if ((res = spa_pod_compare(p1, p2)) != 0)
122 return res;
123
124 p1 = (const struct spa_pod*)spa_pod_next(p1);
125 p2 = (const struct spa_pod*)spa_pod_next(p2);
126 }
127 break;
128 }
129 case SPA_TYPE_Object:
130 {
131 const struct spa_pod_prop *p1, *p2;
132 const struct spa_pod_object *o1, *o2;
133
134 o1 = (const struct spa_pod_object*)pod1;
135 o2 = (const struct spa_pod_object*)pod2;
136
137 p2 = NULL;
138 SPA_POD_OBJECT_FOREACH(o1, p1) {
139 if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
140 return 1;
141 if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
142 return res;
143 }
144 p1 = NULL;
145 SPA_POD_OBJECT_FOREACH(o2, p2) {
146 if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
147 return -1;
148 }
149 break;
150 }
151 case SPA_TYPE_Array:
152 {
153 if (pod1->size != pod2->size)
154 return -EINVAL;
155 res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), pod2->size);
156 break;
157 }
158 default:
159 if (pod1->size != pod2->size)
160 return -EINVAL;
161 if (pod1->size < spa_pod_type_size(pod1->type))
162 return -EINVAL;
164 SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
165 pod1->size);
166 break;
167 }
168 return res;
169}
170
171SPA_API_POD_COMPARE int spa_pod_compare_is_compatible_flags(uint32_t type, const void *r1,
172 const void *r2, uint32_t size SPA_UNUSED)
173{
174 switch (type) {
175 case SPA_TYPE_Int:
176 return ((*(int32_t *) r1) & (*(int32_t *) r2)) != 0;
177 case SPA_TYPE_Long:
178 return ((*(int64_t *) r1) & (*(int64_t *) r2)) != 0;
179 default:
180 return -ENOTSUP;
181 }
182 return 0;
183}
184
185
186SPA_API_POD_COMPARE int spa_pod_compare_is_step_of(uint32_t type, const void *r1,
187 const void *r2, uint32_t size)
188{
189 switch (type) {
190 case SPA_TYPE_Int:
191 if (*(int32_t *)r2 < 1)
192 return -EINVAL;
193 return *(int32_t *) r1 % *(int32_t *) r2 == 0;
194 case SPA_TYPE_Long:
195 if (*(int64_t *)r2 < 1)
196 return -EINVAL;
197 return *(int64_t *) r1 % *(int64_t *) r2 == 0;
199 {
200 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
201 *rec2 = (struct spa_rectangle *) r2;
202
203 if (rec2->width < 1 || rec2->height < 1)
204 return -EINVAL;
205
206 return (rec1->width % rec2->width == 0 &&
207 rec1->height % rec2->height == 0);
208 }
209 default:
210 return -ENOTSUP;
211 }
212 return 0;
213}
214
215SPA_API_POD_COMPARE int spa_pod_compare_is_in_range(uint32_t type, const void *v,
216 const void *min, const void *max, const void *step, uint32_t size SPA_UNUSED)
217{
218 if (spa_pod_compare_value(type, v, min, size) < 0 ||
219 spa_pod_compare_value(type, v, max, size) > 0)
220 return 0;
221 if (step != NULL)
222 return spa_pod_compare_is_step_of(type, v, step, size);
223 return 1;
224}
225
226SPA_API_POD_COMPARE int spa_pod_compare_is_valid_choice(uint32_t type, uint32_t size,
227 const void *val, const void *vals, uint32_t n_vals, uint32_t choice)
228{
229 switch (choice) {
230 case SPA_CHOICE_None:
231 if (spa_pod_compare_value(type, val, vals, size) == 0)
232 return 1;
233 return 0;
234 case SPA_CHOICE_Enum:
235 {
236 const void *next = vals;
237 for (uint32_t i = 1; i < n_vals; i++) {
238 next = SPA_PTROFF(next, size, void);
239 if (spa_pod_compare_value(type, val, next, size) == 0)
240 return 1;
241 }
242 return 0;
243 }
244 case SPA_CHOICE_Range:
245 case SPA_CHOICE_Step:
246 {
247 void *min = SPA_PTROFF(vals,size,void);
248 void *max = SPA_PTROFF(min,size,void);
249 void *step = choice == SPA_CHOICE_Step ? SPA_PTROFF(max,size,void) : NULL;
250 return spa_pod_compare_is_in_range(type, val, min, max, step, size);
251 }
252 case SPA_CHOICE_Flags:
253 return 1;
254 }
255 return 0;
256}
257
262#ifdef __cplusplus
263}
264#endif
265
266#endif
spa/pod/builder.h
#define SPA_API_POD_COMPARE
Definition compare.h:34
uint32_t int int res
Definition core.h:433
SPA_API_POD_COMPARE int spa_pod_compare_is_in_range(uint32_t type, const void *v, const void *min, const void *max, const void *step, uint32_t size 1)
Definition compare.h:222
SPA_API_POD_COMPARE int spa_pod_compare_is_step_of(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:193
#define SPA_POD_BODY_CONST(pod)
Definition pod.h:46
SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, const struct spa_pod *pod2)
Definition compare.h:90
SPA_API_POD_ITER struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:229
SPA_API_POD_COMPARE int spa_pod_compare_is_valid_choice(uint32_t type, uint32_t size, const void *val, const void *vals, uint32_t n_vals, uint32_t choice)
Definition compare.h:233
SPA_API_POD_ITER const struct spa_pod_prop * spa_pod_object_find_prop(const struct spa_pod_object *pod, const struct spa_pod_prop *start, uint32_t key)
Definition iter.h:254
SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:43
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:118
#define SPA_POD_BODY(pod)
Definition pod.h:44
SPA_API_POD_ITER void * spa_pod_next(const void *iter)
Definition iter.h:46
#define SPA_POD_BODY_SIZE(pod)
Definition pod.h:31
SPA_API_POD_COMPARE int spa_pod_compare_is_compatible_flags(uint32_t type, const void *r1, const void *r2, uint32_t size 1)
Definition compare.h:178
SPA_API_POD_BODY uint32_t spa_pod_type_size(uint32_t type)
Definition body.h:45
SPA_API_POD_ITER bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:38
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:163
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:161
@ SPA_CHOICE_Flags
flags: first value is flags
Definition pod.h:165
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:162
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:164
@ SPA_TYPE_Int
Definition type.h:45
@ SPA_TYPE_Rectangle
Definition type.h:51
@ SPA_TYPE_Long
Definition type.h:46
@ SPA_TYPE_Bool
Definition type.h:43
@ SPA_TYPE_Object
Definition type.h:56
@ SPA_TYPE_Float
Definition type.h:47
@ SPA_TYPE_Fraction
Definition type.h:52
@ SPA_TYPE_None
Definition type.h:42
@ SPA_TYPE_Double
Definition type.h:48
@ SPA_TYPE_Id
Definition type.h:44
@ SPA_TYPE_Array
Definition type.h:54
@ SPA_TYPE_String
Definition type.h:49
@ SPA_TYPE_Struct
Definition type.h:55
#define SPA_UNUSED
Definition defs.h:309
#define spa_return_val_if_fail(expr, val)
Definition defs.h:460
#define SPA_CMP(a, b)
3-way comparison.
Definition defs.h:209
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
spa/pod/iter.h
spa/utils/string.h
Definition defs.h:137
uint32_t num
Definition defs.h:138
uint32_t denom
Definition defs.h:139
Definition pod.h:202
Definition pod.h:227
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:228
struct spa_pod value
Definition pod.h:249
Definition pod.h:57
uint32_t type
Definition pod.h:59
uint32_t size
Definition pod.h:58
Definition defs.h:116
uint32_t width
Definition defs.h:117
uint32_t height
Definition defs.h:118