Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 35531929 authored by Karthikeyan Mani's avatar Karthikeyan Mani Committed by Gerrit - the friendly Code Review server
Browse files

hal: Check input buffer size as multiple of channel

Update get input buffer size logic for record use cases
such that the size is also a multiple of bytes per sample
period. For 6 channel 32 bit recording the computed
value was not a multiple of bytes per sample and this
mismatches with the computed value by ALSA causing
buffer invalid errors because the use case pointers
difference was reaching stop threshold.

Change-Id: Ibca14baf26334888819cd15db39626b63c14f626
parent 3751e3e2
Loading
Loading
Loading
Loading
+39 −3
Original line number Diff line number Diff line
@@ -3424,12 +3424,47 @@ static void register_sample_rate(uint32_t sample_rate,
             "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
}

static inline uint32_t lcm(uint32_t num1, uint32_t num2)
{
    uint32_t high = num1, low = num2, temp = 0;

    if (!num1 || !num2)
        return 0;

    if (num1 < num2) {
         high = num2;
         low = num1;
    }

    while (low != 0) {
        temp = low;
        low = high % low;
        high = temp;
    }
    return (num1 * num2)/high;
}

static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
{
    uint32_t remainder = 0;

    if (!multiplier)
        return num;

    remainder = num % multiplier;
    if (remainder)
        num += (multiplier - remainder);

    return num;
}

static size_t get_input_buffer_size(uint32_t sample_rate,
                                    audio_format_t format,
                                    int channel_count,
                                    bool is_low_latency)
{
    size_t size = 0;
    uint32_t bytes_per_period_sample = 0;

    if (check_input_parameters(sample_rate, format, channel_count) != 0)
        return 0;
@@ -3438,15 +3473,16 @@ static size_t get_input_buffer_size(uint32_t sample_rate,
    if (is_low_latency)
        size = configured_low_latency_capture_period_size;

    size *= audio_bytes_per_sample(format) * channel_count;
    bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
    size *= bytes_per_period_sample;

    /* make sure the size is multiple of 32 bytes
     * At 48 kHz mono 16-bit PCM:
     *  5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
     *  3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
     * Also, make sure the size is multiple of bytes per period sample
     */
    size += 0x1f;
    size &= ~0x1f;
    size = nearest_multiple(size, lcm(32, bytes_per_period_sample));

    return size;
}