+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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 |
| 20 | extern "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 | |
| 31 | /** |
| 32 | * \addtogroup spa_pod |
| 33 | * \{ |
| 34 | */ |
| 35 | |
| 36 | SPA_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); |
| 43 | case SPA_TYPE_Id: |
| 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); |
| 55 | case SPA_TYPE_Rectangle: |
| 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 | } |
| 68 | case SPA_TYPE_Fraction: |
| 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 | |
| 83 | SPA_API_POD_COMPARE int spa_pod_memcmp(const struct spa_pod *a, |
| 84 | const struct spa_pod *b) |
| 85 | { |
| 86 | return ((a == b) || (a && b && SPA_POD_SIZE(a) == SPA_POD_SIZE(b) && |
| 87 | memcmp(a, b, SPA_POD_SIZE(b)) == 0)) ? 0 : 1; |
| 88 | } |
| 89 | |
| 90 | SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, |
| 91 | const struct spa_pod *pod2) |
| 92 | { |
| 93 | int res = 0; |
| 94 | uint32_t n_vals1, n_vals2; |
| 95 | uint32_t choice1, choice2; |
| 96 | |
| 97 | spa_return_val_if_fail(pod1 != NULL, -EINVAL); |
| 98 | spa_return_val_if_fail(pod2 != NULL, -EINVAL); |
| 99 | |
| 100 | pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1); |
| 101 | pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2); |
| 102 | |
| 103 | if (n_vals1 != n_vals2) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | if (pod1->type != pod2->type) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | if (n_vals1 < 1) |
| 110 | return -EINVAL; /* empty choice */ |
| 111 | |
| 112 | switch (pod1->type) { |
| 113 | case SPA_TYPE_Struct: |
| 114 | { |
| 115 | const struct spa_pod *p1, *p2; |
| 116 | size_t p1s, p2s; |
| 117 | |
| 118 | p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1); |
| 119 | p1s = SPA_POD_BODY_SIZE(pod1); |
| 120 | p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2); |
| 121 | p2s = SPA_POD_BODY_SIZE(pod2); |
| 122 | |
| 123 | while (true) { |
| 124 | if (!spa_pod_is_inside(pod1, p1s, p1) || |
| 125 | !spa_pod_is_inside(pod2, p2s, p2)) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | if ((res = spa_pod_compare(p1, p2)) != 0) |
| 129 | return res; |
| 130 | |
| 131 | p1 = (const struct spa_pod*)spa_pod_next(p1); |
| 132 | p2 = (const struct spa_pod*)spa_pod_next(p2); |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | case SPA_TYPE_Object: |
| 137 | { |
| 138 | const struct spa_pod_prop *p1, *p2; |
| 139 | const struct spa_pod_object *o1, *o2; |
| 140 | |
| 141 | o1 = (const struct spa_pod_object*)pod1; |
| 142 | o2 = (const struct spa_pod_object*)pod2; |
| 143 | |
| 144 | p2 = NULL; |
| 145 | SPA_POD_OBJECT_FOREACH(o1, p1) { |
| 146 | if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL) |
| 147 | return 1; |
| 148 | if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0) |
| 149 | return res; |
| 150 | } |
| 151 | p1 = NULL; |
| 152 | SPA_POD_OBJECT_FOREACH(o2, p2) { |
| 153 | if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL) |
| 154 | return -1; |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | case SPA_TYPE_Array: |
| 159 | res = spa_pod_memcmp(pod1, pod2); |
| 160 | break; |
| 161 | default: |
| 162 | if (pod1->size != pod2->size) |
| 163 | return -EINVAL; |
| 164 | if (pod1->size < spa_pod_type_size(pod1->type)) |
| 165 | return -EINVAL; |
| 166 | res = spa_pod_compare_value(pod1->type, |
| 167 | SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), |
| 168 | pod1->size); |
| 169 | break; |
| 170 | } |
| 171 | return res; |
| 172 | } |
| 173 | |
| 174 | SPA_API_POD_COMPARE int spa_pod_compare_is_compatible_flags(uint32_t type, const void *r1, |
| 175 | const void *r2, uint32_t size SPA_UNUSED) |
| 176 | { |
| 177 | switch (type) { |
| 178 | case SPA_TYPE_Int: |
| 179 | return ((*(int32_t *) r1) & (*(int32_t *) r2)) != 0; |
| 180 | case SPA_TYPE_Long: |
| 181 | return ((*(int64_t *) r1) & (*(int64_t *) r2)) != 0; |
| 182 | default: |
| 183 | return -ENOTSUP; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | SPA_API_POD_COMPARE int spa_pod_compare_is_step_of(uint32_t type, const void *r1, |
| 190 | const void *r2, uint32_t size) |
| 191 | { |
| 192 | switch (type) { |
| 193 | case SPA_TYPE_Int: |
| 194 | if (*(int32_t *)r2 < 1) |
| 195 | return -EINVAL; |
| 196 | return *(int32_t *) r1 % *(int32_t *) r2 == 0; |
| 197 | case SPA_TYPE_Long: |
| 198 | if (*(int64_t *)r2 < 1) |
| 199 | return -EINVAL; |
| 200 | return *(int64_t *) r1 % *(int64_t *) r2 == 0; |
| 201 | case SPA_TYPE_Rectangle: |
| 202 | { |
| 203 | const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1, |
| 204 | *rec2 = (struct spa_rectangle *) r2; |
| 205 | |
| 206 | if (rec2->width < 1 || rec2->height < 1) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | return (rec1->width % rec2->width == 0 && |
| 210 | rec1->height % rec2->height == 0); |
| 211 | } |
| 212 | default: |
| 213 | return -ENOTSUP; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | SPA_API_POD_COMPARE int spa_pod_compare_is_in_range(uint32_t type, const void *v, |
| 219 | const void *min, const void *max, const void *step, uint32_t size SPA_UNUSED) |
| 220 | { |
| 221 | if (spa_pod_compare_value(type, v, min, size) < 0 || |
| 222 | spa_pod_compare_value(type, v, max, size) > 0) |
| 223 | return 0; |
| 224 | if (step != NULL) |
| 225 | return spa_pod_compare_is_step_of(type, v, step, size); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | SPA_API_POD_COMPARE int spa_pod_compare_is_valid_choice(uint32_t type, uint32_t size, |
| 230 | const void *val, const void *vals, uint32_t n_vals, uint32_t choice) |
| 231 | { |
| 232 | switch (choice) { |
| 233 | case SPA_CHOICE_None: |
| 234 | if (spa_pod_compare_value(type, val, vals, size) == 0) |
| 235 | return 1; |
| 236 | return 0; |
| 237 | case SPA_CHOICE_Enum: |
| 238 | { |
| 239 | const void *next = vals; |
| 240 | for (uint32_t i = 1; i < n_vals; i++) { |
| 241 | next = SPA_PTROFF(next, size, void); |
| 242 | if (spa_pod_compare_value(type, val, next, size) == 0) |
| 243 | return 1; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | case SPA_CHOICE_Range: |
| 248 | case SPA_CHOICE_Step: |
| 249 | { |
| 250 | void *min = SPA_PTROFF(vals,size,void); |
| 251 | void *max = SPA_PTROFF(min,size,void); |
| 252 | void *step = choice == SPA_CHOICE_Step ? SPA_PTROFF(max,size,void) : NULL; |
| 253 | return spa_pod_compare_is_in_range(type, val, min, max, step, size); |
| 254 | } |
| 255 | case SPA_CHOICE_Flags: |
| 256 | return 1; |
| 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * \} |
| 263 | */ |
| 264 | |
| 265 | #ifdef __cplusplus |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | #endif |