blob: d8bc2edddd7c1a4b40a30f23b959bbdb468cbed7
1 | /* |
2 | ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
3 | ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
4 | ** |
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. |
9 | ** |
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. |
14 | ** |
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. |
18 | ** |
19 | ** Any non-GPL usage of this software or parts of this software is strictly |
20 | ** forbidden. |
21 | ** |
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" |
24 | ** |
25 | ** Commercial non-GPL licensing of this software is possible. |
26 | ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. |
27 | ** |
28 | ** $Id: ssr.c,v 1.19 2007/11/01 12:33:36 menno Exp $ |
29 | **/ |
30 | |
31 | #include "common.h" |
32 | #include "structs.h" |
33 | |
34 | #ifdef SSR_DEC |
35 | |
36 | #include "syntax.h" |
37 | #include "filtbank.h" |
38 | #include "ssr.h" |
39 | #include "ssr_fb.h" |
40 | |
41 | void ssr_decode(ssr_info *ssr, fb_info *fb, uint8_t window_sequence, |
42 | uint8_t window_shape, uint8_t window_shape_prev, |
43 | real_t *freq_in, real_t *time_out, real_t *overlap, |
44 | real_t ipqf_buffer[SSR_BANDS][96 / 4], |
45 | real_t *prev_fmd, uint16_t frame_len) |
46 | { |
47 | uint8_t band; |
48 | uint16_t ssr_frame_len = frame_len / SSR_BANDS; |
49 | real_t time_tmp[2048] = {0}; |
50 | real_t output[1024] = {0}; |
51 | |
52 | for (band = 0; band < SSR_BANDS; band++) { |
53 | int16_t j; |
54 | |
55 | /* uneven bands have inverted frequency scale */ |
56 | if (band == 1 || band == 3) { |
57 | for (j = 0; j < ssr_frame_len / 2; j++) { |
58 | real_t tmp; |
59 | tmp = freq_in[j + ssr_frame_len * band]; |
60 | freq_in[j + ssr_frame_len * band] = |
61 | freq_in[ssr_frame_len - j - 1 + ssr_frame_len * band]; |
62 | freq_in[ssr_frame_len - j - 1 + ssr_frame_len * band] = tmp; |
63 | } |
64 | } |
65 | |
66 | /* non-overlapping inverse filterbank for SSR */ |
67 | ssr_ifilter_bank(fb, window_sequence, window_shape, window_shape_prev, |
68 | freq_in + band * ssr_frame_len, time_tmp + band * ssr_frame_len, |
69 | ssr_frame_len); |
70 | |
71 | /* gain control */ |
72 | ssr_gain_control(ssr, time_tmp, output, overlap, prev_fmd, |
73 | band, window_sequence, ssr_frame_len); |
74 | } |
75 | |
76 | /* inverse pqf to bring subbands together again */ |
77 | ssr_ipqf(ssr, output, time_out, ipqf_buffer, frame_len, SSR_BANDS); |
78 | } |
79 | |
80 | static void ssr_gain_control(ssr_info *ssr, real_t *data, real_t *output, |
81 | real_t *overlap, real_t *prev_fmd, uint8_t band, |
82 | uint8_t window_sequence, uint16_t frame_len) |
83 | { |
84 | uint16_t i; |
85 | real_t gc_function[2 * 1024 / SSR_BANDS]; |
86 | |
87 | if (window_sequence != EIGHT_SHORT_SEQUENCE) { |
88 | ssr_gc_function(ssr, &prev_fmd[band * frame_len * 2], |
89 | gc_function, window_sequence, band, frame_len); |
90 | |
91 | for (i = 0; i < frame_len * 2; i++) { |
92 | data[band * frame_len * 2 + i] *= gc_function[i]; |
93 | } |
94 | for (i = 0; i < frame_len; i++) { |
95 | output[band * frame_len + i] = overlap[band * frame_len + i] + |
96 | data[band * frame_len * 2 + i]; |
97 | } |
98 | for (i = 0; i < frame_len; i++) { |
99 | overlap[band * frame_len + i] = |
100 | data[band * frame_len * 2 + frame_len + i]; |
101 | } |
102 | } else { |
103 | uint8_t w; |
104 | for (w = 0; w < 8; w++) { |
105 | uint16_t frame_len8 = frame_len / 8; |
106 | uint16_t frame_len16 = frame_len / 16; |
107 | |
108 | ssr_gc_function(ssr, &prev_fmd[band * frame_len * 2 + w * frame_len * 2 / 8], |
109 | gc_function, window_sequence, frame_len); |
110 | |
111 | for (i = 0; i < frame_len8 * 2; i++) { |
112 | data[band * frame_len * 2 + w * frame_len8 * 2 + i] *= gc_function[i]; |
113 | } |
114 | for (i = 0; i < frame_len8; i++) { |
115 | overlap[band * frame_len + i + 7 * frame_len16 + w * frame_len8] += |
116 | data[band * frame_len * 2 + 2 * w * frame_len8 + i]; |
117 | } |
118 | for (i = 0; i < frame_len8; i++) { |
119 | overlap[band * frame_len + i + 7 * frame_len16 + (w + 1)*frame_len8] = |
120 | data[band * frame_len * 2 + 2 * w * frame_len8 + frame_len8 + i]; |
121 | } |
122 | } |
123 | for (i = 0; i < frame_len; i++) { |
124 | output[band * frame_len + i] = overlap[band * frame_len + i]; |
125 | } |
126 | for (i = 0; i < frame_len; i++) { |
127 | overlap[band * frame_len + i] = overlap[band * frame_len + i + frame_len]; |
128 | } |
129 | } |
130 | } |
131 | |
132 | static void ssr_gc_function(ssr_info *ssr, real_t *prev_fmd, |
133 | real_t *gc_function, uint8_t window_sequence, |
134 | uint8_t band, uint16_t frame_len) |
135 | { |
136 | uint16_t i; |
137 | uint16_t len_area1, len_area2; |
138 | int32_t aloc[10]; |
139 | real_t alev[10]; |
140 | |
141 | switch (window_sequence) { |
142 | case ONLY_LONG_SEQUENCE: |
143 | len_area1 = frame_len / SSR_BANDS; |
144 | len_area2 = 0; |
145 | break; |
146 | case LONG_START_SEQUENCE: |
147 | len_area1 = (frame_len / SSR_BANDS) * 7 / 32; |
148 | len_area2 = (frame_len / SSR_BANDS) / 16; |
149 | break; |
150 | case EIGHT_SHORT_SEQUENCE: |
151 | len_area1 = (frame_len / 8) / SSR_BANDS; |
152 | len_area2 = 0; |
153 | break; |
154 | case LONG_STOP_SEQUENCE: |
155 | len_area1 = (frame_len / SSR_BANDS); |
156 | len_area2 = 0; |
157 | break; |
158 | } |
159 | |
160 | /* decode bitstream information */ |
161 | |
162 | /* build array M */ |
163 | |
164 | |
165 | for (i = 0; i < frame_len * 2; i++) { |
166 | gc_function[i] = 1; |
167 | } |
168 | } |
169 | |
170 | #endif |
171 |