• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <inttypes.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <vector>
20 
21 #include <audio_effects/effect_downmix.h>
22 #include <audio_utils/channels.h>
23 #include <audio_utils/primitives.h>
24 #include <log/log.h>
25 #include <system/audio.h>
26 
27 #include "EffectDownmix.h"
28 #define FRAME_LENGTH 256
29 #define MAX_NUM_CHANNELS 8
30 
31 struct downmix_cntxt_s {
32   effect_handle_t handle;
33   effect_config_t config;
34 
35   int numFileChannels;
36   int numProcessChannels;
37 };
38 
39 extern audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM;
40 
printUsage()41 void printUsage() {
42   printf("\nUsage:");
43   printf("\n     downmixtest <input_file> <out_file> [options]\n");
44   printf("\nwhere,");
45   printf("\n     <input_file>  is the input file name");
46   printf("\n                  on which LVM effects are applied");
47   printf("\n     <output_file> processed output file");
48   printf("\n     and options are mentioned below");
49   printf("\n");
50   printf("\n      -h");
51   printf("\n           Prints this usage information");
52   printf("\n");
53   printf("\n     -ch_fmt:<format_of_input_audio>");
54   printf("\n         0:AUDIO_CHANNEL_OUT_7POINT1(default)");
55   printf("\n         1:AUDIO_CHANNEL_OUT_5POINT1_SIDE");
56   printf("\n         2:AUDIO_CHANNEL_OUT_5POINT1_BACK");
57   printf("\n         3:AUDIO_CHANNEL_OUT_QUAD_SIDE");
58   printf("\n         4:AUDIO_CHANNEL_OUT_QUAD_BACK");
59   printf("\n");
60   printf("\n     -fch:<file_channels> (1 through 8)");
61   printf("\n");
62 }
63 
DownmixDefaultConfig(effect_config_t * pConfig)64 int32_t DownmixDefaultConfig(effect_config_t *pConfig) {
65   pConfig->inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
66   pConfig->inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
67   pConfig->inputCfg.channels = AUDIO_CHANNEL_OUT_7POINT1;
68   pConfig->inputCfg.bufferProvider.getBuffer = nullptr;
69   pConfig->inputCfg.bufferProvider.releaseBuffer = nullptr;
70   pConfig->inputCfg.bufferProvider.cookie = nullptr;
71   pConfig->inputCfg.mask = EFFECT_CONFIG_ALL;
72 
73   pConfig->inputCfg.samplingRate = 44100;
74   pConfig->outputCfg.samplingRate = pConfig->inputCfg.samplingRate;
75 
76   // set a default value for the access mode, but should be overwritten by caller
77   pConfig->outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
78   pConfig->outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
79   pConfig->outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
80   pConfig->outputCfg.bufferProvider.getBuffer = nullptr;
81   pConfig->outputCfg.bufferProvider.releaseBuffer = nullptr;
82   pConfig->outputCfg.bufferProvider.cookie = nullptr;
83   pConfig->outputCfg.mask = EFFECT_CONFIG_ALL;
84 
85   return 0;
86 }
87 
DownmixConfiureAndEnable(downmix_cntxt_s * pDescriptor)88 int32_t DownmixConfiureAndEnable(downmix_cntxt_s *pDescriptor) {
89   effect_handle_t effectHandle = pDescriptor->handle;
90   const struct effect_interface_s *Downmix_api = *effectHandle;
91   int32_t err = 0;
92   uint32_t replySize = (uint32_t)sizeof(err);
93 
94   err = (Downmix_api->command)(effectHandle, EFFECT_CMD_SET_CONFIG,
95                                sizeof(effect_config_t), &(pDescriptor->config),
96                                &replySize, &err);
97   if (err != 0) {
98     ALOGE("Downmix command to configure returned an error %d", err);
99     return err;
100   }
101 
102   err = ((Downmix_api->command))(effectHandle, EFFECT_CMD_ENABLE, 0, nullptr,
103                                  &replySize, &err);
104   if (err != 0) {
105     ALOGE("Downmix command to enable effect returned an error %d", err);
106     return err;
107   }
108   return 0;
109 }
110 
DownmixExecute(downmix_cntxt_s * pDescriptor,FILE * finp,FILE * fout)111 int32_t DownmixExecute(downmix_cntxt_s *pDescriptor, FILE *finp,
112                        FILE *fout) {
113   effect_handle_t effectHandle = pDescriptor->handle;
114   const struct effect_interface_s *Downmix_api = *effectHandle;
115 
116   const int numFileChannels = pDescriptor->numFileChannels;
117   const int numProcessChannels = pDescriptor->numProcessChannels;
118   const int fileFrameSize = numFileChannels * sizeof(short);
119   const unsigned int outputChannels =
120       audio_channel_count_from_out_mask(AUDIO_CHANNEL_OUT_STEREO);
121 
122   std::vector<float> outFloat(FRAME_LENGTH * MAX_NUM_CHANNELS);
123   std::vector<float> inFloat(FRAME_LENGTH * MAX_NUM_CHANNELS);
124 
125   audio_buffer_t inbuffer, outbuffer;
126   inbuffer.f32 = inFloat.data();
127   outbuffer.f32 = outFloat.data();
128   inbuffer.frameCount = FRAME_LENGTH;
129   outbuffer.frameCount = FRAME_LENGTH;
130 
131   audio_buffer_t *pinbuf, *poutbuf;
132   pinbuf = &inbuffer;
133   poutbuf = &outbuffer;
134 
135   int frameCounter = 0;
136   std::vector<short> inS16(FRAME_LENGTH * MAX_NUM_CHANNELS);
137   std::vector<short> outS16(FRAME_LENGTH * MAX_NUM_CHANNELS);
138 
139   while (fread(inS16.data(), fileFrameSize, FRAME_LENGTH, finp) ==
140          FRAME_LENGTH) {
141     if (numFileChannels != numProcessChannels) {
142       adjust_channels(inS16.data(), numFileChannels, inS16.data(),
143                       numProcessChannels, sizeof(short),
144                       FRAME_LENGTH * fileFrameSize);
145     }
146 
147     memcpy_to_float_from_i16(inFloat.data(), inS16.data(),
148                              FRAME_LENGTH * numProcessChannels);
149 
150     const int32_t err = (Downmix_api->process)(effectHandle, pinbuf, poutbuf);
151     if (err != 0) {
152       ALOGE("DownmixProcess returned an error %d", err);
153       return -1;
154     }
155 
156     memcpy_to_i16_from_float(outS16.data(), outFloat.data(),
157                              FRAME_LENGTH * outputChannels);
158     fwrite(outS16.data(), sizeof(short), (FRAME_LENGTH * outputChannels),
159            fout);
160     frameCounter++;
161   }
162   printf("frameCounter: [%d]\n", frameCounter);
163   return 0;
164 }
165 
DowmixMainProcess(downmix_cntxt_s * pDescriptor,FILE * finp,FILE * fout)166 int32_t DowmixMainProcess(downmix_cntxt_s *pDescriptor, FILE *finp,
167                           FILE *fout) {
168   effect_handle_t *effectHandle = &pDescriptor->handle;
169   int32_t sessionId = 0, ioId = 0;
170   const effect_uuid_t downmix_uuid = {
171       0x93f04452, 0xe4fe, 0x41cc, 0x91f9, {0xe4, 0x75, 0xb6, 0xd1, 0xd6, 0x9f}};
172 
173   int32_t err = AUDIO_EFFECT_LIBRARY_INFO_SYM.create_effect(
174       &downmix_uuid, sessionId, ioId,
175       effectHandle);
176   if (err != 0) {
177     ALOGE("DownmixLib_Create returned an error %d", err);
178     return -1;
179   }
180 
181   // Passing the init config for time being.
182   err = DownmixConfiureAndEnable(pDescriptor);
183   if (err != 0) {
184     ALOGE("DownmixConfigureAndEnable returned an error %d", err);
185     return -1;
186   }
187   // execute call for downmix.
188   err = DownmixExecute(pDescriptor, finp, fout);
189   if (err != 0) {
190     ALOGE("DownmixExecute returned an error %d", err);
191     return -1;
192   }
193   // Release the library function.
194   err = AUDIO_EFFECT_LIBRARY_INFO_SYM.release_effect(*effectHandle);
195   if (err != 0) {
196     ALOGE("DownmixRelease returned an error %d", err);
197     return -1;
198   }
199   return 0;
200 }
201 
main(int argc,const char * argv[])202 int main(int argc, const char *argv[]) {
203   int numFileChannels = 1, numProcessChannels = 8;
204   downmix_cntxt_s descriptor = {};
205   DownmixDefaultConfig(&(descriptor.config));
206 
207   const char *infile = nullptr;
208   const char *outfile = nullptr;
209   for (int i = 1; i < argc; i++) {
210     printf("%s ", argv[i]);
211     if (argv[i][0] != '-') {
212       if (infile == nullptr) {
213         infile = argv[i];
214       } else if (outfile == nullptr) {
215         outfile = argv[i];
216       } else {
217         printUsage();
218         return -1;
219       }
220     } else if (!strncmp(argv[i], "-fs:", 4)) {
221       // Add a check for all the supported streams.
222       const int samplingFreq = atoi(argv[i] + 4);
223       if (samplingFreq != 8000 && samplingFreq != 11025 &&
224           samplingFreq != 12000 && samplingFreq != 16000 &&
225           samplingFreq != 22050 && samplingFreq != 24000 &&
226           samplingFreq != 32000 && samplingFreq != 44100 &&
227           samplingFreq != 48000 && samplingFreq != 88200 &&
228           samplingFreq != 96000 && samplingFreq != 176400 &&
229           samplingFreq != 192000) {
230         printf("Unsupported Sampling Frequency : %d", samplingFreq);
231         printUsage();
232         return -1;
233       }
234 
235       descriptor.config.inputCfg.samplingRate = samplingFreq;
236       descriptor.config.outputCfg.samplingRate = samplingFreq;
237     } else if (!strncmp(argv[i], "-ch_fmt:", 8)) {
238       const int format = atoi(argv[i] + 8);
239       uint32_t *audioType = &descriptor.config.inputCfg.channels;
240       switch (format) {
241         case 0:
242           *audioType = AUDIO_CHANNEL_OUT_7POINT1;
243           break;
244         case 1:
245           *audioType = AUDIO_CHANNEL_OUT_5POINT1_SIDE;
246           break;
247         case 2:
248           *audioType = AUDIO_CHANNEL_OUT_5POINT1_BACK;
249           break;
250         case 3:
251           *audioType = AUDIO_CHANNEL_OUT_QUAD_SIDE;
252           break;
253         case 4:
254           *audioType = AUDIO_CHANNEL_OUT_QUAD_BACK;
255           break;
256         default:
257           *audioType = AUDIO_CHANNEL_OUT_7POINT1;
258           break;
259       }
260       descriptor.numProcessChannels =
261           audio_channel_count_from_out_mask(*audioType);
262     } else if (!strncmp(argv[i], "-fch:", 5)) {
263       const int fChannels = atoi(argv[i] + 5);
264       if (fChannels > 8 || fChannels < 1) {
265         printf("Unsupported number of file channels : %d", fChannels);
266         printUsage();
267         return -1;
268       }
269       descriptor.numFileChannels = fChannels;
270 
271     } else if (!strncmp(argv[i], "-h", 2)) {
272       printUsage();
273       return 0;
274     }
275   }
276 
277   if (/*infile == nullptr || */ outfile == nullptr) {
278     printUsage();
279     return -1;
280   }
281 
282   FILE *finp = fopen(infile, "rb");
283   if (finp == nullptr) {
284     printf("Cannot open input file %s", infile);
285     return -1;
286   }
287   FILE *fout = fopen(outfile, "wb");
288   if (fout == nullptr) {
289     printf("Cannot open output file %s", outfile);
290     fclose(finp);
291     return -1;
292   }
293 
294   const int err = DowmixMainProcess(&descriptor, finp, fout);
295   // close input and output files.
296   fclose(finp);
297   fclose(fout);
298   if (err != 0) {
299     printf("Error: %d\n", err);
300   }
301   return err;
302 }
303