blob: 293ffacce38df9a616dd9f7fcddbefda24574150 [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 */
Meng Wang4c32fb42020-01-16 17:57:11 +0800253#ifdef AUDIO_QGKI_ENABLED
254 /* out->compr_config.codec->reserved[0] is for compr_passthr */
255 compr_passthr = out->compr_config.codec->reserved[0];
256#else
257 compr_passthr = out->compr_config.codec->compr_passthr;
258#endif
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800259 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Ashish Jaind84fd6a2016-07-27 12:33:25 +0530260 (((out->format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) ||
Meng Wang4c32fb42020-01-16 17:57:11 +0800261 ((out->compr_config.codec != NULL) && (compr_passthr == LEGACY_PCM)))) {
Mingming Yin21854652016-04-13 11:54:02 -0700262 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
263 ALOGI("drop data as pass thru is active");
264 return true;
265 }
266 }
267
268 return false;
269}
270
271/* called with adev lock held */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700272void passthru_on_start(struct stream_out * out)
Mingming Yin21854652016-04-13 11:54:02 -0700273{
274
275 uint64_t max_period_us = 0;
276 uint64_t temp;
277 struct audio_usecase * usecase;
278 struct listnode *node;
279 struct stream_out * o;
280 struct audio_device *adev = out->dev;
281
282 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
283 ALOGI("pass thru is already active");
284 return;
285 }
286
287 ALOGV("inc pass thru count to notify other streams");
288 android_atomic_inc(&compress_passthru_active);
289
Mingming Yin21854652016-04-13 11:54:02 -0700290 while (true) {
291 /* find max period time among active playback use cases */
292 list_for_each(node, &adev->usecase_list) {
293 usecase = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +0530294 if (usecase->stream.out && usecase->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800295 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Mingming Yin21854652016-04-13 11:54:02 -0700296 o = usecase->stream.out;
297 temp = o->config.period_size * 1000000LL / o->sample_rate;
298 if (temp > max_period_us)
299 max_period_us = temp;
300 }
301 }
302
303 if (max_period_us) {
304 pthread_mutex_unlock(&adev->lock);
305 usleep(2*max_period_us);
306 max_period_us = 0;
307 pthread_mutex_lock(&adev->lock);
308 } else
309 break;
310 }
311}
312
313/* called with adev lock held */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700314void passthru_on_stop(struct stream_out * out)
Mingming Yin21854652016-04-13 11:54:02 -0700315{
Mingming Yin21854652016-04-13 11:54:02 -0700316 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
317 /*
318 * its possible the count is already zero if pause was called before
319 * stop output stream
320 */
321 android_atomic_dec(&compress_passthru_active);
322 }
323
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800324 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530325 ALOGD("%s: passthru on aux digital, start keep alive", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700326 fp_audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Mingming Yin21854652016-04-13 11:54:02 -0700327 }
328}
329
Arun Mirpurie008ed22019-03-21 11:21:04 -0700330void passthru_on_pause(struct stream_out * out __unused)
Mingming Yin21854652016-04-13 11:54:02 -0700331{
332 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
333 return;
Mingming Yin21854652016-04-13 11:54:02 -0700334}
335
Arun Mirpurie008ed22019-03-21 11:21:04 -0700336bool passthru_is_active()
337{
338 return android_atomic_acquire_load(&compress_passthru_active) > 0;
339}
340
341int passthru_set_parameters(struct audio_device *adev __unused,
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530342 struct str_parms *parms)
Mingming Yin21854652016-04-13 11:54:02 -0700343{
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530344 char value[32];
345 int ret;
346 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
347 if (ret >= 0) {
348 int val = atoi(value);
349 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700350 if (!passthru_is_active()) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530351 ALOGV("%s: start keep alive on aux digital", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700352 fp_audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530353 }
354 }
355 }
356
357 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
358 sizeof(value));
359 if (ret >= 0) {
360 int val = atoi(value);
361 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
362 ALOGV("%s: stop keep_alive on aux digital on device", __func__);
Arun Mirpurie008ed22019-03-21 11:21:04 -0700363 fp_audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530364 }
365 }
Mingming Yin21854652016-04-13 11:54:02 -0700366 return 0;
367}
368
Arun Mirpurie008ed22019-03-21 11:21:04 -0700369bool passthru_is_enabled() { return true; }
370
371void passthru_init(passthru_init_config_t init_config)
Mingming Yin21854652016-04-13 11:54:02 -0700372{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700373 fp_platform_is_edid_supported_format =
374 init_config.fp_platform_is_edid_supported_format;
375 fp_platform_set_device_params = init_config.fp_platform_set_device_params;
376 fp_platform_edid_get_max_channels =
377 init_config.fp_platform_edid_get_max_channels;
378 fp_platform_get_output_snd_device = init_config.fp_platform_get_output_snd_device;
379 fp_platform_get_codec_backend_cfg =
380 init_config.fp_platform_get_codec_backend_cfg;
381 fp_platform_get_snd_device_name = init_config.fp_platform_get_snd_device_name;
382 fp_platform_is_edid_supported_sample_rate =
383 init_config.fp_platform_is_edid_supported_sample_rate;
384 fp_audio_extn_keep_alive_start = init_config.fp_audio_extn_keep_alive_start;
385 fp_audio_extn_keep_alive_stop = init_config.fp_audio_extn_keep_alive_stop;
386 fp_audio_extn_utils_is_dolby_format = init_config.fp_audio_extn_utils_is_dolby_format;
Mingming Yin21854652016-04-13 11:54:02 -0700387}
388
Arun Mirpurie008ed22019-03-21 11:21:04 -0700389bool passthru_should_standby(struct stream_out * out __unused)
Mingming Yin21854652016-04-13 11:54:02 -0700390{
391 return true;
392}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530393
Arun Mirpurie008ed22019-03-21 11:21:04 -0700394bool passthru_is_convert_supported(struct audio_device *adev,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530395 struct stream_out *out)
396{
397
398 bool convert = false;
399 switch (out->format) {
400 case AUDIO_FORMAT_E_AC3:
401 case AUDIO_FORMAT_E_AC3_JOC:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700402 if (!fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530403 out->format)) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700404 if (fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530405 AUDIO_FORMAT_AC3)) {
406 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
407 convert = true;
408 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530409 }
410 break;
411 default:
412 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
413 __func__, out->format);
414 break;
415 }
416 ALOGD("%s: convert %d", __func__, convert);
417 return convert;
418}
419
Arun Mirpurie008ed22019-03-21 11:21:04 -0700420bool passthru_is_passt_supported(struct audio_device *adev,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530421 struct stream_out *out)
422{
423 bool passt = false;
424 switch (out->format) {
425 case AUDIO_FORMAT_E_AC3:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700426 case AUDIO_FORMAT_DTS_HD:
427 case AUDIO_FORMAT_DOLBY_TRUEHD:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700428 if (fp_platform_is_edid_supported_format(adev->platform, out->format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530429 ALOGV("%s:PASSTHROUGH supported for format %x",
430 __func__, out->format);
431 passt = true;
432 }
433 break;
434 case AUDIO_FORMAT_AC3:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700435 if (fp_platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
436 || fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530437 AUDIO_FORMAT_E_AC3)) {
438 ALOGV("%s:PASSTHROUGH supported for format %x",
439 __func__, out->format);
440 passt = true;
441 }
442 break;
443 case AUDIO_FORMAT_E_AC3_JOC:
444 /* Check for DDP capability in edid for JOC contents.*/
Arun Mirpurie008ed22019-03-21 11:21:04 -0700445 if (fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530446 AUDIO_FORMAT_E_AC3)) {
447 ALOGV("%s:PASSTHROUGH supported for format %x",
448 __func__, out->format);
449 passt = true;
450 }
451 break;
452 case AUDIO_FORMAT_DTS:
Arun Mirpurie008ed22019-03-21 11:21:04 -0700453 if (fp_platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
454 || fp_platform_is_edid_supported_format(adev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530455 AUDIO_FORMAT_DTS_HD)) {
456 ALOGV("%s:PASSTHROUGH supported for format %x",
457 __func__, out->format);
458 passt = true;
459 }
460 break;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530461 default:
462 ALOGV("%s:Passthrough not supported", __func__);
463 }
464 return passt;
465}
466
Arun Mirpurie008ed22019-03-21 11:21:04 -0700467void passthru_update_stream_configuration(
Manish Dewangan37864bc2017-06-09 12:28:37 +0530468 struct audio_device *adev, struct stream_out *out,
Garmond Leung317cbf12017-09-13 16:20:50 -0700469 const void *buffer __unused, size_t bytes __unused)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530470{
Meng Wang4c32fb42020-01-16 17:57:11 +0800471 uint32_t compr_passthr = 0;
472
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530473 if(out->compr_config.codec != NULL) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700474 if (passthru_is_passt_supported(adev, out)) {
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530475 ALOGV("%s:PASSTHROUGH", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800476 compr_passthr = PASSTHROUGH;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700477 } else if (passthru_is_convert_supported(adev, out)) {
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530478 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800479 compr_passthr = PASSTHROUGH_CONVERT;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530480 } else if (out->format == AUDIO_FORMAT_IEC61937) {
481 ALOGV("%s:PASSTHROUGH IEC61937", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800482 compr_passthr = PASSTHROUGH_IEC61937;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530483 } else {
484 ALOGV("%s:NO PASSTHROUGH", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +0800485 compr_passthr = LEGACY_PCM;
Naresh Tanniruccd9f382018-01-17 16:02:19 +0530486 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530487 }
Meng Wang4c32fb42020-01-16 17:57:11 +0800488
489#ifdef AUDIO_QGKI_ENABLED
490 /* out->compr_config.codec->reserved[0] is for compr_passthr */
491 out->compr_config.codec->reserved[0] = compr_passthr;
492#else
493 out->compr_config.codec->compr_passthr = compr_passthr;
494#endif
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530495}
496
Arun Mirpurie008ed22019-03-21 11:21:04 -0700497bool passthru_is_passthrough_stream(struct stream_out *out)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530498{
499 //check passthrough system property
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700500 if (!property_get_bool("vendor.audio.offload.passthrough", false)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530501 return false;
502 }
503
504 //check supported device, currently only on HDMI.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800505 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530506 //passthrough flag
507 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
508 return true;
509 //direct flag, check supported formats.
510 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
Arun Mirpurie008ed22019-03-21 11:21:04 -0700511 if (passthru_is_supported_format(out->format)) {
512 if (fp_platform_is_edid_supported_format(out->dev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530513 out->format)) {
514 ALOGV("%s : return true",__func__);
515 return true;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700516 } else if (fp_audio_extn_utils_is_dolby_format(out->format) &&
517 fp_platform_is_edid_supported_format(out->dev->platform,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530518 AUDIO_FORMAT_AC3)){
519 //return true for EAC3/EAC3_JOC formats
520 //if sink supports only AC3
521 ALOGV("%s : return true",__func__);
522 return true;
523 }
524 }
525 }
526 }
527 ALOGV("%s : return false",__func__);
528 return false;
529}
530
Arun Mirpurie008ed22019-03-21 11:21:04 -0700531bool passthru_is_direct_passthrough(struct stream_out *out)
Manish Dewangan672001f2017-08-16 13:44:07 +0530532{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700533 if (((out != NULL) && passthru_is_passthrough_stream(out)) &&
534 !passthru_is_convert_supported(out->dev, out))
Manish Dewangan672001f2017-08-16 13:44:07 +0530535 return true;
536 else
537 return false;
538}
539
Arun Mirpurie008ed22019-03-21 11:21:04 -0700540int passthru_get_buffer_size(audio_offload_info_t* info)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530541{
Manish Dewangan37864bc2017-06-09 12:28:37 +0530542 uint32_t fragment_size = MIN_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
543 char value[PROPERTY_VALUE_MAX] = {0};
544
545 if (((info->format == AUDIO_FORMAT_DOLBY_TRUEHD) ||
546 (info->format == AUDIO_FORMAT_IEC61937)) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +0530547 property_get("vendor.audio.truehd.buffer.size.kb", value, "") &&
Manish Dewangan37864bc2017-06-09 12:28:37 +0530548 atoi(value)) {
549 fragment_size = atoi(value) * 1024;
550 goto done;
551 } else if ((info->format == AUDIO_FORMAT_DTS) ||
552 (info->format == AUDIO_FORMAT_DTS_HD)) {
553 fragment_size = MAX_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
554 goto done;
Harsh Bansalc83207c2017-09-01 16:04:36 +0530555 } else if (info->format == AUDIO_FORMAT_E_AC3) {
556 fragment_size = DDP_COMPRESS_PASSTHROUGH_FRAGMENT_SIZE;
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +0530557 if(property_get("vendor.audio.ddp.buffer.size.kb", value, "") &&
Harsh Bansalc83207c2017-09-01 16:04:36 +0530558 atoi(value)) {
559 fragment_size = atoi(value) * 1024;
560 }
561 goto done;
Manish Dewangan37864bc2017-06-09 12:28:37 +0530562 }
Manish Dewangan37864bc2017-06-09 12:28:37 +0530563done:
564 return fragment_size;
565
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530566}
567
Arun Mirpurie008ed22019-03-21 11:21:04 -0700568int passthru_set_volume(struct stream_out *out, int mute)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530569{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700570 return fp_platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530571}
572
Arun Mirpurie008ed22019-03-21 11:21:04 -0700573int passthru_set_latency(struct stream_out *out, int latency)
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530574{
Arun Mirpurie008ed22019-03-21 11:21:04 -0700575 return fp_platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530576}
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530577
Arun Mirpurie008ed22019-03-21 11:21:04 -0700578bool passthru_is_supported_backend_edid_cfg(struct audio_device *adev,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530579 struct stream_out *out)
580{
581 struct audio_backend_cfg backend_cfg;
mpang787a6aa2018-11-29 11:25:58 +0800582 backend_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
583 backend_cfg.channels = CODEC_BACKEND_DEFAULT_CHANNELS;
584 backend_cfg.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
585 backend_cfg.format = AUDIO_FORMAT_PCM_16_BIT;
586 backend_cfg.passthrough_enabled = false;
587
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530588 snd_device_t out_snd_device = SND_DEVICE_NONE;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700589 int max_edid_channels = fp_platform_edid_get_max_channels(out->dev->platform);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530590
Arun Mirpurie008ed22019-03-21 11:21:04 -0700591 out_snd_device = fp_platform_get_output_snd_device(adev->platform, out);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530592
Arun Mirpurie008ed22019-03-21 11:21:04 -0700593 if (fp_platform_get_codec_backend_cfg(adev, out_snd_device, &backend_cfg)) {
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530594 ALOGE("%s: ERROR: Unable to get current backend config!!!", __func__);
595 return false;
596 }
597
598 ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d format %d"
599 ", device (%s)", __func__, backend_cfg.bit_width,
600 backend_cfg.sample_rate, backend_cfg.channels, backend_cfg.format,
Arun Mirpurie008ed22019-03-21 11:21:04 -0700601 fp_platform_get_snd_device_name(out_snd_device));
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530602
603 /* Check if the channels are supported */
Garmond Leung438932f2017-10-04 19:35:18 -0700604 if (max_edid_channels < (int)backend_cfg.channels) {
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530605
606 ALOGE("%s: ERROR: Unsupported channels in passthru mode!!!"
607 " max_edid_channels - %d backend_channels - %d",
608 __func__, max_edid_channels, backend_cfg.channels);
609 return false;
610 }
611
612 /* Check if the sample rate supported */
Arun Mirpurie008ed22019-03-21 11:21:04 -0700613 if (!fp_platform_is_edid_supported_sample_rate(adev->platform,
Satish Babu Patakokila5933e972017-08-24 12:22:08 +0530614 backend_cfg.sample_rate)) {
615
616 ALOGE("%s: ERROR: Unsupported sample rate in passthru mode!!!"
617 " backend_samplerate - %d",
618 __func__, backend_cfg.sample_rate);
619 return false;
620 }
621
622 return true;
623}