FFmpeg
Loading...
Searching...
No Matches
aptx.h
Go to the documentation of this file.
1/*
2 * Audio Processing Technology codec for Bluetooth (aptX)
3 *
4 * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef AVCODEC_APTX_H
24#define AVCODEC_APTX_H
25
27#include "avcodec.h"
28#include "mathops.h"
29
30
36
38 LF, // Low Frequency (0-5.5 kHz)
39 MLF, // Medium-Low Frequency (5.5-11kHz)
40 MHF, // Medium-High Frequency (11-16.5kHz)
41 HF, // High Frequency (16.5-22kHz)
43};
44
45#define NB_FILTERS 2
46#define FILTER_TAPS 16
47
48typedef struct {
49 int pos;
52
57
63
69
80
91
98
108
110
111/* Rounded right shift with optional clipping */
112#define RSHIFT_SIZE(size) \
113av_always_inline \
114static int##size##_t rshift##size(int##size##_t value, int shift) \
115{ \
116 int##size##_t rounding = (int##size##_t)1 << (shift - 1); \
117 int##size##_t mask = ((int##size##_t)1 << (shift + 1)) - 1; \
118 return ((value + rounding) >> shift) - ((value & mask) == rounding); \
119} \
120av_always_inline \
121static int##size##_t rshift##size##_clip24(int##size##_t value, int shift) \
122{ \
123 return av_clip_intp2(rshift##size(value, shift), 23); \
124}
125RSHIFT_SIZE(32)
126RSHIFT_SIZE(64)
127
128/*
129 * Convolution filter coefficients for the outer QMF of the QMF tree.
130 * The 2 sets are a mirror of each other.
131 */
133 {
134 730, -413, -9611, 43626, -121026, 269973, -585547, 2801966,
135 697128, -160481, 27611, 8478, -10043, 3511, 688, -897,
136 },
137 {
138 -897, 688, 3511, -10043, 8478, 27611, -160481, 697128,
139 2801966, -585547, 269973, -121026, 43626, -9611, -413, 730,
140 },
141};
142
143/*
144 * Convolution filter coefficients for the inner QMF of the QMF tree.
145 * The 2 sets are a mirror of each other.
146 */
148 {
149 1033, -584, -13592, 61697, -171156, 381799, -828088, 3962579,
150 985888, -226954, 39048, 11990, -14203, 4966, 973, -1268,
151 },
152 {
153 -1268, 973, 4966, -14203, 11990, 39048, -226954, 985888,
154 3962579, -828088, 381799, -171156, 61697, -13592, -584, 1033,
155 },
156};
157
158/*
159 * Push one sample into a circular signal buffer.
160 */
163{
164 signal->buffer[signal->pos ] = sample;
165 signal->buffer[signal->pos+FILTER_TAPS] = sample;
166 signal->pos = (signal->pos + 1) & (FILTER_TAPS - 1);
167}
168
169/*
170 * Compute the convolution of the signal with the coefficients, and reduce
171 * to 24 bits by applying the specified right shifting.
172 */
175 const int32_t coeffs[FILTER_TAPS],
176 int shift)
177{
178 int32_t *sig = &signal->buffer[signal->pos];
179 int64_t e = 0;
180 int i;
181
182 for (i = 0; i < FILTER_TAPS; i++)
183 e += MUL64(sig[i], coeffs[i]);
184
185 return rshift64_clip24(e, shift);
186}
187
189{
190 int32_t parity = channel->dither_parity;
191 int subband;
192
193 for (subband = 0; subband < NB_SUBBANDS; subband++)
194 parity ^= channel->quantize[subband].quantized_sample;
195
196 return parity & 1;
197}
198
199/* For each sample, ensure that the parity of all subbands of all channels
200 * is 0 except once every 8 samples where the parity is forced to 1. */
202{
205
206 int eighth = *idx == 7;
207 *idx = (*idx + 1) & 7;
208
209 return parity ^ eighth;
210}
211
214
215int ff_aptx_init(AVCodecContext *avctx);
216
217#endif /* AVCODEC_APTX_H */
ConstTables ff_aptx_quant_tables[2][NB_SUBBANDS]
Definition aptx.c:313
#define NB_FILTERS
Definition aptx.h:45
static av_always_inline void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
Definition aptx.h:162
static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS]
Definition aptx.h:132
int ff_aptx_init(AVCodecContext *avctx)
Definition aptx.c:508
static int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx)
Definition aptx.h:201
static int32_t aptx_quantized_parity(Channel *channel)
Definition aptx.h:188
static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS]
Definition aptx.h:147
#define RSHIFT_SIZE(size)
Definition aptx.h:112
channels
Definition aptx.h:31
@ NB_CHANNELS
Definition aptx.h:34
static av_always_inline int32_t aptx_qmf_convolution(FilterSignal *signal, const int32_t coeffs[FILTER_TAPS], int shift)
Definition aptx.h:174
void ff_aptx_invert_quantize_and_prediction(Channel *channel, int hd)
Definition aptx.c:497
#define FILTER_TAPS
Definition aptx.h:46
void ff_aptx_generate_dither(Channel *channel)
Definition aptx.c:385
subbands
Definition aptx.h:37
@ LF
Definition aptx.h:38
@ HF
Definition aptx.h:41
@ MLF
Definition aptx.h:39
@ MHF
Definition aptx.h:40
@ NB_SUBBANDS
Definition aptx.h:42
int32_t
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define LEFT
Definition cdgraphics.c:168
#define RIGHT
Definition cdgraphics.c:169
long long int64_t
Definition coverity.c:34
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
#define sample
static int shift(int a, int b)
Definition bonk.c:261
#define av_always_inline
Definition attributes.h:72
#define MUL64(a, b)
Definition mathops.h:56
static volatile sig_atomic_t sig
Definition signal.c:48
main external API structure.
Definition avcodec.h:443
int block_size
Definition aptx.h:94
int32_t sync_idx
Definition aptx.h:95
int hd
Definition aptx.h:93
Channel channels[NB_CHANNELS]
Definition aptx.h:96
Definition aptx.h:81
Quantize quantize[NB_SUBBANDS]
Definition aptx.h:87
Prediction prediction[NB_SUBBANDS]
Definition aptx.h:89
InvertQuantize invert_quantize[NB_SUBBANDS]
Definition aptx.h:88
int32_t codeword_history
Definition aptx.h:82
int32_t dither[NB_SUBBANDS]
Definition aptx.h:84
QMFAnalysis qmf
Definition aptx.h:86
int32_t dither_parity
Definition aptx.h:83
int32_t prediction_order
Definition aptx.h:106
const int32_t * invert_quantize_dither_factors
Definition aptx.h:101
int32_t factor_max
Definition aptx.h:105
const int32_t * quantize_intervals
Definition aptx.h:100
const int16_t * quantize_factor_select_offset
Definition aptx.h:103
int tables_size
Definition aptx.h:104
const int32_t * quantize_dither_factors
Definition aptx.h:102
int pos
Definition aptx.h:49
int32_t buffer[2 *FILTER_TAPS]
Definition aptx.h:50
int32_t quantization_factor
Definition aptx.h:65
int32_t reconstructed_difference
Definition aptx.h:67
int32_t factor_select
Definition aptx.h:66
int32_t s_weight[2]
Definition aptx.h:72
int32_t pos
Definition aptx.h:74
int32_t reconstructed_differences[48]
Definition aptx.h:75
int32_t predicted_sample
Definition aptx.h:78
int32_t previous_reconstructed_sample
Definition aptx.h:76
int32_t prev_sign[2]
Definition aptx.h:71
int32_t predicted_difference
Definition aptx.h:77
int32_t d_weight[24]
Definition aptx.h:73
FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS]
Definition aptx.h:55
FilterSignal outer_filter_signal[NB_FILTERS]
Definition aptx.h:54
int32_t quantized_sample
Definition aptx.h:59
int32_t quantized_sample_parity_change
Definition aptx.h:60
int32_t error
Definition aptx.h:61
mcdeint parity
Definition vf_mcdeint.c:293