blob: cd9ead7d7ec9b5648f1b4950fcca590924e5bab4 [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),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700102 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
103 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
104 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
105 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Mingming Yinae3530f2014-07-03 16:50:18 -0700106 STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB),
107 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700108 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
Mingming Yinae3530f2014-07-03 16:50:18 -0700109 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +0530110#ifdef AUDIO_EXTN_FORMATS_ENABLED
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700111 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700112 STRING_TO_ENUM(AUDIO_FORMAT_WMA),
113 STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO),
114 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700115 STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS),
116 STRING_TO_ENUM(AUDIO_FORMAT_EVRC),
117 STRING_TO_ENUM(AUDIO_FORMAT_EVRCB),
118 STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB),
119 STRING_TO_ENUM(AUDIO_FORMAT_QCELP),
120 STRING_TO_ENUM(AUDIO_FORMAT_MP2),
121 STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW),
Amit Shekhar6f461b12014-08-01 14:52:58 -0700122 STRING_TO_ENUM(AUDIO_FORMAT_FLAC),
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530123 STRING_TO_ENUM(AUDIO_FORMAT_ALAC),
124 STRING_TO_ENUM(AUDIO_FORMAT_APE),
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700125 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Alexy Josephcd8eaed2014-12-11 12:46:53 -0800126 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
127 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
128 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
Manish Dewangana6fc5442015-08-24 20:30:31 +0530129 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS),
Ashish Jaine513a872015-11-19 17:00:56 +0530130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_LC),
131 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V1),
132 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADTS_HE_V2),
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700133#endif
134};
135
Ben Rombergera04fabc2014-11-14 12:16:03 -0800136static char bTable[BASE_TABLE_SIZE] = {
137 'A','B','C','D','E','F','G','H','I','J','K','L',
138 'M','N','O','P','Q','R','S','T','U','V','W','X',
139 'Y','Z','a','b','c','d','e','f','g','h','i','j',
140 'k','l','m','n','o','p','q','r','s','t','u','v',
141 'w','x','y','z','0','1','2','3','4','5','6','7',
142 '8','9','+','/'
143};
144
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700145static uint32_t string_to_enum(const struct string_to_enum *table, size_t size,
146 const char *name)
147{
148 size_t i;
149 for (i = 0; i < size; i++) {
150 if (strcmp(table[i].name, name) == 0) {
151 ALOGV("%s found %s", __func__, table[i].name);
152 return table[i].value;
153 }
154 }
155 return 0;
156}
157
158static audio_output_flags_t parse_flag_names(char *name)
159{
160 uint32_t flag = 0;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800161 char *last_r;
162 char *flag_name = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700163 while (flag_name != NULL) {
164 if (strlen(flag_name) != 0) {
165 flag |= string_to_enum(s_flag_name_to_enum_table,
166 ARRAY_SIZE(s_flag_name_to_enum_table),
167 flag_name);
168 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800169 flag_name = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700170 }
171
172 ALOGV("parse_flag_names: flag - %d", flag);
173 return (audio_output_flags_t)flag;
174}
175
176static void parse_format_names(char *name, struct streams_output_cfg *so_info)
177{
178 struct stream_format *sf_info = NULL;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800179 char *last_r;
180 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700181
182 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0)
183 return;
184
185 list_init(&so_info->format_list);
186 while (str != NULL) {
187 audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table,
188 ARRAY_SIZE(s_format_name_to_enum_table), str);
189 ALOGV("%s: format - %d", __func__, format);
190 if (format != 0) {
191 sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700192 if (sf_info == NULL)
193 break; /* return whatever was parsed */
194
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700195 sf_info->format = format;
196 list_add_tail(&so_info->format_list, &sf_info->list);
197 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800198 str = strtok_r(NULL, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700199 }
200}
201
Amit Shekhar6f461b12014-08-01 14:52:58 -0700202static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700203{
Amit Shekhar6f461b12014-08-01 14:52:58 -0700204 struct stream_sample_rate *ss_info = NULL;
205 uint32_t sample_rate = 48000;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800206 char *last_r;
207 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700208
Amit Shekhar6f461b12014-08-01 14:52:58 -0700209 if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG))
210 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700211
Amit Shekhar6f461b12014-08-01 14:52:58 -0700212 list_init(&so_info->sample_rate_list);
213 while (str != NULL) {
214 sample_rate = (uint32_t)strtol(str, (char **)NULL, 10);
215 ALOGV("%s: sample_rate - %d", __func__, sample_rate);
216 if (0 != sample_rate) {
217 ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate));
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800218 if (!ss_info) {
219 ALOGE("%s: memory allocation failure", __func__);
220 return;
221 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700222 ss_info->sample_rate = sample_rate;
223 list_add_tail(&so_info->sample_rate_list, &ss_info->list);
224 }
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800225 str = strtok_r(NULL, "|", &last_r);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700226 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700227}
228
229static int parse_bit_width_names(char *name)
230{
231 int bit_width = 16;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800232 char *last_r;
233 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700234
235 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
236 bit_width = (int)strtol(str, (char **)NULL, 10);
237
238 ALOGV("%s: bit_width - %d", __func__, bit_width);
239 return bit_width;
240}
241
242static int parse_app_type_names(void *platform, char *name)
243{
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700244 int app_type = platform_get_default_app_type(platform);
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800245 char *last_r;
246 char *str = strtok_r(name, "|", &last_r);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700247
248 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG))
249 app_type = (int)strtol(str, (char **)NULL, 10);
250
251 ALOGV("%s: app_type - %d", __func__, app_type);
252 return app_type;
253}
254
255static void update_streams_output_cfg_list(cnode *root, void *platform,
256 struct listnode *streams_output_cfg_list)
257{
258 cnode *node = root->first_child;
259 struct streams_output_cfg *so_info;
260
261 ALOGV("%s", __func__);
262 so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700263
264 if (!so_info) {
265 ALOGE("failed to allocate mem for so_info list element");
266 return;
267 }
268
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700269 while (node) {
270 if (strcmp(node->name, FLAGS_TAG) == 0) {
271 so_info->flags = parse_flag_names((char *)node->value);
272 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
273 parse_format_names((char *)node->value, so_info);
274 } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700275 so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
276 parse_sample_rate_names((char *)node->value, so_info);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700277 } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) {
278 so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value);
279 } else if (strcmp(node->name, APP_TYPE_TAG) == 0) {
280 so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value);
281 }
282 node = node->next;
283 }
284 list_add_tail(streams_output_cfg_list, &so_info->list);
285}
286
287static void load_output(cnode *root, void *platform,
288 struct listnode *streams_output_cfg_list)
289{
290 cnode *node = config_find(root, OUTPUTS_TAG);
291 if (node == NULL) {
292 ALOGE("%s: could not load output, node is NULL", __func__);
293 return;
294 }
295
296 node = node->first_child;
297 while (node) {
298 ALOGV("%s: loading output %s", __func__, node->name);
299 update_streams_output_cfg_list(node, platform, streams_output_cfg_list);
300 node = node->next;
301 }
302}
303
304static void send_app_type_cfg(void *platform, struct mixer *mixer,
305 struct listnode *streams_output_cfg_list)
306{
307 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1};
308 int length = 0, i, num_app_types = 0;
309 struct listnode *node;
310 bool update;
311 struct mixer_ctl *ctl = NULL;
312 const char *mixer_ctl_name = "App Type Config";
313 struct streams_output_cfg *so_info;
314
315 if (!mixer) {
316 ALOGE("%s: mixer is null",__func__);
317 return;
318 }
319 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
320 if (!ctl) {
321 ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
322 return;
323 }
324 if (streams_output_cfg_list == NULL) {
325 app_type_cfg[length++] = 1;
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700326 app_type_cfg[length++] = platform_get_default_app_type(platform);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700327 app_type_cfg[length++] = 48000;
328 app_type_cfg[length++] = 16;
329 mixer_ctl_set_array(ctl, app_type_cfg, length);
330 return;
331 }
332
333 app_type_cfg[length++] = num_app_types;
334 list_for_each(node, streams_output_cfg_list) {
335 so_info = node_to_item(node, struct streams_output_cfg, list);
336 update = true;
337 for (i=0; i<length; i=i+3) {
338 if (app_type_cfg[i+1] == -1)
339 break;
340 else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) {
341 update = false;
342 break;
343 }
344 }
345 if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) {
346 num_app_types += 1 ;
347 app_type_cfg[length++] = so_info->app_type_cfg.app_type;
348 app_type_cfg[length++] = so_info->app_type_cfg.sample_rate;
349 app_type_cfg[length++] = so_info->app_type_cfg.bit_width;
350 }
351 }
352 ALOGV("%s: num_app_types: %d", __func__, num_app_types);
353 if (num_app_types) {
354 app_type_cfg[0] = num_app_types;
355 mixer_ctl_set_array(ctl, app_type_cfg, length);
356 }
357}
358
359void audio_extn_utils_update_streams_output_cfg_list(void *platform,
360 struct mixer *mixer,
361 struct listnode *streams_output_cfg_list)
362{
363 cnode *root;
364 char *data;
365
366 ALOGV("%s", __func__);
367 list_init(streams_output_cfg_list);
368 data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL);
369 if (data == NULL) {
370 send_app_type_cfg(platform, mixer, NULL);
371 ALOGE("%s: could not load output policy config file", __func__);
372 return;
373 }
374
375 root = config_node("", "");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700376 if (root == NULL) {
377 ALOGE("cfg_list, NULL config root");
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800378 free(data);
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700379 return;
380 }
381
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700382 config_load(root, data);
383 load_output(root, platform, streams_output_cfg_list);
384
385 send_app_type_cfg(platform, mixer, streams_output_cfg_list);
Alexy Josephaee4fdd2016-01-29 13:02:07 -0800386
387 config_free(root);
388 free(data);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700389}
390
391void audio_extn_utils_dump_streams_output_cfg_list(
392 struct listnode *streams_output_cfg_list)
393{
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700394 struct listnode *node_i, *node_j;
395 struct streams_output_cfg *so_info;
396 struct stream_format *sf_info;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700397 struct stream_sample_rate *ss_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700398 ALOGV("%s", __func__);
399 list_for_each(node_i, streams_output_cfg_list) {
400 so_info = node_to_item(node_i, struct streams_output_cfg, list);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700401 ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d",
402 __func__, so_info->flags, so_info->app_type_cfg.sample_rate,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700403 so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type);
404 list_for_each(node_j, &so_info->format_list) {
405 sf_info = node_to_item(node_j, struct stream_format, list);
406 ALOGV("format-%x", sf_info->format);
407 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700408 list_for_each(node_j, &so_info->sample_rate_list) {
409 ss_info = node_to_item(node_j, struct stream_sample_rate, list);
410 ALOGV("sample rate-%d", ss_info->sample_rate);
411 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700412 }
413}
414
415void audio_extn_utils_release_streams_output_cfg_list(
416 struct listnode *streams_output_cfg_list)
417{
418 struct listnode *node_i, *node_j;
419 struct streams_output_cfg *so_info;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700420
421 ALOGV("%s", __func__);
422 while (!list_empty(streams_output_cfg_list)) {
423 node_i = list_head(streams_output_cfg_list);
424 so_info = node_to_item(node_i, struct streams_output_cfg, list);
425 while (!list_empty(&so_info->format_list)) {
426 node_j = list_head(&so_info->format_list);
427 list_remove(node_j);
428 free(node_to_item(node_j, struct stream_format, list));
429 }
Amit Shekhar6f461b12014-08-01 14:52:58 -0700430 while (!list_empty(&so_info->sample_rate_list)) {
431 node_j = list_head(&so_info->sample_rate_list);
432 list_remove(node_j);
433 free(node_to_item(node_j, struct stream_sample_rate, list));
434 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700435 list_remove(node_i);
436 free(node_to_item(node_i, struct streams_output_cfg, list));
437 }
438}
439
Amit Shekhar6f461b12014-08-01 14:52:58 -0700440static bool set_output_cfg(struct streams_output_cfg *so_info,
441 struct stream_app_type_cfg *app_type_cfg,
442 uint32_t sample_rate, uint32_t bit_width)
443 {
444 struct listnode *node_i;
445 struct stream_sample_rate *ss_info;
446 list_for_each(node_i, &so_info->sample_rate_list) {
447 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
448 if ((sample_rate <= ss_info->sample_rate) &&
449 (bit_width == so_info->app_type_cfg.bit_width)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -0700450
Amit Shekhar6f461b12014-08-01 14:52:58 -0700451 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
452 app_type_cfg->sample_rate = ss_info->sample_rate;
453 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
454 ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d",
455 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
456 return true;
457 }
458 }
459 /*
460 * Reiterate through the list assuming dafault sample rate.
461 * Handles scenario where input sample rate is higher
462 * than all sample rates in list for the input bit width.
463 */
464 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800465
Amit Shekhar6f461b12014-08-01 14:52:58 -0700466 list_for_each(node_i, &so_info->sample_rate_list) {
467 ss_info = node_to_item(node_i, struct stream_sample_rate, list);
468 if ((sample_rate <= ss_info->sample_rate) &&
469 (bit_width == so_info->app_type_cfg.bit_width)) {
470 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
471 app_type_cfg->sample_rate = sample_rate;
472 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -0800473 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 -0700474 __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width);
475 return true;
476 }
477 }
478 return false;
479}
480
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700481void audio_extn_utils_update_stream_app_type_cfg(void *platform,
482 struct listnode *streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700483 audio_devices_t devices,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700484 audio_output_flags_t flags,
485 audio_format_t format,
Amit Shekhar6f461b12014-08-01 14:52:58 -0700486 uint32_t sample_rate,
487 uint32_t bit_width,
Manish Dewangan837dc462015-05-27 10:17:41 +0530488 audio_channel_mask_t channel_mask,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700489 struct stream_app_type_cfg *app_type_cfg)
490{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530491 struct listnode *node_i, *node_j;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700492 struct streams_output_cfg *so_info;
493 struct stream_format *sf_info;
Manish Dewangan837dc462015-05-27 10:17:41 +0530494 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700495
Amit Shekhar1d896042014-10-03 13:16:09 -0700496 if ((24 == bit_width) &&
497 (devices & AUDIO_DEVICE_OUT_SPEAKER)) {
Amit Shekhar5a39c912014-10-14 15:39:30 -0700498 int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER);
499 if (-ENOSYS != bw)
500 bit_width = (uint32_t)bw;
Amit Shekhar1d896042014-10-03 13:16:09 -0700501 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
502 ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__);
503 }
504
Manish Dewangan837dc462015-05-27 10:17:41 +0530505 property_get("audio.playback.mch.downsample",value,"");
506 if (!strncmp("true", value, sizeof("true"))) {
507 if ((popcount(channel_mask) > 2) &&
508 (sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
509 !(flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)) {
510 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
511 ALOGD("%s: MCH session defaulting sample rate to %d",
512 __func__, sample_rate);
513 }
514 }
Amit Shekhar1d896042014-10-03 13:16:09 -0700515 ALOGV("%s: flags: %x, format: %x sample_rate %d",
516 __func__, flags, format, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700517 list_for_each(node_i, streams_output_cfg_list) {
518 so_info = node_to_item(node_i, struct streams_output_cfg, list);
519 if (so_info->flags == flags) {
520 list_for_each(node_j, &so_info->format_list) {
521 sf_info = node_to_item(node_j, struct stream_format, list);
522 if (sf_info->format == format) {
Amit Shekhar6f461b12014-08-01 14:52:58 -0700523 if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width))
524 return;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700525 }
526 }
527 }
528 }
529 list_for_each(node_i, streams_output_cfg_list) {
530 so_info = node_to_item(node_i, struct streams_output_cfg, list);
531 if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) {
532 ALOGV("Compatible output profile not found.");
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700533 app_type_cfg->app_type = so_info->app_type_cfg.app_type;
534 app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate;
535 app_type_cfg->bit_width = so_info->app_type_cfg.bit_width;
Amit Shekhar6f461b12014-08-01 14:52:58 -0700536 ALOGV("%s Default to primary output: App type: %d sample_rate %d",
537 __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700538 return;
539 }
540 }
541 ALOGW("%s: App type could not be selected. Falling back to default", __func__);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700542 app_type_cfg->app_type = platform_get_default_app_type(platform);
Amit Shekhar6f461b12014-08-01 14:52:58 -0700543 app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700544 app_type_cfg->bit_width = 16;
545}
546
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530547static bool audio_is_this_native_usecase(struct audio_usecase *uc)
548{
549 bool native_usecase = false;
550 struct stream_out *out = (struct stream_out*) uc->stream.out;
551
552 if (PCM_PLAYBACK == uc->type && out != NULL &&
553 NATIVE_AUDIO_MODE_INVALID != platform_get_native_support() &&
554 is_offload_usecase(uc->id) &&
555 (out->sample_rate == OUTPUT_SAMPLING_RATE_44100))
556 native_usecase = true;
557
558 return native_usecase;
559}
560
Ben Romberger1fafdde2015-09-09 19:43:15 -0700561int audio_extn_utils_send_app_type_cfg(struct audio_device *adev,
562 struct audio_usecase *usecase)
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700563{
564 char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT];
565 int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700566 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +0530567 int pcm_device_id = 0, acdb_dev_id, snd_device = usecase->out_snd_device;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530568 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Manish Dewangan837dc462015-05-27 10:17:41 +0530569 char value[PROPERTY_VALUE_MAX] = {0};
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700570
571 ALOGV("%s", __func__);
572
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530573 if (usecase->type != PCM_PLAYBACK && usecase->type != PCM_CAPTURE) {
574 ALOGE("%s: not a playback or capture path, no need to cfg app type", __func__);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700575 rc = 0;
576 goto exit_send_app_type_cfg;
577 }
578 if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) &&
579 (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) &&
580 (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) &&
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530581 (!is_offload_usecase(usecase->id)) &&
582 (usecase->type != PCM_CAPTURE)) {
Ben Romberger1fafdde2015-09-09 19:43:15 -0700583 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 -0700584 rc = 0;
585 goto exit_send_app_type_cfg;
586 }
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530587 if (usecase->type == PCM_PLAYBACK) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530588 snd_device = usecase->out_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700589 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_PLAYBACK);
590 } else if (usecase->type == PCM_CAPTURE) {
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530591 snd_device = usecase->in_snd_device;
Ben Romberger1fafdde2015-09-09 19:43:15 -0700592 pcm_device_id = platform_get_pcm_device_id(usecase->id, PCM_CAPTURE);
Srikanth Uyyala9d551402015-08-25 16:03:42 +0530593 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700594
595 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
596 "Audio Stream %d App Type Cfg", pcm_device_id);
597
598 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
599 if (!ctl) {
600 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__,
601 mixer_ctl_name);
602 rc = -EINVAL;
603 goto exit_send_app_type_cfg;
604 }
605 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800606 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700607 acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
608 if (acdb_dev_id < 0) {
609 ALOGE("%s: Couldn't get the acdb dev id", __func__);
610 rc = -EINVAL;
611 goto exit_send_app_type_cfg;
612 }
Ashish Jainc02430d2016-01-04 10:42:43 +0530613 if ((usecase->type == PCM_PLAYBACK) && (usecase->stream.out == NULL)) {
614 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
615 app_type_cfg[len++] = platform_get_default_app_type(adev->platform);
616 app_type_cfg[len++] = acdb_dev_id;
617 app_type_cfg[len++] = sample_rate;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530618 ALOGI("%s:%d PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
619 __func__, __LINE__,
620 platform_get_default_app_type(adev->platform),
621 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530622 } else if (usecase->type == PCM_PLAYBACK) {
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530623
Sidipotu Ashokfdd505a2016-02-11 10:31:38 +0530624 if (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Ashish Jainc02430d2016-01-04 10:42:43 +0530625 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530626 } else if ((usecase->stream.out->app_type_cfg.sample_rate == OUTPUT_SAMPLING_RATE_44100 &&
627 !(audio_is_this_native_usecase(usecase))) ||
628 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
629 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ashish Jainc02430d2016-01-04 10:42:43 +0530630 }
Venkata Narendra Kumar Gutta4bd09d02016-01-29 15:31:04 +0530631
Ashish Jainc02430d2016-01-04 10:42:43 +0530632 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
Manish Dewangan837dc462015-05-27 10:17:41 +0530633
Ashish Jainc02430d2016-01-04 10:42:43 +0530634 property_get("audio.playback.mch.downsample",value,"");
635 if (!strncmp("true", value, sizeof("true"))) {
636 if ((popcount(usecase->stream.out->channel_mask) > 2) &&
637 (usecase->stream.out->app_type_cfg.sample_rate > CODEC_BACKEND_DEFAULT_SAMPLE_RATE) &&
638 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
639 sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
640 }
641
642 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
643 app_type_cfg[len++] = acdb_dev_id;
644 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
645 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC)) &&
646 (usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH))
647 app_type_cfg[len++] = sample_rate * 4;
648 else
649 app_type_cfg[len++] = sample_rate;
650
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530651 ALOGI("%s:%d PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
652 __func__, __LINE__,
653 platform_get_default_app_type(adev->platform),
654 acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530655
Mingming Yin21854652016-04-13 11:54:02 -0700656 if ((24 == usecase->stream.out->bit_width) &&
657 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
658 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
659 } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
660 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
661 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
662 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
663 }
664 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
665
666 app_type_cfg[len++] = usecase->stream.out->app_type_cfg.app_type;
667 app_type_cfg[len++] = acdb_dev_id;
668 if (((usecase->stream.out->format == AUDIO_FORMAT_E_AC3) ||
669 (usecase->stream.out->format == AUDIO_FORMAT_E_AC3_JOC))
670 && audio_extn_dolby_is_passthrough_stream(usecase->stream.out)) {
671 app_type_cfg[len++] = sample_rate * 4;
672 } else {
673 app_type_cfg[len++] = sample_rate;
674 }
675 ALOGI("%s PLAYBACK app_type %d, acdb_dev_id %d, sample_rate %d",
676 __func__, usecase->stream.out->app_type_cfg.app_type, acdb_dev_id, sample_rate);
Ashish Jainc02430d2016-01-04 10:42:43 +0530677 } else if (usecase->type == PCM_CAPTURE) {
678 app_type_cfg[len++] = platform_get_default_app_type_v2(adev->platform, usecase->type);
679 app_type_cfg[len++] = acdb_dev_id;
680 app_type_cfg[len++] = sample_rate;
681 ALOGI("%s CAPTURE app_type %d, acdb_dev_id %d, sample_rate %d",
682 __func__, platform_get_default_app_type_v2(adev->platform, usecase->type),
683 acdb_dev_id, sample_rate);
684 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530685
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700686 mixer_ctl_set_array(ctl, app_type_cfg, len);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700687 rc = 0;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530688 ALOGI("%s:becf: adm: app_type %d, acdb_dev_id %d, sample_rate %d",
689 __func__,
690 platform_get_default_app_type_v2(adev->platform, usecase->type),
691 acdb_dev_id, sample_rate);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700692exit_send_app_type_cfg:
693 return rc;
694}
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700695
Preetam Singh Ranawat9519e9c2015-11-18 16:05:55 +0530696int read_line_from_file(const char *path, char *buf, size_t count)
697{
698 char * fgets_ret;
699 FILE * fd;
700 int rv;
701
702 fd = fopen(path, "r");
703 if (fd == NULL)
704 return -1;
705
706 fgets_ret = fgets(buf, (int)count, fd);
707 if (NULL != fgets_ret) {
708 rv = (int)strlen(buf);
709 } else {
710 rv = ferror(fd);
711 }
712 fclose(fd);
713
714 return rv;
715}
716
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700717void audio_extn_utils_send_audio_calibration(struct audio_device *adev,
718 struct audio_usecase *usecase)
719{
720 int type = usecase->type;
721
722 if (type == PCM_PLAYBACK) {
723 struct stream_out *out = usecase->stream.out;
724 int snd_device = usecase->out_snd_device;
725 snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
Xiaojun Sang040cc9f2015-08-03 19:38:28 +0800726 platform_get_spkr_prot_snd_device(snd_device) : snd_device;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530727 platform_send_audio_calibration(adev->platform, usecase,
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700728 out->app_type_cfg.app_type,
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530729 usecase->stream.out->app_type_cfg.sample_rate);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700730 }
731 if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530732 /* when app type is default. the sample rate is not used to send cal */
733 platform_send_audio_calibration(adev->platform, usecase,
Srikanth Uyyalaa1e32352015-10-09 14:48:04 +0530734 platform_get_default_app_type_v2(adev->platform, usecase->type),
735 48000);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700736 }
737}
738
Ben Rombergera04fabc2014-11-14 12:16:03 -0800739// Base64 Encode and Decode
740// Not all features supported. This must be used only with following conditions.
741// Decode Modes: Support with and without padding
742// CRLF not handling. So no CRLF in string to decode.
743// Encode Modes: Supports only padding
744int b64decode(char *inp, int ilen, uint8_t* outp)
745{
746 int i, j, k, ii, num;
747 int rem, pcnt;
748 uint32_t res=0;
749 uint8_t getIndex[MAX_BASEINDEX_LEN];
750 uint8_t tmp, cflag;
751
752 if(inp == NULL || outp == NULL || ilen <= 0) {
753 ALOGE("[%s] received NULL pointer or zero length",__func__);
754 return -1;
755 }
756
757 memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex));
758 for(i=0;i<BASE_TABLE_SIZE;i++) {
759 getIndex[(uint8_t)bTable[i]] = (uint8_t)i;
760 }
761 getIndex[(uint8_t)'=']=0;
762
763 j=0;k=0;
764 num = ilen/4;
765 rem = ilen%4;
766 if(rem==0)
767 num = num-1;
768 cflag=0;
769 for(i=0; i<num; i++) {
770 res=0;
771 for(ii=0;ii<4;ii++) {
772 res = res << 6;
773 tmp = getIndex[(uint8_t)inp[j++]];
774 res = res | tmp;
775 cflag = cflag | tmp;
776 }
777 outp[k++] = (res >> 16)&0xFF;
778 outp[k++] = (res >> 8)&0xFF;
779 outp[k++] = res & 0xFF;
780 }
781
782 // Handle last bytes special
783 pcnt=0;
784 if(rem == 0) {
785 //With padding or full data
786 res = 0;
787 for(ii=0;ii<4;ii++) {
788 if(inp[j] == '=')
789 pcnt++;
790 res = res << 6;
791 tmp = getIndex[(uint8_t)inp[j++]];
792 res = res | tmp;
793 cflag = cflag | tmp;
794 }
795 outp[k++] = res >> 16;
796 if(pcnt == 2)
797 goto done;
798 outp[k++] = (res>>8)&0xFF;
799 if(pcnt == 1)
800 goto done;
801 outp[k++] = res&0xFF;
802 } else {
803 //without padding
804 res = 0;
805 for(i=0;i<rem;i++) {
806 res = res << 6;
807 tmp = getIndex[(uint8_t)inp[j++]];
808 res = res | tmp;
809 cflag = cflag | tmp;
810 }
811 for(i=rem;i<4;i++) {
812 res = res << 6;
813 pcnt++;
814 }
815 outp[k++] = res >> 16;
816 if(pcnt == 2)
817 goto done;
818 outp[k++] = (res>>8)&0xFF;
819 if(pcnt == 1)
820 goto done;
821 outp[k++] = res&0xFF;
822 }
823done:
824 if(cflag == 0xFF) {
825 ALOGE("[%s] base64 decode failed. Invalid character found %s",
826 __func__, inp);
827 return 0;
828 }
829 return k;
830}
831
832int b64encode(uint8_t *inp, int ilen, char* outp)
833{
834 int i,j,k, num;
835 int rem=0;
836 uint32_t res=0;
837
838 if(inp == NULL || outp == NULL || ilen<=0) {
839 ALOGE("[%s] received NULL pointer or zero input length",__func__);
840 return -1;
841 }
842
843 num = ilen/3;
844 rem = ilen%3;
845 j=0;k=0;
846 for(i=0; i<num; i++) {
847 //prepare index
848 res = inp[j++]<<16;
849 res = res | inp[j++]<<8;
850 res = res | inp[j++];
851 //get output map from index
852 outp[k++] = (char) bTable[(res>>18)&0x3F];
853 outp[k++] = (char) bTable[(res>>12)&0x3F];
854 outp[k++] = (char) bTable[(res>>6)&0x3F];
855 outp[k++] = (char) bTable[res&0x3F];
856 }
857
858 switch(rem) {
859 case 1:
860 res = inp[j++]<<16;
861 outp[k++] = (char) bTable[res>>18];
862 outp[k++] = (char) bTable[(res>>12)&0x3F];
863 //outp[k++] = '=';
864 //outp[k++] = '=';
865 break;
866 case 2:
867 res = inp[j++]<<16;
868 res = res | inp[j++]<<8;
869 outp[k++] = (char) bTable[res>>18];
870 outp[k++] = (char) bTable[(res>>12)&0x3F];
871 outp[k++] = (char) bTable[(res>>6)&0x3F];
872 //outp[k++] = '=';
873 break;
874 default:
875 break;
876 }
Ben Rombergera04fabc2014-11-14 12:16:03 -0800877 outp[k] = '\0';
878 return k;
879}
Ashish Jain81eb2a82015-05-13 10:52:34 +0530880
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +0530881
882int audio_extn_utils_get_codec_version(const char *snd_card_name,
883 int card_num,
884 char *codec_version)
885{
886 char procfs_path[50];
887 FILE *fp;
888
889 if (strstr(snd_card_name, "tasha")) {
890 snprintf(procfs_path, sizeof(procfs_path),
891 "/proc/asound/card%d/codecs/tasha/version", card_num);
892 if ((fp = fopen(procfs_path, "r")) != NULL) {
893 fgets(codec_version, CODEC_VERSION_MAX_LENGTH, fp);
894 fclose(fp);
895 } else {
896 ALOGE("%s: ERROR. cannot open %s", __func__, procfs_path);
897 return -ENOENT;
898 }
899 ALOGD("%s: codec version %s", __func__, codec_version);
900 }
901
902 return 0;
903}
904
905
Ashish Jain81eb2a82015-05-13 10:52:34 +0530906#ifdef AUDIO_EXTERNAL_HDMI_ENABLED
907
908void get_default_compressed_channel_status(
909 unsigned char *channel_status)
910{
Ashish Jain81eb2a82015-05-13 10:52:34 +0530911 memset(channel_status,0,24);
912
913 /* block start bit in preamble bit 3 */
914 channel_status[0] |= PROFESSIONAL;
915 //compre out
916 channel_status[0] |= NON_LPCM;
917 // sample rate; fixed 48K for default/transcode
918 channel_status[3] |= SR_48000;
919}
920
921#ifdef HDMI_PASSTHROUGH_ENABLED
922int32_t get_compressed_channel_status(void *audio_stream_data,
923 uint32_t audio_frame_size,
924 unsigned char *channel_status,
925 enum audio_parser_code_type codec_type)
926 // codec_type - AUDIO_PARSER_CODEC_AC3
927 // - AUDIO_PARSER_CODEC_DTS
928{
929 unsigned char *stream;
930 int ret = 0;
931 stream = (unsigned char *)audio_stream_data;
932
933 if (audio_stream_data == NULL || audio_frame_size == 0) {
934 ALOGW("no buffer to get channel status, return default for compress");
935 get_default_compressed_channel_status(channel_status);
936 return ret;
937 }
938
939 memset(channel_status,0,24);
940 if(init_audio_parser(stream, audio_frame_size, codec_type) == -1)
941 {
942 ALOGE("init audio parser failed");
943 return -1;
944 }
945 ret = get_channel_status(channel_status, codec_type);
946 return ret;
947
948}
949
950#endif
951
952void get_lpcm_channel_status(uint32_t sampleRate,
953 unsigned char *channel_status)
954{
955 int32_t status = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +0530956 memset(channel_status,0,24);
957 /* block start bit in preamble bit 3 */
958 channel_status[0] |= PROFESSIONAL;
959 //LPCM OUT
960 channel_status[0] &= ~NON_LPCM;
961
962 switch (sampleRate) {
963 case 8000:
964 case 11025:
965 case 12000:
966 case 16000:
967 case 22050:
968 channel_status[3] |= SR_NOTID;
Preetam Singh Ranawat61716b12015-12-14 11:55:24 +0530969 break;
Ashish Jain81eb2a82015-05-13 10:52:34 +0530970 case 24000:
971 channel_status[3] |= SR_24000;
972 break;
973 case 32000:
974 channel_status[3] |= SR_32000;
975 break;
976 case 44100:
977 channel_status[3] |= SR_44100;
978 break;
979 case 48000:
980 channel_status[3] |= SR_48000;
981 break;
982 case 88200:
983 channel_status[3] |= SR_88200;
984 break;
985 case 96000:
986 channel_status[3] |= SR_96000;
987 break;
988 case 176400:
989 channel_status[3] |= SR_176400;
990 break;
991 case 192000:
992 channel_status[3] |= SR_192000;
993 break;
994 default:
995 ALOGV("Invalid sample_rate %u\n", sampleRate);
996 status = -1;
997 break;
998 }
999}
1000
1001void audio_utils_set_hdmi_channel_status(struct stream_out *out, char * buffer, size_t bytes)
1002{
1003 unsigned char channel_status[24]={0};
1004 struct snd_aes_iec958 iec958;
1005 const char *mixer_ctl_name = "IEC958 Playback PCM Stream";
1006 struct mixer_ctl *ctl;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05301007 ALOGV("%s: buffer %s bytes %zd", __func__, buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05301008#ifdef HDMI_PASSTHROUGH_ENABLED
1009 if (audio_extn_is_dolby_format(out->format) &&
1010 /*TODO:Extend code to support DTS passthrough*/
1011 /*set compressed channel status bits*/
Mingming Yin21854652016-04-13 11:54:02 -07001012 audio_extn_dolby_is_passthrough_stream(out)){
Ashish Jain81eb2a82015-05-13 10:52:34 +05301013 get_compressed_channel_status(buffer, bytes, channel_status, AUDIO_PARSER_CODEC_AC3);
1014 } else
1015#endif
1016 {
1017 /*set channel status bit for LPCM*/
1018 get_lpcm_channel_status(out->sample_rate, channel_status);
1019 }
1020
1021 memcpy(iec958.status, channel_status,sizeof(iec958.status));
1022 ctl = mixer_get_ctl_by_name(out->dev->mixer, mixer_ctl_name);
1023 if (!ctl) {
1024 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1025 __func__, mixer_ctl_name);
1026 return;
1027 }
1028 if (mixer_ctl_set_array(ctl, &iec958, sizeof(iec958)) < 0) {
1029 ALOGE("%s: Could not set channel status for ext HDMI ",
1030 __func__);
1031 return;
1032 }
1033
1034}
1035#endif