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