blob: 0ff921d9a683627c5d872f6d0789e58180f46d6c [file] [log] [blame]
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07001/*
Alexy Josephaee4fdd2016-01-29 13:02:07 -08002 * Copyright (c) 2014-2016, 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),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700150#endif
151};
152
Ben Rombergera04fabc2014-11-14 12:16:03 -0800153static char bTable[BASE_TABLE_SIZE] = {
154 'A','B','C','D','E','F','G','H','I','J','K','L',
155 'M','N','O','P','Q','R','S','T','U','V','W','X',
156 'Y','Z','a','b','c','d','e','f','g','h','i','j',
157 'k','l','m','n','o','p','q','r','s','t','u','v',
158 'w','x','y','z','0','1','2','3','4','5','6','7',
159 '8','9','+','/'
160};
161
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700162static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
163 const char *name)
164{
165 size_t i;
166 for (i = 0; i < size; i++) {
167 if (strcmp(table[i].name, name) == 0) {
168 ALOGV("%s found %s", __func__, table[i].name);
169 return table[i].value;
170 }
171 }
172 return 0;
173}
174
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530175static audio_io_flags_t parse_flag_names(char *name)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700176{
177 uint32_t flag = 0;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530178 audio_io_flags_t io_flags;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800179 char *last_r;
180 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181 while (flag_name != NULL) {
182 if (strlen(flag_name) != 0) {
183 flag |= string_to_enum(s_flag_name_to_enum_table,
184 ARRAY_SIZE(s_flag_name_to_enum_table),
185 flag_name);
186 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800187 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700188 }
189
190 ALOGV("parse_flag_names: flag - %d", flag);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530191 io_flags.in_flags = (audio_input_flags_t)flag;
192 io_flags.out_flags = (audio_output_flags_t)flag;
193 return io_flags;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700194}
195
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530196static void parse_format_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700197{
198 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800199 char *last_r;
200 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201
202 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
203 return;
204
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530205 list_init(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700206 while (str != NULL) {
207 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
208 ARRAY_SIZE(s_format_name_to_enum_table), str);
209 ALOGV("%s: format - %d", __func__, format);
210 if (format != 0) {
211 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700212 if (sf_info == NULL)
213 break; /* return whatever was parsed */
214
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700215 sf_info->format = format;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530216 list_add_tail(&s_info->format_list, &sf_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700217 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800218 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700219 }
220}
221
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530222static void parse_sample_rate_names(char *name, struct streams_io_cfg *s_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700223{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700224 struct stream_sample_rate *ss_info = NULL;
225 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800226 char *last_r;
227 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700228
Amit Shekhar6f461b12014-08-01 14:52:58 -0700229 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
230 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700231
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530232 list_init(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700233 while (str != NULL) {
234 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
235 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
236 if (0 != sample_rate) {
237 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800238 if (!ss_info) {
239 ALOGE("%s: memory allocation failure", __func__);
240 return;
241 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700242 ss_info->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530243 list_add_tail(&s_info->sample_rate_list, &ss_info->list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700244 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800245 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700246 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247}
248
249static int parse_bit_width_names(char *name)
250{
251 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800252 char *last_r;
253 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700254
255 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
256 bit_width = (int)strtol(str, (char **)NULL, 10);
257
258 ALOGV("%s: bit_width - %d", __func__, bit_width);
259 return bit_width;
260}
261
262static int parse_app_type_names(void *platform, char *name)
263{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700264 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800265 char *last_r;
266 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700267
268 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
269 app_type = (int)strtol(str, (char **)NULL, 10);
270
271 ALOGV("%s: app_type - %d", __func__, app_type);
272 return app_type;
273}
274
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530275static void update_streams_cfg_list(cnode *root, void *platform,
276 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277{
278 cnode *node = root->first_child;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530279 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280
281 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530282 s_info = (struct streams_io_cfg *)calloc(1, sizeof(struct streams_io_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700283
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530284 if (!s_info) {
285 ALOGE("failed to allocate mem for s_info list element");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700286 return;
287 }
288
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700289 while (node) {
290 if (strcmp(node->name, FLAGS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530291 s_info->flags = parse_flag_names((char *)node->value);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530292 } else if (strcmp(node->name, PROFILES_TAG) == 0) {
293 strlcpy(s_info->profile, (char *)node->value, sizeof(s_info->profile));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700294 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530295 parse_format_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700296 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530297 s_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
298 parse_sample_rate_names((char *)node->value, s_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700299 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530300 s_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700301 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530302 s_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700303 }
304 node = node->next;
305 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530306 list_add_tail(streams_cfg_list, &s_info->list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700307}
308
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530309static void load_cfg_list(cnode *root, void *platform,
310 struct listnode *streams_output_cfg_list,
311 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700312{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530313 cnode *node = NULL;
314
315 node = config_find(root, OUTPUTS_TAG);
316 if (node != NULL) {
317 node = node->first_child;
318 while (node) {
319 ALOGV("%s: loading output %s", __func__, node->name);
320 update_streams_cfg_list(node, platform, streams_output_cfg_list);
321 node = node->next;
322 }
323 } else {
324 ALOGI("%s: could not load output, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700325 }
326
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530327 node = config_find(root, INPUTS_TAG);
328 if (node != NULL) {
329 node = node->first_child;
330 while (node) {
331 ALOGV("%s: loading input %s", __func__, node->name);
332 update_streams_cfg_list(node, platform, streams_input_cfg_list);
333 node = node->next;
334 }
335 } else {
336 ALOGI("%s: could not load input, node is NULL", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700337 }
338}
339
340static void send_app_type_cfg(void *platform, struct mixer *mixer,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530341 struct listnode *streams_output_cfg_list,
342 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700343{
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530344 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700345 int length = 0, i, num_app_types = 0;
346 struct listnode *node;
347 bool update;
348 struct mixer_ctl *ctl = NULL;
349 const char *mixer_ctl_name = "App Type Config";
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530350 struct streams_io_cfg *s_info = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700351
352 if (!mixer) {
353 ALOGE("%s: mixer is null",__func__);
354 return;
355 }
356 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
357 if (!ctl) {
358 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
359 return;
360 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530361 app_type_cfg[length++] = num_app_types;
362
363 if (list_empty(streams_output_cfg_list)) {
364 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_PLAYBACK);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700365 app_type_cfg[length++] = 48000;
366 app_type_cfg[length++] = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530367 num_app_types += 1;
368 }
369 if (list_empty(streams_input_cfg_list)) {
370 app_type_cfg[length++] = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
371 app_type_cfg[length++] = 48000;
372 app_type_cfg[length++] = 16;
373 num_app_types += 1;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700374 }
375
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700376 list_for_each(node, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530377 s_info = node_to_item(node, struct streams_io_cfg, list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700378 update = true;
379 for (i=0; i<length; i=i+3) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530380 if (app_type_cfg[i+1] == 0)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700381 break;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530382 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530383 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530384 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530385 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530386 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700387 update = false;
388 break;
389 }
390 }
391 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530392 num_app_types += 1;
393 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
394 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
395 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
396 }
397 }
398 list_for_each(node, streams_input_cfg_list) {
399 s_info = node_to_item(node, struct streams_io_cfg, list);
400 update = true;
401 for (i=0; i<length; i=i+3) {
402 if (app_type_cfg[i+1] == 0)
403 break;
404 else if (app_type_cfg[i+1] == (size_t)s_info->app_type_cfg.app_type) {
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530405 if (app_type_cfg[i+2] < (size_t)s_info->app_type_cfg.sample_rate)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530406 app_type_cfg[i+2] = s_info->app_type_cfg.sample_rate;
Dhananjay Kumar9cc498b2016-12-20 21:04:13 +0530407 if (app_type_cfg[i+3] < (size_t)s_info->app_type_cfg.bit_width)
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530408 app_type_cfg[i+3] = s_info->app_type_cfg.bit_width;
409 update = false;
410 break;
411 }
412 }
413 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
414 num_app_types += 1;
415 app_type_cfg[length++] = s_info->app_type_cfg.app_type;
416 app_type_cfg[length++] = s_info->app_type_cfg.sample_rate;
417 app_type_cfg[length++] = s_info->app_type_cfg.bit_width;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700418 }
419 }
420 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
421 if (num_app_types) {
422 app_type_cfg[0] = num_app_types;
423 mixer_ctl_set_array(ctl, app_type_cfg, length);
424 }
425}
426
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530427void audio_extn_utils_update_streams_cfg_lists(void *platform,
428 struct mixer *mixer,
429 struct listnode *streams_output_cfg_list,
430 struct listnode *streams_input_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700431{
432 cnode *root;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530433 char *data = NULL;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700434
435 ALOGV("%s", __func__);
436 list_init(streams_output_cfg_list);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530437 list_init(streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438
439 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700440 if (root == NULL) {
441 ALOGE("cfg_list, NULL config root");
442 return;
443 }
444
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530445 data = (char *)load_file(AUDIO_IO_POLICY_VENDOR_CONFIG_FILE, NULL);
446 if (data == NULL) {
447 ALOGD("%s: failed to open io config file(%s), trying older config file",
448 __func__, AUDIO_IO_POLICY_VENDOR_CONFIG_FILE);
449 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
450 if (data == NULL) {
451 send_app_type_cfg(platform, mixer,
452 streams_output_cfg_list,
453 streams_input_cfg_list);
454 ALOGE("%s: could not load io policy config!", __func__);
455 return;
456 }
457 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700458
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530459 config_load(root, data);
460 load_cfg_list(root, platform, streams_output_cfg_list,
461 streams_input_cfg_list);
462
463 send_app_type_cfg(platform, mixer, streams_output_cfg_list,
464 streams_input_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800465
466 config_free(root);
467 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700468}
469
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530470static void audio_extn_utils_dump_streams_cfg_list(
471 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700472{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700473 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530474 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700475 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700476 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530477
478 list_for_each(node_i, streams_cfg_list) {
479 s_info = node_to_item(node_i, struct streams_io_cfg, list);
480 ALOGV("%s: flags-%d, sample_rate-%d, bit_width-%d, app_type-%d",
481 __func__, s_info->flags.out_flags, s_info->app_type_cfg.sample_rate,
482 s_info->app_type_cfg.bit_width, s_info->app_type_cfg.app_type);
483 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 sf_info = node_to_item(node_j, struct stream_format, list);
485 ALOGV("format-%x", sf_info->format);
486 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530487 list_for_each(node_j, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700488 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
489 ALOGV("sample rate-%d", ss_info->sample_rate);
490 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491 }
492}
493
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530494void audio_extn_utils_dump_streams_cfg_lists(
495 struct listnode *streams_output_cfg_list,
496 struct listnode *streams_input_cfg_list)
497{
498 ALOGV("%s", __func__);
499 audio_extn_utils_dump_streams_cfg_list(streams_output_cfg_list);
500 audio_extn_utils_dump_streams_cfg_list(streams_input_cfg_list);
501}
502
503static void audio_extn_utils_release_streams_cfg_list(
504 struct listnode *streams_cfg_list)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700505{
506 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530507 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700508
509 ALOGV("%s", __func__);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530510
511 while (!list_empty(streams_cfg_list)) {
512 node_i = list_head(streams_cfg_list);
513 s_info = node_to_item(node_i, struct streams_io_cfg, list);
514 while (!list_empty(&s_info->format_list)) {
515 node_j = list_head(&s_info->format_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700516 list_remove(node_j);
517 free(node_to_item(node_j, struct stream_format, list));
518 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530519 while (!list_empty(&s_info->sample_rate_list)) {
520 node_j = list_head(&s_info->sample_rate_list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700521 list_remove(node_j);
522 free(node_to_item(node_j, struct stream_sample_rate, list));
523 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700524 list_remove(node_i);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530525 free(node_to_item(node_i, struct streams_io_cfg, list));
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700526 }
527}
528
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530529void audio_extn_utils_release_streams_cfg_lists(
530 struct listnode *streams_output_cfg_list,
531 struct listnode *streams_input_cfg_list)
532{
533 ALOGV("%s", __func__);
534 audio_extn_utils_release_streams_cfg_list(streams_output_cfg_list);
535 audio_extn_utils_release_streams_cfg_list(streams_input_cfg_list);
536}
537
538static bool set_app_type_cfg(struct streams_io_cfg *s_info,
539 struct stream_app_type_cfg *app_type_cfg,
540 uint32_t sample_rate, uint32_t bit_width)
Amit Shekhar6f461b12014-08-01 14:52:58 -0700541 {
542 struct listnode *node_i;
543 struct stream_sample_rate *ss_info;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530544 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700545 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
546 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530547 (bit_width == s_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700548
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530549 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700550 app_type_cfg->sample_rate = ss_info->sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530551 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700552 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
553 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
554 return true;
555 }
556 }
557 /*
558 * Reiterate through the list assuming dafault sample rate.
559 * Handles scenario where input sample rate is higher
560 * than all sample rates in list for the input bit width.
561 */
562 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800563
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530564 list_for_each(node_i, &s_info->sample_rate_list) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700565 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
566 if ((sample_rate <= ss_info->sample_rate) &&
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530567 (bit_width == s_info->app_type_cfg.bit_width)) {
568 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700569 app_type_cfg->sample_rate = sample_rate;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530570 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800571 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 -0700572 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
573 return true;
574 }
575 }
576 return false;
577}
578
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530579void audio_extn_utils_update_stream_input_app_type_cfg(void *platform,
580 struct listnode *streams_input_cfg_list,
581 audio_devices_t devices __unused,
582 audio_input_flags_t flags,
583 audio_format_t format,
584 uint32_t sample_rate,
585 uint32_t bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530586 char* profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530587 struct stream_app_type_cfg *app_type_cfg)
588{
589 struct listnode *node_i, *node_j;
590 struct streams_io_cfg *s_info;
591 struct stream_format *sf_info;
592
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530593 ALOGV("%s: flags: 0x%x, format: 0x%x sample_rate %d, profile %s",
594 __func__, flags, format, sample_rate, profile);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530595
596 list_for_each(node_i, streams_input_cfg_list) {
597 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530598 /* Along with flags do profile matching if set at either end.*/
599 if (s_info->flags.in_flags == flags &&
600 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
601 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530602 list_for_each(node_j, &s_info->format_list) {
603 sf_info = node_to_item(node_j, struct stream_format, list);
604 if (sf_info->format == format) {
605 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
606 return;
607 }
608 }
609 }
610 }
611 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
612 app_type_cfg->app_type = platform_get_default_app_type_v2(platform, PCM_CAPTURE);
613 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
614 app_type_cfg->bit_width = 16;
615}
616
617void audio_extn_utils_update_stream_output_app_type_cfg(void *platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700619 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700620 audio_output_flags_t flags,
621 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700622 uint32_t sample_rate,
623 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530624 audio_channel_mask_t channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530625 char *profile,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700626 struct stream_app_type_cfg *app_type_cfg)
627{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530628 struct listnode *node_i, *node_j;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530629 struct streams_io_cfg *s_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700630 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530631 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700632
Ashish Jain058165c2016-09-28 23:18:48 +0530633 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700634 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700635 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
636 if (-ENOSYS != bw)
637 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700638 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
639 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
640 }
641
Manish Dewangan837dc462015-05-27 10:17:41 +0530642 property_get("audio.playback.mch.downsample",value,"");
643 if (!strncmp("true", value, sizeof("true"))) {
644 if ((popcount(channel_mask) > 2) &&
645 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
646 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
647 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
648 ALOGD("%s: MCH session defaulting sample rate to %d",
649 __func__, sample_rate);
650 }
651 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530652
653 /* Set sampling rate to 176.4 for DSD64
654 * and 352.8Khz for DSD128.
655 * Set Bit Width to 16. output will be 16 bit
656 * post DoP in ASM.
657 */
658 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
659 (format == AUDIO_FORMAT_DSD)) {
660 bit_width = 16;
661 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
662 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
663 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
664 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
665 }
666
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530667 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
668 //TODO: Handle fractional sampling rate configuration for LL
669 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
670 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
671 __func__, sample_rate, bit_width);
672 }
673
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530674 ALOGV("%s: flags: %x, format: %x sample_rate %d, profile %s",
675 __func__, flags, format, sample_rate, profile);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700676 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530677 s_info = node_to_item(node_i, struct streams_io_cfg, list);
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530678 /* Along with flags do profile matching if set at either end.*/
679 if (s_info->flags.out_flags == flags &&
680 ((profile[0] == '\0' && s_info->profile[0] == '\0') ||
681 strncmp(s_info->profile, profile, sizeof(s_info->profile)) == 0)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530682 list_for_each(node_j, &s_info->format_list) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700683 sf_info = node_to_item(node_j, struct stream_format, list);
684 if (sf_info->format == format) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530685 if (set_app_type_cfg(s_info, app_type_cfg, sample_rate, bit_width))
Amit Shekhar6f461b12014-08-01 14:52:58 -0700686 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 }
688 }
689 }
690 }
691 list_for_each(node_i, streams_output_cfg_list) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530692 s_info = node_to_item(node_i, struct streams_io_cfg, list);
693 if (s_info->flags.out_flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700694 ALOGV("Compatible output profile not found.");
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530695 app_type_cfg->app_type = s_info->app_type_cfg.app_type;
696 app_type_cfg->sample_rate = s_info->app_type_cfg.sample_rate;
697 app_type_cfg->bit_width = s_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700698 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530699 __func__, s_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700700 return;
701 }
702 }
703 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700704 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700705 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700706 app_type_cfg->bit_width = 16;
707}
708
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530709static bool audio_is_this_native_usecase(struct audio_usecase *uc)
710{
711 bool native_usecase = false;
712 struct stream_out *out = (struct stream_out*) uc->stream.out;
713
714 if (PCM_PLAYBACK == uc->type && out != NULL &&
715 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
716 is_offload_usecase(uc->id) &&
717 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
718 native_usecase = true;
719
720 return native_usecase;
721}
722
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530723void audio_extn_utils_update_stream_app_type_cfg_for_usecase(
724 struct audio_device *adev,
725 struct audio_usecase *usecase)
726{
727 ALOGV("%s", __func__);
728
729 switch(usecase->type) {
730 case PCM_PLAYBACK:
731 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
732 &adev->streams_output_cfg_list,
733 usecase->stream.out->devices,
734 usecase->stream.out->flags,
735 usecase->stream.out->format,
736 usecase->stream.out->sample_rate,
737 usecase->stream.out->bit_width,
738 usecase->stream.out->channel_mask,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530739 usecase->stream.out->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530740 &usecase->stream.out->app_type_cfg);
741 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
742 break;
743 case PCM_CAPTURE:
744 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
745 &adev->streams_input_cfg_list,
746 usecase->stream.in->device,
747 usecase->stream.in->flags,
748 usecase->stream.in->format,
749 usecase->stream.in->sample_rate,
750 usecase->stream.in->bit_width,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +0530751 usecase->stream.in->profile,
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530752 &usecase->stream.in->app_type_cfg);
753 ALOGV("%s Selected apptype: %d", __func__, usecase->stream.in->app_type_cfg.app_type);
754 break;
755 default:
756 ALOGE("%s: app type cfg not supported for usecase type (%d)",
757 __func__, usecase->type);
758 }
759}
760
Ben Romberger1fafdde2015-09-09 19:43:15 -0700761int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
762 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700763{
764 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530765 size_t app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {0};
766 int len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700767 struct mixer_ctl *ctl;
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530768 int pcm_device_id, acdb_dev_id = 0, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530769 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530770 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700771
772 ALOGV("%s", __func__);
773
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530774 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530775 ALOGE("%s: not a playback/capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700776 rc = 0;
777 goto exit_send_app_type_cfg;
778 }
779 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
780 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
781 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530782 (!is_offload_usecase(usecase->id)) &&
783 (usecase->type != PCM_CAPTURE)) {
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530784 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 -0700785 rc = 0;
786 goto exit_send_app_type_cfg;
787 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530788 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530789 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700790 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530791 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
792 "Audio Stream %d App Type Cfg", pcm_device_id);
793 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700794 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530795 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700796 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530797 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
798 "Audio Stream Capture %d App Type Cfg", pcm_device_id);
799 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->in_snd_device);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530800 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530801 if (acdb_dev_id <= 0) {
802 ALOGE("%s: Couldn't get the acdb dev id", __func__);
803 rc = -EINVAL;
804 goto exit_send_app_type_cfg;
805 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700806
807 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
808 if (!ctl) {
809 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
810 mixer_ctl_name);
811 rc = -EINVAL;
812 goto exit_send_app_type_cfg;
813 }
814 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800815 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530816
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530817 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out != NULL)) {
818 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
819 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
820 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
821 !(audio_is_this_native_usecase(usecase))) ||
822 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
823 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
824 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530825
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530826 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530827
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530828 property_get("audio.playback.mch.downsample",value,"");
829 if (!strncmp("true", value, sizeof("true"))) {
830 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
831 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
832 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
833 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
834 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530835
Mingming Yin21854652016-04-13 11:54:02 -0700836 if ((24 == usecase->stream.out->bit_width) &&
837 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
838 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530839 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
840 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
841 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
842 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
843 /*
844 * To best utlize DSP, check if the stream sample rate is supported/multiple of
845 * configured device sample rate, if not update the COPP rate to be equal to the
846 * device sample rate, else open COPP at stream sample rate
847 */
848 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
849 usecase->stream.out->sample_rate,
850 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700851 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
852 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
853 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
854 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
855 }
856 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
857
858 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
859 app_type_cfg[len++] = acdb_dev_id;
860 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
861 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530862 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700863 app_type_cfg[len++] = sample_rate * 4;
864 } else {
865 app_type_cfg[len++] = sample_rate;
866 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +0530867
868 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
869 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
870
871 } else if ((usecase->type == PCM_CAPTURE) && (usecase->stream.in != NULL)) {
872 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.app_type;
873 app_type_cfg[len++] = acdb_dev_id;
874 app_type_cfg[len++] = usecase->stream.in->app_type_cfg.sample_rate;
875 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
876 __func__, usecase->stream.in->app_type_cfg.app_type, acdb_dev_id,
877 usecase->stream.in->app_type_cfg.sample_rate);
878 } else {
879 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
880 app_type_cfg[len++] = acdb_dev_id;
881 app_type_cfg[len++] = sample_rate;
882 ALOGI("%s default app_type %d, acdb_dev_id %d, sample_rate %d",
883 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
884 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530885 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530886
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700887 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700888 rc = 0;
889exit_send_app_type_cfg:
890 return rc;
891}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700892
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530893int read_line_from_file(const char *path, char *buf, size_t count)
894{
895 char * fgets_ret;
896 FILE * fd;
897 int rv;
898
899 fd = fopen(path, "r");
900 if (fd == NULL)
901 return -1;
902
903 fgets_ret = fgets(buf, (int)count, fd);
904 if (NULL != fgets_ret) {
905 rv = (int)strlen(buf);
906 } else {
907 rv = ferror(fd);
908 }
909 fclose(fd);
910
911 return rv;
912}
913
Ashish Jainf1eaa582016-05-23 20:54:24 +0530914/*Translates ALSA formats to AOSP PCM formats*/
915audio_format_t alsa_format_to_hal(uint32_t alsa_format)
916{
917 audio_format_t format;
918
919 switch(alsa_format) {
920 case SNDRV_PCM_FORMAT_S16_LE:
921 format = AUDIO_FORMAT_PCM_16_BIT;
922 break;
923 case SNDRV_PCM_FORMAT_S24_3LE:
924 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
925 break;
926 case SNDRV_PCM_FORMAT_S24_LE:
927 format = AUDIO_FORMAT_PCM_8_24_BIT;
928 break;
929 case SNDRV_PCM_FORMAT_S32_LE:
930 format = AUDIO_FORMAT_PCM_32_BIT;
931 break;
932 default:
933 ALOGW("Incorrect ALSA format");
934 format = AUDIO_FORMAT_INVALID;
935 }
936 return format;
937}
938
939/*Translates hal format (AOSP) to alsa formats*/
940uint32_t hal_format_to_alsa(audio_format_t hal_format)
941{
942 uint32_t alsa_format;
943
944 switch (hal_format) {
945 case AUDIO_FORMAT_PCM_32_BIT: {
946 if (platform_supports_true_32bit())
947 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
948 else
949 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
950 }
951 break;
952 case AUDIO_FORMAT_PCM_8_BIT:
953 alsa_format = SNDRV_PCM_FORMAT_S8;
954 break;
955 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
956 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
957 break;
958 case AUDIO_FORMAT_PCM_8_24_BIT: {
959 if (platform_supports_true_32bit())
960 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
961 else
962 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
963 }
964 break;
965 case AUDIO_FORMAT_PCM_FLOAT:
966 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
967 break;
968 default:
969 case AUDIO_FORMAT_PCM_16_BIT:
970 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
971 break;
972 }
973 return alsa_format;
974}
975
Ashish Jain83a6cc22016-06-28 14:34:17 +0530976/*Translates PCM formats to AOSP formats*/
977audio_format_t pcm_format_to_hal(uint32_t pcm_format)
978{
979 audio_format_t format = AUDIO_FORMAT_INVALID;
980
981 switch(pcm_format) {
982 case PCM_FORMAT_S16_LE:
983 format = AUDIO_FORMAT_PCM_16_BIT;
984 break;
985 case PCM_FORMAT_S24_3LE:
986 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
987 break;
988 case PCM_FORMAT_S24_LE:
989 format = AUDIO_FORMAT_PCM_8_24_BIT;
990 break;
991 case PCM_FORMAT_S32_LE:
992 format = AUDIO_FORMAT_PCM_32_BIT;
993 break;
994 default:
995 ALOGW("Incorrect PCM format");
996 format = AUDIO_FORMAT_INVALID;
997 }
998 return format;
999}
1000
1001/*Translates hal format (AOSP) to alsa formats*/
1002uint32_t hal_format_to_pcm(audio_format_t hal_format)
1003{
1004 uint32_t pcm_format;
1005
1006 switch (hal_format) {
1007 case AUDIO_FORMAT_PCM_32_BIT:
1008 case AUDIO_FORMAT_PCM_8_24_BIT:
1009 case AUDIO_FORMAT_PCM_FLOAT: {
1010 if (platform_supports_true_32bit())
1011 pcm_format = PCM_FORMAT_S32_LE;
1012 else
1013 pcm_format = PCM_FORMAT_S24_3LE;
1014 }
1015 break;
1016 case AUDIO_FORMAT_PCM_8_BIT:
1017 pcm_format = PCM_FORMAT_S8;
1018 break;
1019 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1020 pcm_format = PCM_FORMAT_S24_3LE;
1021 break;
1022 default:
1023 case AUDIO_FORMAT_PCM_16_BIT:
1024 pcm_format = PCM_FORMAT_S16_LE;
1025 break;
1026 }
1027 return pcm_format;
1028}
1029
Ashish Jainf1eaa582016-05-23 20:54:24 +05301030uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
1031 uint32_t sample_rate,
1032 uint32_t noOfChannels)
1033{
1034 uint32_t fragment_size = 0;
1035 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
1036
1037 fragment_size = (pcm_offload_time
1038 * sample_rate
1039 * bytes_per_sample
1040 * noOfChannels)/1000;
1041 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
1042 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
1043 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
1044 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
1045 /*To have same PCM samples for all channels, the buffer size requires to
1046 *be multiple of (number of channels * bytes per sample)
1047 *For writes to succeed, the buffer must be written at address which is multiple of 32
1048 */
1049 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
1050
1051 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
1052 return fragment_size;
1053}
1054
1055/* Calculates the fragment size required to configure compress session.
1056 * Based on the alsa format selected, decide if conversion is needed in
1057
1058 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
1059 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
1060 */
1061void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
1062{
Ashish Jain83a6cc22016-06-28 14:34:17 +05301063 audio_format_t dst_format = out->hal_op_format;
1064 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301065 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
1066 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
1067
1068 out->compr_config.fragment_size =
1069 get_alsa_fragment_size(hal_op_bytes_per_sample,
1070 out->sample_rate,
1071 popcount(out->channel_mask));
1072
1073 if ((src_format != dst_format) &&
1074 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
1075
Ashish Jain83a6cc22016-06-28 14:34:17 +05301076 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +05301077 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
1078 hal_op_bytes_per_sample);
1079 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +05301080 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +05301081 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05301082 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +05301083 }
1084}
1085
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301086/* converts pcm format 24_8 to 8_24 inplace */
1087size_t audio_extn_utils_convert_format_24_8_to_8_24(void *buf, size_t bytes)
1088{
1089 size_t i = 0;
1090 int *int_buf_stream = buf;
1091
1092 if ((bytes % 4) != 0) {
1093 ALOGE("%s: wrong inout buffer! ... is not 32 bit aligned ", __func__);
1094 return -EINVAL;
1095 }
1096
1097 for (; i < (bytes / 4); i++)
1098 int_buf_stream[i] >>= 8;
1099
1100 return bytes;
1101}
1102
1103int get_snd_codec_id(audio_format_t format)
1104{
1105 int id = 0;
1106
1107 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1108 case AUDIO_FORMAT_MP3:
1109 id = SND_AUDIOCODEC_MP3;
1110 break;
1111 case AUDIO_FORMAT_AAC:
1112 id = SND_AUDIOCODEC_AAC;
1113 break;
1114 case AUDIO_FORMAT_AAC_ADTS:
1115 id = SND_AUDIOCODEC_AAC;
1116 break;
1117 case AUDIO_FORMAT_PCM_OFFLOAD:
1118 case AUDIO_FORMAT_PCM:
1119 id = SND_AUDIOCODEC_PCM;
1120 break;
1121 case AUDIO_FORMAT_FLAC:
1122 id = SND_AUDIOCODEC_FLAC;
1123 break;
1124 case AUDIO_FORMAT_ALAC:
1125 id = SND_AUDIOCODEC_ALAC;
1126 break;
1127 case AUDIO_FORMAT_APE:
1128 id = SND_AUDIOCODEC_APE;
1129 break;
1130 case AUDIO_FORMAT_VORBIS:
1131 id = SND_AUDIOCODEC_VORBIS;
1132 break;
1133 case AUDIO_FORMAT_WMA:
1134 id = SND_AUDIOCODEC_WMA;
1135 break;
1136 case AUDIO_FORMAT_WMA_PRO:
1137 id = SND_AUDIOCODEC_WMA_PRO;
1138 break;
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301139 case AUDIO_FORMAT_MP2:
1140 id = SND_AUDIOCODEC_MP2;
1141 break;
Satish Babu Patakokila915ecba2017-01-10 17:43:56 +05301142 case AUDIO_FORMAT_AC3:
1143 id = SND_AUDIOCODEC_AC3;
1144 break;
1145 case AUDIO_FORMAT_E_AC3:
1146 case AUDIO_FORMAT_E_AC3_JOC:
1147 id = SND_AUDIOCODEC_EAC3;
1148 break;
1149 case AUDIO_FORMAT_DTS:
1150 case AUDIO_FORMAT_DTS_HD:
1151 id = SND_AUDIOCODEC_DTS;
1152 break;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05301153 default:
1154 ALOGE("%s: Unsupported audio format :%x", __func__, format);
1155 }
1156
1157 return id;
1158}
1159
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001160void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
1161 struct audio_usecase *usecase)
1162{
1163 int type = usecase->type;
1164
1165 if (type == PCM_PLAYBACK) {
1166 struct stream_out *out = usecase->stream.out;
1167 int snd_device = usecase->out_snd_device;
1168 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001169 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301170 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001171 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301172 usecase->stream.out->app_type_cfg.sample_rate);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301173 } else if (type == PCM_CAPTURE) {
1174 platform_send_audio_calibration(adev->platform, usecase,
1175 usecase->stream.in->app_type_cfg.app_type,
1176 usecase->stream.in->app_type_cfg.sample_rate);
1177 } else {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05301178 /* when app type is default. the sample rate is not used to send cal */
1179 platform_send_audio_calibration(adev->platform, usecase,
Dhananjay Kumard6d32152016-10-13 16:11:03 +05301180 platform_get_default_app_type_v2(adev->platform, usecase->type),
1181 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001182 }
1183}
1184
Ben Rombergera04fabc2014-11-14 12:16:03 -08001185// Base64 Encode and Decode
1186// Not all features supported. This must be used only with following conditions.
1187// Decode Modes: Support with and without padding
1188// CRLF not handling. So no CRLF in string to decode.
1189// Encode Modes: Supports only padding
1190int b64decode(char *inp, int ilen, uint8_t* outp)
1191{
1192 int i, j, k, ii, num;
1193 int rem, pcnt;
1194 uint32_t res=0;
1195 uint8_t getIndex[MAX_BASEINDEX_LEN];
1196 uint8_t tmp, cflag;
1197
1198 if(inp == NULL || outp == NULL || ilen <= 0) {
1199 ALOGE("[%s] received NULL pointer or zero length",__func__);
1200 return -1;
1201 }
1202
1203 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
1204 for(i=0;i<BASE_TABLE_SIZE;i++) {
1205 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
1206 }
1207 getIndex[(uint8_t)'=']=0;
1208
1209 j=0;k=0;
1210 num = ilen/4;
1211 rem = ilen%4;
1212 if(rem==0)
1213 num = num-1;
1214 cflag=0;
1215 for(i=0; i<num; i++) {
1216 res=0;
1217 for(ii=0;ii<4;ii++) {
1218 res = res << 6;
1219 tmp = getIndex[(uint8_t)inp[j++]];
1220 res = res | tmp;
1221 cflag = cflag | tmp;
1222 }
1223 outp[k++] = (res >> 16)&0xFF;
1224 outp[k++] = (res >> 8)&0xFF;
1225 outp[k++] = res & 0xFF;
1226 }
1227
1228 // Handle last bytes special
1229 pcnt=0;
1230 if(rem == 0) {
1231 //With padding or full data
1232 res = 0;
1233 for(ii=0;ii<4;ii++) {
1234 if(inp[j] == '=')
1235 pcnt++;
1236 res = res << 6;
1237 tmp = getIndex[(uint8_t)inp[j++]];
1238 res = res | tmp;
1239 cflag = cflag | tmp;
1240 }
1241 outp[k++] = res >> 16;
1242 if(pcnt == 2)
1243 goto done;
1244 outp[k++] = (res>>8)&0xFF;
1245 if(pcnt == 1)
1246 goto done;
1247 outp[k++] = res&0xFF;
1248 } else {
1249 //without padding
1250 res = 0;
1251 for(i=0;i<rem;i++) {
1252 res = res << 6;
1253 tmp = getIndex[(uint8_t)inp[j++]];
1254 res = res | tmp;
1255 cflag = cflag | tmp;
1256 }
1257 for(i=rem;i<4;i++) {
1258 res = res << 6;
1259 pcnt++;
1260 }
1261 outp[k++] = res >> 16;
1262 if(pcnt == 2)
1263 goto done;
1264 outp[k++] = (res>>8)&0xFF;
1265 if(pcnt == 1)
1266 goto done;
1267 outp[k++] = res&0xFF;
1268 }
1269done:
1270 if(cflag == 0xFF) {
1271 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1272 __func__, inp);
1273 return 0;
1274 }
1275 return k;
1276}
1277
1278int b64encode(uint8_t *inp, int ilen, char* outp)
1279{
1280 int i,j,k, num;
1281 int rem=0;
1282 uint32_t res=0;
1283
1284 if(inp == NULL || outp == NULL || ilen<=0) {
1285 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1286 return -1;
1287 }
1288
1289 num = ilen/3;
1290 rem = ilen%3;
1291 j=0;k=0;
1292 for(i=0; i<num; i++) {
1293 //prepare index
1294 res = inp[j++]<<16;
1295 res = res | inp[j++]<<8;
1296 res = res | inp[j++];
1297 //get output map from index
1298 outp[k++] = (char) bTable[(res>>18)&0x3F];
1299 outp[k++] = (char) bTable[(res>>12)&0x3F];
1300 outp[k++] = (char) bTable[(res>>6)&0x3F];
1301 outp[k++] = (char) bTable[res&0x3F];
1302 }
1303
1304 switch(rem) {
1305 case 1:
1306 res = inp[j++]<<16;
1307 outp[k++] = (char) bTable[res>>18];
1308 outp[k++] = (char) bTable[(res>>12)&0x3F];
1309 //outp[k++] = '=';
1310 //outp[k++] = '=';
1311 break;
1312 case 2:
1313 res = inp[j++]<<16;
1314 res = res | inp[j++]<<8;
1315 outp[k++] = (char) bTable[res>>18];
1316 outp[k++] = (char) bTable[(res>>12)&0x3F];
1317 outp[k++] = (char) bTable[(res>>6)&0x3F];
1318 //outp[k++] = '=';
1319 break;
1320 default:
1321 break;
1322 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001323 outp[k] = '\0';
1324 return k;
1325}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301326
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301327
1328int audio_extn_utils_get_codec_version(const char *snd_card_name,
1329 int card_num,
1330 char *codec_version)
1331{
1332 char procfs_path[50];
1333 FILE *fp;
1334
1335 if (strstr(snd_card_name, "tasha")) {
1336 snprintf(procfs_path, sizeof(procfs_path),
1337 "/proc/asound/card%d/codecs/tasha/version", card_num);
1338 if ((fp = fopen(procfs_path, "r")) != NULL) {
1339 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1340 fclose(fp);
1341 } else {
1342 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1343 return -ENOENT;
1344 }
1345 ALOGD("%s: codec version %s", __func__, codec_version);
1346 }
1347
1348 return 0;
1349}
1350
1351
Ashish Jain81eb2a82015-05-13 10:52:34 +05301352#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1353
1354void get_default_compressed_channel_status(
1355 unsigned char *channel_status)
1356{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301357 memset(channel_status,0,24);
1358
1359 /* block start bit in preamble bit 3 */
1360 channel_status[0] |= PROFESSIONAL;
1361 //compre out
1362 channel_status[0] |= NON_LPCM;
1363 // sample rate; fixed 48K for default/transcode
1364 channel_status[3] |= SR_48000;
1365}
1366
1367#ifdef HDMI_PASSTHROUGH_ENABLED
1368int32_t get_compressed_channel_status(void *audio_stream_data,
1369 uint32_t audio_frame_size,
1370 unsigned char *channel_status,
1371 enum audio_parser_code_type codec_type)
1372 // codec_type - AUDIO_PARSER_CODEC_AC3
1373 // - AUDIO_PARSER_CODEC_DTS
1374{
1375 unsigned char *stream;
1376 int ret = 0;
1377 stream = (unsigned char *)audio_stream_data;
1378
1379 if (audio_stream_data == NULL || audio_frame_size == 0) {
1380 ALOGW("no buffer to get channel status, return default for compress");
1381 get_default_compressed_channel_status(channel_status);
1382 return ret;
1383 }
1384
1385 memset(channel_status,0,24);
1386 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1387 {
1388 ALOGE("init audio parser failed");
1389 return -1;
1390 }
1391 ret = get_channel_status(channel_status, codec_type);
1392 return ret;
1393
1394}
1395
1396#endif
1397
1398void get_lpcm_channel_status(uint32_t sampleRate,
1399 unsigned char *channel_status)
1400{
1401 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301402 memset(channel_status,0,24);
1403 /* block start bit in preamble bit 3 */
1404 channel_status[0] |= PROFESSIONAL;
1405 //LPCM OUT
1406 channel_status[0] &= ~NON_LPCM;
1407
1408 switch (sampleRate) {
1409 case 8000:
1410 case 11025:
1411 case 12000:
1412 case 16000:
1413 case 22050:
1414 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301415 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301416 case 24000:
1417 channel_status[3] |= SR_24000;
1418 break;
1419 case 32000:
1420 channel_status[3] |= SR_32000;
1421 break;
1422 case 44100:
1423 channel_status[3] |= SR_44100;
1424 break;
1425 case 48000:
1426 channel_status[3] |= SR_48000;
1427 break;
1428 case 88200:
1429 channel_status[3] |= SR_88200;
1430 break;
1431 case 96000:
1432 channel_status[3] |= SR_96000;
1433 break;
1434 case 176400:
1435 channel_status[3] |= SR_176400;
1436 break;
1437 case 192000:
1438 channel_status[3] |= SR_192000;
1439 break;
1440 default:
1441 ALOGV("Invalid sample_rate %u\n", sampleRate);
1442 status = -1;
1443 break;
1444 }
1445}
1446
1447void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1448{
1449 unsigned char channel_status[24]={0};
1450 struct snd_aes_iec958 iec958;
1451 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1452 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301453 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301454#ifdef HDMI_PASSTHROUGH_ENABLED
1455 if (audio_extn_is_dolby_format(out->format) &&
1456 /*TODO:Extend code to support DTS passthrough*/
1457 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301458 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301459 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1460 } else
1461#endif
1462 {
1463 /*set channel status bit for LPCM*/
1464 get_lpcm_channel_status(out->sample_rate, channel_status);
1465 }
1466
1467 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1468 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1469 if (!ctl) {
1470 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1471 __func__, mixer_ctl_name);
1472 return;
1473 }
1474 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1475 ALOGE("%s: Could not set channel status for ext HDMI ",
1476 __func__);
1477 return;
1478 }
1479
1480}
1481#endif