Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <assert.h> |
| 17 | #include <inttypes.h> |
Saketh Sathuvalli | cb398ab | 2019-02-16 13:12:59 +0530 | [diff] [blame] | 18 | #include <iterator> |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 19 | #include <math.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <audio_utils/channels.h> |
| 25 | #include <audio_utils/primitives.h> |
| 26 | #include <log/log.h> |
Saketh Sathuvalli | 7b66e3b | 2019-02-08 15:18:54 +0530 | [diff] [blame] | 27 | #include <system/audio.h> |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 28 | |
| 29 | #include "EffectBundle.h" |
| 30 | #include "LVM_Private.h" |
| 31 | |
| 32 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 33 | #define ALOGVV ALOGV |
| 34 | #else |
| 35 | #define ALOGVV(a...) \ |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 36 | do { \ |
| 37 | } while (false) |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 38 | #endif |
| 39 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 40 | #define CHECK_ARG(cond) \ |
| 41 | { \ |
| 42 | if (!(cond)) { \ |
| 43 | ALOGE("\tLVM_ERROR : Invalid argument: " #cond); \ |
| 44 | return -EINVAL; \ |
| 45 | } \ |
| 46 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 47 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 48 | #define LVM_ERROR_CHECK(LvmStatus, callingFunc, calledFunc) \ |
| 49 | { \ |
| 50 | if ((LvmStatus) == LVM_NULLADDRESS) { \ |
| 51 | ALOGE("\tLVM_ERROR : Parameter error - " \ |
| 52 | "null pointer returned by %s in %s\n\n\n\n", \ |
| 53 | callingFunc, calledFunc); \ |
| 54 | } \ |
| 55 | if ((LvmStatus) == LVM_ALIGNMENTERROR) { \ |
| 56 | ALOGE("\tLVM_ERROR : Parameter error - " \ |
| 57 | "bad alignment returned by %s in %s\n\n\n\n", \ |
| 58 | callingFunc, calledFunc); \ |
| 59 | } \ |
| 60 | if ((LvmStatus) == LVM_INVALIDNUMSAMPLES) { \ |
| 61 | ALOGE("\tLVM_ERROR : Parameter error - " \ |
| 62 | "bad number of samples returned by %s in %s\n\n\n\n", \ |
| 63 | callingFunc, calledFunc); \ |
| 64 | } \ |
| 65 | if ((LvmStatus) == LVM_OUTOFRANGE) { \ |
| 66 | ALOGE("\tLVM_ERROR : Parameter error - " \ |
| 67 | "out of range returned by %s in %s\n", \ |
| 68 | callingFunc, calledFunc); \ |
| 69 | } \ |
| 70 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 71 | |
| 72 | struct lvmConfigParams_t { |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 73 | int samplingFreq = 44100; |
| 74 | int nrChannels = 2; |
| 75 | int chMask = AUDIO_CHANNEL_OUT_STEREO; |
| 76 | int vcBal = 0; |
| 77 | int fChannels = 2; |
| 78 | bool monoMode = false; |
| 79 | int bassEffectLevel = 0; |
| 80 | int eqPresetLevel = 0; |
| 81 | int frameLength = 256; |
| 82 | LVM_BE_Mode_en bassEnable = LVM_BE_OFF; |
| 83 | LVM_TE_Mode_en trebleEnable = LVM_TE_OFF; |
| 84 | LVM_EQNB_Mode_en eqEnable = LVM_EQNB_OFF; |
| 85 | LVM_Mode_en csEnable = LVM_MODE_OFF; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 86 | }; |
| 87 | |
Saketh Sathuvalli | 7b66e3b | 2019-02-08 15:18:54 +0530 | [diff] [blame] | 88 | constexpr audio_channel_mask_t lvmConfigChMask[] = { |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 89 | AUDIO_CHANNEL_OUT_MONO, |
| 90 | AUDIO_CHANNEL_OUT_STEREO, |
| 91 | AUDIO_CHANNEL_OUT_2POINT1, |
| 92 | AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 93 | AUDIO_CHANNEL_OUT_QUAD, |
| 94 | AUDIO_CHANNEL_OUT_QUAD_BACK, |
| 95 | AUDIO_CHANNEL_OUT_QUAD_SIDE, |
| 96 | AUDIO_CHANNEL_OUT_SURROUND, |
Mikhail Naganov | 48e2345 | 2020-10-07 20:57:51 +0000 | [diff] [blame] | 97 | AUDIO_CHANNEL_INDEX_MASK_4, |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 98 | AUDIO_CHANNEL_OUT_2POINT1POINT2, |
| 99 | AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 100 | AUDIO_CHANNEL_OUT_PENTA, |
Mikhail Naganov | 48e2345 | 2020-10-07 20:57:51 +0000 | [diff] [blame] | 101 | AUDIO_CHANNEL_INDEX_MASK_5, |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 102 | AUDIO_CHANNEL_OUT_3POINT1POINT2, |
| 103 | AUDIO_CHANNEL_OUT_5POINT1, |
| 104 | AUDIO_CHANNEL_OUT_5POINT1_BACK, |
| 105 | AUDIO_CHANNEL_OUT_5POINT1_SIDE, |
Mikhail Naganov | 48e2345 | 2020-10-07 20:57:51 +0000 | [diff] [blame] | 106 | AUDIO_CHANNEL_INDEX_MASK_6, |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 107 | AUDIO_CHANNEL_OUT_6POINT1, |
Mikhail Naganov | 48e2345 | 2020-10-07 20:57:51 +0000 | [diff] [blame] | 108 | AUDIO_CHANNEL_INDEX_MASK_7, |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 109 | AUDIO_CHANNEL_OUT_5POINT1POINT2, |
| 110 | AUDIO_CHANNEL_OUT_7POINT1, |
Mikhail Naganov | 48e2345 | 2020-10-07 20:57:51 +0000 | [diff] [blame] | 111 | AUDIO_CHANNEL_INDEX_MASK_8, |
Harish Mahendrakar | a458f10 | 2021-01-08 10:25:47 -0800 | [diff] [blame] | 112 | AUDIO_CHANNEL_INDEX_MASK_9, |
| 113 | AUDIO_CHANNEL_INDEX_MASK_10, |
| 114 | AUDIO_CHANNEL_INDEX_MASK_11, |
| 115 | AUDIO_CHANNEL_INDEX_MASK_12, |
| 116 | AUDIO_CHANNEL_INDEX_MASK_13, |
| 117 | AUDIO_CHANNEL_INDEX_MASK_14, |
| 118 | AUDIO_CHANNEL_INDEX_MASK_15, |
| 119 | AUDIO_CHANNEL_INDEX_MASK_16, |
| 120 | AUDIO_CHANNEL_INDEX_MASK_17, |
| 121 | AUDIO_CHANNEL_INDEX_MASK_18, |
| 122 | AUDIO_CHANNEL_INDEX_MASK_19, |
| 123 | AUDIO_CHANNEL_INDEX_MASK_20, |
| 124 | AUDIO_CHANNEL_INDEX_MASK_21, |
| 125 | AUDIO_CHANNEL_INDEX_MASK_22, |
| 126 | AUDIO_CHANNEL_INDEX_MASK_23, |
| 127 | AUDIO_CHANNEL_INDEX_MASK_24, |
Saketh Sathuvalli | 7b66e3b | 2019-02-08 15:18:54 +0530 | [diff] [blame] | 128 | }; |
| 129 | |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 130 | void printUsage() { |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 131 | printf("\nUsage: "); |
| 132 | printf("\n <executable> -i:<input_file> -o:<out_file> [options]\n"); |
| 133 | printf("\nwhere, \n <inputfile> is the input file name"); |
| 134 | printf("\n on which LVM effects are applied"); |
| 135 | printf("\n <outputfile> processed output file"); |
| 136 | printf("\n and options are mentioned below"); |
| 137 | printf("\n"); |
| 138 | printf("\n -help (or) -h"); |
| 139 | printf("\n Prints this usage information"); |
| 140 | printf("\n"); |
| 141 | printf("\n -chMask:<channel_mask>\n"); |
| 142 | printf("\n 0 - AUDIO_CHANNEL_OUT_MONO"); |
| 143 | printf("\n 1 - AUDIO_CHANNEL_OUT_STEREO"); |
| 144 | printf("\n 2 - AUDIO_CHANNEL_OUT_2POINT1"); |
| 145 | printf("\n 3 - AUDIO_CHANNEL_OUT_2POINT0POINT2"); |
| 146 | printf("\n 4 - AUDIO_CHANNEL_OUT_QUAD"); |
| 147 | printf("\n 5 - AUDIO_CHANNEL_OUT_QUAD_BACK"); |
| 148 | printf("\n 6 - AUDIO_CHANNEL_OUT_QUAD_SIDE"); |
| 149 | printf("\n 7 - AUDIO_CHANNEL_OUT_SURROUND"); |
| 150 | printf("\n 8 - canonical channel index mask for 4 ch: (1 << 4) - 1"); |
| 151 | printf("\n 9 - AUDIO_CHANNEL_OUT_2POINT1POINT2"); |
| 152 | printf("\n 10 - AUDIO_CHANNEL_OUT_3POINT0POINT2"); |
| 153 | printf("\n 11 - AUDIO_CHANNEL_OUT_PENTA"); |
| 154 | printf("\n 12 - canonical channel index mask for 5 ch: (1 << 5) - 1"); |
| 155 | printf("\n 13 - AUDIO_CHANNEL_OUT_3POINT1POINT2"); |
| 156 | printf("\n 14 - AUDIO_CHANNEL_OUT_5POINT1"); |
| 157 | printf("\n 15 - AUDIO_CHANNEL_OUT_5POINT1_BACK"); |
| 158 | printf("\n 16 - AUDIO_CHANNEL_OUT_5POINT1_SIDE"); |
| 159 | printf("\n 17 - canonical channel index mask for 6 ch: (1 << 6) - 1"); |
| 160 | printf("\n 18 - AUDIO_CHANNEL_OUT_6POINT1"); |
| 161 | printf("\n 19 - canonical channel index mask for 7 ch: (1 << 7) - 1"); |
| 162 | printf("\n 20 - AUDIO_CHANNEL_OUT_5POINT1POINT2"); |
| 163 | printf("\n 21 - AUDIO_CHANNEL_OUT_7POINT1"); |
| 164 | printf("\n 22 - canonical channel index mask for 8 ch: (1 << 8) - 1"); |
| 165 | printf("\n default 0"); |
| 166 | printf("\n -vcBal:<Left Right Balance control in dB [-96 to 96 dB]>"); |
| 167 | printf("\n -ve values reduce Right channel while +ve value reduces Left channel"); |
| 168 | printf("\n default 0"); |
| 169 | printf("\n -fch:<file_channels> (1 through 8)\n\n"); |
| 170 | printf("\n -M"); |
| 171 | printf("\n Mono mode (force all input audio channels to be identical)"); |
| 172 | printf("\n -basslvl:<effect_level>"); |
| 173 | printf("\n A value that ranges between %d - %d default 0", LVM_BE_MIN_EFFECTLEVEL, |
| 174 | LVM_BE_MAX_EFFECTLEVEL); |
| 175 | printf("\n"); |
| 176 | printf("\n -eqPreset:<preset Value>"); |
| 177 | const size_t numPresetLvls = std::size(gEqualizerPresets); |
| 178 | for (size_t i = 0; i < numPresetLvls; ++i) { |
| 179 | printf("\n %zu - %s", i, gEqualizerPresets[i].name); |
| 180 | } |
| 181 | printf("\n default - 0"); |
| 182 | printf("\n -bE "); |
| 183 | printf("\n Enable Dynamic Bass Enhancement"); |
| 184 | printf("\n"); |
| 185 | printf("\n -tE "); |
| 186 | printf("\n Enable Treble Boost"); |
| 187 | printf("\n"); |
| 188 | printf("\n -csE "); |
| 189 | printf("\n Enable Concert Surround"); |
| 190 | printf("\n"); |
| 191 | printf("\n -eqE "); |
| 192 | printf("\n Enable Equalizer"); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 193 | } |
| 194 | |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 195 | //---------------------------------------------------------------------------- |
| 196 | // LvmBundle_init() |
| 197 | //---------------------------------------------------------------------------- |
| 198 | // Purpose: Initialize engine with default configuration, creates instance |
| 199 | // with all effects disabled. |
| 200 | // |
| 201 | // Inputs: |
| 202 | // pContext: effect engine context |
| 203 | // |
| 204 | // Outputs: |
| 205 | // |
| 206 | //---------------------------------------------------------------------------- |
| 207 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 208 | int LvmBundle_init(struct EffectContext* pContext, LVM_ControlParams_t* params) { |
| 209 | ALOGV("\tLvmBundle_init start"); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 210 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 211 | pContext->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 212 | pContext->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 213 | pContext->config.inputCfg.format = EFFECT_BUFFER_FORMAT; |
| 214 | pContext->config.inputCfg.samplingRate = 44100; |
| 215 | pContext->config.inputCfg.bufferProvider.getBuffer = NULL; |
| 216 | pContext->config.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 217 | pContext->config.inputCfg.bufferProvider.cookie = NULL; |
| 218 | pContext->config.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 219 | pContext->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 220 | pContext->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 221 | pContext->config.outputCfg.format = EFFECT_BUFFER_FORMAT; |
| 222 | pContext->config.outputCfg.samplingRate = 44100; |
| 223 | pContext->config.outputCfg.bufferProvider.getBuffer = NULL; |
| 224 | pContext->config.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 225 | pContext->config.outputCfg.bufferProvider.cookie = NULL; |
| 226 | pContext->config.outputCfg.mask = EFFECT_CONFIG_ALL; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 227 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 228 | if (pContext->pBundledContext->hInstance != NULL) { |
| 229 | ALOGV("\tLvmBundle_init pContext->pBassBoost != NULL " |
| 230 | "-> Calling pContext->pBassBoost->free()"); |
| 231 | LVM_DelInstanceHandle(&pContext->pBundledContext->hInstance); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 232 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 233 | ALOGV("\tLvmBundle_init pContext->pBassBoost != NULL " |
| 234 | "-> Called pContext->pBassBoost->free()"); |
| 235 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 236 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 237 | LVM_ReturnStatus_en LvmStatus = LVM_SUCCESS; /* Function call status */ |
| 238 | LVM_InstParams_t InstParams; /* Instance parameters */ |
| 239 | LVM_EQNB_BandDef_t BandDefs[MAX_NUM_BANDS]; /* Equaliser band definitions */ |
| 240 | LVM_HeadroomParams_t HeadroomParams; /* Headroom parameters */ |
| 241 | LVM_HeadroomBandDef_t HeadroomBandDef[LVM_HEADROOM_MAX_NBANDS]; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 242 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 243 | /* Set the capabilities */ |
| 244 | InstParams.BufferMode = LVM_UNMANAGED_BUFFERS; |
| 245 | InstParams.MaxBlockSize = MAX_CALL_SIZE; |
| 246 | InstParams.EQNB_NumBands = MAX_NUM_BANDS; |
| 247 | InstParams.PSA_Included = LVM_PSA_ON; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 248 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 249 | LvmStatus = LVM_GetInstanceHandle(&pContext->pBundledContext->hInstance, &InstParams); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 250 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 251 | LVM_ERROR_CHECK(LvmStatus, "LVM_GetInstanceHandle", "LvmBundle_init"); |
| 252 | if (LvmStatus != LVM_SUCCESS) return -EINVAL; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 253 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 254 | ALOGV("\tLvmBundle_init CreateInstance Successfully called " |
| 255 | "LVM_GetInstanceHandle\n"); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 256 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 257 | /* Set the initial process parameters */ |
| 258 | /* General parameters */ |
| 259 | params->OperatingMode = LVM_MODE_ON; |
| 260 | params->SampleRate = LVM_FS_44100; |
| 261 | params->SourceFormat = LVM_STEREO; |
| 262 | params->ChMask = AUDIO_CHANNEL_OUT_STEREO; |
| 263 | params->SpeakerType = LVM_HEADPHONES; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 264 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 265 | pContext->pBundledContext->SampleRate = LVM_FS_44100; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 266 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 267 | /* Concert Sound parameters */ |
| 268 | params->VirtualizerOperatingMode = LVM_MODE_OFF; |
| 269 | params->VirtualizerType = LVM_CONCERTSOUND; |
| 270 | params->VirtualizerReverbLevel = 100; |
| 271 | params->CS_EffectLevel = LVM_CS_EFFECT_NONE; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 272 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 273 | /* N-Band Equaliser parameters */ |
| 274 | params->EQNB_OperatingMode = LVM_EQNB_ON; |
| 275 | params->EQNB_NBands = FIVEBAND_NUMBANDS; |
| 276 | params->pEQNB_BandDefinition = &BandDefs[0]; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 277 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 278 | for (int i = 0; i < FIVEBAND_NUMBANDS; i++) { |
| 279 | BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i]; |
| 280 | BandDefs[i].QFactor = EQNB_5BandPresetsQFactors[i]; |
| 281 | BandDefs[i].Gain = EQNB_5BandSoftPresets[i]; |
| 282 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 283 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 284 | /* Volume Control parameters */ |
| 285 | params->VC_EffectLevel = 0; |
| 286 | params->VC_Balance = 0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 287 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 288 | /* Treble Enhancement parameters */ |
| 289 | params->TE_OperatingMode = LVM_TE_OFF; |
| 290 | params->TE_EffectLevel = 0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 291 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 292 | /* PSA Control parameters */ |
| 293 | params->PSA_Enable = LVM_PSA_OFF; |
| 294 | params->PSA_PeakDecayRate = (LVM_PSA_DecaySpeed_en)0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 295 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 296 | /* Bass Enhancement parameters */ |
| 297 | params->BE_OperatingMode = LVM_BE_ON; |
| 298 | params->BE_EffectLevel = 0; |
| 299 | params->BE_CentreFreq = LVM_BE_CENTRE_90Hz; |
| 300 | params->BE_HPF = LVM_BE_HPF_ON; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 301 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 302 | /* PSA Control parameters */ |
| 303 | params->PSA_Enable = LVM_PSA_OFF; |
| 304 | params->PSA_PeakDecayRate = LVM_PSA_SPEED_MEDIUM; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 305 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 306 | /* TE Control parameters */ |
| 307 | params->TE_OperatingMode = LVM_TE_OFF; |
| 308 | params->TE_EffectLevel = 0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 309 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 310 | /* Activate the initial settings */ |
| 311 | LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, params); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 312 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 313 | LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init"); |
| 314 | if (LvmStatus != LVM_SUCCESS) return -EINVAL; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 315 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 316 | ALOGV("\tLvmBundle_init CreateInstance Successfully called " |
| 317 | "LVM_SetControlParameters\n"); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 318 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 319 | /* Set the headroom parameters */ |
| 320 | HeadroomBandDef[0].Limit_Low = 20; |
| 321 | HeadroomBandDef[0].Limit_High = 4999; |
| 322 | HeadroomBandDef[0].Headroom_Offset = 0; |
| 323 | HeadroomBandDef[1].Limit_Low = 5000; |
| 324 | HeadroomBandDef[1].Limit_High = 24000; |
| 325 | HeadroomBandDef[1].Headroom_Offset = 0; |
| 326 | HeadroomParams.pHeadroomDefinition = &HeadroomBandDef[0]; |
| 327 | HeadroomParams.Headroom_OperatingMode = LVM_HEADROOM_ON; |
| 328 | HeadroomParams.NHeadroomBands = 2; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 329 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 330 | LvmStatus = LVM_SetHeadroomParams(pContext->pBundledContext->hInstance, &HeadroomParams); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 331 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 332 | LVM_ERROR_CHECK(LvmStatus, "LVM_SetHeadroomParams", "LvmBundle_init"); |
| 333 | if (LvmStatus != LVM_SUCCESS) return -EINVAL; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 334 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 335 | ALOGV("\tLvmBundle_init CreateInstance Successfully called " |
| 336 | "LVM_SetHeadroomParams\n"); |
| 337 | ALOGV("\tLvmBundle_init End"); |
| 338 | return 0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 339 | } /* end LvmBundle_init */ |
| 340 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 341 | int lvmCreate(struct EffectContext* pContext, lvmConfigParams_t* plvmConfigParams, |
| 342 | LVM_ControlParams_t* params) { |
| 343 | int ret = 0; |
| 344 | pContext->pBundledContext = NULL; |
| 345 | pContext->pBundledContext = (BundledEffectContext*)malloc(sizeof(struct BundledEffectContext)); |
| 346 | if (NULL == pContext->pBundledContext) { |
| 347 | return -EINVAL; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 348 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 349 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 350 | pContext->pBundledContext->SessionNo = 0; |
| 351 | pContext->pBundledContext->SessionId = 0; |
| 352 | pContext->pBundledContext->hInstance = NULL; |
| 353 | pContext->pBundledContext->bVolumeEnabled = LVM_FALSE; |
| 354 | pContext->pBundledContext->bEqualizerEnabled = LVM_FALSE; |
| 355 | pContext->pBundledContext->bBassEnabled = LVM_FALSE; |
| 356 | pContext->pBundledContext->bBassTempDisabled = LVM_FALSE; |
| 357 | pContext->pBundledContext->bVirtualizerEnabled = LVM_FALSE; |
| 358 | pContext->pBundledContext->bVirtualizerTempDisabled = LVM_FALSE; |
| 359 | pContext->pBundledContext->nOutputDevice = AUDIO_DEVICE_NONE; |
| 360 | pContext->pBundledContext->nVirtualizerForcedDevice = AUDIO_DEVICE_NONE; |
| 361 | pContext->pBundledContext->NumberEffectsEnabled = 0; |
| 362 | pContext->pBundledContext->NumberEffectsCalled = 0; |
| 363 | pContext->pBundledContext->firstVolume = LVM_TRUE; |
| 364 | pContext->pBundledContext->volume = 0; |
| 365 | |
| 366 | /* Saved strength is used to return the exact strength that was used in the |
| 367 | * set to the get |
| 368 | * because we map the original strength range of 0:1000 to 1:15, and this will |
| 369 | * avoid |
| 370 | * quantisation like effect when returning |
| 371 | */ |
| 372 | pContext->pBundledContext->BassStrengthSaved = 0; |
| 373 | pContext->pBundledContext->VirtStrengthSaved = 0; |
| 374 | pContext->pBundledContext->CurPreset = PRESET_CUSTOM; |
| 375 | pContext->pBundledContext->levelSaved = 0; |
| 376 | pContext->pBundledContext->bMuteEnabled = LVM_FALSE; |
| 377 | pContext->pBundledContext->bStereoPositionEnabled = LVM_FALSE; |
| 378 | pContext->pBundledContext->positionSaved = 0; |
| 379 | pContext->pBundledContext->workBuffer = NULL; |
| 380 | pContext->pBundledContext->frameCount = -1; |
| 381 | pContext->pBundledContext->SamplesToExitCountVirt = 0; |
| 382 | pContext->pBundledContext->SamplesToExitCountBb = 0; |
| 383 | pContext->pBundledContext->SamplesToExitCountEq = 0; |
| 384 | for (int i = 0; i < FIVEBAND_NUMBANDS; i++) { |
| 385 | pContext->pBundledContext->bandGaindB[i] = EQNB_5BandSoftPresets[i]; |
| 386 | } |
| 387 | pContext->config.inputCfg.channels = plvmConfigParams->nrChannels; |
| 388 | ALOGV("\tEffectCreate - Calling LvmBundle_init"); |
| 389 | ret = LvmBundle_init(pContext, params); |
| 390 | |
| 391 | if (ret < 0) { |
| 392 | ALOGE("\tLVM_ERROR : lvmCreate() Bundle init failed"); |
| 393 | return ret; |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int lvmControl(struct EffectContext* pContext, lvmConfigParams_t* plvmConfigParams, |
| 399 | LVM_ControlParams_t* params) { |
| 400 | LVM_ReturnStatus_en LvmStatus = LVM_SUCCESS; /* Function call status */ |
| 401 | |
| 402 | /* Set the initial process parameters */ |
| 403 | /* General parameters */ |
| 404 | params->OperatingMode = LVM_MODE_ON; |
| 405 | params->SpeakerType = LVM_HEADPHONES; |
| 406 | |
| 407 | params->ChMask = plvmConfigParams->chMask; |
| 408 | params->NrChannels = plvmConfigParams->nrChannels; |
| 409 | if (params->NrChannels == 1) { |
| 410 | params->SourceFormat = LVM_MONO; |
| 411 | } else if (params->NrChannels == 2) { |
| 412 | params->SourceFormat = LVM_STEREO; |
Harish Mahendrakar | a458f10 | 2021-01-08 10:25:47 -0800 | [diff] [blame] | 413 | } else if (params->NrChannels > FCC_2 && params->NrChannels <= FCC_24) { |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 414 | params->SourceFormat = LVM_MULTICHANNEL; |
| 415 | } else { |
| 416 | return -EINVAL; |
| 417 | } |
Harish Mahendrakar | f0fb496 | 2021-02-18 14:21:45 -0800 | [diff] [blame] | 418 | params->SampleRate = lvmFsForSampleRate(plvmConfigParams->samplingFreq); |
| 419 | if (params->SampleRate == LVM_FS_INVALID) { |
| 420 | ALOGE("lvmControl invalid sampling rate %d", plvmConfigParams->samplingFreq); |
| 421 | return -EINVAL; |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 422 | } |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 423 | |
| 424 | /* Concert Sound parameters */ |
| 425 | params->VirtualizerOperatingMode = plvmConfigParams->csEnable; |
| 426 | params->VirtualizerType = LVM_CONCERTSOUND; |
| 427 | params->VirtualizerReverbLevel = 100; |
| 428 | params->CS_EffectLevel = LVM_CS_EFFECT_NONE; |
| 429 | |
| 430 | /* N-Band Equaliser parameters */ |
| 431 | const int eqPresetLevel = plvmConfigParams->eqPresetLevel; |
| 432 | LVM_EQNB_BandDef_t BandDefs[MAX_NUM_BANDS]; /* Equaliser band definitions */ |
| 433 | for (int i = 0; i < FIVEBAND_NUMBANDS; i++) { |
| 434 | BandDefs[i].Frequency = EQNB_5BandPresetsFrequencies[i]; |
| 435 | BandDefs[i].QFactor = EQNB_5BandPresetsQFactors[i]; |
| 436 | BandDefs[i].Gain = EQNB_5BandSoftPresets[(FIVEBAND_NUMBANDS * eqPresetLevel) + i]; |
| 437 | } |
| 438 | params->EQNB_OperatingMode = plvmConfigParams->eqEnable; |
| 439 | // Caution: raw pointer to stack data, stored in instance by LVM_SetControlParameters. |
| 440 | params->pEQNB_BandDefinition = &BandDefs[0]; |
| 441 | |
| 442 | /* Volume Control parameters */ |
| 443 | params->VC_EffectLevel = 0; |
| 444 | params->VC_Balance = plvmConfigParams->vcBal; |
| 445 | |
| 446 | /* Treble Enhancement parameters */ |
| 447 | params->TE_OperatingMode = plvmConfigParams->trebleEnable; |
| 448 | |
| 449 | /* PSA Control parameters */ |
| 450 | params->PSA_Enable = LVM_PSA_ON; |
| 451 | |
| 452 | /* Bass Enhancement parameters */ |
| 453 | params->BE_OperatingMode = plvmConfigParams->bassEnable; |
| 454 | |
| 455 | /* Activate the initial settings */ |
| 456 | LvmStatus = LVM_SetControlParameters(pContext->pBundledContext->hInstance, params); |
| 457 | |
| 458 | LVM_ERROR_CHECK(LvmStatus, "LVM_SetControlParameters", "LvmBundle_init"); |
| 459 | if (LvmStatus != LVM_SUCCESS) return -EINVAL; |
| 460 | |
| 461 | LvmStatus = LVM_ApplyNewSettings(pContext->pBundledContext->hInstance); |
| 462 | |
| 463 | if (LvmStatus != LVM_SUCCESS) return -EINVAL; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | int lvmExecute(float* floatIn, float* floatOut, struct EffectContext* pContext, |
| 469 | lvmConfigParams_t* plvmConfigParams) { |
| 470 | const int frameLength = plvmConfigParams->frameLength; |
| 471 | return LVM_Process(pContext->pBundledContext->hInstance, /* Instance handle */ |
| 472 | floatIn, /* Input buffer */ |
| 473 | floatOut, /* Output buffer */ |
| 474 | (LVM_UINT16)frameLength, /* Number of samples to read */ |
| 475 | 0); /* Audio Time */ |
| 476 | } |
| 477 | |
| 478 | int lvmMainProcess(EffectContext* pContext, LVM_ControlParams_t* pParams, |
| 479 | lvmConfigParams_t* plvmConfigParams, FILE* finp, FILE* fout) { |
| 480 | int errCode = lvmControl(pContext, plvmConfigParams, pParams); |
| 481 | if (errCode) { |
| 482 | ALOGE("Error: lvmControl returned with %d\n", errCode); |
| 483 | return errCode; |
| 484 | } |
| 485 | |
| 486 | const int channelCount = plvmConfigParams->nrChannels; |
| 487 | const int frameLength = plvmConfigParams->frameLength; |
| 488 | const int frameSize = channelCount * sizeof(float); // processing size |
| 489 | const int ioChannelCount = plvmConfigParams->fChannels; |
| 490 | const int ioFrameSize = ioChannelCount * sizeof(short); // file load size |
| 491 | const int maxChannelCount = std::max(channelCount, ioChannelCount); |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 492 | |
| 493 | std::vector<short> in(frameLength * maxChannelCount); |
| 494 | std::vector<short> out(frameLength * maxChannelCount); |
| 495 | std::vector<float> floatIn(frameLength * channelCount); |
Rivukanta Bhattacharya | 59c2d1c | 2021-01-20 20:56:15 +0530 | [diff] [blame] | 496 | std::vector<float> floatOut(frameLength * channelCount); |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 497 | |
| 498 | int frameCounter = 0; |
| 499 | while (fread(in.data(), ioFrameSize, frameLength, finp) == (size_t)frameLength) { |
| 500 | if (ioChannelCount != channelCount) { |
| 501 | adjust_channels(in.data(), ioChannelCount, in.data(), channelCount, sizeof(short), |
| 502 | frameLength * ioFrameSize); |
| 503 | } |
| 504 | memcpy_to_float_from_i16(floatIn.data(), in.data(), frameLength * channelCount); |
| 505 | |
| 506 | // Mono mode will replicate the first channel to all other channels. |
| 507 | // This ensures all audio channels are identical. This is useful for testing |
| 508 | // Bass Boost, which extracts a mono signal for processing. |
| 509 | if (plvmConfigParams->monoMode && channelCount > 1) { |
| 510 | for (int i = 0; i < frameLength; ++i) { |
| 511 | auto* fp = &floatIn[i * channelCount]; |
| 512 | std::fill(fp + 1, fp + channelCount, *fp); // replicate ch 0 |
| 513 | } |
| 514 | } |
| 515 | #ifndef BYPASS_EXEC |
| 516 | errCode = lvmExecute(floatIn.data(), floatOut.data(), pContext, plvmConfigParams); |
| 517 | if (errCode) { |
| 518 | printf("\nError: lvmExecute returned with %d\n", errCode); |
| 519 | return errCode; |
| 520 | } |
| 521 | |
| 522 | (void)frameSize; // eliminate warning |
| 523 | #else |
| 524 | memcpy(floatOut.data(), floatIn.data(), frameLength * frameSize); |
| 525 | #endif |
| 526 | memcpy_to_i16_from_float(out.data(), floatOut.data(), frameLength * channelCount); |
| 527 | if (ioChannelCount != channelCount) { |
| 528 | adjust_channels(out.data(), channelCount, out.data(), ioChannelCount, sizeof(short), |
| 529 | frameLength * channelCount * sizeof(short)); |
| 530 | } |
| 531 | (void)fwrite(out.data(), ioFrameSize, frameLength, fout); |
| 532 | frameCounter += frameLength; |
| 533 | } |
| 534 | printf("frameCounter: [%d]\n", frameCounter); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int main(int argc, const char* argv[]) { |
| 539 | if (argc == 1) { |
| 540 | printUsage(); |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | lvmConfigParams_t lvmConfigParams{}; // default initialize |
| 545 | const char* infile = nullptr; |
| 546 | const char* outfile = nullptr; |
| 547 | |
| 548 | for (int i = 1; i < argc; i++) { |
| 549 | printf("%s ", argv[i]); |
| 550 | if (!strncmp(argv[i], "-i:", 3)) { |
| 551 | infile = argv[i] + 3; |
| 552 | } else if (!strncmp(argv[i], "-o:", 3)) { |
| 553 | outfile = argv[i] + 3; |
| 554 | } else if (!strncmp(argv[i], "-fs:", 4)) { |
| 555 | const int samplingFreq = atoi(argv[i] + 4); |
| 556 | if (samplingFreq != 8000 && samplingFreq != 11025 && samplingFreq != 12000 && |
| 557 | samplingFreq != 16000 && samplingFreq != 22050 && samplingFreq != 24000 && |
| 558 | samplingFreq != 32000 && samplingFreq != 44100 && samplingFreq != 48000 && |
| 559 | samplingFreq != 88200 && samplingFreq != 96000 && samplingFreq != 176400 && |
| 560 | samplingFreq != 192000) { |
| 561 | printf("Error: Unsupported Sampling Frequency : %d\n", samplingFreq); |
| 562 | return -1; |
| 563 | } |
| 564 | lvmConfigParams.samplingFreq = samplingFreq; |
| 565 | } else if (!strncmp(argv[i], "-chMask:", 8)) { |
| 566 | const int chMaskConfigIdx = atoi(argv[i] + 8); |
| 567 | if (chMaskConfigIdx < 0 || (size_t)chMaskConfigIdx >= std::size(lvmConfigChMask)) { |
| 568 | ALOGE("\nError: Unsupported Channel Mask : %d\n", chMaskConfigIdx); |
| 569 | return -1; |
| 570 | } |
| 571 | const audio_channel_mask_t chMask = lvmConfigChMask[chMaskConfigIdx]; |
| 572 | lvmConfigParams.chMask = chMask; |
| 573 | lvmConfigParams.nrChannels = audio_channel_count_from_out_mask(chMask); |
| 574 | } else if (!strncmp(argv[i], "-vcBal:", 7)) { |
| 575 | const int vcBalance = atoi(argv[i] + 7); |
| 576 | if (vcBalance > 96 || vcBalance < -96) { |
| 577 | ALOGE("\nError: Unsupported volume balance value: %d\n", vcBalance); |
| 578 | } |
| 579 | lvmConfigParams.vcBal = vcBalance; |
| 580 | } else if (!strncmp(argv[i], "-fch:", 5)) { |
| 581 | const int fChannels = atoi(argv[i] + 5); |
| 582 | if (fChannels > 8 || fChannels < 1) { |
| 583 | printf("Error: Unsupported number of file channels : %d\n", fChannels); |
| 584 | return -1; |
| 585 | } |
| 586 | lvmConfigParams.fChannels = fChannels; |
| 587 | } else if (!strcmp(argv[i], "-M")) { |
| 588 | lvmConfigParams.monoMode = true; |
| 589 | } else if (!strncmp(argv[i], "-basslvl:", 9)) { |
| 590 | const int bassEffectLevel = atoi(argv[i] + 9); |
| 591 | if (bassEffectLevel > LVM_BE_MAX_EFFECTLEVEL || |
| 592 | bassEffectLevel < LVM_BE_MIN_EFFECTLEVEL) { |
| 593 | printf("Error: Unsupported Bass Effect Level : %d\n", bassEffectLevel); |
| 594 | printUsage(); |
| 595 | return -1; |
| 596 | } |
| 597 | lvmConfigParams.bassEffectLevel = bassEffectLevel; |
| 598 | } else if (!strncmp(argv[i], "-eqPreset:", 10)) { |
| 599 | const int eqPresetLevel = atoi(argv[i] + 10); |
| 600 | const int numPresetLvls = std::size(gEqualizerPresets); |
| 601 | if (eqPresetLevel >= numPresetLvls || eqPresetLevel < 0) { |
| 602 | printf("Error: Unsupported Equalizer Preset : %d\n", eqPresetLevel); |
| 603 | printUsage(); |
| 604 | return -1; |
| 605 | } |
| 606 | lvmConfigParams.eqPresetLevel = eqPresetLevel; |
| 607 | } else if (!strcmp(argv[i], "-bE")) { |
| 608 | lvmConfigParams.bassEnable = LVM_BE_ON; |
| 609 | } else if (!strcmp(argv[i], "-eqE")) { |
| 610 | lvmConfigParams.eqEnable = LVM_EQNB_ON; |
| 611 | } else if (!strcmp(argv[i], "-tE")) { |
| 612 | lvmConfigParams.trebleEnable = LVM_TE_ON; |
| 613 | } else if (!strcmp(argv[i], "-csE")) { |
| 614 | lvmConfigParams.csEnable = LVM_MODE_ON; |
| 615 | } else if (!strcmp(argv[i], "-h")) { |
| 616 | printUsage(); |
| 617 | return 0; |
Andy Hung | b144b4b | 2018-12-20 14:15:49 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 620 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 621 | if (infile == nullptr || outfile == nullptr) { |
| 622 | printf("Error: missing input/output files\n"); |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 623 | printUsage(); |
| 624 | return -1; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 625 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 626 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 627 | FILE* finp = fopen(infile, "rb"); |
| 628 | if (finp == nullptr) { |
| 629 | printf("Cannot open input file %s", infile); |
| 630 | return -1; |
| 631 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 632 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 633 | FILE* fout = fopen(outfile, "wb"); |
| 634 | if (fout == nullptr) { |
| 635 | printf("Cannot open output file %s", outfile); |
| 636 | fclose(finp); |
| 637 | return -1; |
| 638 | } |
Saketh Sathuvalli | ab18bac | 2019-01-24 17:15:10 +0530 | [diff] [blame] | 639 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 640 | EffectContext context; |
| 641 | LVM_ControlParams_t params; |
| 642 | int errCode = lvmCreate(&context, &lvmConfigParams, ¶ms); |
| 643 | if (errCode == 0) { |
| 644 | errCode = lvmMainProcess(&context, ¶ms, &lvmConfigParams, finp, fout); |
| 645 | if (errCode != 0) { |
| 646 | printf("Error: lvmMainProcess returned with the error: %d", errCode); |
| 647 | } |
| 648 | } else { |
| 649 | printf("Error: lvmCreate returned with the error: %d", errCode); |
| 650 | } |
Saketh Sathuvalli | ab18bac | 2019-01-24 17:15:10 +0530 | [diff] [blame] | 651 | fclose(finp); |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 652 | fclose(fout); |
| 653 | /* Free the allocated buffers */ |
| 654 | if (context.pBundledContext != nullptr) { |
| 655 | if (context.pBundledContext->hInstance != nullptr) { |
| 656 | LVM_DelInstanceHandle(&context.pBundledContext->hInstance); |
| 657 | } |
| 658 | free(context.pBundledContext); |
Saketh Sathuvalli | ab18bac | 2019-01-24 17:15:10 +0530 | [diff] [blame] | 659 | } |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 660 | |
Harish Mahendrakar | 0bbbe07 | 2020-10-01 23:28:48 +0530 | [diff] [blame] | 661 | if (errCode) { |
| 662 | return -1; |
| 663 | } |
| 664 | return 0; |
Saketh Sathuvalli | b99e1bc | 2018-02-21 17:10:34 +0530 | [diff] [blame] | 665 | } |