PipeWire 1.2.8
Loading...
Searching...
No Matches
filter.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_FILTER_H
6#define SPA_POD_FILTER_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <errno.h>
13#include <stdint.h>
14#include <stddef.h>
15#include <stdio.h>
16#include <string.h>
17
18#include <spa/param/props.h>
19#include <spa/pod/iter.h>
20#include <spa/pod/builder.h>
21#include <spa/pod/compare.h>
22
28static inline int spa_pod_choice_fix_default(struct spa_pod_choice *choice)
29{
30 void *val, *alt;
31 int i, nvals;
32 uint32_t type, size;
34 nvals = SPA_POD_CHOICE_N_VALUES(choice);
35 type = SPA_POD_CHOICE_VALUE_TYPE(choice);
36 size = SPA_POD_CHOICE_VALUE_SIZE(choice);
37 alt = val = SPA_POD_CHOICE_VALUES(choice);
38
39 switch (choice->body.type) {
40 case SPA_CHOICE_None:
41 break;
43 case SPA_CHOICE_Step:
44 if (nvals > 1) {
45 alt = SPA_PTROFF(alt, size, void);
46 if (spa_pod_compare_value(type, val, alt, size) < 0)
47 memcpy(val, alt, size);
48 }
49 if (nvals > 2) {
50 alt = SPA_PTROFF(alt, size, void);
51 if (spa_pod_compare_value(type, val, alt, size) > 0)
52 memcpy(val, alt, size);
53 }
54 break;
56 case SPA_CHOICE_Enum:
57 {
58 void *best = NULL;
59
60 for (i = 1; i < nvals; i++) {
61 alt = SPA_PTROFF(alt, size, void);
62 if (spa_pod_compare_value(type, val, alt, size) == 0) {
63 best = alt;
64 break;
65 }
66 if (best == NULL)
67 best = alt;
68 }
69 if (best)
70 memcpy(val, best, size);
71
72 if (nvals <= 1)
73 choice->body.type = SPA_CHOICE_None;
74 break;
75 }
76 }
77 return 0;
78}
79
80static inline int spa_pod_filter_flags_value(struct spa_pod_builder *b,
81 uint32_t type, const void *r1, const void *r2, uint32_t size SPA_UNUSED)
82{
83 switch (type) {
84 case SPA_TYPE_Int:
85 {
86 int32_t val = (*(int32_t *) r1) & (*(int32_t *) r2);
87 if (val == 0)
88 return 0;
89 spa_pod_builder_int(b, val);
90 break;
91 }
92 case SPA_TYPE_Long:
93 {
94 int64_t val = (*(int64_t *) r1) & (*(int64_t *) r2);
95 if (val == 0)
96 return 0;
98 break;
99 }
100 default:
101 return -ENOTSUP;
102 }
103 return 1;
104}
105
106static inline int spa_pod_filter_is_step_of(uint32_t type, const void *r1,
107 const void *r2, uint32_t size SPA_UNUSED)
108{
109 switch (type) {
110 case SPA_TYPE_Int:
111 return *(int32_t *) r1 % *(int32_t *) r2 == 0;
112 case SPA_TYPE_Long:
113 return *(int64_t *) r1 % *(int64_t *) r2 == 0;
115 {
116 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
117 *rec2 = (struct spa_rectangle *) r2;
118
119 return (rec1->width % rec2->width == 0 &&
120 rec1->height % rec2->height == 0);
121 }
122 default:
123 return -ENOTSUP;
124 }
125 return 0;
126}
127
128static inline int
130 const struct spa_pod_prop *p1,
131 const struct spa_pod_prop *p2)
132{
133 const struct spa_pod *v1, *v2;
134 struct spa_pod_choice *nc, dummy;
135 uint32_t j, k, nalt1, nalt2;
136 void *alt1, *alt2, *a1, *a2;
137 uint32_t type, size, p1c, p2c;
138 struct spa_pod_frame f;
139
140 v1 = spa_pod_get_values(&p1->value, &nalt1, &p1c);
141 alt1 = SPA_POD_BODY(v1);
142 v2 = spa_pod_get_values(&p2->value, &nalt2, &p2c);
143 alt2 = SPA_POD_BODY(v2);
144
145 type = v1->type;
146 size = v1->size;
147
148 /* incompatible property types */
149 if (type != v2->type || size != v2->size || p1->key != p2->key)
150 return -EINVAL;
151
152 if (p1c == SPA_CHOICE_None || p1c == SPA_CHOICE_Flags) {
153 nalt1 = 1;
154 } else {
155 alt1 = SPA_PTROFF(alt1, size, void);
156 nalt1--;
157 }
158
159 if (p2c == SPA_CHOICE_None || p2c == SPA_CHOICE_Flags) {
160 nalt2 = 1;
161 } else {
162 alt2 = SPA_PTROFF(alt2, size, void);
163 nalt2--;
164 }
165
166 /* start with copying the property */
167 spa_pod_builder_prop(b, p1->key, p1->flags & p2->flags);
168 spa_pod_builder_push_choice(b, &f, 0, 0);
169 nc = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f);
170 /* write to dummy value when builder overflows. We don't want to error
171 * because overflowing is a way to determine the required buffer size. */
172 if (nc == NULL)
173 nc = &dummy;
174
175 /* default value */
177
178 if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_None) ||
179 (p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Enum) ||
180 (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_None) ||
181 (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Enum)) {
182 int n_copied = 0;
183 /* copy all equal values but don't copy the default value again */
184 for (j = 0, a1 = alt1; j < nalt1; j++, a1 = SPA_PTROFF(a1, size, void)) {
185 for (k = 0, a2 = alt2; k < nalt2; k++, a2 = SPA_PTROFF(a2,size,void)) {
186 if (spa_pod_compare_value(type, a1, a2, size) == 0) {
187 if (p1c == SPA_CHOICE_Enum || j > 0)
188 spa_pod_builder_raw(b, a1, size);
189 n_copied++;
190 }
191 }
192 }
193 if (n_copied == 0)
194 return -EINVAL;
196 }
197
198 if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Range) ||
199 (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Range)) {
200 int n_copied = 0;
201 /* copy all values inside the range */
202 for (j = 0, a1 = alt1, a2 = alt2; j < nalt1; j++, a1 = SPA_PTROFF(a1,size,void)) {
203 if (spa_pod_compare_value(type, a1, a2, size) < 0)
204 continue;
205 if (spa_pod_compare_value(type, a1, SPA_PTROFF(a2,size,void), size) > 0)
206 continue;
207 spa_pod_builder_raw(b, a1, size);
208 n_copied++;
209 }
210 if (n_copied == 0)
211 return -EINVAL;
213 }
214
215 if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Step) ||
216 (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Step)) {
217 int n_copied = 0;
218 for (j = 0, a1 = alt1, a2 = alt2; j < nalt1; j++, a1 = SPA_PTROFF(a1,size,void)) {
219 int res;
220 if (spa_pod_compare_value(type, a1, a2, size) < 0)
221 continue;
222 if (spa_pod_compare_value(type, a1, SPA_PTROFF(a2,size,void), size) > 0)
223 continue;
224
225 res = spa_pod_filter_is_step_of(type, a1, SPA_PTROFF(a2,size*2,void), size);
226 if (res == 0)
227 continue;
228 if (res == -ENOTSUP)
229 return -EINVAL;
230
231 spa_pod_builder_raw(b, a1, size);
232 n_copied++;
233 }
234 if (n_copied == 0)
235 return -EINVAL;
237 }
238
239 if ((p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_None) ||
240 (p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Enum)) {
241 int n_copied = 0;
242 /* copy all values inside the range */
243 for (k = 0, a1 = alt1, a2 = alt2; k < nalt2; k++, a2 = SPA_PTROFF(a2,size,void)) {
244 if (spa_pod_compare_value(type, a2, a1, size) < 0)
245 continue;
246 if (spa_pod_compare_value(type, a2, SPA_PTROFF(a1,size,void), size) > 0)
247 continue;
248 spa_pod_builder_raw(b, a2, size);
249 n_copied++;
250 }
251 if (n_copied == 0)
252 return -EINVAL;
254 }
255
256 if ((p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Range) ||
257 (p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Step) ||
258 (p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Range) ||
259 (p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Step)) {
260 if (spa_pod_compare_value(type, alt1, alt2, size) < 0)
261 spa_pod_builder_raw(b, alt2, size);
262 else
263 spa_pod_builder_raw(b, alt1, size);
264
265 alt1 = SPA_PTROFF(alt1,size,void);
266 alt2 = SPA_PTROFF(alt2,size,void);
267
268 if (spa_pod_compare_value(type, alt1, alt2, size) < 0)
269 spa_pod_builder_raw(b, alt1, size);
270 else
271 spa_pod_builder_raw(b, alt2, size);
272
274 }
275
276 if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Flags) ||
277 (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_None) ||
278 (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Flags)) {
279 if (spa_pod_filter_flags_value(b, type, alt1, alt2, size) != 1)
280 return -EINVAL;
282 }
283
284 if (p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Flags)
285 return -ENOTSUP;
286
287 if (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Flags)
288 return -ENOTSUP;
289
290 if ((p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_None) ||
291 (p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Enum)) {
292 int n_copied = 0;
293 for (j = 0, a1 = alt1, a2 = alt2; j < nalt2; j++, a2 = SPA_PTROFF(a1,size,void)) {
294 int res;
295 if (spa_pod_compare_value(type, a2, a1, size) < 0)
296 continue;
297 if (spa_pod_compare_value(type, a2, SPA_PTROFF(a1,size,void), size) > 0)
298 continue;
299
300 res = spa_pod_filter_is_step_of(type, a2, SPA_PTROFF(a1,size*2,void), size);
301 if (res == 0)
302 continue;
303 if (res == -ENOTSUP)
304 return -EINVAL;
305
306 spa_pod_builder_raw(b, a2, size);
307 n_copied++;
308 }
309 if (n_copied == 0)
310 return -EINVAL;
312 }
313 if (p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Flags)
314 return -ENOTSUP;
315
316 if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Range)
317 return -ENOTSUP;
318 if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Step)
319 return -ENOTSUP;
320 if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Enum)
321 return -ENOTSUP;
322
323 spa_pod_builder_pop(b, &f);
325
326 return 0;
327}
328
329static inline int spa_pod_filter_part(struct spa_pod_builder *b,
330 const struct spa_pod *pod, uint32_t pod_size,
331 const struct spa_pod *filter, uint32_t filter_size)
332{
333 const struct spa_pod *pp, *pf;
334 int res = 0;
335
336 pf = filter;
337
338 SPA_POD_FOREACH(pod, pod_size, pp) {
339 bool do_copy = false, do_advance = false;
340 uint32_t filter_offset = 0;
341 struct spa_pod_frame f;
342
343 switch (SPA_POD_TYPE(pp)) {
344 case SPA_TYPE_Object:
345 if (pf != NULL) {
346 struct spa_pod_object *op = (struct spa_pod_object *) pp;
347 struct spa_pod_object *of = (struct spa_pod_object *) pf;
348 const struct spa_pod_prop *p1, *p2;
349
350 if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp))
351 return -EINVAL;
352
354 p2 = NULL;
355 SPA_POD_OBJECT_FOREACH(op, p1) {
356 p2 = spa_pod_object_find_prop(of, p2, p1->key);
357 if (p2 != NULL)
358 res = spa_pod_filter_prop(b, p1, p2);
359 else if ((p1->flags & SPA_POD_PROP_FLAG_MANDATORY) != 0)
360 res = -EINVAL;
361 else
363 if (res < 0)
364 break;
365 }
366 if (res >= 0) {
367 p1 = NULL;
368 SPA_POD_OBJECT_FOREACH(of, p2) {
369 p1 = spa_pod_object_find_prop(op, p1, p2->key);
370 if (p1 != NULL)
371 continue;
372 if ((p2->flags & SPA_POD_PROP_FLAG_MANDATORY) != 0)
373 res = -EINVAL;
374 if (res < 0)
375 break;
377 }
378 }
379 spa_pod_builder_pop(b, &f);
380 do_advance = true;
381 }
382 else
383 do_copy = true;
384 break;
385
386 case SPA_TYPE_Struct:
387 if (pf != NULL) {
388 if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp))
389 return -EINVAL;
390
391 filter_offset = sizeof(struct spa_pod_struct);
393 res = spa_pod_filter_part(b,
394 SPA_PTROFF(pp,filter_offset,const struct spa_pod),
395 SPA_POD_SIZE(pp) - filter_offset,
396 SPA_PTROFF(pf,filter_offset,const struct spa_pod),
397 SPA_POD_SIZE(pf) - filter_offset);
398 spa_pod_builder_pop(b, &f);
399 do_advance = true;
400 }
401 else
402 do_copy = true;
403 break;
404
405 default:
406 if (pf != NULL) {
407 if (SPA_POD_SIZE(pp) != SPA_POD_SIZE(pf))
408 return -EINVAL;
409 if (memcmp(pp, pf, SPA_POD_SIZE(pp)) != 0)
410 return -EINVAL;
411 do_advance = true;
412 }
413 do_copy = true;
414 break;
415 }
416 if (do_copy)
418 if (do_advance) {
419 pf = (const struct spa_pod*)spa_pod_next(pf);
420 if (!spa_pod_is_inside(filter, filter_size, pf))
421 pf = NULL;
422 }
423 if (res < 0)
424 break;
425 }
426 return res;
427}
428
429static inline int
431 struct spa_pod **result,
432 const struct spa_pod *pod,
433 const struct spa_pod *filter)
434{
435 int res;
436 struct spa_pod_builder_state state;
437
438 spa_return_val_if_fail(pod != NULL, -EINVAL);
439 spa_return_val_if_fail(b != NULL, -EINVAL);
440
441 spa_pod_builder_get_state(b, &state);
442 if (filter == NULL)
443 res = spa_pod_builder_raw_padded(b, pod, SPA_POD_SIZE(pod));
444 else
445 res = spa_pod_filter_part(b, pod, SPA_POD_SIZE(pod), filter, SPA_POD_SIZE(filter));
446
447 if (res < 0) {
448 spa_pod_builder_reset(b, &state);
449 } else if (result) {
450 *result = (struct spa_pod*)spa_pod_builder_deref(b, state.offset);
451 if (*result == NULL)
452 res = -ENOSPC;
453 }
454 return res;
455}
456
461#ifdef __cplusplus
462} /* extern "C" */
463#endif
464
465#endif /* SPA_POD_FILTER_H */
spa/pod/builder.h
spa/pod/compare.h
static int spa_pod_filter_is_step_of(uint32_t type, const void *r1, const void *r2, uint32_t size 1)
Definition filter.h:111
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:450
#define SPA_POD_CHOICE_VALUE_TYPE(choice)
Definition pod.h:138
static 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:394
static int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:33
#define SPA_POD_PROP_FLAG_MANDATORY
is mandatory
Definition pod.h:222
#define SPA_POD_CHOICE_VALUE_SIZE(choice)
Definition pod.h:140
static void * spa_pod_next(const void *iter)
Definition iter.h:42
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:114
#define SPA_POD_BODY(pod)
Definition pod.h:39
static struct spa_pod * spa_pod_builder_frame(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:105
static void spa_pod_builder_reset(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:78
#define SPA_POD_TYPE(pod)
Definition pod.h:28
static int spa_pod_filter_flags_value(struct spa_pod_builder *b, uint32_t type, const void *r1, const void *r2, uint32_t size 1)
Definition filter.h:85
static int spa_pod_filter_part(struct spa_pod_builder *b, const struct spa_pod *pod, uint32_t pod_size, const struct spa_pod *filter, uint32_t filter_size)
Definition filter.h:334
static int spa_pod_choice_fix_default(struct spa_pod_choice *choice)
Definition filter.h:33
static int spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:160
static int spa_pod_filter_prop(struct spa_pod_builder *b, const struct spa_pod_prop *p1, const struct spa_pod_prop *p2)
Definition filter.h:134
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:168
static int spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
Definition builder.h:186
static bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:34
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:422
static void spa_pod_builder_get_state(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:65
#define SPA_POD_FOREACH(pod, size, iter)
Definition iter.h:101
#define SPA_POD_CHOICE_VALUES(choice)
Definition pod.h:144
#define SPA_POD_PROP_SIZE(prop)
Definition pod.h:205
static struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:353
static int spa_pod_builder_raw(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:128
static int spa_pod_builder_push_choice(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t flags)
Definition builder.h:406
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:247
static 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:435
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:256
#define SPA_POD_CHOICE_N_VALUES(choice)
Definition pod.h:142
#define SPA_POD_SIZE(pod)
Definition pod.h:30
static struct spa_pod * spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t offset)
Definition builder.h:93
static int spa_pod_filter(struct spa_pod_builder *b, struct spa_pod **result, const struct spa_pod *pod, const struct spa_pod *filter)
Definition filter.h:435
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:149
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:147
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition pod.h:151
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:148
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:150
@ SPA_TYPE_Int
Definition type.h:34
@ SPA_TYPE_Rectangle
Definition type.h:40
@ SPA_TYPE_Long
Definition type.h:35
@ SPA_TYPE_Object
Definition type.h:45
@ SPA_TYPE_Struct
Definition type.h:44
#define SPA_UNUSED
Definition defs.h:307
#define spa_return_val_if_fail(expr, val)
Definition defs.h:431
#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 builder.h:32
Definition builder.h:53
uint32_t type
type of choice, one of enum spa_choice_type
Definition pod.h:155
Definition pod.h:162
struct spa_pod_choice_body body
Definition pod.h:164
struct spa_pod pod
Definition pod.h:163
Definition iter.h:27
uint32_t type
one of enum spa_type
Definition pod.h:178
uint32_t id
id of the object, depends on the object type
Definition pod.h:179
Definition pod.h:183
struct spa_pod_object_body body
Definition pod.h:185
Definition pod.h:208
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
uint32_t flags
flags for property
Definition pod.h:225
struct spa_pod value
Definition pod.h:226
Definition pod.h:167
Definition pod.h:43
uint32_t type
Definition pod.h:45
uint32_t size
Definition pod.h:44
Definition defs.h:116
uint32_t width
Definition defs.h:117
uint32_t height
Definition defs.h:118