• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "mathops.h"
30 #include "audio_frame_queue.h"
31 
32 
33 enum channels {
34     LEFT,
35     RIGHT,
36     NB_CHANNELS
37 };
38 
39 enum subbands {
40     LF,  // Low Frequency (0-5.5 kHz)
41     MLF, // Medium-Low Frequency (5.5-11kHz)
42     MHF, // Medium-High Frequency (11-16.5kHz)
43     HF,  // High Frequency (16.5-22kHz)
44     NB_SUBBANDS
45 };
46 
47 #define NB_FILTERS 2
48 #define FILTER_TAPS 16
49 
50 typedef struct {
51     int pos;
52     int32_t buffer[2*FILTER_TAPS];
53 } FilterSignal;
54 
55 typedef struct {
56     FilterSignal outer_filter_signal[NB_FILTERS];
57     FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS];
58 } QMFAnalysis;
59 
60 typedef struct {
61     int32_t quantized_sample;
62     int32_t quantized_sample_parity_change;
63     int32_t error;
64 } Quantize;
65 
66 typedef struct {
67     int32_t quantization_factor;
68     int32_t factor_select;
69     int32_t reconstructed_difference;
70 } InvertQuantize;
71 
72 typedef struct {
73     int32_t prev_sign[2];
74     int32_t s_weight[2];
75     int32_t d_weight[24];
76     int32_t pos;
77     int32_t reconstructed_differences[48];
78     int32_t previous_reconstructed_sample;
79     int32_t predicted_difference;
80     int32_t predicted_sample;
81 } Prediction;
82 
83 typedef struct {
84     int32_t codeword_history;
85     int32_t dither_parity;
86     int32_t dither[NB_SUBBANDS];
87 
88     QMFAnalysis qmf;
89     Quantize quantize[NB_SUBBANDS];
90     InvertQuantize invert_quantize[NB_SUBBANDS];
91     Prediction prediction[NB_SUBBANDS];
92 } Channel;
93 
94 typedef struct {
95     int hd;
96     int block_size;
97     int32_t sync_idx;
98     Channel channels[NB_CHANNELS];
99     AudioFrameQueue afq;
100 } AptXContext;
101 
102 typedef const struct {
103     const int32_t *quantize_intervals;
104     const int32_t *invert_quantize_dither_factors;
105     const int32_t *quantize_dither_factors;
106     const int16_t *quantize_factor_select_offset;
107     int tables_size;
108     int32_t factor_max;
109     int32_t prediction_order;
110 } ConstTables;
111 
112 extern ConstTables ff_aptx_quant_tables[2][NB_SUBBANDS];
113 
114 /* Rounded right shift with optionnal clipping */
115 #define RSHIFT_SIZE(size)                                                     \
116 av_always_inline                                                              \
117 static int##size##_t rshift##size(int##size##_t value, int shift)             \
118 {                                                                             \
119     int##size##_t rounding = (int##size##_t)1 << (shift - 1);                 \
120     int##size##_t mask = ((int##size##_t)1 << (shift + 1)) - 1;               \
121     return ((value + rounding) >> shift) - ((value & mask) == rounding);      \
122 }                                                                             \
123 av_always_inline                                                              \
124 static int##size##_t rshift##size##_clip24(int##size##_t value, int shift)    \
125 {                                                                             \
126     return av_clip_intp2(rshift##size(value, shift), 23);                     \
127 }
128 RSHIFT_SIZE(32)
129 RSHIFT_SIZE(64)
130 
131 /*
132  * Convolution filter coefficients for the outer QMF of the QMF tree.
133  * The 2 sets are a mirror of each other.
134  */
135 static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS] = {
136     {
137         730, -413, -9611, 43626, -121026, 269973, -585547, 2801966,
138         697128, -160481, 27611, 8478, -10043, 3511, 688, -897,
139     },
140     {
141         -897, 688, 3511, -10043, 8478, 27611, -160481, 697128,
142         2801966, -585547, 269973, -121026, 43626, -9611, -413, 730,
143     },
144 };
145 
146 /*
147  * Convolution filter coefficients for the inner QMF of the QMF tree.
148  * The 2 sets are a mirror of each other.
149  */
150 static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS] = {
151     {
152        1033, -584, -13592, 61697, -171156, 381799, -828088, 3962579,
153        985888, -226954, 39048, 11990, -14203, 4966, 973, -1268,
154     },
155     {
156       -1268, 973, 4966, -14203, 11990, 39048, -226954, 985888,
157       3962579, -828088, 381799, -171156, 61697, -13592, -584, 1033,
158     },
159 };
160 
161 /*
162  * Push one sample into a circular signal buffer.
163  */
164 av_always_inline
aptx_qmf_filter_signal_push(FilterSignal * signal,int32_t sample)165 static void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
166 {
167     signal->buffer[signal->pos            ] = sample;
168     signal->buffer[signal->pos+FILTER_TAPS] = sample;
169     signal->pos = (signal->pos + 1) & (FILTER_TAPS - 1);
170 }
171 
172 /*
173  * Compute the convolution of the signal with the coefficients, and reduce
174  * to 24 bits by applying the specified right shifting.
175  */
176 av_always_inline
aptx_qmf_convolution(FilterSignal * signal,const int32_t coeffs[FILTER_TAPS],int shift)177 static int32_t aptx_qmf_convolution(FilterSignal *signal,
178                                     const int32_t coeffs[FILTER_TAPS],
179                                     int shift)
180 {
181     int32_t *sig = &signal->buffer[signal->pos];
182     int64_t e = 0;
183     int i;
184 
185     for (i = 0; i < FILTER_TAPS; i++)
186         e += MUL64(sig[i], coeffs[i]);
187 
188     return rshift64_clip24(e, shift);
189 }
190 
aptx_quantized_parity(Channel * channel)191 static inline int32_t aptx_quantized_parity(Channel *channel)
192 {
193     int32_t parity = channel->dither_parity;
194     int subband;
195 
196     for (subband = 0; subband < NB_SUBBANDS; subband++)
197         parity ^= channel->quantize[subband].quantized_sample;
198 
199     return parity & 1;
200 }
201 
202 /* For each sample, ensure that the parity of all subbands of all channels
203  * is 0 except once every 8 samples where the parity is forced to 1. */
aptx_check_parity(Channel channels[NB_CHANNELS],int32_t * idx)204 static inline int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx)
205 {
206     int32_t parity = aptx_quantized_parity(&channels[LEFT])
207                    ^ aptx_quantized_parity(&channels[RIGHT]);
208 
209     int eighth = *idx == 7;
210     *idx = (*idx + 1) & 7;
211 
212     return parity ^ eighth;
213 }
214 
215 void ff_aptx_invert_quantize_and_prediction(Channel *channel, int hd);
216 void ff_aptx_generate_dither(Channel *channel);
217 
218 int ff_aptx_init(AVCodecContext *avctx);
219 
220 #endif /* AVCODEC_APTX_H */
221