2 ** Copyright 2003-2010, VisualOn, Inc.
\r
4 ** Licensed under the Apache License, Version 2.0 (the "License");
\r
5 ** you may not use this file except in compliance with the License.
\r
6 ** You may obtain a copy of the License at
\r
8 ** http://www.apache.org/licenses/LICENSE-2.0
\r
10 ** Unless required by applicable law or agreed to in writing, software
\r
11 ** distributed under the License is distributed on an "AS IS" BASIS,
\r
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 ** See the License for the specific language governing permissions and
\r
14 ** limitations under the License.
\r
17 /***********************************************************************
\r
20 * Description: Coding and decoding of algebraic codebook *
\r
22 ************************************************************************/
\r
25 #include "typedef.h"
\r
26 #include "basic_op.h"
\r
27 #include "q_pulse.h"
\r
29 #define NB_POS 16 /* pos in track, mask for sign bit */
\r
31 Word32 quant_1p_N1( /* (o) return N+1 bits */
\r
32 Word16 pos, /* (i) position of the pulse */
\r
33 Word16 N) /* (i) number of bits for position */
\r
38 mask = (1 << N) - 1; /* mask = ((1<<N)-1); */
\r
39 /*-------------------------------------------------------*
\r
40 * Quantization of 1 pulse with N+1 bits: *
\r
41 *-------------------------------------------------------*/
\r
42 index = L_deposit_l((Word16) (pos & mask));
\r
43 if ((pos & NB_POS) != 0)
\r
45 index = vo_L_add(index, L_deposit_l(1 << N)); /* index += 1 << N; */
\r
51 Word32 quant_2p_2N1( /* (o) return (2*N)+1 bits */
\r
52 Word16 pos1, /* (i) position of the pulse 1 */
\r
53 Word16 pos2, /* (i) position of the pulse 2 */
\r
54 Word16 N) /* (i) number of bits for position */
\r
58 mask = (1 << N) - 1; /* mask = ((1<<N)-1); */
\r
59 /*-------------------------------------------------------*
\r
60 * Quantization of 2 pulses with 2*N+1 bits: *
\r
61 *-------------------------------------------------------*/
\r
62 if (((pos2 ^ pos1) & NB_POS) == 0)
\r
64 /* sign of 1st pulse == sign of 2th pulse */
\r
65 if(pos1 <= pos2) /* ((pos1 - pos2) <= 0) */
\r
67 /* index = ((pos1 & mask) << N) + (pos2 & mask); */
\r
68 index = L_deposit_l(add1((((Word16) (pos1 & mask)) << N), ((Word16) (pos2 & mask))));
\r
71 /* ((pos2 & mask) << N) + (pos1 & mask); */
\r
72 index = L_deposit_l(add1((((Word16) (pos2 & mask)) << N), ((Word16) (pos1 & mask))));
\r
74 if ((pos1 & NB_POS) != 0)
\r
77 index = vo_L_add(index, (1L << tmp)); /* index += 1 << (2*N); */
\r
81 /* sign of 1st pulse != sign of 2th pulse */
\r
82 if (vo_sub((Word16) (pos1 & mask), (Word16) (pos2 & mask)) <= 0)
\r
84 /* index = ((pos2 & mask) << N) + (pos1 & mask); */
\r
85 index = L_deposit_l(add1((((Word16) (pos2 & mask)) << N), ((Word16) (pos1 & mask))));
\r
86 if ((pos2 & NB_POS) != 0)
\r
88 tmp = (N << 1); /* index += 1 << (2*N); */
\r
89 index = vo_L_add(index, (1L << tmp));
\r
93 /* index = ((pos1 & mask) << N) + (pos2 & mask); */
\r
94 index = L_deposit_l(add1((((Word16) (pos1 & mask)) << N), ((Word16) (pos2 & mask))));
\r
95 if ((pos1 & NB_POS) != 0)
\r
98 index = vo_L_add(index, (1 << tmp)); /* index += 1 << (2*N); */
\r
106 Word32 quant_3p_3N1( /* (o) return (3*N)+1 bits */
\r
107 Word16 pos1, /* (i) position of the pulse 1 */
\r
108 Word16 pos2, /* (i) position of the pulse 2 */
\r
109 Word16 pos3, /* (i) position of the pulse 3 */
\r
110 Word16 N) /* (i) number of bits for position */
\r
115 nb_pos =(1 <<(N - 1)); /* nb_pos = (1<<(N-1)); */
\r
116 /*-------------------------------------------------------*
\r
117 * Quantization of 3 pulses with 3*N+1 bits: *
\r
118 *-------------------------------------------------------*/
\r
119 if (((pos1 ^ pos2) & nb_pos) == 0)
\r
121 index = quant_2p_2N1(pos1, pos2, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos2, (N-1)); */
\r
122 /* index += (pos1 & nb_pos) << N; */
\r
123 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
\r
124 /* index += quant_1p_N1(pos3, N) << (2*N); */
\r
125 index = vo_L_add(index, (quant_1p_N1(pos3, N)<<(N << 1)));
\r
127 } else if (((pos1 ^ pos3) & nb_pos) == 0)
\r
129 index = quant_2p_2N1(pos1, pos3, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos3, (N-1)); */
\r
130 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
\r
131 /* index += (pos1 & nb_pos) << N; */
\r
132 index = vo_L_add(index, (quant_1p_N1(pos2, N) << (N << 1)));
\r
133 /* index += quant_1p_N1(pos2, N) <<
\r
137 index = quant_2p_2N1(pos2, pos3, (N - 1)); /* index = quant_2p_2N1(pos2, pos3, (N-1)); */
\r
138 /* index += (pos2 & nb_pos) << N; */
\r
139 index = vo_L_add(index, (L_deposit_l((Word16) (pos2 & nb_pos)) << N));
\r
140 /* index += quant_1p_N1(pos1, N) << (2*N); */
\r
141 index = vo_L_add(index, (quant_1p_N1(pos1, N) << (N << 1)));
\r
147 Word32 quant_4p_4N1( /* (o) return (4*N)+1 bits */
\r
148 Word16 pos1, /* (i) position of the pulse 1 */
\r
149 Word16 pos2, /* (i) position of the pulse 2 */
\r
150 Word16 pos3, /* (i) position of the pulse 3 */
\r
151 Word16 pos4, /* (i) position of the pulse 4 */
\r
152 Word16 N) /* (i) number of bits for position */
\r
157 nb_pos = 1 << (N - 1); /* nb_pos = (1<<(N-1)); */
\r
158 /*-------------------------------------------------------*
\r
159 * Quantization of 4 pulses with 4*N+1 bits: *
\r
160 *-------------------------------------------------------*/
\r
161 if (((pos1 ^ pos2) & nb_pos) == 0)
\r
163 index = quant_2p_2N1(pos1, pos2, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos2, (N-1)); */
\r
164 /* index += (pos1 & nb_pos) << N; */
\r
165 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
\r
166 /* index += quant_2p_2N1(pos3, pos4, N) << (2*N); */
\r
167 index = vo_L_add(index, (quant_2p_2N1(pos3, pos4, N) << (N << 1)));
\r
168 } else if (((pos1 ^ pos3) & nb_pos) == 0)
\r
170 index = quant_2p_2N1(pos1, pos3, (N - 1));
\r
171 /* index += (pos1 & nb_pos) << N; */
\r
172 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
\r
173 /* index += quant_2p_2N1(pos2, pos4, N) << (2*N); */
\r
174 index = vo_L_add(index, (quant_2p_2N1(pos2, pos4, N) << (N << 1)));
\r
177 index = quant_2p_2N1(pos2, pos3, (N - 1));
\r
178 /* index += (pos2 & nb_pos) << N; */
\r
179 index = vo_L_add(index, (L_deposit_l((Word16) (pos2 & nb_pos)) << N));
\r
180 /* index += quant_2p_2N1(pos1, pos4, N) << (2*N); */
\r
181 index = vo_L_add(index, (quant_2p_2N1(pos1, pos4, N) << (N << 1)));
\r
187 Word32 quant_4p_4N( /* (o) return 4*N bits */
\r
188 Word16 pos[], /* (i) position of the pulse 1..4 */
\r
189 Word16 N) /* (i) number of bits for position */
\r
191 Word16 nb_pos, mask, n_1, tmp;
\r
192 Word16 posA[4], posB[4];
\r
193 Word32 i, j, k, index;
\r
195 n_1 = (Word16) (N - 1);
\r
196 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
\r
197 mask = vo_sub((1 << N), 1); /* mask = ((1<<N)-1); */
\r
201 for (k = 0; k < 4; k++)
\r
203 if ((pos[k] & nb_pos) == 0)
\r
205 posA[i++] = pos[k];
\r
208 posB[j++] = pos[k];
\r
215 tmp = vo_sub((N << 2), 3); /* index = 1 << ((4*N)-3); */
\r
216 index = (1L << tmp);
\r
217 /* index += quant_4p_4N1(posB[0], posB[1], posB[2], posB[3], n_1); */
\r
218 index = vo_L_add(index, quant_4p_4N1(posB[0], posB[1], posB[2], posB[3], n_1));
\r
221 /* index = quant_1p_N1(posA[0], n_1) << ((3*n_1)+1); */
\r
222 tmp = add1((Word16)((vo_L_mult(3, n_1) >> 1)), 1);
\r
223 index = L_shl(quant_1p_N1(posA[0], n_1), tmp);
\r
224 /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1); */
\r
225 index = vo_L_add(index, quant_3p_3N1(posB[0], posB[1], posB[2], n_1));
\r
228 tmp = ((n_1 << 1) + 1); /* index = quant_2p_2N1(posA[0], posA[1], n_1) << ((2*n_1)+1); */
\r
229 index = L_shl(quant_2p_2N1(posA[0], posA[1], n_1), tmp);
\r
230 /* index += quant_2p_2N1(posB[0], posB[1], n_1); */
\r
231 index = vo_L_add(index, quant_2p_2N1(posB[0], posB[1], n_1));
\r
234 /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << N; */
\r
235 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), N);
\r
236 index = vo_L_add(index, quant_1p_N1(posB[0], n_1)); /* index += quant_1p_N1(posB[0], n_1); */
\r
239 index = quant_4p_4N1(posA[0], posA[1], posA[2], posA[3], n_1);
\r
243 fprintf(stderr, "Error in function quant_4p_4N\n");
\r
245 tmp = ((N << 2) - 2); /* index += (i & 3) << ((4*N)-2); */
\r
246 index = vo_L_add(index, L_shl((L_deposit_l(i) & (3L)), tmp));
\r
253 Word32 quant_5p_5N( /* (o) return 5*N bits */
\r
254 Word16 pos[], /* (i) position of the pulse 1..5 */
\r
255 Word16 N) /* (i) number of bits for position */
\r
257 Word16 nb_pos, n_1, tmp;
\r
258 Word16 posA[5], posB[5];
\r
259 Word32 i, j, k, index, tmp2;
\r
261 n_1 = (Word16) (N - 1);
\r
262 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
\r
266 for (k = 0; k < 5; k++)
\r
268 if ((pos[k] & nb_pos) == 0)
\r
270 posA[i++] = pos[k];
\r
273 posB[j++] = pos[k];
\r
280 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* ((5*N)-1)) */
\r
281 index = L_shl(1L, tmp); /* index = 1 << ((5*N)-1); */
\r
282 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) << ((2*N)+1);*/
\r
283 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
\r
284 index = vo_L_add(index, tmp2);
\r
285 index = vo_L_add(index, quant_2p_2N1(posB[3], posB[4], N)); /* index += quant_2p_2N1(posB[3], posB[4], N); */
\r
288 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* index = 1 << ((5*N)-1); */
\r
289 index = L_shl(1L, tmp);
\r
290 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) <<((2*N)+1); */
\r
291 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
\r
292 index = vo_L_add(index, tmp2);
\r
293 index = vo_L_add(index, quant_2p_2N1(posB[3], posA[0], N)); /* index += quant_2p_2N1(posB[3], posA[0], N); */
\r
296 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* ((5*N)-1)) */
\r
297 index = L_shl(1L, tmp); /* index = 1 << ((5*N)-1); */
\r
298 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) << ((2*N)+1); */
\r
299 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
\r
300 index = vo_L_add(index, tmp2);
\r
301 index = vo_L_add(index, quant_2p_2N1(posA[0], posA[1], N)); /* index += quant_2p_2N1(posA[0], posA[1], N); */
\r
304 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
\r
305 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
\r
306 index = vo_L_add(index, quant_2p_2N1(posB[0], posB[1], N)); /* index += quant_2p_2N1(posB[0], posB[1], N); */
\r
309 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
\r
310 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
\r
311 index = vo_L_add(index, quant_2p_2N1(posA[3], posB[0], N)); /* index += quant_2p_2N1(posA[3], posB[0], N); */
\r
314 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
\r
315 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
\r
316 index = vo_L_add(index, quant_2p_2N1(posA[3], posA[4], N)); /* index += quant_2p_2N1(posA[3], posA[4], N); */
\r
320 fprintf(stderr, "Error in function quant_5p_5N\n");
\r
327 Word32 quant_6p_6N_2( /* (o) return (6*N)-2 bits */
\r
328 Word16 pos[], /* (i) position of the pulse 1..6 */
\r
329 Word16 N) /* (i) number of bits for position */
\r
331 Word16 nb_pos, n_1;
\r
332 Word16 posA[6], posB[6];
\r
333 Word32 i, j, k, index;
\r
335 /* !! N and n_1 are constants -> it doesn't need to be operated by Basic Operators */
\r
336 n_1 = (Word16) (N - 1);
\r
337 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
\r
341 for (k = 0; k < 6; k++)
\r
343 if ((pos[k] & nb_pos) == 0)
\r
345 posA[i++] = pos[k];
\r
348 posB[j++] = pos[k];
\r
355 index = (1 << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
\r
356 index = vo_L_add(index, (quant_5p_5N(posB, n_1) << N)); /* index += quant_5p_5N(posB, n_1) << N; */
\r
357 index = vo_L_add(index, quant_1p_N1(posB[5], n_1)); /* index += quant_1p_N1(posB[5], n_1); */
\r
360 index = (1L << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
\r
361 index = vo_L_add(index, (quant_5p_5N(posB, n_1) << N)); /* index += quant_5p_5N(posB, n_1) << N; */
\r
362 index = vo_L_add(index, quant_1p_N1(posA[0], n_1)); /* index += quant_1p_N1(posA[0], n_1); */
\r
365 index = (1L << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
\r
366 /* index += quant_4p_4N(posB, n_1) << ((2*n_1)+1); */
\r
367 index = vo_L_add(index, (quant_4p_4N(posB, n_1) << (Word16) (2 * n_1 + 1)));
\r
368 index = vo_L_add(index, quant_2p_2N1(posA[0], posA[1], n_1)); /* index += quant_2p_2N1(posA[0], posA[1], n_1); */
\r
371 index = (quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << (Word16) (3 * n_1 + 1));
\r
372 /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((3*n_1)+1); */
\r
373 index =vo_L_add(index, quant_3p_3N1(posB[0], posB[1], posB[2], n_1));
\r
374 /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1); */
\r
378 index = (quant_4p_4N(posA, n_1) << (Word16) (2 * n_1 + 1)); /* index = quant_4p_4N(posA, n_1) << ((2*n_1)+1); */
\r
379 index = vo_L_add(index, quant_2p_2N1(posB[0], posB[1], n_1)); /* index += quant_2p_2N1(posB[0], posB[1], n_1); */
\r
383 index = (quant_5p_5N(posA, n_1) << N); /* index = quant_5p_5N(posA, n_1) << N; */
\r
384 index = vo_L_add(index, quant_1p_N1(posB[0], n_1)); /* index += quant_1p_N1(posB[0], n_1); */
\r
388 index = (quant_5p_5N(posA, n_1) << N); /* index = quant_5p_5N(posA, n_1) << N; */
\r
389 index = vo_L_add(index, quant_1p_N1(posA[5], n_1)); /* index += quant_1p_N1(posA[5], n_1); */
\r
393 fprintf(stderr, "Error in function quant_6p_6N_2\n");
\r
395 index = vo_L_add(index, ((L_deposit_l(i) & 3L) << (Word16) (6 * N - 4))); /* index += (i & 3) << ((6*N)-4); */
\r