ronnie
2022-10-23 4bf14332546635f50a1bf7f3df4c0a8e29643280
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/******************************************************************************
 *                                                                            *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ixheaacd_type_def.h>
#include "ixheaacd_sbr_const.h"
 
#include "ixheaacd_pvc_dec.h"
#include "ixheaacd_pvc_rom.h"
 
static VOID ixheaacd_pvc_sb_parsing(ia_pvc_data_struct *ptr_pvc_data,
                                    WORD16 first_bnd_idx, FLOAT32 *p_qmfh) {
  WORD32 ksg, k;
  WORD32 start_band, end_band;
  WORD32 time_slot;
 
  FLOAT32 *p_sbr_range_esg = &ptr_pvc_data->sbr_range_esg_arr[0];
 
  for (time_slot = 0; time_slot < PVC_NUM_TIME_SLOTS; time_slot++) {
    start_band = first_bnd_idx;
    end_band = start_band + ptr_pvc_data->nb_high_per_grp - 1;
 
    for (ksg = 0; ksg < ptr_pvc_data->nb_high; ksg++) {
      for (k = start_band; k <= end_band; k++) {
        p_qmfh[k] = (FLOAT32)pow(10.0f, (p_sbr_range_esg[ksg] / 10.0));
      }
      start_band += ptr_pvc_data->nb_high_per_grp;
      if (ksg >= ptr_pvc_data->nb_high - 2) {
        end_band = SBR_NUM_QMF_BANDS - 1;
      } else {
        end_band = start_band + ptr_pvc_data->nb_high_per_grp - 1;
        if (end_band >= SBR_NUM_QMF_BANDS - 1) {
          end_band = SBR_NUM_QMF_BANDS - 1;
        }
      }
    }
    p_sbr_range_esg = p_sbr_range_esg + 8;
    p_qmfh = p_qmfh + SBR_NUM_QMF_BANDS;
  }
  return;
}
 
static VOID ixheaacd_pvc_qmf_grouping(ia_pvc_data_struct *ptr_pvc_data,
                                      WORD16 first_bnd_idx, FLOAT32 *p_qmf_ener,
                                      WORD32 first_pvc_timeslot) {
  WORD32 ksg, time_slot, ib;
  WORD32 lbw, start_band, end_band;
  FLOAT32 esg;
  FLOAT32 *p_esg = (FLOAT32 *)ptr_pvc_data->esg;
 
  lbw = 8 / ptr_pvc_data->pvc_rate;
 
  for (time_slot = 0; time_slot < PVC_NUM_TIME_SLOTS; time_slot++) {
    for (ksg = 0; ksg < PVC_NB_LOW; ksg++) {
      start_band = first_bnd_idx - lbw * PVC_NB_LOW + lbw * ksg;
      end_band = start_band + lbw - 1;
      if (start_band >= 0) {
        esg = 0.0f;
        for (ib = start_band; ib <= end_band; ib++) {
          esg += p_qmf_ener[ib];
        }
        esg = esg / lbw;
      } else {
        esg = PVC_ESG_MIN_VAL;
      }
 
      if (esg > PVC_ESG_MIN_VAL) {
        p_esg[(time_slot + 16 - 1) * 3 + ksg] = 10 * ((FLOAT32)log10(esg));
      } else {
        p_esg[(time_slot + 16 - 1) * 3 + ksg] = PVC_10LOG10_ESG_MIN_VAL;
      }
    }
    p_qmf_ener = p_qmf_ener + SBR_NUM_QMF_BANDS_2;
  }
 
  if ((ptr_pvc_data->prev_pvc_flg == 0) ||
      ((first_bnd_idx * ptr_pvc_data->pvc_rate) !=
       (ptr_pvc_data->prev_first_bnd_idx * ptr_pvc_data->prev_pvc_rate))) {
    for (time_slot = 0; time_slot < 16 - 1 + first_pvc_timeslot; time_slot++) {
      for (ksg = 0; ksg < PVC_NB_LOW; ksg++) {
        p_esg[time_slot * 3 + ksg] =
            p_esg[(16 - 1 + first_pvc_timeslot) * 3 + ksg];
      }
    }
  }
 
  return;
}
 
static VOID ixheaacd_pvc_time_smoothing(ia_pvc_data_struct *ptr_pvc_data) {
  WORD32 time_slot;
  FLOAT32 *p_smooth_esg = (FLOAT32 *)&ptr_pvc_data->smooth_esg_arr[0];
  for (time_slot = 0; time_slot < PVC_NUM_TIME_SLOTS; time_slot++) {
    WORD32 ksg, time_slot_idx;
    FLOAT32 *p_esg = (FLOAT32 *)&ptr_pvc_data->esg[time_slot + 16 - 1][2];
    FLOAT32 *p_smth_wind_coeff = (FLOAT32 *)&ptr_pvc_data->p_smth_wind_coeff[0];
    memset(p_smooth_esg, (WORD32)0.f, sizeof(FLOAT32) * PVC_NB_LOW);
    for (time_slot_idx = 0; time_slot_idx < ptr_pvc_data->num_time_slots;
         time_slot_idx++) {
      ksg = PVC_NB_LOW - 1;
      for (; ksg >= 0; ksg--) {
        p_smooth_esg[ksg] += (*p_esg) * (*p_smth_wind_coeff);
        p_esg--;
      }
      p_smth_wind_coeff++;
    }
    p_smooth_esg = p_smooth_esg + 3;
  }
  return;
}
 
static VOID ixheaacd_pvc_pred_env_sf(ia_pvc_data_struct *ptr_pvc_data) {
  WORD32 ksg, kb;
  WORD32 tab_1_index, tab_2_index;
  WORD32 time_slot;
  WORD8 *pred_tab_1, *pred_tab_2;
 
  FLOAT32 temp;
  FLOAT32 *p_smooth_esg = &ptr_pvc_data->smooth_esg_arr[0];
  FLOAT32 *p_sbr_range_esg = &ptr_pvc_data->sbr_range_esg_arr[0];
 
  for (time_slot = 0; time_slot < PVC_NUM_TIME_SLOTS; time_slot++) {
    tab_2_index = ptr_pvc_data->pvc_id[time_slot];
 
    if (tab_2_index < ptr_pvc_data->p_pvc_id_boundary[0]) {
      tab_1_index = 0;
    } else if (tab_2_index < ptr_pvc_data->p_pvc_id_boundary[1]) {
      tab_1_index = 1;
    } else {
      tab_1_index = 2;
    }
 
    pred_tab_1 =
        (WORD8 *)(&(ptr_pvc_data->p_pred_coeff_tab_1[tab_1_index * PVC_NB_LOW *
                                                     ptr_pvc_data->nb_high]));
    pred_tab_2 = (WORD8 *)(&(
        ptr_pvc_data->p_pred_coeff_tab_2[tab_2_index * ptr_pvc_data->nb_high]));
 
    for (ksg = 0; ksg < ptr_pvc_data->nb_high; ksg++) {
      temp =
          (FLOAT32)(WORD8)(*(pred_tab_2++)) * ptr_pvc_data->p_q_fac[PVC_NB_LOW];
      p_sbr_range_esg[ksg] = temp;
    }
    for (kb = 0; kb < PVC_NB_LOW; kb++) {
      for (ksg = 0; ksg < ptr_pvc_data->nb_high; ksg++) {
        temp = (FLOAT32)(WORD8)(*(pred_tab_1++)) * ptr_pvc_data->p_q_fac[kb];
        p_sbr_range_esg[ksg] += temp * p_smooth_esg[kb];
      }
    }
    p_smooth_esg = p_smooth_esg + 3;
    p_sbr_range_esg = p_sbr_range_esg + 8;
  }
 
  return;
}
 
WORD32 ixheaacd_pvc_process(ia_pvc_data_struct *ptr_pvc_data,
                            WORD16 first_bnd_idx, WORD32 first_pvc_timeslot,
                            FLOAT32 *p_qmf_ener, FLOAT32 *p_qmfh) {
  switch (ptr_pvc_data->pvc_mode) {
    case 1:
      ptr_pvc_data->nb_high = PVC_NB_HIGH_MODE1;
      ptr_pvc_data->nb_high_per_grp = 8 / ptr_pvc_data->pvc_rate;
      ptr_pvc_data->p_pred_coeff_tab_1 =
          (WORD8 *)ixheaacd_pred_coeff_table_1_mode_1;
      ptr_pvc_data->p_pred_coeff_tab_2 =
          (WORD8 *)ixheaacd_pred_coeff_table_2_mode_1;
      ptr_pvc_data->p_pvc_id_boundary =
          (UWORD8 *)ixheaacd_pred_coeff_pvc_id_boundaries_1;
      ptr_pvc_data->p_q_fac = (FLOAT32 *)ixheaacd_q_factor_table_mode_1;
      if (ptr_pvc_data->ns_mode) {
        ptr_pvc_data->num_time_slots = 4;
        ptr_pvc_data->p_smth_wind_coeff =
            (FLOAT32 *)ixheaacd_pvc_smoothing_wind_tab_ns4;
      } else {
        ptr_pvc_data->num_time_slots = 16;
        ptr_pvc_data->p_smth_wind_coeff =
            (FLOAT32 *)ixheaacd_pvc_smoothing_wind_tab_ns16;
      }
      break;
    case 2:
      ptr_pvc_data->nb_high = PVC_NB_HIGH_MODE2;
      ptr_pvc_data->nb_high_per_grp = 12 / ptr_pvc_data->pvc_rate;
      ptr_pvc_data->p_pred_coeff_tab_1 =
          (WORD8 *)ixheaacd_pred_coeff_table_1_mode_2;
      ptr_pvc_data->p_pred_coeff_tab_2 =
          (WORD8 *)ixheaacd_pred_coeff_table_2_mode_2;
      ptr_pvc_data->p_pvc_id_boundary =
          (UWORD8 *)ixheaacd_pred_coeff_pvc_id_boundaries_2;
      ptr_pvc_data->p_q_fac = (FLOAT32 *)ixheaacd_q_factor_table_mode_2;
      if (ptr_pvc_data->ns_mode) {
        ptr_pvc_data->num_time_slots = 3;
        ptr_pvc_data->p_smth_wind_coeff =
            (FLOAT32 *)ixheaacd_pvc_smoothing_wind_tab_ns3;
      } else {
        ptr_pvc_data->num_time_slots = 12;
        ptr_pvc_data->p_smth_wind_coeff =
            (FLOAT32 *)ixheaacd_pvc_smoothing_wind_tab_ns12;
      }
      break;
    default:
      return -1;
  }
  ptr_pvc_data->prev_pvc_id = ptr_pvc_data->pvc_id[PVC_NUM_TIME_SLOTS - 1];
 
  ixheaacd_pvc_qmf_grouping(ptr_pvc_data, first_bnd_idx, p_qmf_ener,
                            first_pvc_timeslot);
 
  ixheaacd_pvc_time_smoothing(ptr_pvc_data);
 
  ixheaacd_pvc_pred_env_sf(ptr_pvc_data);
 
  ixheaacd_pvc_sb_parsing(ptr_pvc_data, first_bnd_idx, p_qmfh);
 
  memcpy((FLOAT32 *)(&ptr_pvc_data->esg[0][0]),
         (FLOAT32 *)(&ptr_pvc_data->esg[PVC_NUM_TIME_SLOTS][0]),
         sizeof(FLOAT32) * (PVC_NUM_TIME_SLOTS - 1) * 3);
 
  return 0;
}