blob: df20120ff57cc9bc2703040a9e7c4383fbf9aafe [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
Mingming Yin497419f2015-07-01 16:57:32 -070093#ifdef HDMI_PASSTHROUGH_ENABLED
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -070094 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH),
Mingming Yin497419f2015-07-01 16:57:32 -070095#endif
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070096};
97
98const struct string_to_enum s_format_name_to_enum_table[] = {
99 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
Ashish Jain5106d362016-05-11 19:23:33 +0530100 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
101 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
Ashish Jainf1eaa582016-05-23 20:54:24 +0530102 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700103 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
104 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
105 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
106 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700107 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
108 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700109 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700110 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700111 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530112 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
113#ifdef AUDIO_EXTN_FORMATS_ENABLED
114 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700115 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
116 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
117 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700118 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
119 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
120 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
121 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
122 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
123 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
124 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700125 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530126 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
127 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700128 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800129 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
131 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530132 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530133 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
134 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
135 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700136#endif
137};
138
Ben Rombergera04fabc2014-11-14 12:16:03 -0800139static char bTable[BASE_TABLE_SIZE] = {
140 'A','B','C','D','E','F','G','H','I','J','K','L',
141 'M','N','O','P','Q','R','S','T','U','V','W','X',
142 'Y','Z','a','b','c','d','e','f','g','h','i','j',
143 'k','l','m','n','o','p','q','r','s','t','u','v',
144 'w','x','y','z','0','1','2','3','4','5','6','7',
145 '8','9','+','/'
146};
147
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700148static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
149 const char *name)
150{
151 size_t i;
152 for (i = 0; i < size; i++) {
153 if (strcmp(table[i].name, name) == 0) {
154 ALOGV("%s found %s", __func__, table[i].name);
155 return table[i].value;
156 }
157 }
158 return 0;
159}
160
161static audio_output_flags_t parse_flag_names(char *name)
162{
163 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800164 char *last_r;
165 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700166 while (flag_name != NULL) {
167 if (strlen(flag_name) != 0) {
168 flag |= string_to_enum(s_flag_name_to_enum_table,
169 ARRAY_SIZE(s_flag_name_to_enum_table),
170 flag_name);
171 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800172 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700173 }
174
175 ALOGV("parse_flag_names: flag - %d", flag);
176 return (audio_output_flags_t)flag;
177}
178
179static void parse_format_names(char *name, struct streams_output_cfg *so_info)
180{
181 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800182 char *last_r;
183 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700184
185 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
186 return;
187
188 list_init(&so_info->format_list);
189 while (str != NULL) {
190 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
191 ARRAY_SIZE(s_format_name_to_enum_table), str);
192 ALOGV("%s: format - %d", __func__, format);
193 if (format != 0) {
194 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700195 if (sf_info == NULL)
196 break; /* return whatever was parsed */
197
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700198 sf_info->format = format;
199 list_add_tail(&so_info->format_list, &sf_info->list);
200 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800201 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700202 }
203}
204
Amit Shekhar6f461b12014-08-01 14:52:58 -0700205static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700206{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700207 struct stream_sample_rate *ss_info = NULL;
208 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800209 char *last_r;
210 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211
Amit Shekhar6f461b12014-08-01 14:52:58 -0700212 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
213 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700214
Amit Shekhar6f461b12014-08-01 14:52:58 -0700215 list_init(&so_info->sample_rate_list);
216 while (str != NULL) {
217 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
218 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
219 if (0 != sample_rate) {
220 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800221 if (!ss_info) {
222 ALOGE("%s: memory allocation failure", __func__);
223 return;
224 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700225 ss_info->sample_rate = sample_rate;
226 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
227 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800228 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700229 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700230}
231
232static int parse_bit_width_names(char *name)
233{
234 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800235 char *last_r;
236 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700237
238 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
239 bit_width = (int)strtol(str, (char **)NULL, 10);
240
241 ALOGV("%s: bit_width - %d", __func__, bit_width);
242 return bit_width;
243}
244
245static int parse_app_type_names(void *platform, char *name)
246{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700247 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800248 char *last_r;
249 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700250
251 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
252 app_type = (int)strtol(str, (char **)NULL, 10);
253
254 ALOGV("%s: app_type - %d", __func__, app_type);
255 return app_type;
256}
257
258static void update_streams_output_cfg_list(cnode *root, void *platform,
259 struct listnode *streams_output_cfg_list)
260{
261 cnode *node = root->first_child;
262 struct streams_output_cfg *so_info;
263
264 ALOGV("%s", __func__);
265 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700266
267 if (!so_info) {
268 ALOGE("failed to allocate mem for so_info list element");
269 return;
270 }
271
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700272 while (node) {
273 if (strcmp(node->name, FLAGS_TAG) == 0) {
274 so_info->flags = parse_flag_names((char *)node->value);
275 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
276 parse_format_names((char *)node->value, so_info);
277 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700278 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
279 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700280 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
281 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
282 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
283 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
284 }
285 node = node->next;
286 }
287 list_add_tail(streams_output_cfg_list, &so_info->list);
288}
289
290static void load_output(cnode *root, void *platform,
291 struct listnode *streams_output_cfg_list)
292{
293 cnode *node = config_find(root, OUTPUTS_TAG);
294 if (node == NULL) {
295 ALOGE("%s: could not load output, node is NULL", __func__);
296 return;
297 }
298
299 node = node->first_child;
300 while (node) {
301 ALOGV("%s: loading output %s", __func__, node->name);
302 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
303 node = node->next;
304 }
305}
306
307static void send_app_type_cfg(void *platform, struct mixer *mixer,
308 struct listnode *streams_output_cfg_list)
309{
310 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
311 int length = 0, i, num_app_types = 0;
312 struct listnode *node;
313 bool update;
314 struct mixer_ctl *ctl = NULL;
315 const char *mixer_ctl_name = "App Type Config";
316 struct streams_output_cfg *so_info;
317
318 if (!mixer) {
319 ALOGE("%s: mixer is null",__func__);
320 return;
321 }
322 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
323 if (!ctl) {
324 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
325 return;
326 }
327 if (streams_output_cfg_list == NULL) {
328 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700329 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 app_type_cfg[length++] = 48000;
331 app_type_cfg[length++] = 16;
332 mixer_ctl_set_array(ctl, app_type_cfg, length);
333 return;
334 }
335
336 app_type_cfg[length++] = num_app_types;
337 list_for_each(node, streams_output_cfg_list) {
338 so_info = node_to_item(node, struct streams_output_cfg, list);
339 update = true;
340 for (i=0; i<length; i=i+3) {
341 if (app_type_cfg[i+1] == -1)
342 break;
343 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
344 update = false;
345 break;
346 }
347 }
348 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
349 num_app_types += 1 ;
350 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
351 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
352 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
353 }
354 }
355 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
356 if (num_app_types) {
357 app_type_cfg[0] = num_app_types;
358 mixer_ctl_set_array(ctl, app_type_cfg, length);
359 }
360}
361
362void audio_extn_utils_update_streams_output_cfg_list(void *platform,
363 struct mixer *mixer,
364 struct listnode *streams_output_cfg_list)
365{
366 cnode *root;
367 char *data;
368
369 ALOGV("%s", __func__);
370 list_init(streams_output_cfg_list);
371 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
372 if (data == NULL) {
373 send_app_type_cfg(platform, mixer, NULL);
374 ALOGE("%s: could not load output policy config file", __func__);
375 return;
376 }
377
378 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700379 if (root == NULL) {
380 ALOGE("cfg_list, NULL config root");
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800381 free(data);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700382 return;
383 }
384
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700385 config_load(root, data);
386 load_output(root, platform, streams_output_cfg_list);
387
388 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800389
390 config_free(root);
391 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700392}
393
394void audio_extn_utils_dump_streams_output_cfg_list(
395 struct listnode *streams_output_cfg_list)
396{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700397 struct listnode *node_i, *node_j;
398 struct streams_output_cfg *so_info;
399 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700400 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700401 ALOGV("%s", __func__);
402 list_for_each(node_i, streams_output_cfg_list) {
403 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700404 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
405 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700406 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
407 list_for_each(node_j, &so_info->format_list) {
408 sf_info = node_to_item(node_j, struct stream_format, list);
409 ALOGV("format-%x", sf_info->format);
410 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700411 list_for_each(node_j, &so_info->sample_rate_list) {
412 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
413 ALOGV("sample rate-%d", ss_info->sample_rate);
414 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700415 }
416}
417
418void audio_extn_utils_release_streams_output_cfg_list(
419 struct listnode *streams_output_cfg_list)
420{
421 struct listnode *node_i, *node_j;
422 struct streams_output_cfg *so_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700423
424 ALOGV("%s", __func__);
425 while (!list_empty(streams_output_cfg_list)) {
426 node_i = list_head(streams_output_cfg_list);
427 so_info = node_to_item(node_i, struct streams_output_cfg, list);
428 while (!list_empty(&so_info->format_list)) {
429 node_j = list_head(&so_info->format_list);
430 list_remove(node_j);
431 free(node_to_item(node_j, struct stream_format, list));
432 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700433 while (!list_empty(&so_info->sample_rate_list)) {
434 node_j = list_head(&so_info->sample_rate_list);
435 list_remove(node_j);
436 free(node_to_item(node_j, struct stream_sample_rate, list));
437 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700438 list_remove(node_i);
439 free(node_to_item(node_i, struct streams_output_cfg, list));
440 }
441}
442
Amit Shekhar6f461b12014-08-01 14:52:58 -0700443static bool set_output_cfg(struct streams_output_cfg *so_info,
444 struct stream_app_type_cfg *app_type_cfg,
445 uint32_t sample_rate, uint32_t bit_width)
446 {
447 struct listnode *node_i;
448 struct stream_sample_rate *ss_info;
449 list_for_each(node_i, &so_info->sample_rate_list) {
450 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
451 if ((sample_rate <= ss_info->sample_rate) &&
452 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700453
Amit Shekhar6f461b12014-08-01 14:52:58 -0700454 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
455 app_type_cfg->sample_rate = ss_info->sample_rate;
456 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
457 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
458 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
459 return true;
460 }
461 }
462 /*
463 * Reiterate through the list assuming dafault sample rate.
464 * Handles scenario where input sample rate is higher
465 * than all sample rates in list for the input bit width.
466 */
467 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800468
Amit Shekhar6f461b12014-08-01 14:52:58 -0700469 list_for_each(node_i, &so_info->sample_rate_list) {
470 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
471 if ((sample_rate <= ss_info->sample_rate) &&
472 (bit_width == so_info->app_type_cfg.bit_width)) {
473 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
474 app_type_cfg->sample_rate = sample_rate;
475 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800476 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 -0700477 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
478 return true;
479 }
480 }
481 return false;
482}
483
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484void audio_extn_utils_update_stream_app_type_cfg(void *platform,
485 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700486 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700487 audio_output_flags_t flags,
488 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700489 uint32_t sample_rate,
490 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530491 audio_channel_mask_t channel_mask,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700492 struct stream_app_type_cfg *app_type_cfg)
493{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530494 struct listnode *node_i, *node_j;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495 struct streams_output_cfg *so_info;
496 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530497 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700498
Amit Shekhar1d896042014-10-03 13:16:09 -0700499 if ((24 == bit_width) &&
500 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700501 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
502 if (-ENOSYS != bw)
503 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700504 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
505 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
506 }
507
Manish Dewangan837dc462015-05-27 10:17:41 +0530508 property_get("audio.playback.mch.downsample",value,"");
509 if (!strncmp("true", value, sizeof("true"))) {
510 if ((popcount(channel_mask) > 2) &&
511 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
512 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
513 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
514 ALOGD("%s: MCH session defaulting sample rate to %d",
515 __func__, sample_rate);
516 }
517 }
Amit Shekhar1d896042014-10-03 13:16:09 -0700518 ALOGV("%s: flags: %x, format: %x sample_rate %d",
519 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700520 list_for_each(node_i, streams_output_cfg_list) {
521 so_info = node_to_item(node_i, struct streams_output_cfg, list);
522 if (so_info->flags == flags) {
523 list_for_each(node_j, &so_info->format_list) {
524 sf_info = node_to_item(node_j, struct stream_format, list);
525 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700526 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
527 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700528 }
529 }
530 }
531 }
532 list_for_each(node_i, streams_output_cfg_list) {
533 so_info = node_to_item(node_i, struct streams_output_cfg, list);
534 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
535 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700536 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
537 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
538 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700539 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
540 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700541 return;
542 }
543 }
544 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700545 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700546 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700547 app_type_cfg->bit_width = 16;
548}
549
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530550static bool audio_is_this_native_usecase(struct audio_usecase *uc)
551{
552 bool native_usecase = false;
553 struct stream_out *out = (struct stream_out*) uc->stream.out;
554
555 if (PCM_PLAYBACK == uc->type && out != NULL &&
556 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
557 is_offload_usecase(uc->id) &&
558 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
559 native_usecase = true;
560
561 return native_usecase;
562}
563
Ben Romberger1fafdde2015-09-09 19:43:15 -0700564int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
565 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700566{
567 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
568 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700569 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530570 int pcm_device_id = 0, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530571 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530572 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700573
574 ALOGV("%s", __func__);
575
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530576 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
577 ALOGE("%s: not a playback or capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700578 rc = 0;
579 goto exit_send_app_type_cfg;
580 }
581 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
582 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
583 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530584 (!is_offload_usecase(usecase->id)) &&
585 (usecase->type != PCM_CAPTURE)) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700586 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 -0700587 rc = 0;
588 goto exit_send_app_type_cfg;
589 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530590 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530591 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700592 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
593 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530594 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700595 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530596 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700597
598 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
599 "Audio Stream %d App Type Cfg", pcm_device_id);
600
601 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
602 if (!ctl) {
603 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
604 mixer_ctl_name);
605 rc = -EINVAL;
606 goto exit_send_app_type_cfg;
607 }
608 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800609 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700610 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
611 if (acdb_dev_id < 0) {
612 ALOGE("%s: Couldn't get the acdb dev id", __func__);
613 rc = -EINVAL;
614 goto exit_send_app_type_cfg;
615 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530616 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out == NULL)) {
617 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
618 app_type_cfg[len++] = platform_get_default_app_type(adev->platform);
619 app_type_cfg[len++] = acdb_dev_id;
620 app_type_cfg[len++] = sample_rate;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530621 ALOGI("%s:%d PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
622 __func__, __LINE__,
623 platform_get_default_app_type(adev->platform),
624 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530625 } else if (usecase->type == PCM_PLAYBACK) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530626
Sidipotu Ashokfdd505a2016-02-11 10:31:38 +0530627 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Ashish Jainc02430d2016-01-04 10:42:43 +0530628 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530629 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
630 !(audio_is_this_native_usecase(usecase))) ||
631 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
632 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jainc02430d2016-01-04 10:42:43 +0530633 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530634
Ashish Jainc02430d2016-01-04 10:42:43 +0530635 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530636
Ashish Jainc02430d2016-01-04 10:42:43 +0530637 property_get("audio.playback.mch.downsample",value,"");
638 if (!strncmp("true", value, sizeof("true"))) {
639 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
640 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
641 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
642 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
643 }
644
Mingming Yin21854652016-04-13 11:54:02 -0700645 if ((24 == usecase->stream.out->bit_width) &&
646 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
647 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
648 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
649 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
650 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
651 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
652 }
653 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
654
655 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
656 app_type_cfg[len++] = acdb_dev_id;
657 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
658 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530659 && audio_extn_passthru_is_passthrough_stream(usecase->stream.out)) {
Mingming Yin21854652016-04-13 11:54:02 -0700660 app_type_cfg[len++] = sample_rate * 4;
661 } else {
662 app_type_cfg[len++] = sample_rate;
663 }
664 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
665 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530666 } else if (usecase->type == PCM_CAPTURE) {
667 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
668 app_type_cfg[len++] = acdb_dev_id;
669 app_type_cfg[len++] = sample_rate;
670 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
671 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
672 acdb_dev_id, sample_rate);
673 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530674
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700675 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700676 rc = 0;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530677 ALOGI("%s:becf: adm: app_type %d, acdb_dev_id %d, sample_rate %d",
678 __func__,
679 platform_get_default_app_type_v2(adev->platform, usecase->type),
680 acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700681exit_send_app_type_cfg:
682 return rc;
683}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700684
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530685int read_line_from_file(const char *path, char *buf, size_t count)
686{
687 char * fgets_ret;
688 FILE * fd;
689 int rv;
690
691 fd = fopen(path, "r");
692 if (fd == NULL)
693 return -1;
694
695 fgets_ret = fgets(buf, (int)count, fd);
696 if (NULL != fgets_ret) {
697 rv = (int)strlen(buf);
698 } else {
699 rv = ferror(fd);
700 }
701 fclose(fd);
702
703 return rv;
704}
705
Ashish Jainf1eaa582016-05-23 20:54:24 +0530706/*Translates ALSA formats to AOSP PCM formats*/
707audio_format_t alsa_format_to_hal(uint32_t alsa_format)
708{
709 audio_format_t format;
710
711 switch(alsa_format) {
712 case SNDRV_PCM_FORMAT_S16_LE:
713 format = AUDIO_FORMAT_PCM_16_BIT;
714 break;
715 case SNDRV_PCM_FORMAT_S24_3LE:
716 format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
717 break;
718 case SNDRV_PCM_FORMAT_S24_LE:
719 format = AUDIO_FORMAT_PCM_8_24_BIT;
720 break;
721 case SNDRV_PCM_FORMAT_S32_LE:
722 format = AUDIO_FORMAT_PCM_32_BIT;
723 break;
724 default:
725 ALOGW("Incorrect ALSA format");
726 format = AUDIO_FORMAT_INVALID;
727 }
728 return format;
729}
730
731/*Translates hal format (AOSP) to alsa formats*/
732uint32_t hal_format_to_alsa(audio_format_t hal_format)
733{
734 uint32_t alsa_format;
735
736 switch (hal_format) {
737 case AUDIO_FORMAT_PCM_32_BIT: {
738 if (platform_supports_true_32bit())
739 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
740 else
741 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
742 }
743 break;
744 case AUDIO_FORMAT_PCM_8_BIT:
745 alsa_format = SNDRV_PCM_FORMAT_S8;
746 break;
747 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
748 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
749 break;
750 case AUDIO_FORMAT_PCM_8_24_BIT: {
751 if (platform_supports_true_32bit())
752 alsa_format = SNDRV_PCM_FORMAT_S32_LE;
753 else
754 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
755 }
756 break;
757 case AUDIO_FORMAT_PCM_FLOAT:
758 alsa_format = SNDRV_PCM_FORMAT_S24_3LE;
759 break;
760 default:
761 case AUDIO_FORMAT_PCM_16_BIT:
762 alsa_format = SNDRV_PCM_FORMAT_S16_LE;
763 break;
764 }
765 return alsa_format;
766}
767
768uint32_t get_alsa_fragment_size(uint32_t bytes_per_sample,
769 uint32_t sample_rate,
770 uint32_t noOfChannels)
771{
772 uint32_t fragment_size = 0;
773 uint32_t pcm_offload_time = PCM_OFFLOAD_BUFFER_DURATION;
774
775 fragment_size = (pcm_offload_time
776 * sample_rate
777 * bytes_per_sample
778 * noOfChannels)/1000;
779 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
780 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
781 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
782 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
783 /*To have same PCM samples for all channels, the buffer size requires to
784 *be multiple of (number of channels * bytes per sample)
785 *For writes to succeed, the buffer must be written at address which is multiple of 32
786 */
787 fragment_size = ALIGN(fragment_size, (bytes_per_sample * noOfChannels * 32));
788
789 ALOGI("PCM offload Fragment size to %d bytes", fragment_size);
790 return fragment_size;
791}
792
793/* Calculates the fragment size required to configure compress session.
794 * Based on the alsa format selected, decide if conversion is needed in
795
796 * HAL ( e.g. convert AUDIO_FORMAT_PCM_FLOAT input format to
797 * AUDIO_FORMAT_PCM_24_BIT_PACKED before writing to the compress driver.
798 */
799void audio_extn_utils_update_direct_pcm_fragment_size(struct stream_out *out)
800{
801 audio_format_t dst_format = out->compr_pcm_config.hal_op_format;
802 audio_format_t src_format = out->compr_pcm_config.hal_ip_format;
803 uint32_t hal_op_bytes_per_sample = audio_bytes_per_sample(dst_format);
804 uint32_t hal_ip_bytes_per_sample = audio_bytes_per_sample(src_format);
805
806 out->compr_config.fragment_size =
807 get_alsa_fragment_size(hal_op_bytes_per_sample,
808 out->sample_rate,
809 popcount(out->channel_mask));
810
811 if ((src_format != dst_format) &&
812 hal_op_bytes_per_sample != hal_ip_bytes_per_sample) {
813
814 out->compr_pcm_config.hal_fragment_size =
815 ((out->compr_config.fragment_size * hal_ip_bytes_per_sample) /
816 hal_op_bytes_per_sample);
817 ALOGI("enable conversion hal_input_fragment_size is %d src_format %x dst_format %x",
818 out->compr_pcm_config.hal_fragment_size, src_format, dst_format);
819 } else {
820 out->compr_pcm_config.hal_fragment_size = out->compr_config.fragment_size;
821 }
822}
823
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700824void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
825 struct audio_usecase *usecase)
826{
827 int type = usecase->type;
828
829 if (type == PCM_PLAYBACK) {
830 struct stream_out *out = usecase->stream.out;
831 int snd_device = usecase->out_snd_device;
832 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800833 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530834 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700835 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530836 usecase->stream.out->app_type_cfg.sample_rate);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700837 }
838 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530839 /* when app type is default. the sample rate is not used to send cal */
840 platform_send_audio_calibration(adev->platform, usecase,
Srikanth Uyyalaa1e32352015-10-09 14:48:04 +0530841 platform_get_default_app_type_v2(adev->platform, usecase->type),
842 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700843 }
844}
845
Ben Rombergera04fabc2014-11-14 12:16:03 -0800846// Base64 Encode and Decode
847// Not all features supported. This must be used only with following conditions.
848// Decode Modes: Support with and without padding
849// CRLF not handling. So no CRLF in string to decode.
850// Encode Modes: Supports only padding
851int b64decode(char *inp, int ilen, uint8_t* outp)
852{
853 int i, j, k, ii, num;
854 int rem, pcnt;
855 uint32_t res=0;
856 uint8_t getIndex[MAX_BASEINDEX_LEN];
857 uint8_t tmp, cflag;
858
859 if(inp == NULL || outp == NULL || ilen <= 0) {
860 ALOGE("[%s] received NULL pointer or zero length",__func__);
861 return -1;
862 }
863
864 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
865 for(i=0;i<BASE_TABLE_SIZE;i++) {
866 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
867 }
868 getIndex[(uint8_t)'=']=0;
869
870 j=0;k=0;
871 num = ilen/4;
872 rem = ilen%4;
873 if(rem==0)
874 num = num-1;
875 cflag=0;
876 for(i=0; i<num; i++) {
877 res=0;
878 for(ii=0;ii<4;ii++) {
879 res = res << 6;
880 tmp = getIndex[(uint8_t)inp[j++]];
881 res = res | tmp;
882 cflag = cflag | tmp;
883 }
884 outp[k++] = (res >> 16)&0xFF;
885 outp[k++] = (res >> 8)&0xFF;
886 outp[k++] = res & 0xFF;
887 }
888
889 // Handle last bytes special
890 pcnt=0;
891 if(rem == 0) {
892 //With padding or full data
893 res = 0;
894 for(ii=0;ii<4;ii++) {
895 if(inp[j] == '=')
896 pcnt++;
897 res = res << 6;
898 tmp = getIndex[(uint8_t)inp[j++]];
899 res = res | tmp;
900 cflag = cflag | tmp;
901 }
902 outp[k++] = res >> 16;
903 if(pcnt == 2)
904 goto done;
905 outp[k++] = (res>>8)&0xFF;
906 if(pcnt == 1)
907 goto done;
908 outp[k++] = res&0xFF;
909 } else {
910 //without padding
911 res = 0;
912 for(i=0;i<rem;i++) {
913 res = res << 6;
914 tmp = getIndex[(uint8_t)inp[j++]];
915 res = res | tmp;
916 cflag = cflag | tmp;
917 }
918 for(i=rem;i<4;i++) {
919 res = res << 6;
920 pcnt++;
921 }
922 outp[k++] = res >> 16;
923 if(pcnt == 2)
924 goto done;
925 outp[k++] = (res>>8)&0xFF;
926 if(pcnt == 1)
927 goto done;
928 outp[k++] = res&0xFF;
929 }
930done:
931 if(cflag == 0xFF) {
932 ALOGE("[%s] base64 decode failed. Invalid character found %s",
933 __func__, inp);
934 return 0;
935 }
936 return k;
937}
938
939int b64encode(uint8_t *inp, int ilen, char* outp)
940{
941 int i,j,k, num;
942 int rem=0;
943 uint32_t res=0;
944
945 if(inp == NULL || outp == NULL || ilen<=0) {
946 ALOGE("[%s] received NULL pointer or zero input length",__func__);
947 return -1;
948 }
949
950 num = ilen/3;
951 rem = ilen%3;
952 j=0;k=0;
953 for(i=0; i<num; i++) {
954 //prepare index
955 res = inp[j++]<<16;
956 res = res | inp[j++]<<8;
957 res = res | inp[j++];
958 //get output map from index
959 outp[k++] = (char) bTable[(res>>18)&0x3F];
960 outp[k++] = (char) bTable[(res>>12)&0x3F];
961 outp[k++] = (char) bTable[(res>>6)&0x3F];
962 outp[k++] = (char) bTable[res&0x3F];
963 }
964
965 switch(rem) {
966 case 1:
967 res = inp[j++]<<16;
968 outp[k++] = (char) bTable[res>>18];
969 outp[k++] = (char) bTable[(res>>12)&0x3F];
970 //outp[k++] = '=';
971 //outp[k++] = '=';
972 break;
973 case 2:
974 res = inp[j++]<<16;
975 res = res | inp[j++]<<8;
976 outp[k++] = (char) bTable[res>>18];
977 outp[k++] = (char) bTable[(res>>12)&0x3F];
978 outp[k++] = (char) bTable[(res>>6)&0x3F];
979 //outp[k++] = '=';
980 break;
981 default:
982 break;
983 }
Ben Rombergera04fabc2014-11-14 12:16:03 -0800984 outp[k] = '\0';
985 return k;
986}
Ashish Jain81eb2a82015-05-13 10:52:34 +0530987
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530988
989int audio_extn_utils_get_codec_version(const char *snd_card_name,
990 int card_num,
991 char *codec_version)
992{
993 char procfs_path[50];
994 FILE *fp;
995
996 if (strstr(snd_card_name, "tasha")) {
997 snprintf(procfs_path, sizeof(procfs_path),
998 "/proc/asound/card%d/codecs/tasha/version", card_num);
999 if ((fp = fopen(procfs_path, "r")) != NULL) {
1000 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
1001 fclose(fp);
1002 } else {
1003 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
1004 return -ENOENT;
1005 }
1006 ALOGD("%s: codec version %s", __func__, codec_version);
1007 }
1008
1009 return 0;
1010}
1011
1012
Ashish Jain81eb2a82015-05-13 10:52:34 +05301013#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
1014
1015void get_default_compressed_channel_status(
1016 unsigned char *channel_status)
1017{
Ashish Jain81eb2a82015-05-13 10:52:34 +05301018 memset(channel_status,0,24);
1019
1020 /* block start bit in preamble bit 3 */
1021 channel_status[0] |= PROFESSIONAL;
1022 //compre out
1023 channel_status[0] |= NON_LPCM;
1024 // sample rate; fixed 48K for default/transcode
1025 channel_status[3] |= SR_48000;
1026}
1027
1028#ifdef HDMI_PASSTHROUGH_ENABLED
1029int32_t get_compressed_channel_status(void *audio_stream_data,
1030 uint32_t audio_frame_size,
1031 unsigned char *channel_status,
1032 enum audio_parser_code_type codec_type)
1033 // codec_type - AUDIO_PARSER_CODEC_AC3
1034 // - AUDIO_PARSER_CODEC_DTS
1035{
1036 unsigned char *stream;
1037 int ret = 0;
1038 stream = (unsigned char *)audio_stream_data;
1039
1040 if (audio_stream_data == NULL || audio_frame_size == 0) {
1041 ALOGW("no buffer to get channel status, return default for compress");
1042 get_default_compressed_channel_status(channel_status);
1043 return ret;
1044 }
1045
1046 memset(channel_status,0,24);
1047 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
1048 {
1049 ALOGE("init audio parser failed");
1050 return -1;
1051 }
1052 ret = get_channel_status(channel_status, codec_type);
1053 return ret;
1054
1055}
1056
1057#endif
1058
1059void get_lpcm_channel_status(uint32_t sampleRate,
1060 unsigned char *channel_status)
1061{
1062 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301063 memset(channel_status,0,24);
1064 /* block start bit in preamble bit 3 */
1065 channel_status[0] |= PROFESSIONAL;
1066 //LPCM OUT
1067 channel_status[0] &= ~NON_LPCM;
1068
1069 switch (sampleRate) {
1070 case 8000:
1071 case 11025:
1072 case 12000:
1073 case 16000:
1074 case 22050:
1075 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +05301076 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +05301077 case 24000:
1078 channel_status[3] |= SR_24000;
1079 break;
1080 case 32000:
1081 channel_status[3] |= SR_32000;
1082 break;
1083 case 44100:
1084 channel_status[3] |= SR_44100;
1085 break;
1086 case 48000:
1087 channel_status[3] |= SR_48000;
1088 break;
1089 case 88200:
1090 channel_status[3] |= SR_88200;
1091 break;
1092 case 96000:
1093 channel_status[3] |= SR_96000;
1094 break;
1095 case 176400:
1096 channel_status[3] |= SR_176400;
1097 break;
1098 case 192000:
1099 channel_status[3] |= SR_192000;
1100 break;
1101 default:
1102 ALOGV("Invalid sample_rate %u\n", sampleRate);
1103 status = -1;
1104 break;
1105 }
1106}
1107
1108void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1109{
1110 unsigned char channel_status[24]={0};
1111 struct snd_aes_iec958 iec958;
1112 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1113 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301114 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301115#ifdef HDMI_PASSTHROUGH_ENABLED
1116 if (audio_extn_is_dolby_format(out->format) &&
1117 /*TODO:Extend code to support DTS passthrough*/
1118 /*set compressed channel status bits*/
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301119 audio_extn_passthru_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301120 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1121 } else
1122#endif
1123 {
1124 /*set channel status bit for LPCM*/
1125 get_lpcm_channel_status(out->sample_rate, channel_status);
1126 }
1127
1128 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1129 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1130 if (!ctl) {
1131 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1132 __func__, mixer_ctl_name);
1133 return;
1134 }
1135 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1136 ALOGE("%s: Could not set channel status for ext HDMI ",
1137 __func__);
1138 return;
1139 }
1140
1141}
1142#endif