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