You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
11KB

  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. #include "aptx.h"
  23. /*
  24. * Half-band QMF analysis filter realized with a polyphase FIR filter.
  25. * Split into 2 subbands and downsample by 2.
  26. * So for each pair of samples that goes in, one sample goes out,
  27. * split into 2 separate subbands.
  28. */
  29. av_always_inline
  30. static void aptx_qmf_polyphase_analysis(FilterSignal signal[NB_FILTERS],
  31. const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
  32. int shift,
  33. int32_t samples[NB_FILTERS],
  34. int32_t *low_subband_output,
  35. int32_t *high_subband_output)
  36. {
  37. int32_t subbands[NB_FILTERS];
  38. int i;
  39. for (i = 0; i < NB_FILTERS; i++) {
  40. aptx_qmf_filter_signal_push(&signal[i], samples[NB_FILTERS-1-i]);
  41. subbands[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
  42. }
  43. *low_subband_output = av_clip_intp2(subbands[0] + subbands[1], 23);
  44. *high_subband_output = av_clip_intp2(subbands[0] - subbands[1], 23);
  45. }
  46. /*
  47. * Two stage QMF analysis tree.
  48. * Split 4 input samples into 4 subbands and downsample by 4.
  49. * So for each group of 4 samples that goes in, one sample goes out,
  50. * split into 4 separate subbands.
  51. */
  52. static void aptx_qmf_tree_analysis(QMFAnalysis *qmf,
  53. int32_t samples[4],
  54. int32_t subband_samples[4])
  55. {
  56. int32_t intermediate_samples[4];
  57. int i;
  58. /* Split 4 input samples into 2 intermediate subbands downsampled to 2 samples */
  59. for (i = 0; i < 2; i++)
  60. aptx_qmf_polyphase_analysis(qmf->outer_filter_signal,
  61. aptx_qmf_outer_coeffs, 23,
  62. &samples[2*i],
  63. &intermediate_samples[0+i],
  64. &intermediate_samples[2+i]);
  65. /* Split 2 intermediate subband samples into 4 final subbands downsampled to 1 sample */
  66. for (i = 0; i < 2; i++)
  67. aptx_qmf_polyphase_analysis(qmf->inner_filter_signal[i],
  68. aptx_qmf_inner_coeffs, 23,
  69. &intermediate_samples[2*i],
  70. &subband_samples[2*i+0],
  71. &subband_samples[2*i+1]);
  72. }
  73. av_always_inline
  74. static int32_t aptx_bin_search(int32_t value, int32_t factor,
  75. const int32_t *intervals, int32_t nb_intervals)
  76. {
  77. int32_t idx = 0;
  78. int i;
  79. for (i = nb_intervals >> 1; i > 0; i >>= 1)
  80. if (MUL64(factor, intervals[idx + i]) <= ((int64_t)value << 24))
  81. idx += i;
  82. return idx;
  83. }
  84. static void aptx_quantize_difference(Quantize *quantize,
  85. int32_t sample_difference,
  86. int32_t dither,
  87. int32_t quantization_factor,
  88. ConstTables *tables)
  89. {
  90. const int32_t *intervals = tables->quantize_intervals;
  91. int32_t quantized_sample, dithered_sample, parity_change;
  92. int32_t d, mean, interval, inv, sample_difference_abs;
  93. int64_t error;
  94. sample_difference_abs = FFABS(sample_difference);
  95. sample_difference_abs = FFMIN(sample_difference_abs, (1 << 23) - 1);
  96. quantized_sample = aptx_bin_search(sample_difference_abs >> 4,
  97. quantization_factor,
  98. intervals, tables->tables_size);
  99. d = rshift32_clip24(MULH(dither, dither), 7) - (1 << 23);
  100. d = rshift64(MUL64(d, tables->quantize_dither_factors[quantized_sample]), 23);
  101. intervals += quantized_sample;
  102. mean = (intervals[1] + intervals[0]) / 2;
  103. interval = (intervals[1] - intervals[0]) * (-(sample_difference < 0) | 1);
  104. dithered_sample = rshift64_clip24(MUL64(dither, interval) + ((int64_t)av_clip_intp2(mean + d, 23) << 32), 32);
  105. error = ((int64_t)sample_difference_abs << 20) - MUL64(dithered_sample, quantization_factor);
  106. quantize->error = FFABS(rshift64(error, 23));
  107. parity_change = quantized_sample;
  108. if (error < 0)
  109. quantized_sample--;
  110. else
  111. parity_change--;
  112. inv = -(sample_difference < 0);
  113. quantize->quantized_sample = quantized_sample ^ inv;
  114. quantize->quantized_sample_parity_change = parity_change ^ inv;
  115. }
  116. static void aptx_encode_channel(Channel *channel, int32_t samples[4], int hd)
  117. {
  118. int32_t subband_samples[4];
  119. int subband;
  120. aptx_qmf_tree_analysis(&channel->qmf, samples, subband_samples);
  121. ff_aptx_generate_dither(channel);
  122. for (subband = 0; subband < NB_SUBBANDS; subband++) {
  123. int32_t diff = av_clip_intp2(subband_samples[subband] - channel->prediction[subband].predicted_sample, 23);
  124. aptx_quantize_difference(&channel->quantize[subband], diff,
  125. channel->dither[subband],
  126. channel->invert_quantize[subband].quantization_factor,
  127. &ff_aptx_quant_tables[hd][subband]);
  128. }
  129. }
  130. static void aptx_insert_sync(Channel channels[NB_CHANNELS], int32_t *idx)
  131. {
  132. if (aptx_check_parity(channels, idx)) {
  133. int i;
  134. Channel *c;
  135. static const int map[] = { 1, 2, 0, 3 };
  136. Quantize *min = &channels[NB_CHANNELS-1].quantize[map[0]];
  137. for (c = &channels[NB_CHANNELS-1]; c >= channels; c--)
  138. for (i = 0; i < NB_SUBBANDS; i++)
  139. if (c->quantize[map[i]].error < min->error)
  140. min = &c->quantize[map[i]];
  141. /* Forcing the desired parity is done by offsetting by 1 the quantized
  142. * sample from the subband featuring the smallest quantization error. */
  143. min->quantized_sample = min->quantized_sample_parity_change;
  144. }
  145. }
  146. static uint16_t aptx_pack_codeword(Channel *channel)
  147. {
  148. int32_t parity = aptx_quantized_parity(channel);
  149. return (((channel->quantize[3].quantized_sample & 0x06) | parity) << 13)
  150. | (((channel->quantize[2].quantized_sample & 0x03) ) << 11)
  151. | (((channel->quantize[1].quantized_sample & 0x0F) ) << 7)
  152. | (((channel->quantize[0].quantized_sample & 0x7F) ) << 0);
  153. }
  154. static uint32_t aptxhd_pack_codeword(Channel *channel)
  155. {
  156. int32_t parity = aptx_quantized_parity(channel);
  157. return (((channel->quantize[3].quantized_sample & 0x01E) | parity) << 19)
  158. | (((channel->quantize[2].quantized_sample & 0x00F) ) << 15)
  159. | (((channel->quantize[1].quantized_sample & 0x03F) ) << 9)
  160. | (((channel->quantize[0].quantized_sample & 0x1FF) ) << 0);
  161. }
  162. static void aptx_encode_samples(AptXContext *ctx,
  163. int32_t samples[NB_CHANNELS][4],
  164. uint8_t *output)
  165. {
  166. int channel;
  167. for (channel = 0; channel < NB_CHANNELS; channel++)
  168. aptx_encode_channel(&ctx->channels[channel], samples[channel], ctx->hd);
  169. aptx_insert_sync(ctx->channels, &ctx->sync_idx);
  170. for (channel = 0; channel < NB_CHANNELS; channel++) {
  171. ff_aptx_invert_quantize_and_prediction(&ctx->channels[channel], ctx->hd);
  172. if (ctx->hd)
  173. AV_WB24(output + 3*channel,
  174. aptxhd_pack_codeword(&ctx->channels[channel]));
  175. else
  176. AV_WB16(output + 2*channel,
  177. aptx_pack_codeword(&ctx->channels[channel]));
  178. }
  179. }
  180. static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  181. const AVFrame *frame, int *got_packet_ptr)
  182. {
  183. AptXContext *s = avctx->priv_data;
  184. int pos, ipos, channel, sample, output_size, ret;
  185. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  186. return ret;
  187. output_size = s->block_size * frame->nb_samples/4;
  188. if ((ret = ff_alloc_packet2(avctx, avpkt, output_size, 0)) < 0)
  189. return ret;
  190. for (pos = 0, ipos = 0; pos < output_size; pos += s->block_size, ipos += 4) {
  191. int32_t samples[NB_CHANNELS][4];
  192. for (channel = 0; channel < NB_CHANNELS; channel++)
  193. for (sample = 0; sample < 4; sample++)
  194. samples[channel][sample] = (int32_t)AV_RN32A(&frame->data[channel][4*(ipos+sample)]) >> 8;
  195. aptx_encode_samples(s, samples, avpkt->data + pos);
  196. }
  197. ff_af_queue_remove(&s->afq, frame->nb_samples, &avpkt->pts, &avpkt->duration);
  198. *got_packet_ptr = 1;
  199. return 0;
  200. }
  201. static av_cold int aptx_close(AVCodecContext *avctx)
  202. {
  203. AptXContext *s = avctx->priv_data;
  204. ff_af_queue_close(&s->afq);
  205. return 0;
  206. }
  207. #if CONFIG_APTX_ENCODER
  208. AVCodec ff_aptx_encoder = {
  209. .name = "aptx",
  210. .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
  211. .type = AVMEDIA_TYPE_AUDIO,
  212. .id = AV_CODEC_ID_APTX,
  213. .priv_data_size = sizeof(AptXContext),
  214. .init = ff_aptx_init,
  215. .encode2 = aptx_encode_frame,
  216. .close = aptx_close,
  217. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  218. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  219. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  220. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  221. AV_SAMPLE_FMT_NONE },
  222. .supported_samplerates = (const int[]) {8000, 16000, 24000, 32000, 44100, 48000, 0},
  223. };
  224. #endif
  225. #if CONFIG_APTX_HD_ENCODER
  226. AVCodec ff_aptx_hd_encoder = {
  227. .name = "aptx_hd",
  228. .long_name = NULL_IF_CONFIG_SMALL("aptX HD (Audio Processing Technology for Bluetooth)"),
  229. .type = AVMEDIA_TYPE_AUDIO,
  230. .id = AV_CODEC_ID_APTX_HD,
  231. .priv_data_size = sizeof(AptXContext),
  232. .init = ff_aptx_init,
  233. .encode2 = aptx_encode_frame,
  234. .close = aptx_close,
  235. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  236. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  237. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  238. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  239. AV_SAMPLE_FMT_NONE },
  240. .supported_samplerates = (const int[]) {8000, 16000, 24000, 32000, 44100, 48000, 0},
  241. };
  242. #endif