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