2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: sbr_tf_grid.c,v 1.15 2004/09/04 14:56:28 menno Exp $
28 /* Time/Frequency grid */
37 #include "sbr_syntax.h"
38 #include "sbr_tf_grid.h"
41 /* static function declarations */
43 static int16_t rel_bord_lead(sbr_info
*sbr
, uint8_t ch
, uint8_t l
);
44 static int16_t rel_bord_trail(sbr_info
*sbr
, uint8_t ch
, uint8_t l
);
46 static uint8_t middleBorder(sbr_info
*sbr
, uint8_t ch
);
49 /* function constructs new time border vector */
50 /* first build into temp vector to be able to use previous vector on error */
51 uint8_t envelope_time_border_vector(sbr_info
*sbr
, uint8_t ch
)
53 uint8_t l
, border
, temp
;
54 uint8_t t_E_temp
[6] = {0};
56 t_E_temp
[0] = sbr
->rate
* sbr
->abs_bord_lead
[ch
];
57 t_E_temp
[sbr
->L_E
[ch
]] = sbr
->rate
* sbr
->abs_bord_trail
[ch
];
59 switch (sbr
->bs_frame_class
[ch
])
65 temp
= (int) (sbr
->numTimeSlots
/ 4);
66 t_E_temp
[3] = sbr
->rate
* 3 * temp
;
67 t_E_temp
[2] = sbr
->rate
* 2 * temp
;
68 t_E_temp
[1] = sbr
->rate
* temp
;
71 t_E_temp
[1] = sbr
->rate
* (int) (sbr
->numTimeSlots
/ 2);
81 int8_t i
= sbr
->L_E
[ch
];
82 border
= sbr
->abs_bord_trail
[ch
];
84 for (l
= 0; l
< (sbr
->L_E
[ch
] - 1); l
++)
86 if (border
< sbr
->bs_rel_bord
[ch
][l
])
89 border
-= sbr
->bs_rel_bord
[ch
][l
];
90 t_E_temp
[--i
] = sbr
->rate
* border
;
99 border
= sbr
->abs_bord_lead
[ch
];
101 for (l
= 0; l
< (sbr
->L_E
[ch
] - 1); l
++)
103 border
+= sbr
->bs_rel_bord
[ch
][l
];
105 if (sbr
->rate
* border
+ sbr
->tHFAdj
> sbr
->numTimeSlotsRate
+sbr
->tHFGen
)
108 t_E_temp
[i
++] = sbr
->rate
* border
;
114 if (sbr
->bs_num_rel_0
[ch
])
117 border
= sbr
->abs_bord_lead
[ch
];
119 for (l
= 0; l
< sbr
->bs_num_rel_0
[ch
]; l
++)
121 border
+= sbr
->bs_rel_bord_0
[ch
][l
];
123 if (sbr
->rate
* border
+ sbr
->tHFAdj
> sbr
->numTimeSlotsRate
+sbr
->tHFGen
)
126 t_E_temp
[i
++] = sbr
->rate
* border
;
130 if (sbr
->bs_num_rel_1
[ch
])
132 int8_t i
= sbr
->L_E
[ch
];
133 border
= sbr
->abs_bord_trail
[ch
];
135 for (l
= 0; l
< sbr
->bs_num_rel_1
[ch
]; l
++)
137 if (border
< sbr
->bs_rel_bord_1
[ch
][l
])
140 border
-= sbr
->bs_rel_bord_1
[ch
][l
];
141 t_E_temp
[--i
] = sbr
->rate
* border
;
147 /* no error occured, we can safely use this t_E vector */
148 for (l
= 0; l
< 6; l
++)
150 sbr
->t_E
[ch
][l
] = t_E_temp
[l
];
156 void noise_floor_time_border_vector(sbr_info
*sbr
, uint8_t ch
)
158 sbr
->t_Q
[ch
][0] = sbr
->t_E
[ch
][0];
160 if (sbr
->L_E
[ch
] == 1)
162 sbr
->t_Q
[ch
][1] = sbr
->t_E
[ch
][1];
165 uint8_t index
= middleBorder(sbr
, ch
);
166 sbr
->t_Q
[ch
][1] = sbr
->t_E
[ch
][index
];
167 sbr
->t_Q
[ch
][2] = sbr
->t_E
[ch
][sbr
->L_E
[ch
]];
172 static int16_t rel_bord_lead(sbr_info
*sbr
, uint8_t ch
, uint8_t l
)
177 switch (sbr
->bs_frame_class
[ch
])
180 return sbr
->numTimeSlots
/sbr
->L_E
[ch
];
184 for (i
= 0; i
< l
; i
++)
186 acc
+= sbr
->bs_rel_bord
[ch
][i
];
190 for (i
= 0; i
< l
; i
++)
192 acc
+= sbr
->bs_rel_bord_0
[ch
][i
];
200 static int16_t rel_bord_trail(sbr_info
*sbr
, uint8_t ch
, uint8_t l
)
205 switch (sbr
->bs_frame_class
[ch
])
211 for (i
= 0; i
< l
; i
++)
213 acc
+= sbr
->bs_rel_bord
[ch
][i
];
217 for (i
= 0; i
< l
; i
++)
219 acc
+= sbr
->bs_rel_bord_1
[ch
][i
];
228 static uint8_t middleBorder(sbr_info
*sbr
, uint8_t ch
)
232 switch (sbr
->bs_frame_class
[ch
])
235 retval
= sbr
->L_E
[ch
]/2;
238 if (sbr
->bs_pointer
[ch
] == 0)
240 else if (sbr
->bs_pointer
[ch
] == 1)
241 retval
= sbr
->L_E
[ch
] - 1;
243 retval
= sbr
->bs_pointer
[ch
] - 1;
247 if (sbr
->bs_pointer
[ch
] > 1)
248 retval
= sbr
->L_E
[ch
] + 1 - sbr
->bs_pointer
[ch
];
250 retval
= sbr
->L_E
[ch
] - 1;
254 return (retval
> 0) ? retval
: 0;