]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/ic_pred.c
wwww
[16.git] / src / lib / doslib / ext / faad / ic_pred.c
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: ic_predict.c,v 1.28 2007/11/01 12:33:31 menno Exp $
29 **/
30
31 #include "common.h"
32 #include "structs.h"
33
34 #ifdef MAIN_DEC
35
36 #include "syntax.h"
37 #include "ic_pred.h"
38 #include "pns.h"
39
40
41 static void flt_round(float32_t *pf)
42 {
43     int32_t flg;
44     uint32_t tmp, tmp1, tmp2;
45
46     tmp = *(uint32_t*)pf;
47     flg = tmp & (uint32_t)0x00008000;
48     tmp &= (uint32_t)0xffff0000;
49     tmp1 = tmp;
50     /* round 1/2 lsb toward infinity */
51     if (flg)
52     {
53         tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
54         tmp |= (uint32_t)0x00010000;       /* insert 1 lsb */
55         tmp2 = tmp;                             /* add 1 lsb and elided one */
56         tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
57         
58         *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
59     } else {
60         *pf = *(float32_t*)&tmp;
61     }
62 }
63
64 static int16_t quant_pred(float32_t x)
65 {
66     int16_t q;
67     uint32_t *tmp = (uint32_t*)&x;
68
69     q = (int16_t)(*tmp>>16);
70
71     return q;
72 }
73
74 static float32_t inv_quant_pred(int16_t q)
75 {
76     float32_t x;
77     uint32_t *tmp = (uint32_t*)&x;
78     *tmp = ((uint32_t)q)<<16;
79
80     return x;
81 }
82
83 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
84 {
85     uint16_t tmp;
86     int16_t i, j;
87     real_t dr1;
88         float32_t predictedvalue;
89     real_t e0, e1;
90     real_t k1, k2;
91
92     real_t r[2];
93     real_t COR[2];
94     real_t VAR[2];
95
96     r[0] = inv_quant_pred(state->r[0]);
97     r[1] = inv_quant_pred(state->r[1]);
98     COR[0] = inv_quant_pred(state->COR[0]);
99     COR[1] = inv_quant_pred(state->COR[1]);
100     VAR[0] = inv_quant_pred(state->VAR[0]);
101     VAR[1] = inv_quant_pred(state->VAR[1]);
102
103
104 #if 1
105     tmp = state->VAR[0];
106     j = (tmp >> 7);
107     i = tmp & 0x7f;
108     if (j >= 128)
109     {
110         j -= 128;
111         k1 = COR[0] * exp_table[j] * mnt_table[i];
112     } else {
113         k1 = REAL_CONST(0);
114     }
115 #else
116
117     {
118 #define B 0.953125
119         real_t c = COR[0];
120         real_t v = VAR[0];
121         float32_t tmp;
122         if (c == 0 || v <= 1)
123         {
124             k1 = 0;
125         } else {
126             tmp = B / v;
127             flt_round(&tmp);
128             k1 = c * tmp;
129         }
130     }
131 #endif
132
133     if (pred)
134     {
135 #if 1
136         tmp = state->VAR[1];
137         j = (tmp >> 7);
138         i = tmp & 0x7f;
139         if (j >= 128)
140         {
141             j -= 128;
142             k2 = COR[1] * exp_table[j] * mnt_table[i];
143         } else {
144             k2 = REAL_CONST(0);
145         }
146 #else
147
148 #define B 0.953125
149         real_t c = COR[1];
150         real_t v = VAR[1];
151         float32_t tmp;
152         if (c == 0 || v <= 1)
153         {
154             k2 = 0;
155         } else {
156             tmp = B / v;
157             flt_round(&tmp);
158             k2 = c * tmp;
159         }
160 #endif
161
162         predictedvalue = k1*r[0] + k2*r[1];
163         flt_round(&predictedvalue);
164         *output = input + predictedvalue;
165     }
166
167     /* calculate new state data */
168     e0 = *output;
169     e1 = e0 - k1*r[0];
170     dr1 = k1*e0;
171
172     VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0);
173     COR[0] = ALPHA*COR[0] + r[0]*e0;
174     VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1);
175     COR[1] = ALPHA*COR[1] + r[1]*e1;
176
177     r[1] = A * (r[0]-dr1);
178     r[0] = A * e0;
179
180     state->r[0] = quant_pred(r[0]);
181     state->r[1] = quant_pred(r[1]);
182     state->COR[0] = quant_pred(COR[0]);
183     state->COR[1] = quant_pred(COR[1]);
184     state->VAR[0] = quant_pred(VAR[0]);
185     state->VAR[1] = quant_pred(VAR[1]);
186 }
187
188 static void reset_pred_state(pred_state *state)
189 {
190     state->r[0]   = 0;
191     state->r[1]   = 0;
192     state->COR[0] = 0;
193     state->COR[1] = 0;
194     state->VAR[0] = 0x3F80;
195     state->VAR[1] = 0x3F80;
196 }
197
198 void pns_reset_pred_state(ic_stream *ics, pred_state *state)
199 {
200     uint8_t sfb, g, b;
201     uint16_t i, offs, offs2;
202
203     /* prediction only for long blocks */
204     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
205         return;
206
207     for (g = 0; g < ics->num_window_groups; g++)
208     {
209         for (b = 0; b < ics->window_group_length[g]; b++)
210         {
211             for (sfb = 0; sfb < ics->max_sfb; sfb++)
212             {
213                 if (is_noise(ics, g, sfb))
214                 {
215                     offs = ics->swb_offset[sfb];
216                     offs2 = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
217
218                     for (i = offs; i < offs2; i++)
219                         reset_pred_state(&state[i]);
220                 }
221             }
222         }
223     }
224 }
225
226 void reset_all_predictors(pred_state *state, uint16_t frame_len)
227 {
228     uint16_t i;
229
230     for (i = 0; i < frame_len; i++)
231         reset_pred_state(&state[i]);
232 }
233
234 /* intra channel prediction */
235 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
236                    uint16_t frame_len, uint8_t sf_index)
237 {
238     uint8_t sfb;
239     uint16_t bin;
240
241     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
242     {
243         reset_all_predictors(state, frame_len);
244     } else {
245         for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++)
246         {
247             uint16_t low  = ics->swb_offset[sfb];
248             uint16_t high = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
249
250             for (bin = low; bin < high; bin++)
251             {
252                 ic_predict(&state[bin], spec[bin], &spec[bin],
253                     (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
254             }
255         }
256
257         if (ics->predictor_data_present)
258         {
259             if (ics->pred.predictor_reset)
260             {
261                 for (bin = ics->pred.predictor_reset_group_number - 1;
262                      bin < frame_len; bin += 30)
263                 {
264                     reset_pred_state(&state[bin]);
265                 }
266             }
267         }
268     }
269 }
270
271 #endif