blob: cdba91e566b1391f7a30bcb7bc2ed40a4320e8c0 [file] [log] [blame]
Jean-Marc Valin35a1f882008-03-26 10:34:23 +11001/* (C) 2007-2008 Jean-Marc Valin, CSIRO
Jean-Marc Valin41af4212007-11-30 18:35:37 +11002*/
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
Jean-Marc Valin02fa9132008-02-20 12:09:29 +110032#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
Jean-Marc Valin3ca9b1d2008-02-27 23:50:31 +110036#include "mathops.h"
Jean-Marc Valin29ccab82007-12-06 15:39:38 +110037#include "cwrs.h"
Jean-Marc Valin9cace642007-12-06 17:44:09 +110038#include "vq.h"
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +110039#include "arch.h"
Jean-Marc Valinb60340f2008-02-26 15:41:51 +110040#include "os_support.h"
Jean-Marc Valin41af4212007-11-30 18:35:37 +110041
Jean-Marc Valin35a1f882008-03-26 10:34:23 +110042/** Takes the pitch vector and the decoded residual vector, computes the gain
43 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
Jean-Marc Valin5de868c2008-03-25 22:38:58 +110044static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P)
Jean-Marc Valind4018c32008-02-27 10:09:48 +110045{
46 int i;
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110047 celt_word32_t Ryp, Ryy, Rpp;
Jean-Marc Valina847b772008-02-27 17:46:49 +110048 celt_word32_t g;
Jean-Marc Valin31b79d12008-03-12 17:17:23 +110049 VARDECL(celt_norm_t, y);
Jean-Marc Valind9de5932008-03-05 08:11:57 +110050#ifdef FIXED_POINT
51 int yshift;
52#endif
Jean-Marc Valin8600f692008-02-29 15:14:12 +110053 SAVE_STACK;
Jean-Marc Valind17edd32008-02-27 16:52:30 +110054#ifdef FIXED_POINT
Jean-Marc Valind9de5932008-03-05 08:11:57 +110055 yshift = 14-EC_ILOG(K);
Jean-Marc Valind17edd32008-02-27 16:52:30 +110056#endif
57 ALLOC(y, N, celt_norm_t);
Jean-Marc Valind4018c32008-02-27 10:09:48 +110058
59 /*for (i=0;i<N;i++)
60 printf ("%d ", iy[i]);*/
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110061 Rpp = 0;
Jean-Marc Valind4018c32008-02-27 10:09:48 +110062 for (i=0;i<N;i++)
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110063 Rpp = MAC16_16(Rpp,P[i],P[i]);
Jean-Marc Valind4018c32008-02-27 10:09:48 +110064
Jean-Marc Valind4018c32008-02-27 10:09:48 +110065 for (i=0;i<N;i++)
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110066 y[i] = SHL16(iy[i],yshift);
Jean-Marc Valin95088d42008-03-26 17:57:49 +110067
Jean-Marc Valind4018c32008-02-27 10:09:48 +110068 Ryp = 0;
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110069 Ryy = 0;
Jean-Marc Valindf7ab432008-03-26 18:03:22 +110070 /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
Jean-Marc Valind4018c32008-02-27 10:09:48 +110071 for (i=0;i<N;i++)
Jean-Marc Valindf7ab432008-03-26 18:03:22 +110072 {
73 Ryp = MAC16_16(Ryp, y[i], P[i]);
74 Ryy = MAC16_16(Ryy, y[i], y[i]);
75 }
76
Jean-Marc Valin1ca07222008-02-27 17:23:04 +110077 /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
Jean-Marc Valin9d5b4a62008-03-13 11:36:45 +110078 g = MULT16_32_Q15(
Jean-Marc Valinf5b05872008-03-21 10:46:17 +110079 celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy -
80 MULT16_16(ROUND16(Ryy,14),ROUND16(Rpp,14)))
81 - ROUND16(Ryp,14),
Jean-Marc Valin9d5b4a62008-03-13 11:36:45 +110082 celt_rcp(SHR32(Ryy,9)));
Jean-Marc Valind4018c32008-02-27 10:09:48 +110083
84 for (i=0;i<N;i++)
Jean-Marc Valinf5b05872008-03-21 10:46:17 +110085 X[i] = P[i] + ROUND16(MULT16_16(y[i], g),11);
Jean-Marc Valin8600f692008-02-29 15:14:12 +110086 RESTORE_STACK;
Jean-Marc Valind4018c32008-02-27 10:09:48 +110087}
88
Jean-Marc Valin41af4212007-11-30 18:35:37 +110089
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110090void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *P, ec_enc *enc)
Jean-Marc Valin41af4212007-11-30 18:35:37 +110091{
Jean-Marc Valin44c63352008-03-25 21:28:40 +110092 VARDECL(celt_norm_t, y);
93 VARDECL(int, iy);
Jean-Marc Valin49134382008-03-25 16:07:05 +110094 VARDECL(int, signx);
Jean-Marc Valin44c63352008-03-25 21:28:40 +110095 int i, j, is;
96 celt_word16_t s;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +110097 int pulsesLeft;
Jean-Marc Valin44c63352008-03-25 21:28:40 +110098 celt_word32_t sum;
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110099 celt_word32_t xy, yy, yp;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100100 celt_word16_t Rpp;
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100101#ifdef FIXED_POINT
Jean-Marc Valind748cd52008-03-01 07:27:03 +1100102 int yshift;
103#endif
104 SAVE_STACK;
105
106#ifdef FIXED_POINT
107 yshift = 14-EC_ILOG(K);
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100108#endif
Jean-Marc Valin9d8d9b32008-02-27 16:17:39 +1100109
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100110 ALLOC(y, N, celt_norm_t);
111 ALLOC(iy, N, int);
Jean-Marc Valin49134382008-03-25 16:07:05 +1100112 ALLOC(signx, N, int);
113
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100114 for (j=0;j<N;j++)
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100115 {
Jean-Marc Valin49134382008-03-25 16:07:05 +1100116 if (X[j]>0)
117 signx[j]=1;
118 else
119 signx[j]=-1;
120 }
121
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100122 sum = 0;
Jean-Marc Valin49134382008-03-25 16:07:05 +1100123 for (j=0;j<N;j++)
124 {
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100125 sum = MAC16_16(sum, P[j],P[j]);
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100126 }
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100127 Rpp = ROUND16(sum, NORM_SHIFT);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100128
Jean-Marc Valin4ff068e2008-03-15 23:34:39 +1100129 celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");
Jean-Marc Valinb60340f2008-02-26 15:41:51 +1100130
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100131 for (i=0;i<N;i++)
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100132 y[i] = 0;
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100133 for (i=0;i<N;i++)
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100134 iy[i] = 0;
135 xy = yy = yp = 0;
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100136
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100137 pulsesLeft = K;
138 while (pulsesLeft > 0)
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100139 {
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100140 int pulsesAtOnce=1;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100141 int sign;
142 celt_word32_t Rxy, Ryy, Ryp;
143 celt_word32_t g;
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100144 celt_word32_t best_num;
145 celt_word16_t best_den;
146 int best_id;
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100147
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100148 /* Decide on how many pulses to find at once */
Jean-Marc Valincab576e2008-02-12 17:21:14 +1100149 pulsesAtOnce = pulsesLeft/N;
150 if (pulsesAtOnce<1)
151 pulsesAtOnce = 1;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100152
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100153 /* This should ensure that anything we can process will have a better score */
154 best_num = -SHR32(VERY_LARGE32,4);
155 best_den = 0;
156 best_id = 0;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100157 /* Choose between fast and accurate strategy depending on where we are in the search */
158 if (pulsesLeft>1)
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100159 {
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100160 for (j=0;j<N;j++)
161 {
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100162 celt_word32_t num;
163 celt_word16_t den;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100164 /* Select sign based on X[j] alone */
165 sign = signx[j];
166 s = SHL16(sign*pulsesAtOnce, yshift);
167 /* Temporary sums of the new pulse(s) */
168 Rxy = xy + MULT16_16(s,X[j]);
169 Ryy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s);
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100170
171 /* Approximate score: we maximise Rxy/sqrt(Ryy) */
172 num = MULT16_16(ROUND16(Rxy,14),ABS16(ROUND16(Rxy,14)));
173 den = ROUND16(Ryy,14);
174 /* The idea is to check for num/den >= best_num/best_den, but that way
175 we can do it without any division */
Jean-Marc Valin233e3172008-03-26 15:46:51 +1100176 if (MULT16_32_Q15(best_den, num) > MULT16_32_Q15(den, best_num))
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100177 {
178 best_den = den;
179 best_num = num;
180 best_id = j;
181 }
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100182 }
183 } else {
184 for (j=0;j<N;j++)
185 {
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100186 celt_word32_t num;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100187 /* Select sign based on X[j] alone */
188 sign = signx[j];
189 s = SHL16(sign*pulsesAtOnce, yshift);
190 /* Temporary sums of the new pulse(s) */
191 Rxy = xy + MULT16_16(s,X[j]);
192 Ryy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s);
193 Ryp = yp + MULT16_16(s, P[j]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100194
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100195 /* Compute the gain such that ||p + g*y|| = 1 */
196 g = MULT16_32_Q15(
197 celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy -
198 MULT16_16(ROUND16(Ryy,14),Rpp))
199 - ROUND16(Ryp,14),
200 celt_rcp(SHR32(Ryy,12)));
201 /* Knowing that gain, what's the error: (x-g*y)^2
202 (result is negated and we discard x^2 because it's constant) */
203 /* score = 2.f*g*Rxy - 1.f*g*g*Ryy*NORM_SCALING_1;*/
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100204 num = 2*MULT16_32_Q14(ROUND16(Rxy,14),g)
205 - MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND16(Ryy,14),g)),g);
206 if (num >= best_num)
207 {
208 best_num = num;
209 best_id = j;
210 }
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100211 }
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100212 }
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100213
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100214 j = best_id;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100215 is = signx[j]*pulsesAtOnce;
216 s = SHL16(is, yshift);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100217
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100218 /* Updating the sums of the new pulse(s) */
219 xy = xy + MULT16_16(s,X[j]);
220 yy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s);
221 yp = yp + MULT16_16(s, P[j]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100222
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100223 /* Only now that we've made the final choice, update y/iy */
224 y[j] += s;
225 iy[j] += is;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100226 pulsesLeft -= pulsesAtOnce;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100227 }
228
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100229 encode_pulses(iy, N, K, enc);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100230
Jean-Marc Valina4833ff2008-01-10 15:34:00 +1100231 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100232 due to the recursive computation used in quantisation. */
233 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100234 RESTORE_STACK;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100235}
236
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100237
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100238/** Decode pulse vector and combine the result with the pitch vector to produce
239 the final normalised signal in the current band. */
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100240void alg_unquant(celt_norm_t *X, int N, int K, celt_norm_t *P, ec_dec *dec)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100241{
Jean-Marc Valin31b79d12008-03-12 17:17:23 +1100242 VARDECL(int, iy);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100243 SAVE_STACK;
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +1100244 ALLOC(iy, N, int);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100245 decode_pulses(iy, N, K, dec);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100246 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100247 RESTORE_STACK;
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100248}
249
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100250#ifdef FIXED_POINT
251static const celt_word16_t pg[11] = {32767, 24576, 21299, 19661, 19661, 19661, 18022, 18022, 16384, 16384, 16384};
252#else
Jean-Marc Valin3e650972008-03-07 17:38:58 +1100253static const celt_word16_t pg[11] = {1.f, .75f, .65f, 0.6f, 0.6f, .6f, .55f, .55f, .5f, .5f, .5f};
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100254#endif
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100255
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100256#define MAX_INTRA 32
257#define LOG_MAX_INTRA 5
258
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100259void intra_prediction(celt_norm_t *x, celt_mask_t *W, int N, int K, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, ec_enc *enc)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100260{
261 int i,j;
262 int best=0;
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100263 celt_word32_t best_num=-SHR32(VERY_LARGE32,4);
264 celt_word16_t best_den=0;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100265 celt_word16_t s = 1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100266 int sign;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100267 celt_word32_t E;
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100268 celt_word16_t pred_gain;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100269 int max_pos = N0-N/B;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100270 if (max_pos > MAX_INTRA)
271 max_pos = MAX_INTRA;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100272
273 for (i=0;i<max_pos*B;i+=B)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100274 {
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100275 celt_word32_t xy=0, yy=0;
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100276 celt_word32_t num;
277 celt_word16_t den;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100278 /* If this doesn't generate a double-MAC on supported architectures,
279 complain to your compilor vendor */
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100280 for (j=0;j<N;j++)
281 {
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100282 xy = MAC16_16(xy, x[j], Y[i+N-j-1]);
283 yy = MAC16_16(yy, Y[i+N-j-1], Y[i+N-j-1]);
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100284 }
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100285 /* Using xy^2/yy as the score but without having to do the division */
286 num = MULT16_16(ROUND16(xy,14),ROUND16(xy,14));
287 den = ROUND16(yy,14);
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100288 /* If you're really desperate for speed, just use xy as the score */
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100289 if (MULT16_32_Q15(best_den, num) > MULT16_32_Q15(den, best_num))
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100290 {
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100291 best_num = num;
292 best_den = den;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100293 best = i;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100294 /* Store xy as the sign. We'll normalise it to +/- 1 later. */
295 s = ROUND16(xy,14);
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100296 }
297 }
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100298 if (s<0)
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100299 {
300 s = -1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100301 sign = 1;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100302 } else {
303 s = 1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100304 sign = 0;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100305 }
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100306 /*printf ("%d %d ", sign, best);*/
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100307 ec_enc_bits(enc,sign,1);
308 if (max_pos == MAX_INTRA)
309 ec_enc_bits(enc,best/B,LOG_MAX_INTRA);
310 else
311 ec_enc_uint(enc,best/B,max_pos);
312
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100313 /*printf ("%d %f\n", best, best_score);*/
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100314
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100315 if (K>10)
316 pred_gain = pg[10];
317 else
318 pred_gain = pg[K];
Jean-Marc Valin03892c12008-03-07 17:25:47 +1100319 E = EPSILON;
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100320 for (j=0;j<N;j++)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100321 {
Jean-Marc Valind501f612008-02-21 12:16:57 +1100322 P[j] = s*Y[best+N-j-1];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100323 E = MAC16_16(E, P[j],P[j]);
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100324 }
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100325 /*pred_gain = pred_gain/sqrt(E);*/
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100326 pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9)));
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100327 for (j=0;j<N;j++)
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100328 P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8);
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100329 if (K>0)
330 {
331 for (j=0;j<N;j++)
332 x[j] -= P[j];
333 } else {
334 for (j=0;j<N;j++)
335 x[j] = P[j];
336 }
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100337 /*printf ("quant ");*/
338 /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100339
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100340}
Jean-Marc Valinfc08d0a2007-12-07 13:26:15 +1100341
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100342void intra_unquant(celt_norm_t *x, int N, int K, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, ec_dec *dec)
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100343{
Jean-Marc Valin11f01722007-12-09 01:19:36 +1100344 int j;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100345 int sign;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100346 celt_word16_t s;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100347 int best;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100348 celt_word32_t E;
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100349 celt_word16_t pred_gain;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100350 int max_pos = N0-N/B;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100351 if (max_pos > MAX_INTRA)
352 max_pos = MAX_INTRA;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100353
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100354 sign = ec_dec_bits(dec, 1);
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100355 if (sign == 0)
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100356 s = 1;
357 else
358 s = -1;
359
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100360 if (max_pos == MAX_INTRA)
361 best = B*ec_dec_bits(dec, LOG_MAX_INTRA);
362 else
363 best = B*ec_dec_uint(dec, max_pos);
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100364 /*printf ("%d %d ", sign, best);*/
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100365
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100366 if (K>10)
367 pred_gain = pg[10];
368 else
369 pred_gain = pg[K];
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100370 E = EPSILON;
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100371 for (j=0;j<N;j++)
372 {
Jean-Marc Valind501f612008-02-21 12:16:57 +1100373 P[j] = s*Y[best+N-j-1];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100374 E = MAC16_16(E, P[j],P[j]);
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100375 }
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100376 /*pred_gain = pred_gain/sqrt(E);*/
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100377 pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9)));
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100378 for (j=0;j<N;j++)
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100379 P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8);
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100380 if (K==0)
381 {
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100382 for (j=0;j<N;j++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100383 x[j] = P[j];
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100384 }
385}
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100386
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100387void intra_fold(celt_norm_t *x, int N, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, int Nmax)
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100388{
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100389 int i, j;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100390 celt_word32_t E;
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100391 celt_word16_t g;
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100392
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100393 E = EPSILON;
Jean-Marc Valina536f772008-03-22 09:01:50 +1100394 if (N0 >= (Nmax>>1))
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100395 {
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100396 for (i=0;i<B;i++)
397 {
398 for (j=0;j<N/B;j++)
399 {
400 P[j*B+i] = Y[(Nmax-N0-j-1)*B+i];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100401 E += P[j*B+i]*P[j*B+i];
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100402 }
403 }
404 } else {
405 for (j=0;j<N;j++)
406 {
407 P[j] = Y[j];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100408 E = MAC16_16(E, P[j],P[j]);
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100409 }
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100410 }
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100411 g = celt_rcp(SHL32(celt_sqrt(E),9));
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100412 for (j=0;j<N;j++)
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100413 P[j] = PSHR32(MULT16_16(g, P[j]),8);
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100414 for (j=0;j<N;j++)
415 x[j] = P[j];
416}
417