blob: 271ae23e47e6f34a628022fd1612159cf0509503 [file] [log] [blame]
Mingming Yin21854652016-04-13 11:54:02 -07001/*
Meng Wang4c32fb42020-01-16 17:57:11 +08002* Copyright (c) 2014-2020, 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>
Weiyin Jiang2995f662019-04-17 14:25:12 +080034#include <cutils/properties.h>
Mingming Yin21854652016-04-13 11:54:02 -070035#include <cutils/str_parms.h>
Weiyin Jiang2995f662019-04-17 14:25:12 +080036#include <log/log.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053037#include <unistd.h>
Arun Mirpurie008ed22019-03-21 11:21:04 -070038#include <pthread.h>
Mingming Yin21854652016-04-13 11:54:02 -070039#include "audio_hw.h"
40#include "audio_extn.h"
41#include "platform_api.h"
42#include <platform.h>
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053043
44#include "sound/compress_params.h"
Manish Dewangan37864bc2017-06-09 12:28:37 +053045
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_PASSTH
49#include <log_utils.h>
50#endif
Manish Dewangan37864bc2017-06-09 12:28:37 +053051/*
52 * Offload buffer size for compress passthrough
53 */
54
55#ifdef DTSHD_PARSER_ENABLED
56#include "audio_parsers.h"
57
58/* list of all supported DTS transmission sample rates */
59static const int dts_transmission_sample_rates[] = {
60 44100, 48000, 88200, 96000, 176400, 192000
61};
62
63 /*
64 * for DTSHD stream one frame size can be upto 36kb and to extract iec61937
65 * info for parsing usecase minimum one frame needs to be sent to dts parser
66 */
67#define MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (36 * 1024)
68#else
69#define MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (8 * 1024)
70#endif
71
72#define MIN_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (2 * 1024)
Mingming Yin21854652016-04-13 11:54:02 -070073
Harsh Bansalc83207c2017-09-01 16:04:36 +053074#define DDP_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE (10 * 1024)
75
Mingming Yin21854652016-04-13 11:54:02 -070076static const audio_format_t audio_passthru_formats[] = {
77 AUDIO_FORMAT_AC3,
78 AUDIO_FORMAT_E_AC3,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053079 AUDIO_FORMAT_E_AC3_JOC,
Mingming Yin21854652016-04-13 11:54:02 -070080 AUDIO_FORMAT_DTS,
Ben Romberger1aaaf862017-04-06 17:49:46 -070081 AUDIO_FORMAT_DTS_HD,
Naresh Tanniru928f0862017-04-07 16:44:23 -070082 AUDIO_FORMAT_DOLBY_TRUEHD,
83 AUDIO_FORMAT_IEC61937
Mingming Yin21854652016-04-13 11:54:02 -070084};
85
Arun Mirpurie008ed22019-03-21 11:21:04 -070086//external function depedency
87static fp_platform_is_edid_supported_format_t fp_platform_is_edid_supported_format;
88static fp_platform_set_device_params_t fp_platform_set_device_params;
89static fp_platform_edid_get_max_channels_t fp_platform_edid_get_max_channels;
90static fp_platform_get_output_snd_device_t fp_platform_get_output_snd_device;
91static fp_platform_get_codec_backend_cfg_t fp_platform_get_codec_backend_cfg;
92static fp_platform_get_snd_device_name_t fp_platform_get_snd_device_name;
93static fp_platform_is_edid_supported_sample_rate_t fp_platform_is_edid_supported_sample_rate;
94static fp_audio_extn_keep_alive_start_t fp_audio_extn_keep_alive_start;
95static fp_audio_extn_keep_alive_stop_t fp_audio_extn_keep_alive_stop;
96static fp_audio_extn_utils_is_dolby_format_t fp_audio_extn_utils_is_dolby_format;
97
Mingming Yin21854652016-04-13 11:54:02 -070098/*
99 * This atomic var is incremented/decremented by the offload stream to notify
100 * other pcm playback streams that a pass thru session is about to start or has
101 * finished. This hint can be used by the other streams to move to standby or
102 * start calling pcm_write respectively.
103 * This behavior is necessary as the DSP backend can only be configured to one
104 * of PCM or compressed.
105 */
106static volatile int32_t compress_passthru_active;
107
Arun Mirpurie008ed22019-03-21 11:21:04 -0700108int passthru_update_dts_stream_configuration(struct stream_out *out,
Manish Dewangan37864bc2017-06-09 12:28:37 +0530109 const void *buffer, size_t bytes)
110{
111 struct audio_parser_codec_info codec_info;
112 struct dtshd_iec61937_info dtshd_tr_info;
113 int i;
114 int ret;
115 bool is_valid_transmission_rate = false;
116 bool is_valid_transmission_channels = false;
117
Manish Dewangan671a4202017-08-18 17:30:46 +0530118 if (!out) {
119 ALOGE("Invalid session");
120 return -EINVAL;
121 }
122
123 if ((out->format != AUDIO_FORMAT_DTS) &&
124 (out->format != AUDIO_FORMAT_DTS_HD)) {
125 ALOGE("Non DTS format %d", out->format);
126 return -EINVAL;
127 }
128
129 if (!buffer || bytes <= 0) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700130 ALOGD("Invalid buffer %p size %lu skipping dts stream conf update",
131 buffer, (unsigned long)bytes);
Manish Dewangan671a4202017-08-18 17:30:46 +0530132 out->sample_rate = 48000;
133 out->compr_config.codec->sample_rate = out->sample_rate;
134 out->compr_config.codec->ch_in = 2;
135 out->channel_mask = audio_channel_out_mask_from_count(2);
136 return -EINVAL;
137 }
138
Manish Dewangan37864bc2017-06-09 12:28:37 +0530139 /* codec format is AUDIO_PARSER_CODEC_DTSHD for both DTS and DTSHD as
140 * DTSHD parser can support both DTS and DTSHD
141 */
142 memset(&codec_info, 0, sizeof(struct audio_parser_codec_info));
143 memset(&dtshd_tr_info, 0, sizeof(struct dtshd_iec61937_info));
144
145 init_audio_parser((unsigned char *)buffer, bytes, AUDIO_PARSER_CODEC_DTSHD);
146 codec_info.codec_type = AUDIO_PARSER_CODEC_DTSHD;
147 if (!(ret = get_iec61937_info(&codec_info))) {
148 dtshd_tr_info = codec_info.codec_config.dtshd_tr_info;
149 ALOGD("dts new sample rate %d and channels %d\n",
150 dtshd_tr_info.sample_rate,
151 dtshd_tr_info.num_channels);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700152 for (i = 0; i < (sizeof(dts_transmission_sample_rates)/sizeof(int)); i++) {
Manish Dewangan37864bc2017-06-09 12:28:37 +0530153 if (dts_transmission_sample_rates[i] ==
154 dtshd_tr_info.sample_rate) {
155 out->sample_rate = dtshd_tr_info.sample_rate;
156 out->compr_config.codec->sample_rate = out->sample_rate;
157 is_valid_transmission_rate = true;
158 break;
159 }
160 }
161 /* DTS transmission channels should be 2 or 8*/
162 if ((dtshd_tr_info.num_channels == 2) ||
163 (dtshd_tr_info.num_channels == 8)) {
164 out->compr_config.codec->ch_in = dtshd_tr_info.num_channels;
165 out->channel_mask = audio_channel_out_mask_from_count
166 (dtshd_tr_info.num_channels);
167 is_valid_transmission_channels = true;
168 }
169 } else {
170 ALOGE("%s:: get_iec61937_info failed %d", __func__, ret);
171 }
172
173 if (!is_valid_transmission_rate) {
Harsh Bansal0ab6b182017-08-14 11:49:43 +0530174 ALOGE("%s:: Invalid dts transmission rate %d\n using default sample rate 48000",
Arun Mirpurie008ed22019-03-21 11:21:04 -0700175 __func__, dtshd_tr_info.sample_rate);
Harsh Bansal0ab6b182017-08-14 11:49:43 +0530176 out->sample_rate = 48000;
Manish Dewangan37864bc2017-06-09 12:28:37 +0530177 out->compr_config.codec->sample_rate = out->sample_rate;
178 }
179
180 if (!is_valid_transmission_channels) {
181 ALOGE("%s:: Invalid transmission channels %d using default transmission"
182 " channels as 2", __func__, dtshd_tr_info.num_channels);
183 out->compr_config.codec->ch_in = 2;
184 out->channel_mask = audio_channel_out_mask_from_count(2);
185 }
Manish Dewangan671a4202017-08-18 17:30:46 +0530186 return 0;
Manish Dewangan37864bc2017-06-09 12:28:37 +0530187}
Manish Dewangan37864bc2017-06-09 12:28:37 +0530188
Arun Mirpurie008ed22019-03-21 11:21:04 -0700189bool passthru_is_supported_format(audio_format_t format)
190{
191 int32_t num_passthru_formats = sizeof(audio_passthru_formats) /
192 sizeof(audio_passthru_formats[0]);
193 int32_t i;
194
195 for (i = 0; i < num_passthru_formats; i++) {
196 if (format == audio_passthru_formats[i]) {
197 ALOGD("%s : pass through format is true", __func__);
198 return true;
199 }
200 }
201 ALOGD("%s : pass through format is false", __func__);
202 return false;
203}
204
205int passthru_get_channel_count(struct stream_out *out)
Manish Dewangan37864bc2017-06-09 12:28:37 +0530206{
207 int channel_count = DEFAULT_HDMI_OUT_CHANNELS;
208
209 if (!out) {
210 ALOGE("%s:: Invalid param out %p", __func__, out);
211 return -EINVAL;
212 }
213
Arun Mirpurie008ed22019-03-21 11:21:04 -0700214 if (!passthru_is_supported_format(out->format)) {
Manish Dewangan37864bc2017-06-09 12:28:37 +0530215 ALOGE("%s:: not a passthrough format %d", __func__, out->format);
216 return -EINVAL;
217 }
218
219 switch(out->format) {
220 case AUDIO_FORMAT_DOLBY_TRUEHD:
221 channel_count = 8;
222 break;
223 case AUDIO_FORMAT_DTS:
224 case AUDIO_FORMAT_DTS_HD:
225#ifdef DTSHD_PARSER_ENABLED
226 /* taken channel count from parser*/
227 channel_count = audio_channel_count_from_out_mask(out->channel_mask);
228#endif
229 break;
Harsh Bansal14d47262018-01-31 13:32:37 +0530230 case AUDIO_FORMAT_IEC61937:
231 channel_count = audio_channel_count_from_out_mask(out->channel_mask);
Manish Dewangan37864bc2017-06-09 12:28:37 +0530232 default:
233 break;
234 }
235
236 ALOGE("%s: pass through channel count %d\n", __func__, channel_count);
237 return channel_count;
238}
239
Mingming Yin21854652016-04-13 11:54:02 -0700240/*
241 * must be called with stream lock held
242 * This function decides based on some rules whether the data
243 * coming on stream out must be rendered or dropped.
244 */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700245bool passthru_should_drop_data(struct stream_out * out)
Mingming Yin21854652016-04-13 11:54:02 -0700246{
Meng Wang4c32fb42020-01-16 17:57:11 +0800247 uint32_t compr_passthr = 0;
Ashish Jaind84fd6a2016-07-27 12:33:25 +0530248 /*Drop data only
249 *stream is routed to HDMI and
250 *stream has PCM format or
251 *if a compress offload (DSP decode) session
252 */
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800253
254 if(out->compr_config.codec != NULL) {
Vignesh Kulothungan4fa89d12020-01-27 11:24:13 -0800255#ifdef AUDIO_GKI_ENABLED
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800256 /* out->compr_config.codec->reserved[0] is for compr_passthr */
257 compr_passthr = out->compr_config.codec->reserved[0];
Meng Wang4c32fb42020-01-16 17:57:11 +0800258#else
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800259 compr_passthr = out->compr_config.codec->compr_passthr;
Meng Wang4c32fb42020-01-16 17:57:11 +0800260#endif
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800261 }
262
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800263 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Ashish Jaind84fd6a2016-07-27 12:33:25 +0530264 (((out->format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) ||
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800265 (compr_passthr == LEGACY_PCM))) {
Mingming Yin21854652016-04-13 11:54:02 -0700266 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
267 ALOGI("drop data as pass thru is active");
268 return true;
269 }
270 }
271
272 return false;
273}
274
275/* called with adev lock held */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700276void passthru_on_start(struct stream_out * out)
Mingming Yin21854652016-04-13 11:54:02 -0700277{
278
279 uint64_t max_period_us = 0;
280 uint64_t temp;
281 struct audio_usecase * usecase;
282 struct listnode *node;
283 struct stream_out * o;
284 struct audio_device *adev = out->dev;
285
286 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
287 ALOGI("pass thru is already active");
288 return;
289 }
290
291 ALOGV("inc pass thru count to notify other streams");
292 android_atomic_inc(&compress_passthru_active);
293
Mingming Yin21854652016-04-13 11:54:02 -0700294 while (true) {
295 /* find max period time among active playback use cases */
296 list_for_each(node, &adev->usecase_list) {
297 usecase = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +0530298 if (usecase->stream.out && usecase->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800299 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Mingming Yin21854652016-04-13 11:54:02 -0700300 o = usecase->stream.out;
301 temp = o->config.period_size * 1000000LL / o->sample_rate;
302 if (temp > max_period_us)
303 max_period_us = temp;
304 }
305 }
306
307 if (max_period_us) {
308 pthread_mutex_unlock(&adev->lock);
309 usleep(2*max_period_us);
310 max_period_us = 0;
311 pthread_mutex_lock(&adev->lock);
312 } else
313 break;
314 }
315}
316
317/* called with adev lock held */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700318void passthru_on_stop(struct stream_out * out)
Mingming Yin21854652016-04-13 11:54:02 -0700319{
Mingming Yin21854652016-04-13 11:54:02 -0700320 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
321 /*
322 * its possible the count is already zero if pause was called before
323 * stop output stream
324 */
325 android_atomic_dec(&compress_passthru_active);
326 }
327
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800328 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530329 ALOGD("%s: passthru on aux digital, start keep alive", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700330 fp_audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Mingming Yin21854652016-04-13 11:54:02 -0700331 }
332}
333
Arun Mirpurie008ed22019-03-21 11:21:04 -0700334void passthru_on_pause(struct stream_out * out __unused)
Mingming Yin21854652016-04-13 11:54:02 -0700335{
336 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
337 return;
Mingming Yin21854652016-04-13 11:54:02 -0700338}
339
Arun Mirpurie008ed22019-03-21 11:21:04 -0700340bool passthru_is_active()
341{
342 return android_atomic_acquire_load(&compress_passthru_active) > 0;
343}
344
345int passthru_set_parameters(struct audio_device *adev __unused,
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530346 struct str_parms *parms)
Mingming Yin21854652016-04-13 11:54:02 -0700347{
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530348 char value[32];
349 int ret;
350 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
351 if (ret >= 0) {
352 int val = atoi(value);
353 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700354 if (!passthru_is_active()) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530355 ALOGV("%s: start keep alive on aux digital", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700356 fp_audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530357 }
358 }
359 }
360
361 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
362 sizeof(value));
363 if (ret >= 0) {
364 int val = atoi(value);
365 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
366 ALOGV("%s: stop keep_alive on aux digital on device", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700367 fp_audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530368 }
369 }
Mingming Yin21854652016-04-13 11:54:02 -0700370 return 0;
371}
372
Arun Mirpurie008ed22019-03-21 11:21:04 -0700373bool passthru_is_enabled() { return true; }
374
375void passthru_init(passthru_init_config_t init_config)
Mingming Yin21854652016-04-13 11:54:02 -0700376{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700377 fp_platform_is_edid_supported_format =
378 init_config.fp_platform_is_edid_supported_format;
379 fp_platform_set_device_params = init_config.fp_platform_set_device_params;
380 fp_platform_edid_get_max_channels =
381 init_config.fp_platform_edid_get_max_channels;
382 fp_platform_get_output_snd_device = init_config.fp_platform_get_output_snd_device;
383 fp_platform_get_codec_backend_cfg =
384 init_config.fp_platform_get_codec_backend_cfg;
385 fp_platform_get_snd_device_name = init_config.fp_platform_get_snd_device_name;
386 fp_platform_is_edid_supported_sample_rate =
387 init_config.fp_platform_is_edid_supported_sample_rate;
388 fp_audio_extn_keep_alive_start = init_config.fp_audio_extn_keep_alive_start;
389 fp_audio_extn_keep_alive_stop = init_config.fp_audio_extn_keep_alive_stop;
390 fp_audio_extn_utils_is_dolby_format = init_config.fp_audio_extn_utils_is_dolby_format;
Mingming Yin21854652016-04-13 11:54:02 -0700391}
392
Arun Mirpurie008ed22019-03-21 11:21:04 -0700393bool passthru_should_standby(struct stream_out * out __unused)
Mingming Yin21854652016-04-13 11:54:02 -0700394{
395 return true;
396}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530397
Arun Mirpurie008ed22019-03-21 11:21:04 -0700398bool passthru_is_convert_supported(struct audio_device *adev,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530399 struct stream_out *out)
400{
401
402 bool convert = false;
403 switch (out->format) {
404 case AUDIO_FORMAT_E_AC3:
405 case AUDIO_FORMAT_E_AC3_JOC:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700406 if (!fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530407 out->format)) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700408 if (fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530409 AUDIO_FORMAT_AC3)) {
410 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
411 convert = true;
412 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530413 }
414 break;
415 default:
416 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
417 __func__, out->format);
418 break;
419 }
420 ALOGD("%s: convert %d", __func__, convert);
421 return convert;
422}
423
Arun Mirpurie008ed22019-03-21 11:21:04 -0700424bool passthru_is_passt_supported(struct audio_device *adev,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530425 struct stream_out *out)
426{
427 bool passt = false;
428 switch (out->format) {
429 case AUDIO_FORMAT_E_AC3:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700430 case AUDIO_FORMAT_DTS_HD:
431 case AUDIO_FORMAT_DOLBY_TRUEHD:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700432 if (fp_platform_is_edid_supported_format(adev->platform, out->format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530433 ALOGV("%s:PASSTHROUGH supported for format %x",
434 __func__, out->format);
435 passt = true;
436 }
437 break;
438 case AUDIO_FORMAT_AC3:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700439 if (fp_platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
440 || fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530441 AUDIO_FORMAT_E_AC3)) {
442 ALOGV("%s:PASSTHROUGH supported for format %x",
443 __func__, out->format);
444 passt = true;
445 }
446 break;
447 case AUDIO_FORMAT_E_AC3_JOC:
448 /* Check for DDP capability in edid for JOC contents.*/
Arun Mirpurie008ed22019-03-21 11:21:04 -0700449 if (fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530450 AUDIO_FORMAT_E_AC3)) {
451 ALOGV("%s:PASSTHROUGH supported for format %x",
452 __func__, out->format);
453 passt = true;
454 }
455 break;
456 case AUDIO_FORMAT_DTS:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700457 if (fp_platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
458 || fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530459 AUDIO_FORMAT_DTS_HD)) {
460 ALOGV("%s:PASSTHROUGH supported for format %x",
461 __func__, out->format);
462 passt = true;
463 }
464 break;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530465 default:
466 ALOGV("%s:Passthrough not supported", __func__);
467 }
468 return passt;
469}
470
Arun Mirpurie008ed22019-03-21 11:21:04 -0700471void passthru_update_stream_configuration(
Manish Dewangan37864bc2017-06-09 12:28:37 +0530472 struct audio_device *adev, struct stream_out *out,
Garmond Leung317cbf12017-09-13 16:20:50 -0700473 const void *buffer __unused, size_t bytes __unused)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530474{
Meng Wang4c32fb42020-01-16 17:57:11 +0800475 uint32_t compr_passthr = 0;
476
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530477 if(out->compr_config.codec != NULL) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700478 if (passthru_is_passt_supported(adev, out)) {
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530479 ALOGV("%s:PASSTHROUGH", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800480 compr_passthr = PASSTHROUGH;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700481 } else if (passthru_is_convert_supported(adev, out)) {
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530482 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800483 compr_passthr = PASSTHROUGH_CONVERT;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530484 } else if (out->format == AUDIO_FORMAT_IEC61937) {
485 ALOGV("%s:PASSTHROUGH IEC61937", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800486 compr_passthr = PASSTHROUGH_IEC61937;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530487 } else {
488 ALOGV("%s:NO PASSTHROUGH", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800489 compr_passthr = LEGACY_PCM;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530490 }
Vignesh Kulothungan431207d2020-02-27 17:35:20 -0800491#ifdef AUDIO_GKI_ENABLED
492 /* out->compr_config.codec->reserved[0] is for compr_passthr */
493 out->compr_config.codec->reserved[0] = compr_passthr;
494#else
495 out->compr_config.codec->compr_passthr = compr_passthr;
496#endif
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530497 }
Meng Wang4c32fb42020-01-16 17:57:11 +0800498
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530499}
500
Arun Mirpurie008ed22019-03-21 11:21:04 -0700501bool passthru_is_passthrough_stream(struct stream_out *out)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530502{
503 //check passthrough system property
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700504 if (!property_get_bool("vendor.audio.offload.passthrough", false)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530505 return false;
506 }
507
508 //check supported device, currently only on HDMI.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800509 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530510 //passthrough flag
511 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
512 return true;
513 //direct flag, check supported formats.
514 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700515 if (passthru_is_supported_format(out->format)) {
516 if (fp_platform_is_edid_supported_format(out->dev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530517 out->format)) {
518 ALOGV("%s : return true",__func__);
519 return true;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700520 } else if (fp_audio_extn_utils_is_dolby_format(out->format) &&
521 fp_platform_is_edid_supported_format(out->dev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530522 AUDIO_FORMAT_AC3)){
523 //return true for EAC3/EAC3_JOC formats
524 //if sink supports only AC3
525 ALOGV("%s : return true",__func__);
526 return true;
527 }
528 }
529 }
530 }
531 ALOGV("%s : return false",__func__);
532 return false;
533}
534
Arun Mirpurie008ed22019-03-21 11:21:04 -0700535bool passthru_is_direct_passthrough(struct stream_out *out)
Manish Dewangan672001f2017-08-16 13:44:07 +0530536{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700537 if (((out != NULL) && passthru_is_passthrough_stream(out)) &&
538 !passthru_is_convert_supported(out->dev, out))
Manish Dewangan672001f2017-08-16 13:44:07 +0530539 return true;
540 else
541 return false;
542}
543
Arun Mirpurie008ed22019-03-21 11:21:04 -0700544int passthru_get_buffer_size(audio_offload_info_t* info)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530545{
Manish Dewangan37864bc2017-06-09 12:28:37 +0530546 uint32_t fragment_size = MIN_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
547 char value[PROPERTY_VALUE_MAX] = {0};
548
549 if (((info->format == AUDIO_FORMAT_DOLBY_TRUEHD) ||
550 (info->format == AUDIO_FORMAT_IEC61937)) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +0530551 property_get("vendor.audio.truehd.buffer.size.kb", value, "") &&
Manish Dewangan37864bc2017-06-09 12:28:37 +0530552 atoi(value)) {
553 fragment_size = atoi(value) * 1024;
554 goto done;
555 } else if ((info->format == AUDIO_FORMAT_DTS) ||
556 (info->format == AUDIO_FORMAT_DTS_HD)) {
557 fragment_size = MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
558 goto done;
Harsh Bansalc83207c2017-09-01 16:04:36 +0530559 } else if (info->format == AUDIO_FORMAT_E_AC3) {
560 fragment_size = DDP_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +0530561 if(property_get("vendor.audio.ddp.buffer.size.kb", value, "") &&
Harsh Bansalc83207c2017-09-01 16:04:36 +0530562 atoi(value)) {
563 fragment_size = atoi(value) * 1024;
564 }
565 goto done;
Manish Dewangan37864bc2017-06-09 12:28:37 +0530566 }
Manish Dewangan37864bc2017-06-09 12:28:37 +0530567done:
568 return fragment_size;
569
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530570}
571
Arun Mirpurie008ed22019-03-21 11:21:04 -0700572int passthru_set_volume(struct stream_out *out, int mute)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530573{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700574 return fp_platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530575}
576
Arun Mirpurie008ed22019-03-21 11:21:04 -0700577int passthru_set_latency(struct stream_out *out, int latency)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530578{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700579 return fp_platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530580}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530581
Arun Mirpurie008ed22019-03-21 11:21:04 -0700582bool passthru_is_supported_backend_edid_cfg(struct audio_device *adev,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530583 struct stream_out *out)
584{
585 struct audio_backend_cfg backend_cfg;
mpang787a6aa2018-11-29 11:25:58 +0800586 backend_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
587 backend_cfg.channels = CODEC_BACKEND_DEFAULT_CHANNELS;
588 backend_cfg.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
589 backend_cfg.format = AUDIO_FORMAT_PCM_16_BIT;
590 backend_cfg.passthrough_enabled = false;
591
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530592 snd_device_t out_snd_device = SND_DEVICE_NONE;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700593 int max_edid_channels = fp_platform_edid_get_max_channels(out->dev->platform);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530594
Jaideep Sharma477917f2020-03-13 18:13:33 +0530595 out_snd_device = fp_platform_get_output_snd_device(adev->platform, out, USECASE_TYPE_MAX);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530596
Arun Mirpurie008ed22019-03-21 11:21:04 -0700597 if (fp_platform_get_codec_backend_cfg(adev, out_snd_device, &backend_cfg)) {
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530598 ALOGE("%s: ERROR: Unable to get current backend config!!!", __func__);
599 return false;
600 }
601
602 ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d format %d"
603 ", device (%s)", __func__, backend_cfg.bit_width,
604 backend_cfg.sample_rate, backend_cfg.channels, backend_cfg.format,
Arun Mirpurie008ed22019-03-21 11:21:04 -0700605 fp_platform_get_snd_device_name(out_snd_device));
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530606
607 /* Check if the channels are supported */
Garmond Leung438932f2017-10-04 19:35:18 -0700608 if (max_edid_channels < (int)backend_cfg.channels) {
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530609
610 ALOGE("%s: ERROR: Unsupported channels in passthru mode!!!"
611 " max_edid_channels - %d backend_channels - %d",
612 __func__, max_edid_channels, backend_cfg.channels);
613 return false;
614 }
615
616 /* Check if the sample rate supported */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700617 if (!fp_platform_is_edid_supported_sample_rate(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530618 backend_cfg.sample_rate)) {
619
620 ALOGE("%s: ERROR: Unsupported sample rate in passthru mode!!!"
621 " backend_samplerate - %d",
622 __func__, backend_cfg.sample_rate);
623 return false;
624 }
625
626 return true;
627}