The encoder was reverse engineered from binary library and from
EP0398973B1 patent (long expired).
The decoder was simply deduced from the encoder.
| ... | ... |
@@ -993,6 +993,8 @@ following image formats are supported: |
| 993 | 993 |
@item Amazing Studio PAF Audio @tab @tab X |
| 994 | 994 |
@item Apple lossless audio @tab X @tab X |
| 995 | 995 |
@tab QuickTime fourcc 'alac' |
| 996 |
+@item aptX @tab X @tab X |
|
| 997 |
+ @tab Used in Bluetooth A2DP |
|
| 996 | 998 |
@item ATRAC1 @tab @tab X |
| 997 | 999 |
@item ATRAC3 @tab @tab X |
| 998 | 1000 |
@item ATRAC3+ @tab @tab X |
| ... | ... |
@@ -188,6 +188,8 @@ OBJS-$(CONFIG_AMV_ENCODER) += mjpegenc.o mjpegenc_common.o \ |
| 188 | 188 |
OBJS-$(CONFIG_ANM_DECODER) += anm.o |
| 189 | 189 |
OBJS-$(CONFIG_ANSI_DECODER) += ansi.o cga_data.o |
| 190 | 190 |
OBJS-$(CONFIG_APE_DECODER) += apedec.o |
| 191 |
+OBJS-$(CONFIG_APTX_DECODER) += aptx.o |
|
| 192 |
+OBJS-$(CONFIG_APTX_ENCODER) += aptx.o |
|
| 191 | 193 |
OBJS-$(CONFIG_APNG_DECODER) += png.o pngdec.o pngdsp.o |
| 192 | 194 |
OBJS-$(CONFIG_APNG_ENCODER) += png.o pngenc.o |
| 193 | 195 |
OBJS-$(CONFIG_SSA_DECODER) += assdec.o ass.o |
| ... | ... |
@@ -406,6 +406,7 @@ static void register_all(void) |
| 406 | 406 |
REGISTER_DECODER(AMRNB, amrnb); |
| 407 | 407 |
REGISTER_DECODER(AMRWB, amrwb); |
| 408 | 408 |
REGISTER_DECODER(APE, ape); |
| 409 |
+ REGISTER_ENCDEC (APTX, aptx); |
|
| 409 | 410 |
REGISTER_DECODER(ATRAC1, atrac1); |
| 410 | 411 |
REGISTER_DECODER(ATRAC3, atrac3); |
| 411 | 412 |
REGISTER_DECODER(ATRAC3AL, atrac3al); |
| 412 | 413 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,860 @@ |
| 0 |
+/* |
|
| 1 |
+ * Audio Processing Technology codec for Bluetooth (aptX) |
|
| 2 |
+ * |
|
| 3 |
+ * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org> |
|
| 4 |
+ * |
|
| 5 |
+ * This file is part of FFmpeg. |
|
| 6 |
+ * |
|
| 7 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
| 8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
| 9 |
+ * License as published by the Free Software Foundation; either |
|
| 10 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
| 11 |
+ * |
|
| 12 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
| 13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 15 |
+ * Lesser General Public License for more details. |
|
| 16 |
+ * |
|
| 17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
| 18 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
| 19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 20 |
+ */ |
|
| 21 |
+ |
|
| 22 |
+#include "libavutil/intreadwrite.h" |
|
| 23 |
+#include "avcodec.h" |
|
| 24 |
+#include "internal.h" |
|
| 25 |
+#include "mathops.h" |
|
| 26 |
+#include "audio_frame_queue.h" |
|
| 27 |
+ |
|
| 28 |
+ |
|
| 29 |
+enum channels {
|
|
| 30 |
+ LEFT, |
|
| 31 |
+ RIGHT, |
|
| 32 |
+ NB_CHANNELS |
|
| 33 |
+}; |
|
| 34 |
+ |
|
| 35 |
+enum subbands {
|
|
| 36 |
+ LF, // Low Frequency (0-5.5 kHz) |
|
| 37 |
+ MLF, // Medium-Low Frequency (5.5-11kHz) |
|
| 38 |
+ MHF, // Medium-High Frequency (11-16.5kHz) |
|
| 39 |
+ HF, // High Frequency (16.5-22kHz) |
|
| 40 |
+ NB_SUBBANDS |
|
| 41 |
+}; |
|
| 42 |
+ |
|
| 43 |
+#define NB_FILTERS 2 |
|
| 44 |
+#define FILTER_TAPS 16 |
|
| 45 |
+ |
|
| 46 |
+typedef struct {
|
|
| 47 |
+ int pos; |
|
| 48 |
+ int32_t buffer[2*FILTER_TAPS]; |
|
| 49 |
+} FilterSignal; |
|
| 50 |
+ |
|
| 51 |
+typedef struct {
|
|
| 52 |
+ FilterSignal outer_filter_signal[NB_FILTERS]; |
|
| 53 |
+ FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS]; |
|
| 54 |
+} QMFAnalysis; |
|
| 55 |
+ |
|
| 56 |
+typedef struct {
|
|
| 57 |
+ int32_t quantized_sample; |
|
| 58 |
+ int32_t quantized_sample_parity_change; |
|
| 59 |
+ int32_t error; |
|
| 60 |
+} Quantize; |
|
| 61 |
+ |
|
| 62 |
+typedef struct {
|
|
| 63 |
+ int32_t quantization_factor; |
|
| 64 |
+ int32_t factor_select; |
|
| 65 |
+ int32_t reconstructed_difference; |
|
| 66 |
+} InvertQuantize; |
|
| 67 |
+ |
|
| 68 |
+typedef struct {
|
|
| 69 |
+ int32_t prev_sign[2]; |
|
| 70 |
+ int32_t s_weight[2]; |
|
| 71 |
+ int32_t d_weight[24]; |
|
| 72 |
+ int32_t pos; |
|
| 73 |
+ int32_t reconstructed_differences[48]; |
|
| 74 |
+ int32_t previous_reconstructed_sample; |
|
| 75 |
+ int32_t predicted_difference; |
|
| 76 |
+ int32_t predicted_sample; |
|
| 77 |
+} Prediction; |
|
| 78 |
+ |
|
| 79 |
+typedef struct {
|
|
| 80 |
+ int32_t codeword_history; |
|
| 81 |
+ int32_t dither_parity; |
|
| 82 |
+ int32_t dither[NB_SUBBANDS]; |
|
| 83 |
+ |
|
| 84 |
+ QMFAnalysis qmf; |
|
| 85 |
+ Quantize quantize[NB_SUBBANDS]; |
|
| 86 |
+ InvertQuantize invert_quantize[NB_SUBBANDS]; |
|
| 87 |
+ Prediction prediction[NB_SUBBANDS]; |
|
| 88 |
+} Channel; |
|
| 89 |
+ |
|
| 90 |
+typedef struct {
|
|
| 91 |
+ int32_t sync_idx; |
|
| 92 |
+ Channel channels[NB_CHANNELS]; |
|
| 93 |
+ AudioFrameQueue afq; |
|
| 94 |
+} AptXContext; |
|
| 95 |
+ |
|
| 96 |
+ |
|
| 97 |
+static const int32_t quantize_intervals_LF[65] = {
|
|
| 98 |
+ -9948, 9948, 29860, 49808, 69822, 89926, 110144, 130502, |
|
| 99 |
+ 151026, 171738, 192666, 213832, 235264, 256982, 279014, 301384, |
|
| 100 |
+ 324118, 347244, 370790, 394782, 419250, 444226, 469742, 495832, |
|
| 101 |
+ 522536, 549890, 577936, 606720, 636290, 666700, 698006, 730270, |
|
| 102 |
+ 763562, 797958, 833538, 870398, 908640, 948376, 989740, 1032874, |
|
| 103 |
+ 1077948, 1125150, 1174700, 1226850, 1281900, 1340196, 1402156, 1468282, |
|
| 104 |
+ 1539182, 1615610, 1698514, 1789098, 1888944, 2000168, 2125700, 2269750, |
|
| 105 |
+ 2438670, 2642660, 2899462, 3243240, 3746078, 4535138, 5664098, 7102424, |
|
| 106 |
+ 8897462, |
|
| 107 |
+}; |
|
| 108 |
+static const int32_t invert_quantize_dither_factors_LF[65] = {
|
|
| 109 |
+ 9948, 9948, 9962, 9988, 10026, 10078, 10142, 10218, |
|
| 110 |
+ 10306, 10408, 10520, 10646, 10784, 10934, 11098, 11274, |
|
| 111 |
+ 11462, 11664, 11880, 12112, 12358, 12618, 12898, 13194, |
|
| 112 |
+ 13510, 13844, 14202, 14582, 14988, 15422, 15884, 16380, |
|
| 113 |
+ 16912, 17484, 18098, 18762, 19480, 20258, 21106, 22030, |
|
| 114 |
+ 23044, 24158, 25390, 26760, 28290, 30008, 31954, 34172, |
|
| 115 |
+ 36728, 39700, 43202, 47382, 52462, 58762, 66770, 77280, |
|
| 116 |
+ 91642, 112348, 144452, 199326, 303512, 485546, 643414, 794914, |
|
| 117 |
+ 1000124, |
|
| 118 |
+}; |
|
| 119 |
+static const int32_t quantize_dither_factors_LF[65] = {
|
|
| 120 |
+ 0, 4, 7, 10, 13, 16, 19, 22, |
|
| 121 |
+ 26, 28, 32, 35, 38, 41, 44, 47, |
|
| 122 |
+ 51, 54, 58, 62, 65, 70, 74, 79, |
|
| 123 |
+ 84, 90, 95, 102, 109, 116, 124, 133, |
|
| 124 |
+ 143, 154, 166, 180, 195, 212, 231, 254, |
|
| 125 |
+ 279, 308, 343, 383, 430, 487, 555, 639, |
|
| 126 |
+ 743, 876, 1045, 1270, 1575, 2002, 2628, 3591, |
|
| 127 |
+ 5177, 8026, 13719, 26047, 45509, 39467, 37875, 51303, |
|
| 128 |
+ 0, |
|
| 129 |
+}; |
|
| 130 |
+static const int16_t quantize_factor_select_offset_LF[65] = {
|
|
| 131 |
+ 0, -21, -19, -17, -15, -12, -10, -8, |
|
| 132 |
+ -6, -4, -1, 1, 3, 6, 8, 10, |
|
| 133 |
+ 13, 15, 18, 20, 23, 26, 29, 31, |
|
| 134 |
+ 34, 37, 40, 43, 47, 50, 53, 57, |
|
| 135 |
+ 60, 64, 68, 72, 76, 80, 85, 89, |
|
| 136 |
+ 94, 99, 105, 110, 116, 123, 129, 136, |
|
| 137 |
+ 144, 152, 161, 171, 182, 194, 207, 223, |
|
| 138 |
+ 241, 263, 291, 328, 382, 467, 522, 522, |
|
| 139 |
+ 522, |
|
| 140 |
+}; |
|
| 141 |
+ |
|
| 142 |
+ |
|
| 143 |
+static const int32_t quantize_intervals_MLF[9] = {
|
|
| 144 |
+ -89806, 89806, 278502, 494338, 759442, 1113112, 1652322, 2720256, 5190186, |
|
| 145 |
+}; |
|
| 146 |
+static const int32_t invert_quantize_dither_factors_MLF[9] = {
|
|
| 147 |
+ 89806, 89806, 98890, 116946, 148158, 205512, 333698, 734236, 1735696, |
|
| 148 |
+}; |
|
| 149 |
+static const int32_t quantize_dither_factors_MLF[9] = {
|
|
| 150 |
+ 0, 2271, 4514, 7803, 14339, 32047, 100135, 250365, 0, |
|
| 151 |
+}; |
|
| 152 |
+static const int16_t quantize_factor_select_offset_MLF[9] = {
|
|
| 153 |
+ 0, -14, 6, 29, 58, 96, 154, 270, 521, |
|
| 154 |
+}; |
|
| 155 |
+ |
|
| 156 |
+ |
|
| 157 |
+static const int32_t quantize_intervals_MHF[3] = {
|
|
| 158 |
+ -194080, 194080, 890562, |
|
| 159 |
+}; |
|
| 160 |
+static const int32_t invert_quantize_dither_factors_MHF[3] = {
|
|
| 161 |
+ 194080, 194080, 502402, |
|
| 162 |
+}; |
|
| 163 |
+static const int32_t quantize_dither_factors_MHF[3] = {
|
|
| 164 |
+ 0, 77081, 0, |
|
| 165 |
+}; |
|
| 166 |
+static const int16_t quantize_factor_select_offset_MHF[3] = {
|
|
| 167 |
+ 0, -33, 136, |
|
| 168 |
+}; |
|
| 169 |
+ |
|
| 170 |
+ |
|
| 171 |
+static const int32_t quantize_intervals_HF[5] = {
|
|
| 172 |
+ -163006, 163006, 542708, 1120554, 2669238, |
|
| 173 |
+}; |
|
| 174 |
+static const int32_t invert_quantize_dither_factors_HF[5] = {
|
|
| 175 |
+ 163006, 163006, 216698, 361148, 1187538, |
|
| 176 |
+}; |
|
| 177 |
+static const int32_t quantize_dither_factors_HF[5] = {
|
|
| 178 |
+ 0, 13423, 36113, 206598, 0, |
|
| 179 |
+}; |
|
| 180 |
+static const int16_t quantize_factor_select_offset_HF[5] = {
|
|
| 181 |
+ 0, -8, 33, 95, 262, |
|
| 182 |
+}; |
|
| 183 |
+ |
|
| 184 |
+typedef const struct {
|
|
| 185 |
+ const int32_t *quantize_intervals; |
|
| 186 |
+ const int32_t *invert_quantize_dither_factors; |
|
| 187 |
+ const int32_t *quantize_dither_factors; |
|
| 188 |
+ const int16_t *quantize_factor_select_offset; |
|
| 189 |
+ int tables_size; |
|
| 190 |
+ int32_t quantized_bits; |
|
| 191 |
+ int32_t prediction_order; |
|
| 192 |
+} ConstTables; |
|
| 193 |
+ |
|
| 194 |
+static ConstTables tables[NB_SUBBANDS] = {
|
|
| 195 |
+ [LF] = { quantize_intervals_LF,
|
|
| 196 |
+ invert_quantize_dither_factors_LF, |
|
| 197 |
+ quantize_dither_factors_LF, |
|
| 198 |
+ quantize_factor_select_offset_LF, |
|
| 199 |
+ FF_ARRAY_ELEMS(quantize_intervals_LF), |
|
| 200 |
+ 7, 24 }, |
|
| 201 |
+ [MLF] = { quantize_intervals_MLF,
|
|
| 202 |
+ invert_quantize_dither_factors_MLF, |
|
| 203 |
+ quantize_dither_factors_MLF, |
|
| 204 |
+ quantize_factor_select_offset_MLF, |
|
| 205 |
+ FF_ARRAY_ELEMS(quantize_intervals_MLF), |
|
| 206 |
+ 4, 12 }, |
|
| 207 |
+ [MHF] = { quantize_intervals_MHF,
|
|
| 208 |
+ invert_quantize_dither_factors_MHF, |
|
| 209 |
+ quantize_dither_factors_MHF, |
|
| 210 |
+ quantize_factor_select_offset_MHF, |
|
| 211 |
+ FF_ARRAY_ELEMS(quantize_intervals_MHF), |
|
| 212 |
+ 2, 6 }, |
|
| 213 |
+ [HF] = { quantize_intervals_HF,
|
|
| 214 |
+ invert_quantize_dither_factors_HF, |
|
| 215 |
+ quantize_dither_factors_HF, |
|
| 216 |
+ quantize_factor_select_offset_HF, |
|
| 217 |
+ FF_ARRAY_ELEMS(quantize_intervals_HF), |
|
| 218 |
+ 3, 12 }, |
|
| 219 |
+}; |
|
| 220 |
+ |
|
| 221 |
+static const int16_t quantization_factors[32] = {
|
|
| 222 |
+ 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, |
|
| 223 |
+ 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, |
|
| 224 |
+ 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, |
|
| 225 |
+ 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008, |
|
| 226 |
+}; |
|
| 227 |
+ |
|
| 228 |
+ |
|
| 229 |
+/* Rounded right shift with optionnal clipping */ |
|
| 230 |
+#define RSHIFT_SIZE(size) \ |
|
| 231 |
+av_always_inline \ |
|
| 232 |
+static int##size##_t rshift##size(int##size##_t value, int shift) \ |
|
| 233 |
+{ \
|
|
| 234 |
+ int##size##_t rounding = (int##size##_t)1 << (shift - 1); \ |
|
| 235 |
+ int##size##_t mask = ((int##size##_t)1 << (shift + 1)) - 1; \ |
|
| 236 |
+ return ((value + rounding) >> shift) - ((value & mask) == rounding); \ |
|
| 237 |
+} \ |
|
| 238 |
+av_always_inline \ |
|
| 239 |
+static int##size##_t rshift##size##_clip24(int##size##_t value, int shift) \ |
|
| 240 |
+{ \
|
|
| 241 |
+ return av_clip_intp2(rshift##size(value, shift), 23); \ |
|
| 242 |
+} |
|
| 243 |
+RSHIFT_SIZE(32) |
|
| 244 |
+RSHIFT_SIZE(64) |
|
| 245 |
+ |
|
| 246 |
+ |
|
| 247 |
+av_always_inline |
|
| 248 |
+static void aptx_update_codeword_history(Channel *channel) |
|
| 249 |
+{
|
|
| 250 |
+ int32_t cw = ((channel->quantize[0].quantized_sample & 3) << 0) + |
|
| 251 |
+ ((channel->quantize[1].quantized_sample & 2) << 1) + |
|
| 252 |
+ ((channel->quantize[2].quantized_sample & 1) << 3); |
|
| 253 |
+ channel->codeword_history = (cw << 8) + (channel->codeword_history << 4); |
|
| 254 |
+} |
|
| 255 |
+ |
|
| 256 |
+static void aptx_generate_dither(Channel *channel) |
|
| 257 |
+{
|
|
| 258 |
+ int subband; |
|
| 259 |
+ int64_t m; |
|
| 260 |
+ int32_t d; |
|
| 261 |
+ |
|
| 262 |
+ aptx_update_codeword_history(channel); |
|
| 263 |
+ |
|
| 264 |
+ m = (int64_t)5184443 * (channel->codeword_history >> 7); |
|
| 265 |
+ d = (m << 2) + (m >> 22); |
|
| 266 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) |
|
| 267 |
+ channel->dither[subband] = d << (23 - 5*subband); |
|
| 268 |
+ channel->dither_parity = (d >> 25) & 1; |
|
| 269 |
+} |
|
| 270 |
+ |
|
| 271 |
+/* |
|
| 272 |
+ * Convolution filter coefficients for the outer QMF of the QMF tree. |
|
| 273 |
+ * The 2 sets are a mirror of each other. |
|
| 274 |
+ */ |
|
| 275 |
+static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS] = {
|
|
| 276 |
+ {
|
|
| 277 |
+ 730, -413, -9611, 43626, -121026, 269973, -585547, 2801966, |
|
| 278 |
+ 697128, -160481, 27611, 8478, -10043, 3511, 688, -897, |
|
| 279 |
+ }, |
|
| 280 |
+ {
|
|
| 281 |
+ -897, 688, 3511, -10043, 8478, 27611, -160481, 697128, |
|
| 282 |
+ 2801966, -585547, 269973, -121026, 43626, -9611, -413, 730, |
|
| 283 |
+ }, |
|
| 284 |
+}; |
|
| 285 |
+ |
|
| 286 |
+/* |
|
| 287 |
+ * Convolution filter coefficients for the inner QMF of the QMF tree. |
|
| 288 |
+ * The 2 sets are a mirror of each other. |
|
| 289 |
+ */ |
|
| 290 |
+static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS] = {
|
|
| 291 |
+ {
|
|
| 292 |
+ 1033, -584, -13592, 61697, -171156, 381799, -828088, 3962579, |
|
| 293 |
+ 985888, -226954, 39048, 11990, -14203, 4966, 973, -1268, |
|
| 294 |
+ }, |
|
| 295 |
+ {
|
|
| 296 |
+ -1268, 973, 4966, -14203, 11990, 39048, -226954, 985888, |
|
| 297 |
+ 3962579, -828088, 381799, -171156, 61697, -13592, -584, 1033, |
|
| 298 |
+ }, |
|
| 299 |
+}; |
|
| 300 |
+ |
|
| 301 |
+/* |
|
| 302 |
+ * Push one sample into a circular signal buffer. |
|
| 303 |
+ */ |
|
| 304 |
+av_always_inline |
|
| 305 |
+static void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample) |
|
| 306 |
+{
|
|
| 307 |
+ signal->buffer[signal->pos ] = sample; |
|
| 308 |
+ signal->buffer[signal->pos+FILTER_TAPS] = sample; |
|
| 309 |
+ signal->pos = (signal->pos + 1) & (FILTER_TAPS - 1); |
|
| 310 |
+} |
|
| 311 |
+ |
|
| 312 |
+/* |
|
| 313 |
+ * Compute the convolution of the signal with the coefficients, and reduce |
|
| 314 |
+ * to 24 bits by applying the specified right shifting. |
|
| 315 |
+ */ |
|
| 316 |
+av_always_inline |
|
| 317 |
+static int32_t aptx_qmf_convolution(FilterSignal *signal, |
|
| 318 |
+ const int32_t coeffs[FILTER_TAPS], |
|
| 319 |
+ int shift) |
|
| 320 |
+{
|
|
| 321 |
+ int32_t *sig = &signal->buffer[signal->pos]; |
|
| 322 |
+ int64_t e = 0; |
|
| 323 |
+ int i; |
|
| 324 |
+ |
|
| 325 |
+ for (i = 0; i < FILTER_TAPS; i++) |
|
| 326 |
+ e += MUL64(sig[i], coeffs[i]); |
|
| 327 |
+ |
|
| 328 |
+ return rshift64_clip24(e, shift); |
|
| 329 |
+} |
|
| 330 |
+ |
|
| 331 |
+/* |
|
| 332 |
+ * Half-band QMF analysis filter realized with a polyphase FIR filter. |
|
| 333 |
+ * Split into 2 subbands and downsample by 2. |
|
| 334 |
+ * So for each pair of samples that goes in, one sample goes out, |
|
| 335 |
+ * split into 2 separate subbands. |
|
| 336 |
+ */ |
|
| 337 |
+av_always_inline |
|
| 338 |
+static void aptx_qmf_polyphase_analysis(FilterSignal signal[NB_FILTERS], |
|
| 339 |
+ const int32_t coeffs[NB_FILTERS][FILTER_TAPS], |
|
| 340 |
+ int shift, |
|
| 341 |
+ int32_t samples[NB_FILTERS], |
|
| 342 |
+ int32_t *low_subband_output, |
|
| 343 |
+ int32_t *high_subband_output) |
|
| 344 |
+{
|
|
| 345 |
+ int32_t subbands[NB_FILTERS]; |
|
| 346 |
+ int i; |
|
| 347 |
+ |
|
| 348 |
+ for (i = 0; i < NB_FILTERS; i++) {
|
|
| 349 |
+ aptx_qmf_filter_signal_push(&signal[i], samples[NB_FILTERS-1-i]); |
|
| 350 |
+ subbands[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift); |
|
| 351 |
+ } |
|
| 352 |
+ |
|
| 353 |
+ *low_subband_output = av_clip_intp2(subbands[0] + subbands[1], 23); |
|
| 354 |
+ *high_subband_output = av_clip_intp2(subbands[0] - subbands[1], 23); |
|
| 355 |
+} |
|
| 356 |
+ |
|
| 357 |
+/* |
|
| 358 |
+ * Two stage QMF analysis tree. |
|
| 359 |
+ * Split 4 input samples into 4 subbands and downsample by 4. |
|
| 360 |
+ * So for each group of 4 samples that goes in, one sample goes out, |
|
| 361 |
+ * split into 4 separate subbands. |
|
| 362 |
+ */ |
|
| 363 |
+static void aptx_qmf_tree_analysis(QMFAnalysis *qmf, |
|
| 364 |
+ int32_t samples[4], |
|
| 365 |
+ int32_t subband_samples[4]) |
|
| 366 |
+{
|
|
| 367 |
+ int32_t intermediate_samples[4]; |
|
| 368 |
+ int i; |
|
| 369 |
+ |
|
| 370 |
+ /* Split 4 input samples into 2 intermediate subbands downsampled to 2 samples */ |
|
| 371 |
+ for (i = 0; i < 2; i++) |
|
| 372 |
+ aptx_qmf_polyphase_analysis(qmf->outer_filter_signal, |
|
| 373 |
+ aptx_qmf_outer_coeffs, 23, |
|
| 374 |
+ &samples[2*i], |
|
| 375 |
+ &intermediate_samples[0+i], |
|
| 376 |
+ &intermediate_samples[2+i]); |
|
| 377 |
+ |
|
| 378 |
+ /* Split 2 intermediate subband samples into 4 final subbands downsampled to 1 sample */ |
|
| 379 |
+ for (i = 0; i < 2; i++) |
|
| 380 |
+ aptx_qmf_polyphase_analysis(qmf->inner_filter_signal[i], |
|
| 381 |
+ aptx_qmf_inner_coeffs, 23, |
|
| 382 |
+ &intermediate_samples[2*i], |
|
| 383 |
+ &subband_samples[2*i+0], |
|
| 384 |
+ &subband_samples[2*i+1]); |
|
| 385 |
+} |
|
| 386 |
+ |
|
| 387 |
+/* |
|
| 388 |
+ * Half-band QMF synthesis filter realized with a polyphase FIR filter. |
|
| 389 |
+ * Join 2 subbands and upsample by 2. |
|
| 390 |
+ * So for each 2 subbands sample that goes in, a pair of samples goes out. |
|
| 391 |
+ */ |
|
| 392 |
+av_always_inline |
|
| 393 |
+static void aptx_qmf_polyphase_synthesis(FilterSignal signal[NB_FILTERS], |
|
| 394 |
+ const int32_t coeffs[NB_FILTERS][FILTER_TAPS], |
|
| 395 |
+ int shift, |
|
| 396 |
+ int32_t low_subband_input, |
|
| 397 |
+ int32_t high_subband_input, |
|
| 398 |
+ int32_t samples[NB_FILTERS]) |
|
| 399 |
+{
|
|
| 400 |
+ int32_t subbands[NB_FILTERS]; |
|
| 401 |
+ int i; |
|
| 402 |
+ |
|
| 403 |
+ subbands[0] = low_subband_input + high_subband_input; |
|
| 404 |
+ subbands[1] = low_subband_input - high_subband_input; |
|
| 405 |
+ |
|
| 406 |
+ for (i = 0; i < NB_FILTERS; i++) {
|
|
| 407 |
+ aptx_qmf_filter_signal_push(&signal[i], subbands[1-i]); |
|
| 408 |
+ samples[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift); |
|
| 409 |
+ } |
|
| 410 |
+} |
|
| 411 |
+ |
|
| 412 |
+/* |
|
| 413 |
+ * Two stage QMF synthesis tree. |
|
| 414 |
+ * Join 4 subbands and upsample by 4. |
|
| 415 |
+ * So for each 4 subbands sample that goes in, a group of 4 samples goes out. |
|
| 416 |
+ */ |
|
| 417 |
+static void aptx_qmf_tree_synthesis(QMFAnalysis *qmf, |
|
| 418 |
+ int32_t subband_samples[4], |
|
| 419 |
+ int32_t samples[4]) |
|
| 420 |
+{
|
|
| 421 |
+ int32_t intermediate_samples[4]; |
|
| 422 |
+ int i; |
|
| 423 |
+ |
|
| 424 |
+ /* Join 4 subbands into 2 intermediate subbands upsampled to 2 samples. */ |
|
| 425 |
+ for (i = 0; i < 2; i++) |
|
| 426 |
+ aptx_qmf_polyphase_synthesis(qmf->inner_filter_signal[i], |
|
| 427 |
+ aptx_qmf_inner_coeffs, 22, |
|
| 428 |
+ subband_samples[2*i+0], |
|
| 429 |
+ subband_samples[2*i+1], |
|
| 430 |
+ &intermediate_samples[2*i]); |
|
| 431 |
+ |
|
| 432 |
+ /* Join 2 samples from intermediate subbands upsampled to 4 samples. */ |
|
| 433 |
+ for (i = 0; i < 2; i++) |
|
| 434 |
+ aptx_qmf_polyphase_synthesis(qmf->outer_filter_signal, |
|
| 435 |
+ aptx_qmf_outer_coeffs, 21, |
|
| 436 |
+ intermediate_samples[0+i], |
|
| 437 |
+ intermediate_samples[2+i], |
|
| 438 |
+ &samples[2*i]); |
|
| 439 |
+} |
|
| 440 |
+ |
|
| 441 |
+ |
|
| 442 |
+av_always_inline |
|
| 443 |
+static int32_t aptx_bin_search(int32_t value, int32_t factor, |
|
| 444 |
+ const int32_t *intervals, int32_t nb_intervals) |
|
| 445 |
+{
|
|
| 446 |
+ int32_t idx = 0; |
|
| 447 |
+ int i; |
|
| 448 |
+ |
|
| 449 |
+ for (i = nb_intervals >> 1; i > 0; i >>= 1) |
|
| 450 |
+ if (MUL64(factor, intervals[idx + i]) <= ((int64_t)value << 24)) |
|
| 451 |
+ idx += i; |
|
| 452 |
+ |
|
| 453 |
+ return idx; |
|
| 454 |
+} |
|
| 455 |
+ |
|
| 456 |
+static void aptx_quantize_difference(Quantize *quantize, |
|
| 457 |
+ int32_t sample_difference, |
|
| 458 |
+ int32_t dither, |
|
| 459 |
+ int32_t quantization_factor, |
|
| 460 |
+ ConstTables *tables) |
|
| 461 |
+{
|
|
| 462 |
+ const int32_t *intervals = tables->quantize_intervals; |
|
| 463 |
+ int32_t quantized_sample, dithered_sample, parity_change; |
|
| 464 |
+ int32_t d, mean, interval, inv; |
|
| 465 |
+ int64_t error; |
|
| 466 |
+ |
|
| 467 |
+ quantized_sample = aptx_bin_search(FFABS(sample_difference) >> 4, |
|
| 468 |
+ quantization_factor, |
|
| 469 |
+ intervals, tables->tables_size); |
|
| 470 |
+ |
|
| 471 |
+ d = rshift32_clip24(MULH(dither, dither), 7) - (1 << 23); |
|
| 472 |
+ d = rshift64(MUL64(d, tables->quantize_dither_factors[quantized_sample]), 23); |
|
| 473 |
+ |
|
| 474 |
+ intervals += quantized_sample; |
|
| 475 |
+ mean = (intervals[1] + intervals[0]) / 2; |
|
| 476 |
+ interval = (intervals[1] - intervals[0]) * (-(sample_difference < 0) | 1); |
|
| 477 |
+ |
|
| 478 |
+ dithered_sample = rshift64_clip24(MUL64(dither, interval) + ((int64_t)(mean + d) << 32), 32); |
|
| 479 |
+ error = ((int64_t)FFABS(sample_difference) << 20) - MUL64(dithered_sample, quantization_factor); |
|
| 480 |
+ quantize->error = FFABS(rshift64(error, 23)); |
|
| 481 |
+ |
|
| 482 |
+ parity_change = quantized_sample; |
|
| 483 |
+ if (error < 0) |
|
| 484 |
+ quantized_sample--; |
|
| 485 |
+ else |
|
| 486 |
+ parity_change--; |
|
| 487 |
+ |
|
| 488 |
+ inv = -(sample_difference < 0); |
|
| 489 |
+ quantize->quantized_sample = quantized_sample ^ inv; |
|
| 490 |
+ quantize->quantized_sample_parity_change = parity_change ^ inv; |
|
| 491 |
+} |
|
| 492 |
+ |
|
| 493 |
+static void aptx_encode_channel(Channel *channel, int32_t samples[4]) |
|
| 494 |
+{
|
|
| 495 |
+ int32_t subband_samples[4]; |
|
| 496 |
+ int subband; |
|
| 497 |
+ aptx_qmf_tree_analysis(&channel->qmf, samples, subband_samples); |
|
| 498 |
+ aptx_generate_dither(channel); |
|
| 499 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) {
|
|
| 500 |
+ int32_t diff = av_clip_intp2(subband_samples[subband] - channel->prediction[subband].predicted_sample, 23); |
|
| 501 |
+ aptx_quantize_difference(&channel->quantize[subband], diff, |
|
| 502 |
+ channel->dither[subband], |
|
| 503 |
+ channel->invert_quantize[subband].quantization_factor, |
|
| 504 |
+ &tables[subband]); |
|
| 505 |
+ } |
|
| 506 |
+} |
|
| 507 |
+ |
|
| 508 |
+static void aptx_decode_channel(Channel *channel, int32_t samples[4]) |
|
| 509 |
+{
|
|
| 510 |
+ int32_t subband_samples[4]; |
|
| 511 |
+ int subband; |
|
| 512 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) |
|
| 513 |
+ subband_samples[subband] = channel->prediction[subband].previous_reconstructed_sample; |
|
| 514 |
+ aptx_qmf_tree_synthesis(&channel->qmf, subband_samples, samples); |
|
| 515 |
+} |
|
| 516 |
+ |
|
| 517 |
+ |
|
| 518 |
+static void aptx_invert_quantization(InvertQuantize *invert_quantize, |
|
| 519 |
+ int32_t quantized_sample, int32_t dither, |
|
| 520 |
+ ConstTables *tables) |
|
| 521 |
+{
|
|
| 522 |
+ int32_t qr, idx, shift, factor_select; |
|
| 523 |
+ |
|
| 524 |
+ idx = (quantized_sample ^ -(quantized_sample < 0)) + 1; |
|
| 525 |
+ qr = tables->quantize_intervals[idx] / 2; |
|
| 526 |
+ if (quantized_sample < 0) |
|
| 527 |
+ qr = -qr; |
|
| 528 |
+ |
|
| 529 |
+ qr = rshift64_clip24(((int64_t)qr<<32) + MUL64(dither, tables->invert_quantize_dither_factors[idx]), 32); |
|
| 530 |
+ invert_quantize->reconstructed_difference = MUL64(invert_quantize->quantization_factor, qr) >> 19; |
|
| 531 |
+ |
|
| 532 |
+ shift = 24 - tables->quantized_bits; |
|
| 533 |
+ |
|
| 534 |
+ /* update factor_select */ |
|
| 535 |
+ factor_select = 32620 * invert_quantize->factor_select; |
|
| 536 |
+ factor_select = rshift32(factor_select + (tables->quantize_factor_select_offset[idx] << 15), 15); |
|
| 537 |
+ invert_quantize->factor_select = av_clip(factor_select, 0, (shift << 8) | 0xFF); |
|
| 538 |
+ |
|
| 539 |
+ /* update quantization factor */ |
|
| 540 |
+ idx = (invert_quantize->factor_select & 0xFF) >> 3; |
|
| 541 |
+ shift -= invert_quantize->factor_select >> 8; |
|
| 542 |
+ invert_quantize->quantization_factor = (quantization_factors[idx] << 11) >> shift; |
|
| 543 |
+} |
|
| 544 |
+ |
|
| 545 |
+static int32_t *aptx_reconstructed_differences_update(Prediction *prediction, |
|
| 546 |
+ int32_t reconstructed_difference, |
|
| 547 |
+ int order) |
|
| 548 |
+{
|
|
| 549 |
+ int32_t *rd1 = prediction->reconstructed_differences, *rd2 = rd1 + order; |
|
| 550 |
+ int p = prediction->pos; |
|
| 551 |
+ |
|
| 552 |
+ rd1[p] = rd2[p]; |
|
| 553 |
+ prediction->pos = p = (p + 1) % order; |
|
| 554 |
+ rd2[p] = reconstructed_difference; |
|
| 555 |
+ return &rd2[p]; |
|
| 556 |
+} |
|
| 557 |
+ |
|
| 558 |
+static void aptx_prediction_filtering(Prediction *prediction, |
|
| 559 |
+ int32_t reconstructed_difference, |
|
| 560 |
+ int order) |
|
| 561 |
+{
|
|
| 562 |
+ int32_t reconstructed_sample, predictor, srd0; |
|
| 563 |
+ int32_t *reconstructed_differences; |
|
| 564 |
+ int64_t predicted_difference = 0; |
|
| 565 |
+ int i; |
|
| 566 |
+ |
|
| 567 |
+ reconstructed_sample = av_clip_intp2(reconstructed_difference + prediction->predicted_sample, 23); |
|
| 568 |
+ predictor = av_clip_intp2((MUL64(prediction->s_weight[0], prediction->previous_reconstructed_sample) |
|
| 569 |
+ + MUL64(prediction->s_weight[1], reconstructed_sample)) >> 22, 23); |
|
| 570 |
+ prediction->previous_reconstructed_sample = reconstructed_sample; |
|
| 571 |
+ |
|
| 572 |
+ reconstructed_differences = aptx_reconstructed_differences_update(prediction, reconstructed_difference, order); |
|
| 573 |
+ srd0 = FFDIFFSIGN(reconstructed_difference, 0) << 23; |
|
| 574 |
+ for (i = 0; i < order; i++) {
|
|
| 575 |
+ int32_t srd = FF_SIGNBIT(reconstructed_differences[-i-1]) | 1; |
|
| 576 |
+ prediction->d_weight[i] -= rshift32(prediction->d_weight[i] - srd*srd0, 8); |
|
| 577 |
+ predicted_difference += MUL64(reconstructed_differences[-i], prediction->d_weight[i]); |
|
| 578 |
+ } |
|
| 579 |
+ |
|
| 580 |
+ prediction->predicted_difference = av_clip_intp2(predicted_difference >> 22, 23); |
|
| 581 |
+ prediction->predicted_sample = av_clip_intp2(predictor + prediction->predicted_difference, 23); |
|
| 582 |
+} |
|
| 583 |
+ |
|
| 584 |
+static void aptx_process_subband(InvertQuantize *invert_quantize, |
|
| 585 |
+ Prediction *prediction, |
|
| 586 |
+ int32_t quantized_sample, int32_t dither, |
|
| 587 |
+ ConstTables *tables) |
|
| 588 |
+{
|
|
| 589 |
+ int32_t sign, same_sign[2], weight[2], sw1, range; |
|
| 590 |
+ |
|
| 591 |
+ aptx_invert_quantization(invert_quantize, quantized_sample, dither, tables); |
|
| 592 |
+ |
|
| 593 |
+ sign = FFDIFFSIGN(invert_quantize->reconstructed_difference, |
|
| 594 |
+ -prediction->predicted_difference); |
|
| 595 |
+ same_sign[0] = sign * prediction->prev_sign[0]; |
|
| 596 |
+ same_sign[1] = sign * prediction->prev_sign[1]; |
|
| 597 |
+ prediction->prev_sign[0] = prediction->prev_sign[1]; |
|
| 598 |
+ prediction->prev_sign[1] = sign | 1; |
|
| 599 |
+ |
|
| 600 |
+ range = 0x100000; |
|
| 601 |
+ sw1 = rshift32(-same_sign[1] * prediction->s_weight[1], 1); |
|
| 602 |
+ sw1 = (av_clip(sw1, -range, range) & ~0xF) << 4; |
|
| 603 |
+ |
|
| 604 |
+ range = 0x300000; |
|
| 605 |
+ weight[0] = 254 * prediction->s_weight[0] + 0x800000*same_sign[0] + sw1; |
|
| 606 |
+ prediction->s_weight[0] = av_clip(rshift32(weight[0], 8), -range, range); |
|
| 607 |
+ |
|
| 608 |
+ range = 0x3C0000 - prediction->s_weight[0]; |
|
| 609 |
+ weight[1] = 255 * prediction->s_weight[1] + 0xC00000*same_sign[1]; |
|
| 610 |
+ prediction->s_weight[1] = av_clip(rshift32(weight[1], 8), -range, range); |
|
| 611 |
+ |
|
| 612 |
+ aptx_prediction_filtering(prediction, |
|
| 613 |
+ invert_quantize->reconstructed_difference, |
|
| 614 |
+ tables->prediction_order); |
|
| 615 |
+} |
|
| 616 |
+ |
|
| 617 |
+static void aptx_invert_quantize_and_prediction(Channel *channel) |
|
| 618 |
+{
|
|
| 619 |
+ int subband; |
|
| 620 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) |
|
| 621 |
+ aptx_process_subband(&channel->invert_quantize[subband], |
|
| 622 |
+ &channel->prediction[subband], |
|
| 623 |
+ channel->quantize[subband].quantized_sample, |
|
| 624 |
+ channel->dither[subband], |
|
| 625 |
+ &tables[subband]); |
|
| 626 |
+} |
|
| 627 |
+ |
|
| 628 |
+static int32_t aptx_quantized_parity(Channel *channel) |
|
| 629 |
+{
|
|
| 630 |
+ int32_t parity = channel->dither_parity; |
|
| 631 |
+ int subband; |
|
| 632 |
+ |
|
| 633 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) |
|
| 634 |
+ parity ^= channel->quantize[subband].quantized_sample; |
|
| 635 |
+ |
|
| 636 |
+ return parity & 1; |
|
| 637 |
+} |
|
| 638 |
+ |
|
| 639 |
+/* For each sample, ensure that the parity of all subbands of all channels |
|
| 640 |
+ * is 0 except once every 8 samples where the parity is forced to 1. */ |
|
| 641 |
+static int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx) |
|
| 642 |
+{
|
|
| 643 |
+ int32_t parity = aptx_quantized_parity(&channels[LEFT]) |
|
| 644 |
+ ^ aptx_quantized_parity(&channels[RIGHT]); |
|
| 645 |
+ |
|
| 646 |
+ int eighth = *idx == 7; |
|
| 647 |
+ *idx = (*idx + 1) & 7; |
|
| 648 |
+ |
|
| 649 |
+ return parity ^ eighth; |
|
| 650 |
+} |
|
| 651 |
+ |
|
| 652 |
+static void aptx_insert_sync(Channel channels[NB_CHANNELS], int32_t *idx) |
|
| 653 |
+{
|
|
| 654 |
+ if (aptx_check_parity(channels, idx)) {
|
|
| 655 |
+ int i; |
|
| 656 |
+ Channel *c; |
|
| 657 |
+ static const int map[] = { 1, 2, 0, 3 };
|
|
| 658 |
+ Quantize *min = &channels[NB_CHANNELS-1].quantize[map[0]]; |
|
| 659 |
+ for (c = &channels[NB_CHANNELS-1]; c >= channels; c--) |
|
| 660 |
+ for (i = 0; i < NB_SUBBANDS; i++) |
|
| 661 |
+ if (c->quantize[map[i]].error < min->error) |
|
| 662 |
+ min = &c->quantize[map[i]]; |
|
| 663 |
+ |
|
| 664 |
+ /* Forcing the desired parity is done by offsetting by 1 the quantized |
|
| 665 |
+ * sample from the subband featuring the smallest quantization error. */ |
|
| 666 |
+ min->quantized_sample = min->quantized_sample_parity_change; |
|
| 667 |
+ } |
|
| 668 |
+} |
|
| 669 |
+ |
|
| 670 |
+static uint16_t aptx_pack_codeword(Channel *channel) |
|
| 671 |
+{
|
|
| 672 |
+ int32_t parity = aptx_quantized_parity(channel); |
|
| 673 |
+ return (((channel->quantize[3].quantized_sample & 0x06) | parity) << 13) |
|
| 674 |
+ | (((channel->quantize[2].quantized_sample & 0x03) ) << 11) |
|
| 675 |
+ | (((channel->quantize[1].quantized_sample & 0x0F) ) << 7) |
|
| 676 |
+ | (((channel->quantize[0].quantized_sample & 0x7F) ) << 0); |
|
| 677 |
+} |
|
| 678 |
+ |
|
| 679 |
+static void aptx_unpack_codeword(Channel *channel, uint16_t codeword) |
|
| 680 |
+{
|
|
| 681 |
+ channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 7); |
|
| 682 |
+ channel->quantize[1].quantized_sample = sign_extend(codeword >> 7, 4); |
|
| 683 |
+ channel->quantize[2].quantized_sample = sign_extend(codeword >> 11, 2); |
|
| 684 |
+ channel->quantize[3].quantized_sample = sign_extend(codeword >> 13, 3); |
|
| 685 |
+ channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1) |
|
| 686 |
+ | aptx_quantized_parity(channel); |
|
| 687 |
+} |
|
| 688 |
+ |
|
| 689 |
+static void aptx_encode_samples(AptXContext *ctx, |
|
| 690 |
+ int32_t samples[NB_CHANNELS][4], |
|
| 691 |
+ uint8_t output[2*NB_CHANNELS]) |
|
| 692 |
+{
|
|
| 693 |
+ int channel; |
|
| 694 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) |
|
| 695 |
+ aptx_encode_channel(&ctx->channels[channel], samples[channel]); |
|
| 696 |
+ |
|
| 697 |
+ aptx_insert_sync(ctx->channels, &ctx->sync_idx); |
|
| 698 |
+ |
|
| 699 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) {
|
|
| 700 |
+ aptx_invert_quantize_and_prediction(&ctx->channels[channel]); |
|
| 701 |
+ AV_WB16(output + 2*channel, aptx_pack_codeword(&ctx->channels[channel])); |
|
| 702 |
+ } |
|
| 703 |
+} |
|
| 704 |
+ |
|
| 705 |
+static int aptx_decode_samples(AptXContext *ctx, |
|
| 706 |
+ const uint8_t input[2*NB_CHANNELS], |
|
| 707 |
+ int32_t samples[NB_CHANNELS][4]) |
|
| 708 |
+{
|
|
| 709 |
+ int channel, ret; |
|
| 710 |
+ |
|
| 711 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) {
|
|
| 712 |
+ uint16_t codeword; |
|
| 713 |
+ aptx_generate_dither(&ctx->channels[channel]); |
|
| 714 |
+ |
|
| 715 |
+ codeword = AV_RB16(input + 2*channel); |
|
| 716 |
+ aptx_unpack_codeword(&ctx->channels[channel], codeword); |
|
| 717 |
+ aptx_invert_quantize_and_prediction(&ctx->channels[channel]); |
|
| 718 |
+ } |
|
| 719 |
+ |
|
| 720 |
+ ret = aptx_check_parity(ctx->channels, &ctx->sync_idx); |
|
| 721 |
+ |
|
| 722 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) |
|
| 723 |
+ aptx_decode_channel(&ctx->channels[channel], samples[channel]); |
|
| 724 |
+ |
|
| 725 |
+ return ret; |
|
| 726 |
+} |
|
| 727 |
+ |
|
| 728 |
+ |
|
| 729 |
+static av_cold int aptx_init(AVCodecContext *avctx) |
|
| 730 |
+{
|
|
| 731 |
+ AptXContext *s = avctx->priv_data; |
|
| 732 |
+ int chan, subband; |
|
| 733 |
+ |
|
| 734 |
+ if (avctx->frame_size == 0) |
|
| 735 |
+ avctx->frame_size = 1024; |
|
| 736 |
+ |
|
| 737 |
+ if (avctx->frame_size & 3) {
|
|
| 738 |
+ av_log(avctx, AV_LOG_ERROR, "Frame size must be a multiple of 4 samples\n"); |
|
| 739 |
+ return AVERROR(EINVAL); |
|
| 740 |
+ } |
|
| 741 |
+ |
|
| 742 |
+ for (chan = 0; chan < NB_CHANNELS; chan++) {
|
|
| 743 |
+ Channel *channel = &s->channels[chan]; |
|
| 744 |
+ for (subband = 0; subband < NB_SUBBANDS; subband++) {
|
|
| 745 |
+ Prediction *prediction = &channel->prediction[subband]; |
|
| 746 |
+ prediction->prev_sign[0] = 1; |
|
| 747 |
+ prediction->prev_sign[1] = 1; |
|
| 748 |
+ } |
|
| 749 |
+ } |
|
| 750 |
+ |
|
| 751 |
+ ff_af_queue_init(avctx, &s->afq); |
|
| 752 |
+ return 0; |
|
| 753 |
+} |
|
| 754 |
+ |
|
| 755 |
+static int aptx_decode_frame(AVCodecContext *avctx, void *data, |
|
| 756 |
+ int *got_frame_ptr, AVPacket *avpkt) |
|
| 757 |
+{
|
|
| 758 |
+ AptXContext *s = avctx->priv_data; |
|
| 759 |
+ AVFrame *frame = data; |
|
| 760 |
+ int pos, channel, sample, ret; |
|
| 761 |
+ |
|
| 762 |
+ if (avpkt->size < 4) {
|
|
| 763 |
+ av_log(avctx, AV_LOG_ERROR, "Packet is too small\n"); |
|
| 764 |
+ return AVERROR_INVALIDDATA; |
|
| 765 |
+ } |
|
| 766 |
+ |
|
| 767 |
+ /* get output buffer */ |
|
| 768 |
+ frame->channels = NB_CHANNELS; |
|
| 769 |
+ frame->format = AV_SAMPLE_FMT_S32P; |
|
| 770 |
+ frame->nb_samples = avpkt->size & ~3; |
|
| 771 |
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
|
| 772 |
+ return ret; |
|
| 773 |
+ |
|
| 774 |
+ for (pos = 0; pos < frame->nb_samples; pos += 4) {
|
|
| 775 |
+ int32_t samples[NB_CHANNELS][4]; |
|
| 776 |
+ |
|
| 777 |
+ if (aptx_decode_samples(s, &avpkt->data[pos], samples)) {
|
|
| 778 |
+ av_log(avctx, AV_LOG_ERROR, "Synchronization error\n"); |
|
| 779 |
+ return AVERROR_INVALIDDATA; |
|
| 780 |
+ } |
|
| 781 |
+ |
|
| 782 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) |
|
| 783 |
+ for (sample = 0; sample < 4; sample++) |
|
| 784 |
+ AV_WN32A(&frame->data[channel][4*(sample+pos)], |
|
| 785 |
+ samples[channel][sample] << 8); |
|
| 786 |
+ } |
|
| 787 |
+ |
|
| 788 |
+ *got_frame_ptr = 1; |
|
| 789 |
+ return frame->nb_samples; |
|
| 790 |
+} |
|
| 791 |
+ |
|
| 792 |
+static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
|
| 793 |
+ const AVFrame *frame, int *got_packet_ptr) |
|
| 794 |
+{
|
|
| 795 |
+ AptXContext *s = avctx->priv_data; |
|
| 796 |
+ int pos, channel, sample, ret; |
|
| 797 |
+ |
|
| 798 |
+ if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) |
|
| 799 |
+ return ret; |
|
| 800 |
+ |
|
| 801 |
+ if ((ret = ff_alloc_packet2(avctx, avpkt, frame->nb_samples, 0)) < 0) |
|
| 802 |
+ return ret; |
|
| 803 |
+ |
|
| 804 |
+ for (pos = 0; pos < frame->nb_samples; pos += 4) {
|
|
| 805 |
+ int32_t samples[NB_CHANNELS][4]; |
|
| 806 |
+ |
|
| 807 |
+ for (channel = 0; channel < NB_CHANNELS; channel++) |
|
| 808 |
+ for (sample = 0; sample < 4; sample++) |
|
| 809 |
+ samples[channel][sample] = (int32_t)AV_RN32A(&frame->data[channel][4*(sample+pos)]) >> 8; |
|
| 810 |
+ |
|
| 811 |
+ aptx_encode_samples(s, samples, avpkt->data + pos); |
|
| 812 |
+ } |
|
| 813 |
+ |
|
| 814 |
+ ff_af_queue_remove(&s->afq, frame->nb_samples, &avpkt->pts, &avpkt->duration); |
|
| 815 |
+ *got_packet_ptr = 1; |
|
| 816 |
+ return 0; |
|
| 817 |
+} |
|
| 818 |
+ |
|
| 819 |
+static av_cold int aptx_close(AVCodecContext *avctx) |
|
| 820 |
+{
|
|
| 821 |
+ AptXContext *s = avctx->priv_data; |
|
| 822 |
+ ff_af_queue_close(&s->afq); |
|
| 823 |
+ return 0; |
|
| 824 |
+} |
|
| 825 |
+ |
|
| 826 |
+ |
|
| 827 |
+#if CONFIG_APTX_DECODER |
|
| 828 |
+AVCodec ff_aptx_decoder = {
|
|
| 829 |
+ .name = "aptx", |
|
| 830 |
+ .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
|
|
| 831 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
| 832 |
+ .id = AV_CODEC_ID_APTX, |
|
| 833 |
+ .priv_data_size = sizeof(AptXContext), |
|
| 834 |
+ .init = aptx_init, |
|
| 835 |
+ .decode = aptx_decode_frame, |
|
| 836 |
+ .close = aptx_close, |
|
| 837 |
+ .capabilities = AV_CODEC_CAP_DR1, |
|
| 838 |
+ .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
|
|
| 839 |
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
|
|
| 840 |
+ AV_SAMPLE_FMT_NONE }, |
|
| 841 |
+}; |
|
| 842 |
+#endif |
|
| 843 |
+ |
|
| 844 |
+#if CONFIG_APTX_ENCODER |
|
| 845 |
+AVCodec ff_aptx_encoder = {
|
|
| 846 |
+ .name = "aptx", |
|
| 847 |
+ .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
|
|
| 848 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
| 849 |
+ .id = AV_CODEC_ID_APTX, |
|
| 850 |
+ .priv_data_size = sizeof(AptXContext), |
|
| 851 |
+ .init = aptx_init, |
|
| 852 |
+ .encode2 = aptx_encode_frame, |
|
| 853 |
+ .close = aptx_close, |
|
| 854 |
+ .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
|
|
| 855 |
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
|
|
| 856 |
+ AV_SAMPLE_FMT_NONE }, |
|
| 857 |
+ .supported_samplerates = (const int[]) {8000, 16000, 24000, 32000, 44100, 48000, 0},
|
|
| 858 |
+}; |
|
| 859 |
+#endif |
| ... | ... |
@@ -632,6 +632,7 @@ enum AVCodecID {
|
| 632 | 632 |
AV_CODEC_ID_ATRAC3AL, |
| 633 | 633 |
AV_CODEC_ID_ATRAC3PAL, |
| 634 | 634 |
AV_CODEC_ID_DOLBY_E, |
| 635 |
+ AV_CODEC_ID_APTX, |
|
| 635 | 636 |
|
| 636 | 637 |
/* subtitle codecs */ |
| 637 | 638 |
AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. |
| ... | ... |
@@ -2859,6 +2859,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
|
| 2859 | 2859 |
.long_name = NULL_IF_CONFIG_SMALL("ADPCM MTAF"),
|
| 2860 | 2860 |
.props = AV_CODEC_PROP_LOSSY, |
| 2861 | 2861 |
}, |
| 2862 |
+ {
|
|
| 2863 |
+ .id = AV_CODEC_ID_APTX, |
|
| 2864 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
| 2865 |
+ .name = "aptx", |
|
| 2866 |
+ .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
|
|
| 2867 |
+ .props = AV_CODEC_PROP_LOSSY, |
|
| 2868 |
+ }, |
|
| 2862 | 2869 |
|
| 2863 | 2870 |
/* subtitle codecs */ |
| 2864 | 2871 |
{
|