1 /*
2  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright (C) 2013 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_ssr"
21 /*#define LOG_NDEBUG 0*/
22 #define LOG_NDDEBUG 0
23 
24 #include <errno.h>
25 #include <cutils/properties.h>
26 #include <stdlib.h>
27 #include <dlfcn.h>
28 #include <cutils/str_parms.h>
29 #include <cutils/log.h>
30 
31 #include "audio_hw.h"
32 #include "platform.h"
33 #include "platform_api.h"
34 #include "surround_filters_interface.h"
35 
36 #ifdef SSR_ENABLED
37 #define COEFF_ARRAY_SIZE            4
38 #define FILT_SIZE                   ((512+1)* 6)  /* # ((FFT bins)/2+1)*numOutputs */
39 #define SSR_CHANNEL_INPUT_NUM       4
40 #define SSR_CHANNEL_OUTPUT_NUM      6
41 #define SSR_PERIOD_COUNT            8
42 #define SSR_PERIOD_SIZE             512
43 #define SSR_INPUT_FRAME_SIZE        (SSR_PERIOD_SIZE * SSR_PERIOD_COUNT)
44 
45 #define SURROUND_FILE_1R "/system/etc/surround_sound/filter1r.pcm"
46 #define SURROUND_FILE_2R "/system/etc/surround_sound/filter2r.pcm"
47 #define SURROUND_FILE_3R "/system/etc/surround_sound/filter3r.pcm"
48 #define SURROUND_FILE_4R "/system/etc/surround_sound/filter4r.pcm"
49 
50 #define SURROUND_FILE_1I "/system/etc/surround_sound/filter1i.pcm"
51 #define SURROUND_FILE_2I "/system/etc/surround_sound/filter2i.pcm"
52 #define SURROUND_FILE_3I "/system/etc/surround_sound/filter3i.pcm"
53 #define SURROUND_FILE_4I "/system/etc/surround_sound/filter4i.pcm"
54 
55 #define LIB_SURROUND_PROC       "libsurround_proc.so"
56 
57 typedef int  (*surround_filters_init_t)(void *, int, int, Word16 **,
58                                         Word16 **, int, int, int, Profiler *);
59 typedef void (*surround_filters_release_t)(void *);
60 typedef int  (*surround_filters_set_channel_map_t)(void *, const int *);
61 typedef void (*surround_filters_intl_process_t)(void *, Word16 *, Word16 *);
62 
63 struct ssr_module {
64     FILE                *fp_4ch;
65     FILE                *fp_6ch;
66     Word16             **real_coeffs;
67     Word16             **imag_coeffs;
68     void                *surround_obj;
69     Word16             *surround_raw_buffer;
70     bool                is_ssr_enabled;
71 
72     void *surround_filters_handle;
73     surround_filters_init_t surround_filters_init;
74     surround_filters_release_t surround_filters_release;
75     surround_filters_set_channel_map_t surround_filters_set_channel_map;
76     surround_filters_intl_process_t surround_filters_intl_process;
77 };
78 
79 static struct ssr_module ssrmod = {
80     .fp_4ch = NULL,
81     .fp_6ch = NULL,
82     .real_coeffs = NULL,
83     .imag_coeffs = NULL,
84     .surround_obj = NULL,
85     .surround_raw_buffer = NULL,
86     .is_ssr_enabled = 0,
87 
88     .surround_filters_handle = NULL,
89     .surround_filters_init = NULL,
90     .surround_filters_release = NULL,
91     .surround_filters_set_channel_map = NULL,
92     .surround_filters_intl_process = NULL,
93 };
94 
95 /* Use AAC/DTS channel mapping as default channel mapping: C,FL,FR,Ls,Rs,LFE */
96 static const int chan_map[] = { 1, 2, 4, 3, 0, 5};
97 
98 /* Rotine to read coeffs from File and updates real and imaginary
99    coeff array member variable */
ssr_read_coeffs_from_file()100 static int32_t ssr_read_coeffs_from_file()
101 {
102     FILE    *flt1r;
103     FILE    *flt2r;
104     FILE    *flt3r;
105     FILE    *flt4r;
106     FILE    *flt1i;
107     FILE    *flt2i;
108     FILE    *flt3i;
109     FILE    *flt4i;
110     int i;
111 
112     if ( (flt1r = fopen(SURROUND_FILE_1R, "rb")) == NULL ) {
113         ALOGE("%s: Cannot open filter co-efficient "
114               "file %s", __func__, SURROUND_FILE_1R);
115         return -EINVAL;
116     }
117 
118     if ( (flt2r = fopen(SURROUND_FILE_2R, "rb")) == NULL ) {
119         ALOGE("%s: Cannot open filter "
120               "co-efficient file %s", __func__, SURROUND_FILE_2R);
121         return -EINVAL;
122     }
123 
124     if ( (flt3r = fopen(SURROUND_FILE_3R, "rb")) == NULL ) {
125         ALOGE("%s: Cannot open filter "
126               "co-efficient file %s", __func__, SURROUND_FILE_3R);
127         return  -EINVAL;
128     }
129 
130     if ( (flt4r = fopen(SURROUND_FILE_4R, "rb")) == NULL ) {
131         ALOGE("%s: Cannot open filter "
132               "co-efficient file %s", __func__, SURROUND_FILE_4R);
133         return  -EINVAL;
134     }
135 
136     if ( (flt1i = fopen(SURROUND_FILE_1I, "rb")) == NULL ) {
137         ALOGE("%s: Cannot open filter "
138               "co-efficient file %s", __func__, SURROUND_FILE_1I);
139         return -EINVAL;
140     }
141 
142     if ( (flt2i = fopen(SURROUND_FILE_2I, "rb")) == NULL ) {
143         ALOGE("%s: Cannot open filter "
144               "co-efficient file %s", __func__, SURROUND_FILE_2I);
145         return -EINVAL;
146     }
147 
148     if ( (flt3i = fopen(SURROUND_FILE_3I, "rb")) == NULL ) {
149         ALOGE("%s: Cannot open filter "
150               "co-efficient file %s", __func__, SURROUND_FILE_3I);
151         return -EINVAL;
152     }
153 
154     if ( (flt4i = fopen(SURROUND_FILE_4I, "rb")) == NULL ) {
155         ALOGE("%s: Cannot open filter "
156               "co-efficient file %s", __func__, SURROUND_FILE_4I);
157         return -EINVAL;
158     }
159     ALOGV("%s: readCoeffsFromFile all filter "
160           "files opened", __func__);
161 
162     for (i=0; i<COEFF_ARRAY_SIZE; i++) {
163         ssrmod.real_coeffs[i] = (Word16 *)calloc(FILT_SIZE, sizeof(Word16));
164     }
165     for (i=0; i<COEFF_ARRAY_SIZE; i++) {
166         ssrmod.imag_coeffs[i] = (Word16 *)calloc(FILT_SIZE, sizeof(Word16));
167     }
168 
169     /* Read real co-efficients */
170     if (NULL != ssrmod.real_coeffs[0]) {
171         fread(ssrmod.real_coeffs[0], sizeof(int16), FILT_SIZE, flt1r);
172     }
173     if (NULL != ssrmod.real_coeffs[0]) {
174         fread(ssrmod.real_coeffs[1], sizeof(int16), FILT_SIZE, flt2r);
175     }
176     if (NULL != ssrmod.real_coeffs[0]) {
177         fread(ssrmod.real_coeffs[2], sizeof(int16), FILT_SIZE, flt3r);
178     }
179     if (NULL != ssrmod.real_coeffs[0]) {
180         fread(ssrmod.real_coeffs[3], sizeof(int16), FILT_SIZE, flt4r);
181     }
182 
183     /* read imaginary co-efficients */
184     if (NULL != ssrmod.imag_coeffs[0]) {
185         fread(ssrmod.imag_coeffs[0], sizeof(int16), FILT_SIZE, flt1i);
186     }
187     if (NULL != ssrmod.imag_coeffs[0]) {
188         fread(ssrmod.imag_coeffs[1], sizeof(int16), FILT_SIZE, flt2i);
189     }
190     if (NULL != ssrmod.imag_coeffs[0]) {
191         fread(ssrmod.imag_coeffs[2], sizeof(int16), FILT_SIZE, flt3i);
192     }
193     if (NULL != ssrmod.imag_coeffs[0]) {
194         fread(ssrmod.imag_coeffs[3], sizeof(int16), FILT_SIZE, flt4i);
195     }
196 
197     fclose(flt1r);
198     fclose(flt2r);
199     fclose(flt3r);
200     fclose(flt4r);
201     fclose(flt1i);
202     fclose(flt2i);
203     fclose(flt3i);
204     fclose(flt4i);
205 
206     return 0;
207 }
208 
ssr_init_surround_sound_lib(unsigned long buffersize)209 static int32_t ssr_init_surround_sound_lib(unsigned long buffersize)
210 {
211     /* sub_woofer channel assignment: default as first
212        microphone input channel */
213     int sub_woofer = 0;
214     /* frequency upper bound for sub_woofer:
215        frequency=(low_freq-1)/FFT_SIZE*samplingRate, default as 4 */
216     int low_freq = 4;
217     /* frequency upper bound for spatial processing:
218        frequency=(high_freq-1)/FFT_SIZE*samplingRate, default as 100 */
219     int high_freq = 100;
220     int i, ret = 0;
221 
222     if ( ssrmod.surround_obj ) {
223         ALOGE("%s: ola filter library is already initialized", __func__);
224         return 0;
225     }
226 
227     /* Allocate memory for input buffer */
228     ssrmod.surround_raw_buffer = (Word16 *) calloc(buffersize,
229                                               sizeof(Word16));
230     if ( !ssrmod.surround_raw_buffer ) {
231        ALOGE("%s: Memory allocation failure. Not able to allocate "
232              "memory for surroundInputBuffer", __func__);
233        goto init_fail;
234     }
235 
236     /* Allocate memory for real and imag coeffs array */
237     ssrmod.real_coeffs = (Word16 **) calloc(COEFF_ARRAY_SIZE, sizeof(Word16 *));
238     if ( !ssrmod.real_coeffs ) {
239         ALOGE("%s: Memory allocation failure during real "
240               "Coefficient array", __func__);
241         goto init_fail;
242     }
243 
244     ssrmod.imag_coeffs = (Word16 **) calloc(COEFF_ARRAY_SIZE, sizeof(Word16 *));
245     if ( !ssrmod.imag_coeffs ) {
246         ALOGE("%s: Memory allocation failure during imaginary "
247               "Coefficient array", __func__);
248         goto init_fail;
249     }
250 
251     if( ssr_read_coeffs_from_file() != 0) {
252         ALOGE("%s: Error while loading coeffs from file", __func__);
253         goto init_fail;
254     }
255 
256     ssrmod.surround_filters_handle = dlopen(LIB_SURROUND_PROC, RTLD_NOW);
257     if (ssrmod.surround_filters_handle == NULL) {
258         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_SURROUND_PROC);
259     } else {
260         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_SURROUND_PROC);
261         ssrmod.surround_filters_init = (surround_filters_init_t)
262         dlsym(ssrmod.surround_filters_handle, "surround_filters_init");
263 
264         ssrmod.surround_filters_release = (surround_filters_release_t)
265          dlsym(ssrmod.surround_filters_handle, "surround_filters_release");
266 
267         ssrmod.surround_filters_set_channel_map = (surround_filters_set_channel_map_t)
268          dlsym(ssrmod.surround_filters_handle, "surround_filters_set_channel_map");
269 
270         ssrmod.surround_filters_intl_process = (surround_filters_intl_process_t)
271         dlsym(ssrmod.surround_filters_handle, "surround_filters_intl_process");
272 
273         if (!ssrmod.surround_filters_init ||
274             !ssrmod.surround_filters_release ||
275             !ssrmod.surround_filters_set_channel_map ||
276             !ssrmod.surround_filters_intl_process){
277             ALOGW("%s: Could not find the one of the symbols from %s",
278                   __func__, LIB_SURROUND_PROC);
279             goto init_fail;
280         }
281     }
282 
283     /* calculate the size of data to allocate for surround_obj */
284     ret = ssrmod.surround_filters_init(NULL,
285                   6, // Num output channel
286                   4,     // Num input channel
287                   ssrmod.real_coeffs,       // Coeffs hardcoded in header
288                   ssrmod.imag_coeffs,       // Coeffs hardcoded in header
289                   sub_woofer,
290                   low_freq,
291                   high_freq,
292                   NULL);
293 
294     if ( ret > 0 ) {
295         ALOGV("%s: Allocating surroundObj size is %d", __func__, ret);
296         ssrmod.surround_obj = (void *)malloc(ret);
297         if (NULL != ssrmod.surround_obj) {
298             memset(ssrmod.surround_obj,0,ret);
299             /* initialize after allocating the memory for surround_obj */
300             ret = ssrmod.surround_filters_init(ssrmod.surround_obj,
301                         6,
302                         4,
303                         ssrmod.real_coeffs,
304                         ssrmod.imag_coeffs,
305                         sub_woofer,
306                         low_freq,
307                         high_freq,
308                         NULL);
309             if (0 != ret) {
310                ALOGE("%s: surround_filters_init failed with ret:%d",__func__, ret);
311                ssrmod.surround_filters_release(ssrmod.surround_obj);
312                goto init_fail;
313             }
314         } else {
315             ALOGE("%s: Allocationg surround_obj failed", __func__);
316             goto init_fail;
317         }
318     } else {
319         ALOGE("%s: surround_filters_init(surround_obj=Null) "
320               "failed with ret: %d", __func__, ret);
321         goto init_fail;
322     }
323 
324     (void) ssrmod.surround_filters_set_channel_map(ssrmod.surround_obj, chan_map);
325 
326     return 0;
327 
328 init_fail:
329     if (ssrmod.surround_obj) {
330         free(ssrmod.surround_obj);
331         ssrmod.surround_obj = NULL;
332     }
333     if (ssrmod.surround_raw_buffer) {
334         free(ssrmod.surround_raw_buffer);
335         ssrmod.surround_raw_buffer = NULL;
336     }
337     if (ssrmod.real_coeffs){
338         for (i =0; i<COEFF_ARRAY_SIZE; i++ ) {
339             if (ssrmod.real_coeffs[i]) {
340                 free(ssrmod.real_coeffs[i]);
341                 ssrmod.real_coeffs[i] = NULL;
342             }
343         }
344         free(ssrmod.real_coeffs);
345         ssrmod.real_coeffs = NULL;
346     }
347     if (ssrmod.imag_coeffs){
348         for (i =0; i<COEFF_ARRAY_SIZE; i++ ) {
349             if (ssrmod.imag_coeffs[i]) {
350                 free(ssrmod.imag_coeffs[i]);
351                 ssrmod.imag_coeffs[i] = NULL;
352             }
353         }
354         free(ssrmod.imag_coeffs);
355         ssrmod.imag_coeffs = NULL;
356     }
357 
358     return -ENOMEM;
359 }
360 
audio_extn_ssr_update_enabled()361 void audio_extn_ssr_update_enabled()
362 {
363     char ssr_enabled[PROPERTY_VALUE_MAX] = "false";
364 
365     property_get("ro.qc.sdk.audio.ssr",ssr_enabled,"0");
366     if (!strncmp("true", ssr_enabled, 4)) {
367         ALOGD("%s: surround sound recording is supported", __func__);
368         ssrmod.is_ssr_enabled = true;
369     } else {
370         ALOGD("%s: surround sound recording is not supported", __func__);
371         ssrmod.is_ssr_enabled = false;
372     }
373 }
374 
audio_extn_ssr_get_enabled()375 bool audio_extn_ssr_get_enabled()
376 {
377     ALOGV("%s: is_ssr_enabled:%d", __func__, ssrmod.is_ssr_enabled);
378     return (ssrmod.is_ssr_enabled ? true: false);
379 }
380 
audio_extn_ssr_init(struct stream_in * in)381 int32_t audio_extn_ssr_init(struct stream_in *in)
382 {
383     uint32_t ret;
384     char c_multi_ch_dump[128] = {0};
385     uint32_t buffer_size;
386 
387     ALOGD("%s: ssr case ", __func__);
388     in->config.channels = SSR_CHANNEL_INPUT_NUM;
389     in->config.period_size = SSR_PERIOD_SIZE;
390     in->config.period_count = SSR_PERIOD_COUNT;
391 
392     /* use 4k hardcoded buffer size for ssr*/
393     buffer_size = SSR_INPUT_FRAME_SIZE;
394     ALOGV("%s: buffer_size: %d", __func__, buffer_size);
395 
396     ret = ssr_init_surround_sound_lib(buffer_size);
397     if (0 != ret) {
398         ALOGE("%s: initSurroundSoundLibrary failed: %d  "
399               "handle->bufferSize:%d", __func__, ret, buffer_size);
400         return ret;
401     }
402 
403     property_get("ssr.pcmdump",c_multi_ch_dump,"0");
404     if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
405         /* Remember to change file system permission of data(e.g. chmod 777 data/),
406           otherwise, fopen may fail */
407         if ( !ssrmod.fp_4ch)
408             ssrmod.fp_4ch = fopen("/data/4ch.pcm", "wb");
409         if ( !ssrmod.fp_6ch)
410             ssrmod.fp_6ch = fopen("/data/6ch.pcm", "wb");
411         if ((!ssrmod.fp_4ch) || (!ssrmod.fp_6ch))
412             ALOGE("%s: mfp_4ch or mfp_6ch open failed: mfp_4ch:%p mfp_6ch:%p",
413                   __func__, ssrmod.fp_4ch, ssrmod.fp_6ch);
414     }
415 
416     return 0;
417 }
418 
audio_extn_ssr_deinit()419 int32_t audio_extn_ssr_deinit()
420 {
421     int i;
422 
423     if (ssrmod.surround_obj) {
424         ALOGV("%s: entry", __func__);
425         ssrmod.surround_filters_release(ssrmod.surround_obj);
426         if (ssrmod.surround_obj)
427             free(ssrmod.surround_obj);
428         ssrmod.surround_obj = NULL;
429         if (ssrmod.real_coeffs){
430             for (i =0; i<COEFF_ARRAY_SIZE; i++ ) {
431                 if (ssrmod.real_coeffs[i]) {
432                     free(ssrmod.real_coeffs[i]);
433                     ssrmod.real_coeffs[i] = NULL;
434                 }
435             }
436             free(ssrmod.real_coeffs);
437             ssrmod.real_coeffs = NULL;
438         }
439         if (ssrmod.imag_coeffs){
440             for (i =0; i<COEFF_ARRAY_SIZE; i++ ) {
441                 if (ssrmod.imag_coeffs[i]) {
442                     free(ssrmod.imag_coeffs[i]);
443                     ssrmod.imag_coeffs[i] = NULL;
444                 }
445             }
446             free(ssrmod.imag_coeffs);
447             ssrmod.imag_coeffs = NULL;
448         }
449         if (ssrmod.surround_raw_buffer) {
450             free(ssrmod.surround_raw_buffer);
451             ssrmod.surround_raw_buffer = NULL;
452         }
453         if (ssrmod.fp_4ch)
454             fclose(ssrmod.fp_4ch);
455         if (ssrmod.fp_6ch)
456             fclose(ssrmod.fp_6ch);
457     }
458 
459     if(ssrmod.surround_filters_handle) {
460         dlclose(ssrmod.surround_filters_handle);
461         ssrmod.surround_filters_handle = NULL;
462     }
463     ALOGV("%s: exit", __func__);
464 
465     return 0;
466 }
467 
audio_extn_ssr_read(struct audio_stream_in * stream,void * buffer,size_t bytes)468 int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
469                        void *buffer, size_t bytes)
470 {
471     struct stream_in *in = (struct stream_in *)stream;
472     struct audio_device *adev = in->dev;
473     size_t peroid_bytes;
474     int32_t ret;
475 
476     /* Convert bytes for 6ch to 4ch*/
477     peroid_bytes = (bytes / SSR_CHANNEL_OUTPUT_NUM) * SSR_CHANNEL_INPUT_NUM;
478 
479     if (!ssrmod.surround_obj) {
480         ALOGE("%s: surround_obj not initialized", __func__);
481         return -ENOMEM;
482     }
483 
484     ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, peroid_bytes);
485     if (ret < 0) {
486         ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
487         return ret;
488     }
489 
490     /* apply ssr libs to conver 4ch to 6ch */
491     ssrmod.surround_filters_intl_process(ssrmod.surround_obj,
492         buffer, ssrmod.surround_raw_buffer);
493 
494     /*dump for raw pcm data*/
495     if (ssrmod.fp_4ch)
496         fwrite(ssrmod.surround_raw_buffer, 1, peroid_bytes, ssrmod.fp_4ch);
497     if (ssrmod.fp_6ch)
498         fwrite(buffer, 1, bytes, ssrmod.fp_6ch);
499 
500     return ret;
501 }
502 
503 #endif /* SSR_ENABLED */
504