aptx.h 6.5 KB

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