Loading...
Searching...
No Matches
filter.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_POD_FILTER_H
#define SPA_POD_FILTER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <spa/param/props.h>
#include <spa/pod/iter.h>
#include <spa/pod/builder.h>
#include <spa/pod/compare.h>
static inline int spa_pod_choice_fix_default(struct spa_pod_choice *choice)
{
void *val, *alt;
int i, nvals;
uint32_t type, size;
nvals = SPA_POD_CHOICE_N_VALUES(choice);
type = SPA_POD_CHOICE_VALUE_TYPE(choice);
size = SPA_POD_CHOICE_VALUE_SIZE(choice);
alt = val = SPA_POD_CHOICE_VALUES(choice);
switch (choice->body.type) {
case SPA_CHOICE_None:
break;
case SPA_CHOICE_Range:
case SPA_CHOICE_Step:
if (nvals > 1) {
alt = SPA_PTROFF(alt, size, void);
if (spa_pod_compare_value(type, val, alt, size) < 0)
memcpy(val, alt, size);
}
if (nvals > 2) {
alt = SPA_PTROFF(alt, size, void);
if (spa_pod_compare_value(type, val, alt, size) > 0)
memcpy(val, alt, size);
}
break;
case SPA_CHOICE_Flags:
case SPA_CHOICE_Enum:
{
void *best = NULL;
for (i = 1; i < nvals; i++) {
alt = SPA_PTROFF(alt, size, void);
if (spa_pod_compare_value(type, val, alt, size) == 0) {
best = alt;
break;
}
if (best == NULL)
best = alt;
}
if (best)
memcpy(val, best, size);
if (nvals <= 1)
choice->body.type = SPA_CHOICE_None;
break;
}
}
return 0;
}
static inline int spa_pod_filter_flags_value(struct spa_pod_builder *b,
uint32_t type, const void *r1, const void *r2, uint32_t size SPA_UNUSED)
{
switch (type) {
case SPA_TYPE_Int:
{
int32_t val = (*(int32_t *) r1) & (*(int32_t *) r2);
if (val == 0)
return 0;
spa_pod_builder_int(b, val);
break;
}
case SPA_TYPE_Long:
{
int64_t val = (*(int64_t *) r1) & (*(int64_t *) r2);
if (val == 0)
return 0;
spa_pod_builder_long(b, val);
break;
}
default:
return -ENOTSUP;
}
return 1;
}
static inline int spa_pod_filter_is_step_of(uint32_t type, const void *r1,
const void *r2, uint32_t size SPA_UNUSED)
{
switch (type) {
case SPA_TYPE_Int:
return *(int32_t *) r1 % *(int32_t *) r2 == 0;
case SPA_TYPE_Long:
return *(int64_t *) r1 % *(int64_t *) r2 == 0;
case SPA_TYPE_Rectangle:
{
const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
*rec2 = (struct spa_rectangle *) r2;
return (rec1->width % rec2->width == 0 &&
rec1->height % rec2->height == 0);
}
default:
return -ENOTSUP;
}
return 0;
}
static inline int
spa_pod_filter_prop(struct spa_pod_builder *b,
const struct spa_pod_prop *p1,
const struct spa_pod_prop *p2)
{
const struct spa_pod *v1, *v2;
struct spa_pod_choice *nc;
uint32_t j, k, nalt1, nalt2;
void *alt1, *alt2, *a1, *a2;
uint32_t type, size, p1c, p2c;
struct spa_pod_frame f;
v1 = spa_pod_get_values(&p1->value, &nalt1, &p1c);
alt1 = SPA_POD_BODY(v1);
v2 = spa_pod_get_values(&p2->value, &nalt2, &p2c);
alt2 = SPA_POD_BODY(v2);
type = v1->type;
size = v1->size;
/* incompatible property types */
if (type != v2->type || size != v2->size || p1->key != p2->key)
return -EINVAL;
if (p1c == SPA_CHOICE_None || p1c == SPA_CHOICE_Flags) {
nalt1 = 1;
} else {
alt1 = SPA_PTROFF(alt1, size, void);
nalt1--;
}
if (p2c == SPA_CHOICE_None || p2c == SPA_CHOICE_Flags) {
nalt2 = 1;
} else {
alt2 = SPA_PTROFF(alt2, size, void);
nalt2--;
}
/* start with copying the property */
spa_pod_builder_prop(b, p1->key, p1->flags & p2->flags);
spa_pod_builder_push_choice(b, &f, 0, 0);
nc = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f);
/* default value */
spa_pod_builder_primitive(b, v1);
if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_None) ||
(p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Enum) ||
(p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_None) ||
(p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Enum)) {
int n_copied = 0;
/* copy all equal values but don't copy the default value again */
for (j = 0, a1 = alt1; j < nalt1; j++, a1 = SPA_PTROFF(a1, size, void)) {
for (k = 0, a2 = alt2; k < nalt2; k++, a2 = SPA_PTROFF(a2,size,void)) {
if (spa_pod_compare_value(type, a1, a2, size) == 0) {
if (p1c == SPA_CHOICE_Enum || j > 0)
spa_pod_builder_raw(b, a1, size);
n_copied++;
}
}
}
if (n_copied == 0)
return -EINVAL;
nc->body.type = SPA_CHOICE_Enum;
}
if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Range) ||
(p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Range)) {
int n_copied = 0;
/* copy all values inside the range */
for (j = 0, a1 = alt1, a2 = alt2; j < nalt1; j++, a1 = SPA_PTROFF(a1,size,void)) {
if (spa_pod_compare_value(type, a1, a2, size) < 0)
continue;
if (spa_pod_compare_value(type, a1, SPA_PTROFF(a2,size,void), size) > 0)
continue;
spa_pod_builder_raw(b, a1, size);
n_copied++;
}
if (n_copied == 0)
return -EINVAL;
nc->body.type = SPA_CHOICE_Enum;
}
if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Step) ||
(p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Step)) {
int n_copied = 0;
for (j = 0, a1 = alt1, a2 = alt2; j < nalt1; j++, a1 = SPA_PTROFF(a1,size,void)) {
int res;
if (spa_pod_compare_value(type, a1, a2, size) < 0)
continue;
if (spa_pod_compare_value(type, a1, SPA_PTROFF(a2,size,void), size) > 0)
continue;
res = spa_pod_filter_is_step_of(type, a1, SPA_PTROFF(a2,size*2,void), size);
if (res == 0)
continue;
if (res == -ENOTSUP)
return -EINVAL;
spa_pod_builder_raw(b, a1, size);
n_copied++;
}
if (n_copied == 0)
return -EINVAL;
nc->body.type = SPA_CHOICE_Enum;
}
if ((p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_None) ||
(p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Enum)) {
int n_copied = 0;
/* copy all values inside the range */
for (k = 0, a1 = alt1, a2 = alt2; k < nalt2; k++, a2 = SPA_PTROFF(a2,size,void)) {
if (spa_pod_compare_value(type, a2, a1, size) < 0)
continue;
if (spa_pod_compare_value(type, a2, SPA_PTROFF(a1,size,void), size) > 0)
continue;
spa_pod_builder_raw(b, a2, size);
n_copied++;
}
if (n_copied == 0)
return -EINVAL;
nc->body.type = SPA_CHOICE_Enum;
}
if ((p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Range) ||
(p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Step) ||
(p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Range) ||
(p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Step)) {
if (spa_pod_compare_value(type, alt1, alt2, size) < 0)
spa_pod_builder_raw(b, alt2, size);
else
spa_pod_builder_raw(b, alt1, size);
alt1 = SPA_PTROFF(alt1,size,void);
alt2 = SPA_PTROFF(alt2,size,void);
if (spa_pod_compare_value(type, alt1, alt2, size) < 0)
spa_pod_builder_raw(b, alt1, size);
else
spa_pod_builder_raw(b, alt2, size);
nc->body.type = SPA_CHOICE_Range;
}
if ((p1c == SPA_CHOICE_None && p2c == SPA_CHOICE_Flags) ||
(p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_None) ||
(p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Flags)) {
if (spa_pod_filter_flags_value(b, type, alt1, alt2, size) != 1)
return -EINVAL;
nc->body.type = SPA_CHOICE_Flags;
}
if (p1c == SPA_CHOICE_Range && p2c == SPA_CHOICE_Flags)
return -ENOTSUP;
if (p1c == SPA_CHOICE_Enum && p2c == SPA_CHOICE_Flags)
return -ENOTSUP;
if ((p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_None) ||
(p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Enum)) {
int n_copied = 0;
for (j = 0, a1 = alt1, a2 = alt2; j < nalt2; j++, a2 = SPA_PTROFF(a1,size,void)) {
int res;
if (spa_pod_compare_value(type, a2, a1, size) < 0)
continue;
if (spa_pod_compare_value(type, a2, SPA_PTROFF(a1,size,void), size) > 0)
continue;
res = spa_pod_filter_is_step_of(type, a2, SPA_PTROFF(a1,size*2,void), size);
if (res == 0)
continue;
if (res == -ENOTSUP)
return -EINVAL;
spa_pod_builder_raw(b, a2, size);
n_copied++;
}
if (n_copied == 0)
return -EINVAL;
nc->body.type = SPA_CHOICE_Enum;
}
if (p1c == SPA_CHOICE_Step && p2c == SPA_CHOICE_Flags)
return -ENOTSUP;
if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Range)
return -ENOTSUP;
if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Step)
return -ENOTSUP;
if (p1c == SPA_CHOICE_Flags && p2c == SPA_CHOICE_Enum)
return -ENOTSUP;
spa_pod_builder_pop(b, &f);
spa_pod_choice_fix_default(nc);
return 0;
}
static inline 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)
{
const struct spa_pod *pp, *pf;
int res = 0;
pf = filter;
SPA_POD_FOREACH(pod, pod_size, pp) {
bool do_copy = false, do_advance = false;
uint32_t filter_offset = 0;
struct spa_pod_frame f;
switch (SPA_POD_TYPE(pp)) {
case SPA_TYPE_Object:
if (pf != NULL) {
struct spa_pod_object *op = (struct spa_pod_object *) pp;
struct spa_pod_object *of = (struct spa_pod_object *) pf;
const struct spa_pod_prop *p1, *p2;
if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp))
return -EINVAL;
spa_pod_builder_push_object(b, &f, op->body.type, op->body.id);
p2 = NULL;
SPA_POD_OBJECT_FOREACH(op, p1) {
p2 = spa_pod_object_find_prop(of, p2, p1->key);
if (p2 != NULL)
res = spa_pod_filter_prop(b, p1, p2);
else if ((p1->flags & SPA_POD_PROP_FLAG_MANDATORY) != 0)
res = -EINVAL;
else
spa_pod_builder_raw_padded(b, p1, SPA_POD_PROP_SIZE(p1));
if (res < 0)
break;
}
if (res >= 0) {
p1 = NULL;
SPA_POD_OBJECT_FOREACH(of, p2) {
p1 = spa_pod_object_find_prop(op, p1, p2->key);
if (p1 != NULL)
continue;
if ((p2->flags & SPA_POD_PROP_FLAG_MANDATORY) != 0)
res = -EINVAL;
if (res < 0)
break;
spa_pod_builder_raw_padded(b, p2, SPA_POD_PROP_SIZE(p2));
}
}
spa_pod_builder_pop(b, &f);
do_advance = true;
}
else
do_copy = true;
break;
case SPA_TYPE_Struct:
if (pf != NULL) {
if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp))
return -EINVAL;
filter_offset = sizeof(struct spa_pod_struct);
spa_pod_builder_push_struct(b, &f);
res = spa_pod_filter_part(b,
SPA_PTROFF(pp,filter_offset,const struct spa_pod),
SPA_POD_SIZE(pp) - filter_offset,
SPA_PTROFF(pf,filter_offset,const struct spa_pod),
SPA_POD_SIZE(pf) - filter_offset);
spa_pod_builder_pop(b, &f);
do_advance = true;
}
else
do_copy = true;
break;
default:
if (pf != NULL) {
if (SPA_POD_SIZE(pp) != SPA_POD_SIZE(pf))
return -EINVAL;
if (memcmp(pp, pf, SPA_POD_SIZE(pp)) != 0)
return -EINVAL;
do_advance = true;
}
do_copy = true;
break;
}
if (do_copy)
spa_pod_builder_raw_padded(b, pp, SPA_POD_SIZE(pp));
if (do_advance) {
pf = (const struct spa_pod*)spa_pod_next(pf);
if (!spa_pod_is_inside(filter, filter_size, pf))
pf = NULL;
}
if (res < 0)
break;
}
return res;
}
static inline int
spa_pod_filter(struct spa_pod_builder *b,
struct spa_pod **result,
const struct spa_pod *pod,
const struct spa_pod *filter)
{
int res;
struct spa_pod_builder_state state;
spa_return_val_if_fail(pod != NULL, -EINVAL);
spa_return_val_if_fail(b != NULL, -EINVAL);
spa_pod_builder_get_state(b, &state);
if (filter == NULL)
res = spa_pod_builder_raw_padded(b, pod, SPA_POD_SIZE(pod));
else
res = spa_pod_filter_part(b, pod, SPA_POD_SIZE(pod), filter, SPA_POD_SIZE(filter));
if (res < 0) {
spa_pod_builder_reset(b, &state);
} else if (result) {
*result = (struct spa_pod*)spa_pod_builder_deref(b, state.offset);
if (*result == NULL)
res = -ENOSPC;
}
return res;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_POD_FILTER_H */
builder.h
spa/pod/builder.h
compare.h
spa/pod/compare.h
spa_pod_filter_is_step_of
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
spa_pod_builder_prop
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:450
SPA_POD_CHOICE_VALUE_TYPE
#define SPA_POD_CHOICE_VALUE_TYPE(choice)
Definition pod.h:138
spa_pod_object_find_prop
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:388
spa_pod_compare_value
static int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:33
SPA_POD_PROP_FLAG_MANDATORY
#define SPA_POD_PROP_FLAG_MANDATORY
is mandatory
Definition pod.h:222
SPA_POD_CHOICE_VALUE_SIZE
#define SPA_POD_CHOICE_VALUE_SIZE(choice)
Definition pod.h:140
spa_pod_next
static void * spa_pod_next(const void *iter)
Definition iter.h:40
SPA_POD_OBJECT_FOREACH
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:108
SPA_POD_BODY
#define SPA_POD_BODY(pod)
Definition pod.h:39
spa_pod_builder_frame
static struct spa_pod * spa_pod_builder_frame(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:105
spa_pod_builder_reset
static void spa_pod_builder_reset(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:78
SPA_POD_TYPE
#define SPA_POD_TYPE(pod)
Definition pod.h:28
spa_pod_filter_flags_value
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
spa_pod_filter_part
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:330
spa_pod_choice_fix_default
static int spa_pod_choice_fix_default(struct spa_pod_choice *choice)
Definition filter.h:33
spa_pod_builder_raw_padded
static int spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:160
spa_pod_filter_prop
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
spa_pod_builder_pop
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:168
spa_pod_builder_primitive
static int spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
Definition builder.h:186
spa_pod_is_inside
static bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:34
spa_pod_builder_push_struct
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:422
spa_pod_builder_get_state
static void spa_pod_builder_get_state(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:65
SPA_POD_FOREACH
#define SPA_POD_FOREACH(pod, size, iter)
Definition iter.h:95
SPA_POD_CHOICE_VALUES
#define SPA_POD_CHOICE_VALUES(choice)
Definition pod.h:144
SPA_POD_PROP_SIZE
#define SPA_POD_PROP_SIZE(prop)
Definition pod.h:205
spa_pod_get_values
static struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:347
spa_pod_builder_raw
static int spa_pod_builder_raw(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:128
spa_pod_builder_push_choice
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
spa_pod_builder_int
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:247
spa_pod_builder_push_object
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
spa_pod_builder_long
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:256
SPA_POD_CHOICE_N_VALUES
#define SPA_POD_CHOICE_N_VALUES(choice)
Definition pod.h:142
SPA_POD_SIZE
#define SPA_POD_SIZE(pod)
Definition pod.h:30
spa_pod_builder_deref
static struct spa_pod * spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t offset)
Definition builder.h:93
spa_pod_filter
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:431
SPA_CHOICE_Step
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:149
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:147
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: default, possible flags,...
Definition pod.h:151
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:148
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:150
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition type.h:34
SPA_TYPE_Rectangle
@ SPA_TYPE_Rectangle
Definition type.h:40
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:35
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition type.h:45
SPA_TYPE_Struct
@ SPA_TYPE_Struct
Definition type.h:44
SPA_UNUSED
#define SPA_UNUSED
Definition defs.h:289
spa_return_val_if_fail
#define spa_return_val_if_fail(expr, val)
Definition defs.h:369
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:198
iter.h
spa/pod/iter.h
props.h
string.h
spa/utils/string.h
spa_pod_builder_state
Definition builder.h:32
spa_pod_builder
Definition builder.h:53
spa_pod_choice_body::type
uint32_t type
type of choice, one of enum spa_choice_type
Definition pod.h:155
spa_pod_choice
Definition pod.h:162
spa_pod_choice::body
struct spa_pod_choice_body body
Definition pod.h:164
spa_pod_choice::pod
struct spa_pod pod
Definition pod.h:163
spa_pod_frame
Definition iter.h:27
spa_pod_object_body::type
uint32_t type
one of enum spa_type
Definition pod.h:178
spa_pod_object_body::id
uint32_t id
id of the object, depends on the object type
Definition pod.h:179
spa_pod_object
Definition pod.h:183
spa_pod_object::body
struct spa_pod_object_body body
Definition pod.h:185
spa_pod_prop
Definition pod.h:208
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
spa_pod_prop::flags
uint32_t flags
flags for property
Definition pod.h:225
spa_pod_prop::value
struct spa_pod value
Definition pod.h:226
spa_pod_struct
Definition pod.h:167
spa_pod
Definition pod.h:43
spa_pod::type
uint32_t type
Definition pod.h:45
spa_pod::size
uint32_t size
Definition pod.h:44
spa_rectangle
Definition defs.h:102
spa_rectangle::width
uint32_t width
Definition defs.h:103
spa_rectangle::height
uint32_t height
Definition defs.h:104