blob: 17fe4c97c24a940e783b0f0888c30bacdebd6f69 [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),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700154#endif
155};
156
Ben Rombergera04fabc2014-11-14 12:16:03 -0800157static char bTable[BASE_TABLE_SIZE] = {
158 'A','B','C','D','E','F','G','H','I','J','K','L',
159 'M','N','O','P','Q','R','S','T','U','V','W','X',
160 'Y','Z','a','b','c','d','e','f','g','h','i','j',
161 'k','l','m','n','o','p','q','r','s','t','u','v',
162 'w','x','y','z','0','1','2','3','4','5','6','7',
163 '8','9','+','/'
164};
165
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700166static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
167 const char *name)
168{
169 size_t i;
170 for (i = 0; i < size; i++) {
171 if (strcmp(table[i].name, name) == 0) {
172 ALOGV("%s found %s", __func__, table[i].name);
173 return table[i].value;
174 }
175 }
176 return 0;
177}
178
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530179static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700180{
181 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530182 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800183 char *last_r;
184 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700185 while (flag_name != NULL) {
186 if (strlen(flag_name) != 0) {
187 flag |= string_to_enum(s_flag_name_to_enum_table,
188 ARRAY_SIZE(s_flag_name_to_enum_table),
189 flag_name);
190 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800191 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700192 }
193
194 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530195 io_flags.in_flags = (audio_input_flags_t)flag;
196 io_flags.out_flags = (audio_output_flags_t)flag;
197 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198}
199
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530200static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201{
202 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800203 char *last_r;
204 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700205
206 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
207 return;
208
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530209 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700210 while (str != NULL) {
211 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
212 ARRAY_SIZE(s_format_name_to_enum_table), str);
213 ALOGV("%s: format - %d", __func__, format);
214 if (format != 0) {
215 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700216 if (sf_info == NULL)
217 break; /* return whatever was parsed */
218
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530220 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700221 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800222 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223 }
224}
225
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530226static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700228 struct stream_sample_rate *ss_info = NULL;
229 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800230 char *last_r;
231 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700232
Amit Shekhar6f461b12014-08-01 14:52:58 -0700233 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
234 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700235
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530236 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700237 while (str != NULL) {
238 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
239 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
240 if (0 != sample_rate) {
241 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800242 if (!ss_info) {
243 ALOGE("%s: memory allocation failure", __func__);
244 return;
245 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700246 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530247 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700248 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800249 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700250 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700251}
252
253static int parse_bit_width_names(char *name)
254{
255 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800256 char *last_r;
257 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700258
259 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
260 bit_width = (int)strtol(str, (char **)NULL, 10);
261
262 ALOGV("%s: bit_width - %d", __func__, bit_width);
263 return bit_width;
264}
265
266static int parse_app_type_names(void *platform, char *name)
267{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700268 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800269 char *last_r;
270 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271
272 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
273 app_type = (int)strtol(str, (char **)NULL, 10);
274
275 ALOGV("%s: app_type - %d", __func__, app_type);
276 return app_type;
277}
278
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279static void update_streams_cfg_list(cnode *root, void *platform,
280 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700281{
282 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530283 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700284
285 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530286 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700287
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530288 if (!s_info) {
289 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700290 return;
291 }
292
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700293 while (node) {
294 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530296 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
297 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700298 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530299 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700300 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530301 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
302 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530304 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700305 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307 }
308 node = node->next;
309 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530310 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700311}
312
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530313static void load_cfg_list(cnode *root, void *platform,
314 struct listnode *streams_output_cfg_list,
315 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700316{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530317 cnode *node = NULL;
318
319 node = config_find(root, OUTPUTS_TAG);
320 if (node != NULL) {
321 node = node->first_child;
322 while (node) {
323 ALOGV("%s: loading output %s", __func__, node->name);
324 update_streams_cfg_list(node, platform, streams_output_cfg_list);
325 node = node->next;
326 }
327 } else {
328 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329 }
330
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530331 node = config_find(root, INPUTS_TAG);
332 if (node != NULL) {
333 node = node->first_child;
334 while (node) {
335 ALOGV("%s: loading input %s", __func__, node->name);
336 update_streams_cfg_list(node, platform, streams_input_cfg_list);
337 node = node->next;
338 }
339 } else {
340 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700341 }
342}
343
344static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530345 struct listnode *streams_output_cfg_list,
346 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700347{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530348 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700349 int length = 0, i, num_app_types = 0;
350 struct listnode *node;
351 bool update;
352 struct mixer_ctl *ctl = NULL;
353 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530354 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700355
356 if (!mixer) {
357 ALOGE("%s: mixer is null",__func__);
358 return;
359 }
360 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
361 if (!ctl) {
362 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
363 return;
364 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530365 app_type_cfg[length++] = num_app_types;
366
367 if (list_empty(streams_output_cfg_list)) {
368 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700369 app_type_cfg[length++] = 48000;
370 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530371 num_app_types += 1;
372 }
373 if (list_empty(streams_input_cfg_list)) {
374 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
375 app_type_cfg[length++] = 48000;
376 app_type_cfg[length++] = 16;
377 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 }
379
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700380 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530381 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 update = true;
383 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530387 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530388 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530389 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530390 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391 update = false;
392 break;
393 }
394 }
395 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530396 num_app_types += 1;
397 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
398 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
399 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
400 }
401 }
402 list_for_each(node, streams_input_cfg_list) {
403 s_info = node_to_item(node, struct streams_io_cfg, list);
404 update = true;
405 for (i=0; i<length; i=i+3) {
406 if (app_type_cfg[i+1] == 0)
407 break;
408 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530409 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530410 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530411 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530412 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
413 update = false;
414 break;
415 }
416 }
417 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
418 num_app_types += 1;
419 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
420 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
421 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422 }
423 }
424 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
425 if (num_app_types) {
426 app_type_cfg[0] = num_app_types;
427 mixer_ctl_set_array(ctl, app_type_cfg, length);
428 }
429}
430
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530431void audio_extn_utils_update_streams_cfg_lists(void *platform,
432 struct mixer *mixer,
433 struct listnode *streams_output_cfg_list,
434 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435{
436 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438
439 ALOGV("%s", __func__);
440 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530441 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700442
443 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700444 if (root == NULL) {
445 ALOGE("cfg_list, NULL config root");
446 return;
447 }
448
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530449 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
450 if (data == NULL) {
451 ALOGD("%s: failed to open io config file(%s), trying older config file",
452 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
453 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
454 if (data == NULL) {
455 send_app_type_cfg(platform, mixer,
456 streams_output_cfg_list,
457 streams_input_cfg_list);
458 ALOGE("%s: could not load io policy config!", __func__);
459 return;
460 }
461 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700462
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530463 config_load(root, data);
464 load_cfg_list(root, platform, streams_output_cfg_list,
465 streams_input_cfg_list);
466
467 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
468 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800469
470 config_free(root);
471 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700472}
473
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474static void audio_extn_utils_dump_streams_cfg_list(
475 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700476{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700477 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530478 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700479 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700480 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530481
482 list_for_each(node_i, streams_cfg_list) {
483 s_info = node_to_item(node_i, struct streams_io_cfg, list);
484 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
485 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
486 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
487 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700488 sf_info = node_to_item(node_j, struct stream_format, list);
489 ALOGV("format-%x", sf_info->format);
490 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530491 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700492 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
493 ALOGV("sample rate-%d", ss_info->sample_rate);
494 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495 }
496}
497
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530498void audio_extn_utils_dump_streams_cfg_lists(
499 struct listnode *streams_output_cfg_list,
500 struct listnode *streams_input_cfg_list)
501{
502 ALOGV("%s", __func__);
503 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
504 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
505}
506
507static void audio_extn_utils_release_streams_cfg_list(
508 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700509{
510 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530511 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700512
513 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530514
515 while (!list_empty(streams_cfg_list)) {
516 node_i = list_head(streams_cfg_list);
517 s_info = node_to_item(node_i, struct streams_io_cfg, list);
518 while (!list_empty(&s_info->format_list)) {
519 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 list_remove(node_j);
521 free(node_to_item(node_j, struct stream_format, list));
522 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530523 while (!list_empty(&s_info->sample_rate_list)) {
524 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700525 list_remove(node_j);
526 free(node_to_item(node_j, struct stream_sample_rate, list));
527 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530529 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700530 }
531}
532
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530533void audio_extn_utils_release_streams_cfg_lists(
534 struct listnode *streams_output_cfg_list,
535 struct listnode *streams_input_cfg_list)
536{
537 ALOGV("%s", __func__);
538 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
539 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
540}
541
542static bool set_app_type_cfg(struct streams_io_cfg *s_info,
543 struct stream_app_type_cfg *app_type_cfg,
544 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700545 {
546 struct listnode *node_i;
547 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530548 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700549 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
550 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530551 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700552
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530553 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700554 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530555 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700556 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
557 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
558 return true;
559 }
560 }
561 /*
562 * Reiterate through the list assuming dafault sample rate.
563 * Handles scenario where input sample rate is higher
564 * than all sample rates in list for the input bit width.
565 */
566 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800567
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530568 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700569 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
570 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530571 (bit_width == s_info->app_type_cfg.bit_width)) {
572 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700573 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530574 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800575 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 -0700576 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
577 return true;
578 }
579 }
580 return false;
581}
582
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530583void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
584 struct listnode *streams_input_cfg_list,
585 audio_devices_t devices __unused,
586 audio_input_flags_t flags,
587 audio_format_t format,
588 uint32_t sample_rate,
589 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530590 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530591 struct stream_app_type_cfg *app_type_cfg)
592{
593 struct listnode *node_i, *node_j;
594 struct streams_io_cfg *s_info;
595 struct stream_format *sf_info;
596
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530597 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
598 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530599
600 list_for_each(node_i, streams_input_cfg_list) {
601 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530602 /* Along with flags do profile matching if set at either end.*/
603 if (s_info->flags.in_flags == flags &&
604 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
605 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530606 list_for_each(node_j, &s_info->format_list) {
607 sf_info = node_to_item(node_j, struct stream_format, list);
608 if (sf_info->format == format) {
609 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
610 return;
611 }
612 }
613 }
614 }
615 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
616 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
617 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
618 app_type_cfg->bit_width = 16;
619}
620
621void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700622 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700623 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700624 audio_output_flags_t flags,
625 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700626 uint32_t sample_rate,
627 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530628 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530629 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700630 struct stream_app_type_cfg *app_type_cfg)
631{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530632 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530633 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700634 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530635 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700636
Ashish Jain058165c2016-09-28 23:18:48 +0530637 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700638 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700639 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
640 if (-ENOSYS != bw)
641 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700642 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
643 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
644 }
645
Manish Dewangan837dc462015-05-27 10:17:41 +0530646 property_get("audio.playback.mch.downsample",value,"");
647 if (!strncmp("true", value, sizeof("true"))) {
648 if ((popcount(channel_mask) > 2) &&
649 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
650 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
651 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
652 ALOGD("%s: MCH session defaulting sample rate to %d",
653 __func__, sample_rate);
654 }
655 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530656
657 /* Set sampling rate to 176.4 for DSD64
658 * and 352.8Khz for DSD128.
659 * Set Bit Width to 16. output will be 16 bit
660 * post DoP in ASM.
661 */
662 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
663 (format == AUDIO_FORMAT_DSD)) {
664 bit_width = 16;
665 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
666 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
667 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
668 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
669 }
670
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530671 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
672 //TODO: Handle fractional sampling rate configuration for LL
673 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
674 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
675 __func__, sample_rate, bit_width);
676 }
677
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530678 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
679 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700680 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530681 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530682 /* Along with flags do profile matching if set at either end.*/
683 if (s_info->flags.out_flags == flags &&
684 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
685 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530686 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 sf_info = node_to_item(node_j, struct stream_format, list);
688 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530689 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700690 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700691 }
692 }
693 }
694 }
695 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530696 s_info = node_to_item(node_i, struct streams_io_cfg, list);
697 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700698 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
700 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
701 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700702 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530703 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700704 return;
705 }
706 }
707 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700708 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700709 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700710 app_type_cfg->bit_width = 16;
711}
712
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530713static bool audio_is_this_native_usecase(struct audio_usecase *uc)
714{
715 bool native_usecase = false;
716 struct stream_out *out = (struct stream_out*) uc->stream.out;
717
718 if (PCM_PLAYBACK == uc->type && out != NULL &&
719 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
720 is_offload_usecase(uc->id) &&
721 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
722 native_usecase = true;
723
724 return native_usecase;
725}
726
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530727void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
728 struct audio_device *adev,
729 struct audio_usecase *usecase)
730{
731 ALOGV("%s", __func__);
732
733 switch(usecase->type) {
734 case PCM_PLAYBACK:
735 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
736 &adev->streams_output_cfg_list,
737 usecase->stream.out->devices,
738 usecase->stream.out->flags,
739 usecase->stream.out->format,
740 usecase->stream.out->sample_rate,
741 usecase->stream.out->bit_width,
742 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530743 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530744 &usecase->stream.out->app_type_cfg);
745 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
746 break;
747 case PCM_CAPTURE:
748 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
749 &adev->streams_input_cfg_list,
750 usecase->stream.in->device,
751 usecase->stream.in->flags,
752 usecase->stream.in->format,
753 usecase->stream.in->sample_rate,
754 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530755 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530756 &usecase->stream.in->app_type_cfg);
757 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
758 break;
759 default:
760 ALOGE("%s: app type cfg not supported for usecase type (%d)",
761 __func__, usecase->type);
762 }
763}
764
Ben Romberger1fafdde2015-09-09 19:43:15 -0700765int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
766 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767{
768 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530769 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
770 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700771 struct mixer_ctl *ctl;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530772 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530773 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530774 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700775
776 ALOGV("%s", __func__);
777
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530778 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530779 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700780 rc = 0;
781 goto exit_send_app_type_cfg;
782 }
783 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
784 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
785 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530786 (!is_offload_usecase(usecase->id)) &&
787 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530788 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 -0700789 rc = 0;
790 goto exit_send_app_type_cfg;
791 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530792 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530793 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700794 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530795 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
796 "Audio Stream %d App Type Cfg", pcm_device_id);
797 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700798 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530799 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700800 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530801 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
802 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
803 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->in_snd_device);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530804 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530805 if (acdb_dev_id <= 0) {
806 ALOGE("%s: Couldn't get the acdb dev id", __func__);
807 rc = -EINVAL;
808 goto exit_send_app_type_cfg;
809 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700810
811 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
812 if (!ctl) {
813 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
814 mixer_ctl_name);
815 rc = -EINVAL;
816 goto exit_send_app_type_cfg;
817 }
818 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800819 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530820
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530821 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
822 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
823 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
824 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
825 !(audio_is_this_native_usecase(usecase))) ||
826 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
827 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
828 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530829
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530830 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530831
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530832 property_get("audio.playback.mch.downsample",value,"");
833 if (!strncmp("true", value, sizeof("true"))) {
834 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
835 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
836 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
837 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
838 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530839
Mingming Yin21854652016-04-13 11:54:02 -0700840 if ((24 == usecase->stream.out->bit_width) &&
841 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
842 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530843 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
844 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
845 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
846 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
847 /*
848 * To best utlize DSP, check if the stream sample rate is supported/multiple of
849 * configured device sample rate, if not update the COPP rate to be equal to the
850 * device sample rate, else open COPP at stream sample rate
851 */
852 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
853 usecase->stream.out->sample_rate,
854 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700855 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
856 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
857 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
858 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
859 }
860 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
861
862 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
863 app_type_cfg[len++] = acdb_dev_id;
864 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
865 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530866 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700867 app_type_cfg[len++] = sample_rate * 4;
868 } else {
869 app_type_cfg[len++] = sample_rate;
870 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530871
872 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
873 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
874
875 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
876 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.app_type;
877 app_type_cfg[len++] = acdb_dev_id;
878 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.sample_rate;
879 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
880 __func__, usecase->stream.in->app_type_cfg.app_type, acdb_dev_id,
881 usecase->stream.in->app_type_cfg.sample_rate);
882 } else {
883 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
884 app_type_cfg[len++] = acdb_dev_id;
885 app_type_cfg[len++] = sample_rate;
886 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d",
887 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
888 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530889 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530890
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700891 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700892 rc = 0;
893exit_send_app_type_cfg:
894 return rc;
895}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700896
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530897int read_line_from_file(const char *path, char *buf, size_t count)
898{
899 char * fgets_ret;
900 FILE * fd;
901 int rv;
902
903 fd = fopen(path, "r");
904 if (fd == NULL)
905 return -1;
906
907 fgets_ret = fgets(buf, (int)count, fd);
908 if (NULL != fgets_ret) {
909 rv = (int)strlen(buf);
910 } else {
911 rv = ferror(fd);
912 }
913 fclose(fd);
914
915 return rv;
916}
917
Ashish Jainf1eaa582016-05-23 20:54:24 +0530918/*Translates ALSA formats to AOSP PCM formats*/
919audio_format_t alsa_format_to_hal(uint32_t alsa_format)
920{
921 audio_format_t format;
922
923 switch(alsa_format) {
924 case SNDRV_PCM_FORMAT_S16_LE:
925 format = AUDIO_FORMAT_PCM_16_BIT;
926 break;
927 case SNDRV_PCM_FORMAT_S24_3LE:
928 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
929 break;
930 case SNDRV_PCM_FORMAT_S24_LE:
931 format = AUDIO_FORMAT_PCM_8_24_BIT;
932 break;
933 case SNDRV_PCM_FORMAT_S32_LE:
934 format = AUDIO_FORMAT_PCM_32_BIT;
935 break;
936 default:
937 ALOGW("Incorrect ALSA format");
938 format = AUDIO_FORMAT_INVALID;
939 }
940 return format;
941}
942
943/*Translates hal format (AOSP) to alsa formats*/
944uint32_t hal_format_to_alsa(audio_format_t hal_format)
945{
946 uint32_t alsa_format;
947
948 switch (hal_format) {
949 case AUDIO_FORMAT_PCM_32_BIT: {
950 if (platform_supports_true_32bit())
951 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
952 else
953 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
954 }
955 break;
956 case AUDIO_FORMAT_PCM_8_BIT:
957 alsa_format = SNDRV_PCM_FORMAT_S8;
958 break;
959 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
960 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
961 break;
962 case AUDIO_FORMAT_PCM_8_24_BIT: {
963 if (platform_supports_true_32bit())
964 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
965 else
966 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
967 }
968 break;
969 case AUDIO_FORMAT_PCM_FLOAT:
970 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
971 break;
972 default:
973 case AUDIO_FORMAT_PCM_16_BIT:
974 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
975 break;
976 }
977 return alsa_format;
978}
979
Ashish Jain83a6cc22016-06-28 14:34:17 +0530980/*Translates PCM formats to AOSP formats*/
981audio_format_t pcm_format_to_hal(uint32_t pcm_format)
982{
983 audio_format_t format = AUDIO_FORMAT_INVALID;
984
985 switch(pcm_format) {
986 case PCM_FORMAT_S16_LE:
987 format = AUDIO_FORMAT_PCM_16_BIT;
988 break;
989 case PCM_FORMAT_S24_3LE:
990 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
991 break;
992 case PCM_FORMAT_S24_LE:
993 format = AUDIO_FORMAT_PCM_8_24_BIT;
994 break;
995 case PCM_FORMAT_S32_LE:
996 format = AUDIO_FORMAT_PCM_32_BIT;
997 break;
998 default:
999 ALOGW("Incorrect PCM format");
1000 format = AUDIO_FORMAT_INVALID;
1001 }
1002 return format;
1003}
1004
1005/*Translates hal format (AOSP) to alsa formats*/
1006uint32_t hal_format_to_pcm(audio_format_t hal_format)
1007{
1008 uint32_t pcm_format;
1009
1010 switch (hal_format) {
1011 case AUDIO_FORMAT_PCM_32_BIT:
1012 case AUDIO_FORMAT_PCM_8_24_BIT:
1013 case AUDIO_FORMAT_PCM_FLOAT: {
1014 if (platform_supports_true_32bit())
1015 pcm_format = PCM_FORMAT_S32_LE;
1016 else
1017 pcm_format = PCM_FORMAT_S24_3LE;
1018 }
1019 break;
1020 case AUDIO_FORMAT_PCM_8_BIT:
1021 pcm_format = PCM_FORMAT_S8;
1022 break;
1023 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1024 pcm_format = PCM_FORMAT_S24_3LE;
1025 break;
1026 default:
1027 case AUDIO_FORMAT_PCM_16_BIT:
1028 pcm_format = PCM_FORMAT_S16_LE;
1029 break;
1030 }
1031 return pcm_format;
1032}
1033
Ashish Jainf1eaa582016-05-23 20:54:24 +05301034uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1035 uint32_t sample_rate,
1036 uint32_t noOfChannels)
1037{
1038 uint32_t fragment_size = 0;
1039 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1040
1041 fragment_size = (pcm_offload_time
1042 * sample_rate
1043 * bytes_per_sample
1044 * noOfChannels)/1000;
1045 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1046 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1047 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1048 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1049 /*To have same PCM samples for all channels, the buffer size requires to
1050 *be multiple of (number of channels * bytes per sample)
1051 *For writes to succeed, the buffer must be written at address which is multiple of 32
1052 */
1053 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1054
1055 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1056 return fragment_size;
1057}
1058
1059/* Calculates the fragment size required to configure compress session.
1060 * Based on the alsa format selected, decide if conversion is needed in
1061
1062 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1063 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1064 */
1065void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1066{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301067 audio_format_t dst_format = out->hal_op_format;
1068 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301069 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1070 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1071
1072 out->compr_config.fragment_size =
1073 get_alsa_fragment_size(hal_op_bytes_per_sample,
1074 out->sample_rate,
1075 popcount(out->channel_mask));
1076
1077 if ((src_format != dst_format) &&
1078 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1079
Ashish Jain83a6cc22016-06-28 14:34:17 +05301080 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301081 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1082 hal_op_bytes_per_sample);
1083 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301084 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301085 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301086 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301087 }
1088}
1089
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301090/* converts pcm format 24_8 to 8_24 inplace */
1091size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1092{
1093 size_t i = 0;
1094 int *int_buf_stream = buf;
1095
1096 if ((bytes % 4) != 0) {
1097 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1098 return -EINVAL;
1099 }
1100
1101 for (; i < (bytes / 4); i++)
1102 int_buf_stream[i] >>= 8;
1103
1104 return bytes;
1105}
1106
1107int get_snd_codec_id(audio_format_t format)
1108{
1109 int id = 0;
1110
1111 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1112 case AUDIO_FORMAT_MP3:
1113 id = SND_AUDIOCODEC_MP3;
1114 break;
1115 case AUDIO_FORMAT_AAC:
1116 id = SND_AUDIOCODEC_AAC;
1117 break;
1118 case AUDIO_FORMAT_AAC_ADTS:
1119 id = SND_AUDIOCODEC_AAC;
1120 break;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301121 case AUDIO_FORMAT_AAC_LATM:
1122 id = SND_AUDIOCODEC_AAC;
1123 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301124 case AUDIO_FORMAT_PCM_OFFLOAD:
1125 case AUDIO_FORMAT_PCM:
1126 id = SND_AUDIOCODEC_PCM;
1127 break;
1128 case AUDIO_FORMAT_FLAC:
1129 id = SND_AUDIOCODEC_FLAC;
1130 break;
1131 case AUDIO_FORMAT_ALAC:
1132 id = SND_AUDIOCODEC_ALAC;
1133 break;
1134 case AUDIO_FORMAT_APE:
1135 id = SND_AUDIOCODEC_APE;
1136 break;
1137 case AUDIO_FORMAT_VORBIS:
1138 id = SND_AUDIOCODEC_VORBIS;
1139 break;
1140 case AUDIO_FORMAT_WMA:
1141 id = SND_AUDIOCODEC_WMA;
1142 break;
1143 case AUDIO_FORMAT_WMA_PRO:
1144 id = SND_AUDIOCODEC_WMA_PRO;
1145 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301146 case AUDIO_FORMAT_MP2:
1147 id = SND_AUDIOCODEC_MP2;
1148 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301149 case AUDIO_FORMAT_AC3:
1150 id = SND_AUDIOCODEC_AC3;
1151 break;
1152 case AUDIO_FORMAT_E_AC3:
1153 case AUDIO_FORMAT_E_AC3_JOC:
1154 id = SND_AUDIOCODEC_EAC3;
1155 break;
1156 case AUDIO_FORMAT_DTS:
1157 case AUDIO_FORMAT_DTS_HD:
1158 id = SND_AUDIOCODEC_DTS;
1159 break;
Preetam Singh Ranawat4277a5a2017-01-18 19:02:24 +05301160 case AUDIO_FORMAT_DSD:
1161 id = SND_AUDIOCODEC_DSD;
1162 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301163 default:
1164 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1165 }
1166
1167 return id;
1168}
1169
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001170void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1171 struct audio_usecase *usecase)
1172{
1173 int type = usecase->type;
1174
Dhananjay Kumar14245982017-01-16 20:21:00 +05301175 if (type == PCM_PLAYBACK && usecase->stream.out != NULL) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001176 struct stream_out *out = usecase->stream.out;
1177 int snd_device = usecase->out_snd_device;
1178 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001179 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301180 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001181 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301182 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumar14245982017-01-16 20:21:00 +05301183 } else if (type == PCM_CAPTURE && usecase->stream.in != NULL) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301184 platform_send_audio_calibration(adev->platform, usecase,
1185 usecase->stream.in->app_type_cfg.app_type,
1186 usecase->stream.in->app_type_cfg.sample_rate);
1187 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301188 /* when app type is default. the sample rate is not used to send cal */
1189 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301190 platform_get_default_app_type_v2(adev->platform, usecase->type),
1191 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001192 }
1193}
1194
Ben Rombergera04fabc2014-11-14 12:16:03 -08001195// Base64 Encode and Decode
1196// Not all features supported. This must be used only with following conditions.
1197// Decode Modes: Support with and without padding
1198// CRLF not handling. So no CRLF in string to decode.
1199// Encode Modes: Supports only padding
1200int b64decode(char *inp, int ilen, uint8_t* outp)
1201{
1202 int i, j, k, ii, num;
1203 int rem, pcnt;
1204 uint32_t res=0;
1205 uint8_t getIndex[MAX_BASEINDEX_LEN];
1206 uint8_t tmp, cflag;
1207
1208 if(inp == NULL || outp == NULL || ilen <= 0) {
1209 ALOGE("[%s] received NULL pointer or zero length",__func__);
1210 return -1;
1211 }
1212
1213 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1214 for(i=0;i<BASE_TABLE_SIZE;i++) {
1215 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1216 }
1217 getIndex[(uint8_t)'=']=0;
1218
1219 j=0;k=0;
1220 num = ilen/4;
1221 rem = ilen%4;
1222 if(rem==0)
1223 num = num-1;
1224 cflag=0;
1225 for(i=0; i<num; i++) {
1226 res=0;
1227 for(ii=0;ii<4;ii++) {
1228 res = res << 6;
1229 tmp = getIndex[(uint8_t)inp[j++]];
1230 res = res | tmp;
1231 cflag = cflag | tmp;
1232 }
1233 outp[k++] = (res >> 16)&0xFF;
1234 outp[k++] = (res >> 8)&0xFF;
1235 outp[k++] = res & 0xFF;
1236 }
1237
1238 // Handle last bytes special
1239 pcnt=0;
1240 if(rem == 0) {
1241 //With padding or full data
1242 res = 0;
1243 for(ii=0;ii<4;ii++) {
1244 if(inp[j] == '=')
1245 pcnt++;
1246 res = res << 6;
1247 tmp = getIndex[(uint8_t)inp[j++]];
1248 res = res | tmp;
1249 cflag = cflag | tmp;
1250 }
1251 outp[k++] = res >> 16;
1252 if(pcnt == 2)
1253 goto done;
1254 outp[k++] = (res>>8)&0xFF;
1255 if(pcnt == 1)
1256 goto done;
1257 outp[k++] = res&0xFF;
1258 } else {
1259 //without padding
1260 res = 0;
1261 for(i=0;i<rem;i++) {
1262 res = res << 6;
1263 tmp = getIndex[(uint8_t)inp[j++]];
1264 res = res | tmp;
1265 cflag = cflag | tmp;
1266 }
1267 for(i=rem;i<4;i++) {
1268 res = res << 6;
1269 pcnt++;
1270 }
1271 outp[k++] = res >> 16;
1272 if(pcnt == 2)
1273 goto done;
1274 outp[k++] = (res>>8)&0xFF;
1275 if(pcnt == 1)
1276 goto done;
1277 outp[k++] = res&0xFF;
1278 }
1279done:
1280 if(cflag == 0xFF) {
1281 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1282 __func__, inp);
1283 return 0;
1284 }
1285 return k;
1286}
1287
1288int b64encode(uint8_t *inp, int ilen, char* outp)
1289{
1290 int i,j,k, num;
1291 int rem=0;
1292 uint32_t res=0;
1293
1294 if(inp == NULL || outp == NULL || ilen<=0) {
1295 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1296 return -1;
1297 }
1298
1299 num = ilen/3;
1300 rem = ilen%3;
1301 j=0;k=0;
1302 for(i=0; i<num; i++) {
1303 //prepare index
1304 res = inp[j++]<<16;
1305 res = res | inp[j++]<<8;
1306 res = res | inp[j++];
1307 //get output map from index
1308 outp[k++] = (char) bTable[(res>>18)&0x3F];
1309 outp[k++] = (char) bTable[(res>>12)&0x3F];
1310 outp[k++] = (char) bTable[(res>>6)&0x3F];
1311 outp[k++] = (char) bTable[res&0x3F];
1312 }
1313
1314 switch(rem) {
1315 case 1:
1316 res = inp[j++]<<16;
1317 outp[k++] = (char) bTable[res>>18];
1318 outp[k++] = (char) bTable[(res>>12)&0x3F];
1319 //outp[k++] = '=';
1320 //outp[k++] = '=';
1321 break;
1322 case 2:
1323 res = inp[j++]<<16;
1324 res = res | inp[j++]<<8;
1325 outp[k++] = (char) bTable[res>>18];
1326 outp[k++] = (char) bTable[(res>>12)&0x3F];
1327 outp[k++] = (char) bTable[(res>>6)&0x3F];
1328 //outp[k++] = '=';
1329 break;
1330 default:
1331 break;
1332 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001333 outp[k] = '\0';
1334 return k;
1335}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301336
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301337
1338int audio_extn_utils_get_codec_version(const char *snd_card_name,
1339 int card_num,
1340 char *codec_version)
1341{
1342 char procfs_path[50];
1343 FILE *fp;
1344
1345 if (strstr(snd_card_name, "tasha")) {
1346 snprintf(procfs_path, sizeof(procfs_path),
1347 "/proc/asound/card%d/codecs/tasha/version", card_num);
1348 if ((fp = fopen(procfs_path, "r")) != NULL) {
1349 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1350 fclose(fp);
1351 } else {
1352 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1353 return -ENOENT;
1354 }
1355 ALOGD("%s: codec version %s", __func__, codec_version);
1356 }
1357
1358 return 0;
1359}
1360
1361
Ashish Jain81eb2a82015-05-13 10:52:34 +05301362#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1363
1364void get_default_compressed_channel_status(
1365 unsigned char *channel_status)
1366{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301367 memset(channel_status,0,24);
1368
1369 /* block start bit in preamble bit 3 */
1370 channel_status[0] |= PROFESSIONAL;
1371 //compre out
1372 channel_status[0] |= NON_LPCM;
1373 // sample rate; fixed 48K for default/transcode
1374 channel_status[3] |= SR_48000;
1375}
1376
1377#ifdef HDMI_PASSTHROUGH_ENABLED
1378int32_t get_compressed_channel_status(void *audio_stream_data,
1379 uint32_t audio_frame_size,
1380 unsigned char *channel_status,
1381 enum audio_parser_code_type codec_type)
1382 // codec_type - AUDIO_PARSER_CODEC_AC3
1383 // - AUDIO_PARSER_CODEC_DTS
1384{
1385 unsigned char *stream;
1386 int ret = 0;
1387 stream = (unsigned char *)audio_stream_data;
1388
1389 if (audio_stream_data == NULL || audio_frame_size == 0) {
1390 ALOGW("no buffer to get channel status, return default for compress");
1391 get_default_compressed_channel_status(channel_status);
1392 return ret;
1393 }
1394
1395 memset(channel_status,0,24);
1396 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1397 {
1398 ALOGE("init audio parser failed");
1399 return -1;
1400 }
1401 ret = get_channel_status(channel_status, codec_type);
1402 return ret;
1403
1404}
1405
1406#endif
1407
1408void get_lpcm_channel_status(uint32_t sampleRate,
1409 unsigned char *channel_status)
1410{
1411 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301412 memset(channel_status,0,24);
1413 /* block start bit in preamble bit 3 */
1414 channel_status[0] |= PROFESSIONAL;
1415 //LPCM OUT
1416 channel_status[0] &= ~NON_LPCM;
1417
1418 switch (sampleRate) {
1419 case 8000:
1420 case 11025:
1421 case 12000:
1422 case 16000:
1423 case 22050:
1424 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301425 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301426 case 24000:
1427 channel_status[3] |= SR_24000;
1428 break;
1429 case 32000:
1430 channel_status[3] |= SR_32000;
1431 break;
1432 case 44100:
1433 channel_status[3] |= SR_44100;
1434 break;
1435 case 48000:
1436 channel_status[3] |= SR_48000;
1437 break;
1438 case 88200:
1439 channel_status[3] |= SR_88200;
1440 break;
1441 case 96000:
1442 channel_status[3] |= SR_96000;
1443 break;
1444 case 176400:
1445 channel_status[3] |= SR_176400;
1446 break;
1447 case 192000:
1448 channel_status[3] |= SR_192000;
1449 break;
1450 default:
1451 ALOGV("Invalid sample_rate %u\n", sampleRate);
1452 status = -1;
1453 break;
1454 }
1455}
1456
1457void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1458{
1459 unsigned char channel_status[24]={0};
1460 struct snd_aes_iec958 iec958;
1461 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1462 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301463 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301464#ifdef HDMI_PASSTHROUGH_ENABLED
1465 if (audio_extn_is_dolby_format(out->format) &&
1466 /*TODO:Extend code to support DTS passthrough*/
1467 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301468 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301469 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1470 } else
1471#endif
1472 {
1473 /*set channel status bit for LPCM*/
1474 get_lpcm_channel_status(out->sample_rate, channel_status);
1475 }
1476
1477 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1478 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1479 if (!ctl) {
1480 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1481 __func__, mixer_ctl_name);
1482 return;
1483 }
1484 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1485 ALOGE("%s: Could not set channel status for ext HDMI ",
1486 __func__);
1487 return;
1488 }
1489
1490}
1491#endif