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 <stdio.h> 21 #include <stdlib.h> 22 #include <math.h> 23 #include <assert.h> 24 #include <string.h> 25 26 #include "impd_type_def.h" 27 #include "impd_drc_bitbuffer.h" 28 #include "impd_drc_extr_delta_coded_info.h" 29 #include "impd_drc_common.h" 30 #include "impd_drc_struct.h" 31 #include "impd_drc_parser.h" 32 33 WORD32 impd_read_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) { 34 UWORD32 ret_val; 35 UWORD8* ptr_read_next = it_bit_buff->ptr_read_next; 36 WORD bit_pos = it_bit_buff->bit_pos; 37 38 if (it_bit_buff->cnt_bits <= 0) { 39 it_bit_buff->error = 1; 40 return -1; 41 } 42 43 if (no_of_bits == 0) { 44 return 0; 45 } 46 47 it_bit_buff->cnt_bits -= no_of_bits; 48 ret_val = (UWORD32)*ptr_read_next; 49 50 bit_pos -= no_of_bits; 51 while (bit_pos < 0) { 52 bit_pos += 8; 53 ptr_read_next++; 54 55 if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) { 56 ptr_read_next = it_bit_buff->ptr_bit_buf_base; 57 } 58 59 ret_val <<= 8; 60 61 ret_val |= (UWORD32)*ptr_read_next; 62 } 63 64 ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits); 65 it_bit_buff->ptr_read_next = ptr_read_next; 66 it_bit_buff->bit_pos = (WORD16)bit_pos; 67 return ret_val; 68 } 69 70 WORD32 impd_skip_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) { 71 UWORD8* ptr_read_next = it_bit_buff->ptr_read_next; 72 WORD bit_pos = it_bit_buff->bit_pos; 73 74 if (it_bit_buff->cnt_bits < no_of_bits) { 75 it_bit_buff->error = 1; 76 return -1; 77 } 78 79 it_bit_buff->cnt_bits -= no_of_bits; 80 81 bit_pos -= no_of_bits; 82 while (bit_pos < 0) { 83 bit_pos += 8; 84 ptr_read_next++; 85 } 86 it_bit_buff->ptr_read_next = ptr_read_next; 87 it_bit_buff->bit_pos = (WORD16)bit_pos; 88 return no_of_bits; 89 } 90 ia_bit_buf_struct* impd_create_bit_buf(ia_bit_buf_struct* it_bit_buff, 91 UWORD8* ptr_bit_buf_base, 92 WORD32 bit_buf_size) { 93 it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base; 94 it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1; 95 96 it_bit_buff->ptr_read_next = ptr_bit_buf_base; 97 it_bit_buff->bit_pos = 7; 98 99 it_bit_buff->cnt_bits = 0; 100 it_bit_buff->size = bit_buf_size << 3; 101 it_bit_buff->error = 0; 102 103 return it_bit_buff; 104 } 105 106 ia_bit_buf_struct* impd_create_init_bit_buf(ia_bit_buf_struct* it_bit_buff, 107 UWORD8* ptr_bit_buf_base, 108 WORD32 bit_buf_size) { 109 impd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size); 110 it_bit_buff->cnt_bits = (bit_buf_size << 3); 111 return (it_bit_buff); 112 } 113 114 WORD32 impd_init_drc_bitstream_dec(ia_drc_bits_dec_struct* p_drc_bs_dec_struct, 115 WORD32 sample_rate, WORD32 frame_size, 116 WORD32 delay_mode, 117 WORD32 lfe_channel_map_count, 118 WORD32* lfe_channel_map) { 119 WORD32 i, err_code = 0; 120 121 ia_drc_params_bs_dec_struct* ia_drc_params_struct = 122 &p_drc_bs_dec_struct->ia_drc_params_struct; 123 ia_drc_params_struct->drc_frame_size = frame_size; 124 if (sample_rate < MIN_DRC_SAMP_FREQ) { 125 return -1; 126 } 127 ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate); 128 ia_drc_params_struct->num_gain_values_max_default = 129 ia_drc_params_struct->drc_frame_size / 130 ia_drc_params_struct->delta_tmin_default; 131 ia_drc_params_struct->delay_mode = delay_mode; 132 133 if ((frame_size < 1) || (frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) || 134 (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate)) { 135 return -1; 136 } 137 138 if (ia_drc_params_struct->delta_tmin_default > 139 ia_drc_params_struct->drc_frame_size) { 140 return -1; 141 } 142 143 if (lfe_channel_map_count >= 0) { 144 if ((lfe_channel_map == NULL) || 145 (lfe_channel_map_count > MAX_CHANNEL_COUNT)) { 146 return (-1); 147 } 148 149 ia_drc_params_struct->lfe_channel_map_count = lfe_channel_map_count; 150 151 for (i = 0; i < lfe_channel_map_count; i++) { 152 ia_drc_params_struct->lfe_channel_map[i] = lfe_channel_map[i]; 153 } 154 } else { 155 ia_drc_params_struct->lfe_channel_map_count = -1; 156 157 for (i = 0; i < MAX_CHANNEL_COUNT; i++) { 158 ia_drc_params_struct->lfe_channel_map[i] = 0; 159 } 160 } 161 162 err_code = impd_init_tbls(ia_drc_params_struct->num_gain_values_max_default, 163 &p_drc_bs_dec_struct->tables_default); 164 165 return err_code; 166 } 167 168 WORD32 impd_process_drc_bitstream_dec_config( 169 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff, 170 ia_drc_config* pstr_drc_config, UWORD8* bitstream_config, 171 WORD32 num_bytes) { 172 WORD32 err_code = 0; 173 174 it_bit_buff = 175 impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes); 176 177 err_code = impd_parse_drc_config( 178 it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct, pstr_drc_config); 179 if (err_code) return (err_code); 180 181 return err_code; 182 } 183 184 WORD32 impd_process_drc_bitstream_dec_gain( 185 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff, 186 ia_drc_config* pstr_drc_config, ia_drc_gain_struct* pstr_drc_gain, 187 UWORD8* bitstream_gain, WORD32 num_bytes, WORD32 num_bits_offset, 188 WORD32* num_bits_read) { 189 WORD32 err_code = 0; 190 191 WORD32 dummy; 192 193 it_bit_buff = 194 impd_create_init_bit_buf(it_bit_buff, bitstream_gain, num_bytes); 195 196 dummy = impd_read_bits_buf(it_bit_buff, num_bits_offset); 197 if (it_bit_buff->error) return it_bit_buff->error; 198 199 err_code = impd_drc_uni_gain_read(it_bit_buff, p_drc_bs_dec_struct, 200 pstr_drc_config, pstr_drc_gain); 201 202 if (err_code > PROC_COMPLETE) return (err_code); 203 204 *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits; 205 206 if (err_code == PROC_COMPLETE) { 207 return err_code; 208 } 209 210 return 0; 211 } 212 213 WORD32 impd_process_drc_bitstream_dec_loudness_info_set( 214 ia_bit_buf_struct* it_bit_buff, 215 ia_drc_loudness_info_set_struct* pstr_loudness_info, 216 UWORD8* bit_stream_loudness, WORD32 num_bytes_loudness) { 217 WORD32 err_code = 0; 218 219 it_bit_buff = impd_create_init_bit_buf(it_bit_buff, bit_stream_loudness, 220 num_bytes_loudness); 221 222 err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info); 223 if (err_code) return (err_code); 224 225 return 0; 226 } 227