summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/drm_dec.c (plain)
blob: cc7dc5c637ee55c0224499fd7062b5a4f29689a1
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: drm_dec.c,v 1.9 2007/11/01 12:33:30 menno Exp $
29**/
30
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <math.h>
35#include "common.h"
36
37#ifdef DRM
38
39#include "sbr_dec.h"
40#include "drm_dec.h"
41#include "bits.h"
42
43/* constants */
44#define DECAY_CUTOFF 3
45#define DECAY_SLOPE 0.05f
46
47/* type definitaions */
48typedef const int8_t (*drm_ps_huff_tab)[2];
49
50
51/* binary search huffman tables */
52static const int8_t f_huffman_sa[][2] = {
53 { /*0*/ -15, 1 }, /* index 0: 1 bits: x */
54 { 2, 3 }, /* index 1: 2 bits: 1x */
55 { /*7*/ -8, 4 }, /* index 2: 3 bits: 10x */
56 { 5, 6 }, /* index 3: 3 bits: 11x */
57 { /*1*/ -14, /*-1*/ -16 }, /* index 4: 4 bits: 101x */
58 { /*-2*/ -17, 7 }, /* index 5: 4 bits: 110x */
59 { 8, 9 }, /* index 6: 4 bits: 111x */
60 { /*2*/ -13, /*-3*/ -18 }, /* index 7: 5 bits: 1101x */
61 { /*3*/ -12, 10 }, /* index 8: 5 bits: 1110x */
62 { 11, 12 }, /* index 9: 5 bits: 1111x */
63 { /*4*/ -11, /*5*/ -10 }, /* index 10: 6 bits: 11101x */
64 { /*-4*/ -19, /*-5*/ -20 }, /* index 11: 6 bits: 11110x */
65 { /*6*/ -9, 13 }, /* index 12: 6 bits: 11111x */
66 { /*-7*/ -22, /*-6*/ -21 } /* index 13: 7 bits: 111111x */
67};
68
69static const int8_t t_huffman_sa[][2] = {
70 { /*0*/ -15, 1 }, /* index 0: 1 bits: x */
71 { 2, 3 }, /* index 1: 2 bits: 1x */
72 { /*-1*/ -16, /*1*/ -14 }, /* index 2: 3 bits: 10x */
73 { 4, 5 }, /* index 3: 3 bits: 11x */
74 { /*-2*/ -17, /*2*/ -13 }, /* index 4: 4 bits: 110x */
75 { 6, 7 }, /* index 5: 4 bits: 111x */
76 { /*-3*/ -18, /*3*/ -12 }, /* index 6: 5 bits: 1110x */
77 { 8, 9 }, /* index 7: 5 bits: 1111x */
78 { /*-4*/ -19, /*4*/ -11 }, /* index 8: 6 bits: 11110x */
79 { 10, 11 }, /* index 9: 6 bits: 11111x */
80 { /*-5*/ -20, /*5*/ -10 }, /* index 10: 7 bits: 111110x */
81 { /*-6*/ -21, 12 }, /* index 11: 7 bits: 111111x */
82 { /*-7*/ -22, 13 }, /* index 12: 8 bits: 1111111x */
83 { /*6*/ -9, /*7*/ -8 } /* index 13: 9 bits: 11111111x */
84};
85
86static const int8_t f_huffman_pan[][2] = {
87 { /*0*/ -15, 1 }, /* index 0: 1 bits: x */
88 { /*-1*/ -16, 2 }, /* index 1: 2 bits: 1x */
89 { /*1*/ -14, 3 }, /* index 2: 3 bits: 11x */
90 { 4, 5 }, /* index 3: 4 bits: 111x */
91 { /*-2*/ -17, /*2*/ -13 }, /* index 4: 5 bits: 1110x */
92 { 6, 7 }, /* index 5: 5 bits: 1111x */
93 { /*-3*/ -18, /*3*/ -12 }, /* index 6: 6 bits: 11110x */
94 { 8, 9 }, /* index 7: 6 bits: 11111x */
95 { /*-4*/ -19, /*4*/ -11 }, /* index 8: 7 bits: 111110x */
96 { 10, 11 }, /* index 9: 7 bits: 111111x */
97 { /*-5*/ -20, /*5*/ -10 }, /* index 10: 8 bits: 1111110x */
98 { 12, 13 }, /* index 11: 8 bits: 1111111x */
99 { /*-6*/ -21, /*6*/ -9 }, /* index 12: 9 bits: 11111110x */
100 { /*-7*/ -22, 14 }, /* index 13: 9 bits: 11111111x */
101 { /*7*/ -8, 15 }, /* index 14: 10 bits: 111111111x */
102 { 16, 17 }, /* index 15: 11 bits: 1111111111x */
103 { /*-8*/ -23, /*8*/ -7 }, /* index 16: 12 bits: 11111111110x */
104 { 18, 19 }, /* index 17: 12 bits: 11111111111x */
105 { /*-10*/ -25, 20 }, /* index 18: 13 bits: 111111111110x */
106 { 21, 22 }, /* index 19: 13 bits: 111111111111x */
107 { /*-9*/ -24, /*9*/ -6 }, /* index 20: 14 bits: 1111111111101x */
108 { /*10*/ -5, 23 }, /* index 21: 14 bits: 1111111111110x */
109 { 24, 25 }, /* index 22: 14 bits: 1111111111111x */
110 { /*-13*/ -28, /*-11*/ -26 }, /* index 23: 15 bits: 11111111111101x */
111 { /*11*/ -4, /*13*/ -2 }, /* index 24: 15 bits: 11111111111110x */
112 { 26, 27 }, /* index 25: 15 bits: 11111111111111x */
113 { /*-14*/ -29, /*-12*/ -27 }, /* index 26: 16 bits: 111111111111110x */
114 { /*12*/ -3, /*14*/ -1 } /* index 27: 16 bits: 111111111111111x */
115};
116
117static const int8_t t_huffman_pan[][2] = {
118 { /*0*/ -15, 1 }, /* index 0: 1 bits: x */
119 { /*-1*/ -16, 2 }, /* index 1: 2 bits: 1x */
120 { /*1*/ -14, 3 }, /* index 2: 3 bits: 11x */
121 { /*-2*/ -17, 4 }, /* index 3: 4 bits: 111x */
122 { /*2*/ -13, 5 }, /* index 4: 5 bits: 1111x */
123 { /*-3*/ -18, 6 }, /* index 5: 6 bits: 11111x */
124 { /*3*/ -12, 7 }, /* index 6: 7 bits: 111111x */
125 { /*-4*/ -19, 8 }, /* index 7: 8 bits: 1111111x */
126 { /*4*/ -11, 9 }, /* index 8: 9 bits: 11111111x */
127 { 10, 11 }, /* index 9: 10 bits: 111111111x */
128 { /*-5*/ -20, /*5*/ -10 }, /* index 10: 11 bits: 1111111110x */
129 { 12, 13 }, /* index 11: 11 bits: 1111111111x */
130 { /*-6*/ -21, /*6*/ -9 }, /* index 12: 12 bits: 11111111110x */
131 { 14, 15 }, /* index 13: 12 bits: 11111111111x */
132 { /*-7*/ -22, /*7*/ -8 }, /* index 14: 13 bits: 111111111110x */
133 { 16, 17 }, /* index 15: 13 bits: 111111111111x */
134 { /*-8*/ -23, /*8*/ -7 }, /* index 16: 14 bits: 1111111111110x */
135 { 18, 19 }, /* index 17: 14 bits: 1111111111111x */
136 { /*-10*/ -25, /*10*/ -5 }, /* index 18: 15 bits: 11111111111110x */
137 { 20, 21 }, /* index 19: 15 bits: 11111111111111x */
138 { /*-9*/ -24, /*9*/ -6 }, /* index 20: 16 bits: 111111111111110x */
139 { 22, 23 }, /* index 21: 16 bits: 111111111111111x */
140 { 24, 25 }, /* index 22: 17 bits: 1111111111111110x */
141 { 26, 27 }, /* index 23: 17 bits: 1111111111111111x */
142 { /*-14*/ -29, /*-13*/ -28 }, /* index 24: 18 bits: 11111111111111100x */
143 { /*-12*/ -27, /*-11*/ -26 }, /* index 25: 18 bits: 11111111111111101x */
144 { /*11*/ -4, /*12*/ -3 }, /* index 26: 18 bits: 11111111111111110x */
145 { /*13*/ -2, /*14*/ -1 } /* index 27: 18 bits: 11111111111111111x */
146};
147
148/* There are 3 classes in the standard but the last 2 are identical */
149static const real_t sa_quant[8][2] = {
150 { FRAC_CONST(0.0000), FRAC_CONST(0.0000) },
151 { FRAC_CONST(0.0501), FRAC_CONST(0.1778) },
152 { FRAC_CONST(0.0706), FRAC_CONST(0.2818) },
153 { FRAC_CONST(0.0995), FRAC_CONST(0.4467) },
154 { FRAC_CONST(0.1399), FRAC_CONST(0.5623) },
155 { FRAC_CONST(0.1957), FRAC_CONST(0.7079) },
156 { FRAC_CONST(0.2713), FRAC_CONST(0.8913) },
157 { FRAC_CONST(0.3699), FRAC_CONST(1.0000) },
158};
159
160/* We don't need the actual quantizer values */
161#if 0
162static const real_t pan_quant[8][5] = {
163 { COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000), COEF_CONST(0.0000) },
164 { COEF_CONST(0.1661), COEF_CONST(0.1661), COEF_CONST(0.3322), COEF_CONST(0.3322), COEF_CONST(0.3322) },
165 { COEF_CONST(0.3322), COEF_CONST(0.3322), COEF_CONST(0.6644), COEF_CONST(0.8305), COEF_CONST(0.8305) },
166 { COEF_CONST(0.4983), COEF_CONST(0.6644), COEF_CONST(0.9966), COEF_CONST(1.4949), COEF_CONST(1.6610) },
167 { COEF_CONST(0.6644), COEF_CONST(0.9966), COEF_CONST(1.4949), COEF_CONST(2.1593), COEF_CONST(2.4914) },
168 { COEF_CONST(0.8305), COEF_CONST(1.3288), COEF_CONST(2.1593), COEF_CONST(2.9897), COEF_CONST(3.4880) },
169 { COEF_CONST(0.9966), COEF_CONST(1.8271), COEF_CONST(2.8236), COEF_CONST(3.8202), COEF_CONST(4.6507) },
170 { COEF_CONST(1.3288), COEF_CONST(2.3253), COEF_CONST(3.4880), COEF_CONST(4.6507), COEF_CONST(5.8134) },
171};
172#endif
173
174/* 2^(pan_quant[x][y] */
175static const real_t pan_pow_2_pos[8][5] = {
176 { REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000), REAL_CONST(1.0000000) },
177 { REAL_CONST(1.1220021), REAL_CONST(1.1220021), REAL_CONST(1.2589312), REAL_CONST(1.2589312), REAL_CONST(1.2589312) },
178 { REAL_CONST(1.2589312), REAL_CONST(1.2589312), REAL_CONST(1.5849090), REAL_CONST(1.7783016), REAL_CONST(1.7783016) },
179 { REAL_CONST(1.4125481), REAL_CONST(1.5849090), REAL_CONST(1.9952921), REAL_CONST(2.8184461), REAL_CONST(3.1623565) },
180 { REAL_CONST(1.5849090), REAL_CONST(1.9952922), REAL_CONST(2.8184461), REAL_CONST(4.4669806), REAL_CONST(5.6232337) },
181 { REAL_CONST(1.7783016), REAL_CONST(2.5119365), REAL_CONST(4.4669806), REAL_CONST(7.9430881), REAL_CONST(11.219994) },
182 { REAL_CONST(1.9952921), REAL_CONST(3.5482312), REAL_CONST(7.0792671), REAL_CONST(14.125206), REAL_CONST(25.118876) },
183 { REAL_CONST(2.5119365), REAL_CONST(5.0116998), REAL_CONST(11.219994), REAL_CONST(25.118876), REAL_CONST(56.235140) }
184};
185
186/* 2^(-pan_quant[x][y] */
187static const real_t pan_pow_2_neg[8][5] = {
188 { REAL_CONST(1), REAL_CONST(1), REAL_CONST(1), REAL_CONST(1), REAL_CONST(1) },
189 { REAL_CONST(0.8912487), REAL_CONST(0.8912487), REAL_CONST(0.7943242), REAL_CONST(0.7943242), REAL_CONST(0.7943242) },
190 { REAL_CONST(0.7943242), REAL_CONST(0.7943242), REAL_CONST(0.6309511), REAL_CONST(0.5623344), REAL_CONST(0.5623344) },
191 { REAL_CONST(0.7079405), REAL_CONST(0.6309511), REAL_CONST(0.5011797), REAL_CONST(0.3548054), REAL_CONST(0.3162199) },
192 { REAL_CONST(0.6309511), REAL_CONST(0.5011797), REAL_CONST(0.3548054), REAL_CONST(0.2238649), REAL_CONST(0.1778336) },
193 { REAL_CONST(0.5623343), REAL_CONST(0.3980992), REAL_CONST(0.2238649), REAL_CONST(0.1258956), REAL_CONST(0.0891266) },
194 { REAL_CONST(0.5011797), REAL_CONST(0.2818306), REAL_CONST(0.1412576), REAL_CONST(0.0707954), REAL_CONST(0.0398107) },
195 { REAL_CONST(0.3980992), REAL_CONST(0.1995331), REAL_CONST(0.0891267), REAL_CONST(0.0398107), REAL_CONST(0.0177825) }
196};
197
198/* 2^(pan_quant[x][y]/30) */
199static const real_t pan_pow_2_30_pos[8][5] = {
200 { COEF_CONST(1), COEF_CONST(1), COEF_CONST(1), COEF_CONST(1), COEF_CONST(1) },
201 { COEF_CONST(1.003845098), COEF_CONST(1.003845098), COEF_CONST(1.007704982), COEF_CONST(1.007704982), COEF_CONST(1.007704982) },
202 { COEF_CONST(1.007704982), COEF_CONST(1.007704982), COEF_CONST(1.01546933), COEF_CONST(1.019373909), COEF_CONST(1.019373909) },
203 { COEF_CONST(1.011579706), COEF_CONST(1.01546933), COEF_CONST(1.023293502), COEF_CONST(1.035142941), COEF_CONST(1.039123167) },
204 { COEF_CONST(1.01546933), COEF_CONST(1.023293502), COEF_CONST(1.035142941), COEF_CONST(1.051155908), COEF_CONST(1.059252598) },
205 { COEF_CONST(1.019373909), COEF_CONST(1.03117796), COEF_CONST(1.051155908), COEF_CONST(1.071518432), COEF_CONST(1.0839263) },
206 { COEF_CONST(1.023293502), COEF_CONST(1.043118698), COEF_CONST(1.067414119), COEF_CONST(1.092277933), COEF_CONST(1.113439626) },
207 { COEF_CONST(1.03117796), COEF_CONST(1.055195268), COEF_CONST(1.0839263), COEF_CONST(1.113439626), COEF_CONST(1.143756546) }
208};
209
210/* 2^(-pan_quant[x][y]/30) */
211static const real_t pan_pow_2_30_neg[8][5] = {
212 { COEF_CONST(1), COEF_CONST(1), COEF_CONST(1), COEF_CONST(1), COEF_CONST(1) },
213 { COEF_CONST(0.99616963), COEF_CONST(0.99616963), COEF_CONST(0.992353931), COEF_CONST(0.992353931), COEF_CONST(0.99235393) },
214 { COEF_CONST(0.992353931), COEF_CONST(0.992353931), COEF_CONST(0.984766325), COEF_CONST(0.980994305), COEF_CONST(0.980994305) },
215 { COEF_CONST(0.988552848), COEF_CONST(0.984766325), COEF_CONST(0.977236734), COEF_CONST(0.966050157), COEF_CONST(0.962349827) },
216 { COEF_CONST(0.984766325), COEF_CONST(0.977236734), COEF_CONST(0.966050157), COEF_CONST(0.951333663), COEF_CONST(0.944061881) },
217 { COEF_CONST(0.980994305), COEF_CONST(0.969764715), COEF_CONST(0.951333663), COEF_CONST(0.933255062), COEF_CONST(0.922571949) },
218 { COEF_CONST(0.977236734), COEF_CONST(0.958663671), COEF_CONST(0.936843519), COEF_CONST(0.915517901), COEF_CONST(0.898117847) },
219 { COEF_CONST(0.969764715), COEF_CONST(0.947691892), COEF_CONST(0.922571949), COEF_CONST(0.898117847), COEF_CONST(0.874311936) }
220};
221
222static const real_t g_decayslope[MAX_SA_BAND] = {
223 FRAC_CONST(1), FRAC_CONST(1), FRAC_CONST(1), FRAC_CONST(0.95), FRAC_CONST(0.9), FRAC_CONST(0.85), FRAC_CONST(0.8),
224 FRAC_CONST(0.75), FRAC_CONST(0.7), FRAC_CONST(0.65), FRAC_CONST(0.6), FRAC_CONST(0.55), FRAC_CONST(0.5), FRAC_CONST(0.45),
225 FRAC_CONST(0.4), FRAC_CONST(0.35), FRAC_CONST(0.3), FRAC_CONST(0.25), FRAC_CONST(0.2), FRAC_CONST(0.15), FRAC_CONST(0.1),
226 FRAC_CONST(0.05), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0),
227 FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0),
228 FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0),
229 FRAC_CONST(0), FRAC_CONST(0), FRAC_CONST(0)
230};
231
232static const real_t sa_sqrt_1_minus[8][2] = {
233 { FRAC_CONST(1), FRAC_CONST(1) },
234 { FRAC_CONST(0.998744206), FRAC_CONST(0.984066644) },
235 { FRAC_CONST(0.997504707), FRAC_CONST(0.959473168) },
236 { FRAC_CONST(0.995037562), FRAC_CONST(0.894683804) },
237 { FRAC_CONST(0.990165638), FRAC_CONST(0.826933317) },
238 { FRAC_CONST(0.980663811), FRAC_CONST(0.706312672) },
239 { FRAC_CONST(0.962494836), FRAC_CONST(0.45341406) },
240 { FRAC_CONST(0.929071574), FRAC_CONST(0) }
241};
242
243static const uint8_t sa_freq_scale[9] = {
244 0, 1, 2, 3, 5, 7, 10, 13, 23
245};
246
247static const uint8_t pan_freq_scale[21] = {
248 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
249 11, 12, 13, 14, 15, 18, 22, 26, 32, 64
250};
251
252static const uint8_t pan_quant_class[20] = {
253 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
254 2, 2, 2, 2, 3, 3, 3, 4, 4, 4
255};
256
257/* Inverse mapping lookup */
258static const uint8_t pan_inv_freq[64] = {
259 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
260 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18,
261 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
262 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19
263};
264
265static const uint8_t sa_inv_freq[MAX_SA_BAND] = {
266 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6,
267 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
268 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
269 7, 7, 7, 7, 7, 7, 7
270};
271
272static const real_t filter_coeff[] = {
273 FRAC_CONST(0.65143905754106),
274 FRAC_CONST(0.56471812200776),
275 FRAC_CONST(0.48954165955695)
276};
277
278static const uint8_t delay_length[3] = {
279 3, 4, 5
280};
281
282static const real_t delay_fraction[] = {
283 FRAC_CONST(0.43), FRAC_CONST(0.75), FRAC_CONST(0.347)
284};
285
286static const real_t peak_decay = FRAC_CONST(0.76592833836465);
287
288static const real_t smooth_coeff = FRAC_CONST(0.25);
289
290/* Please note that these are the same tables as in plain PS */
291static const complex_t Q_Fract_allpass_Qmf[][3] = {
292 { { FRAC_CONST(0.7804303765), FRAC_CONST(0.6252426505) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.8550928831), FRAC_CONST(0.5184748173) } },
293 { { FRAC_CONST(-0.4399392009), FRAC_CONST(0.8980275393) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.0643581524), FRAC_CONST(0.9979268909) } },
294 { { FRAC_CONST(-0.9723699093), FRAC_CONST(-0.2334454209) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.9146071672), FRAC_CONST(0.4043435752) } },
295 { { FRAC_CONST(0.0157073960), FRAC_CONST(-0.9998766184) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.7814115286), FRAC_CONST(-0.6240159869) } },
296 { { FRAC_CONST(0.9792228341), FRAC_CONST(-0.2027871907) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.1920081824), FRAC_CONST(-0.9813933372) } },
297 { { FRAC_CONST(0.4115142524), FRAC_CONST(0.9114032984) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.9589683414), FRAC_CONST(-0.2835132182) } },
298 { { FRAC_CONST(-0.7996847630), FRAC_CONST(0.6004201174) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.6947838664), FRAC_CONST(0.7192186117) } },
299 { { FRAC_CONST(-0.7604058385), FRAC_CONST(-0.6494481564) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.3164770305), FRAC_CONST(0.9486001730) } },
300 { { FRAC_CONST(0.4679299891), FRAC_CONST(-0.8837655187) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.9874414206), FRAC_CONST(0.1579856575) } },
301 { { FRAC_CONST(0.9645573497), FRAC_CONST(0.2638732493) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.5966450572), FRAC_CONST(-0.8025052547) } },
302 { { FRAC_CONST(-0.0471066870), FRAC_CONST(0.9988898635) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.4357025325), FRAC_CONST(-0.9000906944) } },
303 { { FRAC_CONST(-0.9851093888), FRAC_CONST(0.1719288528) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9995546937), FRAC_CONST(-0.0298405960) } },
304 { { FRAC_CONST(-0.3826831877), FRAC_CONST(-0.9238796234) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.4886211455), FRAC_CONST(0.8724960685) } },
305 { { FRAC_CONST(0.8181498647), FRAC_CONST(-0.5750049949) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.5477093458), FRAC_CONST(0.8366686702) } },
306 { { FRAC_CONST(0.7396308780), FRAC_CONST(0.6730127335) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9951074123), FRAC_CONST(-0.0987988561) } },
307 { { FRAC_CONST(-0.4954589605), FRAC_CONST(0.8686313629) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.3725017905), FRAC_CONST(-0.9280315042) } },
308 { { FRAC_CONST(-0.9557929039), FRAC_CONST(-0.2940406799) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.6506417990), FRAC_CONST(-0.7593847513) } },
309 { { FRAC_CONST(0.0784594864), FRAC_CONST(-0.9969173074) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9741733670), FRAC_CONST(0.2258014232) } },
310 { { FRAC_CONST(0.9900237322), FRAC_CONST(-0.1409008205) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.2502108514), FRAC_CONST(0.9681913853) } },
311 { { FRAC_CONST(0.3534744382), FRAC_CONST(0.9354441762) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.7427945137), FRAC_CONST(0.6695194840) } },
312 { { FRAC_CONST(-0.8358076215), FRAC_CONST(0.5490224361) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9370992780), FRAC_CONST(-0.3490629196) } },
313 { { FRAC_CONST(-0.7181259394), FRAC_CONST(-0.6959131360) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.1237744763), FRAC_CONST(-0.9923103452) } },
314 { { FRAC_CONST(0.5224990249), FRAC_CONST(-0.8526399136) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.8226406574), FRAC_CONST(-0.5685616732) } },
315 { { FRAC_CONST(0.9460852146), FRAC_CONST(0.3239179254) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.8844994903), FRAC_CONST(0.4665412009) } },
316 { { FRAC_CONST(-0.1097348556), FRAC_CONST(0.9939609170) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.0047125919), FRAC_CONST(0.9999889135) } },
317 { { FRAC_CONST(-0.9939610362), FRAC_CONST(0.1097337380) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8888573647), FRAC_CONST(0.4581840038) } },
318 { { FRAC_CONST(-0.3239168525), FRAC_CONST(-0.9460855722) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8172453642), FRAC_CONST(-0.5762898922) } },
319 { { FRAC_CONST(0.8526405096), FRAC_CONST(-0.5224980116) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.1331215799), FRAC_CONST(-0.9910997152) } },
320 { { FRAC_CONST(0.6959123611), FRAC_CONST(0.7181267142) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.9403476119), FRAC_CONST(-0.3402152061) } },
321 { { FRAC_CONST(-0.5490233898), FRAC_CONST(0.8358070254) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.7364512086), FRAC_CONST(0.6764906645) } },
322 { { FRAC_CONST(-0.9354437590), FRAC_CONST(-0.3534754813) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.2593250275), FRAC_CONST(0.9657900929) } },
323 { { FRAC_CONST(0.1409019381), FRAC_CONST(-0.9900235534) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9762582779), FRAC_CONST(0.2166097313) } },
324 { { FRAC_CONST(0.9969173670), FRAC_CONST(-0.0784583688) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.6434556246), FRAC_CONST(-0.7654833794) } },
325 { { FRAC_CONST(0.2940396070), FRAC_CONST(0.9557932615) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.3812320232), FRAC_CONST(-0.9244794250) } },
326 { { FRAC_CONST(-0.8686318994), FRAC_CONST(0.4954580069) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9959943891), FRAC_CONST(-0.0894154981) } },
327 { { FRAC_CONST(-0.6730118990), FRAC_CONST(-0.7396316528) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.5397993922), FRAC_CONST(0.8417937160) } },
328 { { FRAC_CONST(0.5750059485), FRAC_CONST(-0.8181492686) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.4968227744), FRAC_CONST(0.8678520322) } },
329 { { FRAC_CONST(0.9238792062), FRAC_CONST(0.3826842010) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9992290139), FRAC_CONST(-0.0392601527) } },
330 { { FRAC_CONST(-0.1719299555), FRAC_CONST(0.9851091504) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.4271997511), FRAC_CONST(-0.9041572809) } },
331 { { FRAC_CONST(-0.9988899231), FRAC_CONST(0.0471055657) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.6041822433), FRAC_CONST(-0.7968461514) } },
332 { { FRAC_CONST(-0.2638721764), FRAC_CONST(-0.9645576477) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9859085083), FRAC_CONST(0.1672853529) } },
333 { { FRAC_CONST(0.8837660551), FRAC_CONST(-0.4679289758) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.3075223565), FRAC_CONST(0.9515408874) } },
334 { { FRAC_CONST(0.6494473219), FRAC_CONST(0.7604066133) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.7015317082), FRAC_CONST(0.7126382589) } },
335 { { FRAC_CONST(-0.6004210114), FRAC_CONST(0.7996840477) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.9562535882), FRAC_CONST(-0.2925389707) } },
336 { { FRAC_CONST(-0.9114028811), FRAC_CONST(-0.4115152657) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.1827499419), FRAC_CONST(-0.9831594229) } },
337 { { FRAC_CONST(0.2027882934), FRAC_CONST(-0.9792225957) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.7872582674), FRAC_CONST(-0.6166234016) } },
338 { { FRAC_CONST(0.9998766780), FRAC_CONST(-0.0157062728) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.9107555747), FRAC_CONST(0.4129458666) } },
339 { { FRAC_CONST(0.2334443331), FRAC_CONST(0.9723701477) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.0549497530), FRAC_CONST(0.9984891415) } },
340 { { FRAC_CONST(-0.8980280757), FRAC_CONST(0.4399381876) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.8599416018), FRAC_CONST(0.5103924870) } },
341 { { FRAC_CONST(-0.6252418160), FRAC_CONST(-0.7804310918) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(-0.8501682281), FRAC_CONST(-0.5265110731) } },
342 { { FRAC_CONST(0.6252435446), FRAC_CONST(-0.7804297209) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.0737608299), FRAC_CONST(-0.9972759485) } },
343 { { FRAC_CONST(0.8980270624), FRAC_CONST(0.4399402142) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.9183775187), FRAC_CONST(-0.3957053721) } },
344 { { FRAC_CONST(-0.2334465086), FRAC_CONST(0.9723696709) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.7754954696), FRAC_CONST(0.6313531399) } },
345 { { FRAC_CONST(-0.9998766184), FRAC_CONST(-0.0157085191) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.2012493610), FRAC_CONST(0.9795400500) } },
346 { { FRAC_CONST(-0.2027861029), FRAC_CONST(-0.9792230725) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.9615978599), FRAC_CONST(0.2744622827) } },
347 { { FRAC_CONST(0.9114037752), FRAC_CONST(-0.4115132093) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.6879743338), FRAC_CONST(-0.7257350087) } },
348 { { FRAC_CONST(0.6004192233), FRAC_CONST(0.7996854186) }, { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(0.3254036009), FRAC_CONST(-0.9455752373) } },
349 { { FRAC_CONST(-0.6494490504), FRAC_CONST(0.7604051232) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.9888865948), FRAC_CONST(-0.1486719251) } },
350 { { FRAC_CONST(-0.8837650418), FRAC_CONST(-0.4679309726) }, { FRAC_CONST(0.9238795042), FRAC_CONST(-0.3826834261) }, { FRAC_CONST(0.5890548825), FRAC_CONST(0.8080930114) } },
351 { { FRAC_CONST(0.2638743520), FRAC_CONST(-0.9645570517) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) }, { FRAC_CONST(-0.4441666007), FRAC_CONST(0.8959442377) } },
352 { { FRAC_CONST(0.9988898039), FRAC_CONST(0.0471078083) }, { FRAC_CONST(-0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(-0.9997915030), FRAC_CONST(0.0204183888) } },
353 { { FRAC_CONST(0.1719277352), FRAC_CONST(0.9851095676) }, { FRAC_CONST(0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(-0.4803760946), FRAC_CONST(-0.8770626187) } },
354 { { FRAC_CONST(-0.9238800406), FRAC_CONST(0.3826821446) }, { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) }, { FRAC_CONST(0.5555707216), FRAC_CONST(-0.8314692974) } },
355 { { FRAC_CONST(-0.5750041008), FRAC_CONST(-0.8181505203) }, { FRAC_CONST(0.3826834261), FRAC_CONST(-0.9238795042) }, { FRAC_CONST(0.9941320419), FRAC_CONST(0.1081734300) } }
356};
357
358static const complex_t Phi_Fract_Qmf[] = {
359 { FRAC_CONST(0.8181497455), FRAC_CONST(0.5750052333) },
360 { FRAC_CONST(-0.2638730407), FRAC_CONST(0.9645574093) },
361 { FRAC_CONST(-0.9969173074), FRAC_CONST(0.0784590989) },
362 { FRAC_CONST(-0.4115143716), FRAC_CONST(-0.9114032984) },
363 { FRAC_CONST(0.7181262970), FRAC_CONST(-0.6959127784) },
364 { FRAC_CONST(0.8980275989), FRAC_CONST(0.4399391711) },
365 { FRAC_CONST(-0.1097343117), FRAC_CONST(0.9939609766) },
366 { FRAC_CONST(-0.9723699093), FRAC_CONST(0.2334453613) },
367 { FRAC_CONST(-0.5490227938), FRAC_CONST(-0.8358073831) },
368 { FRAC_CONST(0.6004202366), FRAC_CONST(-0.7996846437) },
369 { FRAC_CONST(0.9557930231), FRAC_CONST(0.2940403223) },
370 { FRAC_CONST(0.0471064523), FRAC_CONST(0.9988898635) },
371 { FRAC_CONST(-0.9238795042), FRAC_CONST(0.3826834261) },
372 { FRAC_CONST(-0.6730124950), FRAC_CONST(-0.7396311164) },
373 { FRAC_CONST(0.4679298103), FRAC_CONST(-0.8837656379) },
374 { FRAC_CONST(0.9900236726), FRAC_CONST(0.1409012377) },
375 { FRAC_CONST(0.2027872950), FRAC_CONST(0.9792228341) },
376 { FRAC_CONST(-0.8526401520), FRAC_CONST(0.5224985480) },
377 { FRAC_CONST(-0.7804304361), FRAC_CONST(-0.6252426505) },
378 { FRAC_CONST(0.3239174187), FRAC_CONST(-0.9460853338) },
379 { FRAC_CONST(0.9998766184), FRAC_CONST(-0.0157073177) },
380 { FRAC_CONST(0.3534748554), FRAC_CONST(0.9354440570) },
381 { FRAC_CONST(-0.7604059577), FRAC_CONST(0.6494480371) },
382 { FRAC_CONST(-0.8686315417), FRAC_CONST(-0.4954586625) },
383 { FRAC_CONST(0.1719291061), FRAC_CONST(-0.9851093292) },
384 { FRAC_CONST(0.9851093292), FRAC_CONST(-0.1719291061) },
385 { FRAC_CONST(0.4954586625), FRAC_CONST(0.8686315417) },
386 { FRAC_CONST(-0.6494480371), FRAC_CONST(0.7604059577) },
387 { FRAC_CONST(-0.9354440570), FRAC_CONST(-0.3534748554) },
388 { FRAC_CONST(0.0157073177), FRAC_CONST(-0.9998766184) },
389 { FRAC_CONST(0.9460853338), FRAC_CONST(-0.3239174187) },
390 { FRAC_CONST(0.6252426505), FRAC_CONST(0.7804304361) },
391 { FRAC_CONST(-0.5224985480), FRAC_CONST(0.8526401520) },
392 { FRAC_CONST(-0.9792228341), FRAC_CONST(-0.2027872950) },
393 { FRAC_CONST(-0.1409012377), FRAC_CONST(-0.9900236726) },
394 { FRAC_CONST(0.8837656379), FRAC_CONST(-0.4679298103) },
395 { FRAC_CONST(0.7396311164), FRAC_CONST(0.6730124950) },
396 { FRAC_CONST(-0.3826834261), FRAC_CONST(0.9238795042) },
397 { FRAC_CONST(-0.9988898635), FRAC_CONST(-0.0471064523) },
398 { FRAC_CONST(-0.2940403223), FRAC_CONST(-0.9557930231) },
399 { FRAC_CONST(0.7996846437), FRAC_CONST(-0.6004202366) },
400 { FRAC_CONST(0.8358073831), FRAC_CONST(0.5490227938) },
401 { FRAC_CONST(-0.2334453613), FRAC_CONST(0.9723699093) },
402 { FRAC_CONST(-0.9939609766), FRAC_CONST(0.1097343117) },
403 { FRAC_CONST(-0.4399391711), FRAC_CONST(-0.8980275989) },
404 { FRAC_CONST(0.6959127784), FRAC_CONST(-0.7181262970) },
405 { FRAC_CONST(0.9114032984), FRAC_CONST(0.4115143716) },
406 { FRAC_CONST(-0.0784590989), FRAC_CONST(0.9969173074) },
407 { FRAC_CONST(-0.9645574093), FRAC_CONST(0.2638730407) },
408 { FRAC_CONST(-0.5750052333), FRAC_CONST(-0.8181497455) },
409 { FRAC_CONST(0.5750052333), FRAC_CONST(-0.8181497455) },
410 { FRAC_CONST(0.9645574093), FRAC_CONST(0.2638730407) },
411 { FRAC_CONST(0.0784590989), FRAC_CONST(0.9969173074) },
412 { FRAC_CONST(-0.9114032984), FRAC_CONST(0.4115143716) },
413 { FRAC_CONST(-0.6959127784), FRAC_CONST(-0.7181262970) },
414 { FRAC_CONST(0.4399391711), FRAC_CONST(-0.8980275989) },
415 { FRAC_CONST(0.9939609766), FRAC_CONST(0.1097343117) },
416 { FRAC_CONST(0.2334453613), FRAC_CONST(0.9723699093) },
417 { FRAC_CONST(-0.8358073831), FRAC_CONST(0.5490227938) },
418 { FRAC_CONST(-0.7996846437), FRAC_CONST(-0.6004202366) },
419 { FRAC_CONST(0.2940403223), FRAC_CONST(-0.9557930231) },
420 { FRAC_CONST(0.9988898635), FRAC_CONST(-0.0471064523) },
421 { FRAC_CONST(0.3826834261), FRAC_CONST(0.9238795042) },
422 { FRAC_CONST(-0.7396311164), FRAC_CONST(0.6730124950) }
423};
424
425
426/* static function declarations */
427static void drm_ps_sa_element(drm_ps_info *ps, bitfile *ld);
428static void drm_ps_pan_element(drm_ps_info *ps, bitfile *ld);
429static int8_t huff_dec(bitfile *ld, drm_ps_huff_tab huff);
430
431
432uint16_t drm_ps_data(drm_ps_info *ps, bitfile *ld)
433{
434 uint16_t bits = (uint16_t)faad_get_processed_bits(ld);
435
436 ps->drm_ps_data_available = 1;
437
438 ps->bs_enable_sa = faad_get1bit(ld);
439 ps->bs_enable_pan = faad_get1bit(ld);
440
441 if (ps->bs_enable_sa) {
442 drm_ps_sa_element(ps, ld);
443 }
444
445 if (ps->bs_enable_pan) {
446 drm_ps_pan_element(ps, ld);
447 }
448
449 bits = (uint16_t)faad_get_processed_bits(ld) - bits;
450
451 return bits;
452}
453
454static void drm_ps_sa_element(drm_ps_info *ps, bitfile *ld)
455{
456 drm_ps_huff_tab huff;
457 uint8_t band;
458
459 ps->bs_sa_dt_flag = faad_get1bit(ld);
460 if (ps->bs_sa_dt_flag) {
461 huff = t_huffman_sa;
462 } else {
463 huff = f_huffman_sa;
464 }
465
466 for (band = 0; band < DRM_NUM_SA_BANDS; band++) {
467 ps->bs_sa_data[band] = huff_dec(ld, huff);
468 }
469}
470
471static void drm_ps_pan_element(drm_ps_info *ps, bitfile *ld)
472{
473 drm_ps_huff_tab huff;
474 uint8_t band;
475
476 ps->bs_pan_dt_flag = faad_get1bit(ld);
477 if (ps->bs_pan_dt_flag) {
478 huff = t_huffman_pan;
479 } else {
480 huff = f_huffman_pan;
481 }
482
483 for (band = 0; band < DRM_NUM_PAN_BANDS; band++) {
484 ps->bs_pan_data[band] = huff_dec(ld, huff);
485 }
486}
487
488/* binary search huffman decoding */
489static int8_t huff_dec(bitfile *ld, drm_ps_huff_tab huff)
490{
491 uint8_t bit;
492 int16_t index = 0;
493
494 while (index >= 0) {
495 bit = (uint8_t)faad_get1bit(ld);
496 index = huff[index][bit];
497 }
498
499 return index + 15;
500}
501
502
503static int8_t sa_delta_clip(drm_ps_info *ps, int8_t i)
504{
505 if (i < 0) {
506 /* printf(" SAminclip %d", i); */
507 ps->sa_decode_error = 1;
508 return 0;
509 } else if (i > 7) {
510 /* printf(" SAmaxclip %d", i); */
511 ps->sa_decode_error = 1;
512 return 7;
513 } else {
514 return i;
515 }
516}
517
518static int8_t pan_delta_clip(drm_ps_info *ps, int8_t i)
519{
520 if (i < -7) {
521 /* printf(" PANminclip %d", i); */
522 ps->pan_decode_error = 1;
523 return -7;
524 } else if (i > 7) {
525 /* printf(" PANmaxclip %d", i); */
526 ps->pan_decode_error = 1;
527 return 7;
528 } else {
529 return i;
530 }
531}
532
533static void drm_ps_delta_decode(drm_ps_info *ps)
534{
535 uint8_t band;
536
537 if (ps->bs_enable_sa) {
538 if (ps->bs_sa_dt_flag && !ps->g_last_had_sa) {
539 /* wait until we get a DT frame */
540 ps->bs_enable_sa = 0;
541 } else if (ps->bs_sa_dt_flag) {
542 /* DT frame, we have a last frame, so we can decode */
543 ps->g_sa_index[0] = sa_delta_clip(ps, ps->g_prev_sa_index[0] + ps->bs_sa_data[0]);
544 } else {
545 /* DF always decodable */
546 ps->g_sa_index[0] = sa_delta_clip(ps, ps->bs_sa_data[0]);
547 }
548
549 for (band = 1; band < DRM_NUM_SA_BANDS; band++) {
550 if (ps->bs_sa_dt_flag && ps->g_last_had_sa) {
551 ps->g_sa_index[band] = sa_delta_clip(ps, ps->g_prev_sa_index[band] + ps->bs_sa_data[band]);
552 } else if (!ps->bs_sa_dt_flag) {
553 ps->g_sa_index[band] = sa_delta_clip(ps, ps->g_sa_index[band - 1] + ps->bs_sa_data[band]);
554 }
555 }
556 }
557
558 /* An error during SA decoding implies PAN data will be undecodable, too */
559 /* Also, we don't like on/off switching in PS, so we force to last settings */
560 if (ps->sa_decode_error) {
561 ps->pan_decode_error = 1;
562 ps->bs_enable_pan = ps->g_last_had_pan;
563 ps->bs_enable_sa = ps->g_last_had_sa;
564 }
565
566
567 if (ps->bs_enable_sa) {
568 if (ps->sa_decode_error) {
569 for (band = 0; band < DRM_NUM_SA_BANDS; band++) {
570 ps->g_sa_index[band] = ps->g_last_good_sa_index[band];
571 }
572 } else {
573 for (band = 0; band < DRM_NUM_SA_BANDS; band++) {
574 ps->g_last_good_sa_index[band] = ps->g_sa_index[band];
575 }
576 }
577 }
578
579 if (ps->bs_enable_pan) {
580 if (ps->bs_pan_dt_flag && !ps->g_last_had_pan) {
581 ps->bs_enable_pan = 0;
582 } else if (ps->bs_pan_dt_flag) {
583 ps->g_pan_index[0] = pan_delta_clip(ps, ps->g_prev_pan_index[0] + ps->bs_pan_data[0]);
584 } else {
585 ps->g_pan_index[0] = pan_delta_clip(ps, ps->bs_pan_data[0]);
586 }
587
588 for (band = 1; band < DRM_NUM_PAN_BANDS; band++) {
589 if (ps->bs_pan_dt_flag && ps->g_last_had_pan) {
590 ps->g_pan_index[band] = pan_delta_clip(ps, ps->g_prev_pan_index[band] + ps->bs_pan_data[band]);
591 } else if (!ps->bs_pan_dt_flag) {
592 ps->g_pan_index[band] = pan_delta_clip(ps, ps->g_pan_index[band - 1] + ps->bs_pan_data[band]);
593 }
594 }
595
596 if (ps->pan_decode_error) {
597 for (band = 0; band < DRM_NUM_PAN_BANDS; band++) {
598 ps->g_pan_index[band] = ps->g_last_good_pan_index[band];
599 }
600 } else {
601 for (band = 0; band < DRM_NUM_PAN_BANDS; band++) {
602 ps->g_last_good_pan_index[band] = ps->g_pan_index[band];
603 }
604 }
605 }
606}
607
608static void drm_calc_sa_side_signal(drm_ps_info *ps, qmf_t X[38][64])
609{
610 uint8_t s, b, k;
611 complex_t qfrac, tmp0, tmp, in, R0;
612 real_t peakdiff;
613 real_t nrg;
614 real_t power;
615 real_t transratio;
616 real_t new_delay_slopes[NUM_OF_LINKS];
617 uint8_t temp_delay_ser[NUM_OF_LINKS];
618 complex_t Phi_Fract;
619#ifdef FIXED_POINT
620 uint32_t in_re, in_im;
621#endif
622
623 for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++) {
624 /* set delay indices */
625 for (k = 0; k < NUM_OF_LINKS; k++) {
626 temp_delay_ser[k] = ps->delay_buf_index_ser[k];
627 }
628
629 RE(Phi_Fract) = RE(Phi_Fract_Qmf[b]);
630 IM(Phi_Fract) = IM(Phi_Fract_Qmf[b]);
631
632 for (s = 0; s < NUM_OF_SUBSAMPLES; s++) {
633 const real_t gamma = REAL_CONST(1.5);
634 const real_t sigma = REAL_CONST(1.5625);
635
636 RE(in) = QMF_RE(X[s][b]);
637 IM(in) = QMF_IM(X[s][b]);
638
639#ifdef FIXED_POINT
640 /* NOTE: all input is scaled by 2^(-5) because of fixed point QMF
641 * meaning that P will be scaled by 2^(-10) compared to floating point version
642 */
643 in_re = ((abs(RE(in)) + (1 << (REAL_BITS - 1))) >> REAL_BITS);
644 in_im = ((abs(IM(in)) + (1 << (REAL_BITS - 1))) >> REAL_BITS);
645 power = in_re * in_re + in_im * in_im;
646#else
647 power = MUL_R(RE(in), RE(in)) + MUL_R(IM(in), IM(in));
648#endif
649
650 ps->peakdecay_fast[b] = MUL_F(ps->peakdecay_fast[b], peak_decay);
651 if (ps->peakdecay_fast[b] < power) {
652 ps->peakdecay_fast[b] = power;
653 }
654
655 peakdiff = ps->prev_peakdiff[b];
656 peakdiff += MUL_F((ps->peakdecay_fast[b] - power - ps->prev_peakdiff[b]), smooth_coeff);
657 ps->prev_peakdiff[b] = peakdiff;
658
659 nrg = ps->prev_nrg[b];
660 nrg += MUL_F((power - ps->prev_nrg[b]), smooth_coeff);
661 ps->prev_nrg[b] = nrg;
662
663 if (MUL_R(peakdiff, gamma) <= nrg) {
664 transratio = sigma;
665 } else {
666 transratio = MUL_R(DIV_R(nrg, MUL_R(peakdiff, gamma)), sigma);
667 }
668
669 for (k = 0; k < NUM_OF_LINKS; k++) {
670 new_delay_slopes[k] = MUL_F(g_decayslope[b], filter_coeff[k]);
671 }
672
673 RE(tmp0) = RE(ps->d_buff[0][b]);
674 IM(tmp0) = IM(ps->d_buff[0][b]);
675
676 RE(ps->d_buff[0][b]) = RE(ps->d_buff[1][b]);
677 IM(ps->d_buff[0][b]) = IM(ps->d_buff[1][b]);
678
679 RE(ps->d_buff[1][b]) = RE(in);
680 IM(ps->d_buff[1][b]) = IM(in);
681
682 ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Phi_Fract), IM(Phi_Fract));
683
684 RE(R0) = RE(tmp);
685 IM(R0) = IM(tmp);
686
687 for (k = 0; k < NUM_OF_LINKS; k++) {
688 RE(qfrac) = RE(Q_Fract_allpass_Qmf[b][k]);
689 IM(qfrac) = IM(Q_Fract_allpass_Qmf[b][k]);
690
691 RE(tmp0) = RE(ps->d2_buff[k][temp_delay_ser[k]][b]);
692 IM(tmp0) = IM(ps->d2_buff[k][temp_delay_ser[k]][b]);
693
694 ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(qfrac), IM(qfrac));
695
696 RE(tmp) += -MUL_F(new_delay_slopes[k], RE(R0));
697 IM(tmp) += -MUL_F(new_delay_slopes[k], IM(R0));
698
699 RE(ps->d2_buff[k][temp_delay_ser[k]][b]) = RE(R0) + MUL_F(new_delay_slopes[k], RE(tmp));
700 IM(ps->d2_buff[k][temp_delay_ser[k]][b]) = IM(R0) + MUL_F(new_delay_slopes[k], IM(tmp));
701
702 RE(R0) = RE(tmp);
703 IM(R0) = IM(tmp);
704 }
705
706 QMF_RE(ps->SA[s][b]) = MUL_R(RE(R0), transratio);
707 QMF_IM(ps->SA[s][b]) = MUL_R(IM(R0), transratio);
708
709 for (k = 0; k < NUM_OF_LINKS; k++) {
710 if (++temp_delay_ser[k] >= delay_length[k]) {
711 temp_delay_ser[k] = 0;
712 }
713 }
714 }
715 }
716
717 for (k = 0; k < NUM_OF_LINKS; k++) {
718 ps->delay_buf_index_ser[k] = temp_delay_ser[k];
719 }
720}
721
722static void drm_add_ambiance(drm_ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
723{
724 uint8_t s, b, ifreq, qclass;
725 real_t sa_map[MAX_SA_BAND], sa_dir_map[MAX_SA_BAND], k_sa_map[MAX_SA_BAND], k_sa_dir_map[MAX_SA_BAND];
726 real_t new_dir_map, new_sa_map;
727
728 if (ps->bs_enable_sa) {
729 /* Instead of dequantization and mapping, we use an inverse mapping
730 to look up all the values we need */
731 for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++) {
732 const real_t inv_f_num_of_subsamples = FRAC_CONST(0.03333333333);
733
734 ifreq = sa_inv_freq[b];
735 qclass = (b != 0);
736
737 sa_map[b] = sa_quant[ps->g_prev_sa_index[ifreq]][qclass];
738 new_sa_map = sa_quant[ps->g_sa_index[ifreq]][qclass];
739
740 k_sa_map[b] = MUL_F(inv_f_num_of_subsamples, (new_sa_map - sa_map[b]));
741
742 sa_dir_map[b] = sa_sqrt_1_minus[ps->g_prev_sa_index[ifreq]][qclass];
743 new_dir_map = sa_sqrt_1_minus[ps->g_sa_index[ifreq]][qclass];
744
745 k_sa_dir_map[b] = MUL_F(inv_f_num_of_subsamples, (new_dir_map - sa_dir_map[b]));
746
747 }
748
749 for (s = 0; s < NUM_OF_SUBSAMPLES; s++) {
750 for (b = 0; b < sa_freq_scale[DRM_NUM_SA_BANDS]; b++) {
751 QMF_RE(X_right[s][b]) = MUL_F(QMF_RE(X_left[s][b]), sa_dir_map[b]) - MUL_F(QMF_RE(ps->SA[s][b]), sa_map[b]);
752 QMF_IM(X_right[s][b]) = MUL_F(QMF_IM(X_left[s][b]), sa_dir_map[b]) - MUL_F(QMF_IM(ps->SA[s][b]), sa_map[b]);
753 QMF_RE(X_left[s][b]) = MUL_F(QMF_RE(X_left[s][b]), sa_dir_map[b]) + MUL_F(QMF_RE(ps->SA[s][b]), sa_map[b]);
754 QMF_IM(X_left[s][b]) = MUL_F(QMF_IM(X_left[s][b]), sa_dir_map[b]) + MUL_F(QMF_IM(ps->SA[s][b]), sa_map[b]);
755
756 sa_map[b] += k_sa_map[b];
757 sa_dir_map[b] += k_sa_dir_map[b];
758 }
759 for (b = sa_freq_scale[DRM_NUM_SA_BANDS]; b < NUM_OF_QMF_CHANNELS; b++) {
760 QMF_RE(X_right[s][b]) = QMF_RE(X_left[s][b]);
761 QMF_IM(X_right[s][b]) = QMF_IM(X_left[s][b]);
762 }
763 }
764 } else {
765 for (s = 0; s < NUM_OF_SUBSAMPLES; s++) {
766 for (b = 0; b < NUM_OF_QMF_CHANNELS; b++) {
767 QMF_RE(X_right[s][b]) = QMF_RE(X_left[s][b]);
768 QMF_IM(X_right[s][b]) = QMF_IM(X_left[s][b]);
769 }
770 }
771 }
772}
773
774static void drm_add_pan(drm_ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
775{
776 uint8_t s, b, qclass, ifreq;
777 real_t tmp, coeff1, coeff2;
778 real_t pan_base[MAX_PAN_BAND];
779 real_t pan_delta[MAX_PAN_BAND];
780 qmf_t temp_l, temp_r;
781
782 if (ps->bs_enable_pan) {
783 for (b = 0; b < NUM_OF_QMF_CHANNELS; b++) {
784 /* Instead of dequantization, 20->64 mapping and 2^G(x,y) we do an
785 inverse mapping 64->20 and look up the 2^G(x,y) values directly */
786 ifreq = pan_inv_freq[b];
787 qclass = pan_quant_class[ifreq];
788
789 if (ps->g_prev_pan_index[ifreq] >= 0) {
790 pan_base[b] = pan_pow_2_pos[ps->g_prev_pan_index[ifreq]][qclass];
791 } else {
792 pan_base[b] = pan_pow_2_neg[-ps->g_prev_pan_index[ifreq]][qclass];
793 }
794
795 /* 2^((a-b)/30) = 2^(a/30) * 1/(2^(b/30)) */
796 /* a en b can be negative so we may need to inverse parts */
797 if (ps->g_pan_index[ifreq] >= 0) {
798 if (ps->g_prev_pan_index[ifreq] >= 0) {
799 pan_delta[b] = MUL_C(pan_pow_2_30_pos[ps->g_pan_index[ifreq]][qclass],
800 pan_pow_2_30_neg[ps->g_prev_pan_index[ifreq]][qclass]);
801 } else {
802 pan_delta[b] = MUL_C(pan_pow_2_30_pos[ps->g_pan_index[ifreq]][qclass],
803 pan_pow_2_30_pos[-ps->g_prev_pan_index[ifreq]][qclass]);
804 }
805 } else {
806 if (ps->g_prev_pan_index[ifreq] >= 0) {
807 pan_delta[b] = MUL_C(pan_pow_2_30_neg[-ps->g_pan_index[ifreq]][qclass],
808 pan_pow_2_30_neg[ps->g_prev_pan_index[ifreq]][qclass]);
809 } else {
810 pan_delta[b] = MUL_C(pan_pow_2_30_neg[-ps->g_pan_index[ifreq]][qclass],
811 pan_pow_2_30_pos[-ps->g_prev_pan_index[ifreq]][qclass]);
812 }
813 }
814 }
815
816 for (s = 0; s < NUM_OF_SUBSAMPLES; s++) {
817 /* PAN always uses all 64 channels */
818 for (b = 0; b < NUM_OF_QMF_CHANNELS; b++) {
819 tmp = pan_base[b];
820
821 coeff2 = DIV_R(REAL_CONST(2.0), (REAL_CONST(1.0) + tmp));
822 coeff1 = MUL_R(coeff2, tmp);
823
824 QMF_RE(temp_l) = QMF_RE(X_left[s][b]);
825 QMF_IM(temp_l) = QMF_IM(X_left[s][b]);
826 QMF_RE(temp_r) = QMF_RE(X_right[s][b]);
827 QMF_IM(temp_r) = QMF_IM(X_right[s][b]);
828
829 QMF_RE(X_left[s][b]) = MUL_R(QMF_RE(temp_l), coeff1);
830 QMF_IM(X_left[s][b]) = MUL_R(QMF_IM(temp_l), coeff1);
831 QMF_RE(X_right[s][b]) = MUL_R(QMF_RE(temp_r), coeff2);
832 QMF_IM(X_right[s][b]) = MUL_R(QMF_IM(temp_r), coeff2);
833
834 /* 2^(a+k*b) = 2^a * 2^b * ... * 2^b */
835 /* ^^^^^^^^^^^^^^^ k times */
836 pan_base[b] = MUL_C(pan_base[b], pan_delta[b]);
837 }
838 }
839 }
840}
841
842drm_ps_info *drm_ps_init(void)
843{
844 drm_ps_info *ps = (drm_ps_info*)faad_malloc(sizeof(drm_ps_info));
845
846 memset(ps, 0, sizeof(drm_ps_info));
847
848 return ps;
849}
850
851void drm_ps_free(drm_ps_info *ps)
852{
853 faad_free(ps);
854}
855
856/* main DRM PS decoding function */
857uint8_t drm_ps_decode(drm_ps_info *ps, uint8_t guess, qmf_t X_left[38][64], qmf_t X_right[38][64])
858{
859 if (ps == NULL) {
860 memcpy(X_right, X_left, sizeof(qmf_t) * 30 * 64);
861 return 0;
862 }
863
864 if (!ps->drm_ps_data_available && !guess) {
865 memcpy(X_right, X_left, sizeof(qmf_t) * 30 * 64);
866 memset(ps->g_prev_sa_index, 0, sizeof(ps->g_prev_sa_index));
867 memset(ps->g_prev_pan_index, 0, sizeof(ps->g_prev_pan_index));
868 return 0;
869 }
870
871 /* if SBR CRC doesn't match out, we can assume decode errors to start with,
872 and we'll guess what the parameters should be */
873 if (!guess) {
874 ps->sa_decode_error = 0;
875 ps->pan_decode_error = 0;
876 drm_ps_delta_decode(ps);
877 } else {
878 ps->sa_decode_error = 1;
879 ps->pan_decode_error = 1;
880 /* don't even bother decoding */
881 }
882
883 ps->drm_ps_data_available = 0;
884
885 drm_calc_sa_side_signal(ps, X_left);
886 drm_add_ambiance(ps, X_left, X_right);
887
888 if (ps->bs_enable_sa) {
889 ps->g_last_had_sa = 1;
890
891 memcpy(ps->g_prev_sa_index, ps->g_sa_index, sizeof(int8_t) * DRM_NUM_SA_BANDS);
892
893 } else {
894 ps->g_last_had_sa = 0;
895 }
896
897 if (ps->bs_enable_pan) {
898 drm_add_pan(ps, X_left, X_right);
899
900 ps->g_last_had_pan = 1;
901
902 memcpy(ps->g_prev_pan_index, ps->g_pan_index, sizeof(int8_t) * DRM_NUM_PAN_BANDS);
903
904 } else {
905 ps->g_last_had_pan = 0;
906 }
907
908
909 return 0;
910}
911
912#endif
913