FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aptxenc.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 0 97 0.0%
Branches: 0 40 0.0%

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