2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: sbr_tf_grid.c,v 1.20 2008/09/19 22:50:20 menno Exp $
31 /* Time/Frequency grid */
44 /* static function declarations */
46 static int16_t rel_bord_lead(sbr_info *sbr, uint8_t ch, uint8_t l);
47 static int16_t rel_bord_trail(sbr_info *sbr, uint8_t ch, uint8_t l);
49 static uint8_t middleBorder(sbr_info *sbr, uint8_t ch);
52 /* function constructs new time border vector */
53 /* first build into temp vector to be able to use previous vector on error */
54 uint8_t envelope_time_border_vector(sbr_info *sbr, uint8_t ch)
56 uint8_t l, border, temp;
57 uint8_t t_E_temp[6] = {0};
59 t_E_temp[0] = sbr->rate * sbr->abs_bord_lead[ch];
60 t_E_temp[sbr->L_E[ch]] = sbr->rate * sbr->abs_bord_trail[ch];
62 switch (sbr->bs_frame_class[ch])
68 temp = (sbr->numTimeSlots / 4);
69 t_E_temp[3] = sbr->rate * 3 * temp;
70 t_E_temp[2] = sbr->rate * 2 * temp;
71 t_E_temp[1] = sbr->rate * temp;
74 t_E_temp[1] = sbr->rate * (sbr->numTimeSlots / 2);
84 int8_t i = sbr->L_E[ch];
85 border = sbr->abs_bord_trail[ch];
87 for (l = 0; l < (sbr->L_E[ch] - 1); l++)
89 if (border < sbr->bs_rel_bord[ch][l])
92 border -= sbr->bs_rel_bord[ch][l];
93 t_E_temp[--i] = sbr->rate * border;
102 border = sbr->abs_bord_lead[ch];
104 for (l = 0; l < (sbr->L_E[ch] - 1); l++)
106 border += sbr->bs_rel_bord[ch][l];
108 if (sbr->rate * border + sbr->tHFAdj > sbr->numTimeSlotsRate+sbr->tHFGen)
111 t_E_temp[i++] = sbr->rate * border;
117 if (sbr->bs_num_rel_0[ch])
120 border = sbr->abs_bord_lead[ch];
122 for (l = 0; l < sbr->bs_num_rel_0[ch]; l++)
124 border += sbr->bs_rel_bord_0[ch][l];
126 if (sbr->rate * border + sbr->tHFAdj > sbr->numTimeSlotsRate+sbr->tHFGen)
129 t_E_temp[i++] = sbr->rate * border;
133 if (sbr->bs_num_rel_1[ch])
135 int8_t i = sbr->L_E[ch];
136 border = sbr->abs_bord_trail[ch];
138 for (l = 0; l < sbr->bs_num_rel_1[ch]; l++)
140 if (border < sbr->bs_rel_bord_1[ch][l])
143 border -= sbr->bs_rel_bord_1[ch][l];
144 t_E_temp[--i] = sbr->rate * border;
150 /* no error occured, we can safely use this t_E vector */
151 for (l = 0; l < 6; l++)
153 sbr->t_E[ch][l] = t_E_temp[l];
159 void noise_floor_time_border_vector(sbr_info *sbr, uint8_t ch)
161 sbr->t_Q[ch][0] = sbr->t_E[ch][0];
163 if (sbr->L_E[ch] == 1)
165 sbr->t_Q[ch][1] = sbr->t_E[ch][1];
168 uint8_t index = middleBorder(sbr, ch);
169 sbr->t_Q[ch][1] = sbr->t_E[ch][index];
170 sbr->t_Q[ch][2] = sbr->t_E[ch][sbr->L_E[ch]];
175 static int16_t rel_bord_lead(sbr_info *sbr, uint8_t ch, uint8_t l)
180 switch (sbr->bs_frame_class[ch])
183 return sbr->numTimeSlots/sbr->L_E[ch];
187 for (i = 0; i < l; i++)
189 acc += sbr->bs_rel_bord[ch][i];
193 for (i = 0; i < l; i++)
195 acc += sbr->bs_rel_bord_0[ch][i];
203 static int16_t rel_bord_trail(sbr_info *sbr, uint8_t ch, uint8_t l)
208 switch (sbr->bs_frame_class[ch])
214 for (i = 0; i < l; i++)
216 acc += sbr->bs_rel_bord[ch][i];
220 for (i = 0; i < l; i++)
222 acc += sbr->bs_rel_bord_1[ch][i];
231 static uint8_t middleBorder(sbr_info *sbr, uint8_t ch)
235 switch (sbr->bs_frame_class[ch])
238 retval = sbr->L_E[ch]/2;
241 if (sbr->bs_pointer[ch] == 0)
243 else if (sbr->bs_pointer[ch] == 1)
244 retval = sbr->L_E[ch] - 1;
246 retval = sbr->bs_pointer[ch] - 1;
250 if (sbr->bs_pointer[ch] > 1)
251 retval = sbr->L_E[ch] + 1 - sbr->bs_pointer[ch];
253 retval = sbr->L_E[ch] - 1;
257 return (retval > 0) ? retval : 0;