1 /*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17 /***********************************************************************
18 * File: q_pulse.c *
19 * *
20 * Description: Coding and decoding of algebraic codebook *
21 * *
22 ************************************************************************/
23
24 #include <stdio.h>
25 #include "typedef.h"
26 #include "basic_op.h"
27 #include "q_pulse.h"
28
29 #define NB_POS 16 /* pos in track, mask for sign bit */
30
quant_1p_N1(Word16 pos,Word16 N)31 Word32 quant_1p_N1( /* (o) return N+1 bits */
32 Word16 pos, /* (i) position of the pulse */
33 Word16 N) /* (i) number of bits for position */
34 {
35 Word16 mask;
36 Word32 index;
37
38 mask = (1 << N) - 1; /* mask = ((1<<N)-1); */
39 /*-------------------------------------------------------*
40 * Quantization of 1 pulse with N+1 bits: *
41 *-------------------------------------------------------*/
42 index = L_deposit_l((Word16) (pos & mask));
43 if ((pos & NB_POS) != 0)
44 {
45 index = vo_L_add(index, L_deposit_l(1 << N)); /* index += 1 << N; */
46 }
47 return (index);
48 }
49
50
quant_2p_2N1(Word16 pos1,Word16 pos2,Word16 N)51 Word32 quant_2p_2N1( /* (o) return (2*N)+1 bits */
52 Word16 pos1, /* (i) position of the pulse 1 */
53 Word16 pos2, /* (i) position of the pulse 2 */
54 Word16 N) /* (i) number of bits for position */
55 {
56 Word16 mask, tmp;
57 Word32 index;
58 mask = (1 << N) - 1; /* mask = ((1<<N)-1); */
59 /*-------------------------------------------------------*
60 * Quantization of 2 pulses with 2*N+1 bits: *
61 *-------------------------------------------------------*/
62 if (((pos2 ^ pos1) & NB_POS) == 0)
63 {
64 /* sign of 1st pulse == sign of 2th pulse */
65 if(pos1 <= pos2) /* ((pos1 - pos2) <= 0) */
66 {
67 /* index = ((pos1 & mask) << N) + (pos2 & mask); */
68 index = L_deposit_l(add1((((Word16) (pos1 & mask)) << N), ((Word16) (pos2 & mask))));
69 } else
70 {
71 /* ((pos2 & mask) << N) + (pos1 & mask); */
72 index = L_deposit_l(add1((((Word16) (pos2 & mask)) << N), ((Word16) (pos1 & mask))));
73 }
74 if ((pos1 & NB_POS) != 0)
75 {
76 tmp = (N << 1);
77 index = vo_L_add(index, (1L << tmp)); /* index += 1 << (2*N); */
78 }
79 } else
80 {
81 /* sign of 1st pulse != sign of 2th pulse */
82 if (vo_sub((Word16) (pos1 & mask), (Word16) (pos2 & mask)) <= 0)
83 {
84 /* index = ((pos2 & mask) << N) + (pos1 & mask); */
85 index = L_deposit_l(add1((((Word16) (pos2 & mask)) << N), ((Word16) (pos1 & mask))));
86 if ((pos2 & NB_POS) != 0)
87 {
88 tmp = (N << 1); /* index += 1 << (2*N); */
89 index = vo_L_add(index, (1L << tmp));
90 }
91 } else
92 {
93 /* index = ((pos1 & mask) << N) + (pos2 & mask); */
94 index = L_deposit_l(add1((((Word16) (pos1 & mask)) << N), ((Word16) (pos2 & mask))));
95 if ((pos1 & NB_POS) != 0)
96 {
97 tmp = (N << 1);
98 index = vo_L_add(index, (1 << tmp)); /* index += 1 << (2*N); */
99 }
100 }
101 }
102 return (index);
103 }
104
105
quant_3p_3N1(Word16 pos1,Word16 pos2,Word16 pos3,Word16 N)106 Word32 quant_3p_3N1( /* (o) return (3*N)+1 bits */
107 Word16 pos1, /* (i) position of the pulse 1 */
108 Word16 pos2, /* (i) position of the pulse 2 */
109 Word16 pos3, /* (i) position of the pulse 3 */
110 Word16 N) /* (i) number of bits for position */
111 {
112 Word16 nb_pos;
113 Word32 index;
114
115 nb_pos =(1 <<(N - 1)); /* nb_pos = (1<<(N-1)); */
116 /*-------------------------------------------------------*
117 * Quantization of 3 pulses with 3*N+1 bits: *
118 *-------------------------------------------------------*/
119 if (((pos1 ^ pos2) & nb_pos) == 0)
120 {
121 index = quant_2p_2N1(pos1, pos2, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos2, (N-1)); */
122 /* index += (pos1 & nb_pos) << N; */
123 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
124 /* index += quant_1p_N1(pos3, N) << (2*N); */
125 index = vo_L_add(index, (quant_1p_N1(pos3, N)<<(N << 1)));
126
127 } else if (((pos1 ^ pos3) & nb_pos) == 0)
128 {
129 index = quant_2p_2N1(pos1, pos3, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos3, (N-1)); */
130 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
131 /* index += (pos1 & nb_pos) << N; */
132 index = vo_L_add(index, (quant_1p_N1(pos2, N) << (N << 1)));
133 /* index += quant_1p_N1(pos2, N) <<
134 * (2*N); */
135 } else
136 {
137 index = quant_2p_2N1(pos2, pos3, (N - 1)); /* index = quant_2p_2N1(pos2, pos3, (N-1)); */
138 /* index += (pos2 & nb_pos) << N; */
139 index = vo_L_add(index, (L_deposit_l((Word16) (pos2 & nb_pos)) << N));
140 /* index += quant_1p_N1(pos1, N) << (2*N); */
141 index = vo_L_add(index, (quant_1p_N1(pos1, N) << (N << 1)));
142 }
143 return (index);
144 }
145
146
quant_4p_4N1(Word16 pos1,Word16 pos2,Word16 pos3,Word16 pos4,Word16 N)147 Word32 quant_4p_4N1( /* (o) return (4*N)+1 bits */
148 Word16 pos1, /* (i) position of the pulse 1 */
149 Word16 pos2, /* (i) position of the pulse 2 */
150 Word16 pos3, /* (i) position of the pulse 3 */
151 Word16 pos4, /* (i) position of the pulse 4 */
152 Word16 N) /* (i) number of bits for position */
153 {
154 Word16 nb_pos;
155 Word32 index;
156
157 nb_pos = 1 << (N - 1); /* nb_pos = (1<<(N-1)); */
158 /*-------------------------------------------------------*
159 * Quantization of 4 pulses with 4*N+1 bits: *
160 *-------------------------------------------------------*/
161 if (((pos1 ^ pos2) & nb_pos) == 0)
162 {
163 index = quant_2p_2N1(pos1, pos2, sub(N, 1)); /* index = quant_2p_2N1(pos1, pos2, (N-1)); */
164 /* index += (pos1 & nb_pos) << N; */
165 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
166 /* index += quant_2p_2N1(pos3, pos4, N) << (2*N); */
167 index = vo_L_add(index, (quant_2p_2N1(pos3, pos4, N) << (N << 1)));
168 } else if (((pos1 ^ pos3) & nb_pos) == 0)
169 {
170 index = quant_2p_2N1(pos1, pos3, (N - 1));
171 /* index += (pos1 & nb_pos) << N; */
172 index = vo_L_add(index, (L_deposit_l((Word16) (pos1 & nb_pos)) << N));
173 /* index += quant_2p_2N1(pos2, pos4, N) << (2*N); */
174 index = vo_L_add(index, (quant_2p_2N1(pos2, pos4, N) << (N << 1)));
175 } else
176 {
177 index = quant_2p_2N1(pos2, pos3, (N - 1));
178 /* index += (pos2 & nb_pos) << N; */
179 index = vo_L_add(index, (L_deposit_l((Word16) (pos2 & nb_pos)) << N));
180 /* index += quant_2p_2N1(pos1, pos4, N) << (2*N); */
181 index = vo_L_add(index, (quant_2p_2N1(pos1, pos4, N) << (N << 1)));
182 }
183 return (index);
184 }
185
186
quant_4p_4N(Word16 pos[],Word16 N)187 Word32 quant_4p_4N( /* (o) return 4*N bits */
188 Word16 pos[], /* (i) position of the pulse 1..4 */
189 Word16 N) /* (i) number of bits for position */
190 {
191 Word16 nb_pos, mask __unused, n_1, tmp;
192 Word16 posA[4], posB[4];
193 Word32 i, j, k, index;
194
195 n_1 = (Word16) (N - 1);
196 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
197 mask = vo_sub((1 << N), 1); /* mask = ((1<<N)-1); */
198
199 i = 0;
200 j = 0;
201 for (k = 0; k < 4; k++)
202 {
203 if ((pos[k] & nb_pos) == 0)
204 {
205 posA[i++] = pos[k];
206 } else
207 {
208 posB[j++] = pos[k];
209 }
210 }
211
212 switch (i)
213 {
214 case 0:
215 tmp = vo_sub((N << 2), 3); /* index = 1 << ((4*N)-3); */
216 index = (1L << tmp);
217 /* index += quant_4p_4N1(posB[0], posB[1], posB[2], posB[3], n_1); */
218 index = vo_L_add(index, quant_4p_4N1(posB[0], posB[1], posB[2], posB[3], n_1));
219 break;
220 case 1:
221 /* index = quant_1p_N1(posA[0], n_1) << ((3*n_1)+1); */
222 tmp = add1((Word16)((vo_L_mult(3, n_1) >> 1)), 1);
223 index = L_shl(quant_1p_N1(posA[0], n_1), tmp);
224 /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1); */
225 index = vo_L_add(index, quant_3p_3N1(posB[0], posB[1], posB[2], n_1));
226 break;
227 case 2:
228 tmp = ((n_1 << 1) + 1); /* index = quant_2p_2N1(posA[0], posA[1], n_1) << ((2*n_1)+1); */
229 index = L_shl(quant_2p_2N1(posA[0], posA[1], n_1), tmp);
230 /* index += quant_2p_2N1(posB[0], posB[1], n_1); */
231 index = vo_L_add(index, quant_2p_2N1(posB[0], posB[1], n_1));
232 break;
233 case 3:
234 /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << N; */
235 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), N);
236 index = vo_L_add(index, quant_1p_N1(posB[0], n_1)); /* index += quant_1p_N1(posB[0], n_1); */
237 break;
238 case 4:
239 index = quant_4p_4N1(posA[0], posA[1], posA[2], posA[3], n_1);
240 break;
241 default:
242 index = 0;
243 fprintf(stderr, "Error in function quant_4p_4N\n");
244 }
245 tmp = ((N << 2) - 2); /* index += (i & 3) << ((4*N)-2); */
246 index = vo_L_add(index, L_shl((L_deposit_l(i) & (3L)), tmp));
247
248 return (index);
249 }
250
251
252
quant_5p_5N(Word16 pos[],Word16 N)253 Word32 quant_5p_5N( /* (o) return 5*N bits */
254 Word16 pos[], /* (i) position of the pulse 1..5 */
255 Word16 N) /* (i) number of bits for position */
256 {
257 Word16 nb_pos, n_1, tmp;
258 Word16 posA[5], posB[5];
259 Word32 i, j, k, index, tmp2;
260
261 n_1 = (Word16) (N - 1);
262 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
263
264 i = 0;
265 j = 0;
266 for (k = 0; k < 5; k++)
267 {
268 if ((pos[k] & nb_pos) == 0)
269 {
270 posA[i++] = pos[k];
271 } else
272 {
273 posB[j++] = pos[k];
274 }
275 }
276
277 switch (i)
278 {
279 case 0:
280 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* ((5*N)-1)) */
281 index = L_shl(1L, tmp); /* index = 1 << ((5*N)-1); */
282 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) << ((2*N)+1);*/
283 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
284 index = vo_L_add(index, tmp2);
285 index = vo_L_add(index, quant_2p_2N1(posB[3], posB[4], N)); /* index += quant_2p_2N1(posB[3], posB[4], N); */
286 break;
287 case 1:
288 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* index = 1 << ((5*N)-1); */
289 index = L_shl(1L, tmp);
290 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) <<((2*N)+1); */
291 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
292 index = vo_L_add(index, tmp2);
293 index = vo_L_add(index, quant_2p_2N1(posB[3], posA[0], N)); /* index += quant_2p_2N1(posB[3], posA[0], N); */
294 break;
295 case 2:
296 tmp = vo_sub((Word16)((vo_L_mult(5, N) >> 1)), 1); /* ((5*N)-1)) */
297 index = L_shl(1L, tmp); /* index = 1 << ((5*N)-1); */
298 tmp = add1((N << 1), 1); /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1) << ((2*N)+1); */
299 tmp2 = L_shl(quant_3p_3N1(posB[0], posB[1], posB[2], n_1), tmp);
300 index = vo_L_add(index, tmp2);
301 index = vo_L_add(index, quant_2p_2N1(posA[0], posA[1], N)); /* index += quant_2p_2N1(posA[0], posA[1], N); */
302 break;
303 case 3:
304 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
305 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
306 index = vo_L_add(index, quant_2p_2N1(posB[0], posB[1], N)); /* index += quant_2p_2N1(posB[0], posB[1], N); */
307 break;
308 case 4:
309 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
310 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
311 index = vo_L_add(index, quant_2p_2N1(posA[3], posB[0], N)); /* index += quant_2p_2N1(posA[3], posB[0], N); */
312 break;
313 case 5:
314 tmp = add1((N << 1), 1); /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((2*N)+1); */
315 index = L_shl(quant_3p_3N1(posA[0], posA[1], posA[2], n_1), tmp);
316 index = vo_L_add(index, quant_2p_2N1(posA[3], posA[4], N)); /* index += quant_2p_2N1(posA[3], posA[4], N); */
317 break;
318 default:
319 index = 0;
320 fprintf(stderr, "Error in function quant_5p_5N\n");
321 }
322
323 return (index);
324 }
325
326
quant_6p_6N_2(Word16 pos[],Word16 N)327 Word32 quant_6p_6N_2( /* (o) return (6*N)-2 bits */
328 Word16 pos[], /* (i) position of the pulse 1..6 */
329 Word16 N) /* (i) number of bits for position */
330 {
331 Word16 nb_pos, n_1;
332 Word16 posA[6], posB[6];
333 Word32 i, j, k, index;
334
335 /* !! N and n_1 are constants -> it doesn't need to be operated by Basic Operators */
336 n_1 = (Word16) (N - 1);
337 nb_pos = (1 << n_1); /* nb_pos = (1<<n_1); */
338
339 i = 0;
340 j = 0;
341 for (k = 0; k < 6; k++)
342 {
343 if ((pos[k] & nb_pos) == 0)
344 {
345 posA[i++] = pos[k];
346 } else
347 {
348 posB[j++] = pos[k];
349 }
350 }
351
352 switch (i)
353 {
354 case 0:
355 index = (1 << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
356 index = vo_L_add(index, (quant_5p_5N(posB, n_1) << N)); /* index += quant_5p_5N(posB, n_1) << N; */
357 index = vo_L_add(index, quant_1p_N1(posB[5], n_1)); /* index += quant_1p_N1(posB[5], n_1); */
358 break;
359 case 1:
360 index = (1L << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
361 index = vo_L_add(index, (quant_5p_5N(posB, n_1) << N)); /* index += quant_5p_5N(posB, n_1) << N; */
362 index = vo_L_add(index, quant_1p_N1(posA[0], n_1)); /* index += quant_1p_N1(posA[0], n_1); */
363 break;
364 case 2:
365 index = (1L << (Word16) (6 * N - 5)); /* index = 1 << ((6*N)-5); */
366 /* index += quant_4p_4N(posB, n_1) << ((2*n_1)+1); */
367 index = vo_L_add(index, (quant_4p_4N(posB, n_1) << (Word16) (2 * n_1 + 1)));
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); */
369 break;
370 case 3:
371 index = (quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << (Word16) (3 * n_1 + 1));
372 /* index = quant_3p_3N1(posA[0], posA[1], posA[2], n_1) << ((3*n_1)+1); */
373 index =vo_L_add(index, quant_3p_3N1(posB[0], posB[1], posB[2], n_1));
374 /* index += quant_3p_3N1(posB[0], posB[1], posB[2], n_1); */
375 break;
376 case 4:
377 i = 2;
378 index = (quant_4p_4N(posA, n_1) << (Word16) (2 * n_1 + 1)); /* index = quant_4p_4N(posA, n_1) << ((2*n_1)+1); */
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); */
380 break;
381 case 5:
382 i = 1;
383 index = (quant_5p_5N(posA, n_1) << N); /* index = quant_5p_5N(posA, n_1) << N; */
384 index = vo_L_add(index, quant_1p_N1(posB[0], n_1)); /* index += quant_1p_N1(posB[0], n_1); */
385 break;
386 case 6:
387 i = 0;
388 index = (quant_5p_5N(posA, n_1) << N); /* index = quant_5p_5N(posA, n_1) << N; */
389 index = vo_L_add(index, quant_1p_N1(posA[5], n_1)); /* index += quant_1p_N1(posA[5], n_1); */
390 break;
391 default:
392 index = 0;
393 fprintf(stderr, "Error in function quant_6p_6N_2\n");
394 }
395 index = vo_L_add(index, ((L_deposit_l(i) & 3L) << (Word16) (6 * N - 4))); /* index += (i & 3) << ((6*N)-4); */
396
397 return (index);
398 }
399
400
401