1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bluetooth-a2dp"
18
19 #include "a2dp_vendor_aptx_hd_encoder.h"
20
21 #include <bluetooth/log.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <cstdint>
27
28 #include "a2dp_codec_api.h"
29 #include "a2dp_vendor.h"
30 #include "a2dp_vendor_aptx_hd.h"
31 #include "aptXHDbtenc.h"
32 #include "avdt_api.h"
33 #include "common/time_util.h"
34 #include "internal_include/bt_target.h"
35 #include "osi/include/allocator.h"
36 #include "stack/include/bt_hdr.h"
37
38 using namespace bluetooth;
39
40 //
41 // Encoder for aptX-HD Source Codec
42 //
43
44 static const tAPTX_HD_API aptx_hd_api = {
45 .init_func = aptxhdbtenc_init,
46 .encode_stereo_func = aptxhdbtenc_encodestereo,
47 .sizeof_params_func = SizeofAptxhdbtenc,
48 };
49
50 // offset
51 #define A2DP_APTX_HD_OFFSET AVDT_MEDIA_OFFSET
52
53 #define A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ 4096
54
55 typedef struct {
56 uint64_t sleep_time_ns;
57 uint32_t pcm_reads;
58 uint32_t pcm_bytes_per_read;
59 uint32_t aptx_hd_bytes;
60 uint32_t frame_size_counter;
61 } tAPTX_HD_FRAMING_PARAMS;
62
63 typedef struct {
64 uint64_t session_start_us;
65
66 size_t media_read_total_expected_packets;
67 size_t media_read_total_expected_reads_count;
68 size_t media_read_total_expected_read_bytes;
69
70 size_t media_read_total_dropped_packets;
71 size_t media_read_total_actual_reads_count;
72 size_t media_read_total_actual_read_bytes;
73 } a2dp_aptx_hd_encoder_stats_t;
74
75 typedef struct {
76 a2dp_source_read_callback_t read_callback;
77 a2dp_source_enqueue_callback_t enqueue_callback;
78
79 bool use_SCMS_T;
80 tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
81 uint32_t timestamp; // Timestamp for the A2DP frames
82
83 tA2DP_FEEDING_PARAMS feeding_params;
84 tAPTX_HD_FRAMING_PARAMS framing_params;
85 void* aptx_hd_encoder_state;
86 a2dp_aptx_hd_encoder_stats_t stats;
87 } tA2DP_APTX_HD_ENCODER_CB;
88
89 static tA2DP_APTX_HD_ENCODER_CB a2dp_aptx_hd_encoder_cb;
90
91 static void a2dp_vendor_aptx_hd_encoder_update(A2dpCodecConfig* a2dp_codec_config,
92 bool* p_restart_input, bool* p_restart_output,
93 bool* p_config_updated);
94 static void aptx_hd_init_framing_params(tAPTX_HD_FRAMING_PARAMS* framing_params);
95 static void aptx_hd_update_framing_params(tAPTX_HD_FRAMING_PARAMS* framing_params);
96 static size_t aptx_hd_encode_24bit(tAPTX_HD_FRAMING_PARAMS* framing_params, size_t* data_out_index,
97 uint32_t* data32_in, uint8_t* data_out);
98
99 /*******************************************************************************
100 *
101 * Function A2DP_VendorLoadEncoderAptxHd
102 *
103 * Description This function will try to load the aptx HD encoder library.
104 *
105 * Returns LOAD_SUCCESS on success
106 * LOAD_ERROR_MISSING_CODEC on missing library
107 * LOAD_ERROR_VERSION_MISMATCH on symbol loading error
108 *
109 ******************************************************************************/
A2DP_VendorLoadEncoderAptxHd(void)110 tLOADING_CODEC_STATUS A2DP_VendorLoadEncoderAptxHd(void) {
111 // Nothing to do - the library is statically linked
112 return LOAD_SUCCESS;
113 }
114
A2DP_VendorCopyAptxHdApi(tAPTX_HD_API & external_api)115 bool A2DP_VendorCopyAptxHdApi(tAPTX_HD_API& external_api) {
116 external_api = aptx_hd_api;
117 return true;
118 }
119
A2DP_VendorUnloadEncoderAptxHd(void)120 void A2DP_VendorUnloadEncoderAptxHd(void) {
121 // nothing to do
122 }
123
a2dp_vendor_aptx_hd_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS * p_peer_params,A2dpCodecConfig * a2dp_codec_config,a2dp_source_read_callback_t read_callback,a2dp_source_enqueue_callback_t enqueue_callback)124 void a2dp_vendor_aptx_hd_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
125 A2dpCodecConfig* a2dp_codec_config,
126 a2dp_source_read_callback_t read_callback,
127 a2dp_source_enqueue_callback_t enqueue_callback) {
128 memset(&a2dp_aptx_hd_encoder_cb, 0, sizeof(a2dp_aptx_hd_encoder_cb));
129
130 a2dp_aptx_hd_encoder_cb.stats.session_start_us = bluetooth::common::time_get_os_boottime_us();
131
132 a2dp_aptx_hd_encoder_cb.read_callback = read_callback;
133 a2dp_aptx_hd_encoder_cb.enqueue_callback = enqueue_callback;
134 a2dp_aptx_hd_encoder_cb.peer_params = *p_peer_params;
135 a2dp_aptx_hd_encoder_cb.timestamp = 0;
136
137 /* aptX-HD encoder config */
138 a2dp_aptx_hd_encoder_cb.use_SCMS_T = false;
139
140 a2dp_aptx_hd_encoder_cb.aptx_hd_encoder_state = osi_malloc(aptx_hd_api.sizeof_params_func());
141 if (a2dp_aptx_hd_encoder_cb.aptx_hd_encoder_state != NULL) {
142 aptx_hd_api.init_func(a2dp_aptx_hd_encoder_cb.aptx_hd_encoder_state, 0);
143 } else {
144 log::error("Cannot allocate aptX-HD encoder state");
145 // TODO: Return an error?
146 }
147
148 // NOTE: Ignore the restart_input / restart_output flags - this initization
149 // happens when the audio session is (re)started.
150 bool restart_input = false;
151 bool restart_output = false;
152 bool config_updated = false;
153 a2dp_vendor_aptx_hd_encoder_update(a2dp_codec_config, &restart_input, &restart_output,
154 &config_updated);
155 }
156
157 // Update the A2DP aptX-HD encoder.
158 // |a2dp_codec_config| is the A2DP codec to use for the update.
a2dp_vendor_aptx_hd_encoder_update(A2dpCodecConfig * a2dp_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)159 static void a2dp_vendor_aptx_hd_encoder_update(A2dpCodecConfig* a2dp_codec_config,
160 bool* p_restart_input, bool* p_restart_output,
161 bool* p_config_updated) {
162 uint8_t codec_info[AVDT_CODEC_SIZE];
163
164 *p_restart_input = false;
165 *p_restart_output = false;
166 *p_config_updated = false;
167 if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) {
168 log::error("Cannot update the codec encoder for {}: invalid codec config",
169 a2dp_codec_config->name());
170 return;
171 }
172 const uint8_t* p_codec_info = codec_info;
173
174 // The feeding parameters
175 tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_aptx_hd_encoder_cb.feeding_params;
176 p_feeding_params->sample_rate = A2DP_VendorGetTrackSampleRateAptxHd(p_codec_info);
177 p_feeding_params->bits_per_sample = a2dp_codec_config->getAudioBitsPerSample();
178 p_feeding_params->channel_count = A2DP_VendorGetTrackChannelCountAptxHd(p_codec_info);
179 log::info("sample_rate={} bits_per_sample={} channel_count={}", p_feeding_params->sample_rate,
180 p_feeding_params->bits_per_sample, p_feeding_params->channel_count);
181 a2dp_vendor_aptx_hd_feeding_reset();
182 }
183
a2dp_vendor_aptx_hd_encoder_cleanup(void)184 void a2dp_vendor_aptx_hd_encoder_cleanup(void) {
185 osi_free(a2dp_aptx_hd_encoder_cb.aptx_hd_encoder_state);
186 memset(&a2dp_aptx_hd_encoder_cb, 0, sizeof(a2dp_aptx_hd_encoder_cb));
187 }
188
189 //
190 // Initialize the framing parameters, and set those that don't change
191 // while streaming (e.g., 'sleep_time_ns').
192 //
aptx_hd_init_framing_params(tAPTX_HD_FRAMING_PARAMS * framing_params)193 static void aptx_hd_init_framing_params(tAPTX_HD_FRAMING_PARAMS* framing_params) {
194 framing_params->sleep_time_ns = 0;
195 framing_params->pcm_reads = 0;
196 framing_params->pcm_bytes_per_read = 0;
197 framing_params->aptx_hd_bytes = 0;
198 framing_params->frame_size_counter = 0;
199
200 framing_params->sleep_time_ns = 9000000;
201
202 log::info("sleep_time_ns={}", framing_params->sleep_time_ns);
203 }
204
205 //
206 // Set frame size and transmission interval needed to stream the required
207 // sample rate using 2-DH5 packets for aptX and 2-DH3 packets for aptX-LL.
208 // With SCMS-T enabled we need to reserve room for extra headers added later.
209 // Packets are always sent at equals time intervals but to achieve the
210 // required sample rate, the frame size needs to change on occasion.
211 //
212 // Also need to specify how many of the required PCM samples are read at a
213 // time:
214 // aptx_bytes = pcm_reads * pcm_bytes_per_read / 4
215 // and
216 // number of aptX samples produced = pcm_bytes_per_read / 16
217 //
aptx_hd_update_framing_params(tAPTX_HD_FRAMING_PARAMS * framing_params)218 static void aptx_hd_update_framing_params(tAPTX_HD_FRAMING_PARAMS* framing_params) {
219 if (a2dp_aptx_hd_encoder_cb.feeding_params.sample_rate == 48000) {
220 framing_params->aptx_hd_bytes = 648;
221 framing_params->pcm_bytes_per_read = 24;
222 framing_params->pcm_reads = 108;
223 } else {
224 // Assume the sample rate is 44100
225
226 //
227 // Total of 80 iterations:
228 // - Iteration 80: packet size 648, with 108 reads of 24 PCM bytes
229 // - Iterations 20, 40, 60: packet size 612, with 102 reads of 24 PCM bytes
230 // - All other iterations: packet size 594, with 99 reads of 24 PCM bytes
231 //
232 if (framing_params->frame_size_counter + 1 == 80) {
233 framing_params->aptx_hd_bytes = 648;
234 framing_params->pcm_bytes_per_read = 24;
235 framing_params->pcm_reads = 108;
236 } else if (((framing_params->frame_size_counter + 1) % 20) == 0) {
237 framing_params->aptx_hd_bytes = 612;
238 framing_params->pcm_bytes_per_read = 24;
239 framing_params->pcm_reads = 102;
240 } else {
241 framing_params->aptx_hd_bytes = 594;
242 framing_params->pcm_bytes_per_read = 24;
243 framing_params->pcm_reads = 99;
244 }
245 framing_params->frame_size_counter++;
246 if (framing_params->frame_size_counter == 80) {
247 framing_params->frame_size_counter = 0;
248 }
249 }
250
251 log::verbose(
252 "sleep_time_ns={} aptx_hd_bytes={} pcm_bytes_per_read={} pcm_reads={} "
253 "frame_size_counter={}",
254 framing_params->sleep_time_ns, framing_params->aptx_hd_bytes,
255 framing_params->pcm_bytes_per_read, framing_params->pcm_reads,
256 framing_params->frame_size_counter);
257 }
258
a2dp_vendor_aptx_hd_feeding_reset(void)259 void a2dp_vendor_aptx_hd_feeding_reset(void) {
260 aptx_hd_init_framing_params(&a2dp_aptx_hd_encoder_cb.framing_params);
261 }
262
a2dp_vendor_aptx_hd_feeding_flush(void)263 void a2dp_vendor_aptx_hd_feeding_flush(void) {
264 aptx_hd_init_framing_params(&a2dp_aptx_hd_encoder_cb.framing_params);
265 }
266
a2dp_vendor_aptx_hd_get_encoder_interval_ms(void)267 uint64_t a2dp_vendor_aptx_hd_get_encoder_interval_ms(void) {
268 return a2dp_aptx_hd_encoder_cb.framing_params.sleep_time_ns / (1000 * 1000);
269 }
270
a2dp_vendor_aptx_hd_get_effective_frame_size()271 int a2dp_vendor_aptx_hd_get_effective_frame_size() {
272 return a2dp_aptx_hd_encoder_cb.peer_params.peer_mtu;
273 }
274
a2dp_vendor_aptx_hd_send_frames(uint64_t)275 void a2dp_vendor_aptx_hd_send_frames(uint64_t /* timestamp_us */) {
276 tAPTX_HD_FRAMING_PARAMS* framing_params = &a2dp_aptx_hd_encoder_cb.framing_params;
277
278 // Prepare the packet to send
279 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
280 p_buf->offset = A2DP_APTX_HD_OFFSET;
281 p_buf->len = 0;
282 p_buf->layer_specific = 0;
283
284 uint8_t* encoded_ptr = (uint8_t*)(p_buf + 1);
285 encoded_ptr += p_buf->offset;
286
287 aptx_hd_update_framing_params(framing_params);
288
289 //
290 // Read the PCM data and encode it
291 //
292 uint32_t read_buffer32[A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ / sizeof(uint32_t)];
293 uint32_t expected_read_bytes = framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
294 size_t encoded_ptr_index = 0;
295 size_t pcm_bytes_encoded = 0;
296 uint32_t bytes_read = 0;
297
298 a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_packets++;
299 a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_reads_count++;
300 a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_read_bytes += expected_read_bytes;
301
302 log::verbose("PCM read of size {}", expected_read_bytes);
303 bytes_read = a2dp_aptx_hd_encoder_cb.read_callback((uint8_t*)read_buffer32, expected_read_bytes);
304 a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_read_bytes += bytes_read;
305 if (bytes_read < expected_read_bytes) {
306 log::warn("underflow at PCM reading: read {} bytes instead of {}", bytes_read,
307 expected_read_bytes);
308 a2dp_aptx_hd_encoder_cb.stats.media_read_total_dropped_packets++;
309 osi_free(p_buf);
310 return;
311 }
312 a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_reads_count++;
313
314 for (uint32_t reads = 0, offset = 0; reads < framing_params->pcm_reads;
315 reads++, offset += framing_params->pcm_bytes_per_read / sizeof(uint32_t)) {
316 pcm_bytes_encoded += aptx_hd_encode_24bit(framing_params, &encoded_ptr_index,
317 read_buffer32 + offset, encoded_ptr);
318 }
319
320 // Compute the number of encoded bytes
321 const int COMPRESSION_RATIO = 4;
322 size_t encoded_bytes = pcm_bytes_encoded / COMPRESSION_RATIO;
323 p_buf->len += encoded_bytes;
324 log::verbose("encoded {} PCM bytes to {}", pcm_bytes_encoded, encoded_bytes);
325
326 // Update the RTP timestamp
327 *((uint32_t*)(p_buf + 1)) = a2dp_aptx_hd_encoder_cb.timestamp;
328 const uint8_t BYTES_PER_FRAME = 3;
329 uint32_t rtp_timestamp =
330 (pcm_bytes_encoded / a2dp_aptx_hd_encoder_cb.feeding_params.channel_count) /
331 BYTES_PER_FRAME;
332
333 // Timestamp will wrap over to 0 if stream continues on long enough
334 // (>25H @ 48KHz). The parameters are promoted to 64bit to ensure that
335 // no unsigned overflow is triggered as ubsan is always enabled.
336 a2dp_aptx_hd_encoder_cb.timestamp =
337 ((uint64_t)a2dp_aptx_hd_encoder_cb.timestamp + rtp_timestamp) & UINT32_MAX;
338
339 if (p_buf->len > 0) {
340 a2dp_aptx_hd_encoder_cb.enqueue_callback(p_buf, 1, bytes_read);
341 } else {
342 a2dp_aptx_hd_encoder_cb.stats.media_read_total_dropped_packets++;
343 osi_free(p_buf);
344 }
345 }
346
aptx_hd_encode_24bit(tAPTX_HD_FRAMING_PARAMS * framing_params,size_t * data_out_index,uint32_t * data32_in,uint8_t * data_out)347 static size_t aptx_hd_encode_24bit(tAPTX_HD_FRAMING_PARAMS* framing_params, size_t* data_out_index,
348 uint32_t* data32_in, uint8_t* data_out) {
349 size_t pcm_bytes_encoded = 0;
350 const uint8_t* p = (const uint8_t*)(data32_in);
351
352 for (size_t aptx_hd_samples = 0; aptx_hd_samples < framing_params->pcm_bytes_per_read / 24;
353 aptx_hd_samples++) {
354 uint32_t pcmL[4];
355 uint32_t pcmR[4];
356 uint32_t encoded_sample[2];
357
358 // Expand from AUDIO_FORMAT_PCM_24_BIT_PACKED data (3 bytes per sample)
359 // into AUDIO_FORMAT_PCM_8_24_BIT (4 bytes per sample).
360 for (size_t i = 0; i < 4; i++) {
361 pcmL[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
362 p += 3;
363 pcmR[i] = ((p[0] << 0) | (p[1] << 8) | (((int8_t)p[2]) << 16));
364 p += 3;
365 }
366
367 aptx_hd_api.encode_stereo_func(a2dp_aptx_hd_encoder_cb.aptx_hd_encoder_state, &pcmL, &pcmR,
368 &encoded_sample);
369
370 uint8_t* encoded_ptr = (uint8_t*)&encoded_sample[0];
371 data_out[*data_out_index + 0] = *(encoded_ptr + 2);
372 data_out[*data_out_index + 1] = *(encoded_ptr + 1);
373 data_out[*data_out_index + 2] = *(encoded_ptr + 0);
374 data_out[*data_out_index + 3] = *(encoded_ptr + 6);
375 data_out[*data_out_index + 4] = *(encoded_ptr + 5);
376 data_out[*data_out_index + 5] = *(encoded_ptr + 4);
377
378 pcm_bytes_encoded += 24;
379 *data_out_index += 6;
380 }
381
382 return pcm_bytes_encoded;
383 }
384
debug_codec_dump(int fd)385 void A2dpCodecConfigAptxHd::debug_codec_dump(int fd) {
386 a2dp_aptx_hd_encoder_stats_t* stats = &a2dp_aptx_hd_encoder_cb.stats;
387
388 A2dpCodecConfig::debug_codec_dump(fd);
389
390 dprintf(fd, " Encoder interval (ms): %" PRIu64 "\n",
391 a2dp_vendor_aptx_hd_get_encoder_interval_ms());
392 dprintf(fd, " Effective MTU: %d\n", a2dp_vendor_aptx_hd_get_effective_frame_size());
393 dprintf(fd, " Packet counts (expected/dropped) : %zu / %zu\n",
394 stats->media_read_total_expected_packets, stats->media_read_total_dropped_packets);
395
396 dprintf(fd, " PCM read counts (expected/actual) : %zu / %zu\n",
397 stats->media_read_total_expected_reads_count, stats->media_read_total_actual_reads_count);
398
399 dprintf(fd, " PCM read bytes (expected/actual) : %zu / %zu\n",
400 stats->media_read_total_expected_read_bytes, stats->media_read_total_actual_read_bytes);
401 }
402