Commit graph

5275 commits

Author SHA1 Message Date
Wim Taymans
a3da53f66e alsa: fix seq compilation 2025-07-30 18:54:16 +02:00
Wim Taymans
7f7b4be82d alsa: use the safer pod parser for control events 2025-07-30 18:43:34 +02:00
Wim Taymans
abcf70538d pod: add barrier around memcpy
We need to be sure that the compiler does not perform invented loads
after we checked the pod size. Otherwise we could have found that the
size was ok, only to be overwritten by an invalid size.

One way of avoiding this is to surround the memcpy with a barrier.

See #4822
2025-07-30 18:10:09 +02:00
Wim Taymans
6d07eaea1f seq: rework port handling
Dynamically allocate ports as we need them.
Use port lists to iterate active ports.
2025-07-30 17:32:02 +02:00
Wim Taymans
8f45cfcbc9 audiomixer: rework the port logic
Use port lists for faster and safer updates of the mixer ports.
2025-07-30 16:49:45 +02:00
Wim Taymans
77494086c1 pod: add support for vararg building and parsing of pod+body 2025-07-30 15:14:49 +02:00
Wim Taymans
f7ae61cb1e pod: remove checks from spa_pod_body_get_*()
The pattern is _is_foo() and then _get_foo(). This allows us to reuse
some of the get code in the parser without doing checks twice.
2025-07-30 12:56:39 +02:00
Wim Taymans
2c11f65701 pod: avoid child size check
This will give us 0 children later and is not a problem for this
function.
2025-07-30 11:02:00 +02:00
Pauli Virtanen
847982eb0e resample: keep fractional part of in_rate when interpolating
When interpolating with rate correction != 1.0, don't floor the
resulting input rate to the nearest smallest integer.

This allows rate corrections < 1/in_rate to have some effect, and
reduces jumps in the response. One of the jumps is inconveniently
between rate=1.0 and rate=1.0+eps and will cause rate corrections to
oscillate if target rate varies close to 1.0.
2025-07-30 07:59:52 +00:00
Pauli Virtanen
244d5a1cc1 resample: use fixed point for resample phase and input rate
If phase is float, calculations in impl_native_in_len/out_len can
produce wrong results due to rounding error.

It's probably better to not be in the business of predicting
floating-point rounding, so replace this by fixed-point arithmetic.

Also make sure `offset+1` cannot overflow data->filter array in
do_resample_inter* due to float multiplication possibly rounding up.
2025-07-30 07:59:52 +00:00
Pauli Virtanen
3cade43cf3 test-resample: add test for floating point rounding producing bad in_len
If phase is float, calculations in impl_native_in_len/out_len don't
necessarily match with do_resample, because e.g.

    float phase0 = 7999.99;
    float phase = phase0;
    int frac = 8000, out_rate = 8000, n = 64, count = 0;
    for (int j = 0; j < n; ++j) {
        phase += frac;
        if (phase >= out_rate) {
                phase -= out_rate;
                count++;
        }
    }
    printf("count = %d\n", count);      /* count = 64 */

    count = (int)(phase0 + n*frac) / out_rate;
    printf("count = %d\n", count);      /* count = 65 */

don't give the same result.

Also add test where floating point multiplication rounding up to nearest
in

    float ph = phase * pm;
    uint32_t offset = (uint32_t)floorf(ph);

computation results to offset+1 > data->n_phases, accessing filter array
beyond bounds.  (The accessed value is still inside allocated memory
block, but contains unrelated values; the test passes silently.)
2025-07-30 07:59:52 +00:00
Pauli Virtanen
84e8d59782 resample: fix off-by-one in out_len calculation
Fix off-by-one and add test.
2025-07-30 07:59:52 +00:00
Wim Taymans
0cab2fcb75 pod: don't unpack array values in get_values
This is only for choice, everything else stays the pod.
2025-07-29 17:01:47 +02:00
Wim Taymans
3785896533 mixer: rework the control mixers to use the parser
Using the parser for the spa_pod_sequence in the data buffers is
required in order to safely read the pods while there could be
concurrent writes.

See #4822
2025-07-29 15:33:43 +02:00
Wim Taymans
e317edcfb9 pod: rework the parser
Make a new body.h file with some functions to deal with pod and their
body. Make the iter.h functions use mostly this.

Rework the parser so that it only uses body.h functions. With the separation
of pod+body, we can read and verify the pod once and then use the
verified copy to handle the rest of the body safely.

We do this because iter.h only works in pods in memory that doesn't change
because it is vulnerable to modifications of the data after verifying it.

The new parser is not vulnerable to this and will not cause invalid
memory access when used on shared memory. There is however no need for
atomic operations to read the headers, whever is read is either valid
and useable of invalid and rejected.

See #4822
2025-07-29 15:15:02 +02:00
Wim Taymans
56e2d52b65 control: fix fastpath compilation 2025-07-27 17:17:55 +02:00
Wim Taymans
927ab0f4b8 control: improve port handling in control mixer
Use lists to manage the free, allocated and acitve mixer ports. We can
then avoid some checks and make things more threadsafe.
2025-07-27 16:51:52 +02:00
Wim Taymans
87333537d2 pod: also check 0 terminted strings in copy_string
Use get_string() to get a pointer to the string in the pod so that we
also check if it has a 0 terminator.

Fix the test case now that is_string returns true even for non
zero-terminated strings.
2025-07-25 17:33:46 +02:00
Wim Taymans
b991e9acc9 pod: check string zero byte only when parsing
The _is_type() macros should simply check the type in the header and if
the size is large enough to look into the type specifics. Further
validation of the values should be done when the value is retrieved.

Following this logic, the String zero byte check should be done in the
get_string() function.
2025-07-25 17:33:46 +02:00
Wim Taymans
0f6b365138 pod: don't call deref in reserve bytes
We already know that we could succesfully allocate enough space for the
bytes because we checked that before so simply move to the body of the
new bytes pod. We don't need to do the extensive checks we do in deref.
2025-07-24 18:35:21 +02:00
Wim Taymans
0c33101a42 pod: remove unused function 2025-07-24 18:22:14 +02:00
Wim Taymans
a85c24e9ca builder: support building pod + data + suffix
Add a function that can build a pod from a pod definition, a body data
and a suffix.

We can use this to build strings and bytes and arrays like the other
primitives, which makes it possible to add them to choices or arrays.

Fixes #4588
2025-07-24 16:23:54 +02:00
Demi Marie Obenour
a09bf57944 meson: Always use -fno-strict-aliasing and -fno-strict-overflow
SPA does not respect the C strict aliasing rules at all, so any code
that uses it must be built with -fno-strict-aliasing.  Furthermore,
there is code in SPA that compares pointers that point to different
objects, so -fno-strict-overflow is also needed.
2025-07-24 07:30:28 +00:00
Barnabás Pőcze
c7838cbbcb spa: node: io: fix typo in documentation
`SPA_IO_RATE_MATCH_ACTIVE` -> `SPA_IO_RATE_MATCH_FLAG_ACTIVE`
2025-07-23 21:26:29 +02:00
Pauli Virtanen
5fa137cc0d meson.build: make spa-json-dump available for subprojects
Add override that provides host binary for subprojects to use.

Also fix cross-compilation to use the host binary.
2025-07-23 12:19:21 +00:00
Wim Taymans
0b0912cc5b resample: optimize phase scaling
Precalculate the constant factor to avoid a division for each sample.
2025-07-23 14:11:11 +02:00
Wim Taymans
b52c490709 resample: fix compilation
Also fix a compiler warning in clang
2025-07-23 12:52:27 +02:00
Wim Taymans
d2a9141913 resample: avoid calculating GCD in rate updates
We don't actually need to calculate the GCD for each resampler rate
update. The GCD is only used to scale the in/out rates when using the
full resampler and this we can cache and reuse when we did the setup.

The interpolating resampler can work perfectly fine with a GCD of 1 and
so we can just assume that.
2025-07-23 12:23:20 +02:00
Wim Taymans
fcc49ad517 resample: reorder resample function setup
We also don't need to copy the resampler function name with each dynamic
function update, this is just for debugging.
2025-07-23 11:55:49 +02:00
Wim Taymans
685aed1de2 alsa: update resampler requested size before reading
spa_alsa_read is called from the source process function when we are a
follower and no buffer is ready yet.

Part of the rate correction was performed by the ALSA driver when it
woke up but now, the resampler has updated the requested size and we
need to requery it before we can start reading samples.

Otherwise, we end up with requested samples from before the rate update
and we might not give enough samples to the resampler. In that case, the
adapter will call us again and we will again try to produce a buffer
worth of the requested samples, which will xrun.
2025-07-22 16:48:43 +02:00
Niklas Carlsson
fc3a199ca2 filter-graph: fix index off by one in dsp_delay_c
Checking w + 1 > n_buffer means that w will go to n_buffer, which
in turn leads to reading buffer[2 * n_buffer].
2025-07-22 15:44:08 +02:00
Wim Taymans
5b436abef7 pod: improve compare function
Use the area to compare two rectangles. Use the width to break a tie.

This way the sorting is at least a bit more predictable and independent
of the order of the arguments.
2025-07-22 14:19:08 +02:00
Wim Taymans
495d6ba796 pod: remove some size checks
These are already done by the caller.
2025-07-22 14:09:10 +02:00
Wim Taymans
e91c541446 pod: disable padding when in body
Disable the padding to pod alignment for everything when we are building
the body of an array or choice.

This makes it possible to use bytes or strings or any other pod of a
fixed size as entries in arrays or choice.
2025-07-22 13:58:11 +02:00
Wim Taymans
b904cb14a9 pod: do size check before calling type/size/data functions
Assume that all the functions that take a type/size/data from a pod have
at least the right number of bytes in the data for the given type.
Callers need to ensure this.

Fix the callers of such functions to always make sure they deref a pod
type/size/body into something of at least the min size of the type.
2025-07-22 13:14:17 +02:00
Wim Taymans
0a52f959ac pod: add a function to return the min size of a type 2025-07-22 13:13:05 +02:00
Wim Taymans
a03bbc79fe pod: fix compilation 2025-07-22 12:20:05 +02:00
Demi Marie Obenour
05bd4547d0 pod: parser: avoid unneeded integer division
SPA_POD_CHOICE_N_VALUES involves an integer division, which is slow.
Replace it with subtraction and comparison.

No functional change intended.
2025-07-22 10:19:33 +00:00
Wim Taymans
da1d4fb30c pod: also check choice size before cast
Do a more thorough test of the choice type by not only checking the type
but also if the size is at least large enough to be able to cast it to
the pod_choice type and look at the contents.
2025-07-22 12:14:06 +02:00
Wim Taymans
ed7398a64a pod: use _deref and _frame in safe place
In the filter, don't _deref or use _frame before we are going to add
more pods to the builder. If we are using a dynamic builder, the
dereffed pod might become invalid when the memory is reallocated.

Instead, take the offset of the frame and deref later when we are not
going to add more things.
2025-07-22 10:06:07 +02:00
Wim Taymans
ff7fb675f1 pod: avoid overflow in _frame() 2025-07-22 09:43:50 +02:00
Karl Relton
9189b1d8b7 Add ACP for Logitech 407 USB PC Speaker set 2025-07-21 12:25:26 +00:00
Karl Relton
eac495f0e7 Option of analog or iec958 stereo output, both merging volume switches 2025-07-21 12:19:13 +00:00
Karl Relton
47e8984450 Upload New File 2025-07-21 12:17:40 +00:00
Wim Taymans
36f288f884 pod: require at least 1 choice value in _can_collect()
We can't collect the choice value when there is none so check for the
number of choice values in _can_collect() as well.
2025-07-21 10:14:39 +02:00
Wim Taymans
f2fb0b0aa5 alsa: update resample state in all cases
We need to manually recheck the resample state when the matching state
got updated.
2025-07-18 19:20:00 +02:00
Wim Taymans
5fba59b41d mixer-dsp: fix compilation with fastpath debug 2025-07-18 19:19:34 +02:00
Wim Taymans
ce2989891d alsa: init the bw_max value
Otherwise, rate matching doesn't work when it's left to 0.0.
2025-07-18 18:21:18 +02:00
Wim Taymans
5cd7b1de16 mixer-dsp: rework the port management
Keep a list of active ports in the port_list. These are all ports added
with add_port and not yet removed. When a port is removed, move it to
the free_list and reuse the port later when needed.

Update a mix_list of ports when a valid io is set on a port. This then
makes it possible to more efficiently and safely iterate the ports in
the processing loop.
2025-07-18 16:43:22 +02:00
Wim Taymans
ec5d2d2a29 audioconvert: rework the stage recalc a little
Use bits to capture the work that is needed. We clear the bit when
we added the stage, when all bits are cleared we have nothing more to
do. This avoids having to check multiple bookleans.

Make a helper function to calculate the destination buffer. When all
bits are cleared, we can use the output buffer.
2025-07-18 12:10:30 +02:00