-/*
- * Convolution filter coefficients for the outer QMF of the QMF tree.
- * The 2 sets are a mirror of each other.
- */
-static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS] = {
- {
- 730, -413, -9611, 43626, -121026, 269973, -585547, 2801966,
- 697128, -160481, 27611, 8478, -10043, 3511, 688, -897,
- },
- {
- -897, 688, 3511, -10043, 8478, 27611, -160481, 697128,
- 2801966, -585547, 269973, -121026, 43626, -9611, -413, 730,
- },
-};
-
-/*
- * Convolution filter coefficients for the inner QMF of the QMF tree.
- * The 2 sets are a mirror of each other.
- */
-static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS] = {
- {
- 1033, -584, -13592, 61697, -171156, 381799, -828088, 3962579,
- 985888, -226954, 39048, 11990, -14203, 4966, 973, -1268,
- },
- {
- -1268, 973, 4966, -14203, 11990, 39048, -226954, 985888,
- 3962579, -828088, 381799, -171156, 61697, -13592, -584, 1033,
- },
-};
-
-/*
- * Push one sample into a circular signal buffer.
- */
-av_always_inline
-static void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
-{
- signal->buffer[signal->pos ] = sample;
- signal->buffer[signal->pos+FILTER_TAPS] = sample;
- signal->pos = (signal->pos + 1) & (FILTER_TAPS - 1);
-}
-
-/*
- * Compute the convolution of the signal with the coefficients, and reduce
- * to 24 bits by applying the specified right shifting.
- */
-av_always_inline
-static int32_t aptx_qmf_convolution(FilterSignal *signal,
- const int32_t coeffs[FILTER_TAPS],
- int shift)
-{
- int32_t *sig = &signal->buffer[signal->pos];
- int64_t e = 0;
- int i;
-
- for (i = 0; i < FILTER_TAPS; i++)
- e += MUL64(sig[i], coeffs[i]);
-
- return rshift64_clip24(e, shift);
-}
-
-/*
- * Half-band QMF analysis filter realized with a polyphase FIR filter.
- * Split into 2 subbands and downsample by 2.
- * So for each pair of samples that goes in, one sample goes out,
- * split into 2 separate subbands.
- */
-av_always_inline
-static void aptx_qmf_polyphase_analysis(FilterSignal signal[NB_FILTERS],
- const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
- int shift,
- int32_t samples[NB_FILTERS],
- int32_t *low_subband_output,
- int32_t *high_subband_output)
-{
- int32_t subbands[NB_FILTERS];
- int i;
-
- for (i = 0; i < NB_FILTERS; i++) {
- aptx_qmf_filter_signal_push(&signal[i], samples[NB_FILTERS-1-i]);
- subbands[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
- }
-
- *low_subband_output = av_clip_intp2(subbands[0] + subbands[1], 23);
- *high_subband_output = av_clip_intp2(subbands[0] - subbands[1], 23);
-}
-
-/*
- * Two stage QMF analysis tree.
- * Split 4 input samples into 4 subbands and downsample by 4.
- * So for each group of 4 samples that goes in, one sample goes out,
- * split into 4 separate subbands.
- */
-static void aptx_qmf_tree_analysis(QMFAnalysis *qmf,
- int32_t samples[4],
- int32_t subband_samples[4])
-{
- int32_t intermediate_samples[4];
- int i;
-
- /* Split 4 input samples into 2 intermediate subbands downsampled to 2 samples */
- for (i = 0; i < 2; i++)
- aptx_qmf_polyphase_analysis(qmf->outer_filter_signal,
- aptx_qmf_outer_coeffs, 23,
- &samples[2*i],
- &intermediate_samples[0+i],
- &intermediate_samples[2+i]);
-
- /* Split 2 intermediate subband samples into 4 final subbands downsampled to 1 sample */
- for (i = 0; i < 2; i++)
- aptx_qmf_polyphase_analysis(qmf->inner_filter_signal[i],
- aptx_qmf_inner_coeffs, 23,
- &intermediate_samples[2*i],
- &subband_samples[2*i+0],
- &subband_samples[2*i+1]);
-}
-
-/*
- * Half-band QMF synthesis filter realized with a polyphase FIR filter.
- * Join 2 subbands and upsample by 2.
- * So for each 2 subbands sample that goes in, a pair of samples goes out.
- */
-av_always_inline
-static void aptx_qmf_polyphase_synthesis(FilterSignal signal[NB_FILTERS],
- const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
- int shift,
- int32_t low_subband_input,
- int32_t high_subband_input,
- int32_t samples[NB_FILTERS])
-{
- int32_t subbands[NB_FILTERS];
- int i;
-
- subbands[0] = low_subband_input + high_subband_input;
- subbands[1] = low_subband_input - high_subband_input;
-
- for (i = 0; i < NB_FILTERS; i++) {
- aptx_qmf_filter_signal_push(&signal[i], subbands[1-i]);
- samples[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
- }
-}
-
-/*
- * Two stage QMF synthesis tree.
- * Join 4 subbands and upsample by 4.
- * So for each 4 subbands sample that goes in, a group of 4 samples goes out.
- */
-static void aptx_qmf_tree_synthesis(QMFAnalysis *qmf,
- int32_t subband_samples[4],
- int32_t samples[4])
-{
- int32_t intermediate_samples[4];
- int i;
-
- /* Join 4 subbands into 2 intermediate subbands upsampled to 2 samples. */
- for (i = 0; i < 2; i++)
- aptx_qmf_polyphase_synthesis(qmf->inner_filter_signal[i],
- aptx_qmf_inner_coeffs, 22,
- subband_samples[2*i+0],
- subband_samples[2*i+1],
- &intermediate_samples[2*i]);
-
- /* Join 2 samples from intermediate subbands upsampled to 4 samples. */
- for (i = 0; i < 2; i++)
- aptx_qmf_polyphase_synthesis(qmf->outer_filter_signal,
- aptx_qmf_outer_coeffs, 21,
- intermediate_samples[0+i],
- intermediate_samples[2+i],
- &samples[2*i]);
-}
-
-
-av_always_inline
-static int32_t aptx_bin_search(int32_t value, int32_t factor,
- const int32_t *intervals, int32_t nb_intervals)
-{
- int32_t idx = 0;
- int i;
-
- for (i = nb_intervals >> 1; i > 0; i >>= 1)
- if (MUL64(factor, intervals[idx + i]) <= ((int64_t)value << 24))
- idx += i;
-
- return idx;
-}
-
-static void aptx_quantize_difference(Quantize *quantize,
- int32_t sample_difference,
- int32_t dither,
- int32_t quantization_factor,
- ConstTables *tables)
-{
- const int32_t *intervals = tables->quantize_intervals;
- int32_t quantized_sample, dithered_sample, parity_change;
- int32_t d, mean, interval, inv, sample_difference_abs;
- int64_t error;
-
- sample_difference_abs = FFABS(sample_difference);
- sample_difference_abs = FFMIN(sample_difference_abs, (1 << 23) - 1);
-
- quantized_sample = aptx_bin_search(sample_difference_abs >> 4,
- quantization_factor,
- intervals, tables->tables_size);
-
- d = rshift32_clip24(MULH(dither, dither), 7) - (1 << 23);
- d = rshift64(MUL64(d, tables->quantize_dither_factors[quantized_sample]), 23);
-
- intervals += quantized_sample;
- mean = (intervals[1] + intervals[0]) / 2;
- interval = (intervals[1] - intervals[0]) * (-(sample_difference < 0) | 1);
-
- dithered_sample = rshift64_clip24(MUL64(dither, interval) + ((int64_t)av_clip_intp2(mean + d, 23) << 32), 32);
- error = ((int64_t)sample_difference_abs << 20) - MUL64(dithered_sample, quantization_factor);
- quantize->error = FFABS(rshift64(error, 23));
-
- parity_change = quantized_sample;
- if (error < 0)
- quantized_sample--;
- else
- parity_change--;
-
- inv = -(sample_difference < 0);
- quantize->quantized_sample = quantized_sample ^ inv;
- quantize->quantized_sample_parity_change = parity_change ^ inv;
-}
-
-static void aptx_encode_channel(Channel *channel, int32_t samples[4], int hd)
-{
- int32_t subband_samples[4];
- int subband;
- aptx_qmf_tree_analysis(&channel->qmf, samples, subband_samples);
- aptx_generate_dither(channel);
- for (subband = 0; subband < NB_SUBBANDS; subband++) {
- int32_t diff = av_clip_intp2(subband_samples[subband] - channel->prediction[subband].predicted_sample, 23);
- aptx_quantize_difference(&channel->quantize[subband], diff,
- channel->dither[subband],
- channel->invert_quantize[subband].quantization_factor,
- &tables[hd][subband]);
- }
-}
-
-static void aptx_decode_channel(Channel *channel, int32_t samples[4])
-{
- int32_t subband_samples[4];
- int subband;
- for (subband = 0; subband < NB_SUBBANDS; subband++)
- subband_samples[subband] = channel->prediction[subband].previous_reconstructed_sample;
- aptx_qmf_tree_synthesis(&channel->qmf, subband_samples, samples);
-}
-
-