blob: 63f4d92dd1f286646bed4ecaf7c3ca00d4721273 [file] [log] [blame]
Mingming Yin21854652016-04-13 11:54:02 -07001/*
Ben Romberger1aaaf862017-04-06 17:49:46 -07002* Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Mingming Yin21854652016-04-13 11:54:02 -07003*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#define LOG_TAG "passthru"
31/*#define LOG_NDEBUG 0*/
32#include <stdlib.h>
33#include <cutils/atomic.h>
34#include <cutils/str_parms.h>
35#include <cutils/log.h>
36#include "audio_hw.h"
37#include "audio_extn.h"
38#include "platform_api.h"
39#include <platform.h>
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053040#include <cutils/properties.h>
41
42#include "sound/compress_params.h"
Manish Dewangan37864bc2017-06-09 12:28:37 +053043
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053044#ifdef DYNAMIC_LOG_ENABLED
45#include <log_xml_parser.h>
46#define LOG_MASK HAL_MOD_FILE_PASSTH
47#include <log_utils.h>
48#endif
Manish Dewangan37864bc2017-06-09 12:28:37 +053049/*
50 * Offload buffer size for compress passthrough
51 */
52
53#ifdef DTSHD_PARSER_ENABLED
54#include "audio_parsers.h"
55
56/* list of all supported DTS transmission sample rates */
57static const int dts_transmission_sample_rates[] = {
58 44100, 48000, 88200, 96000, 176400, 192000
59};
60
61 /*
62 * for DTSHD stream one frame size can be upto 36kb and to extract iec61937
63 * info for parsing usecase minimum one frame needs to be sent to dts parser
64 */
65#define MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (36 * 1024)
66#else
67#define MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (8 * 1024)
68#endif
69
70#define MIN_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (2 * 1024)
Mingming Yin21854652016-04-13 11:54:02 -070071
72static const audio_format_t audio_passthru_formats[] = {
73 AUDIO_FORMAT_AC3,
74 AUDIO_FORMAT_E_AC3,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053075 AUDIO_FORMAT_E_AC3_JOC,
Mingming Yin21854652016-04-13 11:54:02 -070076 AUDIO_FORMAT_DTS,
Ben Romberger1aaaf862017-04-06 17:49:46 -070077 AUDIO_FORMAT_DTS_HD,
Naresh Tanniru928f0862017-04-07 16:44:23 -070078 AUDIO_FORMAT_DOLBY_TRUEHD,
79 AUDIO_FORMAT_IEC61937
Mingming Yin21854652016-04-13 11:54:02 -070080};
81
82/*
83 * This atomic var is incremented/decremented by the offload stream to notify
84 * other pcm playback streams that a pass thru session is about to start or has
85 * finished. This hint can be used by the other streams to move to standby or
86 * start calling pcm_write respectively.
87 * This behavior is necessary as the DSP backend can only be configured to one
88 * of PCM or compressed.
89 */
90static volatile int32_t compress_passthru_active;
91
Manish Dewangan37864bc2017-06-09 12:28:37 +053092#ifdef DTSHD_PARSER_ENABLED
93static void passthru_update_stream_configuration_from_dts_parser( struct stream_out *out,
94 const void *buffer, size_t bytes)
95{
96 struct audio_parser_codec_info codec_info;
97 struct dtshd_iec61937_info dtshd_tr_info;
98 int i;
99 int ret;
100 bool is_valid_transmission_rate = false;
101 bool is_valid_transmission_channels = false;
102
103 /* codec format is AUDIO_PARSER_CODEC_DTSHD for both DTS and DTSHD as
104 * DTSHD parser can support both DTS and DTSHD
105 */
106 memset(&codec_info, 0, sizeof(struct audio_parser_codec_info));
107 memset(&dtshd_tr_info, 0, sizeof(struct dtshd_iec61937_info));
108
109 init_audio_parser((unsigned char *)buffer, bytes, AUDIO_PARSER_CODEC_DTSHD);
110 codec_info.codec_type = AUDIO_PARSER_CODEC_DTSHD;
111 if (!(ret = get_iec61937_info(&codec_info))) {
112 dtshd_tr_info = codec_info.codec_config.dtshd_tr_info;
113 ALOGD("dts new sample rate %d and channels %d\n",
114 dtshd_tr_info.sample_rate,
115 dtshd_tr_info.num_channels);
116 for (i = 0; i < sizeof(dts_transmission_sample_rates); i++) {
117 if (dts_transmission_sample_rates[i] ==
118 dtshd_tr_info.sample_rate) {
119 out->sample_rate = dtshd_tr_info.sample_rate;
120 out->compr_config.codec->sample_rate = out->sample_rate;
121 is_valid_transmission_rate = true;
122 break;
123 }
124 }
125 /* DTS transmission channels should be 2 or 8*/
126 if ((dtshd_tr_info.num_channels == 2) ||
127 (dtshd_tr_info.num_channels == 8)) {
128 out->compr_config.codec->ch_in = dtshd_tr_info.num_channels;
129 out->channel_mask = audio_channel_out_mask_from_count
130 (dtshd_tr_info.num_channels);
131 is_valid_transmission_channels = true;
132 }
133 } else {
134 ALOGE("%s:: get_iec61937_info failed %d", __func__, ret);
135 }
136
137 if (!is_valid_transmission_rate) {
Harsh Bansal0ab6b182017-08-14 11:49:43 +0530138 ALOGE("%s:: Invalid dts transmission rate %d\n using default sample rate 48000",
Manish Dewangan37864bc2017-06-09 12:28:37 +0530139 dtshd_tr_info.sample_rate);
Harsh Bansal0ab6b182017-08-14 11:49:43 +0530140 out->sample_rate = 48000;
Manish Dewangan37864bc2017-06-09 12:28:37 +0530141 out->compr_config.codec->sample_rate = out->sample_rate;
142 }
143
144 if (!is_valid_transmission_channels) {
145 ALOGE("%s:: Invalid transmission channels %d using default transmission"
146 " channels as 2", __func__, dtshd_tr_info.num_channels);
147 out->compr_config.codec->ch_in = 2;
148 out->channel_mask = audio_channel_out_mask_from_count(2);
149 }
150}
151#else
152static void passthru_update_stream_configuration_from_dts_parser(
153 struct stream_out *out __unused,
154 const void *buffer __unused,
155 size_t bytes __unused)
156{
157 return;
158}
159#endif
160
161int audio_extn_passthru_get_channel_count(struct stream_out *out)
162{
163 int channel_count = DEFAULT_HDMI_OUT_CHANNELS;
164
165 if (!out) {
166 ALOGE("%s:: Invalid param out %p", __func__, out);
167 return -EINVAL;
168 }
169
170 if (!audio_extn_passthru_is_supported_format(out->format)) {
171 ALOGE("%s:: not a passthrough format %d", __func__, out->format);
172 return -EINVAL;
173 }
174
175 switch(out->format) {
176 case AUDIO_FORMAT_DOLBY_TRUEHD:
177 channel_count = 8;
178 break;
179 case AUDIO_FORMAT_DTS:
180 case AUDIO_FORMAT_DTS_HD:
181#ifdef DTSHD_PARSER_ENABLED
182 /* taken channel count from parser*/
183 channel_count = audio_channel_count_from_out_mask(out->channel_mask);
184#endif
185 break;
186 default:
187 break;
188 }
189
190 ALOGE("%s: pass through channel count %d\n", __func__, channel_count);
191 return channel_count;
192}
193
Mingming Yin21854652016-04-13 11:54:02 -0700194bool audio_extn_passthru_is_supported_format(audio_format_t format)
195{
196 int32_t num_passthru_formats = sizeof(audio_passthru_formats) /
197 sizeof(audio_passthru_formats[0]);
198 int32_t i;
199
200 for (i = 0; i < num_passthru_formats; i++) {
201 if (format == audio_passthru_formats[i]) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530202 ALOGD("%s : pass through format is true", __func__);
Mingming Yin21854652016-04-13 11:54:02 -0700203 return true;
204 }
205 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530206 ALOGD("%s : pass through format is false", __func__);
Mingming Yin21854652016-04-13 11:54:02 -0700207 return false;
208}
209
210/*
211 * must be called with stream lock held
212 * This function decides based on some rules whether the data
213 * coming on stream out must be rendered or dropped.
214 */
215bool audio_extn_passthru_should_drop_data(struct stream_out * out)
216{
Ashish Jaind84fd6a2016-07-27 12:33:25 +0530217 /*Drop data only
218 *stream is routed to HDMI and
219 *stream has PCM format or
220 *if a compress offload (DSP decode) session
221 */
222 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
223 (((out->format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) ||
224 ((out->compr_config.codec != NULL) && (out->compr_config.codec->compr_passthr == LEGACY_PCM)))) {
Mingming Yin21854652016-04-13 11:54:02 -0700225 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
226 ALOGI("drop data as pass thru is active");
227 return true;
228 }
229 }
230
231 return false;
232}
233
234/* called with adev lock held */
235void audio_extn_passthru_on_start(struct stream_out * out)
236{
237
238 uint64_t max_period_us = 0;
239 uint64_t temp;
240 struct audio_usecase * usecase;
241 struct listnode *node;
242 struct stream_out * o;
243 struct audio_device *adev = out->dev;
244
245 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
246 ALOGI("pass thru is already active");
247 return;
248 }
249
250 ALOGV("inc pass thru count to notify other streams");
251 android_atomic_inc(&compress_passthru_active);
252
Mingming Yin21854652016-04-13 11:54:02 -0700253 while (true) {
254 /* find max period time among active playback use cases */
255 list_for_each(node, &adev->usecase_list) {
256 usecase = node_to_item(node, struct audio_usecase, list);
257 if (usecase->type == PCM_PLAYBACK &&
258 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
259 o = usecase->stream.out;
260 temp = o->config.period_size * 1000000LL / o->sample_rate;
261 if (temp > max_period_us)
262 max_period_us = temp;
263 }
264 }
265
266 if (max_period_us) {
267 pthread_mutex_unlock(&adev->lock);
268 usleep(2*max_period_us);
269 max_period_us = 0;
270 pthread_mutex_lock(&adev->lock);
271 } else
272 break;
273 }
274}
275
276/* called with adev lock held */
277void audio_extn_passthru_on_stop(struct stream_out * out)
278{
Mingming Yin21854652016-04-13 11:54:02 -0700279 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
280 /*
281 * its possible the count is already zero if pause was called before
282 * stop output stream
283 */
284 android_atomic_dec(&compress_passthru_active);
285 }
286
287 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
288 ALOGI("passthru on aux digital, start keep alive");
289 audio_extn_keep_alive_start();
290 }
291}
292
293void audio_extn_passthru_on_pause(struct stream_out * out __unused)
294{
295 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
296 return;
Mingming Yin21854652016-04-13 11:54:02 -0700297}
298
299int audio_extn_passthru_set_parameters(struct audio_device *adev __unused,
300 struct str_parms *parms __unused)
301{
302 return 0;
303}
304
305bool audio_extn_passthru_is_active()
306{
307 return android_atomic_acquire_load(&compress_passthru_active) > 0;
308}
309
310bool audio_extn_passthru_is_enabled() { return true; }
311
312void audio_extn_passthru_init(struct audio_device *adev __unused)
313{
314}
315
316bool audio_extn_passthru_should_standby(struct stream_out * out __unused)
317{
318 return true;
319}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530320
321bool audio_extn_passthru_is_convert_supported(struct audio_device *adev,
322 struct stream_out *out)
323{
324
325 bool convert = false;
326 switch (out->format) {
327 case AUDIO_FORMAT_E_AC3:
328 case AUDIO_FORMAT_E_AC3_JOC:
329 case AUDIO_FORMAT_DTS_HD:
330 if (!platform_is_edid_supported_format(adev->platform,
331 out->format)) {
332 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
333 convert = true;
334 }
335 break;
336 default:
337 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
338 __func__, out->format);
339 break;
340 }
341 ALOGD("%s: convert %d", __func__, convert);
342 return convert;
343}
344
345bool audio_extn_passthru_is_passt_supported(struct audio_device *adev,
346 struct stream_out *out)
347{
348 bool passt = false;
349 switch (out->format) {
350 case AUDIO_FORMAT_E_AC3:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700351 case AUDIO_FORMAT_DTS_HD:
352 case AUDIO_FORMAT_DOLBY_TRUEHD:
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530353 if (platform_is_edid_supported_format(adev->platform, out->format)) {
354 ALOGV("%s:PASSTHROUGH supported for format %x",
355 __func__, out->format);
356 passt = true;
357 }
358 break;
359 case AUDIO_FORMAT_AC3:
360 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
361 || platform_is_edid_supported_format(adev->platform,
362 AUDIO_FORMAT_E_AC3)) {
363 ALOGV("%s:PASSTHROUGH supported for format %x",
364 __func__, out->format);
365 passt = true;
366 }
367 break;
368 case AUDIO_FORMAT_E_AC3_JOC:
369 /* Check for DDP capability in edid for JOC contents.*/
370 if (platform_is_edid_supported_format(adev->platform,
371 AUDIO_FORMAT_E_AC3)) {
372 ALOGV("%s:PASSTHROUGH supported for format %x",
373 __func__, out->format);
374 passt = true;
375 }
376 break;
377 case AUDIO_FORMAT_DTS:
378 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
379 || platform_is_edid_supported_format(adev->platform,
380 AUDIO_FORMAT_DTS_HD)) {
381 ALOGV("%s:PASSTHROUGH supported for format %x",
382 __func__, out->format);
383 passt = true;
384 }
385 break;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530386 default:
387 ALOGV("%s:Passthrough not supported", __func__);
388 }
389 return passt;
390}
391
392void audio_extn_passthru_update_stream_configuration(
Manish Dewangan37864bc2017-06-09 12:28:37 +0530393 struct audio_device *adev, struct stream_out *out,
394 const void *buffer, size_t bytes)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530395{
396 if (audio_extn_passthru_is_passt_supported(adev, out)) {
397 ALOGV("%s:PASSTHROUGH", __func__);
398 out->compr_config.codec->compr_passthr = PASSTHROUGH;
Naresh Tanniru928f0862017-04-07 16:44:23 -0700399 } else if (audio_extn_passthru_is_convert_supported(adev, out)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530400 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
401 out->compr_config.codec->compr_passthr = PASSTHROUGH_CONVERT;
Naresh Tanniru928f0862017-04-07 16:44:23 -0700402 } else if (out->format == AUDIO_FORMAT_IEC61937) {
403 ALOGV("%s:PASSTHROUGH IEC61937", __func__);
404 out->compr_config.codec->compr_passthr = PASSTHROUGH_IEC61937;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530405 } else {
406 ALOGV("%s:NO PASSTHROUGH", __func__);
407 out->compr_config.codec->compr_passthr = LEGACY_PCM;
408 }
Manish Dewangan37864bc2017-06-09 12:28:37 +0530409
410 /*
411 * for DTS passthrough, need to get sample rate from bitstream,
412 * based on this sample rate hdmi backend will be configured
413 */
414 if ((out->format == AUDIO_FORMAT_DTS) ||
415 (out->format == AUDIO_FORMAT_DTS_HD))
416 passthru_update_stream_configuration_from_dts_parser(out, buffer, bytes);
417
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530418}
419
Siddartha Shaik15b84ea2017-08-07 12:29:25 +0530420bool audio_extn_passthru_is_direct_passthrough(struct stream_out *out)
421{
422 //check passthrough system property
423 if (!property_get_bool("audio.offload.passthrough", false)) {
424 return false;
425 }
426
427 if ((out != NULL) && (out->compr_config.codec->compr_passthr == PASSTHROUGH || out->compr_config.codec->compr_passthr == PASSTHROUGH_IEC61937))
428 return true;
429 else
430 return false;
431}
432
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530433bool audio_extn_passthru_is_passthrough_stream(struct stream_out *out)
434{
435 //check passthrough system property
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700436 if (!property_get_bool("vendor.audio.offload.passthrough", false)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530437 return false;
438 }
439
440 //check supported device, currently only on HDMI.
441 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
442 //passthrough flag
443 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
444 return true;
445 //direct flag, check supported formats.
446 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
447 if (audio_extn_passthru_is_supported_format(out->format)) {
448 if (platform_is_edid_supported_format(out->dev->platform,
449 out->format)) {
450 ALOGV("%s : return true",__func__);
451 return true;
452 } else if (audio_extn_is_dolby_format(out->format) &&
453 platform_is_edid_supported_format(out->dev->platform,
454 AUDIO_FORMAT_AC3)){
455 //return true for EAC3/EAC3_JOC formats
456 //if sink supports only AC3
457 ALOGV("%s : return true",__func__);
458 return true;
459 }
460 }
461 }
462 }
463 ALOGV("%s : return false",__func__);
464 return false;
465}
466
467int audio_extn_passthru_get_buffer_size(audio_offload_info_t* info)
468{
Manish Dewangan37864bc2017-06-09 12:28:37 +0530469 uint32_t fragment_size = MIN_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
470 char value[PROPERTY_VALUE_MAX] = {0};
471
472 if (((info->format == AUDIO_FORMAT_DOLBY_TRUEHD) ||
473 (info->format == AUDIO_FORMAT_IEC61937)) &&
474 property_get("audio.truehd.buffer.size.kb", value, "") &&
475 atoi(value)) {
476 fragment_size = atoi(value) * 1024;
477 goto done;
478 } else if ((info->format == AUDIO_FORMAT_DTS) ||
479 (info->format == AUDIO_FORMAT_DTS_HD)) {
480 fragment_size = MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
481 goto done;
482 }
483
484done:
485 return fragment_size;
486
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530487}
488
489int audio_extn_passthru_set_volume(struct stream_out *out, int mute)
490{
491 return platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
492}
493
494int audio_extn_passthru_set_latency(struct stream_out *out, int latency)
495{
496 return platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
497}