blob: eb3213c44e570b9b9be333e277105f91863a3317 [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"
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070037
Ashish Jain81eb2a82015-05-13 10:52:34 +053038#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
39#ifdef HDMI_PASSTHROUGH_ENABLED
40#include "audio_parsers.h"
41#endif
42#endif
43
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070044#define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf"
45
46#define OUTPUTS_TAG "outputs"
47
48#define DYNAMIC_VALUE_TAG "dynamic"
49#define FLAGS_TAG "flags"
50#define FORMATS_TAG "formats"
51#define SAMPLING_RATES_TAG "sampling_rates"
52#define BIT_WIDTH_TAG "bit_width"
53#define APP_TYPE_TAG "app_type"
54
55#define STRING_TO_ENUM(string) { #string, string }
56#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
57
Ben Rombergera04fabc2014-11-14 12:16:03 -080058#define BASE_TABLE_SIZE 64
59#define MAX_BASEINDEX_LEN 256
60
Ashish Jain81eb2a82015-05-13 10:52:34 +053061#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
62#define PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
63#define NON_LPCM (1<<1) /* 0 = audio, 1 = non-audio */
64#define SR_44100 (0<<0) /* 44.1kHz */
65#define SR_NOTID (1<<0) /* non indicated */
66#define SR_48000 (2<<0) /* 48kHz */
67#define SR_32000 (3<<0) /* 32kHz */
68#define SR_22050 (4<<0) /* 22.05kHz */
69#define SR_24000 (6<<0) /* 24kHz */
70#define SR_88200 (8<<0) /* 88.2kHz */
71#define SR_96000 (10<<0) /* 96kHz */
72#define SR_176400 (12<<0) /* 176.4kHz */
73#define SR_192000 (14<<0) /* 192kHz */
74
75#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070076struct string_to_enum {
77 const char *name;
78 uint32_t value;
79};
80
81const struct string_to_enum s_flag_name_to_enum_table[] = {
82 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
vivek mehta0ea887a2015-08-26 14:01:20 -070083 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT_PCM),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070084 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
85 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
86 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
87 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
88 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070089 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070090#ifdef INCALL_MUSIC_ENABLED
91 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC),
92#endif
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070093 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070094};
95
96const struct string_to_enum s_format_name_to_enum_table[] = {
Ashish Jain83a6cc22016-06-28 14:34:17 +053097 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070098 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +053099 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
100 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530101 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
103 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
104 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700105 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
106 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700107 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700108 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530110 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
111#ifdef AUDIO_EXTN_FORMATS_ENABLED
112 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700113 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
114 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
115 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700116 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
117 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
118 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
119 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
120 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
121 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
122 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700123 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530124 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
125 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700126 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800127 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
128 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
129 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530131 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
132 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
133 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530134 STRING_TO_ENUM(AUDIO_FORMAT_DSD),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700135#endif
136};
137
Ben Rombergera04fabc2014-11-14 12:16:03 -0800138static char bTable[BASE_TABLE_SIZE] = {
139 'A','B','C','D','E','F','G','H','I','J','K','L',
140 'M','N','O','P','Q','R','S','T','U','V','W','X',
141 'Y','Z','a','b','c','d','e','f','g','h','i','j',
142 'k','l','m','n','o','p','q','r','s','t','u','v',
143 'w','x','y','z','0','1','2','3','4','5','6','7',
144 '8','9','+','/'
145};
146
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700147static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
148 const char *name)
149{
150 size_t i;
151 for (i = 0; i < size; i++) {
152 if (strcmp(table[i].name, name) == 0) {
153 ALOGV("%s found %s", __func__, table[i].name);
154 return table[i].value;
155 }
156 }
157 return 0;
158}
159
160static audio_output_flags_t parse_flag_names(char *name)
161{
162 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800163 char *last_r;
164 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700165 while (flag_name != NULL) {
166 if (strlen(flag_name) != 0) {
167 flag |= string_to_enum(s_flag_name_to_enum_table,
168 ARRAY_SIZE(s_flag_name_to_enum_table),
169 flag_name);
170 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800171 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700172 }
173
174 ALOGV("parse_flag_names: flag - %d", flag);
175 return (audio_output_flags_t)flag;
176}
177
178static void parse_format_names(char *name, struct streams_output_cfg *so_info)
179{
180 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800181 char *last_r;
182 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700183
184 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
185 return;
186
187 list_init(&so_info->format_list);
188 while (str != NULL) {
189 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
190 ARRAY_SIZE(s_format_name_to_enum_table), str);
191 ALOGV("%s: format - %d", __func__, format);
192 if (format != 0) {
193 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700194 if (sf_info == NULL)
195 break; /* return whatever was parsed */
196
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700197 sf_info->format = format;
198 list_add_tail(&so_info->format_list, &sf_info->list);
199 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800200 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700201 }
202}
203
Amit Shekhar6f461b12014-08-01 14:52:58 -0700204static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700205{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700206 struct stream_sample_rate *ss_info = NULL;
207 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800208 char *last_r;
209 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700210
Amit Shekhar6f461b12014-08-01 14:52:58 -0700211 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
212 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700213
Amit Shekhar6f461b12014-08-01 14:52:58 -0700214 list_init(&so_info->sample_rate_list);
215 while (str != NULL) {
216 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
217 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
218 if (0 != sample_rate) {
219 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800220 if (!ss_info) {
221 ALOGE("%s: memory allocation failure", __func__);
222 return;
223 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700224 ss_info->sample_rate = sample_rate;
225 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
226 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800227 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700228 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700229}
230
231static int parse_bit_width_names(char *name)
232{
233 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800234 char *last_r;
235 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700236
237 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
238 bit_width = (int)strtol(str, (char **)NULL, 10);
239
240 ALOGV("%s: bit_width - %d", __func__, bit_width);
241 return bit_width;
242}
243
244static int parse_app_type_names(void *platform, char *name)
245{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700246 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800247 char *last_r;
248 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700249
250 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
251 app_type = (int)strtol(str, (char **)NULL, 10);
252
253 ALOGV("%s: app_type - %d", __func__, app_type);
254 return app_type;
255}
256
257static void update_streams_output_cfg_list(cnode *root, void *platform,
258 struct listnode *streams_output_cfg_list)
259{
260 cnode *node = root->first_child;
261 struct streams_output_cfg *so_info;
262
263 ALOGV("%s", __func__);
264 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700265
266 if (!so_info) {
267 ALOGE("failed to allocate mem for so_info list element");
268 return;
269 }
270
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700271 while (node) {
272 if (strcmp(node->name, FLAGS_TAG) == 0) {
273 so_info->flags = parse_flag_names((char *)node->value);
274 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
275 parse_format_names((char *)node->value, so_info);
276 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700277 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
278 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700279 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
280 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
281 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
282 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
283 }
284 node = node->next;
285 }
286 list_add_tail(streams_output_cfg_list, &so_info->list);
287}
288
289static void load_output(cnode *root, void *platform,
290 struct listnode *streams_output_cfg_list)
291{
292 cnode *node = config_find(root, OUTPUTS_TAG);
293 if (node == NULL) {
294 ALOGE("%s: could not load output, node is NULL", __func__);
295 return;
296 }
297
298 node = node->first_child;
299 while (node) {
300 ALOGV("%s: loading output %s", __func__, node->name);
301 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
302 node = node->next;
303 }
304}
305
306static void send_app_type_cfg(void *platform, struct mixer *mixer,
307 struct listnode *streams_output_cfg_list)
308{
309 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
310 int length = 0, i, num_app_types = 0;
311 struct listnode *node;
312 bool update;
313 struct mixer_ctl *ctl = NULL;
314 const char *mixer_ctl_name = "App Type Config";
315 struct streams_output_cfg *so_info;
316
317 if (!mixer) {
318 ALOGE("%s: mixer is null",__func__);
319 return;
320 }
321 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
322 if (!ctl) {
323 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
324 return;
325 }
326 if (streams_output_cfg_list == NULL) {
327 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700328 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700329 app_type_cfg[length++] = 48000;
330 app_type_cfg[length++] = 16;
331 mixer_ctl_set_array(ctl, app_type_cfg, length);
332 return;
333 }
334
335 app_type_cfg[length++] = num_app_types;
336 list_for_each(node, streams_output_cfg_list) {
337 so_info = node_to_item(node, struct streams_output_cfg, list);
338 update = true;
339 for (i=0; i<length; i=i+3) {
340 if (app_type_cfg[i+1] == -1)
341 break;
342 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
343 update = false;
344 break;
345 }
346 }
347 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
348 num_app_types += 1 ;
349 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
350 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
351 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
352 }
353 }
354 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
355 if (num_app_types) {
356 app_type_cfg[0] = num_app_types;
357 mixer_ctl_set_array(ctl, app_type_cfg, length);
358 }
359}
360
361void audio_extn_utils_update_streams_output_cfg_list(void *platform,
362 struct mixer *mixer,
363 struct listnode *streams_output_cfg_list)
364{
365 cnode *root;
366 char *data;
367
368 ALOGV("%s", __func__);
369 list_init(streams_output_cfg_list);
370 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
371 if (data == NULL) {
372 send_app_type_cfg(platform, mixer, NULL);
373 ALOGE("%s: could not load output policy config file", __func__);
374 return;
375 }
376
377 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700378 if (root == NULL) {
379 ALOGE("cfg_list, NULL config root");
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800380 free(data);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700381 return;
382 }
383
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700384 config_load(root, data);
385 load_output(root, platform, streams_output_cfg_list);
386
387 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800388
389 config_free(root);
390 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700391}
392
393void audio_extn_utils_dump_streams_output_cfg_list(
394 struct listnode *streams_output_cfg_list)
395{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700396 struct listnode *node_i, *node_j;
397 struct streams_output_cfg *so_info;
398 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700399 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700400 ALOGV("%s", __func__);
401 list_for_each(node_i, streams_output_cfg_list) {
402 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700403 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
404 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700405 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
406 list_for_each(node_j, &so_info->format_list) {
407 sf_info = node_to_item(node_j, struct stream_format, list);
408 ALOGV("format-%x", sf_info->format);
409 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700410 list_for_each(node_j, &so_info->sample_rate_list) {
411 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
412 ALOGV("sample rate-%d", ss_info->sample_rate);
413 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700414 }
415}
416
417void audio_extn_utils_release_streams_output_cfg_list(
418 struct listnode *streams_output_cfg_list)
419{
420 struct listnode *node_i, *node_j;
421 struct streams_output_cfg *so_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700422
423 ALOGV("%s", __func__);
424 while (!list_empty(streams_output_cfg_list)) {
425 node_i = list_head(streams_output_cfg_list);
426 so_info = node_to_item(node_i, struct streams_output_cfg, list);
427 while (!list_empty(&so_info->format_list)) {
428 node_j = list_head(&so_info->format_list);
429 list_remove(node_j);
430 free(node_to_item(node_j, struct stream_format, list));
431 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700432 while (!list_empty(&so_info->sample_rate_list)) {
433 node_j = list_head(&so_info->sample_rate_list);
434 list_remove(node_j);
435 free(node_to_item(node_j, struct stream_sample_rate, list));
436 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700437 list_remove(node_i);
438 free(node_to_item(node_i, struct streams_output_cfg, list));
439 }
440}
441
Amit Shekhar6f461b12014-08-01 14:52:58 -0700442static bool set_output_cfg(struct streams_output_cfg *so_info,
443 struct stream_app_type_cfg *app_type_cfg,
444 uint32_t sample_rate, uint32_t bit_width)
445 {
446 struct listnode *node_i;
447 struct stream_sample_rate *ss_info;
448 list_for_each(node_i, &so_info->sample_rate_list) {
449 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
450 if ((sample_rate <= ss_info->sample_rate) &&
451 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700452
Amit Shekhar6f461b12014-08-01 14:52:58 -0700453 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
454 app_type_cfg->sample_rate = ss_info->sample_rate;
455 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
456 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
457 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
458 return true;
459 }
460 }
461 /*
462 * Reiterate through the list assuming dafault sample rate.
463 * Handles scenario where input sample rate is higher
464 * than all sample rates in list for the input bit width.
465 */
466 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800467
Amit Shekhar6f461b12014-08-01 14:52:58 -0700468 list_for_each(node_i, &so_info->sample_rate_list) {
469 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
470 if ((sample_rate <= ss_info->sample_rate) &&
471 (bit_width == so_info->app_type_cfg.bit_width)) {
472 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
473 app_type_cfg->sample_rate = sample_rate;
474 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800475 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 -0700476 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
477 return true;
478 }
479 }
480 return false;
481}
482
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700483void audio_extn_utils_update_stream_app_type_cfg(void *platform,
484 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700485 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700486 audio_output_flags_t flags,
487 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700488 uint32_t sample_rate,
489 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530490 audio_channel_mask_t channel_mask,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700491 struct stream_app_type_cfg *app_type_cfg)
492{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530493 struct listnode *node_i, *node_j;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700494 struct streams_output_cfg *so_info;
495 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530496 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700497
Ashish Jain058165c2016-09-28 23:18:48 +0530498 if ((bit_width >= 24) &&
Amit Shekhar1d896042014-10-03 13:16:09 -0700499 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700500 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
501 if (-ENOSYS != bw)
502 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700503 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
504 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
505 }
506
Manish Dewangan837dc462015-05-27 10:17:41 +0530507 property_get("audio.playback.mch.downsample",value,"");
508 if (!strncmp("true", value, sizeof("true"))) {
509 if ((popcount(channel_mask) > 2) &&
510 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
511 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
512 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
513 ALOGD("%s: MCH session defaulting sample rate to %d",
514 __func__, sample_rate);
515 }
516 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530517
518 /* Set sampling rate to 176.4 for DSD64
519 * and 352.8Khz for DSD128.
520 * Set Bit Width to 16. output will be 16 bit
521 * post DoP in ASM.
522 */
523 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) &&
524 (format == AUDIO_FORMAT_DSD)) {
525 bit_width = 16;
526 if (sample_rate == INPUT_SAMPLING_RATE_DSD64)
527 sample_rate = OUTPUT_SAMPLING_RATE_DSD64;
528 else if (sample_rate == INPUT_SAMPLING_RATE_DSD128)
529 sample_rate = OUTPUT_SAMPLING_RATE_DSD128;
530 }
531
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530532 if(devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
533 //TODO: Handle fractional sampling rate configuration for LL
534 audio_extn_a2dp_get_apptype_params(&sample_rate, &bit_width);
535 ALOGI("%s using %d sampling rate %d bit width for A2DP CoPP",
536 __func__, sample_rate, bit_width);
537 }
538
Amit Shekhar1d896042014-10-03 13:16:09 -0700539 ALOGV("%s: flags: %x, format: %x sample_rate %d",
540 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541 list_for_each(node_i, streams_output_cfg_list) {
542 so_info = node_to_item(node_i, struct streams_output_cfg, list);
543 if (so_info->flags == flags) {
544 list_for_each(node_j, &so_info->format_list) {
545 sf_info = node_to_item(node_j, struct stream_format, list);
546 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700547 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
548 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700549 }
550 }
551 }
552 }
553 list_for_each(node_i, streams_output_cfg_list) {
554 so_info = node_to_item(node_i, struct streams_output_cfg, list);
555 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
556 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700557 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
558 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
559 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700560 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
561 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700562 return;
563 }
564 }
565 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700566 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700567 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700568 app_type_cfg->bit_width = 16;
569}
570
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530571static bool audio_is_this_native_usecase(struct audio_usecase *uc)
572{
573 bool native_usecase = false;
574 struct stream_out *out = (struct stream_out*) uc->stream.out;
575
576 if (PCM_PLAYBACK == uc->type && out != NULL &&
577 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
578 is_offload_usecase(uc->id) &&
579 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
580 native_usecase = true;
581
582 return native_usecase;
583}
584
Ben Romberger1fafdde2015-09-09 19:43:15 -0700585int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
586 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700587{
588 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
589 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700590 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530591 int pcm_device_id = 0, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530592 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530593 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700594
595 ALOGV("%s", __func__);
596
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530597 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
598 ALOGE("%s: not a playback or capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700599 rc = 0;
600 goto exit_send_app_type_cfg;
601 }
602 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
603 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
604 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530605 (!is_offload_usecase(usecase->id)) &&
606 (usecase->type != PCM_CAPTURE)) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700607 ALOGV("%s: a rx/tx path where app type cfg is not required %d", __func__, usecase->id);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700608 rc = 0;
609 goto exit_send_app_type_cfg;
610 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530611 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530612 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700613 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
614 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530615 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700616 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530617 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700618
619 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
620 "Audio Stream %d App Type Cfg", pcm_device_id);
621
622 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
623 if (!ctl) {
624 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
625 mixer_ctl_name);
626 rc = -EINVAL;
627 goto exit_send_app_type_cfg;
628 }
629 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800630 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700631 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
632 if (acdb_dev_id < 0) {
633 ALOGE("%s: Couldn't get the acdb dev id", __func__);
634 rc = -EINVAL;
635 goto exit_send_app_type_cfg;
636 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530637 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out == NULL)) {
638 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
639 app_type_cfg[len++] = platform_get_default_app_type(adev->platform);
640 app_type_cfg[len++] = acdb_dev_id;
641 app_type_cfg[len++] = sample_rate;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530642 ALOGI("%s:%d PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
643 __func__, __LINE__,
644 platform_get_default_app_type(adev->platform),
645 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530646 } else if (usecase->type == PCM_PLAYBACK) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530647
Sidipotu Ashokfdd505a2016-02-11 10:31:38 +0530648 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Ashish Jainc02430d2016-01-04 10:42:43 +0530649 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530650 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
651 !(audio_is_this_native_usecase(usecase))) ||
652 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
653 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jainc02430d2016-01-04 10:42:43 +0530654 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530655
Ashish Jainc02430d2016-01-04 10:42:43 +0530656 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530657
Ashish Jainc02430d2016-01-04 10:42:43 +0530658 property_get("audio.playback.mch.downsample",value,"");
659 if (!strncmp("true", value, sizeof("true"))) {
660 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
661 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
662 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
663 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
664 }
665
Mingming Yin21854652016-04-13 11:54:02 -0700666 if ((24 == usecase->stream.out->bit_width) &&
667 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
668 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jaina052e572016-11-07 16:41:07 +0530669 } else if ((snd_device == SND_DEVICE_OUT_HDMI ||
670 snd_device == SND_DEVICE_OUT_USB_HEADSET ||
671 snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
672 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
673 /*
674 * To best utlize DSP, check if the stream sample rate is supported/multiple of
675 * configured device sample rate, if not update the COPP rate to be equal to the
676 * device sample rate, else open COPP at stream sample rate
677 */
678 platform_check_and_update_copp_sample_rate(adev->platform, snd_device,
679 usecase->stream.out->sample_rate,
680 &usecase->stream.out->app_type_cfg.sample_rate);
Mingming Yin21854652016-04-13 11:54:02 -0700681 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
682 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
683 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
684 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
685 }
686 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
687
688 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
689 app_type_cfg[len++] = acdb_dev_id;
690 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
691 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530692 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700693 app_type_cfg[len++] = sample_rate * 4;
694 } else {
695 app_type_cfg[len++] = sample_rate;
696 }
697 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
698 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530699 } else if (usecase->type == PCM_CAPTURE) {
700 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
701 app_type_cfg[len++] = acdb_dev_id;
702 app_type_cfg[len++] = sample_rate;
703 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
704 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
705 acdb_dev_id, sample_rate);
706 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530707
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700708 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700709 rc = 0;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530710 ALOGI("%s:becf: adm: app_type %d, acdb_dev_id %d, sample_rate %d",
711 __func__,
712 platform_get_default_app_type_v2(adev->platform, usecase->type),
713 acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700714exit_send_app_type_cfg:
715 return rc;
716}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700717
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530718int read_line_from_file(const char *path, char *buf, size_t count)
719{
720 char * fgets_ret;
721 FILE * fd;
722 int rv;
723
724 fd = fopen(path, "r");
725 if (fd == NULL)
726 return -1;
727
728 fgets_ret = fgets(buf, (int)count, fd);
729 if (NULL != fgets_ret) {
730 rv = (int)strlen(buf);
731 } else {
732 rv = ferror(fd);
733 }
734 fclose(fd);
735
736 return rv;
737}
738
Ashish Jainf1eaa582016-05-23 20:54:24 +0530739/*Translates ALSA formats to AOSP PCM formats*/
740audio_format_t alsa_format_to_hal(uint32_t alsa_format)
741{
742 audio_format_t format;
743
744 switch(alsa_format) {
745 case SNDRV_PCM_FORMAT_S16_LE:
746 format = AUDIO_FORMAT_PCM_16_BIT;
747 break;
748 case SNDRV_PCM_FORMAT_S24_3LE:
749 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
750 break;
751 case SNDRV_PCM_FORMAT_S24_LE:
752 format = AUDIO_FORMAT_PCM_8_24_BIT;
753 break;
754 case SNDRV_PCM_FORMAT_S32_LE:
755 format = AUDIO_FORMAT_PCM_32_BIT;
756 break;
757 default:
758 ALOGW("Incorrect ALSA format");
759 format = AUDIO_FORMAT_INVALID;
760 }
761 return format;
762}
763
764/*Translates hal format (AOSP) to alsa formats*/
765uint32_t hal_format_to_alsa(audio_format_t hal_format)
766{
767 uint32_t alsa_format;
768
769 switch (hal_format) {
770 case AUDIO_FORMAT_PCM_32_BIT: {
771 if (platform_supports_true_32bit())
772 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
773 else
774 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
775 }
776 break;
777 case AUDIO_FORMAT_PCM_8_BIT:
778 alsa_format = SNDRV_PCM_FORMAT_S8;
779 break;
780 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
781 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
782 break;
783 case AUDIO_FORMAT_PCM_8_24_BIT: {
784 if (platform_supports_true_32bit())
785 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
786 else
787 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
788 }
789 break;
790 case AUDIO_FORMAT_PCM_FLOAT:
791 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
792 break;
793 default:
794 case AUDIO_FORMAT_PCM_16_BIT:
795 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
796 break;
797 }
798 return alsa_format;
799}
800
Ashish Jain83a6cc22016-06-28 14:34:17 +0530801/*Translates PCM formats to AOSP formats*/
802audio_format_t pcm_format_to_hal(uint32_t pcm_format)
803{
804 audio_format_t format = AUDIO_FORMAT_INVALID;
805
806 switch(pcm_format) {
807 case PCM_FORMAT_S16_LE:
808 format = AUDIO_FORMAT_PCM_16_BIT;
809 break;
810 case PCM_FORMAT_S24_3LE:
811 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
812 break;
813 case PCM_FORMAT_S24_LE:
814 format = AUDIO_FORMAT_PCM_8_24_BIT;
815 break;
816 case PCM_FORMAT_S32_LE:
817 format = AUDIO_FORMAT_PCM_32_BIT;
818 break;
819 default:
820 ALOGW("Incorrect PCM format");
821 format = AUDIO_FORMAT_INVALID;
822 }
823 return format;
824}
825
826/*Translates hal format (AOSP) to alsa formats*/
827uint32_t hal_format_to_pcm(audio_format_t hal_format)
828{
829 uint32_t pcm_format;
830
831 switch (hal_format) {
832 case AUDIO_FORMAT_PCM_32_BIT:
833 case AUDIO_FORMAT_PCM_8_24_BIT:
834 case AUDIO_FORMAT_PCM_FLOAT: {
835 if (platform_supports_true_32bit())
836 pcm_format = PCM_FORMAT_S32_LE;
837 else
838 pcm_format = PCM_FORMAT_S24_3LE;
839 }
840 break;
841 case AUDIO_FORMAT_PCM_8_BIT:
842 pcm_format = PCM_FORMAT_S8;
843 break;
844 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
845 pcm_format = PCM_FORMAT_S24_3LE;
846 break;
847 default:
848 case AUDIO_FORMAT_PCM_16_BIT:
849 pcm_format = PCM_FORMAT_S16_LE;
850 break;
851 }
852 return pcm_format;
853}
854
Ashish Jainf1eaa582016-05-23 20:54:24 +0530855uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
856 uint32_t sample_rate,
857 uint32_t noOfChannels)
858{
859 uint32_t fragment_size = 0;
860 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
861
862 fragment_size = (pcm_offload_time
863 * sample_rate
864 * bytes_per_sample
865 * noOfChannels)/1000;
866 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
867 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
868 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
869 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
870 /*To have same PCM samples for all channels, the buffer size requires to
871 *be multiple of (number of channels * bytes per sample)
872 *For writes to succeed, the buffer must be written at address which is multiple of 32
873 */
874 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
875
876 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
877 return fragment_size;
878}
879
880/* Calculates the fragment size required to configure compress session.
881 * Based on the alsa format selected, decide if conversion is needed in
882
883 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
884 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
885 */
886void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
887{
Ashish Jain83a6cc22016-06-28 14:34:17 +0530888 audio_format_t dst_format = out->hal_op_format;
889 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +0530890 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
891 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
892
893 out->compr_config.fragment_size =
894 get_alsa_fragment_size(hal_op_bytes_per_sample,
895 out->sample_rate,
896 popcount(out->channel_mask));
897
898 if ((src_format != dst_format) &&
899 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
900
Ashish Jain83a6cc22016-06-28 14:34:17 +0530901 out->hal_fragment_size =
Ashish Jainf1eaa582016-05-23 20:54:24 +0530902 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
903 hal_op_bytes_per_sample);
904 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
Ashish Jain83a6cc22016-06-28 14:34:17 +0530905 out->hal_fragment_size, src_format, dst_format);
Ashish Jainf1eaa582016-05-23 20:54:24 +0530906 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +0530907 out->hal_fragment_size = out->compr_config.fragment_size;
Ashish Jainf1eaa582016-05-23 20:54:24 +0530908 }
909}
910
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700911void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
912 struct audio_usecase *usecase)
913{
914 int type = usecase->type;
915
916 if (type == PCM_PLAYBACK) {
917 struct stream_out *out = usecase->stream.out;
918 int snd_device = usecase->out_snd_device;
919 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800920 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530921 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700922 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530923 usecase->stream.out->app_type_cfg.sample_rate);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700924 }
925 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530926 /* when app type is default. the sample rate is not used to send cal */
927 platform_send_audio_calibration(adev->platform, usecase,
Srikanth Uyyalaa1e32352015-10-09 14:48:04 +0530928 platform_get_default_app_type_v2(adev->platform, usecase->type),
929 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700930 }
931}
932
Ben Rombergera04fabc2014-11-14 12:16:03 -0800933// Base64 Encode and Decode
934// Not all features supported. This must be used only with following conditions.
935// Decode Modes: Support with and without padding
936// CRLF not handling. So no CRLF in string to decode.
937// Encode Modes: Supports only padding
938int b64decode(char *inp, int ilen, uint8_t* outp)
939{
940 int i, j, k, ii, num;
941 int rem, pcnt;
942 uint32_t res=0;
943 uint8_t getIndex[MAX_BASEINDEX_LEN];
944 uint8_t tmp, cflag;
945
946 if(inp == NULL || outp == NULL || ilen <= 0) {
947 ALOGE("[%s] received NULL pointer or zero length",__func__);
948 return -1;
949 }
950
951 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
952 for(i=0;i<BASE_TABLE_SIZE;i++) {
953 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
954 }
955 getIndex[(uint8_t)'=']=0;
956
957 j=0;k=0;
958 num = ilen/4;
959 rem = ilen%4;
960 if(rem==0)
961 num = num-1;
962 cflag=0;
963 for(i=0; i<num; i++) {
964 res=0;
965 for(ii=0;ii<4;ii++) {
966 res = res << 6;
967 tmp = getIndex[(uint8_t)inp[j++]];
968 res = res | tmp;
969 cflag = cflag | tmp;
970 }
971 outp[k++] = (res >> 16)&0xFF;
972 outp[k++] = (res >> 8)&0xFF;
973 outp[k++] = res & 0xFF;
974 }
975
976 // Handle last bytes special
977 pcnt=0;
978 if(rem == 0) {
979 //With padding or full data
980 res = 0;
981 for(ii=0;ii<4;ii++) {
982 if(inp[j] == '=')
983 pcnt++;
984 res = res << 6;
985 tmp = getIndex[(uint8_t)inp[j++]];
986 res = res | tmp;
987 cflag = cflag | tmp;
988 }
989 outp[k++] = res >> 16;
990 if(pcnt == 2)
991 goto done;
992 outp[k++] = (res>>8)&0xFF;
993 if(pcnt == 1)
994 goto done;
995 outp[k++] = res&0xFF;
996 } else {
997 //without padding
998 res = 0;
999 for(i=0;i<rem;i++) {
1000 res = res << 6;
1001 tmp = getIndex[(uint8_t)inp[j++]];
1002 res = res | tmp;
1003 cflag = cflag | tmp;
1004 }
1005 for(i=rem;i<4;i++) {
1006 res = res << 6;
1007 pcnt++;
1008 }
1009 outp[k++] = res >> 16;
1010 if(pcnt == 2)
1011 goto done;
1012 outp[k++] = (res>>8)&0xFF;
1013 if(pcnt == 1)
1014 goto done;
1015 outp[k++] = res&0xFF;
1016 }
1017done:
1018 if(cflag == 0xFF) {
1019 ALOGE("[%s] base64 decode failed. Invalid character found %s",
1020 __func__, inp);
1021 return 0;
1022 }
1023 return k;
1024}
1025
1026int b64encode(uint8_t *inp, int ilen, char* outp)
1027{
1028 int i,j,k, num;
1029 int rem=0;
1030 uint32_t res=0;
1031
1032 if(inp == NULL || outp == NULL || ilen<=0) {
1033 ALOGE("[%s] received NULL pointer or zero input length",__func__);
1034 return -1;
1035 }
1036
1037 num = ilen/3;
1038 rem = ilen%3;
1039 j=0;k=0;
1040 for(i=0; i<num; i++) {
1041 //prepare index
1042 res = inp[j++]<<16;
1043 res = res | inp[j++]<<8;
1044 res = res | inp[j++];
1045 //get output map from index
1046 outp[k++] = (char) bTable[(res>>18)&0x3F];
1047 outp[k++] = (char) bTable[(res>>12)&0x3F];
1048 outp[k++] = (char) bTable[(res>>6)&0x3F];
1049 outp[k++] = (char) bTable[res&0x3F];
1050 }
1051
1052 switch(rem) {
1053 case 1:
1054 res = inp[j++]<<16;
1055 outp[k++] = (char) bTable[res>>18];
1056 outp[k++] = (char) bTable[(res>>12)&0x3F];
1057 //outp[k++] = '=';
1058 //outp[k++] = '=';
1059 break;
1060 case 2:
1061 res = inp[j++]<<16;
1062 res = res | inp[j++]<<8;
1063 outp[k++] = (char) bTable[res>>18];
1064 outp[k++] = (char) bTable[(res>>12)&0x3F];
1065 outp[k++] = (char) bTable[(res>>6)&0x3F];
1066 //outp[k++] = '=';
1067 break;
1068 default:
1069 break;
1070 }
Ben Rombergera04fabc2014-11-14 12:16:03 -08001071 outp[k] = '\0';
1072 return k;
1073}
Ashish Jain81eb2a82015-05-13 10:52:34 +05301074
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301075
1076int audio_extn_utils_get_codec_version(const char *snd_card_name,
1077 int card_num,
1078 char *codec_version)
1079{
1080 char procfs_path[50];
1081 FILE *fp;
1082
1083 if (strstr(snd_card_name, "tasha")) {
1084 snprintf(procfs_path, sizeof(procfs_path),
1085 "/proc/asound/card%d/codecs/tasha/version", card_num);
1086 if ((fp = fopen(procfs_path, "r")) != NULL) {
1087 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1088 fclose(fp);
1089 } else {
1090 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1091 return -ENOENT;
1092 }
1093 ALOGD("%s: codec version %s", __func__, codec_version);
1094 }
1095
1096 return 0;
1097}
1098
1099
Ashish Jain81eb2a82015-05-13 10:52:34 +05301100#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1101
1102void get_default_compressed_channel_status(
1103 unsigned char *channel_status)
1104{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301105 memset(channel_status,0,24);
1106
1107 /* block start bit in preamble bit 3 */
1108 channel_status[0] |= PROFESSIONAL;
1109 //compre out
1110 channel_status[0] |= NON_LPCM;
1111 // sample rate; fixed 48K for default/transcode
1112 channel_status[3] |= SR_48000;
1113}
1114
1115#ifdef HDMI_PASSTHROUGH_ENABLED
1116int32_t get_compressed_channel_status(void *audio_stream_data,
1117 uint32_t audio_frame_size,
1118 unsigned char *channel_status,
1119 enum audio_parser_code_type codec_type)
1120 // codec_type - AUDIO_PARSER_CODEC_AC3
1121 // - AUDIO_PARSER_CODEC_DTS
1122{
1123 unsigned char *stream;
1124 int ret = 0;
1125 stream = (unsigned char *)audio_stream_data;
1126
1127 if (audio_stream_data == NULL || audio_frame_size == 0) {
1128 ALOGW("no buffer to get channel status, return default for compress");
1129 get_default_compressed_channel_status(channel_status);
1130 return ret;
1131 }
1132
1133 memset(channel_status,0,24);
1134 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1135 {
1136 ALOGE("init audio parser failed");
1137 return -1;
1138 }
1139 ret = get_channel_status(channel_status, codec_type);
1140 return ret;
1141
1142}
1143
1144#endif
1145
1146void get_lpcm_channel_status(uint32_t sampleRate,
1147 unsigned char *channel_status)
1148{
1149 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301150 memset(channel_status,0,24);
1151 /* block start bit in preamble bit 3 */
1152 channel_status[0] |= PROFESSIONAL;
1153 //LPCM OUT
1154 channel_status[0] &= ~NON_LPCM;
1155
1156 switch (sampleRate) {
1157 case 8000:
1158 case 11025:
1159 case 12000:
1160 case 16000:
1161 case 22050:
1162 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301163 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301164 case 24000:
1165 channel_status[3] |= SR_24000;
1166 break;
1167 case 32000:
1168 channel_status[3] |= SR_32000;
1169 break;
1170 case 44100:
1171 channel_status[3] |= SR_44100;
1172 break;
1173 case 48000:
1174 channel_status[3] |= SR_48000;
1175 break;
1176 case 88200:
1177 channel_status[3] |= SR_88200;
1178 break;
1179 case 96000:
1180 channel_status[3] |= SR_96000;
1181 break;
1182 case 176400:
1183 channel_status[3] |= SR_176400;
1184 break;
1185 case 192000:
1186 channel_status[3] |= SR_192000;
1187 break;
1188 default:
1189 ALOGV("Invalid sample_rate %u\n", sampleRate);
1190 status = -1;
1191 break;
1192 }
1193}
1194
1195void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1196{
1197 unsigned char channel_status[24]={0};
1198 struct snd_aes_iec958 iec958;
1199 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1200 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301201 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301202#ifdef HDMI_PASSTHROUGH_ENABLED
1203 if (audio_extn_is_dolby_format(out->format) &&
1204 /*TODO:Extend code to support DTS passthrough*/
1205 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301206 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301207 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1208 } else
1209#endif
1210 {
1211 /*set channel status bit for LPCM*/
1212 get_lpcm_channel_status(out->sample_rate, channel_status);
1213 }
1214
1215 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1216 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1217 if (!ctl) {
1218 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1219 __func__, mixer_ctl_name);
1220 return;
1221 }
1222 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1223 ALOGE("%s: Could not set channel status for ext HDMI ",
1224 __func__);
1225 return;
1226 }
1227
1228}
1229#endif