Home | History | Annotate | Download | only in drc_src
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2018 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 #include <stdlib.h>
     21 #include "impd_type_def.h"
     22 #include "impd_drc_extr_delta_coded_info.h"
     23 #include "impd_drc_common.h"
     24 #include "impd_drc_struct.h"
     25 #include "impd_parametric_drc_dec.h"
     26 #include "impd_drc_gain_dec.h"
     27 #include "impd_drc_filter_bank.h"
     28 #include "impd_drc_multi_band.h"
     29 
     30 WORD32 impd_init_drc_params(WORD32 frame_size, WORD32 sample_rate,
     31                             WORD32 gain_delay_samples, WORD32 delay_mode,
     32                             WORD32 sub_band_domain_mode,
     33                             ia_drc_params_struct* ia_drc_params_struct) {
     34   WORD32 k;
     35   if (frame_size < 1 || frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) {
     36     return -1;
     37   }
     38 
     39   if (sample_rate < 1000) {
     40     return -1;
     41   }
     42 
     43   ia_drc_params_struct->drc_frame_size = frame_size;
     44 
     45   if (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate) {
     46     return -1;
     47   }
     48 
     49   ia_drc_params_struct->sample_rate = sample_rate;
     50 
     51   ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate);
     52 
     53   if (ia_drc_params_struct->delta_tmin_default >
     54       ia_drc_params_struct->drc_frame_size) {
     55     return -1;
     56   }
     57 
     58   if ((delay_mode != DELAY_MODE_REGULAR_DELAY) &&
     59       (delay_mode != DELAY_MODE_LOW_DELAY)) {
     60     return -1;
     61   }
     62 
     63   ia_drc_params_struct->delay_mode = delay_mode;
     64 
     65   ia_drc_params_struct->drc_set_counter = 0;
     66   ia_drc_params_struct->multiband_sel_drc_idx = -1;
     67 
     68   for (k = 0; k < SEL_DRC_COUNT; k++) {
     69     ia_drc_params_struct->sel_drc_array[k].drc_instructions_index = -1;
     70     ia_drc_params_struct->sel_drc_array[k].dwnmix_instructions_index = -1;
     71     ia_drc_params_struct->sel_drc_array[k].drc_coeff_idx = -1;
     72   }
     73 
     74   if ((gain_delay_samples > MAX_SIGNAL_DELAY) || (gain_delay_samples < 0)) {
     75     return -1;
     76   } else {
     77     ia_drc_params_struct->gain_delay_samples = gain_delay_samples;
     78   }
     79 
     80   switch (sub_band_domain_mode) {
     81     case SUBBAND_DOMAIN_MODE_OFF:
     82     case SUBBAND_DOMAIN_MODE_QMF64:
     83     case SUBBAND_DOMAIN_MODE_QMF71:
     84     case SUBBAND_DOMAIN_MODE_STFT256:
     85       ia_drc_params_struct->sub_band_domain_mode = sub_band_domain_mode;
     86       break;
     87     default:
     88       return -1;
     89       break;
     90   }
     91 
     92   ia_drc_params_struct->parametric_drc_delay = 0;
     93   ia_drc_params_struct->eq_delay = 0;
     94 
     95   return 0;
     96 }
     97 
     98 WORD32 impd_select_drc_coefficients(
     99     ia_drc_config* drc_config, ia_uni_drc_coeffs_struct** drc_coefficients_drc,
    100     WORD32* drc_coefficients_selected) {
    101   WORD32 i;
    102   WORD32 cof1 = -1;
    103   WORD32 cof0 = -1;
    104   for (i = 0; i < drc_config->drc_coefficients_drc_count; i++) {
    105     if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].drc_location == 1) {
    106       if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].version == 0) {
    107         cof0 = i;
    108         *drc_coefficients_selected = cof0;
    109       } else {
    110         cof1 = i;
    111         *drc_coefficients_selected = cof1;
    112       }
    113     }
    114   }
    115 
    116   if (cof1 >= 0) {
    117     *drc_coefficients_drc =
    118         &(drc_config->str_p_loc_drc_coefficients_uni_drc[cof1]);
    119   } else if (cof0 >= 0) {
    120     *drc_coefficients_drc =
    121         &(drc_config->str_p_loc_drc_coefficients_uni_drc[cof0]);
    122   } else {
    123     *drc_coefficients_drc = NULL;
    124   }
    125 
    126   return 0;
    127 }
    128 
    129 WORD32 impd_init_selected_drc_set(
    130     ia_drc_config* drc_config, ia_drc_params_struct* ia_drc_params_struct,
    131     ia_parametric_drc_params_struct* p_parametric_drc_params,
    132     WORD32 audio_num_chan, WORD32 drc_set_id_selected,
    133     WORD32 downmix_id_selected, ia_filter_banks_struct* ia_filter_banks_struct,
    134     ia_overlap_params_struct* pstr_overlap_params
    135 
    136     ,
    137     shape_filter_block* shape_filter_block) {
    138   WORD32 g, n, c, err = 0;
    139   WORD32 channel_count = 0;
    140   WORD32 i;
    141 
    142   ia_drc_instructions_struct* drc_instructions_uni_drc = NULL;
    143   ia_uni_drc_coeffs_struct* drc_coefficients_uni_drc = NULL;
    144   WORD32 selected_drc_is_multiband = 0;
    145   WORD32 drc_instructions_selected = -1;
    146   WORD32 downmix_instructions_selected = -1;
    147   WORD32 drc_coefficients_selected = -1;
    148   p_parametric_drc_params->parametric_drc_instance_count = 0;
    149 
    150   if (drc_config->drc_coefficients_drc_count &&
    151       drc_config->str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present) {
    152     if (drc_config->str_p_loc_drc_coefficients_uni_drc->drc_frame_size !=
    153         ia_drc_params_struct->drc_frame_size) {
    154       return -1;
    155     }
    156   }
    157 
    158   for (n = 0; n < drc_config->drc_instructions_count_plus; n++) {
    159     if (drc_config->str_drc_instruction_str[n].drc_set_id ==
    160         drc_set_id_selected)
    161       break;
    162   }
    163   if (n == drc_config->drc_instructions_count_plus) {
    164     return -1;
    165   }
    166   drc_instructions_selected = n;
    167   drc_instructions_uni_drc =
    168       &(drc_config->str_drc_instruction_str[drc_instructions_selected]);
    169 
    170   if (downmix_id_selected == ID_FOR_BASE_LAYOUT) {
    171     channel_count = drc_config->channel_layout.base_channel_count;
    172   } else if (downmix_id_selected == ID_FOR_ANY_DOWNMIX) {
    173     channel_count = audio_num_chan;
    174   } else {
    175     for (n = 0; n < drc_config->dwnmix_instructions_count; n++) {
    176       if (drc_config->dwnmix_instructions[n].downmix_id == downmix_id_selected)
    177         break;
    178     }
    179     if (n == drc_config->dwnmix_instructions_count) {
    180       return (UNEXPECTED_ERROR);
    181     }
    182     channel_count = drc_config->dwnmix_instructions[n].target_channel_count;
    183 
    184     downmix_instructions_selected = n;
    185   }
    186   drc_instructions_uni_drc->audio_num_chan = channel_count;
    187 
    188   if (drc_instructions_uni_drc->drc_set_id <= 0) {
    189     drc_coefficients_selected = 0;
    190   } else {
    191     err = impd_select_drc_coefficients(drc_config, &drc_coefficients_uni_drc,
    192                                        &drc_coefficients_selected);
    193   }
    194 
    195   ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
    196       .drc_instructions_index = drc_instructions_selected;
    197   ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
    198       .dwnmix_instructions_index = downmix_instructions_selected;
    199   ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
    200       .drc_coeff_idx = drc_coefficients_selected;
    201 
    202   if ((drc_instructions_uni_drc->downmix_id[0] == ID_FOR_ANY_DOWNMIX) ||
    203       (drc_instructions_uni_drc->dwnmix_id_count > 1)) {
    204     WORD32 idx = drc_instructions_uni_drc->gain_set_index[0];
    205     for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
    206       drc_instructions_uni_drc->channel_group_of_ch[c] = (idx >= 0) ? 0 : -1;
    207     }
    208   }
    209 
    210   for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
    211     drc_instructions_uni_drc->num_chan_per_ch_group[g] = 0;
    212     for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
    213       if (drc_instructions_uni_drc->channel_group_of_ch[c] == g) {
    214         drc_instructions_uni_drc->num_chan_per_ch_group[g]++;
    215       }
    216     }
    217   }
    218 
    219   if (drc_instructions_uni_drc->drc_set_effect &
    220       (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) {
    221     drc_instructions_uni_drc->multiband_audio_sig_count =
    222         drc_instructions_uni_drc->audio_num_chan;
    223   } else {
    224     drc_instructions_uni_drc->multiband_audio_sig_count = 0;
    225     for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
    226       g = drc_instructions_uni_drc->channel_group_of_ch[c];
    227       if (g < 0) {
    228         drc_instructions_uni_drc->multiband_audio_sig_count++;
    229       } else {
    230         drc_instructions_uni_drc->multiband_audio_sig_count +=
    231             drc_instructions_uni_drc->band_count_of_ch_group[g];
    232       }
    233     }
    234   }
    235 
    236   for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
    237     if (g == 0) {
    238       drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max = 0;
    239     }
    240     if (drc_instructions_uni_drc->ch_group_parametric_drc_flag[g] == 0) {
    241       if (drc_instructions_uni_drc->band_count_of_ch_group[g] > 1) {
    242         if (ia_drc_params_struct->multiband_sel_drc_idx != -1) {
    243           return (UNEXPECTED_ERROR);
    244         }
    245         selected_drc_is_multiband = 1;
    246       }
    247     } else {
    248       WORD32 gain_set_index =
    249           drc_instructions_uni_drc->gain_set_idx_of_ch_group_parametric_drc[g];
    250       WORD32 parametric_drc_id =
    251           drc_config->str_drc_config_ext.str_drc_coeff_param_drc
    252               .str_parametric_drc_gain_set_params[gain_set_index]
    253               .parametric_drc_id;
    254       WORD32 parametric_drc_look_ahead_samples = 0;
    255       ia_parametric_drc_instructions_struct* str_parametric_drc_instructions;
    256 
    257       for (i = 0;
    258            i < drc_config->str_drc_config_ext.parametric_drc_instructions_count;
    259            i++) {
    260         if (parametric_drc_id ==
    261             drc_config->str_drc_config_ext.str_parametric_drc_instructions[i]
    262                 .parametric_drc_id)
    263           break;
    264       }
    265       if (i ==
    266           drc_config->str_drc_config_ext.parametric_drc_instructions_count) {
    267         return (UNEXPECTED_ERROR);
    268       }
    269       str_parametric_drc_instructions =
    270           &drc_config->str_drc_config_ext.str_parametric_drc_instructions[i];
    271 
    272       p_parametric_drc_params->parametric_drc_idx
    273           [p_parametric_drc_params->parametric_drc_instance_count] = i;
    274       p_parametric_drc_params->gain_set_index
    275           [p_parametric_drc_params->parametric_drc_instance_count] =
    276           gain_set_index;
    277       if (drc_instructions_uni_drc->drc_apply_to_dwnmix == 0) {
    278         p_parametric_drc_params->dwnmix_id_from_drc_instructions
    279             [p_parametric_drc_params->parametric_drc_instance_count] =
    280             ID_FOR_BASE_LAYOUT;
    281       } else {
    282         if (drc_instructions_uni_drc->dwnmix_id_count > 1) {
    283           p_parametric_drc_params->dwnmix_id_from_drc_instructions
    284               [p_parametric_drc_params->parametric_drc_instance_count] =
    285               ID_FOR_ANY_DOWNMIX;
    286         } else {
    287           p_parametric_drc_params->dwnmix_id_from_drc_instructions
    288               [p_parametric_drc_params->parametric_drc_instance_count] =
    289               drc_instructions_uni_drc->downmix_id[0];
    290         }
    291       }
    292       p_parametric_drc_params->audio_num_chan = channel_count;
    293       for (i = 0; i < p_parametric_drc_params->audio_num_chan; i++) {
    294         if (drc_instructions_uni_drc->channel_group_of_ch[i] == g) {
    295           p_parametric_drc_params->channel_map
    296               [p_parametric_drc_params->parametric_drc_instance_count][i] = 1;
    297         } else {
    298           p_parametric_drc_params->channel_map
    299               [p_parametric_drc_params->parametric_drc_instance_count][i] = 0;
    300         }
    301       }
    302       drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g] = 0;
    303       if (str_parametric_drc_instructions->parametric_drc_look_ahead_flag) {
    304         parametric_drc_look_ahead_samples = (WORD32)(
    305             (FLOAT32)
    306                 str_parametric_drc_instructions->parametric_drc_look_ahead *
    307             (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
    308       } else {
    309         if (str_parametric_drc_instructions->parametric_drc_type ==
    310             PARAM_DRC_TYPE_FF) {
    311           parametric_drc_look_ahead_samples = (WORD32)(
    312               10.0f * (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
    313         } else if (str_parametric_drc_instructions->parametric_drc_type ==
    314                    PARAM_DRC_TYPE_LIM) {
    315           parametric_drc_look_ahead_samples = (WORD32)(
    316               5.0f * (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
    317         } else {
    318           return (UNEXPECTED_ERROR);
    319         }
    320       }
    321       drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g] =
    322           parametric_drc_look_ahead_samples;
    323       if (drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max <
    324           drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g]) {
    325         drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max =
    326             drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g];
    327       }
    328       p_parametric_drc_params->parametric_drc_instance_count += 1;
    329       selected_drc_is_multiband = 0;
    330     }
    331   }
    332   ia_drc_params_struct->parametric_drc_delay +=
    333       drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max;
    334 
    335   if (selected_drc_is_multiband == 1) {
    336     ia_drc_params_struct->multiband_sel_drc_idx =
    337         ia_drc_params_struct->drc_set_counter;
    338     err = impd_init_all_filter_banks(
    339         drc_coefficients_uni_drc,
    340         &(drc_config->str_drc_instruction_str[drc_instructions_selected]),
    341         ia_filter_banks_struct);
    342     if (err) return (err);
    343 
    344     err = impd_init_overlap_weight(
    345         drc_coefficients_uni_drc,
    346         &(drc_config->str_drc_instruction_str[drc_instructions_selected]),
    347         ia_drc_params_struct->sub_band_domain_mode, pstr_overlap_params);
    348     if (err) return (err);
    349   } else {
    350     ia_gain_modifiers_struct* gain_modifiers =
    351         drc_config->str_drc_instruction_str->str_gain_modifiers_of_ch_group;
    352     for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
    353       if (gain_modifiers[g].shape_filter_flag == 1) {
    354         impd_shape_filt_block_init(
    355             &drc_coefficients_uni_drc->str_shape_filter_block_params
    356                  [gain_modifiers[g].shape_filter_idx],
    357             &shape_filter_block[g]);
    358       } else {
    359         shape_filter_block[g].shape_flter_block_flag = 0;
    360       }
    361     }
    362   }
    363 
    364   ia_drc_params_struct->drc_set_counter++;
    365 
    366   return (0);
    367 }
    368