Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 1 | /* |
Karthik Reddy Katta | d633c54 | 2015-07-15 18:18:03 +0530 | [diff] [blame^] | 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 3 | * 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 Chella | 212e254 | 2014-11-17 19:57:04 -0800 | [diff] [blame] | 36 | #include "voice.h" |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 37 | |
| 38 | #define AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE "/vendor/etc/audio_output_policy.conf" |
| 39 | |
| 40 | #define OUTPUTS_TAG "outputs" |
| 41 | |
| 42 | #define DYNAMIC_VALUE_TAG "dynamic" |
| 43 | #define FLAGS_TAG "flags" |
| 44 | #define FORMATS_TAG "formats" |
| 45 | #define SAMPLING_RATES_TAG "sampling_rates" |
| 46 | #define BIT_WIDTH_TAG "bit_width" |
| 47 | #define APP_TYPE_TAG "app_type" |
| 48 | |
| 49 | #define STRING_TO_ENUM(string) { #string, string } |
| 50 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 51 | |
Ben Romberger | a04fabc | 2014-11-14 12:16:03 -0800 | [diff] [blame] | 52 | #define BASE_TABLE_SIZE 64 |
| 53 | #define MAX_BASEINDEX_LEN 256 |
| 54 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 55 | struct string_to_enum { |
| 56 | const char *name; |
| 57 | uint32_t value; |
| 58 | }; |
| 59 | |
| 60 | const struct string_to_enum s_flag_name_to_enum_table[] = { |
| 61 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), |
| 62 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), |
| 63 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), |
| 64 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), |
| 65 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), |
| 66 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 67 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 68 | #ifdef INCALL_MUSIC_ENABLED |
| 69 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_INCALL_MUSIC), |
| 70 | #endif |
Mingming Yin | 497419f | 2015-07-01 16:57:32 -0700 | [diff] [blame] | 71 | #ifdef HDMI_PASSTHROUGH_ENABLED |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 72 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH), |
Mingming Yin | 497419f | 2015-07-01 16:57:32 -0700 | [diff] [blame] | 73 | #endif |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | const struct string_to_enum s_format_name_to_enum_table[] = { |
| 77 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), |
| 78 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), |
| 79 | STRING_TO_ENUM(AUDIO_FORMAT_MP3), |
| 80 | STRING_TO_ENUM(AUDIO_FORMAT_AAC), |
| 81 | STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), |
Mingming Yin | ae3530f | 2014-07-03 16:50:18 -0700 | [diff] [blame] | 82 | STRING_TO_ENUM(AUDIO_FORMAT_AMR_NB), |
| 83 | STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB), |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 84 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), |
Mingming Yin | ae3530f | 2014-07-03 16:50:18 -0700 | [diff] [blame] | 85 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), |
| 86 | #ifdef FORMATS_ENABLED |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 87 | STRING_TO_ENUM(AUDIO_FORMAT_DTS), |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 88 | STRING_TO_ENUM(AUDIO_FORMAT_WMA), |
| 89 | STRING_TO_ENUM(AUDIO_FORMAT_WMA_PRO), |
| 90 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ADIF), |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 91 | STRING_TO_ENUM(AUDIO_FORMAT_AMR_WB_PLUS), |
| 92 | STRING_TO_ENUM(AUDIO_FORMAT_EVRC), |
| 93 | STRING_TO_ENUM(AUDIO_FORMAT_EVRCB), |
| 94 | STRING_TO_ENUM(AUDIO_FORMAT_EVRCWB), |
| 95 | STRING_TO_ENUM(AUDIO_FORMAT_QCELP), |
| 96 | STRING_TO_ENUM(AUDIO_FORMAT_MP2), |
| 97 | STRING_TO_ENUM(AUDIO_FORMAT_EVRCNW), |
| 98 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT_OFFLOAD), |
| 99 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_OFFLOAD), |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 100 | STRING_TO_ENUM(AUDIO_FORMAT_FLAC), |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 101 | STRING_TO_ENUM(AUDIO_FORMAT_ALAC), |
| 102 | STRING_TO_ENUM(AUDIO_FORMAT_APE), |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 103 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC), |
Alexy Joseph | cd8eaed | 2014-12-11 12:46:53 -0800 | [diff] [blame] | 104 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC), |
| 105 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1), |
| 106 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2), |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 107 | #endif |
| 108 | }; |
| 109 | |
Ben Romberger | a04fabc | 2014-11-14 12:16:03 -0800 | [diff] [blame] | 110 | static char bTable[BASE_TABLE_SIZE] = { |
| 111 | 'A','B','C','D','E','F','G','H','I','J','K','L', |
| 112 | 'M','N','O','P','Q','R','S','T','U','V','W','X', |
| 113 | 'Y','Z','a','b','c','d','e','f','g','h','i','j', |
| 114 | 'k','l','m','n','o','p','q','r','s','t','u','v', |
| 115 | 'w','x','y','z','0','1','2','3','4','5','6','7', |
| 116 | '8','9','+','/' |
| 117 | }; |
| 118 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 119 | static uint32_t string_to_enum(const struct string_to_enum *table, size_t size, |
| 120 | const char *name) |
| 121 | { |
| 122 | size_t i; |
| 123 | for (i = 0; i < size; i++) { |
| 124 | if (strcmp(table[i].name, name) == 0) { |
| 125 | ALOGV("%s found %s", __func__, table[i].name); |
| 126 | return table[i].value; |
| 127 | } |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static audio_output_flags_t parse_flag_names(char *name) |
| 133 | { |
| 134 | uint32_t flag = 0; |
| 135 | char *flag_name = strtok(name, "|"); |
| 136 | while (flag_name != NULL) { |
| 137 | if (strlen(flag_name) != 0) { |
| 138 | flag |= string_to_enum(s_flag_name_to_enum_table, |
| 139 | ARRAY_SIZE(s_flag_name_to_enum_table), |
| 140 | flag_name); |
| 141 | } |
| 142 | flag_name = strtok(NULL, "|"); |
| 143 | } |
| 144 | |
| 145 | ALOGV("parse_flag_names: flag - %d", flag); |
| 146 | return (audio_output_flags_t)flag; |
| 147 | } |
| 148 | |
| 149 | static void parse_format_names(char *name, struct streams_output_cfg *so_info) |
| 150 | { |
| 151 | struct stream_format *sf_info = NULL; |
| 152 | char *str = strtok(name, "|"); |
| 153 | |
| 154 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) |
| 155 | return; |
| 156 | |
| 157 | list_init(&so_info->format_list); |
| 158 | while (str != NULL) { |
| 159 | audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table, |
| 160 | ARRAY_SIZE(s_format_name_to_enum_table), str); |
| 161 | ALOGV("%s: format - %d", __func__, format); |
| 162 | if (format != 0) { |
| 163 | sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 164 | if (sf_info == NULL) |
| 165 | break; /* return whatever was parsed */ |
| 166 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 167 | sf_info->format = format; |
| 168 | list_add_tail(&so_info->format_list, &sf_info->list); |
| 169 | } |
| 170 | str = strtok(NULL, "|"); |
| 171 | } |
| 172 | } |
| 173 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 174 | static void parse_sample_rate_names(char *name, struct streams_output_cfg *so_info) |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 175 | { |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 176 | struct stream_sample_rate *ss_info = NULL; |
| 177 | uint32_t sample_rate = 48000; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 178 | char *str = strtok(name, "|"); |
| 179 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 180 | if (str != NULL && 0 == strcmp(str, DYNAMIC_VALUE_TAG)) |
| 181 | return; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 182 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 183 | list_init(&so_info->sample_rate_list); |
| 184 | while (str != NULL) { |
| 185 | sample_rate = (uint32_t)strtol(str, (char **)NULL, 10); |
| 186 | ALOGV("%s: sample_rate - %d", __func__, sample_rate); |
| 187 | if (0 != sample_rate) { |
| 188 | ss_info = (struct stream_sample_rate *)calloc(1, sizeof(struct stream_sample_rate)); |
Chaithanya Krishna Bacharaju | db6236a | 2015-01-30 10:08:33 +0530 | [diff] [blame] | 189 | if (ss_info == NULL) |
| 190 | break; /* return whatever was parsed */ |
| 191 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 192 | ss_info->sample_rate = sample_rate; |
| 193 | list_add_tail(&so_info->sample_rate_list, &ss_info->list); |
| 194 | } |
| 195 | str = strtok(NULL, "|"); |
| 196 | } |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int parse_bit_width_names(char *name) |
| 200 | { |
| 201 | int bit_width = 16; |
| 202 | char *str = strtok(name, "|"); |
| 203 | |
| 204 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG)) |
| 205 | bit_width = (int)strtol(str, (char **)NULL, 10); |
| 206 | |
| 207 | ALOGV("%s: bit_width - %d", __func__, bit_width); |
| 208 | return bit_width; |
| 209 | } |
| 210 | |
| 211 | static int parse_app_type_names(void *platform, char *name) |
| 212 | { |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 213 | int app_type = platform_get_default_app_type(platform); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 214 | char *str = strtok(name, "|"); |
| 215 | |
| 216 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG)) |
| 217 | app_type = (int)strtol(str, (char **)NULL, 10); |
| 218 | |
| 219 | ALOGV("%s: app_type - %d", __func__, app_type); |
| 220 | return app_type; |
| 221 | } |
| 222 | |
| 223 | static void update_streams_output_cfg_list(cnode *root, void *platform, |
| 224 | struct listnode *streams_output_cfg_list) |
| 225 | { |
| 226 | cnode *node = root->first_child; |
| 227 | struct streams_output_cfg *so_info; |
| 228 | |
| 229 | ALOGV("%s", __func__); |
| 230 | so_info = (struct streams_output_cfg *)calloc(1, sizeof(struct streams_output_cfg)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 231 | |
| 232 | if (!so_info) { |
| 233 | ALOGE("failed to allocate mem for so_info list element"); |
| 234 | return; |
| 235 | } |
| 236 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 237 | while (node) { |
| 238 | if (strcmp(node->name, FLAGS_TAG) == 0) { |
| 239 | so_info->flags = parse_flag_names((char *)node->value); |
| 240 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { |
| 241 | parse_format_names((char *)node->value, so_info); |
| 242 | } else if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 243 | so_info->app_type_cfg.sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE; |
| 244 | parse_sample_rate_names((char *)node->value, so_info); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 245 | } else if (strcmp(node->name, BIT_WIDTH_TAG) == 0) { |
| 246 | so_info->app_type_cfg.bit_width = parse_bit_width_names((char *)node->value); |
| 247 | } else if (strcmp(node->name, APP_TYPE_TAG) == 0) { |
| 248 | so_info->app_type_cfg.app_type = parse_app_type_names(platform, (char *)node->value); |
| 249 | } |
| 250 | node = node->next; |
| 251 | } |
| 252 | list_add_tail(streams_output_cfg_list, &so_info->list); |
| 253 | } |
| 254 | |
| 255 | static void load_output(cnode *root, void *platform, |
| 256 | struct listnode *streams_output_cfg_list) |
| 257 | { |
| 258 | cnode *node = config_find(root, OUTPUTS_TAG); |
| 259 | if (node == NULL) { |
| 260 | ALOGE("%s: could not load output, node is NULL", __func__); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | node = node->first_child; |
| 265 | while (node) { |
| 266 | ALOGV("%s: loading output %s", __func__, node->name); |
| 267 | update_streams_output_cfg_list(node, platform, streams_output_cfg_list); |
| 268 | node = node->next; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static void send_app_type_cfg(void *platform, struct mixer *mixer, |
| 273 | struct listnode *streams_output_cfg_list) |
| 274 | { |
| 275 | int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT] = {-1}; |
| 276 | int length = 0, i, num_app_types = 0; |
| 277 | struct listnode *node; |
| 278 | bool update; |
| 279 | struct mixer_ctl *ctl = NULL; |
| 280 | const char *mixer_ctl_name = "App Type Config"; |
| 281 | struct streams_output_cfg *so_info; |
| 282 | |
| 283 | if (!mixer) { |
| 284 | ALOGE("%s: mixer is null",__func__); |
| 285 | return; |
| 286 | } |
| 287 | ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name); |
| 288 | if (!ctl) { |
| 289 | ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name); |
| 290 | return; |
| 291 | } |
| 292 | if (streams_output_cfg_list == NULL) { |
| 293 | app_type_cfg[length++] = 1; |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 294 | app_type_cfg[length++] = platform_get_default_app_type(platform); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 295 | app_type_cfg[length++] = 48000; |
| 296 | app_type_cfg[length++] = 16; |
| 297 | mixer_ctl_set_array(ctl, app_type_cfg, length); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | app_type_cfg[length++] = num_app_types; |
| 302 | list_for_each(node, streams_output_cfg_list) { |
| 303 | so_info = node_to_item(node, struct streams_output_cfg, list); |
| 304 | update = true; |
| 305 | for (i=0; i<length; i=i+3) { |
| 306 | if (app_type_cfg[i+1] == -1) |
| 307 | break; |
| 308 | else if (app_type_cfg[i+1] == so_info->app_type_cfg.app_type) { |
| 309 | update = false; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | if (update && ((length + 3) <= MAX_LENGTH_MIXER_CONTROL_IN_INT)) { |
| 314 | num_app_types += 1 ; |
| 315 | app_type_cfg[length++] = so_info->app_type_cfg.app_type; |
| 316 | app_type_cfg[length++] = so_info->app_type_cfg.sample_rate; |
| 317 | app_type_cfg[length++] = so_info->app_type_cfg.bit_width; |
| 318 | } |
| 319 | } |
| 320 | ALOGV("%s: num_app_types: %d", __func__, num_app_types); |
| 321 | if (num_app_types) { |
| 322 | app_type_cfg[0] = num_app_types; |
| 323 | mixer_ctl_set_array(ctl, app_type_cfg, length); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void audio_extn_utils_update_streams_output_cfg_list(void *platform, |
| 328 | struct mixer *mixer, |
| 329 | struct listnode *streams_output_cfg_list) |
| 330 | { |
| 331 | cnode *root; |
| 332 | char *data; |
| 333 | |
| 334 | ALOGV("%s", __func__); |
| 335 | list_init(streams_output_cfg_list); |
| 336 | data = (char *)load_file(AUDIO_OUTPUT_POLICY_VENDOR_CONFIG_FILE, NULL); |
| 337 | if (data == NULL) { |
| 338 | send_app_type_cfg(platform, mixer, NULL); |
| 339 | ALOGE("%s: could not load output policy config file", __func__); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | root = config_node("", ""); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 344 | if (root == NULL) { |
| 345 | ALOGE("cfg_list, NULL config root"); |
| 346 | return; |
| 347 | } |
| 348 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 349 | config_load(root, data); |
| 350 | load_output(root, platform, streams_output_cfg_list); |
| 351 | |
| 352 | send_app_type_cfg(platform, mixer, streams_output_cfg_list); |
| 353 | } |
| 354 | |
| 355 | void audio_extn_utils_dump_streams_output_cfg_list( |
| 356 | struct listnode *streams_output_cfg_list) |
| 357 | { |
| 358 | int i=0; |
| 359 | struct listnode *node_i, *node_j; |
| 360 | struct streams_output_cfg *so_info; |
| 361 | struct stream_format *sf_info; |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 362 | struct stream_sample_rate *ss_info; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 363 | ALOGV("%s", __func__); |
| 364 | list_for_each(node_i, streams_output_cfg_list) { |
| 365 | so_info = node_to_item(node_i, struct streams_output_cfg, list); |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 366 | ALOGV("%s: flags-%d, output_sample_rate-%d, output_bit_width-%d, app_type-%d", |
| 367 | __func__, so_info->flags, so_info->app_type_cfg.sample_rate, |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 368 | so_info->app_type_cfg.bit_width, so_info->app_type_cfg.app_type); |
| 369 | list_for_each(node_j, &so_info->format_list) { |
| 370 | sf_info = node_to_item(node_j, struct stream_format, list); |
| 371 | ALOGV("format-%x", sf_info->format); |
| 372 | } |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 373 | list_for_each(node_j, &so_info->sample_rate_list) { |
| 374 | ss_info = node_to_item(node_j, struct stream_sample_rate, list); |
| 375 | ALOGV("sample rate-%d", ss_info->sample_rate); |
| 376 | } |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | void audio_extn_utils_release_streams_output_cfg_list( |
| 381 | struct listnode *streams_output_cfg_list) |
| 382 | { |
| 383 | struct listnode *node_i, *node_j; |
| 384 | struct streams_output_cfg *so_info; |
| 385 | struct stream_format *sf_info; |
| 386 | |
| 387 | ALOGV("%s", __func__); |
| 388 | while (!list_empty(streams_output_cfg_list)) { |
| 389 | node_i = list_head(streams_output_cfg_list); |
| 390 | so_info = node_to_item(node_i, struct streams_output_cfg, list); |
| 391 | while (!list_empty(&so_info->format_list)) { |
| 392 | node_j = list_head(&so_info->format_list); |
| 393 | list_remove(node_j); |
| 394 | free(node_to_item(node_j, struct stream_format, list)); |
| 395 | } |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 396 | while (!list_empty(&so_info->sample_rate_list)) { |
| 397 | node_j = list_head(&so_info->sample_rate_list); |
| 398 | list_remove(node_j); |
| 399 | free(node_to_item(node_j, struct stream_sample_rate, list)); |
| 400 | } |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 401 | list_remove(node_i); |
| 402 | free(node_to_item(node_i, struct streams_output_cfg, list)); |
| 403 | } |
| 404 | } |
| 405 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 406 | static bool set_output_cfg(struct streams_output_cfg *so_info, |
| 407 | struct stream_app_type_cfg *app_type_cfg, |
| 408 | uint32_t sample_rate, uint32_t bit_width) |
| 409 | { |
| 410 | struct listnode *node_i; |
| 411 | struct stream_sample_rate *ss_info; |
| 412 | list_for_each(node_i, &so_info->sample_rate_list) { |
| 413 | ss_info = node_to_item(node_i, struct stream_sample_rate, list); |
| 414 | if ((sample_rate <= ss_info->sample_rate) && |
| 415 | (bit_width == so_info->app_type_cfg.bit_width)) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 416 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 417 | app_type_cfg->app_type = so_info->app_type_cfg.app_type; |
| 418 | app_type_cfg->sample_rate = ss_info->sample_rate; |
| 419 | app_type_cfg->bit_width = so_info->app_type_cfg.bit_width; |
| 420 | ALOGV("%s app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d", |
| 421 | __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width); |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | /* |
| 426 | * Reiterate through the list assuming dafault sample rate. |
| 427 | * Handles scenario where input sample rate is higher |
| 428 | * than all sample rates in list for the input bit width. |
| 429 | */ |
| 430 | sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE; |
Apoorv Raghuvanshi | f59bb22 | 2015-02-18 12:23:23 -0800 | [diff] [blame] | 431 | |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 432 | list_for_each(node_i, &so_info->sample_rate_list) { |
| 433 | ss_info = node_to_item(node_i, struct stream_sample_rate, list); |
| 434 | if ((sample_rate <= ss_info->sample_rate) && |
| 435 | (bit_width == so_info->app_type_cfg.bit_width)) { |
| 436 | app_type_cfg->app_type = so_info->app_type_cfg.app_type; |
| 437 | app_type_cfg->sample_rate = sample_rate; |
| 438 | app_type_cfg->bit_width = so_info->app_type_cfg.bit_width; |
Apoorv Raghuvanshi | f59bb22 | 2015-02-18 12:23:23 -0800 | [diff] [blame] | 439 | ALOGV("%s Assuming sample rate. app_type_cfg->app_type %d, app_type_cfg->sample_rate %d, app_type_cfg->bit_width %d", |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 440 | __func__, app_type_cfg->app_type, app_type_cfg->sample_rate, app_type_cfg->bit_width); |
| 441 | return true; |
| 442 | } |
| 443 | } |
| 444 | return false; |
| 445 | } |
| 446 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 447 | void audio_extn_utils_update_stream_app_type_cfg(void *platform, |
| 448 | struct listnode *streams_output_cfg_list, |
Amit Shekhar | 1d89604 | 2014-10-03 13:16:09 -0700 | [diff] [blame] | 449 | audio_devices_t devices, |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 450 | audio_output_flags_t flags, |
| 451 | audio_format_t format, |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 452 | uint32_t sample_rate, |
| 453 | uint32_t bit_width, |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 454 | struct stream_app_type_cfg *app_type_cfg) |
| 455 | { |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 456 | struct listnode *node_i, *node_j, *node_k; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 457 | struct streams_output_cfg *so_info; |
| 458 | struct stream_format *sf_info; |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 459 | struct stream_sample_rate *ss_info; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 460 | |
Amit Shekhar | 1d89604 | 2014-10-03 13:16:09 -0700 | [diff] [blame] | 461 | if ((24 == bit_width) && |
| 462 | (devices & AUDIO_DEVICE_OUT_SPEAKER)) { |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 463 | int32_t bw = platform_get_snd_device_bit_width(SND_DEVICE_OUT_SPEAKER); |
| 464 | if (-ENOSYS != bw) |
| 465 | bit_width = (uint32_t)bw; |
Amit Shekhar | 1d89604 | 2014-10-03 13:16:09 -0700 | [diff] [blame] | 466 | sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 467 | ALOGI("%s Allowing 24-bit playback on speaker ONLY at default sampling rate", __func__); |
| 468 | } |
| 469 | |
| 470 | ALOGV("%s: flags: %x, format: %x sample_rate %d", |
| 471 | __func__, flags, format, sample_rate); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 472 | list_for_each(node_i, streams_output_cfg_list) { |
| 473 | so_info = node_to_item(node_i, struct streams_output_cfg, list); |
| 474 | if (so_info->flags == flags) { |
| 475 | list_for_each(node_j, &so_info->format_list) { |
| 476 | sf_info = node_to_item(node_j, struct stream_format, list); |
| 477 | if (sf_info->format == format) { |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 478 | if (set_output_cfg(so_info, app_type_cfg, sample_rate, bit_width)) |
| 479 | return; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | list_for_each(node_i, streams_output_cfg_list) { |
| 485 | so_info = node_to_item(node_i, struct streams_output_cfg, list); |
| 486 | if (so_info->flags == AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 487 | ALOGV("Compatible output profile not found."); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 488 | app_type_cfg->app_type = so_info->app_type_cfg.app_type; |
| 489 | app_type_cfg->sample_rate = so_info->app_type_cfg.sample_rate; |
| 490 | app_type_cfg->bit_width = so_info->app_type_cfg.bit_width; |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 491 | ALOGV("%s Default to primary output: App type: %d sample_rate %d", |
| 492 | __func__, so_info->app_type_cfg.app_type, app_type_cfg->sample_rate); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | } |
| 496 | ALOGW("%s: App type could not be selected. Falling back to default", __func__); |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 497 | app_type_cfg->app_type = platform_get_default_app_type(platform); |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 498 | app_type_cfg->sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 499 | app_type_cfg->bit_width = 16; |
| 500 | } |
| 501 | |
| 502 | int audio_extn_utils_send_app_type_cfg(struct audio_usecase *usecase) |
| 503 | { |
| 504 | char mixer_ctl_name[MAX_LENGTH_MIXER_CONTROL_IN_INT]; |
| 505 | int app_type_cfg[MAX_LENGTH_MIXER_CONTROL_IN_INT], len = 0, rc; |
Anish Kumar | c8599f2 | 2014-07-20 09:36:07 -0700 | [diff] [blame] | 506 | struct stream_out *out; |
| 507 | struct audio_device *adev; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 508 | struct mixer_ctl *ctl; |
| 509 | int pcm_device_id, acdb_dev_id, snd_device = usecase->out_snd_device; |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 510 | int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 511 | |
| 512 | ALOGV("%s", __func__); |
| 513 | |
| 514 | if (usecase->type != PCM_PLAYBACK) { |
| 515 | ALOGV("%s: not a playback path, no need to cfg app type", __func__); |
| 516 | rc = 0; |
| 517 | goto exit_send_app_type_cfg; |
| 518 | } |
| 519 | if ((usecase->id != USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) && |
| 520 | (usecase->id != USECASE_AUDIO_PLAYBACK_LOW_LATENCY) && |
| 521 | (usecase->id != USECASE_AUDIO_PLAYBACK_MULTI_CH) && |
Alexy Joseph | 2f89cfa | 2014-10-06 12:15:01 -0700 | [diff] [blame] | 522 | (!is_offload_usecase(usecase->id))) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 523 | ALOGV("%s: a playback path where app type cfg is not required %d", __func__, usecase->id); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 524 | rc = 0; |
| 525 | goto exit_send_app_type_cfg; |
| 526 | } |
Anish Kumar | c8599f2 | 2014-07-20 09:36:07 -0700 | [diff] [blame] | 527 | out = usecase->stream.out; |
| 528 | adev = out->dev; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 529 | |
| 530 | snd_device = usecase->out_snd_device; |
| 531 | |
| 532 | pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
| 533 | |
| 534 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 535 | "Audio Stream %d App Type Cfg", pcm_device_id); |
| 536 | |
| 537 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 538 | if (!ctl) { |
| 539 | ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, |
| 540 | mixer_ctl_name); |
| 541 | rc = -EINVAL; |
| 542 | goto exit_send_app_type_cfg; |
| 543 | } |
| 544 | snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ? |
| 545 | audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device; |
| 546 | acdb_dev_id = platform_get_snd_device_acdb_id(snd_device); |
| 547 | if (acdb_dev_id < 0) { |
| 548 | ALOGE("%s: Couldn't get the acdb dev id", __func__); |
| 549 | rc = -EINVAL; |
| 550 | goto exit_send_app_type_cfg; |
| 551 | } |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 552 | |
| 553 | if ((24 == usecase->stream.out->bit_width) && |
Amit Shekhar | 1d89604 | 2014-10-03 13:16:09 -0700 | [diff] [blame] | 554 | (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) { |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 555 | sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame] | 556 | } else if ((snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 && |
| 557 | usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) || |
| 558 | (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) { |
| 559 | sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 560 | } else { |
| 561 | sample_rate = out->app_type_cfg.sample_rate; |
| 562 | } |
| 563 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 564 | app_type_cfg[len++] = out->app_type_cfg.app_type; |
| 565 | app_type_cfg[len++] = acdb_dev_id; |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 566 | if (((out->format == AUDIO_FORMAT_E_AC3) || |
Mingming Yin | 497419f | 2015-07-01 16:57:32 -0700 | [diff] [blame] | 567 | (out->format == AUDIO_FORMAT_E_AC3_JOC)) |
| 568 | #ifdef HDMI_PASSTHROUGH_ENABLED |
| 569 | && (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH) |
| 570 | #endif |
| 571 | ) |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 572 | app_type_cfg[len++] = sample_rate * 4; |
| 573 | else |
| 574 | app_type_cfg[len++] = sample_rate; |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame] | 575 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 576 | mixer_ctl_set_array(ctl, app_type_cfg, len); |
Amit Shekhar | 278e336 | 2014-09-08 14:08:19 -0700 | [diff] [blame] | 577 | ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d", |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 578 | __func__, out->app_type_cfg.app_type, acdb_dev_id, sample_rate); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 579 | rc = 0; |
| 580 | exit_send_app_type_cfg: |
| 581 | return rc; |
| 582 | } |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 583 | |
| 584 | void audio_extn_utils_send_audio_calibration(struct audio_device *adev, |
| 585 | struct audio_usecase *usecase) |
| 586 | { |
| 587 | int type = usecase->type; |
| 588 | |
| 589 | if (type == PCM_PLAYBACK) { |
| 590 | struct stream_out *out = usecase->stream.out; |
| 591 | int snd_device = usecase->out_snd_device; |
| 592 | snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ? |
| 593 | audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device; |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 594 | platform_send_audio_calibration(adev->platform, usecase, |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 595 | out->app_type_cfg.app_type, |
| 596 | out->app_type_cfg.sample_rate); |
| 597 | } |
| 598 | if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) { |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 599 | /* when app type is default. the sample rate is not used to send cal */ |
| 600 | platform_send_audio_calibration(adev->platform, usecase, |
| 601 | platform_get_default_app_type(adev->platform), |
| 602 | 48000); |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Ben Romberger | a04fabc | 2014-11-14 12:16:03 -0800 | [diff] [blame] | 606 | // Base64 Encode and Decode |
| 607 | // Not all features supported. This must be used only with following conditions. |
| 608 | // Decode Modes: Support with and without padding |
| 609 | // CRLF not handling. So no CRLF in string to decode. |
| 610 | // Encode Modes: Supports only padding |
| 611 | int b64decode(char *inp, int ilen, uint8_t* outp) |
| 612 | { |
| 613 | int i, j, k, ii, num; |
| 614 | int rem, pcnt; |
| 615 | uint32_t res=0; |
| 616 | uint8_t getIndex[MAX_BASEINDEX_LEN]; |
| 617 | uint8_t tmp, cflag; |
| 618 | |
| 619 | if(inp == NULL || outp == NULL || ilen <= 0) { |
| 620 | ALOGE("[%s] received NULL pointer or zero length",__func__); |
| 621 | return -1; |
| 622 | } |
| 623 | |
| 624 | memset(getIndex, MAX_BASEINDEX_LEN-1, sizeof(getIndex)); |
| 625 | for(i=0;i<BASE_TABLE_SIZE;i++) { |
| 626 | getIndex[(uint8_t)bTable[i]] = (uint8_t)i; |
| 627 | } |
| 628 | getIndex[(uint8_t)'=']=0; |
| 629 | |
| 630 | j=0;k=0; |
| 631 | num = ilen/4; |
| 632 | rem = ilen%4; |
| 633 | if(rem==0) |
| 634 | num = num-1; |
| 635 | cflag=0; |
| 636 | for(i=0; i<num; i++) { |
| 637 | res=0; |
| 638 | for(ii=0;ii<4;ii++) { |
| 639 | res = res << 6; |
| 640 | tmp = getIndex[(uint8_t)inp[j++]]; |
| 641 | res = res | tmp; |
| 642 | cflag = cflag | tmp; |
| 643 | } |
| 644 | outp[k++] = (res >> 16)&0xFF; |
| 645 | outp[k++] = (res >> 8)&0xFF; |
| 646 | outp[k++] = res & 0xFF; |
| 647 | } |
| 648 | |
| 649 | // Handle last bytes special |
| 650 | pcnt=0; |
| 651 | if(rem == 0) { |
| 652 | //With padding or full data |
| 653 | res = 0; |
| 654 | for(ii=0;ii<4;ii++) { |
| 655 | if(inp[j] == '=') |
| 656 | pcnt++; |
| 657 | res = res << 6; |
| 658 | tmp = getIndex[(uint8_t)inp[j++]]; |
| 659 | res = res | tmp; |
| 660 | cflag = cflag | tmp; |
| 661 | } |
| 662 | outp[k++] = res >> 16; |
| 663 | if(pcnt == 2) |
| 664 | goto done; |
| 665 | outp[k++] = (res>>8)&0xFF; |
| 666 | if(pcnt == 1) |
| 667 | goto done; |
| 668 | outp[k++] = res&0xFF; |
| 669 | } else { |
| 670 | //without padding |
| 671 | res = 0; |
| 672 | for(i=0;i<rem;i++) { |
| 673 | res = res << 6; |
| 674 | tmp = getIndex[(uint8_t)inp[j++]]; |
| 675 | res = res | tmp; |
| 676 | cflag = cflag | tmp; |
| 677 | } |
| 678 | for(i=rem;i<4;i++) { |
| 679 | res = res << 6; |
| 680 | pcnt++; |
| 681 | } |
| 682 | outp[k++] = res >> 16; |
| 683 | if(pcnt == 2) |
| 684 | goto done; |
| 685 | outp[k++] = (res>>8)&0xFF; |
| 686 | if(pcnt == 1) |
| 687 | goto done; |
| 688 | outp[k++] = res&0xFF; |
| 689 | } |
| 690 | done: |
| 691 | if(cflag == 0xFF) { |
| 692 | ALOGE("[%s] base64 decode failed. Invalid character found %s", |
| 693 | __func__, inp); |
| 694 | return 0; |
| 695 | } |
| 696 | return k; |
| 697 | } |
| 698 | |
| 699 | int b64encode(uint8_t *inp, int ilen, char* outp) |
| 700 | { |
| 701 | int i,j,k, num; |
| 702 | int rem=0; |
| 703 | uint32_t res=0; |
| 704 | |
| 705 | if(inp == NULL || outp == NULL || ilen<=0) { |
| 706 | ALOGE("[%s] received NULL pointer or zero input length",__func__); |
| 707 | return -1; |
| 708 | } |
| 709 | |
| 710 | num = ilen/3; |
| 711 | rem = ilen%3; |
| 712 | j=0;k=0; |
| 713 | for(i=0; i<num; i++) { |
| 714 | //prepare index |
| 715 | res = inp[j++]<<16; |
| 716 | res = res | inp[j++]<<8; |
| 717 | res = res | inp[j++]; |
| 718 | //get output map from index |
| 719 | outp[k++] = (char) bTable[(res>>18)&0x3F]; |
| 720 | outp[k++] = (char) bTable[(res>>12)&0x3F]; |
| 721 | outp[k++] = (char) bTable[(res>>6)&0x3F]; |
| 722 | outp[k++] = (char) bTable[res&0x3F]; |
| 723 | } |
| 724 | |
| 725 | switch(rem) { |
| 726 | case 1: |
| 727 | res = inp[j++]<<16; |
| 728 | outp[k++] = (char) bTable[res>>18]; |
| 729 | outp[k++] = (char) bTable[(res>>12)&0x3F]; |
| 730 | //outp[k++] = '='; |
| 731 | //outp[k++] = '='; |
| 732 | break; |
| 733 | case 2: |
| 734 | res = inp[j++]<<16; |
| 735 | res = res | inp[j++]<<8; |
| 736 | outp[k++] = (char) bTable[res>>18]; |
| 737 | outp[k++] = (char) bTable[(res>>12)&0x3F]; |
| 738 | outp[k++] = (char) bTable[(res>>6)&0x3F]; |
| 739 | //outp[k++] = '='; |
| 740 | break; |
| 741 | default: |
| 742 | break; |
| 743 | } |
| 744 | done: |
| 745 | outp[k] = '\0'; |
| 746 | return k; |
| 747 | } |