blob: 7a4a0edbb681813668f03521f1c30fe97005ad9f [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Dhananjay Kumar14245982017-01-16 20:21:00 +05302 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_utils"
21/* #define LOG_NDEBUG 0 */
22
23#include <errno.h>
24#include <cutils/properties.h>
25#include <cutils/config_utils.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <cutils/str_parms.h>
29#include <cutils/log.h>
30#include <cutils/misc.h>
31
32#include "audio_hw.h"
33#include "platform.h"
34#include "platform_api.h"
35#include "audio_extn.h"
Narsinga Rao Chella212e2542014-11-17 19:57:04 -080036#include "voice.h"
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053037#include "sound/compress_params.h"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070038
Ashish Jain81eb2a82015-05-13 10:52:34 +053039#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
40#ifdef HDMI_PASSTHROUGH_ENABLED
41#include "audio_parsers.h"
42#endif
43#endif
44
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053045#ifdef LINUX_ENABLED
46#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053047#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053048#else
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070049#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053050#define AUDIO_IO_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_io_policy.conf"
Yamit Mehtaa0d653a2016-11-25 20:33:25 +053051#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070052
53#define OUTPUTS_TAG "outputs"
Dhananjay Kumard6d32152016-10-13 16:11:03 +053054#define INPUTS_TAG "inputs"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070055
56#define DYNAMIC_VALUE_TAG "dynamic"
57#define FLAGS_TAG "flags"
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +053058#define PROFILES_TAG "profile"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070059#define FORMATS_TAG "formats"
60#define SAMPLING_RATES_TAG "sampling_rates"
61#define BIT_WIDTH_TAG "bit_width"
62#define APP_TYPE_TAG "app_type"
63
64#define STRING_TO_ENUM(string) { #string, string }
65#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
66
Ben Rombergera04fabc2014-11-14 12:16:03 -080067#define BASE_TABLE_SIZE 64
68#define MAX_BASEINDEX_LEN 256
69
Ashish Jain81eb2a82015-05-13 10:52:34 +053070#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
71#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
72#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
73#define SR_44100 (0<<0) /* 44.1kHz */
74#define SR_NOTID (1<<0) /* non indicated */
75#define SR_48000 (2<<0) /* 48kHz */
76#define SR_32000 (3<<0) /* 32kHz */
77#define SR_22050 (4<<0) /* 22.05kHz */
78#define SR_24000 (6<<0) /* 24kHz */
79#define SR_88200 (8<<0) /* 88.2kHz */
80#define SR_96000 (10<<0) /* 96kHz */
81#define SR_176400 (12<<0) /* 176.4kHz */
82#define SR_192000 (14<<0) /* 192kHz */
83
84#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070085struct string_to_enum {
86 const char *name;
87 uint32_t value;
88};
89
90const struct string_to_enum s_flag_name_to_enum_table[] = {
91 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070092 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070093 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
94 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
95 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
96 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
97 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070098 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070099#ifdef INCALL_MUSIC_ENABLED
100 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
101#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700102 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530103 STRING_TO_ENUM(AUDIO_INPUT_FLAG_NONE),
104 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
105 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
106 STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
107 STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530108 STRING_TO_ENUM(AUDIO_INPUT_FLAG_TIMESTAMP),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109};
110
111const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530112 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530114 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
115 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530116 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700117 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
118 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
119 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700120 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
121 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700122 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700123 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700124 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530125 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
126#ifdef AUDIO_EXTN_FORMATS_ENABLED
127 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
129 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700131 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
132 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
133 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
134 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
135 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
136 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
137 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700138 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530139 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
140 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700141 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
144 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530145 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530146 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
147 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
148 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530149 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530150 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM),
151 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_LC),
152 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V1),
153 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LATM_HE_V2),
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530154 STRING_TO_ENUM(AUDIO_FORMAT_APTX),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700155#endif
156};
157
Ben Rombergera04fabc2014-11-14 12:16:03 -0800158static char bTable[BASE_TABLE_SIZE] = {
159 'A','B','C','D','E','F','G','H','I','J','K','L',
160 'M','N','O','P','Q','R','S','T','U','V','W','X',
161 'Y','Z','a','b','c','d','e','f','g','h','i','j',
162 'k','l','m','n','o','p','q','r','s','t','u','v',
163 'w','x','y','z','0','1','2','3','4','5','6','7',
164 '8','9','+','/'
165};
166
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700167static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
168 const char *name)
169{
170 size_t i;
171 for (i = 0; i < size; i++) {
172 if (strcmp(table[i].name, name) == 0) {
173 ALOGV("%s found %s", __func__, table[i].name);
174 return table[i].value;
175 }
176 }
177 return 0;
178}
179
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530180static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181{
182 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530183 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800184 char *last_r;
185 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700186 while (flag_name != NULL) {
187 if (strlen(flag_name) != 0) {
188 flag |= string_to_enum(s_flag_name_to_enum_table,
189 ARRAY_SIZE(s_flag_name_to_enum_table),
190 flag_name);
191 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800192 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700193 }
194
195 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530196 io_flags.in_flags = (audio_input_flags_t)flag;
197 io_flags.out_flags = (audio_output_flags_t)flag;
198 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199}
200
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530201static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202{
203 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800204 char *last_r;
205 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700206
207 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
208 return;
209
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530210 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211 while (str != NULL) {
212 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
213 ARRAY_SIZE(s_format_name_to_enum_table), str);
214 ALOGV("%s: format - %d", __func__, format);
215 if (format != 0) {
216 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700217 if (sf_info == NULL)
218 break; /* return whatever was parsed */
219
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700220 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530221 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700222 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800223 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700224 }
225}
226
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530227static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700229 struct stream_sample_rate *ss_info = NULL;
230 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800231 char *last_r;
232 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700233
Amit Shekhar6f461b12014-08-01 14:52:58 -0700234 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
235 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530237 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700238 while (str != NULL) {
239 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
240 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
241 if (0 != sample_rate) {
242 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800243 if (!ss_info) {
244 ALOGE("%s: memory allocation failure", __func__);
245 return;
246 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700247 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530248 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700249 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800250 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700251 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700252}
253
254static int parse_bit_width_names(char *name)
255{
256 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800257 char *last_r;
258 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700259
260 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
261 bit_width = (int)strtol(str, (char **)NULL, 10);
262
263 ALOGV("%s: bit_width - %d", __func__, bit_width);
264 return bit_width;
265}
266
267static int parse_app_type_names(void *platform, char *name)
268{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700269 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800270 char *last_r;
271 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272
273 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
274 app_type = (int)strtol(str, (char **)NULL, 10);
275
276 ALOGV("%s: app_type - %d", __func__, app_type);
277 return app_type;
278}
279
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530280static void update_streams_cfg_list(cnode *root, void *platform,
281 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700282{
283 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700285
286 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530287 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700288
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530289 if (!s_info) {
290 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700291 return;
292 }
293
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 while (node) {
295 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530296 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530297 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
298 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
303 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700304 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530305 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700306 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530307 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700308 }
309 node = node->next;
310 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530311 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312}
313
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530314static void load_cfg_list(cnode *root, void *platform,
315 struct listnode *streams_output_cfg_list,
316 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700317{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530318 cnode *node = NULL;
319
320 node = config_find(root, OUTPUTS_TAG);
321 if (node != NULL) {
322 node = node->first_child;
323 while (node) {
324 ALOGV("%s: loading output %s", __func__, node->name);
325 update_streams_cfg_list(node, platform, streams_output_cfg_list);
326 node = node->next;
327 }
328 } else {
329 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 }
331
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530332 node = config_find(root, INPUTS_TAG);
333 if (node != NULL) {
334 node = node->first_child;
335 while (node) {
336 ALOGV("%s: loading input %s", __func__, node->name);
337 update_streams_cfg_list(node, platform, streams_input_cfg_list);
338 node = node->next;
339 }
340 } else {
341 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700342 }
343}
344
345static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530346 struct listnode *streams_output_cfg_list,
347 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700348{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530349 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700350 int length = 0, i, num_app_types = 0;
351 struct listnode *node;
352 bool update;
353 struct mixer_ctl *ctl = NULL;
354 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530355 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700356
357 if (!mixer) {
358 ALOGE("%s: mixer is null",__func__);
359 return;
360 }
361 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
362 if (!ctl) {
363 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
364 return;
365 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530366 app_type_cfg[length++] = num_app_types;
367
368 if (list_empty(streams_output_cfg_list)) {
369 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700370 app_type_cfg[length++] = 48000;
371 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530372 num_app_types += 1;
373 }
374 if (list_empty(streams_input_cfg_list)) {
375 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
376 app_type_cfg[length++] = 48000;
377 app_type_cfg[length++] = 16;
378 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700379 }
380
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700383 update = true;
384 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530385 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700386 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530387 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530388 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530389 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530390 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530391 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392 update = false;
393 break;
394 }
395 }
396 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530397 num_app_types += 1;
398 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
399 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
400 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
401 }
402 }
403 list_for_each(node, streams_input_cfg_list) {
404 s_info = node_to_item(node, struct streams_io_cfg, list);
405 update = true;
406 for (i=0; i<length; i=i+3) {
407 if (app_type_cfg[i+1] == 0)
408 break;
409 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530410 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530411 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530412 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530413 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
414 update = false;
415 break;
416 }
417 }
418 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
419 num_app_types += 1;
420 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
421 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
422 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423 }
424 }
425 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
426 if (num_app_types) {
427 app_type_cfg[0] = num_app_types;
428 mixer_ctl_set_array(ctl, app_type_cfg, length);
429 }
430}
431
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530432void audio_extn_utils_update_streams_cfg_lists(void *platform,
433 struct mixer *mixer,
434 struct listnode *streams_output_cfg_list,
435 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700436{
437 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530438 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700439
440 ALOGV("%s", __func__);
441 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530442 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700443
444 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700445 if (root == NULL) {
446 ALOGE("cfg_list, NULL config root");
447 return;
448 }
449
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530450 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
451 if (data == NULL) {
452 ALOGD("%s: failed to open io config file(%s), trying older config file",
453 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
454 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
455 if (data == NULL) {
456 send_app_type_cfg(platform, mixer,
457 streams_output_cfg_list,
458 streams_input_cfg_list);
459 ALOGE("%s: could not load io policy config!", __func__);
460 return;
461 }
462 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700463
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530464 config_load(root, data);
465 load_cfg_list(root, platform, streams_output_cfg_list,
466 streams_input_cfg_list);
467
468 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
469 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800470
471 config_free(root);
472 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473}
474
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530475static void audio_extn_utils_dump_streams_cfg_list(
476 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700478 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530479 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700480 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700481 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530482
483 list_for_each(node_i, streams_cfg_list) {
484 s_info = node_to_item(node_i, struct streams_io_cfg, list);
485 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
486 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
487 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
488 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 sf_info = node_to_item(node_j, struct stream_format, list);
490 ALOGV("format-%x", sf_info->format);
491 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530492 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700493 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
494 ALOGV("sample rate-%d", ss_info->sample_rate);
495 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700496 }
497}
498
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530499void audio_extn_utils_dump_streams_cfg_lists(
500 struct listnode *streams_output_cfg_list,
501 struct listnode *streams_input_cfg_list)
502{
503 ALOGV("%s", __func__);
504 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
505 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
506}
507
508static void audio_extn_utils_release_streams_cfg_list(
509 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700510{
511 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530512 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700513
514 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530515
516 while (!list_empty(streams_cfg_list)) {
517 node_i = list_head(streams_cfg_list);
518 s_info = node_to_item(node_i, struct streams_io_cfg, list);
519 while (!list_empty(&s_info->format_list)) {
520 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700521 list_remove(node_j);
522 free(node_to_item(node_j, struct stream_format, list));
523 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530524 while (!list_empty(&s_info->sample_rate_list)) {
525 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700526 list_remove(node_j);
527 free(node_to_item(node_j, struct stream_sample_rate, list));
528 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700529 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530530 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700531 }
532}
533
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530534void audio_extn_utils_release_streams_cfg_lists(
535 struct listnode *streams_output_cfg_list,
536 struct listnode *streams_input_cfg_list)
537{
538 ALOGV("%s", __func__);
539 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
540 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
541}
542
543static bool set_app_type_cfg(struct streams_io_cfg *s_info,
544 struct stream_app_type_cfg *app_type_cfg,
545 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700546 {
547 struct listnode *node_i;
548 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
551 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530552 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700553
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530554 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700555 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530556 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700557 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
558 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
559 return true;
560 }
561 }
562 /*
563 * Reiterate through the list assuming dafault sample rate.
564 * Handles scenario where input sample rate is higher
565 * than all sample rates in list for the input bit width.
566 */
567 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800568
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530569 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700570 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
571 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530572 (bit_width == s_info->app_type_cfg.bit_width)) {
573 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700574 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530575 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800576 ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
Amit Shekhar6f461b12014-08-01 14:52:58 -0700577 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
578 return true;
579 }
580 }
581 return false;
582}
583
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530584void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
585 struct listnode *streams_input_cfg_list,
586 audio_devices_t devices __unused,
587 audio_input_flags_t flags,
588 audio_format_t format,
589 uint32_t sample_rate,
590 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530591 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530592 struct stream_app_type_cfg *app_type_cfg)
593{
594 struct listnode *node_i, *node_j;
595 struct streams_io_cfg *s_info;
596 struct stream_format *sf_info;
597
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530598 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
599 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530600
601 list_for_each(node_i, streams_input_cfg_list) {
602 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530603 /* Along with flags do profile matching if set at either end.*/
604 if (s_info->flags.in_flags == flags &&
605 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
606 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530607 list_for_each(node_j, &s_info->format_list) {
608 sf_info = node_to_item(node_j, struct stream_format, list);
609 if (sf_info->format == format) {
610 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
611 return;
612 }
613 }
614 }
615 }
616 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
617 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
618 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
619 app_type_cfg->bit_width = 16;
620}
621
622void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700623 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700624 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700625 audio_output_flags_t flags,
626 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700627 uint32_t sample_rate,
628 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530629 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530630 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700631 struct stream_app_type_cfg *app_type_cfg)
632{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530633 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530634 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700635 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530636 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700637
Ashish Jain058165c2016-09-28 23:18:48 +0530638 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700639 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700640 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
641 if (-ENOSYS != bw)
642 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700643 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
644 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
645 }
646
Manish Dewangan837dc462015-05-27 10:17:41 +0530647 property_get("audio.playback.mch.downsample",value,"");
648 if (!strncmp("true", value, sizeof("true"))) {
649 if ((popcount(channel_mask) > 2) &&
650 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
651 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
652 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
653 ALOGD("%s: MCH session defaulting sample rate to %d",
654 __func__, sample_rate);
655 }
656 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530657
658 /* Set sampling rate to 176.4 for DSD64
659 * and 352.8Khz for DSD128.
660 * Set Bit Width to 16. output will be 16 bit
661 * post DoP in ASM.
662 */
663 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
664 (format == AUDIO_FORMAT_DSD)) {
665 bit_width = 16;
666 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
667 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
668 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
669 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
670 }
671
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530672 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
673 //TODO: Handle fractional sampling rate configuration for LL
674 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
675 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
676 __func__, sample_rate, bit_width);
677 }
678
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530679 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
680 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530683 /* Along with flags do profile matching if set at either end.*/
684 if (s_info->flags.out_flags == flags &&
685 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
686 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530687 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700688 sf_info = node_to_item(node_j, struct stream_format, list);
689 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530690 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700691 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692 }
693 }
694 }
695 }
696 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530697 s_info = node_to_item(node_i, struct streams_io_cfg, list);
698 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700699 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530700 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
701 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
702 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700703 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530704 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700705 return;
706 }
707 }
708 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700709 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700710 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700711 app_type_cfg->bit_width = 16;
712}
713
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530714static bool audio_is_this_native_usecase(struct audio_usecase *uc)
715{
716 bool native_usecase = false;
717 struct stream_out *out = (struct stream_out*) uc->stream.out;
718
719 if (PCM_PLAYBACK == uc->type && out != NULL &&
720 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
721 is_offload_usecase(uc->id) &&
722 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
723 native_usecase = true;
724
725 return native_usecase;
726}
727
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530728void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
729 struct audio_device *adev,
730 struct audio_usecase *usecase)
731{
732 ALOGV("%s", __func__);
733
734 switch(usecase->type) {
735 case PCM_PLAYBACK:
736 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
737 &adev->streams_output_cfg_list,
738 usecase->stream.out->devices,
739 usecase->stream.out->flags,
740 usecase->stream.out->format,
741 usecase->stream.out->sample_rate,
742 usecase->stream.out->bit_width,
743 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530744 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530745 &usecase->stream.out->app_type_cfg);
746 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
747 break;
748 case PCM_CAPTURE:
749 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
750 &adev->streams_input_cfg_list,
751 usecase->stream.in->device,
752 usecase->stream.in->flags,
753 usecase->stream.in->format,
754 usecase->stream.in->sample_rate,
755 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530756 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530757 &usecase->stream.in->app_type_cfg);
758 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
759 break;
760 default:
761 ALOGE("%s: app type cfg not supported for usecase type (%d)",
762 __func__, usecase->type);
763 }
764}
765
Ben Romberger1fafdde2015-09-09 19:43:15 -0700766int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
767 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700768{
769 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530770 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
771 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700772 struct mixer_ctl *ctl;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530773 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530774 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530775 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776
777 ALOGV("%s", __func__);
778
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530779 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530780 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700781 rc = 0;
782 goto exit_send_app_type_cfg;
783 }
784 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
785 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
786 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530787 (!is_offload_usecase(usecase->id)) &&
788 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530789 ALOGV("%s: a rx/tx/loopback path where app type cfg is not required %d", __func__, usecase->id);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700790 rc = 0;
791 goto exit_send_app_type_cfg;
792 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530793 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530794 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700795 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530796 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
797 "Audio Stream %d App Type Cfg", pcm_device_id);
798 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700799 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530800 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700801 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530802 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
803 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
804 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->in_snd_device);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530805 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530806 if (acdb_dev_id <= 0) {
807 ALOGE("%s: Couldn't get the acdb dev id", __func__);
808 rc = -EINVAL;
809 goto exit_send_app_type_cfg;
810 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700811
812 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
813 if (!ctl) {
814 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
815 mixer_ctl_name);
816 rc = -EINVAL;
817 goto exit_send_app_type_cfg;
818 }
819 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800820 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530821
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530822 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
823 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
824 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
825 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
826 !(audio_is_this_native_usecase(usecase))) ||
827 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
828 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
829 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530830
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530831 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530832
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530833 property_get("audio.playback.mch.downsample",value,"");
834 if (!strncmp("true", value, sizeof("true"))) {
835 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
836 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
837 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
838 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
839 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530840
Mingming Yin21854652016-04-13 11:54:02 -0700841 if ((24 == usecase->stream.out->bit_width) &&
842 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
843 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530844 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
845 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
846 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
847 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
848 /*
849 * To best utlize DSP, check if the stream sample rate is supported/multiple of
850 * configured device sample rate, if not update the COPP rate to be equal to the
851 * device sample rate, else open COPP at stream sample rate
852 */
853 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
854 usecase->stream.out->sample_rate,
855 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700856 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
857 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
858 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
859 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
860 }
861 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
862
863 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
864 app_type_cfg[len++] = acdb_dev_id;
865 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
866 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530867 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700868 app_type_cfg[len++] = sample_rate * 4;
869 } else {
870 app_type_cfg[len++] = sample_rate;
871 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530872
873 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
874 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
875
876 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
877 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.app_type;
878 app_type_cfg[len++] = acdb_dev_id;
879 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.sample_rate;
880 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
881 __func__, usecase->stream.in->app_type_cfg.app_type, acdb_dev_id,
882 usecase->stream.in->app_type_cfg.sample_rate);
883 } else {
884 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
885 app_type_cfg[len++] = acdb_dev_id;
886 app_type_cfg[len++] = sample_rate;
887 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d",
888 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
889 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530890 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530891
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700892 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700893 rc = 0;
894exit_send_app_type_cfg:
895 return rc;
896}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700897
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530898int read_line_from_file(const char *path, char *buf, size_t count)
899{
900 char * fgets_ret;
901 FILE * fd;
902 int rv;
903
904 fd = fopen(path, "r");
905 if (fd == NULL)
906 return -1;
907
908 fgets_ret = fgets(buf, (int)count, fd);
909 if (NULL != fgets_ret) {
910 rv = (int)strlen(buf);
911 } else {
912 rv = ferror(fd);
913 }
914 fclose(fd);
915
916 return rv;
917}
918
Ashish Jainf1eaa582016-05-23 20:54:24 +0530919/*Translates ALSA formats to AOSP PCM formats*/
920audio_format_t alsa_format_to_hal(uint32_t alsa_format)
921{
922 audio_format_t format;
923
924 switch(alsa_format) {
925 case SNDRV_PCM_FORMAT_S16_LE:
926 format = AUDIO_FORMAT_PCM_16_BIT;
927 break;
928 case SNDRV_PCM_FORMAT_S24_3LE:
929 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
930 break;
931 case SNDRV_PCM_FORMAT_S24_LE:
932 format = AUDIO_FORMAT_PCM_8_24_BIT;
933 break;
934 case SNDRV_PCM_FORMAT_S32_LE:
935 format = AUDIO_FORMAT_PCM_32_BIT;
936 break;
937 default:
938 ALOGW("Incorrect ALSA format");
939 format = AUDIO_FORMAT_INVALID;
940 }
941 return format;
942}
943
944/*Translates hal format (AOSP) to alsa formats*/
945uint32_t hal_format_to_alsa(audio_format_t hal_format)
946{
947 uint32_t alsa_format;
948
949 switch (hal_format) {
950 case AUDIO_FORMAT_PCM_32_BIT: {
951 if (platform_supports_true_32bit())
952 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
953 else
954 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
955 }
956 break;
957 case AUDIO_FORMAT_PCM_8_BIT:
958 alsa_format = SNDRV_PCM_FORMAT_S8;
959 break;
960 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
961 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
962 break;
963 case AUDIO_FORMAT_PCM_8_24_BIT: {
964 if (platform_supports_true_32bit())
965 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
966 else
967 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
968 }
969 break;
970 case AUDIO_FORMAT_PCM_FLOAT:
971 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
972 break;
973 default:
974 case AUDIO_FORMAT_PCM_16_BIT:
975 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
976 break;
977 }
978 return alsa_format;
979}
980
Ashish Jain83a6cc22016-06-28 14:34:17 +0530981/*Translates PCM formats to AOSP formats*/
982audio_format_t pcm_format_to_hal(uint32_t pcm_format)
983{
984 audio_format_t format = AUDIO_FORMAT_INVALID;
985
986 switch(pcm_format) {
987 case PCM_FORMAT_S16_LE:
988 format = AUDIO_FORMAT_PCM_16_BIT;
989 break;
990 case PCM_FORMAT_S24_3LE:
991 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
992 break;
993 case PCM_FORMAT_S24_LE:
994 format = AUDIO_FORMAT_PCM_8_24_BIT;
995 break;
996 case PCM_FORMAT_S32_LE:
997 format = AUDIO_FORMAT_PCM_32_BIT;
998 break;
999 default:
1000 ALOGW("Incorrect PCM format");
1001 format = AUDIO_FORMAT_INVALID;
1002 }
1003 return format;
1004}
1005
1006/*Translates hal format (AOSP) to alsa formats*/
1007uint32_t hal_format_to_pcm(audio_format_t hal_format)
1008{
1009 uint32_t pcm_format;
1010
1011 switch (hal_format) {
1012 case AUDIO_FORMAT_PCM_32_BIT:
1013 case AUDIO_FORMAT_PCM_8_24_BIT:
1014 case AUDIO_FORMAT_PCM_FLOAT: {
1015 if (platform_supports_true_32bit())
1016 pcm_format = PCM_FORMAT_S32_LE;
1017 else
1018 pcm_format = PCM_FORMAT_S24_3LE;
1019 }
1020 break;
1021 case AUDIO_FORMAT_PCM_8_BIT:
1022 pcm_format = PCM_FORMAT_S8;
1023 break;
1024 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1025 pcm_format = PCM_FORMAT_S24_3LE;
1026 break;
1027 default:
1028 case AUDIO_FORMAT_PCM_16_BIT:
1029 pcm_format = PCM_FORMAT_S16_LE;
1030 break;
1031 }
1032 return pcm_format;
1033}
1034
Ashish Jainf1eaa582016-05-23 20:54:24 +05301035uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1036 uint32_t sample_rate,
1037 uint32_t noOfChannels)
1038{
1039 uint32_t fragment_size = 0;
1040 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1041
1042 fragment_size = (pcm_offload_time
1043 * sample_rate
1044 * bytes_per_sample
1045 * noOfChannels)/1000;
1046 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1047 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1048 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1049 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1050 /*To have same PCM samples for all channels, the buffer size requires to
1051 *be multiple of (number of channels * bytes per sample)
1052 *For writes to succeed, the buffer must be written at address which is multiple of 32
1053 */
1054 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1055
1056 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1057 return fragment_size;
1058}
1059
1060/* Calculates the fragment size required to configure compress session.
1061 * Based on the alsa format selected, decide if conversion is needed in
1062
1063 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1064 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1065 */
1066void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1067{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301068 audio_format_t dst_format = out->hal_op_format;
1069 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301070 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1071 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1072
1073 out->compr_config.fragment_size =
1074 get_alsa_fragment_size(hal_op_bytes_per_sample,
1075 out->sample_rate,
1076 popcount(out->channel_mask));
1077
1078 if ((src_format != dst_format) &&
1079 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1080
Ashish Jain83a6cc22016-06-28 14:34:17 +05301081 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301082 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1083 hal_op_bytes_per_sample);
1084 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301085 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301086 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301087 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301088 }
1089}
1090
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301091/* converts pcm format 24_8 to 8_24 inplace */
1092size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1093{
1094 size_t i = 0;
1095 int *int_buf_stream = buf;
1096
1097 if ((bytes % 4) != 0) {
1098 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1099 return -EINVAL;
1100 }
1101
1102 for (; i < (bytes / 4); i++)
1103 int_buf_stream[i] >>= 8;
1104
1105 return bytes;
1106}
1107
1108int get_snd_codec_id(audio_format_t format)
1109{
1110 int id = 0;
1111
1112 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1113 case AUDIO_FORMAT_MP3:
1114 id = SND_AUDIOCODEC_MP3;
1115 break;
1116 case AUDIO_FORMAT_AAC:
1117 id = SND_AUDIOCODEC_AAC;
1118 break;
1119 case AUDIO_FORMAT_AAC_ADTS:
1120 id = SND_AUDIOCODEC_AAC;
1121 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301122 case AUDIO_FORMAT_AAC_LATM:
1123 id = SND_AUDIOCODEC_AAC;
1124 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301125 case AUDIO_FORMAT_PCM_OFFLOAD:
1126 case AUDIO_FORMAT_PCM:
1127 id = SND_AUDIOCODEC_PCM;
1128 break;
1129 case AUDIO_FORMAT_FLAC:
1130 id = SND_AUDIOCODEC_FLAC;
1131 break;
1132 case AUDIO_FORMAT_ALAC:
1133 id = SND_AUDIOCODEC_ALAC;
1134 break;
1135 case AUDIO_FORMAT_APE:
1136 id = SND_AUDIOCODEC_APE;
1137 break;
1138 case AUDIO_FORMAT_VORBIS:
1139 id = SND_AUDIOCODEC_VORBIS;
1140 break;
1141 case AUDIO_FORMAT_WMA:
1142 id = SND_AUDIOCODEC_WMA;
1143 break;
1144 case AUDIO_FORMAT_WMA_PRO:
1145 id = SND_AUDIOCODEC_WMA_PRO;
1146 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301147 case AUDIO_FORMAT_MP2:
1148 id = SND_AUDIOCODEC_MP2;
1149 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301150 case AUDIO_FORMAT_AC3:
1151 id = SND_AUDIOCODEC_AC3;
1152 break;
1153 case AUDIO_FORMAT_E_AC3:
1154 case AUDIO_FORMAT_E_AC3_JOC:
1155 id = SND_AUDIOCODEC_EAC3;
1156 break;
1157 case AUDIO_FORMAT_DTS:
1158 case AUDIO_FORMAT_DTS_HD:
1159 id = SND_AUDIOCODEC_DTS;
1160 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301161 case AUDIO_FORMAT_DSD:
1162 id = SND_AUDIOCODEC_DSD;
1163 break;
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301164 case AUDIO_FORMAT_APTX:
1165 id = SND_AUDIOCODEC_APTX;
1166 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301167 default:
1168 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1169 }
1170
1171 return id;
1172}
1173
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001174void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1175 struct audio_usecase *usecase)
1176{
1177 int type = usecase->type;
1178
Dhananjay Kumar14245982017-01-16 20:21:00 +05301179 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001180 struct stream_out *out = usecase->stream.out;
1181 int snd_device = usecase->out_snd_device;
1182 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001183 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301184 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001185 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301186 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301187 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301188 platform_send_audio_calibration(adev->platform, usecase,
1189 usecase->stream.in->app_type_cfg.app_type,
1190 usecase->stream.in->app_type_cfg.sample_rate);
1191 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301192 /* when app type is default. the sample rate is not used to send cal */
1193 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301194 platform_get_default_app_type_v2(adev->platform, usecase->type),
1195 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001196 }
1197}
1198
Ben Rombergera04fabc2014-11-14 12:16:03 -08001199// Base64 Encode and Decode
1200// Not all features supported. This must be used only with following conditions.
1201// Decode Modes: Support with and without padding
1202// CRLF not handling. So no CRLF in string to decode.
1203// Encode Modes: Supports only padding
1204int b64decode(char *inp, int ilen, uint8_t* outp)
1205{
1206 int i, j, k, ii, num;
1207 int rem, pcnt;
1208 uint32_t res=0;
1209 uint8_t getIndex[MAX_BASEINDEX_LEN];
1210 uint8_t tmp, cflag;
1211
1212 if(inp == NULL || outp == NULL || ilen <= 0) {
1213 ALOGE("[%s] received NULL pointer or zero length",__func__);
1214 return -1;
1215 }
1216
1217 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1218 for(i=0;i<BASE_TABLE_SIZE;i++) {
1219 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1220 }
1221 getIndex[(uint8_t)'=']=0;
1222
1223 j=0;k=0;
1224 num = ilen/4;
1225 rem = ilen%4;
1226 if(rem==0)
1227 num = num-1;
1228 cflag=0;
1229 for(i=0; i<num; i++) {
1230 res=0;
1231 for(ii=0;ii<4;ii++) {
1232 res = res << 6;
1233 tmp = getIndex[(uint8_t)inp[j++]];
1234 res = res | tmp;
1235 cflag = cflag | tmp;
1236 }
1237 outp[k++] = (res >> 16)&0xFF;
1238 outp[k++] = (res >> 8)&0xFF;
1239 outp[k++] = res & 0xFF;
1240 }
1241
1242 // Handle last bytes special
1243 pcnt=0;
1244 if(rem == 0) {
1245 //With padding or full data
1246 res = 0;
1247 for(ii=0;ii<4;ii++) {
1248 if(inp[j] == '=')
1249 pcnt++;
1250 res = res << 6;
1251 tmp = getIndex[(uint8_t)inp[j++]];
1252 res = res | tmp;
1253 cflag = cflag | tmp;
1254 }
1255 outp[k++] = res >> 16;
1256 if(pcnt == 2)
1257 goto done;
1258 outp[k++] = (res>>8)&0xFF;
1259 if(pcnt == 1)
1260 goto done;
1261 outp[k++] = res&0xFF;
1262 } else {
1263 //without padding
1264 res = 0;
1265 for(i=0;i<rem;i++) {
1266 res = res << 6;
1267 tmp = getIndex[(uint8_t)inp[j++]];
1268 res = res | tmp;
1269 cflag = cflag | tmp;
1270 }
1271 for(i=rem;i<4;i++) {
1272 res = res << 6;
1273 pcnt++;
1274 }
1275 outp[k++] = res >> 16;
1276 if(pcnt == 2)
1277 goto done;
1278 outp[k++] = (res>>8)&0xFF;
1279 if(pcnt == 1)
1280 goto done;
1281 outp[k++] = res&0xFF;
1282 }
1283done:
1284 if(cflag == 0xFF) {
1285 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1286 __func__, inp);
1287 return 0;
1288 }
1289 return k;
1290}
1291
1292int b64encode(uint8_t *inp, int ilen, char* outp)
1293{
1294 int i,j,k, num;
1295 int rem=0;
1296 uint32_t res=0;
1297
1298 if(inp == NULL || outp == NULL || ilen<=0) {
1299 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1300 return -1;
1301 }
1302
1303 num = ilen/3;
1304 rem = ilen%3;
1305 j=0;k=0;
1306 for(i=0; i<num; i++) {
1307 //prepare index
1308 res = inp[j++]<<16;
1309 res = res | inp[j++]<<8;
1310 res = res | inp[j++];
1311 //get output map from index
1312 outp[k++] = (char) bTable[(res>>18)&0x3F];
1313 outp[k++] = (char) bTable[(res>>12)&0x3F];
1314 outp[k++] = (char) bTable[(res>>6)&0x3F];
1315 outp[k++] = (char) bTable[res&0x3F];
1316 }
1317
1318 switch(rem) {
1319 case 1:
1320 res = inp[j++]<<16;
1321 outp[k++] = (char) bTable[res>>18];
1322 outp[k++] = (char) bTable[(res>>12)&0x3F];
1323 //outp[k++] = '=';
1324 //outp[k++] = '=';
1325 break;
1326 case 2:
1327 res = inp[j++]<<16;
1328 res = res | inp[j++]<<8;
1329 outp[k++] = (char) bTable[res>>18];
1330 outp[k++] = (char) bTable[(res>>12)&0x3F];
1331 outp[k++] = (char) bTable[(res>>6)&0x3F];
1332 //outp[k++] = '=';
1333 break;
1334 default:
1335 break;
1336 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001337 outp[k] = '\0';
1338 return k;
1339}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301340
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301341
1342int audio_extn_utils_get_codec_version(const char *snd_card_name,
1343 int card_num,
1344 char *codec_version)
1345{
1346 char procfs_path[50];
1347 FILE *fp;
1348
1349 if (strstr(snd_card_name, "tasha")) {
1350 snprintf(procfs_path, sizeof(procfs_path),
1351 "/proc/asound/card%d/codecs/tasha/version", card_num);
1352 if ((fp = fopen(procfs_path, "r")) != NULL) {
1353 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1354 fclose(fp);
1355 } else {
1356 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1357 return -ENOENT;
1358 }
1359 ALOGD("%s: codec version %s", __func__, codec_version);
1360 }
1361
1362 return 0;
1363}
1364
1365
Ashish Jain81eb2a82015-05-13 10:52:34 +05301366#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1367
1368void get_default_compressed_channel_status(
1369 unsigned char *channel_status)
1370{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301371 memset(channel_status,0,24);
1372
1373 /* block start bit in preamble bit 3 */
1374 channel_status[0] |= PROFESSIONAL;
1375 //compre out
1376 channel_status[0] |= NON_LPCM;
1377 // sample rate; fixed 48K for default/transcode
1378 channel_status[3] |= SR_48000;
1379}
1380
1381#ifdef HDMI_PASSTHROUGH_ENABLED
1382int32_t get_compressed_channel_status(void *audio_stream_data,
1383 uint32_t audio_frame_size,
1384 unsigned char *channel_status,
1385 enum audio_parser_code_type codec_type)
1386 // codec_type - AUDIO_PARSER_CODEC_AC3
1387 // - AUDIO_PARSER_CODEC_DTS
1388{
1389 unsigned char *stream;
1390 int ret = 0;
1391 stream = (unsigned char *)audio_stream_data;
1392
1393 if (audio_stream_data == NULL || audio_frame_size == 0) {
1394 ALOGW("no buffer to get channel status, return default for compress");
1395 get_default_compressed_channel_status(channel_status);
1396 return ret;
1397 }
1398
1399 memset(channel_status,0,24);
1400 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1401 {
1402 ALOGE("init audio parser failed");
1403 return -1;
1404 }
1405 ret = get_channel_status(channel_status, codec_type);
1406 return ret;
1407
1408}
1409
1410#endif
1411
1412void get_lpcm_channel_status(uint32_t sampleRate,
1413 unsigned char *channel_status)
1414{
1415 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301416 memset(channel_status,0,24);
1417 /* block start bit in preamble bit 3 */
1418 channel_status[0] |= PROFESSIONAL;
1419 //LPCM OUT
1420 channel_status[0] &= ~NON_LPCM;
1421
1422 switch (sampleRate) {
1423 case 8000:
1424 case 11025:
1425 case 12000:
1426 case 16000:
1427 case 22050:
1428 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301429 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301430 case 24000:
1431 channel_status[3] |= SR_24000;
1432 break;
1433 case 32000:
1434 channel_status[3] |= SR_32000;
1435 break;
1436 case 44100:
1437 channel_status[3] |= SR_44100;
1438 break;
1439 case 48000:
1440 channel_status[3] |= SR_48000;
1441 break;
1442 case 88200:
1443 channel_status[3] |= SR_88200;
1444 break;
1445 case 96000:
1446 channel_status[3] |= SR_96000;
1447 break;
1448 case 176400:
1449 channel_status[3] |= SR_176400;
1450 break;
1451 case 192000:
1452 channel_status[3] |= SR_192000;
1453 break;
1454 default:
1455 ALOGV("Invalid sample_rate %u\n", sampleRate);
1456 status = -1;
1457 break;
1458 }
1459}
1460
1461void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1462{
1463 unsigned char channel_status[24]={0};
1464 struct snd_aes_iec958 iec958;
1465 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1466 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301467 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301468#ifdef HDMI_PASSTHROUGH_ENABLED
1469 if (audio_extn_is_dolby_format(out->format) &&
1470 /*TODO:Extend code to support DTS passthrough*/
1471 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301472 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301473 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1474 } else
1475#endif
1476 {
1477 /*set channel status bit for LPCM*/
1478 get_lpcm_channel_status(out->sample_rate, channel_status);
1479 }
1480
1481 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1482 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1483 if (!ctl) {
1484 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1485 __func__, mixer_ctl_name);
1486 return;
1487 }
1488 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1489 ALOGE("%s: Could not set channel status for ext HDMI ",
1490 __func__);
1491 return;
1492 }
1493
1494}
1495#endif