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 <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 #include <ixheaacd_type_def.h>
25 #include <ixheaacd_interface.h>
26 #include "ixheaacd_mps_polyphase.h"
27 #include <ixheaacd_type_def.h>
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_config.h"
30 #include "ixheaacd_mps_dec.h"
31 #include "ixheaacd_mps_interface.h"
32 #include "ixheaacd_constants.h"
33 #include <ixheaacd_basic_ops32.h>
34 #include "ixheaacd_function_selector.h"
35
36 extern const WORD32
37 ixheaacd_mps_polyphase_filter_coeff_fix[10 * MAX_NUM_QMF_BANDS_SAC / 2];
38 extern WORD32 ixheaacd_mps_pre_re[64];
39 extern WORD32 ixheaacd_mps_pre_im[64];
40 extern WORD32 ixheaacd_mps_post_re[128];
41 extern WORD32 ixheaacd_mps_post_im[128];
42
ixheaacd_mult32(WORD32 a,WORD32 b)43 static PLATFORM_INLINE WORD32 ixheaacd_mult32(WORD32 a, WORD32 b) {
44 WORD32 result;
45 WORD64 temp_result;
46
47 temp_result = (WORD64)a * (WORD64)b;
48 result = (WORD32)(temp_result >> 31);
49
50 return (result);
51 }
52
ixheaacd_mps_synt_create(ia_mps_poly_phase_struct * kernel,WORD32 resolution)53 VOID ixheaacd_mps_synt_create(ia_mps_poly_phase_struct *kernel,
54 WORD32 resolution) {
55 kernel->resolution = resolution;
56 }
57
ixheaacd_mps_synt_init(ia_mps_poly_phase_synth_struct * self)58 VOID ixheaacd_mps_synt_init(ia_mps_poly_phase_synth_struct *self) {
59 memset(self->state, 0, sizeof(WORD32) * 64 * 20);
60 }
61
ixheaacd_float_to_int32(FLOAT32 * in,WORD32 * out,WORD32 q_factor,WORD32 sample)62 static VOID ixheaacd_float_to_int32(FLOAT32 *in, WORD32 *out, WORD32 q_factor,
63 WORD32 sample) {
64 WORD32 loop;
65 UWORD32 temp = (1 << q_factor);
66
67 for (loop = 0; loop < sample; loop++) out[loop] = (WORD32)(in[loop] * temp);
68 }
69
ixheaacd_mps_synt_pre_twiddle_dec(WORD32 * ptr_in,WORD32 * table_re,WORD32 * table_im,WORD32 resolution)70 VOID ixheaacd_mps_synt_pre_twiddle_dec(WORD32 *ptr_in, WORD32 *table_re,
71 WORD32 *table_im, WORD32 resolution) {
72 WORD32 tmp, k;
73 for (k = 0; k < 2 * resolution; k += 2) {
74 tmp = ixheaacd_add32_sat(ixheaacd_mult32(ptr_in[k], table_re[k >> 1]),
75 ixheaacd_mult32(ptr_in[k + 1], table_im[k >> 1]));
76 ptr_in[k + 1] = ixheaacd_add32_sat(
77 ixheaacd_mult32(ixheaacd_negate32_sat(ptr_in[k]), table_im[k >> 1]),
78 ixheaacd_mult32(ptr_in[k + 1], table_re[k >> 1]));
79
80 ptr_in[k] = tmp;
81 }
82 }
83
ixheaacd_mps_synt_post_twiddle_dec(WORD32 * ptr_in,WORD32 * table_re,WORD32 * table_im,WORD32 resolution)84 VOID ixheaacd_mps_synt_post_twiddle_dec(WORD32 *ptr_in, WORD32 *table_re,
85 WORD32 *table_im, WORD32 resolution) {
86 WORD32 tmp, k;
87 for (k = 0; k < 2 * resolution; k += 2) {
88 tmp = ixheaacd_add32_sat(ixheaacd_mult32(ptr_in[k], table_re[k]),
89 ixheaacd_mult32(ptr_in[k + 1], table_im[k]));
90
91 ptr_in[k + 1] =
92 ixheaacd_add32_sat(ixheaacd_mult32(-ptr_in[k], table_im[k]),
93 ixheaacd_mult32(ptr_in[k + 1], table_re[k]));
94
95 ptr_in[k] = tmp;
96 }
97 }
98
ixheaacd_mps_synt_post_fft_twiddle_dec(WORD32 resolution,WORD32 * fin_re,WORD32 * fin_im,WORD32 * table_re,WORD32 * table_im,WORD32 * state)99 VOID ixheaacd_mps_synt_post_fft_twiddle_dec(WORD32 resolution, WORD32 *fin_re,
100 WORD32 *fin_im, WORD32 *table_re,
101 WORD32 *table_im, WORD32 *state) {
102 WORD32 l;
103 for (l = 0; l < 2 * resolution; l++) {
104 state[2 * resolution - l - 1] =
105 ixheaacd_add32_sat(ixheaacd_mult32(fin_re[l], table_re[l]),
106 ixheaacd_mult32(fin_im[l], table_im[l]));
107 }
108 }
109
ixheaacd_mps_synt_out_calc_dec(WORD32 resolution,WORD32 * out,WORD32 * state,const WORD32 * filter_coeff)110 VOID ixheaacd_mps_synt_out_calc_dec(WORD32 resolution, WORD32 *out,
111 WORD32 *state, const WORD32 *filter_coeff) {
112 WORD32 l, k;
113 WORD32 *out1, *out2, *state1, *state2;
114 out1 = out;
115 out2 = out + resolution;
116 state1 = state;
117 state2 = state + (3 * resolution);
118
119 for (k = 0; k < 5; k++) {
120 for (l = 0; l < resolution; l++) {
121 *out1++ = (WORD32)(((WORD64)(*state1++) * (*filter_coeff++)) >> 31);
122 *out2++ = (WORD32)(((WORD64)(*state2++) * (*filter_coeff++)) >> 31);
123 }
124 out1 += resolution;
125 out2 += resolution;
126 state1 += (3 * resolution);
127 state2 += (3 * resolution);
128 }
129 }
130
ixheaacd_mps_synt_calc(ia_mps_dec_state_struct * self)131 VOID ixheaacd_mps_synt_calc(ia_mps_dec_state_struct *self) {
132 WORD32 k, l, ts, ch;
133 WORD64 acc;
134 WORD32 ptr_in[128];
135 WORD32 fin_re[128];
136 WORD32 fin_im[128];
137 FLOAT32 temp;
138 WORD32 *state, *tmp_state, *out;
139 const WORD32 *filt_coeff;
140 WORD32 *tmp_buf = self->tmp_buf;
141
142 ia_mps_poly_phase_struct kernel = self->poly_phase_filt_kernel;
143 WORD32 resolution = kernel.resolution;
144 for (ch = 0; ch < self->out_ch_count; ch++) {
145 tmp_state = (&self->qmf_filt_state[ch])->state;
146 state = &tmp_buf[self->time_slots * 2 * resolution];
147 memcpy(state, tmp_state, sizeof(WORD32) * 20 * resolution);
148 out = &tmp_buf[74 * MAX_NUM_QMF_BANDS_SAC];
149
150 for (ts = 0; ts < self->time_slots; ts++) {
151 ixheaacd_float_to_int32(&self->qmf_out_dir[ch][ts][0].re, ptr_in, 10,
152 resolution * 2);
153
154 filt_coeff = ixheaacd_mps_polyphase_filter_coeff_fix;
155
156 state -= (2 * resolution);
157 (*ixheaacd_mps_synt_pre_twiddle)(ptr_in, ixheaacd_mps_pre_re,
158 ixheaacd_mps_pre_im, resolution);
159
160 (*ixheaacd_mps_complex_fft_64)(ptr_in, fin_re, fin_im, resolution);
161
162 (*ixheaacd_mps_synt_post_twiddle)(ptr_in, ixheaacd_mps_post_re,
163 ixheaacd_mps_post_im, resolution);
164
165 (*ixheaacd_mps_complex_fft_64)(ptr_in, &fin_re[1], &fin_im[1],
166 resolution);
167
168 (*ixheaacd_mps_synt_post_fft_twiddle)(resolution, fin_re, fin_im,
169 ixheaacd_mps_post_re,
170 ixheaacd_mps_post_im, state);
171 (*ixheaacd_mps_synt_out_calc)(resolution, out, state, filt_coeff);
172
173 for (k = 0; k < resolution; k++) {
174 acc = 0;
175 for (l = 0; l < 10; l++) {
176 acc = acc + out[resolution * l + k];
177 }
178 if (acc >= 2147483647)
179 temp = 1.0;
180 else if (acc <= -2147483647 - 1)
181 temp = -1.0f;
182 else
183 temp = (FLOAT32)((WORD32)acc) / ((FLOAT32)(1 << 10));
184
185 self->output_buffer[ch][self->qmf_band_count * ts + k] = (FLOAT32)temp;
186 }
187 }
188
189 memcpy(tmp_state, state, sizeof(WORD32) * 20 * resolution);
190 }
191 }
192