blob: b9a91bab46ca441b4073404a658280a29ac240d1 [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 Valin98c86c72008-03-27 08:40:45 +110055 yshift = 13-celt_ilog2(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 Valin6ea8bae2008-04-15 08:01:33 +100062 i=0;
63 do {
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110064 Rpp = MAC16_16(Rpp,P[i],P[i]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110065 y[i] = SHL16(iy[i],yshift);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100066 } while (++i < N);
67
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 Valin6ea8bae2008-04-15 08:01:33 +100071 i=0;
72 do {
Jean-Marc Valindf7ab432008-03-26 18:03:22 +110073 Ryp = MAC16_16(Ryp, y[i], P[i]);
74 Ryy = MAC16_16(Ryy, y[i], y[i]);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100075 } while (++i < N);
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
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100084 i=0;
85 do
Jean-Marc Valinf5b05872008-03-21 10:46:17 +110086 X[i] = P[i] + ROUND16(MULT16_16(y[i], g),11);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100087 while (++i < N);
88
Jean-Marc Valin8600f692008-02-29 15:14:12 +110089 RESTORE_STACK;
Jean-Marc Valind4018c32008-02-27 10:09:48 +110090}
91
Jean-Marc Valin41af4212007-11-30 18:35:37 +110092
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110093void 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 +110094{
Jean-Marc Valin44c63352008-03-25 21:28:40 +110095 VARDECL(celt_norm_t, y);
96 VARDECL(int, iy);
Jean-Marc Valin49134382008-03-25 16:07:05 +110097 VARDECL(int, signx);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100098 int j, is;
Jean-Marc Valin44c63352008-03-25 21:28:40 +110099 celt_word16_t s;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100100 int pulsesLeft;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100101 celt_word32_t sum;
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100102 celt_word32_t xy, yy, yp;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100103 celt_word16_t Rpp;
Jean-Marc Valinf9584772008-03-27 12:22:44 +1100104 int N_1; /* Inverse of N, in Q14 format (even for float) */
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100105#ifdef FIXED_POINT
Jean-Marc Valind748cd52008-03-01 07:27:03 +1100106 int yshift;
107#endif
108 SAVE_STACK;
109
110#ifdef FIXED_POINT
Jean-Marc Valin98c86c72008-03-27 08:40:45 +1100111 yshift = 13-celt_ilog2(K);
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100112#endif
Jean-Marc Valin9d8d9b32008-02-27 16:17:39 +1100113
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100114 ALLOC(y, N, celt_norm_t);
115 ALLOC(iy, N, int);
Jean-Marc Valin49134382008-03-25 16:07:05 +1100116 ALLOC(signx, N, int);
Jean-Marc Valin124d1cd2008-03-28 00:33:04 +1100117 N_1 = 512/N;
Jean-Marc Valin3d152a52008-04-15 07:46:48 +1000118
119 sum = 0;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100120 for (j=0;j<N;j++)
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100121 {
Jean-Marc Valin49134382008-03-25 16:07:05 +1100122 if (X[j]>0)
123 signx[j]=1;
124 else
125 signx[j]=-1;
Jean-Marc Valin3d152a52008-04-15 07:46:48 +1000126 iy[j] = 0;
127 y[j] = 0;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100128 sum = MAC16_16(sum, P[j],P[j]);
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100129 }
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100130 Rpp = ROUND16(sum, NORM_SHIFT);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100131
Jean-Marc Valin4ff068e2008-03-15 23:34:39 +1100132 celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");
Jean-Marc Valinb60340f2008-02-26 15:41:51 +1100133
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100134 xy = yy = yp = 0;
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100135
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100136 pulsesLeft = K;
137 while (pulsesLeft > 0)
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100138 {
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100139 int pulsesAtOnce=1;
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100140 int best_id;
Jean-Marc Valined317c92008-04-15 17:31:23 +1000141 celt_word16_t magnitude;
142#ifdef FIXED_POINT
143 int rshift;
144#endif
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100145 /* Decide on how many pulses to find at once */
Jean-Marc Valin124d1cd2008-03-28 00:33:04 +1100146 pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
Jean-Marc Valincab576e2008-02-12 17:21:14 +1100147 if (pulsesAtOnce<1)
148 pulsesAtOnce = 1;
Jean-Marc Valined317c92008-04-15 17:31:23 +1000149#ifdef FIXED_POINT
150 rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
151#endif
152 magnitude = SHL16(pulsesAtOnce, yshift);
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100153
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100154 best_id = 0;
Jean-Marc Valined317c92008-04-15 17:31:23 +1000155 /* The squared magnitude term gets added anyway, so we might as well
156 add it outside the loop */
157 yy = ADD32(yy, MULT16_16(magnitude,magnitude));
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100158 /* Choose between fast and accurate strategy depending on where we are in the search */
159 if (pulsesLeft>1)
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100160 {
Jean-Marc Valined317c92008-04-15 17:31:23 +1000161 /* This should ensure that anything we can process will have a better score */
162 celt_word32_t best_num = -VERY_LARGE16;
163 celt_word16_t best_den = 0;
Jean-Marc Valinfed97d52008-03-26 21:31:56 +1100164 j=0;
165 do {
Jean-Marc Valined317c92008-04-15 17:31:23 +1000166 celt_word16_t Rxy, Ryy;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100167 /* Select sign based on X[j] alone */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000168 s = signx[j]*magnitude;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100169 /* Temporary sums of the new pulse(s) */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000170 Rxy = SHR32(xy + MULT16_16(s,X[j]),rshift);
171 /* We're multiplying y[j] by two so we don't have to do it here */
172 Ryy = SHR32(yy + MULT16_16(s,y[j]),rshift);
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100173
Jean-Marc Valined317c92008-04-15 17:31:23 +1000174 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
175 Rxy is positive because the sign is pre-computed) */
176 Rxy = MULT16_16_Q15(Rxy,Rxy);
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100177 /* The idea is to check for num/den >= best_num/best_den, but that way
178 we can do it without any division */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000179 /* OPT: Make sure to use conditional moves here */
180 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100181 {
Jean-Marc Valined317c92008-04-15 17:31:23 +1000182 best_den = Ryy;
183 best_num = Rxy;
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100184 best_id = j;
185 }
Jean-Marc Valinfed97d52008-03-26 21:31:56 +1100186 } while (++j<N); /* Promises we loop at least once */
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100187 } else {
Jean-Marc Valined317c92008-04-15 17:31:23 +1000188 celt_word32_t g;
189 celt_word32_t best_num = -VERY_LARGE32;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100190 for (j=0;j<N;j++)
191 {
Jean-Marc Valined317c92008-04-15 17:31:23 +1000192 celt_word32_t Rxy, Ryy, Ryp;
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100193 celt_word32_t num;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100194 /* Select sign based on X[j] alone */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000195 s = signx[j]*magnitude;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100196 /* Temporary sums of the new pulse(s) */
197 Rxy = xy + MULT16_16(s,X[j]);
Jean-Marc Valined317c92008-04-15 17:31:23 +1000198 /* We're multiplying y[j] by two so we don't have to do it here */
199 Ryy = yy + MULT16_16(s,y[j]);
200 Ryp = yp + MULT16_16(s,P[j]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100201
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100202 /* Compute the gain such that ||p + g*y|| = 1 */
203 g = MULT16_32_Q15(
204 celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy -
205 MULT16_16(ROUND16(Ryy,14),Rpp))
206 - ROUND16(Ryp,14),
207 celt_rcp(SHR32(Ryy,12)));
208 /* Knowing that gain, what's the error: (x-g*y)^2
209 (result is negated and we discard x^2 because it's constant) */
210 /* score = 2.f*g*Rxy - 1.f*g*g*Ryy*NORM_SCALING_1;*/
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100211 num = 2*MULT16_32_Q14(ROUND16(Rxy,14),g)
212 - MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND16(Ryy,14),g)),g);
213 if (num >= best_num)
214 {
215 best_num = num;
216 best_id = j;
217 }
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100218 }
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100219 }
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100220
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100221 j = best_id;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100222 is = signx[j]*pulsesAtOnce;
223 s = SHL16(is, yshift);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100224
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100225 /* Updating the sums of the new pulse(s) */
226 xy = xy + MULT16_16(s,X[j]);
Jean-Marc Valined317c92008-04-15 17:31:23 +1000227 /* We're multiplying y[j] by two so we don't have to do it here */
228 yy = yy + MULT16_16(s,y[j]);
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100229 yp = yp + MULT16_16(s, P[j]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100230
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100231 /* Only now that we've made the final choice, update y/iy */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000232 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
233 y[j] += 2*s;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100234 iy[j] += is;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100235 pulsesLeft -= pulsesAtOnce;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100236 }
237
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100238 encode_pulses(iy, N, K, enc);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100239
Jean-Marc Valina4833ff2008-01-10 15:34:00 +1100240 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100241 due to the recursive computation used in quantisation. */
242 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100243 RESTORE_STACK;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100244}
245
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100246
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100247/** Decode pulse vector and combine the result with the pitch vector to produce
248 the final normalised signal in the current band. */
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100249void 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 +1100250{
Jean-Marc Valin31b79d12008-03-12 17:17:23 +1100251 VARDECL(int, iy);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100252 SAVE_STACK;
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +1100253 ALLOC(iy, N, int);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100254 decode_pulses(iy, N, K, dec);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100255 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100256 RESTORE_STACK;
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100257}
258
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100259#ifdef FIXED_POINT
260static const celt_word16_t pg[11] = {32767, 24576, 21299, 19661, 19661, 19661, 18022, 18022, 16384, 16384, 16384};
261#else
Jean-Marc Valin3e650972008-03-07 17:38:58 +1100262static 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 +1100263#endif
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100264
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100265#define MAX_INTRA 32
266#define LOG_MAX_INTRA 5
267
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100268void 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 +1100269{
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000270 int i,j,c;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100271 int best=0;
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100272 celt_word32_t best_num=-SHR32(VERY_LARGE32,4);
273 celt_word16_t best_den=0;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100274 celt_word16_t s = 1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100275 int sign;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100276 celt_word32_t E;
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100277 celt_word16_t pred_gain;
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100278 int max_pos = N0-N;
Jean-Marc Valin3956de92008-04-12 06:55:39 +1000279 VARDECL(celt_norm_t, Xr);
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000280 SAVE_STACK;
281
282 ALLOC(Xr, B*N, celt_norm_t);
283
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100284 if (max_pos > MAX_INTRA)
285 max_pos = MAX_INTRA;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100286
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000287 /* Reverse the samples of x without reversing the channels */
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000288 for (c=0;c<B;c++)
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000289 for (j=0;j<N;j++)
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000290 Xr[B*N-B*j-B+c] = x[B*j+c];
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000291
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000292 for (i=0;i<max_pos;i++)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100293 {
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100294 celt_word32_t xy=0, yy=0;
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100295 celt_word32_t num;
296 celt_word16_t den;
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000297 const celt_word16_t * restrict xp = Xr;
298 const celt_word16_t * restrict yp = Y+B*i;
Jean-Marc Valinfed97d52008-03-26 21:31:56 +1100299 /* OPT: If this doesn't generate a double-MAC (on supported architectures),
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100300 complain to your compilor vendor */
Jean-Marc Valinfed97d52008-03-26 21:31:56 +1100301 j=0;
302 do {
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000303 xy = MAC16_16(xy, *xp, *yp);
304 yy = MAC16_16(yy, *yp, *yp);
305 xp++;
306 yp++;
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100307 } while (++j<B*N); /* Promises we loop at least once */
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100308 /* Using xy^2/yy as the score but without having to do the division */
309 num = MULT16_16(ROUND16(xy,14),ROUND16(xy,14));
310 den = ROUND16(yy,14);
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100311 /* If you're really desperate for speed, just use xy as the score */
Jean-Marc Valinfed97d52008-03-26 21:31:56 +1100312 /* OPT: Make sure to use a conditional move here */
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100313 if (MULT16_32_Q15(best_den, num) > MULT16_32_Q15(den, best_num))
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100314 {
Jean-Marc Valin89c5fd12008-03-26 12:16:00 +1100315 best_num = num;
316 best_den = den;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100317 best = i;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100318 /* Store xy as the sign. We'll normalise it to +/- 1 later. */
319 s = ROUND16(xy,14);
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100320 }
321 }
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100322 if (s<0)
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100323 {
324 s = -1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100325 sign = 1;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100326 } else {
327 s = 1;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100328 sign = 0;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100329 }
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100330 /*printf ("%d %d ", sign, best);*/
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100331 ec_enc_bits(enc,sign,1);
332 if (max_pos == MAX_INTRA)
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000333 ec_enc_bits(enc,best,LOG_MAX_INTRA);
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100334 else
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000335 ec_enc_uint(enc,best,max_pos);
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100336
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100337 /*printf ("%d %f\n", best, best_score);*/
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100338
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100339 if (K>10)
340 pred_gain = pg[10];
341 else
342 pred_gain = pg[K];
Jean-Marc Valin03892c12008-03-07 17:25:47 +1100343 E = EPSILON;
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000344 for (c=0;c<B;c++)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100345 {
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000346 for (j=0;j<N;j++)
347 {
Jean-Marc Valinc8e3b672008-04-10 12:21:26 +1000348 P[B*j+c] = s*Y[B*best+B*(N-j-1)+c];
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000349 E = MAC16_16(E, P[j],P[j]);
350 }
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100351 }
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100352 /*pred_gain = pred_gain/sqrt(E);*/
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100353 pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9)));
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100354 for (j=0;j<B*N;j++)
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100355 P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8);
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100356 if (K>0)
357 {
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100358 for (j=0;j<B*N;j++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100359 x[j] -= P[j];
360 } else {
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100361 for (j=0;j<B*N;j++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100362 x[j] = P[j];
363 }
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100364 /*printf ("quant ");*/
365 /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/
Jean-Marc Valin6d3289c2008-04-10 14:43:59 +1000366 RESTORE_STACK;
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100367}
Jean-Marc Valinfc08d0a2007-12-07 13:26:15 +1100368
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100369void 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 +1100370{
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000371 int j, c;
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100372 int sign;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100373 celt_word16_t s;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100374 int best;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100375 celt_word32_t E;
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100376 celt_word16_t pred_gain;
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100377 int max_pos = N0-N;
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100378 if (max_pos > MAX_INTRA)
379 max_pos = MAX_INTRA;
Jean-Marc Valin8f0f4b92008-02-11 13:52:44 +1100380
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100381 sign = ec_dec_bits(dec, 1);
Jean-Marc Valin0aa39032007-12-07 15:09:58 +1100382 if (sign == 0)
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100383 s = 1;
384 else
385 s = -1;
386
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100387 if (max_pos == MAX_INTRA)
Jean-Marc Valin15588ad2008-04-10 09:00:12 +1000388 best = B*ec_dec_bits(dec, LOG_MAX_INTRA);
Jean-Marc Valin208ae6e2008-03-25 15:25:08 +1100389 else
Jean-Marc Valin15588ad2008-04-10 09:00:12 +1000390 best = B*ec_dec_uint(dec, max_pos);
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100391 /*printf ("%d %d ", sign, best);*/
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100392
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100393 if (K>10)
394 pred_gain = pg[10];
395 else
396 pred_gain = pg[K];
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100397 E = EPSILON;
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000398 for (c=0;c<B;c++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100399 {
Jean-Marc Valin381b29c2008-04-10 11:00:51 +1000400 for (j=0;j<N;j++)
401 {
402 P[B*j+c] = s*Y[best+B*(N-j-1)+c];
403 E = MAC16_16(E, P[j],P[j]);
404 }
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100405 }
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100406 /*pred_gain = pred_gain/sqrt(E);*/
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100407 pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9)));
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100408 for (j=0;j<B*N;j++)
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100409 P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8);
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100410 if (K==0)
411 {
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100412 for (j=0;j<B*N;j++)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100413 x[j] = P[j];
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100414 }
415}
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100416
Jean-Marc Valin5de868c2008-03-25 22:38:58 +1100417void 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 +1100418{
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100419 int i, j;
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100420 celt_word32_t E;
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100421 celt_word16_t g;
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100422
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100423 E = EPSILON;
Jean-Marc Valina536f772008-03-22 09:01:50 +1100424 if (N0 >= (Nmax>>1))
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100425 {
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100426 for (i=0;i<B;i++)
427 {
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100428 for (j=0;j<N;j++)
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100429 {
430 P[j*B+i] = Y[(Nmax-N0-j-1)*B+i];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100431 E += P[j*B+i]*P[j*B+i];
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100432 }
433 }
434 } else {
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100435 for (j=0;j<B*N;j++)
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100436 {
437 P[j] = Y[j];
Jean-Marc Valin877b1972008-02-29 16:40:39 +1100438 E = MAC16_16(E, P[j],P[j]);
Jean-Marc Valin0df0eb42008-02-13 16:00:10 +1100439 }
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100440 }
Jean-Marc Valin23e82b22008-03-24 08:15:40 +1100441 g = celt_rcp(SHL32(celt_sqrt(E),9));
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100442 for (j=0;j<B*N;j++)
Jean-Marc Valinec9b6df2008-03-07 17:05:47 +1100443 P[j] = PSHR32(MULT16_16(g, P[j]),8);
Jean-Marc Valine28f25f2008-03-27 14:18:28 +1100444 for (j=0;j<B*N;j++)
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100445 x[j] = P[j];
446}
447